dht_test.go 4.32 KB
Newer Older
1 2 3 4
package dht

import (
	"testing"
5

6 7
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
8
	ma "github.com/jbenet/go-multiaddr"
9

10
	"fmt"
11
	"time"
12 13 14 15
)

func TestPing(t *testing.T) {
	u.Debug = false
16
	addr_a, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
17 18 19
	if err != nil {
		t.Fatal(err)
	}
20
	addr_b, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
21 22 23 24 25 26 27 28 29 30 31 32
	if err != nil {
		t.Fatal(err)
	}

	peer_a := new(peer.Peer)
	peer_a.AddAddress(addr_a)
	peer_a.ID = peer.ID([]byte("peer_a"))

	peer_b := new(peer.Peer)
	peer_b.AddAddress(addr_b)
	peer_b.ID = peer.ID([]byte("peer_b"))

33
	dht_a, err := NewDHT(peer_a)
34 35 36 37
	if err != nil {
		t.Fatal(err)
	}

38
	dht_b, err := NewDHT(peer_b)
39 40 41 42 43 44 45
	if err != nil {
		t.Fatal(err)
	}

	dht_a.Start()
	dht_b.Start()

46
	_, err = dht_a.Connect(addr_b)
47 48 49 50 51
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
52
	err = dht_a.Ping(peer_b, time.Second*2)
53 54 55
	if err != nil {
		t.Fatal(err)
	}
56 57 58 59 60 61 62

	dht_a.Halt()
	dht_b.Halt()
}

func TestValueGetSet(t *testing.T) {
	u.Debug = false
63
	addr_a, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
64 65 66
	if err != nil {
		t.Fatal(err)
	}
67
	addr_b, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5679")
68 69 70 71 72 73 74 75 76 77 78 79
	if err != nil {
		t.Fatal(err)
	}

	peer_a := new(peer.Peer)
	peer_a.AddAddress(addr_a)
	peer_a.ID = peer.ID([]byte("peer_a"))

	peer_b := new(peer.Peer)
	peer_b.AddAddress(addr_b)
	peer_b.ID = peer.ID([]byte("peer_b"))

80
	dht_a, err := NewDHT(peer_a)
81 82 83 84
	if err != nil {
		t.Fatal(err)
	}

85
	dht_b, err := NewDHT(peer_b)
86 87 88 89 90 91 92
	if err != nil {
		t.Fatal(err)
	}

	dht_a.Start()
	dht_b.Start()

Jeromy's avatar
Jeromy committed
93 94 95 96 97 98 99 100 101
	go func() {
		select {
		case err := <-dht_a.network.Chan.Errors:
			t.Fatal(err)
		case err := <-dht_b.network.Chan.Errors:
			t.Fatal(err)
		}
	}()

102
	_, err = dht_a.Connect(addr_b)
103 104 105 106
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
107
	dht_a.PutValue("hello", []byte("world"))
108

109
	val, err := dht_a.GetValue("hello", time.Second*2)
110 111 112 113 114 115 116
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
		t.Fatalf("Expected 'world' got %s", string(val))
	}
117
}
118 119 120 121 122

func TestProvides(t *testing.T) {
	u.Debug = false
	var addrs []*ma.Multiaddr
	for i := 0; i < 4; i++ {
123
		a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i))
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		if err != nil {
			t.Fatal(err)
		}
		addrs = append(addrs, a)
	}

	var peers []*peer.Peer
	for i := 0; i < 4; i++ {
		p := new(peer.Peer)
		p.AddAddress(addrs[i])
		p.ID = peer.ID([]byte(fmt.Sprintf("peer_%d", i)))
		peers = append(peers, p)
	}

	var dhts []*IpfsDHT
	for i := 0; i < 4; i++ {
140
		d, err := NewDHT(peers[i])
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		if err != nil {
			t.Fatal(err)
		}
		dhts = append(dhts, d)
		d.Start()
	}

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
		t.Fatal(err)
	}

	_, err = dhts[1].Connect(addrs[2])
	if err != nil {
		t.Fatal(err)
	}

	_, err = dhts[1].Connect(addrs[3])
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[3].PutLocal(u.Key("hello"), []byte("world"))
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[3].Provide(u.Key("hello"))
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

175
	provs, err := dhts[0].FindProviders(u.Key("hello"), time.Second)
176 177 178 179 180 181 182 183
	if err != nil {
		t.Fatal(err)
	}

	if len(provs) != 1 {
		t.Fatal("Didnt get back providers")
	}

184 185 186 187
	for i := 0; i < 4; i++ {
		dhts[i].Halt()
	}
}
Jeromy's avatar
Jeromy committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

func TestLayeredGet(t *testing.T) {
	u.Debug = false
	var addrs []*ma.Multiaddr
	for i := 0; i < 4; i++ {
		a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i))
		if err != nil {
			t.Fatal(err)
		}
		addrs = append(addrs, a)
	}

	var peers []*peer.Peer
	for i := 0; i < 4; i++ {
		p := new(peer.Peer)
		p.AddAddress(addrs[i])
		p.ID = peer.ID([]byte(fmt.Sprintf("peer_%d", i)))
		peers = append(peers, p)
	}

	var dhts []*IpfsDHT
	for i := 0; i < 4; i++ {
		d, err := NewDHT(peers[i])
		if err != nil {
			t.Fatal(err)
		}
		dhts = append(dhts, d)
		d.Start()
	}

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
		t.Fatal(err)
	}

	_, err = dhts[1].Connect(addrs[2])
	if err != nil {
		t.Fatal(err)
	}

	_, err = dhts[1].Connect(addrs[3])
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[3].PutLocal(u.Key("hello"), []byte("world"))
	if err != nil {
		t.Fatal(err)
	}

	err = dhts[3].Provide(u.Key("hello"))
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

	val, err := dhts[0].GetValue(u.Key("hello"), time.Second)
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
		t.Fatal("Got incorrect value.")
	}

	for i := 0; i < 4; i++ {
		dhts[i].Halt()
	}
}