multiaddr_test.go 7.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"
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
	m, err := NewMultiaddr(a)
	if err != nil {
		t.Error(err)
	}
	return m
}

17 18 19 20 21 22 23 24 25 26 27
func TestConstructFails(t *testing.T) {
	cases := []string{
		"/ip4",
		"/ip4/::1",
		"/ip4/fdpsofodsajfdoisa",
		"/ip6",
		"/udp",
		"/tcp",
		"/sctp",
		"/udp/65536",
		"/tcp/65536",
28 29
		"/onion/9imaq4ygg2iegci7:80",
		"/onion/aaimaq4ygg2iegci7:80",
30 31
		"/onion/timaq4ygg2iegci7:0",
		"/onion/timaq4ygg2iegci7:-1",
32
		"/onion/timaq4ygg2iegci7",
33
		"/onion/timaq4ygg2iegci@:666",
34 35 36 37 38 39 40
		"/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",
41 42
		"/ip4/127.0.0.1/ipfs",
		"/ip4/127.0.0.1/ipfs/tcp",
43 44 45 46
	}

	for _, a := range cases {
		if _, err := NewMultiaddr(a); err == nil {
47
			t.Errorf("should have failed: %s - %s", a, err)
48 49 50 51 52 53 54 55 56 57
		}
	}
}

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",
58 59
		"/onion/timaq4ygg2iegci7:1234",
		"/onion/timaq4ygg2iegci7:80/http",
60 61 62 63 64 65 66 67
		"/udp/0",
		"/tcp/0",
		"/sctp/0",
		"/udp/1234",
		"/tcp/1234",
		"/sctp/1234",
		"/udp/65535",
		"/tcp/65535",
68
		"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
69 70 71
		"/udp/1234/sctp/1234",
		"/udp/1234/udt",
		"/udp/1234/utp",
72 73 74
		"/tcp/1234/http",
		"/tcp/1234/https",
		"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
75 76 77 78
		"/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/",
79 80
		"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
		"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
81 82 83 84
	}

	for _, a := range cases {
		if _, err := NewMultiaddr(a); err != nil {
85
			t.Errorf("should have succeeded: %s -- %s", a, err)
86 87 88 89
		}
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92 93
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
94
	m4 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

	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
115 116 117 118 119 120 121 122

	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
123 124
}

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

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
127 128 129 130 131
	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
132

133
		b2, err := stringToBytes(s)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
134 135 136
		if err != nil {
			t.Error("failed to convert", s)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
138 139 140 141
		if !bytes.Equal(b1, b2) {
			t.Error("failed to convert", s, "to", b1, "got", b2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
143
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
144 145
	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
146 147 148 149
}

func TestBytesToString(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
150 151 152 153 154
	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
155

156
		s2, err := bytesToString(b)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
157 158 159
		if err != nil {
			t.Error("failed to convert", b)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
160

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
161 162 163 164
		if s1 != s2 {
			t.Error("failed to convert", b, "to", s1, "got", s2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
166
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
167 168
	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
169 170
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171
func TestBytesSplitAndJoin(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172 173 174 175

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
179
		split := Split(m)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
180 181 182 183 184 185 186 187 188 189 190
		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
191 192 193 194 195
		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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		// 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"})
212 213
	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
214 215
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
216
func TestProtocols(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
217 218 219 220 221
	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
222
	ps := m.Protocols()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
223
	if ps[0].Code != ProtocolWithName("ip4").Code {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
224 225 226 227
		t.Error(ps[0], ProtocolWithName("ip4"))
		t.Error("failed to get ip4 protocol")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
228
	if ps[1].Code != ProtocolWithName("udp").Code {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
229 230 231
		t.Error(ps[1], ProtocolWithName("udp"))
		t.Error("failed to get udp protocol")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
232 233

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
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 {
			t.Error("ProtocolsWithString(%s) should have succeeded", s)
		}

		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 {
			t.Error("ProtocolsWithString(%s) should have failed", s)
		}
	}

}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
275
func TestEncapsulate(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
276 277 278 279 280 281 282 283 284 285 286
	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
287
	if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
288 289 290 291
		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
292
	c := b.Decapsulate(m3)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
293
	if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
294 295 296 297
		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
298
	d := c.Decapsulate(m4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
299
	if s := d.String(); s != "" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
300 301
		t.Error("decapsulate /ip4 failed.", "/", s)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
302
}
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

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 {
		t.Fatalf("expected %q for %d in %d, but got %q instead", exp, p, a, fv)
	}
}

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")

	_, 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)
	}
}