-
Notifications
You must be signed in to change notification settings - Fork 6
/
compat_test.go
378 lines (360 loc) · 9.23 KB
/
compat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package dagpb
// mirrored in JavaScript @ https://github.com/ipld/js-dag-pb/blob/master/test/test-compat.js
import (
"bytes"
"encoding/hex"
"encoding/json"
"strings"
"testing"
cid "github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)
var dataZero []byte = make([]byte, 0)
var dataSome []byte = []byte{0, 1, 2, 3, 4}
var acid cid.Cid = _mkcid()
var zeroName string = ""
var someName string = "some name"
var zeroTsize uint64 = 0
var someTsize uint64 = 1010
var largeTsize uint64 = 9007199254740991 // JavaScript Number.MAX_SAFE_INTEGER
type testCase struct {
name string
node *pbNode
expectedBytes string
expectedForm string
encodeError string
decodeError string
testEncode bool
}
var testCases = []testCase{
{
name: "empty",
node: &pbNode{},
expectedBytes: "",
expectedForm: `{
"Links": []
}`,
encodeError: "missing required fields: Links",
},
{
name: "Data zero",
node: &pbNode{data: dataZero},
expectedBytes: "0a00",
expectedForm: `{
"Data": "",
"Links": []
}`,
encodeError: "missing required fields: Links",
},
{
name: "Data some",
node: &pbNode{data: dataSome},
expectedBytes: "0a050001020304",
expectedForm: `{
"Data": "0001020304",
"Links": []
}`,
encodeError: "missing required fields: Links",
},
{
name: "Data some, short",
node: &pbNode{data: dataSome},
expectedBytes: "0a0500010203",
decodeError: "unexpected EOF",
encodeError: "missing required fields: Links",
},
{
name: "Links zero",
node: &pbNode{links: []pbLink{}},
expectedBytes: "",
expectedForm: `{
"Links": []
}`,
},
{
name: "Data some Links zero",
node: &pbNode{data: dataSome, links: []pbLink{}},
expectedBytes: "0a050001020304",
expectedForm: `{
"Data": "0001020304",
"Links": []
}`,
},
{
name: "Links empty",
node: &pbNode{links: []pbLink{{}}},
expectedBytes: "1200",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Data some Links empty",
node: &pbNode{data: dataSome, links: []pbLink{{}}},
expectedBytes: "12000a050001020304",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash zero",
expectedBytes: "12020a00",
decodeError: "expected CID", // error should come up from go-cid too
},
{
name: "Links Hash some",
node: &pbNode{links: []pbLink{{hash: acid}}},
expectedBytes: "120b0a09015500050001020304",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304"
}
]
}`,
},
{
name: "Links Hash some, short",
node: &pbNode{links: []pbLink{{hash: acid}}},
expectedBytes: "120b0a090155000500010203",
decodeError: "unexpected EOF",
testEncode: false,
}, {
name: "Links Name zero",
node: &pbNode{links: []pbLink{{name: zeroName, hasName: true}}},
expectedBytes: "12021200",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Name zero",
node: &pbNode{links: []pbLink{{hash: acid, name: zeroName, hasName: true}}},
expectedBytes: "120d0a090155000500010203041200",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Name": ""
}
]
}`,
},
{
name: "Links Name some",
node: &pbNode{links: []pbLink{{name: someName, hasName: true}}},
expectedBytes: "120b1209736f6d65206e616d65",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Name some",
node: &pbNode{links: []pbLink{{hash: acid, name: someName, hasName: true}}},
expectedBytes: "12160a090155000500010203041209736f6d65206e616d65",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Name": "some name"
}
]
}`,
},
{
name: "Links Tsize zero",
node: &pbNode{links: []pbLink{{tsize: zeroTsize, hasTsize: true}}},
expectedBytes: "12021800",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Tsize zero",
node: &pbNode{links: []pbLink{{hash: acid, tsize: zeroTsize, hasTsize: true}}},
expectedBytes: "120d0a090155000500010203041800",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Tsize": 0
}
]
}`,
},
{
name: "Links Tsize some",
node: &pbNode{links: []pbLink{{tsize: someTsize, hasTsize: true}}},
expectedBytes: "120318f207",
encodeError: "missing required fields: Hash",
decodeError: "expected CID",
},
{
name: "Links Hash some Tsize some",
node: &pbNode{links: []pbLink{{hash: acid, tsize: largeTsize, hasTsize: true}}},
expectedBytes: "12140a0901550005000102030418ffffffffffffff0f",
expectedForm: `{
"Links": [
{
"Hash": "015500050001020304",
"Tsize": 9007199254740991
}
]
}`,
},
}
func TestCompat(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
verifyRoundTrip(t, tc)
})
}
}
func verifyRoundTrip(t *testing.T, tc testCase) {
var err error
var actualBytes string
var actualForm string
if tc.node != nil {
node := buildNode(*tc.node)
actualBytes, err = nodeToString(t, node)
if tc.testEncode {
if tc.encodeError != "" {
if err != nil {
if !strings.Contains(err.Error(), tc.encodeError) {
t.Fatalf("got unexpeced encode error: [%v] (expected [%v])", err.Error(), tc.encodeError)
}
} else {
t.Fatalf("did not get expected encode error: %v", tc.encodeError)
}
} else {
if err != nil {
t.Fatal(err)
} else {
if actualBytes != tc.expectedBytes {
t.Logf(
"Expected bytes: [%v]\nGot: [%v]\n",
tc.expectedBytes,
actualBytes)
t.Error("Did not match")
}
}
}
}
}
actualForm, err = bytesToFormString(t, tc.expectedBytes, basicnode.Prototype__Map{}.NewBuilder())
if tc.decodeError != "" {
if err != nil {
if !strings.Contains(err.Error(), tc.decodeError) {
t.Fatalf("got unexpeced decode error: [%v] (expected [%v])", err.Error(), tc.decodeError)
}
} else {
t.Fatalf("did not get expected decode error: %v", tc.decodeError)
}
} else {
if err != nil {
t.Fatal(err)
}
if actualForm != tc.expectedForm {
t.Logf(
"Expected form: [%v]\nGot: [%v]\n",
tc.expectedForm,
actualForm)
t.Error("Did not match")
}
}
}
func buildNode(n pbNode) ipld.Node {
return fluent.MustBuildMap(basicnode.Prototype__Map{}, 2, func(fma fluent.MapAssembler) {
if n.data != nil {
fma.AssembleEntry("Data").AssignBytes(n.data)
}
if n.links != nil {
fma.AssembleEntry("Links").CreateList(int64(len(n.links)), func(fla fluent.ListAssembler) {
for _, link := range n.links {
fla.AssembleValue().CreateMap(3, func(fma fluent.MapAssembler) {
if link.hasName {
fma.AssembleEntry("Name").AssignString(link.name)
}
if link.hasTsize {
fma.AssembleEntry("Tsize").AssignInt(int64(link.tsize))
}
if link.hash.ByteLen() != 0 {
fma.AssembleEntry("Hash").AssignLink(cidlink.Link{Cid: link.hash})
}
})
}
})
}
})
}
func nodeToString(t *testing.T, node ipld.Node) (string, error) {
var buf bytes.Buffer
err := Marshal(node, &buf)
if err != nil {
return "", err
}
h := hex.EncodeToString(buf.Bytes())
t.Logf("[%v]\n", h)
return h, nil
}
func bytesToFormString(t *testing.T, bytesHex string, nb ipld.NodeBuilder) (string, error) {
byts, err := hex.DecodeString(bytesHex)
if err != nil {
return "", err
}
if err = Unmarshal(nb, bytes.NewReader(byts)); err != nil {
return "", err
}
node := nb.Build()
str, err := json.MarshalIndent(cleanPBNode(t, node), "", "\t")
if err != nil {
return "", err
}
return string(str), nil
}
// convert a ipld.Node (PBLink) into a map for clean JSON marshalling
func cleanPBLink(t *testing.T, link ipld.Node) map[string]interface{} {
if link == nil {
return nil
}
nl := make(map[string]interface{})
hash, _ := link.LookupByString("Hash")
if hash != nil {
l, _ := hash.AsLink()
cl, _ := l.(cidlink.Link)
nl["Hash"] = hex.EncodeToString(cl.Bytes())
}
name, _ := link.LookupByString("Name")
if name != nil {
name, _ := name.AsString()
nl["Name"] = name
}
tsize, _ := link.LookupByString("Tsize")
if tsize != nil {
tsize, _ := tsize.AsInt()
nl["Tsize"] = tsize
}
return nl
}
// convert an ipld.Node (PBNode) into a map for clean JSON marshalling
func cleanPBNode(t *testing.T, node ipld.Node) map[string]interface{} {
nn := make(map[string]interface{})
data, _ := node.LookupByString("Data")
if data != nil {
byts, _ := data.AsBytes()
nn["Data"] = hex.EncodeToString(byts)
}
links, _ := node.LookupByString("Links")
if links != nil {
linksList := make([]map[string]interface{}, links.Length())
linksIter := links.ListIterator()
for !linksIter.Done() {
ii, link, _ := linksIter.Next()
linksList[ii] = cleanPBLink(t, link)
}
nn["Links"] = linksList
}
return nn
}
func _mkcid() cid.Cid {
_, c, _ := cid.CidFromBytes([]byte{1, 85, 0, 5, 0, 1, 2, 3, 4})
return c
}