Skip to content

Commit 2109cbc

Browse files
committed
coreapi: Name tests
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 396c34b commit 2109cbc

File tree

3 files changed

+185
-7
lines changed

3 files changed

+185
-7
lines changed

core/coreapi/interface/interface.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ type KeyAPI interface {
157157
WithType(algorithm string) options.KeyGenerateOption
158158

159159
// 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
161164
WithSize(size int) options.KeyGenerateOption
162165

163166
// Rename renames oldName key to newName. Returns the key and whether another

core/coreapi/name_test.go

+144
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coreapi_test
33
import (
44
"bytes"
55
"context"
6+
"encoding/base64"
67
"io"
78
"math"
89
"strings"
@@ -12,15 +13,16 @@ import (
1213
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
1314
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1415
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
16+
keystore "github.com/ipfs/go-ipfs/keystore"
1517
mdag "github.com/ipfs/go-ipfs/merkledag"
1618
repo "github.com/ipfs/go-ipfs/repo"
1719
config "github.com/ipfs/go-ipfs/repo/config"
1820
ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2"
1921
unixfs "github.com/ipfs/go-ipfs/unixfs"
2022

23+
peer "gx/ipfs/QmWNY7dV54ZDYmTA1ykVdwNCqC11mpU4zSUp6XDpLTH9eG/go-libp2p-peer"
24+
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
2125
cbor "gx/ipfs/QmeZv9VXw2SfVbX55LV6kGTWASKBc9ZxAVqGBeJcDGdoXy/go-ipld-cbor"
22-
23-
"github.com/ipfs/go-ipfs/keystore"
2426
)
2527

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

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+
3766
r := &repo.Mock{
3867
C: config.Config{
39-
Identity: config.Identity{
40-
PeerID: "Qmfoo", // required by offline node
41-
},
68+
Identity: ident,
4269
},
4370
D: ds2.ThreadSafeCloserMapDatastore(),
4471
K: keystore.NewMemKeystore(),
@@ -51,6 +78,10 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
5178
return node, api, nil
5279
}
5380

81+
func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
82+
return makeAPIIdent(ctx, false)
83+
}
84+
5485
func TestAdd(t *testing.T) {
5586
ctx := context.Background()
5687
_, api, err := makeAPI(ctx)

0 commit comments

Comments
 (0)