From 8f68ceeccdd59cbb6a7c41c3575585f9cc39c5b3 Mon Sep 17 00:00:00 2001 From: Overtorment Date: Thu, 9 Jan 2020 22:32:44 +0000 Subject: [PATCH] TST: decrypt storage --- MockStorage.js | 34 -------------- WatchConnectivity.ios.js | 2 +- screen/settings/encryptStorage.js | 3 +- screen/wallets/walletMigrate.js | 2 +- tests/integration/Storage.test.js | 77 +++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 38 deletions(-) delete mode 100644 MockStorage.js diff --git a/MockStorage.js b/MockStorage.js deleted file mode 100644 index cc461bf5..00000000 --- a/MockStorage.js +++ /dev/null @@ -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))); - }); -} diff --git a/WatchConnectivity.ios.js b/WatchConnectivity.ios.js index 8b35efda..c4e0a048 100644 --- a/WatchConnectivity.ios.js +++ b/WatchConnectivity.ios.js @@ -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 = []; diff --git a/screen/settings/encryptStorage.js b/screen/settings/encryptStorage.js index fd890880..f19fbf52 100644 --- a/screen/settings/encryptStorage.js +++ b/screen/settings/encryptStorage.js @@ -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'); diff --git a/screen/wallets/walletMigrate.js b/screen/wallets/walletMigrate.js index 23aa1b2f..cf946f70 100644 --- a/screen/wallets/walletMigrate.js +++ b/screen/wallets/walletMigrate.js @@ -106,7 +106,7 @@ export default class WalletMigrate { } this.migrationComplete(); } - + // 3: We're done! migrationComplete() { console.log('Migration was successful. Exiting migration...'); diff --git a/tests/integration/Storage.test.js b/tests/integration/Storage.test.js index 67e41a86..b94fbd99 100644 --- a/tests/integration/Storage.test.js +++ b/tests/integration/Storage.test.js @@ -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'); +});