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 | static void ReadyCallback() {} |
| 156 | |
| 157 | protected: |
| 158 | void RunThreads(); |
| 159 | |
| 160 | private: |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 161 | void RunUpdateTask(const UpdateScheduler::UserTask& user_task); |
| 162 | void Schedule(const base::TimeDelta& initial_delay, |
| 163 | const base::TimeDelta& delay, |
| 164 | const UpdateScheduler::UserTask& user_task, |
| 165 | const UpdateScheduler::OnStopTaskCallback& on_stop); |
| 166 | |
Gabriel Charette | dfa3604 | 2019-08-19 17:30:11 | [diff] [blame] | 167 | base::test::TaskEnvironment task_environment_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 168 | base::RunLoop runloop_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 169 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 170 | scoped_refptr<TestConfigurator> config_ = |
| 171 | base::MakeRefCounted<TestConfigurator>(); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 172 | MockUpdateScheduler* scheduler_; |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 173 | scoped_refptr<MockUpdateClient> update_client_ = |
| 174 | base::MakeRefCounted<MockUpdateClient>(); |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 175 | std::unique_ptr<ComponentUpdateService> component_updater_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 176 | |
| 177 | DISALLOW_COPY_AND_ASSIGN(ComponentUpdaterTest); |
| 178 | }; |
| 179 | |
| 180 | class OnDemandTester { |
| 181 | public: |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 182 | void OnDemand(ComponentUpdateService* cus, |
| 183 | const std::string& id, |
| 184 | OnDemandUpdater::Priority priority); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 185 | update_client::Error error() const { return error_; } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 186 | |
| 187 | private: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 188 | void OnDemandComplete(update_client::Error error); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 189 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 190 | update_client::Error error_ = update_client::Error::NONE; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 191 | }; |
| 192 | |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame^] | 193 | MockInstaller::MockInstaller() = default; |
| 194 | MockInstaller::~MockInstaller() = default; |
| 195 | MockUpdateClient::MockUpdateClient() = default; |
| 196 | MockUpdateClient::~MockUpdateClient() = default; |
| 197 | MockServiceObserver::MockServiceObserver() = default; |
| 198 | MockServiceObserver::~MockServiceObserver() = default; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 199 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 200 | void OnDemandTester::OnDemand(ComponentUpdateService* cus, |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 201 | const std::string& id, |
| 202 | OnDemandUpdater::Priority priority) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 203 | cus->GetOnDemandUpdater().OnDemandUpdate( |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 204 | id, priority, |
| 205 | base::BindOnce(&OnDemandTester::OnDemandComplete, |
| 206 | base::Unretained(this))); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 207 | } |
| 208 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 209 | void OnDemandTester::OnDemandComplete(update_client::Error error) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 210 | error_ = error; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 211 | } |
| 212 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 213 | std::unique_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 214 | scoped_refptr<Configurator> config) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 215 | DCHECK(config); |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 216 | return std::make_unique<CrxUpdateService>( |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 217 | config, std::make_unique<MockUpdateScheduler>(), |
| 218 | base::MakeRefCounted<MockUpdateClient>()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 219 | } |
| 220 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 221 | ComponentUpdaterTest::ComponentUpdaterTest() { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 222 | EXPECT_CALL(update_client(), AddObserver(_)).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 223 | auto scheduler = std::make_unique<MockUpdateScheduler>(); |
| 224 | scheduler_ = scheduler.get(); |
| 225 | ON_CALL(*scheduler_, Schedule(_, _, _, _)) |
| 226 | .WillByDefault(Invoke(this, &ComponentUpdaterTest::Schedule)); |
| 227 | component_updater_ = std::make_unique<CrxUpdateService>( |
| 228 | config_, std::move(scheduler), update_client_); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | ComponentUpdaterTest::~ComponentUpdaterTest() { |
| 232 | EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 233 | component_updater_.reset(); |
| 234 | } |
| 235 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 236 | void ComponentUpdaterTest::RunThreads() { |
| 237 | runloop_.Run(); |
| 238 | } |
| 239 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 240 | void ComponentUpdaterTest::RunUpdateTask( |
| 241 | const UpdateScheduler::UserTask& user_task) { |
Gabriel Charette | dfa3604 | 2019-08-19 17:30:11 | [diff] [blame] | 242 | task_environment_.GetMainThreadTaskRunner()->PostTask( |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 243 | FROM_HERE, base::BindRepeating( |
| 244 | [](const UpdateScheduler::UserTask& user_task, |
| 245 | ComponentUpdaterTest* test) { |
| 246 | user_task.Run(base::BindOnce( |
| 247 | [](const UpdateScheduler::UserTask& user_task, |
| 248 | ComponentUpdaterTest* test) { |
| 249 | test->RunUpdateTask(user_task); |
| 250 | }, |
| 251 | user_task, base::Unretained(test))); |
| 252 | }, |
| 253 | user_task, base::Unretained(this))); |
| 254 | } |
| 255 | |
| 256 | void ComponentUpdaterTest::Schedule( |
| 257 | const base::TimeDelta& initial_delay, |
| 258 | const base::TimeDelta& delay, |
| 259 | const UpdateScheduler::UserTask& user_task, |
| 260 | const UpdateScheduler::OnStopTaskCallback& on_stop) { |
| 261 | RunUpdateTask(user_task); |
| 262 | } |
| 263 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 264 | TEST_F(ComponentUpdaterTest, AddObserver) { |
| 265 | MockServiceObserver observer; |
| 266 | EXPECT_CALL(update_client(), AddObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 267 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 268 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 269 | component_updater().AddObserver(&observer); |
| 270 | } |
| 271 | |
| 272 | TEST_F(ComponentUpdaterTest, RemoveObserver) { |
| 273 | MockServiceObserver observer; |
| 274 | EXPECT_CALL(update_client(), RemoveObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 275 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 276 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 277 | component_updater().RemoveObserver(&observer); |
| 278 | } |
| 279 | |
| 280 | // Tests that UpdateClient::Update is called by the timer loop when |
| 281 | // components are registered, and the component update starts. |
| 282 | // Also tests that Uninstall is called when a component is unregistered. |
| 283 | TEST_F(ComponentUpdaterTest, RegisterComponent) { |
| 284 | class LoopHandler { |
| 285 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 286 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 287 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 288 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 289 | void OnUpdate(const std::vector<std::string>& ids) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 290 | static int cnt = 0; |
| 291 | ++cnt; |
| 292 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 293 | std::move(quit_closure_).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | private: |
| 297 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 298 | base::OnceClosure quit_closure_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 299 | }; |
| 300 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 301 | base::HistogramTester ht; |
| 302 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 303 | scoped_refptr<MockInstaller> installer = |
| 304 | base::MakeRefCounted<MockInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 305 | EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true)); |
| 306 | |
| 307 | using update_client::jebg_hash; |
| 308 | using update_client::abag_hash; |
| 309 | |
| 310 | const std::string id1 = "abagagagagagagagagagagagagagagag"; |
| 311 | const std::string id2 = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 312 | std::vector<std::string> ids; |
| 313 | ids.push_back(id1); |
| 314 | ids.push_back(id2); |
| 315 | |
| 316 | CrxComponent crx_component1; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 317 | crx_component1.app_id = id1; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 318 | crx_component1.pk_hash.assign(abag_hash, abag_hash + base::size(abag_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 319 | crx_component1.version = base::Version("1.0"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 320 | crx_component1.installer = installer; |
| 321 | |
| 322 | CrxComponent crx_component2; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 323 | crx_component2.app_id = id2; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 324 | crx_component2.pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 325 | crx_component2.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 326 | crx_component2.installer = installer; |
| 327 | |
| 328 | // Quit after two update checks have fired. |
| 329 | LoopHandler loop_handler(2, quit_closure()); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 330 | EXPECT_CALL(update_client(), DoUpdate(ids)) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 331 | .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 332 | |
| 333 | EXPECT_CALL(update_client(), IsUpdating(id1)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 334 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 335 | EXPECT_CALL(scheduler(), Schedule(_, _, _, _)).Times(1); |
| 336 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 337 | |
| 338 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component1)); |
| 339 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component2)); |
| 340 | |
| 341 | RunThreads(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 342 | EXPECT_TRUE(component_updater().UnregisterComponent(id1)); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 343 | |
| 344 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 1, 2); |
| 345 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); |
| 346 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // Tests that on-demand updates invoke UpdateClient::Install. |
| 350 | TEST_F(ComponentUpdaterTest, OnDemandUpdate) { |
| 351 | class LoopHandler { |
| 352 | public: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 353 | explicit LoopHandler(int max_cnt) : max_cnt_(max_cnt) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 354 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 355 | void OnInstall(const std::string& ids) { |
| 356 | ++cnt_; |
| 357 | if (cnt_ >= max_cnt_) { |
| 358 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 359 | FROM_HERE, |
| 360 | base::BindOnce(&LoopHandler::Quit, base::Unretained(this))); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void OnUpdate(const std::vector<std::string>& ids) { |
| 365 | ++cnt_; |
| 366 | if (cnt_ >= max_cnt_) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 367 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 368 | FROM_HERE, |
| 369 | base::BindOnce(&LoopHandler::Quit, base::Unretained(this))); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 370 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | private: |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 374 | void Quit() { base::RunLoop::QuitCurrentWhenIdleDeprecated(); } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 375 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 376 | int cnt_ = 0; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 377 | const int max_cnt_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 378 | }; |
| 379 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 380 | base::HistogramTester ht; |
| 381 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 382 | // Don't run periodic update task. |
| 383 | ON_CALL(scheduler(), Schedule(_, _, _, _)).WillByDefault(Return()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 384 | |
| 385 | auto& cus = component_updater(); |
| 386 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 387 | // Tests calling OnDemand for an unregistered component. This call results in |
| 388 | // an error, which is recorded by the OnDemandTester instance. Since the |
| 389 | // component was not registered, the call is ignored for UMA metrics. |
| 390 | OnDemandTester ondemand_tester_component_not_registered; |
| 391 | ondemand_tester_component_not_registered.OnDemand( |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 392 | &cus, "ihfokbkgjpifnbbojhneepfflplebdkc", |
| 393 | OnDemandUpdater::Priority::FOREGROUND); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 394 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 395 | // Register two components, then call |OnDemand| for each component, with |
| 396 | // foreground and background priorities. Expect calls to |Schedule| because |
| 397 | // components have registered, calls to |Install| and |Update| corresponding |
| 398 | // to each |OnDemand| invocation, and calls to |Stop| when the mocks are |
| 399 | // torn down. |
| 400 | LoopHandler loop_handler(2); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 401 | EXPECT_CALL(scheduler(), Schedule(_, _, _, _)).Times(1); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 402 | EXPECT_CALL(update_client(), DoInstall("jebgalgnebhfojomionfpkfelancnnkf")) |
| 403 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
| 404 | EXPECT_CALL( |
| 405 | update_client(), |
| 406 | DoUpdate(std::vector<std::string>({"abagagagagagagagagagagagagagagag"}))) |
| 407 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 408 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 409 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 410 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 411 | { |
| 412 | using update_client::jebg_hash; |
| 413 | CrxComponent crx_component; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 414 | crx_component.app_id = "jebgalgnebhfojomionfpkfelancnnkf"; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 415 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
| 416 | crx_component.version = base::Version("0.9"); |
| 417 | crx_component.installer = base::MakeRefCounted<MockInstaller>(); |
| 418 | EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
| 419 | } |
| 420 | { |
| 421 | using update_client::abag_hash; |
| 422 | CrxComponent crx_component; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 423 | crx_component.app_id = "abagagagagagagagagagagagagagagag"; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 424 | crx_component.pk_hash.assign(abag_hash, abag_hash + base::size(abag_hash)); |
| 425 | crx_component.version = base::Version("0.9"); |
| 426 | crx_component.installer = base::MakeRefCounted<MockInstaller>(); |
| 427 | EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
| 428 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 429 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 430 | OnDemandTester ondemand_tester; |
| 431 | ondemand_tester.OnDemand(&cus, "jebgalgnebhfojomionfpkfelancnnkf", |
| 432 | OnDemandUpdater::Priority::FOREGROUND); |
| 433 | ondemand_tester.OnDemand(&cus, "abagagagagagagagagagagagagagagag", |
| 434 | OnDemandUpdater::Priority::BACKGROUND); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 435 | base::RunLoop().Run(); |
| 436 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 437 | EXPECT_EQ(update_client::Error::INVALID_ARGUMENT, |
| 438 | ondemand_tester_component_not_registered.error()); |
| 439 | EXPECT_EQ(update_client::Error::NONE, ondemand_tester.error()); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 440 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 441 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 2); |
| 442 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); |
| 443 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // Tests that throttling an update invokes UpdateClient::Install. |
| 447 | TEST_F(ComponentUpdaterTest, MaybeThrottle) { |
| 448 | class LoopHandler { |
| 449 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 450 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 451 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 452 | |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 453 | void OnInstall(const std::string& ids) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 454 | static int cnt = 0; |
| 455 | ++cnt; |
| 456 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 457 | std::move(quit_closure_).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | private: |
| 461 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 462 | base::OnceClosure quit_closure_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 463 | }; |
| 464 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 465 | base::HistogramTester ht; |
| 466 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 467 | // Don't run periodic update task. |
| 468 | ON_CALL(scheduler(), Schedule(_, _, _, _)).WillByDefault(Return()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 469 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 470 | scoped_refptr<MockInstaller> installer = |
| 471 | base::MakeRefCounted<MockInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 472 | |
| 473 | using update_client::jebg_hash; |
| 474 | CrxComponent crx_component; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 475 | crx_component.app_id = "jebgalgnebhfojomionfpkfelancnnkf"; |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 476 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 477 | crx_component.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 478 | crx_component.installer = installer; |
| 479 | |
| 480 | LoopHandler loop_handler(1, quit_closure()); |
Sorin Jianu | d20ed853 | 2018-06-28 17:24:31 | [diff] [blame] | 481 | EXPECT_CALL(update_client(), DoInstall("jebgalgnebhfojomionfpkfelancnnkf")) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 482 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 483 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 484 | EXPECT_CALL(scheduler(), Schedule(_, _, _, _)).Times(1); |
| 485 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 486 | |
| 487 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component)); |
| 488 | component_updater().MaybeThrottle( |
| 489 | "jebgalgnebhfojomionfpkfelancnnkf", |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 490 | base::BindOnce(&ComponentUpdaterTest::ReadyCallback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 491 | |
| 492 | RunThreads(); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 493 | |
| 494 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 495 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 496 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | } // namespace component_updater |