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

Commit

Permalink
Merge b68b08e into 64440a2
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Feb 10, 2017
2 parents 64440a2 + b68b08e commit 6f7922a
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ const resolver = require('./resolver')
const CID_CBOR_TAG = 42

function tagCID (cid) {
return new cbor.Tagged(CID_CBOR_TAG, cid)
return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
new Buffer('00', 'hex'), // thanks jdag
cid
]))
}

const decoder = new cbor.Decoder({
tags: {
[CID_CBOR_TAG]: (val) => ({'/': val})
[CID_CBOR_TAG]: (val) => {
// remove that 0
val = val.slice(1)
return {'/': val}
}
}
})

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Extracted from: https://github.com/ipfs/go-ipld-cbor/tree/master/test_objects
Binary file added test/fixtures/array-link.cbor
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/array-link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"/":"QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"}]
1 change: 1 addition & 0 deletions test/fixtures/empty-array.cbor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 change: 1 addition & 0 deletions test/fixtures/empty-array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions test/fixtures/empty-obj.cbor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 change: 1 addition & 0 deletions test/fixtures/empty-obj.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
8 changes: 8 additions & 0 deletions test/fixtures/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"array-link": {"/":"zdpuAsZ8roJvCazJFXUnSULspbXfxrLkUkNxy6SToaiDpEiap"},
"empty-array": {"/":"zdpuAtQy7GSHNcZxdBfmtowdL1d2WAFjJBwb6WAEfFJ6T4Gbi"},
"empty-obj": {"/":"zdpuAyTBnYSugBZhqJuLsNpzjmAjSmxDqBbtAqXMtsvxiN2v3"},
"foo": {"/":"zdpuAtu6ZgHLyiGZAX743EurWhSr9ixbVbrtFaxbr8Z8Q16gW"},
"obj-with-link": {"/":"zdpuAp1WRqfq8w953Dep9pz7nNYUbQPKX9duW71G5C4doiPFK"},
"obj-no-link": {"/":"zdpuAtHFXfzgXBSoq5GSn3NTrK2ToYwj4tRZiMTfe2mVApYLa"}
}
Binary file added test/fixtures/foo.cbor
Binary file not shown.
15 changes: 15 additions & 0 deletions test/fixtures/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"foo": "bar",
"cats": [
{"/": "QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"},
{
"something": "interesting"
},
[
"fish",
{"/": "QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"},
9
]
],
"other": {"/": "QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"}
}
1 change: 1 addition & 0 deletions test/fixtures/obj-no-link.cbor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�isassafrashand cats
3 changes: 3 additions & 0 deletions test/fixtures/obj-no-link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sassafras": "and cats"
}
Binary file added test/fixtures/obj-with-link.cbor
Binary file not shown.
3 changes: 3 additions & 0 deletions test/fixtures/obj-with-link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": {"/":"QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL"}
}
170 changes: 170 additions & 0 deletions test/interop.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */

'use strict'

const expect = require('chai').expect
const dagCBOR = require('../src')
const loadFixture = require('aegir/fixtures')
const bs58 = require('bs58')
const isNode = require('detect-node')

const arrayLinkCBOR = loadFixture(__dirname, '/fixtures/array-link.cbor')
const arrayLinkJSON = require('./fixtures/array-link.json')

const emptyArrayCBOR = loadFixture(__dirname, '/fixtures/empty-array.cbor')
const emptyArrayJSON = require('./fixtures/empty-array.json')

const emptyObjCBOR = loadFixture(__dirname, '/fixtures/empty-obj.cbor')
const emptyObjJSON = require('./fixtures/empty-obj.json')

const fooCBOR = loadFixture(__dirname, '/fixtures/foo.cbor')
const fooJSON = require('./fixtures/foo.json')

const objNoLinkCBOR = loadFixture(__dirname, '/fixtures/obj-no-link.cbor')
const objNoLinkJSON = require('./fixtures/obj-no-link.json')

const objWithLinkCBOR = loadFixture(__dirname, '/fixtures/obj-with-link.cbor')
// const objWithLinkJSON = require('./fixtures/obj-with-link.json')

const expectedCIDs = require('./fixtures/expected.json')

describe.only('dag-cbor interop tests', () => {
// the fixtures feature needs to be fixed
if (!isNode) { return }

describe('deserialize and compare', () => {
it('array-link', (done) => {
dagCBOR.util.deserialize(arrayLinkCBOR, (err, node) => {
expect(err).to.not.exist
// the JSON version that gets out of go-ipfs stringifies the CID
const bs58Str = bs58.encode(node[0]['/'])

node[0]['/'] = bs58Str
expect(node).to.eql(arrayLinkJSON)

// put it back to bytes
node[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['array-link']['/'])
done()
})
})
})

it('empty-array', (done) => {
dagCBOR.util.deserialize(emptyArrayCBOR, (err, node) => {
expect(err).to.not.exist
expect(node).to.eql(emptyArrayJSON)

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-array']['/'])
done()
})
})
})

it('empty-obj', (done) => {
dagCBOR.util.deserialize(emptyObjCBOR, (err, node) => {
expect(err).to.not.exist
expect(node).to.eql(emptyObjJSON)

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['empty-obj']['/'])
done()
})
})
})

it.skip('foo', (done) => {
dagCBOR.util.deserialize(fooCBOR, (err, node) => {
expect(err).to.not.exist
expect(node).to.eql(fooJSON)

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['foo']['/'])
done()
})
})
})

it('obj-no-link', (done) => {
dagCBOR.util.deserialize(objNoLinkCBOR, (err, node) => {
expect(err).to.not.exist
expect(node).to.eql(objNoLinkJSON)

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-no-link']['/'])
done()
})
})
})

it('obj-with-link', (done) => {
if (!isNode) { done() }

dagCBOR.util.deserialize(objWithLinkCBOR, (err, node) => {
expect(err).to.not.exist

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['obj-with-link']['/'])
done()
})
})
})
})

describe('serialise and compare', () => {
it('array-link', (done) => {
arrayLinkJSON[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])

dagCBOR.util.serialize(arrayLinkJSON, (err, serialized) => {
expect(err).to.not.exist

expect(serialized).to.eql(arrayLinkCBOR)
done()
})
})

it('empty-array', (done) => {
dagCBOR.util.serialize(emptyArrayJSON, (err, serialized) => {
expect(err).to.not.exist
expect(serialized).to.eql(emptyArrayCBOR)
done()
})
})

it('empty-obj', (done) => {
dagCBOR.util.serialize(emptyObjJSON, (err, serialized) => {
expect(err).to.not.exist
expect(serialized).to.eql(emptyObjCBOR)
done()
})
})

it.skip('foo', (done) => {})

it('obj-no-link', (done) => {
dagCBOR.util.serialize(objNoLinkJSON, (err, serialized) => {
expect(err).to.not.exist
expect(serialized).to.eql(objNoLinkCBOR)
done()
})
})

it.skip('obj-with-link', (done) => {})
})
})

0 comments on commit 6f7922a

Please sign in to comment.