Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ func (p *Peer) Fullname() string {

// Caps returns the capabilities (supported subprotocols) of the remote peer.
func (p *Peer) Caps() []Cap {
// TODO: maybe return copy
return p.rw.caps
capsCopy := make([]Cap, len(p.rw.caps))
copy(capsCopy, p.rw.caps)
return capsCopy
}

// RunningCap returns true if the peer is actively connected using any of the
Expand Down
17 changes: 17 additions & 0 deletions p2p/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading