use native base64url encoding, remove external dependency
[squeep-mystery-box] / test / lib / mystery-box.js
index a6e69b91f25ae0553dee526649e9ae51e103ef14..c71f6ff0fe8081cb22ef4f95399ffe099224b4e1 100644 (file)
@@ -8,7 +8,6 @@ const MysteryBox = require('../../lib/mystery-box');
 const stubLogger = require('../stub-logger');
 
 describe('MysteryBox', function () {
-  const noExpectedException = 'did not get expected exception';
   let mb, options, object;
   beforeEach(function () {
     options = {
@@ -22,68 +21,60 @@ describe('MysteryBox', function () {
   describe('constructor', function () {
     it('needs a secret', async function () {
       options = {};
-      try {
-        mb = new MysteryBox(stubLogger, options);
-        assert.fail(noExpectedException);
-      } catch (e) {
-        assert.strictEqual(e.message, 'missing encryption secret', noExpectedException);
-      }
+      assert.rejects(() => new MysteryBox(stubLogger, options));
+    });
+
+    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);
-        assert.fail(noExpectedException);
-      } catch (e) {
-        assert.strictEqual(e.message, 'missing encryption secret', noExpectedException);
-      }
+      assert.rejects(() => new MysteryBox(stubLogger));
     });
 
     it('covers bad flags', function () {
       options.defaultFlags = 300;
-      try {
-        mb = new MysteryBox(stubLogger, options);
-        assert.fail(noExpectedException);
-      } catch (e) {
-        assert(e instanceof RangeError, noExpectedException);
-      }
+      assert.rejects(() => new MysteryBox(stubLogger, options), RangeError);
     });
 
     it('covers missing ciphers', function () {
       sinon.stub(MysteryBox._test.crypto, 'getCiphers').returns(['rot13']);
-      try {
-        mb = new MysteryBox(stubLogger, options);
-        assert.fail(noExpectedException);
-      } catch (e) {
-        assert.strictEqual(e.message, 'no supported versions available', noExpectedException);
-      }
+      assert.rejects(() => new MysteryBox(stubLogger, options));
     });
   }); // constructor
 
+  describe('_keyFromSecret', function () {
+    it('covers invalid', async function () {
+      assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), RangeError);
+    });
+  }); // _keyFromSecret
+
   describe('pack, unpack', function () {
     beforeEach(function () {
       mb = new MysteryBox(stubLogger, options);
     });
   
     it('covers packing unsupported version', async function () {
-      try {
-        await mb.pack({}, 0);
-        assert.fail(noExpectedException);
-      } catch (e) {
-        assert(e instanceof RangeError, noExpectedException);
-      }
+      assert.rejects(() => mb.pack({}, 0), RangeError);
     });
 
     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('base64');
-      try {
-        await mb.unpack(badPayload);
-        assert.fail(noExpectedException);
-      } catch (e) {
-        assert(e instanceof RangeError, noExpectedException);
-      }
+      const badPayload = badBuffer.toString('base64url');
+      assert.rejects(() => mb.unpack(badPayload), RangeError);
     });
 
     it('encrypts and decrypts default version', async function () {
@@ -106,6 +97,33 @@ 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);
+      assert.rejects(() => newmb.unpack(oldEncrypted));
+    });
+
     it('encrypts and decrypts all available versions +brotli', async function () {
       Object.keys(mb.versionParameters).map((v) => Number(v)).forEach(async (version) => {
         object = {
@@ -163,22 +181,13 @@ describe('MysteryBox', function () {
     });
 
     it('handles undefined', async function () {
-      try {
-        await mb.unpack();
-        assert.fail(noExpectedException);
-      } catch (e) {
-        assert(e instanceof RangeError, noExpectedException);
-      }
+      assert.rejects(() => mb.unpack(), RangeError);
     });
 
     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);
-      }
+      assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), RangeError);
     });
 
   }); // pack, unpack