Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/routing/offline #3516

Merged
merged 3 commits into from
Dec 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions routing/offline/offline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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")
}
}

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.")
}
}
9 changes: 9 additions & 0 deletions thirdparty/testutil/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down