forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.test.js
326 lines (292 loc) · 11 KB
/
App.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* global describe, it, expect, jest, jasmine */
import React from 'react';
import { LegacyWallet, SegwitP2SHWallet, AppStorage } from './class';
import TestRenderer from 'react-test-renderer';
import Settings from './screen/settings/settings';
import Selftest from './screen/selftest';
import { BlueHeader } from './BlueComponents';
import MockStorage from './MockStorage';
import { FiatUnit } from './models/fiatUnit';
global.crypto = require('crypto'); // shall be used by tests under nodejs CLI, but not in RN environment
let assert = require('assert');
jest.mock('react-native-custom-qr-codes', () => 'Video');
const AsyncStorage = new MockStorage();
jest.setMock('AsyncStorage', AsyncStorage);
jest.useFakeTimers();
jest.mock('Picker', () => {
// eslint-disable-next-line import/no-unresolved
const React = require('React');
const PropTypes = require('prop-types');
return class MockPicker extends React.Component {
static Item = props => React.createElement('Item', props, props.children);
static propTypes = { children: PropTypes.any };
static defaultProps = { children: '' };
render() {
return React.createElement('Picker', this.props, this.props.children);
}
};
});
jest.mock('ScrollView', () => {
const RealComponent = require.requireActual('ScrollView');
const React = require('React');
class ScrollView extends React.Component {
scrollTo() {}
render() {
return React.createElement('ScrollView', this.props, this.props.children);
}
}
ScrollView.propTypes = RealComponent.propTypes;
return ScrollView;
});
jest.mock('react-native-google-analytics-bridge', () => ({
GoogleAnalyticsTracker: () => {
this.trackEvent = jest.fn();
return this;
},
}));
describe('unit - LegacyWallet', function() {
it('serialize and unserialize work correctly', () => {
let a = new LegacyWallet();
a.setLabel('my1');
let key = JSON.stringify(a);
let b = LegacyWallet.fromJson(key);
assert(key === JSON.stringify(b));
assert.equal(key, JSON.stringify(b));
});
it('can validate addresses', () => {
let w = new LegacyWallet();
assert.ok(w.isAddressValid('12eQ9m4sgAwTSQoNXkRABKhCXCsjm2jdVG'));
assert.ok(!w.isAddressValid('12eQ9m4sgAwTSQoNXkRABKhCXCsjm2j'));
assert.ok(w.isAddressValid('3BDsBDxDimYgNZzsqszNZobqQq3yeUoJf2'));
assert.ok(!w.isAddressValid('3BDsBDxDimYgNZzsqszNZobqQq3yeUo'));
assert.ok(!w.isAddressValid('12345'));
});
});
it('BlueHeader works', () => {
const rendered = TestRenderer.create(<BlueHeader />).toJSON();
expect(rendered).toBeTruthy();
});
it('Settings work', () => {
const rendered = TestRenderer.create(<Settings />).toJSON();
expect(rendered).toBeTruthy();
});
it('Selftest work', () => {
const component = TestRenderer.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('; '));
});
it('Appstorage - loadFromDisk works', async () => {
AsyncStorage.storageCache = {}; // cleanup from other tests
/** @type {AppStorage} */
let Storage = new AppStorage();
let w = new SegwitP2SHWallet();
w.setLabel('testlabel');
await w.generate();
Storage.wallets.push(w);
await Storage.saveToDisk();
// saved, now trying to load
let Storage2 = new AppStorage();
await Storage2.loadFromDisk();
assert.equal(Storage2.wallets.length, 1);
assert.equal(Storage2.wallets[0].getLabel(), 'testlabel');
let isEncrypted = await Storage2.storageIsEncrypted();
assert.ok(!isEncrypted);
// emulating encrypted storage (and testing flag)
AsyncStorage.storageCache.data = false;
AsyncStorage.storageCache.data_encrypted = '1'; // flag
let Storage3 = new AppStorage();
isEncrypted = await Storage3.storageIsEncrypted();
assert.ok(isEncrypted);
});
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');
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.equal(Storage.cachedPassword, 'password');
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');
await 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');
// 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');
await 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');
});
it('Wallet can fetch UTXO', async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
let w = new SegwitP2SHWallet();
w._address = '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX';
await w.fetchUtxo();
assert.ok(w.utxo.length > 0, 'unexpected empty UTXO');
});
it('Wallet can fetch balance', async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
let w = new LegacyWallet();
w._address = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'; // hack internals
assert.ok(w.getBalance() === 0);
assert.ok(w.getUnconfirmedBalance() === 0);
assert.ok(w._lastBalanceFetch === 0);
await w.fetchBalance();
assert.ok(w.getBalance() > 0);
assert.ok(w.getUnconfirmedBalance() === 0);
assert.ok(w._lastBalanceFetch > 0);
});
it('Wallet can fetch TXs', async () => {
let w = new LegacyWallet();
w._address = '12eQ9m4sgAwTSQoNXkRABKhCXCsjm2jdVG';
await w.fetchTransactions();
assert.equal(w.getTransactions().length, 2);
let tx0 = w.getTransactions()[0];
let txExpected = {
block_hash: '0000000000000000000d05c54a592db8532f134e12b4c3ae0821ce582fad3566',
block_height: 530933,
block_index: 1587,
hash: '4924f3a29acdee007ebcf6084d2c9e1752c4eb7f26f7d1a06ef808780bf5fe6d',
addresses: ['12eQ9m4sgAwTSQoNXkRABKhCXCsjm2jdVG', '3BDsBDxDimYgNZzsqszNZobqQq3yeUoJf2'],
total: 800,
fees: 200,
size: 190,
preference: 'low',
relayed_by: '18.197.135.148:8333',
confirmed: '2018-07-07T20:05:30Z',
received: '2018-07-07T20:02:01.637Z',
ver: 1,
double_spend: false,
vin_sz: 1,
vout_sz: 1,
confirmations: 593,
confidence: 1,
inputs: [
{
prev_hash: 'd0432027a86119c63a0be8fa453275c2333b59067f1e559389cd3e0e377c8b96',
output_index: 1,
script:
'483045022100e443784abe25b6d39e01c95900834bf4eeaa82505ac0eb84c08e11c287d467de02203327c2b1136f4976f755ed7631b427d66db2278414e7faf1268eedf44c034e0c012103c69b905f7242b3688122f06951339a1ee00da652f6ecc6527ea6632146cace62',
output_value: 1000,
sequence: 4294967295,
addresses: ['12eQ9m4sgAwTSQoNXkRABKhCXCsjm2jdVG'],
script_type: 'pay-to-pubkey-hash',
age: 530926,
},
],
outputs: [
{
value: 800,
script: 'a914688eb9af71aab8ca221f4e6171a45fc46ea8743b87',
spent_by: '009c6219deeac341833642193e4a3b72e511105a61b48e375c5025b1bcbd6fb5',
addresses: ['3BDsBDxDimYgNZzsqszNZobqQq3yeUoJf2'],
script_type: 'pay-to-script-hash',
},
],
value: -1000,
};
delete tx0.confirmations;
delete txExpected.confirmations;
assert.deepEqual(tx0, txExpected);
});
describe('currency', () => {
it('fetches exchange rate and saves to AsyncStorage', async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
AsyncStorage.storageCache = {}; // cleanup from other tests
let currency = require('./currency');
await currency.startUpdater();
let cur = AsyncStorage.storageCache[AppStorage.EXCHANGE_RATES];
cur = JSON.parse(cur);
assert.ok(Number.isInteger(cur[currency.STRUCT.LAST_UPDATED]));
assert.ok(cur[currency.STRUCT.LAST_UPDATED] > 0);
assert.ok(cur['BTC_USD'] > 0);
// now, setting other currency as default
AsyncStorage.storageCache[AppStorage.PREFERRED_CURRENCY] = JSON.stringify(FiatUnit.JPY);
await currency.startUpdater();
cur = JSON.parse(AsyncStorage.storageCache[AppStorage.EXCHANGE_RATES]);
assert.ok(cur['BTC_JPY'] > 0);
// now setting with a proper setter
await currency.setPrefferedCurrency(FiatUnit.EUR);
await currency.startUpdater();
let preferred = await currency.getPreferredCurrency();
assert.equal(preferred.endPointKey, 'EUR');
cur = JSON.parse(AsyncStorage.storageCache[AppStorage.EXCHANGE_RATES]);
assert.ok(cur['BTC_EUR'] > 0);
});
});