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