Skip to content

Commit

Permalink
p2p/protocol/identify: test user agent in tests against the real user…
Browse files Browse the repository at this point in the history
… agent used

Fixes #3176
  • Loading branch information
Jorropo committed Feb 6, 2025
1 parent 6a96622 commit ff5b0bb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 2 additions & 3 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/record"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
useragent "github.com/libp2p/go-libp2p/p2p/protocol/identify/internal/user-agent"
"github.com/libp2p/go-libp2p/p2p/protocol/identify/pb"

logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -54,8 +55,6 @@ const (
connectedPeerMaxAddrs = 500
)

var defaultUserAgent = "github.com/libp2p/go-libp2p"

type identifySnapshot struct {
seq uint64
protocols []protocol.ID
Expand Down Expand Up @@ -188,7 +187,7 @@ func NewIDService(h host.Host, opts ...Option) (*idService, error) {
opt(&cfg)
}

userAgent := defaultUserAgent
userAgent := useragent.DefaultUserAgent()
if cfg.userAgent != "" {
userAgent = cfg.userAgent
}
Expand Down
3 changes: 2 additions & 1 deletion p2p/protocol/identify/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/net/swarm"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
useragent "github.com/libp2p/go-libp2p/p2p/protocol/identify/internal/user-agent"
"github.com/libp2p/go-libp2p/p2p/protocol/identify/pb"

mockClock "github.com/benbjohnson/clock"
Expand All @@ -45,7 +46,7 @@ func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiadd
func testHasAgentVersion(t *testing.T, h host.Host, p peer.ID) {
v, err := h.Peerstore().Get(p, "AgentVersion")
require.NoError(t, err, "fetching agent version")
require.Equal(t, "github.com/libp2p/go-libp2p", v, "agent version")
require.Equal(t, useragent.DefaultUserAgent(), v, "agent version")
}

func testHasPublicKey(t *testing.T, h host.Host, p peer.ID, shouldBe ic.PubKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package identify
package useragent

import (
"fmt"
"runtime/debug"
)

func DefaultUserAgent() string {
return defaultUserAgent
}

var defaultUserAgent = "github.com/libp2p/go-libp2p"

func init() {
bi, ok := debug.ReadBuildInfo()
if !ok {
Expand Down

0 comments on commit ff5b0bb

Please sign in to comment.