A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/test/unit/app/edge-encryptor-test.js

102 lines
3.0 KiB

7 years ago
const assert = require('assert')
const EdgeEncryptor = require('../../../app/scripts/edge-encryptor')
7 years ago
var password = 'passw0rd1'
var data = 'some random data'
global.crypto = global.crypto || {
7 years ago
getRandomValues: function (array) {
7 years ago
for (let i = 0; i < array.length; i++) {
7 years ago
array[i] = Math.random() * 100
7 years ago
}
7 years ago
return array
6 years ago
},
7 years ago
}
describe('EdgeEncryptor', function () {
7 years ago
const edgeEncryptor = new EdgeEncryptor()
7 years ago
describe('encrypt', function () {
7 years ago
it('should encrypt the data.', function (done) {
7 years ago
edgeEncryptor.encrypt(password, data)
.then(function (encryptedData) {
assert.notEqual(data, encryptedData)
assert.notEqual(encryptedData.length, 0)
done()
}).catch(function (err) {
done(err)
})
})
7 years ago
it('should return proper format.', function (done) {
edgeEncryptor.encrypt(password, data)
.then(function (encryptedData) {
6 years ago
const encryptedObject = JSON.parse(encryptedData)
7 years ago
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')
7 years ago
done()
}).catch(function (err) {
done(err)
})
})
7 years ago
it('should not return the same twice.', function (done) {
7 years ago
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)
7 years ago
done()
7 years ago
})
6 years ago
})
7 years ago
})
describe('decrypt', function () {
7 years ago
it('should be able to decrypt the encrypted data.', function (done) {
7 years ago
edgeEncryptor.encrypt(password, data)
.then(function (encryptedData) {
edgeEncryptor.decrypt(password, encryptedData)
.then(function (decryptedData) {
assert.equal(decryptedData, data)
7 years ago
done()
7 years ago
})
.catch(function (err) {
done(err)
})
})
.catch(function (err) {
done(err)
})
})
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)
})
})
7 years ago
})
})