package p2p import ( "context" "encoding/json" "testing" "time" pubsub "github.com/libp2p/go-libp2p-pubsub" pb "github.com/libp2p/go-libp2p-pubsub/pb" "git.stinnesbeck.com/nils/allmende/internal/identity" "git.stinnesbeck.com/nils/allmende/internal/report" ) // report makes a signed report by this node's operator about a listing. func (tn *tnode) report(t *testing.T, author, id, category string) *report.Report { t.Helper() r := &report.Report{ Listing: report.Ref{Author: author, ID: id}, Category: category, Note: "Verlangt Vorkasse", CreatedAt: time.Now().Unix(), ExpiresAt: time.Now().Add(7 * 24 * time.Hour).Unix(), } if err := r.Seal(tn.dev, difficulty); err != nil { t.Fatal(err) } return r } func reportCount(tn *tnode) int { rs, _ := tn.s.Reports(context.Background(), 100) return len(rs) } func TestReportsAreGossipedToEveryone(t *testing.T) { // Not even in the same region: reports travel on a topic of their own. a := startNode(t, nil, "u1x") b := startNode(t, nil, "u30") connect(t, a, b) eventually(t, "the report topic has a peer", func() bool { return len(a.ps.ListPeers(ReportTopic)) == 1 }) l := a.listing(t, "u1xjhx") if err := a.PublishReport(context.Background(), a.report(t, l.Author, l.ID, report.Scam)); err != nil { t.Fatal(err) } eventually(t, "b holds the report", func() bool { return reportCount(b) == 1 }) } func TestALateJoinerGetsTheReportsBySync(t *testing.T) { a := startNode(t, nil, "u1x") l := a.listing(t, "u1xjhx") if _, err := a.s.PutReport(context.Background(), a.report(t, l.Author, l.ID, report.Illegal)); err != nil { t.Fatal(err) } b := startNode(t, nil, "u1x") // joins after the report was made, never hears the gossip connect(t, b, a) eventually(t, "b caught up with the reports", func() bool { return reportCount(b) == 1 }) } // What a relay does with a report it is handed, checked on the validator itself // (as the listing validator is): it must be the exact thing the reporter signed, // from a device that is still valid. func TestTheReportValidatorJudgesWhatArrives(t *testing.T) { a := startNode(t, nil, "u1x") l := a.listing(t, "u1xjhx") rep := a.report(t, l.Author, l.ID, report.Scam) data, _ := rep.Bytes() validate := func(data []byte) pubsub.ValidationResult { return a.reportValidator(context.Background(), "", &pubsub.Message{Message: &pb.Message{Data: data}}) } if r := validate(data); r != pubsub.ValidationAccept { t.Fatalf("a good report: %v", r) } if r := validate([]byte(`{"v":1`)); r != pubsub.ValidationReject { t.Fatalf("garbage: %v", r) } tampered := *rep tampered.Category = report.Illegal // the signature no longer fits tb, _ := tampered.Bytes() if r := validate(tb); r != pubsub.ValidationReject { t.Fatalf("a tampered report: %v", r) } var m map[string]any _ = json.Unmarshal(data, &m) pretty, _ := json.MarshalIndent(m, "", " ") if r := validate(pretty); r != pubsub.ValidationReject { t.Fatalf("a report that is not in canonical form: %v", r) } // Clocks differ: ignored, and the relay is not penalised for it. a.clock = func() time.Time { return time.Now().Add(31 * 24 * time.Hour) } if r := validate(data); r != pubsub.ValidationIgnore { t.Fatalf("an expired report: %v", r) } a.clock = time.Now // The reporter's device was revoked: ignored, so it is not passed on. rev, err := identity.Revoke(a.root, a.dev.Cert, time.Now(), difficulty) if err != nil { t.Fatal(err) } if _, err := a.s.PutRevocation(context.Background(), rev); err != nil { t.Fatal(err) } if r := validate(data); r != pubsub.ValidationIgnore { t.Fatalf("a report from a revoked device: %v", r) } }