Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Command Center (MCC)8 * File : services/internal/agent/nonce/nonce_test.go9 * Module : services10 * Repository : https://git.spboucher.ai/maclustr-command-center11 * Project Website : https://clustr.spboucher.ai12 * Created : 2026-11-1813 * Last Modified : 2026-11-1814 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package nonce4344import (45 "fmt"46 "os"47 "path/filepath"48 "sync"49 "testing"50 "time"51)5253func TestRememberReplayRestart(t *testing.T) {54 path := filepath.Join(t.TempDir(), "nonces.log")55 s, err := Open(path)56 if err != nil {57 t.Fatal(err)58 }59 until := time.Now().Add(2 * time.Minute)60 if fresh, err := s.Remember("n1", until); err != nil || !fresh {61 t.Fatal("first use must be fresh")62 }63 if fresh, _ := s.Remember("n1", until); fresh {64 t.Fatal("replay accepted")65 }66 for _, bad := range []string{"", "a b", "a\nb"} {67 if _, err := s.Remember(bad, until); err == nil {68 t.Errorf("invalid nonce %q accepted", bad)69 }70 }71 s.Close()72 // A torn last line, as after a crash during append.73 f, _ := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0o600)74 f.WriteString("torn-nonce-without-expi")75 f.Close()76 s, err = Open(path)77 if err != nil {78 t.Fatal(err)79 }80 if fresh, _ := s.Remember("n1", until); fresh {81 t.Fatal("replay accepted after restart")82 }83 if fresh, _ := s.Remember("n2", until); !fresh {84 t.Fatal("new nonce refused after restart")85 }86 if st, _ := os.Stat(path); st.Mode().Perm() != 0o600 {87 t.Errorf("mode %o", st.Mode().Perm())88 }89}9091func TestExpiryAndCompaction(t *testing.T) {92 path := filepath.Join(t.TempDir(), "nonces.log")93 s, _ := Open(path)94 now := time.Now()95 s.now = func() time.Time { return now }96 s.Remember("short", now.Add(time.Second))97 for i := 0; i < 600; i++ { // crosses the compaction threshold twice98 if fresh, err := s.Remember(fmt.Sprintf("n%d", i), now.Add(time.Hour)); err != nil || !fresh {99 t.Fatal(err)100 }101 }102 now = now.Add(10 * time.Second)103 s.Close()104 s2, _ := Open(path)105 s2.now = func() time.Time { return now }106 if s2.Len() != 600 {107 t.Fatalf("expired entry kept or live ones lost: %d", s2.Len())108 }109 if fresh, _ := s2.Remember("n42", now.Add(time.Hour)); fresh {110 t.Fatal("live nonce forgotten by compaction")111 }112}113114func TestConcurrentSingleWinner(t *testing.T) {115 s, _ := Open(filepath.Join(t.TempDir(), "n"))116 var wg sync.WaitGroup117 var mu sync.Mutex118 wins := 0119 for i := 0; i < 50; i++ {120 wg.Add(1)121 go func() {122 defer wg.Done()123 if fresh, err := s.Remember("same", time.Now().Add(time.Minute)); err == nil && fresh {124 mu.Lock()125 wins++126 mu.Unlock()127 }128 }()129 }130 wg.Wait()131 if wins != 1 {132 t.Fatalf("%d winners", wins)133 }134}135