diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/integration/index.html | 2 | ||||
-rw-r--r-- | test/integration/index.js | 21 | ||||
-rw-r--r-- | test/integration/lib/encryptor-test.js | 45 | ||||
-rw-r--r-- | test/integration/lib/first-time.js (renamed from test/integration/tests.js) | 3 | ||||
-rw-r--r-- | test/lib/mock-encryptor.js | 32 | ||||
-rw-r--r-- | test/lib/mock-simple-keychain.js | 38 | ||||
-rw-r--r-- | test/unit/actions/restore_vault_test.js | 60 | ||||
-rw-r--r-- | test/unit/actions/tx_test.js | 8 | ||||
-rw-r--r-- | test/unit/keyring-controller-test.js | 83 | ||||
-rw-r--r-- | test/unit/keyrings/simple-test.js | 83 |
10 files changed, 309 insertions, 66 deletions
diff --git a/test/integration/index.html b/test/integration/index.html index 6de40b046..ad4b4eb14 100644 --- a/test/integration/index.html +++ b/test/integration/index.html @@ -12,7 +12,7 @@ <script src="https://code.jquery.com/qunit/qunit-2.0.0.js"></script> <script src="./jquery-3.1.0.min.js"></script> <script src="helpers.js"></script> - <script src="tests.js"></script> + <script src="bundle.js"></script> <script src="/testem.js"></script> <iframe src="/development/index.html" height="500px" width="360px"> diff --git a/test/integration/index.js b/test/integration/index.js new file mode 100644 index 000000000..ff6d1baf8 --- /dev/null +++ b/test/integration/index.js @@ -0,0 +1,21 @@ +var fs = require('fs') +var path = require('path') +var browserify = require('browserify'); +var tests = fs.readdirSync(path.join(__dirname, 'lib')) +var bundlePath = path.join(__dirname, 'bundle.js') + +var b = browserify(); + +// Remove old bundle +try { + fs.unlinkSync(bundlePath) +} catch (e) {} + +var writeStream = fs.createWriteStream(bundlePath) + +tests.forEach(function(fileName) { + b.add(path.join(__dirname, 'lib', fileName)) +}) + +b.bundle().pipe(writeStream); + diff --git a/test/integration/lib/encryptor-test.js b/test/integration/lib/encryptor-test.js new file mode 100644 index 000000000..1c8a7605a --- /dev/null +++ b/test/integration/lib/encryptor-test.js @@ -0,0 +1,45 @@ +var encryptor = require('../../../app/scripts/lib/encryptor') + +QUnit.test('encryptor:serializeBufferForStorage', function (assert) { + assert.expect(1) + var buf = new Buffer(2) + buf[0] = 16 + buf[1] = 1 + + var output = encryptor.serializeBufferForStorage(buf) + + var expect = '0x1001' + assert.equal(expect, output) +}) + +QUnit.test('encryptor:serializeBufferFromStorage', function (assert) { + assert.expect(2) + var input = '0x1001' + var output = encryptor.serializeBufferFromStorage(input) + + assert.equal(output[0], 16) + assert.equal(output[1], 1) +}) + +QUnit.test('encryptor:encrypt & decrypt', function(assert) { + var done = assert.async(); + var password, data, encrypted + + password = 'a sample passw0rd' + data = { foo: 'data to encrypt' } + + encryptor.encrypt(password, data) + .then(function(encryptedStr) { + assert.equal(typeof encryptedStr, 'string', 'returns a string') + return encryptor.decrypt(password, encryptedStr) + }) + .then(function (decryptedObj) { + assert.deepEqual(decryptedObj, data, 'decrypted what was encrypted') + done() + }) + .catch(function(reason) { + assert.ifError(reason, 'threw an error') + done(reason) + }) + +}) diff --git a/test/integration/tests.js b/test/integration/lib/first-time.js index 92111b05b..af9b94e24 100644 --- a/test/integration/tests.js +++ b/test/integration/lib/first-time.js @@ -15,10 +15,11 @@ QUnit.test('agree to terms', function (assert) { assert.equal(title, 'MetaMask', 'title screen') var buttons = app.find('button') - assert.equal(buttons.length, 2, 'two buttons: create and restore') + assert.equal(buttons.length, 1, 'one button: create new vault') done() }) // Wait for view to transition: }) + 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/actions/restore_vault_test.js b/test/unit/actions/restore_vault_test.js deleted file mode 100644 index 609f5429e..000000000 --- a/test/unit/actions/restore_vault_test.js +++ /dev/null @@ -1,60 +0,0 @@ -var jsdom = require('mocha-jsdom') -var assert = require('assert') -var freeze = require('deep-freeze-strict') -var path = require('path') -var sinon = require('sinon') - -var actions = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'actions.js')) -var reducers = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'reducers.js')) - -describe('#recoverFromSeed(password, seed)', function() { - - beforeEach(function() { - // sinon allows stubbing methods that are easily verified - this.sinon = sinon.sandbox.create() - }) - - afterEach(function() { - // sinon requires cleanup otherwise it will overwrite context - this.sinon.restore() - }) - - // stub out account manager - actions._setAccountManager({ - recoverFromSeed(pw, seed, cb) { - cb(null, { - identities: { - foo: 'bar' - } - }) - }, - }) - - it('sets metamask.isUnlocked to true', function() { - var initialState = { - metamask: { - isUnlocked: false, - isInitialized: false, - } - } - freeze(initialState) - - const restorePhrase = 'invite heavy among daring outdoor dice jelly coil stable note seat vicious' - const password = 'foo' - const dispatchFunc = actions.recoverFromSeed(password, restorePhrase) - - var dispatchStub = this.sinon.stub() - dispatchStub.withArgs({ TYPE: actions.unlockMetamask() }).onCall(0) - dispatchStub.withArgs({ TYPE: actions.showAccountsPage() }).onCall(1) - - var action - var resultingState = initialState - dispatchFunc((newAction) => { - action = newAction - resultingState = reducers(resultingState, action) - }) - - assert.equal(resultingState.metamask.isUnlocked, true, 'was unlocked') - assert.equal(resultingState.metamask.isInitialized, true, 'was initialized') - }); -}); diff --git a/test/unit/actions/tx_test.js b/test/unit/actions/tx_test.js index c08a8aa26..1f06b1120 100644 --- a/test/unit/actions/tx_test.js +++ b/test/unit/actions/tx_test.js @@ -46,7 +46,7 @@ describe('tx confirmation screen', function() { describe('cancelTx', function() { before(function(done) { - actions._setAccountManager({ + actions._setBackgroundConnection({ approveTransaction(txId, cb) { cb('An error!') }, cancelTransaction(txId) { /* noop */ }, clearSeedWordCache(cb) { cb() }, @@ -75,7 +75,7 @@ describe('tx confirmation screen', function() { before(function(done) { alert = () => {/* noop */} - actions._setAccountManager({ + actions._setBackgroundConnection({ approveTransaction(txId, cb) { cb({message: 'An error!'}) }, }) @@ -96,7 +96,7 @@ describe('tx confirmation screen', function() { describe('when there is success', function() { it('should complete tx and go home', function() { - actions._setAccountManager({ + actions._setBackgroundConnection({ approveTransaction(txId, cb) { cb() }, }) @@ -135,7 +135,7 @@ describe('tx confirmation screen', function() { } freeze(initialState) - actions._setAccountManager({ + actions._setBackgroundConnection({ approveTransaction(txId, cb) { cb() }, }) diff --git a/test/unit/keyring-controller-test.js b/test/unit/keyring-controller-test.js new file mode 100644 index 000000000..e216b0960 --- /dev/null +++ b/test/unit/keyring-controller-test.js @@ -0,0 +1,83 @@ +var assert = require('assert') +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() { + + let keyringController + let password = 'password123' + let entropy = 'entripppppyy duuude' + let seedWords + let accounts = [] + let originalKeystore + + 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) + }) + }) +}) |