fix: catch error when unmarshaling instead of crashing (#72)

This commit is contained in:
Maciej Krüger
2017-12-01 09:49:50 +01:00
committed by David Dias
parent f91f2b6506
commit 156911e162
4 changed files with 64 additions and 19 deletions

View File

@ -12,6 +12,8 @@ const parallel = require('async/parallel')
const PeerId = require('../src')
const util = require('util')
const testId = require('./fixtures/sample-id')
const testIdHex = testId.id
const testIdBytes = mh.fromHexString(testId.id)
@ -245,6 +247,24 @@ describe('PeerId', () => {
})
})
describe('returns error via cb instead of crashing', () => {
const garbage = [Buffer.from('00010203040506070809', 'hex'), {}, null, false, undefined, true, 1, 0, Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', '']
const fncs = ['createFromPubKey', 'createFromPrivKey', 'createFromJSON']
garbage.forEach(garbage => {
fncs.forEach(fnc => {
it(fnc + '(' + util.inspect(garbage) + ')', cb => {
PeerId[fnc](garbage, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist()
cb()
})
})
})
})
})
describe('throws on inconsistent data', () => {
let k1
let k2