X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Fmystery-box.js;h=a48bad847e18a795ac3b1b9692e053d44f4efb42;hb=f537ed3feb96dd199589bbaf03fc3f5eb670a2c7;hp=8d228e6e25d4a5044b99bb889036f7be38a93d48;hpb=044615f53bacdc366b44941218d808c549607469;p=squeep-mystery-box diff --git a/test/lib/mystery-box.js b/test/lib/mystery-box.js index 8d228e6..a48bad8 100644 --- a/test/lib/mystery-box.js +++ b/test/lib/mystery-box.js @@ -30,6 +30,22 @@ describe('MysteryBox', function () { } }); + it('accepts multiple secrets', async function () { + this.slow(500); + options = { + encryptionSecret: ['first poor secret', 'second poor secret'], + }; + mb = new MysteryBox(stubLogger, options); + object = { + foo: 'bar', + baz: 'quux', + flarp: 13, + }; + const encryptedResult = await mb.pack(object); + const decryptedResult = await mb.unpack(encryptedResult); + assert.deepStrictEqual(decryptedResult, object); + }); + it('covers options', function () { try { mb = new MysteryBox(stubLogger); @@ -60,6 +76,16 @@ describe('MysteryBox', function () { }); }); // constructor + describe('_keyFromSecret', function () { + it('covers invalid', async function () { + try { + await MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32); + } catch (e) { + assert(e instanceof RangeError); + } + }); + }); // _keyFromSecret + describe('pack, unpack', function () { beforeEach(function () { mb = new MysteryBox(stubLogger, options); @@ -106,6 +132,38 @@ describe('MysteryBox', function () { assert.deepStrictEqual(decryptedResult, object); }); + it('decrypts secondary (older) secret', async function () { + this.slow(500); + const oldmb = new MysteryBox(stubLogger, { encryptionSecret: 'old secret' }); + const newmb = new MysteryBox(stubLogger, { encryptionSecret: ['new secret', 'old secret'] }); + object = { + foo: 'bar', + baz: 'quux', + flarp: 13, + }; + const oldEncrypted = await oldmb.pack(object); + const newDecrypted = await newmb.unpack(oldEncrypted); + assert.deepStrictEqual(newDecrypted, object); + }); + + it('fails to decrypt invalid secret', async function () { + this.slow(500); + const oldmb = new MysteryBox(stubLogger, { encryptionSecret: 'very old secret' }); + const newmb = new MysteryBox(stubLogger, { encryptionSecret: ['new secret', 'old secret'] }); + object = { + foo: 'bar', + baz: 'quux', + flarp: 13, + }; + const oldEncrypted = await oldmb.pack(object); + try { + await newmb.unpack(oldEncrypted); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof Error); + } + }); + it('encrypts and decrypts all available versions +brotli', async function () { Object.keys(mb.versionParameters).map((v) => Number(v)).forEach(async (version) => { object = { @@ -161,6 +219,27 @@ describe('MysteryBox', function () { const decryptedResult = await mb.unpack(encryptedResult); assert.deepStrictEqual(decryptedResult, object); }); + + it('handles undefined', async function () { + try { + await mb.unpack(); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof RangeError, noExpectedException); + } + }); + + it('handles incomplete', async function () { + this.slow(500); + const encryptedResult = await mb.pack({ foo: 'bar' }); + try { + await mb.unpack(encryptedResult.slice(0, 6)); + assert.fail(noExpectedException); + } catch (e) { + assert(e instanceof RangeError, noExpectedException); + } + }); + }); // pack, unpack }); // MysteryBox \ No newline at end of file