OLD | NEW |
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 Loading... |
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 IsValidIntegerTestData { |
| 218 bool should_pass; |
| 219 const uint8_t input[2]; |
| 220 size_t length; |
| 221 bool negative; |
| 222 }; |
| 223 |
| 224 const IsValidIntegerTestData kIsValidIntegerTestData[] = { |
| 225 // Empty input (invalid DER). |
| 226 {false, {0x00}, 0}, |
| 227 |
| 228 // The correct encoding for zero. |
| 229 {true, {0x00}, 1, false}, |
| 230 |
| 231 // Invalid representation of zero (not minimal) |
| 232 {false, {0x00, 0x00}, 2}, |
| 233 |
| 234 // Valid single byte negative numbers. |
| 235 {true, {0x80}, 1, true}, |
| 236 {true, {0xFF}, 1, true}, |
| 237 |
| 238 // Non-minimal negative number. |
| 239 {false, {0xFF, 0x80}, 2}, |
| 240 |
| 241 // Positive number with a legitimate leading zero. |
| 242 {true, {0x00, 0x80}, 2, false}, |
| 243 |
| 244 // A legitimate negative number that starts with FF (MSB of second byte is |
| 245 // 0 so OK). |
| 246 {true, {0xFF, 0x7F}, 2, true}, |
| 247 }; |
| 248 |
| 249 TEST(ParseValuesTest, IsValidInteger) { |
| 250 for (size_t i = 0; i < arraysize(kIsValidIntegerTestData); i++) { |
| 251 const auto& test_case = kIsValidIntegerTestData[i]; |
| 252 SCOPED_TRACE(i); |
| 253 |
| 254 bool negative; |
| 255 EXPECT_EQ( |
| 256 test_case.should_pass, |
| 257 IsValidInteger(Input(test_case.input, test_case.length), &negative)); |
| 258 if (test_case.should_pass) |
| 259 EXPECT_EQ(test_case.negative, negative); |
| 260 } |
| 261 } |
| 262 |
210 // Tests parsing an empty BIT STRING. | 263 // Tests parsing an empty BIT STRING. |
211 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) { | 264 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) { |
212 const uint8_t kData[] = {0x00}; | 265 const uint8_t kData[] = {0x00}; |
213 | 266 |
214 BitString bit_string; | 267 BitString bit_string; |
215 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string)); | 268 ASSERT_TRUE(ParseBitString(Input(kData), &bit_string)); |
216 | 269 |
217 EXPECT_EQ(0u, bit_string.unused_bits()); | 270 EXPECT_EQ(0u, bit_string.unused_bits()); |
218 EXPECT_EQ(0u, bit_string.bytes().Length()); | 271 EXPECT_EQ(0u, bit_string.bytes().Length()); |
219 } | 272 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) { | 305 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) { |
253 const uint8_t kData[] = {0x01, 0xFF}; | 306 const uint8_t kData[] = {0x01, 0xFF}; |
254 | 307 |
255 BitString bit_string; | 308 BitString bit_string; |
256 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string)); | 309 EXPECT_FALSE(ParseBitString(Input(kData), &bit_string)); |
257 } | 310 } |
258 | 311 |
259 } // namespace test | 312 } // namespace test |
260 } // namespace der | 313 } // namespace der |
261 } // namespace net | 314 } // namespace net |
OLD | NEW |