|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 /* Copyright 2013 Mozilla Foundation |
|
4 * |
|
5 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 * you may not use this file except in compliance with the License. |
|
7 * You may obtain a copy of the License at |
|
8 * |
|
9 * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 * |
|
11 * Unless required by applicable law or agreed to in writing, software |
|
12 * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 * See the License for the specific language governing permissions and |
|
15 * limitations under the License. |
|
16 */ |
|
17 |
|
18 #include <functional> |
|
19 #include <vector> |
|
20 #include <gtest/gtest.h> |
|
21 |
|
22 #include "pkix/bind.h" |
|
23 #include "pkixder.h" |
|
24 |
|
25 using namespace mozilla::pkix::der; |
|
26 |
|
27 namespace { |
|
28 |
|
29 class pkixder_input_tests : public ::testing::Test |
|
30 { |
|
31 protected: |
|
32 virtual void SetUp() |
|
33 { |
|
34 PR_SetError(0, 0); |
|
35 } |
|
36 }; |
|
37 |
|
38 const uint8_t DER_SEQUENCE_OF_INT8[] = { |
|
39 0x30, // SEQUENCE |
|
40 0x09, // length |
|
41 0x02, 0x01, 0x01, // INTEGER length 1 value 0x01 |
|
42 0x02, 0x01, 0x02, // INTEGER length 1 value 0x02 |
|
43 0x02, 0x01, 0x03 // INTEGER length 1 value 0x03 |
|
44 }; |
|
45 |
|
46 const uint8_t DER_TRUNCATED_SEQUENCE_OF_INT8[] = { |
|
47 0x30, // SEQUENCE |
|
48 0x09, // length |
|
49 0x02, 0x01, 0x01, // INTEGER length 1 value 0x01 |
|
50 0x02, 0x01, 0x02 // INTEGER length 1 value 0x02 |
|
51 // MISSING DATA HERE ON PURPOSE |
|
52 }; |
|
53 |
|
54 const uint8_t DER_OVERRUN_SEQUENCE_OF_INT8[] = { |
|
55 0x30, // SEQUENCE |
|
56 0x09, // length |
|
57 0x02, 0x01, 0x01, // INTEGER length 1 value 0x01 |
|
58 0x02, 0x01, 0x02, // INTEGER length 1 value 0x02 |
|
59 0x02, 0x02, 0xFF, 0x03 // INTEGER length 2 value 0xFF03 |
|
60 }; |
|
61 |
|
62 const uint8_t DER_INT16[] = { |
|
63 0x02, // INTEGER |
|
64 0x02, // length |
|
65 0x12, 0x34 // 0x1234 |
|
66 }; |
|
67 |
|
68 TEST_F(pkixder_input_tests, FailWithError) |
|
69 { |
|
70 ASSERT_EQ(Failure, Fail(SEC_ERROR_BAD_DER)); |
|
71 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
72 |
|
73 ASSERT_EQ(Failure, Fail(SEC_ERROR_INVALID_ARGS)); |
|
74 ASSERT_EQ(SEC_ERROR_INVALID_ARGS, PR_GetError()); |
|
75 } |
|
76 |
|
77 TEST_F(pkixder_input_tests, InputInit) |
|
78 { |
|
79 Input input; |
|
80 ASSERT_EQ(Success, |
|
81 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
82 } |
|
83 |
|
84 TEST_F(pkixder_input_tests, InputInitWithNullPointerOrZeroLength) |
|
85 { |
|
86 Input input; |
|
87 ASSERT_EQ(Failure, input.Init(nullptr, 0)); |
|
88 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
89 |
|
90 ASSERT_EQ(Failure, input.Init(nullptr, 100)); |
|
91 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
92 |
|
93 // Is this a bug? |
|
94 ASSERT_EQ(Success, input.Init((const uint8_t*) "hello", 0)); |
|
95 } |
|
96 |
|
97 TEST_F(pkixder_input_tests, InputInitWithLargeData) |
|
98 { |
|
99 Input input; |
|
100 // Data argument length does not matter, it is not touched, just |
|
101 // needs to be non-null |
|
102 ASSERT_EQ(Failure, input.Init((const uint8_t*) "", 0xffff+1)); |
|
103 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
104 |
|
105 ASSERT_EQ(Success, input.Init((const uint8_t*) "", 0xffff)); |
|
106 } |
|
107 |
|
108 TEST_F(pkixder_input_tests, InputInitMultipleTimes) |
|
109 { |
|
110 Input input; |
|
111 |
|
112 ASSERT_EQ(Success, |
|
113 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
114 |
|
115 ASSERT_EQ(Failure, |
|
116 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
117 ASSERT_EQ(SEC_ERROR_INVALID_ARGS, PR_GetError()); |
|
118 } |
|
119 |
|
120 TEST_F(pkixder_input_tests, ExpectSuccess) |
|
121 { |
|
122 Input input; |
|
123 |
|
124 ASSERT_EQ(Success, |
|
125 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
126 ASSERT_EQ(Success, |
|
127 input.Expect(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
128 ASSERT_TRUE(input.AtEnd()); |
|
129 } |
|
130 |
|
131 TEST_F(pkixder_input_tests, ExpectMismatch) |
|
132 { |
|
133 Input input; |
|
134 |
|
135 ASSERT_EQ(Success, |
|
136 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
137 |
|
138 const uint8_t expected[] = { 0x11, 0x22 }; |
|
139 ASSERT_EQ(Failure, input.Expect(expected, sizeof expected)); |
|
140 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
141 } |
|
142 |
|
143 TEST_F(pkixder_input_tests, ExpectTooMuch) |
|
144 { |
|
145 Input input; |
|
146 |
|
147 const uint8_t der[] = { 0x11, 0x22 }; |
|
148 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
149 |
|
150 const uint8_t expected[] = { 0x11, 0x22, 0x33 }; |
|
151 ASSERT_EQ(Failure, input.Expect(expected, sizeof expected)); |
|
152 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
153 } |
|
154 |
|
155 TEST_F(pkixder_input_tests, PeekWithinBounds) |
|
156 { |
|
157 Input input; |
|
158 const uint8_t der[] = { 0x11, 0x11 }; |
|
159 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
160 ASSERT_TRUE(input.Peek(0x11)); |
|
161 ASSERT_FALSE(input.Peek(0x22)); |
|
162 } |
|
163 |
|
164 TEST_F(pkixder_input_tests, PeekPastBounds) |
|
165 { |
|
166 Input input; |
|
167 const uint8_t der[] = { 0x11, 0x22 }; |
|
168 ASSERT_EQ(Success, input.Init(der, 1)); |
|
169 |
|
170 uint8_t readByte; |
|
171 ASSERT_EQ(Success, input.Read(readByte)); |
|
172 ASSERT_EQ(0x11, readByte); |
|
173 ASSERT_FALSE(input.Peek(0x22)); |
|
174 } |
|
175 |
|
176 TEST_F(pkixder_input_tests, ReadByte) |
|
177 { |
|
178 Input input; |
|
179 const uint8_t der[] = { 0x11, 0x22 }; |
|
180 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
181 |
|
182 uint8_t readByte1; |
|
183 ASSERT_EQ(Success, input.Read(readByte1)); |
|
184 ASSERT_EQ(0x11, readByte1); |
|
185 |
|
186 uint8_t readByte2; |
|
187 ASSERT_EQ(Success, input.Read(readByte2)); |
|
188 ASSERT_EQ(0x22, readByte2); |
|
189 } |
|
190 |
|
191 TEST_F(pkixder_input_tests, ReadBytePastEnd) |
|
192 { |
|
193 Input input; |
|
194 const uint8_t der[] = { 0x11, 0x22 }; |
|
195 // Initialize with too-short length |
|
196 ASSERT_EQ(Success, input.Init(der, 1)); |
|
197 |
|
198 uint8_t readByte1 = 0; |
|
199 ASSERT_EQ(Success, input.Read(readByte1)); |
|
200 ASSERT_EQ(0x11, readByte1); |
|
201 |
|
202 uint8_t readByte2 = 0; |
|
203 ASSERT_EQ(Failure, input.Read(readByte2)); |
|
204 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
205 ASSERT_NE(0x22, readByte2); |
|
206 } |
|
207 |
|
208 TEST_F(pkixder_input_tests, ReadByteWrapAroundPointer) |
|
209 { |
|
210 // The original implementation of our buffer read overflow checks was |
|
211 // susceptible to integer overflows which could make the checks ineffective. |
|
212 // This attempts to verify that we've fixed that. Unfortunately, decrementing |
|
213 // a null pointer is undefined behavior according to the C++ language spec., |
|
214 // but this should catch the problem on at least some compilers, if not all of |
|
215 // them. |
|
216 const uint8_t* der = nullptr; |
|
217 --der; |
|
218 Input input; |
|
219 ASSERT_EQ(Success, input.Init(der, 0)); |
|
220 uint8_t b; |
|
221 ASSERT_EQ(Failure, input.Read(b)); |
|
222 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
223 } |
|
224 |
|
225 TEST_F(pkixder_input_tests, ReadWord) |
|
226 { |
|
227 Input input; |
|
228 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
229 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
230 |
|
231 uint16_t readWord1 = 0; |
|
232 ASSERT_EQ(Success, input.Read(readWord1)); |
|
233 ASSERT_EQ(0x1122, readWord1); |
|
234 |
|
235 uint16_t readWord2 = 0; |
|
236 ASSERT_EQ(Success, input.Read(readWord2)); |
|
237 ASSERT_EQ(0x3344, readWord2); |
|
238 } |
|
239 |
|
240 TEST_F(pkixder_input_tests, ReadWordPastEnd) |
|
241 { |
|
242 Input input; |
|
243 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
244 // Initialize with too-short length |
|
245 ASSERT_EQ(Success, input.Init(der, 2)); |
|
246 |
|
247 uint16_t readWord1 = 0; |
|
248 ASSERT_EQ(Success, input.Read(readWord1)); |
|
249 ASSERT_EQ(0x1122, readWord1); |
|
250 |
|
251 uint16_t readWord2 = 0; |
|
252 ASSERT_EQ(Failure, input.Read(readWord2)); |
|
253 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
254 ASSERT_NE(0x3344, readWord2); |
|
255 } |
|
256 |
|
257 TEST_F(pkixder_input_tests, ReadWordWithInsufficentData) |
|
258 { |
|
259 Input input; |
|
260 const uint8_t der[] = { 0x11, 0x22 }; |
|
261 ASSERT_EQ(Success, input.Init(der, 1)); |
|
262 |
|
263 uint16_t readWord1 = 0; |
|
264 ASSERT_EQ(Failure, input.Read(readWord1)); |
|
265 ASSERT_NE(0x1122, readWord1); |
|
266 } |
|
267 |
|
268 TEST_F(pkixder_input_tests, ReadWordWrapAroundPointer) |
|
269 { |
|
270 // The original implementation of our buffer read overflow checks was |
|
271 // susceptible to integer overflows which could make the checks ineffective. |
|
272 // This attempts to verify that we've fixed that. Unfortunately, decrementing |
|
273 // a null pointer is undefined behavior according to the C++ language spec., |
|
274 // but this should catch the problem on at least some compilers, if not all of |
|
275 // them. |
|
276 const uint8_t* der = nullptr; |
|
277 --der; |
|
278 Input input; |
|
279 ASSERT_EQ(Success, input.Init(der, 0)); |
|
280 uint16_t b; |
|
281 ASSERT_EQ(Failure, input.Read(b)); |
|
282 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
283 } |
|
284 |
|
285 TEST_F(pkixder_input_tests, InputSkip) |
|
286 { |
|
287 Input input; |
|
288 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
289 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
290 |
|
291 ASSERT_EQ(Success, input.Skip(1)); |
|
292 |
|
293 uint8_t readByte1 = 0; |
|
294 ASSERT_EQ(Success, input.Read(readByte1)); |
|
295 ASSERT_EQ(0x22, readByte1); |
|
296 |
|
297 ASSERT_EQ(Success, input.Skip(1)); |
|
298 |
|
299 uint8_t readByte2 = 0; |
|
300 ASSERT_EQ(Success, input.Read(readByte2)); |
|
301 ASSERT_EQ(0x44, readByte2); |
|
302 } |
|
303 |
|
304 TEST_F(pkixder_input_tests, InputSkipToEnd) |
|
305 { |
|
306 Input input; |
|
307 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
308 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
309 ASSERT_EQ(Success, input.Skip(sizeof der)); |
|
310 ASSERT_TRUE(input.AtEnd()); |
|
311 } |
|
312 |
|
313 TEST_F(pkixder_input_tests, InputSkipPastEnd) |
|
314 { |
|
315 Input input; |
|
316 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
317 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
318 |
|
319 ASSERT_EQ(Failure, input.Skip(sizeof der + 1)); |
|
320 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
321 } |
|
322 |
|
323 TEST_F(pkixder_input_tests, InputSkipToNewInput) |
|
324 { |
|
325 Input input; |
|
326 const uint8_t der[] = { 0x01, 0x02, 0x03, 0x04 }; |
|
327 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
328 |
|
329 Input skippedInput; |
|
330 ASSERT_EQ(Success, input.Skip(3, skippedInput)); |
|
331 |
|
332 uint8_t readByte1 = 0; |
|
333 ASSERT_EQ(Success, input.Read(readByte1)); |
|
334 ASSERT_EQ(0x04, readByte1); |
|
335 |
|
336 ASSERT_TRUE(input.AtEnd()); |
|
337 |
|
338 // Input has no Remaining() or Length() so we simply read the bytes |
|
339 // and then expect to be at the end. |
|
340 |
|
341 for (uint8_t i = 1; i <= 3; ++i) { |
|
342 uint8_t readByte = 0; |
|
343 ASSERT_EQ(Success, skippedInput.Read(readByte)); |
|
344 ASSERT_EQ(i, readByte); |
|
345 } |
|
346 |
|
347 ASSERT_TRUE(skippedInput.AtEnd()); |
|
348 } |
|
349 |
|
350 TEST_F(pkixder_input_tests, InputSkipToNewInputPastEnd) |
|
351 { |
|
352 Input input; |
|
353 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
354 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
355 |
|
356 Input skippedInput; |
|
357 ASSERT_EQ(Failure, input.Skip(sizeof der * 2, skippedInput)); |
|
358 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
359 } |
|
360 |
|
361 TEST_F(pkixder_input_tests, InputSkipToSECItem) |
|
362 { |
|
363 Input input; |
|
364 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
365 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
366 |
|
367 const uint8_t expectedItemData[] = { 0x11, 0x22, 0x33 }; |
|
368 |
|
369 SECItem item; |
|
370 ASSERT_EQ(Success, input.Skip(sizeof expectedItemData, item)); |
|
371 ASSERT_EQ(siBuffer, item.type); |
|
372 ASSERT_EQ(sizeof expectedItemData, item.len); |
|
373 ASSERT_EQ(der, item.data); |
|
374 ASSERT_EQ(0, memcmp(item.data, expectedItemData, sizeof expectedItemData)); |
|
375 } |
|
376 |
|
377 TEST_F(pkixder_input_tests, SkipWrapAroundPointer) |
|
378 { |
|
379 // The original implementation of our buffer read overflow checks was |
|
380 // susceptible to integer overflows which could make the checks ineffective. |
|
381 // This attempts to verify that we've fixed that. Unfortunately, decrementing |
|
382 // a null pointer is undefined behavior according to the C++ language spec., |
|
383 // but this should catch the problem on at least some compilers, if not all of |
|
384 // them. |
|
385 const uint8_t* der = nullptr; |
|
386 --der; |
|
387 Input input; |
|
388 ASSERT_EQ(Success, input.Init(der, 0)); |
|
389 ASSERT_EQ(Failure, input.Skip(1)); |
|
390 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
391 } |
|
392 |
|
393 TEST_F(pkixder_input_tests, SkipToSECItemPastEnd) |
|
394 { |
|
395 Input input; |
|
396 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
397 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
398 |
|
399 SECItem skippedSECItem; |
|
400 ASSERT_EQ(Failure, input.Skip(sizeof der + 1, skippedSECItem)); |
|
401 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
402 } |
|
403 |
|
404 TEST_F(pkixder_input_tests, Skip) |
|
405 { |
|
406 Input input; |
|
407 ASSERT_EQ(Success, |
|
408 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
409 |
|
410 ASSERT_EQ(Success, Skip(input, SEQUENCE)); |
|
411 ASSERT_EQ(Success, End(input)); |
|
412 } |
|
413 |
|
414 TEST_F(pkixder_input_tests, SkipWithTruncatedData) |
|
415 { |
|
416 Input input; |
|
417 ASSERT_EQ(Success, input.Init(DER_TRUNCATED_SEQUENCE_OF_INT8, |
|
418 sizeof DER_TRUNCATED_SEQUENCE_OF_INT8)); |
|
419 |
|
420 ASSERT_EQ(Failure, Skip(input, SEQUENCE)); |
|
421 } |
|
422 |
|
423 TEST_F(pkixder_input_tests, SkipWithOverrunData) |
|
424 { |
|
425 Input input; |
|
426 ASSERT_EQ(Success, input.Init(DER_OVERRUN_SEQUENCE_OF_INT8, |
|
427 sizeof DER_OVERRUN_SEQUENCE_OF_INT8)); |
|
428 ASSERT_EQ(Success, Skip(input, SEQUENCE)); |
|
429 ASSERT_EQ(Failure, End(input)); |
|
430 } |
|
431 |
|
432 TEST_F(pkixder_input_tests, AtEndOnUnInitializedInput) |
|
433 { |
|
434 Input input; |
|
435 ASSERT_TRUE(input.AtEnd()); |
|
436 } |
|
437 |
|
438 TEST_F(pkixder_input_tests, AtEndAtBeginning) |
|
439 { |
|
440 Input input; |
|
441 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
442 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
443 ASSERT_FALSE(input.AtEnd()); |
|
444 } |
|
445 |
|
446 TEST_F(pkixder_input_tests, AtEndAtEnd) |
|
447 { |
|
448 Input input; |
|
449 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
450 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
451 ASSERT_EQ(Success, input.Skip(sizeof der)); |
|
452 ASSERT_TRUE(input.AtEnd()); |
|
453 } |
|
454 |
|
455 TEST_F(pkixder_input_tests, MarkAndGetSECItem) |
|
456 { |
|
457 Input input; |
|
458 const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; |
|
459 ASSERT_EQ(Success, input.Init(der, sizeof der)); |
|
460 |
|
461 Input::Mark mark = input.GetMark(); |
|
462 |
|
463 const uint8_t expectedItemData[] = { 0x11, 0x22, 0x33 }; |
|
464 |
|
465 ASSERT_EQ(Success, input.Skip(sizeof expectedItemData)); |
|
466 |
|
467 SECItem item; |
|
468 memset(&item, 0x00, sizeof item); |
|
469 |
|
470 ASSERT_TRUE(input.GetSECItem(siBuffer, mark, item)); |
|
471 ASSERT_EQ(siBuffer, item.type); |
|
472 ASSERT_EQ(sizeof expectedItemData, item.len); |
|
473 ASSERT_TRUE(item.data); |
|
474 ASSERT_EQ(0, memcmp(item.data, expectedItemData, sizeof expectedItemData)); |
|
475 } |
|
476 |
|
477 TEST_F(pkixder_input_tests, ExpectTagAndLength) |
|
478 { |
|
479 Input input; |
|
480 ASSERT_EQ(Success, |
|
481 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
482 |
|
483 ASSERT_EQ(Success, ExpectTagAndLength(input, SEQUENCE, |
|
484 sizeof DER_SEQUENCE_OF_INT8 - 2)); |
|
485 } |
|
486 |
|
487 TEST_F(pkixder_input_tests, ExpectTagAndLengthWithWrongLength) |
|
488 { |
|
489 Input input; |
|
490 ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); |
|
491 |
|
492 // Wrong length |
|
493 ASSERT_EQ(Failure, ExpectTagAndLength(input, INTEGER, 4)); |
|
494 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
495 } |
|
496 |
|
497 TEST_F(pkixder_input_tests, ExpectTagAndLengthWithWrongTag) |
|
498 { |
|
499 Input input; |
|
500 ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); |
|
501 |
|
502 // Wrong type |
|
503 ASSERT_EQ(Failure, ExpectTagAndLength(input, OCTET_STRING, 2)); |
|
504 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
505 } |
|
506 |
|
507 TEST_F(pkixder_input_tests, ExpectTagAndGetLength) |
|
508 { |
|
509 Input input; |
|
510 ASSERT_EQ(Success, |
|
511 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
512 |
|
513 uint16_t length = 0; |
|
514 ASSERT_EQ(Success, ExpectTagAndGetLength(input, SEQUENCE, length)); |
|
515 ASSERT_EQ(sizeof DER_SEQUENCE_OF_INT8 - 2, length); |
|
516 ASSERT_EQ(Success, input.Skip(length)); |
|
517 ASSERT_TRUE(input.AtEnd()); |
|
518 } |
|
519 |
|
520 TEST_F(pkixder_input_tests, ExpectTagAndGetLengthWithWrongTag) |
|
521 { |
|
522 Input input; |
|
523 ASSERT_EQ(Success, |
|
524 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
525 |
|
526 uint16_t length = 0; |
|
527 ASSERT_EQ(Failure, ExpectTagAndGetLength(input, INTEGER, length)); |
|
528 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
529 } |
|
530 |
|
531 TEST_F(pkixder_input_tests, ExpectTagAndGetLengthWithWrongLength) |
|
532 { |
|
533 Input input; |
|
534 ASSERT_EQ(Success, input.Init(DER_TRUNCATED_SEQUENCE_OF_INT8, |
|
535 sizeof DER_TRUNCATED_SEQUENCE_OF_INT8)); |
|
536 |
|
537 uint16_t length = 0; |
|
538 ASSERT_EQ(Failure, ExpectTagAndGetLength(input, SEQUENCE, length)); |
|
539 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
540 } |
|
541 |
|
542 TEST_F(pkixder_input_tests, ExpectTagAndIgnoreLength) |
|
543 { |
|
544 Input input; |
|
545 ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); |
|
546 ASSERT_EQ(Success, ExpectTagAndIgnoreLength(input, INTEGER)); |
|
547 } |
|
548 |
|
549 TEST_F(pkixder_input_tests, ExpectTagAndIgnoreLengthWithWrongTag) |
|
550 { |
|
551 Input input; |
|
552 ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); |
|
553 |
|
554 ASSERT_EQ(Failure, ExpectTagAndIgnoreLength(input, OCTET_STRING)); |
|
555 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
556 } |
|
557 |
|
558 TEST_F(pkixder_input_tests, EndAtEnd) |
|
559 { |
|
560 Input input; |
|
561 ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); |
|
562 ASSERT_EQ(Success, input.Skip(4)); |
|
563 ASSERT_EQ(Success, End(input)); |
|
564 } |
|
565 |
|
566 TEST_F(pkixder_input_tests, EndBeforeEnd) |
|
567 { |
|
568 Input input; |
|
569 ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); |
|
570 ASSERT_EQ(Success, input.Skip(2)); |
|
571 ASSERT_EQ(Failure, End(input)); |
|
572 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
573 } |
|
574 |
|
575 TEST_F(pkixder_input_tests, EndAtBeginning) |
|
576 { |
|
577 Input input; |
|
578 ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); |
|
579 ASSERT_EQ(Failure, End(input)); |
|
580 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
581 } |
|
582 |
|
583 // TODO: Need tests for Nested too? |
|
584 |
|
585 Result NestedOfHelper(Input& input, std::vector<uint8_t>& readValues) |
|
586 { |
|
587 uint8_t value = 0; |
|
588 if (input.Read(value) != Success) { |
|
589 return Failure; |
|
590 } |
|
591 readValues.push_back(value); |
|
592 return Success; |
|
593 } |
|
594 |
|
595 TEST_F(pkixder_input_tests, NestedOf) |
|
596 { |
|
597 Input input; |
|
598 ASSERT_EQ(Success, |
|
599 input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); |
|
600 |
|
601 std::vector<uint8_t> readValues; |
|
602 ASSERT_EQ(Success, |
|
603 NestedOf(input, SEQUENCE, INTEGER, MustNotBeEmpty, |
|
604 mozilla::pkix::bind(NestedOfHelper, mozilla::pkix::_1, |
|
605 mozilla::pkix::ref(readValues)))); |
|
606 ASSERT_EQ((size_t) 3, readValues.size()); |
|
607 ASSERT_EQ(0x01, readValues[0]); |
|
608 ASSERT_EQ(0x02, readValues[1]); |
|
609 ASSERT_EQ(0x03, readValues[2]); |
|
610 ASSERT_EQ(Success, End(input)); |
|
611 } |
|
612 |
|
613 TEST_F(pkixder_input_tests, NestedOfWithTruncatedData) |
|
614 { |
|
615 Input input; |
|
616 ASSERT_EQ(Success, input.Init(DER_TRUNCATED_SEQUENCE_OF_INT8, |
|
617 sizeof DER_TRUNCATED_SEQUENCE_OF_INT8)); |
|
618 |
|
619 std::vector<uint8_t> readValues; |
|
620 ASSERT_EQ(Failure, |
|
621 NestedOf(input, SEQUENCE, INTEGER, MustNotBeEmpty, |
|
622 mozilla::pkix::bind(NestedOfHelper, mozilla::pkix::_1, |
|
623 mozilla::pkix::ref(readValues)))); |
|
624 ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); |
|
625 ASSERT_EQ((size_t) 0, readValues.size()); |
|
626 } |
|
627 } // unnamed namespace |