Fix marshaling of null cbor arrays
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com> Fix non-canonical imports via dag put License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Feb 8, 2017 at 10:47 UTC
85a06abaccab4261304d04e3366aad80170f363d
4 files changed
+41
-4
core/commands/dag/dag.go
+31
-1
@@ -3,14 +3,15 @@ package dagcmd
3
import (
4
"fmt"
5
"io"
6
+ "io/ioutil"
7
"strings"
8
9
cmds "github.com/ipfs/go-ipfs/commands"
10
path "github.com/ipfs/go-ipfs/path"
11
12
node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
13
+ ipldcbor "gx/ipfs/QmT1B6cKXnMMki8nbuhrnLuiU32HLvwi6xe99bJ79482UK/go-ipld-cbor"
14
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
13
- ipldcbor "gx/ipfs/Qmf658QLDTXfRDgnGmUB6TYj671XjmHScG61p3g7dSxUcF/go-ipld-cbor"
15
)
16
17
var DagCmd = &cmds.Command{
@@ -78,6 +79,21 @@ into an object of the specified format.
79
return
80
}
81
82
+ res.SetOutput(&OutputObject{Cid: c})
83
+ return
84
+ case "raw":
85
+ nd, err := convertRawToType(fi, format)
86
+ if err != nil {
87
+ res.SetError(err, cmds.ErrNormal)
88
+ return
89
+ }
90
+
91
+ c, err := n.DAG.Add(nd)
92
+ if err != nil {
93
+ res.SetError(err, cmds.ErrNormal)
94
+ return
95
+ }
96
+
97
res.SetOutput(&OutputObject{Cid: c})
98
return
99
default:
@@ -141,3 +157,17 @@ func convertJsonToType(r io.Reader, format string) (node.Node, error) {
157
return nil, fmt.Errorf("unknown target format: %s", format)
158
}
159
}
160
+
161
+func convertRawToType(r io.Reader, format string) (node.Node, error) {
162
+ switch format {
163
+ case "cbor", "dag-cbor":
164
+ data, err := ioutil.ReadAll(r)
165
+ if err != nil {
166
+ return nil, err
167
+ }
168
+
169
+ return ipldcbor.Decode(data)
170
+ default:
171
+ return nil, fmt.Errorf("unsupported target format for raw input: %s", format)
172
+ }
173
+}
merkledag/merkledag.go
+1
-1
@@ -13,8 +13,8 @@ import (
13
14
node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
15
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
16
+ ipldcbor "gx/ipfs/QmT1B6cKXnMMki8nbuhrnLuiU32HLvwi6xe99bJ79482UK/go-ipld-cbor"
17
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
17
- ipldcbor "gx/ipfs/Qmf658QLDTXfRDgnGmUB6TYj671XjmHScG61p3g7dSxUcF/go-ipld-cbor"
18
)
19
20
var log = logging.Logger("merkledag")
package.json
+2
-2
@@ -272,9 +272,9 @@
272
},
273
{
274
"author": "whyrusleeping",
275
- "hash": "Qmf658QLDTXfRDgnGmUB6TYj671XjmHScG61p3g7dSxUcF",
275
+ "hash": "QmT1B6cKXnMMki8nbuhrnLuiU32HLvwi6xe99bJ79482UK",
276
"name": "go-ipld-cbor",
277
- "version": "1.1.0"
277
+ "version": "1.1.8"
278
},
279
{
280
"author": "lgierth",
test/sharness/t0053-dag.sh
+7
@@ -90,6 +90,13 @@ test_dag_cmd() {
90
echo "{\"data\":\"CAISBGZvbwoYBA==\",\"links\":[]}" > cat_exp &&
91
test_cmp cat_exp cat_out
92
'
93
+
94
+ test_expect_success "non-canonical cbor input is normalized" '
95
+ DATA="\xa2\x65\x61\x6c\x69\x6e\x6b\xd8\x2a\x58\x25\x00\x01\x71\x12\x20\x65\x96\x50\xfc\x34\x43\xc9\x16\x42\x80\x48\xef\xc5\xba\x45\x58\xdc\x86\x35\x94\x98\x0a\x59\xf5\xcb\x3c\x4d\x84\x86\x7e\x6d\x31\x63\x61\x72\x72\x80"
96
+ HASH=$(echo "$DATA" | ipfs dag put --format=cbor --input-enc=raw) &&
97
+ test $HASH = "zdpuAyaAXrG12ax4EWuMxvFqBQuzwMGTW8C7NXAotCrcTnSXP" ||
98
+ test_fsh echo $HASH
99
+ '
100
}
101
102
# should work offline