-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: migrating some sharness tests for name cmd
- Loading branch information
Showing
10 changed files
with
170 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.