diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/lib/mock-encryptor.js | 32 | ||||
-rw-r--r-- | test/lib/mock-simple-keychain.js | 38 | ||||
-rw-r--r-- | test/unit/keyring-controller-test.js | 90 | ||||
-rw-r--r-- | test/unit/keyrings/simple-test.js | 83 |
4 files changed, 222 insertions, 21 deletions
diff --git a/test/lib/mock-encryptor.js b/test/lib/mock-encryptor.js new file mode 100644 index 000000000..09bbf7ad5 --- /dev/null +++ b/test/lib/mock-encryptor.js @@ -0,0 +1,32 @@ +var mockHex = '0xabcdef0123456789' +var mockKey = new Buffer(32) +let cacheVal + +module.exports = { + + encrypt(password, dataObj) { + cacheVal = dataObj + return Promise.resolve(mockHex) + }, + + decrypt(password, text) { + return Promise.resolve(cacheVal || {}) + }, + + encryptWithKey(key, dataObj) { + return this.encrypt(key, dataObj) + }, + + decryptWithKey(key, text) { + return this.decrypt(key, text) + }, + + keyFromPassword(password) { + return Promise.resolve(mockKey) + }, + + generateSalt() { + return 'WHADDASALT!' + }, + +} diff --git a/test/lib/mock-simple-keychain.js b/test/lib/mock-simple-keychain.js new file mode 100644 index 000000000..615b3e537 --- /dev/null +++ b/test/lib/mock-simple-keychain.js @@ -0,0 +1,38 @@ +var fakeWallet = { + privKey: '0x123456788890abcdef', + address: '0xfedcba0987654321', +} +const type = 'Simple Key Pair' + +module.exports = class MockSimpleKeychain { + + static type() { return type } + + constructor(opts) { + this.type = type + this.opts = opts || {} + this.wallets = [] + } + + serialize() { + return [ fakeWallet.privKey ] + } + + deserialize(data) { + if (!Array.isArray(data)) { + throw new Error('Simple keychain deserialize requires a privKey array.') + } + this.wallets = [ fakeWallet ] + } + + addAccounts(n = 1) { + for(var i = 0; i < n; i++) { + this.wallets.push(fakeWallet) + } + } + + getAccounts() { + return this.wallets.map(w => w.address) + } + +} diff --git a/test/unit/keyring-controller-test.js b/test/unit/keyring-controller-test.js index d3685a257..e216b0960 100644 --- a/test/unit/keyring-controller-test.js +++ b/test/unit/keyring-controller-test.js @@ -3,33 +3,81 @@ var KeyringController = require('../../app/scripts/keyring-controller') var configManagerGen = require('../lib/mock-config-manager') const ethUtil = require('ethereumjs-util') const async = require('async') +const mockEncryptor = require('../lib/mock-encryptor') +const MockSimpleKeychain = require('../lib/mock-simple-keychain') +const sinon = require('sinon') describe('KeyringController', function() { - describe('#createNewVault', function () { - let keyringController - let password = 'password123' - let entropy = 'entripppppyy duuude' - let seedWords - let accounts = [] - let originalKeystore - - before(function(done) { - window.localStorage = {} // Hacking localStorage support into JSDom - - keyringController = new KeyringController({ - configManager: configManagerGen(), - ethStore: { - addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) }, - }, - }) + let keyringController + let password = 'password123' + let entropy = 'entripppppyy duuude' + let seedWords + let accounts = [] + let originalKeystore - keyringController.createNewVault(password, entropy, (err, seeds) => { - assert.ifError(err, 'createNewVault threw error') - seedWords = seeds - originalKeystore = keyringController._idmgmt.keyStore + beforeEach(function(done) { + this.sinon = sinon.sandbox.create() + window.localStorage = {} // Hacking localStorage support into JSDom + + keyringController = new KeyringController({ + configManager: configManagerGen(), + ethStore: { + addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) }, + }, + }) + + // Stub out the browser crypto for a mock encryptor. + // Browser crypto is tested in the integration test suite. + keyringController.encryptor = mockEncryptor + + keyringController.createNewVault(password, null, function (err, state) { + done() + }) + }) + + afterEach(function() { + // Cleanup mocks + this.sinon.restore() + }) + + describe('#createNewVault', function () { + it('should set a vault on the configManager', function(done) { + keyringController.configManager.setVault(null) + assert(!keyringController.configManager.getVault(), 'no previous vault') + keyringController.createNewVault(password, null, function (err, state) { + assert.ifError(err) + const vault = keyringController.configManager.getVault() + assert(vault, 'vault created') done() }) }) }) + + describe('#restoreKeyring', function(done) { + + it(`should pass a keyring's serialized data back to the correct type.`, function() { + keyringController.keyringTypes = [ MockSimpleKeychain ] + + const mockSerialized = { + type: MockSimpleKeychain.type(), + data: [ '0x123456null788890abcdef' ], + } + const mock = this.sinon.mock(keyringController) + + mock.expects('loadBalanceAndNickname') + .exactly(1) + + var keyring = keyringController.restoreKeyring(0, mockSerialized) + assert.equal(keyring.wallets.length, 1, 'one wallet restored') + mock.verify() + }) + + }) + }) + + + + + diff --git a/test/unit/keyrings/simple-test.js b/test/unit/keyrings/simple-test.js new file mode 100644 index 000000000..ba000a7a8 --- /dev/null +++ b/test/unit/keyrings/simple-test.js @@ -0,0 +1,83 @@ +const assert = require('assert') +const extend = require('xtend') +const SimpleKeyring = require('../../../app/scripts/keyrings/simple') +const TYPE_STR = 'Simple Key Pair' + +// Sample account: +const privKeyHex = 'b8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952' + +describe('simple-keyring', function() { + + let keyring + beforeEach(function() { + keyring = new SimpleKeyring() + }) + + describe('Keyring.type()', function() { + it('is a class method that returns the type string.', function() { + const type = SimpleKeyring.type() + assert.equal(type, TYPE_STR) + }) + }) + + describe('#type', function() { + it('returns the correct value', function() { + const type = keyring.type + assert.equal(type, TYPE_STR) + }) + }) + + describe('#serialize empty wallets.', function() { + it('serializes an empty array', function() { + const output = keyring.serialize() + assert.deepEqual(output, []) + }) + }) + + describe('#deserialize a private key', function() { + it('serializes what it deserializes', function() { + keyring.deserialize([privKeyHex]) + assert.equal(keyring.wallets.length, 1, 'has one wallet') + + const serialized = keyring.serialize() + assert.equal(serialized[0], privKeyHex) + }) + }) + + describe('#addAccounts', function() { + describe('with no arguments', function() { + it('creates a single wallet', function() { + keyring.addAccounts() + assert.equal(keyring.wallets.length, 1) + }) + }) + + describe('with a numeric argument', function() { + it('creates that number of wallets', function() { + keyring.addAccounts(3) + assert.equal(keyring.wallets.length, 3) + }) + }) + }) + + describe('#getAccounts', function() { + it('calls getAddress on each wallet', function() { + + // Push a mock wallet + const desiredOutput = 'foo' + keyring.wallets.push({ + getAddress() { + return { + toString() { + return desiredOutput + } + } + } + }) + + const output = keyring.getAccounts() + assert.equal(output[0], desiredOutput) + assert.equal(output.length, 1) + }) + }) +}) |