TST: decrypt storage

This commit is contained in:
Overtorment 2020-01-09 22:32:44 +00:00
parent c40eb99bec
commit 8f68ceeccd
5 changed files with 80 additions and 38 deletions

View File

@ -1,34 +0,0 @@
/* global jest */
export default class MockStorage {
constructor(cache = {}) {
this.storageCache = cache;
}
setItem = jest.fn((key, value) => {
return new Promise((resolve, reject) => {
return typeof key !== 'string' || typeof value !== 'string'
? reject(new Error('key and value must be string'))
: resolve((this.storageCache[key] = value));
});
});
getItem = jest.fn(key => {
return new Promise(resolve => {
return this.storageCache.hasOwnProperty(key) ? resolve(this.storageCache[key]) : resolve(null);
});
});
removeItem = jest.fn(key => {
return new Promise((resolve, reject) => {
return this.storageCache.hasOwnProperty(key) ? resolve(delete this.storageCache[key]) : reject(new Error('No such key!'));
});
});
clear = jest.fn(key => {
return new Promise((resolve, reject) => resolve((this.storageCache = {})));
});
getAllKeys = jest.fn(key => {
return new Promise((resolve, reject) => resolve(Object.keys(this.storageCache)));
});
}

View File

@ -65,7 +65,7 @@ export default class WatchConnectivity {
console.log('Wallets array is set. No Wallets set to sync with Watch app. Exiting...');
return;
}
console.log('Wallets set to sync with Watch app. Continuing...');
return InteractionManager.runAfterInteractions(async () => {
if (WatchConnectivity.shared.isAppInstalled) {
let wallets = [];

View File

@ -16,8 +16,7 @@ import AsyncStorage from '@react-native-community/async-storage';
import { AppStorage } from '../../class';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import Biometric from '../../class/biometrics';
/** @type {AppStorage} */
let BlueApp = require('../../BlueApp');
let BlueApp: AppStorage = require('../../BlueApp');
let prompt = require('../../prompt');
let loc = require('../../loc');

View File

@ -106,7 +106,7 @@ export default class WalletMigrate {
}
this.migrationComplete();
}
// 3: We're done!
migrationComplete() {
console.log('Migration was successful. Exiting migration...');

View File

@ -222,3 +222,80 @@ it('Appstorage - encryptStorage & load encrypted, then decryptStorage and load s
assert.strictEqual(Storage5.wallets[0].getLabel(), 'testlabel');
assert.strictEqual(Storage5.wallets[1].getLabel(), 'testlabel2');
});
it('can decrypt storage that is second in a list of buckets; and isPasswordInUse() works', async () => {
/** @type {AppStorage} */
let Storage = new AppStorage();
let w = new SegwitP2SHWallet();
w.setLabel('testlabel');
await w.generate();
Storage.wallets.push(w);
await Storage.saveToDisk();
let isEncrypted = await Storage.storageIsEncrypted();
assert.ok(!isEncrypted);
await Storage.encryptStorage('password');
isEncrypted = await Storage.storageIsEncrypted();
assert.strictEqual(Storage.cachedPassword, 'password');
assert.ok(isEncrypted);
// next, adding new `fake` storage which should be unlocked with `fake` password
let createFakeStorageResult = await Storage.createFakeStorage('fakePassword');
assert.ok(createFakeStorageResult);
assert.strictEqual(Storage.wallets.length, 0);
assert.strictEqual(Storage.cachedPassword, 'fakePassword');
w = new SegwitP2SHWallet();
w.setLabel('fakewallet');
await w.generate();
Storage.wallets.push(w);
await Storage.saveToDisk();
// now will decrypt storage. will try to decrypt FAKE storage (second in the list) while
// currently decrypted is the MAIN (non-fake) storage. this should throw an exception
const Storage4 = new AppStorage();
isEncrypted = await Storage4.storageIsEncrypted();
assert.ok(isEncrypted);
let loadResult = await Storage4.loadFromDisk('password');
assert.ok(loadResult);
let wasException = false;
try {
await Storage4.decryptStorage('fakePassword');
} catch (_) {
wasException = true;
}
assert.ok(wasException);
// now we will load fake storage, and we will decrypt it, which efficiently makes it main
// storage, purging other buckets. this should be possible since if user wants to shoot himsel in the foot
// he should be able to do it.
const Storage5 = new AppStorage();
isEncrypted = await Storage5.storageIsEncrypted();
assert.ok(isEncrypted);
loadResult = await Storage5.loadFromDisk('fakePassword');
assert.ok(loadResult);
// testing that isPasswordInUse() works:
assert.ok(await Storage5.isPasswordInUse('fakePassword'));
assert.ok(await Storage5.isPasswordInUse('password'));
assert.ok(!(await Storage5.isPasswordInUse('blablablabla')));
// now we will decrypt storage. label of wallet should be testlabel
const Storage6 = new AppStorage();
isEncrypted = await Storage6.storageIsEncrypted();
assert.ok(isEncrypted);
loadResult = await Storage6.loadFromDisk('fakePassword');
assert.ok(loadResult);
const decryptStorageResult = await Storage6.decryptStorage('fakePassword');
assert.ok(decryptStorageResult);
const Storage7 = new AppStorage();
isEncrypted = await Storage7.storageIsEncrypted();
assert.strictEqual(isEncrypted, false);
const storage5loadResult = await Storage7.loadFromDisk();
assert.ok(storage5loadResult);
assert.strictEqual(Storage7.wallets[0].getLabel(), 'fakewallet');
});