forked from aws/aws-encryption-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode_body_header.test.ts
553 lines (494 loc) · 17.1 KB
/
decode_body_header.test.ts
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-env mocha */
import { expect } from 'chai'
import 'mocha'
import {
decodeFrameBodyHeader,
decodeNonFrameBodyHeader,
decodeBodyHeader,
decodeFinalFrameBodyHeader
} from '../src/decode_body_header'
import { concatBuffers } from '../src'
import * as fixtures from './fixtures'
import { ContentType } from '../src/identifiers'
describe('decodeBodyHeader', () => {
it('calls decodeFrameBodyHeader', () => {
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const test = decodeBodyHeader(fixtures.basicFrameHeader(), headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.contentType).to.eql(ContentType.FRAMED_DATA)
})
it('calls decodeNonFrameBodyHeader', () => {
const headerInfo = {
messageHeader: {
contentType: ContentType.NO_FRAMING
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const test = decodeBodyHeader(fixtures.basicNonFrameHeader(), headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.contentType).to.eql(ContentType.NO_FRAMING)
})
it('Precondition: The contentType must be a supported format.', () => {
const headerInfo = {
messageHeader: {
contentType: 'does not exist'
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
expect(() => decodeBodyHeader(fixtures.basicNonFrameHeader(), headerInfo, 0)).to.throw('Unknown contentType')
})
})
describe('decodeFrameBodyHeader', () => {
it('return frame header', () => {
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const test = decodeFrameBodyHeader(fixtures.basicFrameHeader(), headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.sequenceNumber).to.eql(1)
expect(test.iv).to.eql(fixtures.basicFrameIV())
expect(test.readPos).to.eql(16)
expect(test.tagLength).to.eql(16)
expect(test.isFinalFrame).to.eql(false)
expect(test.contentType).to.eql(ContentType.FRAMED_DATA)
})
it('return final frame header', () => {
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const test = decodeFrameBodyHeader(fixtures.finalFrameHeader(), headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.sequenceNumber).to.eql(1)
expect(test.iv).to.eql(fixtures.basicFrameIV())
expect(test.readPos).to.eql(24)
expect(test.tagLength).to.eql(16)
expect(test.isFinalFrame).to.eql(true)
expect(test.contentType).to.eql(ContentType.FRAMED_DATA)
})
it('Precondition: The contentType must be FRAMED_DATA.', () => {
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: 'not FRAMED_DATA'
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
expect(() => decodeFrameBodyHeader(fixtures.basicFrameHeader(), headerInfo, 0)).to.throw('Unknown contentType')
})
it('Check for early return (Postcondition): There must be enough data to decodeFrameBodyHeader.', () => {
const frameHeader = fixtures.basicFrameHeader()
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
for (let i = 0; frameHeader.byteLength > i; i++) {
const test = decodeFrameBodyHeader(frameHeader.slice(0, i), headerInfo, 0)
expect(test).to.eql(false)
}
})
it('return frame header from readPos', () => {
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = concatBuffers(
new Uint8Array(10), // pre
fixtures.basicFrameHeader(),
new Uint8Array(10) // post
)
const test = decodeFrameBodyHeader(buffer, headerInfo, 10)
if (!test) throw new Error('failure')
expect(test.sequenceNumber).to.eql(1)
expect(test.iv).to.eql(fixtures.basicFrameIV())
expect(test.readPos).to.eql(26)
expect(test.tagLength).to.eql(16)
expect(test.isFinalFrame).to.eql(false)
expect(test.contentType).to.eql(ContentType.FRAMED_DATA)
})
it('return false for partial basic frame from readPos', () => {
const buffer = concatBuffers(new Uint8Array(10), fixtures.basicFrameHeader())
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
for (let i = 10; buffer.byteLength - 1 > i; i++) {
const test = decodeFrameBodyHeader(buffer.slice(0, i), headerInfo, 10)
expect(test).to.eql(false)
}
})
it('return false for partial frame from readPos', () => {
const buffer = concatBuffers(new Uint8Array(10), fixtures.finalFrameHeader())
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
for (let i = 10; buffer.byteLength > i; i++) {
const test = decodeFrameBodyHeader(buffer.slice(0, i), headerInfo, 10)
expect(test).to.eql(false)
}
})
it('Precondition: decodeFrameBodyHeader readPos must be within the byte length of the buffer given.', () => {
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.basicFrameHeader()
expect(() => decodeFrameBodyHeader(buffer, headerInfo, buffer.byteLength + 1)).to.throw()
expect(() => decodeFrameBodyHeader(buffer, headerInfo, -1)).to.throw()
})
it('Postcondition: decodeFrameBodyHeader sequenceNumber must be greater than 0.', () => {
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
expect(() => decodeFrameBodyHeader(fixtures.invalidSequenceNumberFrameHeader(), headerInfo, 0)).to.throw('Malformed sequenceNumber.')
})
it('ArrayBuffer for a Uint8Array or Buffer may be larger than the Uint8Array or Buffer that it is a view over is.', () => {
const headerInfo = {
messageHeader: {
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
/* Create a Uint8Array that has an a valid FrameHeader but is proceeded by "invalid" bytes. */
const buff = concatBuffers(new Uint8Array(5), fixtures.basicFrameHeader())
expect(() => decodeFrameBodyHeader(buff, headerInfo, 0)).to.throw()
// Now we verify that the if we read from after the "invalid" section everything is OK.
const verify = decodeFrameBodyHeader(buff, headerInfo, 5)
if (!verify) throw new Error('failure')
expect(verify.sequenceNumber).to.eql(1)
expect(verify.iv).to.eql(fixtures.basicFrameIV())
expect(verify.readPos).to.eql(buff.byteLength)
expect(verify.tagLength).to.eql(16)
expect(verify.isFinalFrame).to.eql(false)
expect(verify.contentType).to.eql(ContentType.FRAMED_DATA)
/* Given this I can use this to construct a new view of part of the
* ArrayBuffer to simulate a large ArrayBuffer that is sliced
* into parts for efficiency. */
const sharingArrayBuffer = new Uint8Array(buff.buffer, 5, buff.byteLength - 5)
const test = decodeFrameBodyHeader(sharingArrayBuffer, headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.sequenceNumber).to.eql(1)
expect(test.iv).to.eql(fixtures.basicFrameIV())
expect(test.readPos).to.eql(sharingArrayBuffer.byteLength)
expect(test.tagLength).to.eql(16)
expect(test.isFinalFrame).to.eql(false)
expect(test.contentType).to.eql(ContentType.FRAMED_DATA)
})
})
describe('decodeFinalFrameBodyHeader', () => {
it('return final frame header from readPos', () => {
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = concatBuffers(
new Uint8Array(10), // pre
fixtures.finalFrameHeader(),
new Uint8Array(10) // post
)
const test = decodeFinalFrameBodyHeader(buffer, headerInfo, 10)
if (!test) throw new Error('failure')
expect(test.sequenceNumber).to.eql(1)
expect(test.iv).to.eql(fixtures.basicFrameIV())
expect(test.readPos).to.eql(34)
expect(test.tagLength).to.eql(16)
expect(test.isFinalFrame).to.eql(true)
expect(test.contentType).to.eql(ContentType.FRAMED_DATA)
expect(test.contentLength).to.eql(999)
})
it('The final frame can be 0 length.', () => {
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.finalFrameHeaderZeroBytes()
const test = decodeFinalFrameBodyHeader(buffer, headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.sequenceNumber).to.eql(1)
expect(test.iv).to.eql(fixtures.basicFrameIV())
expect(test.tagLength).to.eql(16)
expect(test.isFinalFrame).to.eql(true)
expect(test.contentType).to.eql(ContentType.FRAMED_DATA)
expect(test.contentLength).to.eql(0)
})
it('Precondition: The contentType must be FRAMED_DATA to be a Final Frame.', () => {
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: 'not FRAMED_DATA'
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
expect(() => decodeFinalFrameBodyHeader(fixtures.finalFrameHeader(), headerInfo, 0)).to.throw('Unknown contentType')
})
it('Precondition: decodeFinalFrameBodyHeader readPos must be within the byte length of the buffer given.', () => {
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.finalFrameHeader()
expect(() => decodeFinalFrameBodyHeader(buffer, headerInfo, buffer.byteLength + 1)).to.throw('readPos out of bounds.')
expect(() => decodeFinalFrameBodyHeader(buffer, headerInfo, -1)).to.throw('readPos out of bounds.')
})
it('Postcondition: sequenceEnd must be SEQUENCE_NUMBER_END.', () => {
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.invalidSequenceEndFinalFrameHeader()
expect(() => decodeFinalFrameBodyHeader(buffer, headerInfo, 0)).to.throw('Malformed final frame: Invalid sequence number end value')
})
it('Postcondition: decodeFinalFrameBodyHeader sequenceNumber must be greater than 0.', () => {
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.invalidSequenceNumberFinalFrameHeader()
expect(() => decodeFinalFrameBodyHeader(buffer, headerInfo, 0)).to.throw('Malformed sequenceNumber.')
})
it('Check for early return (Postcondition): There must be enough data to decodeFinalFrameBodyHeader.', () => {
const frameHeader = fixtures.finalFrameHeader()
const headerInfo = {
messageHeader: {
frameLength: 999,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
for (let i = 0; frameHeader.byteLength > i; i++) {
const test = decodeFinalFrameBodyHeader(frameHeader.slice(0, i), headerInfo, 0)
expect(test).to.eql(false)
}
})
it('Postcondition: The final frame MUST NOT exceed the frameLength.', () => {
const headerInfo = {
messageHeader: {
// The content length in this final frame is 999
// So I set the frame length to less than this
frameLength: 99,
contentType: ContentType.FRAMED_DATA
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.finalFrameHeader()
expect(() => decodeFinalFrameBodyHeader(buffer, headerInfo, 0)).to.throw('Final frame length exceeds frame length.')
})
})
describe('decodeNonFrameBodyHeader', () => {
it('return non frame header', () => {
const headerInfo = {
messageHeader: {
contentType: ContentType.NO_FRAMING
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.basicNonFrameHeader()
const test = decodeNonFrameBodyHeader(buffer, headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.sequenceNumber).to.eql(1)
expect(test.iv).to.eql(fixtures.basicFrameIV())
expect(test.readPos).to.eql(20)
expect(test.tagLength).to.eql(16)
expect(test.isFinalFrame).to.eql(true)
expect(test.contentType).to.eql(ContentType.NO_FRAMING)
})
it('Precondition: The contentType must be NO_FRAMING.', () => {
const headerInfo = {
messageHeader: {
contentType: 'not NO_FRAMING'
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.basicNonFrameHeader()
expect(() => decodeNonFrameBodyHeader(buffer, headerInfo, 0)).to.throw('Unknown contentType')
})
it('Check for early return (Postcondition): There must be enough data to decodeNonFrameBodyHeader.', () => {
const headerInfo = {
messageHeader: {
contentType: ContentType.NO_FRAMING
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const frameHeader = fixtures.basicNonFrameHeader()
for (let i = 0; frameHeader.byteLength > i; i++) {
const test = decodeNonFrameBodyHeader(frameHeader.slice(0, i), headerInfo, 0)
expect(test).to.eql(false)
}
})
it('Precondition: decodeNonFrameBodyHeader readPos must be within the byte length of the buffer given.', () => {
const headerInfo = {
messageHeader: {
contentType: ContentType.NO_FRAMING
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
const buffer = fixtures.basicNonFrameHeader()
expect(() => decodeNonFrameBodyHeader(buffer, headerInfo, buffer.byteLength + 1)).to.throw()
expect(() => decodeNonFrameBodyHeader(buffer, headerInfo, -1)).to.throw()
})
it('ArrayBuffer for a Uint8Array or Buffer may be larger than the Uint8Array or Buffer that it is a view over is.', () => {
const headerInfo = {
messageHeader: {
contentType: ContentType.NO_FRAMING
},
algorithmSuite: {
ivLength: 12,
tagLength: 16
}
} as any
/* Create a Uint8Array that has an a valid FrameHeader but is proceeded by "invalid" bytes. */
const buff = concatBuffers(new Uint8Array(5), fixtures.basicNonFrameHeader())
const shouldFail = decodeNonFrameBodyHeader(buff, headerInfo, 0)
if (!shouldFail) throw new Error('failure')
expect(shouldFail.iv).to.not.eql(fixtures.basicFrameIV())
// Now we verify that the if we read from after the "invalid" section everything is OK.
const verify = decodeNonFrameBodyHeader(buff, headerInfo, 5)
if (!verify) throw new Error('failure')
expect(verify.iv).to.eql(fixtures.basicFrameIV())
/* Given this I can use this to construct a new view of part of the
* ArrayBuffer to simulate a large ArrayBuffer that is sliced
* into parts for efficiency. */
const sharingArrayBuffer = new Uint8Array(buff.buffer, 5, buff.byteLength - 5)
const test = decodeNonFrameBodyHeader(sharingArrayBuffer, headerInfo, 0)
if (!test) throw new Error('failure')
expect(test.iv).to.eql(fixtures.basicFrameIV())
})
})