From b688e72de05cef362e310c90f9968a0614830adf Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 10 Mar 2015 22:58:50 +0100 Subject: [PATCH] ipfs object put: return error if object is empty (fixes #883) --- core/commands/object.go | 9 +++++++++ test/sharness/t0051-object-data/brokenPut.json | 5 +++++ test/sharness/t0051-object.sh | 11 +++++++++++ 3 files changed, 25 insertions(+) create mode 100644 test/sharness/t0051-object-data/brokenPut.json diff --git a/core/commands/object.go b/core/commands/object.go index 91cf389fd..7e8771f2a 100644 --- a/core/commands/object.go +++ b/core/commands/object.go @@ -379,6 +379,9 @@ func objectGet(n *core.IpfsNode, fpath path.Path) (*dag.Node, error) { return dagnode, nil } +// ErrEmptyNode is returned when the input to 'ipfs object put' contains no data +var ErrEmptyNode = errors.New("no data or links in this node") + // objectPut takes a format option, serializes bytes from stdin and updates the dag with that data func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, error) { var ( @@ -404,6 +407,12 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err return nil, err } + // check that we have data in the Node to add + // otherwise we will add the empty object without raising an error + if node.Data == "" && len(node.Links) == 0 { + return nil, ErrEmptyNode + } + dagnode, err = deserializeNode(node) if err != nil { return nil, err diff --git a/test/sharness/t0051-object-data/brokenPut.json b/test/sharness/t0051-object-data/brokenPut.json new file mode 100644 index 000000000..6ba2d6f3b --- /dev/null +++ b/test/sharness/t0051-object-data/brokenPut.json @@ -0,0 +1,5 @@ +{ + "this": "should", + "return": "an", + "error":"not valid dag object" +} \ No newline at end of file diff --git a/test/sharness/t0051-object.sh b/test/sharness/t0051-object.sh index 37a025d60..da5ff3ef3 100755 --- a/test/sharness/t0051-object.sh +++ b/test/sharness/t0051-object.sh @@ -82,5 +82,16 @@ test_expect_success "'ipfs object put' from stdin (pb) output looks good" ' test_cmp expected_putStdinOut actual_putPbStdinOut ' +test_expect_success "'ipfs object put broken.json' should fail" ' + test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.json 2>actual_putBrokenErr >actual_putBroken +' + +test_expect_success "'ipfs object put broken.hjson' output looks good" ' + touch expected_putBroken && + printf "Error: no data or links in this node\n" > expected_putBrokenErr && + test_cmp expected_putBroken actual_putBroken && + test_cmp expected_putBrokenErr actual_putBrokenErr +' + test_done