OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "components/crx_file/crx_verifier.h" | |
6 #include "base/base_paths.h" | |
7 #include "base/files/file_path.h" | |
8 #include "base/path_service.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace { | |
13 | |
14 base::FilePath TestFile(const std::string& file) { | |
15 base::FilePath path; | |
16 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
17 return path.AppendASCII("components") | |
18 .AppendASCII("test") | |
19 .AppendASCII("data") | |
20 .AppendASCII("crx_file") | |
21 .AppendASCII(file); | |
22 } | |
23 | |
24 constexpr char kOjjHash[] = "ojjgnpkioondelmggbekfhllhdaimnho"; | |
25 constexpr char kOjjKey[] = | |
26 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA230uN7vYDEhdDlb4/" | |
27 "+pg2pfL8p0FFzCF/O146NB3D5dPKuLbnNphn0OUzOrDzR/Z1XLVDlDyiA6xnb+qeRp7H8n7Wk/" | |
28 "/gvVDNArZyForlVqWdaHLhl4dyZoNJwPKsggf30p/" | |
29 "MxCbNfy2rzFujzn2nguOrJKzWvNt0BFqssrBpzOQl69blBezE2ZYGOnYW8mPgQV29ekIgOfJk2" | |
30 "GgXoJBQQRRsjoPmUY7GDuEKudEB/" | |
31 "CmWh3+" | |
32 "mCsHBHFWbqtGhSN4YCAw3DYQzwdTcIVaIA8f2Uo4AZ4INKkrEPRL8o9mZDYtO2YHIQg8pMSRMa" | |
33 "6AawBNYi9tZScnmgl5L1qE6z5oIwIDAQAB"; | |
34 | |
35 } // namespace | |
36 | |
37 namespace crx_file { | |
38 | |
39 class CrxVerifierTest : public testing::Test { | |
Devlin
2017/05/23 20:32:41
if we don't need to provide custom behavior, prefe
waffles
2017/05/23 23:05:13
Done.
| |
40 public: | |
41 CrxVerifierTest() = default; | |
42 }; | |
43 | |
44 TEST_F(CrxVerifierTest, ValidFullCrx2) { | |
45 const std::vector<std::vector<uint8_t>> keys; | |
46 const std::vector<uint8_t> hash; | |
47 std::string public_key; | |
48 std::string crx_id; | |
49 | |
50 EXPECT_EQ(VerifierResult::OK_FULL, | |
51 Verify(TestFile("valid.crx2"), VerifierFormat::CRX2_OR_CRX3, keys, | |
52 hash, &public_key, &crx_id)); | |
53 EXPECT_EQ(std::string(kOjjHash), crx_id); | |
54 EXPECT_EQ(std::string(kOjjKey), public_key); | |
55 } | |
56 | |
57 TEST_F(CrxVerifierTest, ValidFullCrx3) { | |
58 const std::vector<std::vector<uint8_t>> keys; | |
59 const std::vector<uint8_t> hash; | |
60 std::string public_key = "UNSET"; | |
61 std::string crx_id = "UNSET"; | |
62 | |
63 EXPECT_EQ(VerifierResult::OK_FULL, Verify(TestFile("valid_no_publisher.crx3"), | |
64 VerifierFormat::CRX2_OR_CRX3, keys, | |
65 hash, &public_key, &crx_id)); | |
66 EXPECT_EQ(std::string(kOjjHash), crx_id); | |
67 EXPECT_EQ(std::string(kOjjKey), public_key); | |
68 | |
69 public_key = "UNSET"; | |
70 crx_id = "UNSET"; | |
71 EXPECT_EQ(VerifierResult::OK_FULL, | |
72 Verify(TestFile("valid_no_publisher.crx3"), VerifierFormat::CRX3, | |
73 keys, hash, &public_key, &crx_id)); | |
74 EXPECT_EQ(std::string(kOjjHash), crx_id); | |
75 EXPECT_EQ(std::string(kOjjKey), public_key); | |
76 } | |
77 | |
78 TEST_F(CrxVerifierTest, Crx3RejectsCrx2) { | |
79 const std::vector<std::vector<uint8_t>> keys; | |
80 const std::vector<uint8_t> hash; | |
81 std::string public_key = "UNSET"; | |
82 std::string crx_id = "UNSET"; | |
83 | |
84 EXPECT_EQ(VerifierResult::ERROR_HEADER_INVALID, | |
85 Verify(TestFile("valid.crx2"), VerifierFormat::CRX3, keys, hash, | |
86 &public_key, &crx_id)); | |
87 EXPECT_EQ("UNSET", crx_id); | |
88 EXPECT_EQ("UNSET", public_key); | |
89 } | |
90 | |
91 TEST_F(CrxVerifierTest, VerifiesFileHash) { | |
92 const std::vector<std::vector<uint8_t>> keys; | |
93 std::vector<uint8_t> hash; | |
94 EXPECT_TRUE(base::HexStringToBytes( | |
95 "d033c510f9e4ee081ccb60ea2bf530dc2e5cb0e71085b55503c8b13b74515fe4", | |
96 &hash)); | |
97 std::string public_key = "UNSET"; | |
98 std::string crx_id = "UNSET"; | |
99 | |
100 EXPECT_EQ(VerifierResult::OK_FULL, Verify(TestFile("valid_no_publisher.crx3"), | |
101 VerifierFormat::CRX2_OR_CRX3, keys, | |
102 hash, &public_key, &crx_id)); | |
103 EXPECT_EQ(std::string(kOjjHash), crx_id); | |
104 EXPECT_EQ(std::string(kOjjKey), public_key); | |
105 | |
106 hash.clear(); | |
107 EXPECT_TRUE(base::HexStringToBytes(std::string(32, '0'), &hash)); | |
108 public_key = "UNSET"; | |
109 crx_id = "UNSET"; | |
110 EXPECT_EQ(VerifierResult::ERROR_EXPECTED_HASH_INVALID, | |
111 Verify(TestFile("valid_no_publisher.crx3"), VerifierFormat::CRX3, | |
112 keys, hash, &public_key, &crx_id)); | |
113 EXPECT_EQ("UNSET", crx_id); | |
114 EXPECT_EQ("UNSET", public_key); | |
115 | |
116 hash.clear(); | |
117 EXPECT_TRUE(base::HexStringToBytes(std::string(64, '0'), &hash)); | |
118 public_key = "UNSET"; | |
119 crx_id = "UNSET"; | |
120 EXPECT_EQ(VerifierResult::ERROR_FILE_HASH_FAILED, | |
121 Verify(TestFile("valid_no_publisher.crx3"), VerifierFormat::CRX3, | |
122 keys, hash, &public_key, &crx_id)); | |
123 EXPECT_EQ("UNSET", crx_id); | |
124 EXPECT_EQ("UNSET", public_key); | |
125 } | |
126 | |
127 TEST_F(CrxVerifierTest, ChecksRequiredKeyHashes) { | |
128 const std::vector<uint8_t> hash; | |
129 | |
130 std::vector<uint8_t> good_key; | |
131 EXPECT_TRUE(base::HexStringToBytes( | |
132 "e996dfa8eed34bc6614a57bb7308cd7e519bcc690841e1969f7cb173ef16800a", | |
133 &good_key)); | |
134 const std::vector<std::vector<uint8_t>> good_keys = {good_key}; | |
135 std::string public_key = "UNSET"; | |
136 std::string crx_id = "UNSET"; | |
137 EXPECT_EQ( | |
138 VerifierResult::OK_FULL, | |
139 Verify(TestFile("valid_no_publisher.crx3"), VerifierFormat::CRX2_OR_CRX3, | |
140 good_keys, hash, &public_key, &crx_id)); | |
141 EXPECT_EQ(std::string(kOjjHash), crx_id); | |
142 EXPECT_EQ(std::string(kOjjKey), public_key); | |
143 | |
144 std::vector<uint8_t> bad_key; | |
145 EXPECT_TRUE(base::HexStringToBytes(std::string(64, '0'), &bad_key)); | |
146 const std::vector<std::vector<uint8_t>> bad_keys = {bad_key}; | |
147 public_key = "UNSET"; | |
148 crx_id = "UNSET"; | |
149 EXPECT_EQ(VerifierResult::ERROR_REQUIRED_PROOF_MISSING, | |
150 Verify(TestFile("valid_no_publisher.crx3"), VerifierFormat::CRX3, | |
151 bad_keys, hash, &public_key, &crx_id)); | |
152 EXPECT_EQ("UNSET", crx_id); | |
153 EXPECT_EQ("UNSET", public_key); | |
154 } | |
155 | |
156 TEST_F(CrxVerifierTest, ChecksPinnedKey) { | |
157 const std::vector<uint8_t> hash; | |
158 const std::vector<std::vector<uint8_t>> keys; | |
159 std::string public_key = "UNSET"; | |
160 std::string crx_id = "UNSET"; | |
161 EXPECT_EQ(VerifierResult::OK_FULL, | |
162 Verify(TestFile("valid_publisher.crx3"), | |
163 VerifierFormat::CRX3_WITH_PUBLISHER_PROOF, keys, hash, | |
164 &public_key, &crx_id)); | |
165 EXPECT_EQ(std::string(kOjjHash), crx_id); | |
166 EXPECT_EQ(std::string(kOjjKey), public_key); | |
167 | |
168 public_key = "UNSET"; | |
169 crx_id = "UNSET"; | |
170 EXPECT_EQ(VerifierResult::ERROR_REQUIRED_PROOF_MISSING, | |
171 Verify(TestFile("valid_no_publisher.crx3"), | |
172 VerifierFormat::CRX3_WITH_PUBLISHER_PROOF, keys, hash, | |
173 &public_key, &crx_id)); | |
174 EXPECT_EQ("UNSET", crx_id); | |
175 EXPECT_EQ("UNSET", public_key); | |
176 } | |
177 | |
178 TEST_F(CrxVerifierTest, NullptrSafe) { | |
179 const std::vector<uint8_t> hash; | |
180 const std::vector<std::vector<uint8_t>> keys; | |
181 EXPECT_EQ(VerifierResult::OK_FULL, | |
182 Verify(TestFile("valid_publisher.crx3"), | |
183 VerifierFormat::CRX3_WITH_PUBLISHER_PROOF, keys, hash, | |
184 nullptr, nullptr)); | |
185 } | |
186 | |
187 TEST_F(CrxVerifierTest, RequiresDeveloperKey) { | |
188 const std::vector<uint8_t> hash; | |
189 const std::vector<std::vector<uint8_t>> keys; | |
190 std::string public_key = "UNSET"; | |
191 std::string crx_id = "UNSET"; | |
192 EXPECT_EQ(VerifierResult::ERROR_REQUIRED_PROOF_MISSING, | |
193 Verify(TestFile("unsigned.crx3"), VerifierFormat::CRX2_OR_CRX3, | |
194 keys, hash, &public_key, &crx_id)); | |
195 EXPECT_EQ("UNSET", crx_id); | |
196 EXPECT_EQ("UNSET", public_key); | |
197 } | |
198 | |
199 } // namespace crx_file | |
OLD | NEW |