27 lines
1020 B
TypeScript
27 lines
1020 B
TypeScript
import { test } from 'node:test';
|
|||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import { clearSecretStorageKeys, cryptoCallbacks, storePrivateKey } from './secretStorageKeys';
|
||
|
|
|
||
|
|
const ask = (...ids: string[]) =>
|
||
|
|
cryptoCallbacks.getSecretStorageKey!(
|
||
|
|
{ keys: Object.fromEntries(ids.map((id) => [id, {} as never])) },
|
||
|
|
'm.cross_signing.master',
|
||
|
|
);
|
||
|
|
|
||
|
|
test('[#210] secret storage key cache: null when unknown, key when stored, copies input', async () => {
|
||
|
|
clearSecretStorageKeys();
|
||
|
|
assert.equal(await ask('k1'), null);
|
||
|
|
|
||
|
|
const key = new Uint8Array([1, 2, 3]);
|
||
|
|
storePrivateKey('k1', key);
|
||
|
|
key[0] = 9; // the cache keeps its own copy
|
||
|
|
assert.deepEqual(await ask('k0', 'k1'), ['k1', new Uint8Array([1, 2, 3])]);
|
||
|
|
|
||
|
|
cryptoCallbacks.cacheSecretStorageKey!('k2', {} as never, new Uint8Array([7]));
|
||
|
|
assert.deepEqual(await ask('k2'), ['k2', new Uint8Array([7])]);
|
||
|
|
|
||
|
|
clearSecretStorageKeys();
|
||
|
|
assert.equal(await ask('k1', 'k2'), null);
|
||
|
|
assert.throws(() => storePrivateKey('k3', 'nope' as never), /invalid/);
|
||
|
|
});
|