| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/browser/extensions/extension_file_util.h" | 5 #include "chrome/browser/extensions/extension_file_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
| 11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_constants.h" | 12 #include "chrome/common/extensions/extension_constants.h" |
| 13 #include "chrome/common/json_value_serializer.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 namespace keys = extension_manifest_keys; | 15 namespace keys = extension_manifest_keys; |
| 17 | 16 |
| 18 TEST(ExtensionFileUtil, MoveDirSafely) { | 17 TEST(ExtensionFileUtil, MoveDirSafely) { |
| 19 // Create a test directory structure with some data in it. | 18 // Create a test directory structure with some data in it. |
| 20 ScopedTempDir temp; | 19 ScopedTempDir temp; |
| 21 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 20 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 22 | 21 |
| 23 FilePath src_path = temp.path().AppendASCII("src"); | 22 FilePath src_path = temp.path().AppendASCII("src"); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 temp.path(), kId, kCurrentVersion, "1.0.0", &version_dir)); | 103 temp.path(), kId, kCurrentVersion, "1.0.0", &version_dir)); |
| 105 | 104 |
| 106 // Compare to a non-existent extension. | 105 // Compare to a non-existent extension. |
| 107 const std::string kMissingVersion = ""; | 106 const std::string kMissingVersion = ""; |
| 108 const std::string kBadId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | 107 const std::string kBadId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 109 ASSERT_EQ(Extension::NEW_INSTALL, | 108 ASSERT_EQ(Extension::NEW_INSTALL, |
| 110 extension_file_util::CompareToInstalledVersion( | 109 extension_file_util::CompareToInstalledVersion( |
| 111 temp.path(), kBadId, kMissingVersion, "1.0.0", &version_dir)); | 110 temp.path(), kBadId, kMissingVersion, "1.0.0", &version_dir)); |
| 112 } | 111 } |
| 113 | 112 |
| 114 // Creates minimal manifest, with or without default_locale section. | |
| 115 bool CreateMinimalManifest(const std::string& locale, | |
| 116 const FilePath& manifest_path) { | |
| 117 DictionaryValue manifest; | |
| 118 | |
| 119 manifest.SetString(keys::kVersion, "1.0.0.0"); | |
| 120 manifest.SetString(keys::kName, "my extension"); | |
| 121 if (!locale.empty()) { | |
| 122 manifest.SetString(keys::kDefaultLocale, locale); | |
| 123 } | |
| 124 | |
| 125 JSONFileValueSerializer serializer(manifest_path); | |
| 126 return serializer.Serialize(manifest); | |
| 127 } | |
| 128 | |
| 129 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { | 113 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { |
| 130 ScopedTempDir temp; | 114 FilePath install_dir; |
| 131 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 115 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 132 ASSERT_TRUE(CreateMinimalManifest( | 116 install_dir = install_dir.AppendASCII("extensions") |
| 133 "en_US", temp.path().AppendASCII(Extension::kManifestFilename))); | 117 .AppendASCII("good") |
| 134 | 118 .AppendASCII("Extensions") |
| 135 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); | 119 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 136 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 120 .AppendASCII("1.0.0.0"); |
| 137 | |
| 138 FilePath locale_1 = src_path.AppendASCII("sr"); | |
| 139 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); | |
| 140 | |
| 141 std::string data = "foobar"; | |
| 142 ASSERT_TRUE( | |
| 143 file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), | |
| 144 data.c_str(), data.length())); | |
| 145 | |
| 146 FilePath locale_2 = src_path.AppendASCII("en_US"); | |
| 147 ASSERT_TRUE(file_util::CreateDirectory(locale_2)); | |
| 148 | |
| 149 ASSERT_TRUE( | |
| 150 file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), | |
| 151 data.c_str(), data.length())); | |
| 152 | 121 |
| 153 std::string error; | 122 std::string error; |
| 154 scoped_ptr<Extension> extension( | 123 scoped_ptr<Extension> extension( |
| 155 extension_file_util::LoadExtension(temp.path(), false, &error)); | 124 extension_file_util::LoadExtension(install_dir, false, &error)); |
| 156 ASSERT_FALSE(extension == NULL); | 125 ASSERT_TRUE(extension != NULL); |
| 157 EXPECT_EQ(static_cast<unsigned int>(2), | 126 EXPECT_EQ("The first extension that I made.", extension->description()); |
| 158 extension->supported_locales().size()); | |
| 159 EXPECT_EQ("en-US", extension->default_locale()); | |
| 160 } | 127 } |
| 161 | 128 |
| 162 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { | 129 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { |
| 163 ScopedTempDir temp; | 130 FilePath install_dir; |
| 164 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 131 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 165 ASSERT_TRUE(CreateMinimalManifest( | 132 install_dir = install_dir.AppendASCII("extensions") |
| 166 "", temp.path().AppendASCII(Extension::kManifestFilename))); | 133 .AppendASCII("good") |
| 134 .AppendASCII("Extensions") |
| 135 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") |
| 136 .AppendASCII("1.0"); |
| 167 | 137 |
| 168 std::string error; | 138 std::string error; |
| 169 scoped_ptr<Extension> extension( | 139 scoped_ptr<Extension> extension( |
| 170 extension_file_util::LoadExtension(temp.path(), false, &error)); | 140 extension_file_util::LoadExtension(install_dir, false, &error)); |
| 171 ASSERT_FALSE(extension == NULL); | 141 ASSERT_FALSE(extension == NULL); |
| 172 EXPECT_TRUE(extension->supported_locales().empty()); | 142 EXPECT_TRUE(NULL == extension->message_bundle()); |
| 173 EXPECT_TRUE(extension->default_locale().empty()); | 143 EXPECT_TRUE(error.empty()); |
| 174 } | 144 } |
| 175 | 145 |
| 176 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { | 146 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { |
| 177 ScopedTempDir temp; | 147 ScopedTempDir temp; |
| 178 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 148 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 179 | 149 |
| 180 FilePath src_path = temp.path().AppendASCII("some_dir"); | 150 FilePath src_path = temp.path().AppendASCII("some_dir"); |
| 181 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 151 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 182 | 152 |
| 183 std::string data = "foobar"; | 153 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; |
| 184 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), | 154 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), |
| 185 data.c_str(), data.length())); | 155 data.c_str(), data.length())); |
| 186 std::string error; | 156 std::string error; |
| 187 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 157 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 188 &error)); | 158 &error)); |
| 189 } | 159 } |
| 190 | 160 |
| 191 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { | 161 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { |
| 192 ScopedTempDir temp; | 162 ScopedTempDir temp; |
| 193 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 163 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 211 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 181 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 212 | 182 |
| 213 std::string error; | 183 std::string error; |
| 214 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 184 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 215 &error)); | 185 &error)); |
| 216 } | 186 } |
| 217 | 187 |
| 218 // TODO(aa): More tests as motivation allows. Maybe steal some from | 188 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 219 // ExtensionsService? Many of them could probably be tested here without the | 189 // ExtensionsService? Many of them could probably be tested here without the |
| 220 // MessageLoop shenanigans. | 190 // MessageLoop shenanigans. |
| OLD | NEW |