| 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" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 ASSERT_TRUE(extension_file_util::MoveDirSafely(src_path, dest_path)); | 54 ASSERT_TRUE(extension_file_util::MoveDirSafely(src_path, dest_path)); |
| 55 ASSERT_TRUE(file_util::DirectoryExists(dest_path)); | 55 ASSERT_TRUE(file_util::DirectoryExists(dest_path)); |
| 56 | 56 |
| 57 data_out.clear(); | 57 data_out.clear(); |
| 58 ASSERT_TRUE(file_util::ReadFileToString(dest_path.AppendASCII("data"), | 58 ASSERT_TRUE(file_util::ReadFileToString(dest_path.AppendASCII("data"), |
| 59 &data_out)); | 59 &data_out)); |
| 60 ASSERT_EQ(data, data_out); | 60 ASSERT_EQ(data, data_out); |
| 61 ASSERT_FALSE(file_util::PathExists(src_path)); | 61 ASSERT_FALSE(file_util::PathExists(src_path)); |
| 62 } | 62 } |
| 63 | 63 |
| 64 TEST(ExtensionFileUtil, SetCurrentVersion) { | |
| 65 // Create an empty test directory. | |
| 66 ScopedTempDir temp; | |
| 67 ASSERT_TRUE(temp.CreateUniqueTempDir()); | |
| 68 | |
| 69 // Set its version. | |
| 70 std::string error; | |
| 71 std::string version = "1.0"; | |
| 72 ASSERT_TRUE(extension_file_util::SetCurrentVersion(temp.path(), version, | |
| 73 &error)); | |
| 74 | |
| 75 // Test the result. | |
| 76 std::string version_out; | |
| 77 ASSERT_TRUE(file_util::ReadFileToString( | |
| 78 temp.path().AppendASCII(extension_file_util::kCurrentVersionFileName), | |
| 79 &version_out)); | |
| 80 ASSERT_EQ(version, version_out); | |
| 81 | |
| 82 // Try again, overwriting the old value. | |
| 83 version = "2.0"; | |
| 84 version_out.clear(); | |
| 85 ASSERT_TRUE(extension_file_util::SetCurrentVersion(temp.path(), version, | |
| 86 &error)); | |
| 87 ASSERT_TRUE(file_util::ReadFileToString( | |
| 88 temp.path().AppendASCII(extension_file_util::kCurrentVersionFileName), | |
| 89 &version_out)); | |
| 90 ASSERT_EQ(version, version_out); | |
| 91 } | |
| 92 | |
| 93 TEST(ExtensionFileUtil, ReadCurrentVersion) { | |
| 94 // Read the version from a valid extension. | |
| 95 FilePath extension_path; | |
| 96 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extension_path)); | |
| 97 extension_path = extension_path.AppendASCII("extensions") | |
| 98 .AppendASCII("good") | |
| 99 .AppendASCII("Extensions") | |
| 100 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj"); | |
| 101 | |
| 102 std::string version; | |
| 103 ASSERT_TRUE(extension_file_util::ReadCurrentVersion(extension_path, | |
| 104 &version)); | |
| 105 ASSERT_EQ("1.0.0.0", version); | |
| 106 | |
| 107 // Create an invalid extension and read its current version. | |
| 108 ScopedTempDir temp; | |
| 109 ASSERT_TRUE(temp.CreateUniqueTempDir()); | |
| 110 ASSERT_FALSE(extension_file_util::ReadCurrentVersion(temp.path(), &version)); | |
| 111 } | |
| 112 | |
| 113 TEST(ExtensionFileUtil, CompareToInstalledVersion) { | 64 TEST(ExtensionFileUtil, CompareToInstalledVersion) { |
| 114 // Compare to an existing extension. | 65 // Compare to an existing extension. |
| 115 FilePath install_directory; | 66 FilePath install_dir; |
| 116 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_directory)); | 67 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); |
| 117 install_directory = install_directory.AppendASCII("extensions") | 68 install_dir = install_dir.AppendASCII("extensions") |
| 118 .AppendASCII("good") | 69 .AppendASCII("good") |
| 119 .AppendASCII("Extensions"); | 70 .AppendASCII("Extensions"); |
| 120 | 71 |
| 121 std::string id = "behllobkkfkfnphdnhnkndlbkcpglgmj"; | 72 const std::string kId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; |
| 122 std::string version = "1.0.0.0"; | 73 const std::string kCurrentVersion = "1.0.0.0"; |
| 123 std::string version_out; | |
| 124 | 74 |
| 125 ASSERT_EQ(Extension::UPGRADE, extension_file_util::CompareToInstalledVersion( | 75 FilePath version_dir; |
| 126 install_directory, id, "1.0.0.1", &version_out)); | |
| 127 ASSERT_EQ(version, version_out); | |
| 128 | 76 |
| 129 version_out.clear(); | 77 ASSERT_EQ(Extension::UPGRADE, |
| 78 extension_file_util::CompareToInstalledVersion( |
| 79 install_dir, kId, kCurrentVersion, "1.0.0.1", &version_dir)); |
| 80 |
| 130 ASSERT_EQ(Extension::REINSTALL, | 81 ASSERT_EQ(Extension::REINSTALL, |
| 131 extension_file_util::CompareToInstalledVersion( | 82 extension_file_util::CompareToInstalledVersion( |
| 132 install_directory, id, "1.0.0.0", &version_out)); | 83 install_dir, kId, kCurrentVersion, "1.0.0.0", &version_dir)); |
| 133 ASSERT_EQ(version, version_out); | |
| 134 | 84 |
| 135 version_out.clear(); | |
| 136 ASSERT_EQ(Extension::REINSTALL, | 85 ASSERT_EQ(Extension::REINSTALL, |
| 137 extension_file_util::CompareToInstalledVersion( | 86 extension_file_util::CompareToInstalledVersion( |
| 138 install_directory, id, "1.0.0", &version_out)); | 87 install_dir, kId, kCurrentVersion, "1.0.0", &version_dir)); |
| 139 ASSERT_EQ(version, version_out); | |
| 140 | 88 |
| 141 version_out.clear(); | |
| 142 ASSERT_EQ(Extension::DOWNGRADE, | 89 ASSERT_EQ(Extension::DOWNGRADE, |
| 143 extension_file_util::CompareToInstalledVersion( | 90 extension_file_util::CompareToInstalledVersion( |
| 144 install_directory, id, "0.0.1.0", &version_out)); | 91 install_dir, kId, kCurrentVersion, "0.0.1.0", &version_dir)); |
| 145 ASSERT_EQ(version, version_out); | |
| 146 | 92 |
| 147 // Compare to an extension that is missing its Current Version file. | 93 // Compare to an extension that is missing its manifest file. |
| 148 ScopedTempDir temp; | 94 ScopedTempDir temp; |
| 149 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 95 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 150 FilePath src = install_directory.AppendASCII(id).AppendASCII(version); | 96 FilePath src = install_dir.AppendASCII(kId).AppendASCII(kCurrentVersion); |
| 151 FilePath dest = temp.path().AppendASCII(id).AppendASCII(version); | 97 FilePath dest = temp.path().AppendASCII(kId).AppendASCII(kCurrentVersion); |
| 152 ASSERT_TRUE(file_util::CreateDirectory(dest.DirName())); | 98 ASSERT_TRUE(file_util::CreateDirectory(dest.DirName())); |
| 153 ASSERT_TRUE(file_util::CopyDirectory(src, dest, true)); | 99 ASSERT_TRUE(file_util::CopyDirectory(src, dest, true)); |
| 100 ASSERT_TRUE(file_util::Delete(dest.AppendASCII("manifest.json"), false)); |
| 154 | 101 |
| 155 version_out.clear(); | |
| 156 ASSERT_EQ(Extension::NEW_INSTALL, | 102 ASSERT_EQ(Extension::NEW_INSTALL, |
| 157 extension_file_util::CompareToInstalledVersion( | 103 extension_file_util::CompareToInstalledVersion( |
| 158 temp.path(), id, "0.0.1.0", &version_out)); | 104 temp.path(), kId, kCurrentVersion, "1.0.0", &version_dir)); |
| 159 ASSERT_EQ("", version_out); | |
| 160 | 105 |
| 161 // Compare to a completely non-existent extension. | 106 // Compare to a non-existent extension. |
| 162 version_out.clear(); | 107 const std::string kMissingVersion = ""; |
| 108 const std::string kBadId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 163 ASSERT_EQ(Extension::NEW_INSTALL, | 109 ASSERT_EQ(Extension::NEW_INSTALL, |
| 164 extension_file_util::CompareToInstalledVersion( | 110 extension_file_util::CompareToInstalledVersion( |
| 165 temp.path(), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "0.0.1.0", | 111 temp.path(), kBadId, kMissingVersion, "1.0.0", &version_dir)); |
| 166 &version_out)); | |
| 167 ASSERT_EQ("", version_out); | |
| 168 } | 112 } |
| 169 | 113 |
| 170 // Creates minimal manifest, with or without default_locale section. | 114 // Creates minimal manifest, with or without default_locale section. |
| 171 bool CreateMinimalManifest(const std::string& locale, | 115 bool CreateMinimalManifest(const std::string& locale, |
| 172 const FilePath& manifest_path) { | 116 const FilePath& manifest_path) { |
| 173 DictionaryValue manifest; | 117 DictionaryValue manifest; |
| 174 | 118 |
| 175 manifest.SetString(keys::kVersion, "1.0.0.0"); | 119 manifest.SetString(keys::kVersion, "1.0.0.0"); |
| 176 manifest.SetString(keys::kName, "my extension"); | 120 manifest.SetString(keys::kName, "my extension"); |
| 177 if (!locale.empty()) { | 121 if (!locale.empty()) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 211 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 268 | 212 |
| 269 std::string error; | 213 std::string error; |
| 270 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 214 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
| 271 &error)); | 215 &error)); |
| 272 } | 216 } |
| 273 | 217 |
| 274 // TODO(aa): More tests as motivation allows. Maybe steal some from | 218 // TODO(aa): More tests as motivation allows. Maybe steal some from |
| 275 // ExtensionsService? Many of them could probably be tested here without the | 219 // ExtensionsService? Many of them could probably be tested here without the |
| 276 // MessageLoop shenanigans. | 220 // MessageLoop shenanigans. |
| OLD | NEW |