send/test/frontend/frontend.test.js

231 lines
5.5 KiB
JavaScript
Raw Normal View History

const FileSender = window.FileSender;
2017-07-12 23:23:55 +00:00
const FileReceiver = window.FileReceiver;
const FakeFile = window.FakeFile;
const assert = window.assert;
2017-07-12 23:23:55 +00:00
const server = window.server;
const hexToArray = window.hexToArray;
const sinon = window.sinon;
let file;
let encryptedIV;
let secretKey;
2017-07-12 23:23:55 +00:00
let originalBlob;
describe('File Sender', function() {
before(function() {
2017-08-02 23:50:58 +00:00
server.respondImmediately = true;
server.respondWith('POST', '/upload', function(request) {
const reader = new FileReader();
reader.readAsArrayBuffer(request.requestBody.get('data'));
reader.onload = function(event) {
file = this.result;
};
const responseObj = JSON.parse(request.requestHeaders['X-File-Metadata']);
request.respond(
200,
{ 'Content-Type': 'application/json' },
JSON.stringify({
url: 'some url',
id: responseObj.id,
delete: responseObj.delete
})
);
});
});
it('Should get a loading event emission', function() {
2017-08-02 23:50:58 +00:00
const file = new FakeFile('hello_world.txt', ['This is some data.']);
const fs = new FileSender(file);
let testLoading = true;
2017-08-02 23:50:58 +00:00
fs.on('loading', isStillLoading => {
assert(!(!testLoading && isStillLoading));
testLoading = isStillLoading;
});
2017-08-02 23:50:58 +00:00
return fs
.upload()
.then(info => {
assert(info);
assert(!testLoading);
})
.catch(err => {
console.log(err, err.stack);
assert.fail();
});
});
it('Should get a encrypting event emission', function() {
2017-08-02 23:50:58 +00:00
const file = new FakeFile('hello_world.txt', ['This is some data.']);
2017-07-12 23:23:55 +00:00
const fs = new FileSender(file);
let testEncrypting = true;
fs.on('encrypting', isStillEncrypting => {
2017-07-12 23:23:55 +00:00
assert(!(!testEncrypting && isStillEncrypting));
testEncrypting = isStillEncrypting;
2017-08-02 23:50:58 +00:00
});
2017-08-02 23:50:58 +00:00
return fs
.upload()
.then(info => {
2017-07-12 23:23:55 +00:00
assert(info);
assert(!testEncrypting);
})
.catch(err => {
2017-07-12 23:23:55 +00:00
console.log(err, err.stack);
assert.fail();
});
2017-08-02 23:50:58 +00:00
});
it('Should encrypt a file properly', function(done) {
2017-08-02 23:50:58 +00:00
const newFile = new FakeFile('hello_world.txt', ['This is some data.']);
2017-07-12 23:23:55 +00:00
const fs = new FileSender(newFile);
fs.upload().then(info => {
2017-07-12 23:23:55 +00:00
const key = info.secretKey;
secretKey = info.secretKey;
2017-07-12 23:23:55 +00:00
const IV = info.fileId;
encryptedIV = info.fileId;
2017-08-02 23:50:58 +00:00
const readRaw = new FileReader();
readRaw.onload = function(event) {
2017-07-12 23:23:55 +00:00
const rawArray = new Uint8Array(this.result);
originalBlob = rawArray;
window.crypto.subtle
.importKey(
'jwk',
{
kty: 'oct',
k: key,
alg: 'A128GCM',
ext: true
},
{
name: 'AES-GCM'
},
true,
['encrypt', 'decrypt']
)
.then(cryptoKey => {
window.crypto.subtle
.encrypt(
{
name: 'AES-GCM',
iv: hexToArray(IV),
tagLength: 128
},
cryptoKey,
rawArray
)
.then(encrypted => {
assert(
new Uint8Array(encrypted).toString() ===
new Uint8Array(file).toString()
);
done();
});
});
2017-08-02 23:50:58 +00:00
};
readRaw.readAsArrayBuffer(newFile);
2017-08-02 23:50:58 +00:00
});
});
});
2017-07-12 23:23:55 +00:00
describe('File Receiver', function() {
class FakeXHR {
constructor() {
this.response = file;
this.status = 200;
}
static setup() {
FakeXHR.prototype.open = sinon.spy();
2017-08-02 23:50:58 +00:00
FakeXHR.prototype.send = function() {
2017-07-12 23:23:55 +00:00
this.onload();
2017-08-02 23:50:58 +00:00
};
2017-07-12 23:23:55 +00:00
FakeXHR.prototype.originalXHR = window.XMLHttpRequest;
2017-08-02 23:50:58 +00:00
FakeXHR.prototype.getResponseHeader = function() {
2017-07-12 23:23:55 +00:00
return JSON.stringify({
filename: 'hello_world.txt',
id: encryptedIV
2017-08-02 23:50:58 +00:00
});
};
2017-07-12 23:23:55 +00:00
window.XMLHttpRequest = FakeXHR;
}
static restore() {
// originalXHR is a sinon FakeXMLHttpRequest, since
// fakeServer.create() is called in frontend.bundle.js
window.XMLHttpRequest.prototype.originalXHR.restore();
}
}
2017-08-02 23:50:58 +00:00
const cb = function(done) {
2017-08-02 23:50:58 +00:00
if (
file === undefined ||
encryptedIV === undefined ||
secretKey === undefined
) {
assert.fail(
'Please run file sending tests before trying to receive the files.'
);
2017-07-12 23:23:55 +00:00
done();
}
2017-07-12 23:23:55 +00:00
FakeXHR.setup();
done();
2017-08-02 23:50:58 +00:00
};
2017-08-02 23:50:58 +00:00
before(cb);
2017-07-12 23:23:55 +00:00
after(function() {
FakeXHR.restore();
2017-08-02 23:50:58 +00:00
});
2017-07-12 23:23:55 +00:00
it('Should decrypt properly', function() {
2017-07-12 23:23:55 +00:00
const fr = new FileReceiver();
location.hash = secretKey;
2017-08-02 23:50:58 +00:00
return fr
.download()
.then(([decrypted, name]) => {
assert(name);
assert(
new Uint8Array(decrypted).toString() ===
new Uint8Array(originalBlob).toString()
);
})
.catch(err => {
console.log(err, err.stack);
assert.fail();
});
});
2017-07-12 23:23:55 +00:00
it('Should emit decrypting events', function() {
const fr = new FileReceiver();
location.hash = secretKey;
2017-07-12 23:23:55 +00:00
let testDecrypting = true;
fr.on('decrypting', isStillDecrypting => {
assert(!(!testDecrypting && isStillDecrypting));
testDecrypting = isStillDecrypting;
});
2017-08-02 23:50:58 +00:00
return fr
.download()
.then(([decrypted, name]) => {
assert(decrypted);
assert(name);
assert(!testDecrypting);
})
.catch(err => {
console.log(err, err.stack);
assert.fail();
});
});
});