xref: /expo/apps/test-suite/tests/SecureStore.js (revision 8f4be4e2)
1'use strict';
2
3import * as SecureStore from 'expo-secure-store';
4import { Platform } from 'react-native';
5
6export const name = 'SecureStore';
7
8export function test(t) {
9  const value = 'value-to-test';
10  const longValue =
11    'EAAT3TDAdWacBAMjZAq4clOJEOvf8JW5ZAxAsRnZCGRBNb1IRVFrzwiNqsM2I5MyogyPCc78TL1KZAFZAnZAFHeZCjkN8VMytKpcKD4HQEtVZBoAS54WkGbA2STjOe1vV3XOx3BY9OsDpDcD4yTZAv1OcI1wNlVvryiujZBeheVnELK6KTyzUgrPM8zZA42ZAB6SEcZADoj4MNsX5DqrJ3FtG0cxjFCD0lhKfBmTQMrZBCmuefRrQZDZDEAAT3TDAdWacBAMjZAq4clOJEOvf8JW5ZAxAsRnZCGRBNb1IRVFrzwiNqsM2I5MyogyPCc78TL1KZAFZAnZAFHeZCjkN8VMytKpcKD4HQEtVZBoAS54WkGbA2STjOe1vV3XOx3BY9OsDpDcD4yTZAv1OcI1wNlVvryiujZBeheVnELK6KTyzUgrPM8zZA42ZAB6SEcZADoj4MNsX5DqrJ3FtG0cxjFCD0lhKfBmTQMrZBCmuefRrQZDZDEAAT3TDAdWacBAMjZAq4clOJEOvf8JW5ZAxAsRnZCGRBNb1IRVFrzwiNqsM2I5MyogyPCc78TL1KZAFZAnZAFHeZCjkN8VMytKpcKD4HQEtVZBoAS54WkGbA2STjOe1vV3XOx3BY9OsDpDcD4yTZAv1OcI1wNlVvryiujZBeheVnELK6KTyzUgrPM8zZA42ZAB6SEcZADoj4MNsX5DqrJ3FtG0cxjFCD0lhKfBmTQMrZBCmuefRrQZDZDEAAT3TDAdWacBAMjZAq4clOJEOvf8JW5ZAxAsRnZCGRBNb1IRVFrzwiNqsM2I5MyogyPCc78TL1KZAFZAnZAFHeZCjkN8VMytKpcKD4HQEtVZBoAS54WkGbA2STjOe1vV3XOx3BY9OsDpDcD4yTZAv1OcI1wNlVvryiujZBeheVnELK6KTyzUgrPM8zZA42ZAB6SEcZADoj4MNsX5DqrJ3FtG0cxjFCD0lhKfBmTQMrZBCmuefRrQZDZD';
12  const key = 'key-to-test';
13  const emptyKey = null;
14  const emptyValue = null;
15  const optionsServiceA = { keychainService: 'service-A' };
16  const optionsServiceB = { keychainService: 'service-B' };
17  t.describe('SecureStore: store -> fetch -> delete -> fetch -> err:', () => {
18    t.it('Sets a value with a key', async () => {
19      try {
20        const result = await SecureStore.setItemAsync(key, value, {});
21        t.expect(result).toBe(undefined);
22      } catch (e) {
23        t.fail(e);
24      }
25    });
26    t.it('Fetch the value stored with the key', async () => {
27      try {
28        const fetchedValue = await SecureStore.getItemAsync(key, {});
29        t.expect(fetchedValue).toBe(value);
30      } catch (e) {
31        t.fail(e);
32      }
33    });
34    t.it('Delete the value associated with the key', async () => {
35      try {
36        const result = await SecureStore.deleteItemAsync(key, {});
37        t.expect(result).toBe(undefined);
38      } catch (e) {
39        t.fail(e);
40      }
41    });
42    t.it('Fetch the previously deleted key, expect null', async () => {
43      const fetchedValue = await SecureStore.getItemAsync(key, {});
44      t.expect(fetchedValue).toBe(null);
45    });
46  });
47  t.describe('SecureStore: store -> fetch -> delete -> fetch -> err with Options:', () => {
48    t.it('Sets a value with a key and keychainService', async () => {
49      const result = await SecureStore.setItemAsync(key, value, {
50        keychainService: 'service',
51      });
52      t.expect(result).toBe(undefined);
53    });
54    t.it('Fetch the value stored with the key and keychainService', async () => {
55      const fetchedValue = await SecureStore.getItemAsync(key, {
56        keychainService: 'service',
57      });
58      t.expect(fetchedValue).toBe(value);
59    });
60    t.it('Delete the value associated with the key', async () => {
61      const result = await SecureStore.deleteItemAsync(key, {
62        keychainService: 'service',
63      });
64      t.expect(result).toBe(undefined);
65    });
66    t.it('Fetch the previously deleted key, expect null', async () => {
67      const fetchedValue = await SecureStore.getItemAsync(key, {
68        keychainService: 'service',
69      });
70      t.expect(fetchedValue).toBe(null);
71    });
72  });
73  t.describe('SecureStore: store with empty key -> err:', () => {
74    t.it('Sets a value with an empty key, expect error', async () => {
75      try {
76        const result = await SecureStore.setItemAsync(emptyKey, value, {});
77        t.fail(result);
78      } catch (e) {
79        t.expect(e).toBeTruthy();
80      }
81    });
82  });
83  t.describe('SecureStore: store with empty Value -> err:', () => {
84    t.it('Sets an empty value with a key, expect error', async () => {
85      try {
86        const result = await SecureStore.setItemAsync(key, emptyValue, {});
87        t.fail(result);
88      } catch (e) {
89        t.expect(e).toBeTruthy();
90      }
91    });
92  });
93  t.describe(
94    'SecureStore: store value with keychainServiceA, fetch with keychainServiceB -> err:',
95    () => {
96      t.it('Sets a value with keychainServiceA', async () => {
97        const result = await SecureStore.setItemAsync(key, value, optionsServiceA);
98        t.expect(result).toBe(undefined);
99      });
100      if (Platform.OS === 'ios') {
101        t.it('Fetch value with keychainServiceB, expect null', async () => {
102          const result = await SecureStore.getItemAsync(key, optionsServiceB);
103          t.expect(result).toBe(null);
104        });
105      } else if (Platform.OS === 'android') {
106        t.it('Fetch value with keychainServiceB, expect decoding error', async () => {
107          try {
108            const result = await SecureStore.getItemAsync(key, optionsServiceB);
109            t.fail(result);
110          } catch (e) {
111            t.expect(e).toBeTruthy();
112            t.expect(e.message).toMatch(`Could not decrypt the item in SecureStore`);
113          }
114        });
115      }
116    }
117  );
118  t.describe('SecureStore: store long value, fetch long value -> Success:', () => {
119    t.it('Set long value', async () => {
120      const result = await SecureStore.setItemAsync(key, longValue);
121      t.expect(result).toBe(undefined);
122    });
123    t.it('Fetch long value', async () => {
124      const result = await SecureStore.getItemAsync(key);
125      t.expect(result).toBe(longValue);
126    });
127  });
128}
129