sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 1 | // Copyright 2016 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 | |
| 5 | #include <iterator> |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 6 | #include <memory> |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 7 | #include <string> |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 8 | #include <utility> |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 11 | #include "base/bind.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 12 | #include "base/callback.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
| 14 | #include "base/files/file_util.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 15 | #include "base/macros.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 17 | #include "base/path_service.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 18 | #include "base/run_loop.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 19 | #include "base/task/post_task.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 20 | #include "base/test/scoped_path_override.h" |
fdoray | b26583c | 2017-05-16 18:47:01 | [diff] [blame] | 21 | #include "base/test/scoped_task_environment.h" |
| 22 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 23 | #include "base/version.h" |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 24 | #include "components/component_updater/component_installer.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 25 | #include "components/component_updater/component_updater_paths.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 26 | #include "components/component_updater/component_updater_service.h" |
| 27 | #include "components/component_updater/component_updater_service_internal.h" |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame^] | 28 | #include "components/crx_file/crx_verifier.h" |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 29 | #include "components/update_client/component_unpacker.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 30 | #include "components/update_client/crx_update_item.h" |
| 31 | #include "components/update_client/test_configurator.h" |
| 32 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 33 | #include "components/update_client/update_client_errors.h" |
Jay Civelli | 29f9b94 | 2017-11-08 20:07:26 | [diff] [blame] | 34 | #include "services/service_manager/public/cpp/connector.h" |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 35 | #include "testing/gmock/include/gmock/gmock.h" |
| 36 | #include "testing/gtest/include/gtest/gtest.h" |
| 37 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 38 | using ComponentUnpacker = update_client::ComponentUnpacker; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 39 | using Configurator = update_client::Configurator; |
| 40 | using CrxUpdateItem = update_client::CrxUpdateItem; |
| 41 | using TestConfigurator = update_client::TestConfigurator; |
| 42 | using UpdateClient = update_client::UpdateClient; |
| 43 | |
| 44 | using ::testing::_; |
| 45 | using ::testing::Invoke; |
| 46 | |
| 47 | namespace component_updater { |
| 48 | |
| 49 | namespace { |
| 50 | |
| 51 | // This hash corresponds to jebgalgnebhfojomionfpkfelancnnkf.crx. |
| 52 | const uint8_t kSha256Hash[] = {0x94, 0x16, 0x0b, 0x6d, 0x41, 0x75, 0xe9, 0xec, |
| 53 | 0x8e, 0xd5, 0xfa, 0x54, 0xb0, 0xd2, 0xdd, 0xa5, |
| 54 | 0x6e, 0x05, 0x6b, 0xe8, 0x73, 0x47, 0xf6, 0xc4, |
| 55 | 0x11, 0x9f, 0xbc, 0xb3, 0x09, 0xb3, 0x5b, 0x40}; |
| 56 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 57 | constexpr base::FilePath::CharType relative_install_dir[] = |
| 58 | FILE_PATH_LITERAL("fake"); |
| 59 | |
| 60 | base::FilePath test_file(const char* file) { |
| 61 | base::FilePath path; |
Avi Drissman | f617d01 | 2018-05-02 18:48:53 | [diff] [blame] | 62 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 63 | return path.AppendASCII("components") |
| 64 | .AppendASCII("test") |
| 65 | .AppendASCII("data") |
| 66 | .AppendASCII("update_client") |
| 67 | .AppendASCII(file); |
| 68 | } |
| 69 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 70 | class MockUpdateClient : public UpdateClient { |
| 71 | public: |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 72 | MockUpdateClient() {} |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 73 | |
| 74 | void Install(const std::string& id, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 75 | CrxDataCallback crx_data_callback, |
Daniel Cheng | 6784852 | 2018-04-27 22:04:41 | [diff] [blame] | 76 | Callback callback) override { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 77 | DoInstall(id, std::move(crx_data_callback)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 78 | std::move(callback).Run(update_client::Error::NONE); |
| 79 | } |
| 80 | |
| 81 | void Update(const std::vector<std::string>& ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 82 | CrxDataCallback crx_data_callback, |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 83 | bool is_foreground, |
Daniel Cheng | 6784852 | 2018-04-27 22:04:41 | [diff] [blame] | 84 | Callback callback) override { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 85 | DoUpdate(ids, std::move(crx_data_callback)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 86 | std::move(callback).Run(update_client::Error::NONE); |
| 87 | } |
| 88 | |
| 89 | void SendUninstallPing(const std::string& id, |
| 90 | const base::Version& version, |
| 91 | int reason, |
Daniel Cheng | 6784852 | 2018-04-27 22:04:41 | [diff] [blame] | 92 | Callback callback) override { |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 93 | DoSendUninstallPing(id, version, reason); |
| 94 | std::move(callback).Run(update_client::Error::NONE); |
| 95 | } |
| 96 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 97 | MOCK_METHOD1(AddObserver, void(Observer* observer)); |
| 98 | MOCK_METHOD1(RemoveObserver, void(Observer* observer)); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 99 | MOCK_METHOD2(DoInstall, |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 100 | void(const std::string& id, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 101 | const CrxDataCallback& crx_data_callback)); |
| 102 | MOCK_METHOD2(DoUpdate, |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 103 | void(const std::vector<std::string>& ids, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 104 | const CrxDataCallback& crx_data_callback)); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 105 | MOCK_CONST_METHOD2(GetCrxUpdateState, |
| 106 | bool(const std::string& id, CrxUpdateItem* update_item)); |
| 107 | MOCK_CONST_METHOD1(IsUpdating, bool(const std::string& id)); |
| 108 | MOCK_METHOD0(Stop, void()); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 109 | MOCK_METHOD3(DoSendUninstallPing, |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 110 | void(const std::string& id, |
| 111 | const base::Version& version, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 112 | int reason)); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 113 | |
| 114 | private: |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 115 | ~MockUpdateClient() override {} |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 116 | }; |
| 117 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 118 | class MockInstallerPolicy : public ComponentInstallerPolicy { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 119 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 120 | MockInstallerPolicy() {} |
| 121 | ~MockInstallerPolicy() override {} |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 122 | |
| 123 | bool VerifyInstallation(const base::DictionaryValue& manifest, |
| 124 | const base::FilePath& dir) const override { |
| 125 | return true; |
| 126 | } |
| 127 | |
sorin | 3574ef9 | 2016-08-03 16:46:01 | [diff] [blame] | 128 | bool SupportsGroupPolicyEnabledComponentUpdates() const override { |
sorin | 098a4a7 | 2016-08-30 04:43:24 | [diff] [blame] | 129 | return true; |
sorin | 3574ef9 | 2016-08-03 16:46:01 | [diff] [blame] | 130 | } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 131 | |
| 132 | bool RequiresNetworkEncryption() const override { return true; } |
| 133 | |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 134 | update_client::CrxInstaller::Result OnCustomInstall( |
| 135 | const base::DictionaryValue& manifest, |
| 136 | const base::FilePath& install_dir) override { |
| 137 | return update_client::CrxInstaller::Result(0); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 138 | } |
| 139 | |
Xiaochu Liu | 1e306a3 | 2017-11-08 18:57:34 | [diff] [blame] | 140 | void OnCustomUninstall() override {} |
| 141 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 142 | void ComponentReady( |
| 143 | const base::Version& version, |
| 144 | const base::FilePath& install_dir, |
| 145 | std::unique_ptr<base::DictionaryValue> manifest) override {} |
| 146 | |
| 147 | base::FilePath GetRelativeInstallDir() const override { |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 148 | return base::FilePath(relative_install_dir); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void GetHash(std::vector<uint8_t>* hash) const override { GetPkHash(hash); } |
| 152 | |
| 153 | std::string GetName() const override { return "fake name"; } |
| 154 | |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 155 | update_client::InstallerAttributes GetInstallerAttributes() const override { |
| 156 | update_client::InstallerAttributes installer_attributes; |
| 157 | installer_attributes["ap"] = "fake-ap"; |
| 158 | installer_attributes["is-enterprise"] = "1"; |
| 159 | return installer_attributes; |
| 160 | } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 161 | |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 162 | std::vector<std::string> GetMimeTypes() const override { |
| 163 | return std::vector<std::string>(); |
| 164 | } |
| 165 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 166 | private: |
| 167 | static void GetPkHash(std::vector<uint8_t>* hash) { |
| 168 | hash->assign(std::begin(kSha256Hash), std::end(kSha256Hash)); |
| 169 | } |
| 170 | }; |
| 171 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 172 | class MockUpdateScheduler : public UpdateScheduler { |
| 173 | public: |
| 174 | MOCK_METHOD4(Schedule, |
| 175 | void(const base::TimeDelta& initial_delay, |
| 176 | const base::TimeDelta& delay, |
| 177 | const UserTask& user_task, |
| 178 | const OnStopTaskCallback& on_stop)); |
| 179 | MOCK_METHOD0(Stop, void()); |
| 180 | }; |
| 181 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 182 | class ComponentInstallerTest : public testing::Test { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 183 | public: |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 184 | ComponentInstallerTest(); |
| 185 | ~ComponentInstallerTest() override; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 186 | |
| 187 | MockUpdateClient& update_client() { return *update_client_; } |
| 188 | ComponentUpdateService* component_updater() { |
| 189 | return component_updater_.get(); |
| 190 | } |
| 191 | scoped_refptr<TestConfigurator> configurator() const { return config_; } |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 192 | base::OnceClosure quit_closure() { return runloop_.QuitClosure(); } |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 193 | MockUpdateScheduler& scheduler() { return *scheduler_; } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 194 | |
| 195 | protected: |
| 196 | void RunThreads(); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 197 | void Unpack(const base::FilePath& crx_path); |
| 198 | ComponentUnpacker::Result result() const { return result_; } |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 199 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 200 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
| 201 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 202 | private: |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 203 | void UnpackComplete(const ComponentUnpacker::Result& result); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 204 | void Schedule(const base::TimeDelta& initial_delay, |
| 205 | const base::TimeDelta& delay, |
| 206 | const UpdateScheduler::UserTask& user_task, |
| 207 | const UpdateScheduler::OnStopTaskCallback& on_stop); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 208 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 209 | const scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_ = |
| 210 | base::ThreadTaskRunnerHandle::Get(); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 211 | base::RunLoop runloop_; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 212 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 213 | scoped_refptr<TestConfigurator> config_ = |
| 214 | base::MakeRefCounted<TestConfigurator>(); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 215 | MockUpdateScheduler* scheduler_ = nullptr; |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 216 | scoped_refptr<MockUpdateClient> update_client_ = |
| 217 | base::MakeRefCounted<MockUpdateClient>(); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 218 | std::unique_ptr<ComponentUpdateService> component_updater_; |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 219 | ComponentUnpacker::Result result_; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 220 | }; |
| 221 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 222 | ComponentInstallerTest::ComponentInstallerTest() { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 223 | EXPECT_CALL(update_client(), AddObserver(_)).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 224 | auto scheduler = std::make_unique<MockUpdateScheduler>(); |
| 225 | scheduler_ = scheduler.get(); |
| 226 | ON_CALL(*scheduler_, Schedule(_, _, _, _)) |
| 227 | .WillByDefault(Invoke(this, &ComponentInstallerTest::Schedule)); |
| 228 | component_updater_ = std::make_unique<CrxUpdateService>( |
| 229 | config_, std::move(scheduler), update_client_); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 230 | } |
| 231 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 232 | ComponentInstallerTest::~ComponentInstallerTest() { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 233 | EXPECT_CALL(update_client(), RemoveObserver(_)).Times(1); |
| 234 | component_updater_.reset(); |
| 235 | } |
| 236 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 237 | void ComponentInstallerTest::RunThreads() { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 238 | runloop_.Run(); |
| 239 | } |
| 240 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 241 | void ComponentInstallerTest::Unpack(const base::FilePath& crx_path) { |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 242 | auto config = base::MakeRefCounted<TestConfigurator>(); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 243 | auto component_unpacker = base::MakeRefCounted<ComponentUnpacker>( |
| 244 | std::vector<uint8_t>(std::begin(kSha256Hash), std::end(kSha256Hash)), |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame^] | 245 | crx_path, nullptr, config->CreateServiceManagerConnector(), |
| 246 | crx_file::VerifierFormat::CRX2_OR_CRX3); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 247 | component_unpacker->Unpack(base::BindOnce( |
| 248 | &ComponentInstallerTest::UnpackComplete, base::Unretained(this))); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 249 | RunThreads(); |
| 250 | } |
| 251 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 252 | void ComponentInstallerTest::UnpackComplete( |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 253 | const ComponentUnpacker::Result& result) { |
| 254 | result_ = result; |
| 255 | |
| 256 | EXPECT_EQ(update_client::UnpackerError::kNone, result_.error); |
| 257 | EXPECT_EQ(0, result_.extended_error); |
| 258 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 259 | main_thread_task_runner_->PostTask(FROM_HERE, quit_closure()); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 260 | } |
| 261 | |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 262 | void ComponentInstallerTest::Schedule( |
| 263 | const base::TimeDelta& initial_delay, |
| 264 | const base::TimeDelta& delay, |
| 265 | const UpdateScheduler::UserTask& user_task, |
| 266 | const UpdateScheduler::OnStopTaskCallback& on_stop) { |
| 267 | user_task.Run(base::DoNothing()); |
| 268 | } |
| 269 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 270 | } // namespace |
| 271 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 272 | // Tests that the component metadata is propagated from the component installer |
| 273 | // and its component policy, through the instance of the CrxComponent, to the |
| 274 | // component updater service. |
| 275 | TEST_F(ComponentInstallerTest, RegisterComponent) { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 276 | class LoopHandler { |
| 277 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 278 | LoopHandler(int max_cnt, base::OnceClosure quit_closure) |
| 279 | : max_cnt_(max_cnt), quit_closure_(std::move(quit_closure)) {} |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 280 | |
| 281 | void OnUpdate(const std::vector<std::string>& ids, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 282 | const UpdateClient::CrxDataCallback& crx_data_callback) { |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 283 | static int cnt = 0; |
| 284 | ++cnt; |
| 285 | if (cnt >= max_cnt_) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 286 | std::move(quit_closure_).Run(); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | private: |
| 290 | const int max_cnt_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 291 | base::OnceClosure quit_closure_; |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 292 | }; |
| 293 | |
Sorin Jianu | 0bf4bd3f | 2017-06-01 23:42:32 | [diff] [blame] | 294 | base::ScopedPathOverride scoped_path_override(DIR_COMPONENT_USER); |
| 295 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 296 | const std::string id("jebgalgnebhfojomionfpkfelancnnkf"); |
| 297 | |
| 298 | // Quit after one update check has been fired. |
| 299 | LoopHandler loop_handler(1, quit_closure()); |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 300 | EXPECT_CALL(update_client(), DoUpdate(_, _)) |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 301 | .WillRepeatedly(Invoke(&loop_handler, &LoopHandler::OnUpdate)); |
| 302 | |
| 303 | EXPECT_CALL(update_client(), GetCrxUpdateState(id, _)).Times(1); |
| 304 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 305 | EXPECT_CALL(scheduler(), Schedule(_, _, _, _)).Times(1); |
| 306 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 307 | |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 308 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 309 | std::make_unique<MockInstallerPolicy>()); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 310 | installer->Register(component_updater(), base::OnceClosure()); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 311 | |
| 312 | RunThreads(); |
| 313 | |
| 314 | CrxUpdateItem item; |
| 315 | EXPECT_TRUE(component_updater()->GetComponentDetails(id, &item)); |
Sorin Jianu | 7390024 | 2018-08-17 01:11:53 | [diff] [blame] | 316 | ASSERT_TRUE(item.component); |
| 317 | const CrxComponent& component = *item.component; |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 318 | |
| 319 | update_client::InstallerAttributes expected_attrs; |
| 320 | expected_attrs["ap"] = "fake-ap"; |
| 321 | expected_attrs["is-enterprise"] = "1"; |
| 322 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 323 | EXPECT_EQ( |
| 324 | std::vector<uint8_t>(std::begin(kSha256Hash), std::end(kSha256Hash)), |
| 325 | component.pk_hash); |
| 326 | EXPECT_EQ(base::Version("0.0.0.0"), component.version); |
| 327 | EXPECT_TRUE(component.fingerprint.empty()); |
| 328 | EXPECT_STREQ("fake name", component.name.c_str()); |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 329 | EXPECT_EQ(expected_attrs, component.installer_attributes); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 330 | EXPECT_TRUE(component.requires_network_encryption); |
sorin | 098a4a7 | 2016-08-30 04:43:24 | [diff] [blame] | 331 | EXPECT_TRUE(component.supports_group_policy_enable_component_updates); |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 332 | } |
| 333 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 334 | // Tests that the unpack path is removed when the install succeeded. |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 335 | TEST_F(ComponentInstallerTest, UnpackPathInstallSuccess) { |
| 336 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 337 | std::make_unique<MockInstallerPolicy>()); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 338 | |
| 339 | Unpack(test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 340 | |
| 341 | const auto unpack_path = result().unpack_path; |
| 342 | EXPECT_TRUE(base::DirectoryExists(unpack_path)); |
Minh X. Nguyen | aafd763 | 2017-10-19 21:12:35 | [diff] [blame] | 343 | EXPECT_EQ(update_client::jebg_public_key, result().public_key); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 344 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 345 | base::ScopedPathOverride scoped_path_override(DIR_COMPONENT_USER); |
| 346 | base::FilePath base_dir; |
Avi Drissman | f617d01 | 2018-05-02 18:48:53 | [diff] [blame] | 347 | EXPECT_TRUE(base::PathService::Get(DIR_COMPONENT_USER, &base_dir)); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 348 | base_dir = base_dir.Append(relative_install_dir); |
| 349 | EXPECT_TRUE(base::CreateDirectory(base_dir)); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 350 | installer->Install( |
Sorin Jianu | ea5534e9 | 2017-10-27 01:40:28 | [diff] [blame] | 351 | unpack_path, update_client::jebg_public_key, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 352 | base::BindOnce([](const update_client::CrxInstaller::Result& result) { |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 353 | EXPECT_EQ(0, result.error); |
| 354 | })); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 355 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 356 | scoped_task_environment_.RunUntilIdle(); |
| 357 | |
| 358 | EXPECT_FALSE(base::PathExists(unpack_path)); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 359 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 360 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Tests that the unpack path is removed when the install failed. |
Sorin Jianu | 08f92b3c | 2017-09-25 16:17:12 | [diff] [blame] | 364 | TEST_F(ComponentInstallerTest, UnpackPathInstallError) { |
| 365 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 366 | std::make_unique<MockInstallerPolicy>()); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 367 | |
| 368 | Unpack(test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
| 369 | |
| 370 | const auto unpack_path = result().unpack_path; |
| 371 | EXPECT_TRUE(base::DirectoryExists(unpack_path)); |
| 372 | |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 373 | // Test the precondition that DIR_COMPONENT_USER is not registered with |
| 374 | // the path service. |
| 375 | base::FilePath base_dir; |
Avi Drissman | ea15ea0 | 2018-05-07 18:55:12 | [diff] [blame] | 376 | EXPECT_FALSE(base::PathService::Get(DIR_COMPONENT_USER, &base_dir)); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 377 | |
| 378 | // Calling |Install| fails since DIR_COMPONENT_USER does not exist. |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 379 | installer->Install( |
Sorin Jianu | ea5534e9 | 2017-10-27 01:40:28 | [diff] [blame] | 380 | unpack_path, update_client::jebg_public_key, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 381 | base::BindOnce([](const update_client::CrxInstaller::Result& result) { |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 382 | EXPECT_EQ(static_cast<int>( |
| 383 | update_client::InstallError::NO_DIR_COMPONENT_USER), |
| 384 | result.error); |
| 385 | })); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 386 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 387 | scoped_task_environment_.RunUntilIdle(); |
| 388 | |
| 389 | EXPECT_FALSE(base::PathExists(unpack_path)); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 390 | EXPECT_CALL(update_client(), Stop()).Times(1); |
Tibor Goldschwendt | 5f173cb | 2018-06-21 22:50:40 | [diff] [blame] | 391 | EXPECT_CALL(scheduler(), Stop()).Times(1); |
Sorin Jianu | 316232a | 2017-05-26 20:22:30 | [diff] [blame] | 392 | } |
| 393 | |
sorin | 6a57db9 | 2016-06-27 22:28:15 | [diff] [blame] | 394 | } // namespace component_updater |