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

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: 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.cc ('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, 0},
187 {true, {0x01}, 1, 1}, 189 {true, {0x01}, 1, 1},
188 {false, {0xFF}, 1, 0}, 190 {false, {0xFF}, 1, 0},
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},
192 {true,
193 {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
194 9,
195 UINT64_MAX},
196 // This number fails because it is negative.
197 {false, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, 0},
190 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0}, 198 {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8, 0},
191 {false, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 9, 0}, 199 {false, {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 9, 0},
192 {false, {0x00, 0x01}, 2, 1}, 200 {false, {0x00, 0x01}, 2, 1},
193 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9, 0}, 201 {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9, 0},
194 {false, {0}, 0, 0}, 202 {false, {0}, 0, 0},
nharper 2015/08/14 22:23:41 I noticed your ParseIntegerTestData initialization
eroman 2015/08/14 23:19:38 Done
195 }; 203 };
196 204
197 TEST(ParseValuesTest, ParseUint64) { 205 TEST(ParseValuesTest, ParseUint64) {
198 for (size_t i = 0; i < arraysize(kUint64TestData); i++) { 206 for (size_t i = 0; i < arraysize(kUint64TestData); i++) {
199 Uint64TestData test_case = kUint64TestData[i]; 207 const Uint64TestData& test_case = kUint64TestData[i];
200 SCOPED_TRACE(i); 208 SCOPED_TRACE(i);
201 209
202 uint64_t result; 210 uint64_t result;
203 EXPECT_EQ(test_case.should_pass, 211 EXPECT_EQ(test_case.should_pass,
204 ParseUint64(Input(test_case.input, test_case.length), &result)); 212 ParseUint64(Input(test_case.input, test_case.length), &result));
205 if (test_case.should_pass) 213 if (test_case.should_pass)
206 EXPECT_EQ(test_case.expected_value, result); 214 EXPECT_EQ(test_case.expected_value, result);
207 } 215 }
208 } 216 }
209 217
218 struct ParseIntegerTestData {
219 bool should_pass;
220 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
221 size_t length;
222 bool negative;
223 int length_difference;
224 };
225
226 const ParseIntegerTestData kParseIntegerTestData[] = {
227 // Empty input (invalid DER).
228 {false, {0x00}, 0},
229
230 // The correct encoding for zero.
231 {true, {0x00}, 1, false},
232
233 // Invalid representation of zero (not minimal)
234 {false, {0x00, 0x00}, 2},
235
236 // Valid single byte negative numbers.
237 {true, {0x80}, 1, true},
238 {true, {0xFF}, 1, true},
239
240 // Non-minimal negative number.
241 {false, {0xFF, 0x80}, 2},
242
243 // Positive number with a legitimate leading zero.
244 {true, {0x00, 0x80}, 2, false, -1},
245
246 // A legitimate negative number that starts with FF (MSB of second byte is
247 // 0 so OK).
248 {true, {0xFF, 0x7F}, 2, true},
249 };
250
251 TEST(ParseValuesTest, ParseInteger) {
252 for (size_t i = 0; i < arraysize(kParseIntegerTestData); i++) {
253 const ParseIntegerTestData& test_case = kParseIntegerTestData[i];
254 SCOPED_TRACE(i);
255
256 bool negative;
257 size_t numeric_length;
258 EXPECT_EQ(test_case.should_pass,
259 ParseInteger(Input(test_case.input, test_case.length), &negative,
260 &numeric_length));
261 if (test_case.should_pass) {
262 EXPECT_EQ(test_case.negative, negative);
263 EXPECT_EQ(test_case.length + test_case.length_difference, numeric_length);
264 }
265 }
266 }
267
210 // Tests parsing an empty BIT STRING. 268 // Tests parsing an empty BIT STRING.
211 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) { 269 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) {
212 const uint8_t kData[] = {0x00}; 270 const uint8_t kData[] = {0x00};
213 271
214 BitString bit_string; 272 BitString bit_string;
215 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string)); 273 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string));
216 274
217 EXPECT_EQ(0u, bit_string.unused_bits()); 275 EXPECT_EQ(0u, bit_string.unused_bits());
218 EXPECT_EQ(0u, bit_string.bytes().Length()); 276 EXPECT_EQ(0u, bit_string.bytes().Length());
219 } 277 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) { 310 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) {
253 const uint8_t kData[] = {0x01, 0xFF}; 311 const uint8_t kData[] = {0x01, 0xFF};
254 312
255 BitString bit_string; 313 BitString bit_string;
256 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string)); 314 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string));
257 } 315 }
258 316
259 } // namespace test 317 } // namespace test
260 } // namespace der 318 } // namespace der
261 } // namespace net 319 } // namespace net
OLDNEW
« net/der/parse_values.cc ('K') | « net/der/parse_values.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698