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

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, 512)
33 34 35
		if err != nil {
			panic(err)
		}
36 37
		p.PubKey = pk
		p.PrivKey = sk
Siraj Ravel's avatar
Siraj Ravel committed
38
		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, 512)
65 66 67
	if err != nil {
		panic(err)
	}
68 69
	p.PrivKey = sk
	p.PubKey = pk
Siraj Ravel's avatar
Siraj Ravel committed
70
	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
	}
Siraj Ravel's avatar
Siraj Ravel committed
182

183
}
184 185 186

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

	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
205
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
206 207 208 209
	if err != nil {
		t.Fatal(err)
	}

Siraj Ravel's avatar
Siraj Ravel committed
210 211 212 213 214
	_, err = dhts[3].getLocal(u.Key("hello"))
	if err != nil {
		t.Fatal(err)
	}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	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
238
	addrs, _, dhts := setupDHTS(4, t)
239 240 241

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
242
		t.Fatalf("Failed to connect: %s", err)
243 244 245 246 247 248 249 250 251 252 253 254
	}

	_, 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
255
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
256 257 258 259 260 261 262 263 264 265 266
	if err != nil {
		t.Fatal(err)
	}

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

	time.Sleep(time.Millisecond * 60)

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

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

276 277 278 279
	for i := 0; i < 4; i++ {
		dhts[i].Halt()
	}
}
Jeromy's avatar
Jeromy committed
280

281
func TestFindPeer(t *testing.T) {
Jeromy's avatar
Jeromy committed
282 283
	u.Debug = false

284
	addrs, peers, dhts := setupDHTS(4, t)
Jeromy's avatar
Jeromy committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

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

301
	p, err := dhts[0].FindPeer(peers[2].ID, time.Second)
Jeromy's avatar
Jeromy committed
302 303 304 305
	if err != nil {
		t.Fatal(err)
	}

306 307
	if p == nil {
		t.Fatal("Failed to find peer.")
Jeromy's avatar
Jeromy committed
308 309
	}

310 311
	if !p.ID.Equal(peers[2].ID) {
		t.Fatal("Didnt find expected peer.")
Jeromy's avatar
Jeromy committed
312 313 314 315 316 317
	}

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