From 695c22a7d1ddce4b1bba94dc062eb217cc20f40d Mon Sep 17 00:00:00 2001 From: Zander Mackie Date: Tue, 13 Dec 2016 07:46:29 -0500 Subject: [PATCH 1/3] Tests for OfflineRouting storage and Retrieval License: MIT Signed-off-by: Zander Mackie --- routing/offline/offline_test.go | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 routing/offline/offline_test.go diff --git a/routing/offline/offline_test.go b/routing/offline/offline_test.go new file mode 100644 index 00000000000..5f031a14ade --- /dev/null +++ b/routing/offline/offline_test.go @@ -0,0 +1,48 @@ +package offline + +import ( + "bytes" + "context" + "github.com/ipfs/go-ipfs/thirdparty/testutil" + ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore" + "testing" +) + +func TestOfflineRouterStorage(t *testing.T) { + ctx := context.Background() + + nds := ds.NewMapDatastore() + privkey, _, _ := testutil.RandTestKeyPair(128) + offline := NewOfflineRouter(nds, privkey) + + err := offline.PutValue(ctx, "key", []byte("testing 1 2 3")) + if err != nil { + t.Fatal(err) + } + + val, err := offline.GetValue(ctx, "key") + if !bytes.Equal([]byte("testing 1 2 3"), val) { + t.Fatal("OfflineRouter does not properly store") + } + + val, err = offline.GetValue(ctx, "notHere") + if err == nil { + t.Fatal("Router should throw errors for unfound records") + } + + recVal, err := offline.GetValues(ctx, "key", 0) + if err != nil { + t.Fatal(err) + } + + _, err = offline.GetValues(ctx, "notHere", 0) + if err == nil { + t.Fatal("Router should throw errors for unfound records") + } + + local := recVal[0].Val + if !bytes.Equal([]byte("testing 1 2 3"), local) { + t.Fatal("OfflineRouter does not properly store") + } +} + From 058bfc3b54dfba9ccd64770e5fce08196b7e1ec7 Mon Sep 17 00:00:00 2001 From: Zander Mackie Date: Thu, 15 Dec 2016 07:20:31 -0500 Subject: [PATCH 2/3] Testutil to gen random CidV0 License: MIT Signed-off-by: Zander Mackie --- thirdparty/testutil/gen.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/thirdparty/testutil/gen.go b/thirdparty/testutil/gen.go index 9212b4d0887..fc9e0ef45bf 100644 --- a/thirdparty/testutil/gen.go +++ b/thirdparty/testutil/gen.go @@ -9,6 +9,7 @@ import ( "testing" u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" + cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid" peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer" ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto" @@ -50,6 +51,14 @@ func RandPeerID() (peer.ID, error) { return peer.ID(h), nil } +func RandCidV0() (*cid.Cid, error) { + buf := make([]byte, 16) + if _, err := io.ReadFull(u.NewTimeSeededRand(), buf); err != nil { + return &cid.Cid{}, err + } + return cid.NewCidV0(buf), nil +} + func RandPeerIDFatal(t testing.TB) peer.ID { p, err := RandPeerID() if err != nil { From 834f22328cb3c24051d706b88aa24d6976f195ca Mon Sep 17 00:00:00 2001 From: Zander Mackie Date: Wed, 14 Dec 2016 07:25:21 -0500 Subject: [PATCH 3/3] Testing the rest of the interface License: MIT Signed-off-by: Zander Mackie --- routing/offline/offline_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/routing/offline/offline_test.go b/routing/offline/offline_test.go index 5f031a14ade..629206b4e2c 100644 --- a/routing/offline/offline_test.go +++ b/routing/offline/offline_test.go @@ -46,3 +46,34 @@ func TestOfflineRouterStorage(t *testing.T) { } } +func TestOfflineRouterLocal(t *testing.T) { + ctx := context.Background() + + nds := ds.NewMapDatastore() + privkey, _, _ := testutil.RandTestKeyPair(128) + offline := NewOfflineRouter(nds, privkey) + + id, _ := testutil.RandPeerID() + _, err := offline.FindPeer(ctx, id) + if err != ErrOffline { + t.Fatal("OfflineRouting should alert that its offline") + } + + cid, _ := testutil.RandCidV0() + pChan := offline.FindProvidersAsync(ctx, cid, 1) + p, ok := <-pChan + if ok { + t.Fatalf("FindProvidersAsync did not return a closed channel. Instead we got %+v !", p) + } + + cid, _ = testutil.RandCidV0() + err = offline.Provide(ctx, cid) + if err != ErrOffline { + t.Fatal("OfflineRouting should alert that its offline") + } + + err = offline.Bootstrap(ctx) + if err != nil { + t.Fatal("You shouldn't be able to bootstrap offline routing.") + } +}