dht_test.go 4.87 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
	addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/2222")
51 52 53
	if err != nil {
		t.Fatal(err)
	}
54
	addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5678")
55 56 57 58
	if err != nil {
		t.Fatal(err)
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

135 136
	errsa := dhtA.network.GetChan().Errors
	errsb := dhtB.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 = dhtA.Connect(addrB)
147 148 149 150
	if err != nil {
		t.Fatal(err)
	}

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

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

	if string(val) != "world" {
Jeromy's avatar
Jeromy committed
159
		t.Fatalf("Expected 'world' got '%s'", string(val))
160
	}
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()
	}
}