Implemented multi-file upload/download

This commit is contained in:
Danny Coates 2018-07-25 22:26:11 -07:00
parent b2aed06328
commit 7bf104960e
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
18 changed files with 475 additions and 183 deletions

View file

@ -1,9 +1,10 @@
import assert from 'assert';
import Archive from '../../../app/archive';
import * as api from '../../../app/api';
import Keychain from '../../../app/keychain';
const encoder = new TextEncoder();
const plaintext = new Blob([encoder.encode('hello world!')]);
const plaintext = new Archive([new Blob([encoder.encode('hello world!')])]);
const metadata = {
name: 'test.txt',
type: 'text/plain'
@ -13,11 +14,11 @@ describe('API', function() {
describe('websocket upload', function() {
it('returns file info on success', async function() {
const keychain = new Keychain();
const enc = await keychain.encryptStream(plaintext);
const enc = await keychain.encryptStream(plaintext.stream);
const meta = await keychain.encryptMetadata(metadata);
const verifierB64 = await keychain.authKeyB64();
const p = function() {};
const up = api.uploadWs(enc.stream, enc.streamInfo, meta, verifierB64, p);
const up = api.uploadWs(enc, meta, verifierB64, p);
const result = await up.result;
assert.ok(result.url);
@ -27,11 +28,11 @@ describe('API', function() {
it('can be cancelled', async function() {
const keychain = new Keychain();
const enc = await keychain.encryptStream(plaintext);
const enc = await keychain.encryptStream(plaintext.stream);
const meta = await keychain.encryptMetadata(metadata);
const verifierB64 = await keychain.authKeyB64();
const p = function() {};
const up = api.uploadWs(enc.stream, enc.streamInfo, meta, verifierB64, p);
const up = api.uploadWs(enc, meta, verifierB64, p);
up.cancel();
try {
await up.result;

View file

@ -1,17 +1,19 @@
import assert from 'assert';
import FileSender from '../../../app/fileSender';
import Archive from '../../../app/archive';
// FileSender uses a File in real life but a Blob works for testing
const blob = new Blob(['hello world!'], { type: 'text/plain' });
blob.name = 'text.txt';
const archive = new Archive([blob]);
describe('FileSender', function() {
describe('upload', function() {
it('returns an OwnedFile on success', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
assert.ok(file.id);
assert.equal(file.name, blob.name);
assert.equal(file.name, archive.name);
});
});
});

View file

@ -2,9 +2,10 @@ const ece = require('http_ece');
require('buffer');
import assert from 'assert';
import Archive from '../../../app/archive';
import { b64ToArray } from '../../../app/utils';
import BlobSlicer from '../../../app/blobSlicer';
import ECE from '../../../app/ece.js';
import { blobStream, concatStream } from '../../../app/streams';
import { decryptStream, encryptStream } from '../../../app/ece.js';
const rs = 36;
@ -25,15 +26,52 @@ const encrypted = ece.encrypt(buffer, params);
const decrypted = ece.decrypt(encrypted, params);
describe('Streaming', function() {
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);
});
});
//testing against http_ece's implementation
describe('ECE', function() {
const key = b64ToArray(keystr);
const salt = b64ToArray(testSalt).buffer;
const blob = new Blob([str], { type: 'text/plain' });
it('can encrypt', async function() {
const ece = new ECE(blob, key, 'encrypt', rs, salt);
const encStream = await ece.transform();
const stream = new Archive([new Blob([str], { type: 'text/plain' })])
.stream;
const encStream = encryptStream(stream, key, rs, salt);
const reader = encStream.getReader();
let result = Buffer.from([]);
@ -48,11 +86,8 @@ describe('Streaming', function() {
});
it('can decrypt', async function() {
const blobStream = new ReadableStream(
new BlobSlicer(new Blob([encrypted]), rs)
);
const ece = new ECE(blobStream, key, 'decrypt', rs);
const decStream = await ece.transform();
const stream = new Archive([new Blob([encrypted])]).stream;
const decStream = decryptStream(stream, key, rs);
const reader = decStream.getReader();
let result = Buffer.from([]);

View file

@ -1,4 +1,5 @@
import assert from 'assert';
import Archive from '../../../app/archive';
import FileSender from '../../../app/fileSender';
import FileReceiver from '../../../app/fileReceiver';
@ -11,12 +12,13 @@ const noSave = true || !headless; // only run the saveFile code if headless
// FileSender uses a File in real life but a Blob works for testing
const blob = new Blob([new ArrayBuffer(1024 * 128)], { type: 'text/plain' });
blob.name = 'test.txt';
const archive = new Archive([blob]);
navigator.serviceWorker.register('/serviceWorker.js');
describe('Upload / Download flow', function() {
this.timeout(0);
it('can only download once by default', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
const fr = new FileReceiver({
secretKey: file.toJSON().secretKey,
@ -36,7 +38,7 @@ describe('Upload / Download flow', function() {
});
it('downloads with the correct password', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
await file.setPassword('magic');
const fr = new FileReceiver({
@ -53,7 +55,7 @@ describe('Upload / Download flow', function() {
});
it('blocks invalid passwords from downloading', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
await file.setPassword('magic');
const fr = new FileReceiver({
@ -81,7 +83,7 @@ describe('Upload / Download flow', function() {
});
it('retries a bad nonce', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
const fr = new FileReceiver({
secretKey: file.toJSON().secretKey,
@ -90,11 +92,11 @@ describe('Upload / Download flow', function() {
requiresPassword: false
});
await fr.getMetadata();
assert.equal(fr.fileInfo.name, blob.name);
assert.equal(fr.fileInfo.name, archive.name);
});
it('can cancel the upload', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const up = fs.upload();
fs.cancel(); // before encrypting
try {
@ -122,7 +124,7 @@ describe('Upload / Download flow', function() {
});
it('can cancel the download', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
const fr = new FileReceiver({
secretKey: file.toJSON().secretKey,
@ -142,7 +144,7 @@ describe('Upload / Download flow', function() {
it('can increase download count on download', async function() {
this.timeout(0);
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
const fr = new FileReceiver({
secretKey: file.toJSON().secretKey,
@ -157,7 +159,7 @@ describe('Upload / Download flow', function() {
});
it('does not increase download count when download cancelled', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
const fr = new FileReceiver({
secretKey: file.toJSON().secretKey,
@ -178,7 +180,7 @@ describe('Upload / Download flow', function() {
});
it('can allow multiple downloads', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
const fr = new FileReceiver({
secretKey: file.toJSON().secretKey,
@ -204,7 +206,7 @@ describe('Upload / Download flow', function() {
});
it('can delete the file before download', async function() {
const fs = new FileSender(blob);
const fs = new FileSender(archive);
const file = await fs.upload();
const fr = new FileReceiver({
secretKey: file.toJSON().secretKey,