sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <iterator> |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 6 | #include <memory> |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 7 | #include <string> |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 8 | #include <utility> |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 11 | #include "base/bind.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 12 | #include "base/callback.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
| 14 | #include "base/files/file_util.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 15 | #include "base/macros.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 17 | #include "base/path_service.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 18 | #include "base/run_loop.h" |
fdoray | b26583c | 2017-05-16 18:47:01 | [diff] [blame] | 19 | #include "base/task_scheduler/post_task.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 20 | #include "base/test/scoped_path_override.h" |
fdoray | b26583c | 2017-05-16 18:47:01 | [diff] [blame] | 21 | #include "base/test/scoped_task_environment.h" |
| 22 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 23 | #include "base/version.h" |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 24 | #include "components/component_updater/component_installer.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 25 | #include "components/component_updater/component_updater_paths.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 26 | #include "components/component_updater/component_updater_service.h" |
| 27 | #include "components/component_updater/component_updater_service_internal.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 28 | #include "components/update_client/component_unpacker.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 29 | #include "components/update_client/crx_update_item.h" |
| 30 | #include "components/update_client/test_configurator.h" |
| 31 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 32 | #include "components/update_client/update_client_errors.h" |
Jay Civelli | 29f9b94 | 2017-11-08 20:07:26 | [diff] [blame] | 33 | #include "services/service_manager/public/cpp/connector.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 34 | #include "testing/gmock/include/gmock/gmock.h" |
| 35 | #include "testing/gtest/include/gtest/gtest.h" |
| 36 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 37 | using ComponentUnpacker = update_client::ComponentUnpacker; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 38 | using Configurator = update_client::Configurator; |
| 39 | using CrxUpdateItem = update_client::CrxUpdateItem; |
| 40 | using TestConfigurator = update_client::TestConfigurator; |
| 41 | using UpdateClient = update_client::UpdateClient; |
| 42 | |
| 43 | using ::testing::_; |
| 44 | using ::testing::Invoke; |
| 45 | |
| 46 | namespace component_updater { |
| 47 | |
| 48 | namespace { |
| 49 | |
| 50 | // This hash corresponds to jebgalgnebhfojomionfpkfelancnnkf.crx. |
| 51 | const uint8_t kSha256Hash[] = {0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec, |
| 52 | 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5, |
| 53 | 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4, |
| 54 | 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40}; |
| 55 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 56 | constexpr base::FilePath::CharType relative_install_dir[] = |
| 57 | FILE_PATH_LITERAL("fake"); |
| 58 | |
| 59 | base::FilePath test_file(const char* file) { |
| 60 | base::FilePath path; |
| 61 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 62 | return path.AppendASCII("components") |
| 63 | .AppendASCII("test") |
| 64 | .AppendASCII("data") |
| 65 | .AppendASCII("update_client") |
| 66 | .AppendASCII(file); |
| 67 | } |
| 68 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 69 | class MockUpdateClient : public UpdateClient { |
| 70 | public: |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 71 | MockUpdateClient() {} |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 72 | |
| 73 | void Install(const std::string& id, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 74 | CrxDataCallback crx_data_callback, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 75 | Callback callback) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 76 | DoInstall(id, std::move(crx_data_callback)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 77 | std::move(callback).Run(update_client::Error::NONE); |
| 78 | } |
| 79 | |
| 80 | void Update(const std::vector<std::string>& ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 81 | CrxDataCallback crx_data_callback, |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 82 | bool is_foreground, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 83 | Callback callback) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 84 | DoUpdate(ids, std::move(crx_data_callback)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 85 | std::move(callback).Run(update_client::Error::NONE); |
| 86 | } |
| 87 | |
| 88 | void SendUninstallPing(const std::string& id, |
| 89 | const base::Version& version, |
| 90 | int reason, |
| 91 | Callback callback) { |
| 92 | DoSendUninstallPing(id, version, reason); |
| 93 | std::move(callback).Run(update_client::Error::NONE); |
| 94 | } |
| 95 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 96 | MOCK_METHOD1(AddObserver, void(Observer* observer)); |
| 97 | MOCK_METHOD1(RemoveObserver, void(Observer* observer)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 98 | MOCK_METHOD2(DoInstall, |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 99 | void(const std::string& id, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 100 | const CrxDataCallback& crx_data_callback)); |
| 101 | MOCK_METHOD2(DoUpdate, |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 102 | void(const std::vector<std::string>& ids, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 103 | const CrxDataCallback& crx_data_callback)); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 104 | MOCK_CONST_METHOD2(GetCrxUpdateState, |
| 105 | bool(const std::string& id, CrxUpdateItem* update_item)); |
| 106 | MOCK_CONST_METHOD1(IsUpdating, bool(const std::string& id)); |
| 107 | MOCK_METHOD0(Stop, void()); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 108 | MOCK_METHOD3(DoSendUninstallPing, |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 109 | void(const std::string& id, |
| 110 | const base::Version& version, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 111 | int reason)); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 112 | |
| 113 | private: |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 114 | ~MockUpdateClient() override {} |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 115 | }; |
| 116 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame^] | 117 | class MockInstallerPolicy : public ComponentInstallerPolicy { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 118 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame^] | 119 | MockInstallerPolicy() {} |
| 120 | ~MockInstallerPolicy() override {} |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 121 | |
| 122 | bool VerifyInstallation(const base::DictionaryValue& manifest, |
| 123 | const base::FilePath& dir) const override { |
| 124 | return true; |
| 125 | } |
| 126 | |
sorin | 3574ef9 | 2016-08-03 16:46:01 | [diff] [blame] | 127 | bool SupportsGroupPolicyEnabledComponentUpdates() const override { |
sorin | 098a4a7 | 2016-08-30 04:43:24 | [diff] [blame] | 128 | return true; |
sorin | 3574ef9 | 2016-08-03 16:46:01 | [diff] [blame] | 129 | } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 130 | |
| 131 | bool RequiresNetworkEncryption() const override { return true; } |
| 132 | |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 133 | update_client::CrxInstaller::Result OnCustomInstall( |
| 134 | const base::DictionaryValue& manifest, |
| 135 | const base::FilePath& install_dir) override { |
| 136 | return update_client::CrxInstaller::Result(0); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 137 | } |
| 138 | |
Xiaochu Liu | 1e306a3 | 2017-11-08 18:57:34 | [diff] [blame] | 139 | void OnCustomUninstall() override {} |
| 140 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 141 | void ComponentReady( |
| 142 | const base::Version& version, |
| 143 | const base::FilePath& install_dir, |
| 144 | std::unique_ptr<base::DictionaryValue> manifest) override {} |
| 145 | |
| 146 | base::FilePath GetRelativeInstallDir() const override { |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 147 | return base::FilePath(relative_install_dir); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void GetHash(std::vector<uint8_t>* hash) const override { GetPkHash(hash); } |
| 151 | |
| 152 | std::string GetName() const override { return "fake name"; } |
| 153 | |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 154 | update_client::InstallerAttributes GetInstallerAttributes() const override { |
| 155 | update_client::InstallerAttributes installer_attributes; |
| 156 | installer_attributes["ap"] = "fake-ap"; |
| 157 | installer_attributes["is-enterprise"] = "1"; |
| 158 | return installer_attributes; |
| 159 | } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 160 | |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 161 | std::vector<std::string> GetMimeTypes() const override { |
| 162 | return std::vector<std::string>(); |
| 163 | } |
| 164 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 165 | private: |
| 166 | static void GetPkHash(std::vector<uint8_t>* hash) { |
| 167 | hash->assign(std::begin(kSha256Hash), std::end(kSha256Hash)); |
| 168 | } |
| 169 | }; |
| 170 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 171 | class ComponentInstallerTest : public testing::Test { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 172 | public: |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 173 | ComponentInstallerTest(); |
| 174 | ~ComponentInstallerTest() override; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 175 | |
| 176 | MockUpdateClient& update_client() { return *update_client_; } |
| 177 | ComponentUpdateService* component_updater() { |
| 178 | return component_updater_.get(); |
| 179 | } |
| 180 | scoped_refptr<TestConfigurator> configurator() const { return config_; } |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 181 | base::OnceClosure quit_closure() { return runloop_.QuitClosure(); } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 182 | |
| 183 | protected: |
| 184 | void RunThreads(); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 185 | void Unpack(const base::FilePath& crx_path); |
| 186 | ComponentUnpacker::Result result() const { return result_; } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 187 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 188 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 189 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 190 | private: |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 191 | void UnpackComplete(const ComponentUnpacker::Result& result); |
| 192 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 193 | const scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_ = |
| 194 | base::ThreadTaskRunnerHandle::Get(); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 195 | base::RunLoop runloop_; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 196 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 197 | scoped_refptr<TestConfigurator> config_ = |
| 198 | base::MakeRefCounted<TestConfigurator>(); |
| 199 | scoped_refptr<MockUpdateClient> update_client_ = |
| 200 | base::MakeRefCounted<MockUpdateClient>(); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 201 | std::unique_ptr<ComponentUpdateService> component_updater_; |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 202 | ComponentUnpacker::Result result_; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 203 | }; |
| 204 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 205 | ComponentInstallerTest::ComponentInstallerTest() { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 206 | EXPECT_CALL(update_client(), AddObserver(_)).Times(1); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 207 | component_updater_ = |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 208 | std::make_unique<CrxUpdateService>(config_, update_client_); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 209 | } |
| 210 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 211 | ComponentInstallerTest::~ComponentInstallerTest() { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 212 | EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1); |
| 213 | component_updater_.reset(); |
| 214 | } |
| 215 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 216 | void ComponentInstallerTest::RunThreads() { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 217 | runloop_.Run(); |
| 218 | } |
| 219 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 220 | void ComponentInstallerTest::Unpack(const base::FilePath& crx_path) { |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 221 | auto config = base::MakeRefCounted<TestConfigurator>(); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 222 | auto component_unpacker = base::MakeRefCounted<ComponentUnpacker>( |
| 223 | std::vector<uint8_t>(std::begin(kSha256Hash), std::end(kSha256Hash)), |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 224 | crx_path, nullptr, config->CreateServiceManagerConnector()); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 225 | component_unpacker->Unpack(base::BindOnce( |
| 226 | &ComponentInstallerTest::UnpackComplete, base::Unretained(this))); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 227 | RunThreads(); |
| 228 | } |
| 229 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 230 | void ComponentInstallerTest::UnpackComplete( |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 231 | const ComponentUnpacker::Result& result) { |
| 232 | result_ = result; |
| 233 | |
| 234 | EXPECT_EQ(update_client::UnpackerError::kNone, result_.error); |
| 235 | EXPECT_EQ(0, result_.extended_error); |
| 236 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 237 | main_thread_task_runner_->PostTask(FROM_HERE, quit_closure()); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 238 | } |
| 239 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 240 | } // namespace |
| 241 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 242 | // Tests that the component metadata is propagated from the component installer |
| 243 | // and its component policy, through the instance of the CrxComponent, to the |
| 244 | // component updater service. |
| 245 | TEST_F(ComponentInstallerTest, RegisterComponent) { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 246 | class LoopHandler { |
| 247 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 248 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 249 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 250 | |
| 251 | void OnUpdate(const std::vector<std::string>& ids, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 252 | const UpdateClient::CrxDataCallback& crx_data_callback) { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 253 | static int cnt = 0; |
| 254 | ++cnt; |
| 255 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 256 | std::move(quit_closure_).Run(); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | private: |
| 260 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 261 | base::OnceClosure quit_closure_; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 262 | }; |
| 263 | |
Sorin Jianu | 0bf4bd3f | 2017-06-01 23:42:32 | [diff] [blame] | 264 | base::ScopedPathOverride scoped_path_override(DIR_COMPONENT_USER); |
| 265 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 266 | const std::string id("jebgalgnebhfojomionfpkfelancnnkf"); |
| 267 | |
| 268 | // Quit after one update check has been fired. |
| 269 | LoopHandler loop_handler(1, quit_closure()); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 270 | EXPECT_CALL(update_client(), DoUpdate(_, _)) |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 271 | .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 272 | |
| 273 | EXPECT_CALL(update_client(), GetCrxUpdateState(id, _)).Times(1); |
| 274 | EXPECT_CALL(update_client(), Stop()).Times(1); |
| 275 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 276 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame^] | 277 | std::make_unique<MockInstallerPolicy>()); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 278 | installer->Register(component_updater(), base::OnceClosure()); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 279 | |
| 280 | RunThreads(); |
| 281 | |
| 282 | CrxUpdateItem item; |
| 283 | EXPECT_TRUE(component_updater()->GetComponentDetails(id, &item)); |
| 284 | const CrxComponent& component(item.component); |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 285 | |
| 286 | update_client::InstallerAttributes expected_attrs; |
| 287 | expected_attrs["ap"] = "fake-ap"; |
| 288 | expected_attrs["is-enterprise"] = "1"; |
| 289 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 290 | EXPECT_EQ( |
| 291 | std::vector<uint8_t>(std::begin(kSha256Hash), std::end(kSha256Hash)), |
| 292 | component.pk_hash); |
| 293 | EXPECT_EQ(base::Version("0.0.0.0"), component.version); |
| 294 | EXPECT_TRUE(component.fingerprint.empty()); |
| 295 | EXPECT_STREQ("fake name", component.name.c_str()); |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 296 | EXPECT_EQ(expected_attrs, component.installer_attributes); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 297 | EXPECT_TRUE(component.requires_network_encryption); |
sorin | 098a4a7 | 2016-08-30 04:43:24 | [diff] [blame] | 298 | EXPECT_TRUE(component.supports_group_policy_enable_component_updates); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 299 | } |
| 300 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 301 | // Tests that the unpack path is removed when the install succeeded. |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 302 | TEST_F(ComponentInstallerTest, UnpackPathInstallSuccess) { |
| 303 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame^] | 304 | std::make_unique<MockInstallerPolicy>()); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 305 | |
| 306 | Unpack(test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 307 | |
| 308 | const auto unpack_path = result().unpack_path; |
| 309 | EXPECT_TRUE(base::DirectoryExists(unpack_path)); |
Minh X. Nguyen | aafd763 | 2017-10-19 21:12:35 | [diff] [blame] | 310 | EXPECT_EQ(update_client::jebg_public_key, result().public_key); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 311 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 312 | base::ScopedPathOverride scoped_path_override(DIR_COMPONENT_USER); |
| 313 | base::FilePath base_dir; |
| 314 | EXPECT_TRUE(PathService::Get(DIR_COMPONENT_USER, &base_dir)); |
| 315 | base_dir = base_dir.Append(relative_install_dir); |
| 316 | EXPECT_TRUE(base::CreateDirectory(base_dir)); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 317 | installer->Install( |
Sorin Jianu | ea5534e9 | 2017-10-27 01:40:28 | [diff] [blame] | 318 | unpack_path, update_client::jebg_public_key, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 319 | base::BindOnce([](const update_client::CrxInstaller::Result& result) { |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 320 | EXPECT_EQ(0, result.error); |
| 321 | })); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 322 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 323 | scoped_task_environment_.RunUntilIdle(); |
| 324 | |
| 325 | EXPECT_FALSE(base::PathExists(unpack_path)); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 326 | EXPECT_CALL(update_client(), Stop()).Times(1); |
| 327 | } |
| 328 | |
| 329 | // Tests that the unpack path is removed when the install failed. |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 330 | TEST_F(ComponentInstallerTest, UnpackPathInstallError) { |
| 331 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame^] | 332 | std::make_unique<MockInstallerPolicy>()); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 333 | |
| 334 | Unpack(test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 335 | |
| 336 | const auto unpack_path = result().unpack_path; |
| 337 | EXPECT_TRUE(base::DirectoryExists(unpack_path)); |
| 338 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 339 | // Test the precondition that DIR_COMPONENT_USER is not registered with |
| 340 | // the path service. |
| 341 | base::FilePath base_dir; |
| 342 | EXPECT_FALSE(PathService::Get(DIR_COMPONENT_USER, &base_dir)); |
| 343 | |
| 344 | // Calling |Install| fails since DIR_COMPONENT_USER does not exist. |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 345 | installer->Install( |
Sorin Jianu | ea5534e9 | 2017-10-27 01:40:28 | [diff] [blame] | 346 | unpack_path, update_client::jebg_public_key, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 347 | base::BindOnce([](const update_client::CrxInstaller::Result& result) { |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 348 | EXPECT_EQ(static_cast<int>( |
| 349 | update_client::InstallError::NO_DIR_COMPONENT_USER), |
| 350 | result.error); |
| 351 | })); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 352 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 353 | scoped_task_environment_.RunUntilIdle(); |
| 354 | |
| 355 | EXPECT_FALSE(base::PathExists(unpack_path)); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 356 | EXPECT_CALL(update_client(), Stop()).Times(1); |
| 357 | } |
| 358 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 359 | } // namespace component_updater |