finished sender tests, figuring out array buffer to string conversions

This commit is contained in:
Abhinav Adduri 2017-07-12 13:05:06 -07:00
parent af0c497aab
commit b411447ebb
6 changed files with 222 additions and 93 deletions

View File

@ -52,7 +52,7 @@
"lint:js": "eslint .", "lint:js": "eslint .",
"start": "node server/server", "start": "node server/server",
"test": "mocha test/unit && mocha test/server", "test": "mocha test/unit && mocha test/server",
"test-browser": "watchify test/frontend/filetest.bundle.js -o test/frontend/bundle.js -d", "test-browser": "watchify test/frontend/frontend.bundle.js -o test/frontend/bundle.js -d",
"version": "node scripts/version" "version": "node scripts/version"
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -1,85 +0,0 @@
const FileSender = window.FileSender;
const FakeFile = window.FakeFile;
const stubXML = window.stubXML;
const assert = window.assert;
describe('File Sender', function() {
let xhr;
let request;
let server;
before(function() {
xhr = sinon.useFakeXMLHttpRequest();
server = sinon.fakeServer.create();
server.respondImmediately = true;
server.respondWith(
'POST',
'/upload',
function(request) {
request.respond(
200,
{'Content-Type': 'application/json'},
JSON.stringify({url: '1', id: '1', uuid: 'del'})
)
}
)
})
it('Should get a loading event emission', function() {
let file = new FakeFile('hello_world.txt', ['This is some data.'])
let fs = new FileSender(file);
let testLoading = true;
fs.on('loading', isStillLoading => {
assert(!(!testLoading && isStillLoading))
testLoading = isStillLoading
})
return fs.upload()
.then(info => {
assert(!testLoading)
})
.catch(err => {
assert.fail();
});
})
it('Should get a hashing event emission', function() {
let file = new FakeFile('hello_world.txt', ['This is some data.'])
let fs = new FileSender(file);
let testHashing = true;
fs.on('hashing', isStillHashing => {
assert(!(!testHashing && isStillHashing))
testHashing = isStillHashing
})
return fs.upload()
.then(info => {
assert(!testHashing)
})
.catch(err => {
assert.fail();
});
})
it('Should get a encrypting event emission', function() {
let file = new FakeFile('hello_world.txt', ['This is some data.'])
let fs = new FileSender(file);
let testEncrypting = true;
fs.on('encrypting', isStillEncrypting => {
assert(!(!testEncrypting && isStillEncrypting))
testEncrypting = isStillEncrypting
})
return fs.upload()
.then(info => {
assert(!testEncrypting)
})
.catch(err => {
assert.fail();
});
})
});

View File

@ -16,3 +16,6 @@ window.FileSender = require('../../frontend/src/fileSender');
window.FileReceiver = require('../../frontend/src/fileReceiver'); window.FileReceiver = require('../../frontend/src/fileReceiver');
window.sinon = require('sinon'); window.sinon = require('sinon');
window.assert = require('assert'); window.assert = require('assert');
const utils = require('../../frontend/src/utils');
window.hexToArray = utils.hexToArray;
window.arrayToHex = utils.arrayToHex;

View File

@ -11,10 +11,7 @@
<script src="../../node_modules/mocha/mocha.js"></script> <script src="../../node_modules/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script> <script>mocha.setup('bdd')</script>
<!-- load code you want to test here --> <script src="frontend.test.js"></script>
<script src="filesender.test.js"></script>
<script> <script>
mocha.checkLeaks(); mocha.checkLeaks();

View File

@ -0,0 +1,211 @@
const FileSender = window.FileSender;
const FakeFile = window.FakeFile;
const stubXML = window.stubXML;
const assert = window.assert;
let file;
let encryptedIV;
let fileHash;
let secretKey;
let blobReplacement;
describe('File Sender', function() {
let xhr;
let server;
before(function() {
xhr = sinon.useFakeXMLHttpRequest();
server = sinon.fakeServer.create();
server.respondImmediately = true;
server.respondWith(
'POST',
'/upload',
function(request) {
let 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() {
let file = new FakeFile('hello_world.txt', ['This is some data.'])
let fs = new FileSender(file);
let testLoading = true;
fs.on('loading', isStillLoading => {
assert(!(!testLoading && isStillLoading))
testLoading = isStillLoading
})
return fs.upload()
.then(info => {
assert(!testLoading)
})
.catch(err => {
assert.fail();
});
})
it('Should get a hashing event emission', function() {
let file = new FakeFile('hello_world.txt', ['This is some data.'])
blobReplacement = file;
let fs = new FileSender(file);
let testHashing = true;
fs.on('hashing', isStillHashing => {
assert(!(!testHashing && isStillHashing))
testHashing = isStillHashing
})
return fs.upload()
.then(info => {
assert(!testHashing)
})
.catch(err => {
assert.fail();
});
})
it('Should get a encrypting event emission', function() {
let file = new FakeFile('hello_world.txt', ['This is some data.'])
let fs = new FileSender(file);
let testEncrypting = true;
fs.on('encrypting', isStillEncrypting => {
assert(!(!testEncrypting && isStillEncrypting))
testEncrypting = isStillEncrypting
})
return fs.upload()
.then(info => {
assert(!testEncrypting)
})
.catch(err => {
assert.fail();
});
})
it('Should encrypt a file properly', function(done) {
let newFile = new FakeFile('hello_world.txt', ['This is some data.'])
let fs = new FileSender(newFile);
fs.upload().then(info => {
let key = info.secretKey;
secretKey = info.secretKey;
let IV = info.fileId;
encryptedIV = info.fileId;
let readRaw = new FileReader;
readRaw.onload = function(event) {
let rawArray = new Uint8Array(this.result);
window.crypto.subtle.digest('SHA-256', rawArray).then(hash => {
fileHash = hash;
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),
additionalData: hash,
tagLength: 128
},
cryptoKey,
rawArray
)
.then(encrypted => {
assert(new Uint8Array(encrypted).toString() ===
new Uint8Array(file).toString());
done();
})
})
})
}
readRaw.readAsArrayBuffer(newFile);
})
})
});
describe('File Receiver', function(done) {
const cb = function(done) {
if (file === undefined ||
encryptedIV === undefined ||
fileHash === undefined ||
secretKey === undefined) {
setTimeout(cb, 1000);
return;
}
let xhr;
let server;
xhr = sinon.useFakeXMLHttpRequest();
server = sinon.fakeServer.create();
server.respondImmediately = true;
// xhr.prototype.response = file;
server.respondWith(
'GET',
'/assets' + location.pathname.slice(0, -1),
function(request) {
request.respond(
200,
{
'Content-Disposition': 'attachment; filename=hello_world.txt',
'Content-Type': 'application/octet-stream',
'Content-Length': file.byteLength,
'X-File-Metadata': JSON.stringify({
aad: arrayToHex(new Uint8Array(fileHash)),
filename: 'hello_world.txt',
id: encryptedIV
})
},
String.fromCharCode.apply(String, new Uint8Array(file))
)
}
)
done();
}
before(cb)
it('Should decrypt properly', function() {
let fr = new FileReceiver();
location.hash = secretKey;
return fr.download().then(([decrypted, name]) => {
console.log('here')
}).catch(err => {
console.log(err, err.stack)
})
})
})