dht.go 3.17 KB
Newer Older
1
package tests
2 3 4 5 6 7

import (
	"context"
	"io"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
8 9
	"github.com/ipfs/interface-go-ipfs-core"
	"github.com/ipfs/interface-go-ipfs-core/options"
10 11
)

12
func (tp *provider) TestDht(t *testing.T) {
13 14 15 16 17 18 19
	tp.hasApi(t, func(api iface.CoreAPI) error {
		if api.Dht() == nil {
			return apiNotImplemented
		}
		return nil
	})

20 21 22
	t.Run("TestDhtFindPeer", tp.TestDhtFindPeer)
	t.Run("TestDhtFindProviders", tp.TestDhtFindProviders)
	t.Run("TestDhtProvide", tp.TestDhtProvide)
23 24
}

25
func (tp *provider) TestDhtFindPeer(t *testing.T) {
26 27
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
28
	apis, err := tp.MakeAPISwarm(ctx, true, 5)
29 30 31 32 33 34 35 36 37
	if err != nil {
		t.Fatal(err)
	}

	self0, err := apis[0].Key().Self(ctx)
	if err != nil {
		t.Fatal(err)
	}

38 39 40 41 42 43 44 45
	laddrs0, err := apis[0].Swarm().LocalAddrs(ctx)
	if err != nil {
		t.Fatal(err)
	}
	if len(laddrs0) != 1 {
		t.Fatal("unexpected number of local addrs")
	}

46 47 48 49 50
	pi, err := apis[2].Dht().FindPeer(ctx, self0.ID())
	if err != nil {
		t.Fatal(err)
	}

51
	if pi.Addrs[0].String() != laddrs0[0].String() {
52 53 54 55 56 57 58 59 60 61 62 63 64
		t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String())
	}

	self2, err := apis[2].Key().Self(ctx)
	if err != nil {
		t.Fatal(err)
	}

	pi, err = apis[1].Dht().FindPeer(ctx, self2.ID())
	if err != nil {
		t.Fatal(err)
	}

65 66 67 68 69 70 71 72 73
	laddrs2, err := apis[2].Swarm().LocalAddrs(ctx)
	if err != nil {
		t.Fatal(err)
	}
	if len(laddrs2) != 1 {
		t.Fatal("unexpected number of local addrs")
	}

	if pi.Addrs[0].String() != laddrs2[0].String() {
74 75 76 77
		t.Errorf("got unexpected address from FindPeer: %s", pi.Addrs[0].String())
	}
}

78
func (tp *provider) TestDhtFindProviders(t *testing.T) {
79 80
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
81
	apis, err := tp.MakeAPISwarm(ctx, true, 5)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	if err != nil {
		t.Fatal(err)
	}

	p, err := addTestObject(ctx, apis[0])
	if err != nil {
		t.Fatal(err)
	}

	out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
	if err != nil {
		t.Fatal(err)
	}

	provider := <-out

	self0, err := apis[0].Key().Self(ctx)
	if err != nil {
		t.Fatal(err)
	}

	if provider.ID.String() != self0.ID().String() {
		t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String())
	}
}

108
func (tp *provider) TestDhtProvide(t *testing.T) {
109 110
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
111
	apis, err := tp.MakeAPISwarm(ctx, true, 5)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	if err != nil {
		t.Fatal(err)
	}

	off0, err := apis[0].WithOptions(options.Api.Offline(true))
	if err != nil {
		t.Fatal(err)
	}

	s, err := off0.Block().Put(ctx, &io.LimitedReader{R: rnd, N: 4092})
	if err != nil {
		t.Fatal(err)
	}

	p := s.Path()

	out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
133 134 135 136 137
	_, ok := <-out

	if ok {
		t.Fatal("did not expect to find any providers")
	}
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

	self0, err := apis[0].Key().Self(ctx)
	if err != nil {
		t.Fatal(err)
	}

	err = apis[0].Dht().Provide(ctx, p)
	if err != nil {
		t.Fatal(err)
	}

	out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
	if err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
154
	provider := <-out
155 156 157 158 159

	if provider.ID.String() != self0.ID().String() {
		t.Errorf("got wrong provider: %s != %s", provider.ID.String(), self0.ID().String())
	}
}