forked from ably/ably-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher_spec.rb
266 lines (207 loc) · 9.01 KB
/
cipher_spec.rb
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
require 'spec_helper'
require 'ably/models/message_encoders/cipher'
require 'msgpack'
describe Ably::Models::MessageEncoders::Cipher do
let(:secret_key) { Ably::Util::Crypto.generate_random_key(128) }
let(:crypto_options) { { key: secret_key, algorithm: 'AES', mode: 'CBC', key_length: 128 } }
let(:crypto) { Ably::Util::Crypto.new(cipher_params) }
let(:decoded_data) { random_str(32) }
let(:cipher_data) { crypto.encrypt(decoded_data) }
let(:binary_data) { MessagePack.pack(decoded_data) }
let(:binary_cipher_data) { crypto.encrypt(binary_data) }
let(:client) { instance_double('Ably::Realtime::Client') }
subject { Ably::Models::MessageEncoders::Cipher.new(client) }
context '#decode' do
context 'with channel set up for AES-128-CBC' do
let(:cipher_params) { crypto_options }
context 'valid cipher data' do
before do
subject.decode message, { cipher: cipher_params }
end
context 'message with cipher payload' do
let(:message) { { data: cipher_data, encoding: 'cipher+aes-128-cbc' } }
it 'decodes cipher' do
expect(message[:data]).to eql(decoded_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to be_nil
end
end
context 'message with cipher payload before other payloads' do
let(:message) { { data: cipher_data, encoding: 'utf-8/cipher+aes-128-cbc' } }
it 'decodes cipher' do
expect(message[:data]).to eql(decoded_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to eql('utf-8')
end
end
context 'message with binary payload' do
let(:message) { { data: binary_cipher_data, encoding: 'base64/cipher+aes-128-cbc' } }
it 'decodes cipher' do
expect(message[:data]).to eql(binary_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to eql('base64')
end
it 'returns ASCII_8BIT encoded binary data' do
expect(message[:data].encoding).to eql(Encoding::ASCII_8BIT)
end
end
context 'message with another payload' do
let(:message) { { data: decoded_data, encoding: 'utf-8' } }
it 'leaves the message data intact' do
expect(message[:data]).to eql(decoded_data)
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to eql('utf-8')
end
end
end
context '256 bit key' do
let(:secret_key) { Ably::Util::Crypto.generate_random_key(256) }
context 'with invalid channel_option cipher params' do
let(:message) { { data: decoded_data, encoding: 'cipher+aes-128-cbc' } }
let(:cipher_params) { crypto_options.merge(key_length: 256) }
let(:decode_method) { subject.decode message, { cipher: cipher_params } }
it 'raise an exception' do
expect { decode_method }.to raise_error Ably::Exceptions::CipherError, /Cipher algorithm [\w-]+ does not match message cipher algorithm of AES-128-CBC/
end
end
context 'without any configured encryption' do
let(:message) { { data: decoded_data, encoding: 'cipher+aes-128-cbc' } }
let(:cipher_params) { crypto_options.merge(key_length: 256) }
let(:decode_method) { subject.decode message, {} }
it 'raise an exception' do
expect { decode_method }.to raise_error Ably::Exceptions::CipherError, /Message cannot be decrypted as the channel is not set up for encryption & decryption/
end
end
end
context 'with invalid cipher data' do
let(:message) { { data: decoded_data, encoding: 'cipher+aes-128-cbc' } }
let(:decode_method) { subject.decode(message, { cipher: cipher_params }) }
it 'raise an exception' do
expect { decode_method }.to raise_error Ably::Exceptions::CipherError, /CipherError decrypting data/
end
end
end
context 'with AES-256-CBC' do
let(:secret_key) { Ably::Util::Crypto.generate_random_key(256) }
let(:cipher_params) { crypto_options.merge(key_length: 256) }
before do
subject.decode message, { cipher: cipher_params }
end
context 'message with cipher payload' do
let(:message) { { data: cipher_data, encoding: 'cipher+aes-256-cbc' } }
it 'decodes cipher' do
expect(message[:data]).to eql(decoded_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to be_nil
end
end
end
end
context '#encode' do
context 'with channel set up for AES-128-CBC' do
let(:cipher_params) { crypto_options }
let(:channel_options) { { cipher: cipher_params } }
context 'with encrypted set to true' do
before do
subject.encode message, channel_options
end
context 'message with string payload' do
let(:message) { { data: decoded_data, encoding: nil } }
it 'encodes cipher' do
expect(message[:data]).to_not eql(decoded_data)
expect(crypto.decrypt(message[:data])).to eql(decoded_data)
end
it 'adds the encoding with utf-8' do
expect(message[:encoding]).to eql('utf-8/cipher+aes-128-cbc')
end
end
context 'message with binary payload' do
let(:message) { { data: binary_data, encoding: nil } }
it 'encodes cipher' do
expect(message[:data]).to_not eql(binary_data)
expect(crypto.decrypt(message[:data])).to eql(binary_data)
end
it 'adds the encoding without utf-8 prefixed' do
expect(message[:encoding]).to eql('cipher+aes-128-cbc')
end
it 'returns ASCII_8BIT encoded binary data' do
expect(message[:data].encoding).to eql(Encoding::ASCII_8BIT)
end
end
context 'message with json payload' do
let(:message) { { data: decoded_data, encoding: 'json' } }
it 'encodes cipher' do
expect(message[:data]).to_not eql(decoded_data)
expect(crypto.decrypt(message[:data])).to eql(decoded_data)
end
it 'adds the encoding with utf-8' do
expect(message[:encoding]).to eql('json/utf-8/cipher+aes-128-cbc')
end
end
context 'message with existing cipher encoding before' do
let(:message) { { data: decoded_data, encoding: 'utf-8/cipher+aes-128-cbc' } }
it 'leaves message intact as it is already encrypted' do
expect(message[:data]).to eql(decoded_data)
end
it 'leaves encoding intact' do
expect(message[:encoding]).to eql('utf-8/cipher+aes-128-cbc')
end
end
context 'with encryption set to to false' do
let(:message) { { data: decoded_data, encoding: 'utf-8' } }
let(:channel_options) { { encrypted: false, cipher_params: cipher_params } }
it 'leaves message intact as encryption is not enable' do
expect(message[:data]).to eql(decoded_data)
end
it 'leaves encoding intact' do
expect(message[:encoding]).to eql('utf-8')
end
end
end
context 'channel_option cipher params' do
let(:message) { { data: decoded_data, encoding: nil } }
let(:encode_method) { subject.encode message, { cipher: cipher_params } }
context 'have invalid key length' do
let(:cipher_params) { crypto_options.merge(key_length: 1) }
it 'raise an exception' do
expect { encode_method }.to raise_error Ably::Exceptions::CipherError, /Incompatible :key length/
end
end
context 'have invalid algorithm' do
let(:cipher_params) { crypto_options.merge(algorithm: 'does not exist') }
it 'raise an exception' do
expect { encode_method }.to raise_error Ably::Exceptions::CipherError, /unsupported cipher algorithm/
end
end
context 'have missing key' do
let(:cipher_params) { {} }
it 'raise an exception' do
expect { encode_method }.to raise_error Ably::Exceptions::CipherError, /key.*required/
end
end
end
end
context 'with AES-256-CBC' do
let(:secret_key) { Ably::Util::Crypto.generate_random_key(256) }
let(:cipher_params) { crypto_options.merge(key_length: 256) }
before do
subject.encode message, { cipher: cipher_params }
end
context 'message with cipher payload' do
let(:message) { { data: decoded_data, encoding: 'utf-8' } }
it 'decodes cipher' do
expect(message[:data]).to_not eql(decoded_data)
expect(crypto.decrypt(message[:data])).to eql(decoded_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to eql('utf-8/cipher+aes-256-cbc')
end
end
end
end
end