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