| 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/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 226 |
| 227 std::string error; | 227 std::string error; |
| 228 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 228 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 229 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 229 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
| 230 ASSERT_TRUE(extension.get() == NULL); | 230 ASSERT_TRUE(extension.get() == NULL); |
| 231 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " | 231 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " |
| 232 "It isn't UTF-8 encoded.", | 232 "It isn't UTF-8 encoded.", |
| 233 error.c_str()); | 233 error.c_str()); |
| 234 } | 234 } |
| 235 | 235 |
| 236 TEST_F(ExtensionFileUtilTest, ExtensionURLToRelativeFilePath) { | |
| 237 #define URL_PREFIX "chrome-extension://extension-id/" | |
| 238 struct TestCase { | |
| 239 const char* url; | |
| 240 const char* expected_relative_path; | |
| 241 } test_cases[] = { | |
| 242 { URL_PREFIX "simple.html", | |
| 243 "simple.html" }, | |
| 244 { URL_PREFIX "directory/to/file.html", | |
| 245 "directory/to/file.html" }, | |
| 246 { URL_PREFIX "escape%20spaces.html", | |
| 247 "escape spaces.html" }, | |
| 248 { URL_PREFIX "%C3%9Cber.html", | |
| 249 "\xC3\x9C" "ber.html" }, | |
| 250 #if defined(OS_WIN) | |
| 251 { URL_PREFIX "C%3A/simple.html", | |
| 252 "" }, | |
| 253 #endif | |
| 254 { URL_PREFIX "////simple.html", | |
| 255 "simple.html" }, | |
| 256 { URL_PREFIX "/simple.html", | |
| 257 "simple.html" }, | |
| 258 { URL_PREFIX "\\simple.html", | |
| 259 "simple.html" }, | |
| 260 { URL_PREFIX "\\\\foo\\simple.html", | |
| 261 "foo/simple.html" }, | |
| 262 }; | |
| 263 #undef URL_PREFIX | |
| 264 | |
| 265 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | |
| 266 GURL url(test_cases[i].url); | |
| 267 base::FilePath expected_path = | |
| 268 base::FilePath::FromUTF8Unsafe(test_cases[i].expected_relative_path); | |
| 269 base::FilePath actual_path = | |
| 270 extension_file_util::ExtensionURLToRelativeFilePath(url); | |
| 271 EXPECT_FALSE(actual_path.IsAbsolute()) << | |
| 272 " For the path " << actual_path.value(); | |
| 273 EXPECT_EQ(expected_path.value(), actual_path.value()) << | |
| 274 " For the path " << url; | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 TEST_F(ExtensionFileUtilTest, ExtensionResourceURLToFilePath) { | |
| 279 // Setup filesystem for testing. | |
| 280 base::FilePath root_path; | |
| 281 ASSERT_TRUE(file_util::CreateNewTempDirectory( | |
| 282 base::FilePath::StringType(), &root_path)); | |
| 283 root_path = base::MakeAbsoluteFilePath(root_path); | |
| 284 ASSERT_FALSE(root_path.empty()); | |
| 285 | |
| 286 base::FilePath api_path = root_path.Append(FILE_PATH_LITERAL("apiname")); | |
| 287 ASSERT_TRUE(file_util::CreateDirectory(api_path)); | |
| 288 | |
| 289 const char data[] = "Test Data"; | |
| 290 base::FilePath resource_path = api_path.Append(FILE_PATH_LITERAL("test.js")); | |
| 291 ASSERT_TRUE(file_util::WriteFile(resource_path, data, sizeof(data))); | |
| 292 resource_path = api_path.Append(FILE_PATH_LITERAL("escape spaces.js")); | |
| 293 ASSERT_TRUE(file_util::WriteFile(resource_path, data, sizeof(data))); | |
| 294 | |
| 295 #ifdef FILE_PATH_USES_WIN_SEPARATORS | |
| 296 #define SEP "\\" | |
| 297 #else | |
| 298 #define SEP "/" | |
| 299 #endif | |
| 300 #define URL_PREFIX "chrome-extension-resource://" | |
| 301 struct TestCase { | |
| 302 const char* url; | |
| 303 const base::FilePath::CharType* expected_path; | |
| 304 } test_cases[] = { | |
| 305 { URL_PREFIX "apiname/test.js", | |
| 306 FILE_PATH_LITERAL("test.js") }, | |
| 307 { URL_PREFIX "/apiname/test.js", | |
| 308 FILE_PATH_LITERAL("test.js") }, | |
| 309 // Test % escape | |
| 310 { URL_PREFIX "apiname/%74%65st.js", | |
| 311 FILE_PATH_LITERAL("test.js") }, | |
| 312 { URL_PREFIX "apiname/escape%20spaces.js", | |
| 313 FILE_PATH_LITERAL("escape spaces.js") }, | |
| 314 // Test file does not exist. | |
| 315 { URL_PREFIX "apiname/directory/to/file.js", | |
| 316 NULL }, | |
| 317 // Test apiname/../../test.js | |
| 318 { URL_PREFIX "apiname/../../test.js", | |
| 319 FILE_PATH_LITERAL("test.js") }, | |
| 320 { URL_PREFIX "apiname/..%2F../test.js", | |
| 321 NULL }, | |
| 322 { URL_PREFIX "apiname/f/../../../test.js", | |
| 323 FILE_PATH_LITERAL("test.js") }, | |
| 324 { URL_PREFIX "apiname/f%2F..%2F..%2F../test.js", | |
| 325 NULL }, | |
| 326 }; | |
| 327 #undef SEP | |
| 328 #undef URL_PREFIX | |
| 329 | |
| 330 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | |
| 331 GURL url(test_cases[i].url); | |
| 332 base::FilePath expected_path; | |
| 333 if (test_cases[i].expected_path) | |
| 334 expected_path = root_path.Append(FILE_PATH_LITERAL("apiname")).Append( | |
| 335 test_cases[i].expected_path); | |
| 336 base::FilePath actual_path = | |
| 337 extension_file_util::ExtensionResourceURLToFilePath(url, root_path); | |
| 338 EXPECT_EQ(expected_path.value(), actual_path.value()) << | |
| 339 " For the path " << url; | |
| 340 } | |
| 341 // Remove temp files. | |
| 342 ASSERT_TRUE(base::DeleteFile(root_path, true)); | |
| 343 } | |
| 344 | |
| 345 static scoped_refptr<Extension> LoadExtensionManifest( | 236 static scoped_refptr<Extension> LoadExtensionManifest( |
| 346 base::DictionaryValue* manifest, | 237 base::DictionaryValue* manifest, |
| 347 const base::FilePath& manifest_dir, | 238 const base::FilePath& manifest_dir, |
| 348 Manifest::Location location, | 239 Manifest::Location location, |
| 349 int extra_flags, | 240 int extra_flags, |
| 350 std::string* error) { | 241 std::string* error) { |
| 351 scoped_refptr<Extension> extension = Extension::Create( | 242 scoped_refptr<Extension> extension = Extension::Create( |
| 352 manifest_dir, location, *manifest, extra_flags, error); | 243 manifest_dir, location, *manifest, extra_flags, error); |
| 353 return extension; | 244 return extension; |
| 354 } | 245 } |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( | 458 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( |
| 568 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 459 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
| 569 EXPECT_TRUE(extension3.get() == NULL); | 460 EXPECT_TRUE(extension3.get() == NULL); |
| 570 EXPECT_STREQ("Could not load icon 'icon.png' for page action.", | 461 EXPECT_STREQ("Could not load icon 'icon.png' for page action.", |
| 571 error.c_str()); | 462 error.c_str()); |
| 572 } | 463 } |
| 573 | 464 |
| 574 // TODO(aa): More tests as motivation allows. Maybe steal some from | 465 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 575 // ExtensionService? Many of them could probably be tested here without the | 466 // ExtensionService? Many of them could probably be tested here without the |
| 576 // MessageLoop shenanigans. | 467 // MessageLoop shenanigans. |
| OLD | NEW |