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