multiaddr_test.go 4.08 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
)

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

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
21
	m4 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

	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
42 43 44 45 46 47 48 49

	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
50 51
}

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

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
54 55 56 57 58
	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
59

60
		b2, err := stringToBytes(s)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
61 62 63
		if err != nil {
			t.Error("failed to convert", s)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
65 66 67 68
		if !bytes.Equal(b1, b2) {
			t.Error("failed to convert", s, "to", b1, "got", b2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
70
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
71 72
	testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
	testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75 76
}

func TestBytesToString(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
77 78 79 80 81
	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
82

83
		s2, err := bytesToString(b)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
84 85 86
		if err != nil {
			t.Error("failed to convert", b)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
88 89 90 91
		if s1 != s2 {
			t.Error("failed to convert", b, "to", s1, "got", s2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
93
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
94 95
	testString("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1")
	testString("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f0000011104d2047f0000010610e1")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
func TestBytesSplitAndJoin(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99 100 101 102

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106
		split := Split(m)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
107 108 109 110 111 112 113 114 115 116 117
		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
118 119 120 121 122
		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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		// modifying underlying bytes is fine.
		m2 := m.(*multiaddr)
		for i := range m2.bytes {
			m2.bytes[i] = 0
		}

		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"})
139 140
	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
141 142
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143
func TestProtocols(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
144 145 146 147 148
	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
149
	ps := m.Protocols()
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
150 151 152 153 154 155 156 157 158
	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
159 160

}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161 162

func TestEncapsulate(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
163 164 165 166 167 168 169 170 171 172 173
	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
174
	if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
175 176 177 178
		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
179
	c := b.Decapsulate(m3)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
180
	if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
181 182 183 184
		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
185
	d := c.Decapsulate(m4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186
	if s := d.String(); s != "" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
187 188
		t.Error("decapsulate /ip4 failed.", "/", s)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
189
}