chore: add error codes (#155)

* chore: add error codes

* chore: create errors with new Error()

* fix: better error testin

* refactor: simplify random bytes error checks
This commit is contained in:
dirkmc
2019-07-22 06:16:02 -04:00
committed by Jacob Heun
parent 2d15e717e4
commit 0b686d363c
23 changed files with 172 additions and 72 deletions

View File

@ -8,6 +8,7 @@ const expect = chai.expect
chai.use(dirtyChai)
const crypto = require('../src')
const fixtures = require('./fixtures/go-key-rsa')
const { expectErrCode } = require('./util')
describe('libp2p-crypto', function () {
this.timeout(20 * 1000)
@ -38,6 +39,15 @@ describe('libp2p-crypto', function () {
expect(key2.public.equals(key.public)).to.be.eql(true)
})
it('generateKeyPair', () => {
return expectErrCode(crypto.keys.generateKeyPair('invalid-key-type', 512), 'ERR_UNSUPPORTED_KEY_TYPE')
})
it('generateKeyPairFromSeed', () => {
var seed = crypto.randomBytes(32)
return expectErrCode(crypto.keys.generateKeyPairFromSeed('invalid-key-type', seed, 512), 'ERR_UNSUPPORTED_KEY_TYPE')
})
// marshalled keys seem to be slightly different
// unsure as to if this is just a difference in encoding
// or a bug
@ -93,7 +103,8 @@ describe('libp2p-crypto', function () {
})
it('throws on invalid hash name', () => {
expect(() => crypto.pbkdf2('password', 'at least 16 character salt', 500, 512 / 8, 'shaX-xxx')).to.throw()
const fn = () => crypto.pbkdf2('password', 'at least 16 character salt', 500, 512 / 8, 'shaX-xxx')
expect(fn).to.throw().with.property('code', 'ERR_UNSUPPORTED_HASH_TYPE')
})
})