OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "extensions/common/cast/cast_cert_validator_test_helpers.h" | |
6 | |
7 #include "base/files/file_util.h" | |
8 #include "base/path_service.h" | |
9 #include "extensions/common/extension_paths.h" | |
10 #include "net/cert/pem_tokenizer.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace cast_test_helpers { | |
14 | |
15 namespace { | |
16 | |
17 // Reads a file from the extensions test data directory | |
18 // (//src/extensions/test/data/) | |
19 std::string ReadTestFileToString(const base::StringPiece& file_name) { | |
20 base::FilePath filepath; | |
21 if (!PathService::Get(extensions::DIR_TEST_DATA, &filepath)) { | |
22 ADD_FAILURE() << "Couldn't retrieve test data root"; | |
23 return std::string(); | |
24 } | |
25 filepath = filepath.AppendASCII(file_name); | |
26 | |
27 // Read the full contents of the file. | |
28 std::string file_data; | |
29 if (!base::ReadFileToString(filepath, &file_data)) { | |
30 ADD_FAILURE() << "Couldn't read file: " << filepath.value(); | |
31 return std::string(); | |
32 } | |
33 | |
34 return file_data; | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 std::vector<std::string> ReadCertificateChainFromFile( | |
40 const base::StringPiece& file_name) { | |
41 std::string file_data = ReadTestFileToString(file_name); | |
42 | |
43 std::vector<std::string> pem_headers; | |
44 pem_headers.push_back("CERTIFICATE"); | |
45 | |
46 std::vector<std::string> certs; | |
47 net::PEMTokenizer pem_tokenizer(file_data, pem_headers); | |
48 while (pem_tokenizer.GetNext()) | |
49 certs.push_back(pem_tokenizer.data()); | |
50 | |
51 EXPECT_FALSE(certs.empty()); | |
52 return certs; | |
53 } | |
54 | |
55 SignatureTestData ReadSignatureTestData(const base::StringPiece& file_name) { | |
56 SignatureTestData result; | |
57 | |
58 std::string file_data = ReadTestFileToString(file_name); | |
59 EXPECT_FALSE(file_data.empty()); | |
60 | |
61 std::vector<std::string> pem_headers; | |
62 pem_headers.push_back("MESSAGE"); | |
63 pem_headers.push_back("SIGNATURE SHA1"); | |
64 pem_headers.push_back("SIGNATURE SHA256"); | |
65 | |
66 net::PEMTokenizer pem_tokenizer(file_data, pem_headers); | |
67 while (pem_tokenizer.GetNext()) { | |
68 const std::string& type = pem_tokenizer.block_type(); | |
69 const std::string& value = pem_tokenizer.data(); | |
70 | |
71 if (type == "MESSAGE") { | |
72 result.message = value; | |
73 } else if (type == "SIGNATURE SHA1") { | |
74 result.signature_sha1 = value; | |
75 } else if (type == "SIGNATURE SHA256") { | |
76 result.signature_sha256 = value; | |
77 } | |
78 } | |
79 | |
80 EXPECT_FALSE(result.message.empty()); | |
81 EXPECT_FALSE(result.signature_sha1.empty()); | |
82 EXPECT_FALSE(result.signature_sha256.empty()); | |
83 | |
84 return result; | |
85 } | |
86 | |
87 } // namespace cast_test_helpers | |
OLD | NEW |