Chromium Code Reviews| 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 "net/cert/internal/verify_name_match.h" | 5 #include "net/cert/internal/verify_name_match.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 13 #include "net/cert/pem_tokenizer.h" | 9 #include "net/cert/internal/test_helpers.h" |
| 14 #include "net/der/input.h" | 10 #include "net/der/input.h" |
| 15 #include "net/der/parser.h" | 11 #include "net/der/parser.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 13 |
| 18 namespace net { | 14 namespace net { |
| 19 namespace { | 15 namespace { |
| 20 | 16 |
| 21 der::Input SequenceValueFromString(const std::string& s) { | 17 der::Input SequenceValueFromString(const std::string& s) { |
|
mattm
2015/08/12 21:30:26
Do you want to update this to also take a pointer
eroman
2015/08/12 21:51:30
Good point!
I will make that change in this CL.
eroman
2015/08/12 22:51:00
Done.
| |
| 22 der::Parser parser( | 18 der::Parser parser(InputFromString(&s)); |
| 23 der::Input(reinterpret_cast<const uint8_t*>(s.data()), s.size())); | |
| 24 der::Input data; | 19 der::Input data; |
| 25 if (!parser.ReadTag(der::kSequence, &data)) { | 20 if (!parser.ReadTag(der::kSequence, &data)) { |
| 26 ADD_FAILURE(); | 21 ADD_FAILURE(); |
| 27 return der::Input(); | 22 return der::Input(); |
| 28 } | 23 } |
| 29 if (parser.HasMore()) { | 24 if (parser.HasMore()) { |
| 30 ADD_FAILURE(); | 25 ADD_FAILURE(); |
| 31 return der::Input(); | 26 return der::Input(); |
| 32 } | 27 } |
| 33 return data; | 28 return data; |
| 34 } | 29 } |
| 35 | 30 |
| 36 // Loads test data from file. The filename is constructed from the parameters: | 31 // Loads test data from file. The filename is constructed from the parameters: |
| 37 // |prefix| describes the type of data being tested, e.g. "ascii", | 32 // |prefix| describes the type of data being tested, e.g. "ascii", |
| 38 // "unicode_bmp", "unicode_supplementary", and "invalid". | 33 // "unicode_bmp", "unicode_supplementary", and "invalid". |
| 39 // |value_type| indicates what ASN.1 type is used to encode the data. | 34 // |value_type| indicates what ASN.1 type is used to encode the data. |
| 40 // |suffix| indicates any additional modifications, such as caseswapping, | 35 // |suffix| indicates any additional modifications, such as caseswapping, |
| 41 // whitespace adding, etc. | 36 // whitespace adding, etc. |
| 42 ::testing::AssertionResult LoadTestData(const std::string& prefix, | 37 ::testing::AssertionResult LoadTestData(const std::string& prefix, |
| 43 const std::string& value_type, | 38 const std::string& value_type, |
| 44 const std::string& suffix, | 39 const std::string& suffix, |
| 45 std::string* result) { | 40 std::string* result) { |
| 46 base::FilePath src_root; | 41 std::string path = "net/data/verify_name_match_unittest/names/" + prefix + |
| 47 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | 42 "-" + value_type + "-" + suffix + ".pem"; |
| 48 std::string filename = prefix + "-" + value_type + "-" + suffix + ".pem"; | |
| 49 base::FilePath filepath = | |
| 50 src_root.Append(FILE_PATH_LITERAL( | |
| 51 "net/data/verify_name_match_unittest/names")) | |
| 52 .AppendASCII(filename); | |
| 53 std::string file_data; | |
| 54 if (!base::ReadFileToString(filepath, &file_data)) { | |
| 55 return ::testing::AssertionFailure() | |
| 56 << "ReadFileToString returned false on " << filename; | |
| 57 } | |
| 58 | 43 |
| 59 std::vector<std::string> pem_headers; | 44 const PemBlockMapping mappings[] = { |
| 60 pem_headers.push_back("NAME"); | 45 {"NAME", result}, |
| 61 PEMTokenizer pem_tokenizer(file_data, pem_headers); | 46 }; |
| 62 if (!pem_tokenizer.GetNext()) { | |
| 63 return ::testing::AssertionFailure() << "PEM.GetNext returned false on " | |
| 64 << filename; | |
| 65 } | |
| 66 | 47 |
| 67 result->assign(pem_tokenizer.data()); | 48 return ReadTestDataFromPemFile(path, mappings); |
| 68 return ::testing::AssertionSuccess(); | |
| 69 } | 49 } |
| 70 | 50 |
| 71 bool TypesAreComparable(const std::string& type_1, const std::string& type_2) { | 51 bool TypesAreComparable(const std::string& type_1, const std::string& type_2) { |
| 72 if (type_1 == type_2) | 52 if (type_1 == type_2) |
| 73 return true; | 53 return true; |
| 74 if ((type_1 == "PRINTABLESTRING" || type_1 == "UTF8" || | 54 if ((type_1 == "PRINTABLESTRING" || type_1 == "UTF8" || |
| 75 type_1 == "BMPSTRING" || type_1 == "UNIVERSALSTRING") && | 55 type_1 == "BMPSTRING" || type_1 == "UNIVERSALSTRING") && |
| 76 (type_2 == "PRINTABLESTRING" || type_2 == "UTF8" || | 56 (type_2 == "PRINTABLESTRING" || type_2 == "UTF8" || |
| 77 type_2 == "BMPSTRING" || type_2 == "UNIVERSALSTRING")) { | 57 type_2 == "BMPSTRING" || type_2 == "UNIVERSALSTRING")) { |
| 78 return true; | 58 return true; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1", &a)); | 434 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_1", &a)); |
| 455 std::string b; | 435 std::string b; |
| 456 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_2", &b)); | 436 ASSERT_TRUE(LoadTestData("ascii", "mixed", "rdn_dupetype_sorting_2", &b)); |
| 457 EXPECT_TRUE( | 437 EXPECT_TRUE( |
| 458 VerifyNameMatch(SequenceValueFromString(a), SequenceValueFromString(b))); | 438 VerifyNameMatch(SequenceValueFromString(a), SequenceValueFromString(b))); |
| 459 EXPECT_TRUE( | 439 EXPECT_TRUE( |
| 460 VerifyNameMatch(SequenceValueFromString(b), SequenceValueFromString(a))); | 440 VerifyNameMatch(SequenceValueFromString(b), SequenceValueFromString(a))); |
| 461 } | 441 } |
| 462 | 442 |
| 463 } // namespace net | 443 } // namespace net |
| OLD | NEW |