@cryptotaxi247 / kubo / commits / 2109cbc17

coreapi: Name tests

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Jan 2, 2018 at 00:53 UTC 2109cbc1728b25c3f57d67cc920bc37dbb9cf194
3 files changed +185 -7
core/coreapi/interface/interface.go
+4 -1
@@ -157,7 +157,10 @@ type KeyAPI interface {
157 WithType(algorithm string) options.KeyGenerateOption
158
159 // WithSize is an option for Generate which specifies the size of the key to
160 - // generated. Default is 0
160 + // generated. Default is -1
161 + //
162 + // value of -1 means 'use default size for key type':
163 + // * 2048 for RSA
164 WithSize(size int) options.KeyGenerateOption
165
166 // Rename renames oldName key to newName. Returns the key and whether another
core/coreapi/name_test.go new
+144
@@ -0,0 +1,144 @@
1 +package coreapi_test
2 +
3 +import (
4 + "context"
5 + "io"
6 + "math/rand"
7 + "testing"
8 + "time"
9 +
10 + ipath "github.com/ipfs/go-ipfs/path"
11 +
12 + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
13 +)
14 +
15 +var rnd = rand.New(rand.NewSource(0x62796532303137))
16 +
17 +func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) {
18 + return api.Unixfs().Add(ctx, &io.LimitedReader{R: rnd, N: 4092})
19 +}
20 +
21 +func TestBasicPublishResolve(t *testing.T) {
22 + ctx := context.Background()
23 + n, api, err := makeAPIIdent(ctx, true)
24 + if err != nil {
25 + t.Fatal(err)
26 + return
27 + }
28 +
29 + p, err := addTestObject(ctx, api)
30 + if err != nil {
31 + t.Fatal(err)
32 + return
33 + }
34 +
35 + e, err := api.Name().Publish(ctx, p)
36 + if err != nil {
37 + t.Fatal(err)
38 + return
39 + }
40 +
41 + if e.Name() != n.Identity.Pretty() {
42 + t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
43 + }
44 +
45 + if e.Value().String() != p.String() {
46 + t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
47 + }
48 +
49 + resPath, err := api.Name().Resolve(ctx, e.Name())
50 + if err != nil {
51 + t.Fatal(err)
52 + return
53 + }
54 +
55 + if resPath.String() != p.String() {
56 + t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
57 + }
58 +}
59 +
60 +func TestBasicPublishResolveKey(t *testing.T) {
61 + ctx := context.Background()
62 + _, api, err := makeAPIIdent(ctx, true)
63 + if err != nil {
64 + t.Fatal(err)
65 + return
66 + }
67 +
68 + k, err := api.Key().Generate(ctx, "foo")
69 + if err != nil {
70 + t.Fatal(err)
71 + return
72 + }
73 +
74 + p, err := addTestObject(ctx, api)
75 + if err != nil {
76 + t.Fatal(err)
77 + return
78 + }
79 +
80 + e, err := api.Name().Publish(ctx, p, api.Name().WithKey(k.Name()))
81 + if err != nil {
82 + t.Fatal(err)
83 + return
84 + }
85 +
86 + if ipath.Join([]string{"/ipns", e.Name()}) != k.Path().String() {
87 + t.Errorf("expected e.Name to equal '%s', got '%s'", e.Name(), k.Path().String())
88 + }
89 +
90 + if e.Value().String() != p.String() {
91 + t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
92 + }
93 +
94 + resPath, err := api.Name().Resolve(ctx, e.Name())
95 + if err != nil {
96 + t.Fatal(err)
97 + return
98 + }
99 +
100 + if resPath.String() != p.String() {
101 + t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
102 + }
103 +}
104 +
105 +func TestBasicPublishResolveTimeout(t *testing.T) {
106 + t.Skip("ValidTime doesn't appear to work at this time resolution")
107 +
108 + ctx := context.Background()
109 + n, api, err := makeAPIIdent(ctx, true)
110 + if err != nil {
111 + t.Fatal(err)
112 + return
113 + }
114 +
115 + p, err := addTestObject(ctx, api)
116 + if err != nil {
117 + t.Fatal(err)
118 + return
119 + }
120 +
121 + e, err := api.Name().Publish(ctx, p, api.Name().WithValidTime(time.Millisecond*100))
122 + if err != nil {
123 + t.Fatal(err)
124 + return
125 + }
126 +
127 + if e.Name() != n.Identity.Pretty() {
128 + t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
129 + }
130 +
131 + if e.Value().String() != p.String() {
132 + t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
133 + }
134 +
135 + time.Sleep(time.Second)
136 +
137 + _, err = api.Name().Resolve(ctx, e.Name())
138 + if err == nil {
139 + t.Fatal("Expected an error")
140 + return
141 + }
142 +}
143 +
144 +//TODO: When swarm api is created, add multinode tests
core/coreapi/unixfs_test.go
+37 -6
@@ -3,6 +3,7 @@ package coreapi_test
3 import (
4 "bytes"
5 "context"
6 + "encoding/base64"
7 "io"
8 "math"
9 "strings"
@@ -12,15 +13,16 @@ import (
13 coreapi "github.com/ipfs/go-ipfs/core/coreapi"
14 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
15 coreunix "github.com/ipfs/go-ipfs/core/coreunix"
16 + keystore "github.com/ipfs/go-ipfs/keystore"
17 mdag "github.com/ipfs/go-ipfs/merkledag"
18 repo "github.com/ipfs/go-ipfs/repo"
19 config "github.com/ipfs/go-ipfs/repo/config"
20 ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
21 unixfs "github.com/ipfs/go-ipfs/unixfs"
22
23 + peer "gx/ipfs/QmWNY7dV54ZDYmTA1ykVdwNCqC11mpU4zSUp6XDpLTH9eG/go-libp2p-peer"
24 + ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
25 cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor"
22 -
23 - "github.com/ipfs/go-ipfs/keystore"
26 )
27
28 // `echo -n 'hello, world!' | ipfs add`
@@ -33,12 +35,37 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs
35 // `echo -n | ipfs add`
36 var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
37
36 -func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
38 +func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) {
39 + var ident config.Identity
40 + if fullIdentity {
41 + sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
42 + if err != nil {
43 + return nil, nil, err
44 + }
45 +
46 + id, err := peer.IDFromPublicKey(pk)
47 + if err != nil {
48 + return nil, nil, err
49 + }
50 +
51 + kbytes, err := sk.Bytes()
52 + if err != nil {
53 + return nil, nil, err
54 + }
55 +
56 + ident = config.Identity{
57 + PeerID: id.Pretty(),
58 + PrivKey: base64.StdEncoding.EncodeToString(kbytes),
59 + }
60 + } else {
61 + ident = config.Identity{
62 + PeerID: "Qmfoo",
63 + }
64 + }
65 +
66 r := &repo.Mock{
67 C: config.Config{
39 - Identity: config.Identity{
40 - PeerID: "Qmfoo", // required by offline node
41 - },
68 + Identity: ident,
69 },
70 D: ds2.ThreadSafeCloserMapDatastore(),
71 K: keystore.NewMemKeystore(),
@@ -51,6 +78,10 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
78 return node, api, nil
79 }
80
81 +func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
82 + return makeAPIIdent(ctx, false)
83 +}
84 +
85 func TestAdd(t *testing.T) {
86 ctx := context.Background()
87 _, api, err := makeAPI(ctx)