From d27ab3ee4d96a10a8580d39fe9fcb9be5dc5e6b2 Mon Sep 17 00:00:00 2001 From: Ocenka Date: Tue, 5 Aug 2025 15:38:08 +0100 Subject: [PATCH] Update peer_test.go --- p2p/peer_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/p2p/peer_test.go b/p2p/peer_test.go index dea72875fe..468b06557f 100644 --- a/p2p/peer_test.go +++ b/p2p/peer_test.go @@ -266,6 +266,23 @@ func TestNewPeer(t *testing.T) { p.Disconnect(DiscAlreadyConnected) // Should not hang } +// TestCapsReturnsCopy verifies that Caps() returns a copy of the internal slice. +func TestCapsReturnsCopy(t *testing.T) { + id := randomID() + name := "nodename" + caps := []Cap{{"a", 1}, {"b", 2}} + p := NewPeer(id, name, caps) + + returned := p.Caps() + // Modify the returned slice. + returned[0] = Cap{"modified", 99} + + // Ensure the internal slice was not affected. + if reflect.DeepEqual(returned, p.Caps()) { + t.Fatal("Caps() should return a copy, not a reference to the internal slice") + } +} + func TestMatchProtocols(t *testing.T) { tests := []struct { Remote []Cap