multiaddr_test.go 2.64 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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8 9 10
)

func TestStringToBytes(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
11 12 13 14 15
	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
16

17
		b2, err := stringToBytes(s)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
18 19 20
		if err != nil {
			t.Error("failed to convert", s)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
22 23 24 25
		if !bytes.Equal(b1, b2) {
			t.Error("failed to convert", s, "to", b1, "got", b2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
27
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28 29 30 31
}

func TestBytesToString(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
32 33 34 35 36
	testString := func(s1 string, h string) {
		b, err := hex.DecodeString(h)
		if err != nil {
			t.Error("failed to decode hex", h)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37

38
		s2, err := bytesToString(b)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
39 40 41
		if err != nil {
			t.Error("failed to convert", b)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
43 44 45 46
		if s1 != s2 {
			t.Error("failed to convert", b, "to", s1, "got", s2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
48
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51
func TestProtocols(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	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")
	}

	ps, err := m.Protocols()
	if err != nil {
		t.Error("failed to get protocols", "/ip4/127.0.0.1/udp/1234")
	}

	if ps[0] != ProtocolWithName("ip4") {
		t.Error(ps[0], ProtocolWithName("ip4"))
		t.Error("failed to get ip4 protocol")
	}

	if ps[1] != ProtocolWithName("udp") {
		t.Error(ps[1], ProtocolWithName("udp"))
		t.Error("failed to get udp protocol")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71 72

}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74

func TestEncapsulate(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	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)
	if s, _ := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
		t.Error("encapsulate /ip4/127.0.0.1/udp/1234/udp/5678 failed.", s)
	}

	m3, _ := NewMultiaddr("/udp/5678")
	c, err := b.Decapsulate(m3)
	if err != nil {
		t.Error("decapsulate /udp failed.", err)
	}

	if s, _ := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
		t.Error("decapsulate /udp failed.", "/ip4/127.0.0.1/udp/1234", s)
	}

	m4, _ := NewMultiaddr("/ip4/127.0.0.1")
	d, err := c.Decapsulate(m4)
	if err != nil {
		t.Error("decapsulate /ip4 failed.", err)
	}

	if s, _ := d.String(); s != "" {
		t.Error("decapsulate /ip4 failed.", "/", s)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

func TestDialArgs(t *testing.T) {
	m, err := NewMultiaddr("/ip4/127.0.0.1/udp/1234")
	if err != nil {
		t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234")
	}

	nw, host, err := m.DialArgs()
	if err != nil {
		t.Fatal("failed to get dial args", "/ip4/127.0.0.1/udp/1234", err)
	}

	if nw != "udp" {
		t.Error("failed to get udp network Dial Arg")
	}

	if host != "127.0.0.1:1234" {
		t.Error("failed to get host:port Dial Arg")
	}
}