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" |
| 14 | #include "base/bind_helpers.h" |
| 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" |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 20 | #include "base/task_scheduler/post_task.h" |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 21 | #include "base/test/histogram_tester.h" |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 22 | #include "base/test/scoped_task_environment.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 23 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 24 | #include "base/values.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 25 | #include "components/component_updater/component_updater_service_internal.h" |
| 26 | #include "components/update_client/test_configurator.h" |
| 27 | #include "components/update_client/test_installer.h" |
| 28 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 29 | #include "components/update_client/update_client_errors.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 30 | #include "testing/gmock/include/gmock/gmock.h" |
| 31 | #include "testing/gtest/include/gtest/gtest.h" |
| 32 | |
| 33 | using Configurator = update_client::Configurator; |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 34 | using Result = update_client::CrxInstaller::Result; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 35 | using TestConfigurator = update_client::TestConfigurator; |
| 36 | using UpdateClient = update_client::UpdateClient; |
| 37 | |
| 38 | using ::testing::_; |
| 39 | using ::testing::AnyNumber; |
| 40 | using ::testing::Invoke; |
| 41 | using ::testing::Mock; |
| 42 | using ::testing::Return; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 43 | using ::testing::Unused; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 44 | |
| 45 | namespace component_updater { |
| 46 | |
| 47 | class MockInstaller : public CrxInstaller { |
| 48 | public: |
| 49 | MockInstaller(); |
| 50 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 51 | // gMock does not support mocking functions with parameters which have |
| 52 | // move semantics. This function is a shim to work around it. |
| 53 | void Install(const base::FilePath& unpack_path, |
| 54 | const std::string& public_key, |
| 55 | update_client::CrxInstaller::Callback callback) { |
| 56 | DoInstall(unpack_path, callback); |
| 57 | } |
| 58 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 59 | MOCK_METHOD1(OnUpdateError, void(int error)); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 60 | MOCK_METHOD2(DoInstall, |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 61 | void(const base::FilePath& unpack_path, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 62 | const update_client::CrxInstaller::Callback& callback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 63 | MOCK_METHOD2(GetInstalledFile, |
| 64 | bool(const std::string& file, base::FilePath* installed_file)); |
| 65 | MOCK_METHOD0(Uninstall, bool()); |
| 66 | |
| 67 | private: |
| 68 | ~MockInstaller() override; |
| 69 | }; |
| 70 | |
| 71 | class MockUpdateClient : public UpdateClient { |
| 72 | public: |
| 73 | MockUpdateClient(); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 74 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 75 | // gMock does not support mocking functions with parameters which have |
| 76 | // move semantics. This function is a shim to work around it. |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 77 | void Install(const std::string& id, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 78 | CrxDataCallback crx_data_callback, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 79 | Callback callback) override { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 80 | DoInstall(id, std::move(crx_data_callback)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 81 | std::move(callback).Run(update_client::Error::NONE); |
| 82 | } |
| 83 | |
| 84 | void Update(const std::vector<std::string>& ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 85 | CrxDataCallback crx_data_callback, |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame^] | 86 | bool is_foreground, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 87 | Callback callback) override { |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame^] | 88 | // All update calls initiated by the component update service are |
| 89 | // automatically triggered as background updates without user intervention. |
| 90 | EXPECT_FALSE(is_foreground); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 91 | DoUpdate(ids, std::move(crx_data_callback)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 92 | std::move(callback).Run(update_client::Error::NONE); |
| 93 | } |
| 94 | |
| 95 | void SendUninstallPing(const std::string& id, |
| 96 | const base::Version& version, |
| 97 | int reason, |
| 98 | Callback callback) { |
| 99 | DoSendUninstallPing(id, version, reason); |
| 100 | std::move(callback).Run(update_client::Error::NONE); |
| 101 | } |
| 102 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 103 | MOCK_METHOD1(AddObserver, void(Observer* observer)); |
| 104 | MOCK_METHOD1(RemoveObserver, void(Observer* observer)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 105 | MOCK_METHOD2(DoInstall, |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 106 | void(const std::string& id, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 107 | const CrxDataCallback& crx_data_callback)); |
| 108 | MOCK_METHOD2(DoUpdate, |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 109 | void(const std::vector<std::string>& ids, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 110 | const CrxDataCallback& crx_data_callback)); |
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 | |
| 132 | class ComponentUpdaterTest : public testing::Test { |
| 133 | public: |
| 134 | ComponentUpdaterTest(); |
| 135 | ~ComponentUpdaterTest() override; |
| 136 | |
| 137 | void SetUp() override; |
| 138 | |
| 139 | void TearDown() override; |
| 140 | |
| 141 | // Makes the full path to a component updater test file. |
| 142 | const base::FilePath test_file(const char* file); |
| 143 | |
| 144 | MockUpdateClient& update_client() { return *update_client_; } |
| 145 | ComponentUpdateService& component_updater() { return *component_updater_; } |
| 146 | scoped_refptr<TestConfigurator> configurator() const { return config_; } |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 147 | base::OnceClosure quit_closure() { return runloop_.QuitClosure(); } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 148 | static void ReadyCallback() {} |
| 149 | |
| 150 | protected: |
| 151 | void RunThreads(); |
| 152 | |
| 153 | private: |
fdoray | dc4d994 | 2017-05-12 20:13:50 | [diff] [blame] | 154 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 155 | base::RunLoop runloop_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 156 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 157 | scoped_refptr<TestConfigurator> config_ = |
| 158 | base::MakeRefCounted<TestConfigurator>(); |
| 159 | scoped_refptr<MockUpdateClient> update_client_ = |
| 160 | base::MakeRefCounted<MockUpdateClient>(); |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 161 | std::unique_ptr<ComponentUpdateService> component_updater_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(ComponentUpdaterTest); |
| 164 | }; |
| 165 | |
| 166 | class OnDemandTester { |
| 167 | public: |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 168 | void OnDemand(ComponentUpdateService* cus, const std::string& id); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 169 | update_client::Error error() const { return error_; } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 170 | |
| 171 | private: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 172 | void OnDemandComplete(update_client::Error error); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 173 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 174 | update_client::Error error_ = update_client::Error::NONE; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | MockInstaller::MockInstaller() { |
| 178 | } |
| 179 | |
| 180 | MockInstaller::~MockInstaller() { |
| 181 | } |
| 182 | |
| 183 | MockUpdateClient::MockUpdateClient() { |
| 184 | } |
| 185 | |
| 186 | MockUpdateClient::~MockUpdateClient() { |
| 187 | } |
| 188 | |
| 189 | MockServiceObserver::MockServiceObserver() { |
| 190 | } |
| 191 | |
| 192 | MockServiceObserver::~MockServiceObserver() { |
| 193 | } |
| 194 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 195 | void OnDemandTester::OnDemand(ComponentUpdateService* cus, |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 196 | const std::string& id) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 197 | cus->GetOnDemandUpdater().OnDemandUpdate( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 198 | id, base::BindOnce(&OnDemandTester::OnDemandComplete, |
| 199 | base::Unretained(this))); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 200 | } |
| 201 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 202 | void OnDemandTester::OnDemandComplete(update_client::Error error) { |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 203 | error_ = error; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 204 | } |
| 205 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 206 | std::unique_ptr<ComponentUpdateService> TestComponentUpdateServiceFactory( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 207 | scoped_refptr<Configurator> config) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 208 | DCHECK(config); |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 209 | return std::make_unique<CrxUpdateService>( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 210 | config, base::MakeRefCounted<MockUpdateClient>()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 211 | } |
| 212 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 213 | ComponentUpdaterTest::ComponentUpdaterTest() { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 214 | EXPECT_CALL(update_client(), AddObserver(_)).Times(1); |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 215 | component_updater_ = |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 216 | std::make_unique<CrxUpdateService>(config_, update_client_); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | ComponentUpdaterTest::~ComponentUpdaterTest() { |
| 220 | EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 221 | component_updater_.reset(); |
| 222 | } |
| 223 | |
| 224 | void ComponentUpdaterTest::SetUp() { |
| 225 | } |
| 226 | |
| 227 | void ComponentUpdaterTest::TearDown() { |
| 228 | } |
| 229 | |
| 230 | void ComponentUpdaterTest::RunThreads() { |
| 231 | runloop_.Run(); |
| 232 | } |
| 233 | |
| 234 | TEST_F(ComponentUpdaterTest, AddObserver) { |
| 235 | MockServiceObserver observer; |
| 236 | EXPECT_CALL(update_client(), AddObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 237 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 238 | component_updater().AddObserver(&observer); |
| 239 | } |
| 240 | |
| 241 | TEST_F(ComponentUpdaterTest, RemoveObserver) { |
| 242 | MockServiceObserver observer; |
| 243 | EXPECT_CALL(update_client(), RemoveObserver(&observer)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 244 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 245 | component_updater().RemoveObserver(&observer); |
| 246 | } |
| 247 | |
| 248 | // Tests that UpdateClient::Update is called by the timer loop when |
| 249 | // components are registered, and the component update starts. |
| 250 | // Also tests that Uninstall is called when a component is unregistered. |
| 251 | TEST_F(ComponentUpdaterTest, RegisterComponent) { |
| 252 | class LoopHandler { |
| 253 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 254 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 255 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 256 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 257 | void OnUpdate(const std::vector<std::string>& ids, Unused) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 258 | static int cnt = 0; |
| 259 | ++cnt; |
| 260 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 261 | std::move(quit_closure_).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | private: |
| 265 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 266 | base::OnceClosure quit_closure_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 267 | }; |
| 268 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 269 | base::HistogramTester ht; |
| 270 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 271 | scoped_refptr<MockInstaller> installer = |
| 272 | base::MakeRefCounted<MockInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 273 | EXPECT_CALL(*installer, Uninstall()).WillOnce(Return(true)); |
| 274 | |
| 275 | using update_client::jebg_hash; |
| 276 | using update_client::abag_hash; |
| 277 | |
| 278 | const std::string id1 = "abagagagagagagagagagagagagagagag"; |
| 279 | const std::string id2 = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 280 | std::vector<std::string> ids; |
| 281 | ids.push_back(id1); |
| 282 | ids.push_back(id2); |
| 283 | |
| 284 | CrxComponent crx_component1; |
| 285 | crx_component1.pk_hash.assign(abag_hash, abag_hash + arraysize(abag_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 286 | crx_component1.version = base::Version("1.0"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 287 | crx_component1.installer = installer; |
| 288 | |
| 289 | CrxComponent crx_component2; |
| 290 | crx_component2.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 291 | crx_component2.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 292 | crx_component2.installer = installer; |
| 293 | |
| 294 | // Quit after two update checks have fired. |
| 295 | LoopHandler loop_handler(2, quit_closure()); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 296 | EXPECT_CALL(update_client(), DoUpdate(ids, _)) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 297 | .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 298 | |
| 299 | EXPECT_CALL(update_client(), IsUpdating(id1)).Times(1); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 300 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 301 | |
| 302 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component1)); |
| 303 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component2)); |
| 304 | |
| 305 | RunThreads(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 306 | EXPECT_TRUE(component_updater().UnregisterComponent(id1)); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 307 | |
| 308 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 1, 2); |
| 309 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 2); |
| 310 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 2); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // Tests that on-demand updates invoke UpdateClient::Install. |
| 314 | TEST_F(ComponentUpdaterTest, OnDemandUpdate) { |
| 315 | class LoopHandler { |
| 316 | public: |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 317 | explicit LoopHandler(int max_cnt) : max_cnt_(max_cnt) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 318 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 319 | void OnInstall(const std::string& ids, Unused) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 320 | static int cnt = 0; |
| 321 | ++cnt; |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 322 | if (cnt >= max_cnt_) { |
| 323 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 324 | FROM_HERE, |
| 325 | base::BindOnce(&LoopHandler::Quit, base::Unretained(this))); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 326 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | private: |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 330 | void Quit() { base::RunLoop::QuitCurrentWhenIdleDeprecated(); } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 331 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 332 | const int max_cnt_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 333 | }; |
| 334 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 335 | base::HistogramTester ht; |
| 336 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 337 | auto config = configurator(); |
| 338 | config->SetInitialDelay(3600); |
| 339 | |
| 340 | auto& cus = component_updater(); |
| 341 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 342 | // Tests calling OnDemand for an unregistered component. This call results in |
| 343 | // an error, which is recorded by the OnDemandTester instance. Since the |
| 344 | // component was not registered, the call is ignored for UMA metrics. |
| 345 | OnDemandTester ondemand_tester_component_not_registered; |
| 346 | ondemand_tester_component_not_registered.OnDemand( |
| 347 | &cus, "ihfokbkgjpifnbbojhneepfflplebdkc"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 348 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 349 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 350 | |
| 351 | using update_client::jebg_hash; |
| 352 | CrxComponent crx_component; |
| 353 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 354 | crx_component.version = base::Version("0.9"); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 355 | crx_component.installer = base::MakeRefCounted<MockInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 356 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 357 | LoopHandler loop_handler(1); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 358 | EXPECT_CALL(update_client(), DoInstall("jebgalgnebhfojomionfpkfelancnnkf", _)) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 359 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 360 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 361 | |
| 362 | EXPECT_TRUE(cus.RegisterComponent(crx_component)); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 363 | OnDemandTester ondemand_tester; |
| 364 | ondemand_tester.OnDemand(&cus, id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 365 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 366 | base::RunLoop().Run(); |
| 367 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 368 | EXPECT_EQ(update_client::Error::INVALID_ARGUMENT, |
| 369 | ondemand_tester_component_not_registered.error()); |
| 370 | EXPECT_EQ(update_client::Error::NONE, ondemand_tester.error()); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 371 | |
| 372 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 373 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 374 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Tests that throttling an update invokes UpdateClient::Install. |
| 378 | TEST_F(ComponentUpdaterTest, MaybeThrottle) { |
| 379 | class LoopHandler { |
| 380 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 381 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 382 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 383 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 384 | void OnInstall(const std::string& ids, Unused) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 385 | static int cnt = 0; |
| 386 | ++cnt; |
| 387 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 388 | std::move(quit_closure_).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | private: |
| 392 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 393 | base::OnceClosure quit_closure_; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 394 | }; |
| 395 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 396 | base::HistogramTester ht; |
| 397 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 398 | auto config = configurator(); |
| 399 | config->SetInitialDelay(3600); |
| 400 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 401 | scoped_refptr<MockInstaller> installer = |
| 402 | base::MakeRefCounted<MockInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 403 | |
| 404 | using update_client::jebg_hash; |
| 405 | CrxComponent crx_component; |
| 406 | crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
pwnall | 15745b31 | 2016-08-19 21:45:29 | [diff] [blame] | 407 | crx_component.version = base::Version("0.9"); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 408 | crx_component.installer = installer; |
| 409 | |
| 410 | LoopHandler loop_handler(1, quit_closure()); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 411 | EXPECT_CALL(update_client(), DoInstall("jebgalgnebhfojomionfpkfelancnnkf", _)) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 412 | .WillOnce(Invoke(&loop_handler, &LoopHandler::OnInstall)); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 413 | EXPECT_CALL(update_client(), Stop()).Times(1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 414 | |
| 415 | EXPECT_TRUE(component_updater().RegisterComponent(crx_component)); |
| 416 | component_updater().MaybeThrottle( |
| 417 | "jebgalgnebhfojomionfpkfelancnnkf", |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 418 | base::BindOnce(&ComponentUpdaterTest::ReadyCallback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 419 | |
| 420 | RunThreads(); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 421 | |
| 422 | ht.ExpectUniqueSample("ComponentUpdater.Calls", 0, 1); |
| 423 | ht.ExpectUniqueSample("ComponentUpdater.UpdateCompleteResult", 0, 1); |
| 424 | ht.ExpectTotalCount("ComponentUpdater.UpdateCompleteTime", 1); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | } // namespace component_updater |