-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbackupVault.test.ts
More file actions
293 lines (234 loc) · 8.34 KB
/
backupVault.test.ts
File metadata and controls
293 lines (234 loc) · 8.34 KB
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
import {
VAULT_FAILED_TO_GET_VAULT_FROM_BACKUP,
VAULT_BACKUP_KEY,
VAULT_BACKUP_TEMP_KEY,
} from './constants';
import {
backupVault,
getVaultFromBackup,
clearAllVaultBackups,
} from './backupVault';
import { KeyringControllerState } from '@metamask/keyring-controller';
import {
getInternetCredentials,
resetInternetCredentials,
setInternetCredentials,
type Result,
STORAGE_TYPE,
} from 'react-native-keychain';
let mockKeychainState: Record<string, { username: string; password: string }> =
{};
const mockStorageType = STORAGE_TYPE.AES_GCM;
// Mock the react-native-keychain module
jest.mock('react-native-keychain', () => ({
...jest.requireActual('react-native-keychain'),
STORAGE_TYPE: {
FB: 'FacebookConceal',
AES: 'KeystoreAES',
AES_CBC: 'KeystoreAESCBC',
AES_GCM_NO_AUTH: 'KeystoreAESGCM_NoAuth',
AES_GCM: 'KeystoreAESGCM',
RSA: 'KeystoreRSAECB',
},
setInternetCredentials: jest.fn(
async (
server: string,
username: string,
password: string,
_?,
): Promise<Result> => {
mockKeychainState[server] = { username, password };
return {
service: 'service',
storage: mockStorageType,
};
},
),
getInternetCredentials: jest.fn(
async (server: string) => mockKeychainState[server],
),
resetInternetCredentials: jest.fn(async (server: string) => {
delete mockKeychainState[server];
}),
}));
//TODO Mock the react-native-keychain module test the other functions inside backupVault
/*
These tests are extremely limited since we are unable to mock the react-native-keychain module
Despite the fact that they are mocked in the jest setup file, they do not appear to be working.
Therefore the best we can do for now is to test the error case that does not hit the keychain.
Documentation for the testing react-native-keychain can be found here: https://github.com/oblador/react-native-keychain#unit-testing-with-jest
More information on the issue can be found here: https://github.com/oblador/react-native-keychain/issues/460
*/
describe('backupVault file', () => {
const dummyPassword = 'dummy-password';
beforeEach(() => {
jest.clearAllMocks();
mockKeychainState = {};
});
describe('clearAllVaultBackups', () => {
it('should clear all vault backups', async () => {
await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
dummyPassword,
);
await setInternetCredentials(
VAULT_BACKUP_TEMP_KEY,
VAULT_BACKUP_TEMP_KEY,
dummyPassword,
);
const primaryVaultCredentials =
await getInternetCredentials(VAULT_BACKUP_KEY);
const temporaryVaultCredentials = await getInternetCredentials(
VAULT_BACKUP_TEMP_KEY,
);
expect(primaryVaultCredentials).toEqual({
username: VAULT_BACKUP_KEY,
password: dummyPassword,
});
expect(temporaryVaultCredentials).toEqual({
username: VAULT_BACKUP_TEMP_KEY,
password: dummyPassword,
});
await clearAllVaultBackups();
const primaryVaultCredentialsAfterReset =
await getInternetCredentials(VAULT_BACKUP_KEY);
const temporaryVaultCredentialsAfterReset = await getInternetCredentials(
VAULT_BACKUP_TEMP_KEY,
);
expect(primaryVaultCredentialsAfterReset).toBeUndefined();
expect(temporaryVaultCredentialsAfterReset).toBeUndefined();
});
});
describe('backupVault', () => {
it('should throw error and skip primary backup if failed to backup temporary vault', async () => {
const mockedFailedResponse = {
error: 'Failed to backup temporary vault',
success: false,
};
// Populate primary vault backup
await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
dummyPassword,
);
// Mock the setInternetCredentials function to return false, which simulates a failed vault backup
(setInternetCredentials as jest.Mock).mockImplementationOnce(() => false);
const keyringState: KeyringControllerState = {
vault: undefined,
keyrings: [],
isUnlocked: false,
};
const response = await backupVault(keyringState);
expect(response).toEqual(mockedFailedResponse);
});
it('should throw error when primary vault backup fails', async () => {
const mockedFailedResponse = {
error: 'Vault backup failed',
success: false,
};
// Mock the setInternetCredentials function to return false, which simulates a failed vault backup
(setInternetCredentials as jest.Mock).mockImplementationOnce(() => false);
const keyringState: KeyringControllerState = {
vault: undefined,
keyrings: [],
isUnlocked: false,
};
const response = await backupVault(keyringState);
expect(response).toEqual(mockedFailedResponse);
});
it('should successfully backup primary vault', async () => {
const mockedSuccessResponse = { success: true };
// Populate primary vault backup
await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
dummyPassword,
);
const keyringState: KeyringControllerState = {
vault: undefined,
keyrings: [],
isUnlocked: false,
};
const response = await backupVault(keyringState);
expect(response).toEqual(mockedSuccessResponse);
});
it('should still succeed if reading the existing backup throws (e.g. Android Keystore key invalidation)', async () => {
const newVault = 'new-vault';
// Simulate a stale entry already in the keychain
await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
dummyPassword,
);
// Simulate Android Keystore throwing on the read
(getInternetCredentials as jest.Mock).mockImplementationOnce(() => {
throw new Error('Android Keystore key permanently invalidated');
});
const keyringState: KeyringControllerState = {
vault: newVault,
keyrings: [],
isUnlocked: false,
};
const response = await backupVault(keyringState);
expect(response).toEqual({ success: true, vault: newVault });
});
it('should reset vault before backup', async () => {
const mockedSuccessResponse = { success: true };
await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
dummyPassword,
);
const internetCredentialsBeforeReset =
await getInternetCredentials(VAULT_BACKUP_KEY);
expect(internetCredentialsBeforeReset).toEqual({
username: VAULT_BACKUP_KEY,
password: dummyPassword,
});
const keyringState: KeyringControllerState = {
vault: undefined,
keyrings: [],
isUnlocked: false,
};
const response = await backupVault(keyringState);
// First reset temporary, then primary, then temporary again
expect(resetInternetCredentials).toHaveBeenCalledTimes(3);
expect(response).toEqual(mockedSuccessResponse);
});
});
describe('getVaultFromBackup', () => {
it('should successfully get primary vault from backup', async () => {
const mockedSuccessResponse = { success: true, vault: dummyPassword };
await setInternetCredentials(
VAULT_BACKUP_KEY,
VAULT_BACKUP_KEY,
dummyPassword,
);
const response = await getVaultFromBackup();
expect(response).toEqual(mockedSuccessResponse);
});
it('should successfully get temporary vault from backup if primary vault does not exist', async () => {
const tempDummyPassword = 'temp-dummy-password';
const mockedSuccessResponse = { success: true, vault: tempDummyPassword };
await setInternetCredentials(
VAULT_BACKUP_TEMP_KEY,
VAULT_BACKUP_TEMP_KEY,
tempDummyPassword,
);
const response = await getVaultFromBackup();
expect(response).toEqual(mockedSuccessResponse);
});
it('should return error when vault backup fails', async () => {
const mockedFailedResponse = {
error: VAULT_FAILED_TO_GET_VAULT_FROM_BACKUP,
success: false,
};
(getInternetCredentials as jest.Mock).mockImplementationOnce(
() => undefined,
);
const response = await getVaultFromBackup();
expect(response).toEqual(mockedFailedResponse);
});
});
});