|
| 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 | +} |
0 commit comments