Skip to content

Commit

Permalink
wip: migrating some sharness tests for name cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jun 15, 2023
1 parent c8d00d1 commit b1a2c07
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 198 deletions.
23 changes: 6 additions & 17 deletions core/commands/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/libp2p/go-libp2p/core/crypto"
mbase "github.com/multiformats/go-multibase"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -83,9 +82,9 @@ Resolve the value of a dnslink:
}

type IpnsInspectValidation struct {
Valid bool
Reason string
PublicKey string
Valid bool
Reason string
Name string
}

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

pk, err := ipns.ExtractPublicKey(rec, name)
if err != nil {
return err
}

bytes, err := crypto.MarshalPublicKey(pk)
if err != nil {
return err
}

result.Validation = &IpnsInspectValidation{
PublicKey: encoder.Encode(bytes),
Name: name.String(),
}

err = ipns.Validate(rec, pk)
err = ipns.ValidateWithName(rec, name)
if err == nil {
result.Validation.Valid = true
} else {
Expand Down Expand Up @@ -278,7 +267,7 @@ Passing --verify will verify signature against provided public key.
if out.Validation.Reason != "" {
fmt.Fprintf(tw, "\tReason:\t%s\n", out.Validation.Reason)
}
fmt.Fprintf(tw, "\tPublicKey:\t%s\n", out.Validation.PublicKey)
fmt.Fprintf(tw, "\tName:\t%s\n", out.Validation.Name)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.18
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.27.5
github.com/multiformats/go-multiaddr v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
github.com/ipfs/go-block-format v0.1.2
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-cidutil v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down
Binary file added test/cli/fixtures/TestName.car
Binary file not shown.
154 changes: 154 additions & 0 deletions test/cli/name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package cli

import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"testing"

"github.com/ipfs/boxo/ipns"
"github.com/ipfs/kubo/core/commands/name"
"github.com/ipfs/kubo/test/cli/harness"
"github.com/stretchr/testify/require"
)

func TestName(t *testing.T) {
const (
fixturePath = "fixtures/TestName.car"
fixtureCid = "bafybeicxptj5cposoeaj5cwk3to7kivguoz6rdj56dr2aogzkeodzqfzfi"
)

makeDaemon := func(t *testing.T, initArgs []string, startArgs []string) *harness.Node {
node := harness.NewT(t).NewNode().Init(initArgs...).StartDaemon(startArgs...)
r, err := os.Open(fixturePath)
require.Nil(t, err)
defer r.Close()
err = node.IPFSDagImport(r, fixtureCid)
require.NoError(t, err)
return node
}

testPublishingWithSelf := func(keyType string) {
t.Run("Publishing with self (keyType = "+keyType+")", func(t *testing.T) {
t.Parallel()

args := []string{}
if keyType != "default" {
args = append(args, "-a="+keyType)
}

node := makeDaemon(t, args, nil)

t.Run("Publishing a CID", func(t *testing.T) {
name := ipns.NameFromPeer(node.PeerID())
publishPath := "/ipfs/" + fixtureCid

res := node.IPFS("name", "publish", "--allow-offline", publishPath)
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name.String(), publishPath), res.Stdout.String())

res = node.IPFS("name", "resolve", "/ipns/"+name.String())
require.Equal(t, publishPath+"\n", res.Stdout.String())
})

t.Run("Publishing a CID with -Q option", func(t *testing.T) {
name := ipns.NameFromPeer(node.PeerID())
publishPath := "/ipfs/" + fixtureCid

res := node.IPFS("name", "publish", "--allow-offline", "-Q", publishPath)
require.Equal(t, name.String()+"\n", res.Stdout.String())

res = node.IPFS("name", "resolve", "/ipns/"+name.String())
require.Equal(t, publishPath+"\n", res.Stdout.String())
})

t.Run("Publishing a CID+Path", func(t *testing.T) {
name := ipns.NameFromPeer(node.PeerID())
publishPath := "/ipfs/" + fixtureCid + "/hello"

res := node.IPFS("name", "publish", "--allow-offline", publishPath)
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name.String(), publishPath), res.Stdout.String())

res = node.IPFS("name", "resolve", "/ipns/"+name.String())
require.Equal(t, publishPath+"\n", res.Stdout.String())
})

t.Run("Publishing Nothing Fails", func(t *testing.T) {
res := node.RunIPFS("name", "publish")
require.Error(t, res.Err)
require.Equal(t, 1, res.ExitCode())
require.Contains(t, res.Stderr.String(), `argument "ipfs-path" is required`)
})
})
}

testPublishingWithSelf("default")
testPublishingWithSelf("rsa")
testPublishingWithSelf("ed25519")

testPublishWithKey := func(name string, keyArgs ...string) {
t.Run(name, func(t *testing.T) {
t.Parallel()
node := makeDaemon(t, nil, nil)

keyGenArgs := []string{"key", "gen"}
keyGenArgs = append(keyGenArgs, keyArgs...)
keyGenArgs = append(keyGenArgs, "key")

res := node.IPFS(keyGenArgs...)
key := strings.TrimSpace(res.Stdout.String())

publishPath := "/ipfs/" + fixtureCid
name, err := ipns.NameFromString(key)
require.NoError(t, err)

res = node.IPFS("name", "publish", "--allow-offline", "--key="+key, publishPath)
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name.String(), publishPath), res.Stdout.String())
})
}

testPublishWithKey("Publishing with RSA (with b58mh) Key", "--ipns-base=b58mh", "--type=rsa", "--size=2048")
testPublishWithKey("Publishing with ED25519 (with b58mh) Key", "--ipns-base=b58mh", "--type=ed25519")
testPublishWithKey("Publishing with ED25519 (with base36) Key", "--ipns-base=base36", "--type=ed25519")

t.Run("Inspect with verification using wrong RSA key errors", func(t *testing.T) {
t.Parallel()
node := makeDaemon(t, nil, nil)

// Prepare RSA Key 1
res := node.IPFS("key", "gen", "--type=rsa", "--size=4096", "key1")
key1 := strings.TrimSpace(res.Stdout.String())
name1, err := ipns.NameFromString(key1)
require.NoError(t, err)

// Prepare RSA Key 2
res = node.IPFS("key", "gen", "--type=rsa", "--size=4096", "key2")
key2 := strings.TrimSpace(res.Stdout.String())
name2, err := ipns.NameFromString(key2)
require.NoError(t, err)

// Publish using Key 1
publishPath := "/ipfs/" + fixtureCid
res = node.IPFS("name", "publish", "--allow-offline", "--key="+key1, publishPath)
require.Equal(t, fmt.Sprintf("Published to %s: %s\n", name1.String(), publishPath), res.Stdout.String())

// Get IPNS Record
res = node.IPFS("routing", "get", ipns.NamespacePrefix+name1.String())
record := res.Stdout.Bytes()

// Validate with correct key succeeds
res = node.PipeToIPFS(bytes.NewReader(record), "name", "inspect", "--verify="+name1.String(), "--enc=json")
val := name.IpnsInspectResult{}
err = json.Unmarshal(res.Stdout.Bytes(), &val)
require.NoError(t, err)
require.True(t, val.Validation.Valid)

// Validate with wrong key fails
res = node.PipeToIPFS(bytes.NewReader(record), "name", "inspect", "--verify="+name2.String(), "--enc=json")
val = name.IpnsInspectResult{}
err = json.Unmarshal(res.Stdout.Bytes(), &val)
require.NoError(t, err)
require.False(t, val.Validation.Valid)
})
}
2 changes: 1 addition & 1 deletion test/dependencies/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ replace github.com/ipfs/kubo => ../../
require (
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd
github.com/golangci/golangci-lint v1.49.0
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-cidutil v0.1.0
github.com/ipfs/go-datastore v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions test/dependencies/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-block-format v0.1.2 h1:GAjkfhVx1f4YTODS6Esrj1wt2HhrtwTnhEr+DyPUaJo=
Expand Down
Loading

0 comments on commit b1a2c07

Please sign in to comment.