support providing multiple secrets, always encrypt with first, attempt decryption...
[squeep-mystery-box] / test / lib / mystery-box.js
index a6e69b91f25ae0553dee526649e9ae51e103ef14..802d8b3da8e1bb03e46475ed904c306535ec1336 100644 (file)
@@ -30,6 +30,23 @@ 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);
@@ -106,6 +123,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 = {
@@ -172,6 +221,7 @@ describe('MysteryBox', function () {
     });
 
     it('handles incomplete', async function () {
+      this.slow(500);
       const encryptedResult = await mb.pack({ foo: 'bar' });
       try {
         await mb.unpack(encryptedResult.slice(0, 6));