From 6e2435211fa0dc20b40eaf47618512667cf59af1 Mon Sep 17 00:00:00 2001 From: Forrest Weston Date: Wed, 7 Oct 2015 10:45:57 -0700 Subject: [PATCH] Added an xml decoder, Fixes #1612 License: MIT Signed-off-by: Forrest Weston --- core/commands/object.go | 26 +++++++- test/sharness/t0051-object-data/brokenPut.xml | 1 + test/sharness/t0051-object-data/testPut.xml | 1 + test/sharness/t0051-object.sh | 65 +++++++++++++++---- 4 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 test/sharness/t0051-object-data/brokenPut.xml create mode 100644 test/sharness/t0051-object-data/testPut.xml diff --git a/core/commands/object.go b/core/commands/object.go index e8929c61f..2b6a1494e 100644 --- a/core/commands/object.go +++ b/core/commands/object.go @@ -3,6 +3,7 @@ package commands import ( "bytes" "encoding/json" + "encoding/xml" "errors" "fmt" "io" @@ -689,7 +690,7 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, 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 { + if NodeEmpty(node) { return nil, ErrEmptyNode } @@ -701,6 +702,24 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err case objectEncodingProtobuf: dagnode, err = dag.Decoded(data) + case objectEncodingXML: + node := new(Node) + err = xml.Unmarshal(data, node) + if err != nil { + 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 NodeEmpty(node) { + return nil, ErrEmptyNode + } + + dagnode, err = deserializeNode(node) + if err != nil { + return nil, err + } + default: return nil, ErrUnknownObjectEnc } @@ -725,6 +744,7 @@ type objectEncoding string const ( objectEncodingJSON objectEncoding = "json" objectEncodingProtobuf = "protobuf" + objectEncodingXML = "xml" ) func getObjectEnc(o interface{}) objectEncoding { @@ -779,3 +799,7 @@ func deserializeNode(node *Node) (*dag.Node, error) { return dagnode, nil } + +func NodeEmpty(node *Node) bool { + return (node.Data == "" && len(node.Links) == 0) +} diff --git a/test/sharness/t0051-object-data/brokenPut.xml b/test/sharness/t0051-object-data/brokenPut.xml new file mode 100644 index 000000000..331bbac99 --- /dev/null +++ b/test/sharness/t0051-object-data/brokenPut.xml @@ -0,0 +1 @@ +This is not a valid dag object fail diff --git a/test/sharness/t0051-object-data/testPut.xml b/test/sharness/t0051-object-data/testPut.xml new file mode 100644 index 000000000..5cc290b27 --- /dev/null +++ b/test/sharness/t0051-object-data/testPut.xml @@ -0,0 +1 @@ +Test xml for sharness test diff --git a/test/sharness/t0051-object.sh b/test/sharness/t0051-object.sh index edf94b88b..a718f20b6 100755 --- a/test/sharness/t0051-object.sh +++ b/test/sharness/t0051-object.sh @@ -32,25 +32,25 @@ test_object_cmd() { printf "Hello Mars" >expected_in && ipfs add expected_in >actual_Addout ' - + test_expect_success "'ipfs add testData' output looks good" ' HASH="QmWkHFpYBZ9mpPRreRbMhhYWXfUhBAue3JkbbpFqwowSRb" && echo "added $HASH expected_in" >expected_Addout && test_cmp expected_Addout actual_Addout ' - + test_expect_success "'ipfs object get' succeeds" ' ipfs object get $HASH >actual_getOut ' - + test_expect_success "'ipfs object get' output looks good" ' test_cmp ../t0051-object-data/expected_getOut actual_getOut ' - + test_expect_success "'ipfs object stat' succeeds" ' ipfs object stat $HASH >actual_stat ' - + test_expect_success "'ipfs object get' output looks good" ' echo "NumLinks: 0" > expected_stat && echo "BlockSize: 18" >> expected_stat && @@ -63,47 +63,84 @@ test_object_cmd() { test_expect_success "'ipfs object put file.json' succeeds" ' ipfs object put ../t0051-object-data/testPut.json > actual_putOut ' - + test_expect_success "'ipfs object put file.json' output looks good" ' HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" && printf "added $HASH" > expected_putOut && test_cmp expected_putOut actual_putOut ' - + + test_expect_success "'ipfs object put file.xml' succeeds" ' + ipfs object put ../t0051-object-data/testPut.xml --inputenc=xml > actual_putOut + ' + + test_expect_success "'ipfs object put file.xml' output looks good" ' + HASH="QmQzNKUHy4HyEUGkqKe3q3t796ffPLQXYCkHCcXUNT5JNK" && + printf "added $HASH" > expected_putOut && + test_cmp expected_putOut actual_putOut + ' + + test_expect_success "'ipfs object put' from stdin succeeds" ' + cat ../t0051-object-data/testPut.xml | ipfs object put --inputenc=xml > actual_putStdinOut + ' + + test_expect_success "'ipfs object put broken.xml' should fail" ' + test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.xml --inputenc=xml 2>actual_putBrokenErr >actual_putBroken + ' + + test_expect_success "'ipfs object put broken.hxml' 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_expect_success "'ipfs object get --enc=xml' succeeds" ' + ipfs object get --enc=xml $HASH >utf8_xml + ' + + test_expect_success "'ipfs object put --inputenc=xml' succeeds" ' + ipfs object put --inputenc=xml actual + ' + + test_expect_failure "'ipfs object put --inputenc=xml' output looks good" ' + echo "added $HASH" >expected && + test_cmp expected actual + ' + test_expect_success "'ipfs object put file.pb' succeeds" ' ipfs object put --inputenc=protobuf ../t0051-object-data/testPut.pb > actual_putOut ' - + test_expect_success "'ipfs object put file.pb' output looks good" ' HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" && printf "added $HASH" > expected_putOut && test_cmp expected_putOut actual_putOut ' - + test_expect_success "'ipfs object put' from stdin succeeds" ' cat ../t0051-object-data/testPut.json | ipfs object put > actual_putStdinOut ' - + test_expect_success "'ipfs object put' from stdin output looks good" ' HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" && printf "added $HASH" > expected_putStdinOut && test_cmp expected_putStdinOut actual_putStdinOut ' - + test_expect_success "'ipfs object put' from stdin (pb) succeeds" ' cat ../t0051-object-data/testPut.pb | ipfs object put --inputenc=protobuf > actual_putPbStdinOut ' - + test_expect_success "'ipfs object put' from stdin (pb) output looks good" ' HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" && printf "added $HASH" > expected_putStdinOut && 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 &&