2018-03-18 16:15:10 +01:00
|
|
|
/* global describe, it, expect, jest */
|
|
|
|
import React from 'react';
|
2018-03-30 20:31:10 +02:00
|
|
|
import { LegacyWallet, SegwitP2SHWallet, AppStorage } from './class';
|
2018-03-18 16:15:10 +01:00
|
|
|
import renderer from 'react-test-renderer';
|
|
|
|
import App from './App';
|
|
|
|
import Settings from './screen/settings';
|
2018-03-24 21:12:59 +01:00
|
|
|
import Selftest from './screen/selftest';
|
2018-03-18 16:15:10 +01:00
|
|
|
import { BlueHeader } from './BlueComponents';
|
2018-03-30 20:31:10 +02:00
|
|
|
import MockStorage from './MockStorage';
|
2018-03-17 21:39:21 +01:00
|
|
|
let assert = require('assert');
|
2018-03-18 16:15:10 +01:00
|
|
|
jest.mock('react-native-qrcode', () => 'Video');
|
2018-03-30 20:31:10 +02:00
|
|
|
const AsyncStorage = new MockStorage();
|
|
|
|
jest.setMock('AsyncStorage', AsyncStorage);
|
2018-03-17 15:27:37 +01:00
|
|
|
|
2018-03-17 21:39:21 +01:00
|
|
|
describe('unit - LegacyWallet', function() {
|
2018-03-17 15:27:37 +01:00
|
|
|
it('serialize and unserialize work correctly', () => {
|
2018-03-17 21:39:21 +01:00
|
|
|
let a = new LegacyWallet();
|
|
|
|
a.setLabel('my1');
|
|
|
|
let key = JSON.stringify(a);
|
2018-01-30 23:42:38 +01:00
|
|
|
|
2018-03-17 21:39:21 +01:00
|
|
|
let b = LegacyWallet.fromJson(key);
|
|
|
|
assert(key === JSON.stringify(b));
|
2018-01-30 23:42:38 +01:00
|
|
|
|
2018-03-17 21:39:21 +01:00
|
|
|
assert.equal(key, JSON.stringify(b));
|
|
|
|
});
|
|
|
|
});
|
2018-03-18 16:15:10 +01:00
|
|
|
|
|
|
|
it('App does not crash', () => {
|
|
|
|
const rendered = renderer.create(<App />).toJSON();
|
|
|
|
expect(rendered).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('BlueHeader works', () => {
|
|
|
|
const rendered = renderer.create(<BlueHeader />).toJSON();
|
|
|
|
expect(rendered).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Settings work', () => {
|
|
|
|
const rendered = renderer.create(<Settings />).toJSON();
|
|
|
|
expect(rendered).toBeTruthy();
|
|
|
|
});
|
2018-03-24 21:12:59 +01:00
|
|
|
|
|
|
|
it('Selftest work', () => {
|
|
|
|
const component = renderer.create(<Selftest />);
|
|
|
|
const root = component.root;
|
|
|
|
const rendered = component.toJSON();
|
|
|
|
expect(rendered).toBeTruthy();
|
|
|
|
// console.log((root.findAllByType('Text')[0].props));
|
|
|
|
|
|
|
|
let okFound = false;
|
|
|
|
let allTests = [];
|
|
|
|
for (var v of root.findAllByType('Text')) {
|
|
|
|
let text = v.props.children;
|
|
|
|
if (text.join) {
|
|
|
|
text = text.join('');
|
|
|
|
}
|
|
|
|
if (text === 'OK') {
|
|
|
|
okFound = true;
|
|
|
|
}
|
|
|
|
allTests.push(text);
|
|
|
|
// console.log(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ok(okFound, 'OK not found. Got: ' + allTests.join('; '));
|
|
|
|
});
|
2018-03-30 20:31:10 +02:00
|
|
|
|
2018-03-31 02:03:58 +02:00
|
|
|
it('Appstorage - loadFromDisk works', async () => {
|
|
|
|
AsyncStorage.storageCache = {}; // cleanup from other tests
|
2018-03-30 20:31:10 +02:00
|
|
|
/** @type {AppStorage} */
|
|
|
|
let Storage = new AppStorage();
|
|
|
|
let w = new SegwitP2SHWallet();
|
|
|
|
w.setLabel('testlabel');
|
|
|
|
w.generate();
|
|
|
|
Storage.wallets.push(w);
|
2018-03-31 02:03:58 +02:00
|
|
|
await Storage.saveToDisk();
|
|
|
|
|
|
|
|
// saved, now trying to load
|
2018-03-30 20:31:10 +02:00
|
|
|
|
|
|
|
let Storage2 = new AppStorage();
|
|
|
|
await Storage2.loadFromDisk();
|
|
|
|
assert.equal(Storage2.wallets.length, 1);
|
|
|
|
assert.equal(Storage2.wallets[0].getLabel(), 'testlabel');
|
2018-03-31 02:03:58 +02:00
|
|
|
let isEncrypted = await Storage2.storageIsEncrypted();
|
|
|
|
assert.ok(!isEncrypted);
|
2018-03-30 20:31:10 +02:00
|
|
|
|
2018-03-31 02:03:58 +02:00
|
|
|
// emulating encrypted storage (and testing flag)
|
2018-03-30 20:31:10 +02:00
|
|
|
|
|
|
|
AsyncStorage.storageCache.data = false;
|
2018-03-31 02:03:58 +02:00
|
|
|
AsyncStorage.storageCache.data_encrypted = '1'; // flag
|
2018-03-30 20:31:10 +02:00
|
|
|
let Storage3 = new AppStorage();
|
|
|
|
isEncrypted = await Storage3.storageIsEncrypted();
|
|
|
|
assert.ok(isEncrypted);
|
|
|
|
});
|
2018-03-31 02:03:58 +02:00
|
|
|
|
|
|
|
it('Appstorage - encryptStorage & load encrypted storage works', async () => {
|
|
|
|
AsyncStorage.storageCache = {}; // cleanup from other tests
|
|
|
|
|
|
|
|
/** @type {AppStorage} */
|
|
|
|
let Storage = new AppStorage();
|
|
|
|
let w = new SegwitP2SHWallet();
|
|
|
|
w.setLabel('testlabel');
|
|
|
|
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();
|
2018-04-01 01:16:42 +02:00
|
|
|
assert.equal(Storage.cachedPassword, 'password');
|
2018-03-31 02:03:58 +02:00
|
|
|
assert.ok(isEncrypted);
|
|
|
|
|
|
|
|
// saved, now trying to load, using good password
|
|
|
|
|
|
|
|
let Storage2 = new AppStorage();
|
|
|
|
isEncrypted = await Storage2.storageIsEncrypted();
|
|
|
|
assert.ok(isEncrypted);
|
|
|
|
let loadResult = await Storage2.loadFromDisk('password');
|
|
|
|
assert.ok(loadResult);
|
|
|
|
assert.equal(Storage2.wallets.length, 1);
|
|
|
|
assert.equal(Storage2.wallets[0].getLabel(), 'testlabel');
|
|
|
|
|
|
|
|
// now trying to load, using bad password
|
|
|
|
|
|
|
|
Storage2 = new AppStorage();
|
|
|
|
isEncrypted = await Storage2.storageIsEncrypted();
|
|
|
|
assert.ok(isEncrypted);
|
|
|
|
loadResult = await Storage2.loadFromDisk('passwordBAD');
|
|
|
|
assert.ok(!loadResult);
|
|
|
|
assert.equal(Storage2.wallets.length, 0);
|
|
|
|
|
|
|
|
// now, trying case with adding data after decrypt.
|
|
|
|
// saveToDisk should be handled correctly
|
|
|
|
|
|
|
|
Storage2 = new AppStorage();
|
|
|
|
isEncrypted = await Storage2.storageIsEncrypted();
|
|
|
|
assert.ok(isEncrypted);
|
|
|
|
loadResult = await Storage2.loadFromDisk('password');
|
|
|
|
assert.ok(loadResult);
|
|
|
|
assert.equal(Storage2.wallets.length, 1);
|
|
|
|
assert.equal(Storage2.wallets[0].getLabel(), 'testlabel');
|
|
|
|
w = new SegwitP2SHWallet();
|
|
|
|
w.setLabel('testlabel2');
|
|
|
|
w.generate();
|
|
|
|
Storage2.wallets.push(w);
|
|
|
|
assert.equal(Storage2.wallets.length, 2);
|
|
|
|
assert.equal(Storage2.wallets[1].getLabel(), 'testlabel2');
|
|
|
|
await Storage2.saveToDisk();
|
|
|
|
// saved to encrypted storage after load. next load should be successfull
|
|
|
|
Storage2 = new AppStorage();
|
|
|
|
isEncrypted = await Storage2.storageIsEncrypted();
|
|
|
|
assert.ok(isEncrypted);
|
|
|
|
loadResult = await Storage2.loadFromDisk('password');
|
|
|
|
assert.ok(loadResult);
|
|
|
|
assert.equal(Storage2.wallets.length, 2);
|
|
|
|
assert.equal(Storage2.wallets[0].getLabel(), 'testlabel');
|
|
|
|
assert.equal(Storage2.wallets[1].getLabel(), 'testlabel2');
|
2018-04-01 01:16:42 +02:00
|
|
|
|
|
|
|
// next, adding new `fake` storage which should be unlocked with `fake` password
|
|
|
|
let createFakeStorageResult = await Storage2.createFakeStorage(
|
|
|
|
'fakePassword',
|
|
|
|
);
|
|
|
|
assert.ok(createFakeStorageResult);
|
|
|
|
assert.equal(Storage2.wallets.length, 0);
|
|
|
|
assert.equal(Storage2.cachedPassword, 'fakePassword');
|
|
|
|
w = new SegwitP2SHWallet();
|
|
|
|
w.setLabel('fakewallet');
|
|
|
|
w.generate();
|
|
|
|
Storage2.wallets.push(w);
|
|
|
|
await Storage2.saveToDisk();
|
|
|
|
// now, will try to load & decrypt with real password and with fake password
|
|
|
|
// real:
|
|
|
|
let Storage3 = new AppStorage();
|
|
|
|
loadResult = await Storage3.loadFromDisk('password');
|
|
|
|
assert.ok(loadResult);
|
|
|
|
assert.equal(Storage3.wallets.length, 2);
|
|
|
|
assert.equal(Storage3.wallets[0].getLabel(), 'testlabel');
|
|
|
|
// fake:
|
|
|
|
Storage3 = new AppStorage();
|
|
|
|
loadResult = await Storage3.loadFromDisk('fakePassword');
|
|
|
|
assert.ok(loadResult);
|
|
|
|
assert.equal(Storage3.wallets.length, 1);
|
|
|
|
assert.equal(Storage3.wallets[0].getLabel(), 'fakewallet');
|
2018-03-31 02:03:58 +02:00
|
|
|
});
|