net_test.go 15.3 KB
Newer Older
1
package manet
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3 4

import (
	"bytes"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"fmt"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	"net"
Cole Brown's avatar
Cole Brown committed
7 8
	"os"
	"path/filepath"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10
	"sync"
	"testing"
Cole Brown's avatar
Cole Brown committed
11
	"time"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12

13
	ma "github.com/multiformats/go-multiaddr"
14
	"github.com/stretchr/testify/require"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16 17 18 19
)

func newMultiaddr(t *testing.T, m string) ma.Multiaddr {
	maddr, err := ma.NewMultiaddr(m)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
		t.Fatal("failed to construct multiaddr:", m, err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	}
	return maddr
}

func TestDial(t *testing.T) {

	listener, err := net.Listen("tcp", "127.0.0.1:4321")
	if err != nil {
		t.Fatal("failed to listen")
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		cB, err := listener.Accept()
		if err != nil {
37
			t.Error("failed to accept")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		}

		// echo out
		buf := make([]byte, 1024)
		for {
			_, err := cB.Read(buf)
			if err != nil {
				break
			}
			cB.Write(buf)
		}

		wg.Done()
	}()

	maddr := newMultiaddr(t, "/ip4/127.0.0.1/tcp/4321")
	cA, err := Dial(maddr)
	if err != nil {
		t.Fatal("failed to dial")
	}

	buf := make([]byte, 1024)
	if _, err := cA.Write([]byte("beep boop")); err != nil {
		t.Fatal("failed to write:", err)
	}

	if _, err := cA.Read(buf); err != nil {
		t.Fatal("failed to read:", buf, err)
	}

	if !bytes.Equal(buf[:9], []byte("beep boop")) {
		t.Fatal("failed to echo:", buf)
	}

	maddr2 := cA.RemoteMultiaddr()
	if !maddr2.Equal(maddr) {
		t.Fatal("remote multiaddr not equal:", maddr, maddr2)
	}

	cA.Close()
	wg.Wait()
}

Cole Brown's avatar
Cole Brown committed
81
func TestUnixSockets(t *testing.T) {
web3-bot's avatar
web3-bot committed
82
	dir, err := os.MkdirTemp(os.TempDir(), "manettest")
Cole Brown's avatar
Cole Brown committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	if err != nil {
		t.Fatal(err)
	}
	path := filepath.Join(dir, "listen.sock")
	maddr := newMultiaddr(t, "/unix/"+path)

	listener, err := Listen(maddr)
	if err != nil {
		t.Fatal(err)
	}

	payload := []byte("hello")

	// listen
	done := make(chan struct{}, 1)
	go func() {
		conn, err := listener.Accept()
		if err != nil {
101
			t.Error(err)
Cole Brown's avatar
Cole Brown committed
102 103 104 105 106
		}
		defer conn.Close()
		buf := make([]byte, 1024)
		n, err := conn.Read(buf)
		if err != nil {
107
			t.Error(err)
Cole Brown's avatar
Cole Brown committed
108 109
		}
		if n != len(payload) {
110
			t.Error("failed to read appropriate number of bytes")
Cole Brown's avatar
Cole Brown committed
111 112
		}
		if !bytes.Equal(buf[0:n], payload) {
113
			t.Error("payload did not match")
Cole Brown's avatar
Cole Brown committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		}
		done <- struct{}{}
	}()

	// dial
	conn, err := Dial(maddr)
	if err != nil {
		t.Fatal(err)
	}
	n, err := conn.Write(payload)
	if err != nil {
		t.Fatal(err)
	}
	if n != len(payload) {
		t.Fatal("failed to write appropriate number of bytes")
	}
	select {
	case <-done:
	case <-time.After(1 * time.Second):
		t.Fatal("timed out waiting for read")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137 138 139 140 141 142 143 144 145 146 147 148 149
func TestListen(t *testing.T) {

	maddr := newMultiaddr(t, "/ip4/127.0.0.1/tcp/4322")
	listener, err := Listen(maddr)
	if err != nil {
		t.Fatal("failed to listen")
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		cB, err := listener.Accept()
		if err != nil {
150
			t.Error("failed to accept")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151 152 153
		}

		if !cB.LocalMultiaddr().Equal(maddr) {
154
			t.Error("local multiaddr not equal:", maddr, cB.LocalMultiaddr())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		}

		// echo out
		buf := make([]byte, 1024)
		for {
			_, err := cB.Read(buf)
			if err != nil {
				break
			}
			cB.Write(buf)
		}

		wg.Done()
	}()

	cA, err := net.Dial("tcp", "127.0.0.1:4322")
	if err != nil {
		t.Fatal("failed to dial")
	}

	buf := make([]byte, 1024)
	if _, err := cA.Write([]byte("beep boop")); err != nil {
		t.Fatal("failed to write:", err)
	}

	if _, err := cA.Read(buf); err != nil {
		t.Fatal("failed to read:", buf, err)
	}

	if !bytes.Equal(buf[:9], []byte("beep boop")) {
		t.Fatal("failed to echo:", buf)
	}

	maddr2, err := FromNetAddr(cA.RemoteAddr())
	if err != nil {
		t.Fatal("failed to convert", err)
	}
	if !maddr2.Equal(maddr) {
		t.Fatal("remote multiaddr not equal:", maddr, maddr2)
	}

	cA.Close()
	wg.Wait()
}

200 201
func TestListenAddrs(t *testing.T) {

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202 203 204 205
	test := func(addr, resaddr string, succeed bool) {
		if resaddr == "" {
			resaddr = addr
		}
206 207 208 209 210 211 212 213 214 215

		maddr := newMultiaddr(t, addr)
		l, err := Listen(maddr)
		if !succeed {
			if err == nil {
				t.Fatal("succeeded in listening", addr)
			}
			return
		}
		if succeed && err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
216
			t.Error("failed to listen", addr, err)
217 218
		}
		if l == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
219 220 221 222
			t.Error("failed to listen", addr, succeed, err)
		}
		if l.Multiaddr().String() != resaddr {
			t.Error("listen addr did not resolve properly", l.Multiaddr().String(), resaddr, succeed, err)
223 224 225 226 227 228 229
		}

		if err = l.Close(); err != nil {
			t.Fatal("failed to close listener", addr, err)
		}
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230 231 232
	test("/ip4/127.0.0.1/tcp/4324", "", true)
	test("/ip4/127.0.0.1/udp/4325", "", false)
	test("/ip4/127.0.0.1/udp/4326/udt", "", false)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

	if len(os.Getenv("CI")) > 0 {
		test("/ip4/0.0.0.0/tcp/4324", "", true)
		test("/ip4/0.0.0.0/udp/4325", "", false)
		test("/ip4/0.0.0.0/udp/4326/udt", "", false)

		test("/ip6/::1/tcp/4324", "", true)
		test("/ip6/::1/udp/4325", "", false)
		test("/ip6/::1/udp/4326/udt", "", false)
		test("/ip6/::/tcp/4324", "", true)
		test("/ip6/::/udp/4325", "", false)
		test("/ip6/::/udp/4326/udt", "", false)

		/* "An implementation should also support the concept of a "default"
		 * zone for each scope.  And, when supported, the index value zero
		 * at each scope SHOULD be reserved to mean "use the default zone"."
		 * -- rfc4007. So, this _should_ work everywhere(?). */
		test("/ip6zone/0/ip6/::1/tcp/4324", "/ip6/::1/tcp/4324", true)
		test("/ip6zone/0/ip6/::1/udp/4324", "", false)
	} else {
		t.Skip("all tests only run on CI")
	}
255 256
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
257 258 259 260 261 262 263 264 265 266 267 268 269
func TestListenAndDial(t *testing.T) {

	maddr := newMultiaddr(t, "/ip4/127.0.0.1/tcp/4323")
	listener, err := Listen(maddr)
	if err != nil {
		t.Fatal("failed to listen")
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		cB, err := listener.Accept()
		if err != nil {
270
			t.Error("failed to accept")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
271 272 273
		}

		if !cB.LocalMultiaddr().Equal(maddr) {
274
			t.Error("local multiaddr not equal:", maddr, cB.LocalMultiaddr())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
		}

		// echo out
		buf := make([]byte, 1024)
		for {
			_, err := cB.Read(buf)
			if err != nil {
				break
			}
			cB.Write(buf)
		}

		wg.Done()
	}()

	cA, err := Dial(newMultiaddr(t, "/ip4/127.0.0.1/tcp/4323"))
	if err != nil {
		t.Fatal("failed to dial")
	}

	buf := make([]byte, 1024)
	if _, err := cA.Write([]byte("beep boop")); err != nil {
		t.Fatal("failed to write:", err)
	}

	if _, err := cA.Read(buf); err != nil {
		t.Fatal("failed to read:", buf, err)
	}

	if !bytes.Equal(buf[:9], []byte("beep boop")) {
		t.Fatal("failed to echo:", buf)
	}

	maddr2 := cA.RemoteMultiaddr()
	if !maddr2.Equal(maddr) {
		t.Fatal("remote multiaddr not equal:", maddr, maddr2)
	}

	cA.Close()
	wg.Wait()
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
316

317 318 319 320 321 322 323 324 325 326 327
func TestListenPacketAndDial(t *testing.T) {
	maddr := newMultiaddr(t, "/ip4/127.0.0.1/udp/4324")
	pc, err := ListenPacket(maddr)
	if err != nil {
		t.Fatal("failed to listen", err)
	}

	var wg sync.WaitGroup
	wg.Add(1)

	go func() {
328
		if !pc.LocalMultiaddr().Equal(maddr) {
329
			t.Error("connection multiaddr not equal:", maddr, pc.LocalMultiaddr())
330 331 332 333 334
		}

		buffer := make([]byte, 1024)
		_, addr, err := pc.ReadFrom(buffer)
		if err != nil {
335
			t.Error("failed to read into buffer", err)
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		}
		pc.WriteTo(buffer, addr)

		wg.Done()
	}()

	cn, err := Dial(maddr)
	if err != nil {
		t.Fatal("failed to dial", err)
	}

	buf := make([]byte, 1024)
	if _, err := cn.Write([]byte("beep boop")); err != nil {
		t.Fatal("failed to write", err)
	}

	if _, err := cn.Read(buf); err != nil {
		t.Fatal("failed to read:", buf, err)
	}

	if !bytes.Equal(buf[:9], []byte("beep boop")) {
		t.Fatal("failed to echk:", buf)
	}

	maddr2 := cn.RemoteMultiaddr()
	if !maddr2.Equal(maddr) {
		t.Fatal("remote multiaddr not equal:", maddr, maddr2)
	}

	cn.Close()
	pc.Close()
	wg.Wait()
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
370
func TestIPLoopback(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
371 372 373 374 375 376 377 378
	if IP4Loopback.String() != "/ip4/127.0.0.1" {
		t.Error("IP4Loopback incorrect:", IP4Loopback)
	}

	if IP6Loopback.String() != "/ip6/::1" {
		t.Error("IP6Loopback incorrect:", IP6Loopback)
	}

Steven Allen's avatar
Steven Allen committed
379
	if IP4MappedIP6Loopback.String() != "/ip6/::ffff:127.0.0.1" {
Steven Allen's avatar
Steven Allen committed
380
		t.Error("IP4MappedIP6Loopback incorrect:", IP4MappedIP6Loopback)
381 382
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
383
	if !IsIPLoopback(IP4Loopback) {
384
		t.Error("IsIPLoopback failed (IP4Loopback)")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
385 386
	}

387 388 389 390 391 392 393 394 395 396 397 398
	if !IsIPLoopback(newMultiaddr(t, "/ip4/127.1.80.9")) {
		t.Error("IsIPLoopback failed (/ip4/127.1.80.9)")
	}

	if IsIPLoopback(newMultiaddr(t, "/ip4/112.123.11.1")) {
		t.Error("IsIPLoopback false positive (/ip4/112.123.11.1)")
	}

	if IsIPLoopback(newMultiaddr(t, "/ip4/192.168.0.1/ip6/::1")) {
		t.Error("IsIPLoopback false positive (/ip4/192.168.0.1/ip6/::1)")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
399
	if !IsIPLoopback(IP6Loopback) {
400 401 402
		t.Error("IsIPLoopback failed (IP6Loopback)")
	}

Steven Allen's avatar
Steven Allen committed
403 404 405 406 407 408 409 410 411 412
	if !IsIPLoopback(newMultiaddr(t, "/ip6/127.0.0.1")) {
		t.Error("IsIPLoopback failed (/ip6/127.0.0.1)")
	}

	if !IsIPLoopback(newMultiaddr(t, "/ip6/127.99.3.2")) {
		t.Error("IsIPLoopback failed (/ip6/127.99.3.2)")
	}

	if IsIPLoopback(newMultiaddr(t, "/ip6/::fffa:127.99.3.2")) {
		t.Error("IsIPLoopback false positive (/ip6/::fffa:127.99.3.2)")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
413
	}
mwnx's avatar
mwnx committed
414 415 416 417 418 419 420 421 422 423 424 425

	if !IsIPLoopback(newMultiaddr(t, "/ip6zone/0/ip6/::1")) {
		t.Error("IsIPLoopback failed (/ip6zone/0/ip6/::1)")
	}

	if !IsIPLoopback(newMultiaddr(t, "/ip6zone/xxx/ip6/::1")) {
		t.Error("IsIPLoopback failed (/ip6zone/xxx/ip6/::1)")
	}

	if IsIPLoopback(newMultiaddr(t, "/ip6zone/0/ip6/1::1")) {
		t.Errorf("IsIPLoopback false positive (/ip6zone/0/ip6/1::1)")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
426
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

func TestIPUnspecified(t *testing.T) {
	if IP4Unspecified.String() != "/ip4/0.0.0.0" {
		t.Error("IP4Unspecified incorrect:", IP4Unspecified)
	}

	if IP6Unspecified.String() != "/ip6/::" {
		t.Error("IP6Unspecified incorrect:", IP6Unspecified)
	}

	if !IsIPUnspecified(IP4Unspecified) {
		t.Error("IsIPUnspecified failed (IP4Unspecified)")
	}

	if !IsIPUnspecified(IP6Unspecified) {
		t.Error("IsIPUnspecified failed (IP6Unspecified)")
	}
mwnx's avatar
mwnx committed
444 445 446 447

	if !IsIPUnspecified(newMultiaddr(t, "/ip6zone/xxx/ip6/::")) {
		t.Error("IsIPUnspecified failed (/ip6zone/xxx/ip6/::)")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
448
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
449 450 451

func TestIP6LinkLocal(t *testing.T) {
	for a := 0; a < 65536; a++ {
452
		isLinkLocal := a&0xffc0 == 0xfe80 || a&0xff0f == 0xff02
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
453 454
		m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a))
		if IsIP6LinkLocal(m) != isLinkLocal {
Jeromy's avatar
Jeromy committed
455
			t.Errorf("IsIP6LinkLocal failed (%s != %v)", m, isLinkLocal)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
456 457
		}
	}
mwnx's avatar
mwnx committed
458 459 460 461

	if !IsIP6LinkLocal(newMultiaddr(t, "/ip6zone/hello/ip6/fe80::9999")) {
		t.Error("IsIP6LinkLocal failed (/ip6/fe80::9999)")
	}
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

	bad := []ma.Multiaddr{
		newMultiaddr(t, "/ip6/fe80::1/tcp/1234"),   // link local
		newMultiaddr(t, "/ip6/fe80::100/tcp/1234"), // link local
	}
	good := []ma.Multiaddr{
		newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
		newMultiaddr(t, "/ip6/::1/tcp/1234"),
		newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"),
	}
	for _, addr := range bad {
		require.True(t, IsIP6LinkLocal(addr), "%s is a link local addr", addr)
	}
	for _, addr := range good {
		require.False(t, IsIP6LinkLocal(addr), "%s is not a link local addr", addr)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
478
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
479

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
func TestConvertNetAddr(t *testing.T) {
	m1 := newMultiaddr(t, "/ip4/1.2.3.4/tcp/4001")

	n1, err := ToNetAddr(m1)
	if err != nil {
		t.Fatal(err)
	}

	m2, err := FromNetAddr(n1)
	if err != nil {
		t.Fatal(err)
	}

	if m1.String() != m2.String() {
		t.Fatal("ToNetAddr + FromNetAddr did not work")
	}
}

func TestWrapNetConn(t *testing.T) {
	// test WrapNetConn nil
	if _, err := WrapNetConn(nil); err == nil {
		t.Error("WrapNetConn(nil) should return an error")
	}

	checkErr := func(err error, s string) {
		if err != nil {
			t.Fatal(s, err)
		}
	}

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	checkErr(err, "failed to listen")

	var wg sync.WaitGroup
	defer wg.Wait()
	wg.Add(1)
	go func() {
		defer wg.Done()
		cB, err := listener.Accept()
		checkErr(err, "failed to accept")
520
		_ = cB.(halfOpen)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
521 522 523 524 525 526
		cB.Close()
	}()

	cA, err := net.Dial("tcp", listener.Addr().String())
	checkErr(err, "failed to dial")
	defer cA.Close()
527
	_ = cA.(halfOpen)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
528 529 530 531 532 533 534 535 536

	lmaddr, err := FromNetAddr(cA.LocalAddr())
	checkErr(err, "failed to get local addr")
	rmaddr, err := FromNetAddr(cA.RemoteAddr())
	checkErr(err, "failed to get remote addr")

	mcA, err := WrapNetConn(cA)
	checkErr(err, "failed to wrap conn")

537 538
	_ = mcA.(halfOpen)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552
	if mcA.LocalAddr().String() != cA.LocalAddr().String() {
		t.Error("wrapped conn local addr differs")
	}
	if mcA.RemoteAddr().String() != cA.RemoteAddr().String() {
		t.Error("wrapped conn remote addr differs")
	}
	if mcA.LocalMultiaddr().String() != lmaddr.String() {
		t.Error("wrapped conn local maddr differs")
	}
	if mcA.RemoteMultiaddr().String() != rmaddr.String() {
		t.Error("wrapped conn remote maddr differs")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 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 619 620 621 622 623
func TestAddrMatch(t *testing.T) {

	test := func(m ma.Multiaddr, input, expect []ma.Multiaddr) {
		actual := AddrMatch(m, input)
		testSliceEqual(t, expect, actual)
	}

	a := []ma.Multiaddr{
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/2345"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
		newMultiaddr(t, "/ip6/::1/tcp/1234"),
		newMultiaddr(t, "/ip6/::1/tcp/2345"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
	}

	test(a[0], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/2345"),
	})
	test(a[2], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/tcp/2345"),
	})
	test(a[4], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/udp/1234"),
	})
	test(a[6], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
		newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234/ip6/::1"),
	})
	test(a[8], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip6/::1/tcp/1234"),
		newMultiaddr(t, "/ip6/::1/tcp/2345"),
	})
	test(a[10], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/tcp/2345"),
	})
	test(a[12], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/udp/1234"),
	})
	test(a[14], a, []ma.Multiaddr{
		newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
		newMultiaddr(t, "/ip6/::1/tcp/1234/ip6/::1"),
	})

}

func testSliceEqual(t *testing.T, a, b []ma.Multiaddr) {
	if len(a) != len(b) {
		t.Error("differ", a, b)
	}
	for i, addrA := range a {
		if !addrA.Equal(b[i]) {
			t.Error("differ", a, b)
		}
	}
}
Jeromy's avatar
Jeromy committed
624 625 626 627 628 629 630

func TestInterfaceAddressesWorks(t *testing.T) {
	_, err := InterfaceMultiaddrs()
	if err != nil {
		t.Fatal(err)
	}
}
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

func TestNetListener(t *testing.T) {
	listener, err := net.Listen("tcp", "127.0.0.1:1234")
	if err != nil {
		t.Fatal(err)
	}
	defer listener.Close()
	malist, err := WrapNetListener(listener)
	if err != nil {
		t.Fatal(err)
	}
	if !malist.Multiaddr().Equal(newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")) {
		t.Fatal("unexpected multiaddr")
	}

	go func() {
		c, err := Dial(malist.Multiaddr())
		if err != nil {
649
			t.Error("failed to dial")
650 651
		}
		if !c.RemoteMultiaddr().Equal(malist.Multiaddr()) {
652
			t.Error("dialed wrong target")
653 654 655 656 657
		}
		c.Close()

		c, err = Dial(malist.Multiaddr())
		if err != nil {
658
			t.Error("failed to dial")
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
		}
		c.Close()
	}()

	c, err := malist.Accept()
	if err != nil {
		t.Fatal(err)
	}
	c.Close()
	netList := NetListener(malist)
	malist2, err := WrapNetListener(netList)
	if err != nil {
		t.Fatal(err)
	}
	if malist2 != malist {
		t.Fatal("expected WrapNetListener(NetListener(malist)) == malist")
	}
	nc, err := netList.Accept()
	if err != nil {
		t.Fatal(err)
	}
	if !nc.(Conn).LocalMultiaddr().Equal(malist.Multiaddr()) {
		t.Fatal("wrong multiaddr on conn")
	}
	nc.Close()
}