| Index: net/cert/internal/test_helpers.cc
|
| diff --git a/net/cert/internal/test_helpers.cc b/net/cert/internal/test_helpers.cc
|
| index 71b9a26e90a69bd81cdd42e57d0b6c3ede3853d5..ddaf1dd8fca40f6b763a30672c1296b4325bebf1 100644
|
| --- a/net/cert/internal/test_helpers.cc
|
| +++ b/net/cert/internal/test_helpers.cc
|
| @@ -14,6 +14,28 @@
|
|
|
| namespace net {
|
|
|
| +namespace {
|
| +
|
| +// Reads a data file from the unit-test data.
|
| +::testing::AssertionResult ReadTestFileToString(
|
| + const std::string& file_path_ascii,
|
| + std::string* out_file_data) {
|
| + // Compute the full path, relative to the src/ directory.
|
| + base::FilePath src_root;
|
| + PathService::Get(base::DIR_SOURCE_ROOT, &src_root);
|
| + base::FilePath filepath = src_root.AppendASCII(file_path_ascii);
|
| +
|
| + // Read the full contents of the file.
|
| + if (!base::ReadFileToString(filepath, out_file_data)) {
|
| + return ::testing::AssertionFailure() << "Couldn't read file: "
|
| + << filepath.value();
|
| + }
|
| +
|
| + return ::testing::AssertionSuccess();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| namespace der {
|
|
|
| void PrintTo(const Input& data, ::std::ostream* os) {
|
| @@ -46,17 +68,11 @@ der::Input SequenceValueFromString(const std::string* s) {
|
| const std::string& file_path_ascii,
|
| const PemBlockMapping* mappings,
|
| size_t mappings_length) {
|
| - // Compute the full path, relative to the src/ directory.
|
| - base::FilePath src_root;
|
| - PathService::Get(base::DIR_SOURCE_ROOT, &src_root);
|
| - base::FilePath filepath = src_root.AppendASCII(file_path_ascii);
|
| -
|
| - // Read the full contents of the PEM file.
|
| std::string file_data;
|
| - if (!base::ReadFileToString(filepath, &file_data)) {
|
| - return ::testing::AssertionFailure() << "Couldn't read file: "
|
| - << filepath.value();
|
| - }
|
| + ::testing::AssertionResult r =
|
| + ReadTestFileToString(file_path_ascii, &file_data);
|
| + if (!r)
|
| + return r;
|
|
|
| // mappings_copy is used to keep track of which mappings have already been
|
| // satisfied (by nulling the |value| field). This is used to track when
|
| @@ -100,4 +116,65 @@ der::Input SequenceValueFromString(const std::string* s) {
|
| return ::testing::AssertionSuccess();
|
| }
|
|
|
| +// Reads a test case from |file_name|. Test cases are comprised of a
|
| +// certificate chain, trust store, a timestamp to validate at, and the
|
| +// expected result of verification.
|
| +void ReadCertChainTestFromFile(const std::string& file_path_ascii,
|
| + ParsedCertificateList* chain,
|
| + ParsedCertificateList* roots,
|
| + der::GeneralizedTime* time,
|
| + bool* verify_result) {
|
| + chain->clear();
|
| + roots->clear();
|
| +
|
| + std::string file_data;
|
| + ASSERT_TRUE(ReadTestFileToString(file_path_ascii, &file_data));
|
| +
|
| + std::vector<std::string> pem_headers;
|
| +
|
| + const char kCertificateHeader[] = "CERTIFICATE";
|
| + const char kTrustedCertificateHeader[] = "TRUSTED_CERTIFICATE";
|
| + const char kTimeHeader[] = "TIME";
|
| + const char kResultHeader[] = "VERIFY_RESULT";
|
| +
|
| + pem_headers.push_back(kCertificateHeader);
|
| + pem_headers.push_back(kTrustedCertificateHeader);
|
| + pem_headers.push_back(kTimeHeader);
|
| + pem_headers.push_back(kResultHeader);
|
| +
|
| + bool has_time = false;
|
| + bool has_result = false;
|
| +
|
| + PEMTokenizer pem_tokenizer(file_data, pem_headers);
|
| + while (pem_tokenizer.GetNext()) {
|
| + const std::string& block_type = pem_tokenizer.block_type();
|
| + const std::string& block_data = pem_tokenizer.data();
|
| +
|
| + if (block_type == kCertificateHeader) {
|
| + ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector(
|
| + reinterpret_cast<const uint8_t*>(block_data.data()),
|
| + block_data.size(), net::ParsedCertificate::DataSource::INTERNAL_COPY,
|
| + {}, chain));
|
| + } else if (block_type == kTrustedCertificateHeader) {
|
| + ASSERT_TRUE(net::ParsedCertificate::CreateAndAddToVector(
|
| + reinterpret_cast<const uint8_t*>(block_data.data()),
|
| + block_data.size(), net::ParsedCertificate::DataSource::INTERNAL_COPY,
|
| + {}, roots));
|
| + } else if (block_type == kTimeHeader) {
|
| + ASSERT_FALSE(has_time) << "Duplicate " << kTimeHeader;
|
| + has_time = true;
|
| + ASSERT_TRUE(der::ParseUTCTime(der::Input(&block_data), time));
|
| + } else if (block_type == kResultHeader) {
|
| + ASSERT_FALSE(has_result) << "Duplicate " << kResultHeader;
|
| + ASSERT_TRUE(block_data == "SUCCESS" || block_data == "FAIL")
|
| + << "Unrecognized result: " << block_data;
|
| + has_result = true;
|
| + *verify_result = block_data == "SUCCESS";
|
| + }
|
| + }
|
| +
|
| + ASSERT_TRUE(has_time);
|
| + ASSERT_TRUE(has_result);
|
| +}
|
| +
|
| } // namespace net
|
|
|