fox-send/test/frontend/tests/streaming-tests.js

110 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2018-06-04 17:47:55 +00:00
const ece = require('http_ece');
import assert from 'assert';
2018-07-26 05:26:11 +00:00
import Archive from '../../../app/archive';
2018-06-04 17:47:55 +00:00
import { b64ToArray } from '../../../app/utils';
2018-07-26 05:26:11 +00:00
import { blobStream, concatStream } from '../../../app/streams';
import { decryptStream, encryptStream } from '../../../app/ece.js';
2020-07-28 02:23:03 +00:00
import { encryptedSize, concat } from '../../../app/utils';
2018-06-04 17:47:55 +00:00
const rs = 36;
const str = 'You are the dancing queen, young and sweet, only seventeen.';
const testSalt = 'I1BsxtFttlv3u_Oo94xnmw';
const keystr = 'yqdlZ-tYemfogSmv7Ws5PQ';
const buffer = Buffer.from(str);
const params = {
version: 'aes128gcm',
rs: rs,
salt: testSalt,
keyid: '',
key: keystr
};
const encrypted = ece.encrypt(buffer, params);
const decrypted = ece.decrypt(encrypted, params);
describe('Streaming', function() {
2018-07-26 05:26:11 +00:00
describe('blobStream', function() {
it('reads the entire blob', async function() {
const len = 12345;
const chunkSize = 1024;
const blob = new Blob([new Uint8Array(len)]);
const stream = blobStream(blob, chunkSize);
const reader = stream.getReader();
let bytes = 0;
let data = await reader.read();
while (!data.done) {
bytes += data.value.byteLength;
assert.ok(data.value.byteLength <= chunkSize, 'chunk too big');
data = await reader.read();
}
assert.equal(bytes, len);
});
});
describe('concatStream', function() {
it('reads all the streams', async function() {
const count = 5;
const len = 12345;
const streams = Array.from({ length: count }, () =>
blobStream(new Blob([new Uint8Array(len)]))
);
const concat = concatStream(streams);
const reader = concat.getReader();
let bytes = 0;
let data = await reader.read();
while (!data.done) {
bytes += data.value.byteLength;
data = await reader.read();
}
assert.equal(bytes, len * count);
});
});
2018-06-04 17:47:55 +00:00
//testing against http_ece's implementation
describe('ECE', function() {
const key = b64ToArray(keystr);
const salt = b64ToArray(testSalt).buffer;
it('can encrypt', async function() {
2018-07-26 05:26:11 +00:00
const stream = new Archive([new Blob([str], { type: 'text/plain' })])
.stream;
const encStream = encryptStream(stream, key, rs, salt);
2018-06-21 00:05:33 +00:00
const reader = encStream.getReader();
2018-06-04 17:47:55 +00:00
2020-07-28 02:23:03 +00:00
let result = new Uint8Array(0);
2018-06-04 17:47:55 +00:00
let state = await reader.read();
while (!state.done) {
2020-07-28 02:23:03 +00:00
result = concat(result, state.value);
2018-06-04 17:47:55 +00:00
state = await reader.read();
}
2020-07-28 02:23:03 +00:00
assert.deepEqual(result, new Uint8Array(encrypted));
2018-06-04 17:47:55 +00:00
});
it('can decrypt', async function() {
2018-07-26 05:26:11 +00:00
const stream = new Archive([new Blob([encrypted])]).stream;
const decStream = decryptStream(stream, key, rs);
2018-06-04 17:47:55 +00:00
2018-06-21 00:05:33 +00:00
const reader = decStream.getReader();
2020-07-28 02:23:03 +00:00
let result = new Uint8Array(0);
2018-06-04 17:47:55 +00:00
let state = await reader.read();
while (!state.done) {
2020-07-28 02:23:03 +00:00
result = concat(result, state.value);
2018-06-04 17:47:55 +00:00
state = await reader.read();
}
2020-07-28 02:23:03 +00:00
assert.deepEqual(result, new Uint8Array(decrypted));
2018-06-04 17:47:55 +00:00
});
});
describe('encryptedSize', function() {
it('matches the size of an encrypted buffer', function() {
assert.equal(encryptedSize(buffer.length, rs), encrypted.length);
});
});
2018-06-04 17:47:55 +00:00
});