dht_test.go 51.2 KB
Newer Older
1 2
package dht

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3
import (
4
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"context"
6
	"encoding/binary"
7
	"errors"
8
	"fmt"
9
	"math/rand"
10
	"sort"
11
	"strings"
12
	"sync"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
	"testing"
14
	"time"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15

16 17
	"github.com/libp2p/go-libp2p-core/event"
	"github.com/libp2p/go-libp2p-core/network"
18 19 20
	"github.com/libp2p/go-libp2p-core/peer"
	"github.com/libp2p/go-libp2p-core/peerstore"
	"github.com/libp2p/go-libp2p-core/routing"
21

Will Scott's avatar
Will Scott committed
22
	test "github.com/libp2p/go-libp2p-kad-dht/internal/testing"
Adin Schmahmann's avatar
go fmt  
Adin Schmahmann committed
23
	pb "github.com/libp2p/go-libp2p-kad-dht/pb"
24
	kb "github.com/libp2p/go-libp2p-kbucket"
25
	record "github.com/libp2p/go-libp2p-record"
Steven Allen's avatar
Steven Allen committed
26
	swarmt "github.com/libp2p/go-libp2p-swarm/testing"
Jeromy's avatar
Jeromy committed
27
	bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
28

29
	"github.com/ipfs/go-cid"
30
	detectrace "github.com/ipfs/go-detect-race"
31 32 33 34 35 36 37
	u "github.com/ipfs/go-ipfs-util"
	ma "github.com/multiformats/go-multiaddr"
	"github.com/multiformats/go-multihash"
	"github.com/multiformats/go-multistream"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39
)

40
var testCaseCids []cid.Cid
41 42 43 44

func init() {
	for i := 0; i < 100; i++ {
		v := fmt.Sprintf("%d -- value", i)
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		var newCid cid.Cid
		switch i % 3 {
		case 0:
			mhv := u.Hash([]byte(v))
			newCid = cid.NewCidV0(mhv)
		case 1:
			mhv := u.Hash([]byte(v))
			newCid = cid.NewCidV1(cid.DagCBOR, mhv)
		case 2:
			rawMh := make([]byte, 12)
			binary.PutUvarint(rawMh, cid.Raw)
			binary.PutUvarint(rawMh[1:], 10)
			copy(rawMh[2:], []byte(v)[:10])
			_, mhv, err := multihash.MHFromBytes(rawMh)
			if err != nil {
				panic(err)
			}
			newCid = cid.NewCidV1(cid.Raw, mhv)
		}
		testCaseCids = append(testCaseCids, newCid)
66 67 68
	}
}

69 70 71 72 73
type blankValidator struct{}

func (blankValidator) Validate(_ string, _ []byte) error        { return nil }
func (blankValidator) Select(_ string, _ [][]byte) (int, error) { return 0, nil }

74
type testAtomicPutValidator struct {
Will Scott's avatar
Will Scott committed
75
	test.TestValidator
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}

// selects the entry with the 'highest' last byte
func (testAtomicPutValidator) Select(_ string, bs [][]byte) (int, error) {
	index := -1
	max := uint8(0)
	for i, b := range bs {
		if bytes.Equal(b, []byte("valid")) {
			if index == -1 {
				index = i
			}
			continue
		}

		str := string(b)
		n := str[len(str)-1]
		if n > max {
			max = n
			index = i
		}

	}
	if index == -1 {
		return -1, errors.New("no rec found")
	}
	return index, nil
}

Adin Schmahmann's avatar
Adin Schmahmann committed
104 105
var testPrefix = ProtocolPrefix("/test")

106 107
func setupDHT(ctx context.Context, t *testing.T, client bool, options ...Option) *IpfsDHT {
	baseOpts := []Option{
Adin Schmahmann's avatar
Adin Schmahmann committed
108
		testPrefix,
109 110
		NamespacedValidator("v", blankValidator{}),
		DisableAutoRefresh(),
111 112 113
	}

	if client {
114
		baseOpts = append(baseOpts, Mode(ModeClient))
115
	} else {
116
		baseOpts = append(baseOpts, Mode(ModeServer))
117 118
	}

119 120
	d, err := New(
		ctx,
Steven Allen's avatar
Steven Allen committed
121
		bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)),
122
		append(baseOpts, options...)...,
123 124 125
	)
	if err != nil {
		t.Fatal(err)
126
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127 128 129
	return d
}

Steven Allen's avatar
Steven Allen committed
130
func setupDHTS(t *testing.T, ctx context.Context, n int, options ...Option) []*IpfsDHT {
131
	addrs := make([]ma.Multiaddr, n)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
	dhts := make([]*IpfsDHT, n)
133 134
	peers := make([]peer.ID, n)

135 136 137
	sanityAddrsMap := make(map[string]struct{})
	sanityPeersMap := make(map[string]struct{})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
138
	for i := 0; i < n; i++ {
Steven Allen's avatar
Steven Allen committed
139
		dhts[i] = setupDHT(ctx, t, false, options...)
140 141
		peers[i] = dhts[i].PeerID()
		addrs[i] = dhts[i].host.Addrs()[0]
142 143

		if _, lol := sanityAddrsMap[addrs[i].String()]; lol {
Jakub Sztandera's avatar
Jakub Sztandera committed
144
			t.Fatal("While setting up DHTs address got duplicated.")
145 146 147 148
		} else {
			sanityAddrsMap[addrs[i].String()] = struct{}{}
		}
		if _, lol := sanityPeersMap[peers[i].String()]; lol {
Jakub Sztandera's avatar
Jakub Sztandera committed
149
			t.Fatal("While setting up DHTs peerid got duplicated.")
150 151 152
		} else {
			sanityPeersMap[peers[i].String()] = struct{}{}
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
153 154
	}

155
	return dhts
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156 157
}

158
func connectNoSync(t *testing.T, ctx context.Context, a, b *IpfsDHT) {
159 160
	t.Helper()

161
	idB := b.self
162
	addrB := b.peerstore.Addrs(idB)
163 164
	if len(addrB) == 0 {
		t.Fatal("peers setup incorrectly: no local address")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165
	}
166

167 168
	a.peerstore.AddAddrs(idB, addrB, peerstore.TempAddrTTL)
	pi := peer.AddrInfo{ID: idB}
Jeromy's avatar
Jeromy committed
169
	if err := a.host.Connect(ctx, pi); err != nil {
170
		t.Fatal(err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171
	}
172 173
}

174 175
func wait(t *testing.T, ctx context.Context, a, b *IpfsDHT) {
	t.Helper()
176

177 178
	// loop until connection notification has been received.
	// under high load, this may not happen as immediately as we would like.
179
	for a.routingTable.Find(b.self) == "" {
180 181 182 183 184
		select {
		case <-ctx.Done():
			t.Fatal(ctx.Err())
		case <-time.After(time.Millisecond * 5):
		}
185
	}
186
}
187

188 189 190 191 192
func connect(t *testing.T, ctx context.Context, a, b *IpfsDHT) {
	t.Helper()
	connectNoSync(t, ctx, a, b)
	wait(t, ctx, a, b)
	wait(t, ctx, b, a)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
193 194
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
195
func bootstrap(t *testing.T, ctx context.Context, dhts []*IpfsDHT) {
196
	ctx, cancel := context.WithCancel(ctx)
Steven Allen's avatar
Steven Allen committed
197 198
	defer cancel()

199
	logger.Debugf("refreshing DHTs routing tables...")
200 201 202 203 204 205 206 207 208

	// tried async. sequential fares much better. compare:
	// 100 async https://gist.github.com/jbenet/56d12f0578d5f34810b2
	// 100 sync https://gist.github.com/jbenet/6c59e7c15426e48aaedd
	// probably because results compound

	start := rand.Intn(len(dhts)) // randomize to decrease bias.
	for i := range dhts {
		dht := dhts[(start+i)%len(dhts)]
209 210 211 212 213 214 215 216
		select {
		case err := <-dht.RefreshRoutingTable():
			if err != nil {
				t.Error(err)
			}
		case <-ctx.Done():
			return
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
217 218 219
	}
}

220 221
// Check to make sure we always signal the RefreshRoutingTable channel.
func TestRefreshMultiple(t *testing.T) {
Adin Schmahmann's avatar
Adin Schmahmann committed
222 223
	// TODO: What's with this test? How long should it take and why does RefreshRoutingTable not take a context?
	ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	defer cancel()

	dhts := setupDHTS(t, ctx, 5)
	defer func() {
		for _, dht := range dhts {
			dht.Close()
			defer dht.host.Close()
		}
	}()

	for _, dht := range dhts[1:] {
		connect(t, ctx, dhts[0], dht)
	}

	a := dhts[0].RefreshRoutingTable()
	time.Sleep(time.Nanosecond)
	b := dhts[0].RefreshRoutingTable()
	time.Sleep(time.Nanosecond)
	c := dhts[0].RefreshRoutingTable()

	// make sure that all of these eventually return
	select {
	case <-a:
	case <-ctx.Done():
		t.Fatal("first channel didn't signal")
	}
	select {
	case <-b:
	case <-ctx.Done():
		t.Fatal("second channel didn't signal")
	}
	select {
	case <-c:
	case <-ctx.Done():
		t.Fatal("third channel didn't signal")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
262
func TestValueGetSet(t *testing.T) {
263 264
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
265

Steven Allen's avatar
Steven Allen committed
266
	var dhts [5]*IpfsDHT
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
267

Steven Allen's avatar
Steven Allen committed
268 269 270 271 272
	for i := range dhts {
		dhts[i] = setupDHT(ctx, t, false)
		defer dhts[i].Close()
		defer dhts[i].host.Close()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
273

Steven Allen's avatar
Steven Allen committed
274
	connect(t, ctx, dhts[0], dhts[1])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
275

Steven Allen's avatar
Steven Allen committed
276
	t.Log("adding value on: ", dhts[0].self)
Jeromy's avatar
Jeromy committed
277 278
	ctxT, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
Steven Allen's avatar
Steven Allen committed
279
	err := dhts[0].PutValue(ctxT, "/v/hello", []byte("world"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
280 281 282 283
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
284
	t.Log("requesting value on dhts: ", dhts[1].self)
Adin Schmahmann's avatar
Adin Schmahmann committed
285
	ctxT, cancel = context.WithTimeout(ctx, time.Second*2*60)
Jeromy's avatar
Jeromy committed
286
	defer cancel()
Steven Allen's avatar
Steven Allen committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

	val, err := dhts[1].GetValue(ctxT, "/v/hello")
	if err != nil {
		t.Fatal(err)
	}

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

	// late connect

	connect(t, ctx, dhts[2], dhts[0])
	connect(t, ctx, dhts[2], dhts[1])

	t.Log("requesting value (offline) on dhts: ", dhts[2].self)
	vala, err := dhts[2].GetValue(ctxT, "/v/hello", Quorum(0))
Adin Schmahmann's avatar
Adin Schmahmann committed
304 305
	if err != nil {
		t.Fatal(err)
Steven Allen's avatar
Steven Allen committed
306 307
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
308 309 310
	if string(vala) != "world" {
		t.Fatalf("Expected 'world' got '%s'", string(vala))
	}
Steven Allen's avatar
Steven Allen committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	t.Log("requesting value (online) on dhts: ", dhts[2].self)
	val, err = dhts[2].GetValue(ctxT, "/v/hello")
	if err != nil {
		t.Fatal(err)
	}

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

	for _, d := range dhts[:3] {
		connect(t, ctx, dhts[3], d)
	}
	connect(t, ctx, dhts[4], dhts[3])

	t.Log("requesting value (requires peer routing) on dhts: ", dhts[4].self)
	val, err = dhts[4].GetValue(ctxT, "/v/hello")
328 329 330 331
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
332 333
	if string(val) != "world" {
		t.Fatalf("Expected 'world' got '%s'", string(val))
334
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
335 336
}

337 338 339 340 341 342 343 344 345 346 347 348
func TestValueSetInvalid(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	dhtA := setupDHT(ctx, t, false)
	dhtB := setupDHT(ctx, t, false)

	defer dhtA.Close()
	defer dhtB.Close()
	defer dhtA.host.Close()
	defer dhtB.host.Close()

Will Scott's avatar
Will Scott committed
349
	dhtA.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	dhtB.Validator.(record.NamespacedValidator)["v"] = blankValidator{}

	connect(t, ctx, dhtA, dhtB)

	testSetGet := func(val string, failset bool, exp string, experr error) {
		t.Helper()

		ctxT, cancel := context.WithTimeout(ctx, time.Second)
		defer cancel()
		err := dhtA.PutValue(ctxT, "/v/hello", []byte(val))
		if failset {
			if err == nil {
				t.Error("expected set to fail")
			}
		} else {
			if err != nil {
				t.Error(err)
			}
		}

		ctxT, cancel = context.WithTimeout(ctx, time.Second*2)
		defer cancel()
		valb, err := dhtB.GetValue(ctxT, "/v/hello")
		if err != experr {
			t.Errorf("Set/Get %v: Expected %v error but got %v", val, experr, err)
		} else if err == nil && string(valb) != exp {
			t.Errorf("Expected '%v' got '%s'", exp, string(valb))
		}
	}

	// Expired records should not be set
	testSetGet("expired", true, "", routing.ErrNotFound)
	// Valid record should be returned
	testSetGet("valid", false, "valid", nil)
	// Newer record should supersede previous record
	testSetGet("newer", false, "newer", nil)
	// Attempt to set older record again should be ignored
	testSetGet("valid", true, "newer", nil)
}

Aarsh Shah's avatar
Aarsh Shah committed
390
func TestContextShutDown(t *testing.T) {
Marten Seemann's avatar
Marten Seemann committed
391
	t.Skip("This test is flaky, see https://github.com/libp2p/go-libp2p-kad-dht/issues/724.")
Aarsh Shah's avatar
Aarsh Shah committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	dht := setupDHT(ctx, t, false)

	// context is alive
	select {
	case <-dht.Context().Done():
		t.Fatal("context should not be done")
	default:
	}

	// shut down dht
	require.NoError(t, dht.Close())

	// now context should be done
	select {
	case <-dht.Context().Done():
	default:
		t.Fatal("context should be done")
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
415
func TestSearchValue(t *testing.T) {
Marten Seemann's avatar
Marten Seemann committed
416 417
	t.Skip("This test is flaky, see https://github.com/libp2p/go-libp2p-kad-dht/issues/723.")

Łukasz Magiera's avatar
Łukasz Magiera committed
418 419 420 421 422 423 424 425 426 427 428 429 430
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	dhtA := setupDHT(ctx, t, false)
	dhtB := setupDHT(ctx, t, false)

	defer dhtA.Close()
	defer dhtB.Close()
	defer dhtA.host.Close()
	defer dhtB.host.Close()

	connect(t, ctx, dhtA, dhtB)

Will Scott's avatar
Will Scott committed
431 432
	dhtA.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
	dhtB.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
Łukasz Magiera's avatar
Łukasz Magiera committed
433 434 435 436 437 438 439 440 441 442 443

	ctxT, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()

	err := dhtA.PutValue(ctxT, "/v/hello", []byte("valid"))
	if err != nil {
		t.Error(err)
	}

	ctxT, cancel = context.WithTimeout(ctx, time.Second*2)
	defer cancel()
444
	valCh, err := dhtA.SearchValue(ctxT, "/v/hello", Quorum(0))
445 446 447
	if err != nil {
		t.Fatal(err)
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

	select {
	case v := <-valCh:
		if string(v) != "valid" {
			t.Errorf("expected 'valid', got '%s'", string(v))
		}
	case <-ctxT.Done():
		t.Fatal(ctxT.Err())
	}

	err = dhtB.PutValue(ctxT, "/v/hello", []byte("newer"))
	if err != nil {
		t.Error(err)
	}

	select {
	case v := <-valCh:
		if string(v) != "newer" {
			t.Errorf("expected 'newer', got '%s'", string(v))
		}
	case <-ctxT.Done():
		t.Fatal(ctxT.Err())
	}
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
func TestGetValues(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	dhtA := setupDHT(ctx, t, false)
	dhtB := setupDHT(ctx, t, false)

	defer dhtA.Close()
	defer dhtB.Close()
	defer dhtA.host.Close()
	defer dhtB.host.Close()

	connect(t, ctx, dhtA, dhtB)

	ctxT, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()

	err := dhtB.PutValue(ctxT, "/v/hello", []byte("newer"))
	if err != nil {
		t.Error(err)
	}

	err = dhtA.PutValue(ctxT, "/v/hello", []byte("valid"))
	if err != nil {
		t.Error(err)
	}

	ctxT, cancel = context.WithTimeout(ctx, time.Second*2)
	defer cancel()
	vals, err := dhtA.GetValues(ctxT, "/v/hello", 16)
	if err != nil {
		t.Fatal(err)
	}

	if len(vals) != 2 {
		t.Fatalf("expected to get 2 values, got %d", len(vals))
	}

511
	sort.Slice(vals, func(i, j int) bool { return string(vals[i].Val) < string(vals[j].Val) })
512 513 514 515 516 517 518 519 520

	if string(vals[0].Val) != "valid" {
		t.Errorf("unexpected vals[0]: %s", string(vals[0].Val))
	}
	if string(vals[1].Val) != "valid" {
		t.Errorf("unexpected vals[1]: %s", string(vals[1].Val))
	}
}

521 522 523 524 525 526 527 528 529 530 531 532 533
func TestValueGetInvalid(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	dhtA := setupDHT(ctx, t, false)
	dhtB := setupDHT(ctx, t, false)

	defer dhtA.Close()
	defer dhtB.Close()
	defer dhtA.host.Close()
	defer dhtB.host.Close()

	dhtA.Validator.(record.NamespacedValidator)["v"] = blankValidator{}
Will Scott's avatar
Will Scott committed
534
	dhtB.Validator.(record.NamespacedValidator)["v"] = test.TestValidator{}
535 536 537 538

	connect(t, ctx, dhtA, dhtB)

	testSetGet := func(val string, exp string, experr error) {
539 540
		t.Helper()

541 542 543 544
		ctxT, cancel := context.WithTimeout(ctx, time.Second)
		defer cancel()
		err := dhtA.PutValue(ctxT, "/v/hello", []byte(val))
		if err != nil {
545
			t.Error(err)
546 547 548 549 550 551
		}

		ctxT, cancel = context.WithTimeout(ctx, time.Second*2)
		defer cancel()
		valb, err := dhtB.GetValue(ctxT, "/v/hello")
		if err != experr {
Łukasz Magiera's avatar
Łukasz Magiera committed
552
			t.Errorf("Set/Get %v: Expected '%v' error but got '%v'", val, experr, err)
553 554
		} else if err == nil && string(valb) != exp {
			t.Errorf("Expected '%v' got '%s'", exp, string(valb))
555 556 557 558 559 560 561 562 563 564 565 566 567
		}
	}

	// Expired records should not be returned
	testSetGet("expired", "", routing.ErrNotFound)
	// Valid record should be returned
	testSetGet("valid", "valid", nil)
	// Newer record should supersede previous record
	testSetGet("newer", "newer", nil)
	// Attempt to set older record again should be ignored
	testSetGet("valid", "newer", nil)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
568 569
func TestProvides(t *testing.T) {
	// t.Skip("skipping test to debug another")
Steven Allen's avatar
Steven Allen committed
570 571
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
572

573
	dhts := setupDHTS(t, ctx, 4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
574 575
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
576
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
577
			defer dhts[i].host.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
578 579 580
		}
	}()

581 582 583
	connect(t, ctx, dhts[0], dhts[1])
	connect(t, ctx, dhts[1], dhts[2])
	connect(t, ctx, dhts[1], dhts[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
584

585
	for _, k := range testCaseCids {
Matt Joiner's avatar
Matt Joiner committed
586
		logger.Debugf("announcing provider for %s", k)
Jeromy's avatar
Jeromy committed
587
		if err := dhts[3].Provide(ctx, k, true); err != nil {
588 589
			t.Fatal(err)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
590 591
	}

592 593 594 595
	// what is this timeout for? was 60ms before.
	time.Sleep(time.Millisecond * 6)

	n := 0
596
	for _, c := range testCaseCids {
597 598
		n = (n + 1) % 3

Matt Joiner's avatar
Matt Joiner committed
599
		logger.Debugf("getting providers for %s from %d", c, n)
Jeromy's avatar
Jeromy committed
600 601
		ctxT, cancel := context.WithTimeout(ctx, time.Second)
		defer cancel()
602
		provchan := dhts[n].FindProvidersAsync(ctxT, c, 1)
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

		select {
		case prov := <-provchan:
			if prov.ID == "" {
				t.Fatal("Got back nil provider")
			}
			if prov.ID != dhts[3].self {
				t.Fatal("Got back wrong provider")
			}
		case <-ctxT.Done():
			t.Fatal("Did not get a provider back.")
		}
	}
}

Jeromy's avatar
Jeromy committed
618 619
func TestLocalProvides(t *testing.T) {
	// t.Skip("skipping test to debug another")
Steven Allen's avatar
Steven Allen committed
620 621
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Jeromy's avatar
Jeromy committed
622

623
	dhts := setupDHTS(t, ctx, 4)
Jeromy's avatar
Jeromy committed
624 625 626 627 628 629 630 631 632 633 634 635
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Close()
			defer dhts[i].host.Close()
		}
	}()

	connect(t, ctx, dhts[0], dhts[1])
	connect(t, ctx, dhts[1], dhts[2])
	connect(t, ctx, dhts[1], dhts[3])

	for _, k := range testCaseCids {
Matt Joiner's avatar
Matt Joiner committed
636
		logger.Debugf("announcing provider for %s", k)
Jeromy's avatar
Jeromy committed
637 638 639 640 641 642 643 644 645
		if err := dhts[3].Provide(ctx, k, false); err != nil {
			t.Fatal(err)
		}
	}

	time.Sleep(time.Millisecond * 10)

	for _, c := range testCaseCids {
		for i := 0; i < 3; i++ {
646
			provs := dhts[i].ProviderManager.GetProviders(ctx, c.Hash())
Jeromy's avatar
Jeromy committed
647 648 649 650 651 652 653
			if len(provs) > 0 {
				t.Fatal("shouldnt know this")
			}
		}
	}
}

654
// if minPeers or avgPeers is 0, dont test for it.
Steven Allen's avatar
Steven Allen committed
655
func waitForWellFormedTables(t *testing.T, dhts []*IpfsDHT, minPeers, avgPeers int, timeout time.Duration) {
656
	// test "well-formed-ness" (>= minPeers peers in every routing table)
Steven Allen's avatar
Steven Allen committed
657
	t.Helper()
658 659 660 661 662 663 664

	checkTables := func() bool {
		totalPeers := 0
		for _, dht := range dhts {
			rtlen := dht.routingTable.Size()
			totalPeers += rtlen
			if minPeers > 0 && rtlen < minPeers {
665
				//t.Logf("routing table for %s only has %d peers (should have >%d)", dht.self, rtlen, minPeers)
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
				return false
			}
		}
		actualAvgPeers := totalPeers / len(dhts)
		t.Logf("avg rt size: %d", actualAvgPeers)
		if avgPeers > 0 && actualAvgPeers < avgPeers {
			t.Logf("avg rt size: %d < %d", actualAvgPeers, avgPeers)
			return false
		}
		return true
	}

	timeoutA := time.After(timeout)
	for {
		select {
		case <-timeoutA:
Steven Allen's avatar
Steven Allen committed
682 683
			t.Errorf("failed to reach well-formed routing tables after %s", timeout)
			return
684 685
		case <-time.After(5 * time.Millisecond):
			if checkTables() {
Steven Allen's avatar
Steven Allen committed
686 687
				// succeeded
				return
688 689 690 691 692 693 694
			}
		}
	}
}

func printRoutingTables(dhts []*IpfsDHT) {
	// the routing tables should be full now. let's inspect them.
695
	fmt.Printf("checking routing table of %d\n", len(dhts))
696 697 698 699 700 701 702
	for _, dht := range dhts {
		fmt.Printf("checking routing table of %s\n", dht.self)
		dht.routingTable.Print()
		fmt.Println("")
	}
}

703
func TestRefresh(t *testing.T) {
704 705 706 707
	if testing.Short() {
		t.SkipNow()
	}

Steven Allen's avatar
Steven Allen committed
708 709
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
710

711
	nDHTs := 30
712
	dhts := setupDHTS(t, ctx, nDHTs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
713 714 715
	defer func() {
		for i := 0; i < nDHTs; i++ {
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
716
			defer dhts[i].host.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
717 718 719 720 721 722 723 724
		}
	}()

	t.Logf("connecting %d dhts in a ring", nDHTs)
	for i := 0; i < nDHTs; i++ {
		connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)])
	}

725
	<-time.After(100 * time.Millisecond)
726
	// bootstrap a few times until we get good tables.
Steven Allen's avatar
Steven Allen committed
727 728 729 730
	t.Logf("bootstrapping them so they find each other %d", nDHTs)
	ctxT, cancelT := context.WithTimeout(ctx, 5*time.Second)
	defer cancelT()

Aarsh Shah's avatar
Aarsh Shah committed
731 732 733 734 735 736 737 738 739
	for ctxT.Err() == nil {
		bootstrap(t, ctxT, dhts)

		// wait a bit.
		select {
		case <-time.After(50 * time.Millisecond):
			continue // being explicit
		case <-ctxT.Done():
			return
740
		}
Aarsh Shah's avatar
Aarsh Shah committed
741
	}
742

Aarsh Shah's avatar
Aarsh Shah committed
743
	waitForWellFormedTables(t, dhts, 7, 10, 10*time.Second)
Steven Allen's avatar
Steven Allen committed
744

Steven Allen's avatar
Steven Allen committed
745
	cancelT()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
746

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
747 748
	if u.Debug {
		// the routing tables should be full now. let's inspect them.
749 750 751 752
		printRoutingTables(dhts)
	}
}

753
func TestRefreshBelowMinRTThreshold(t *testing.T) {
Aarsh Shah's avatar
Aarsh Shah committed
754
	ctx := context.Background()
755 756 757 758 759

	// enable auto bootstrap on A
	dhtA, err := New(
		ctx,
		bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)),
Adin Schmahmann's avatar
Adin Schmahmann committed
760
		testPrefix,
761 762
		Mode(ModeServer),
		NamespacedValidator("v", blankValidator{}),
763 764 765 766 767
	)
	if err != nil {
		t.Fatal(err)
	}

Aarsh Shah's avatar
Aarsh Shah committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	dhtB := setupDHT(ctx, t, false)
	dhtC := setupDHT(ctx, t, false)

	defer func() {
		dhtA.Close()
		dhtA.host.Close()

		dhtB.Close()
		dhtB.host.Close()

		dhtC.Close()
		dhtC.host.Close()
	}()

	connect(t, ctx, dhtA, dhtB)
	connect(t, ctx, dhtB, dhtC)

	// we ONLY init bootstrap on A
786
	dhtA.RefreshRoutingTable()
Aarsh Shah's avatar
Aarsh Shah committed
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	// and wait for one round to complete i.e. A should be connected to both B & C
	waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 2, 2, 20*time.Second)

	// now we create two new peers
	dhtD := setupDHT(ctx, t, false)
	dhtE := setupDHT(ctx, t, false)

	// connect them to each other
	connect(t, ctx, dhtD, dhtE)
	defer func() {
		dhtD.Close()
		dhtD.host.Close()

		dhtE.Close()
		dhtE.host.Close()
	}()

	// and then, on connecting the peer D to A, the min RT threshold gets triggered on A which leads to a bootstrap.
	// since the default bootstrap scan interval is 30 mins - 1 hour, we can be sure that if bootstrap happens,
	// it is because of the min RT threshold getting triggered (since default min value is 4 & we only have 2 peers in the RT when D gets connected)
	connect(t, ctx, dhtA, dhtD)

	// and because of the above bootstrap, A also discovers E !
	waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 4, 4, 20*time.Second)
	assert.Equal(t, dhtE.self, dhtA.routingTable.Find(dhtE.self), "A's routing table should have peer E!")
}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
func TestQueryWithEmptyRTShouldNotPanic(t *testing.T) {
	ctx := context.Background()
	d := setupDHT(ctx, t, false)

	// TODO This swallows the error for now, should we change it ?
	// FindProviders
	ps, _ := d.FindProviders(ctx, testCaseCids[0])
	require.Empty(t, ps)

	// GetClosestPeers
	pc, err := d.GetClosestPeers(ctx, "key")
	require.Nil(t, pc)
	require.Equal(t, kb.ErrLookupFailure, err)

	// GetValue
	best, err := d.GetValue(ctx, "key")
	require.Empty(t, best)
	require.Error(t, err)

	// SearchValue
	bchan, err := d.SearchValue(ctx, "key")
	require.Empty(t, bchan)
	require.NoError(t, err)

	// Provide
	err = d.Provide(ctx, testCaseCids[0], true)
	require.Equal(t, kb.ErrLookupFailure, err)
}

843
func TestPeriodicRefresh(t *testing.T) {
844 845 846
	if testing.Short() {
		t.SkipNow()
	}
847 848 849
	if detectrace.WithRace() {
		t.Skip("skipping due to race detector max goroutines")
	}
850

Steven Allen's avatar
Steven Allen committed
851 852
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
853 854

	nDHTs := 30
855
	dhts := setupDHTS(t, ctx, nDHTs)
856 857 858 859 860 861 862
	defer func() {
		for i := 0; i < nDHTs; i++ {
			dhts[i].Close()
			defer dhts[i].host.Close()
		}
	}()

863
	t.Logf("dhts are not connected. %d", nDHTs)
864 865 866 867 868 869 870 871 872 873 874
	for _, dht := range dhts {
		rtlen := dht.routingTable.Size()
		if rtlen > 0 {
			t.Errorf("routing table for %s should have 0 peers. has %d", dht.self, rtlen)
		}
	}

	for i := 0; i < nDHTs; i++ {
		connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)])
	}

875
	t.Logf("DHTs are now connected to 1-2 others. %d", nDHTs)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
876
	for _, dht := range dhts {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
877
		rtlen := dht.routingTable.Size()
878 879
		if rtlen > 2 {
			t.Errorf("routing table for %s should have at most 2 peers. has %d", dht.self, rtlen)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
880
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
881
	}
882

883 884 885 886
	if u.Debug {
		printRoutingTables(dhts)
	}

887
	t.Logf("bootstrapping them so they find each other. %d", nDHTs)
Aarsh Shah's avatar
Aarsh Shah committed
888
	var wg sync.WaitGroup
Matt Joiner's avatar
Matt Joiner committed
889
	for _, dht := range dhts {
Aarsh Shah's avatar
Aarsh Shah committed
890 891 892 893 894
		wg.Add(1)
		go func(d *IpfsDHT) {
			<-d.RefreshRoutingTable()
			wg.Done()
		}(dht)
895
	}
896

Aarsh Shah's avatar
Aarsh Shah committed
897
	wg.Wait()
898 899
	// this is async, and we dont know when it's finished with one cycle, so keep checking
	// until the routing tables look better, or some long timeout for the failure case.
900
	waitForWellFormedTables(t, dhts, 7, 10, 20*time.Second)
901 902 903

	if u.Debug {
		printRoutingTables(dhts)
904
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
905 906
}

907
func TestProvidesMany(t *testing.T) {
908 909 910
	if detectrace.WithRace() {
		t.Skip("skipping due to race detector max goroutines")
	}
Steven Allen's avatar
Steven Allen committed
911 912
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
913 914

	nDHTs := 40
915
	dhts := setupDHTS(t, ctx, nDHTs)
916 917 918
	defer func() {
		for i := 0; i < nDHTs; i++ {
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
919
			defer dhts[i].host.Close()
920 921 922 923 924 925 926 927
		}
	}()

	t.Logf("connecting %d dhts in a ring", nDHTs)
	for i := 0; i < nDHTs; i++ {
		connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)])
	}

928
	<-time.After(100 * time.Millisecond)
929
	t.Logf("bootstrapping them so they find each other. %d", nDHTs)
Jeromy's avatar
Jeromy committed
930 931
	ctxT, cancel := context.WithTimeout(ctx, 20*time.Second)
	defer cancel()
932 933
	bootstrap(t, ctxT, dhts)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
934 935 936 937 938 939 940 941
	if u.Debug {
		// the routing tables should be full now. let's inspect them.
		t.Logf("checking routing table of %d", nDHTs)
		for _, dht := range dhts {
			fmt.Printf("checking routing table of %s\n", dht.self)
			dht.routingTable.Print()
			fmt.Println("")
		}
942
	}
943

944
	providers := make(map[cid.Cid]peer.ID)
945

946
	d := 0
947
	for _, c := range testCaseCids {
948 949
		d = (d + 1) % len(dhts)
		dht := dhts[d]
950
		providers[c] = dht.self
951

952
		t.Logf("announcing provider for %s", c)
Jeromy's avatar
Jeromy committed
953
		if err := dht.Provide(ctx, c, true); err != nil {
954 955
			t.Fatal(err)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
956 957
	}

958 959
	// what is this timeout for? was 60ms before.
	time.Sleep(time.Millisecond * 6)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
960

961 962
	errchan := make(chan error)

Jeromy's avatar
Jeromy committed
963 964
	ctxT, cancel = context.WithTimeout(ctx, 5*time.Second)
	defer cancel()
965 966

	var wg sync.WaitGroup
967
	getProvider := func(dht *IpfsDHT, k cid.Cid) {
968
		defer wg.Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
969

970
		expected := providers[k]
971

972 973 974
		provchan := dht.FindProvidersAsync(ctxT, k, 1)
		select {
		case prov := <-provchan:
975 976
			actual := prov.ID
			if actual == "" {
977
				errchan <- fmt.Errorf("Got back nil provider (%s at %s)", k, dht.self)
978 979 980
			} else if actual != expected {
				errchan <- fmt.Errorf("Got back wrong provider (%s != %s) (%s at %s)",
					expected, actual, k, dht.self)
981 982 983
			}
		case <-ctxT.Done():
			errchan <- fmt.Errorf("Did not get a provider back (%s at %s)", k, dht.self)
Jeromy's avatar
Jeromy committed
984
		}
985 986
	}

987
	for _, c := range testCaseCids {
988 989
		// everyone should be able to find it...
		for _, dht := range dhts {
Matt Joiner's avatar
Matt Joiner committed
990
			logger.Debugf("getting providers for %s at %s", c, dht.self)
991
			wg.Add(1)
992
			go getProvider(dht, c)
993
		}
994 995 996 997 998 999 1000 1001 1002 1003
	}

	// we need this because of printing errors
	go func() {
		wg.Wait()
		close(errchan)
	}()

	for err := range errchan {
		t.Error(err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1004 1005 1006
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1007
func TestProvidesAsync(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1008
	// t.Skip("skipping test to debug another")
1009 1010 1011
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1012

Steven Allen's avatar
Steven Allen committed
1013 1014
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1015

1016
	dhts := setupDHTS(t, ctx, 4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1017 1018
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1019
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1020
			defer dhts[i].host.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1021 1022 1023
		}
	}()

1024 1025 1026
	connect(t, ctx, dhts[0], dhts[1])
	connect(t, ctx, dhts[1], dhts[2])
	connect(t, ctx, dhts[1], dhts[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1027

Jeromy's avatar
Jeromy committed
1028
	err := dhts[3].Provide(ctx, testCaseCids[0], true)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1029 1030 1031 1032 1033 1034
	if err != nil {
		t.Fatal(err)
	}

	time.Sleep(time.Millisecond * 60)

Jeromy's avatar
Jeromy committed
1035 1036
	ctxT, cancel := context.WithTimeout(ctx, time.Millisecond*300)
	defer cancel()
1037
	provs := dhts[0].FindProvidersAsync(ctxT, testCaseCids[0], 5)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1038
	select {
Jeromy's avatar
Jeromy committed
1039 1040 1041 1042
	case p, ok := <-provs:
		if !ok {
			t.Fatal("Provider channel was closed...")
		}
1043
		if p.ID == "" {
Jeromy's avatar
Jeromy committed
1044 1045
			t.Fatal("Got back nil provider!")
		}
1046
		if p.ID != dhts[3].self {
1047
			t.Fatalf("got a provider, but not the right one. %s", p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1048
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1049
	case <-ctxT.Done():
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1050 1051 1052 1053
		t.Fatal("Didnt get back providers")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1054
func TestLayeredGet(t *testing.T) {
1055 1056
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
1057

1058
	dhts := setupDHTS(t, ctx, 4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1059 1060
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1061
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1062
			defer dhts[i].host.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1063 1064 1065
		}
	}()

1066 1067
	connect(t, ctx, dhts[0], dhts[1])
	connect(t, ctx, dhts[1], dhts[2])
1068
	connect(t, ctx, dhts[2], dhts[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1069

1070
	err := dhts[3].PutValue(ctx, "/v/hello", []byte("world"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1071 1072 1073 1074
	if err != nil {
		t.Fatal(err)
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1075
	time.Sleep(time.Millisecond * 6)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1076

Jeromy's avatar
Jeromy committed
1077 1078
	ctxT, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
1079 1080 1081
	val, err := dhts[0].GetValue(ctxT, "/v/hello")
	if err != nil {
		t.Fatal(err)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1082
	}
1083 1084 1085

	if string(val) != "world" {
		t.Error("got wrong value")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1086 1087 1088
	}
}

1089 1090 1091 1092 1093 1094 1095 1096
func TestUnfindablePeer(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

1097
	dhts := setupDHTS(t, ctx, 4)
1098 1099 1100
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Close()
1101
			dhts[i].Host().Close()
1102 1103 1104 1105 1106 1107 1108 1109
		}
	}()

	connect(t, ctx, dhts[0], dhts[1])
	connect(t, ctx, dhts[1], dhts[2])
	connect(t, ctx, dhts[2], dhts[3])

	// Give DHT 1 a bad addr for DHT 2.
1110 1111
	dhts[1].host.Peerstore().ClearAddrs(dhts[2].PeerID())
	dhts[1].host.Peerstore().AddAddr(dhts[2].PeerID(), dhts[0].Host().Addrs()[0], time.Minute)
1112 1113 1114

	ctxT, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
1115
	_, err := dhts[0].FindPeer(ctxT, dhts[3].PeerID())
1116 1117 1118 1119 1120 1121 1122 1123
	if err == nil {
		t.Error("should have failed to find peer")
	}
	if ctxT.Err() != nil {
		t.Error("FindPeer should have failed before context expired")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1124
func TestFindPeer(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1125
	// t.Skip("skipping test to debug another")
1126 1127 1128
	if testing.Short() {
		t.SkipNow()
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1129

Steven Allen's avatar
Steven Allen committed
1130 1131
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1132

1133
	dhts := setupDHTS(t, ctx, 4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1134 1135
	defer func() {
		for i := 0; i < 4; i++ {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1136
			dhts[i].Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1137
			dhts[i].host.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1138 1139 1140
		}
	}()

1141 1142 1143
	connect(t, ctx, dhts[0], dhts[1])
	connect(t, ctx, dhts[1], dhts[2])
	connect(t, ctx, dhts[1], dhts[3])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1144

Jeromy's avatar
Jeromy committed
1145 1146
	ctxT, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
1147
	p, err := dhts[0].FindPeer(ctxT, dhts[2].PeerID())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1148 1149 1150 1151
	if err != nil {
		t.Fatal(err)
	}

1152
	if p.ID == "" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1153 1154 1155
		t.Fatal("Failed to find peer.")
	}

1156
	if p.ID != dhts[2].PeerID() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1157 1158 1159
		t.Fatal("Didnt find expected peer.")
	}
}
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
func TestFindPeerWithQueryFilter(t *testing.T) {
	// t.Skip("skipping test to debug another")
	if testing.Short() {
		t.SkipNow()
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	filteredPeer := bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport))
1171
	dhts := setupDHTS(t, ctx, 4, QueryFilter(func(_ interface{}, ai peer.AddrInfo) bool {
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
		return ai.ID != filteredPeer.ID()
	}))
	defer func() {
		for i := 0; i < 4; i++ {
			dhts[i].Close()
			dhts[i].host.Close()
		}
	}()

	connect(t, ctx, dhts[0], dhts[1])
	connect(t, ctx, dhts[1], dhts[2])
	connect(t, ctx, dhts[1], dhts[3])

	err := filteredPeer.Connect(ctx, peer.AddrInfo{
		ID:    dhts[2].host.ID(),
		Addrs: dhts[2].host.Addrs(),
	})
	if err != nil {
		t.Fatal(err)
	}
	for i, maxLoops := 0, 5; i < maxLoops; i++ {
		if len(dhts[2].host.Network().ConnsToPeer(filteredPeer.ID())) > 0 {
			break
		}
		if i == maxLoops {
			t.Fatal("failed to connect to the filtered peer")
		}
	}

	ctxT, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
	p, err := dhts[0].FindPeer(ctxT, filteredPeer.ID())
	if err != nil {
		t.Fatal(err)
	}

	if p.ID == "" {
		t.Fatal("Failed to find peer.")
	}

	if p.ID != filteredPeer.ID() {
		t.Fatal("Didnt find expected peer.")
	}
}

1217
func TestConnectCollision(t *testing.T) {
1218 1219 1220
	if testing.Short() {
		t.SkipNow()
	}
1221

1222
	runTimes := 10
1223

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1224
	for rtime := 0; rtime < runTimes; rtime++ {
Matt Joiner's avatar
Matt Joiner committed
1225
		logger.Info("Running Time: ", rtime)
1226

Steven Allen's avatar
Steven Allen committed
1227
		ctx, cancel := context.WithCancel(context.Background())
1228

1229 1230
		dhtA := setupDHT(ctx, t, false)
		dhtB := setupDHT(ctx, t, false)
1231

1232 1233
		addrA := dhtA.peerstore.Addrs(dhtA.self)[0]
		addrB := dhtB.peerstore.Addrs(dhtB.self)[0]
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1234

1235 1236
		peerA := dhtA.self
		peerB := dhtB.self
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1237

1238
		errs := make(chan error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1239
		go func() {
1240 1241
			dhtA.peerstore.AddAddr(peerB, addrB, peerstore.TempAddrTTL)
			pi := peer.AddrInfo{ID: peerB}
Jeromy's avatar
Jeromy committed
1242
			err := dhtA.host.Connect(ctx, pi)
1243
			errs <- err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1244 1245
		}()
		go func() {
1246 1247
			dhtB.peerstore.AddAddr(peerA, addrA, peerstore.TempAddrTTL)
			pi := peer.AddrInfo{ID: peerA}
Jeromy's avatar
Jeromy committed
1248
			err := dhtB.host.Connect(ctx, pi)
1249
			errs <- err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1250 1251
		}()

1252
		timeout := time.After(5 * time.Second)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1253
		select {
1254 1255 1256 1257
		case e := <-errs:
			if e != nil {
				t.Fatal(e)
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1258 1259 1260 1261
		case <-timeout:
			t.Fatal("Timeout received!")
		}
		select {
1262 1263 1264 1265
		case e := <-errs:
			if e != nil {
				t.Fatal(e)
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1266 1267 1268 1269
		case <-timeout:
			t.Fatal("Timeout received!")
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1270 1271
		dhtA.Close()
		dhtB.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1272 1273
		dhtA.host.Close()
		dhtB.host.Close()
Steven Allen's avatar
Steven Allen committed
1274
		cancel()
Jeromy's avatar
Jeromy committed
1275
	}
1276
}
1277 1278 1279 1280 1281

func TestBadProtoMessages(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

1282
	d := setupDHT(ctx, t, false)
1283 1284 1285 1286 1287 1288

	nilrec := new(pb.Message)
	if _, err := d.handlePutValue(ctx, "testpeer", nilrec); err == nil {
		t.Fatal("should have errored on nil record")
	}
}
1289

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
func TestAtomicPut(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	d := setupDHT(ctx, t, false)
	d.Validator = testAtomicPutValidator{}

	// fnc to put a record
	key := "testkey"
	putRecord := func(value []byte) error {
		rec := record.MakePutRecord(key, value)
		pmes := pb.NewMessage(pb.Message_PUT_VALUE, rec.Key, 0)
		pmes.Record = rec
		_, err := d.handlePutValue(ctx, "testpeer", pmes)
		return err
	}

	// put a valid record
	if err := putRecord([]byte("valid")); err != nil {
		t.Fatal("should not have errored on a valid record")
	}

	// simultaneous puts for old & new values
	values := [][]byte{[]byte("newer1"), []byte("newer7"), []byte("newer3"), []byte("newer5")}
	var wg sync.WaitGroup
	for _, v := range values {
		wg.Add(1)
		go func(v []byte) {
			defer wg.Done()
Steven Allen's avatar
Steven Allen committed
1319
			_ = putRecord(v) // we expect some of these to fail
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
		}(v)
	}
	wg.Wait()

	// get should return the newest value
	pmes := pb.NewMessage(pb.Message_GET_VALUE, []byte(key), 0)
	msg, err := d.handleGetValue(ctx, "testkey", pmes)
	if err != nil {
		t.Fatalf("should not have errored on final get, but got %+v", err)
	}
	if string(msg.GetRecord().Value) != "newer7" {
		t.Fatalf("Expected 'newer7' got '%s'", string(msg.GetRecord().Value))
	}
}

1335 1336 1337 1338 1339 1340 1341 1342 1343
func TestClientModeConnect(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	a := setupDHT(ctx, t, false)
	b := setupDHT(ctx, t, true)

	connectNoSync(t, ctx, a, b)

1344
	c := testCaseCids[0]
1345
	p := peer.ID("TestPeer")
1346
	a.ProviderManager.AddProvider(ctx, c.Hash(), p)
1347
	time.Sleep(time.Millisecond * 5) // just in case...
1348

1349
	provs, err := b.FindProviders(ctx, c)
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	if err != nil {
		t.Fatal(err)
	}

	if len(provs) == 0 {
		t.Fatal("Expected to get a provider back")
	}

	if provs[0].ID != p {
		t.Fatal("expected it to be our test peer")
	}
Steven Allen's avatar
Steven Allen committed
1361 1362 1363 1364 1365 1366 1367 1368 1369
	if a.routingTable.Find(b.self) != "" {
		t.Fatal("DHT clients should not be added to routing tables")
	}
	if b.routingTable.Find(a.self) == "" {
		t.Fatal("DHT server should have been added to the dht client's routing table")
	}
}

func TestClientModeFindPeer(t *testing.T) {
1370
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Steven Allen's avatar
Steven Allen committed
1371 1372 1373 1374 1375 1376
	defer cancel()

	a := setupDHT(ctx, t, false)
	b := setupDHT(ctx, t, true)
	c := setupDHT(ctx, t, true)

1377 1378
	connectNoSync(t, ctx, b, a)
	connectNoSync(t, ctx, c, a)
Steven Allen's avatar
Steven Allen committed
1379 1380

	// Can't use `connect` because b and c are only clients.
1381 1382
	wait(t, ctx, b, a)
	wait(t, ctx, c, a)
Steven Allen's avatar
Steven Allen committed
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

	pi, err := c.FindPeer(ctx, b.self)
	if err != nil {
		t.Fatal(err)
	}
	if len(pi.Addrs) == 0 {
		t.Fatal("should have found addresses for node b")
	}

	err = c.host.Connect(ctx, pi)
	if err != nil {
		t.Fatal(err)
	}
1396
}
1397

Matt Joiner's avatar
Matt Joiner committed
1398 1399 1400 1401
func minInt(a, b int) int {
	if a < b {
		return a
	}
Alan Shaw's avatar
Alan Shaw committed
1402
	return b
Matt Joiner's avatar
Matt Joiner committed
1403 1404 1405
}

func TestFindPeerQueryMinimal(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
1406
	testFindPeerQuery(t, 2, 22, 1)
Matt Joiner's avatar
Matt Joiner committed
1407 1408
}

1409
func TestFindPeerQuery(t *testing.T) {
1410 1411 1412 1413
	if detectrace.WithRace() {
		t.Skip("skipping due to race detector max goroutines")
	}

Matt Joiner's avatar
Matt Joiner committed
1414 1415 1416
	if testing.Short() {
		t.Skip("skipping test in short mode")
	}
Steven Allen's avatar
Steven Allen committed
1417
	testFindPeerQuery(t, 5, 40, 3)
Matt Joiner's avatar
Matt Joiner committed
1418 1419
}

Aarsh Shah's avatar
Aarsh Shah committed
1420
// NOTE: You must have ATLEAST (minRTRefreshThreshold+1) test peers before using this.
Matt Joiner's avatar
Matt Joiner committed
1421 1422 1423
func testFindPeerQuery(t *testing.T,
	bootstrappers, // Number of nodes connected to the querying node
	leafs, // Number of nodes that might be connected to from the bootstrappers
Steven Allen's avatar
Steven Allen committed
1424
	bootstrapConns int, // Number of bootstrappers each leaf should connect to.
Matt Joiner's avatar
Matt Joiner committed
1425
) {
1426 1427 1428
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

Steven Allen's avatar
Steven Allen committed
1429
	dhts := setupDHTS(t, ctx, 1+bootstrappers+leafs, BucketSize(4))
1430
	defer func() {
Matt Joiner's avatar
Matt Joiner committed
1431 1432 1433
		for _, d := range dhts {
			d.Close()
			d.host.Close()
1434 1435 1436
		}
	}()

Steven Allen's avatar
Steven Allen committed
1437 1438
	t.Log("connecting")

Jeromy's avatar
Jeromy committed
1439
	mrand := rand.New(rand.NewSource(42))
1440 1441
	guy := dhts[0]
	others := dhts[1:]
Steven Allen's avatar
Steven Allen committed
1442 1443 1444
	for i := 0; i < leafs; i++ {
		for _, v := range mrand.Perm(bootstrappers)[:bootstrapConns] {
			connectNoSync(t, ctx, others[v], others[bootstrappers+i])
1445 1446 1447
		}
	}

Matt Joiner's avatar
Matt Joiner committed
1448
	for i := 0; i < bootstrappers; i++ {
Aarsh Shah's avatar
Aarsh Shah committed
1449
		connectNoSync(t, ctx, guy, others[i])
1450 1451
	}

Steven Allen's avatar
Steven Allen committed
1452 1453
	t.Log("waiting for routing tables")

Aarsh Shah's avatar
Aarsh Shah committed
1454
	// give some time for things to settle down
Steven Allen's avatar
Steven Allen committed
1455
	waitForWellFormedTables(t, dhts, bootstrapConns, bootstrapConns, 5*time.Second)
Aarsh Shah's avatar
Aarsh Shah committed
1456

Steven Allen's avatar
Steven Allen committed
1457 1458 1459 1460 1461 1462 1463 1464 1465
	t.Log("refreshing")

	var wg sync.WaitGroup
	for _, dht := range dhts {
		wg.Add(1)
		go func(d *IpfsDHT) {
			<-d.RefreshRoutingTable()
			wg.Done()
		}(dht)
1466 1467
	}

Steven Allen's avatar
Steven Allen committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
	wg.Wait()

	t.Log("waiting for routing tables again")

	// Wait for refresh to work. At least one bucket should be full.
	waitForWellFormedTables(t, dhts, 4, 0, 5*time.Second)

	var peers []peer.ID
	for _, d := range others {
		peers = append(peers, d.PeerID())
Matt Joiner's avatar
Matt Joiner committed
1478
	}
Steven Allen's avatar
Steven Allen committed
1479 1480

	t.Log("querying")
Matt Joiner's avatar
Matt Joiner committed
1481

1482 1483 1484
	val := "foobar"
	rtval := kb.ConvertKey(val)

1485
	outpeers, err := guy.GetClosestPeers(ctx, val)
Matt Joiner's avatar
Matt Joiner committed
1486
	require.NoError(t, err)
1487

Jeromy's avatar
Jeromy committed
1488
	sort.Sort(peer.IDSlice(outpeers))
Steven Allen's avatar
Steven Allen committed
1489

Steven Allen's avatar
Steven Allen committed
1490
	exp := kb.SortClosestPeers(peers, rtval)[:minInt(guy.bucketSize, len(peers))]
Matt Joiner's avatar
Matt Joiner committed
1491
	t.Logf("got %d peers", len(outpeers))
1492
	got := kb.SortClosestPeers(outpeers, rtval)
Jeromy's avatar
Jeromy committed
1493

Matt Joiner's avatar
Matt Joiner committed
1494
	assert.EqualValues(t, exp, got)
1495 1496
}

1497 1498 1499 1500 1501
func TestFindClosestPeers(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	nDHTs := 30
1502
	dhts := setupDHTS(t, ctx, nDHTs)
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
	defer func() {
		for i := 0; i < nDHTs; i++ {
			dhts[i].Close()
			defer dhts[i].host.Close()
		}
	}()

	t.Logf("connecting %d dhts in a ring", nDHTs)
	for i := 0; i < nDHTs; i++ {
		connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)])
	}

1515 1516
	querier := dhts[1]
	peers, err := querier.GetClosestPeers(ctx, "foo")
1517 1518 1519 1520
	if err != nil {
		t.Fatal(err)
	}

1521 1522
	if len(peers) < querier.beta {
		t.Fatalf("got wrong number of peers (got %d, expected at least %d)", len(peers), querier.beta)
1523 1524
	}
}
1525

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
func TestFixLowPeers(t *testing.T) {
	ctx := context.Background()

	dhts := setupDHTS(t, ctx, minRTRefreshThreshold+5)

	defer func() {
		for _, d := range dhts {
			d.Close()
			d.Host().Close()
		}
	}()

	mainD := dhts[0]

	// connect it to everyone else
	for _, d := range dhts[1:] {
		mainD.peerstore.AddAddrs(d.self, d.peerstore.Addrs(d.self), peerstore.TempAddrTTL)
		require.NoError(t, mainD.Host().Connect(ctx, peer.AddrInfo{ID: d.self}))
	}

	waitForWellFormedTables(t, []*IpfsDHT{mainD}, minRTRefreshThreshold, minRTRefreshThreshold+4, 5*time.Second)

	// run a refresh on all of them
	for _, d := range dhts {
		err := <-d.RefreshRoutingTable()
		require.NoError(t, err)
	}

	// now remove peers from RT so threshold gets hit
	for _, d := range dhts[3:] {
		mainD.routingTable.RemovePeer(d.self)
	}

	// but we will still get enough peers in the RT because of fix low Peers
	waitForWellFormedTables(t, []*IpfsDHT{mainD}, minRTRefreshThreshold, minRTRefreshThreshold, 5*time.Second)
}

1563 1564
func TestProvideDisabled(t *testing.T) {
	k := testCaseCids[0]
1565
	kHash := k.Hash()
1566 1567 1568 1569 1570 1571 1572
	for i := 0; i < 3; i++ {
		enabledA := (i & 0x1) > 0
		enabledB := (i & 0x2) > 0
		t.Run(fmt.Sprintf("a=%v/b=%v", enabledA, enabledB), func(t *testing.T) {
			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()

1573
			var (
1574
				optsA, optsB []Option
1575
			)
Adin Schmahmann's avatar
Adin Schmahmann committed
1576 1577 1578
			optsA = append(optsA, ProtocolPrefix("/provMaybeDisabled"))
			optsB = append(optsB, ProtocolPrefix("/provMaybeDisabled"))

1579
			if !enabledA {
1580
				optsA = append(optsA, DisableProviders())
1581 1582
			}
			if !enabledB {
1583
				optsB = append(optsB, DisableProviders())
1584 1585 1586 1587
			}

			dhtA := setupDHT(ctx, t, false, optsA...)
			dhtB := setupDHT(ctx, t, false, optsB...)
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608

			defer dhtA.Close()
			defer dhtB.Close()
			defer dhtA.host.Close()
			defer dhtB.host.Close()

			connect(t, ctx, dhtA, dhtB)

			err := dhtB.Provide(ctx, k, true)
			if enabledB {
				if err != nil {
					t.Fatal("put should have succeeded on node B", err)
				}
			} else {
				if err != routing.ErrNotSupported {
					t.Fatal("should not have put the value to node B", err)
				}
				_, err = dhtB.FindProviders(ctx, k)
				if err != routing.ErrNotSupported {
					t.Fatal("get should have failed on node B")
				}
1609
				provs := dhtB.ProviderManager.GetProviders(ctx, kHash)
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
				if len(provs) != 0 {
					t.Fatal("node B should not have found local providers")
				}
			}

			provs, err := dhtA.FindProviders(ctx, k)
			if enabledA {
				if len(provs) != 0 {
					t.Fatal("node A should not have found providers")
				}
			} else {
				if err != routing.ErrNotSupported {
					t.Fatal("node A should not have found providers")
				}
			}
1625
			provAddrs := dhtA.ProviderManager.GetProviders(ctx, kHash)
1626 1627 1628 1629 1630 1631 1632
			if len(provAddrs) != 0 {
				t.Fatal("node A should not have found local providers")
			}
		})
	}
}

1633 1634
func TestHandleRemotePeerProtocolChanges(t *testing.T) {
	ctx := context.Background()
1635
	os := []Option{
Adin Schmahmann's avatar
Adin Schmahmann committed
1636
		testPrefix,
1637 1638 1639
		Mode(ModeServer),
		NamespacedValidator("v", blankValidator{}),
		DisableAutoRefresh(),
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
	}

	// start host 1 that speaks dht v1
	dhtA, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), os...)
	require.NoError(t, err)
	defer dhtA.Close()

	// start host 2 that also speaks dht v1
	dhtB, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), os...)
	require.NoError(t, err)
	defer dhtB.Close()

	connect(t, ctx, dhtA, dhtB)

	// now assert both have each other in their RT
Steven Allen's avatar
Steven Allen committed
1655
	waitForWellFormedTables(t, []*IpfsDHT{dhtA, dhtB}, 1, 1, 10*time.Second)
1656 1657 1658 1659 1660

	// dhtB becomes a client
	require.NoError(t, dhtB.setMode(modeClient))

	// which means that dhtA should evict it from it's RT
Steven Allen's avatar
Steven Allen committed
1661
	waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 0, 0, 10*time.Second)
1662 1663 1664 1665

	// dhtB becomes a server
	require.NoError(t, dhtB.setMode(modeServer))

Aarsh Shah's avatar
Aarsh Shah committed
1666
	// which means dhtA should have it in the RT again because of fixLowPeers
Steven Allen's avatar
Steven Allen committed
1667
	waitForWellFormedTables(t, []*IpfsDHT{dhtA}, 1, 1, 10*time.Second)
1668 1669
}

1670
func TestGetSetPluggedProtocol(t *testing.T) {
1671 1672 1673
	t.Run("PutValue/GetValue - same protocol", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()
1674

1675
		os := []Option{
Adin Schmahmann's avatar
Adin Schmahmann committed
1676
			ProtocolPrefix("/esh"),
1677 1678 1679
			Mode(ModeServer),
			NamespacedValidator("v", blankValidator{}),
			DisableAutoRefresh(),
1680
		}
1681

Steven Allen's avatar
Steven Allen committed
1682
		dhtA, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), os...)
1683 1684 1685
		if err != nil {
			t.Fatal(err)
		}
1686

Steven Allen's avatar
Steven Allen committed
1687
		dhtB, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), os...)
1688 1689 1690
		if err != nil {
			t.Fatal(err)
		}
1691

1692
		connect(t, ctx, dhtA, dhtB)
1693

1694
		ctxT, cancel := context.WithTimeout(ctx, time.Second)
1695
		defer cancel()
1696 1697 1698
		if err := dhtA.PutValue(ctxT, "/v/cat", []byte("meow")); err != nil {
			t.Fatal(err)
		}
1699

1700 1701 1702 1703
		value, err := dhtB.GetValue(ctxT, "/v/cat")
		if err != nil {
			t.Fatal(err)
		}
1704

1705 1706 1707 1708 1709
		if string(value) != "meow" {
			t.Fatalf("Expected 'meow' got '%s'", string(value))
		}
	})

1710 1711
	t.Run("DHT routing table for peer A won't contain B if A and B don't use same protocol", func(t *testing.T) {
		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1712 1713
		defer cancel()

1714
		dhtA, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), []Option{
Adin Schmahmann's avatar
Adin Schmahmann committed
1715
			ProtocolPrefix("/esh"),
1716 1717 1718
			Mode(ModeServer),
			NamespacedValidator("v", blankValidator{}),
			DisableAutoRefresh(),
1719
		}...)
1720 1721 1722 1723
		if err != nil {
			t.Fatal(err)
		}

1724
		dhtB, err := New(ctx, bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)), []Option{
Adin Schmahmann's avatar
Adin Schmahmann committed
1725
			ProtocolPrefix("/lsr"),
1726 1727 1728
			Mode(ModeServer),
			NamespacedValidator("v", blankValidator{}),
			DisableAutoRefresh(),
1729
		}...)
1730 1731 1732 1733
		if err != nil {
			t.Fatal(err)
		}

1734
		connectNoSync(t, ctx, dhtA, dhtB)
1735

1736 1737 1738 1739 1740 1741
		// We don't expect connection notifications for A to reach B (or vice-versa), given
		// that they've been configured with different protocols - but we'll give them a
		// chance, anyhow.
		time.Sleep(time.Second * 2)

		err = dhtA.PutValue(ctx, "/v/cat", []byte("meow"))
1742 1743
		if err == nil || !strings.Contains(err.Error(), "failed to find any peer in table") {
			t.Fatalf("put should not have been able to find any peers in routing table, err:'%v'", err)
1744 1745
		}

1746 1747 1748
		v, err := dhtB.GetValue(ctx, "/v/cat")
		if v != nil || err != routing.ErrNotFound {
			t.Fatalf("get should have failed from not being able to find the value, err: '%v'", err)
1749
		}
1750
	})
1751
}
1752 1753 1754 1755 1756

func TestPing(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	ds := setupDHTS(t, ctx, 2)
1757
	ds[0].Host().Peerstore().AddAddrs(ds[1].PeerID(), ds[1].Host().Addrs(), peerstore.AddressTTL)
1758 1759 1760 1761 1762 1763 1764 1765
	assert.NoError(t, ds[0].Ping(context.Background(), ds[1].PeerID()))
}

func TestClientModeAtInit(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	pinger := setupDHT(ctx, t, false)
	client := setupDHT(ctx, t, true)
1766
	pinger.Host().Peerstore().AddAddrs(client.PeerID(), client.Host().Addrs(), peerstore.AddressTTL)
1767
	err := pinger.Ping(context.Background(), client.PeerID())
Steven Allen's avatar
Steven Allen committed
1768
	assert.True(t, errors.Is(err, multistream.ErrNotSupported))
1769
}
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793

func TestModeChange(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	clientOnly := setupDHT(ctx, t, true)
	clientToServer := setupDHT(ctx, t, true)
	clientOnly.Host().Peerstore().AddAddrs(clientToServer.PeerID(), clientToServer.Host().Addrs(), peerstore.AddressTTL)
	err := clientOnly.Ping(ctx, clientToServer.PeerID())
	assert.True(t, errors.Is(err, multistream.ErrNotSupported))
	err = clientToServer.setMode(modeServer)
	assert.Nil(t, err)
	err = clientOnly.Ping(ctx, clientToServer.PeerID())
	assert.Nil(t, err)
	err = clientToServer.setMode(modeClient)
	assert.Nil(t, err)
	err = clientOnly.Ping(ctx, clientToServer.PeerID())
	assert.NotNil(t, err)
}

func TestDynamicModeSwitching(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

1794 1795
	prober := setupDHT(ctx, t, true)               // our test harness
	node := setupDHT(ctx, t, true, Mode(ModeAuto)) // the node under test
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
	prober.Host().Peerstore().AddAddrs(node.PeerID(), node.Host().Addrs(), peerstore.AddressTTL)
	if _, err := prober.Host().Network().DialPeer(ctx, node.PeerID()); err != nil {
		t.Fatal(err)
	}

	emitter, err := node.host.EventBus().Emitter(new(event.EvtLocalReachabilityChanged))
	if err != nil {
		t.Fatal(err)
	}

	assertDHTClient := func() {
		err = prober.Ping(ctx, node.PeerID())
		assert.True(t, errors.Is(err, multistream.ErrNotSupported))
		if l := len(prober.RoutingTable().ListPeers()); l != 0 {
			t.Errorf("expected routing table length to be 0; instead is %d", l)
		}
	}

	assertDHTServer := func() {
		err = prober.Ping(ctx, node.PeerID())
		assert.Nil(t, err)
Aarsh Shah's avatar
Aarsh Shah committed
1817 1818
		// the node should be in the RT for the prober
		// because the prober will call fixLowPeers when the node updates it's protocols
1819 1820 1821 1822 1823
		if l := len(prober.RoutingTable().ListPeers()); l != 1 {
			t.Errorf("expected routing table length to be 1; instead is %d", l)
		}
	}

Steven Allen's avatar
Steven Allen committed
1824 1825 1826 1827
	err = emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPrivate})
	if err != nil {
		t.Fatal(err)
	}
1828 1829 1830 1831
	time.Sleep(500 * time.Millisecond)

	assertDHTClient()

Steven Allen's avatar
Steven Allen committed
1832 1833 1834 1835
	err = emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPublic})
	if err != nil {
		t.Fatal(err)
	}
1836 1837 1838 1839
	time.Sleep(500 * time.Millisecond)

	assertDHTServer()

Steven Allen's avatar
Steven Allen committed
1840 1841 1842 1843
	err = emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityUnknown})
	if err != nil {
		t.Fatal(err)
	}
1844 1845 1846 1847
	time.Sleep(500 * time.Millisecond)

	assertDHTClient()
}
Adin Schmahmann's avatar
Adin Schmahmann committed
1848

Steven Allen's avatar
Steven Allen committed
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
func TestInvalidKeys(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	nDHTs := 2
	dhts := setupDHTS(t, ctx, nDHTs)
	defer func() {
		for i := 0; i < nDHTs; i++ {
			dhts[i].Close()
			defer dhts[i].host.Close()
		}
	}()

	t.Logf("connecting %d dhts in a ring", nDHTs)
	for i := 0; i < nDHTs; i++ {
		connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)])
	}

	querier := dhts[0]
	_, err := querier.GetClosestPeers(ctx, "")
	if err == nil {
		t.Fatal("get closest peers should have failed")
	}

	_, err = querier.FindProviders(ctx, cid.Cid{})
	switch err {
	case routing.ErrNotFound, routing.ErrNotSupported, kb.ErrLookupFailure:
		t.Fatal("failed with the wrong error: ", err)
	case nil:
		t.Fatal("find providers should have failed")
	}

	_, err = querier.FindPeer(ctx, peer.ID(""))
	if err != peer.ErrEmptyPeerID {
		t.Fatal("expected to fail due to the empty peer ID")
	}

	_, err = querier.GetValue(ctx, "")
	if err == nil {
		t.Fatal("expected to have failed")
	}

	err = querier.PutValue(ctx, "", []byte("foobar"))
	if err == nil {
		t.Fatal("expected to have failed")
	}
}
1896

1897 1898 1899 1900
func TestV1ProtocolOverride(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

Adin Schmahmann's avatar
Adin Schmahmann committed
1901 1902
	d1 := setupDHT(ctx, t, false, V1ProtocolOverride("/myproto"))
	d2 := setupDHT(ctx, t, false, V1ProtocolOverride("/myproto"))
1903 1904 1905
	d3 := setupDHT(ctx, t, false, V1ProtocolOverride("/myproto2"))
	d4 := setupDHT(ctx, t, false)

Adin Schmahmann's avatar
Adin Schmahmann committed
1906
	dhts := []*IpfsDHT{d1, d2, d3, d4}
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922

	for i, dout := range dhts {
		for _, din := range dhts[i+1:] {
			connectNoSync(t, ctx, dout, din)
		}
	}

	wait(t, ctx, d1, d2)
	wait(t, ctx, d2, d1)

	time.Sleep(time.Second)

	if d1.RoutingTable().Size() != 1 || d2.routingTable.Size() != 1 {
		t.Fatal("should have one peer in the routing table")
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
1923
	if d3.RoutingTable().Size() > 0 || d4.RoutingTable().Size() > 0 {
1924 1925 1926 1927
		t.Fatal("should have an empty routing table")
	}
}

1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
func TestRoutingFilter(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	nDHTs := 2
	dhts := setupDHTS(t, ctx, nDHTs)
	defer func() {
		for i := 0; i < nDHTs; i++ {
			dhts[i].Close()
			defer dhts[i].host.Close()
		}
	}()
	dhts[0].routingTablePeerFilter = PublicRoutingTableFilter

	connectNoSync(t, ctx, dhts[0], dhts[1])
	wait(t, ctx, dhts[1], dhts[0])

	select {
	case <-ctx.Done():
		t.Fatal(ctx.Err())
	case <-time.After(time.Millisecond * 200):
	}
}
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977

func TestBootStrapWhenRTIsEmpty(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// create three boostrap peers each of which is connected to 1 other peer.
	nBootStraps := 3
	bootstrappers := setupDHTS(t, ctx, nBootStraps)
	defer func() {
		for i := 0; i < nBootStraps; i++ {
			bootstrappers[i].Close()
			defer bootstrappers[i].host.Close()
		}
	}()

	bootstrapcons := setupDHTS(t, ctx, nBootStraps)
	defer func() {
		for i := 0; i < nBootStraps; i++ {
			bootstrapcons[i].Close()
			defer bootstrapcons[i].host.Close()
		}
	}()
	for i := 0; i < nBootStraps; i++ {
		connect(t, ctx, bootstrappers[i], bootstrapcons[i])
	}

	// convert the bootstrap addresses to a p2p address
1978
	bootstrapAddrs := make([]peer.AddrInfo, nBootStraps)
1979
	for i := 0; i < nBootStraps; i++ {
1980 1981 1982
		b := peer.AddrInfo{ID: bootstrappers[i].self,
			Addrs: bootstrappers[i].host.Addrs()}
		bootstrapAddrs[i] = b
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
	}

	//----------------
	// We will initialize a DHT with 1 bootstrapper, connect it to another DHT,
	// then remove the latter from the Routing Table
	// This should add the bootstrap peer and the peer that the bootstrap peer is conencted to
	// to it's Routing Table.
	// AutoRefresh needs to be enabled for this.
	dht1, err := New(
		ctx,
		bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)),
		testPrefix,
		NamespacedValidator("v", blankValidator{}),
		Mode(ModeServer),
		BootstrapPeers(bootstrapAddrs[0]),
	)
	require.NoError(t, err)
	dht2 := setupDHT(ctx, t, false)
	defer func() {
		dht1.host.Close()
		dht2.host.Close()
		dht1.Close()
		dht2.Close()
	}()
	connect(t, ctx, dht1, dht2)
	require.NoError(t, dht2.Close())
	require.NoError(t, dht2.host.Close())
	require.NoError(t, dht1.host.Network().ClosePeer(dht2.self))
	dht1.routingTable.RemovePeer(dht2.self)
	require.NotContains(t, dht2.self, dht1.routingTable.ListPeers())
	require.Eventually(t, func() bool {
		return dht1.routingTable.Size() == 2 && dht1.routingTable.Find(bootstrappers[0].self) != "" &&
			dht1.routingTable.Find(bootstrapcons[0].self) != ""
	}, 5*time.Second, 500*time.Millisecond)

	//----------------
	// We will initialize a DHT with 2 bootstrappers, connect it to another DHT,
	// then remove the DHT handler from the other DHT which should make the first DHT's
	// routing table empty.
	// This should add the bootstrap peers and the peer thats the bootstrap peers are connected to
	// to it's Routing Table.
	// AutoRefresh needs to be enabled for this.
	dht1, err = New(
		ctx,
		bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport)),
		testPrefix,
		NamespacedValidator("v", blankValidator{}),
		Mode(ModeServer),
		BootstrapPeers(bootstrapAddrs[1], bootstrapAddrs[2]),
	)
	require.NoError(t, err)

	dht2 = setupDHT(ctx, t, false)
	connect(t, ctx, dht1, dht2)
	defer func() {
		dht1.host.Close()
		dht2.host.Close()
		dht1.Close()
		dht2.Close()
	}()
	connect(t, ctx, dht1, dht2)
	require.NoError(t, dht2.setMode(modeClient))

	require.Eventually(t, func() bool {
		rt := dht1.routingTable

		return rt.Size() == 4 && rt.Find(bootstrappers[1].self) != "" &&
			rt.Find(bootstrappers[2].self) != "" && rt.Find(bootstrapcons[1].self) != "" && rt.Find(bootstrapcons[2].self) != ""
	}, 5*time.Second, 500*time.Millisecond)
}
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107

func TestPreconnectedNodes(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	// If this test fails it may hang so set a timeout
	ctx, cancel = context.WithTimeout(ctx, time.Second*10)
	defer cancel()

	opts := []Option{
		testPrefix,
		DisableAutoRefresh(),
		Mode(ModeServer),
	}

	// Create hosts
	h1 := bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport))
	h2 := bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport))

	// Setup first DHT
	d1, err := New(ctx, h1, opts...)
	if err != nil {
		t.Fatal(err)
	}

	// Connect the first host to the second
	if err := h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()}); err != nil {
		t.Fatal(err)
	}

	// Wait until we know identify has completed by checking for supported protocols
	// TODO: Is this needed? Could we do h2.Connect(h1) and that would wait for identify to complete.
	for {
		h1Protos, err := h2.Peerstore().SupportsProtocols(h1.ID(), d1.protocolsStrs...)
		if err != nil {
			t.Fatal(err)
		}

		if len(h1Protos) > 0 {
			break
		}

		select {
		case <-time.After(time.Millisecond * 100):
		case <-ctx.Done():
			t.Fatal("test hung")
		}
	}

	// Setup the second DHT
	d2, err := New(ctx, h2, opts...)
	if err != nil {
		t.Fatal(err)
	}

	// See if it works
2108
	peers, err := d2.GetClosestPeers(ctx, "testkey")
2109 2110 2111 2112
	if err != nil {
		t.Fatal(err)
	}

2113 2114 2115 2116 2117
	if len(peers) != 1 {
		t.Fatal("why is there more than one peer?")
	}

	if peers[0] != h1.ID() {
2118 2119 2120
		t.Fatal("could not find peer")
	}
}