coreapi: Name tests

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera 2018-01-02 00:53:48 +01:00
parent 396c34b4e1
commit 2109cbc172
3 changed files with 185 additions and 7 deletions

View File

@ -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

144
core/coreapi/name_test.go Normal file
View File

@ -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

View File

@ -3,6 +3,7 @@ package coreapi_test
import (
"bytes"
"context"
"encoding/base64"
"io"
"math"
"strings"
@ -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`
@ -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(),
@ -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)