1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 18:41:38 +01:00
metamask-extension/test/unit/app/edge-encryptor-test.js

102 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-02-23 10:08:23 +01:00
const assert = require('assert')
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-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) {
let encryptedObject = JSON.parse(encryptedData)
assert.ok(encryptedObject.data, 'there is no data')
assert.ok(encryptedObject.iv && encryptedObject.iv.length != 0, 'there is no iv')
assert.ok(encryptedObject.salt && encryptedObject.salt.length != 0, 'there is no salt')
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
})
})
})
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)
.then(function (decryptedData) {
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
})
})