8d228e6e25d4a5044b99bb889036f7be38a93d48
2 /* eslint-disable capitalized-comments */
5 const assert
= require('assert');
6 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
7 const MysteryBox
= require('../../lib/mystery-box');
8 const stubLogger
= require('../stub-logger');
10 describe('MysteryBox', function () {
11 const noExpectedException
= 'did not get expected exception';
12 let mb
, options
, object
;
13 beforeEach(function () {
15 encryptionSecret: 'this is not a very good secret',
18 afterEach(function () {
22 describe('constructor', function () {
23 it('needs a secret', async
function () {
26 mb
= new MysteryBox(stubLogger
, options
);
27 assert
.fail(noExpectedException
);
29 assert
.strictEqual(e
.message
, 'missing encryption secret', noExpectedException
);
33 it('covers options', function () {
35 mb
= new MysteryBox(stubLogger
);
36 assert
.fail(noExpectedException
);
38 assert
.strictEqual(e
.message
, 'missing encryption secret', noExpectedException
);
42 it('covers bad flags', function () {
43 options
.defaultFlags
= 300;
45 mb
= new MysteryBox(stubLogger
, options
);
46 assert
.fail(noExpectedException
);
48 assert(e
instanceof RangeError
, noExpectedException
);
52 it('covers missing ciphers', function () {
53 sinon
.stub(MysteryBox
._test
.crypto
, 'getCiphers').returns(['rot13']);
55 mb
= new MysteryBox(stubLogger
, options
);
56 assert
.fail(noExpectedException
);
58 assert
.strictEqual(e
.message
, 'no supported versions available', noExpectedException
);
63 describe('pack, unpack', function () {
64 beforeEach(function () {
65 mb
= new MysteryBox(stubLogger
, options
);
68 it('covers packing unsupported version', async
function () {
71 assert
.fail(noExpectedException
);
73 assert(e
instanceof RangeError
, noExpectedException
);
77 it('covers unpacking unsupported version', async
function () {
78 const badBuffer
= Buffer
.alloc(128);
79 badBuffer
.writeUInt8(0, 0); // No such thing as version 0
80 const badPayload
= badBuffer
.toString('base64');
82 await mb
.unpack(badPayload
);
83 assert
.fail(noExpectedException
);
85 assert(e
instanceof RangeError
, noExpectedException
);
89 it('encrypts and decrypts default version', async
function () {
96 const encryptedResult
= await mb
.pack(object
);
97 const decryptedResult
= await mb
.unpack(encryptedResult
);
98 assert
.deepStrictEqual(decryptedResult
, object
);
101 it('encrypts and decrypts default version, buffer contents', async
function () {
103 object
= Buffer
.from('a fine little buffer');
104 const encryptedResult
= await mb
.pack(object
);
105 const decryptedResult
= await mb
.unpack(encryptedResult
);
106 assert
.deepStrictEqual(decryptedResult
, object
);
109 it('encrypts and decrypts all available versions +brotli', async
function () {
110 Object
.keys(mb
.versionParameters
).map((v
) => Number(v
)).forEach(async (version
) => {
116 const encryptedResult
= await mb
.pack(object
, version
, 0x00);
117 const decryptedResult
= await mb
.unpack(encryptedResult
);
118 assert
.deepStrictEqual(decryptedResult
, object
, `${version} results not symmetric`);
122 it('encrypts and decrypts all available versions +flate', async
function () {
123 Object
.keys(mb
.versionParameters
).map((v
) => Number(v
)).forEach(async (version
) => {
129 const encryptedResult
= await mb
.pack(object
, version
, 0x01);
130 const decryptedResult
= await mb
.unpack(encryptedResult
);
131 assert
.deepStrictEqual(decryptedResult
, object
, `${version} results not symmetric`);
135 it('handles large object +brotli', async
function () {
138 const firstChar
= 32, lastChar
= 126;
140 return Math
.floor(firstChar
+ (lastChar
- firstChar
+ 1) * Math
.random());
143 longProperty: 'x'.repeat(384 * 1024).split('').map(() => String
.fromCharCode(rnd())).join(''),
145 const encryptedResult
= await mb
.pack(object
, mb
.bestVersion
, 0x00);
146 const decryptedResult
= await mb
.unpack(encryptedResult
);
147 assert
.deepStrictEqual(decryptedResult
, object
);
150 it('handles large object +flate', async
function () {
153 const firstChar
= 32, lastChar
= 126;
155 return Math
.floor(firstChar
+ (lastChar
- firstChar
+ 1) * Math
.random());
158 longProperty: 'x'.repeat(384 * 1024).split('').map(() => String
.fromCharCode(rnd())).join(''),
160 const encryptedResult
= await mb
.pack(object
, mb
.bestVersion
, 0x01);
161 const decryptedResult
= await mb
.unpack(encryptedResult
);
162 assert
.deepStrictEqual(decryptedResult
, object
);