| 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" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/extensions/api/extension_action/browser_action_handler.h
" |
| 14 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_manifest_constants.h" | 16 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 17 #include "chrome/common/extensions/manifest_handler.h" |
| 16 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 20 | 22 |
| 21 using extensions::Extension; | 23 using extensions::Extension; |
| 22 | 24 |
| 23 namespace keys = extension_manifest_keys; | 25 namespace keys = extension_manifest_keys; |
| 24 | 26 |
| 27 class ExtensionFileUtilTest : public testing::Test { |
| 28 protected: |
| 29 virtual void SetUp() OVERRIDE { |
| 30 testing::Test::SetUp(); |
| 31 extensions::ManifestHandler::Register( |
| 32 extension_manifest_keys::kBrowserAction, |
| 33 new extensions::BrowserActionHandler); |
| 34 } |
| 35 }; |
| 36 |
| 25 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 26 // http://crbug.com/106381 | 38 // http://crbug.com/106381 |
| 27 #define InstallUninstallGarbageCollect DISABLED_InstallUninstallGarbageCollect | 39 #define InstallUninstallGarbageCollect DISABLED_InstallUninstallGarbageCollect |
| 28 #endif | 40 #endif |
| 29 TEST(ExtensionFileUtil, InstallUninstallGarbageCollect) { | 41 TEST_F(ExtensionFileUtilTest, InstallUninstallGarbageCollect) { |
| 30 base::ScopedTempDir temp; | 42 base::ScopedTempDir temp; |
| 31 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 43 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 32 | 44 |
| 33 // Create a source extension. | 45 // Create a source extension. |
| 34 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | 46 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
| 35 std::string version("1.0"); | 47 std::string version("1.0"); |
| 36 FilePath src = temp.path().AppendASCII(extension_id); | 48 FilePath src = temp.path().AppendASCII(extension_id); |
| 37 ASSERT_TRUE(file_util::CreateDirectory(src)); | 49 ASSERT_TRUE(file_util::CreateDirectory(src)); |
| 38 | 50 |
| 39 // Create a extensions tree. | 51 // Create a extensions tree. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 ASSERT_TRUE(file_util::DirectoryExists(version_2)); | 102 ASSERT_TRUE(file_util::DirectoryExists(version_2)); |
| 91 ASSERT_TRUE(file_util::DirectoryExists(version_3)); | 103 ASSERT_TRUE(file_util::DirectoryExists(version_3)); |
| 92 | 104 |
| 93 // Uninstall. Should remove entire extension subtree. | 105 // Uninstall. Should remove entire extension subtree. |
| 94 extension_file_util::UninstallExtension(all_extensions, extension_id); | 106 extension_file_util::UninstallExtension(all_extensions, extension_id); |
| 95 ASSERT_FALSE(file_util::DirectoryExists(version_2.DirName())); | 107 ASSERT_FALSE(file_util::DirectoryExists(version_2.DirName())); |
| 96 ASSERT_FALSE(file_util::DirectoryExists(version_3.DirName())); | 108 ASSERT_FALSE(file_util::DirectoryExists(version_3.DirName())); |
| 97 ASSERT_TRUE(file_util::DirectoryExists(all_extensions)); | 109 ASSERT_TRUE(file_util::DirectoryExists(all_extensions)); |
| 98 } | 110 } |
| 99 | 111 |
| 100 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { | 112 TEST_F(ExtensionFileUtilTest, LoadExtensionWithValidLocales) { |
| 101 FilePath install_dir; | 113 FilePath install_dir; |
| 102 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 114 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 103 install_dir = install_dir.AppendASCII("extensions") | 115 install_dir = install_dir.AppendASCII("extensions") |
| 104 .AppendASCII("good") | 116 .AppendASCII("good") |
| 105 .AppendASCII("Extensions") | 117 .AppendASCII("Extensions") |
| 106 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 118 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 107 .AppendASCII("1.0.0.0"); | 119 .AppendASCII("1.0.0.0"); |
| 108 | 120 |
| 109 std::string error; | 121 std::string error; |
| 110 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 122 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 111 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); | 123 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); |
| 112 ASSERT_TRUE(extension != NULL); | 124 ASSERT_TRUE(extension != NULL); |
| 113 EXPECT_EQ("The first extension that I made.", extension->description()); | 125 EXPECT_EQ("The first extension that I made.", extension->description()); |
| 114 } | 126 } |
| 115 | 127 |
| 116 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { | 128 TEST_F(ExtensionFileUtilTest, LoadExtensionWithoutLocalesFolder) { |
| 117 FilePath install_dir; | 129 FilePath install_dir; |
| 118 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 130 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 119 install_dir = install_dir.AppendASCII("extensions") | 131 install_dir = install_dir.AppendASCII("extensions") |
| 120 .AppendASCII("good") | 132 .AppendASCII("good") |
| 121 .AppendASCII("Extensions") | 133 .AppendASCII("Extensions") |
| 122 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") | 134 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") |
| 123 .AppendASCII("1.0"); | 135 .AppendASCII("1.0"); |
| 124 | 136 |
| 125 std::string error; | 137 std::string error; |
| 126 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 138 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 127 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); | 139 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); |
| 128 ASSERT_FALSE(extension == NULL); | 140 ASSERT_FALSE(extension == NULL); |
| 129 EXPECT_TRUE(error.empty()); | 141 EXPECT_TRUE(error.empty()); |
| 130 } | 142 } |
| 131 | 143 |
| 132 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
| 133 // http://crbug.com/106381 | 145 // http://crbug.com/106381 |
| 134 #define CheckIllegalFilenamesNoUnderscores \ | 146 #define CheckIllegalFilenamesNoUnderscores \ |
| 135 DISABLED_CheckIllegalFilenamesNoUnderscores | 147 DISABLED_CheckIllegalFilenamesNoUnderscores |
| 136 #endif | 148 #endif |
| 137 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { | 149 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesNoUnderscores) { |
| 138 base::ScopedTempDir temp; | 150 base::ScopedTempDir temp; |
| 139 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 151 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 140 | 152 |
| 141 FilePath src_path = temp.path().AppendASCII("some_dir"); | 153 FilePath src_path = temp.path().AppendASCII("some_dir"); |
| 142 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 154 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 143 | 155 |
| 144 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; | 156 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; |
| 145 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), | 157 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), |
| 146 data.c_str(), data.length())); | 158 data.c_str(), data.length())); |
| 147 std::string error; | 159 std::string error; |
| 148 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 160 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 149 &error)); | 161 &error)); |
| 150 } | 162 } |
| 151 | 163 |
| 152 #if defined(OS_WIN) | 164 #if defined(OS_WIN) |
| 153 // http://crbug.com/106381 | 165 // http://crbug.com/106381 |
| 154 #define CheckIllegalFilenamesOnlyReserved \ | 166 #define CheckIllegalFilenamesOnlyReserved \ |
| 155 DISABLED_CheckIllegalFilenamesOnlyReserved | 167 DISABLED_CheckIllegalFilenamesOnlyReserved |
| 156 #endif | 168 #endif |
| 157 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { | 169 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesOnlyReserved) { |
| 158 base::ScopedTempDir temp; | 170 base::ScopedTempDir temp; |
| 159 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 171 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 160 | 172 |
| 161 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 173 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
| 162 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 174 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 163 | 175 |
| 164 std::string error; | 176 std::string error; |
| 165 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 177 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 166 &error)); | 178 &error)); |
| 167 } | 179 } |
| 168 | 180 |
| 169 #if defined(OS_WIN) | 181 #if defined(OS_WIN) |
| 170 // http://crbug.com/106381 | 182 // http://crbug.com/106381 |
| 171 #define CheckIllegalFilenamesReservedAndIllegal \ | 183 #define CheckIllegalFilenamesReservedAndIllegal \ |
| 172 DISABLED_CheckIllegalFilenamesReservedAndIllegal | 184 DISABLED_CheckIllegalFilenamesReservedAndIllegal |
| 173 #endif | 185 #endif |
| 174 TEST(ExtensionFileUtil, CheckIllegalFilenamesReservedAndIllegal) { | 186 TEST_F(ExtensionFileUtilTest, CheckIllegalFilenamesReservedAndIllegal) { |
| 175 base::ScopedTempDir temp; | 187 base::ScopedTempDir temp; |
| 176 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 188 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 177 | 189 |
| 178 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); | 190 FilePath src_path = temp.path().Append(Extension::kLocaleFolder); |
| 179 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 191 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 180 | 192 |
| 181 src_path = temp.path().AppendASCII("_some_dir"); | 193 src_path = temp.path().AppendASCII("_some_dir"); |
| 182 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 194 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 183 | 195 |
| 184 std::string error; | 196 std::string error; |
| 185 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 197 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 186 &error)); | 198 &error)); |
| 187 } | 199 } |
| 188 | 200 |
| 189 TEST(ExtensionFileUtil, LoadExtensionGivesHelpfullErrorOnMissingManifest) { | 201 TEST_F(ExtensionFileUtilTest, |
| 202 LoadExtensionGivesHelpfullErrorOnMissingManifest) { |
| 190 FilePath install_dir; | 203 FilePath install_dir; |
| 191 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 204 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 192 install_dir = install_dir.AppendASCII("extensions") | 205 install_dir = install_dir.AppendASCII("extensions") |
| 193 .AppendASCII("bad") | 206 .AppendASCII("bad") |
| 194 .AppendASCII("Extensions") | 207 .AppendASCII("Extensions") |
| 195 .AppendASCII("dddddddddddddddddddddddddddddddd") | 208 .AppendASCII("dddddddddddddddddddddddddddddddd") |
| 196 .AppendASCII("1.0"); | 209 .AppendASCII("1.0"); |
| 197 | 210 |
| 198 std::string error; | 211 std::string error; |
| 199 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 212 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 200 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); | 213 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); |
| 201 ASSERT_TRUE(extension == NULL); | 214 ASSERT_TRUE(extension == NULL); |
| 202 ASSERT_FALSE(error.empty()); | 215 ASSERT_FALSE(error.empty()); |
| 203 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); | 216 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); |
| 204 } | 217 } |
| 205 | 218 |
| 206 TEST(ExtensionFileUtil, LoadExtensionGivesHelpfullErrorOnBadManifest) { | 219 TEST_F(ExtensionFileUtilTest, LoadExtensionGivesHelpfullErrorOnBadManifest) { |
| 207 FilePath install_dir; | 220 FilePath install_dir; |
| 208 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 221 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 209 install_dir = install_dir.AppendASCII("extensions") | 222 install_dir = install_dir.AppendASCII("extensions") |
| 210 .AppendASCII("bad") | 223 .AppendASCII("bad") |
| 211 .AppendASCII("Extensions") | 224 .AppendASCII("Extensions") |
| 212 .AppendASCII("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") | 225 .AppendASCII("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") |
| 213 .AppendASCII("1.0"); | 226 .AppendASCII("1.0"); |
| 214 | 227 |
| 215 std::string error; | 228 std::string error; |
| 216 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 229 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 217 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); | 230 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); |
| 218 ASSERT_TRUE(extension == NULL); | 231 ASSERT_TRUE(extension == NULL); |
| 219 ASSERT_FALSE(error.empty()); | 232 ASSERT_FALSE(error.empty()); |
| 220 ASSERT_STREQ("Manifest is not valid JSON. " | 233 ASSERT_STREQ("Manifest is not valid JSON. " |
| 221 "Line: 2, column: 16, Syntax error.", error.c_str()); | 234 "Line: 2, column: 16, Syntax error.", error.c_str()); |
| 222 } | 235 } |
| 223 | 236 |
| 224 TEST(ExtensionFileUtil, FailLoadingNonUTF8Scripts) { | 237 TEST_F(ExtensionFileUtilTest, FailLoadingNonUTF8Scripts) { |
| 225 FilePath install_dir; | 238 FilePath install_dir; |
| 226 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 239 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 227 install_dir = install_dir.AppendASCII("extensions") | 240 install_dir = install_dir.AppendASCII("extensions") |
| 228 .AppendASCII("bad") | 241 .AppendASCII("bad") |
| 229 .AppendASCII("bad_encoding"); | 242 .AppendASCII("bad_encoding"); |
| 230 | 243 |
| 231 std::string error; | 244 std::string error; |
| 232 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( | 245 scoped_refptr<Extension> extension(extension_file_util::LoadExtension( |
| 233 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); | 246 install_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); |
| 234 ASSERT_TRUE(extension == NULL); | 247 ASSERT_TRUE(extension == NULL); |
| 235 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " | 248 ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " |
| 236 "It isn't UTF-8 encoded.", error.c_str()); | 249 "It isn't UTF-8 encoded.", error.c_str()); |
| 237 } | 250 } |
| 238 | 251 |
| 239 TEST(ExtensionFileUtil, ExtensionURLToRelativeFilePath) { | 252 TEST_F(ExtensionFileUtilTest, ExtensionURLToRelativeFilePath) { |
| 240 #define URL_PREFIX "chrome-extension://extension-id/" | 253 #define URL_PREFIX "chrome-extension://extension-id/" |
| 241 struct TestCase { | 254 struct TestCase { |
| 242 const char* url; | 255 const char* url; |
| 243 const char* expected_relative_path; | 256 const char* expected_relative_path; |
| 244 } test_cases[] = { | 257 } test_cases[] = { |
| 245 { URL_PREFIX "simple.html", | 258 { URL_PREFIX "simple.html", |
| 246 "simple.html" }, | 259 "simple.html" }, |
| 247 { URL_PREFIX "directory/to/file.html", | 260 { URL_PREFIX "directory/to/file.html", |
| 248 "directory/to/file.html" }, | 261 "directory/to/file.html" }, |
| 249 { URL_PREFIX "escape%20spaces.html", | 262 { URL_PREFIX "escape%20spaces.html", |
| (...skipping 25 matching lines...) Expand all Loading... |
| 275 | 288 |
| 276 FilePath actual_path = | 289 FilePath actual_path = |
| 277 extension_file_util::ExtensionURLToRelativeFilePath(url); | 290 extension_file_util::ExtensionURLToRelativeFilePath(url); |
| 278 EXPECT_FALSE(actual_path.IsAbsolute()) << | 291 EXPECT_FALSE(actual_path.IsAbsolute()) << |
| 279 " For the path " << actual_path.value(); | 292 " For the path " << actual_path.value(); |
| 280 EXPECT_EQ(expected_path.value(), actual_path.value()) << | 293 EXPECT_EQ(expected_path.value(), actual_path.value()) << |
| 281 " For the path " << url; | 294 " For the path " << url; |
| 282 } | 295 } |
| 283 } | 296 } |
| 284 | 297 |
| 285 TEST(ExtensionFileUtil, ExtensionResourceURLToFilePath) { | 298 TEST_F(ExtensionFileUtilTest, ExtensionResourceURLToFilePath) { |
| 286 // Setup filesystem for testing. | 299 // Setup filesystem for testing. |
| 287 FilePath root_path; | 300 FilePath root_path; |
| 288 ASSERT_TRUE(file_util::CreateNewTempDirectory( | 301 ASSERT_TRUE(file_util::CreateNewTempDirectory( |
| 289 FILE_PATH_LITERAL(""), &root_path)); | 302 FILE_PATH_LITERAL(""), &root_path)); |
| 290 ASSERT_TRUE(file_util::AbsolutePath(&root_path)); | 303 ASSERT_TRUE(file_util::AbsolutePath(&root_path)); |
| 291 | 304 |
| 292 FilePath api_path = root_path.Append(FILE_PATH_LITERAL("apiname")); | 305 FilePath api_path = root_path.Append(FILE_PATH_LITERAL("apiname")); |
| 293 ASSERT_TRUE(file_util::CreateDirectory(api_path)); | 306 ASSERT_TRUE(file_util::CreateDirectory(api_path)); |
| 294 | 307 |
| 295 const char data[] = "Test Data"; | 308 const char data[] = "Test Data"; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 manifest_dir, | 387 manifest_dir, |
| 375 location, | 388 location, |
| 376 extra_flags, | 389 extra_flags, |
| 377 error); | 390 error); |
| 378 } | 391 } |
| 379 | 392 |
| 380 #if defined(OS_WIN) | 393 #if defined(OS_WIN) |
| 381 // http://crbug.com/108279 | 394 // http://crbug.com/108279 |
| 382 #define ValidateThemeUTF8 DISABLED_ValidateThemeUTF8 | 395 #define ValidateThemeUTF8 DISABLED_ValidateThemeUTF8 |
| 383 #endif | 396 #endif |
| 384 TEST(ExtensionFileUtil, ValidateThemeUTF8) { | 397 TEST_F(ExtensionFileUtilTest, ValidateThemeUTF8) { |
| 385 base::ScopedTempDir temp; | 398 base::ScopedTempDir temp; |
| 386 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 399 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 387 | 400 |
| 388 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. | 401 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. |
| 389 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; | 402 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; |
| 390 FilePath non_ascii_path = temp.path().Append(FilePath::FromUTF8Unsafe( | 403 FilePath non_ascii_path = temp.path().Append(FilePath::FromUTF8Unsafe( |
| 391 non_ascii_file)); | 404 non_ascii_file)); |
| 392 file_util::WriteFile(non_ascii_path, "", 0); | 405 file_util::WriteFile(non_ascii_path, "", 0); |
| 393 | 406 |
| 394 std::string kManifest = | 407 std::string kManifest = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 407 error; | 420 error; |
| 408 EXPECT_EQ(0U, warnings.size()); | 421 EXPECT_EQ(0U, warnings.size()); |
| 409 } | 422 } |
| 410 | 423 |
| 411 #if defined(OS_WIN) | 424 #if defined(OS_WIN) |
| 412 // This test hangs on Windows sometimes. http://crbug.com/110279 | 425 // This test hangs on Windows sometimes. http://crbug.com/110279 |
| 413 #define MAYBE_BackgroundScriptsMustExist DISABLED_BackgroundScriptsMustExist | 426 #define MAYBE_BackgroundScriptsMustExist DISABLED_BackgroundScriptsMustExist |
| 414 #else | 427 #else |
| 415 #define MAYBE_BackgroundScriptsMustExist BackgroundScriptsMustExist | 428 #define MAYBE_BackgroundScriptsMustExist BackgroundScriptsMustExist |
| 416 #endif | 429 #endif |
| 417 TEST(ExtensionFileUtil, MAYBE_BackgroundScriptsMustExist) { | 430 TEST_F(ExtensionFileUtilTest, MAYBE_BackgroundScriptsMustExist) { |
| 418 base::ScopedTempDir temp; | 431 base::ScopedTempDir temp; |
| 419 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 432 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 420 | 433 |
| 421 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 434 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
| 422 value->SetString("name", "test"); | 435 value->SetString("name", "test"); |
| 423 value->SetString("version", "1"); | 436 value->SetString("version", "1"); |
| 424 value->SetInteger("manifest_version", 1); | 437 value->SetInteger("manifest_version", 1); |
| 425 | 438 |
| 426 ListValue* scripts = new ListValue(); | 439 ListValue* scripts = new ListValue(); |
| 427 scripts->Append(Value::CreateStringValue("foo.js")); | 440 scripts->Append(Value::CreateStringValue("foo.js")); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 "4GAAeWonDdkQJBANNb8wrqNWFn7DqyQTfELzcRTRnqQ/r1pdeJo6obzbnwGnlqe3t\n" | 483 "4GAAeWonDdkQJBANNb8wrqNWFn7DqyQTfELzcRTRnqQ/r1pdeJo6obzbnwGnlqe3t\n" |
| 471 "KhLjtJNIGrQg5iC0OVLWFuvPJs0t3z62A1ckCQQDPq2JZuwTwu5Pl4DJ0r9O1FdqN\n" | 484 "KhLjtJNIGrQg5iC0OVLWFuvPJs0t3z62A1ckCQQDPq2JZuwTwu5Pl4DJ0r9O1FdqN\n" |
| 472 "JgqPZyMptokCDQ3khLLGakIu+TqB9YtrzI69rJMSG2Egb+6McaDX+dh3XmR/AkB9t\n" | 485 "JgqPZyMptokCDQ3khLLGakIu+TqB9YtrzI69rJMSG2Egb+6McaDX+dh3XmR/AkB9t\n" |
| 473 "xJf6qDnmA2td/tMtTc0NOk8Qdg/fD8xbZ/YfYMnVoYYs9pQoilBaWRePDRNURMLYZ\n" | 486 "xJf6qDnmA2td/tMtTc0NOk8Qdg/fD8xbZ/YfYMnVoYYs9pQoilBaWRePDRNURMLYZ\n" |
| 474 "vHAI0Llmw7tj7jv17pAkEAz44uXRpjRKtllUIvi5pUENAHwDz+HvdpGH68jpU3hmb\n" | 487 "vHAI0Llmw7tj7jv17pAkEAz44uXRpjRKtllUIvi5pUENAHwDz+HvdpGH68jpU3hmb\n" |
| 475 "uOwrmnQYxaMReFV68Z2w9DcLZn07f7/R9Wn72z89CxwJAFsDoNaDes4h48bX7plct\n" | 488 "uOwrmnQYxaMReFV68Z2w9DcLZn07f7/R9Wn72z89CxwJAFsDoNaDes4h48bX7plct\n" |
| 476 "s9ACjmTwcCigZjN2K7AGv7ntCLF3DnV5dK0dTHNaAdD3SbY3jl29Rk2CwiURSX6Ee\n" | 489 "s9ACjmTwcCigZjN2K7AGv7ntCLF3DnV5dK0dTHNaAdD3SbY3jl29Rk2CwiURSX6Ee\n" |
| 477 "g==\n" | 490 "g==\n" |
| 478 "-----END PRIVATE KEY-----\n"; | 491 "-----END PRIVATE KEY-----\n"; |
| 479 | 492 |
| 480 TEST(ExtensionFileUtil, FindPrivateKeyFiles) { | 493 TEST_F(ExtensionFileUtilTest, FindPrivateKeyFiles) { |
| 481 base::ScopedTempDir temp; | 494 base::ScopedTempDir temp; |
| 482 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 495 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 483 | 496 |
| 484 FilePath src_path = temp.path().AppendASCII("some_dir"); | 497 FilePath src_path = temp.path().AppendASCII("some_dir"); |
| 485 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 498 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 486 | 499 |
| 487 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("a_key.pem"), | 500 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("a_key.pem"), |
| 488 private_key, arraysize(private_key))); | 501 private_key, arraysize(private_key))); |
| 489 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("second_key.pem"), | 502 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("second_key.pem"), |
| 490 private_key, arraysize(private_key))); | 503 private_key, arraysize(private_key))); |
| 491 // Shouldn't find a key with a different extension. | 504 // Shouldn't find a key with a different extension. |
| 492 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("key.diff_ext"), | 505 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("key.diff_ext"), |
| 493 private_key, arraysize(private_key))); | 506 private_key, arraysize(private_key))); |
| 494 // Shouldn't find a key that isn't parsable. | 507 // Shouldn't find a key that isn't parsable. |
| 495 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("unparsable_key.pem"), | 508 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("unparsable_key.pem"), |
| 496 private_key, arraysize(private_key) - 30)); | 509 private_key, arraysize(private_key) - 30)); |
| 497 std::vector<FilePath> private_keys = | 510 std::vector<FilePath> private_keys = |
| 498 extension_file_util::FindPrivateKeyFiles(temp.path()); | 511 extension_file_util::FindPrivateKeyFiles(temp.path()); |
| 499 EXPECT_EQ(2U, private_keys.size()); | 512 EXPECT_EQ(2U, private_keys.size()); |
| 500 EXPECT_THAT(private_keys, | 513 EXPECT_THAT(private_keys, |
| 501 testing::Contains(src_path.AppendASCII("a_key.pem"))); | 514 testing::Contains(src_path.AppendASCII("a_key.pem"))); |
| 502 EXPECT_THAT(private_keys, | 515 EXPECT_THAT(private_keys, |
| 503 testing::Contains(src_path.AppendASCII("second_key.pem"))); | 516 testing::Contains(src_path.AppendASCII("second_key.pem"))); |
| 504 } | 517 } |
| 505 | 518 |
| 506 TEST(ExtensionFileUtil, WarnOnPrivateKey) { | 519 TEST_F(ExtensionFileUtilTest, WarnOnPrivateKey) { |
| 507 base::ScopedTempDir temp; | 520 base::ScopedTempDir temp; |
| 508 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 521 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 509 | 522 |
| 510 FilePath ext_path = temp.path().AppendASCII("ext_root"); | 523 FilePath ext_path = temp.path().AppendASCII("ext_root"); |
| 511 ASSERT_TRUE(file_util::CreateDirectory(ext_path)); | 524 ASSERT_TRUE(file_util::CreateDirectory(ext_path)); |
| 512 | 525 |
| 513 const char manifest[] = | 526 const char manifest[] = |
| 514 "{\n" | 527 "{\n" |
| 515 " \"name\": \"Test Extension\",\n" | 528 " \"name\": \"Test Extension\",\n" |
| 516 " \"version\": \"1.0\",\n" | 529 " \"version\": \"1.0\",\n" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 539 // Turn the warning into an error with ERROR_ON_PRIVATE_KEY. | 552 // Turn the warning into an error with ERROR_ON_PRIVATE_KEY. |
| 540 extension = extension_file_util::LoadExtension( | 553 extension = extension_file_util::LoadExtension( |
| 541 ext_path, "the_id", Extension::EXTERNAL_PREF, | 554 ext_path, "the_id", Extension::EXTERNAL_PREF, |
| 542 Extension::ERROR_ON_PRIVATE_KEY, &error); | 555 Extension::ERROR_ON_PRIVATE_KEY, &error); |
| 543 EXPECT_FALSE(extension.get()); | 556 EXPECT_FALSE(extension.get()); |
| 544 EXPECT_THAT(error, | 557 EXPECT_THAT(error, |
| 545 testing::ContainsRegex( | 558 testing::ContainsRegex( |
| 546 "extension includes the key file.*ext_root.a_key.pem")); | 559 "extension includes the key file.*ext_root.a_key.pem")); |
| 547 } | 560 } |
| 548 | 561 |
| 549 TEST(ExtensionFileUtil, CheckZeroLengthImageFile) { | 562 TEST_F(ExtensionFileUtilTest, CheckZeroLengthImageFile) { |
| 550 FilePath install_dir; | 563 FilePath install_dir; |
| 551 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 564 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 552 | 565 |
| 553 // Try to install an extension with a zero-length icon file. | 566 // Try to install an extension with a zero-length icon file. |
| 554 FilePath ext_dir = install_dir.AppendASCII("extensions") | 567 FilePath ext_dir = install_dir.AppendASCII("extensions") |
| 555 .AppendASCII("bad") | 568 .AppendASCII("bad") |
| 556 .AppendASCII("Extensions") | 569 .AppendASCII("Extensions") |
| 557 .AppendASCII("ffffffffffffffffffffffffffffffff"); | 570 .AppendASCII("ffffffffffffffffffffffffffffffff"); |
| 558 | 571 |
| 559 std::string error; | 572 std::string error; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 584 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( | 597 scoped_refptr<Extension> extension3(extension_file_util::LoadExtension( |
| 585 ext_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); | 598 ext_dir, Extension::LOAD, Extension::NO_FLAGS, &error)); |
| 586 ASSERT_TRUE(extension3 == NULL); | 599 ASSERT_TRUE(extension3 == NULL); |
| 587 ASSERT_STREQ("Could not load icon 'icon.png' for page action.", | 600 ASSERT_STREQ("Could not load icon 'icon.png' for page action.", |
| 588 error.c_str()); | 601 error.c_str()); |
| 589 } | 602 } |
| 590 | 603 |
| 591 // TODO(aa): More tests as motivation allows. Maybe steal some from | 604 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 592 // ExtensionService? Many of them could probably be tested here without the | 605 // ExtensionService? Many of them could probably be tested here without the |
| 593 // MessageLoop shenanigans. | 606 // MessageLoop shenanigans. |
| OLD | NEW |