2017-07-12 22:05:06 +02:00
|
|
|
const FileSender = window.FileSender;
|
2017-07-13 01:23:55 +02:00
|
|
|
const FileReceiver = window.FileReceiver;
|
2017-07-12 22:05:06 +02:00
|
|
|
const FakeFile = window.FakeFile;
|
|
|
|
const assert = window.assert;
|
2017-07-13 01:23:55 +02:00
|
|
|
const server = window.server;
|
|
|
|
const hexToArray = window.hexToArray;
|
|
|
|
const arrayToHex = window.arrayToHex;
|
|
|
|
const sinon = window.sinon;
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
let file;
|
|
|
|
let encryptedIV;
|
|
|
|
let fileHash;
|
|
|
|
let secretKey;
|
2017-07-13 01:23:55 +02:00
|
|
|
let originalBlob;
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
describe('File Sender', function() {
|
|
|
|
before(function() {
|
2017-08-03 01:50:58 +02: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
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
it('Should get a loading event emission', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
const file = new FakeFile('hello_world.txt', ['This is some data.']);
|
|
|
|
const fs = new FileSender(file);
|
|
|
|
let testLoading = true;
|
2017-07-12 22:05:06 +02:00
|
|
|
|
2017-08-03 01:50:58 +02:00
|
|
|
fs.on('loading', isStillLoading => {
|
|
|
|
assert(!(!testLoading && isStillLoading));
|
|
|
|
testLoading = isStillLoading;
|
|
|
|
});
|
2017-07-12 22:05:06 +02:00
|
|
|
|
2017-08-03 01:50:58 +02:00
|
|
|
return fs
|
|
|
|
.upload()
|
|
|
|
.then(info => {
|
|
|
|
assert(info);
|
|
|
|
assert(!testLoading);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err, err.stack);
|
|
|
|
assert.fail();
|
|
|
|
});
|
|
|
|
});
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
it('Should get a hashing event emission', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
const file = new FakeFile('hello_world.txt', ['This is some data.']);
|
2017-07-13 01:23:55 +02:00
|
|
|
const fs = new FileSender(file);
|
2017-07-12 22:05:06 +02:00
|
|
|
let testHashing = true;
|
|
|
|
|
|
|
|
fs.on('hashing', isStillHashing => {
|
2017-08-03 01:50:58 +02:00
|
|
|
assert(!(!testHashing && isStillHashing));
|
|
|
|
testHashing = isStillHashing;
|
|
|
|
});
|
|
|
|
|
|
|
|
return fs
|
|
|
|
.upload()
|
|
|
|
.then(info => {
|
|
|
|
assert(info);
|
|
|
|
assert(!testHashing);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err, err.stack);
|
|
|
|
assert.fail();
|
|
|
|
});
|
|
|
|
});
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
it('Should get a encrypting event emission', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
const file = new FakeFile('hello_world.txt', ['This is some data.']);
|
2017-07-13 01:23:55 +02:00
|
|
|
const fs = new FileSender(file);
|
2017-07-12 22:05:06 +02:00
|
|
|
let testEncrypting = true;
|
|
|
|
|
|
|
|
fs.on('encrypting', isStillEncrypting => {
|
2017-07-13 01:23:55 +02:00
|
|
|
assert(!(!testEncrypting && isStillEncrypting));
|
|
|
|
testEncrypting = isStillEncrypting;
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-12 22:05:06 +02:00
|
|
|
|
2017-08-03 01:50:58 +02:00
|
|
|
return fs
|
|
|
|
.upload()
|
2017-07-12 22:05:06 +02:00
|
|
|
.then(info => {
|
2017-07-13 01:23:55 +02:00
|
|
|
assert(info);
|
|
|
|
assert(!testEncrypting);
|
2017-07-12 22:05:06 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2017-07-13 01:23:55 +02:00
|
|
|
console.log(err, err.stack);
|
2017-07-12 22:05:06 +02:00
|
|
|
assert.fail();
|
|
|
|
});
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
it('Should encrypt a file properly', function(done) {
|
2017-08-03 01:50:58 +02:00
|
|
|
const newFile = new FakeFile('hello_world.txt', ['This is some data.']);
|
2017-07-13 01:23:55 +02:00
|
|
|
const fs = new FileSender(newFile);
|
2017-07-12 22:05:06 +02:00
|
|
|
fs.upload().then(info => {
|
2017-07-13 01:23:55 +02:00
|
|
|
const key = info.secretKey;
|
2017-07-12 22:05:06 +02:00
|
|
|
secretKey = info.secretKey;
|
2017-07-13 01:23:55 +02:00
|
|
|
const IV = info.fileId;
|
2017-07-12 22:05:06 +02:00
|
|
|
encryptedIV = info.fileId;
|
2017-08-03 01:50:58 +02:00
|
|
|
|
|
|
|
const readRaw = new FileReader();
|
2017-07-12 22:05:06 +02:00
|
|
|
readRaw.onload = function(event) {
|
2017-07-13 01:23:55 +02:00
|
|
|
const rawArray = new Uint8Array(this.result);
|
|
|
|
originalBlob = rawArray;
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
window.crypto.subtle.digest('SHA-256', rawArray).then(hash => {
|
|
|
|
fileHash = hash;
|
2017-08-03 01:50:58 +02:00
|
|
|
window.crypto.subtle
|
|
|
|
.importKey(
|
|
|
|
'jwk',
|
2017-07-12 22:05:06 +02:00
|
|
|
{
|
2017-08-03 01:50:58 +02:00
|
|
|
kty: 'oct',
|
|
|
|
k: key,
|
|
|
|
alg: 'A128GCM',
|
|
|
|
ext: true
|
2017-07-12 22:05:06 +02:00
|
|
|
},
|
2017-08-03 01:50:58 +02:00
|
|
|
{
|
|
|
|
name: 'AES-GCM'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
2017-07-12 22:05:06 +02:00
|
|
|
)
|
2017-08-03 01:50:58 +02:00
|
|
|
.then(cryptoKey => {
|
|
|
|
window.crypto.subtle
|
|
|
|
.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: hexToArray(IV),
|
|
|
|
additionalData: hash,
|
|
|
|
tagLength: 128
|
|
|
|
},
|
|
|
|
cryptoKey,
|
|
|
|
rawArray
|
|
|
|
)
|
|
|
|
.then(encrypted => {
|
|
|
|
assert(
|
|
|
|
new Uint8Array(encrypted).toString() ===
|
|
|
|
new Uint8Array(file).toString()
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2017-07-12 22:05:06 +02:00
|
|
|
|
|
|
|
readRaw.readAsArrayBuffer(newFile);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-12 22:05:06 +02:00
|
|
|
});
|
|
|
|
|
2017-07-13 01:23:55 +02:00
|
|
|
describe('File Receiver', function() {
|
|
|
|
class FakeXHR {
|
|
|
|
constructor() {
|
|
|
|
this.response = file;
|
|
|
|
this.status = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
static setup() {
|
|
|
|
FakeXHR.prototype.open = sinon.spy();
|
2017-08-03 01:50:58 +02:00
|
|
|
FakeXHR.prototype.send = function() {
|
2017-07-13 01:23:55 +02:00
|
|
|
this.onload();
|
2017-08-03 01:50:58 +02:00
|
|
|
};
|
2017-07-13 01:23:55 +02:00
|
|
|
|
|
|
|
FakeXHR.prototype.originalXHR = window.XMLHttpRequest;
|
|
|
|
|
2017-08-03 01:50:58 +02:00
|
|
|
FakeXHR.prototype.getResponseHeader = function() {
|
2017-07-13 01:23:55 +02:00
|
|
|
return JSON.stringify({
|
|
|
|
aad: arrayToHex(new Uint8Array(fileHash)),
|
|
|
|
filename: 'hello_world.txt',
|
|
|
|
id: encryptedIV
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
|
|
|
};
|
2017-07-13 01:23:55 +02: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-03 01:50:58 +02:00
|
|
|
|
2017-07-12 22:05:06 +02:00
|
|
|
const cb = function(done) {
|
2017-08-03 01:50:58 +02:00
|
|
|
if (
|
|
|
|
file === undefined ||
|
|
|
|
encryptedIV === undefined ||
|
|
|
|
fileHash === undefined ||
|
|
|
|
secretKey === undefined
|
|
|
|
) {
|
|
|
|
assert.fail(
|
|
|
|
'Please run file sending tests before trying to receive the files.'
|
|
|
|
);
|
2017-07-13 01:23:55 +02:00
|
|
|
done();
|
2017-07-12 22:05:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 01:23:55 +02:00
|
|
|
FakeXHR.setup();
|
2017-07-12 22:05:06 +02:00
|
|
|
done();
|
2017-08-03 01:50:58 +02:00
|
|
|
};
|
2017-07-12 22:05:06 +02:00
|
|
|
|
2017-08-03 01:50:58 +02:00
|
|
|
before(cb);
|
2017-07-12 22:05:06 +02:00
|
|
|
|
2017-07-13 01:23:55 +02:00
|
|
|
after(function() {
|
|
|
|
FakeXHR.restore();
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-13 01:23:55 +02:00
|
|
|
|
2017-07-12 22:05:06 +02:00
|
|
|
it('Should decrypt properly', function() {
|
2017-07-13 01:23:55 +02:00
|
|
|
const fr = new FileReceiver();
|
|
|
|
location.hash = secretKey;
|
2017-08-03 01:50:58 +02: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-13 01:23:55 +02:00
|
|
|
|
|
|
|
it('Should emit decrypting events', function() {
|
|
|
|
const fr = new FileReceiver();
|
2017-07-12 22:05:06 +02:00
|
|
|
location.hash = secretKey;
|
2017-07-13 01:23:55 +02:00
|
|
|
|
|
|
|
let testDecrypting = true;
|
|
|
|
|
|
|
|
fr.on('decrypting', isStillDecrypting => {
|
|
|
|
assert(!(!testDecrypting && isStillDecrypting));
|
|
|
|
testDecrypting = isStillDecrypting;
|
|
|
|
});
|
|
|
|
|
2017-07-13 18:53:59 +02:00
|
|
|
fr.on('safe', isSafe => {
|
|
|
|
assert(isSafe);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return fr
|
|
|
|
.download()
|
|
|
|
.then(([decrypted, name]) => {
|
|
|
|
assert(decrypted);
|
|
|
|
assert(name);
|
|
|
|
assert(!testDecrypting);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err, err.stack);
|
|
|
|
assert.fail();
|
|
|
|
});
|
|
|
|
});
|
2017-07-13 01:23:55 +02:00
|
|
|
|
|
|
|
it('Should emit hashing events', function() {
|
|
|
|
const fr = new FileReceiver();
|
|
|
|
location.hash = secretKey;
|
|
|
|
|
|
|
|
let testHashing = true;
|
|
|
|
|
|
|
|
fr.on('hashing', isStillHashing => {
|
|
|
|
assert(!(!testHashing && isStillHashing));
|
|
|
|
testHashing = isStillHashing;
|
|
|
|
});
|
|
|
|
|
2017-07-13 18:53:59 +02:00
|
|
|
fr.on('safe', isSafe => {
|
|
|
|
assert(isSafe);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-13 18:53:59 +02:00
|
|
|
|
2017-08-03 01:50:58 +02:00
|
|
|
return fr
|
|
|
|
.download()
|
|
|
|
.then(([decrypted, name]) => {
|
|
|
|
assert(decrypted);
|
|
|
|
assert(name);
|
|
|
|
assert(!testHashing);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
assert.fail();
|
|
|
|
});
|
|
|
|
});
|
2017-07-13 01:23:55 +02:00
|
|
|
|
2017-07-13 21:14:27 +02:00
|
|
|
it('Should catch fraudulent checksums', function(done) {
|
|
|
|
// Use the secret key and file hash of the previous file to encrypt,
|
|
|
|
// which has a different hash than this one (different strings).
|
2017-08-03 01:50:58 +02:00
|
|
|
const newFile = new FakeFile('hello_world.txt', [
|
|
|
|
'This is some data, with a changed hash.'
|
|
|
|
]);
|
2017-07-13 01:23:55 +02:00
|
|
|
const readRaw = new FileReader();
|
2017-08-03 01:50:58 +02:00
|
|
|
|
2017-07-13 01:23:55 +02:00
|
|
|
readRaw.onload = function(event) {
|
|
|
|
const plaintext = new Uint8Array(this.result);
|
2017-08-03 01:50:58 +02:00
|
|
|
window.crypto.subtle
|
|
|
|
.importKey(
|
|
|
|
'jwk',
|
|
|
|
{
|
|
|
|
kty: 'oct',
|
|
|
|
k: secretKey,
|
|
|
|
alg: 'A128GCM',
|
|
|
|
ext: true
|
|
|
|
},
|
2017-07-13 01:23:55 +02:00
|
|
|
{
|
2017-08-03 01:50:58 +02:00
|
|
|
name: 'AES-GCM'
|
2017-07-13 01:23:55 +02:00
|
|
|
},
|
2017-08-03 01:50:58 +02:00
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
2017-07-13 01:23:55 +02:00
|
|
|
)
|
2017-08-03 01:50:58 +02:00
|
|
|
.then(key => {
|
|
|
|
// The file hash used here is the hash of the fake
|
|
|
|
// file from the previous test; it's a phony checksum.
|
|
|
|
return window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: hexToArray(encryptedIV),
|
|
|
|
additionalData: fileHash,
|
|
|
|
tagLength: 128
|
|
|
|
},
|
|
|
|
key,
|
|
|
|
plaintext
|
|
|
|
);
|
2017-07-13 01:23:55 +02:00
|
|
|
})
|
2017-08-03 01:50:58 +02:00
|
|
|
.then(encrypted => {
|
|
|
|
file = encrypted;
|
|
|
|
const fr = new FileReceiver();
|
|
|
|
location.hash = secretKey;
|
|
|
|
|
|
|
|
fr.on('unsafe', isUnsafe => {
|
|
|
|
assert(isUnsafe);
|
|
|
|
});
|
|
|
|
|
|
|
|
fr.on('safe', () => {
|
|
|
|
// This event should not be emitted.
|
|
|
|
assert.fail();
|
|
|
|
});
|
|
|
|
|
|
|
|
fr
|
|
|
|
.download()
|
|
|
|
.then(() => {
|
|
|
|
assert.fail();
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
assert(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2017-07-13 01:23:55 +02:00
|
|
|
readRaw.readAsArrayBuffer(newFile);
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
2017-07-13 01:23:55 +02:00
|
|
|
|
|
|
|
it('Should not decrypt with an incorrect checksum', function() {
|
2017-08-03 01:50:58 +02:00
|
|
|
FakeXHR.prototype.getResponseHeader = function() {
|
2017-07-13 01:23:55 +02:00
|
|
|
return JSON.stringify({
|
|
|
|
aad: 'some_bad_hashz',
|
|
|
|
filename: 'hello_world.txt',
|
|
|
|
id: encryptedIV
|
2017-08-03 01:50:58 +02:00
|
|
|
});
|
|
|
|
};
|
2017-07-13 01:23:55 +02:00
|
|
|
|
|
|
|
const fr = new FileReceiver();
|
|
|
|
location.hash = secretKey;
|
|
|
|
|
2017-08-03 01:50:58 +02:00
|
|
|
return fr
|
|
|
|
.download()
|
|
|
|
.then(([decrypted, name]) => {
|
|
|
|
assert(decrypted);
|
|
|
|
assert(name);
|
|
|
|
assert.fail();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
assert(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|