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

Side by Side 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 unified diff | Download patch
« net/der/parse_values.h ('K') | « net/der/parse_values.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "net/der/parse_values.h" 8 #include "net/der/parse_values.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 177
178 struct Uint64TestData { 178 struct Uint64TestData {
179 bool should_pass; 179 bool should_pass;
180 const uint8_t input[9]; 180 const uint8_t input[9];
181 size_t length; 181 size_t length;
182 uint64_t expected_value; 182 uint64_t expected_value;
183 }; 183 };
184 184
185 const Uint64TestData kUint64TestData[] = { 185 const Uint64TestData kUint64TestData[] = {
186 {true, {0x00}, 1, 0}, 186 {true, {0x00}, 1, 0},
187 // This number fails because it is not a minimal representation.
188 {false, {0x00, 0x00}, 2},
187 {true, {0x01}, 1, 1}, 189 {true, {0x01}, 1, 1},
188 {false, {0xFF}, 1, 0}, 190 {false, {0xFF}, 1},
189 {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX}, 191 {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX},
190 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0}, 192 {true,
191 {false, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 9, 0}, 193 {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
192 {false, {0x00, 0x01}, 2, 1}, 194 9,
193 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9, 0}, 195 UINT64_MAX},
194 {false, {0}, 0, 0}, 196 // This number fails because it is negative.
197 {false, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8},
198 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8},
199 {false, {0x00, 0x01}, 2},
200 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9},
201 {false, {0}, 0},
195 }; 202 };
196 203
197 TEST(ParseValuesTest, ParseUint64) { 204 TEST(ParseValuesTest, ParseUint64) {
198 for (size_t i = 0; i < arraysize(kUint64TestData); i++) { 205 for (size_t i = 0; i < arraysize(kUint64TestData); i++) {
199 Uint64TestData test_case = kUint64TestData[i]; 206 const Uint64TestData& test_case = kUint64TestData[i];
200 SCOPED_TRACE(i); 207 SCOPED_TRACE(i);
201 208
202 uint64_t result; 209 uint64_t result;
203 EXPECT_EQ(test_case.should_pass, 210 EXPECT_EQ(test_case.should_pass,
204 ParseUint64(Input(test_case.input, test_case.length), &result)); 211 ParseUint64(Input(test_case.input, test_case.length), &result));
205 if (test_case.should_pass) 212 if (test_case.should_pass)
206 EXPECT_EQ(test_case.expected_value, result); 213 EXPECT_EQ(test_case.expected_value, result);
207 } 214 }
208 } 215 }
209 216
217 struct ParseIntegerTestData {
218 bool should_pass;
219 const uint8_t input[2];
220 size_t length;
221 bool negative;
222 // The difference between length and numeric_length. If it is 0 then
223 // numeric_length is expected to match length.
224 int length_difference;
225 };
226
227 const ParseIntegerTestData kParseIntegerTestData[] = {
228 // Empty input (invalid DER).
229 {false, {0x00}, 0},
230
231 // The correct encoding for zero.
232 {true, {0x00}, 1, false},
233
234 // Invalid representation of zero (not minimal)
235 {false, {0x00, 0x00}, 2},
236
237 // Valid single byte negative numbers.
238 {true, {0x80}, 1, true},
239 {true, {0xFF}, 1, true},
240
241 // Non-minimal negative number.
242 {false, {0xFF, 0x80}, 2},
243
244 // Positive number with a legitimate leading zero.
245 {true, {0x00, 0x80}, 2, false, -1},
246
247 // A legitimate negative number that starts with FF (MSB of second byte is
248 // 0 so OK).
249 {true, {0xFF, 0x7F}, 2, true},
250 };
251
252 TEST(ParseValuesTest, IsValidInteger) {
253 for (size_t i = 0; i < arraysize(kParseIntegerTestData); i++) {
254 const ParseIntegerTestData& test_case = kParseIntegerTestData[i];
255 SCOPED_TRACE(i);
256
257 bool negative;
258 size_t numeric_length;
259 EXPECT_EQ(test_case.should_pass,
260 IsValidInteger(Input(test_case.input, test_case.length),
261 &negative, &numeric_length));
262 if (test_case.should_pass) {
263 EXPECT_EQ(test_case.negative, negative);
264 EXPECT_EQ(test_case.length + test_case.length_difference, numeric_length);
265 }
266 }
267 }
268
210 // Tests parsing an empty BIT STRING. 269 // Tests parsing an empty BIT STRING.
211 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) { 270 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) {
212 const uint8_t kData[] = {0x00}; 271 const uint8_t kData[] = {0x00};
213 272
214 BitString bit_string; 273 BitString bit_string;
215 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string)); 274 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string));
216 275
217 EXPECT_EQ(0u, bit_string.unused_bits()); 276 EXPECT_EQ(0u, bit_string.unused_bits());
218 EXPECT_EQ(0u, bit_string.bytes().Length()); 277 EXPECT_EQ(0u, bit_string.bytes().Length());
219 } 278 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) { 311 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) {
253 const uint8_t kData[] = {0x01, 0xFF}; 312 const uint8_t kData[] = {0x01, 0xFF};
254 313
255 BitString bit_string; 314 BitString bit_string;
256 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string)); 315 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string));
257 } 316 }
258 317
259 } // namespace test 318 } // namespace test
260 } // namespace der 319 } // namespace der
261 } // namespace net 320 } // namespace net
OLDNEW
« 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