-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.spec.js
90 lines (79 loc) · 2.06 KB
/
util.spec.js
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
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const garbage = require('garbage')
const map = require('async/map')
const dagCBOR = require('../src')
describe('util', () => {
const obj = {
someKey: 'someValue',
link: { '/': new Buffer('aaaaa') },
links: [
{ '/': new Buffer('1a') },
{ '/': new Buffer('2b') }
],
nested: {
hello: 'world',
link: { '/': new Buffer('mylink') }
}
}
it('.serialize and .deserialize', (done) => {
dagCBOR.util.serialize(obj, (err, serialized) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(serialized)).to.equal(true)
// Check for the tag 42
// d8 = tag, 2a = 42
expect(
serialized.toString('hex').match(/d82a/g)
).to.have.length(4)
dagCBOR.util.deserialize(serialized, (err, deserialized) => {
expect(err).to.not.exist()
expect(obj).to.eql(deserialized)
done()
})
})
})
it('error catching', (done) => {
const circlarObj = {}
circlarObj.a = circlarObj
dagCBOR.util.serialize(circlarObj, (err, serialized) => {
expect(err).to.exist()
expect(serialized).to.not.exist()
done()
})
})
it('.cid', (done) => {
dagCBOR.util.cid(obj, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
done()
})
})
it.skip('serialize and deserialize - garbage', (done) => {
const inputs = []
for (let i = 0; i < 1000; i++) {
inputs.push({ in: garbage(100) })
}
map(inputs, (input, cb) => {
dagCBOR.util.serialize(input, cb)
}, (err, encoded) => {
if (err) {
return done(err)
}
map(encoded, (enc, cb) => {
dagCBOR.util.deserialize(enc, cb)
}, (err, out) => {
if (err) {
return done(err)
}
expect(inputs).to.eql(out)
done()
})
})
})
})