2018-02-21 05:31:27 +01:00
|
|
|
import assert from 'assert';
|
2018-07-26 07:26:11 +02:00
|
|
|
import Archive from '../../../app/archive';
|
2018-02-21 05:31:27 +01:00
|
|
|
import FileSender from '../../../app/fileSender';
|
|
|
|
import FileReceiver from '../../../app/fileReceiver';
|
2020-07-27 20:18:52 +02:00
|
|
|
import storage from '../../../app/storage';
|
2018-02-21 05:31:27 +01:00
|
|
|
|
|
|
|
const headless = /Headless/.test(navigator.userAgent);
|
2018-07-25 21:46:40 +02:00
|
|
|
// TODO: save on headless doesn't work as it used to since it now
|
|
|
|
// follows a link instead of fetch. Maybe there's a way to make it
|
|
|
|
// work? For now always set noSave.
|
2020-07-27 20:18:52 +02:00
|
|
|
const options = { noSave: true || !headless, stream: true, storage }; // only run the saveFile code if headless
|
2018-02-21 05:31:27 +01:00
|
|
|
|
|
|
|
// FileSender uses a File in real life but a Blob works for testing
|
2018-07-23 18:49:16 +02:00
|
|
|
const blob = new Blob([new ArrayBuffer(1024 * 128)], { type: 'text/plain' });
|
2018-02-21 05:31:27 +01:00
|
|
|
blob.name = 'test.txt';
|
2018-07-26 07:26:11 +02:00
|
|
|
const archive = new Archive([blob]);
|
2018-07-12 01:52:46 +02:00
|
|
|
navigator.serviceWorker.register('/serviceWorker.js');
|
2018-02-21 05:31:27 +01:00
|
|
|
|
|
|
|
describe('Upload / Download flow', function() {
|
2018-07-23 18:49:16 +02:00
|
|
|
this.timeout(0);
|
2018-02-21 05:31:27 +01:00
|
|
|
it('can only download once by default', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-02-21 05:31:27 +01:00
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: false
|
|
|
|
});
|
|
|
|
await fr.getMetadata();
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-07-23 18:49:16 +02:00
|
|
|
|
2018-02-21 05:31:27 +01:00
|
|
|
try {
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-02-21 05:31:27 +01:00
|
|
|
assert.fail('downloaded again');
|
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '404');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('downloads with the correct password', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-02-21 05:31:27 +01:00
|
|
|
await file.setPassword('magic');
|
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
url: file.url,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: true,
|
|
|
|
password: 'magic'
|
|
|
|
});
|
|
|
|
await fr.getMetadata();
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-02-21 05:31:27 +01:00
|
|
|
assert.equal(fr.state, 'complete');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('blocks invalid passwords from downloading', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-02-21 05:31:27 +01:00
|
|
|
await file.setPassword('magic');
|
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
url: file.url,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: true,
|
|
|
|
password: 'password'
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await fr.getMetadata();
|
|
|
|
assert.fail('got metadata with bad password');
|
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '401');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// We can't decrypt without IV from metadata
|
|
|
|
// but let's try to download anyway
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-02-21 05:31:27 +01:00
|
|
|
assert.fail('downloaded file with bad password');
|
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '401');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('retries a bad nonce', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-02-21 05:31:27 +01:00
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
nonce: null, // oops
|
|
|
|
requiresPassword: false
|
|
|
|
});
|
|
|
|
await fr.getMetadata();
|
2018-07-26 07:26:11 +02:00
|
|
|
assert.equal(fr.fileInfo.name, archive.name);
|
2018-02-21 05:31:27 +01:00
|
|
|
});
|
|
|
|
|
2018-02-24 20:24:12 +01:00
|
|
|
it('can cancel the upload', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const up = fs.upload(archive);
|
2018-02-24 20:24:12 +01:00
|
|
|
fs.cancel(); // before encrypting
|
|
|
|
try {
|
|
|
|
await up;
|
2018-06-21 02:05:33 +02:00
|
|
|
assert.fail('not cancelled 1');
|
2018-02-24 20:24:12 +01:00
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '0');
|
|
|
|
}
|
|
|
|
fs.reset();
|
|
|
|
fs.once('encrypting', () => fs.cancel());
|
|
|
|
try {
|
2018-08-31 23:20:15 +02:00
|
|
|
await fs.upload(archive);
|
2018-06-21 02:05:33 +02:00
|
|
|
assert.fail('not cancelled 2');
|
2018-02-24 20:24:12 +01:00
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '0');
|
|
|
|
}
|
|
|
|
fs.reset();
|
|
|
|
fs.once('progress', () => fs.cancel());
|
|
|
|
try {
|
2018-08-31 23:20:15 +02:00
|
|
|
await fs.upload(archive);
|
2018-06-21 02:05:33 +02:00
|
|
|
assert.fail('not cancelled 3');
|
2018-02-24 20:24:12 +01:00
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '0');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can cancel the download', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-02-24 20:24:12 +01:00
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: false
|
|
|
|
});
|
|
|
|
await fr.getMetadata();
|
|
|
|
fr.once('progress', () => fr.cancel());
|
|
|
|
try {
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-02-24 20:24:12 +01:00
|
|
|
assert.fail('not cancelled');
|
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '0');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-17 18:48:47 +02:00
|
|
|
it('can increase download count on download', async function() {
|
2018-07-23 18:49:16 +02:00
|
|
|
this.timeout(0);
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-07-17 18:48:47 +02:00
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: false
|
|
|
|
});
|
|
|
|
await fr.getMetadata();
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-07-17 18:48:47 +02:00
|
|
|
await file.updateDownloadCount();
|
|
|
|
assert.equal(file.dtotal, 1);
|
|
|
|
});
|
|
|
|
|
2018-07-25 21:29:19 +02:00
|
|
|
it('does not increase download count when download cancelled', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-07-25 21:29:19 +02:00
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: false
|
|
|
|
});
|
|
|
|
await fr.getMetadata();
|
|
|
|
fr.once('progress', () => fr.cancel());
|
2018-07-13 01:07:18 +02:00
|
|
|
|
2018-07-25 21:29:19 +02:00
|
|
|
try {
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-07-25 21:29:19 +02:00
|
|
|
assert.fail('not cancelled');
|
|
|
|
} catch (e) {
|
|
|
|
await file.updateDownloadCount();
|
|
|
|
assert.equal(file.dtotal, 0);
|
|
|
|
}
|
|
|
|
});
|
2018-07-13 01:07:18 +02:00
|
|
|
|
2018-02-21 05:31:27 +01:00
|
|
|
it('can allow multiple downloads', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
2020-07-25 03:11:50 +02:00
|
|
|
const a = new Archive([blob]);
|
|
|
|
a.dlimit = 2;
|
|
|
|
const file = await fs.upload(a);
|
2018-02-21 05:31:27 +01:00
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: false
|
|
|
|
});
|
|
|
|
await fr.getMetadata();
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-02-21 05:31:27 +01:00
|
|
|
await file.updateDownloadCount();
|
|
|
|
assert.equal(file.dtotal, 1);
|
|
|
|
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-02-21 05:31:27 +01:00
|
|
|
await file.updateDownloadCount();
|
|
|
|
assert.equal(file.dtotal, 2);
|
|
|
|
try {
|
2018-07-31 20:29:26 +02:00
|
|
|
await fr.download(options);
|
2018-02-21 05:31:27 +01:00
|
|
|
assert.fail('downloaded too many times');
|
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '404');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can delete the file before download', async function() {
|
2018-08-31 23:20:15 +02:00
|
|
|
const fs = new FileSender();
|
|
|
|
const file = await fs.upload(archive);
|
2018-02-21 05:31:27 +01:00
|
|
|
const fr = new FileReceiver({
|
|
|
|
secretKey: file.toJSON().secretKey,
|
|
|
|
id: file.id,
|
|
|
|
nonce: file.keychain.nonce,
|
|
|
|
requiresPassword: false
|
|
|
|
});
|
|
|
|
await file.del();
|
|
|
|
try {
|
|
|
|
await fr.getMetadata();
|
|
|
|
assert.fail('file still exists');
|
|
|
|
} catch (e) {
|
|
|
|
assert.equal(e.message, '404');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|