X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Fmystery-box.js;h=510362bd06c8ab3a7884909e41404e0350d40494;hb=17613b91c12670f5cdcdfd163166f7a902a0b49d;hp=dd54a88dae9bfbd67cf0cec3f168e4c78e6bbb81;hpb=2ef5f9c74d92c8eb8cc0bd03fecc9d1a1d306489;p=squeep-mystery-box diff --git a/test/lib/mystery-box.js b/test/lib/mystery-box.js index dd54a88..510362b 100644 --- a/test/lib/mystery-box.js +++ b/test/lib/mystery-box.js @@ -5,6 +5,7 @@ const assert = require('assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require const MysteryBox = require('../../lib/mystery-box'); +const { MysteryBoxError } = require('../../lib/errors'); const crypto = require('crypto'); function _verbose(mb) { @@ -53,7 +54,7 @@ describe('MysteryBox', function () { it('covers bad flags', function () { options.defaultFlags = 300; - assert.rejects(() => new MysteryBox(options), RangeError); + assert.rejects(() => new MysteryBox(options), MysteryBoxError); }); it('covers missing ciphers', function () { @@ -62,9 +63,26 @@ describe('MysteryBox', function () { }); }); // constructor + describe('_ensureArray', function () { + it('returns empty array for no data', function () { + const result = MysteryBox._ensureArray(); + assert.deepStrictEqual(result, []); + }); + it('returns same array passed in', function () { + const expected = [1, 2, 3, 'foo']; + const result = MysteryBox._ensureArray(expected); + assert.deepStrictEqual(result, expected); + }); + it('returns array containing non-array data', function () { + const data = 'bar'; + const result = MysteryBox._ensureArray(data); + assert.deepStrictEqual(result, [data]); + }); + }); // _ensureArray + describe('_keyFromSecret', function () { it('covers invalid', async function () { - assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), RangeError); + assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), MysteryBoxError); }); }); // _keyFromSecret @@ -123,7 +141,7 @@ describe('MysteryBox', function () { _check(0xfe, 8, 0x00); }); it('covers unsupported', function () { - assert.throws(() => MysteryBox._versionHeaderDecode(0xff), RangeError); + assert.throws(() => MysteryBox._versionHeaderDecode(0xff), MysteryBoxError); }); }); // _versionHeaderDecode @@ -172,7 +190,7 @@ describe('MysteryBox', function () { }); it('covers too big', function () { const buffer = Buffer.from([0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); - assert.throws(() => MysteryBox._versionDecode(buffer), RangeError); + assert.throws(() => MysteryBox._versionDecode(buffer), MysteryBoxError); }); }); // _versionDecode @@ -226,7 +244,7 @@ describe('MysteryBox', function () { }); it('covers too large', function () { const version = 277076930199552; - assert.rejects(() => MysteryBox._versionEncode(version), RangeError); + assert.rejects(() => MysteryBox._versionEncode(version), MysteryBoxError); }); it('recipricates', function () { [ @@ -247,14 +265,14 @@ describe('MysteryBox', function () { }); it('covers packing unsupported version', async function () { - assert.rejects(() => mb.pack({}, 0), RangeError); + assert.rejects(() => mb.pack({}, 0), MysteryBoxError); }); it('covers unpacking unsupported version', async function () { const badBuffer = Buffer.alloc(128); badBuffer.writeUInt8(0, 0); // No such thing as version 0 const badPayload = badBuffer.toString('base64url'); - assert.rejects(() => mb.unpack(badPayload), RangeError); + assert.rejects(() => mb.unpack(badPayload), MysteryBoxError); }); it('encrypts and decrypts default version', async function () { @@ -386,13 +404,13 @@ describe('MysteryBox', function () { }); it('handles undefined', async function () { - assert.rejects(() => mb.unpack(), RangeError); + assert.rejects(() => mb.unpack(), MysteryBoxError); }); it('handles incomplete', async function () { this.slow(500); const encryptedResult = await mb.pack({ foo: 'bar' }); - assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), RangeError); + assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), MysteryBoxError); }); it('covers internal error, incorrect version byte size, pack', async function () {