| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/memory/scoped_temp_dir.h" | 7 #include "base/memory/scoped_temp_dir.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/memory/scoped_temp_dir.h" |
| 10 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" | 11 #include "chrome/browser/extensions/sandboxed_extension_unpacker.h" |
| 11 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_constants.h" | 14 #include "chrome/common/extensions/extension_constants.h" |
| 14 #include "chrome/common/extensions/extension_unpacker.h" | 15 #include "chrome/common/extensions/extension_unpacker.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 | 19 |
| 19 namespace errors = extension_manifest_errors; | 20 namespace errors = extension_manifest_errors; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 47 | 48 |
| 48 void DelegateToFake() { | 49 void DelegateToFake() { |
| 49 ON_CALL(*this, OnUnpackSuccess(_, _, _)) | 50 ON_CALL(*this, OnUnpackSuccess(_, _, _)) |
| 50 .WillByDefault(Invoke(::OnUnpackSuccess)); | 51 .WillByDefault(Invoke(::OnUnpackSuccess)); |
| 51 } | 52 } |
| 52 }; | 53 }; |
| 53 | 54 |
| 54 class SandboxedExtensionUnpackerTest : public testing::Test { | 55 class SandboxedExtensionUnpackerTest : public testing::Test { |
| 55 public: | 56 public: |
| 56 virtual void SetUp() { | 57 virtual void SetUp() { |
| 58 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 57 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &loop_)); | 59 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &loop_)); |
| 58 // It will delete itself. | 60 // It will delete itself. |
| 59 client_ = new MockSandboxedExtensionUnpackerClient; | 61 client_ = new MockSandboxedExtensionUnpackerClient; |
| 60 client_->DelegateToFake(); | 62 client_->DelegateToFake(); |
| 61 } | 63 } |
| 62 | 64 |
| 63 virtual void TearDown() { | 65 virtual void TearDown() { |
| 64 // Need to destruct SandboxedExtensionUnpacker before the message loop since | 66 // Need to destruct SandboxedExtensionUnpacker before the message loop since |
| 65 // it posts a task to it. | 67 // it posts a task to it. |
| 66 sandboxed_unpacker_ = NULL; | 68 sandboxed_unpacker_ = NULL; |
| 67 loop_.RunAllPending(); | 69 loop_.RunAllPending(); |
| 68 // Clean up finally. | |
| 69 ASSERT_TRUE(file_util::Delete(install_dir_, true)) << | |
| 70 install_dir_.value(); | |
| 71 } | 70 } |
| 72 | 71 |
| 73 void SetupUnpacker(const std::string& crx_name) { | 72 void SetupUnpacker(const std::string& crx_name) { |
| 74 FilePath original_path; | 73 FilePath original_path; |
| 75 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); | 74 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); |
| 76 original_path = original_path.AppendASCII("extensions") | 75 original_path = original_path.AppendASCII("extensions") |
| 77 .AppendASCII("unpacker") | 76 .AppendASCII("unpacker") |
| 78 .AppendASCII(crx_name); | 77 .AppendASCII(crx_name); |
| 79 ASSERT_TRUE(file_util::PathExists(original_path)) << original_path.value(); | 78 ASSERT_TRUE(file_util::PathExists(original_path)) << original_path.value(); |
| 80 | 79 |
| 81 // Try bots won't let us write into DIR_TEST_DATA, so we have to create | 80 // Try bots won't let us write into DIR_TEST_DATA, so we have to write the |
| 82 // a temp folder to play in. | 81 // CRX to the temp directory, and create a subdirectory into which to |
| 83 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &install_dir_)); | 82 // unpack it. |
| 84 install_dir_ = | 83 FilePath crx_path = temp_dir_.path().AppendASCII(crx_name); |
| 85 install_dir_.AppendASCII("sandboxed_extension_unpacker_test"); | |
| 86 file_util::Delete(install_dir_, true); | |
| 87 file_util::CreateDirectory(install_dir_); | |
| 88 | |
| 89 FilePath crx_path = install_dir_.AppendASCII(crx_name); | |
| 90 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) << | 84 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) << |
| 91 "Original path: " << original_path.value() << | 85 "Original path: " << original_path.value() << |
| 92 ", Crx path: " << crx_path.value(); | 86 ", Crx path: " << crx_path.value(); |
| 93 | 87 |
| 94 unpacker_.reset(new ExtensionUnpacker(crx_path)); | 88 unpacker_.reset(new ExtensionUnpacker(crx_path)); |
| 95 | 89 |
| 96 // Build a temp area where the extension will be unpacked. | 90 // Build a temp area where the extension will be unpacked. |
| 97 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &temp_dir_)); | 91 temp_path_ = |
| 98 temp_dir_ = temp_dir_.AppendASCII("sandboxed_extension_unpacker_test_Temp"); | 92 temp_dir_.path().AppendASCII("sandboxed_extension_unpacker_test_Temp"); |
| 99 ASSERT_TRUE(file_util::CreateDirectory(temp_dir_)); | 93 ASSERT_TRUE(file_util::CreateDirectory(temp_path_)); |
| 100 | 94 |
| 101 sandboxed_unpacker_ = | 95 sandboxed_unpacker_ = |
| 102 new SandboxedExtensionUnpacker(crx_path, NULL, client_); | 96 new SandboxedExtensionUnpacker(crx_path, NULL, client_); |
| 103 | 97 |
| 104 // Hack since SandboxedExtensionUnpacker gets its background thread id from | 98 // Hack since SandboxedExtensionUnpacker gets its background thread id from |
| 105 // the Start call, but we don't call it here. | 99 // the Start call, but we don't call it here. |
| 106 sandboxed_unpacker_->thread_identifier_ = BrowserThread::FILE; | 100 sandboxed_unpacker_->thread_identifier_ = BrowserThread::FILE; |
| 107 EXPECT_TRUE(PrepareUnpackerEnv()); | 101 EXPECT_TRUE(PrepareUnpackerEnv()); |
| 108 } | 102 } |
| 109 | 103 |
| 110 bool PrepareUnpackerEnv() { | 104 bool PrepareUnpackerEnv() { |
| 111 sandboxed_unpacker_->extension_root_ = | 105 sandboxed_unpacker_->extension_root_ = |
| 112 install_dir_.AppendASCII(extension_filenames::kTempExtensionName); | 106 temp_dir_.path().AppendASCII(extension_filenames::kTempExtensionName); |
| 113 | 107 |
| 114 if (!sandboxed_unpacker_->temp_dir_.Set(install_dir_)) | 108 if (!sandboxed_unpacker_->temp_dir_.Set(temp_dir_.path())) |
| 115 return false; | 109 return false; |
| 116 sandboxed_unpacker_->public_key_ = | 110 sandboxed_unpacker_->public_key_ = |
| 117 "ocnapchkplbmjmpfehjocmjnipfmogkh"; | 111 "ocnapchkplbmjmpfehjocmjnipfmogkh"; |
| 118 return true; | 112 return true; |
| 119 } | 113 } |
| 120 | 114 |
| 121 void OnUnpackSucceeded() { | 115 void OnUnpackSucceeded() { |
| 122 sandboxed_unpacker_->OnUnpackExtensionSucceeded( | 116 sandboxed_unpacker_->OnUnpackExtensionSucceeded( |
| 123 *unpacker_->parsed_manifest()); | 117 *unpacker_->parsed_manifest()); |
| 124 } | 118 } |
| 125 | 119 |
| 126 FilePath GetInstallPath() { | 120 FilePath GetInstallPath() { |
| 127 return install_dir_.AppendASCII(extension_filenames::kTempExtensionName); | 121 return temp_dir_.path().AppendASCII( |
| 122 extension_filenames::kTempExtensionName); |
| 128 } | 123 } |
| 129 | 124 |
| 130 bool TempFilesRemoved() { | 125 bool TempFilesRemoved() { |
| 131 // Check that temporary files were cleaned up. | 126 // Check that temporary files were cleaned up. |
| 132 file_util::FileEnumerator::FILE_TYPE files_and_dirs = | 127 file_util::FileEnumerator::FILE_TYPE files_and_dirs = |
| 133 static_cast<file_util::FileEnumerator::FILE_TYPE>( | 128 static_cast<file_util::FileEnumerator::FILE_TYPE>( |
| 134 file_util::FileEnumerator::DIRECTORIES | | 129 file_util::FileEnumerator::DIRECTORIES | |
| 135 file_util::FileEnumerator::FILES); | 130 file_util::FileEnumerator::FILES); |
| 136 | 131 |
| 137 file_util::FileEnumerator temp_iterator( | 132 file_util::FileEnumerator temp_iterator( |
| 138 temp_dir_, | 133 temp_path_, |
| 139 true, // recursive | 134 true, // recursive |
| 140 files_and_dirs | 135 files_and_dirs |
| 141 ); | 136 ); |
| 142 int items_not_removed = 0; | 137 int items_not_removed = 0; |
| 143 FilePath item_in_temp; | 138 FilePath item_in_temp; |
| 144 item_in_temp = temp_iterator.Next(); | 139 item_in_temp = temp_iterator.Next(); |
| 145 while (!item_in_temp.value().empty()) { | 140 while (!item_in_temp.value().empty()) { |
| 146 items_not_removed++; | 141 items_not_removed++; |
| 147 EXPECT_STREQ(FILE_PATH_LITERAL(""), item_in_temp.value().c_str()) | 142 EXPECT_STREQ(FILE_PATH_LITERAL(""), item_in_temp.value().c_str()) |
| 148 << "File was not removed on success."; | 143 << "File was not removed on success."; |
| 149 item_in_temp = temp_iterator.Next(); | 144 item_in_temp = temp_iterator.Next(); |
| 150 } | 145 } |
| 151 return (items_not_removed == 0); | 146 return (items_not_removed == 0); |
| 152 } | 147 } |
| 153 | 148 |
| 154 protected: | 149 protected: |
| 155 FilePath install_dir_; | 150 ScopedTempDir temp_dir_; |
| 156 FilePath temp_dir_; | 151 FilePath temp_path_; |
| 157 MockSandboxedExtensionUnpackerClient* client_; | 152 MockSandboxedExtensionUnpackerClient* client_; |
| 158 scoped_ptr<ExtensionUnpacker> unpacker_; | 153 scoped_ptr<ExtensionUnpacker> unpacker_; |
| 159 scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_; | 154 scoped_refptr<SandboxedExtensionUnpacker> sandboxed_unpacker_; |
| 160 MessageLoop loop_; | 155 MessageLoop loop_; |
| 161 scoped_ptr<BrowserThread> file_thread_; | 156 scoped_ptr<BrowserThread> file_thread_; |
| 162 }; | 157 }; |
| 163 | 158 |
| 164 TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) { | 159 TEST_F(SandboxedExtensionUnpackerTest, NoCatalogsSuccess) { |
| 165 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); | 160 EXPECT_CALL(*client_, OnUnpackSuccess(_, _, _)); |
| 166 EXPECT_CALL(*client_, OnUnpackFailure(_)).Times(0); | 161 EXPECT_CALL(*client_, OnUnpackFailure(_)).Times(0); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 OnUnpackSucceeded(); | 203 OnUnpackSucceeded(); |
| 209 | 204 |
| 210 // Check that there is newer _locales/en_US/messages.json file. | 205 // Check that there is newer _locales/en_US/messages.json file. |
| 211 base::PlatformFileInfo new_info; | 206 base::PlatformFileInfo new_info; |
| 212 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &new_info)); | 207 EXPECT_TRUE(file_util::GetFileInfo(messages_file, &new_info)); |
| 213 | 208 |
| 214 EXPECT_TRUE(new_info.last_modified > old_info.last_modified); | 209 EXPECT_TRUE(new_info.last_modified > old_info.last_modified); |
| 215 | 210 |
| 216 ASSERT_TRUE(TempFilesRemoved()); | 211 ASSERT_TRUE(TempFilesRemoved()); |
| 217 } | 212 } |
| OLD | NEW |