peer_test.go 1.28 KB
Newer Older
1 2 3
package peer

import (
4 5
	"testing"

6 7
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
)

func TestNetAddress(t *testing.T) {

	tcp, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
	if err != nil {
		t.Error(err)
		return
	}

	udp, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/2345")
	if err != nil {
		t.Error(err)
		return
	}

	mh, err := mh.FromHexString("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
	if err != nil {
		t.Error(err)
		return
	}

30
	p := WithID(ID(mh))
31 32
	p.AddAddress(tcp)
	p.AddAddress(udp)
33 34
	p.AddAddress(tcp)

35
	if len(p.Addresses()) == 3 {
36 37
		t.Error("added same address twice")
	}
38 39 40 41 42 43 44 45 46 47 48

	tcp2 := p.NetAddress("tcp")
	if tcp2 != tcp {
		t.Error("NetAddress lookup failed", tcp, tcp2)
	}

	udp2 := p.NetAddress("udp")
	if udp2 != udp {
		t.Error("NetAddress lookup failed", udp, udp2)
	}
}
49 50 51 52 53 54 55 56 57

func TestStringMethodWithSmallId(t *testing.T) {
	p := WithID([]byte(string(0)))
	p1, ok := p.(*peer)
	if !ok {
		t.Fatal("WithID doesn't return a peer")
	}
	p1.String()
}
58 59 60 61 62 63 64 65

func TestDefaultType(t *testing.T) {
	t.Log("Ensure that peers are initialized to Unspecified by default")
	p := peer{}
	if p.GetType() != Unspecified {
		t.Fatalf("Peer's default type is was not `Unspecified`")
	}
}