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

import (
	"testing"
5

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

8 9
	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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10

11
	ci "github.com/jbenet/go-ipfs/crypto"
12
	spipe "github.com/jbenet/go-ipfs/crypto/spipe"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 15
	inet "github.com/jbenet/go-ipfs/net"
	mux "github.com/jbenet/go-ipfs/net/mux"
	netservice "github.com/jbenet/go-ipfs/net/service"
16 17 18
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"

Siraj Ravel's avatar
Siraj Ravel committed
19
	"bytes"
20
	"fmt"
21
	"time"
22 23
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT {
	ctx := context.TODO()

	peerstore := peer.NewPeerstore()

	ctx, _ = context.WithCancel(ctx)
	dhts := netservice.NewService(nil) // nil handler for now, need to patch it
	if err := dhts.Start(ctx); err != nil {
		t.Fatal(err)
	}

	net, err := inet.NewIpfsNetwork(context.TODO(), p, &mux.ProtocolMap{
		mux.ProtocolID_Routing: dhts,
	})
	if err != nil {
		t.Fatal(err)
	}

	d := NewDHT(p, peerstore, net, dhts, ds.NewMapDatastore())
	dhts.Handler = d
	return d
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60
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])
61
		sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
62 63 64
		if err != nil {
			panic(err)
		}
65 66
		p.PubKey = pk
		p.PrivKey = sk
67
		id, err := spipe.IDFromPubKey(pk)
68 69 70 71
		if err != nil {
			panic(err)
		}
		p.ID = id
72 73 74 75 76
		peers = append(peers, p)
	}

	var dhts []*IpfsDHT
	for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
		dhts[i] = setupDHT(t, peers[i])
78 79 80 81 82
	}

	return addrs, peers, dhts
}

83 84 85
func makePeer(addr *ma.Multiaddr) *peer.Peer {
	p := new(peer.Peer)
	p.AddAddress(addr)
86
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
87 88 89
	if err != nil {
		panic(err)
	}
90 91
	p.PrivKey = sk
	p.PubKey = pk
92
	id, err := spipe.IDFromPubKey(pk)
93 94 95 96 97 98 99 100
	if err != nil {
		panic(err)
	}

	p.ID = id
	return p
}

101
func TestPing(t *testing.T) {
102
	u.Debug = true
103
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
104 105 106
	if err != nil {
		t.Fatal(err)
	}
107
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
108 109 110 111
	if err != nil {
		t.Fatal(err)
	}

112 113
	peerA := makePeer(addrA)
	peerB := makePeer(addrB)
114

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
115 116
	dhtA := setupDHT(t, peerA)
	dhtB := setupDHT(t, peerB)
117

Jeromy's avatar
Jeromy committed
118 119
	defer dhtA.Halt()
	defer dhtB.Halt()
120

121
	_, err = dhtA.Connect(addrB)
122 123 124 125 126
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
127
	err = dhtA.Ping(peerB, time.Second*2)
128 129 130
	if err != nil {
		t.Fatal(err)
	}
131 132 133 134
}

func TestValueGetSet(t *testing.T) {
	u.Debug = false
135
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
136 137 138
	if err != nil {
		t.Fatal(err)
	}
139
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5679")
140 141 142 143
	if err != nil {
		t.Fatal(err)
	}

144 145
	peerA := makePeer(addrA)
	peerB := makePeer(addrB)
146

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147 148
	dhtA := setupDHT(t, peerA)
	dhtB := setupDHT(t, peerB)
149

Jeromy's avatar
Jeromy committed
150 151
	defer dhtA.Halt()
	defer dhtB.Halt()
152

153
	_, err = dhtA.Connect(addrB)
154 155 156 157
	if err != nil {
		t.Fatal(err)
	}

158
	dhtA.PutValue("hello", []byte("world"))
159

160
	val, err := dhtA.GetValue("hello", time.Second*2)
161 162 163 164 165
	if err != nil {
		t.Fatal(err)
	}

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

169
}
170 171 172

func TestProvides(t *testing.T) {
	u.Debug = false
173 174

	addrs, _, dhts := setupDHTS(4, t)
Jeromy's avatar
Jeromy committed
175 176 177 178 179
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Halt()
		}
	}()
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

	_, 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
196
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
197 198 199 200
	if err != nil {
		t.Fatal(err)
	}

Siraj Ravel's avatar
Siraj Ravel committed
201 202
	bits, err := dhts[3].getLocal(u.Key("hello"))
	if err != nil && bytes.Equal(bits, []byte("world")) {
Siraj Ravel's avatar
Siraj Ravel committed
203 204 205
		t.Fatal(err)
	}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	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
225
	addrs, _, dhts := setupDHTS(4, t)
Jeromy's avatar
Jeromy committed
226 227 228 229 230
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Halt()
		}
	}()
231 232 233

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

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

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

	time.Sleep(time.Millisecond * 60)

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

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

268
}
Jeromy's avatar
Jeromy committed
269

270
func TestFindPeer(t *testing.T) {
Jeromy's avatar
Jeromy committed
271 272
	u.Debug = false

273
	addrs, peers, dhts := setupDHTS(4, t)
274
	defer func() {
Jeromy's avatar
Jeromy committed
275 276 277 278
		for i := 0; i < 4; i++ {
			dhts[i].Halt()
		}
	}()
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
	}
}