-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmessagePipeline.test.ts
More file actions
190 lines (178 loc) · 7.54 KB
/
messagePipeline.test.ts
File metadata and controls
190 lines (178 loc) · 7.54 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import 'reflect-metadata'
import { fastWallet, randomEthereumAddress } from '@streamr/test-utils'
import { StreamPartID, StreamPartIDUtils, collect, hexToBinary, toUserId, utf8ToBinary } from '@streamr/utils'
import { Wallet } from 'ethers'
import { mock } from 'jest-mock-extended'
import { createPrivateKeyAuthentication } from '../../src/Authentication'
import { StrictStreamrClientConfig } from '../../src/Config'
import { DestroySignal } from '../../src/DestroySignal'
import { ERC1271ContractFacade } from '../../src/contracts/ERC1271ContractFacade'
import { StreamRegistry } from '../../src/contracts/StreamRegistry'
import { DecryptError, EncryptionUtil } from '../../src/encryption/EncryptionUtil'
import { GroupKey } from '../../src/encryption/GroupKey'
import { GroupKeyManager } from '../../src/encryption/GroupKeyManager'
import { LitProtocolFacade } from '../../src/encryption/LitProtocolFacade'
import { SubscriberKeyExchange } from '../../src/encryption/SubscriberKeyExchange'
import { StreamrClientEventEmitter } from '../../src/events'
import { MessageSigner } from '../../src/signature/MessageSigner'
import { SignatureValidator } from '../../src/signature/SignatureValidator'
import { createMessagePipeline } from '../../src/subscribe/messagePipeline'
import { PushPipeline } from '../../src/utils/PushPipeline'
import { mockLoggerFactory } from '../test-utils/utils'
import { MessageID } from './../../src/protocol/MessageID'
import { ContentType, EncryptionType, SignatureType, StreamMessage, StreamMessageType } from './../../src/protocol/StreamMessage'
const CONTENT = {
foo: 'bar'
}
describe('messagePipeline', () => {
let pipeline: PushPipeline<StreamMessage, StreamMessage>
let streamRegistry: Partial<StreamRegistry>
let streamPartId: StreamPartID
let publisher: Wallet
const createMessage = async (opts: {
content?: Uint8Array
encryptionType?: EncryptionType
groupKeyId?: string
contentType?: ContentType
} = {}): Promise<StreamMessage> => {
const [streamId, partition] = StreamPartIDUtils.getStreamIDAndPartition(streamPartId)
const messageSigner = new MessageSigner(createPrivateKeyAuthentication(publisher.privateKey))
return messageSigner.createSignedMessage({
messageId: new MessageID(
streamId,
partition,
Date.now(),
0,
toUserId(publisher.address),
'mock-msgChainId'
),
messageType: StreamMessageType.MESSAGE,
content: opts.contentType === ContentType.BINARY ? opts.content! : utf8ToBinary(JSON.stringify(CONTENT)),
contentType: opts.contentType ?? ContentType.JSON,
encryptionType: EncryptionType.NONE,
...opts
}, SignatureType.SECP256K1)
}
beforeEach(async () => {
streamPartId = StreamPartIDUtils.parse(`${randomEthereumAddress()}/path#0`)
publisher = fastWallet()
const groupKeyStore = {
get: async () => undefined
} as any
const destroySignal = new DestroySignal()
const config: Pick<StrictStreamrClientConfig, 'encryption'> = {
encryption: {
litProtocolEnabled: false,
litProtocolLogging: false,
keyRequestTimeout: 50,
maxKeyRequestsPerSecond: 0
} as any
}
streamRegistry = {
getStreamMetadata: async () => ({ partitions: 1 }),
isStreamPublisher: async () => true,
invalidatePermissionCaches: jest.fn()
}
pipeline = createMessagePipeline({
streamPartId,
getStorageNodes: undefined as any,
resends: undefined as any,
streamRegistry: streamRegistry as any,
signatureValidator: new SignatureValidator(mock<ERC1271ContractFacade>()),
groupKeyManager: new GroupKeyManager(
mock<SubscriberKeyExchange>(),
mock<LitProtocolFacade>(),
groupKeyStore,
config,
createPrivateKeyAuthentication(publisher.privateKey),
new StreamrClientEventEmitter(),
destroySignal
),
config: config as any,
destroySignal,
loggerFactory: mockLoggerFactory(),
})
})
it('happy path', async () => {
const msg = await createMessage()
await pipeline.push(msg)
pipeline.endWrite()
const output = await collect(pipeline)
expect(output).toHaveLength(1)
expect(output[0].getParsedContent()).toEqual(CONTENT)
})
it('binary content', async () => {
const content = new Uint8Array([1, 2, 3])
const msg = await createMessage({
content: content,
contentType: ContentType.BINARY
})
await pipeline.push(msg)
pipeline.endWrite()
const output = await collect(pipeline)
expect(output).toHaveLength(1)
expect(output[0].getParsedContent()).toEqual(content)
})
it('error: invalid signature', async () => {
const originalMsg = await createMessage()
const msg = new StreamMessage({
...originalMsg,
signature: hexToBinary('0x111111')
})
await pipeline.push(msg)
pipeline.endWrite()
const onError = jest.fn()
pipeline.onError.listen(onError)
const output = await collect(pipeline)
expect(onError).toBeCalledTimes(1)
const error = onError.mock.calls[0][0]
expect(error.message).toContain('Signature validation failed')
expect(output).toEqual([])
})
it('error: invalid content', async () => {
const msg = await createMessage({
content: utf8ToBinary('{ invalid-json'),
})
await pipeline.push(msg)
pipeline.endWrite()
const onError = jest.fn()
pipeline.onError.listen(onError)
const output = await collect(pipeline)
expect(onError).toBeCalledTimes(1)
const error = onError.mock.calls[0][0]
expect(error.message).toContain('Invalid JSON')
expect(output).toEqual([])
})
it('error: no encryption key available', async () => {
const encryptionKey = GroupKey.generate()
const content = EncryptionUtil.encryptWithAES(Buffer.from(JSON.stringify(CONTENT), 'utf8'), encryptionKey.data)
await pipeline.push(await createMessage({
content,
encryptionType: EncryptionType.AES,
groupKeyId: encryptionKey.id
}))
pipeline.endWrite()
const onError = jest.fn()
pipeline.onError.listen(onError)
const output = await collect(pipeline)
expect(onError).toBeCalledTimes(1)
const error = onError.mock.calls[0][0]
expect(error).toBeInstanceOf(DecryptError)
expect(error.message).toMatch(/timed out/)
expect(output).toEqual([])
expect(streamRegistry.invalidatePermissionCaches).toBeCalledTimes(1)
expect(streamRegistry.invalidatePermissionCaches).toBeCalledWith(StreamPartIDUtils.getStreamID(streamPartId))
})
it('error: exception', async () => {
const err = new Error('mock-error')
const msg = await createMessage()
await pipeline.push(msg)
pipeline.endWrite(err)
const onError = jest.fn()
pipeline.onError.listen(onError)
const output = await collect(pipeline)
expect(output).toHaveLength(1)
expect(onError).toBeCalledTimes(1)
expect(onError).toBeCalledWith(err)
})
})