| 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/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/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/common/chrome_paths.h" | 11 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_constants.h" | 13 #include "chrome/common/extensions/extension_constants.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace keys = extension_manifest_keys; | 16 namespace keys = extension_manifest_keys; |
| 17 | 17 |
| 18 TEST(ExtensionFileUtil, MoveDirSafely) { | 18 TEST(ExtensionFileUtil, InstallUninstallGarbageCollect) { |
| 19 // 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 // Create a source extension. |
| 24 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 23 std::string extension_id("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
| 24 std::string version("1.0"); |
| 25 FilePath src = temp.path().AppendASCII(extension_id); |
| 26 ASSERT_TRUE(file_util::CreateDirectory(src)); |
| 25 | 27 |
| 26 std::string data = "foobar"; | 28 // Create a extensions tree. |
| 27 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("data"), | 29 FilePath all_extensions = temp.path().AppendASCII("extensions"); |
| 28 data.c_str(), data.length())); | 30 ASSERT_TRUE(file_util::CreateDirectory(all_extensions)); |
| 29 | 31 |
| 30 // Move it to a path that doesn't exist yet. | 32 // Install in empty directory. Should create parent directories as needed. |
| 31 FilePath dest_path = temp.path().AppendASCII("dest").AppendASCII("dest"); | 33 FilePath version_1 = extension_file_util::InstallExtension(src, |
| 32 ASSERT_TRUE(extension_file_util::MoveDirSafely(src_path, dest_path)); | 34 extension_id, |
| 35 version, |
| 36 all_extensions); |
| 37 ASSERT_EQ(version_1.value(), |
| 38 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_0") |
| 39 .value()); |
| 40 ASSERT_TRUE(file_util::DirectoryExists(version_1)); |
| 33 | 41 |
| 34 // The path should get created. | 42 // Should have moved the source. |
| 35 ASSERT_TRUE(file_util::DirectoryExists(dest_path)); | 43 ASSERT_FALSE(file_util::DirectoryExists(src)); |
| 36 | 44 |
| 37 // The data should match. | 45 // Install again. Should create a new one with different name. |
| 38 std::string data_out; | 46 ASSERT_TRUE(file_util::CreateDirectory(src)); |
| 39 ASSERT_TRUE(file_util::ReadFileToString(dest_path.AppendASCII("data"), | 47 FilePath version_2 = extension_file_util::InstallExtension(src, |
| 40 &data_out)); | 48 extension_id, |
| 41 ASSERT_EQ(data, data_out); | 49 version, |
| 50 all_extensions); |
| 51 ASSERT_EQ(version_2.value(), |
| 52 all_extensions.AppendASCII(extension_id).AppendASCII("1.0_1") |
| 53 .value()); |
| 54 ASSERT_TRUE(file_util::DirectoryExists(version_2)); |
| 42 | 55 |
| 43 // The src path should be gone. | 56 // Collect garbage. Should remove first one. |
| 44 ASSERT_FALSE(file_util::PathExists(src_path)); | 57 std::map<std::string, FilePath> extension_paths; |
| 58 extension_paths[extension_id] = |
| 59 FilePath().AppendASCII(extension_id).Append(version_2.BaseName()); |
| 60 extension_file_util::GarbageCollectExtensions(all_extensions, |
| 61 extension_paths); |
| 62 ASSERT_FALSE(file_util::DirectoryExists(version_1)); |
| 63 ASSERT_TRUE(file_util::DirectoryExists(version_2)); |
| 45 | 64 |
| 46 // Create some new test data. | 65 // Uninstall. Should remove entire extension subtree. |
| 47 ASSERT_TRUE(file_util::CopyDirectory(dest_path, src_path, | 66 extension_file_util::UninstallExtension(all_extensions, extension_id); |
| 48 true)); // recursive | 67 ASSERT_FALSE(file_util::DirectoryExists(version_2.DirName())); |
| 49 data = "hotdog"; | 68 ASSERT_TRUE(file_util::DirectoryExists(all_extensions)); |
| 50 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("data"), | |
| 51 data.c_str(), data.length())); | |
| 52 | |
| 53 // Test again, overwriting the old path. | |
| 54 ASSERT_TRUE(extension_file_util::MoveDirSafely(src_path, dest_path)); | |
| 55 ASSERT_TRUE(file_util::DirectoryExists(dest_path)); | |
| 56 | |
| 57 data_out.clear(); | |
| 58 ASSERT_TRUE(file_util::ReadFileToString(dest_path.AppendASCII("data"), | |
| 59 &data_out)); | |
| 60 ASSERT_EQ(data, data_out); | |
| 61 ASSERT_FALSE(file_util::PathExists(src_path)); | |
| 62 } | |
| 63 | |
| 64 TEST(ExtensionFileUtil, CompareToInstalledVersion) { | |
| 65 // Compare to an existing extension. | |
| 66 FilePath install_dir; | |
| 67 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | |
| 68 install_dir = install_dir.AppendASCII("extensions") | |
| 69 .AppendASCII("good") | |
| 70 .AppendASCII("Extensions"); | |
| 71 | |
| 72 const std::string kId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; | |
| 73 const std::string kCurrentVersion = "1.0.0.0"; | |
| 74 | |
| 75 FilePath version_dir; | |
| 76 | |
| 77 ASSERT_EQ(Extension::UPGRADE, | |
| 78 extension_file_util::CompareToInstalledVersion( | |
| 79 install_dir, kId, kCurrentVersion, "1.0.0.1", &version_dir)); | |
| 80 | |
| 81 ASSERT_EQ(Extension::REINSTALL, | |
| 82 extension_file_util::CompareToInstalledVersion( | |
| 83 install_dir, kId, kCurrentVersion, "1.0.0.0", &version_dir)); | |
| 84 | |
| 85 ASSERT_EQ(Extension::REINSTALL, | |
| 86 extension_file_util::CompareToInstalledVersion( | |
| 87 install_dir, kId, kCurrentVersion, "1.0.0", &version_dir)); | |
| 88 | |
| 89 ASSERT_EQ(Extension::DOWNGRADE, | |
| 90 extension_file_util::CompareToInstalledVersion( | |
| 91 install_dir, kId, kCurrentVersion, "0.0.1.0", &version_dir)); | |
| 92 | |
| 93 // Compare to an extension that is missing its manifest file. | |
| 94 ScopedTempDir temp; | |
| 95 ASSERT_TRUE(temp.CreateUniqueTempDir()); | |
| 96 FilePath src = install_dir.AppendASCII(kId).AppendASCII(kCurrentVersion); | |
| 97 FilePath dest = temp.path().AppendASCII(kId).AppendASCII(kCurrentVersion); | |
| 98 ASSERT_TRUE(file_util::CreateDirectory(dest.DirName())); | |
| 99 ASSERT_TRUE(file_util::CopyDirectory(src, dest, true)); | |
| 100 ASSERT_TRUE(file_util::Delete(dest.AppendASCII("manifest.json"), false)); | |
| 101 | |
| 102 ASSERT_EQ(Extension::NEW_INSTALL, | |
| 103 extension_file_util::CompareToInstalledVersion( | |
| 104 temp.path(), kId, kCurrentVersion, "1.0.0", &version_dir)); | |
| 105 | |
| 106 // Compare to a non-existent extension. | |
| 107 const std::string kMissingVersion = ""; | |
| 108 const std::string kBadId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
| 109 ASSERT_EQ(Extension::NEW_INSTALL, | |
| 110 extension_file_util::CompareToInstalledVersion( | |
| 111 temp.path(), kBadId, kMissingVersion, "1.0.0", &version_dir)); | |
| 112 } | 69 } |
| 113 | 70 |
| 114 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { | 71 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { |
| 115 FilePath install_dir; | 72 FilePath install_dir; |
| 116 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 73 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 117 install_dir = install_dir.AppendASCII("extensions") | 74 install_dir = install_dir.AppendASCII("extensions") |
| 118 .AppendASCII("good") | 75 .AppendASCII("good") |
| 119 .AppendASCII("Extensions") | 76 .AppendASCII("Extensions") |
| 120 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | 77 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") |
| 121 .AppendASCII("1.0.0.0"); | 78 .AppendASCII("1.0.0.0"); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 extension_file_util::ExtensionURLToRelativeFilePath(url); | 206 extension_file_util::ExtensionURLToRelativeFilePath(url); |
| 250 EXPECT_FALSE(actual_path.IsAbsolute()) << | 207 EXPECT_FALSE(actual_path.IsAbsolute()) << |
| 251 " For the path " << actual_path.value(); | 208 " For the path " << actual_path.value(); |
| 252 EXPECT_EQ(expected_path.value(), actual_path.value()); | 209 EXPECT_EQ(expected_path.value(), actual_path.value()); |
| 253 } | 210 } |
| 254 } | 211 } |
| 255 | 212 |
| 256 // TODO(aa): More tests as motivation allows. Maybe steal some from | 213 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 257 // ExtensionsService? Many of them could probably be tested here without the | 214 // ExtensionsService? Many of them could probably be tested here without the |
| 258 // MessageLoop shenanigans. | 215 // MessageLoop shenanigans. |
| OLD | NEW |