-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
chore: Basic UnixFS sanity checks #10701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 21 commits
f8a2fe5
9db46b1
3031cf3
b28bce9
e922ef0
3297308
4a8bb62
a687b2b
de94ec6
58e4c90
8e6796f
fa7c769
1296054
888e1e8
8ab8a83
d945049
34f05a2
b77142d
9f7f8b6
9a59b8d
7ad6847
548f630
5c93132
bf04ec1
23c8b6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "testing" | ||
|
|
||
| dag "github.com/ipfs/boxo/ipld/merkledag" | ||
| cmds "github.com/ipfs/go-ipfs-cmds" | ||
| coremock "github.com/ipfs/kubo/core/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFilesCp_DagCborNodeFails(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| cmdCtx, err := coremock.MockCmdsCtx() | ||
| require.NoError(t, err) | ||
|
|
||
| node, err := cmdCtx.ConstructNode() | ||
| require.NoError(t, err) | ||
|
|
||
| invalidData := []byte{0x00} | ||
| protoNode := dag.NodeWithData(invalidData) | ||
| err = node.DAG.Add(ctx, protoNode) | ||
| require.NoError(t, err) | ||
|
|
||
| req := &cmds.Request{ | ||
| Context: ctx, | ||
| Arguments: []string{ | ||
| "/ipfs/" + protoNode.Cid().String(), | ||
| "/test-destination", | ||
| }, | ||
| Options: map[string]interface{}{ | ||
| "force": false, | ||
| }, | ||
| } | ||
|
|
||
| _, pw := io.Pipe() | ||
| res, err := cmds.NewWriterResponseEmitter(pw, req) | ||
| require.NoError(t, err) | ||
|
|
||
| err = filesCpCmd.Run(req, res, &cmdCtx) | ||
| require.Error(t, err) | ||
| require.ErrorContains(t, err, "cp: source must be a UnixFS node or raw data") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/ipfs/kubo/test/cli/harness" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFilesCp(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("files cp with valid UnixFS", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| node := harness.NewT(t).NewNode().Init().StartDaemon() | ||
|
|
||
| // Create simple text file | ||
| data := "testing files cp command" | ||
| cid := node.IPFSAddStr(data) | ||
|
|
||
| // Copy form IPFS => MFS | ||
| res := node.IPFS("files", "cp", fmt.Sprintf("/ipfs/%s", cid), "/valid-file") | ||
| assert.NoError(t, res.Err) | ||
|
|
||
| // verification | ||
| catRes := node.IPFS("files", "read", "/valid-file") | ||
| assert.Equal(t, data, catRes.Stdout.Trimmed()) | ||
| }) | ||
|
|
||
| t.Run("files cp with invalid DAG node fails without force", func(t *testing.T) { | ||
| t.Parallel() | ||
| node := harness.NewT(t).NewNode().Init().StartDaemon() | ||
|
|
||
| // create a dag-cbor node | ||
| jsonData := `{"data": "not a UnixFS node"}` | ||
| tempFile := filepath.Join(node.Dir, "test.json") | ||
| err := os.WriteFile(tempFile, []byte(jsonData), 0644) | ||
| require.NoError(t, err) | ||
| cid := node.IPFS("dag", "put", "--input-codec=json", "--store-codec=dag-cbor", tempFile).Stdout.Trimmed() | ||
|
|
||
| // copy without --force | ||
| res := node.RunIPFS("files", "cp", fmt.Sprintf("/ipfs/%s", cid), "/invalid-file") | ||
| assert.NotEqual(t, 0, res.ExitErr.ExitCode()) | ||
| assert.Contains(t, res.Stderr.String(), "source must be a UnixFS") | ||
| }) | ||
|
|
||
| t.Run("files cp with invalid DAG node succeeds with force", func(t *testing.T) { | ||
|
||
| t.Parallel() | ||
| node := harness.NewT(t).NewNode().Init().StartDaemon() | ||
|
|
||
| // create dag-bor node | ||
| jsonData := `{"data": "not a UnixFS node"}` | ||
| tempFile := filepath.Join(node.Dir, "test.json") | ||
| err := os.WriteFile(tempFile, []byte(jsonData), 0644) | ||
| require.NoError(t, err) | ||
| cid := node.IPFS("dag", "put", "--input-codec=json", "--store-codec=dag-cbor", tempFile).Stdout.Trimmed() | ||
|
|
||
| // copy with --force | ||
| resWithForce := node.RunIPFS("files", "cp", "--force", fmt.Sprintf("/ipfs/%s", cid), "/forced-file") | ||
| assert.NotEqual(t, 0, resWithForce.ExitErr.ExitCode()) | ||
|
|
||
| // Verification | ||
| // Should NOT contain the validation error | ||
| assert.NotContains(t, resWithForce.Stderr.String(), "source must be a valid UnixFS") | ||
|
|
||
| // But should contain flush error instead | ||
| assert.Contains(t, resWithForce.Stderr.String(), "cannot flush the created file") | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.