multiaddr_test.go 31.9 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package multiaddr

import (
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
4 5 6
	"bytes"
	"encoding/hex"
	"testing"
7

8 9
	"github.com/ipfs/go-cid"
	mh "github.com/multiformats/go-multihash"
web3-bot's avatar
web3-bot committed
10
	"github.com/stretchr/testify/require"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
func newMultiaddr(t *testing.T, a string) Multiaddr {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16 17 18 19 20
	m, err := NewMultiaddr(a)
	if err != nil {
		t.Error(err)
	}
	return m
}

21 22 23 24 25
func TestConstructFails(t *testing.T) {
	cases := []string{
		"/ip4",
		"/ip4/::1",
		"/ip4/fdpsofodsajfdoisa",
Marco Munizaga's avatar
Marco Munizaga committed
26 27
		"/ip4/::/ipcidr/256",
		"/ip6/::/ipcidr/1026",
28
		"/ip6",
mwnx's avatar
mwnx committed
29 30 31
		"/ip6zone",
		"/ip6zone/",
		"/ip6zone//ip6/fe80::1",
32 33 34 35 36
		"/udp",
		"/tcp",
		"/sctp",
		"/udp/65536",
		"/tcp/65536",
Marten Seemann's avatar
Marten Seemann committed
37
		"/quic/65536",
Marco Munizaga's avatar
Marco Munizaga committed
38
		"/quic-v1/65536",
39 40
		"/onion/9imaq4ygg2iegci7:80",
		"/onion/aaimaq4ygg2iegci7:80",
41 42
		"/onion/timaq4ygg2iegci7:0",
		"/onion/timaq4ygg2iegci7:-1",
43
		"/onion/timaq4ygg2iegci7",
44
		"/onion/timaq4ygg2iegci@:666",
idk's avatar
idk committed
45 46 47 48 49 50
		"/onion3/9ww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:80",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd7:80",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:0",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:-1",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy@:666",
51 52 53 54 55 56 57 58 59 60
		"/garlic64/jT~IyXaoauTni6N4517EG8mrFUKpy0IlgZh-EY9csMAk82Odatmzr~YTZy8Hv7u~wvkg75EFNOyqb~nAPg-khyp2TS~ObUz8WlqYAM2VlEzJ7wJB91P-cUlKF18zSzVoJFmsrcQHZCirSbWoOknS6iNmsGRh5KVZsBEfp1Dg3gwTipTRIx7Vl5Vy~1OSKQVjYiGZS9q8RL0MF~7xFiKxZDLbPxk0AK9TzGGqm~wMTI2HS0Gm4Ycy8LYPVmLvGonIBYndg2bJC7WLuF6tVjVquiokSVDKFwq70BCUU5AU-EvdOD5KEOAM7mPfw-gJUG4tm1TtvcobrObqoRnmhXPTBTN5H7qDD12AvlwFGnfAlBXjuP4xOUAISL5SRLiulrsMSiT4GcugSI80mF6sdB0zWRgL1yyvoVWeTBn1TqjO27alr95DGTluuSqrNAxgpQzCKEWAyzrQkBfo2avGAmmz2NaHaAvYbOg0QSJz1PLjv2jdPW~ofiQmrGWM1cd~1cCqAAAA7:80",
		"/garlic64/jT~IyXaoauTni6N4517EG8mrFUKpy0IlgZh-EY9csMAk82Odatmzr~YTZy8Hv7u~wvkg75EFNOyqb~nAPg-khyp2TS~ObUz8WlqYAM2VlEzJ7wJB91P-cUlKF18zSzVoJFmsrcQHZCirSbWoOknS6iNmsGRh5KVZsBEfp1Dg3gwTipTRIx7Vl5Vy~1OSKQVjYiGZS9q8RL0MF~7xFiKxZDLbPxk0AK9TzGGqm~wMTI2HS0Gm4Ycy8LYPVmLvGonIBYndg2bJC7WLuF6tVjVquiokS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H7qDD12AvlwFGnfAlBXjuP4xOUAISL5SRLiulrsMSiT4GcugSI80mF6sdB0zWRgL1yyvoVWeTBn1TqjO27alr95DGTluuSqrNAxgpQzCKEWAyzrQkBfo2avGAmmz2NaHaAvYbOg0QSJz1PLjv2jdPW~ofiQmrGWM1cd~1cCqAAAA@:666",
idk's avatar
idk committed
61 62 63 64 65
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu",
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu77",
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu:80",
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:-1",
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu@",
66 67 68 69 70 71 72
		"/udp/1234/sctp",
		"/udp/1234/udt/1234",
		"/udp/1234/utp/1234",
		"/ip4/127.0.0.1/udp/jfodsajfidosajfoidsa",
		"/ip4/127.0.0.1/udp",
		"/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa",
		"/ip4/127.0.0.1/tcp",
Marten Seemann's avatar
Marten Seemann committed
73
		"/ip4/127.0.0.1/quic/1234",
Marco Munizaga's avatar
Marco Munizaga committed
74 75 76
		"/ip4/127.0.0.1/quic-v1/1234",
		"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash",
		"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmp", // 1 character missing from certhash
77 78
		"/ip4/127.0.0.1/ipfs",
		"/ip4/127.0.0.1/ipfs/tcp",
Steven Allen's avatar
Steven Allen committed
79 80
		"/ip4/127.0.0.1/p2p",
		"/ip4/127.0.0.1/p2p/tcp",
jbenet's avatar
jbenet committed
81 82
		"/unix",
		"/ip4/1.2.3.4/tcp/80/unix",
backkem's avatar
backkem committed
83
		"/ip4/127.0.0.1/tcp/9090/http/p2p-webcrt-direct",
Steven Allen's avatar
Steven Allen committed
84 85
		"/",
		"",
86 87 88 89
	}

	for _, a := range cases {
		if _, err := NewMultiaddr(a); err == nil {
90
			t.Errorf("should have failed: %s - %s", a, err)
91 92 93 94
		}
	}
}

Steven Allen's avatar
Steven Allen committed
95 96 97 98 99 100 101
func TestEmptyMultiaddr(t *testing.T) {
	_, err := NewMultiaddrBytes([]byte{})
	if err == nil {
		t.Fatal("should have failed to parse empty multiaddr")
	}
}

102 103 104 105
func TestConstructSucceeds(t *testing.T) {
	cases := []string{
		"/ip4/1.2.3.4",
		"/ip4/0.0.0.0",
Marco Munizaga's avatar
Marco Munizaga committed
106
		"/ip4/192.0.2.0/ipcidr/24",
107 108
		"/ip6/::1",
		"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21",
Marten Seemann's avatar
Marten Seemann committed
109
		"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21/udp/1234/quic",
Marco Munizaga's avatar
Marco Munizaga committed
110
		"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21/udp/1234/quic-v1",
Marco Munizaga's avatar
Marco Munizaga committed
111
		"/ip6/2001:db8::/ipcidr/32",
mwnx's avatar
mwnx committed
112 113 114 115
		"/ip6zone/x/ip6/fe80::1",
		"/ip6zone/x%y/ip6/fe80::1",
		"/ip6zone/x%y/ip6/::",
		"/ip6zone/x/ip6/fe80::1/udp/1234/quic",
Marco Munizaga's avatar
Marco Munizaga committed
116
		"/ip6zone/x/ip6/fe80::1/udp/1234/quic-v1",
117 118
		"/onion/timaq4ygg2iegci7:1234",
		"/onion/timaq4ygg2iegci7:80/http",
idk's avatar
idk committed
119 120
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:80/http",
121 122 123 124
		"/garlic64/jT~IyXaoauTni6N4517EG8mrFUKpy0IlgZh-EY9csMAk82Odatmzr~YTZy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b~nAPg-khyp2TS~ObUz8WlqYAM2VlEzJ7wJB91P-cUlKF18zSzVoJFmsrcQHZCirSbWoOknS6iNmsGRh5KVZsBEfp1Dg3gwTipTRIx7Vl5Vy~1OSKQVjYiGZS9q8RL0MF~7xFiKxZDLbPxk0AK9TzGGqm~wMTI2HS0Gm4Ycy8LYPVmLvGonIBYndg2bJC7WLuF6tVjVquiokSVDKFwq70BCUU5AU-EvdOD5KEOAM7mPfw-gJUG4tm1TtvcobrObqoRnmhXPTBTN5H7qDD12AvlwFGnfAlBXjuP4xOUAISL5SRLiulrsMSiT4GcugSI80mF6sdB0zWRgL1yyvoVWeTBn1TqjO27alr95DGTluuSqrNAxgpQzCKEWAyzrQkBfo2avGAmmz2NaHaAvYbOg0QSJz1PLjv2jdPW~ofiQmrGWM1cd~1cCqAAAA/udp/8080",
		"/garlic64/jT~IyXaoauTni6N4517EG8mrFUKpy0IlgZh-EY9csMAk82Odatmzr~YTZy8Hv7u~wvkg75EFNOyqb~nAPg-khyp2TS~ObUz8WlqYAM2VlEzJ7wJB91P-cUlKF18zSzVoJFmsrcQHZCirSbWoOknS6iNmsGRh5KVZsBEfp1Dg3gwTipTRIx7Vl5Vy~1OSKQVjYiGZS9q8RL0MF~7xFiKxZDLbPxk0AK9TzGGqm~wMTI2HS0Gm4Ycy8LYPVmLvGonIBYndg2bJC7WLuF6tVjVquiokSVDKFwq70BCUU5AU-EvdOD5KEOAM7mPfw-gJUG4tm1TtvcobrObqoRnmhXPTBTN5H7qDD12AvlwFGnfAlBXjuP4xOUAISL5SRLiulrsMSiT4GcugSI80mF6sdB0zWRgL1yyvoVWeTBn1TqjO27alr95DGTluuSqrNAxgpQzCKEWAyzrQkBfo2avGAmmz2NaHaAvYbOg0QSJz1PLjv2jdPW~ofiQmrGWM1cd~1cCqAAAA/tcp/8080",
idk's avatar
idk committed
125 126
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq",
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuqzwas",
127
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuqzwassw",
idk's avatar
idk committed
128 129 130
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq/http",
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq/tcp/8080",
		"/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq/udp/8080",
131 132 133 134 135 136 137 138
		"/udp/0",
		"/tcp/0",
		"/sctp/0",
		"/udp/1234",
		"/tcp/1234",
		"/sctp/1234",
		"/udp/65535",
		"/tcp/65535",
139
		"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
140
		"/ipfs/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7",
Steven Allen's avatar
Steven Allen committed
141
		"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
142 143 144 145 146
		"/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7",
		"/p2p/bafzbeigvf25ytwc3akrijfecaotc74udrhcxzh2cx3we5qqnw5vgrei4bm",
		"/p2p/12D3KooWCryG7Mon9orvQxcS1rYZjotPgpwoJNHHKcLLfE4Hf5mV",
		"/p2p/k51qzi5uqu5dhb6l8spkdx7yxafegfkee5by8h7lmjh2ehc2sgg34z7c15vzqs",
		"/p2p/bafzaajaiaejcalj543iwv2d7pkjt7ykvefrkfu7qjfi6sduakhso4lay6abn2d5u",
147 148 149
		"/udp/1234/sctp/1234",
		"/udp/1234/udt",
		"/udp/1234/utp",
150
		"/tcp/1234/http",
Steven Allen's avatar
Steven Allen committed
151
		"/tcp/1234/tls/http",
152 153
		"/tcp/1234/https",
		"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
154
		"/ipfs/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7/tcp/1234",
Steven Allen's avatar
Steven Allen committed
155
		"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
156
		"/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7/tcp/1234",
157 158 159 160
		"/ip4/127.0.0.1/udp/1234",
		"/ip4/127.0.0.1/udp/0",
		"/ip4/127.0.0.1/tcp/1234",
		"/ip4/127.0.0.1/tcp/1234/",
Marten Seemann's avatar
Marten Seemann committed
161
		"/ip4/127.0.0.1/udp/1234/quic",
Marco Munizaga's avatar
Marco Munizaga committed
162 163 164 165
		"/ip4/127.0.0.1/udp/1234/quic-v1",
		"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport",
		"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy",
		"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy/certhash/zQmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41",
166 167
		"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
		"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
168 169
		"/ip4/127.0.0.1/ipfs/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7",
		"/ip4/127.0.0.1/ipfs/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7/tcp/1234",
Steven Allen's avatar
Steven Allen committed
170 171
		"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
		"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
172 173
		"/ip4/127.0.0.1/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7",
		"/ip4/127.0.0.1/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7/tcp/1234",
jbenet's avatar
jbenet committed
174 175 176 177
		"/unix/a/b/c/d/e",
		"/unix/stdio",
		"/ip4/1.2.3.4/tcp/80/unix/a/b/c/d/e/f",
		"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio",
178
		"/ip4/127.0.0.1/ipfs/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7/tcp/1234/unix/stdio",
Steven Allen's avatar
Steven Allen committed
179
		"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio",
180
		"/ip4/127.0.0.1/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7/tcp/1234/unix/stdio",
backkem's avatar
backkem committed
181
		"/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct",
Steven Allen's avatar
Steven Allen committed
182 183
		"/ip4/127.0.0.1/tcp/127/ws",
		"/ip4/127.0.0.1/tcp/127/ws",
Marten Seemann's avatar
Marten Seemann committed
184
		"/ip4/127.0.0.1/tcp/127/tls",
Steven Allen's avatar
Steven Allen committed
185
		"/ip4/127.0.0.1/tcp/127/tls/ws",
Marten Seemann's avatar
Marten Seemann committed
186
		"/ip4/127.0.0.1/tcp/127/noise",
Steven Allen's avatar
Steven Allen committed
187 188
		"/ip4/127.0.0.1/tcp/127/wss",
		"/ip4/127.0.0.1/tcp/127/wss",
189 190
		"/ip4/127.0.0.1/tcp/127/webrtc-direct",
		"/ip4/127.0.0.1/tcp/127/webrtc",
191 192 193 194
	}

	for _, a := range cases {
		if _, err := NewMultiaddr(a); err != nil {
195
			t.Errorf("should have succeeded: %s -- %s", a, err)
196 197 198 199
		}
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
200 201 202 203
func TestEqual(t *testing.T) {
	m1 := newMultiaddr(t, "/ip4/127.0.0.1/udp/1234")
	m2 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")
	m3 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
204
	m4 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

	if m1.Equal(m2) {
		t.Error("should not be equal")
	}

	if m2.Equal(m1) {
		t.Error("should not be equal")
	}

	if !m2.Equal(m3) {
		t.Error("should be equal")
	}

	if !m3.Equal(m2) {
		t.Error("should be equal")
	}

	if !m1.Equal(m1) {
		t.Error("should be equal")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225 226 227 228 229 230 231 232

	if !m2.Equal(m4) {
		t.Error("should be equal")
	}

	if !m4.Equal(m3) {
		t.Error("should be equal")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
233 234
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
235 236
func TestStringToBytes(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
237 238 239 240 241
	testString := func(s string, h string) {
		b1, err := hex.DecodeString(h)
		if err != nil {
			t.Error("failed to decode hex", h)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
242

243
		// t.Log("196", h, []byte(b1))
idk's avatar
idk committed
244

245
		b2, err := stringToBytes(s)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
246
		if err != nil {
247
			t.Error("failed to convert", s, err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
248
		}
249

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
250
		if !bytes.Equal(b1, b2) {
idk's avatar
idk committed
251
			t.Error("failed to convert \n", s, "to\n", hex.EncodeToString(b1), "got\n", hex.EncodeToString(b2))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
252
		}
Jeromy's avatar
Jeromy committed
253 254

		if err := validateBytes(b2); err != nil {
idk's avatar
idk committed
255
			t.Error(err, "len:", len(b2))
Jeromy's avatar
Jeromy committed
256
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
257
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
258

259
	testString("/ip4/127.0.0.1/udp/1234", "047f000001910204d2")
260
	testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
261
	testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f000001910204d2047f0000010610e1")
idk's avatar
idk committed
262 263
	testString("/onion/aaimaq4ygg2iegci:80", "bc030010c0439831b48218480050")
	testString("/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234", "bd03adadec040be047f9658668b11a504f3155001f231a37f54c4476c07fb4cc139ed7e30304d2")
264
	testString("/garlic64/jT~IyXaoauTni6N4517EG8mrFUKpy0IlgZh-EY9csMAk82Odatmzr~YTZy8Hv7u~wvkg75EFNOyqb~nAPg-khyp2TS~ObUz8WlqYAM2VlEzJ7wJB91P-cUlKF18zSzVoJFmsrcQHZCirSbWoOknS6iNmsGRh5KVZsBEfp1Dg3gwTipTRIx7Vl5Vy~1OSKQVjYiGZS9q8RL0MF~7xFiKxZDLbPxk0AK9TzGGqm~wMTI2HS0Gm4Ycy8LYPVmLvGonIBYndg2bJC7WLuF6tVjVquiokSVDKFwq70BCUU5AU-EvdOD5KEOAM7mPfw-gJUG4tm1TtvcobrObqoRnmhXPTBTN5H7qDD12AvlwFGnfAlBXjuP4xOUAISL5SRLiulrsMSiT4GcugSI80mF6sdB0zWRgL1yyvoVWeTBn1TqjO27alr95DGTluuSqrNAxgpQzCKEWAyzrQkBfo2avGAmmz2NaHaAvYbOg0QSJz1PLjv2jdPW~ofiQmrGWM1cd~1cCqAAAA",
265
		"be0383038d3fc8c976a86ae4e78ba378e75ec41bc9ab1542a9cb422581987e118f5cb0c024f3639d6ad9b3aff613672f07bfbbbfc2f920ef910534ecaa6ff9c03e0fa4872a764d2fce6d4cfc5a5a9800cd95944cc9ef0241f753fe71494a175f334b35682459acadc4076428ab49b5a83a49d2ea2366b06461e4a559b0111fa750e0de0c138a94d1231ed5979572ff53922905636221994bdabc44bd0c17fef11622b16432db3f193400af53cc61aa9bfc0c4c8d874b41a6e18732f0b60f5662ef1a89c80589dd8366c90bb58bb85ead56356aba2a244950ca170abbd01094539014f84bdd383e4a10e00cee63dfc3e809506e2d9b54edbdca1bace6eaa119e68573d30533791fba830f5d80be5c051a77c09415e3b8fe3139400848be5244b8ae96bb0c4a24f819cba0488f34985eac741d3359180bd72cafa1559e4c19f54ea8cedbb6a5afde4319396eb92aab340c60a50cc2284580cb3ad09017e8d9abc60269b3d8d687680bd86ce834412273d4f2e3bf68dd3d6fe87e2426ac658cd5c77fd5c0aa000000")
idk's avatar
idk committed
266
	testString("/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq",
267
		"bf0320efbcd45d0c5dc79781ac6f20ea5055a036afb48d45a52e7d68ec7d4338919e69")
idk's avatar
idk committed
268

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
269 270 271 272
}

func TestBytesToString(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
273
	testString := func(s1 string, h string) {
Steven Allen's avatar
Steven Allen committed
274
		t.Helper()
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
275 276 277 278
		b, err := hex.DecodeString(h)
		if err != nil {
			t.Error("failed to decode hex", h)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
279

Jeromy's avatar
Jeromy committed
280 281 282 283
		if err := validateBytes(b); err != nil {
			t.Error(err)
		}

284
		s2, err := bytesToString(b)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
285
		if err != nil {
idk's avatar
idk committed
286
			t.Log("236", s1, ":", string(h), ":", s2)
287
			t.Error("failed to convert", b, err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
288
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
289

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
290
		if s1 != s2 {
291
			t.Error("failed to convert", b, "to", s1, "got", s2)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
292 293
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
294

295
	testString("/ip4/127.0.0.1/udp/1234", "047f000001910204d2")
296
	testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
297
	testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f000001910204d2047f0000010610e1")
298
	testString("/onion/aaimaq4ygg2iegci:80", "bc030010c0439831b48218480050")
idk's avatar
idk committed
299
	testString("/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234", "bd03adadec040be047f9658668b11a504f3155001f231a37f54c4476c07fb4cc139ed7e30304d2")
300
	testString("/garlic64/jT~IyXaoauTni6N4517EG8mrFUKpy0IlgZh-EY9csMAk82Odatmzr~YTZy8Hv7u~wvkg75EFNOyqb~nAPg-khyp2TS~ObUz8WlqYAM2VlEzJ7wJB91P-cUlKF18zSzVoJFmsrcQHZCirSbWoOknS6iNmsGRh5KVZsBEfp1Dg3gwTipTRIx7Vl5Vy~1OSKQVjYiGZS9q8RL0MF~7xFiKxZDLbPxk0AK9TzGGqm~wMTI2HS0Gm4Ycy8LYPVmLvGonIBYndg2bJC7WLuF6tVjVquiokSVDKFwq70BCUU5AU-EvdOD5KEOAM7mPfw-gJUG4tm1TtvcobrObqoRnmhXPTBTN5H7qDD12AvlwFGnfAlBXjuP4xOUAISL5SRLiulrsMSiT4GcugSI80mF6sdB0zWRgL1yyvoVWeTBn1TqjO27alr95DGTluuSqrNAxgpQzCKEWAyzrQkBfo2avGAmmz2NaHaAvYbOg0QSJz1PLjv2jdPW~ofiQmrGWM1cd~1cCqAAAA",
301
		"be0383038d3fc8c976a86ae4e78ba378e75ec41bc9ab1542a9cb422581987e118f5cb0c024f3639d6ad9b3aff613672f07bfbbbfc2f920ef910534ecaa6ff9c03e0fa4872a764d2fce6d4cfc5a5a9800cd95944cc9ef0241f753fe71494a175f334b35682459acadc4076428ab49b5a83a49d2ea2366b06461e4a559b0111fa750e0de0c138a94d1231ed5979572ff53922905636221994bdabc44bd0c17fef11622b16432db3f193400af53cc61aa9bfc0c4c8d874b41a6e18732f0b60f5662ef1a89c80589dd8366c90bb58bb85ead56356aba2a244950ca170abbd01094539014f84bdd383e4a10e00cee63dfc3e809506e2d9b54edbdca1bace6eaa119e68573d30533791fba830f5d80be5c051a77c09415e3b8fe3139400848be5244b8ae96bb0c4a24f819cba0488f34985eac741d3359180bd72cafa1559e4c19f54ea8cedbb6a5afde4319396eb92aab340c60a50cc2284580cb3ad09017e8d9abc60269b3d8d687680bd86ce834412273d4f2e3bf68dd3d6fe87e2426ac658cd5c77fd5c0aa000000")
idk's avatar
idk committed
302
	testString("/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq",
303
		"bf0320efbcd45d0c5dc79781ac6f20ea5055a036afb48d45a52e7d68ec7d4338919e69")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
304 305
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
306
func TestBytesSplitAndJoin(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
307 308 309 310

	testString := func(s string, res []string) {
		m, err := NewMultiaddr(s)
		if err != nil {
311
			t.Fatal("failed to convert", s, err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
312 313
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
314
		split := Split(m)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
315 316 317 318 319 320 321 322 323 324 325
		if len(split) != len(res) {
			t.Error("not enough split components", split)
			return
		}

		for i, a := range split {
			if a.String() != res[i] {
				t.Errorf("split component failed: %s != %s", a, res[i])
			}
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
326 327 328 329 330
		joined := Join(split...)
		if !m.Equal(joined) {
			t.Errorf("joined components failed: %s != %s", m, joined)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
331 332 333 334 335 336 337 338 339 340
		for i, a := range split {
			if a.String() != res[i] {
				t.Errorf("split component failed: %s != %s", a, res[i])
			}
		}
	}

	testString("/ip4/1.2.3.4/udp/1234", []string{"/ip4/1.2.3.4", "/udp/1234"})
	testString("/ip4/1.2.3.4/tcp/1/ip4/2.3.4.5/udp/2",
		[]string{"/ip4/1.2.3.4", "/tcp/1", "/ip4/2.3.4.5", "/udp/2"})
341 342
	testString("/ip4/1.2.3.4/utp/ip4/2.3.4.5/udp/2/udt",
		[]string{"/ip4/1.2.3.4", "/utp", "/ip4/2.3.4.5", "/udp/2", "/udt"})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
343 344
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
345
func TestProtocols(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
346 347 348 349 350
	m, err := NewMultiaddr("/ip4/127.0.0.1/udp/1234")
	if err != nil {
		t.Error("failed to construct", "/ip4/127.0.0.1/udp/1234")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
351
	ps := m.Protocols()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
352
	if ps[0].Code != ProtocolWithName("ip4").Code {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
353 354 355 356
		t.Error(ps[0], ProtocolWithName("ip4"))
		t.Error("failed to get ip4 protocol")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
357
	if ps[1].Code != ProtocolWithName("udp").Code {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
358 359 360
		t.Error(ps[1], ProtocolWithName("udp"))
		t.Error("failed to get udp protocol")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
361 362

}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
363

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
364 365 366
func TestProtocolsWithString(t *testing.T) {
	pwn := ProtocolWithName
	good := map[string][]Protocol{
367 368 369 370 371 372
		"/ip4":                    {pwn("ip4")},
		"/ip4/tcp":                {pwn("ip4"), pwn("tcp")},
		"ip4/tcp/udp/ip6":         {pwn("ip4"), pwn("tcp"), pwn("udp"), pwn("ip6")},
		"////////ip4/tcp":         {pwn("ip4"), pwn("tcp")},
		"ip4/udp/////////":        {pwn("ip4"), pwn("udp")},
		"////////ip4/tcp////////": {pwn("ip4"), pwn("tcp")},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
373 374 375 376 377
	}

	for s, ps1 := range good {
		ps2, err := ProtocolsWithString(s)
		if err != nil {
Jeromy's avatar
go vet  
Jeromy committed
378
			t.Errorf("ProtocolsWithString(%s) should have succeeded", s)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
		}

		for i, ps1p := range ps1 {
			ps2p := ps2[i]
			if ps1p.Code != ps2p.Code {
				t.Errorf("mismatch: %s != %s, %s", ps1p.Name, ps2p.Name, s)
			}
		}
	}

	bad := []string{
		"dsijafd",                           // bogus proto
		"/ip4/tcp/fidosafoidsa",             // bogus proto
		"////////ip4/tcp/21432141/////////", // bogus proto
		"////////ip4///////tcp/////////",    // empty protos in between
	}

	for _, s := range bad {
		if _, err := ProtocolsWithString(s); err == nil {
Jeromy's avatar
go vet  
Jeromy committed
398
			t.Errorf("ProtocolsWithString(%s) should have failed", s)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
399 400 401 402 403
		}
	}

}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
404
func TestEncapsulate(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
405 406 407 408 409 410 411 412 413 414 415
	m, err := NewMultiaddr("/ip4/127.0.0.1/udp/1234")
	if err != nil {
		t.Error(err)
	}

	m2, err := NewMultiaddr("/udp/5678")
	if err != nil {
		t.Error(err)
	}

	b := m.Encapsulate(m2)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
416
	if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
417 418 419 420
		t.Error("encapsulate /ip4/127.0.0.1/udp/1234/udp/5678 failed.", s)
	}

	m3, _ := NewMultiaddr("/udp/5678")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
421
	c := b.Decapsulate(m3)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
422
	if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
423 424 425 426
		t.Error("decapsulate /udp failed.", "/ip4/127.0.0.1/udp/1234", s)
	}

	m4, _ := NewMultiaddr("/ip4/127.0.0.1")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
427
	d := c.Decapsulate(m4)
Steven Allen's avatar
Steven Allen committed
428 429
	if d != nil {
		t.Error("decapsulate /ip4 failed: ", d)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
430
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
431
}
432

Marco Munizaga's avatar
Marco Munizaga committed
433 434 435 436 437 438 439
func TestDecapsulateComment(t *testing.T) {
	// shows the behavior from the interface comment
	m := StringCast("/ip4/1.2.3.4/tcp/80")
	rest := m.Decapsulate(StringCast("/tcp/80"))
	if rest.String() != "/ip4/1.2.3.4" {
		t.Fatalf("Documented behavior is not correct. Expected %v saw %v", "/ip4/1.2.3.4/", rest.String())
	}
Marco Munizaga's avatar
Marco Munizaga committed
440 441 442 443 444 445 446 447 448 449

	m = StringCast("/ip4/1.2.3.4/tcp/80")
	rest = m.Decapsulate(StringCast("/udp/80"))
	if !rest.Equal(m) {
		t.Fatalf("Documented behavior is not correct. Expected %v saw %v", "/ip4/1.2.3.4/tcp/80", rest.String())
	}

	m = StringCast("/ip4/1.2.3.4/tcp/80")
	rest = m.Decapsulate(StringCast("/ip4/1.2.3.4"))
	require.Nil(t, rest, "expected a nil multiaddr if we decapsulate everything")
Marco Munizaga's avatar
Marco Munizaga committed
450 451
}

452 453 454 455 456 457 458 459
func assertValueForProto(t *testing.T, a Multiaddr, p int, exp string) {
	t.Logf("checking for %s in %s", ProtocolWithCode(p).Name, a)
	fv, err := a.ValueForProtocol(p)
	if err != nil {
		t.Fatal(err)
	}

	if fv != exp {
Jeromy's avatar
go vet  
Jeromy committed
460
		t.Fatalf("expected %q for %d in %s, but got %q instead", exp, p, a, fv)
461 462 463 464
	}
}

func TestGetValue(t *testing.T) {
Pavel Karpy's avatar
Pavel Karpy committed
465
	a := newMultiaddr(t, "/ip4/127.0.0.1/utp/tcp/5555/udp/1234/tls/utp/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
466 467
	assertValueForProto(t, a, P_IP4, "127.0.0.1")
	assertValueForProto(t, a, P_UTP, "")
Pavel Karpy's avatar
Pavel Karpy committed
468
	assertValueForProto(t, a, P_TLS, "")
469 470 471
	assertValueForProto(t, a, P_TCP, "5555")
	assertValueForProto(t, a, P_UDP, "1234")
	assertValueForProto(t, a, P_IPFS, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
472
	assertValueForProto(t, a, P_P2P, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
473 474 475 476 477 478 479 480 481 482

	_, err := a.ValueForProtocol(P_IP6)
	switch err {
	case ErrProtocolNotFound:
		break
	case nil:
		t.Fatal("expected value lookup to fail")
	default:
		t.Fatalf("expected ErrProtocolNotFound but got: %s", err)
	}
483 484 485 486 487 488 489 490 491 492 493

	a = newMultiaddr(t, "/ip4/0.0.0.0") // only one addr
	assertValueForProto(t, a, P_IP4, "0.0.0.0")

	a = newMultiaddr(t, "/ip4/0.0.0.0/ip4/0.0.0.0/ip4/0.0.0.0") // same sub-addr
	assertValueForProto(t, a, P_IP4, "0.0.0.0")

	a = newMultiaddr(t, "/ip4/0.0.0.0/udp/12345/utp") // ending in a no-value one.
	assertValueForProto(t, a, P_IP4, "0.0.0.0")
	assertValueForProto(t, a, P_UDP, "12345")
	assertValueForProto(t, a, P_UTP, "")
jbenet's avatar
jbenet committed
494 495 496

	a = newMultiaddr(t, "/ip4/0.0.0.0/unix/a/b/c/d") // ending in a path one.
	assertValueForProto(t, a, P_IP4, "0.0.0.0")
Steven Allen's avatar
Steven Allen committed
497
	assertValueForProto(t, a, P_UNIX, "/a/b/c/d")
498
}
499

web3-bot's avatar
web3-bot committed
500 501 502 503
func FuzzNewMultiaddrBytes(f *testing.F) {
	f.Fuzz(func(_ *testing.T, b []byte) {
		// just checking that it doesn't panic
		ma, err := NewMultiaddrBytes(b)
504 505
		if err == nil {
			// for any valid multiaddrs, make sure these calls don't panic
Jeromy's avatar
go vet  
Jeromy committed
506
			_ = ma.String()
507 508
			ma.Protocols()
		}
web3-bot's avatar
web3-bot committed
509
	})
Jeromy's avatar
Jeromy committed
510 511
}

web3-bot's avatar
web3-bot committed
512 513 514 515 516 517 518 519
func FuzzNewMultiaddrString(f *testing.F) {
	f.Add("/ip4/0.0.0.0/udp/12345/utp")
	f.Add("/ip4/1.2.3.4/udp/12345/quic")
	f.Add("/ip4/127.0.0.1/tcp/12345")
	f.Add("/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g")
	f.Fuzz(func(_ *testing.T, s string) {
		// just checking that it doesn't panic
		ma, err := NewMultiaddr(s)
Jeromy's avatar
Jeromy committed
520 521
		if err == nil {
			// for any valid multiaddrs, make sure these calls don't panic
Jeromy's avatar
go vet  
Jeromy committed
522
			_ = ma.String()
Jeromy's avatar
Jeromy committed
523 524
			ma.Protocols()
		}
web3-bot's avatar
web3-bot committed
525
	})
Jeromy's avatar
Jeromy committed
526
}
Lars Gierth's avatar
Lars Gierth committed
527 528 529 530 531 532 533 534 535 536 537 538

func TestBinaryRepresentation(t *testing.T) {
	expected := []byte{0x4, 0x7f, 0x0, 0x0, 0x1, 0x91, 0x2, 0x4, 0xd2}
	ma, err := NewMultiaddr("/ip4/127.0.0.1/udp/1234")
	if err != nil {
		t.Error(err)
	}

	if !bytes.Equal(ma.Bytes(), expected) {
		t.Errorf("expected %x, got %x", expected, ma.Bytes())
	}
}
Steven Allen's avatar
Steven Allen committed
539 540 541 542

func TestRoundTrip(t *testing.T) {
	for _, s := range []string{
		"/unix/a/b/c/d",
543
		"/ip6/::ffff:127.0.0.1/tcp/111",
Steven Allen's avatar
Steven Allen committed
544
		"/ip4/127.0.0.1/tcp/123",
Pavel Karpy's avatar
Pavel Karpy committed
545
		"/ip4/127.0.0.1/tcp/123/tls",
Steven Allen's avatar
Steven Allen committed
546 547
		"/ip4/127.0.0.1/udp/123",
		"/ip4/127.0.0.1/udp/123/ip6/::",
Marco Munizaga's avatar
Marco Munizaga committed
548
		"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
549 550
		"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP",
		"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP/unix/a/b/c",
Steven Allen's avatar
Steven Allen committed
551 552 553 554 555 556 557 558 559 560 561
	} {
		ma, err := NewMultiaddr(s)
		if err != nil {
			t.Errorf("error when parsing %q: %s", s, err)
			continue
		}
		if ma.String() != s {
			t.Errorf("failed to round trip %q", s)
		}
	}
}
Steven Allen's avatar
Steven Allen committed
562 563 564 565 566 567 568 569 570 571 572 573

func TestIPFSvP2P(t *testing.T) {
	var (
		p2pAddr  = "/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"
		ipfsAddr = "/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"
	)

	for _, s := range []string{p2pAddr, ipfsAddr} {
		ma, err := NewMultiaddr(s)
		if err != nil {
			t.Errorf("error when parsing %q: %s", s, err)
		}
574 575
		if ma.String() != p2pAddr {
			t.Errorf("expected %q, got %q", p2pAddr, ma.String())
Steven Allen's avatar
Steven Allen committed
576 577 578
		}
	}
}
Steven Allen's avatar
Steven Allen committed
579

580
func TestInvalidP2PAddrBytes(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
581 582 583 584 585 586 587 588 589 590 591 592
	badAddr := "a503221221c05877cbae039d70a5e600ea02c6f9f2942439285c9e344e26f8d280c850fad6"
	bts, err := hex.DecodeString(badAddr)
	if err != nil {
		t.Fatal(err)
	}
	ma, err := NewMultiaddrBytes(bts)
	if err == nil {
		t.Error("should have failed")
		// Check for panic
		_ = ma.String()
	}
}
mwnx's avatar
mwnx committed
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
func TestInvalidP2PAddrString(t *testing.T) {
	hashedData, err := mh.Sum([]byte("test"), mh.SHA2_256, -1)
	if err != nil {
		t.Fatal(err)
	}

	// using MD5 since it's not a valid data codec
	unknownCodecCID := cid.NewCidV1(mh.MD5, hashedData).String()

	badStringAddrs := []string{
		"/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl-", // invalid multibase encoding
		"/p2p/?unknownmultibase", // invalid multibase encoding
		"/p2p/k2jmtxwoe2phm1hbqp0e7nufqf6umvuu2e9qd7ana7h411a0haqj6i2z", // non-libp2p-key codec
		"/p2p/" + unknownCodecCID, // impossible codec
	}
	for _, a := range badStringAddrs {
		ma, err := NewMultiaddr(a)
		if err == nil {
			t.Error("should have failed")
			// Check for panic
			_ = ma.String()
		}
	}
}

mwnx's avatar
mwnx committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
func TestZone(t *testing.T) {
	ip6String := "/ip6zone/eth0/ip6/::1"
	ip6Bytes := []byte{
		0x2a, 4,
		'e', 't', 'h', '0',
		0x29,
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 1,
	}

	ma, err := NewMultiaddr(ip6String)
	if err != nil {
		t.Error(err)
	}
	if !bytes.Equal(ma.Bytes(), ip6Bytes) {
		t.Errorf("expected %x, got %x", ip6Bytes, ma.Bytes())
	}

	ma2, err2 := NewMultiaddrBytes(ip6Bytes)
	if err2 != nil {
		t.Error(err)
	}
	if ma2.String() != ip6String {
		t.Errorf("expected %s, got %s", ip6String, ma2.String())
	}
}
647 648

func TestBinaryMarshaler(t *testing.T) {
Pavel Karpy's avatar
Pavel Karpy committed
649
	addr := newMultiaddr(t, "/ip4/0.0.0.0/tcp/4001/tls")
650 651 652 653 654
	b, err := addr.MarshalBinary()
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
655
	var addr2 multiaddr
656 657 658
	if err = addr2.UnmarshalBinary(b); err != nil {
		t.Fatal(err)
	}
Steven Allen's avatar
Steven Allen committed
659
	if !addr.Equal(&addr2) {
660 661 662 663 664
		t.Error("expected equal addresses in circular marshaling test")
	}
}

func TestTextMarshaler(t *testing.T) {
Pavel Karpy's avatar
Pavel Karpy committed
665
	addr := newMultiaddr(t, "/ip4/0.0.0.0/tcp/4001/tls")
666 667 668 669 670
	b, err := addr.MarshalText()
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
671
	var addr2 multiaddr
672 673 674
	if err = addr2.UnmarshalText(b); err != nil {
		t.Fatal(err)
	}
Steven Allen's avatar
Steven Allen committed
675
	if !addr.Equal(&addr2) {
676 677 678 679 680
		t.Error("expected equal addresses in circular marshaling test")
	}
}

func TestJSONMarshaler(t *testing.T) {
Pavel Karpy's avatar
Pavel Karpy committed
681
	addr := newMultiaddr(t, "/ip4/0.0.0.0/tcp/4001/tls")
682 683 684 685 686
	b, err := addr.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
687
	var addr2 multiaddr
688 689 690
	if err = addr2.UnmarshalJSON(b); err != nil {
		t.Fatal(err)
	}
Steven Allen's avatar
Steven Allen committed
691
	if !addr.Equal(&addr2) {
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
		t.Error("expected equal addresses in circular marshaling test")
	}
}

func TestComponentBinaryMarshaler(t *testing.T) {
	comp, err := NewComponent("ip4", "0.0.0.0")
	if err != nil {
		t.Fatal(err)
	}
	b, err := comp.MarshalBinary()
	if err != nil {
		t.Fatal(err)
	}

	comp2 := &Component{}
	if err = comp2.UnmarshalBinary(b); err != nil {
		t.Fatal(err)
	}
	if !comp.Equal(comp2) {
		t.Error("expected equal components in circular marshaling test")
	}
}

func TestComponentTextMarshaler(t *testing.T) {
	comp, err := NewComponent("ip4", "0.0.0.0")
	if err != nil {
		t.Fatal(err)
	}
	b, err := comp.MarshalText()
	if err != nil {
		t.Fatal(err)
	}

	comp2 := &Component{}
	if err = comp2.UnmarshalText(b); err != nil {
		t.Fatal(err)
	}
	if !comp.Equal(comp2) {
		t.Error("expected equal components in circular marshaling test")
	}
}

func TestComponentJSONMarshaler(t *testing.T) {
	comp, err := NewComponent("ip4", "0.0.0.0")
	if err != nil {
		t.Fatal(err)
	}
	b, err := comp.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}

	comp2 := &Component{}
	if err = comp2.UnmarshalJSON(b); err != nil {
		t.Fatal(err)
	}
	if !comp.Equal(comp2) {
		t.Error("expected equal components in circular marshaling test")
	}
}
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

func TestFilterAddrs(t *testing.T) {
	bad := []Multiaddr{
		newMultiaddr(t, "/ip6/fe80::1/tcp/1234"),
		newMultiaddr(t, "/ip6/fe80::100/tcp/1234"),
	}
	good := []Multiaddr{
		newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
		newMultiaddr(t, "/ip4/1.1.1.1/tcp/999"),
		newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"),
	}
	goodAndBad := append(good, bad...)

	filter := func(addr Multiaddr) bool {
		return addr.Protocols()[0].Code == P_IP4
	}

	require.Empty(t, FilterAddrs(bad, filter))
	require.ElementsMatch(t, FilterAddrs(good, filter), good)
	require.ElementsMatch(t, FilterAddrs(goodAndBad, filter), good)
}
773 774 775 776 777

func TestContains(t *testing.T) {
	a1 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")
	a2 := newMultiaddr(t, "/ip4/1.1.1.1/tcp/999")
	a3 := newMultiaddr(t, "/ip4/1.2.3.4/udp/443/quic")
Marco Munizaga's avatar
Marco Munizaga committed
778 779
	a4 := newMultiaddr(t, "/ip4/1.2.3.4/udp/443/quic-v1")
	addrs := []Multiaddr{a1, a2, a3, a4}
780 781 782 783

	require.True(t, Contains(addrs, a1))
	require.True(t, Contains(addrs, a2))
	require.True(t, Contains(addrs, a3))
Marco Munizaga's avatar
Marco Munizaga committed
784
	require.True(t, Contains(addrs, a4))
785 786 787
	require.False(t, Contains(addrs, newMultiaddr(t, "/ip4/4.3.2.1/udp/1234/utp")))
	require.False(t, Contains(nil, a1))
}