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