support multi-byte version identifiers for boxes. minor internal cleanup/refactors.
[squeep-mystery-box] / test / lib / mystery-box.js
index c71f6ff0fe8081cb22ef4f95399ffe099224b4e1..9f48ff64a6bfeb9390be1242670afcc2445a6d74 100644 (file)
@@ -13,6 +13,7 @@ describe('MysteryBox', function () {
     options = {
       encryptionSecret: 'this is not a very good secret',
     };
+    mb = new MysteryBox(stubLogger, options);
   });
   afterEach(function () {
     sinon.restore();
@@ -61,6 +62,178 @@ describe('MysteryBox', function () {
     });
   }); // _keyFromSecret
 
+  describe('_versionHeaderDecode', function () {
+    function _check(firstByte, numBytes, value) {
+      const result = MysteryBox._versionHeaderDecode(firstByte);
+      assert.strictEqual(result.numBytes, numBytes);
+      assert.strictEqual(result.firstByte, value);
+    }
+    it('decodes single byte, min', function () {
+      _check(0x00, 1, 0x00);
+    });
+    it('decodes single byte, max', function () {
+      _check(0x7f, 1, 0x7f);
+    });
+    it('decodes double byte, min', function () {
+      _check(0x80, 2, 0x00);
+    });
+    it('decodes double byte, max', function () {
+      _check(0xbf, 2, 0x3f);
+    });
+    it('decodes triple byte, min', function () {
+      _check(0xc0, 3, 0x00);
+    });
+    it('decodes triple byte, max', function () {
+      _check(0xdf, 3, 0x1f);
+    });
+    it('decodes quadruple byte, min', function () {
+      _check(0xe0, 4, 0x00);
+    });
+    it('decodes quadruple byte, max', function () {
+      _check(0xef, 4, 0x0f);
+    });
+    it('decodes quintuple byte, min', function () {
+      _check(0xf0, 5, 0x00);
+    });
+    it('decodes quintuple byte, max', function () {
+      _check(0xf7, 5, 0x07);
+    });
+    it('decodes sextuple byte, min', function () {
+      _check(0xf8, 6, 0x00);
+    });
+    it('decodes sextuple byte, max', function () {
+      _check(0xfb, 6, 0x03);
+    });
+    it('decodes septuple byte, min', function () {
+      _check(0xfc, 7, 0x00);
+    });
+    it('decodes septuple byte, max', function () {
+      _check(0xfd, 7, 0x01);
+    });
+    it('decodes double byte, min', function () {
+      _check(0xfe, 8, 0x00);
+    });
+    it('decodes double byte, max', function () {
+      _check(0xfe, 8, 0x00);
+    });
+    it('covers unsupported', function () {
+      assert.throws(() => MysteryBox._versionHeaderDecode(0xff), RangeError);
+    });
+  }); // _versionHeaderDecode
+
+  describe('_versionDecode', function () {
+    function _checkDecodeRange(start, end, numBytes, headerByte) {
+      const headerMask = ((0xff << (8 - numBytes)) & 0xff) >>> 0;
+      const hByte = ((0xff << (8 - numBytes + 1)) & 0xff) >>> 0;
+      assert.strictEqual(hByte, headerByte, `TEST ERROR: unexpected header for length, computed: ${hByte.toString(16)} passed: ${headerByte.toString(16)}`);
+      for (let v = start; v <= end; v++) {
+        const buffer = Buffer.alloc(numBytes);
+        buffer.writeUIntBE(v, 0, numBytes);
+        assert((buffer[0] & headerMask) === 0, `TEST ERROR: version ${v} encroached on headerByte 0x${headerByte.toString(16)} (${headerByte.toString(2)} & ${buffer[0].toString(2)})`);
+        buffer[0] = (buffer[0] | headerByte) >>> 0;
+        const { version, versionBytes } = MysteryBox._versionDecode(buffer);
+        assert.strictEqual(versionBytes, numBytes);
+        assert.strictEqual(version, v);
+      }
+    }
+    it('covers single-byte versions', function () {
+      _checkDecodeRange(0, 127, 1, 0x00);
+    });
+    it('covers double-byte versions', function () {
+      _checkDecodeRange(128, 136, 2, 0x80);
+      // ...
+      _checkDecodeRange(16375, 16383, 2, 0x80);
+    });
+    it('covers triple-byte versions', function () {
+      _checkDecodeRange(16384, 16390, 3, 0xc0);
+      // ...
+      _checkDecodeRange(2097145, 2097151, 3, 0xc0);
+    });
+    it('covers quadruple-byte versions', function () {
+      _checkDecodeRange(2097151, 2097160, 4, 0xe0);
+      // ...
+      _checkDecodeRange(268435445, 268435455, 4, 0xe0);
+    });
+    it('covers quintuple-byte versions', function () {
+      _checkDecodeRange(268435445, 268435445, 5, 0xf0);
+      // ...
+      _checkDecodeRange(34359738360, 34359738367, 5, 0xf0);
+    });
+    it('covers sextuple-byte versions', function () {
+      _checkDecodeRange(34359738367, 34359738375, 6, 0xf8);
+      // ...
+      _checkDecodeRange(4398046511093, 4398046511103, 6, 0xf8);
+    });
+    it('covers too big', function () {
+      const buffer = Buffer.from([0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
+      assert.throws(() => MysteryBox._versionDecode(buffer), RangeError);
+    });
+  }); // _versionDecode
+
+  describe('_versionEncode', function () {
+    function _checkEncodeRange(start, end, bytes, headerBits) {
+      for (let version = start; version <= end; version++) {
+        const expected = Buffer.alloc(bytes);
+        expected.writeUIntBE(version, 0, bytes);
+        expected[0] = (expected[0] | headerBits) >>> 0;
+
+        const { buffer, versionBytes } = MysteryBox._versionEncode(version);
+        assert.deepStrictEqual(versionBytes, bytes, `version ${version} has ${versionBytes} bytes instead of expected ${bytes}`);
+        assert.deepStrictEqual(buffer, expected, `version ${version} buffer not expected: ${JSON.stringify(buffer)} vs ${JSON.stringify(expected)}`);
+      }
+    }
+    function _cheeckReciprical(version) {
+      const { buffer, versionBytes: encVB } = MysteryBox._versionEncode(version);
+      const { version: decV, versionBytes: decVB } = MysteryBox._versionDecode(buffer);
+      assert.strictEqual(encVB, decVB, `differing lengths for ${version}: enc:${encVB} dec:${decVB}`);
+      assert.strictEqual(decV, version, `failed for ${version}: ${JSON.stringify({ buffer, versionBytes: encVB })}`);
+    }
+    it('covers single-byte-packable versions', function () {
+      _checkEncodeRange(0, 127, 1, 0x00);
+    });
+    it('covers double-byte-packable versions', function () {
+      _checkEncodeRange(128, 200, 2, 0x80);
+      /* ... */
+      _checkEncodeRange(16380, 16383, 2, 0x80);
+    });
+    it('covers triple-byte-packable versions', function () {
+      _checkEncodeRange(16384, 16390, 3, 0xc0);
+      /* ... */
+      _checkEncodeRange(2097141, 2097151, 3, 0xc0);
+    });
+    it('covers quadruple-byte-packable versions', function () {
+      _checkEncodeRange(2097152, 2097161, 4, 0xe0);
+      /* ... */
+      _checkEncodeRange(268435445, 268435455, 4, 0xe0);
+    });
+    it('covers quintuple-byte-packable versions', function () {
+      _checkEncodeRange(268435456, 268435465, 5, 0xf0)
+      /* ... */
+      _checkEncodeRange(4294967294, 4294967296, 5, 0xf0)
+      /* ... */
+      _checkEncodeRange(34359738360, 34359738367, 5, 0xf0)
+    });
+    it('covers sextuple-byte-packable versions', function () {
+      _checkEncodeRange(34359738368, 34359738377, 6, 0xf8)
+      /* ... */
+      _checkEncodeRange(4398046511093, 4398046511103, 6, 0xf8)
+    });
+    it('covers too large', function () {
+      const version = 277076930199552;
+      assert.rejects(() => MysteryBox._versionEncode(version), RangeError);
+    });
+    it('recipricates', function () {
+      [
+        0, 127,
+        128, 16383,
+        16384, 2097151,
+        2097152, 268435455,
+        268435456, 34359738367,
+        34359738368, 4398046511103,
+      ].forEach((v) => _cheeckReciprical(v));
+    });
+  }); // _versionEncode
+
   describe('pack, unpack', function () {
     beforeEach(function () {
       mb = new MysteryBox(stubLogger, options);
@@ -97,6 +270,14 @@ describe('MysteryBox', function () {
       assert.deepStrictEqual(decryptedResult, object);
     });
 
+    it('encrypts and decrypts default version, coerced buffer contents', async function () {
+      this.slow(500);
+      object = 'a string a string';
+      const encryptedResult = await mb.pack(object, undefined, mb.Flags.BufferPayload | mb.defaultFlags);
+      const decryptedResult = await mb.unpack(encryptedResult);
+      assert.deepStrictEqual(decryptedResult, Buffer.from(object));
+    });
+
     it('decrypts secondary (older) secret', async function () {
       this.slow(500);
       const oldmb = new MysteryBox(stubLogger, { encryptionSecret: 'old secret' });
@@ -190,6 +371,19 @@ describe('MysteryBox', function () {
       assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), RangeError);
     });
 
+    it('covers internal error, incorrect version byte size, pack', async function () {
+      const version = 1;
+      sinon.stub(mb.versionParameters[version], 'versionBytes').value(10);
+      assert.rejects(() => mb.pack({}, version), Error);
+    });
+
+    it('covers internal error, incorrect version byte size, unpack', async function () {
+      const version = 1;
+      const encryptedResult = await mb.pack({}, version);
+      sinon.stub(mb.versionParameters[version], 'versionBytes').value(10);
+      assert.rejects(() => mb.unpack(encryptedResult), Error);
+    });
+
   }); // pack, unpack
 
 }); // MysteryBox
\ No newline at end of file