dht_test.go 5.21 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
	identify "github.com/jbenet/go-ipfs/identify"
8
	peer "github.com/jbenet/go-ipfs/peer"
Jeromy's avatar
Jeromy committed
9
	swarm "github.com/jbenet/go-ipfs/swarm"
10
	u "github.com/jbenet/go-ipfs/util"
11
	ma "github.com/jbenet/go-multiaddr"
12

13
	"fmt"
14
	"time"
15 16
)

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

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

	return addrs, peers, dhts
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
func makePeer(addr *ma.Multiaddr) *peer.Peer {
	p := new(peer.Peer)
	p.AddAddress(addr)
	kp, err := identify.GenKeypair(256)
	if err != nil {
		panic(err)
	}
	p.PrivKey = kp.Priv
	p.PubKey = kp.Pub
	id, err := kp.ID()
	if err != nil {
		panic(err)
	}

	p.ID = id
	return p
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	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
203
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
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
	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
231
	addrs, _, dhts := setupDHTS(4, t)
232 233 234

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

	_, 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
248
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
249 250 251 252 253 254 255 256 257 258 259
	if err != nil {
		t.Fatal(err)
	}

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

	time.Sleep(time.Millisecond * 60)

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

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

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

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

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

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

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

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

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

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