Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Unified Diff: net/der/parse_values_unittest.cc

Issue 1295943002: Add a function for validating a DER-encoded INTEGER. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cert_parsing
Patch Set: address comments Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« net/der/parse_values.h ('K') | « net/der/parse_values.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..3eaa4009fb86d4daf81a0aa9b7588fdffe563050 100644
--- a/net/der/parse_values_unittest.cc
+++ b/net/der/parse_values_unittest.cc
@@ -184,19 +184,26 @@ struct Uint64TestData {
const Uint64TestData kUint64TestData[] = {
{true, {0x00}, 1, 0},
+ // This number fails because it is not a minimal representation.
+ {false, {0x00, 0x00}, 2},
{true, {0x01}, 1, 1},
- {false, {0xFF}, 1, 0},
+ {false, {0xFF}, 1},
{true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX},
- {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},
- {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9, 0},
- {false, {0}, 0, 0},
+ {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},
+ {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8},
+ {false, {0x00, 0x01}, 2},
+ {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9},
+ {false, {0}, 0},
};
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 +214,58 @@ TEST(ParseValuesTest, ParseUint64) {
}
}
+struct ParseIntegerTestData {
+ bool should_pass;
+ const uint8_t input[2];
+ size_t length;
+ bool negative;
+ // The difference between length and numeric_length. If it is 0 then
+ // numeric_length is expected to match length.
+ 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, IsValidInteger) {
+ 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,
+ IsValidInteger(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};
« net/der/parse_values.h ('K') | « net/der/parse_values.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698