multiaddr_test.go 15.8 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
	"bytes"
	"encoding/hex"
6
	"math/rand"
Jeromy's avatar
Jeromy committed
7
	"strings"
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
8
	"testing"
9
	"time"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10 11
)

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

20 21 22 23 24 25
func TestConstructFails(t *testing.T) {
	cases := []string{
		"/ip4",
		"/ip4/::1",
		"/ip4/fdpsofodsajfdoisa",
		"/ip6",
mwnx's avatar
mwnx committed
26 27 28
		"/ip6zone",
		"/ip6zone/",
		"/ip6zone//ip6/fe80::1",
29 30 31 32 33
		"/udp",
		"/tcp",
		"/sctp",
		"/udp/65536",
		"/tcp/65536",
Marten Seemann's avatar
Marten Seemann committed
34
		"/quic/65536",
35 36
		"/onion/9imaq4ygg2iegci7:80",
		"/onion/aaimaq4ygg2iegci7:80",
37 38
		"/onion/timaq4ygg2iegci7:0",
		"/onion/timaq4ygg2iegci7:-1",
39
		"/onion/timaq4ygg2iegci7",
40
		"/onion/timaq4ygg2iegci@:666",
idk's avatar
idk committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		"/onion3/9ww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:80",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd7:80",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:0",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:-1",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy@:666",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq7:80",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:0",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:0",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:-1",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniss47li5r6ugoertzuq@:666",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq7:80",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:0",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:0",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:-1",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniss47li5r6ugoertzuq@:666",
59 60 61 62 63 64 65
		"/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
66
		"/ip4/127.0.0.1/quic/1234",
67 68
		"/ip4/127.0.0.1/ipfs",
		"/ip4/127.0.0.1/ipfs/tcp",
Steven Allen's avatar
Steven Allen committed
69 70
		"/ip4/127.0.0.1/p2p",
		"/ip4/127.0.0.1/p2p/tcp",
jbenet's avatar
jbenet committed
71 72
		"/unix",
		"/ip4/1.2.3.4/tcp/80/unix",
73 74 75 76
	}

	for _, a := range cases {
		if _, err := NewMultiaddr(a); err == nil {
77
			t.Errorf("should have failed: %s - %s", a, err)
78 79 80 81 82 83 84 85 86 87
		}
	}
}

func TestConstructSucceeds(t *testing.T) {
	cases := []string{
		"/ip4/1.2.3.4",
		"/ip4/0.0.0.0",
		"/ip6/::1",
		"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21",
Marten Seemann's avatar
Marten Seemann committed
88
		"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21/udp/1234/quic",
mwnx's avatar
mwnx committed
89 90 91 92
		"/ip6zone/x/ip6/fe80::1",
		"/ip6zone/x%y/ip6/fe80::1",
		"/ip6zone/x%y/ip6/::",
		"/ip6zone/x/ip6/fe80::1/udp/1234/quic",
93 94
		"/onion/timaq4ygg2iegci7:1234",
		"/onion/timaq4ygg2iegci7:80/http",
idk's avatar
idk committed
95 96 97 98 99 100
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234",
		"/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:80/http",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:1234",
		"/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:80/http",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:1234",
		"/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:80/http",
101 102 103 104 105 106 107 108
		"/udp/0",
		"/tcp/0",
		"/sctp/0",
		"/udp/1234",
		"/tcp/1234",
		"/sctp/1234",
		"/udp/65535",
		"/tcp/65535",
109
		"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
Steven Allen's avatar
Steven Allen committed
110
		"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
111 112 113
		"/udp/1234/sctp/1234",
		"/udp/1234/udt",
		"/udp/1234/utp",
114 115 116
		"/tcp/1234/http",
		"/tcp/1234/https",
		"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
Steven Allen's avatar
Steven Allen committed
117
		"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
118 119 120 121
		"/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
122
		"/ip4/127.0.0.1/udp/1234/quic",
123 124
		"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
		"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
Steven Allen's avatar
Steven Allen committed
125 126
		"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
		"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
jbenet's avatar
jbenet committed
127 128 129 130
		"/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",
Steven Allen's avatar
Steven Allen committed
131
		"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio",
132 133 134 135
	}

	for _, a := range cases {
		if _, err := NewMultiaddr(a); err != nil {
136
			t.Errorf("should have succeeded: %s -- %s", a, err)
137 138 139 140
		}
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
141 142 143 144
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
145
	m4 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	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
166 167 168 169 170 171 172 173

	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
174 175
}

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

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
178 179 180 181 182
	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
183

184
		b2, err := stringToBytes(s)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
185
		if err != nil {
idk's avatar
idk committed
186
			t.Error("failed to convert", s, err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
187 188 189 190
		}
		if !bytes.Equal(b1, b2) {
			t.Error("failed to convert", s, "to", b1, "got", b2)
		}
Jeromy's avatar
Jeromy committed
191 192

		if err := validateBytes(b2); err != nil {
idk's avatar
idk committed
193
			t.Error(err, "len:", len(b2))
Jeromy's avatar
Jeromy committed
194
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
195
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
196

197
	testString("/ip4/127.0.0.1/udp/1234", "047f000001910204d2")
198
	testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
199
	testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f000001910204d2047f0000010610e1")
idk's avatar
idk committed
200 201 202 203 204
	testString("/onion/aaimaq4ygg2iegci:80", "bc030010c0439831b48218480050")
	testString("/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234", "bd03adadec040be047f9658668b11a504f3155001f231a37f54c4476c07fb4cc139ed7e30304d2")
	testString("/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:1234", "ca03efbcd45d0c5dc79781ac6f20ea5055a036afb48d45a52e7d68ec7d4338919e6904d2")
	testString("/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:1234", "cb03efbcd45d0c5dc79781ac6f20ea5055a036afb48d45a52e7d68ec7d4338919e6904d2")

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
205 206 207 208
}

func TestBytesToString(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
209
	testString := func(s1 string, h string) {
Steven Allen's avatar
Steven Allen committed
210
		t.Helper()
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
211 212 213 214
		b, err := hex.DecodeString(h)
		if err != nil {
			t.Error("failed to decode hex", h)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
215

Jeromy's avatar
Jeromy committed
216 217 218 219
		if err := validateBytes(b); err != nil {
			t.Error(err)
		}

220
		s2, err := bytesToString(b)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
221
		if err != nil {
222
			t.Error("failed to convert", b, err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
223
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
224

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
225 226 227 228
		if s1 != s2 {
			t.Error("failed to convert", b, "to", s1, "got", s2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
229

230
	testString("/ip4/127.0.0.1/udp/1234", "047f000001910204d2")
231
	testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
232
	testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f000001910204d2047f0000010610e1")
233
	testString("/onion/aaimaq4ygg2iegci:80", "bc030010c0439831b48218480050")
idk's avatar
idk committed
234 235 236
	testString("/onion3/vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd:1234", "bd03adadec040be047f9658668b11a504f3155001f231a37f54c4476c07fb4cc139ed7e30304d2")
	testString("/garlict/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:1234", "ca03efbcd45d0c5dc79781ac6f20ea5055a036afb48d45a52e7d68ec7d4338919e6904d2")
	testString("/garlicu/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:1234", "cb03efbcd45d0c5dc79781ac6f20ea5055a036afb48d45a52e7d68ec7d4338919e6904d2")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237 238
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
239
func TestBytesSplitAndJoin(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
240 241 242 243

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
247
		split := Split(m)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
248 249 250 251 252 253 254 255 256 257 258
		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
259 260 261 262 263
		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
264 265 266 267 268 269 270 271 272 273
		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"})
274 275
	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
276 277
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
278
func TestProtocols(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
279 280 281 282 283
	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
284
	ps := m.Protocols()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
285
	if ps[0].Code != ProtocolWithName("ip4").Code {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
286 287 288 289
		t.Error(ps[0], ProtocolWithName("ip4"))
		t.Error("failed to get ip4 protocol")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
290
	if ps[1].Code != ProtocolWithName("udp").Code {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
291 292 293
		t.Error(ps[1], ProtocolWithName("udp"))
		t.Error("failed to get udp protocol")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
294 295

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310
func TestProtocolsWithString(t *testing.T) {
	pwn := ProtocolWithName
	good := map[string][]Protocol{
		"/ip4":                    []Protocol{pwn("ip4")},
		"/ip4/tcp":                []Protocol{pwn("ip4"), pwn("tcp")},
		"ip4/tcp/udp/ip6":         []Protocol{pwn("ip4"), pwn("tcp"), pwn("udp"), pwn("ip6")},
		"////////ip4/tcp":         []Protocol{pwn("ip4"), pwn("tcp")},
		"ip4/udp/////////":        []Protocol{pwn("ip4"), pwn("udp")},
		"////////ip4/tcp////////": []Protocol{pwn("ip4"), pwn("tcp")},
	}

	for s, ps1 := range good {
		ps2, err := ProtocolsWithString(s)
		if err != nil {
Jeromy's avatar
go vet  
Jeromy committed
311
			t.Errorf("ProtocolsWithString(%s) should have succeeded", s)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		}

		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
331
			t.Errorf("ProtocolsWithString(%s) should have failed", s)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
332 333 334 335 336
		}
	}

}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
337
func TestEncapsulate(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
338 339 340 341 342 343 344 345 346 347 348
	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
349
	if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
350 351 352 353
		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
354
	c := b.Decapsulate(m3)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
355
	if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
356 357 358 359
		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
360
	d := c.Decapsulate(m4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
361
	if s := d.String(); s != "" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
362 363
		t.Error("decapsulate /ip4 failed.", "/", s)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
364
}
365 366 367 368 369 370 371 372 373

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
374
		t.Fatalf("expected %q for %d in %s, but got %q instead", exp, p, a, fv)
375 376 377 378 379 380 381 382 383 384
	}
}

func TestGetValue(t *testing.T) {
	a := newMultiaddr(t, "/ip4/127.0.0.1/utp/tcp/5555/udp/1234/utp/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
	assertValueForProto(t, a, P_IP4, "127.0.0.1")
	assertValueForProto(t, a, P_UTP, "")
	assertValueForProto(t, a, P_TCP, "5555")
	assertValueForProto(t, a, P_UDP, "1234")
	assertValueForProto(t, a, P_IPFS, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
385
	assertValueForProto(t, a, P_P2P, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
386 387 388 389 390 391 392 393 394 395

	_, 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)
	}
396 397 398 399 400 401 402 403 404 405 406

	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
407 408 409

	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
410
	assertValueForProto(t, a, P_UNIX, "/a/b/c/d")
411
}
412 413 414 415 416 417 418 419 420 421 422 423 424

func TestFuzzBytes(t *testing.T) {
	rand.Seed(time.Now().UnixNano())
	// Bump up these numbers if you want to stress this
	buf := make([]byte, 256)
	for i := 0; i < 2000; i++ {
		l := rand.Intn(len(buf))
		rand.Read(buf[:l])

		// just checking that it doesnt panic
		ma, err := NewMultiaddrBytes(buf[:l])
		if err == nil {
			// for any valid multiaddrs, make sure these calls don't panic
Jeromy's avatar
go vet  
Jeromy committed
425
			_ = ma.String()
426 427 428 429
			ma.Protocols()
		}
	}
}
Jeromy's avatar
Jeromy committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

func randMaddrString() string {
	good_corpus := []string{"tcp", "ip", "udp", "ipfs", "0.0.0.0", "127.0.0.1", "12345", "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"}

	size := rand.Intn(256)
	parts := make([]string, 0, size)
	for i := 0; i < size; i++ {
		switch rand.Intn(5) {
		case 0, 1, 2:
			parts = append(parts, good_corpus[rand.Intn(len(good_corpus))])
		default:
			badbuf := make([]byte, rand.Intn(256))
			rand.Read(badbuf)
			parts = append(parts, string(badbuf))
		}
	}

	return "/" + strings.Join(parts, "/")
}

func TestFuzzString(t *testing.T) {
	rand.Seed(time.Now().UnixNano())
	// Bump up these numbers if you want to stress this
	for i := 0; i < 2000; i++ {

		// just checking that it doesnt panic
		ma, err := NewMultiaddr(randMaddrString())
		if err == nil {
			// for any valid multiaddrs, make sure these calls don't panic
Jeromy's avatar
go vet  
Jeromy committed
459
			_ = ma.String()
Jeromy's avatar
Jeromy committed
460 461 462 463
			ma.Protocols()
		}
	}
}
Lars Gierth's avatar
Lars Gierth committed
464 465 466 467 468 469 470 471 472 473 474 475

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
476 477 478 479

func TestRoundTrip(t *testing.T) {
	for _, s := range []string{
		"/unix/a/b/c/d",
480
		"/ip6/::ffff:127.0.0.1/tcp/111",
Steven Allen's avatar
Steven Allen committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
		"/ip4/127.0.0.1/tcp/123",
		"/ip4/127.0.0.1/udp/123",
		"/ip4/127.0.0.1/udp/123/ip6/::",
		"/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP",
		"/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP/unix/a/b/c",
	} {
		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
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

// XXX: Change this test when we switch to /p2p by default.
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)
		}
		if ma.String() != ipfsAddr {
			t.Errorf("expected %q, got %q", ipfsAddr, ma.String())
		}
	}
}
Steven Allen's avatar
Steven Allen committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528

func TestInvalidP2PAddr(t *testing.T) {
	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
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

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())
	}
}