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

12
	"fmt"
13
	"time"
14 15
)

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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])
		p.ID = peer.ID([]byte(fmt.Sprintf("peer_%d", i)))
		peers = append(peers, p)
	}

	var dhts []*IpfsDHT
	for i := 0; i < 4; i++ {
Jeromy's avatar
Jeromy committed
36 37
		net := swarm.NewSwarm(peers[i])
		err := net.Listen()
38 39 40
		if err != nil {
			t.Fatal(err)
		}
Jeromy's avatar
Jeromy committed
41
		d := NewDHT(peers[i], net, ds.NewMapDatastore())
42 43 44 45 46 47 48
		dhts = append(dhts, d)
		d.Start()
	}

	return addrs, peers, dhts
}

49 50
func TestPing(t *testing.T) {
	u.Debug = false
51
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
52 53 54
	if err != nil {
		t.Fatal(err)
	}
55
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
56 57 58 59
	if err != nil {
		t.Fatal(err)
	}

60 61 62
	peerA := new(peer.Peer)
	peerA.AddAddress(addrA)
	peerA.ID = peer.ID([]byte("peerA"))
63

64 65 66
	peerB := new(peer.Peer)
	peerB.AddAddress(addrB)
	peerB.ID = peer.ID([]byte("peerB"))
67

68
	neta := swarm.NewSwarm(peerA)
Jeromy's avatar
Jeromy committed
69
	err = neta.Listen()
70 71 72
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
73
	dhtA := NewDHT(peerA, neta, ds.NewMapDatastore())
74

75
	netb := swarm.NewSwarm(peerB)
Jeromy's avatar
Jeromy committed
76
	err = netb.Listen()
77 78 79
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
80
	dhtB := NewDHT(peerB, netb, ds.NewMapDatastore())
81

82 83
	dhtA.Start()
	dhtB.Start()
84

85
	_, err = dhtA.Connect(addrB)
86 87 88 89 90
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
91
	err = dhtA.Ping(peerB, time.Second*2)
92 93 94
	if err != nil {
		t.Fatal(err)
	}
95

96 97
	dhtA.Halt()
	dhtB.Halt()
98 99 100 101
}

func TestValueGetSet(t *testing.T) {
	u.Debug = false
102
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
103 104 105
	if err != nil {
		t.Fatal(err)
	}
106
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5679")
107 108 109 110
	if err != nil {
		t.Fatal(err)
	}

111 112 113
	peerA := new(peer.Peer)
	peerA.AddAddress(addrA)
	peerA.ID = peer.ID([]byte("peerA"))
114

115 116 117
	peerB := new(peer.Peer)
	peerB.AddAddress(addrB)
	peerB.ID = peer.ID([]byte("peerB"))
118

119
	neta := swarm.NewSwarm(peerA)
Jeromy's avatar
Jeromy committed
120
	err = neta.Listen()
121 122 123
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
124
	dhtA := NewDHT(peerA, neta, ds.NewMapDatastore())
125

126
	netb := swarm.NewSwarm(peerB)
Jeromy's avatar
Jeromy committed
127
	err = netb.Listen()
128 129 130
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
131
	dhtB := NewDHT(peerB, netb, ds.NewMapDatastore())
132

133 134
	dhtA.Start()
	dhtB.Start()
135

136 137
	errsa := dhtA.network.GetErrChan()
	errsb := dhtB.network.GetErrChan()
Jeromy's avatar
Jeromy committed
138 139
	go func() {
		select {
140
		case err := <-errsa:
Jeromy's avatar
Jeromy committed
141
			t.Fatal(err)
142
		case err := <-errsb:
Jeromy's avatar
Jeromy committed
143 144 145 146
			t.Fatal(err)
		}
	}()

147
	_, err = dhtA.Connect(addrB)
148 149 150 151
	if err != nil {
		t.Fatal(err)
	}

152
	dhtA.PutValue("hello", []byte("world"))
153

154
	val, err := dhtA.GetValue("hello", time.Second*2)
155 156 157 158 159
	if err != nil {
		t.Fatal(err)
	}

	if string(val) != "world" {
Jeromy's avatar
Jeromy committed
160
		t.Fatalf("Expected 'world' got '%s'", string(val))
161
	}
162
}
163 164 165

func TestProvides(t *testing.T) {
	u.Debug = false
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

	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
184
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	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
212
	addrs, _, dhts := setupDHTS(4, t)
213 214 215

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
216
		t.Fatalf("Failed to connect: %s", err)
217 218 219 220 221 222 223 224 225 226 227 228
	}

	_, 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
229
	err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
230 231 232 233 234 235 236 237 238 239 240
	if err != nil {
		t.Fatal(err)
	}

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

	time.Sleep(time.Millisecond * 60)

241
	val, err := dhts[0].GetValue(u.Key("hello"), time.Second)
242 243 244 245
	if err != nil {
		t.Fatal(err)
	}

246 247
	if string(val) != "world" {
		t.Fatal("Got incorrect value.")
248 249
	}

250 251 252 253
	for i := 0; i < 4; i++ {
		dhts[i].Halt()
	}
}
Jeromy's avatar
Jeromy committed
254

255
func TestFindPeer(t *testing.T) {
Jeromy's avatar
Jeromy committed
256 257
	u.Debug = false

258
	addrs, peers, dhts := setupDHTS(4, t)
Jeromy's avatar
Jeromy committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

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

275
	p, err := dhts[0].FindPeer(peers[2].ID, time.Second)
Jeromy's avatar
Jeromy committed
276 277 278 279
	if err != nil {
		t.Fatal(err)
	}

280 281
	if p == nil {
		t.Fatal("Failed to find peer.")
Jeromy's avatar
Jeromy committed
282 283
	}

284 285
	if !p.ID.Equal(peers[2].ID) {
		t.Fatal("Didnt find expected peer.")
Jeromy's avatar
Jeromy committed
286 287 288 289 290 291
	}

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