Skip to content

Commit

Permalink
coreapi: Name tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Jan 2, 2018
1 parent 396c34b commit 2109cbc
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 7 deletions.
5 changes: 4 additions & 1 deletion core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ type KeyAPI interface {
WithType(algorithm string) options.KeyGenerateOption

// WithSize is an option for Generate which specifies the size of the key to
// generated. Default is 0
// generated. Default is -1
//
// value of -1 means 'use default size for key type':
// * 2048 for RSA
WithSize(size int) options.KeyGenerateOption

// Rename renames oldName key to newName. Returns the key and whether another
Expand Down
144 changes: 144 additions & 0 deletions core/coreapi/name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package coreapi_test

import (
"context"
"io"
"math/rand"
"testing"
"time"

ipath "github.com/ipfs/go-ipfs/path"

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

var rnd = rand.New(rand.NewSource(0x62796532303137))

func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) {
return api.Unixfs().Add(ctx, &io.LimitedReader{R: rnd, N: 4092})
}

func TestBasicPublishResolve(t *testing.T) {
ctx := context.Background()
n, api, err := makeAPIIdent(ctx, true)
if err != nil {
t.Fatal(err)
return
}

p, err := addTestObject(ctx, api)
if err != nil {
t.Fatal(err)
return
}

e, err := api.Name().Publish(ctx, p)
if err != nil {
t.Fatal(err)
return
}

if e.Name() != n.Identity.Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
}

if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}

resPath, err := api.Name().Resolve(ctx, e.Name())
if err != nil {
t.Fatal(err)
return
}

if resPath.String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
}
}

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

k, err := api.Key().Generate(ctx, "foo")
if err != nil {
t.Fatal(err)
return
}

p, err := addTestObject(ctx, api)
if err != nil {
t.Fatal(err)
return
}

e, err := api.Name().Publish(ctx, p, api.Name().WithKey(k.Name()))
if err != nil {
t.Fatal(err)
return
}

if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() {
t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String())
}

if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}

resPath, err := api.Name().Resolve(ctx, e.Name())
if err != nil {
t.Fatal(err)
return
}

if resPath.String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
}
}

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)
if err != nil {
t.Fatal(err)
return
}

p, err := addTestObject(ctx, api)
if err != nil {
t.Fatal(err)
return
}

e, err := api.Name().Publish(ctx, p, api.Name().WithValidTime(time.Millisecond*100))
if err != nil {
t.Fatal(err)
return
}

if e.Name() != n.Identity.Pretty() {
t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
}

if e.Value().String() != p.String() {
t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
}

time.Sleep(time.Second)

_, err = api.Name().Resolve(ctx, e.Name())
if err == nil {
t.Fatal("Expected an error")
return
}
}

//TODO: When swarm api is created, add multinode tests
43 changes: 37 additions & 6 deletions core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coreapi_test
import (
"bytes"
"context"
"encoding/base64"
"io"
"math"
"strings"
Expand All @@ -12,15 +13,16 @@ 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"
keystore "github.com/ipfs/go-ipfs/keystore"
mdag "github.com/ipfs/go-ipfs/merkledag"
repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config"
ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
unixfs "github.com/ipfs/go-ipfs/unixfs"

peer "gx/ipfs/QmWNY7dV54ZDYmTA1ykVdwNCqC11mpU4zSUp6XDpLTH9eG/go-libp2p-peer"
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor"

"github.com/ipfs/go-ipfs/keystore"
)

// `echo -n 'hello, world!' | ipfs add`
Expand All @@ -33,12 +35,37 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs
// `echo -n | ipfs add`
var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)

func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
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
}

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: "Qmfoo",
}
}

r := &repo.Mock{
C: config.Config{
Identity: config.Identity{
PeerID: "Qmfoo", // required by offline node
},
Identity: ident,
},
D: ds2.ThreadSafeCloserMapDatastore(),
K: keystore.NewMemKeystore(),
Expand All @@ -51,6 +78,10 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
return node, api, nil
}

func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
return makeAPIIdent(ctx, false)
}

func TestAdd(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
Expand Down

0 comments on commit 2109cbc

Please sign in to comment.