@@ -2,12 +2,10 @@ package p2p
2
2
3
3
import (
4
4
"crypto/rand"
5
- "strings"
6
5
"testing"
7
6
8
7
"github.com/libp2p/go-libp2p-core/crypto"
9
8
"github.com/libp2p/go-libp2p-core/peer"
10
- "github.com/multiformats/go-multiaddr"
11
9
"github.com/stretchr/testify/assert"
12
10
"github.com/stretchr/testify/require"
13
11
@@ -20,14 +18,17 @@ type TestLogger struct {
20
18
}
21
19
22
20
func (t * TestLogger ) Debug (msg string , keyvals ... interface {}) {
21
+ t .t .Helper ()
23
22
t .t .Log (append ([]interface {}{"DEBUG: " + msg }, keyvals ... )... )
24
23
}
25
24
26
25
func (t * TestLogger ) Info (msg string , keyvals ... interface {}) {
26
+ t .t .Helper ()
27
27
t .t .Log (append ([]interface {}{"INFO: " + msg }, keyvals ... )... )
28
28
}
29
29
30
30
func (t * TestLogger ) Error (msg string , keyvals ... interface {}) {
31
+ t .t .Helper ()
31
32
t .t .Log (append ([]interface {}{"ERROR: " + msg }, keyvals ... )... )
32
33
}
33
34
@@ -40,6 +41,8 @@ func TestClientStartup(t *testing.T) {
40
41
41
42
err = client .Start ()
42
43
assert .NoError (err )
44
+
45
+ client .host .Close ()
43
46
}
44
47
45
48
func TestBootstrapping (t * testing.T ) {
@@ -60,21 +63,21 @@ func TestBootstrapping(t *testing.T) {
60
63
require .NotEmpty (cid2 )
61
64
62
65
// client1 has no seeds
63
- client1 , err := NewClient (config.P2PConfig {ListenAddress : "127.0.0.1: 7676" }, privKey1 , logger )
66
+ client1 , err := NewClient (config.P2PConfig {ListenAddress : "/ip4/ 127.0.0.1/tcp/ 7676" }, privKey1 , logger )
64
67
require .NoError (err )
65
68
require .NotNil (client1 )
66
69
67
70
// client2 will use client1 as predefined seed
68
71
client2 , err := NewClient (config.P2PConfig {
69
- ListenAddress : "127.0.0.1: 7677" ,
70
- Seeds : cid1 . Pretty () + "@ 127.0.0.1: 7676" ,
72
+ ListenAddress : "/ip4/ 127.0.0.1/tcp/ 7677" ,
73
+ Seeds : "/ip4/ 127.0.0.1/tcp/ 7676/p2p/" + cid1 . Pretty () ,
71
74
}, privKey2 , logger )
72
75
require .NoError (err )
73
76
74
77
// client3 will use clien1 and client2 as seeds
75
78
client3 , err := NewClient (config.P2PConfig {
76
- ListenAddress : "127.0.0.1: 7678" ,
77
- Seeds : cid1 . Pretty () + "@ 127.0.0.1: 7676" + "," + cid2 .Pretty () + "@ 127.0.0.1: 7677" ,
79
+ ListenAddress : "/ip4/ 127.0.0.1/tcp/ 7678" ,
80
+ Seeds : "/ip4/ 127.0.0.1/tcp/ 7676/p2p/ " + cid1 .Pretty () + ",/ip4/ 127.0.0.1/tcp/ 7677/p2p/" + cid2 . Pretty () ,
78
81
}, privKey3 , logger )
79
82
require .NoError (err )
80
83
@@ -91,48 +94,3 @@ func TestBootstrapping(t *testing.T) {
91
94
assert .Equal (2 , len (client2 .host .Network ().Peers ()))
92
95
assert .Equal (2 , len (client3 .host .Network ().Peers ()))
93
96
}
94
-
95
- func TestGetMultiaddr (t * testing.T ) {
96
- t .Parallel ()
97
-
98
- valid := mustGetMultiaddr (t , "/ip4/127.0.0.1/tcp/1234" )
99
- withId := mustGetMultiaddr (t , "/ip4/127.0.0.1/tcp/1234/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7" )
100
-
101
- cases := []struct {
102
- name string
103
- input string
104
- expected multiaddr.Multiaddr
105
- expectedErr string
106
- }{
107
- {"empty" , "" , nil , ErrInvalidAddress .Error ()},
108
- {"no port" , "127.0.0.1:" , nil , "failed to parse multiaddr" },
109
- {"ip only" , "127.0.0.1" , nil , ErrInvalidAddress .Error ()},
110
- {"with invalid id" , "deadbeef@127.0.0.1:1234" , nil , "failed to parse multiaddr" },
111
- {"valid" , "127.0.0.1:1234" , valid , "" },
112
- {"valid with id" , "k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7@127.0.0.1:1234" , withId , "" },
113
- }
114
-
115
- for _ , c := range cases {
116
- t .Run (c .name , func (t * testing.T ) {
117
- assert := assert .New (t )
118
- actual , err := GetMultiAddr (c .input )
119
- if c .expectedErr != "" {
120
- assert .Error (err )
121
- assert .Nil (actual )
122
- assert .True (strings .HasPrefix (err .Error (), c .expectedErr ), "invalid error message" )
123
- } else {
124
- assert .NoError (err )
125
- assert .Equal (c .expected , actual )
126
- }
127
- })
128
- }
129
- }
130
-
131
- func mustGetMultiaddr (t * testing.T , addr string ) multiaddr.Multiaddr {
132
- t .Helper ()
133
- maddr , err := multiaddr .NewMultiaddr (addr )
134
- if err != nil {
135
- t .Fatal (err )
136
- }
137
- return maddr
138
- }
0 commit comments