2018-02-23 10:08:23 +01:00
|
|
|
const assert = require('assert')
|
|
|
|
|
2018-05-21 14:59:26 +02:00
|
|
|
const EdgeEncryptor = require('../../../app/scripts/edge-encryptor')
|
2018-02-23 10:08:23 +01:00
|
|
|
|
|
|
|
var password = 'passw0rd1'
|
|
|
|
var data = 'some random data'
|
|
|
|
|
|
|
|
global.crypto = global.crypto || {
|
2018-02-23 10:49:56 +01:00
|
|
|
getRandomValues: function (array) {
|
2018-02-23 10:08:23 +01:00
|
|
|
for (let i = 0; i < array.length; i++) {
|
2018-02-23 10:49:56 +01:00
|
|
|
array[i] = Math.random() * 100
|
2018-02-23 10:08:23 +01:00
|
|
|
}
|
2018-02-23 10:49:56 +01:00
|
|
|
return array
|
2018-07-03 00:49:33 +02:00
|
|
|
},
|
2018-02-23 10:08:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('EdgeEncryptor', function () {
|
|
|
|
|
2018-02-23 10:49:56 +01:00
|
|
|
const edgeEncryptor = new EdgeEncryptor()
|
2018-02-23 10:08:23 +01:00
|
|
|
describe('encrypt', function () {
|
|
|
|
|
2018-02-23 10:49:56 +01:00
|
|
|
it('should encrypt the data.', function (done) {
|
2018-02-23 10:08:23 +01:00
|
|
|
edgeEncryptor.encrypt(password, data)
|
|
|
|
.then(function (encryptedData) {
|
|
|
|
assert.notEqual(data, encryptedData)
|
|
|
|
assert.notEqual(encryptedData.length, 0)
|
|
|
|
done()
|
|
|
|
}).catch(function (err) {
|
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-02-23 10:49:56 +01:00
|
|
|
it('should return proper format.', function (done) {
|
|
|
|
edgeEncryptor.encrypt(password, data)
|
|
|
|
.then(function (encryptedData) {
|
2018-07-03 00:49:33 +02:00
|
|
|
const encryptedObject = JSON.parse(encryptedData)
|
2018-02-23 10:49:56 +01:00
|
|
|
assert.ok(encryptedObject.data, 'there is no data')
|
2018-07-03 02:12:50 +02:00
|
|
|
assert.ok(encryptedObject.iv && encryptedObject.iv.length !== 0, 'there is no iv')
|
|
|
|
assert.ok(encryptedObject.salt && encryptedObject.salt.length !== 0, 'there is no salt')
|
2018-02-23 10:49:56 +01:00
|
|
|
done()
|
|
|
|
}).catch(function (err) {
|
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-02-23 11:11:14 +01:00
|
|
|
it('should not return the same twice.', function (done) {
|
2018-02-23 10:08:23 +01:00
|
|
|
|
|
|
|
const encryptPromises = []
|
|
|
|
encryptPromises.push(edgeEncryptor.encrypt(password, data))
|
|
|
|
encryptPromises.push(edgeEncryptor.encrypt(password, data))
|
|
|
|
|
|
|
|
Promise.all(encryptPromises).then((encryptedData) => {
|
|
|
|
assert.equal(encryptedData.length, 2)
|
|
|
|
assert.notEqual(encryptedData[0], encryptedData[1])
|
|
|
|
assert.notEqual(encryptedData[0].length, 0)
|
|
|
|
assert.notEqual(encryptedData[1].length, 0)
|
2018-02-23 11:11:14 +01:00
|
|
|
done()
|
2018-02-23 10:08:23 +01:00
|
|
|
})
|
2018-07-03 00:49:33 +02:00
|
|
|
})
|
2018-02-23 10:08:23 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('decrypt', function () {
|
2018-02-23 10:49:56 +01:00
|
|
|
it('should be able to decrypt the encrypted data.', function (done) {
|
2018-02-23 10:08:23 +01:00
|
|
|
|
|
|
|
edgeEncryptor.encrypt(password, data)
|
|
|
|
.then(function (encryptedData) {
|
|
|
|
edgeEncryptor.decrypt(password, encryptedData)
|
|
|
|
.then(function (decryptedData) {
|
|
|
|
assert.equal(decryptedData, data)
|
2018-02-23 10:49:56 +01:00
|
|
|
done()
|
2018-02-23 10:08:23 +01:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
2018-02-23 11:03:53 +01:00
|
|
|
|
|
|
|
it('cannot decrypt the encrypted data with wrong password.', function (done) {
|
|
|
|
|
|
|
|
edgeEncryptor.encrypt(password, data)
|
|
|
|
.then(function (encryptedData) {
|
|
|
|
edgeEncryptor.decrypt('wrong password', encryptedData)
|
2019-05-08 21:51:33 +02:00
|
|
|
.then(function () {
|
2018-02-23 11:03:53 +01:00
|
|
|
assert.fail('could decrypt with wrong password')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
assert.ok(err instanceof Error)
|
|
|
|
assert.equal(err.message, 'Incorrect password')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
2018-02-23 10:08:23 +01:00
|
|
|
})
|
|
|
|
})
|