From 534895a00927c50bd253853000ddccac98cacc22 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 15 Aug 2014 18:55:26 -0400 Subject: [PATCH] rebase --- js/models/core/WalletLock.js | 1 + test/test.LocalEncrypted.js | 418 +++++++++++++++------------- test/test.WalletLock.js | 25 +- test/test.storage.LocalEncrypted.js | 284 ------------------- 4 files changed, 240 insertions(+), 488 deletions(-) delete mode 100644 test/test.storage.LocalEncrypted.js diff --git a/js/models/core/WalletLock.js b/js/models/core/WalletLock.js index 329f5d9eb..edd2bf67d 100644 --- a/js/models/core/WalletLock.js +++ b/js/models/core/WalletLock.js @@ -12,6 +12,7 @@ function WalletLock(storage, walletId, timeoutMin) { this.key = WalletLock._keyFor(walletId); this._setLock(); } + WalletLock._keyFor = function(walletId) { return 'lock' + '::' + walletId; }; diff --git a/test/test.LocalEncrypted.js b/test/test.LocalEncrypted.js index f5c96ba98..d010bc805 100644 --- a/test/test.LocalEncrypted.js +++ b/test/test.LocalEncrypted.js @@ -1,11 +1,22 @@ +//Crypto Mock +CryptoJS = {}; +CryptoJS.AES = {}; +CryptoJS.AES.encrypt = function(a) { + return a; +}; + +CryptoJS.enc = { + utf8: '' +}; + +CryptoJS.AES.decrypt = function(a) { + return a; +}; + 'use strict'; var chai = chai || require('chai'); var should = chai.should(); -var fakeWallet = 'fake-wallet-id'; -var timeStamp = Date.now(); -var localMock = require('./mocks/FakeLocalStorage'); var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined'; - if (is_browser) { var copay = require('copay'); //browser } else { @@ -13,226 +24,245 @@ if (is_browser) { } var LocalEncrypted = copay.StorageLocalEncrypted; +var fakeWallet = 'fake-wallet-id'; +var timeStamp = Date.now(); +var localMock = require('./mocks/FakeLocalStorage'); +var sessionMock = require('./mocks/FakeLocalStorage'); + + describe('Storage/LocalEncrypted model', function() { var s = new LocalEncrypted({ localStorage: localMock, + sessionStorage: sessionMock, }); s._setPassphrase('mysupercoolpassword'); it('should create an instance', function() { var s2 = new LocalEncrypted({ localStorage: localMock, + sessionStorage: sessionMock, }); should.exist(s2); }); -}); - -it('should fail when encrypting without a password', function() { - var s2 = new LocalEncrypted({ - localStorage: localMock, + it('should fail when encrypting without a password', function() { + var s2 = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + }); + (function() { + s2.set(fakeWallet, timeStamp, 1); + }).should.throw(); }); - (function() { - s2.set(fakeWallet, timeStamp, 1); - }).should.throw(); -}); -it('should be able to encrypt and decrypt', function() { - s._write(fakeWallet + timeStamp, 'value'); - s._read(fakeWallet + timeStamp).should.equal('value'); - localMock.removeItem(fakeWallet + timeStamp); -}); -it('should be able to set a value', function() { - s.set(fakeWallet, timeStamp, 1); - localMock.removeItem(fakeWallet + '::' + timeStamp); -}); -var getSetData = [ - 1, 1000, -15, -1000, - 0.1, -0.5, -0.5e-10, Math.PI, - 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', - '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], { - x: 1, - y: 2 - }, { - x: 'hi', - y: null - }, { - a: {}, - b: [], - c: [1, 2, 'hi'] - }, - null -]; -getSetData.forEach(function(obj) { - it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { - s.set(fakeWallet, timeStamp, obj); - var obj2 = s.get(fakeWallet, timeStamp); - JSON.stringify(obj2).should.equal(JSON.stringify(obj)); + it('should be able to encrypt and decrypt', function() { + s._write(fakeWallet + timeStamp, 'value'); + s._read(fakeWallet + timeStamp).should.equal('value'); + localMock.removeItem(fakeWallet + timeStamp); + }); + it('should be able to set a value', function() { + s.set(fakeWallet, timeStamp, 1); localMock.removeItem(fakeWallet + '::' + timeStamp); }); -}); - -describe('#export', function() { - it('should export the encrypted wallet', function() { - var storage = new LocalEncrypted({ - localStorage: localMock, - password: 'password', - }); - storage.set(fakeWallet, timeStamp, 'testval'); - var obj = { - test: 'testval' - }; - var encrypted = storage.export(obj); - encrypted.length.should.be.greaterThan(10); - localMock.removeItem(fakeWallet + '::' + timeStamp); - //encrypted.slice(0,6).should.equal("53616c"); - }); -}); -describe('#_decryptObj', function() { - it('should decrypt and Obj', function() { - var storage = new LocalEncrypted({ - password: 'password', - localStorage: localMock, - }); - storage._decryptObj('{"a":"2"}').should.deep.equal({ - a: "2" + var getSetData = [ + 1, 1000, -15, -1000, + 0.1, -0.5, -0.5e-10, Math.PI, + 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', + '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], { + x: 1, + y: 2 + }, { + x: 'hi', + y: null + }, { + a: {}, + b: [], + c: [1, 2, 'hi'] + }, + null + ]; + getSetData.forEach(function(obj) { + it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { + s.set(fakeWallet, timeStamp, obj); + var obj2 = s.get(fakeWallet, timeStamp); + JSON.stringify(obj2).should.equal(JSON.stringify(obj)); + localMock.removeItem(fakeWallet + '::' + timeStamp); }); }); -}); - -describe('#remove', function() { - it('should remove an item', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' + describe('#export', function() { + it('should export the encrypted wallet', function() { + var storage = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password', + }); + storage.set(fakeWallet, timeStamp, 'testval'); + var obj = { + test: 'testval' + }; + var encrypted = storage.export(obj); + encrypted.length.should.be.greaterThan(10); + localMock.removeItem(fakeWallet + '::' + timeStamp); + //encrypted.slice(0,6).should.equal("53616c"); }); - s.set('1', "hola", 'juan'); - s.get('1', 'hola').should.equal('juan'); - s.remove('1', 'hola'); - - should.not.exist(s.get('1', 'hola')); }); -}); - -describe('#getWalletIds', function() { - it('should get wallet ids', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.getWalletIds().should.deep.equal(['1', '2']); - }); -}); - -describe('#getName #setName', function() { - it('should get/set names', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setName(1, 'hola'); - s.getName(1).should.equal('hola'); - }); -}); - -describe('#getLastOpened #setLastOpened', function() { - it('should get/set names', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setLastOpened('hey'); - s.getLastOpened().should.equal('hey'); - }); -}); - -if (is_browser) { - describe('#getSessionId', function() { - it('should get SessionId', function() { + describe('#remove', function() { + it('should remove an item', function() { var s = new LocalEncrypted({ localStorage: localMock, + sessionStorage: sessionMock, password: 'password' }); - var sid = s.getSessionId(); - should.exist(sid); - var sid2 = s.getSessionId(); - sid2.should.equal(sid); + s.set('1', "hola", 'juan'); + s.get('1', 'hola').should.equal('juan'); + s.remove('1', 'hola'); + + should.not.exist(s.get('1', 'hola')); }); }); -} -describe('#getWallets', function() { - it('should retreive wallets from storage', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' + + describe('#getWalletIds', function() { + it('should get wallet ids', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.set('1', "hola", 'juan'); + s.set('2', "hola", 'juan'); + s.getWalletIds().should.deep.equal(['1', '2']); }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.setName(1, 'hola'); - s.getWallets()[0].should.deep.equal({ - id: '1', - name: 'hola', + }); + + describe('#getName #setName', function() { + it('should get/set names', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.setName(1, 'hola'); + s.getName(1).should.equal('hola'); }); - s.getWallets()[1].should.deep.equal({ - id: '2', - name: undefined + }); + + describe('#getLastOpened #setLastOpened', function() { + it('should get/set names', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.setLastOpened('hey'); + s.getLastOpened().should.equal('hey'); + }); + }); + + if (is_browser) { + describe('#getSessionId', function() { + it('should get SessionId', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + var sid = s.getSessionId(); + should.exist(sid); + var sid2 = s.getSessionId(); + sid2.should.equal(sid); + }); + }); + } + + describe('#getWallets', function() { + it('should retreive wallets from storage', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.set('1', "hola", 'juan'); + s.set('2', "hola", 'juan'); + s.setName(1, 'hola'); + s.getWallets()[0].should.deep.equal({ + id: '1', + name: 'hola', + }); + s.getWallets()[1].should.deep.equal({ + id: '2', + name: undefined + }); + }); + }); + describe('#deleteWallet', function() { + it('should delete a wallet', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.set('1', "hola", 'juan'); + s.set('2', "hola", 'juan'); + s.setName(1, 'hola'); + + s.deleteWallet('1'); + s.getWallets().length.should.equal(1); + s.getWallets()[0].should.deep.equal({ + id: '2', + name: undefined + }); + }); + }); + + describe('#setFromObj', function() { + it('set localstorage from an object', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.setFromObj('id1', { + 'key': 'val', + 'opts': { + 'name': 'nameid1' + }, + }); + + s.get('id1', 'key').should.equal('val'); + + }); + }); + + + describe('#globals', function() { + it('should set, get and remove keys', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.setGlobal('a', { + b: 1 + }); + JSON.parse(s.getGlobal('a')).should.deep.equal({ + b: 1 + }); + s.removeGlobal('a'); + should.not.exist(s.getGlobal('a')); + }); + }); + + + describe('session storage', function() { + it('should get a session ID', function() { + var s = new LocalEncrypted({ + localStorage: localMock, + sessionStorage: sessionMock, + password: 'password' + }); + s.getSessionId().length.should.equal(16); + (new Buffer(s.getSessionId(),'hex')).length.should.equal(8); }); }); }); -describe('#deleteWallet', function() { - it('should delete a wallet', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.setName(1, 'hola'); - - s.deleteWallet('1'); - s.getWallets().length.should.equal(1); - s.getWallets()[0].should.deep.equal({ - id: '2', - name: undefined - }); - }); -}); - -describe('#setFromObj', function() { - it('set localstorage from an object', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setFromObj('id1', { - 'key': 'val', - 'opts': { - 'name': 'nameid1' - }, - }); - - s.get('id1', 'key').should.equal('val'); - - }); -}); - - -describe('#globals', function() { - it('should set, get and remove keys', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - password: 'password' - }); - s.setGlobal('a', { - b: 1 - }); - JSON.parse(s.getGlobal('a')).should.deep.equal({ - b: 1 - }); - s.removeGlobal('a'); - should.not.exist(s.getGlobal('a')); - }); -}); diff --git a/test/test.WalletLock.js b/test/test.WalletLock.js index 54ae794e7..f3ccf051d 100644 --- a/test/test.WalletLock.js +++ b/test/test.WalletLock.js @@ -36,27 +36,32 @@ describe('WalletLock model', function() { should.exist(w); }); - it('should fail if locked already', function() { + it('should NOT fail if locked already', function() { var w = new WalletLock(storage, 'walletId'); storage.sessionId = 'xxx'; - (function() { - new WalletLock(storage, 'walletId') - }).should.throw('already open'); + var w2= new WalletLock(storage, 'walletId'); + should.exist(w2); }); + it('should change status of previously openned wallet', function() { + storage.sessionId = 'session1'; + var w = new WalletLock(storage, 'walletId'); + storage.sessionId = 'xxx'; + var w2= new WalletLock(storage, 'walletId'); + w2.keepAlive(); + (function() {w.keepAlive();}).should.throw('already open'); + + }); + + it('should not fail if locked by me', function() { var s = new Storage(); var w = new WalletLock(s, 'walletId'); var w2 = new WalletLock(s, 'walletId') + w2.keepAlive(); should.exist(w2); }); - it('should not fail if locked by me', function() { - var s = new Storage(); - var w = new WalletLock(s, 'walletId'); - var w2 = new WalletLock(s, 'walletId') - should.exist(w2); - }); it('should not fail if expired', function() { var s = new Storage(); var w = new WalletLock(s, 'walletId'); diff --git a/test/test.storage.LocalEncrypted.js b/test/test.storage.LocalEncrypted.js deleted file mode 100644 index 3d4e4d3e0..000000000 --- a/test/test.storage.LocalEncrypted.js +++ /dev/null @@ -1,284 +0,0 @@ -//Crypto Mock -CryptoJS = {}; -CryptoJS.AES = {}; -CryptoJS.AES.encrypt = function(a) { - return a; -}; - -CryptoJS.enc = { - utf8: '' -}; - -CryptoJS.AES.decrypt = function(a) { - return a; -}; - - - - -'use strict'; -var chai = chai || require('chai'); -var should = chai.should(); -var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined'; -if (is_browser) { - var copay = require('copay'); //browser -} else { - var copay = require('../copay'); //node -} -var LocalEncrypted = copay.StorageLocalEncrypted; - -var fakeWallet = 'fake-wallet-id'; -var timeStamp = Date.now(); -var localMock = require('./mocks/FakeLocalStorage'); -var sessionMock = require('./mocks/FakeLocalStorage'); - - -describe('Storage/LocalEncrypted model', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - }); - s._setPassphrase('mysupercoolpassword'); - - it('should create an instance', function() { - var s2 = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - }); - should.exist(s2); - }); - it('should fail when encrypting without a password', function() { - var s2 = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - }); - (function() { - s2.set(fakeWallet, timeStamp, 1); - }).should.throw(); - }); - it('should be able to encrypt and decrypt', function() { - s._write(fakeWallet + timeStamp, 'value'); - s._read(fakeWallet + timeStamp).should.equal('value'); - localMock.removeItem(fakeWallet + timeStamp); - }); - it('should be able to set a value', function() { - s.set(fakeWallet, timeStamp, 1); - localMock.removeItem(fakeWallet + '::' + timeStamp); - }); - var getSetData = [ - 1, 1000, -15, -1000, - 0.1, -0.5, -0.5e-10, Math.PI, - 'hi', 'auydoaiusyodaisudyoa', '0b5b8556a0c2ce828c9ccfa58b3dd0a1ae879b9b', - '1CjPR7Z5ZSyWk6WtXvSFgkptmpoi4UM9BC', 'OP_DUP OP_HASH160 80ad90d4035', [1, 2, 3, 4, 5, 6], { - x: 1, - y: 2 - }, { - x: 'hi', - y: null - }, { - a: {}, - b: [], - c: [1, 2, 'hi'] - }, - null - ]; - getSetData.forEach(function(obj) { - it('should be able to set a value and get it for ' + JSON.stringify(obj), function() { - s.set(fakeWallet, timeStamp, obj); - var obj2 = s.get(fakeWallet, timeStamp); - JSON.stringify(obj2).should.equal(JSON.stringify(obj)); - localMock.removeItem(fakeWallet + '::' + timeStamp); - }); - }); - - describe('#export', function() { - it('should export the encrypted wallet', function() { - var storage = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password', - }); - storage.set(fakeWallet, timeStamp, 'testval'); - var obj = { - test: 'testval' - }; - var encrypted = storage.export(obj); - encrypted.length.should.be.greaterThan(10); - localMock.removeItem(fakeWallet + '::' + timeStamp); - //encrypted.slice(0,6).should.equal("53616c"); - }); - }); - describe('#_decryptObj', function() { - it('should decrypt and Obj', function() { - var storage = new LocalEncrypted({ - password: 'password', - localStorage: localMock, - sessionStorage: sessionMock, - }); - storage._decryptObj('{"a":"2"}').should.deep.equal({ - a: "2" - }); - }); - }); - - - describe('#remove', function() { - it('should remove an item', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.get('1', 'hola').should.equal('juan'); - s.remove('1', 'hola'); - - should.not.exist(s.get('1', 'hola')); - }); - }); - - - describe('#getWalletIds', function() { - it('should get wallet ids', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.getWalletIds().should.deep.equal(['1', '2']); - }); - }); - - describe('#getName #setName', function() { - it('should get/set names', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.setName(1, 'hola'); - s.getName(1).should.equal('hola'); - }); - }); - - describe('#getLastOpened #setLastOpened', function() { - it('should get/set names', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.setLastOpened('hey'); - s.getLastOpened().should.equal('hey'); - }); - }); - - if (is_browser) { - describe('#getSessionId', function() { - it('should get SessionId', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - var sid = s.getSessionId(); - should.exist(sid); - var sid2 = s.getSessionId(); - sid2.should.equal(sid); - }); - }); - } - - describe('#getWallets', function() { - it('should retreive wallets from storage', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.setName(1, 'hola'); - s.getWallets()[0].should.deep.equal({ - id: '1', - name: 'hola', - }); - s.getWallets()[1].should.deep.equal({ - id: '2', - name: undefined - }); - }); - }); - describe('#deleteWallet', function() { - it('should delete a wallet', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.set('1', "hola", 'juan'); - s.set('2', "hola", 'juan'); - s.setName(1, 'hola'); - - s.deleteWallet('1'); - s.getWallets().length.should.equal(1); - s.getWallets()[0].should.deep.equal({ - id: '2', - name: undefined - }); - }); - }); - - describe('#setFromObj', function() { - it('set localstorage from an object', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.setFromObj('id1', { - 'key': 'val', - 'opts': { - 'name': 'nameid1' - }, - }); - - s.get('id1', 'key').should.equal('val'); - - }); - }); - - - describe('#globals', function() { - it('should set, get and remove keys', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.setGlobal('a', { - b: 1 - }); - JSON.parse(s.getGlobal('a')).should.deep.equal({ - b: 1 - }); - s.removeGlobal('a'); - should.not.exist(s.getGlobal('a')); - }); - }); - - - describe('session storage', function() { - it('should get a session ID', function() { - var s = new LocalEncrypted({ - localStorage: localMock, - sessionStorage: sessionMock, - password: 'password' - }); - s.getSessionId().length.should.equal(16); - (new Buffer(s.getSessionId(),'hex')).length.should.equal(8); - }); - }); -});