2014-07-03 23:42:24 -07:00
|
|
|
package multiaddr
|
|
|
|
|
|
|
|
import (
|
2014-07-04 11:21:39 -07:00
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
2014-07-03 23:42:24 -07:00
|
|
|
)
|
|
|
|
|
2014-10-06 03:27:29 -07:00
|
|
|
func newMultiaddr(t *testing.T, a string) Multiaddr {
|
2014-09-16 06:08:48 -07:00
|
|
|
m, err := NewMultiaddr(a)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-01-09 05:30:33 -08:00
|
|
|
func TestConstructFails(t *testing.T) {
|
|
|
|
cases := []string{
|
|
|
|
"/ip4",
|
|
|
|
"/ip4/::1",
|
|
|
|
"/ip4/fdpsofodsajfdoisa",
|
|
|
|
"/ip6",
|
|
|
|
"/udp",
|
|
|
|
"/tcp",
|
|
|
|
"/sctp",
|
|
|
|
"/udp/65536",
|
|
|
|
"/tcp/65536",
|
|
|
|
"/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",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range cases {
|
|
|
|
if _, err := NewMultiaddr(a); err == nil {
|
|
|
|
t.Errorf("should have failed: %s", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
"/udp/0",
|
|
|
|
"/tcp/0",
|
|
|
|
"/sctp/0",
|
|
|
|
"/udp/1234",
|
|
|
|
"/tcp/1234",
|
|
|
|
"/sctp/1234",
|
|
|
|
"/udp/65535",
|
|
|
|
"/tcp/65535",
|
|
|
|
"/udp/1234/sctp/1234",
|
|
|
|
"/udp/1234/udt",
|
|
|
|
"/udp/1234/utp",
|
|
|
|
"/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/",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range cases {
|
|
|
|
if _, err := NewMultiaddr(a); err != nil {
|
|
|
|
t.Errorf("should have succeeded: %s", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:08:48 -07:00
|
|
|
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")
|
2014-10-10 20:40:51 -07:00
|
|
|
m4 := newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/")
|
2014-09-16 06:08:48 -07:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2014-10-10 20:40:51 -07:00
|
|
|
|
|
|
|
if !m2.Equal(m4) {
|
|
|
|
t.Error("should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !m4.Equal(m3) {
|
|
|
|
t.Error("should be equal")
|
|
|
|
}
|
2014-09-16 06:08:48 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 23:42:24 -07:00
|
|
|
func TestStringToBytes(t *testing.T) {
|
|
|
|
|
2014-07-04 11:21:39 -07:00
|
|
|
testString := func(s string, h string) {
|
|
|
|
b1, err := hex.DecodeString(h)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to decode hex", h)
|
|
|
|
}
|
2014-07-03 23:42:24 -07:00
|
|
|
|
2014-09-11 10:47:56 -07:00
|
|
|
b2, err := stringToBytes(s)
|
2014-07-04 11:21:39 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to convert", s)
|
|
|
|
}
|
2014-07-03 23:42:24 -07:00
|
|
|
|
2014-07-04 11:21:39 -07:00
|
|
|
if !bytes.Equal(b1, b2) {
|
|
|
|
t.Error("failed to convert", s, "to", b1, "got", b2)
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 23:42:24 -07:00
|
|
|
|
2014-07-04 11:21:39 -07:00
|
|
|
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
|
2014-11-19 13:01:52 -08:00
|
|
|
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")
|
2014-07-03 23:42:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesToString(t *testing.T) {
|
|
|
|
|
2014-07-04 11:21:39 -07:00
|
|
|
testString := func(s1 string, h string) {
|
|
|
|
b, err := hex.DecodeString(h)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to decode hex", h)
|
|
|
|
}
|
2014-07-03 23:42:24 -07:00
|
|
|
|
2014-09-11 10:47:56 -07:00
|
|
|
s2, err := bytesToString(b)
|
2014-07-04 11:21:39 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to convert", b)
|
|
|
|
}
|
2014-07-03 23:42:24 -07:00
|
|
|
|
2014-07-04 11:21:39 -07:00
|
|
|
if s1 != s2 {
|
|
|
|
t.Error("failed to convert", b, "to", s1, "got", s2)
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 23:42:24 -07:00
|
|
|
|
2014-07-04 11:21:39 -07:00
|
|
|
testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
|
2014-11-19 13:01:52 -08:00
|
|
|
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")
|
2014-07-03 23:42:24 -07:00
|
|
|
}
|
|
|
|
|
2014-11-05 02:20:27 -08:00
|
|
|
func TestBytesSplitAndJoin(t *testing.T) {
|
2014-11-04 23:20:17 -08:00
|
|
|
|
|
|
|
testString := func(s string, res []string) {
|
|
|
|
m, err := NewMultiaddr(s)
|
|
|
|
if err != nil {
|
2014-11-19 13:01:52 -08:00
|
|
|
t.Fatal("failed to convert", s, err)
|
2014-11-04 23:20:17 -08:00
|
|
|
}
|
|
|
|
|
2014-11-05 02:20:27 -08:00
|
|
|
split := Split(m)
|
2014-11-04 23:20:17 -08:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 02:20:27 -08:00
|
|
|
joined := Join(split...)
|
|
|
|
if !m.Equal(joined) {
|
|
|
|
t.Errorf("joined components failed: %s != %s", m, joined)
|
|
|
|
}
|
|
|
|
|
2014-11-04 23:20:17 -08:00
|
|
|
// 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"})
|
2014-11-19 13:01:52 -08:00
|
|
|
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"})
|
2014-11-04 23:20:17 -08:00
|
|
|
}
|
|
|
|
|
2014-07-04 00:12:05 -07:00
|
|
|
func TestProtocols(t *testing.T) {
|
2014-07-04 11:21:39 -07:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2014-10-06 03:27:29 -07:00
|
|
|
ps := m.Protocols()
|
2014-07-04 11:21:39 -07:00
|
|
|
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")
|
|
|
|
}
|
2014-07-04 00:12:05 -07:00
|
|
|
|
|
|
|
}
|
2014-07-04 00:48:33 -07:00
|
|
|
|
|
|
|
func TestEncapsulate(t *testing.T) {
|
2014-07-04 11:21:39 -07:00
|
|
|
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)
|
2014-10-06 02:38:56 -07:00
|
|
|
if s := b.String(); s != "/ip4/127.0.0.1/udp/1234/udp/5678" {
|
2014-07-04 11:21:39 -07:00
|
|
|
t.Error("encapsulate /ip4/127.0.0.1/udp/1234/udp/5678 failed.", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
m3, _ := NewMultiaddr("/udp/5678")
|
2014-10-06 03:27:29 -07:00
|
|
|
c := b.Decapsulate(m3)
|
2014-10-06 02:38:56 -07:00
|
|
|
if s := c.String(); s != "/ip4/127.0.0.1/udp/1234" {
|
2014-07-04 11:21:39 -07:00
|
|
|
t.Error("decapsulate /udp failed.", "/ip4/127.0.0.1/udp/1234", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
m4, _ := NewMultiaddr("/ip4/127.0.0.1")
|
2014-10-06 03:27:29 -07:00
|
|
|
d := c.Decapsulate(m4)
|
2014-10-06 02:38:56 -07:00
|
|
|
if s := d.String(); s != "" {
|
2014-07-04 11:21:39 -07:00
|
|
|
t.Error("decapsulate /ip4 failed.", "/", s)
|
|
|
|
}
|
2014-07-04 00:48:33 -07:00
|
|
|
}
|