1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/pkix/test/gtest/pkixder_input_tests.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,627 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* Copyright 2013 Mozilla Foundation 1.7 + * 1.8 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.9 + * you may not use this file except in compliance with the License. 1.10 + * You may obtain a copy of the License at 1.11 + * 1.12 + * http://www.apache.org/licenses/LICENSE-2.0 1.13 + * 1.14 + * Unless required by applicable law or agreed to in writing, software 1.15 + * distributed under the License is distributed on an "AS IS" BASIS, 1.16 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.17 + * See the License for the specific language governing permissions and 1.18 + * limitations under the License. 1.19 + */ 1.20 + 1.21 +#include <functional> 1.22 +#include <vector> 1.23 +#include <gtest/gtest.h> 1.24 + 1.25 +#include "pkix/bind.h" 1.26 +#include "pkixder.h" 1.27 + 1.28 +using namespace mozilla::pkix::der; 1.29 + 1.30 +namespace { 1.31 + 1.32 +class pkixder_input_tests : public ::testing::Test 1.33 +{ 1.34 +protected: 1.35 + virtual void SetUp() 1.36 + { 1.37 + PR_SetError(0, 0); 1.38 + } 1.39 +}; 1.40 + 1.41 +const uint8_t DER_SEQUENCE_OF_INT8[] = { 1.42 + 0x30, // SEQUENCE 1.43 + 0x09, // length 1.44 + 0x02, 0x01, 0x01, // INTEGER length 1 value 0x01 1.45 + 0x02, 0x01, 0x02, // INTEGER length 1 value 0x02 1.46 + 0x02, 0x01, 0x03 // INTEGER length 1 value 0x03 1.47 +}; 1.48 + 1.49 +const uint8_t DER_TRUNCATED_SEQUENCE_OF_INT8[] = { 1.50 + 0x30, // SEQUENCE 1.51 + 0x09, // length 1.52 + 0x02, 0x01, 0x01, // INTEGER length 1 value 0x01 1.53 + 0x02, 0x01, 0x02 // INTEGER length 1 value 0x02 1.54 + // MISSING DATA HERE ON PURPOSE 1.55 +}; 1.56 + 1.57 +const uint8_t DER_OVERRUN_SEQUENCE_OF_INT8[] = { 1.58 + 0x30, // SEQUENCE 1.59 + 0x09, // length 1.60 + 0x02, 0x01, 0x01, // INTEGER length 1 value 0x01 1.61 + 0x02, 0x01, 0x02, // INTEGER length 1 value 0x02 1.62 + 0x02, 0x02, 0xFF, 0x03 // INTEGER length 2 value 0xFF03 1.63 +}; 1.64 + 1.65 +const uint8_t DER_INT16[] = { 1.66 + 0x02, // INTEGER 1.67 + 0x02, // length 1.68 + 0x12, 0x34 // 0x1234 1.69 +}; 1.70 + 1.71 +TEST_F(pkixder_input_tests, FailWithError) 1.72 +{ 1.73 + ASSERT_EQ(Failure, Fail(SEC_ERROR_BAD_DER)); 1.74 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.75 + 1.76 + ASSERT_EQ(Failure, Fail(SEC_ERROR_INVALID_ARGS)); 1.77 + ASSERT_EQ(SEC_ERROR_INVALID_ARGS, PR_GetError()); 1.78 +} 1.79 + 1.80 +TEST_F(pkixder_input_tests, InputInit) 1.81 +{ 1.82 + Input input; 1.83 + ASSERT_EQ(Success, 1.84 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.85 +} 1.86 + 1.87 +TEST_F(pkixder_input_tests, InputInitWithNullPointerOrZeroLength) 1.88 +{ 1.89 + Input input; 1.90 + ASSERT_EQ(Failure, input.Init(nullptr, 0)); 1.91 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.92 + 1.93 + ASSERT_EQ(Failure, input.Init(nullptr, 100)); 1.94 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.95 + 1.96 + // Is this a bug? 1.97 + ASSERT_EQ(Success, input.Init((const uint8_t*) "hello", 0)); 1.98 +} 1.99 + 1.100 +TEST_F(pkixder_input_tests, InputInitWithLargeData) 1.101 +{ 1.102 + Input input; 1.103 + // Data argument length does not matter, it is not touched, just 1.104 + // needs to be non-null 1.105 + ASSERT_EQ(Failure, input.Init((const uint8_t*) "", 0xffff+1)); 1.106 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.107 + 1.108 + ASSERT_EQ(Success, input.Init((const uint8_t*) "", 0xffff)); 1.109 +} 1.110 + 1.111 +TEST_F(pkixder_input_tests, InputInitMultipleTimes) 1.112 +{ 1.113 + Input input; 1.114 + 1.115 + ASSERT_EQ(Success, 1.116 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.117 + 1.118 + ASSERT_EQ(Failure, 1.119 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.120 + ASSERT_EQ(SEC_ERROR_INVALID_ARGS, PR_GetError()); 1.121 +} 1.122 + 1.123 +TEST_F(pkixder_input_tests, ExpectSuccess) 1.124 +{ 1.125 + Input input; 1.126 + 1.127 + ASSERT_EQ(Success, 1.128 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.129 + ASSERT_EQ(Success, 1.130 + input.Expect(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.131 + ASSERT_TRUE(input.AtEnd()); 1.132 +} 1.133 + 1.134 +TEST_F(pkixder_input_tests, ExpectMismatch) 1.135 +{ 1.136 + Input input; 1.137 + 1.138 + ASSERT_EQ(Success, 1.139 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.140 + 1.141 + const uint8_t expected[] = { 0x11, 0x22 }; 1.142 + ASSERT_EQ(Failure, input.Expect(expected, sizeof expected)); 1.143 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.144 +} 1.145 + 1.146 +TEST_F(pkixder_input_tests, ExpectTooMuch) 1.147 +{ 1.148 + Input input; 1.149 + 1.150 + const uint8_t der[] = { 0x11, 0x22 }; 1.151 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.152 + 1.153 + const uint8_t expected[] = { 0x11, 0x22, 0x33 }; 1.154 + ASSERT_EQ(Failure, input.Expect(expected, sizeof expected)); 1.155 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.156 +} 1.157 + 1.158 +TEST_F(pkixder_input_tests, PeekWithinBounds) 1.159 +{ 1.160 + Input input; 1.161 + const uint8_t der[] = { 0x11, 0x11 }; 1.162 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.163 + ASSERT_TRUE(input.Peek(0x11)); 1.164 + ASSERT_FALSE(input.Peek(0x22)); 1.165 +} 1.166 + 1.167 +TEST_F(pkixder_input_tests, PeekPastBounds) 1.168 +{ 1.169 + Input input; 1.170 + const uint8_t der[] = { 0x11, 0x22 }; 1.171 + ASSERT_EQ(Success, input.Init(der, 1)); 1.172 + 1.173 + uint8_t readByte; 1.174 + ASSERT_EQ(Success, input.Read(readByte)); 1.175 + ASSERT_EQ(0x11, readByte); 1.176 + ASSERT_FALSE(input.Peek(0x22)); 1.177 +} 1.178 + 1.179 +TEST_F(pkixder_input_tests, ReadByte) 1.180 +{ 1.181 + Input input; 1.182 + const uint8_t der[] = { 0x11, 0x22 }; 1.183 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.184 + 1.185 + uint8_t readByte1; 1.186 + ASSERT_EQ(Success, input.Read(readByte1)); 1.187 + ASSERT_EQ(0x11, readByte1); 1.188 + 1.189 + uint8_t readByte2; 1.190 + ASSERT_EQ(Success, input.Read(readByte2)); 1.191 + ASSERT_EQ(0x22, readByte2); 1.192 +} 1.193 + 1.194 +TEST_F(pkixder_input_tests, ReadBytePastEnd) 1.195 +{ 1.196 + Input input; 1.197 + const uint8_t der[] = { 0x11, 0x22 }; 1.198 + // Initialize with too-short length 1.199 + ASSERT_EQ(Success, input.Init(der, 1)); 1.200 + 1.201 + uint8_t readByte1 = 0; 1.202 + ASSERT_EQ(Success, input.Read(readByte1)); 1.203 + ASSERT_EQ(0x11, readByte1); 1.204 + 1.205 + uint8_t readByte2 = 0; 1.206 + ASSERT_EQ(Failure, input.Read(readByte2)); 1.207 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.208 + ASSERT_NE(0x22, readByte2); 1.209 +} 1.210 + 1.211 +TEST_F(pkixder_input_tests, ReadByteWrapAroundPointer) 1.212 +{ 1.213 + // The original implementation of our buffer read overflow checks was 1.214 + // susceptible to integer overflows which could make the checks ineffective. 1.215 + // This attempts to verify that we've fixed that. Unfortunately, decrementing 1.216 + // a null pointer is undefined behavior according to the C++ language spec., 1.217 + // but this should catch the problem on at least some compilers, if not all of 1.218 + // them. 1.219 + const uint8_t* der = nullptr; 1.220 + --der; 1.221 + Input input; 1.222 + ASSERT_EQ(Success, input.Init(der, 0)); 1.223 + uint8_t b; 1.224 + ASSERT_EQ(Failure, input.Read(b)); 1.225 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.226 +} 1.227 + 1.228 +TEST_F(pkixder_input_tests, ReadWord) 1.229 +{ 1.230 + Input input; 1.231 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.232 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.233 + 1.234 + uint16_t readWord1 = 0; 1.235 + ASSERT_EQ(Success, input.Read(readWord1)); 1.236 + ASSERT_EQ(0x1122, readWord1); 1.237 + 1.238 + uint16_t readWord2 = 0; 1.239 + ASSERT_EQ(Success, input.Read(readWord2)); 1.240 + ASSERT_EQ(0x3344, readWord2); 1.241 +} 1.242 + 1.243 +TEST_F(pkixder_input_tests, ReadWordPastEnd) 1.244 +{ 1.245 + Input input; 1.246 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.247 + // Initialize with too-short length 1.248 + ASSERT_EQ(Success, input.Init(der, 2)); 1.249 + 1.250 + uint16_t readWord1 = 0; 1.251 + ASSERT_EQ(Success, input.Read(readWord1)); 1.252 + ASSERT_EQ(0x1122, readWord1); 1.253 + 1.254 + uint16_t readWord2 = 0; 1.255 + ASSERT_EQ(Failure, input.Read(readWord2)); 1.256 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.257 + ASSERT_NE(0x3344, readWord2); 1.258 +} 1.259 + 1.260 +TEST_F(pkixder_input_tests, ReadWordWithInsufficentData) 1.261 +{ 1.262 + Input input; 1.263 + const uint8_t der[] = { 0x11, 0x22 }; 1.264 + ASSERT_EQ(Success, input.Init(der, 1)); 1.265 + 1.266 + uint16_t readWord1 = 0; 1.267 + ASSERT_EQ(Failure, input.Read(readWord1)); 1.268 + ASSERT_NE(0x1122, readWord1); 1.269 +} 1.270 + 1.271 +TEST_F(pkixder_input_tests, ReadWordWrapAroundPointer) 1.272 +{ 1.273 + // The original implementation of our buffer read overflow checks was 1.274 + // susceptible to integer overflows which could make the checks ineffective. 1.275 + // This attempts to verify that we've fixed that. Unfortunately, decrementing 1.276 + // a null pointer is undefined behavior according to the C++ language spec., 1.277 + // but this should catch the problem on at least some compilers, if not all of 1.278 + // them. 1.279 + const uint8_t* der = nullptr; 1.280 + --der; 1.281 + Input input; 1.282 + ASSERT_EQ(Success, input.Init(der, 0)); 1.283 + uint16_t b; 1.284 + ASSERT_EQ(Failure, input.Read(b)); 1.285 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.286 +} 1.287 + 1.288 +TEST_F(pkixder_input_tests, InputSkip) 1.289 +{ 1.290 + Input input; 1.291 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.292 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.293 + 1.294 + ASSERT_EQ(Success, input.Skip(1)); 1.295 + 1.296 + uint8_t readByte1 = 0; 1.297 + ASSERT_EQ(Success, input.Read(readByte1)); 1.298 + ASSERT_EQ(0x22, readByte1); 1.299 + 1.300 + ASSERT_EQ(Success, input.Skip(1)); 1.301 + 1.302 + uint8_t readByte2 = 0; 1.303 + ASSERT_EQ(Success, input.Read(readByte2)); 1.304 + ASSERT_EQ(0x44, readByte2); 1.305 +} 1.306 + 1.307 +TEST_F(pkixder_input_tests, InputSkipToEnd) 1.308 +{ 1.309 + Input input; 1.310 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.311 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.312 + ASSERT_EQ(Success, input.Skip(sizeof der)); 1.313 + ASSERT_TRUE(input.AtEnd()); 1.314 +} 1.315 + 1.316 +TEST_F(pkixder_input_tests, InputSkipPastEnd) 1.317 +{ 1.318 + Input input; 1.319 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.320 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.321 + 1.322 + ASSERT_EQ(Failure, input.Skip(sizeof der + 1)); 1.323 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.324 +} 1.325 + 1.326 +TEST_F(pkixder_input_tests, InputSkipToNewInput) 1.327 +{ 1.328 + Input input; 1.329 + const uint8_t der[] = { 0x01, 0x02, 0x03, 0x04 }; 1.330 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.331 + 1.332 + Input skippedInput; 1.333 + ASSERT_EQ(Success, input.Skip(3, skippedInput)); 1.334 + 1.335 + uint8_t readByte1 = 0; 1.336 + ASSERT_EQ(Success, input.Read(readByte1)); 1.337 + ASSERT_EQ(0x04, readByte1); 1.338 + 1.339 + ASSERT_TRUE(input.AtEnd()); 1.340 + 1.341 + // Input has no Remaining() or Length() so we simply read the bytes 1.342 + // and then expect to be at the end. 1.343 + 1.344 + for (uint8_t i = 1; i <= 3; ++i) { 1.345 + uint8_t readByte = 0; 1.346 + ASSERT_EQ(Success, skippedInput.Read(readByte)); 1.347 + ASSERT_EQ(i, readByte); 1.348 + } 1.349 + 1.350 + ASSERT_TRUE(skippedInput.AtEnd()); 1.351 +} 1.352 + 1.353 +TEST_F(pkixder_input_tests, InputSkipToNewInputPastEnd) 1.354 +{ 1.355 + Input input; 1.356 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.357 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.358 + 1.359 + Input skippedInput; 1.360 + ASSERT_EQ(Failure, input.Skip(sizeof der * 2, skippedInput)); 1.361 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.362 +} 1.363 + 1.364 +TEST_F(pkixder_input_tests, InputSkipToSECItem) 1.365 +{ 1.366 + Input input; 1.367 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.368 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.369 + 1.370 + const uint8_t expectedItemData[] = { 0x11, 0x22, 0x33 }; 1.371 + 1.372 + SECItem item; 1.373 + ASSERT_EQ(Success, input.Skip(sizeof expectedItemData, item)); 1.374 + ASSERT_EQ(siBuffer, item.type); 1.375 + ASSERT_EQ(sizeof expectedItemData, item.len); 1.376 + ASSERT_EQ(der, item.data); 1.377 + ASSERT_EQ(0, memcmp(item.data, expectedItemData, sizeof expectedItemData)); 1.378 +} 1.379 + 1.380 +TEST_F(pkixder_input_tests, SkipWrapAroundPointer) 1.381 +{ 1.382 + // The original implementation of our buffer read overflow checks was 1.383 + // susceptible to integer overflows which could make the checks ineffective. 1.384 + // This attempts to verify that we've fixed that. Unfortunately, decrementing 1.385 + // a null pointer is undefined behavior according to the C++ language spec., 1.386 + // but this should catch the problem on at least some compilers, if not all of 1.387 + // them. 1.388 + const uint8_t* der = nullptr; 1.389 + --der; 1.390 + Input input; 1.391 + ASSERT_EQ(Success, input.Init(der, 0)); 1.392 + ASSERT_EQ(Failure, input.Skip(1)); 1.393 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.394 +} 1.395 + 1.396 +TEST_F(pkixder_input_tests, SkipToSECItemPastEnd) 1.397 +{ 1.398 + Input input; 1.399 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.400 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.401 + 1.402 + SECItem skippedSECItem; 1.403 + ASSERT_EQ(Failure, input.Skip(sizeof der + 1, skippedSECItem)); 1.404 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.405 +} 1.406 + 1.407 +TEST_F(pkixder_input_tests, Skip) 1.408 +{ 1.409 + Input input; 1.410 + ASSERT_EQ(Success, 1.411 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.412 + 1.413 + ASSERT_EQ(Success, Skip(input, SEQUENCE)); 1.414 + ASSERT_EQ(Success, End(input)); 1.415 +} 1.416 + 1.417 +TEST_F(pkixder_input_tests, SkipWithTruncatedData) 1.418 +{ 1.419 + Input input; 1.420 + ASSERT_EQ(Success, input.Init(DER_TRUNCATED_SEQUENCE_OF_INT8, 1.421 + sizeof DER_TRUNCATED_SEQUENCE_OF_INT8)); 1.422 + 1.423 + ASSERT_EQ(Failure, Skip(input, SEQUENCE)); 1.424 +} 1.425 + 1.426 +TEST_F(pkixder_input_tests, SkipWithOverrunData) 1.427 +{ 1.428 + Input input; 1.429 + ASSERT_EQ(Success, input.Init(DER_OVERRUN_SEQUENCE_OF_INT8, 1.430 + sizeof DER_OVERRUN_SEQUENCE_OF_INT8)); 1.431 + ASSERT_EQ(Success, Skip(input, SEQUENCE)); 1.432 + ASSERT_EQ(Failure, End(input)); 1.433 +} 1.434 + 1.435 +TEST_F(pkixder_input_tests, AtEndOnUnInitializedInput) 1.436 +{ 1.437 + Input input; 1.438 + ASSERT_TRUE(input.AtEnd()); 1.439 +} 1.440 + 1.441 +TEST_F(pkixder_input_tests, AtEndAtBeginning) 1.442 +{ 1.443 + Input input; 1.444 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.445 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.446 + ASSERT_FALSE(input.AtEnd()); 1.447 +} 1.448 + 1.449 +TEST_F(pkixder_input_tests, AtEndAtEnd) 1.450 +{ 1.451 + Input input; 1.452 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.453 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.454 + ASSERT_EQ(Success, input.Skip(sizeof der)); 1.455 + ASSERT_TRUE(input.AtEnd()); 1.456 +} 1.457 + 1.458 +TEST_F(pkixder_input_tests, MarkAndGetSECItem) 1.459 +{ 1.460 + Input input; 1.461 + const uint8_t der[] = { 0x11, 0x22, 0x33, 0x44 }; 1.462 + ASSERT_EQ(Success, input.Init(der, sizeof der)); 1.463 + 1.464 + Input::Mark mark = input.GetMark(); 1.465 + 1.466 + const uint8_t expectedItemData[] = { 0x11, 0x22, 0x33 }; 1.467 + 1.468 + ASSERT_EQ(Success, input.Skip(sizeof expectedItemData)); 1.469 + 1.470 + SECItem item; 1.471 + memset(&item, 0x00, sizeof item); 1.472 + 1.473 + ASSERT_TRUE(input.GetSECItem(siBuffer, mark, item)); 1.474 + ASSERT_EQ(siBuffer, item.type); 1.475 + ASSERT_EQ(sizeof expectedItemData, item.len); 1.476 + ASSERT_TRUE(item.data); 1.477 + ASSERT_EQ(0, memcmp(item.data, expectedItemData, sizeof expectedItemData)); 1.478 +} 1.479 + 1.480 +TEST_F(pkixder_input_tests, ExpectTagAndLength) 1.481 +{ 1.482 + Input input; 1.483 + ASSERT_EQ(Success, 1.484 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.485 + 1.486 + ASSERT_EQ(Success, ExpectTagAndLength(input, SEQUENCE, 1.487 + sizeof DER_SEQUENCE_OF_INT8 - 2)); 1.488 +} 1.489 + 1.490 +TEST_F(pkixder_input_tests, ExpectTagAndLengthWithWrongLength) 1.491 +{ 1.492 + Input input; 1.493 + ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); 1.494 + 1.495 + // Wrong length 1.496 + ASSERT_EQ(Failure, ExpectTagAndLength(input, INTEGER, 4)); 1.497 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.498 +} 1.499 + 1.500 +TEST_F(pkixder_input_tests, ExpectTagAndLengthWithWrongTag) 1.501 +{ 1.502 + Input input; 1.503 + ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); 1.504 + 1.505 + // Wrong type 1.506 + ASSERT_EQ(Failure, ExpectTagAndLength(input, OCTET_STRING, 2)); 1.507 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.508 +} 1.509 + 1.510 +TEST_F(pkixder_input_tests, ExpectTagAndGetLength) 1.511 +{ 1.512 + Input input; 1.513 + ASSERT_EQ(Success, 1.514 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.515 + 1.516 + uint16_t length = 0; 1.517 + ASSERT_EQ(Success, ExpectTagAndGetLength(input, SEQUENCE, length)); 1.518 + ASSERT_EQ(sizeof DER_SEQUENCE_OF_INT8 - 2, length); 1.519 + ASSERT_EQ(Success, input.Skip(length)); 1.520 + ASSERT_TRUE(input.AtEnd()); 1.521 +} 1.522 + 1.523 +TEST_F(pkixder_input_tests, ExpectTagAndGetLengthWithWrongTag) 1.524 +{ 1.525 + Input input; 1.526 + ASSERT_EQ(Success, 1.527 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.528 + 1.529 + uint16_t length = 0; 1.530 + ASSERT_EQ(Failure, ExpectTagAndGetLength(input, INTEGER, length)); 1.531 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.532 +} 1.533 + 1.534 +TEST_F(pkixder_input_tests, ExpectTagAndGetLengthWithWrongLength) 1.535 +{ 1.536 + Input input; 1.537 + ASSERT_EQ(Success, input.Init(DER_TRUNCATED_SEQUENCE_OF_INT8, 1.538 + sizeof DER_TRUNCATED_SEQUENCE_OF_INT8)); 1.539 + 1.540 + uint16_t length = 0; 1.541 + ASSERT_EQ(Failure, ExpectTagAndGetLength(input, SEQUENCE, length)); 1.542 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.543 +} 1.544 + 1.545 +TEST_F(pkixder_input_tests, ExpectTagAndIgnoreLength) 1.546 +{ 1.547 + Input input; 1.548 + ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); 1.549 + ASSERT_EQ(Success, ExpectTagAndIgnoreLength(input, INTEGER)); 1.550 +} 1.551 + 1.552 +TEST_F(pkixder_input_tests, ExpectTagAndIgnoreLengthWithWrongTag) 1.553 +{ 1.554 + Input input; 1.555 + ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); 1.556 + 1.557 + ASSERT_EQ(Failure, ExpectTagAndIgnoreLength(input, OCTET_STRING)); 1.558 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.559 +} 1.560 + 1.561 +TEST_F(pkixder_input_tests, EndAtEnd) 1.562 +{ 1.563 + Input input; 1.564 + ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); 1.565 + ASSERT_EQ(Success, input.Skip(4)); 1.566 + ASSERT_EQ(Success, End(input)); 1.567 +} 1.568 + 1.569 +TEST_F(pkixder_input_tests, EndBeforeEnd) 1.570 +{ 1.571 + Input input; 1.572 + ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); 1.573 + ASSERT_EQ(Success, input.Skip(2)); 1.574 + ASSERT_EQ(Failure, End(input)); 1.575 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.576 +} 1.577 + 1.578 +TEST_F(pkixder_input_tests, EndAtBeginning) 1.579 +{ 1.580 + Input input; 1.581 + ASSERT_EQ(Success, input.Init(DER_INT16, sizeof DER_INT16)); 1.582 + ASSERT_EQ(Failure, End(input)); 1.583 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.584 +} 1.585 + 1.586 +// TODO: Need tests for Nested too? 1.587 + 1.588 +Result NestedOfHelper(Input& input, std::vector<uint8_t>& readValues) 1.589 +{ 1.590 + uint8_t value = 0; 1.591 + if (input.Read(value) != Success) { 1.592 + return Failure; 1.593 + } 1.594 + readValues.push_back(value); 1.595 + return Success; 1.596 +} 1.597 + 1.598 +TEST_F(pkixder_input_tests, NestedOf) 1.599 +{ 1.600 + Input input; 1.601 + ASSERT_EQ(Success, 1.602 + input.Init(DER_SEQUENCE_OF_INT8, sizeof DER_SEQUENCE_OF_INT8)); 1.603 + 1.604 + std::vector<uint8_t> readValues; 1.605 + ASSERT_EQ(Success, 1.606 + NestedOf(input, SEQUENCE, INTEGER, MustNotBeEmpty, 1.607 + mozilla::pkix::bind(NestedOfHelper, mozilla::pkix::_1, 1.608 + mozilla::pkix::ref(readValues)))); 1.609 + ASSERT_EQ((size_t) 3, readValues.size()); 1.610 + ASSERT_EQ(0x01, readValues[0]); 1.611 + ASSERT_EQ(0x02, readValues[1]); 1.612 + ASSERT_EQ(0x03, readValues[2]); 1.613 + ASSERT_EQ(Success, End(input)); 1.614 +} 1.615 + 1.616 +TEST_F(pkixder_input_tests, NestedOfWithTruncatedData) 1.617 +{ 1.618 + Input input; 1.619 + ASSERT_EQ(Success, input.Init(DER_TRUNCATED_SEQUENCE_OF_INT8, 1.620 + sizeof DER_TRUNCATED_SEQUENCE_OF_INT8)); 1.621 + 1.622 + std::vector<uint8_t> readValues; 1.623 + ASSERT_EQ(Failure, 1.624 + NestedOf(input, SEQUENCE, INTEGER, MustNotBeEmpty, 1.625 + mozilla::pkix::bind(NestedOfHelper, mozilla::pkix::_1, 1.626 + mozilla::pkix::ref(readValues)))); 1.627 + ASSERT_EQ(SEC_ERROR_BAD_DER, PR_GetError()); 1.628 + ASSERT_EQ((size_t) 0, readValues.size()); 1.629 +} 1.630 +} // unnamed namespace