Skip to content

Commit 548490f

Browse files
Merge pull request #3516 from Zanadar/test/routing/offline
Test/routing/offline
2 parents e2ba43c + 834f223 commit 548490f

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

routing/offline/offline_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package offline
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"github.com/ipfs/go-ipfs/thirdparty/testutil"
7+
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
8+
"testing"
9+
)
10+
11+
func TestOfflineRouterStorage(t *testing.T) {
12+
ctx := context.Background()
13+
14+
nds := ds.NewMapDatastore()
15+
privkey, _, _ := testutil.RandTestKeyPair(128)
16+
offline := NewOfflineRouter(nds, privkey)
17+
18+
err := offline.PutValue(ctx, "key", []byte("testing 1 2 3"))
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
23+
val, err := offline.GetValue(ctx, "key")
24+
if !bytes.Equal([]byte("testing 1 2 3"), val) {
25+
t.Fatal("OfflineRouter does not properly store")
26+
}
27+
28+
val, err = offline.GetValue(ctx, "notHere")
29+
if err == nil {
30+
t.Fatal("Router should throw errors for unfound records")
31+
}
32+
33+
recVal, err := offline.GetValues(ctx, "key", 0)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
_, err = offline.GetValues(ctx, "notHere", 0)
39+
if err == nil {
40+
t.Fatal("Router should throw errors for unfound records")
41+
}
42+
43+
local := recVal[0].Val
44+
if !bytes.Equal([]byte("testing 1 2 3"), local) {
45+
t.Fatal("OfflineRouter does not properly store")
46+
}
47+
}
48+
49+
func TestOfflineRouterLocal(t *testing.T) {
50+
ctx := context.Background()
51+
52+
nds := ds.NewMapDatastore()
53+
privkey, _, _ := testutil.RandTestKeyPair(128)
54+
offline := NewOfflineRouter(nds, privkey)
55+
56+
id, _ := testutil.RandPeerID()
57+
_, err := offline.FindPeer(ctx, id)
58+
if err != ErrOffline {
59+
t.Fatal("OfflineRouting should alert that its offline")
60+
}
61+
62+
cid, _ := testutil.RandCidV0()
63+
pChan := offline.FindProvidersAsync(ctx, cid, 1)
64+
p, ok := <-pChan
65+
if ok {
66+
t.Fatalf("FindProvidersAsync did not return a closed channel. Instead we got %+v !", p)
67+
}
68+
69+
cid, _ = testutil.RandCidV0()
70+
err = offline.Provide(ctx, cid)
71+
if err != ErrOffline {
72+
t.Fatal("OfflineRouting should alert that its offline")
73+
}
74+
75+
err = offline.Bootstrap(ctx)
76+
if err != nil {
77+
t.Fatal("You shouldn't be able to bootstrap offline routing.")
78+
}
79+
}

thirdparty/testutil/gen.go

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
12+
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
1213
peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
1314
ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto"
1415

@@ -50,6 +51,14 @@ func RandPeerID() (peer.ID, error) {
5051
return peer.ID(h), nil
5152
}
5253

54+
func RandCidV0() (*cid.Cid, error) {
55+
buf := make([]byte, 16)
56+
if _, err := io.ReadFull(u.NewTimeSeededRand(), buf); err != nil {
57+
return &cid.Cid{}, err
58+
}
59+
return cid.NewCidV0(buf), nil
60+
}
61+
5362
func RandPeerIDFatal(t testing.TB) peer.ID {
5463
p, err := RandPeerID()
5564
if err != nil {

0 commit comments

Comments
 (0)