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

import (
	"testing"
5

6 7
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
8
	ci "github.com/jbenet/go-ipfs/crypto"
9
	spipe "github.com/jbenet/go-ipfs/crypto/spipe"
10
	swarm "github.com/jbenet/go-ipfs/net/swarm"
11 12 13
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"

Siraj Ravel's avatar
Siraj Ravel committed
14
	"bytes"
15
	"fmt"
16
	"time"
17 18
)

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

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

	return addrs, peers, dhts
}

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

	p.ID = id
	return p
}

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

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

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

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

108 109
	dhtA.Start()
	dhtB.Start()
Jeromy's avatar
Jeromy committed
110 111
	defer dhtA.Halt()
	defer dhtB.Halt()
112

113
	_, err = dhtA.Connect(addrB)
114 115 116 117 118
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
119
	err = dhtA.Ping(peerB, time.Second*2)
120 121 122
	if err != nil {
		t.Fatal(err)
	}
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()
Jeromy's avatar
Jeromy committed
155 156
	defer dhtA.Halt()
	defer dhtB.Halt()
157

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

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

174
	dhtA.PutValue("hello", []byte("world"))
175

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

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

185
}
186 187 188

func TestProvides(t *testing.T) {
	u.Debug = false
189 190

	addrs, _, dhts := setupDHTS(4, t)
Jeromy's avatar
Jeromy committed
191 192 193 194 195
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Halt()
		}
	}()
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

	_, 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
212
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
213 214 215 216
	if err != nil {
		t.Fatal(err)
	}

Siraj Ravel's avatar
Siraj Ravel committed
217 218
	bits, err := dhts[3].getLocal(u.Key("hello"))
	if err != nil && bytes.Equal(bits, []byte("world")) {
Siraj Ravel's avatar
Siraj Ravel committed
219 220 221
		t.Fatal(err)
	}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	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")
	}
}

func TestLayeredGet(t *testing.T) {
	u.Debug = false
241
	addrs, _, dhts := setupDHTS(4, t)
Jeromy's avatar
Jeromy committed
242 243 244 245 246
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Halt()
		}
	}()
247 248 249

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
250
		t.Fatalf("Failed to connect: %s", err)
251 252 253 254 255 256 257 258 259 260 261 262
	}

	_, 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
263
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
264 265 266 267 268 269 270 271 272 273 274
	if err != nil {
		t.Fatal(err)
	}

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

	time.Sleep(time.Millisecond * 60)

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

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

284
}
Jeromy's avatar
Jeromy committed
285

286
func TestFindPeer(t *testing.T) {
Jeromy's avatar
Jeromy committed
287 288
	u.Debug = false

289
	addrs, peers, dhts := setupDHTS(4, t)
290
	defer func() {
Jeromy's avatar
Jeromy committed
291 292 293 294
		for i := 0; i < 4; i++ {
			dhts[i].Halt()
		}
	}()
Jeromy's avatar
Jeromy committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

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

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

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

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