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

import (
	"testing"
5

6
	peer "github.com/jbenet/go-ipfs/peer"
Jeromy's avatar
Jeromy committed
7
	swarm "github.com/jbenet/go-ipfs/swarm"
8
	u "github.com/jbenet/go-ipfs/util"
9
	ma "github.com/jbenet/go-multiaddr"
10

11
	"fmt"
12
	"time"
13 14
)

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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
35 36
		net := swarm.NewSwarm(peers[i])
		err := net.Listen()
37 38 39
		if err != nil {
			t.Fatal(err)
		}
Jeromy's avatar
Jeromy committed
40
		d := NewDHT(peers[i], net)
41 42 43 44 45 46 47
		dhts = append(dhts, d)
		d.Start()
	}

	return addrs, peers, dhts
}

48 49
func TestPing(t *testing.T) {
	u.Debug = false
50
	addr_a, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
51 52 53
	if err != nil {
		t.Fatal(err)
	}
54
	addr_b, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
55 56 57 58 59 60 61 62 63 64 65 66
	if err != nil {
		t.Fatal(err)
	}

	peer_a := new(peer.Peer)
	peer_a.AddAddress(addr_a)
	peer_a.ID = peer.ID([]byte("peer_a"))

	peer_b := new(peer.Peer)
	peer_b.AddAddress(addr_b)
	peer_b.ID = peer.ID([]byte("peer_b"))

Jeromy's avatar
Jeromy committed
67 68
	neta := swarm.NewSwarm(peer_a)
	err = neta.Listen()
69 70 71
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
72
	dht_a := NewDHT(peer_a, neta)
73

Jeromy's avatar
Jeromy committed
74 75
	netb := swarm.NewSwarm(peer_b)
	err = netb.Listen()
76 77 78
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
79
	dht_b := NewDHT(peer_b, netb)
80 81 82 83

	dht_a.Start()
	dht_b.Start()

84
	_, err = dht_a.Connect(addr_b)
85 86 87 88 89
	if err != nil {
		t.Fatal(err)
	}

	//Test that we can ping the node
90
	err = dht_a.Ping(peer_b, time.Second*2)
91 92 93
	if err != nil {
		t.Fatal(err)
	}
94 95 96 97 98 99 100

	dht_a.Halt()
	dht_b.Halt()
}

func TestValueGetSet(t *testing.T) {
	u.Debug = false
101
	addr_a, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1235")
102 103 104
	if err != nil {
		t.Fatal(err)
	}
105
	addr_b, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5679")
106 107 108 109 110 111 112 113 114 115 116 117
	if err != nil {
		t.Fatal(err)
	}

	peer_a := new(peer.Peer)
	peer_a.AddAddress(addr_a)
	peer_a.ID = peer.ID([]byte("peer_a"))

	peer_b := new(peer.Peer)
	peer_b.AddAddress(addr_b)
	peer_b.ID = peer.ID([]byte("peer_b"))

Jeromy's avatar
Jeromy committed
118 119
	neta := swarm.NewSwarm(peer_a)
	err = neta.Listen()
120 121 122
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
123
	dht_a := NewDHT(peer_a, neta)
124

Jeromy's avatar
Jeromy committed
125 126
	netb := swarm.NewSwarm(peer_b)
	err = netb.Listen()
127 128 129
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
130
	dht_b := NewDHT(peer_b, netb)
131 132 133 134

	dht_a.Start()
	dht_b.Start()

135 136
	errsa := dht_a.network.GetChan().Errors
	errsb := dht_b.network.GetChan().Errors
Jeromy's avatar
Jeromy committed
137 138
	go func() {
		select {
139
		case err := <-errsa:
Jeromy's avatar
Jeromy committed
140
			t.Fatal(err)
141
		case err := <-errsb:
Jeromy's avatar
Jeromy committed
142 143 144 145
			t.Fatal(err)
		}
	}()

146
	_, err = dht_a.Connect(addr_b)
147 148 149 150
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
151
	dht_a.PutValue("hello", []byte("world"))
152

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

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

func TestProvides(t *testing.T) {
	u.Debug = false
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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

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

	err = dhts[3].PutLocal(u.Key("hello"), []byte("world"))
	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
211
	addrs, _, dhts := setupDHTS(4, t)
212 213 214

	_, err := dhts[0].Connect(addrs[1])
	if err != nil {
215
		t.Fatalf("Failed to connect: %s", err)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	}

	_, err = dhts[1].Connect(addrs[2])
	if err != nil {
		t.Fatal(err)
	}

	_, err = dhts[1].Connect(addrs[3])
	if err != nil {
		t.Fatal(err)
	}

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

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

	time.Sleep(time.Millisecond * 60)

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

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

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

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

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

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

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

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

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

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