multiaddr_test.go 3.2 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
func newMultiaddr(t *testing.T, a string) *Multiaddr {
	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")

	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
43 44
func TestStringToBytes(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
45 46 47 48 49
	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
50

51
		b2, err := stringToBytes(s)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
52 53 54
		if err != nil {
			t.Error("failed to convert", s)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
56 57 58 59
		if !bytes.Equal(b1, b2) {
			t.Error("failed to convert", s, "to", b1, "got", b2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
61
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65
}

func TestBytesToString(t *testing.T) {

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
66 67 68 69 70
	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
71

72
		s2, err := bytesToString(b)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
73 74 75
		if err != nil {
			t.Error("failed to convert", b)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
77 78 79 80
		if s1 != s2 {
			t.Error("failed to convert", b, "to", s1, "got", s2)
		}
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
82
	testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
func TestProtocols(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	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
105 106

}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
107 108

func TestEncapsulate(t *testing.T) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
109 110 111 112 113 114 115 116 117 118 119
	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
120
	if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
121 122 123 124 125 126 127 128 129
		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)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130
	if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
131 132 133 134 135 136 137 138 139
		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)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140
	if s := d.String(); s != "" {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
141 142
		t.Error("decapsulate /ip4 failed.", "/", s)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

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