-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potential persistence issue (#620)
* Fix potential persistence issue Fix RangeError when converting array buffer to base64 for large arrays. Using String.fromCharCodeApply(null, array) may cause too many arguments to be passed * Add test
- Loading branch information
1 parent
2ccf5bf
commit 11926b4
Showing
2 changed files
with
40 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { decrypt, encrypt } from './aes'; | ||
|
||
describe('encrypt function', () => { | ||
test('encrypted value is the same after decryption', async () => { | ||
const value = { name: 'apple' }; | ||
|
||
const password = 'test-123'; | ||
const encrypted = await encrypt(password, value); | ||
expect(encrypted).not.toBe(JSON.stringify(value)); | ||
|
||
const decrypted = await decrypt(password, encrypted); | ||
expect(decrypted).toEqual(value); | ||
}); | ||
|
||
test('very large value is encrypted correctly', async () => { | ||
function createVeryLargeString() { | ||
// A large string will at some point be split into bytes by the encrypting function | ||
// and those bytes may be passed as arguments, but browsers have a different | ||
// limit on maximum number of agruments: https://stackoverflow.com/a/22747272/3523645 | ||
// As a solution we avoid doing that and have this test as a guard, but its outcome | ||
// may differ across javascript environments | ||
const uint8Array = crypto.getRandomValues(new Uint8Array(65536)); | ||
return uint8Array.reduce((str, c) => str + String.fromCharCode(c), ''); | ||
} | ||
const largeValue = { message: createVeryLargeString() }; | ||
|
||
const password = 'test-123'; | ||
const encrypted = await encrypt(password, largeValue); | ||
expect(encrypted).not.toBe(JSON.stringify(largeValue)); | ||
|
||
const decrypted = await decrypt(password, encrypted); | ||
expect(decrypted).toEqual(largeValue); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters