burn in hell fake storage

This commit is contained in:
Matias Alejo Garcia 2014-09-17 09:42:23 -03:00
commit 8bc1eb15e4
11 changed files with 34 additions and 185 deletions

View file

@ -1,162 +0,0 @@
var FakeStorage = function() {
this.reset();
};
FakeStorage.prototype.reset = function(password) {
this.storage = {};
};
FakeStorage.prototype.setPassphrase = function(password) {
this.storage.passphrase = password;
};
FakeStorage.prototype.setGlobal = function(id, v, cb) {
this.storage[id] = typeof v === 'object' ? JSON.stringify(v) : v;
cb();
};
FakeStorage.prototype.getGlobal = function(id, cb) {
return cb(this.storage[id]);
};
FakeStorage.prototype.getMany = function(wid, fields, cb) {
var self = this;
var ret = [];
for (var ii in fields) {
var k = fields[ii];
ret[k] = this.storage[wid + '::' + k];
}
return cb(ret);
};
FakeStorage.prototype.setLastOpened = function(val, cb) {
this.storage['lastOpened'] = val;
return cb();
};
FakeStorage.prototype.getLastOpened = function(cb) {
return cb(this.storage['lastOpened']);
};
FakeStorage.prototype.setLock = function(id) {
this.storage[id + '::lock'] = true;
return cb();
}
FakeStorage.prototype.getLock = function(id, cb) {
return cb(this.storage[id + '::lock']);
}
FakeStorage.prototype.getSessionId = function(cb) {
this.sessionId = this.sessionId || 'aSessionId';
return cb(this.sessionId);
};
FakeStorage.prototype.removeLock = function(id, cb) {
delete this.storage[id + '::lock'];
cb();
}
FakeStorage.prototype.removeGlobal = function(id, cb) {
delete this.storage[id];
cb();
};
FakeStorage.prototype.set = function(wid, id, payload, cb) {
this.storage[wid + '::' + id] = payload;
if (cb) return cb();
};
FakeStorage.prototype.get = function(wid, id, cb) {
return cb(this.storage[wid + '::' + id]);
};
FakeStorage.prototype.clear = function(cb) {
delete this['storage'];
cb();
};
FakeStorage.prototype.getWalletIds = function(cb) {
var walletIds = [];
var uniq = {};
for (var ii in this.storage) {
var split = ii.split('::');
if (split.length == 2) {
var walletId = split[0];
if (!walletId || walletId === 'nameFor' || walletId === 'lock')
continue;
if (typeof uniq[walletId] === 'undefined') {
walletIds.push(walletId);
uniq[walletId] = 1;
}
}
}
return cb(walletIds);
};
FakeStorage.prototype.deleteWallet = function(walletId, cb) {
var toDelete = {};
toDelete['nameFor::' + walletId] = 1;
for (var key in this.storage) {
var split = key.split('::');
if (split.length == 2 && split[0] === walletId) {
toDelete[key] = 1;
}
}
var l = toDelete.length,
j = 0;
if (!l)
return cb(new Error('WNOTFOUND: Wallet not found'));
for (var i in toDelete) {
this.removeGlobal(i, cb);
if (++j == l)
return cb(err);
}
};
FakeStorage.prototype.getName = function(walletId, cb) {
this.getGlobal('nameFor::' + walletId, cb);
};
FakeStorage.prototype.setName = function(walletId, name, cb) {
this.setGlobal('nameFor::' + walletId, name, cb);
};
FakeStorage.prototype.getWallets = function(cb) {
var wallets = [];
var self= this;
this.getWalletIds(function(ids) {
for (var i in ids) {
wallets.push({
id: ids[i],
name: self.storage['nameFor::' + ids[i]],
});
}
return cb(wallets);
});
};
FakeStorage.prototype.setFromObj = function(walletId, obj, cb) {
for (var k in obj) {
this.set(walletId, k, obj[k]);
}
this.setName(walletId, obj.opts.name, cb);
};
module.exports = FakeStorage;

View file

@ -11,7 +11,6 @@ if (is_browser) {
}
var Wallet = copay.Wallet;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
var Network = require('./mocks/FakeNetwork');
var Blockchain = require('./mocks/FakeBlockchain');
var bitcore = bitcore || require('bitcore');
@ -21,6 +20,10 @@ var Address = bitcore.Address;
var PayPro = bitcore.PayPro;
var bignum = bitcore.Bignum;
var startServer = copay.FakePayProServer; // TODO should be require('./mocks/FakePayProServer');
var localMock = require('./mocks/FakeLocalStorage');
var sessionMock = require('./mocks/FakeLocalStorage');
var Storage = copay.Storage;
var server;
@ -30,6 +33,10 @@ var walletConfig = {
spendUnconfirmed: true,
reconnectDelay: 100,
networkName: 'testnet',
storage: {
storage: localMock,
sessionStorage: sessionMock,
}
};
var getNewEpk = function() {

View file

@ -25,8 +25,7 @@ describe('Storage model', function() {
});
should.exist(s2);
});
it.only('should fail when encrypting without a password', function() {
it('should fail when encrypting without a password', function() {
var s2 = new Storage({
storage: localMock,
sessionStorage: sessionMock,

View file

@ -13,7 +13,7 @@ if (is_browser) {
var copayConfig = require('../config');
var Wallet = copay.Wallet;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
var Storage = copay.Storage;
var Network = require('./mocks/FakeNetwork');
var Blockchain = require('./mocks/FakeBlockchain');
var Builder = require('./mocks/FakeBuilder');
@ -21,6 +21,8 @@ var bitcore = bitcore || require('bitcore');
var TransactionBuilder = bitcore.TransactionBuilder;
var Transaction = bitcore.Transaction;
var Address = bitcore.Address;
var localMock = require('./mocks/FakeLocalStorage');
var sessionMock = require('./mocks/FakeLocalStorage');
var walletConfig = {
requiredCopayers: 3,
@ -28,6 +30,10 @@ var walletConfig = {
spendUnconfirmed: true,
reconnectDelay: 100,
networkName: 'testnet',
storage: {
storage: localMock,
sessionStorage: sessionMock,
}
};
var getNewEpk = function() {

View file

@ -13,12 +13,19 @@ var copayConfig = require('../config');
var WalletLock = copay.WalletLock;
var PrivateKey = copay.PrivateKey;
var Storage = require('./mocks/FakeStorage');
var localMock = require('./mocks/FakeLocalStorage');
var sessionMock = require('./mocks/FakeLocalStorage');
var Storage = copay.Storage;
var storage;
describe('WalletLock model', function() {
beforeEach(function() {
storage = new Storage();
storage = new Storage({
storage: localMock,
sessionStorage: sessionMock,
});
});
it('should fail with missing args', function() {