| 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/test_helpers.h" | 5 #include "net/cert/internal/test_helpers.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/base_paths.h" | 8 #include "base/base_paths.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 return ::testing::AssertionSuccess(); | 100 return ::testing::AssertionSuccess(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void ReadVerifyCertChainTestFromFile(const std::string& file_name, | 103 void ReadVerifyCertChainTestFromFile(const std::string& file_name, |
| 104 ParsedCertificateList* chain, | 104 ParsedCertificateList* chain, |
| 105 scoped_refptr<TrustAnchor>* trust_anchor, | 105 scoped_refptr<TrustAnchor>* trust_anchor, |
| 106 der::GeneralizedTime* time, | 106 der::GeneralizedTime* time, |
| 107 bool* verify_result) { | 107 bool* verify_result, |
| 108 std::string* expected_errors) { |
| 108 chain->clear(); | 109 chain->clear(); |
| 109 *trust_anchor = nullptr; | 110 *trust_anchor = nullptr; |
| 111 expected_errors->clear(); |
| 110 | 112 |
| 111 std::string file_data = ReadTestFileToString( | 113 std::string file_data = ReadTestFileToString( |
| 112 std::string("net/data/verify_certificate_chain_unittest/") + file_name); | 114 std::string("net/data/verify_certificate_chain_unittest/") + file_name); |
| 113 | 115 |
| 114 std::vector<std::string> pem_headers; | 116 std::vector<std::string> pem_headers; |
| 115 | 117 |
| 116 // For details on the file format refer to: | 118 // For details on the file format refer to: |
| 117 // net/data/verify_certificate_chain_unittest/README. | 119 // net/data/verify_certificate_chain_unittest/README. |
| 118 const char kCertificateHeader[] = "CERTIFICATE"; | 120 const char kCertificateHeader[] = "CERTIFICATE"; |
| 119 const char kTrustAnchorUnconstrained[] = "TRUST_ANCHOR_UNCONSTRAINED"; | 121 const char kTrustAnchorUnconstrained[] = "TRUST_ANCHOR_UNCONSTRAINED"; |
| 120 const char kTrustAnchorConstrained[] = "TRUST_ANCHOR_CONSTRAINED"; | 122 const char kTrustAnchorConstrained[] = "TRUST_ANCHOR_CONSTRAINED"; |
| 121 const char kTimeHeader[] = "TIME"; | 123 const char kTimeHeader[] = "TIME"; |
| 122 const char kResultHeader[] = "VERIFY_RESULT"; | 124 const char kResultHeader[] = "VERIFY_RESULT"; |
| 125 const char kErrorsHeader[] = "ERRORS"; |
| 123 | 126 |
| 124 pem_headers.push_back(kCertificateHeader); | 127 pem_headers.push_back(kCertificateHeader); |
| 125 pem_headers.push_back(kTrustAnchorUnconstrained); | 128 pem_headers.push_back(kTrustAnchorUnconstrained); |
| 126 pem_headers.push_back(kTrustAnchorConstrained); | 129 pem_headers.push_back(kTrustAnchorConstrained); |
| 127 pem_headers.push_back(kTimeHeader); | 130 pem_headers.push_back(kTimeHeader); |
| 128 pem_headers.push_back(kResultHeader); | 131 pem_headers.push_back(kResultHeader); |
| 132 pem_headers.push_back(kErrorsHeader); |
| 129 | 133 |
| 130 bool has_time = false; | 134 bool has_time = false; |
| 131 bool has_result = false; | 135 bool has_result = false; |
| 136 bool has_errors = false; |
| 132 | 137 |
| 133 PEMTokenizer pem_tokenizer(file_data, pem_headers); | 138 PEMTokenizer pem_tokenizer(file_data, pem_headers); |
| 134 while (pem_tokenizer.GetNext()) { | 139 while (pem_tokenizer.GetNext()) { |
| 135 const std::string& block_type = pem_tokenizer.block_type(); | 140 const std::string& block_type = pem_tokenizer.block_type(); |
| 136 const std::string& block_data = pem_tokenizer.data(); | 141 const std::string& block_data = pem_tokenizer.data(); |
| 137 | 142 |
| 138 if (block_type == kCertificateHeader) { | 143 if (block_type == kCertificateHeader) { |
| 139 ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector( | 144 ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector( |
| 140 reinterpret_cast<const uint8_t*>(block_data.data()), | 145 reinterpret_cast<const uint8_t*>(block_data.data()), |
| 141 block_data.size(), net::ParsedCertificate::DataSource::INTERNAL_COPY, | 146 block_data.size(), net::ParsedCertificate::DataSource::INTERNAL_COPY, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 157 } else if (block_type == kTimeHeader) { | 162 } else if (block_type == kTimeHeader) { |
| 158 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader; | 163 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader; |
| 159 has_time = true; | 164 has_time = true; |
| 160 ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), time)); | 165 ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), time)); |
| 161 } else if (block_type == kResultHeader) { | 166 } else if (block_type == kResultHeader) { |
| 162 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader; | 167 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader; |
| 163 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL") | 168 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL") |
| 164 << "Unrecognized result: " << block_data; | 169 << "Unrecognized result: " << block_data; |
| 165 has_result = true; | 170 has_result = true; |
| 166 *verify_result = block_data == "SUCCESS"; | 171 *verify_result = block_data == "SUCCESS"; |
| 172 } else if (block_type == kErrorsHeader) { |
| 173 ASSERT_FALSE(has_errors) << "Duplicate " << kErrorsHeader; |
| 174 has_errors = true; |
| 175 *expected_errors = block_data; |
| 167 } | 176 } |
| 168 } | 177 } |
| 169 | 178 |
| 170 ASSERT_TRUE(has_time); | 179 ASSERT_TRUE(has_time); |
| 171 ASSERT_TRUE(has_result); | 180 ASSERT_TRUE(has_result); |
| 172 ASSERT_TRUE(*trust_anchor); | 181 ASSERT_TRUE(*trust_anchor); |
| 173 } | 182 } |
| 174 | 183 |
| 175 std::string ReadTestFileToString(const std::string& file_name) { | 184 std::string ReadTestFileToString(const std::string& file_name) { |
| 176 // Compute the full path, relative to the src/ directory. | 185 // Compute the full path, relative to the src/ directory. |
| 177 base::FilePath src_root; | 186 base::FilePath src_root; |
| 178 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); | 187 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); |
| 179 base::FilePath filepath = src_root.AppendASCII(file_name); | 188 base::FilePath filepath = src_root.AppendASCII(file_name); |
| 180 | 189 |
| 181 // Read the full contents of the file. | 190 // Read the full contents of the file. |
| 182 std::string file_data; | 191 std::string file_data; |
| 183 if (!base::ReadFileToString(filepath, &file_data)) { | 192 if (!base::ReadFileToString(filepath, &file_data)) { |
| 184 ADD_FAILURE() << "Couldn't read file: " << filepath.value(); | 193 ADD_FAILURE() << "Couldn't read file: " << filepath.value(); |
| 185 return std::string(); | 194 return std::string(); |
| 186 } | 195 } |
| 187 | 196 |
| 188 return file_data; | 197 return file_data; |
| 189 } | 198 } |
| 190 | 199 |
| 191 } // namespace net | 200 } // namespace net |
| OLD | NEW |