sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1 | // Copyright 2015 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 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 5 | #include "components/component_updater/component_updater_service.h" |
| 6 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 7 | #include <limits> |
Sorin Jianu | 990ee14 | 2017-06-02 22:34:08 | [diff] [blame] | 8 | #include <memory> |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 9 | #include <string> |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 10 | #include <utility> |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
| 13 | #include "base/bind.h" |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 14 | #include "base/bind_helpers.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
| 16 | #include "base/files/file_util.h" |
| 17 | #include "base/macros.h" |
| 18 | #include "base/memory/ref_counted.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 19 | #include "base/run_loop.h" |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 20 | #include "base/stl_util.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 21 | #include "base/task/post_task.h" |
Devlin Cronin | 69228f4 | 2018-06-01 17:25:10 | [diff] [blame] | 22 | #include "base/test/metrics/histogram_tester.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 23 | #include "base/test/task_environment.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 24 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 25 | #include "base/values.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 26 | #include "components/component_updater/component_updater_service_internal.h" |
| 27 | #include "components/update_client/test_configurator.h" |
| 28 | #include "components/update_client/test_installer.h" |
| 29 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 30 | #include "components/update_client/update_client_errors.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 31 | #include "testing/gmock/include/gmock/gmock.h" |
| 32 | #include "testing/gtest/include/gtest/gtest.h" |
| 33 | |
| 34 | using Configurator = update_client::Configurator; |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 35 | using Result = update_client::CrxInstaller::Result; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 36 | using TestConfigurator = update_client::TestConfigurator; |
| 37 | using UpdateClient = update_client::UpdateClient; |
| 38 | |
| 39 | using ::testing::_; |
| 40 | using ::testing::AnyNumber; |
| 41 | using ::testing::Invoke; |
| 42 | using ::testing::Mock; |
| 43 | using ::testing::Return; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 44 | using ::testing::Unused; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 45 | |
| 46 | namespace component_updater { |
| 47 | |
| 48 | class MockInstaller : public CrxInstaller { |
| 49 | public: |
| 50 | MockInstaller(); |
| 51 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 52 | // gMock does not support mocking functions with parameters which have |
| 53 | // move semantics. This function is a shim to work around it. |
| 54 | void Install(const base::FilePath& unpack_path, |
| 55 | const std::string& public_key, |
Sorin Jianu | 9d64af67 | 2020-02-05 19:14:34 | [diff] [blame] | 56 | std::unique_ptr<InstallParams> install_params, |
Daniel Cheng | 6784852 | 2018-04-27 22:04:41 | [diff] [blame] | 57 | update_client::CrxInstaller::Callback callback) override { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 58 | DoInstall(unpack_path, callback); |
| 59 | } |
| 60 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 61 | MOCK_METHOD1(OnUpdateError, void(int error)); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 62 | MOCK_METHOD2(DoInstall, |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 63 | void(const base::FilePath& unpack_path, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 64 | const update_client::CrxInstaller::Callback& callback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 65 | MOCK_METHOD2(GetInstalledFile, |
| 66 | bool(const std::string& file, base::FilePath* installed_file)); |
| 67 | MOCK_METHOD0(Uninstall, bool()); |
| 68 | |
| 69 | private: |
| 70 | ~MockInstaller() override; |
| 71 | }; |
| 72 | |
| 73 | class MockUpdateClient : public UpdateClient { |
| 74 | public: |
| 75 | MockUpdateClient(); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 76 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 77 | // gMock does not support mocking functions with parameters which have |
| 78 | // move semantics. This function is a shim to work around it. |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 79 | void Install(const std::string& id, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 80 | CrxDataCallback crx_data_callback, |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 81 | CrxStateChangeCallback crx_state_change_callback, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 82 | Callback callback) override { |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 83 | DoInstall(id); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 84 | std::move(callback).Run(update_client::Error::NONE); |
| 85 | } |
| 86 | |
| 87 | void Update(const std::vector<std::string>& ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 88 | CrxDataCallback crx_data_callback, |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 89 | CrxStateChangeCallback crx_state_change_callback, |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 90 | bool is_foreground, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 91 | Callback callback) override { |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 92 | // All update calls initiated by the component update service are |
| 93 | // automatically triggered as background updates without user intervention. |
| 94 | EXPECT_FALSE(is_foreground); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 95 | DoUpdate(ids); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 96 | std::move(callback).Run(update_client::Error::NONE); |
| 97 | } |
| 98 | |
| 99 | void SendUninstallPing(const std::string& id, |
| 100 | const base::Version& version, |
| 101 | int reason, |
Daniel Cheng | 6784852 | 2018-04-27 22:04:41 | [diff] [blame] | 102 | Callback callback) override { |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 103 | DoSendUninstallPing(id, version, reason); |
| 104 | std::move(callback).Run(update_client::Error::NONE); |
| 105 | } |
| 106 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 107 | MOCK_METHOD1(AddObserver, void(Observer* observer)); |
| 108 | MOCK_METHOD1(RemoveObserver, void(Observer* observer)); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 109 | MOCK_METHOD1(DoInstall, void(const std::string& id)); |
| 110 | MOCK_METHOD1(DoUpdate, void(const std::vector<std::string>& ids)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 111 | MOCK_CONST_METHOD2(GetCrxUpdateState, |
| 112 | bool(const std::string& id, CrxUpdateItem* update_item)); |
| 113 | MOCK_CONST_METHOD1(IsUpdating, bool(const std::string& id)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 114 | MOCK_METHOD0(Stop, void()); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 115 | MOCK_METHOD3(DoSendUninstallPing, |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 116 | void(const std::string& id, |
| 117 | const base::Version& version, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 118 | int reason)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | ~MockUpdateClient() override; |
| 122 | }; |
| 123 | |
| 124 | class MockServiceObserver : public ServiceObserver { |
| 125 | public: |
| 126 | MockServiceObserver(); |
| 127 | ~MockServiceObserver() override; |
| 128 | |
| 129 | MOCK_METHOD2(OnEvent, void(Events event, const std::string&)); |
| 130 | }; |
| 131 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 132 | class MockUpdateScheduler : public UpdateScheduler { |
| 133 | public: |
| 134 | MOCK_METHOD4(Schedule, |
| 135 | void(const base::TimeDelta& initial_delay, |
| 136 | const base::TimeDelta& delay, |
| 137 | const UserTask& user_task, |
| 138 | const OnStopTaskCallback& on_stop)); |
| 139 | MOCK_METHOD0(Stop, void()); |
| 140 | }; |
| 141 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 142 | class ComponentUpdaterTest : public testing::Test { |
| 143 | public: |
| 144 | ComponentUpdaterTest(); |
| 145 | ~ComponentUpdaterTest() override; |
| 146 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 147 | // Makes the full path to a component updater test file. |
| 148 | const base::FilePath test_file(const char* file); |
| 149 | |
| 150 | MockUpdateClient& update_client() { return *update_client_; } |
| 151 | ComponentUpdateService& component_updater() { return *component_updater_; } |
| 152 | scoped_refptr<TestConfigurator> configurator() const { return config_; } |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 153 | base::OnceClosure quit_closure() { return runloop_.QuitClosure(); } |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 154 | MockUpdateScheduler& scheduler() { return *scheduler_; } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 155 | |
| 156 | protected: |
| 157 | void RunThreads(); |
| 158 | |
| 159 | private: |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 160 | void RunUpdateTask(const UpdateScheduler::UserTask& user_task); |
| 161 | void Schedule(const base::TimeDelta& initial_delay, |
| 162 | const base::TimeDelta& delay, |
| 163 | const UpdateScheduler::UserTask& user_task, |
| 164 | const UpdateScheduler::OnStopTaskCallback& on_stop); |
| 165 | |
Gabriel Charette | dfa3604 | 2019-08-19 17:30:11 | [diff] [blame] | 166 | base::test::TaskEnvironment task_environment_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 167 | base::RunLoop runloop_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 168 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 169 | scoped_refptr<TestConfigurator> config_ = |
| 170 | base::MakeRefCounted<TestConfigurator>(); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 171 | MockUpdateScheduler* scheduler_; |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 172 | scoped_refptr<MockUpdateClient> update_client_ = |
| 173 | base::MakeRefCounted<MockUpdateClient>(); |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 174 | std::unique_ptr<ComponentUpdateService> component_updater_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 175 | |
| 176 | DISALLOW_COPY_AND_ASSIGN(ComponentUpdaterTest); |
| 177 | }; |
| 178 | |
| 179 | class OnDemandTester { |
| 180 | public: |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 181 | void OnDemand(ComponentUpdateService* cus, |
| 182 | const std::string& id, |
| 183 | OnDemandUpdater::Priority priority); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 184 | update_client::Error error() const { return error_; } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 185 | |
| 186 | private: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 187 | void OnDemandComplete(update_client::Error error); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 188 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 189 | update_client::Error error_ = update_client::Error::NONE; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 190 | }; |
| 191 | |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 192 | MockInstaller::MockInstaller() = default; |
| 193 | MockInstaller::~MockInstaller() = default; |
| 194 | MockUpdateClient::MockUpdateClient() = default; |
| 195 | MockUpdateClient::~MockUpdateClient() = default; |
| 196 | MockServiceObserver::MockServiceObserver() = default; |
| 197 | MockServiceObserver::~MockServiceObserver() = default; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 198 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 199 | void OnDemandTester::OnDemand(ComponentUpdateService* cus, |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 200 | const std::string& id, |
| 201 | OnDemandUpdater::Priority priority) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 202 | cus->GetOnDemandUpdater().OnDemandUpdate( |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 203 | id, priority, |
| 204 | base::BindOnce(&OnDemandTester::OnDemandComplete, |
| 205 | base::Unretained(this))); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 206 | } |
| 207 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 208 | void OnDemandTester::OnDemandComplete(update_client::Error error) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 209 | error_ = error; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 210 | } |
| 211 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 212 | std::unique_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 213 | scoped_refptr<Configurator> config) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 214 | DCHECK(config); |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 215 | return std::make_unique<CrxUpdateService>( |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 216 | config, std::make_unique<MockUpdateScheduler>(), |
| 217 | base::MakeRefCounted<MockUpdateClient>()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 218 | } |
| 219 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 220 | ComponentUpdaterTest::ComponentUpdaterTest() { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 221 | EXPECT_CALL(update_client(), AddObserver(_)).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 222 | auto scheduler = std::make_unique<MockUpdateScheduler>(); |
| 223 | scheduler_ = scheduler.get(); |
| 224 | ON_CALL(*scheduler_, Schedule(_, _, _, _)) |
| 225 | .WillByDefault(Invoke(this, &ComponentUpdaterTest::Schedule)); |
| 226 | component_updater_ = std::make_unique<CrxUpdateService>( |
| 227 | config_, std::move(scheduler), update_client_); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | ComponentUpdaterTest::~ComponentUpdaterTest() { |
| 231 | EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 232 | component_updater_.reset(); |
| 233 | } |
| 234 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 235 | void ComponentUpdaterTest::RunThreads() { |
| 236 | runloop_.Run(); |
| 237 | } |
| 238 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 239 | void ComponentUpdaterTest::RunUpdateTask( |
| 240 | const UpdateScheduler::UserTask& user_task) { |
Gabriel Charette | dfa3604 | 2019-08-19 17:30:11 | [diff] [blame] | 241 | task_environment_.GetMainThreadTaskRunner()->PostTask( |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 242 | FROM_HERE, base::BindRepeating( |
| 243 | [](const UpdateScheduler::UserTask& user_task, |
| 244 | ComponentUpdaterTest* test) { |
| 245 | user_task.Run(base::BindOnce( |
| 246 | [](const UpdateScheduler::UserTask& user_task, |
| 247 | ComponentUpdaterTest* test) { |
| 248 | test->RunUpdateTask(user_task); |
| 249 | }, |
| 250 | user_task, base::Unretained(test))); |
| 251 | }, |
| 252 | user_task, base::Unretained(this))); |
| 253 | } |
| 254 | |
| 255 | void ComponentUpdaterTest::Schedule( |
| 256 | const base::TimeDelta& initial_delay, |
| 257 | const base::TimeDelta& delay, |
| 258 | const UpdateScheduler::UserTask& user_task, |
| 259 | const UpdateScheduler::OnStopTaskCallback& on_stop) { |
| 260 | RunUpdateTask(user_task); |
| 261 | } |
| 262 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 263 | TEST_F(ComponentUpdaterTest, AddObserver) { |
| 264 | MockServiceObserver observer; |
| 265 | EXPECT_CALL(update_client(), AddObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 266 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 267 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 268 | component_updater().AddObserver(&observer); |
| 269 | } |
| 270 | |
| 271 | TEST_F(ComponentUpdaterTest, RemoveObserver) { |
| 272 | MockServiceObserver observer; |
| 273 | EXPECT_CALL(update_client(), RemoveObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 274 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 275 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 276 | component_updater().RemoveObserver(&observer); |
| 277 | } |
| 278 | |
| 279 | // Tests that UpdateClient::Update is called by the timer loop when |
| 280 | // components are registered, and the component update starts. |
| 281 | // Also tests that Uninstall is called when a component is unregistered. |
| 282 | TEST_F(ComponentUpdaterTest, RegisterComponent) { |
| 283 | class LoopHandler { |
| 284 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 285 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 286 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 287 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 288 | void OnUpdate(const std::vector<std::string>& ids) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 289 | static int cnt = 0; |
| 290 | ++cnt; |
| 291 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 292 | std::move(quit_closure_).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | private: |
| 296 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 297 | base::OnceClosure quit_closure_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 298 | }; |
| 299 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 300 | base::HistogramTester ht; |
| 301 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 302 | scoped_refptr<MockInstaller> installer = |
| 303 | base::MakeRefCounted<MockInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 304 | EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true)); |
| 305 | |
| 306 | using update_client::jebg_hash; |
| 307 | using update_client::abag_hash; |
| 308 | |
| 309 | const std::string id1 = "abagagagagagagagagagagagagagagag"; |
| 310 | const std::string id2 = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 311 | std::vector<std::string> ids; |
| 312 | ids.push_back(id1); |
| 313 | ids.push_back(id2); |
| 314 | |
| 315 | CrxComponent crx_component1; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 316 | crx_component1.app_id = id1; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 317 | crx_component1.pk_hash.assign(abag_hash, abag_hash + base::size(abag_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 318 | crx_component1.version = base::Version("1.0"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 319 | crx_component1.installer = installer; |
| 320 | |
| 321 | CrxComponent crx_component2; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 322 | crx_component2.app_id = id2; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 323 | crx_component2.pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 324 | crx_component2.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 325 | crx_component2.installer = installer; |
| 326 | |
| 327 | // Quit after two update checks have fired. |
| 328 | LoopHandler loop_handler(2, quit_closure()); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 329 | EXPECT_CALL(update_client(), DoUpdate(ids)) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 330 | .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 331 | |
| 332 | EXPECT_CALL(update_client(), IsUpdating(id1)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 333 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 334 | EXPECT_CALL(scheduler(), Schedule(_, _, _, _)).Times(1); |
| 335 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 336 | |
| 337 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component1)); |
| 338 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component2)); |
| 339 | |
| 340 | RunThreads(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 341 | EXPECT_TRUE(component_updater().UnregisterComponent(id1)); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 342 | |
| 343 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 1, 2); |
| 344 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); |
| 345 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // Tests that on-demand updates invoke UpdateClient::Install. |
| 349 | TEST_F(ComponentUpdaterTest, OnDemandUpdate) { |
| 350 | class LoopHandler { |
| 351 | public: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 352 | explicit LoopHandler(int max_cnt) : max_cnt_(max_cnt) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 353 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 354 | void OnInstall(const std::string& ids) { |
| 355 | ++cnt_; |
| 356 | if (cnt_ >= max_cnt_) { |
| 357 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 358 | FROM_HERE, |
| 359 | base::BindOnce(&LoopHandler::Quit, base::Unretained(this))); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void OnUpdate(const std::vector<std::string>& ids) { |
| 364 | ++cnt_; |
| 365 | if (cnt_ >= max_cnt_) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 366 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 367 | FROM_HERE, |
| 368 | base::BindOnce(&LoopHandler::Quit, base::Unretained(this))); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 369 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | private: |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 373 | void Quit() { base::RunLoop::QuitCurrentWhenIdleDeprecated(); } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 374 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 375 | int cnt_ = 0; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 376 | const int max_cnt_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 377 | }; |
| 378 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 379 | base::HistogramTester ht; |
| 380 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 381 | // Don't run periodic update task. |
| 382 | ON_CALL(scheduler(), Schedule(_, _, _, _)).WillByDefault(Return()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 383 | |
| 384 | auto& cus = component_updater(); |
| 385 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 386 | // Tests calling OnDemand for an unregistered component. This call results in |
| 387 | // an error, which is recorded by the OnDemandTester instance. Since the |
| 388 | // component was not registered, the call is ignored for UMA metrics. |
| 389 | OnDemandTester ondemand_tester_component_not_registered; |
| 390 | ondemand_tester_component_not_registered.OnDemand( |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 391 | &cus, "ihfokbkgjpifnbbojhneepfflplebdkc", |
| 392 | OnDemandUpdater::Priority::FOREGROUND); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 393 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 394 | // Register two components, then call |OnDemand| for each component, with |
| 395 | // foreground and background priorities. Expect calls to |Schedule| because |
| 396 | // components have registered, calls to |Install| and |Update| corresponding |
| 397 | // to each |OnDemand| invocation, and calls to |Stop| when the mocks are |
| 398 | // torn down. |
| 399 | LoopHandler loop_handler(2); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 400 | EXPECT_CALL(scheduler(), Schedule(_, _, _, _)).Times(1); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 401 | EXPECT_CALL(update_client(), DoInstall("jebgalgnebhfojomionfpkfelancnnkf")) |
| 402 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
| 403 | EXPECT_CALL( |
| 404 | update_client(), |
| 405 | DoUpdate(std::vector<std::string>({"abagagagagagagagagagagagagagagag"}))) |
| 406 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 407 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 408 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 409 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 410 | { |
| 411 | using update_client::jebg_hash; |
| 412 | CrxComponent crx_component; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 413 | crx_component.app_id = "jebgalgnebhfojomionfpkfelancnnkf"; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 414 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
| 415 | crx_component.version = base::Version("0.9"); |
| 416 | crx_component.installer = base::MakeRefCounted<MockInstaller>(); |
| 417 | EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
| 418 | } |
| 419 | { |
| 420 | using update_client::abag_hash; |
| 421 | CrxComponent crx_component; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 422 | crx_component.app_id = "abagagagagagagagagagagagagagagag"; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 423 | crx_component.pk_hash.assign(abag_hash, abag_hash + base::size(abag_hash)); |
| 424 | crx_component.version = base::Version("0.9"); |
| 425 | crx_component.installer = base::MakeRefCounted<MockInstaller>(); |
| 426 | EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
| 427 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 428 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 429 | OnDemandTester ondemand_tester; |
| 430 | ondemand_tester.OnDemand(&cus, "jebgalgnebhfojomionfpkfelancnnkf", |
| 431 | OnDemandUpdater::Priority::FOREGROUND); |
| 432 | ondemand_tester.OnDemand(&cus, "abagagagagagagagagagagagagagagag", |
| 433 | OnDemandUpdater::Priority::BACKGROUND); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 434 | base::RunLoop().Run(); |
| 435 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 436 | EXPECT_EQ(update_client::Error::INVALID_ARGUMENT, |
| 437 | ondemand_tester_component_not_registered.error()); |
| 438 | EXPECT_EQ(update_client::Error::NONE, ondemand_tester.error()); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 439 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 440 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 2); |
| 441 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); |
| 442 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Tests that throttling an update invokes UpdateClient::Install. |
| 446 | TEST_F(ComponentUpdaterTest, MaybeThrottle) { |
| 447 | class LoopHandler { |
| 448 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 449 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 450 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 451 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 452 | void OnInstall(const std::string& ids) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 453 | static int cnt = 0; |
| 454 | ++cnt; |
| 455 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 456 | std::move(quit_closure_).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | private: |
| 460 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 461 | base::OnceClosure quit_closure_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 462 | }; |
| 463 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 464 | base::HistogramTester ht; |
| 465 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 466 | // Don't run periodic update task. |
| 467 | ON_CALL(scheduler(), Schedule(_, _, _, _)).WillByDefault(Return()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 468 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 469 | scoped_refptr<MockInstaller> installer = |
| 470 | base::MakeRefCounted<MockInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 471 | |
| 472 | using update_client::jebg_hash; |
| 473 | CrxComponent crx_component; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 474 | crx_component.app_id = "jebgalgnebhfojomionfpkfelancnnkf"; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 475 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 476 | crx_component.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 477 | crx_component.installer = installer; |
| 478 | |
| 479 | LoopHandler loop_handler(1, quit_closure()); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 480 | EXPECT_CALL(update_client(), DoInstall("jebgalgnebhfojomionfpkfelancnnkf")) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 481 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 482 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 483 | EXPECT_CALL(scheduler(), Schedule(_, _, _, _)).Times(1); |
| 484 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 485 | |
| 486 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component)); |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 487 | component_updater().MaybeThrottle("jebgalgnebhfojomionfpkfelancnnkf", |
| 488 | base::BindOnce([]() {})); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 489 | |
| 490 | RunThreads(); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 491 | |
| 492 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 493 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 494 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | } // namespace component_updater |