Skip to content

Commit b1a2c07

Browse files
committed
wip: migrating some sharness tests for name cmd
1 parent c8d00d1 commit b1a2c07

File tree

10 files changed

+170
-198
lines changed

10 files changed

+170
-198
lines changed

core/commands/name/name.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ipld/go-ipld-prime"
1616
"github.com/ipld/go-ipld-prime/codec/dagcbor"
1717
"github.com/ipld/go-ipld-prime/codec/dagjson"
18-
"github.com/libp2p/go-libp2p/core/crypto"
1918
mbase "github.com/multiformats/go-multibase"
2019
"google.golang.org/protobuf/proto"
2120
)
@@ -83,9 +82,9 @@ Resolve the value of a dnslink:
8382
}
8483

8584
type IpnsInspectValidation struct {
86-
Valid bool
87-
Reason string
88-
PublicKey string
85+
Valid bool
86+
Reason string
87+
Name string
8988
}
9089

9190
// IpnsInspectEntry contains the deserialized values from an IPNS Entry:
@@ -218,21 +217,11 @@ Passing --verify will verify signature against provided public key.
218217
return err
219218
}
220219

221-
pk, err := ipns.ExtractPublicKey(rec, name)
222-
if err != nil {
223-
return err
224-
}
225-
226-
bytes, err := crypto.MarshalPublicKey(pk)
227-
if err != nil {
228-
return err
229-
}
230-
231220
result.Validation = &IpnsInspectValidation{
232-
PublicKey: encoder.Encode(bytes),
221+
Name: name.String(),
233222
}
234223

235-
err = ipns.Validate(rec, pk)
224+
err = ipns.ValidateWithName(rec, name)
236225
if err == nil {
237226
result.Validation.Valid = true
238227
} else {
@@ -278,7 +267,7 @@ Passing --verify will verify signature against provided public key.
278267
if out.Validation.Reason != "" {
279268
fmt.Fprintf(tw, "\tReason:\t%s\n", out.Validation.Reason)
280269
}
281-
fmt.Fprintf(tw, "\tPublicKey:\t%s\n", out.Validation.PublicKey)
270+
fmt.Fprintf(tw, "\tName:\t%s\n", out.Validation.Name)
282271
}
283272

284273
return nil

docs/examples/kubo-as-a-library/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go 1.18
77
replace github.com/ipfs/kubo => ./../../..
88

99
require (
10-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
10+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
1111
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
1212
github.com/libp2p/go-libp2p v0.27.5
1313
github.com/multiformats/go-multiaddr v0.9.0

docs/examples/kubo-as-a-library/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
319319
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
320320
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
321321
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
322-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
323-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
322+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
323+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
324324
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
325325
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
326326
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/fsnotify/fsnotify v1.6.0
1616
github.com/google/uuid v1.3.0
1717
github.com/hashicorp/go-multierror v1.1.1
18-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
18+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
1919
github.com/ipfs/go-block-format v0.1.2
2020
github.com/ipfs/go-cid v0.4.1
2121
github.com/ipfs/go-cidutil v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
354354
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
355355
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
356356
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
357-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
358-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
357+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
358+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
359359
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
360360
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
361361
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=

test/cli/fixtures/TestName.car

198 Bytes
Binary file not shown.

test/cli/name_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package cli
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"strings"
9+
"testing"
10+
11+
"github.com/ipfs/boxo/ipns"
12+
"github.com/ipfs/kubo/core/commands/name"
13+
"github.com/ipfs/kubo/test/cli/harness"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestName(t *testing.T) {
18+
const (
19+
fixturePath = "fixtures/TestName.car"
20+
fixtureCid = "bafybeicxptj5cposoeaj5cwk3to7kivguoz6rdj56dr2aogzkeodzqfzfi"
21+
)
22+
23+
makeDaemon := func(t *testing.T, initArgs []string, startArgs []string) *harness.Node {
24+
node := harness.NewT(t).NewNode().Init(initArgs...).StartDaemon(startArgs...)
25+
r, err := os.Open(fixturePath)
26+
require.Nil(t, err)
27+
defer r.Close()
28+
err = node.IPFSDagImport(r, fixtureCid)
29+
require.NoError(t, err)
30+
return node
31+
}
32+
33+
testPublishingWithSelf := func(keyType string) {
34+
t.Run("Publishing with self (keyType = "+keyType+")", func(t *testing.T) {
35+
t.Parallel()
36+
37+
args := []string{}
38+
if keyType != "default" {
39+
args = append(args, "-a="+keyType)
40+
}
41+
42+
node := makeDaemon(t, args, nil)
43+
44+
t.Run("Publishing a CID", func(t *testing.T) {
45+
name := ipns.NameFromPeer(node.PeerID())
46+
publishPath := "/ipfs/" + fixtureCid
47+
48+
res := node.IPFS("name", "publish", "--allow-offline", publishPath)
49+
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name.String(), publishPath), res.Stdout.String())
50+
51+
res = node.IPFS("name", "resolve", "/ipns/"+name.String())
52+
require.Equal(t, publishPath+"\n", res.Stdout.String())
53+
})
54+
55+
t.Run("Publishing a CID with -Q option", func(t *testing.T) {
56+
name := ipns.NameFromPeer(node.PeerID())
57+
publishPath := "/ipfs/" + fixtureCid
58+
59+
res := node.IPFS("name", "publish", "--allow-offline", "-Q", publishPath)
60+
require.Equal(t, name.String()+"\n", res.Stdout.String())
61+
62+
res = node.IPFS("name", "resolve", "/ipns/"+name.String())
63+
require.Equal(t, publishPath+"\n", res.Stdout.String())
64+
})
65+
66+
t.Run("Publishing a CID+Path", func(t *testing.T) {
67+
name := ipns.NameFromPeer(node.PeerID())
68+
publishPath := "/ipfs/" + fixtureCid + "/hello"
69+
70+
res := node.IPFS("name", "publish", "--allow-offline", publishPath)
71+
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name.String(), publishPath), res.Stdout.String())
72+
73+
res = node.IPFS("name", "resolve", "/ipns/"+name.String())
74+
require.Equal(t, publishPath+"\n", res.Stdout.String())
75+
})
76+
77+
t.Run("Publishing Nothing Fails", func(t *testing.T) {
78+
res := node.RunIPFS("name", "publish")
79+
require.Error(t, res.Err)
80+
require.Equal(t, 1, res.ExitCode())
81+
require.Contains(t, res.Stderr.String(), `argument "ipfs-path" is required`)
82+
})
83+
})
84+
}
85+
86+
testPublishingWithSelf("default")
87+
testPublishingWithSelf("rsa")
88+
testPublishingWithSelf("ed25519")
89+
90+
testPublishWithKey := func(name string, keyArgs ...string) {
91+
t.Run(name, func(t *testing.T) {
92+
t.Parallel()
93+
node := makeDaemon(t, nil, nil)
94+
95+
keyGenArgs := []string{"key", "gen"}
96+
keyGenArgs = append(keyGenArgs, keyArgs...)
97+
keyGenArgs = append(keyGenArgs, "key")
98+
99+
res := node.IPFS(keyGenArgs...)
100+
key := strings.TrimSpace(res.Stdout.String())
101+
102+
publishPath := "/ipfs/" + fixtureCid
103+
name, err := ipns.NameFromString(key)
104+
require.NoError(t, err)
105+
106+
res = node.IPFS("name", "publish", "--allow-offline", "--key="+key, publishPath)
107+
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name.String(), publishPath), res.Stdout.String())
108+
})
109+
}
110+
111+
testPublishWithKey("Publishing with RSA (with b58mh) Key", "--ipns-base=b58mh", "--type=rsa", "--size=2048")
112+
testPublishWithKey("Publishing with ED25519 (with b58mh) Key", "--ipns-base=b58mh", "--type=ed25519")
113+
testPublishWithKey("Publishing with ED25519 (with base36) Key", "--ipns-base=base36", "--type=ed25519")
114+
115+
t.Run("Inspect with verification using wrong RSA key errors", func(t *testing.T) {
116+
t.Parallel()
117+
node := makeDaemon(t, nil, nil)
118+
119+
// Prepare RSA Key 1
120+
res := node.IPFS("key", "gen", "--type=rsa", "--size=4096", "key1")
121+
key1 := strings.TrimSpace(res.Stdout.String())
122+
name1, err := ipns.NameFromString(key1)
123+
require.NoError(t, err)
124+
125+
// Prepare RSA Key 2
126+
res = node.IPFS("key", "gen", "--type=rsa", "--size=4096", "key2")
127+
key2 := strings.TrimSpace(res.Stdout.String())
128+
name2, err := ipns.NameFromString(key2)
129+
require.NoError(t, err)
130+
131+
// Publish using Key 1
132+
publishPath := "/ipfs/" + fixtureCid
133+
res = node.IPFS("name", "publish", "--allow-offline", "--key="+key1, publishPath)
134+
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name1.String(), publishPath), res.Stdout.String())
135+
136+
// Get IPNS Record
137+
res = node.IPFS("routing", "get", ipns.NamespacePrefix+name1.String())
138+
record := res.Stdout.Bytes()
139+
140+
// Validate with correct key succeeds
141+
res = node.PipeToIPFS(bytes.NewReader(record), "name", "inspect", "--verify="+name1.String(), "--enc=json")
142+
val := name.IpnsInspectResult{}
143+
err = json.Unmarshal(res.Stdout.Bytes(), &val)
144+
require.NoError(t, err)
145+
require.True(t, val.Validation.Valid)
146+
147+
// Validate with wrong key fails
148+
res = node.PipeToIPFS(bytes.NewReader(record), "name", "inspect", "--verify="+name2.String(), "--enc=json")
149+
val = name.IpnsInspectResult{}
150+
err = json.Unmarshal(res.Stdout.Bytes(), &val)
151+
require.NoError(t, err)
152+
require.False(t, val.Validation.Valid)
153+
})
154+
}

test/dependencies/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ replace github.com/ipfs/kubo => ../../
77
require (
88
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd
99
github.com/golangci/golangci-lint v1.49.0
10-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
10+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
1111
github.com/ipfs/go-cid v0.4.1
1212
github.com/ipfs/go-cidutil v0.1.0
1313
github.com/ipfs/go-datastore v0.6.0

test/dependencies/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
412412
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
413413
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
414414
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
415-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
416-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
415+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
416+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
417417
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
418418
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
419419
github.com/ipfs/go-block-format v0.1.2 h1:GAjkfhVx1f4YTODS6Esrj1wt2HhrtwTnhEr+DyPUaJo=

0 commit comments

Comments
 (0)