|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ipfs/kubo/test/cli/harness" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestFilesCp(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + t.Run("files cp with valid UnixFS succeeds", func(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + node := harness.NewT(t).NewNode().Init().StartDaemon() |
| 21 | + |
| 22 | + // Create simple text file |
| 23 | + data := "testing files cp command" |
| 24 | + cid := node.IPFSAddStr(data) |
| 25 | + |
| 26 | + // Copy form IPFS => MFS |
| 27 | + res := node.IPFS("files", "cp", fmt.Sprintf("/ipfs/%s", cid), "/valid-file") |
| 28 | + assert.NoError(t, res.Err) |
| 29 | + |
| 30 | + // verification |
| 31 | + catRes := node.IPFS("files", "read", "/valid-file") |
| 32 | + assert.Equal(t, data, catRes.Stdout.Trimmed()) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("files cp with unsupported DAG node type fails", func(t *testing.T) { |
| 36 | + t.Parallel() |
| 37 | + node := harness.NewT(t).NewNode().Init().StartDaemon() |
| 38 | + |
| 39 | + // MFS UnixFS is limited to dag-pb or raw, so we create a dag-cbor node to test this |
| 40 | + jsonData := `{"data": "not a UnixFS node"}` |
| 41 | + tempFile := filepath.Join(node.Dir, "test.json") |
| 42 | + err := os.WriteFile(tempFile, []byte(jsonData), 0644) |
| 43 | + require.NoError(t, err) |
| 44 | + cid := node.IPFS("dag", "put", "--input-codec=json", "--store-codec=dag-cbor", tempFile).Stdout.Trimmed() |
| 45 | + |
| 46 | + // copy without --force |
| 47 | + res := node.RunIPFS("files", "cp", fmt.Sprintf("/ipfs/%s", cid), "/invalid-file") |
| 48 | + assert.NotEqual(t, 0, res.ExitErr.ExitCode()) |
| 49 | + assert.Contains(t, res.Stderr.String(), "Error: cp: source must be a valid UnixFS (dag-pb or raw codec)") |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("files cp with invalid UnixFS data structure fails", func(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + node := harness.NewT(t).NewNode().Init().StartDaemon() |
| 55 | + |
| 56 | + // Create an invalid proto file |
| 57 | + data := []byte{0xDE, 0xAD, 0xBE, 0xEF} // Invalid protobuf data |
| 58 | + tempFile := filepath.Join(node.Dir, "invalid-proto.bin") |
| 59 | + err := os.WriteFile(tempFile, data, 0644) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + res := node.IPFS("block", "put", "--format=raw", tempFile) |
| 63 | + require.NoError(t, res.Err) |
| 64 | + |
| 65 | + // we manually changed codec from raw to dag-pb to test "bad dag-pb" scenario |
| 66 | + cid := "bafybeic7pdbte5heh6u54vszezob3el6exadoiw4wc4ne7ny2x7kvajzkm" |
| 67 | + |
| 68 | + // should fail because node cant be read as a valid dag-pb |
| 69 | + cpResNoForce := node.RunIPFS("files", "cp", fmt.Sprintf("/ipfs/%s", cid), "/invalid-proto") |
| 70 | + assert.NotEqual(t, 0, cpResNoForce.ExitErr.ExitCode()) |
| 71 | + assert.Contains(t, cpResNoForce.Stderr.String(), "Error") |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("files cp with raw node succeeds", func(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + node := harness.NewT(t).NewNode().Init().StartDaemon() |
| 77 | + |
| 78 | + // Create a raw node |
| 79 | + data := "raw data" |
| 80 | + tempFile := filepath.Join(node.Dir, "raw.bin") |
| 81 | + err := os.WriteFile(tempFile, []byte(data), 0644) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + res := node.IPFS("block", "put", "--format=raw", tempFile) |
| 85 | + require.NoError(t, res.Err) |
| 86 | + cid := res.Stdout.Trimmed() |
| 87 | + |
| 88 | + // Copy from IPFS to MFS (raw nodes should work without --force) |
| 89 | + cpRes := node.IPFS("files", "cp", fmt.Sprintf("/ipfs/%s", cid), "/raw-file") |
| 90 | + assert.NoError(t, cpRes.Err) |
| 91 | + |
| 92 | + // Verify the file was copied correctly |
| 93 | + catRes := node.IPFS("files", "read", "/raw-file") |
| 94 | + assert.Equal(t, data, catRes.Stdout.Trimmed()) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("files cp creates intermediate directories with -p", func(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + node := harness.NewT(t).NewNode().Init().StartDaemon() |
| 100 | + |
| 101 | + // Create a simple text file and add it to IPFS |
| 102 | + data := "hello parent directories" |
| 103 | + tempFile := filepath.Join(node.Dir, "parent-test.txt") |
| 104 | + err := os.WriteFile(tempFile, []byte(data), 0644) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + cid := node.IPFS("add", "-Q", tempFile).Stdout.Trimmed() |
| 108 | + |
| 109 | + // Copy from IPFS to MFS with parent flag |
| 110 | + res := node.IPFS("files", "cp", "-p", fmt.Sprintf("/ipfs/%s", cid), "/parent/dir/file") |
| 111 | + assert.NoError(t, res.Err) |
| 112 | + |
| 113 | + // Verify the file and directories were created |
| 114 | + lsRes := node.IPFS("files", "ls", "/parent/dir") |
| 115 | + assert.Contains(t, lsRes.Stdout.String(), "file") |
| 116 | + |
| 117 | + catRes := node.IPFS("files", "read", "/parent/dir/file") |
| 118 | + assert.Equal(t, data, catRes.Stdout.Trimmed()) |
| 119 | + }) |
| 120 | +} |
0 commit comments