|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ipfs/kubo/test/cli/harness" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPinNameValidation(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + // Create a test node and add a test file |
| 16 | + node := harness.NewT(t).NewNode().Init().StartDaemon("--offline") |
| 17 | + defer node.StopDaemon() |
| 18 | + |
| 19 | + // Add a test file to get a CID |
| 20 | + testContent := "test content for pin name validation" |
| 21 | + testCID := node.IPFSAddStr(testContent, "--pin=false") |
| 22 | + |
| 23 | + t.Run("pin add accepts valid names", func(t *testing.T) { |
| 24 | + testCases := []struct { |
| 25 | + name string |
| 26 | + pinName string |
| 27 | + description string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "empty_name", |
| 31 | + pinName: "", |
| 32 | + description: "Empty name should be allowed", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "short_name", |
| 36 | + pinName: "test", |
| 37 | + description: "Short ASCII name should be allowed", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "max_255_bytes", |
| 41 | + pinName: strings.Repeat("a", 255), |
| 42 | + description: "Exactly 255 bytes should be allowed", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "unicode_within_limit", |
| 46 | + pinName: "测试名称🔥", // Chinese characters and emoji |
| 47 | + description: "Unicode characters within 255 bytes should be allowed", |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tc := range testCases { |
| 52 | + t.Run(tc.name, func(t *testing.T) { |
| 53 | + var args []string |
| 54 | + if tc.pinName != "" { |
| 55 | + args = []string{"pin", "add", "--name", tc.pinName, testCID} |
| 56 | + } else { |
| 57 | + args = []string{"pin", "add", testCID} |
| 58 | + } |
| 59 | + |
| 60 | + res := node.RunIPFS(args...) |
| 61 | + require.Equal(t, 0, res.ExitCode(), tc.description) |
| 62 | + |
| 63 | + // Clean up - unpin |
| 64 | + node.RunIPFS("pin", "rm", testCID) |
| 65 | + }) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("pin add rejects names exceeding 255 bytes", func(t *testing.T) { |
| 70 | + testCases := []struct { |
| 71 | + name string |
| 72 | + pinName string |
| 73 | + description string |
| 74 | + }{ |
| 75 | + { |
| 76 | + name: "256_bytes", |
| 77 | + pinName: strings.Repeat("a", 256), |
| 78 | + description: "256 bytes should be rejected", |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "300_bytes", |
| 82 | + pinName: strings.Repeat("b", 300), |
| 83 | + description: "300 bytes should be rejected", |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "unicode_exceeding_limit", |
| 87 | + pinName: strings.Repeat("测", 100), // Each Chinese character is 3 bytes, total 300 bytes |
| 88 | + description: "Unicode string exceeding 255 bytes should be rejected", |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, tc := range testCases { |
| 93 | + t.Run(tc.name, func(t *testing.T) { |
| 94 | + res := node.RunIPFS("pin", "add", "--name", tc.pinName, testCID) |
| 95 | + require.NotEqual(t, 0, res.ExitCode(), tc.description) |
| 96 | + require.Contains(t, res.Stderr.String(), "max 255 bytes", "Error should mention the 255 byte limit") |
| 97 | + }) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("pin ls with name filter validates length", func(t *testing.T) { |
| 102 | + // Test valid filter |
| 103 | + res := node.RunIPFS("pin", "ls", "--name", strings.Repeat("a", 255)) |
| 104 | + require.Equal(t, 0, res.ExitCode(), "255-byte name filter should be accepted") |
| 105 | + |
| 106 | + // Test invalid filter |
| 107 | + res = node.RunIPFS("pin", "ls", "--name", strings.Repeat("a", 256)) |
| 108 | + require.NotEqual(t, 0, res.ExitCode(), "256-byte name filter should be rejected") |
| 109 | + require.Contains(t, res.Stderr.String(), "max 255 bytes", "Error should mention the 255 byte limit") |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +func TestAddPinNameValidation(t *testing.T) { |
| 114 | + t.Parallel() |
| 115 | + |
| 116 | + node := harness.NewT(t).NewNode().Init().StartDaemon("--offline") |
| 117 | + defer node.StopDaemon() |
| 118 | + |
| 119 | + // Create a test file |
| 120 | + testFile := "test.txt" |
| 121 | + node.WriteBytes(testFile, []byte("test content for add command")) |
| 122 | + |
| 123 | + t.Run("ipfs add with --pin-name accepts valid names", func(t *testing.T) { |
| 124 | + testCases := []struct { |
| 125 | + name string |
| 126 | + pinName string |
| 127 | + description string |
| 128 | + }{ |
| 129 | + { |
| 130 | + name: "short_name", |
| 131 | + pinName: "test-add", |
| 132 | + description: "Short ASCII name should be allowed", |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "max_255_bytes", |
| 136 | + pinName: strings.Repeat("x", 255), |
| 137 | + description: "Exactly 255 bytes should be allowed", |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tc := range testCases { |
| 142 | + t.Run(tc.name, func(t *testing.T) { |
| 143 | + res := node.RunIPFS("add", fmt.Sprintf("--pin-name=%s", tc.pinName), "-q", testFile) |
| 144 | + require.Equal(t, 0, res.ExitCode(), tc.description) |
| 145 | + cid := strings.TrimSpace(res.Stdout.String()) |
| 146 | + |
| 147 | + // Verify pin exists with name |
| 148 | + lsRes := node.RunIPFS("pin", "ls", "--names", "--type=recursive", cid) |
| 149 | + require.Equal(t, 0, lsRes.ExitCode()) |
| 150 | + require.Contains(t, lsRes.Stdout.String(), tc.pinName, "Pin should have the specified name") |
| 151 | + |
| 152 | + // Clean up |
| 153 | + node.RunIPFS("pin", "rm", cid) |
| 154 | + }) |
| 155 | + } |
| 156 | + }) |
| 157 | + |
| 158 | + t.Run("ipfs add with --pin-name rejects names exceeding 255 bytes", func(t *testing.T) { |
| 159 | + testCases := []struct { |
| 160 | + name string |
| 161 | + pinName string |
| 162 | + description string |
| 163 | + }{ |
| 164 | + { |
| 165 | + name: "256_bytes", |
| 166 | + pinName: strings.Repeat("y", 256), |
| 167 | + description: "256 bytes should be rejected", |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "500_bytes", |
| 171 | + pinName: strings.Repeat("z", 500), |
| 172 | + description: "500 bytes should be rejected", |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + for _, tc := range testCases { |
| 177 | + t.Run(tc.name, func(t *testing.T) { |
| 178 | + res := node.RunIPFS("add", fmt.Sprintf("--pin-name=%s", tc.pinName), testFile) |
| 179 | + require.NotEqual(t, 0, res.ExitCode(), tc.description) |
| 180 | + require.Contains(t, res.Stderr.String(), "max 255 bytes", "Error should mention the 255 byte limit") |
| 181 | + }) |
| 182 | + } |
| 183 | + }) |
| 184 | +} |
0 commit comments