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

Side by Side Diff: net/cert/internal/test_helpers.cc

Issue 2801813004: Refactor VerifyCertificateChain test data to include a key purpose (Closed)
Patch Set: Address mattm's comment Created 3 years, 8 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
« no previous file with comments | « net/cert/internal/test_helpers.h ('k') | net/cert/internal/trust_store_collection_unittest.cc » ('j') | 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 "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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 for (const auto& mapping : mappings_copy) { 95 for (const auto& mapping : mappings_copy) {
96 if (mapping.value && !mapping.optional) { 96 if (mapping.value && !mapping.optional) {
97 return ::testing::AssertionFailure() << "PEM block missing: " 97 return ::testing::AssertionFailure() << "PEM block missing: "
98 << mapping.block_name; 98 << mapping.block_name;
99 } 99 }
100 } 100 }
101 101
102 return ::testing::AssertionSuccess(); 102 return ::testing::AssertionSuccess();
103 } 103 }
104 104
105 VerifyCertChainTest::VerifyCertChainTest() = default;
106 VerifyCertChainTest::~VerifyCertChainTest() = default;
107
105 void ReadVerifyCertChainTestFromFile(const std::string& file_path_ascii, 108 void ReadVerifyCertChainTestFromFile(const std::string& file_path_ascii,
106 ParsedCertificateList* chain, 109 VerifyCertChainTest* test) {
107 scoped_refptr<TrustAnchor>* trust_anchor, 110 // Reset all the out parameters to their defaults.
108 der::GeneralizedTime* time, 111 *test = {};
109 bool* verify_result,
110 std::string* expected_errors) {
111 chain->clear();
112 *trust_anchor = nullptr;
113 expected_errors->clear();
114 112
115 std::string file_data = ReadTestFileToString(file_path_ascii); 113 std::string file_data = ReadTestFileToString(file_path_ascii);
116 114
117 std::vector<std::string> pem_headers; 115 std::vector<std::string> pem_headers;
118 116
119 // For details on the file format refer to: 117 // For details on the file format refer to:
120 // net/data/verify_certificate_chain_unittest/README. 118 // net/data/verify_certificate_chain_unittest/README.
121 const char kCertificateHeader[] = "CERTIFICATE"; 119 const char kCertificateHeader[] = "CERTIFICATE";
122 const char kTrustAnchorUnconstrained[] = "TRUST_ANCHOR_UNCONSTRAINED"; 120 const char kTrustAnchorUnconstrained[] = "TRUST_ANCHOR_UNCONSTRAINED";
123 const char kTrustAnchorConstrained[] = "TRUST_ANCHOR_CONSTRAINED"; 121 const char kTrustAnchorConstrained[] = "TRUST_ANCHOR_CONSTRAINED";
124 const char kTimeHeader[] = "TIME"; 122 const char kTimeHeader[] = "TIME";
125 const char kResultHeader[] = "VERIFY_RESULT"; 123 const char kResultHeader[] = "VERIFY_RESULT";
126 const char kErrorsHeader[] = "ERRORS"; 124 const char kErrorsHeader[] = "ERRORS";
125 const char kKeyPurpose[] = "KEY_PURPOSE";
127 126
128 pem_headers.push_back(kCertificateHeader); 127 pem_headers.push_back(kCertificateHeader);
129 pem_headers.push_back(kTrustAnchorUnconstrained); 128 pem_headers.push_back(kTrustAnchorUnconstrained);
130 pem_headers.push_back(kTrustAnchorConstrained); 129 pem_headers.push_back(kTrustAnchorConstrained);
131 pem_headers.push_back(kTimeHeader); 130 pem_headers.push_back(kTimeHeader);
132 pem_headers.push_back(kResultHeader); 131 pem_headers.push_back(kResultHeader);
133 pem_headers.push_back(kErrorsHeader); 132 pem_headers.push_back(kErrorsHeader);
133 pem_headers.push_back(kKeyPurpose);
134 134
135 bool has_time = false; 135 bool has_time = false;
136 bool has_result = false; 136 bool has_result = false;
137 bool has_errors = false; 137 bool has_errors = false;
138 bool has_key_purpose = false;
138 139
139 PEMTokenizer pem_tokenizer(file_data, pem_headers); 140 PEMTokenizer pem_tokenizer(file_data, pem_headers);
140 while (pem_tokenizer.GetNext()) { 141 while (pem_tokenizer.GetNext()) {
141 const std::string& block_type = pem_tokenizer.block_type(); 142 const std::string& block_type = pem_tokenizer.block_type();
142 const std::string& block_data = pem_tokenizer.data(); 143 const std::string& block_data = pem_tokenizer.data();
143 144
144 if (block_type == kCertificateHeader) { 145 if (block_type == kCertificateHeader) {
145 CertErrors errors; 146 CertErrors errors;
146 ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector( 147 ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector(
147 bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new( 148 bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new(
148 reinterpret_cast<const uint8_t*>(block_data.data()), 149 reinterpret_cast<const uint8_t*>(block_data.data()),
149 block_data.size(), nullptr)), 150 block_data.size(), nullptr)),
150 {}, chain, &errors)) 151 {}, &test->chain, &errors))
151 << errors.ToDebugString(); 152 << errors.ToDebugString();
152 } else if (block_type == kTrustAnchorUnconstrained || 153 } else if (block_type == kTrustAnchorUnconstrained ||
153 block_type == kTrustAnchorConstrained) { 154 block_type == kTrustAnchorConstrained) {
154 ASSERT_FALSE(*trust_anchor) << "Duplicate trust anchor"; 155 ASSERT_FALSE(test->trust_anchor) << "Duplicate trust anchor";
155 CertErrors errors; 156 CertErrors errors;
156 scoped_refptr<ParsedCertificate> root = net::ParsedCertificate::Create( 157 scoped_refptr<ParsedCertificate> root = net::ParsedCertificate::Create(
157 bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new( 158 bssl::UniquePtr<CRYPTO_BUFFER>(CRYPTO_BUFFER_new(
158 reinterpret_cast<const uint8_t*>(block_data.data()), 159 reinterpret_cast<const uint8_t*>(block_data.data()),
159 block_data.size(), nullptr)), 160 block_data.size(), nullptr)),
160 {}, &errors); 161 {}, &errors);
161 ASSERT_TRUE(root) << errors.ToDebugString(); 162 ASSERT_TRUE(root) << errors.ToDebugString();
162 *trust_anchor = 163 test->trust_anchor =
163 block_type == kTrustAnchorUnconstrained 164 block_type == kTrustAnchorUnconstrained
164 ? TrustAnchor::CreateFromCertificateNoConstraints(std::move(root)) 165 ? TrustAnchor::CreateFromCertificateNoConstraints(std::move(root))
165 : TrustAnchor::CreateFromCertificateWithConstraints( 166 : TrustAnchor::CreateFromCertificateWithConstraints(
166 std::move(root)); 167 std::move(root));
167 } else if (block_type == kTimeHeader) { 168 } else if (block_type == kTimeHeader) {
168 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader; 169 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader;
169 has_time = true; 170 has_time = true;
170 ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), time)); 171 ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), &test->time));
172 } else if (block_type == kKeyPurpose) {
173 ASSERT_FALSE(has_key_purpose) << "Duplicate " << kKeyPurpose;
174 has_key_purpose = true;
175
176 if (block_data == "anyExtendedKeyUsage") {
177 // TODO(eroman): test->key_purpose = ....
178 } else if (block_data == "serverAuth") {
179 // TODO(eroman): test->key_purpose = ....
180 } else if (block_data == "clientAuth") {
181 // TODO(eroman): test->key_purpose = ....
182 } else {
183 ADD_FAILURE() << "Unrecognized " << block_type << ": " << block_data;
184 }
171 } else if (block_type == kResultHeader) { 185 } else if (block_type == kResultHeader) {
172 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader; 186 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader;
173 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL") 187 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL")
174 << "Unrecognized result: " << block_data; 188 << "Unrecognized result: " << block_data;
175 has_result = true; 189 has_result = true;
176 *verify_result = block_data == "SUCCESS"; 190 test->expected_result = block_data == "SUCCESS";
177 } else if (block_type == kErrorsHeader) { 191 } else if (block_type == kErrorsHeader) {
178 ASSERT_FALSE(has_errors) << "Duplicate " << kErrorsHeader; 192 ASSERT_FALSE(has_errors) << "Duplicate " << kErrorsHeader;
179 has_errors = true; 193 has_errors = true;
180 *expected_errors = block_data; 194 test->expected_errors = block_data;
181 } 195 }
182 } 196 }
183 197
184 ASSERT_TRUE(has_time); 198 ASSERT_TRUE(has_time);
185 ASSERT_TRUE(has_result); 199 ASSERT_TRUE(has_result);
186 ASSERT_TRUE(*trust_anchor); 200 ASSERT_TRUE(test->trust_anchor);
201 ASSERT_TRUE(has_key_purpose);
187 } 202 }
188 203
189 std::string ReadTestFileToString(const std::string& file_path_ascii) { 204 std::string ReadTestFileToString(const std::string& file_path_ascii) {
190 // Compute the full path, relative to the src/ directory. 205 // Compute the full path, relative to the src/ directory.
191 base::FilePath src_root; 206 base::FilePath src_root;
192 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); 207 PathService::Get(base::DIR_SOURCE_ROOT, &src_root);
193 base::FilePath filepath = src_root.AppendASCII(file_path_ascii); 208 base::FilePath filepath = src_root.AppendASCII(file_path_ascii);
194 209
195 // Read the full contents of the file. 210 // Read the full contents of the file.
196 std::string file_data; 211 std::string file_data;
197 if (!base::ReadFileToString(filepath, &file_data)) { 212 if (!base::ReadFileToString(filepath, &file_data)) {
198 ADD_FAILURE() << "Couldn't read file: " << filepath.value(); 213 ADD_FAILURE() << "Couldn't read file: " << filepath.value();
199 return std::string(); 214 return std::string();
200 } 215 }
201 216
202 return file_data; 217 return file_data;
203 } 218 }
204 219
205 } // namespace net 220 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/internal/test_helpers.h ('k') | net/cert/internal/trust_store_collection_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698