package report import ( "errors" "strings" "testing" "time" "git.stinnesbeck.com/nils/allmende/internal/identity" "git.stinnesbeck.com/nils/allmende/internal/pow" ) const difficulty = 6 var t0 = time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC) func device(t *testing.T) *identity.Device { t.Helper() root, _, _ := identity.NewRoot() dev, err := identity.Enroll(root, t0.Add(-time.Hour), 90*24*time.Hour) if err != nil { t.Fatal(err) } return dev } func sealed(t *testing.T, dev *identity.Device, mutate func(*Report)) *Report { t.Helper() r := &Report{ Listing: Ref{Author: strings.Repeat("ab", 32), ID: strings.Repeat("cd", 16)}, Category: Scam, Note: "Verlangt Vorkasse", CreatedAt: t0.Unix(), ExpiresAt: t0.Add(7 * 24 * time.Hour).Unix(), } if mutate != nil { mutate(r) } if err := r.Seal(dev, difficulty); err != nil { t.Fatal(err) } return r } // The fast miner must keep finding what the old loop found. func TestSealFindsTheSmallestNonceThatTryingEachOneInOrderWouldFind(t *testing.T) { r := sealed(t, device(t), nil) probe := *r probe.Sig = "" for n := uint64(0); ; n++ { probe.PoW = n b, err := probe.unsignedBytes() if err != nil { t.Fatal(err) } if pow.LeadingZeroBits(b) >= difficulty { if n != r.PoW { t.Fatalf("Seal chose nonce %d, the smallest that works is %d", r.PoW, n) } return } } } func TestASealedReportChecks(t *testing.T) { r := sealed(t, device(t), nil) if err := r.Check(t0, difficulty); err != nil { t.Fatal(err) } if err := r.Check(t0, difficulty+8); err == nil { t.Fatal("a report with too little proof-of-work was accepted") } } func TestTamperedReportsAreRefused(t *testing.T) { dev := device(t) cases := map[string]func(*Report){ "another listing": func(r *Report) { r.Listing.ID = strings.Repeat("00", 16) }, "another category": func(r *Report) { r.Category = Illegal }, "another note": func(r *Report) { r.Note = "anders" }, "longer lifetime": func(r *Report) { r.ExpiresAt += 3600 }, "another reporter": func(r *Report) { r.Reporter = strings.Repeat("11", 32) }, } for name, tamper := range cases { r := sealed(t, dev, nil) tamper(r) if err := r.Check(t0, difficulty); err == nil { t.Errorf("%s: accepted", name) } } } func TestTheCertificateMustBelongToTheReporter(t *testing.T) { a, b := device(t), device(t) r := sealed(t, a, nil) r.Cert = b.Cert // somebody else's certificate if err := r.Check(t0, difficulty); err == nil { t.Fatal("a report carrying another reporter's certificate was accepted") } } func TestFieldRules(t *testing.T) { dev := device(t) for name, mutate := range map[string]func(*Report){ "unknown category": func(r *Report) { r.Category = "spam" }, "bad author": func(r *Report) { r.Listing.Author = "xyz" }, "note too long": func(r *Report) { r.Note = strings.Repeat("a", MaxNote+1) }, "control character": func(r *Report) { r.Note = "a\x00b" }, "lifetime over 30 days": func(r *Report) { r.ExpiresAt = r.CreatedAt + int64(31*24*time.Hour/time.Second) }, "expires before start": func(r *Report) { r.ExpiresAt = r.CreatedAt }, } { r := &Report{ Listing: Ref{Author: strings.Repeat("ab", 32), ID: strings.Repeat("cd", 16)}, Category: Scam, CreatedAt: t0.Unix(), ExpiresAt: t0.Add(24 * time.Hour).Unix(), } mutate(r) if err := r.Seal(dev, difficulty); err != nil { t.Fatal(err) } if err := r.Check(t0, difficulty); err == nil { t.Errorf("%s: accepted", name) } } } func TestClockProblemsAreMarkedAsSuch(t *testing.T) { dev := device(t) r := sealed(t, dev, nil) if err := r.Check(t0.Add(8*24*time.Hour), difficulty); !errors.Is(err, ErrTimeWindow) { t.Fatalf("expired: %v", err) } if err := r.Check(t0.Add(-time.Hour), difficulty); !errors.Is(err, ErrTimeWindow) { t.Fatalf("from the future: %v", err) } } func TestTheNewerReportWinsAndTiesConverge(t *testing.T) { dev := device(t) old := sealed(t, dev, func(r *Report) { r.CreatedAt = t0.Unix() - 100 }) newer := sealed(t, dev, func(r *Report) { r.Category = Withdrawn }) if !newer.Supersedes(old) || old.Supersedes(newer) { t.Fatal("the newer report must win") } a := sealed(t, dev, func(r *Report) { r.Note = "a" }) b := sealed(t, dev, func(r *Report) { r.Note = "b" }) if a.Supersedes(b) == b.Supersedes(a) { t.Fatal("on a tie exactly one of two reports must win, on every node") } }