-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDecrypt.test.ts
More file actions
61 lines (58 loc) · 2.82 KB
/
Decrypt.test.ts
File metadata and controls
61 lines (58 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { createTestWallet } from '@streamr/test-utils'
import { StreamPartIDUtils, utf8ToBinary } from '@streamr/utils'
import { mock } from 'jest-mock-extended'
import { DestroySignal } from '../../src/DestroySignal'
import { StreamrClientError } from '../../src/StreamrClientError'
import { GroupKey } from '../../src/encryption/GroupKey'
import { GroupKeyManager } from '../../src/encryption/GroupKeyManager'
import { decrypt } from '../../src/encryption/decrypt'
import { createGroupKeyManager, createMockMessage } from '../test-utils/utils'
import { StreamMessage, StreamMessageAESEncrypted } from './../../src/protocol/StreamMessage'
import { EncryptionType } from '@streamr/trackerless-network'
import { EthereumKeyPairIdentity } from '../../src/identity/EthereumKeyPairIdentity'
describe('Decrypt', () => {
it('happy path', async () => {
const groupKey = GroupKey.generate()
const groupKeyManager = mock<GroupKeyManager>()
groupKeyManager.fetchKey.mockResolvedValueOnce(groupKey)
const destroySignal = new DestroySignal()
// using Buffer to get around Jest's strict equality check
const unencryptedContent = Buffer.from(utf8ToBinary(JSON.stringify({ hello: 'world' })))
const encryptedMessage = await createMockMessage({
streamPartId: StreamPartIDUtils.parse('stream#0'),
publisher: await createTestWallet(),
encryptionKey: groupKey,
content: unencryptedContent
}) as StreamMessageAESEncrypted
const decryptedMessage = await decrypt(encryptedMessage, groupKeyManager, destroySignal)
expect(decryptedMessage).toEqual(new StreamMessage({
...encryptedMessage,
encryptionType: EncryptionType.NONE,
content: unencryptedContent
}))
expect(groupKeyManager.fetchKey).toHaveBeenCalledWith(
encryptedMessage.getStreamPartID(),
encryptedMessage.groupKeyId,
encryptedMessage.getPublisherId()
)
})
it('group key not available: timeout while waiting', async () => {
const wallet = await createTestWallet()
const groupKeyManager = await createGroupKeyManager(EthereumKeyPairIdentity.fromPrivateKey(wallet.privateKey))
const destroySignal = new DestroySignal()
const groupKey = GroupKey.generate()
const msg = await createMockMessage({
streamPartId: StreamPartIDUtils.parse('stream#0'),
publisher: wallet,
encryptionKey: groupKey
})
await expect(() => {
return decrypt(
msg as StreamMessageAESEncrypted,
groupKeyManager,
destroySignal)
}).rejects.toThrowStreamrClientError(
new StreamrClientError(`Could not get encryption key ${groupKey.id}`, 'DECRYPT_ERROR', msg)
)
})
})