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/extension_file_util.h" | 5 #include "chrome/common/extensions/extension_file_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 .AppendASCII("bad_encoding"); | 208 .AppendASCII("bad_encoding"); |
209 | 209 |
210 std::string error; | 210 std::string error; |
211 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 211 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
212 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); | 212 install_dir, Extension::LOAD, Extension::STRICT_ERROR_CHECKS, &error)); |
213 ASSERT_TRUE(extension == NULL); | 213 ASSERT_TRUE(extension == NULL); |
214 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " | 214 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " |
215 "It isn't UTF-8 encoded.", error.c_str()); | 215 "It isn't UTF-8 encoded.", error.c_str()); |
216 } | 216 } |
217 | 217 |
| 218 TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) { |
218 #define URL_PREFIX "chrome-extension://extension-id/" | 219 #define URL_PREFIX "chrome-extension://extension-id/" |
219 | |
220 TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) { | |
221 struct TestCase { | 220 struct TestCase { |
222 const char* url; | 221 const char* url; |
223 const char* expected_relative_path; | 222 const char* expected_relative_path; |
224 } test_cases[] = { | 223 } test_cases[] = { |
225 { URL_PREFIX "simple.html", | 224 { URL_PREFIX "simple.html", |
226 "simple.html" }, | 225 "simple.html" }, |
227 { URL_PREFIX "directory/to/file.html", | 226 { URL_PREFIX "directory/to/file.html", |
228 "directory/to/file.html" }, | 227 "directory/to/file.html" }, |
229 { URL_PREFIX "escape%20spaces.html", | 228 { URL_PREFIX "escape%20spaces.html", |
230 "escape spaces.html" }, | 229 "escape spaces.html" }, |
231 { URL_PREFIX "%C3%9Cber.html", | 230 { URL_PREFIX "%C3%9Cber.html", |
232 "\xC3\x9C" "ber.html" }, | 231 "\xC3\x9C" "ber.html" }, |
233 #if defined(OS_WIN) | 232 #if defined(OS_WIN) |
234 { URL_PREFIX "C%3A/simple.html", | 233 { URL_PREFIX "C%3A/simple.html", |
235 "" }, | 234 "" }, |
236 #endif | 235 #endif |
237 { URL_PREFIX "////simple.html", | 236 { URL_PREFIX "////simple.html", |
238 "simple.html" }, | 237 "simple.html" }, |
239 { URL_PREFIX "/simple.html", | 238 { URL_PREFIX "/simple.html", |
240 "simple.html" }, | 239 "simple.html" }, |
241 { URL_PREFIX "\\simple.html", | 240 { URL_PREFIX "\\simple.html", |
242 "simple.html" }, | 241 "simple.html" }, |
243 { URL_PREFIX "\\\\foo\\simple.html", | 242 { URL_PREFIX "\\\\foo\\simple.html", |
244 "foo/simple.html" }, | 243 "foo/simple.html" }, |
245 }; | 244 }; |
| 245 #undef URL_PREFIX |
246 | 246 |
247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
248 GURL url(test_cases[i].url); | 248 GURL url(test_cases[i].url); |
249 #if defined(OS_POSIX) | 249 #if defined(OS_POSIX) |
250 FilePath expected_path(test_cases[i].expected_relative_path); | 250 FilePath expected_path(test_cases[i].expected_relative_path); |
251 #elif defined(OS_WIN) | 251 #elif defined(OS_WIN) |
252 FilePath expected_path(UTF8ToWide(test_cases[i].expected_relative_path)); | 252 FilePath expected_path(UTF8ToWide(test_cases[i].expected_relative_path)); |
253 #endif | 253 #endif |
254 | 254 |
255 FilePath actual_path = | 255 FilePath actual_path = |
256 extension_file_util::ExtensionURLToRelativeFilePath(url); | 256 extension_file_util::ExtensionURLToRelativeFilePath(url); |
257 EXPECT_FALSE(actual_path.IsAbsolute()) << | 257 EXPECT_FALSE(actual_path.IsAbsolute()) << |
258 " For the path " << actual_path.value(); | 258 " For the path " << actual_path.value(); |
259 EXPECT_EQ(expected_path.value(), actual_path.value()) << | 259 EXPECT_EQ(expected_path.value(), actual_path.value()) << |
260 " For the path " << url; | 260 " For the path " << url; |
261 } | 261 } |
262 } | 262 } |
263 | 263 |
| 264 TEST(ExtensionFileUtil, ExtensionResourceURLToFilePath) { |
| 265 // Setup filesystem for testing. |
| 266 FilePath root_path; |
| 267 ASSERT_TRUE(file_util::CreateNewTempDirectory( |
| 268 FILE_PATH_LITERAL(""), &root_path)); |
| 269 ASSERT_TRUE(file_util::AbsolutePath(&root_path)); |
| 270 |
| 271 FilePath api_path = root_path.Append(FILE_PATH_LITERAL("apiname")); |
| 272 ASSERT_TRUE(file_util::CreateDirectory(api_path)); |
| 273 |
| 274 const char data[] = "Test Data"; |
| 275 FilePath resource_path = api_path.Append(FILE_PATH_LITERAL("test.js")); |
| 276 ASSERT_TRUE(file_util::WriteFile(resource_path, data, sizeof(data))); |
| 277 resource_path = api_path.Append(FILE_PATH_LITERAL("escape spaces.js")); |
| 278 ASSERT_TRUE(file_util::WriteFile(resource_path, data, sizeof(data))); |
| 279 |
| 280 #ifdef FILE_PATH_USES_WIN_SEPARATORS |
| 281 #define SEP "\\" |
| 282 #else |
| 283 #define SEP "/" |
| 284 #endif |
| 285 #define URL_PREFIX "chrome-extension-resource://" |
| 286 struct TestCase { |
| 287 const char* url; |
| 288 const FilePath::CharType* expected_path; |
| 289 } test_cases[] = { |
| 290 { URL_PREFIX "apiname/test.js", |
| 291 FILE_PATH_LITERAL("test.js") }, |
| 292 { URL_PREFIX "/apiname/test.js", |
| 293 FILE_PATH_LITERAL("test.js") }, |
| 294 // Test % escape |
| 295 { URL_PREFIX "apiname/%74%65st.js", |
| 296 FILE_PATH_LITERAL("test.js") }, |
| 297 { URL_PREFIX "apiname/escape%20spaces.js", |
| 298 FILE_PATH_LITERAL("escape spaces.js") }, |
| 299 // Test file does not exist. |
| 300 { URL_PREFIX "apiname/directory/to/file.js", |
| 301 NULL }, |
| 302 // Test apiname/../../test.js |
| 303 { URL_PREFIX "apiname/../../test.js", |
| 304 FILE_PATH_LITERAL("test.js") }, |
| 305 { URL_PREFIX "apiname/..%2F../test.js", |
| 306 NULL }, |
| 307 { URL_PREFIX "apiname/f/../../../test.js", |
| 308 FILE_PATH_LITERAL("test.js") }, |
| 309 { URL_PREFIX "apiname/f%2F..%2F..%2F../test.js", |
| 310 NULL }, |
| 311 }; |
| 312 #undef SEP |
| 313 #undef URL_PREFIX |
| 314 |
| 315 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 316 GURL url(test_cases[i].url); |
| 317 FilePath expected_path; |
| 318 if (test_cases[i].expected_path) |
| 319 expected_path = root_path.Append(FILE_PATH_LITERAL("apiname")).Append( |
| 320 test_cases[i].expected_path); |
| 321 FilePath actual_path = |
| 322 extension_file_util::ExtensionResourceURLToFilePath(url, root_path); |
| 323 EXPECT_EQ(expected_path.value(), actual_path.value()) << |
| 324 " For the path " << url; |
| 325 } |
| 326 // Remove temp files. |
| 327 ASSERT_TRUE(file_util::Delete(root_path, true)); |
| 328 } |
| 329 |
264 static scoped_refptr<Extension> LoadExtensionManifest( | 330 static scoped_refptr<Extension> LoadExtensionManifest( |
265 DictionaryValue* manifest, | 331 DictionaryValue* manifest, |
266 const FilePath& manifest_dir, | 332 const FilePath& manifest_dir, |
267 Extension::Location location, | 333 Extension::Location location, |
268 int extra_flags, | 334 int extra_flags, |
269 std::string* error) { | 335 std::string* error) { |
270 scoped_refptr<Extension> extension = Extension::Create( | 336 scoped_refptr<Extension> extension = Extension::Create( |
271 manifest_dir, location, *manifest, extra_flags, error); | 337 manifest_dir, location, *manifest, extra_flags, error); |
272 return extension; | 338 return extension; |
273 } | 339 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error)); | 423 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error)); |
358 EXPECT_EQ(l10n_util::GetStringFUTF8( | 424 EXPECT_EQ(l10n_util::GetStringFUTF8( |
359 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, | 425 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, |
360 ASCIIToUTF16("http://google.com/foo.js")), | 426 ASCIIToUTF16("http://google.com/foo.js")), |
361 error); | 427 error); |
362 } | 428 } |
363 | 429 |
364 // TODO(aa): More tests as motivation allows. Maybe steal some from | 430 // TODO(aa): More tests as motivation allows. Maybe steal some from |
365 // ExtensionService? Many of them could probably be tested here without the | 431 // ExtensionService? Many of them could probably be tested here without the |
366 // MessageLoop shenanigans. | 432 // MessageLoop shenanigans. |
OLD | NEW |