OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/common/extensions/manifest_tests/extension_manifest_test.h" | 5 #include "extensions/common/manifest_test.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/common/chrome_paths.h" | |
14 #include "extensions/common/extension_l10n_util.h" | 13 #include "extensions/common/extension_l10n_util.h" |
| 14 #include "extensions/common/extension_paths.h" |
15 #include "extensions/common/test_util.h" | 15 #include "extensions/common/test_util.h" |
16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
17 | 17 |
18 using extensions::Extension; | 18 namespace extensions { |
19 | |
20 namespace { | 19 namespace { |
21 | 20 |
22 // If filename is a relative path, LoadManifestFile will treat it relative to | 21 // |manifest_path| is an absolute path to a manifest file. |
23 // the appropriate test directory. | 22 base::DictionaryValue* LoadManifestFile(const base::FilePath& manifest_path, |
24 base::DictionaryValue* LoadManifestFile(const base::FilePath& filename_path, | |
25 std::string* error) { | 23 std::string* error) { |
26 base::FilePath extension_path; | 24 base::FilePath extension_path = manifest_path.DirName(); |
27 base::FilePath manifest_path; | |
28 | |
29 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path); | |
30 manifest_path = manifest_path.Append(filename_path); | |
31 extension_path = manifest_path.DirName(); | |
32 | 25 |
33 EXPECT_TRUE(base::PathExists(manifest_path)) << | 26 EXPECT_TRUE(base::PathExists(manifest_path)) << |
34 "Couldn't find " << manifest_path.value(); | 27 "Couldn't find " << manifest_path.value(); |
35 | 28 |
36 JSONFileValueSerializer serializer(manifest_path); | 29 JSONFileValueSerializer serializer(manifest_path); |
37 base::DictionaryValue* manifest = | 30 base::DictionaryValue* manifest = |
38 static_cast<base::DictionaryValue*>(serializer.Deserialize(NULL, error)); | 31 static_cast<base::DictionaryValue*>(serializer.Deserialize(NULL, error)); |
39 | 32 |
40 // Most unit tests don't need localization, and they'll fail if we try to | 33 // Most unit tests don't need localization, and they'll fail if we try to |
41 // localize them, since their manifests don't have a default_locale key. | 34 // localize them, since their manifests don't have a default_locale key. |
42 // Only localize manifests that indicate they want to be localized. | 35 // Only localize manifests that indicate they want to be localized. |
43 // Calling LocalizeExtension at this point mirrors file_util::LoadExtension. | 36 // Calling LocalizeExtension at this point mirrors file_util::LoadExtension. |
44 if (manifest && | 37 if (manifest && |
45 filename_path.value().find(FILE_PATH_LITERAL("localized")) != | 38 manifest_path.value().find(FILE_PATH_LITERAL("localized")) != |
46 std::string::npos) | 39 std::string::npos) |
47 extension_l10n_util::LocalizeExtension(extension_path, manifest, error); | 40 extension_l10n_util::LocalizeExtension(extension_path, manifest, error); |
48 | 41 |
49 return manifest; | 42 return manifest; |
50 } | 43 } |
51 | 44 |
52 } // namespace | 45 } // namespace |
53 | 46 |
54 ExtensionManifestTest::ExtensionManifestTest() | 47 ManifestTest::ManifestTest() |
55 : enable_apps_(true), | 48 : enable_apps_(true) { |
56 // UNKNOWN == trunk. | 49 } |
57 current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {} | 50 |
| 51 ManifestTest::~ManifestTest() { |
| 52 } |
58 | 53 |
59 // Helper class that simplifies creating methods that take either a filename | 54 // Helper class that simplifies creating methods that take either a filename |
60 // to a manifest or the manifest itself. | 55 // to a manifest or the manifest itself. |
61 ExtensionManifestTest::Manifest::Manifest(const char* name) | 56 ManifestTest::ManifestData::ManifestData(const char* name) |
62 : name_(name), manifest_(NULL) { | 57 : name_(name), manifest_(NULL) { |
63 } | 58 } |
64 | 59 |
65 ExtensionManifestTest::Manifest::Manifest(base::DictionaryValue* manifest, | 60 ManifestTest::ManifestData::ManifestData(base::DictionaryValue* manifest, |
66 const char* name) | 61 const char* name) |
67 : name_(name), manifest_(manifest) { | 62 : name_(name), manifest_(manifest) { |
68 CHECK(manifest_) << "Manifest NULL"; | 63 CHECK(manifest_) << "Manifest NULL"; |
69 } | 64 } |
70 | 65 |
71 ExtensionManifestTest::Manifest::Manifest( | 66 ManifestTest::ManifestData::ManifestData( |
72 scoped_ptr<base::DictionaryValue> manifest) | 67 scoped_ptr<base::DictionaryValue> manifest) |
73 : manifest_(manifest.get()), manifest_holder_(manifest.Pass()) { | 68 : manifest_(manifest.get()), manifest_holder_(manifest.Pass()) { |
74 CHECK(manifest_) << "Manifest NULL"; | 69 CHECK(manifest_) << "Manifest NULL"; |
75 } | 70 } |
76 | 71 |
77 ExtensionManifestTest::Manifest::Manifest(const Manifest& m) { | 72 ManifestTest::ManifestData::ManifestData(const ManifestData& m) { |
78 NOTREACHED(); | 73 NOTREACHED(); |
79 } | 74 } |
80 | 75 |
81 ExtensionManifestTest::Manifest::~Manifest() { | 76 ManifestTest::ManifestData::~ManifestData() { |
82 } | 77 } |
83 | 78 |
84 base::DictionaryValue* ExtensionManifestTest::Manifest::GetManifest( | 79 base::DictionaryValue* ManifestTest::ManifestData::GetManifest( |
85 char const* test_data_dir, std::string* error) const { | 80 base::FilePath test_data_dir, std::string* error) const { |
86 if (manifest_) | 81 if (manifest_) |
87 return manifest_; | 82 return manifest_; |
88 | 83 |
89 base::FilePath filename_path; | 84 base::FilePath manifest_path = test_data_dir.AppendASCII(name_); |
90 filename_path = filename_path.AppendASCII("extensions") | 85 manifest_ = LoadManifestFile(manifest_path, error); |
91 .AppendASCII(test_data_dir) | |
92 .AppendASCII(name_); | |
93 manifest_ = LoadManifestFile(filename_path, error); | |
94 manifest_holder_.reset(manifest_); | 86 manifest_holder_.reset(manifest_); |
95 return manifest_; | 87 return manifest_; |
96 } | 88 } |
97 | 89 |
98 char const* ExtensionManifestTest::test_data_dir() { | 90 base::FilePath ManifestTest::GetTestDataDir() { |
99 return "manifest_tests"; | 91 base::FilePath path; |
| 92 PathService::Get(DIR_TEST_DATA, &path); |
| 93 return path.AppendASCII("manifest_tests"); |
100 } | 94 } |
101 | 95 |
102 scoped_ptr<base::DictionaryValue> ExtensionManifestTest::LoadManifest( | 96 scoped_ptr<base::DictionaryValue> ManifestTest::LoadManifest( |
103 char const* manifest_name, std::string* error) { | 97 char const* manifest_name, std::string* error) { |
104 base::FilePath filename_path; | 98 base::FilePath manifest_path = GetTestDataDir().AppendASCII(manifest_name); |
105 filename_path = filename_path.AppendASCII("extensions") | 99 return make_scoped_ptr(LoadManifestFile(manifest_path, error)); |
106 .AppendASCII(test_data_dir()) | |
107 .AppendASCII(manifest_name); | |
108 return make_scoped_ptr(LoadManifestFile(filename_path, error)); | |
109 } | 100 } |
110 | 101 |
111 scoped_refptr<Extension> ExtensionManifestTest::LoadExtension( | 102 scoped_refptr<Extension> ManifestTest::LoadExtension( |
112 const Manifest& manifest, | 103 const ManifestData& manifest, |
113 std::string* error, | 104 std::string* error, |
114 extensions::Manifest::Location location, | 105 extensions::Manifest::Location location, |
115 int flags) { | 106 int flags) { |
116 base::DictionaryValue* value = manifest.GetManifest(test_data_dir(), error); | 107 base::FilePath test_data_dir = GetTestDataDir(); |
| 108 base::DictionaryValue* value = manifest.GetManifest(test_data_dir, error); |
117 if (!value) | 109 if (!value) |
118 return NULL; | 110 return NULL; |
119 base::FilePath path; | 111 return Extension::Create( |
120 PathService::Get(chrome::DIR_TEST_DATA, &path); | 112 test_data_dir.DirName(), location, *value, flags, error); |
121 path = path.AppendASCII("extensions").AppendASCII(test_data_dir()); | |
122 return Extension::Create(path.DirName(), location, *value, flags, error); | |
123 } | 113 } |
124 | 114 |
125 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess( | 115 scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess( |
126 const Manifest& manifest, | 116 const ManifestData& manifest, |
127 extensions::Manifest::Location location, | 117 extensions::Manifest::Location location, |
128 int flags) { | 118 int flags) { |
129 std::string error; | 119 std::string error; |
130 scoped_refptr<Extension> extension = | 120 scoped_refptr<Extension> extension = |
131 LoadExtension(manifest, &error, location, flags); | 121 LoadExtension(manifest, &error, location, flags); |
132 EXPECT_TRUE(extension.get()) << manifest.name(); | 122 EXPECT_TRUE(extension.get()) << manifest.name(); |
133 EXPECT_EQ("", error) << manifest.name(); | 123 EXPECT_EQ("", error) << manifest.name(); |
134 return extension; | 124 return extension; |
135 } | 125 } |
136 | 126 |
137 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess( | 127 scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess( |
138 char const* manifest_name, | 128 char const* manifest_name, |
139 extensions::Manifest::Location location, | 129 extensions::Manifest::Location location, |
140 int flags) { | 130 int flags) { |
141 return LoadAndExpectSuccess(Manifest(manifest_name), location, flags); | 131 return LoadAndExpectSuccess(ManifestData(manifest_name), location, flags); |
142 } | 132 } |
143 | 133 |
144 scoped_refptr<Extension> ExtensionManifestTest::LoadFromStringAndExpectSuccess( | 134 scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning( |
145 char const* manifest_json) { | 135 const ManifestData& manifest, |
146 return LoadAndExpectSuccess( | |
147 Manifest(extensions::test_util::ParseJsonDictionaryWithSingleQuotes( | |
148 manifest_json))); | |
149 } | |
150 | |
151 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectWarning( | |
152 const Manifest& manifest, | |
153 const std::string& expected_warning, | 136 const std::string& expected_warning, |
154 extensions::Manifest::Location location, | 137 extensions::Manifest::Location location, |
155 int flags) { | 138 int flags) { |
156 std::string error; | 139 std::string error; |
157 scoped_refptr<Extension> extension = | 140 scoped_refptr<Extension> extension = |
158 LoadExtension(manifest, &error, location, flags); | 141 LoadExtension(manifest, &error, location, flags); |
159 EXPECT_TRUE(extension.get()) << manifest.name(); | 142 EXPECT_TRUE(extension.get()) << manifest.name(); |
160 EXPECT_EQ("", error) << manifest.name(); | 143 EXPECT_EQ("", error) << manifest.name(); |
161 EXPECT_EQ(1u, extension->install_warnings().size()); | 144 EXPECT_EQ(1u, extension->install_warnings().size()); |
162 EXPECT_EQ(expected_warning, extension->install_warnings()[0].message); | 145 EXPECT_EQ(expected_warning, extension->install_warnings()[0].message); |
163 return extension; | 146 return extension; |
164 } | 147 } |
165 | 148 |
166 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectWarning( | 149 scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning( |
167 char const* manifest_name, | 150 char const* manifest_name, |
168 const std::string& expected_warning, | 151 const std::string& expected_warning, |
169 extensions::Manifest::Location location, | 152 extensions::Manifest::Location location, |
170 int flags) { | 153 int flags) { |
171 return LoadAndExpectWarning( | 154 return LoadAndExpectWarning( |
172 Manifest(manifest_name), expected_warning, location, flags); | 155 ManifestData(manifest_name), expected_warning, location, flags); |
173 } | 156 } |
174 | 157 |
175 void ExtensionManifestTest::VerifyExpectedError( | 158 void ManifestTest::VerifyExpectedError( |
176 Extension* extension, | 159 Extension* extension, |
177 const std::string& name, | 160 const std::string& name, |
178 const std::string& error, | 161 const std::string& error, |
179 const std::string& expected_error) { | 162 const std::string& expected_error) { |
180 EXPECT_FALSE(extension) << | 163 EXPECT_FALSE(extension) << |
181 "Expected failure loading extension '" << name << | 164 "Expected failure loading extension '" << name << |
182 "', but didn't get one."; | 165 "', but didn't get one."; |
183 EXPECT_TRUE(MatchPattern(error, expected_error)) << name << | 166 EXPECT_TRUE(MatchPattern(error, expected_error)) << name << |
184 " expected '" << expected_error << "' but got '" << error << "'"; | 167 " expected '" << expected_error << "' but got '" << error << "'"; |
185 } | 168 } |
186 | 169 |
187 void ExtensionManifestTest::LoadAndExpectError( | 170 void ManifestTest::LoadAndExpectError( |
188 const Manifest& manifest, | 171 const ManifestData& manifest, |
189 const std::string& expected_error, | 172 const std::string& expected_error, |
190 extensions::Manifest::Location location, | 173 extensions::Manifest::Location location, |
191 int flags) { | 174 int flags) { |
192 std::string error; | 175 std::string error; |
193 scoped_refptr<Extension> extension( | 176 scoped_refptr<Extension> extension( |
194 LoadExtension(manifest, &error, location, flags)); | 177 LoadExtension(manifest, &error, location, flags)); |
195 VerifyExpectedError(extension.get(), manifest.name(), error, | 178 VerifyExpectedError(extension.get(), manifest.name(), error, |
196 expected_error); | 179 expected_error); |
197 } | 180 } |
198 | 181 |
199 void ExtensionManifestTest::LoadAndExpectError( | 182 void ManifestTest::LoadAndExpectError( |
200 char const* manifest_name, | 183 char const* manifest_name, |
201 const std::string& expected_error, | 184 const std::string& expected_error, |
202 extensions::Manifest::Location location, | 185 extensions::Manifest::Location location, |
203 int flags) { | 186 int flags) { |
204 return LoadAndExpectError( | 187 return LoadAndExpectError( |
205 Manifest(manifest_name), expected_error, location, flags); | 188 ManifestData(manifest_name), expected_error, location, flags); |
206 } | 189 } |
207 | 190 |
208 void ExtensionManifestTest::LoadFromStringAndExpectError( | 191 void ManifestTest::AddPattern(extensions::URLPatternSet* extent, |
209 char const* manifest_json, | |
210 const std::string& expected_error) { | |
211 return LoadAndExpectError( | |
212 Manifest(extensions::test_util::ParseJsonDictionaryWithSingleQuotes( | |
213 manifest_json)), | |
214 expected_error); | |
215 } | |
216 | |
217 void ExtensionManifestTest::AddPattern(extensions::URLPatternSet* extent, | |
218 const std::string& pattern) { | 192 const std::string& pattern) { |
219 int schemes = URLPattern::SCHEME_ALL; | 193 int schemes = URLPattern::SCHEME_ALL; |
220 extent->AddPattern(URLPattern(schemes, pattern)); | 194 extent->AddPattern(URLPattern(schemes, pattern)); |
221 } | 195 } |
222 | 196 |
223 ExtensionManifestTest::Testcase::Testcase( | 197 ManifestTest::Testcase::Testcase( |
224 std::string manifest_filename, | 198 std::string manifest_filename, |
225 std::string expected_error, | 199 std::string expected_error, |
226 extensions::Manifest::Location location, | 200 extensions::Manifest::Location location, |
227 int flags) | 201 int flags) |
228 : manifest_filename_(manifest_filename), | 202 : manifest_filename_(manifest_filename), |
229 expected_error_(expected_error), | 203 expected_error_(expected_error), |
230 location_(location), flags_(flags) { | 204 location_(location), flags_(flags) { |
231 } | 205 } |
232 | 206 |
233 ExtensionManifestTest::Testcase::Testcase(std::string manifest_filename, | 207 ManifestTest::Testcase::Testcase(std::string manifest_filename, |
234 std::string expected_error) | 208 std::string expected_error) |
235 : manifest_filename_(manifest_filename), | 209 : manifest_filename_(manifest_filename), |
236 expected_error_(expected_error), | 210 expected_error_(expected_error), |
237 location_(extensions::Manifest::INTERNAL), | 211 location_(extensions::Manifest::INTERNAL), |
238 flags_(Extension::NO_FLAGS) { | 212 flags_(Extension::NO_FLAGS) { |
239 } | 213 } |
240 | 214 |
241 ExtensionManifestTest::Testcase::Testcase(std::string manifest_filename) | 215 ManifestTest::Testcase::Testcase(std::string manifest_filename) |
242 : manifest_filename_(manifest_filename), | 216 : manifest_filename_(manifest_filename), |
243 location_(extensions::Manifest::INTERNAL), | 217 location_(extensions::Manifest::INTERNAL), |
244 flags_(Extension::NO_FLAGS) {} | 218 flags_(Extension::NO_FLAGS) {} |
245 | 219 |
246 ExtensionManifestTest::Testcase::Testcase( | 220 ManifestTest::Testcase::Testcase( |
247 std::string manifest_filename, | 221 std::string manifest_filename, |
248 extensions::Manifest::Location location, | 222 extensions::Manifest::Location location, |
249 int flags) | 223 int flags) |
250 : manifest_filename_(manifest_filename), | 224 : manifest_filename_(manifest_filename), |
251 location_(location), | 225 location_(location), |
252 flags_(flags) {} | 226 flags_(flags) {} |
253 | 227 |
254 void ExtensionManifestTest::RunTestcases(const Testcase* testcases, | 228 void ManifestTest::RunTestcases(const Testcase* testcases, |
255 size_t num_testcases, | 229 size_t num_testcases, |
256 ExpectType type) { | 230 ExpectType type) { |
257 for (size_t i = 0; i < num_testcases; ++i) | 231 for (size_t i = 0; i < num_testcases; ++i) |
258 RunTestcase(testcases[i], type); | 232 RunTestcase(testcases[i], type); |
259 } | 233 } |
260 | 234 |
261 void ExtensionManifestTest::RunTestcase(const Testcase& testcase, | 235 void ManifestTest::RunTestcase(const Testcase& testcase, |
262 ExpectType type) { | 236 ExpectType type) { |
263 switch (type) { | 237 switch (type) { |
264 case EXPECT_TYPE_ERROR: | 238 case EXPECT_TYPE_ERROR: |
265 LoadAndExpectError(testcase.manifest_filename_.c_str(), | 239 LoadAndExpectError(testcase.manifest_filename_.c_str(), |
266 testcase.expected_error_, | 240 testcase.expected_error_, |
267 testcase.location_, | 241 testcase.location_, |
268 testcase.flags_); | 242 testcase.flags_); |
269 break; | 243 break; |
270 case EXPECT_TYPE_WARNING: | 244 case EXPECT_TYPE_WARNING: |
271 LoadAndExpectWarning(testcase.manifest_filename_.c_str(), | 245 LoadAndExpectWarning(testcase.manifest_filename_.c_str(), |
272 testcase.expected_error_, | 246 testcase.expected_error_, |
273 testcase.location_, | 247 testcase.location_, |
274 testcase.flags_); | 248 testcase.flags_); |
275 break; | 249 break; |
276 case EXPECT_TYPE_SUCCESS: | 250 case EXPECT_TYPE_SUCCESS: |
277 LoadAndExpectSuccess(testcase.manifest_filename_.c_str(), | 251 LoadAndExpectSuccess(testcase.manifest_filename_.c_str(), |
278 testcase.location_, | 252 testcase.location_, |
279 testcase.flags_); | 253 testcase.flags_); |
280 break; | 254 break; |
281 } | 255 } |
282 } | 256 } |
| 257 |
| 258 } // namespace extensions |
OLD | NEW |