dht_test.go 5.57 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 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 43 44
func setupDHTS(n int, t *testing.T) ([]*ma.Multiaddr, []*peer.Peer, []*IpfsDHT) {
	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()
	}

	return addrs, peers, dhts
}

45 46
func TestPing(t *testing.T) {
	u.Debug = false
47
	addr_a, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
48 49 50
	if err != nil {
		t.Fatal(err)
	}
51
	addr_b, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
52 53 54 55 56 57 58 59 60 61 62 63
	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"))

64
	dht_a, err := NewDHT(peer_a)
65 66 67 68
	if err != nil {
		t.Fatal(err)
	}

69
	dht_b, err := NewDHT(peer_b)
70 71 72 73 74 75 76
	if err != nil {
		t.Fatal(err)
	}

	dht_a.Start()
	dht_b.Start()

77
	_, err = dht_a.Connect(addr_b)
78 79 80 81 82
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
83
	err = dht_a.Ping(peer_b, time.Second*2)
84 85 86
	if err != nil {
		t.Fatal(err)
	}
87 88 89 90 91 92 93

	dht_a.Halt()
	dht_b.Halt()
}

func TestValueGetSet(t *testing.T) {
	u.Debug = false
94
	addr_a, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
95 96 97
	if err != nil {
		t.Fatal(err)
	}
98
	addr_b, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5679")
99 100 101 102 103 104 105 106 107 108 109 110
	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"))

111
	dht_a, err := NewDHT(peer_a)
112 113 114 115
	if err != nil {
		t.Fatal(err)
	}

116
	dht_b, err := NewDHT(peer_b)
117 118 119 120 121 122 123
	if err != nil {
		t.Fatal(err)
	}

	dht_a.Start()
	dht_b.Start()

124 125
	errsa := dht_a.network.GetChan().Errors
	errsb := dht_b.network.GetChan().Errors
Jeromy's avatar
Jeromy committed
126 127
	go func() {
		select {
128
		case err := <-errsa:
Jeromy's avatar
Jeromy committed
129
			t.Fatal(err)
130
		case err := <-errsb:
Jeromy's avatar
Jeromy committed
131 132 133 134
			t.Fatal(err)
		}
	}()

135
	_, err = dht_a.Connect(addr_b)
136 137 138 139
	if err != nil {
		t.Fatal(err)
	}

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

142
	val, err := dht_a.GetValue("hello", time.Second*2)
143 144 145 146 147 148 149
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
		t.Fatalf("Expected 'world' got %s", string(val))
	}
150
}
151 152 153

func TestProvides(t *testing.T) {
	u.Debug = false
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

	addrs, _, dhts := setupDHTS(4, t)

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

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

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

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

func TestLayeredGet(t *testing.T) {
	u.Debug = false
200 201
	var addrs []*ma.Multiaddr
	for i := 0; i < 4; i++ {
202
		a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5000+i))
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
		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++ {
219
		d, err := NewDHT(peers[i])
220 221 222 223 224 225 226 227 228
		if err != nil {
			t.Fatal(err)
		}
		dhts = append(dhts, d)
		d.Start()
	}

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
229
		t.Fatalf("Failed to connect: %s", err)
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	}

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

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

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

263 264 265 266
	for i := 0; i < 4; i++ {
		dhts[i].Halt()
	}
}
Jeromy's avatar
Jeromy committed
267

268
func TestFindPeer(t *testing.T) {
Jeromy's avatar
Jeromy committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	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)
	}

312
	p, err := dhts[0].FindPeer(peers[2].ID, time.Second)
Jeromy's avatar
Jeromy committed
313 314 315 316
	if err != nil {
		t.Fatal(err)
	}

317 318
	if p == nil {
		t.Fatal("Failed to find peer.")
Jeromy's avatar
Jeromy committed
319 320
	}

321 322
	if !p.ID.Equal(peers[2].ID) {
		t.Fatal("Didnt find expected peer.")
Jeromy's avatar
Jeromy committed
323 324 325 326 327 328
	}

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