package p2p import ( "context" "io" "log/slog" "testing" "time" pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/libp2p/go-libp2p/core/peer" ) type guardFixture struct { g *peerGuard now time.Time verdict pubsub.ValidationResult ran int dropped []peer.ID v pubsub.ValidatorEx } func newGuardFixture(t *testing.T) *guardFixture { t.Helper() f := &guardFixture{now: time.Unix(1_800_000_000, 0), verdict: pubsub.ValidationAccept} g, err := newPeerGuard("self", func() time.Time { return f.now }, slog.New(slog.NewTextHandler(io.Discard, nil)), func(p peer.ID) { f.dropped = append(f.dropped, p) }) if err != nil { t.Fatal(err) } f.g = g f.v = g.wrap(func(context.Context, peer.ID, *pubsub.Message) pubsub.ValidationResult { f.ran++ return f.verdict }) return f } func (f *guardFixture) send(from peer.ID) pubsub.ValidationResult { return f.v(context.Background(), from, &pubsub.Message{}) } func TestAPeerThatSendsInvalidMessagesIsBannedAndDisconnected(t *testing.T) { f := newGuardFixture(t) f.verdict = pubsub.ValidationReject for i := 0; i < maxBadPerWindow-1; i++ { f.send("mallory") } if f.g.banned("mallory") || len(f.dropped) != 0 { t.Fatal("banned before the limit") } f.send("mallory") if !f.g.banned("mallory") { t.Fatalf("not banned after %d invalid messages", maxBadPerWindow) } if len(f.dropped) != 1 || f.dropped[0] != "mallory" { t.Fatalf("disconnected %v, want mallory once", f.dropped) } f.send("mallory") // more invalid ones must not disconnect again and again if len(f.dropped) != 1 { t.Fatalf("disconnected %d times", len(f.dropped)) } if f.g.banned("alice") { t.Fatal("an innocent peer is banned") } } func TestInvalidMessagesSpreadOverTimeDoNotBan(t *testing.T) { f := newGuardFixture(t) f.verdict = pubsub.ValidationReject for i := 0; i < 5*maxBadPerWindow; i++ { f.send("slow") f.now = f.now.Add(guardWindow / (maxBadPerWindow / 2)) // half the limit per window } if f.g.banned("slow") { t.Fatal("banned for a rate below the limit") } } func TestIgnoredMessagesAreNotHeldAgainstThePeer(t *testing.T) { f := newGuardFixture(t) f.verdict = pubsub.ValidationIgnore // clock trouble, unknown listing for i := 0; i < 10*maxBadPerWindow; i++ { f.send("honest") } if f.g.banned("honest") { t.Fatal("ignored messages must not count as bad") } } func TestAFloodIsDroppedUnreadAndTheNextMinuteStartsFresh(t *testing.T) { f := newGuardFixture(t) for i := 0; i < maxPerWindow; i++ { if res := f.send("busy"); res != pubsub.ValidationAccept { t.Fatalf("message %d within the limit got %v", i, res) } } ran := f.ran if res := f.send("busy"); res != pubsub.ValidationIgnore { t.Fatalf("over the limit got %v, want Ignore", res) } if f.ran != ran { t.Fatal("a message over the limit was still validated") } if f.g.banned("busy") { t.Fatal("a flood of valid messages is dropped, not banned") } if res := f.send("calm"); res != pubsub.ValidationAccept { t.Fatal("one peer's flood must not limit another") } f.now = f.now.Add(guardWindow) if res := f.send("busy"); res != pubsub.ValidationAccept { t.Fatalf("after the window the peer got %v", res) } } func TestOurOwnMessagesAreNeverLimited(t *testing.T) { f := newGuardFixture(t) f.verdict = pubsub.ValidationReject for i := 0; i < 3*maxPerWindow; i++ { if f.send("self") == pubsub.ValidationIgnore { t.Fatal("limited our own publish") } } if f.g.banned("self") { t.Fatal("banned ourselves") } } func TestFinishedWindowsAreForgotten(t *testing.T) { f := newGuardFixture(t) for i := 0; i < 5000; i++ { f.send(peer.ID("p" + string(rune('a'+i%26)) + time.Duration(i).String())) } f.now = f.now.Add(2 * guardWindow) f.send("fresh") if n := len(f.g.peers); n > 10 { t.Fatalf("%d peers still tracked after their windows ended", n) } }