Skip to content

Commit 1a9a8de

Browse files
authored
test(p2p/protocol/identify): fix user agent assertion in Go 1.24 (#3177)
1 parent 080e6c8 commit 1a9a8de

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

p2p/protocol/identify/id.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/libp2p/go-libp2p/core/protocol"
2020
"github.com/libp2p/go-libp2p/core/record"
2121
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
22+
useragent "github.com/libp2p/go-libp2p/p2p/protocol/identify/internal/user-agent"
2223
"github.com/libp2p/go-libp2p/p2p/protocol/identify/pb"
2324

2425
logging "github.com/ipfs/go-log/v2"
@@ -54,8 +55,6 @@ const (
5455
connectedPeerMaxAddrs = 500
5556
)
5657

57-
var defaultUserAgent = "github.com/libp2p/go-libp2p"
58-
5958
type identifySnapshot struct {
6059
seq uint64
6160
protocols []protocol.ID
@@ -188,7 +187,7 @@ func NewIDService(h host.Host, opts ...Option) (*idService, error) {
188187
opt(&cfg)
189188
}
190189

191-
userAgent := defaultUserAgent
190+
userAgent := useragent.DefaultUserAgent()
192191
if cfg.userAgent != "" {
193192
userAgent = cfg.userAgent
194193
}

p2p/protocol/identify/id_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/libp2p/go-libp2p/p2p/net/swarm"
2828
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
2929
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
30+
useragent "github.com/libp2p/go-libp2p/p2p/protocol/identify/internal/user-agent"
3031
"github.com/libp2p/go-libp2p/p2p/protocol/identify/pb"
3132

3233
mockClock "github.com/benbjohnson/clock"
@@ -44,9 +45,8 @@ func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiadd
4445

4546
func testHasAgentVersion(t *testing.T, h host.Host, p peer.ID) {
4647
v, err := h.Peerstore().Get(p, "AgentVersion")
47-
if v.(string) != "github.com/libp2p/go-libp2p" { // this is the default user agent
48-
t.Error("agent version mismatch", err)
49-
}
48+
require.NoError(t, err, "fetching agent version")
49+
require.Equal(t, useragent.DefaultUserAgent(), v, "agent version")
5050
}
5151

5252
func testHasPublicKey(t *testing.T, h host.Host, p peer.ID, shouldBe ic.PubKey) {
@@ -104,7 +104,7 @@ func emitAddrChangeEvt(t *testing.T, h host.Host) {
104104
}
105105
}
106106

107-
// TestIDServiceWait gives the ID service 1s to finish after dialing
107+
// TestIDService gives the ID service 1s to finish after dialing
108108
// this is because it used to be concurrent. Now, Dial wait till the
109109
// id service is done.
110110
func TestIDService(t *testing.T) {

p2p/protocol/identify/user_agent.go renamed to p2p/protocol/identify/internal/user-agent/user_agent.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
package identify
1+
package useragent
22

33
import (
44
"fmt"
55
"runtime/debug"
66
)
77

8+
func DefaultUserAgent() string {
9+
return defaultUserAgent
10+
}
11+
12+
var defaultUserAgent = "github.com/libp2p/go-libp2p"
13+
814
func init() {
915
bi, ok := debug.ReadBuildInfo()
1016
if !ok {

0 commit comments

Comments
 (0)