Chromium Code Reviews| Index: net/der/parse_values_unittest.cc |
| diff --git a/net/der/parse_values_unittest.cc b/net/der/parse_values_unittest.cc |
| index ff0aafc107977389954653d15186ad816d92ebeb..c64beaa4ea5715dc113408d1dfde264effb31b1f 100644 |
| --- a/net/der/parse_values_unittest.cc |
| +++ b/net/der/parse_values_unittest.cc |
| @@ -184,9 +184,17 @@ struct Uint64TestData { |
| const Uint64TestData kUint64TestData[] = { |
| {true, {0x00}, 1, 0}, |
| + // This number fails because it is not a minimal representation. |
| + {false, {0x00, 0x00}, 2, 0}, |
| {true, {0x01}, 1, 1}, |
| {false, {0xFF}, 1, 0}, |
| {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX}, |
| + {true, |
| + {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| + 9, |
| + UINT64_MAX}, |
| + // This number fails because it is negative. |
| + {false, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, 0}, |
| {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0}, |
| {false, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 9, 0}, |
| {false, {0x00, 0x01}, 2, 1}, |
| @@ -196,7 +204,7 @@ const Uint64TestData kUint64TestData[] = { |
| TEST(ParseValuesTest, ParseUint64) { |
| for (size_t i = 0; i < arraysize(kUint64TestData); i++) { |
| - Uint64TestData test_case = kUint64TestData[i]; |
| + const Uint64TestData& test_case = kUint64TestData[i]; |
| SCOPED_TRACE(i); |
| uint64_t result; |
| @@ -207,6 +215,56 @@ TEST(ParseValuesTest, ParseUint64) { |
| } |
| } |
| +struct ParseIntegerTestData { |
| + bool should_pass; |
| + const uint8_t input[16]; |
|
nharper
2015/08/14 22:23:41
Why 16? The maximum length used in those test case
eroman
2015/08/14 23:19:38
Done (no reason, just a consequence of copy-paste
|
| + size_t length; |
| + bool negative; |
| + int length_difference; |
| +}; |
| + |
| +const ParseIntegerTestData kParseIntegerTestData[] = { |
| + // Empty input (invalid DER). |
| + {false, {0x00}, 0}, |
| + |
| + // The correct encoding for zero. |
| + {true, {0x00}, 1, false}, |
| + |
| + // Invalid representation of zero (not minimal) |
| + {false, {0x00, 0x00}, 2}, |
| + |
| + // Valid single byte negative numbers. |
| + {true, {0x80}, 1, true}, |
| + {true, {0xFF}, 1, true}, |
| + |
| + // Non-minimal negative number. |
| + {false, {0xFF, 0x80}, 2}, |
| + |
| + // Positive number with a legitimate leading zero. |
| + {true, {0x00, 0x80}, 2, false, -1}, |
| + |
| + // A legitimate negative number that starts with FF (MSB of second byte is |
| + // 0 so OK). |
| + {true, {0xFF, 0x7F}, 2, true}, |
| +}; |
| + |
| +TEST(ParseValuesTest, ParseInteger) { |
| + for (size_t i = 0; i < arraysize(kParseIntegerTestData); i++) { |
| + const ParseIntegerTestData& test_case = kParseIntegerTestData[i]; |
| + SCOPED_TRACE(i); |
| + |
| + bool negative; |
| + size_t numeric_length; |
| + EXPECT_EQ(test_case.should_pass, |
| + ParseInteger(Input(test_case.input, test_case.length), &negative, |
| + &numeric_length)); |
| + if (test_case.should_pass) { |
| + EXPECT_EQ(test_case.negative, negative); |
| + EXPECT_EQ(test_case.length + test_case.length_difference, numeric_length); |
| + } |
| + } |
| +} |
| + |
| // Tests parsing an empty BIT STRING. |
| TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) { |
| const uint8_t kData[] = {0x00}; |