/
pkixder_input_tests.cpp
921 lines (789 loc) · 25.5 KB
1
2
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
4
5
6
7
8
9
10
/* This code is made available to you under your choice of the following sets
* of licensing terms:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* Copyright 2013 Mozilla Contributors
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
#include <functional>
#include <vector>
27
#include "pkixgtest.h"
28
29
#include "mozpkix/pkixder.h"
30
31
using namespace mozilla::pkix;
32
33
34
35
using namespace mozilla::pkix::der;
namespace {
36
class pkixder_input_tests : public ::testing::Test { };
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
static const uint8_t DER_SEQUENCE_EMPTY[] = {
0x30, // SEQUENCE
0x00, // length
};
static const uint8_t DER_SEQUENCE_NOT_EMPTY[] = {
0x30, // SEQUENCE
0x01, // length
'X', // value
};
static const uint8_t DER_SEQUENCE_NOT_EMPTY_VALUE[] = {
'X', // value
};
static const uint8_t DER_SEQUENCE_NOT_EMPTY_VALUE_TRUNCATED[] = {
0x30, // SEQUENCE
0x01, // length
};
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
const uint8_t DER_SEQUENCE_OF_INT8[] = {
0x30, // SEQUENCE
0x09, // length
0x02, 0x01, 0x01, // INTEGER length 1 value 0x01
0x02, 0x01, 0x02, // INTEGER length 1 value 0x02
0x02, 0x01, 0x03 // INTEGER length 1 value 0x03
};
const uint8_t DER_TRUNCATED_SEQUENCE_OF_INT8[] = {
0x30, // SEQUENCE
0x09, // length
0x02, 0x01, 0x01, // INTEGER length 1 value 0x01
0x02, 0x01, 0x02 // INTEGER length 1 value 0x02
// MISSING DATA HERE ON PURPOSE
};
const uint8_t DER_OVERRUN_SEQUENCE_OF_INT8[] = {
0x30, // SEQUENCE
0x09, // length
0x02, 0x01, 0x01, // INTEGER length 1 value 0x01
0x02, 0x01, 0x02, // INTEGER length 1 value 0x02
0x02, 0x02, 0xFF, 0x03 // INTEGER length 2 value 0xFF03
};
const uint8_t DER_INT16[] = {
0x02, // INTEGER
0x02, // length
0x12, 0x34 // 0x1234
};
88
89
static const Input EMPTY_INPUT;
90
TEST_F(pkixder_input_tests, InputInit)
91
{
92
Input buf;
93
ASSERT_EQ(Success,
94
buf.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8));
95
96
}
97
TEST_F(pkixder_input_tests, InputInitWithNullPointerOrZeroLength)
98
{
99
Input buf;
100
ASSERT_EQ(Result::ERROR_BAD_DER, buf.Init(nullptr, 0));
101
102
ASSERT_EQ(Result::ERROR_BAD_DER, buf.Init(nullptr, 100));
103
104
// Though it seems odd to initialize with zero-length and non-null ptr, this
105
// is working as intended. The Reader class was intended to protect against
106
107
// buffer overflows, and there's no risk with the current behavior. See bug
// 1000354.
108
109
ASSERT_EQ(Success, buf.Init((const uint8_t*) "hello", 0));
ASSERT_TRUE(buf.GetLength() == 0);
110
111
}
112
TEST_F(pkixder_input_tests, InputInitWithLargeData)
113
{
114
Input buf;
115
116
// Data argument length does not matter, it is not touched, just
// needs to be non-null
117
ASSERT_EQ(Result::ERROR_BAD_DER, buf.Init((const uint8_t*) "", 0xffff+1));
118
119
ASSERT_EQ(Success, buf.Init((const uint8_t*) "", 0xffff));
120
121
}
122
TEST_F(pkixder_input_tests, InputInitMultipleTimes)
123
{
124
Input buf;
125
126
ASSERT_EQ(Success,
127
buf.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8));
128
129
ASSERT_EQ(Result::FATAL_ERROR_INVALID_ARGS,
130
buf.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8));
131
132
133
134
135
}
TEST_F(pkixder_input_tests, PeekWithinBounds)
{
const uint8_t der[] = { 0x11, 0x11 };
136
137
Input buf(der);
Reader input(buf);
138
139
140
141
142
143
144
ASSERT_TRUE(input.Peek(0x11));
ASSERT_FALSE(input.Peek(0x22));
}
TEST_F(pkixder_input_tests, PeekPastBounds)
{
const uint8_t der[] = { 0x11, 0x22 };
145
Input buf;
146
ASSERT_EQ(Success, buf.Init(der, 1));
147
Reader input(buf);
148
149
150
151
152
153
154
155
156
157
uint8_t readByte;
ASSERT_EQ(Success, input.Read(readByte));
ASSERT_EQ(0x11, readByte);
ASSERT_FALSE(input.Peek(0x22));
}
TEST_F(pkixder_input_tests, ReadByte)
{
const uint8_t der[] = { 0x11, 0x22 };
158
159
Input buf(der);
Reader input(buf);
160
161
162
163
164
165
166
167
168
169
170
171
172
uint8_t readByte1;
ASSERT_EQ(Success, input.Read(readByte1));
ASSERT_EQ(0x11, readByte1);
uint8_t readByte2;
ASSERT_EQ(Success, input.Read(readByte2));
ASSERT_EQ(0x22, readByte2);
}
TEST_F(pkixder_input_tests, ReadBytePastEnd)
{
const uint8_t der[] = { 0x11, 0x22 };
173
Input buf;
174
ASSERT_EQ(Success, buf.Init(der, 1));
175
Reader input(buf);
176
177
178
179
180
181
uint8_t readByte1 = 0;
ASSERT_EQ(Success, input.Read(readByte1));
ASSERT_EQ(0x11, readByte1);
uint8_t readByte2 = 0;
182
ASSERT_EQ(Result::ERROR_BAD_DER, input.Read(readByte2));
183
184
185
ASSERT_NE(0x22, readByte2);
}
186
187
188
189
190
191
192
193
194
195
TEST_F(pkixder_input_tests, ReadByteWrapAroundPointer)
{
// The original implementation of our buffer read overflow checks was
// susceptible to integer overflows which could make the checks ineffective.
// This attempts to verify that we've fixed that. Unfortunately, decrementing
// a null pointer is undefined behavior according to the C++ language spec.,
// but this should catch the problem on at least some compilers, if not all of
// them.
const uint8_t* der = nullptr;
--der;
196
Input buf;
197
ASSERT_EQ(Success, buf.Init(der, 0));
198
Reader input(buf);
199
200
uint8_t b;
201
ASSERT_EQ(Result::ERROR_BAD_DER, input.Read(b));
202
203
}
204
205
206
TEST_F(pkixder_input_tests, ReadWord)
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
207
208
Input buf(der);
Reader input(buf);
209
210
211
212
213
214
215
216
217
218
219
220
221
uint16_t readWord1 = 0;
ASSERT_EQ(Success, input.Read(readWord1));
ASSERT_EQ(0x1122, readWord1);
uint16_t readWord2 = 0;
ASSERT_EQ(Success, input.Read(readWord2));
ASSERT_EQ(0x3344, readWord2);
}
TEST_F(pkixder_input_tests, ReadWordPastEnd)
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
222
Input buf;
223
ASSERT_EQ(Success, buf.Init(der, 2)); // Initialize with too-short length
224
Reader input(buf);
225
226
227
228
229
230
uint16_t readWord1 = 0;
ASSERT_EQ(Success, input.Read(readWord1));
ASSERT_EQ(0x1122, readWord1);
uint16_t readWord2 = 0;
231
ASSERT_EQ(Result::ERROR_BAD_DER, input.Read(readWord2));
232
233
234
235
236
237
ASSERT_NE(0x3344, readWord2);
}
TEST_F(pkixder_input_tests, ReadWordWithInsufficentData)
{
const uint8_t der[] = { 0x11, 0x22 };
238
Input buf;
239
ASSERT_EQ(Success, buf.Init(der, 1));
240
Reader input(buf);
241
242
uint16_t readWord1 = 0;
243
ASSERT_EQ(Result::ERROR_BAD_DER, input.Read(readWord1));
244
245
246
ASSERT_NE(0x1122, readWord1);
}
247
248
249
250
251
252
253
254
255
256
TEST_F(pkixder_input_tests, ReadWordWrapAroundPointer)
{
// The original implementation of our buffer read overflow checks was
// susceptible to integer overflows which could make the checks ineffective.
// This attempts to verify that we've fixed that. Unfortunately, decrementing
// a null pointer is undefined behavior according to the C++ language spec.,
// but this should catch the problem on at least some compilers, if not all of
// them.
const uint8_t* der = nullptr;
--der;
257
Input buf;
258
ASSERT_EQ(Success, buf.Init(der, 0));
259
Reader input(buf);
260
uint16_t b;
261
ASSERT_EQ(Result::ERROR_BAD_DER, input.Read(b));
262
263
}
264
TEST_F(pkixder_input_tests, Skip)
265
266
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
267
268
Input buf(der);
Reader input(buf);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
ASSERT_EQ(Success, input.Skip(1));
uint8_t readByte1 = 0;
ASSERT_EQ(Success, input.Read(readByte1));
ASSERT_EQ(0x22, readByte1);
ASSERT_EQ(Success, input.Skip(1));
uint8_t readByte2 = 0;
ASSERT_EQ(Success, input.Read(readByte2));
ASSERT_EQ(0x44, readByte2);
}
283
TEST_F(pkixder_input_tests, Skip_ToEnd)
284
285
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
286
287
Input buf(der);
Reader input(buf);
288
289
290
291
ASSERT_EQ(Success, input.Skip(sizeof der));
ASSERT_TRUE(input.AtEnd());
}
292
TEST_F(pkixder_input_tests, Skip_PastEnd)
293
294
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
295
296
Input buf(der);
Reader input(buf);
297
298
ASSERT_EQ(Result::ERROR_BAD_DER, input.Skip(sizeof der + 1));
299
300
}
301
TEST_F(pkixder_input_tests, Skip_ToNewInput)
302
303
{
const uint8_t der[] = { 0x01, 0x02, 0x03, 0x04 };
304
305
Input buf(der);
Reader input(buf);
306
307
Reader skippedInput;
308
309
310
311
312
313
314
315
ASSERT_EQ(Success, input.Skip(3, skippedInput));
uint8_t readByte1 = 0;
ASSERT_EQ(Success, input.Read(readByte1));
ASSERT_EQ(0x04, readByte1);
ASSERT_TRUE(input.AtEnd());
316
// Reader has no Remaining() or Length() so we simply read the bytes
317
318
319
320
321
322
323
324
325
326
327
// and then expect to be at the end.
for (uint8_t i = 1; i <= 3; ++i) {
uint8_t readByte = 0;
ASSERT_EQ(Success, skippedInput.Read(readByte));
ASSERT_EQ(i, readByte);
}
ASSERT_TRUE(skippedInput.AtEnd());
}
328
TEST_F(pkixder_input_tests, Skip_ToNewInputPastEnd)
329
330
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
331
332
Input buf(der);
Reader input(buf);
333
334
Reader skippedInput;
335
ASSERT_EQ(Result::ERROR_BAD_DER, input.Skip(sizeof der * 2, skippedInput));
336
337
}
338
TEST_F(pkixder_input_tests, Skip_ToInput)
339
340
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
341
342
Input buf(der);
Reader input(buf);
343
344
345
const uint8_t expectedItemData[] = { 0x11, 0x22, 0x33 };
346
Input item;
347
ASSERT_EQ(Success, input.Skip(sizeof expectedItemData, item));
348
349
350
Input expected(expectedItemData);
ASSERT_TRUE(InputsAreEqual(expected, item));
351
352
}
353
TEST_F(pkixder_input_tests, Skip_WrapAroundPointer)
354
355
356
357
358
359
360
361
{
// The original implementation of our buffer read overflow checks was
// susceptible to integer overflows which could make the checks ineffective.
// This attempts to verify that we've fixed that. Unfortunately, decrementing
// a null pointer is undefined behavior according to the C++ language spec.,
// but this should catch the problem on at least some compilers, if not all of
// them.
const uint8_t* der = nullptr;
362
// coverity[FORWARD_NULL]
363
--der;
364
Input buf;
365
ASSERT_EQ(Success, buf.Init(der, 0));
366
Reader input(buf);
367
ASSERT_EQ(Result::ERROR_BAD_DER, input.Skip(1));
368
369
}
370
TEST_F(pkixder_input_tests, Skip_ToInputPastEnd)
371
372
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
373
374
Input buf(der);
Reader input(buf);
375
376
Input skipped;
377
ASSERT_EQ(Result::ERROR_BAD_DER, input.Skip(sizeof der + 1, skipped));
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
TEST_F(pkixder_input_tests, SkipToEnd_ToInput)
{
static const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
Input buf(der);
Reader input(buf);
Input skipped;
ASSERT_EQ(Success, input.SkipToEnd(skipped));
}
TEST_F(pkixder_input_tests, SkipToEnd_ToInput_InputAlreadyInited)
{
static const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
Input buf(der);
Reader input(buf);
static const uint8_t initialValue[] = { 0x01, 0x02, 0x03 };
Input x(initialValue);
// Fails because skipped was already initialized once, and Inputs are not
// allowed to be Init()d multiple times.
ASSERT_EQ(Result::FATAL_ERROR_INVALID_ARGS, input.SkipToEnd(x));
ASSERT_TRUE(InputsAreEqual(x, Input(initialValue)));
}
404
TEST_F(pkixder_input_tests, ExpectTagAndSkipValue)
405
{
406
407
Input buf(DER_SEQUENCE_OF_INT8);
Reader input(buf);
408
409
ASSERT_EQ(Success, ExpectTagAndSkipValue(input, SEQUENCE));
410
411
412
ASSERT_EQ(Success, End(input));
}
413
TEST_F(pkixder_input_tests, ExpectTagAndSkipValueWithTruncatedData)
414
{
415
416
Input buf(DER_TRUNCATED_SEQUENCE_OF_INT8);
Reader input(buf);
417
418
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndSkipValue(input, SEQUENCE));
419
420
}
421
TEST_F(pkixder_input_tests, ExpectTagAndSkipValueWithOverrunData)
422
{
423
424
Input buf(DER_OVERRUN_SEQUENCE_OF_INT8);
Reader input(buf);
425
ASSERT_EQ(Success, ExpectTagAndSkipValue(input, SEQUENCE));
426
ASSERT_EQ(Result::ERROR_BAD_DER, End(input));
427
428
429
430
}
TEST_F(pkixder_input_tests, AtEndOnUnInitializedInput)
{
431
Reader input;
432
433
434
435
436
437
ASSERT_TRUE(input.AtEnd());
}
TEST_F(pkixder_input_tests, AtEndAtBeginning)
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
438
439
Input buf(der);
Reader input(buf);
440
441
442
443
444
445
ASSERT_FALSE(input.AtEnd());
}
TEST_F(pkixder_input_tests, AtEndAtEnd)
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
446
447
Input buf(der);
Reader input(buf);
448
449
450
451
ASSERT_EQ(Success, input.Skip(sizeof der));
ASSERT_TRUE(input.AtEnd());
}
452
TEST_F(pkixder_input_tests, MarkAndGetInput)
453
454
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
455
456
Input buf(der);
Reader input(buf);
457
458
Reader::Mark mark = input.GetMark();
459
460
461
462
463
const uint8_t expectedItemData[] = { 0x11, 0x22, 0x33 };
ASSERT_EQ(Success, input.Skip(sizeof expectedItemData));
464
465
466
467
Input item;
ASSERT_EQ(Success, input.GetInput(mark, item));
Input expected(expectedItemData);
ASSERT_TRUE(InputsAreEqual(expected, item));
468
469
}
470
// Cannot run this test on debug builds because of the NotReached
471
#ifdef NDEBUG
472
TEST_F(pkixder_input_tests, MarkAndGetInputDifferentInput)
473
474
{
const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 };
475
476
Input buf(der);
Reader input(buf);
477
478
479
Reader another;
Reader::Mark mark = another.GetMark();
480
481
482
ASSERT_EQ(Success, input.Skip(3));
483
484
Input item;
ASSERT_EQ(Result::FATAL_ERROR_INVALID_ARGS, input.GetInput(mark, item));
485
486
487
}
#endif
488
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_AtEnd)
489
{
490
491
492
493
Reader input(EMPTY_INPUT);
uint8_t tag;
Input value;
ASSERT_EQ(Result::ERROR_BAD_DER, ReadTagAndGetValue(input, tag, value));
494
495
}
496
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_TruncatedAfterTag)
497
{
498
499
static const uint8_t DER[] = { SEQUENCE };
Input buf(DER);
500
Reader input(buf);
501
502
503
uint8_t tag;
Input value;
ASSERT_EQ(Result::ERROR_BAD_DER, ReadTagAndGetValue(input, tag, value));
504
505
}
506
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_ValidEmpty)
507
{
508
Input buf(DER_SEQUENCE_EMPTY);
509
Reader input(buf);
510
511
512
513
514
515
uint8_t tag = 0;
Input value;
ASSERT_EQ(Success, ReadTagAndGetValue(input, tag, value));
ASSERT_EQ(SEQUENCE, tag);
ASSERT_EQ(0u, value.GetLength());
ASSERT_TRUE(input.AtEnd());
516
517
}
518
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_ValidNotEmpty)
519
{
520
Input buf(DER_SEQUENCE_NOT_EMPTY);
521
Reader input(buf);
522
523
524
525
526
527
uint8_t tag = 0;
Input value;
ASSERT_EQ(Success, ReadTagAndGetValue(input, tag, value));
ASSERT_EQ(SEQUENCE, tag);
Input expected(DER_SEQUENCE_NOT_EMPTY_VALUE);
ASSERT_TRUE(InputsAreEqual(expected, value));
528
529
530
ASSERT_TRUE(input.AtEnd());
}
531
532
TEST_F(pkixder_input_tests,
ReadTagAndGetValue_Input_InvalidNotEmptyValueTruncated)
533
{
534
Input buf(DER_SEQUENCE_NOT_EMPTY_VALUE_TRUNCATED);
535
Reader input(buf);
536
537
538
uint8_t tag;
Input value;
ASSERT_EQ(Result::ERROR_BAD_DER, ReadTagAndGetValue(input, tag, value));
539
540
}
541
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_InvalidWrongLength)
542
{
543
544
Input buf(DER_TRUNCATED_SEQUENCE_OF_INT8);
Reader input(buf);
545
546
uint8_t tag;
Input value;
547
ASSERT_EQ(Result::ERROR_BAD_DER,
548
ReadTagAndGetValue(input, tag, value));
549
550
}
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_InvalidHighTagNumberForm1)
{
// High tag number form is not allowed (illegal 1 byte tag)
//
// If the decoder treats 0x1F as a valid low tag number tag, then it will
// treat the actual tag (1) as a length, and then it will return Success
// with value == { 0x00 } and tag == 0x1f.
//
// It is illegal to encode tag 1 in the high tag number form because it isn't
// the shortest encoding (the low tag number form is).
static const uint8_t DER[] = {
0x1F, // high tag number form indicator
1, // tag 1 (not legal!)
0 // length zero
};
Input buf(DER);
Reader input(buf);
uint8_t tag;
Input value;
ASSERT_EQ(Result::ERROR_BAD_DER,
ReadTagAndGetValue(input, tag, value));
}
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_InvalidHighTagNumberForm2)
{
// High tag number form is not allowed (legal 1 byte tag).
//
// ReadTagAndGetValue's check to prohibit the high tag number form has no
// effect on whether this test passes or fails, because ReadTagAndGetValue
// will interpret the second byte (31) as a length, and the input doesn't
// have 31 bytes following it. This test is here to guard against the case
// where somebody actually implements high tag number form parsing, to remind
// that person that they need to add tests here, including in particular
// tests for overly-long encodings.
static const uint8_t DER[] = {
0x1F, // high tag number form indicator
31, // tag 31
0 // length zero
};
Input buf(DER);
Reader input(buf);
uint8_t tag;
Input value;
ASSERT_EQ(Result::ERROR_BAD_DER,
ReadTagAndGetValue(input, tag, value));
}
TEST_F(pkixder_input_tests, ReadTagAndGetValue_Input_InvalidHighTagNumberForm3)
{
// High tag number form is not allowed (2 byte legal tag)
//
// ReadTagAndGetValue's check to prohibit the high tag number form has no
// effect on whether this test passes or fails, because ReadTagAndGetValue
// will interpret the second byte as a length, and the input doesn't have
// that many bytes following it. This test is here to guard against the case
// where somebody actually implements high tag number form parsing, to remind
// that person that they need to add tests here, including in particular
// tests for overly-long encodings.
static const uint8_t DER[] = {
0x1F, // high tag number form indicator
0x80 | 0x01, 0x00, // tag 0x100 (256)
0 // length zero
};
Input buf(DER);
Reader input(buf);
uint8_t tag;
Input value;
ASSERT_EQ(Result::ERROR_BAD_DER,
ReadTagAndGetValue(input, tag, value));
}
622
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Reader_ValidEmpty)
623
{
624
625
626
Input buf(DER_SEQUENCE_EMPTY);
Reader input(buf);
Reader value;
627
628
629
630
631
ASSERT_EQ(Success, ExpectTagAndGetValue(input, SEQUENCE, value));
ASSERT_TRUE(value.AtEnd());
ASSERT_TRUE(input.AtEnd());
}
632
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Reader_ValidNotEmpty)
633
{
634
635
636
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
Reader value;
637
638
639
640
641
642
ASSERT_EQ(Success, ExpectTagAndGetValue(input, SEQUENCE, value));
ASSERT_TRUE(value.MatchRest(DER_SEQUENCE_NOT_EMPTY_VALUE));
ASSERT_TRUE(input.AtEnd());
}
TEST_F(pkixder_input_tests,
643
ExpectTagAndGetValue_Reader_InvalidNotEmptyValueTruncated)
644
{
645
646
647
Input buf(DER_SEQUENCE_NOT_EMPTY_VALUE_TRUNCATED);
Reader input(buf);
Reader value;
648
649
ASSERT_EQ(Result::ERROR_BAD_DER,
ExpectTagAndGetValue(input, SEQUENCE, value));
650
651
}
652
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Reader_InvalidWrongLength)
653
{
654
655
656
Input buf(DER_TRUNCATED_SEQUENCE_OF_INT8);
Reader input(buf);
Reader value;
657
658
ASSERT_EQ(Result::ERROR_BAD_DER,
ExpectTagAndGetValue(input, SEQUENCE, value));
659
660
}
661
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Reader_InvalidWrongTag)
662
{
663
664
665
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
Reader value;
666
667
ASSERT_EQ(Result::ERROR_BAD_DER,
ExpectTagAndGetValue(input, INTEGER, value));
668
669
}
670
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Input_ValidEmpty)
671
{
672
673
674
Input buf(DER_SEQUENCE_EMPTY);
Reader input(buf);
Input value;
675
ASSERT_EQ(Success, ExpectTagAndGetValue(input, SEQUENCE, value));
676
ASSERT_EQ(0u, value.GetLength());
677
678
679
ASSERT_TRUE(input.AtEnd());
}
680
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Input_ValidNotEmpty)
681
{
682
683
684
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
Input value;
685
ASSERT_EQ(Success, ExpectTagAndGetValue(input, SEQUENCE, value));
686
687
Input expected(DER_SEQUENCE_NOT_EMPTY_VALUE);
ASSERT_TRUE(InputsAreEqual(expected, value));
688
689
690
691
ASSERT_TRUE(input.AtEnd());
}
TEST_F(pkixder_input_tests,
692
ExpectTagAndGetValue_Input_InvalidNotEmptyValueTruncated)
693
{
694
695
696
Input buf(DER_SEQUENCE_NOT_EMPTY_VALUE_TRUNCATED);
Reader input(buf);
Input value;
697
698
ASSERT_EQ(Result::ERROR_BAD_DER,
ExpectTagAndGetValue(input, SEQUENCE, value));
699
700
}
701
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Input_InvalidWrongLength)
702
{
703
704
705
Input buf(DER_TRUNCATED_SEQUENCE_OF_INT8);
Reader input(buf);
Input value;
706
707
ASSERT_EQ(Result::ERROR_BAD_DER,
ExpectTagAndGetValue(input, SEQUENCE, value));
708
709
}
710
TEST_F(pkixder_input_tests, ExpectTagAndGetValue_Input_InvalidWrongTag)
711
{
712
713
714
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
Input value;
715
716
ASSERT_EQ(Result::ERROR_BAD_DER,
ExpectTagAndGetValue(input, INTEGER, value));
717
718
}
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
TEST_F(pkixder_input_tests, ExpectTagAndEmptyValue_ValidEmpty)
{
Input buf(DER_SEQUENCE_EMPTY);
Reader input(buf);
ASSERT_EQ(Success, ExpectTagAndEmptyValue(input, SEQUENCE));
ASSERT_TRUE(input.AtEnd());
}
TEST_F(pkixder_input_tests, ExpectTagAndEmptyValue_InValidNotEmpty)
{
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndEmptyValue(input, SEQUENCE));
}
TEST_F(pkixder_input_tests,
ExpectTagAndEmptyValue_Input_InvalidNotEmptyValueTruncated)
{
Input buf(DER_SEQUENCE_NOT_EMPTY_VALUE_TRUNCATED);
Reader input(buf);
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndEmptyValue(input, SEQUENCE));
}
TEST_F(pkixder_input_tests, ExpectTagAndEmptyValue_InvalidWrongLength)
{
Input buf(DER_TRUNCATED_SEQUENCE_OF_INT8);
Reader input(buf);
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndEmptyValue(input, SEQUENCE));
}
TEST_F(pkixder_input_tests, ExpectTagAndEmptyValue_InvalidWrongTag)
{
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndEmptyValue(input, INTEGER));
}
756
TEST_F(pkixder_input_tests, ExpectTagAndGetTLV_Input_ValidEmpty)
757
{
758
759
760
Input buf(DER_SEQUENCE_EMPTY);
Reader input(buf);
Input tlv;
761
ASSERT_EQ(Success, ExpectTagAndGetTLV(input, SEQUENCE, tlv));
762
763
Input expected(DER_SEQUENCE_EMPTY);
ASSERT_TRUE(InputsAreEqual(expected, tlv));
764
765
766
ASSERT_TRUE(input.AtEnd());
}
767
TEST_F(pkixder_input_tests, ExpectTagAndGetTLV_Input_ValidNotEmpty)
768
{
769
770
771
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
Input tlv;
772
ASSERT_EQ(Success, ExpectTagAndGetTLV(input, SEQUENCE, tlv));
773
774
Input expected(DER_SEQUENCE_NOT_EMPTY);
ASSERT_TRUE(InputsAreEqual(expected, tlv));
775
776
777
778
ASSERT_TRUE(input.AtEnd());
}
TEST_F(pkixder_input_tests,
779
ExpectTagAndGetTLV_Input_InvalidNotEmptyValueTruncated)
780
{
781
782
783
Input buf(DER_SEQUENCE_NOT_EMPTY_VALUE_TRUNCATED);
Reader input(buf);
Input tlv;
784
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndGetTLV(input, SEQUENCE, tlv));
785
786
}
787
TEST_F(pkixder_input_tests, ExpectTagAndGetTLV_Input_InvalidWrongLength)
788
{
789
790
791
Input buf(DER_TRUNCATED_SEQUENCE_OF_INT8);
Reader input(buf);
Input tlv;
792
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndGetTLV(input, SEQUENCE, tlv));
793
794
}
795
TEST_F(pkixder_input_tests, ExpectTagAndGetTLV_Input_InvalidWrongTag)
796
{
797
798
799
Input buf(DER_SEQUENCE_NOT_EMPTY);
Reader input(buf);
Input tlv;
800
ASSERT_EQ(Result::ERROR_BAD_DER, ExpectTagAndGetTLV(input, INTEGER, tlv));
801
802
}
803
804
TEST_F(pkixder_input_tests, EndAtEnd)
{
805
806
Input buf(DER_INT16);
Reader input(buf);
807
808
809
810
811
812
ASSERT_EQ(Success, input.Skip(4));
ASSERT_EQ(Success, End(input));
}
TEST_F(pkixder_input_tests, EndBeforeEnd)
{
813
814
Input buf(DER_INT16);
Reader input(buf);
815
ASSERT_EQ(Success, input.Skip(2));
816
ASSERT_EQ(Result::ERROR_BAD_DER, End(input));
817
818
819
820
}
TEST_F(pkixder_input_tests, EndAtBeginning)
{
821
822
Input buf(DER_INT16);
Reader input(buf);
823
ASSERT_EQ(Result::ERROR_BAD_DER, End(input));
824
825
826
827
}
// TODO: Need tests for Nested too?
828
Result NestedOfHelper(Reader& input, std::vector<uint8_t>& readValues)
829
830
{
uint8_t value = 0;
831
832
833
834
Result rv = input.Read(value);
EXPECT_EQ(Success, rv);
if (rv != Success) {
return rv;
835
836
837
838
839
840
841
}
readValues.push_back(value);
return Success;
}
TEST_F(pkixder_input_tests, NestedOf)
{
842
843
Input buf(DER_SEQUENCE_OF_INT8);
Reader input(buf);
844
845
846
std::vector<uint8_t> readValues;
ASSERT_EQ(Success,
847
848
849
850
NestedOf(input, SEQUENCE, INTEGER, EmptyAllowed::No,
[&readValues](Reader& r) {
return NestedOfHelper(r, readValues);
}));
851
ASSERT_EQ(3u, readValues.size());
852
853
854
855
856
857
858
859
ASSERT_EQ(0x01, readValues[0]);
ASSERT_EQ(0x02, readValues[1]);
ASSERT_EQ(0x03, readValues[2]);
ASSERT_EQ(Success, End(input));
}
TEST_F(pkixder_input_tests, NestedOfWithTruncatedData)
{
860
861
Input buf(DER_TRUNCATED_SEQUENCE_OF_INT8);
Reader input(buf);
862
863
std::vector<uint8_t> readValues;
864
865
ASSERT_EQ(Result::ERROR_BAD_DER,
NestedOf(input, SEQUENCE, INTEGER, EmptyAllowed::No,
866
867
868
[&readValues](Reader& r) {
return NestedOfHelper(r, readValues);
}));
869
ASSERT_EQ(0u, readValues.size());
870
}
871
872
TEST_F(pkixder_input_tests, MatchRestAtEnd)
873
874
{
static const uint8_t der[1] = { };
875
Input buf;
876
ASSERT_EQ(Success, buf.Init(der, 0));
877
Reader input(buf);
878
879
ASSERT_TRUE(input.AtEnd());
static const uint8_t toMatch[] = { 1 };
880
ASSERT_FALSE(input.MatchRest(toMatch));
881
882
}
883
TEST_F(pkixder_input_tests, MatchRest1Match)
884
885
{
static const uint8_t der[] = { 1 };
886
887
Input buf(der);
Reader input(buf);
888
ASSERT_FALSE(input.AtEnd());
889
ASSERT_TRUE(input.MatchRest(der));
890
891
}
892
TEST_F(pkixder_input_tests, MatchRest1Mismatch)
893
894
{
static const uint8_t der[] = { 1 };
895
896
Input buf(der);
Reader input(buf);
897
static const uint8_t toMatch[] = { 2 };
898
ASSERT_FALSE(input.MatchRest(toMatch));
899
900
901
ASSERT_FALSE(input.AtEnd());
}
902
TEST_F(pkixder_input_tests, MatchRest2WithTrailingByte)
903
904
{
static const uint8_t der[] = { 1, 2, 3 };
905
906
Input buf(der);
Reader input(buf);
907
static const uint8_t toMatch[] = { 1, 2 };
908
ASSERT_FALSE(input.MatchRest(toMatch));
909
910
}
911
TEST_F(pkixder_input_tests, MatchRest2Mismatch)
912
913
{
static const uint8_t der[] = { 1, 2, 3 };
914
915
Input buf(der);
Reader input(buf);
916
static const uint8_t toMatchMismatch[] = { 1, 3 };
917
918
ASSERT_FALSE(input.MatchRest(toMatchMismatch));
ASSERT_TRUE(input.MatchRest(der));
919
920
}
921
} // namespace