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

import (
	"testing"
5

Jeromy's avatar
Jeromy committed
6
	ds "github.com/jbenet/datastore.go"
7
	ci "github.com/jbenet/go-ipfs/crypto"
8
	identify "github.com/jbenet/go-ipfs/identify"
9
	peer "github.com/jbenet/go-ipfs/peer"
Jeromy's avatar
Jeromy committed
10
	swarm "github.com/jbenet/go-ipfs/swarm"
11
	u "github.com/jbenet/go-ipfs/util"
12
	ma "github.com/jbenet/go-multiaddr"
13

14
	"fmt"
15
	"time"
16 17
)

18 19 20 21 22 23 24 25 26 27 28 29 30 31
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])
32
		sk, pk, err := ci.GenerateKeyPair(ci.RSA, 256)
33 34 35
		if err != nil {
			panic(err)
		}
36 37 38
		p.PubKey = pk
		p.PrivKey = sk
		id, err := identify.IdFromPubKey(pk)
39 40 41 42
		if err != nil {
			panic(err)
		}
		p.ID = id
43 44 45 46 47
		peers = append(peers, p)
	}

	var dhts []*IpfsDHT
	for i := 0; i < 4; i++ {
Jeromy's avatar
Jeromy committed
48 49
		net := swarm.NewSwarm(peers[i])
		err := net.Listen()
50 51 52
		if err != nil {
			t.Fatal(err)
		}
Jeromy's avatar
Jeromy committed
53
		d := NewDHT(peers[i], net, ds.NewMapDatastore())
54 55 56 57 58 59 60
		dhts = append(dhts, d)
		d.Start()
	}

	return addrs, peers, dhts
}

61 62 63
func makePeer(addr *ma.Multiaddr) *peer.Peer {
	p := new(peer.Peer)
	p.AddAddress(addr)
64
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 256)
65 66 67
	if err != nil {
		panic(err)
	}
68 69 70
	p.PrivKey = sk
	p.PubKey = pk
	id, err := identify.IdFromPubKey(pk)
71 72 73 74 75 76 77 78
	if err != nil {
		panic(err)
	}

	p.ID = id
	return p
}

79
func TestPing(t *testing.T) {
80
	u.Debug = true
81
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
82 83 84
	if err != nil {
		t.Fatal(err)
	}
85
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
86 87 88 89
	if err != nil {
		t.Fatal(err)
	}

90 91
	peerA := makePeer(addrA)
	peerB := makePeer(addrB)
92

93
	neta := swarm.NewSwarm(peerA)
Jeromy's avatar
Jeromy committed
94
	err = neta.Listen()
95 96 97
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
98
	dhtA := NewDHT(peerA, neta, ds.NewMapDatastore())
99

100
	netb := swarm.NewSwarm(peerB)
Jeromy's avatar
Jeromy committed
101
	err = netb.Listen()
102 103 104
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
105
	dhtB := NewDHT(peerB, netb, ds.NewMapDatastore())
106

107 108
	dhtA.Start()
	dhtB.Start()
109

110
	_, err = dhtA.Connect(addrB)
111 112 113 114 115
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
116
	err = dhtA.Ping(peerB, time.Second*2)
117 118 119
	if err != nil {
		t.Fatal(err)
	}
120

121 122
	dhtA.Halt()
	dhtB.Halt()
123 124 125 126
}

func TestValueGetSet(t *testing.T) {
	u.Debug = false
127
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
128 129 130
	if err != nil {
		t.Fatal(err)
	}
131
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5679")
132 133 134 135
	if err != nil {
		t.Fatal(err)
	}

136 137
	peerA := makePeer(addrA)
	peerB := makePeer(addrB)
138

139
	neta := swarm.NewSwarm(peerA)
Jeromy's avatar
Jeromy committed
140
	err = neta.Listen()
141 142 143
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
144
	dhtA := NewDHT(peerA, neta, ds.NewMapDatastore())
145

146
	netb := swarm.NewSwarm(peerB)
Jeromy's avatar
Jeromy committed
147
	err = netb.Listen()
148 149 150
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
151
	dhtB := NewDHT(peerB, netb, ds.NewMapDatastore())
152

153 154
	dhtA.Start()
	dhtB.Start()
155

156 157
	errsa := dhtA.network.GetErrChan()
	errsb := dhtB.network.GetErrChan()
Jeromy's avatar
Jeromy committed
158 159
	go func() {
		select {
160
		case err := <-errsa:
Jeromy's avatar
Jeromy committed
161
			t.Fatal(err)
162
		case err := <-errsb:
Jeromy's avatar
Jeromy committed
163 164 165 166
			t.Fatal(err)
		}
	}()

167
	_, err = dhtA.Connect(addrB)
168 169 170 171
	if err != nil {
		t.Fatal(err)
	}

172
	dhtA.PutValue("hello", []byte("world"))
173

174
	val, err := dhtA.GetValue("hello", time.Second*2)
175 176 177 178 179
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
Jeromy's avatar
Jeromy committed
180
		t.Fatalf("Expected 'world' got '%s'", string(val))
181
	}
182
}
183 184 185

func TestProvides(t *testing.T) {
	u.Debug = false
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

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

Chas Leichner's avatar
Chas Leichner committed
204
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
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
	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
232
	addrs, _, dhts := setupDHTS(4, t)
233 234 235

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
236
		t.Fatalf("Failed to connect: %s", err)
237 238 239 240 241 242 243 244 245 246 247 248
	}

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

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

Chas Leichner's avatar
Chas Leichner committed
249
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
250 251 252 253 254 255 256 257 258 259 260
	if err != nil {
		t.Fatal(err)
	}

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

	time.Sleep(time.Millisecond * 60)

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

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

270 271 272 273
	for i := 0; i < 4; i++ {
		dhts[i].Halt()
	}
}
Jeromy's avatar
Jeromy committed
274

275
func TestFindPeer(t *testing.T) {
Jeromy's avatar
Jeromy committed
276 277
	u.Debug = false

278
	addrs, peers, dhts := setupDHTS(4, t)
Jeromy's avatar
Jeromy committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

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

295
	p, err := dhts[0].FindPeer(peers[2].ID, time.Second)
Jeromy's avatar
Jeromy committed
296 297 298 299
	if err != nil {
		t.Fatal(err)
	}

300 301
	if p == nil {
		t.Fatal("Failed to find peer.")
Jeromy's avatar
Jeromy committed
302 303
	}

304 305
	if !p.ID.Equal(peers[2].ID) {
		t.Fatal("Didnt find expected peer.")
Jeromy's avatar
Jeromy committed
306 307 308 309 310 311
	}

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