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

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

Issue 2233233002: Refactor some certificate verification tests in preparation to adding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@trust_anchor
Patch Set: address matt's feedback Created 4 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
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 for (const auto& mapping : mappings_copy) { 93 for (const auto& mapping : mappings_copy) {
94 if (mapping.value && !mapping.optional) { 94 if (mapping.value && !mapping.optional) {
95 return ::testing::AssertionFailure() << "PEM block missing: " 95 return ::testing::AssertionFailure() << "PEM block missing: "
96 << mapping.block_name; 96 << mapping.block_name;
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,
104 ParsedCertificateList* chain,
105 scoped_refptr<TrustAnchor>* trust_anchor,
106 der::GeneralizedTime* time,
107 bool* verify_result) {
108 chain->clear();
mattm 2016/08/11 22:14:50 Oh, might want to *trust_anchor = nullptr; here to
eroman 2016/08/11 22:19:04 Done (independently noticed this while testing :)
109
110 std::string file_data = ReadTestFileToString(
111 std::string("net/data/verify_certificate_chain_unittest/") + file_name);
112
113 std::vector<std::string> pem_headers;
114
115 // For details on the file format refer to:
116 // net/data/verify_certificate_chain_unittest/README.
117 const char kCertificateHeader[] = "CERTIFICATE";
118 const char kTrustAnchorUnconstrained[] = "TRUST_ANCHOR_UNCONSTRAINED";
119 const char kTimeHeader[] = "TIME";
120 const char kResultHeader[] = "VERIFY_RESULT";
121
122 pem_headers.push_back(kCertificateHeader);
123 pem_headers.push_back(kTrustAnchorUnconstrained);
124 pem_headers.push_back(kTimeHeader);
125 pem_headers.push_back(kResultHeader);
126
127 bool has_time = false;
128 bool has_result = false;
129
130 PEMTokenizer pem_tokenizer(file_data, pem_headers);
131 while (pem_tokenizer.GetNext()) {
132 const std::string& block_type = pem_tokenizer.block_type();
133 const std::string& block_data = pem_tokenizer.data();
134
135 if (block_type == kCertificateHeader) {
136 ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector(
137 reinterpret_cast<const uint8_t*>(block_data.data()),
138 block_data.size(), net::ParsedCertificate::DataSource::INTERNAL_COPY,
139 {}, chain));
140 } else if (block_type == kTrustAnchorUnconstrained) {
141 ASSERT_FALSE(*trust_anchor) << "Duplicate trust anchor";
142 scoped_refptr<ParsedCertificate> root =
143 net::ParsedCertificate::CreateFromCertificateData(
144 reinterpret_cast<const uint8_t*>(block_data.data()),
145 block_data.size(),
146 net::ParsedCertificate::DataSource::INTERNAL_COPY, {});
147 ASSERT_TRUE(root);
148 *trust_anchor =
149 TrustAnchor::CreateFromCertificateNoConstraints(std::move(root));
150 } else if (block_type == kTimeHeader) {
151 ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader;
152 has_time = true;
153 ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), time));
154 } else if (block_type == kResultHeader) {
155 ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader;
156 ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL")
157 << "Unrecognized result: " << block_data;
158 has_result = true;
159 *verify_result = block_data == "SUCCESS";
160 }
161 }
162
163 ASSERT_TRUE(has_time);
164 ASSERT_TRUE(has_result);
165 ASSERT_TRUE(*trust_anchor);
166 }
167
168 std::string ReadTestFileToString(const std::string& file_name) {
169 // Compute the full path, relative to the src/ directory.
170 base::FilePath src_root;
171 PathService::Get(base::DIR_SOURCE_ROOT, &src_root);
172 base::FilePath filepath = src_root.AppendASCII(file_name);
173
174 // Read the full contents of the file.
175 std::string file_data;
176 if (!base::ReadFileToString(filepath, &file_data)) {
177 ADD_FAILURE() << "Couldn't read file: " << filepath.value();
178 return std::string();
179 }
180
181 return file_data;
182 }
183
103 } // namespace net 184 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/internal/test_helpers.h ('k') | net/cert/internal/verify_certificate_chain_typed_unittest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698