security/pkix/test/gtest/pkixder_input_tests.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial