From 2109cbc1728b25c3f57d67cc920bc37dbb9cf194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 2 Jan 2018 00:53:48 +0100 Subject: [PATCH] coreapi: Name tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/interface.go | 5 +- core/coreapi/name_test.go | 144 ++++++++++++++++++++++++++++ core/coreapi/unixfs_test.go | 43 +++++++-- 3 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 core/coreapi/name_test.go diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index dc836566991..cdbb2508bd0 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -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 diff --git a/core/coreapi/name_test.go b/core/coreapi/name_test.go new file mode 100644 index 00000000000..74fdacda41b --- /dev/null +++ b/core/coreapi/name_test.go @@ -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 diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index fbcc2ebbb45..c4b00e4b650 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -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)