reprovide_test.go 3.68 KB
Newer Older
1 2 3 4 5 6 7
package simple_test

import (
	"context"
	"testing"
	"time"

Steven Allen's avatar
Steven Allen committed
8 9
	bsrv "github.com/ipfs/go-blockservice"
	"github.com/ipfs/go-cid"
10 11 12
	ds "github.com/ipfs/go-datastore"
	dssync "github.com/ipfs/go-datastore/sync"
	"github.com/ipfs/go-ipfs-blockstore"
Steven Allen's avatar
Steven Allen committed
13
	offline "github.com/ipfs/go-ipfs-exchange-offline"
14
	mock "github.com/ipfs/go-ipfs-routing/mock"
Steven Allen's avatar
Steven Allen committed
15 16
	cbor "github.com/ipfs/go-ipld-cbor"
	merkledag "github.com/ipfs/go-merkledag"
Raúl Kripalani's avatar
Raúl Kripalani committed
17 18
	peer "github.com/libp2p/go-libp2p-core/peer"
	testutil "github.com/libp2p/go-libp2p-testing/net"
Steven Allen's avatar
Steven Allen committed
19
	mh "github.com/multiformats/go-multihash"
20

Michael Avila's avatar
Michael Avila committed
21
	. "github.com/ipfs/go-ipfs-provider/simple"
22 23
)

Steven Allen's avatar
Steven Allen committed
24
func setupRouting(t *testing.T) (clA, clB mock.Client, idA, idB peer.ID) {
25 26
	mrserv := mock.NewServer()

Steven Allen's avatar
Steven Allen committed
27 28
	iidA := testutil.RandIdentityOrFatal(t)
	iidB := testutil.RandIdentityOrFatal(t)
29

Steven Allen's avatar
Steven Allen committed
30 31
	clA = mrserv.Client(iidA)
	clB = mrserv.Client(iidB)
32

Steven Allen's avatar
Steven Allen committed
33 34
	return clA, clB, iidA.ID(), iidB.ID()
}
35

Steven Allen's avatar
Steven Allen committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
func setupDag(t *testing.T) (nodes []cid.Cid, bstore blockstore.Blockstore) {
	bstore = blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
	for _, data := range []string{"foo", "bar"} {
		blk, err := cbor.WrapObject(data, mh.SHA2_256, -1)
		if err != nil {
			t.Fatal(err)
		}
		err = bstore.Put(blk)
		if err != nil {
			t.Fatal(err)
		}
		nodes = append(nodes, blk.Cid())

		blk, err = cbor.WrapObject(map[string]interface{}{
			"child": blk.Cid(),
		}, mh.SHA2_256, -1)
		if err != nil {
			t.Fatal(err)
		}
		err = bstore.Put(blk)
		if err != nil {
			t.Fatal(err)
		}
		nodes = append(nodes, blk.Cid())
60 61
	}

Steven Allen's avatar
Steven Allen committed
62 63 64 65 66 67 68 69 70 71
	return nodes, bstore
}

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

	clA, clB, idA, _ := setupRouting(t)
	nodes, bstore := setupDag(t)

72 73
	keyProvider := NewBlockstoreProvider(bstore)
	reprov := NewReprovider(ctx, time.Hour, clA, keyProvider)
Steven Allen's avatar
Steven Allen committed
74
	err := reprov.Reprovide()
75 76 77 78
	if err != nil {
		t.Fatal(err)
	}

Raúl Kripalani's avatar
Raúl Kripalani committed
79
	var providers []peer.AddrInfo
80 81
	maxProvs := 100

Steven Allen's avatar
Steven Allen committed
82 83 84 85 86
	for _, c := range nodes {
		provChan := clB.FindProvidersAsync(ctx, c, maxProvs)
		for p := range provChan {
			providers = append(providers, p)
		}
87

Steven Allen's avatar
Steven Allen committed
88 89 90 91 92 93 94
		if len(providers) == 0 {
			t.Fatal("Should have gotten a provider")
		}

		if providers[0].ID != idA {
			t.Fatal("Somehow got the wrong peer back as a provider.")
		}
95
	}
Steven Allen's avatar
Steven Allen committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
}

type mockPinner struct {
	recursive []cid.Cid
	direct    []cid.Cid
}

func (mp *mockPinner) DirectKeys() []cid.Cid {
	return mp.direct
}

func (mp *mockPinner) RecursiveKeys() []cid.Cid {
	return mp.recursive
}

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

	nodes, bstore := setupDag(t)

	dag := merkledag.NewDAGService(bsrv.New(bstore, offline.Exchange(bstore)))

	for i := 0; i < 2; i++ {
		clA, clB, idA, _ := setupRouting(t)

		onlyRoots := i == 0
		t.Logf("only roots: %v", onlyRoots)

		var provide, dont []cid.Cid
		if onlyRoots {
			provide = []cid.Cid{nodes[1], nodes[3]}
			dont = []cid.Cid{nodes[0], nodes[2]}
		} else {
			provide = []cid.Cid{nodes[0], nodes[1], nodes[3]}
			dont = []cid.Cid{nodes[2]}
		}

		keyProvider := NewPinnedProvider(onlyRoots, &mockPinner{
			recursive: []cid.Cid{nodes[1]},
			direct:    []cid.Cid{nodes[3]},
		}, dag)

		reprov := NewReprovider(ctx, time.Hour, clA, keyProvider)
		err := reprov.Reprovide()
		if err != nil {
			t.Fatal(err)
		}

		for i, c := range provide {
			prov, ok := <-clB.FindProvidersAsync(ctx, c, 1)
			if !ok {
				t.Errorf("Should have gotten a provider for %d", i)
				continue
			}
151

Steven Allen's avatar
Steven Allen committed
152 153 154 155 156 157 158 159 160 161 162
			if prov.ID != idA {
				t.Errorf("Somehow got the wrong peer back as a provider.")
				continue
			}
		}
		for i, c := range dont {
			prov, ok := <-clB.FindProvidersAsync(ctx, c, 1)
			if ok {
				t.Fatalf("found provider %s for %d, expected none", prov.ID, i)
			}
		}
163 164
	}
}