802d8b3da8e1bb03e46475ed904c306535ec1336
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('accepts multiple secrets', async
function () {
36 encryptionSecret: ['first poor secret', 'second poor secret'],
38 mb
= new MysteryBox(stubLogger
, options
);
44 const encryptedResult
= await mb
.pack(object
);
45 const decryptedResult
= await mb
.unpack(encryptedResult
);
46 assert
.deepStrictEqual(decryptedResult
, object
);
50 it('covers options', function () {
52 mb
= new MysteryBox(stubLogger
);
53 assert
.fail(noExpectedException
);
55 assert
.strictEqual(e
.message
, 'missing encryption secret', noExpectedException
);
59 it('covers bad flags', function () {
60 options
.defaultFlags
= 300;
62 mb
= new MysteryBox(stubLogger
, options
);
63 assert
.fail(noExpectedException
);
65 assert(e
instanceof RangeError
, noExpectedException
);
69 it('covers missing ciphers', function () {
70 sinon
.stub(MysteryBox
._test
.crypto
, 'getCiphers').returns(['rot13']);
72 mb
= new MysteryBox(stubLogger
, options
);
73 assert
.fail(noExpectedException
);
75 assert
.strictEqual(e
.message
, 'no supported versions available', noExpectedException
);
80 describe('pack, unpack', function () {
81 beforeEach(function () {
82 mb
= new MysteryBox(stubLogger
, options
);
85 it('covers packing unsupported version', async
function () {
88 assert
.fail(noExpectedException
);
90 assert(e
instanceof RangeError
, noExpectedException
);
94 it('covers unpacking unsupported version', async
function () {
95 const badBuffer
= Buffer
.alloc(128);
96 badBuffer
.writeUInt8(0, 0); // No such thing as version 0
97 const badPayload
= badBuffer
.toString('base64');
99 await mb
.unpack(badPayload
);
100 assert
.fail(noExpectedException
);
102 assert(e
instanceof RangeError
, noExpectedException
);
106 it('encrypts and decrypts default version', async
function () {
113 const encryptedResult
= await mb
.pack(object
);
114 const decryptedResult
= await mb
.unpack(encryptedResult
);
115 assert
.deepStrictEqual(decryptedResult
, object
);
118 it('encrypts and decrypts default version, buffer contents', async
function () {
120 object
= Buffer
.from('a fine little buffer');
121 const encryptedResult
= await mb
.pack(object
);
122 const decryptedResult
= await mb
.unpack(encryptedResult
);
123 assert
.deepStrictEqual(decryptedResult
, object
);
126 it('decrypts secondary (older) secret', async
function () {
128 const oldmb
= new MysteryBox(stubLogger
, { encryptionSecret: 'old secret' });
129 const newmb
= new MysteryBox(stubLogger
, { encryptionSecret: ['new secret', 'old secret'] });
135 const oldEncrypted
= await oldmb
.pack(object
);
136 const newDecrypted
= await newmb
.unpack(oldEncrypted
);
137 assert
.deepStrictEqual(newDecrypted
, object
);
140 it('fails to decrypt invalid secret', async
function () {
142 const oldmb
= new MysteryBox(stubLogger
, { encryptionSecret: 'very old secret' });
143 const newmb
= new MysteryBox(stubLogger
, { encryptionSecret: ['new secret', 'old secret'] });
149 const oldEncrypted
= await oldmb
.pack(object
);
151 await newmb
.unpack(oldEncrypted
);
152 assert
.fail(noExpectedException
);
154 assert(e
instanceof Error
);
158 it('encrypts and decrypts all available versions +brotli', async
function () {
159 Object
.keys(mb
.versionParameters
).map((v
) => Number(v
)).forEach(async (version
) => {
165 const encryptedResult
= await mb
.pack(object
, version
, 0x00);
166 const decryptedResult
= await mb
.unpack(encryptedResult
);
167 assert
.deepStrictEqual(decryptedResult
, object
, `${version} results not symmetric`);
171 it('encrypts and decrypts all available versions +flate', async
function () {
172 Object
.keys(mb
.versionParameters
).map((v
) => Number(v
)).forEach(async (version
) => {
178 const encryptedResult
= await mb
.pack(object
, version
, 0x01);
179 const decryptedResult
= await mb
.unpack(encryptedResult
);
180 assert
.deepStrictEqual(decryptedResult
, object
, `${version} results not symmetric`);
184 it('handles large object +brotli', async
function () {
187 const firstChar
= 32, lastChar
= 126;
189 return Math
.floor(firstChar
+ (lastChar
- firstChar
+ 1) * Math
.random());
192 longProperty: 'x'.repeat(384 * 1024).split('').map(() => String
.fromCharCode(rnd())).join(''),
194 const encryptedResult
= await mb
.pack(object
, mb
.bestVersion
, 0x00);
195 const decryptedResult
= await mb
.unpack(encryptedResult
);
196 assert
.deepStrictEqual(decryptedResult
, object
);
199 it('handles large object +flate', async
function () {
202 const firstChar
= 32, lastChar
= 126;
204 return Math
.floor(firstChar
+ (lastChar
- firstChar
+ 1) * Math
.random());
207 longProperty: 'x'.repeat(384 * 1024).split('').map(() => String
.fromCharCode(rnd())).join(''),
209 const encryptedResult
= await mb
.pack(object
, mb
.bestVersion
, 0x01);
210 const decryptedResult
= await mb
.unpack(encryptedResult
);
211 assert
.deepStrictEqual(decryptedResult
, object
);
214 it('handles undefined', async
function () {
217 assert
.fail(noExpectedException
);
219 assert(e
instanceof RangeError
, noExpectedException
);
223 it('handles incomplete', async
function () {
225 const encryptedResult
= await mb
.pack({ foo: 'bar' });
227 await mb
.unpack(encryptedResult
.slice(0, 6));
228 assert
.fail(noExpectedException
);
230 assert(e
instanceof RangeError
, noExpectedException
);