Skip to content

Commit

Permalink
coreapi: dht tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Mar 10, 2018
1 parent e3867ff commit e5690b9
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 34 deletions.
112 changes: 112 additions & 0 deletions core/coreapi/dht_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package coreapi_test

import (
"context"
"io"
"io/ioutil"
"testing"

coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"

blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
)

func TestDhtFindPeer(t *testing.T) {
ctx := context.Background()
nds, apis, err := makeAPISwarm(ctx, true, 3)
if err != nil {
t.Fatal(err)
}

out, err := apis[2].Dht().FindPeer(ctx, coreiface.PeerID(nds[0].Identity))
if err != nil {
t.Fatal(err)
}

addr := <-out

if addr.String() != "/ip4/127.0.0.1/tcp/4001" {
t.Errorf("got unexpected address from FindPeer: %s", addr.String())
}

out, err = apis[1].Dht().FindPeer(ctx, coreiface.PeerID(nds[2].Identity))
if err != nil {
t.Fatal(err)
}

addr = <-out

if addr.String() != "/ip4/127.0.2.1/tcp/4001" {
t.Errorf("got unexpected address from FindPeer: %s", addr.String())
}
}

func TestDhtFindProviders(t *testing.T) {
ctx := context.Background()
nds, apis, err := makeAPISwarm(ctx, true, 3)
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, apis[2].Dht().WithNumProviders(1))
if err != nil {
t.Fatal(err)
}

provider := <-out

if provider.String() != nds[0].Identity.String() {
t.Errorf("got wrong provider: %s != %s", provider.String(), nds[0].Identity.String())
}
}

func TestDhtProvide(t *testing.T) {
ctx := context.Background()
nds, apis, err := makeAPISwarm(ctx, true, 3)
if err != nil {
t.Fatal(err)
}

// TODO: replace once there is local add on unixfs or somewhere
data, err := ioutil.ReadAll(&io.LimitedReader{R: rnd, N: 4092})
if err != nil {
t.Fatal(err)
}

b := blocks.NewBlock(data)
nds[0].Blockstore.Put(b)
p := coreapi.ParseCid(b.Cid())

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

provider := <-out

if provider.String() != "<peer.ID >" {
t.Errorf("got wrong provider: %s != %s", provider.String(), nds[0].Identity.String())
}

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

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

provider = <-out

if provider.String() != nds[0].Identity.String() {
t.Errorf("got wrong provider: %s != %s", provider.String(), nds[0].Identity.String())
}
}
2 changes: 1 addition & 1 deletion core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Path interface {
// if we didn't, godoc would generate nice links straight to go-ipld-format
type Node ipld.Node
type Link ipld.Link
type PeerID peer.ID
type PeerID = peer.ID
type Addr ma.Multiaddr

type Reader interface {
Expand Down
12 changes: 8 additions & 4 deletions core/coreapi/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path,

func TestBasicPublishResolve(t *testing.T) {
ctx := context.Background()
n, api, err := makeAPIIdent(ctx, true)
nds, apis, err := makeAPISwarm(ctx, true, 2)
if err != nil {
t.Fatal(err)
return
}
n := nds[0]
api := apis[0]

p, err := addTestObject(ctx, api)
if err != nil {
Expand Down Expand Up @@ -59,11 +61,12 @@ func TestBasicPublishResolve(t *testing.T) {

func TestBasicPublishResolveKey(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPIIdent(ctx, true)
_, apis, err := makeAPISwarm(ctx, true, 2)
if err != nil {
t.Fatal(err)
return
}
api := apis[0]

k, err := api.Key().Generate(ctx, "foo")
if err != nil {
Expand Down Expand Up @@ -106,12 +109,13 @@ func TestBasicPublishResolveTimeout(t *testing.T) {
t.Skip("ValidTime doesn't appear to work at this time resolution")

ctx := context.Background()
n, api, err := makeAPIIdent(ctx, true)
nds, apis, err := makeAPISwarm(ctx, true, 2)
if err != nil {
t.Fatal(err)
return
}

n := nds[0]
api := apis[0]
p, err := addTestObject(ctx, api)
if err != nil {
t.Fatal(err)
Expand Down
100 changes: 71 additions & 29 deletions core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"math"
"strings"
Expand All @@ -13,6 +14,7 @@ import (
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
mock "github.com/ipfs/go-ipfs/core/mock"
keystore "github.com/ipfs/go-ipfs/keystore"
mdag "github.com/ipfs/go-ipfs/merkledag"
repo "github.com/ipfs/go-ipfs/repo"
Expand All @@ -21,6 +23,8 @@ import (
unixfs "github.com/ipfs/go-ipfs/unixfs"

cbor "gx/ipfs/QmNRz7BDWfdFNVLt7AVvmRefkrURD25EeoipcXqo6yoXU1/go-ipld-cbor"
mocknet "gx/ipfs/QmNh1kGFFdsPu79KNSaL4NUKUPb4Eiz4KHdMtFY6664RDp/go-libp2p/p2p/net/mock"
pstore "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
)
Expand All @@ -37,51 +41,89 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs
// `echo -n | ipfs add`
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)

func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) {
var ident config.Identity
if fullIdentity {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
return nil, nil, err
func makeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
mn := mocknet.New(ctx)

nodes := make([]*core.IpfsNode, n)
apis := make([]coreiface.CoreAPI, n)

for i := 0; i < n; i++ {
var ident config.Identity
if fullIdentity {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
return nil, nil, err
}

id, err := peer.IDFromPublicKey(pk)
if err != nil {
return nil, nil, err
}

kbytes, err := sk.Bytes()
if err != nil {
return nil, nil, err
}

ident = config.Identity{
PeerID: id.Pretty(),
PrivKey: base64.StdEncoding.EncodeToString(kbytes),
}
} else {
ident = config.Identity{
PeerID: testPeerID,
}
}

id, err := peer.IDFromPublicKey(pk)
if err != nil {
return nil, nil, err
c := config.Config{}
c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
c.Identity = ident

r := &repo.Mock{
C: c,
D: ds2.ThreadSafeCloserMapDatastore(),
K: keystore.NewMemKeystore(),
}

kbytes, err := sk.Bytes()
node, err := core.NewNode(ctx, &core.BuildCfg{
Repo: r,
Host: mock.MockHostOption(mn),
Online: fullIdentity,
})
if err != nil {
return nil, nil, err
}
nodes[i] = node
apis[i] = coreapi.NewCoreAPI(node)
}

ident = config.Identity{
PeerID: id.Pretty(),
PrivKey: base64.StdEncoding.EncodeToString(kbytes),
}
} else {
ident = config.Identity{
PeerID: testPeerID,
}
err := mn.LinkAll()
if err != nil {
return nil, nil, err
}

r := &repo.Mock{
C: config.Config{
Identity: ident,
bsinf := core.BootstrapConfigWithPeers(
[]pstore.PeerInfo{
nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
},
D: ds2.ThreadSafeCloserMapDatastore(),
K: keystore.NewMemKeystore(),
)

for _, n := range nodes[1:] {
if err := n.Bootstrap(bsinf); err != nil {
return nil, nil, err
}
}
node, err := core.NewNode(ctx, &core.BuildCfg{Repo: r})

return nodes, apis, nil
}

func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
nd, api, err := makeAPISwarm(ctx, false, 1)
if err != nil {
return nil, nil, err
}
api := coreapi.NewCoreAPI(node)
return node, api, nil
}

func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
return makeAPIIdent(ctx, false)
return nd[0], api[0], nil
}

func TestAdd(t *testing.T) {
Expand Down

0 comments on commit e5690b9

Please sign in to comment.