Files
nilsandClaude Sonnet 5.5 6d55222ac5 abuse reports: signed reports, gossip and sync, a local hide rule, visitor tips
A report is signed by a node operator (device key and certificate) and
gossiped on one global topic. Each node decides whose reports count: the
operator's own report hides a listing on that node, one trusted report of
illegal content hides it, and --hide-after trusted scam reports hide it
(--trust-reporter names others). Hidden listings stay stored and reachable.
Visitors can tip the node's operator off with a proof-of-work request that
hides nothing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
2026-11-18 21:18:43 +01:00

308 lines
8.8 KiB
Go

package store
import (
"errors"
"fmt"
"testing"
"time"
"git.stinnesbeck.com/nils/allmende/internal/identity"
"git.stinnesbeck.com/nils/allmende/internal/listing"
"git.stinnesbeck.com/nils/allmende/internal/report"
)
// reporter is somebody who can sign reports.
type reporter struct {
root *identity.Identity
dev *identity.Device
}
func newReporter(t *testing.T) *reporter {
t.Helper()
root, _, _ := identity.NewRoot()
return &reporter{root: root, dev: newDevice(t, root)}
}
func (r *reporter) key() string { return r.dev.Cert.Author }
// reportEnv is an env whose store trusts the given reporters; self is the
// node's own operator.
func reportEnv(t *testing.T, self *reporter, trusted ...*reporter) *env {
t.Helper()
e := &env{now: t0}
e.root, _, _ = identity.NewRoot()
e.dev = newDevice(t, e.root)
tr := Trust{}
if self != nil {
tr.Self = self.key()
}
for _, r := range trusted {
tr.Reporters = append(tr.Reporters, r.key())
}
s, err := Open(":memory:", Options{Difficulty: difficulty, Clock: func() time.Time { return e.now }, Trust: tr})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { s.Close() })
e.s = s
return e
}
func (e *env) report(t *testing.T, by *reporter, l *listing.Listing, category string, at time.Time) *report.Report {
t.Helper()
r := &report.Report{
Listing: report.Ref{Author: l.Author, ID: l.ID},
Category: category,
CreatedAt: at.Unix(),
ExpiresAt: at.Add(7 * 24 * time.Hour).Unix(),
}
if err := r.Seal(by.dev, difficulty); err != nil {
t.Fatal(err)
}
return r
}
func (e *env) putReport(t *testing.T, r *report.Report) Outcome {
t.Helper()
o, err := e.s.PutReport(ctx, r)
if err != nil {
t.Fatal(err)
}
return o
}
func (e *env) visible(t *testing.T) int {
t.Helper()
ls, err := e.s.Search(ctx, Query{})
if err != nil {
t.Fatal(err)
}
return len(ls)
}
func (e *env) everything(t *testing.T) int {
t.Helper()
ls, err := e.s.Search(ctx, Query{IncludeHidden: true})
if err != nil {
t.Fatal(err)
}
return len(ls)
}
func TestTheOperatorsOwnReportHidesAtOnce(t *testing.T) {
me := newReporter(t)
e := reportEnv(t, me)
l := e.listing(t, nil)
e.put(t, l)
e.put(t, e.listing(t, nil))
e.putReport(t, e.report(t, me, l, report.Scam, e.now))
if e.visible(t) != 1 || e.everything(t) != 2 {
t.Fatalf("visible %d, all %d: want 1 and 2 (hidden, not deleted)", e.visible(t), e.everything(t))
}
if _, err := e.s.Get(ctx, l.Author, l.ID); err != nil {
t.Fatalf("a hidden listing must stay reachable by its link: %v", err)
}
sum, _ := e.s.Summary(ctx, l.Author, l.ID)
if !sum.Hidden || sum.Own != report.Scam || sum.Trusted[report.Scam] != 1 {
t.Fatalf("summary: %+v", sum)
}
}
func TestSeveralTrustedReportersHideAScam(t *testing.T) {
a, b, stranger := newReporter(t), newReporter(t), newReporter(t)
e := reportEnv(t, nil, a, b)
l := e.listing(t, nil)
e.put(t, l)
e.putReport(t, e.report(t, a, l, report.Scam, e.now))
if e.visible(t) != 1 {
t.Fatal("one trusted scam report is not enough to hide")
}
e.putReport(t, e.report(t, stranger, l, report.Scam, e.now))
e.putReport(t, e.report(t, stranger, l, report.Illegal, e.now.Add(time.Second)))
if e.visible(t) != 1 {
t.Fatal("reports from strangers must never hide anything")
}
e.putReport(t, e.report(t, b, l, report.Scam, e.now))
if e.visible(t) != 0 {
t.Fatal("two trusted reporters did not hide the listing")
}
sum, _ := e.s.Summary(ctx, l.Author, l.ID)
if sum.Trusted[report.Scam] != 2 || sum.Others != 1 {
t.Fatalf("summary: %+v", sum)
}
}
func TestOneTrustedReportOfIllegalContentHides(t *testing.T) {
a := newReporter(t)
e := reportEnv(t, nil, a)
l := e.listing(t, nil)
e.put(t, l)
e.putReport(t, e.report(t, a, l, report.Illegal, e.now))
if e.visible(t) != 0 {
t.Fatal("a trusted report of illegal content must hide at once")
}
}
func TestLabelsNeverHide(t *testing.T) {
a, b := newReporter(t), newReporter(t)
e := reportEnv(t, a, b)
l := e.listing(t, nil)
e.put(t, l)
e.putReport(t, e.report(t, a, l, report.Category, e.now))
e.putReport(t, e.report(t, b, l, report.Other, e.now))
if e.visible(t) != 1 {
t.Fatal("category and other reports must only label a listing")
}
sum, _ := e.s.Summary(ctx, l.Author, l.ID)
if sum.Hidden || sum.Trusted[report.Category] != 1 || sum.Trusted[report.Other] != 1 {
t.Fatalf("summary: %+v", sum)
}
}
func TestAReporterCanTakeAReportBackAndTheNewerOneWins(t *testing.T) {
me := newReporter(t)
e := reportEnv(t, me)
l := e.listing(t, nil)
e.put(t, l)
old := e.report(t, me, l, report.Scam, e.now)
e.putReport(t, old)
if e.visible(t) != 0 {
t.Fatal("not hidden")
}
withdrawn := e.report(t, me, l, report.Withdrawn, e.now.Add(time.Minute))
if o := e.putReport(t, withdrawn); o != Replaced {
t.Fatalf("withdrawing: %v", o)
}
if e.visible(t) != 1 {
t.Fatal("a withdrawn report still hides the listing")
}
// The old one arriving late (sync order is arbitrary) changes nothing.
if o := e.putReport(t, old); o != Ignored {
t.Fatalf("an older report replaced a newer one: %v", o)
}
if e.visible(t) != 1 {
t.Fatal("the listing was hidden again by an old report")
}
}
func TestReportsExpireAndHidingLapsesWithThem(t *testing.T) {
me := newReporter(t)
e := reportEnv(t, me)
l := e.listing(t, func(l *listing.Listing) { l.ExpiresAt = e.now.Add(30 * 24 * time.Hour).Unix() })
e.put(t, l)
e.putReport(t, e.report(t, me, l, report.Scam, e.now))
if e.visible(t) != 0 {
t.Fatal("not hidden")
}
e.now = e.now.Add(8 * 24 * time.Hour)
if e.visible(t) != 1 {
t.Fatal("an expired report still hides the listing")
}
if _, err := e.s.Expire(ctx); err != nil {
t.Fatal(err)
}
if reps, _ := e.s.Reports(ctx, 10); len(reps) != 0 {
t.Fatal("an expired report was kept")
}
}
func TestARevokedReportersReportsGoAway(t *testing.T) {
a := newReporter(t)
e := reportEnv(t, nil, a)
l := e.listing(t, nil)
e.put(t, l)
e.putReport(t, e.report(t, a, l, report.Illegal, e.now))
if e.visible(t) != 0 {
t.Fatal("not hidden")
}
rev, err := identity.Revoke(a.root, a.dev.Cert, e.now, difficulty)
if err != nil {
t.Fatal(err)
}
if _, err := e.s.PutRevocation(ctx, rev); err != nil {
t.Fatal(err)
}
if e.visible(t) != 1 {
t.Fatal("the reports of a revoked device still count")
}
if _, err := e.s.PutReport(ctx, e.report(t, a, l, report.Illegal, e.now.Add(time.Minute))); !errors.Is(err, ErrRevoked) {
t.Fatalf("a revoked device's new report: %v", err)
}
}
func TestInvalidReportsAreRefused(t *testing.T) {
a := newReporter(t)
e := reportEnv(t, nil, a)
l := e.listing(t, nil)
r := e.report(t, a, l, report.Scam, e.now)
r.Note = "changed after signing"
if _, err := e.s.PutReport(ctx, r); !errors.Is(err, ErrInvalid) {
t.Fatalf("a tampered report: %v", err)
}
}
func TestReportDigestsMatchWhenTheSetsDo(t *testing.T) {
a, b := newReporter(t), newReporter(t)
x, y := reportEnv(t, nil, a, b), reportEnv(t, nil, a, b)
l := x.listing(t, nil)
r1, r2 := x.report(t, a, l, report.Scam, x.now), x.report(t, b, l, report.Other, x.now)
x.putReport(t, r1)
x.putReport(t, r2)
y.putReport(t, r2)
y.putReport(t, r1)
dx, _ := x.s.ReportDigest(ctx)
dy, _ := y.s.ReportDigest(ctx)
if dx != dy || dx.Count != 2 {
t.Fatalf("digests differ: %+v %+v", dx, dy)
}
y.putReport(t, y.report(t, a, l, report.Withdrawn, y.now.Add(time.Minute)))
if dy2, _ := y.s.ReportDigest(ctx); dy2 == dx {
t.Fatal("a newer report did not change the digest")
}
}
func TestVisitorsTipsAreBoundedAndNeverHide(t *testing.T) {
e := reportEnv(t, nil)
l := e.listing(t, nil)
e.put(t, l)
for i := 0; i < maxSignalsPerList+10; i++ {
err := e.s.AddSignal(ctx, Signal{Hash: fmt.Sprint("h", i), Author: l.Author, ListingID: l.ID, Category: report.Scam, Note: "Betrug", Work: 10 + i%5})
if err != nil {
t.Fatal(err)
}
}
tips, _ := e.s.Signals(ctx)
if len(tips) != maxSignalsPerList {
t.Fatalf("%d tips kept, want %d", len(tips), maxSignalsPerList)
}
if tips[0].Work < tips[len(tips)-1].Work {
t.Fatal("tips are not listed strongest first")
}
if e.visible(t) != 1 {
t.Fatal("visitor tips must never hide a listing")
}
sum, _ := e.s.Summary(ctx, l.Author, l.ID)
if sum.Signals != maxSignalsPerList || sum.Hidden {
t.Fatalf("summary: %+v", sum)
}
if err := e.s.AddSignal(ctx, Signal{Hash: "x", Author: l.Author, ListingID: l.ID, Category: "spam"}); !errors.Is(err, ErrInvalid) {
t.Fatalf("an unknown category: %v", err)
}
// Sending the same request twice adds nothing.
// (h5x tips were kept: the strongest have the highest i%5, so h59 is among them.)
before, _ := e.s.Signals(ctx)
if err := e.s.AddSignal(ctx, Signal{Hash: "h59", Author: l.Author, ListingID: l.ID, Category: report.Scam, Work: 99}); err != nil {
t.Fatal(err)
}
after, _ := e.s.Signals(ctx)
if len(after) != len(before) || after[0].Work == 99 {
t.Fatalf("a repeated tip was added: %d -> %d, strongest %d", len(before), len(after), after[0].Work)
}
if n, _ := e.s.DeleteSignals(ctx, l.Author, l.ID); n != int64(maxSignalsPerList) {
t.Fatalf("dismissed %d", n)
}
}