Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit b68b08e

Browse files
committed
test: interop testing and bug fixing
1 parent 64440a2 commit b68b08e

16 files changed

+215
-2
lines changed

src/util.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ const resolver = require('./resolver')
1313
const CID_CBOR_TAG = 42
1414

1515
function tagCID (cid) {
16-
return new cbor.Tagged(CID_CBOR_TAG, cid)
16+
return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
17+
new Buffer('00', 'hex'), // thanks jdag
18+
cid
19+
]))
1720
}
1821

1922
const decoder = new cbor.Decoder({
2023
tags: {
21-
[CID_CBOR_TAG]: (val) => ({'/': val})
24+
[CID_CBOR_TAG]: (val) => {
25+
// remove that 0
26+
val = val.slice(1)
27+
return {'/': val}
28+
}
2229
}
2330
})
2431

test/fixtures/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Extracted from: https://github.com/ipfs/go-ipld-cbor/tree/master/test_objects

test/fixtures/array-link.cbor

40 Bytes
Binary file not shown.

test/fixtures/array-link.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"/":"QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"}]

test/fixtures/empty-array.cbor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/fixtures/empty-array.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

test/fixtures/empty-obj.cbor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/fixtures/empty-obj.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/fixtures/expected.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"array-link": {"/":"zdpuAsZ8roJvCazJFXUnSULspbXfxrLkUkNxy6SToaiDpEiap"},
3+
"empty-array": {"/":"zdpuAtQy7GSHNcZxdBfmtowdL1d2WAFjJBwb6WAEfFJ6T4Gbi"},
4+
"empty-obj": {"/":"zdpuAyTBnYSugBZhqJuLsNpzjmAjSmxDqBbtAqXMtsvxiN2v3"},
5+
"foo": {"/":"zdpuAtu6ZgHLyiGZAX743EurWhSr9ixbVbrtFaxbr8Z8Q16gW"},
6+
"obj-with-link": {"/":"zdpuAp1WRqfq8w953Dep9pz7nNYUbQPKX9duW71G5C4doiPFK"},
7+
"obj-no-link": {"/":"zdpuAtHFXfzgXBSoq5GSn3NTrK2ToYwj4tRZiMTfe2mVApYLa"}
8+
}

test/fixtures/foo.cbor

176 Bytes
Binary file not shown.

test/fixtures/foo.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"foo": "bar",
3+
"cats": [
4+
{"/": "QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"},
5+
{
6+
"something": "interesting"
7+
},
8+
[
9+
"fish",
10+
{"/": "QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"},
11+
9
12+
]
13+
],
14+
"other": {"/": "QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"}
15+
}

test/fixtures/obj-no-link.cbor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�isassafrashand cats

test/fixtures/obj-no-link.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sassafras": "and cats"
3+
}

test/fixtures/obj-with-link.cbor

44 Bytes
Binary file not shown.

test/fixtures/obj-with-link.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": {"/":"QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"}
3+
}

test/interop.spec.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const expect = require('chai').expect
7+
const dagCBOR = require('../src')
8+
const loadFixture = require('aegir/fixtures')
9+
const bs58 = require('bs58')
10+
const isNode = require('detect-node')
11+
12+
const arrayLinkCBOR = loadFixture(__dirname, '/fixtures/array-link.cbor')
13+
const arrayLinkJSON = require('./fixtures/array-link.json')
14+
15+
const emptyArrayCBOR = loadFixture(__dirname, '/fixtures/empty-array.cbor')
16+
const emptyArrayJSON = require('./fixtures/empty-array.json')
17+
18+
const emptyObjCBOR = loadFixture(__dirname, '/fixtures/empty-obj.cbor')
19+
const emptyObjJSON = require('./fixtures/empty-obj.json')
20+
21+
const fooCBOR = loadFixture(__dirname, '/fixtures/foo.cbor')
22+
const fooJSON = require('./fixtures/foo.json')
23+
24+
const objNoLinkCBOR = loadFixture(__dirname, '/fixtures/obj-no-link.cbor')
25+
const objNoLinkJSON = require('./fixtures/obj-no-link.json')
26+
27+
const objWithLinkCBOR = loadFixture(__dirname, '/fixtures/obj-with-link.cbor')
28+
// const objWithLinkJSON = require('./fixtures/obj-with-link.json')
29+
30+
const expectedCIDs = require('./fixtures/expected.json')
31+
32+
describe.only('dag-cbor interop tests', () => {
33+
// the fixtures feature needs to be fixed
34+
if (!isNode) { return }
35+
36+
describe('deserialize and compare', () => {
37+
it('array-link', (done) => {
38+
dagCBOR.util.deserialize(arrayLinkCBOR, (err, node) => {
39+
expect(err).to.not.exist
40+
// the JSON version that gets out of go-ipfs stringifies the CID
41+
const bs58Str = bs58.encode(node[0]['/'])
42+
43+
node[0]['/'] = bs58Str
44+
expect(node).to.eql(arrayLinkJSON)
45+
46+
// put it back to bytes
47+
node[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])
48+
49+
dagCBOR.util.cid(node, (err, cid) => {
50+
expect(err).to.not.exist
51+
const cidStr = cid.toBaseEncodedString()
52+
expect(cidStr).to.eql(expectedCIDs['array-link']['/'])
53+
done()
54+
})
55+
})
56+
})
57+
58+
it('empty-array', (done) => {
59+
dagCBOR.util.deserialize(emptyArrayCBOR, (err, node) => {
60+
expect(err).to.not.exist
61+
expect(node).to.eql(emptyArrayJSON)
62+
63+
dagCBOR.util.cid(node, (err, cid) => {
64+
expect(err).to.not.exist
65+
const cidStr = cid.toBaseEncodedString()
66+
expect(cidStr).to.eql(expectedCIDs['empty-array']['/'])
67+
done()
68+
})
69+
})
70+
})
71+
72+
it('empty-obj', (done) => {
73+
dagCBOR.util.deserialize(emptyObjCBOR, (err, node) => {
74+
expect(err).to.not.exist
75+
expect(node).to.eql(emptyObjJSON)
76+
77+
dagCBOR.util.cid(node, (err, cid) => {
78+
expect(err).to.not.exist
79+
const cidStr = cid.toBaseEncodedString()
80+
expect(cidStr).to.eql(expectedCIDs['empty-obj']['/'])
81+
done()
82+
})
83+
})
84+
})
85+
86+
it.skip('foo', (done) => {
87+
dagCBOR.util.deserialize(fooCBOR, (err, node) => {
88+
expect(err).to.not.exist
89+
expect(node).to.eql(fooJSON)
90+
91+
dagCBOR.util.cid(node, (err, cid) => {
92+
expect(err).to.not.exist
93+
const cidStr = cid.toBaseEncodedString()
94+
expect(cidStr).to.eql(expectedCIDs['foo']['/'])
95+
done()
96+
})
97+
})
98+
})
99+
100+
it('obj-no-link', (done) => {
101+
dagCBOR.util.deserialize(objNoLinkCBOR, (err, node) => {
102+
expect(err).to.not.exist
103+
expect(node).to.eql(objNoLinkJSON)
104+
105+
dagCBOR.util.cid(node, (err, cid) => {
106+
expect(err).to.not.exist
107+
const cidStr = cid.toBaseEncodedString()
108+
expect(cidStr).to.eql(expectedCIDs['obj-no-link']['/'])
109+
done()
110+
})
111+
})
112+
})
113+
114+
it('obj-with-link', (done) => {
115+
if (!isNode) { done() }
116+
117+
dagCBOR.util.deserialize(objWithLinkCBOR, (err, node) => {
118+
expect(err).to.not.exist
119+
120+
dagCBOR.util.cid(node, (err, cid) => {
121+
expect(err).to.not.exist
122+
const cidStr = cid.toBaseEncodedString()
123+
expect(cidStr).to.eql(expectedCIDs['obj-with-link']['/'])
124+
done()
125+
})
126+
})
127+
})
128+
})
129+
130+
describe('serialise and compare', () => {
131+
it('array-link', (done) => {
132+
arrayLinkJSON[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])
133+
134+
dagCBOR.util.serialize(arrayLinkJSON, (err, serialized) => {
135+
expect(err).to.not.exist
136+
137+
expect(serialized).to.eql(arrayLinkCBOR)
138+
done()
139+
})
140+
})
141+
142+
it('empty-array', (done) => {
143+
dagCBOR.util.serialize(emptyArrayJSON, (err, serialized) => {
144+
expect(err).to.not.exist
145+
expect(serialized).to.eql(emptyArrayCBOR)
146+
done()
147+
})
148+
})
149+
150+
it('empty-obj', (done) => {
151+
dagCBOR.util.serialize(emptyObjJSON, (err, serialized) => {
152+
expect(err).to.not.exist
153+
expect(serialized).to.eql(emptyObjCBOR)
154+
done()
155+
})
156+
})
157+
158+
it.skip('foo', (done) => {})
159+
160+
it('obj-no-link', (done) => {
161+
dagCBOR.util.serialize(objNoLinkJSON, (err, serialized) => {
162+
expect(err).to.not.exist
163+
expect(serialized).to.eql(objNoLinkCBOR)
164+
done()
165+
})
166+
})
167+
168+
it.skip('obj-with-link', (done) => {})
169+
})
170+
})

0 commit comments

Comments
 (0)