-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b5e42a
commit 95a0eba
Showing
4 changed files
with
104 additions
and
7 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
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
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,61 @@ | ||
import { WebCryptoCipher } from '../../../src/ciphers/services/web-crypto.service'; | ||
import { Key } from '../../../src/helpers/key.helper'; | ||
import { Buffer } from '../../../src/helpers/buffer.helper'; | ||
|
||
describe('Web crypto service', () => { | ||
test(`Negative: encrypt() - key does not exist.`, async () => { | ||
const spy = jest.spyOn(Key as any, 'pbkdf2'); | ||
spy.mockImplementation(() => { | ||
return undefined; | ||
}); | ||
|
||
const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12); | ||
|
||
expect(async () => { | ||
await cipher.encrypt('text'); | ||
}).rejects.toThrow(Error('key does not exist.')); | ||
|
||
spy.mockReset(); | ||
spy.mockRestore(); | ||
}); | ||
|
||
test(`Negative: decrypt() - key does not exist.`, async () => { | ||
const spy = jest.spyOn(Key as any, 'pbkdf2'); | ||
spy.mockImplementation(() => { | ||
return undefined; | ||
}); | ||
|
||
const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12); | ||
|
||
expect(async () => { | ||
await cipher.decrypt('text'); | ||
}).rejects.toThrow(Error('key does not exist.')); | ||
|
||
spy.mockReset(); | ||
spy.mockRestore(); | ||
}); | ||
|
||
test(`Negative: decrypt() - key does not exist.`, async () => { | ||
const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12); | ||
|
||
expect(async () => { | ||
await cipher.decrypt('0'); | ||
}).rejects.toThrow(Error('invalid text.')); | ||
}); | ||
|
||
test(`Negative: decrypt() - data conversion failed.`, async () => { | ||
const spy = jest.spyOn(Buffer as any, 'toUint8Array'); | ||
spy.mockImplementation(() => { | ||
return undefined; | ||
}); | ||
|
||
const cipher = await WebCryptoCipher.create('AES-GCM', 256, 'password', 12); | ||
|
||
expect(async () => { | ||
await cipher.decrypt('00000000000000000000000000000000'); | ||
}).rejects.toThrow(Error('data conversion failed.')); | ||
|
||
spy.mockReset(); | ||
spy.mockRestore(); | ||
}); | ||
}); |
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