OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/cert/internal/test_helpers.h" |
| 6 |
| 7 #include "base/base_paths.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/path_service.h" |
| 10 #include "net/cert/pem_tokenizer.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 der::Input InputFromString(const std::string* s) { |
| 15 return der::Input(reinterpret_cast<const uint8_t*>(s->data()), s->size()); |
| 16 } |
| 17 |
| 18 ::testing::AssertionResult ReadTestDataFromPemFile( |
| 19 const std::string& file_path_ascii, |
| 20 const PemBlockMapping* mappings, |
| 21 size_t mappings_length) { |
| 22 // Compute the full path, relative to the src/ directory. |
| 23 base::FilePath src_root; |
| 24 PathService::Get(base::DIR_SOURCE_ROOT, &src_root); |
| 25 base::FilePath filepath = src_root.AppendASCII(file_path_ascii); |
| 26 |
| 27 // Read the full contents of the PEM file. |
| 28 std::string file_data; |
| 29 if (!base::ReadFileToString(filepath, &file_data)) { |
| 30 return ::testing::AssertionFailure() << "Couldn't read file: " |
| 31 << filepath.value(); |
| 32 } |
| 33 |
| 34 // mappings_copy is used to keep track of which mappings have already been |
| 35 // satisfied (by nulling the |value| field). This is used to track when |
| 36 // blocks are mulitply defined. |
| 37 std::vector<PemBlockMapping> mappings_copy(mappings, |
| 38 mappings + mappings_length); |
| 39 |
| 40 // Build the |pem_headers| vector needed for PEMTokenzier. |
| 41 std::vector<std::string> pem_headers; |
| 42 for (const auto& mapping : mappings_copy) { |
| 43 pem_headers.push_back(mapping.block_name); |
| 44 } |
| 45 |
| 46 PEMTokenizer pem_tokenizer(file_data, pem_headers); |
| 47 while (pem_tokenizer.GetNext()) { |
| 48 for (auto& mapping : mappings_copy) { |
| 49 // Find the mapping for this block type. |
| 50 if (pem_tokenizer.block_type() == mapping.block_name) { |
| 51 if (!mapping.value) { |
| 52 return ::testing::AssertionFailure() |
| 53 << "PEM block defined multiple times: " << mapping.block_name; |
| 54 } |
| 55 |
| 56 // Copy the data to the result. |
| 57 mapping.value->assign(pem_tokenizer.data()); |
| 58 |
| 59 // Mark the mapping as having been satisfied. |
| 60 mapping.value = nullptr; |
| 61 } |
| 62 } |
| 63 } |
| 64 |
| 65 // Ensure that all specified blocks were found. |
| 66 for (const auto& mapping : mappings_copy) { |
| 67 if (mapping.value) { |
| 68 return ::testing::AssertionFailure() << "PEM block missing: " |
| 69 << mapping.block_name; |
| 70 } |
| 71 } |
| 72 |
| 73 return ::testing::AssertionSuccess(); |
| 74 } |
| 75 |
| 76 } // namespace net |
OLD | NEW |