| 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" |
| 12 #include "chrome/common/extensions/extension_constants.h" |
| 13 #include "chrome/common/json_value_serializer.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 15 |
| 16 namespace keys = extension_manifest_keys; |
| 17 |
| 13 TEST(ExtensionFileUtil, MoveDirSafely) { | 18 TEST(ExtensionFileUtil, MoveDirSafely) { |
| 14 // Create a test directory structure with some data in it. | 19 // Create a test directory structure with some data in it. |
| 15 ScopedTempDir temp; | 20 ScopedTempDir temp; |
| 16 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 21 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 17 | 22 |
| 18 FilePath src_path = temp.path().AppendASCII("src"); | 23 FilePath src_path = temp.path().AppendASCII("src"); |
| 19 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 24 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 20 | 25 |
| 21 std::string data = "foobar"; | 26 std::string data = "foobar"; |
| 22 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("data"), | 27 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("data"), |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 160 |
| 156 // Compare to a completely non-existent extension. | 161 // Compare to a completely non-existent extension. |
| 157 version_out.clear(); | 162 version_out.clear(); |
| 158 ASSERT_EQ(Extension::NEW_INSTALL, | 163 ASSERT_EQ(Extension::NEW_INSTALL, |
| 159 extension_file_util::CompareToInstalledVersion( | 164 extension_file_util::CompareToInstalledVersion( |
| 160 temp.path(), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "0.0.1.0", | 165 temp.path(), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "0.0.1.0", |
| 161 &version_out)); | 166 &version_out)); |
| 162 ASSERT_EQ("", version_out); | 167 ASSERT_EQ("", version_out); |
| 163 } | 168 } |
| 164 | 169 |
| 170 // Creates minimal manifest, with or without default_locale section. |
| 171 bool CreateMinimalManifest(const std::string& locale, |
| 172 const FilePath& manifest_path) { |
| 173 DictionaryValue manifest; |
| 174 |
| 175 manifest.SetString(keys::kVersion, "1.0.0.0"); |
| 176 manifest.SetString(keys::kName, "my extension"); |
| 177 if (!locale.empty()) { |
| 178 manifest.SetString(keys::kDefaultLocale, locale); |
| 179 } |
| 180 |
| 181 JSONFileValueSerializer serializer(manifest_path); |
| 182 return serializer.Serialize(manifest); |
| 183 } |
| 184 |
| 185 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { |
| 186 ScopedTempDir temp; |
| 187 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 188 ASSERT_TRUE(CreateMinimalManifest( |
| 189 "en_US", temp.path().AppendASCII(Extension::kManifestFilename))); |
| 190 |
| 191 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); |
| 192 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 193 |
| 194 FilePath locale_1 = src_path.AppendASCII("sr"); |
| 195 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); |
| 196 |
| 197 std::string data = "foobar"; |
| 198 ASSERT_TRUE( |
| 199 file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), |
| 200 data.c_str(), data.length())); |
| 201 |
| 202 FilePath locale_2 = src_path.AppendASCII("en_US"); |
| 203 ASSERT_TRUE(file_util::CreateDirectory(locale_2)); |
| 204 |
| 205 ASSERT_TRUE( |
| 206 file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), |
| 207 data.c_str(), data.length())); |
| 208 |
| 209 std::string error; |
| 210 scoped_ptr<Extension> extension( |
| 211 extension_file_util::LoadExtension(temp.path(), false, &error)); |
| 212 ASSERT_FALSE(extension == NULL); |
| 213 EXPECT_EQ(2, extension->supported_locales().size()); |
| 214 EXPECT_EQ("en-US", extension->default_locale()); |
| 215 } |
| 216 |
| 217 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { |
| 218 FilePath extension_path; |
| 219 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path)); |
| 220 extension_path = extension_path.AppendASCII("extensions") |
| 221 .AppendASCII("good") |
| 222 .AppendASCII("Extensions") |
| 223 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") |
| 224 .AppendASCII("1.0"); |
| 225 |
| 226 std::string error; |
| 227 scoped_ptr<Extension> extension( |
| 228 extension_file_util::LoadExtension(extension_path, false, &error)); |
| 229 ASSERT_FALSE(extension == NULL); |
| 230 EXPECT_TRUE(extension->supported_locales().empty()); |
| 231 EXPECT_TRUE(extension->default_locale().empty()); |
| 232 } |
| 233 |
| 234 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { |
| 235 ScopedTempDir temp; |
| 236 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 237 |
| 238 FilePath src_path = temp.path().AppendASCII("some_dir"); |
| 239 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 240 |
| 241 std::string data = "foobar"; |
| 242 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), |
| 243 data.c_str(), data.length())); |
| 244 std::string error; |
| 245 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 246 &error)); |
| 247 } |
| 248 |
| 249 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { |
| 250 ScopedTempDir temp; |
| 251 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 252 |
| 253 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); |
| 254 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 255 |
| 256 std::string error; |
| 257 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 258 &error)); |
| 259 } |
| 260 |
| 261 TEST(ExtensionFileUtil, CheckIllegalFilenamesReservedAndIllegal) { |
| 262 ScopedTempDir temp; |
| 263 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 264 |
| 265 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); |
| 266 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 267 |
| 268 src_path = temp.path().AppendASCII("_some_dir"); |
| 269 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 270 |
| 271 std::string error; |
| 272 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 273 &error)); |
| 274 } |
| 275 |
| 165 // TODO(aa): More tests as motivation allows. Maybe steal some from | 276 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 166 // ExtensionsService? Many of them could probably be tested here without the | 277 // ExtensionsService? Many of them could probably be tested here without the |
| 167 // MessageLoop shenanigans. | 278 // MessageLoop shenanigans. |
| OLD | NEW |