sorin | 9797aba | 2015-04-17 17:15:03 | [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 | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 5 | #include <memory> |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 6 | #include <utility> |
| 7 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 8 | #include "base/bind.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
| 10 | #include "base/files/file_util.h" |
| 11 | #include "base/location.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 12 | #include "base/macros.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 14 | #include "base/path_service.h" |
| 15 | #include "base/run_loop.h" |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 16 | #include "base/stl_util.h" |
fdoray | 03349c1 | 2017-05-17 18:06:51 | [diff] [blame] | 17 | #include "base/task_scheduler/post_task.h" |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 18 | #include "base/task_scheduler/task_traits.h" |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 19 | #include "base/test/scoped_path_override.h" |
fdoray | 03349c1 | 2017-05-17 18:06:51 | [diff] [blame] | 20 | #include "base/test/scoped_task_environment.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 21 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 22 | #include "base/values.h" |
| 23 | #include "base/version.h" |
Sorin Jianu | 18890707 | 2018-01-22 18:42:22 | [diff] [blame] | 24 | #include "build/build_config.h" |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 25 | #include "components/prefs/testing_pref_service.h" |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 26 | #include "components/update_client/component_unpacker.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 27 | #include "components/update_client/crx_update_item.h" |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 28 | #include "components/update_client/persisted_data.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 29 | #include "components/update_client/ping_manager.h" |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 30 | #include "components/update_client/protocol_parser.h" |
sorin | b120440b | 2015-04-27 16:34:15 | [diff] [blame] | 31 | #include "components/update_client/test_configurator.h" |
| 32 | #include "components/update_client/test_installer.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 33 | #include "components/update_client/update_checker.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 34 | #include "components/update_client/update_client_errors.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 35 | #include "components/update_client/update_client_internal.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 36 | #include "testing/gmock/include/gmock/gmock.h" |
| 37 | #include "testing/gtest/include/gtest/gtest.h" |
| 38 | #include "url/gurl.h" |
| 39 | |
| 40 | namespace update_client { |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | using base::FilePath; |
| 45 | |
| 46 | // Makes a copy of the file specified by |from_path| in a temporary directory |
| 47 | // and returns the path of the copy. Returns true if successful. Cleans up if |
| 48 | // there was an error creating the copy. |
| 49 | bool MakeTestFile(const FilePath& from_path, FilePath* to_path) { |
| 50 | FilePath temp_dir; |
| 51 | bool result = |
| 52 | CreateNewTempDirectory(FILE_PATH_LITERAL("update_client"), &temp_dir); |
| 53 | if (!result) |
| 54 | return false; |
| 55 | |
| 56 | FilePath temp_file; |
| 57 | result = CreateTemporaryFileInDir(temp_dir, &temp_file); |
| 58 | if (!result) |
| 59 | return false; |
| 60 | |
| 61 | result = CopyFile(from_path, temp_file); |
| 62 | if (!result) { |
| 63 | DeleteFile(temp_file, false); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | *to_path = temp_file; |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | using Events = UpdateClient::Observer::Events; |
| 72 | |
| 73 | class MockObserver : public UpdateClient::Observer { |
| 74 | public: |
| 75 | MOCK_METHOD2(OnEvent, void(Events event, const std::string&)); |
| 76 | }; |
| 77 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 78 | } // namespace |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 79 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 80 | using ::testing::_; |
| 81 | using ::testing::AtLeast; |
| 82 | using ::testing::AnyNumber; |
| 83 | using ::testing::DoAll; |
| 84 | using ::testing::InSequence; |
| 85 | using ::testing::Invoke; |
| 86 | using ::testing::Mock; |
| 87 | using ::testing::Return; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 88 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 89 | using std::string; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 90 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 91 | class MockPingManagerImpl : public PingManager { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 92 | public: |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 93 | struct PingData { |
| 94 | std::string id; |
| 95 | base::Version previous_version; |
| 96 | base::Version next_version; |
| 97 | int error_category = 0; |
| 98 | int error_code = 0; |
| 99 | int extra_code1 = 0; |
| 100 | int diff_error_category = 0; |
| 101 | int diff_error_code = 0; |
| 102 | bool diff_update_failed = false; |
| 103 | }; |
| 104 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 105 | explicit MockPingManagerImpl(scoped_refptr<Configurator> config); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 106 | |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 107 | void SendPing(const Component& component, Callback callback) override; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 108 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 109 | const std::vector<PingData>& ping_data() const; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 110 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 111 | const std::vector<std::string>& events() const; |
| 112 | |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 113 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 114 | ~MockPingManagerImpl() override; |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 115 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 116 | private: |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 117 | std::vector<PingData> ping_data_; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 118 | std::vector<std::string> events_; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(MockPingManagerImpl); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 120 | }; |
| 121 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 122 | MockPingManagerImpl::MockPingManagerImpl(scoped_refptr<Configurator> config) |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame] | 123 | : PingManager(config) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 124 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 125 | MockPingManagerImpl::~MockPingManagerImpl() {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 126 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 127 | void MockPingManagerImpl::SendPing(const Component& component, |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 128 | Callback callback) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 129 | PingData ping_data; |
| 130 | ping_data.id = component.id_; |
| 131 | ping_data.previous_version = component.previous_version_; |
| 132 | ping_data.next_version = component.next_version_; |
| 133 | ping_data.error_category = component.error_category_; |
| 134 | ping_data.error_code = component.error_code_; |
| 135 | ping_data.extra_code1 = component.extra_code1_; |
| 136 | ping_data.diff_error_category = component.diff_error_category_; |
| 137 | ping_data.diff_error_code = component.diff_error_code_; |
| 138 | ping_data.diff_update_failed = component.diff_update_failed(); |
| 139 | ping_data_.push_back(ping_data); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 140 | |
| 141 | const auto& events = component.events(); |
| 142 | events_.insert(events_.end(), events.begin(), events.end()); |
| 143 | |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 144 | std::move(callback).Run(0, ""); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 145 | } |
| 146 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 147 | const std::vector<MockPingManagerImpl::PingData>& |
| 148 | MockPingManagerImpl::ping_data() const { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 149 | return ping_data_; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 150 | } |
| 151 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 152 | const std::vector<std::string>& MockPingManagerImpl::events() const { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 153 | return events_; |
| 154 | } |
| 155 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 156 | class UpdateClientTest : public testing::Test { |
| 157 | public: |
| 158 | UpdateClientTest(); |
| 159 | ~UpdateClientTest() override; |
| 160 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 161 | protected: |
| 162 | void RunThreads(); |
| 163 | |
| 164 | // Returns the full path to a test file. |
| 165 | static base::FilePath TestFilePath(const char* file); |
| 166 | |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 167 | scoped_refptr<update_client::TestConfigurator> config() { return config_; } |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 168 | update_client::PersistedData* metadata() { return metadata_.get(); } |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 169 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 170 | base::OnceClosure quit_closure() { return runloop_.QuitClosure(); } |
sorin | f9f4834d | 2015-04-28 17:15:02 | [diff] [blame] | 171 | |
| 172 | private: |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 173 | static constexpr int kNumWorkerThreads_ = 2; |
sorin | f9f4834d | 2015-04-28 17:15:02 | [diff] [blame] | 174 | |
fdoray | 03349c1 | 2017-05-17 18:06:51 | [diff] [blame] | 175 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 176 | base::RunLoop runloop_; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 177 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 178 | scoped_refptr<update_client::TestConfigurator> config_ = |
| 179 | base::MakeRefCounted<TestConfigurator>(); |
| 180 | std::unique_ptr<TestingPrefServiceSimple> pref_ = |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 181 | std::make_unique<TestingPrefServiceSimple>(); |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 182 | std::unique_ptr<update_client::PersistedData> metadata_; |
sorin | f9f4834d | 2015-04-28 17:15:02 | [diff] [blame] | 183 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 184 | DISALLOW_COPY_AND_ASSIGN(UpdateClientTest); |
| 185 | }; |
| 186 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 187 | constexpr int UpdateClientTest::kNumWorkerThreads_; |
| 188 | |
Sorin Jianu | cc048f89 | 2017-07-26 02:05:54 | [diff] [blame] | 189 | UpdateClientTest::UpdateClientTest() { |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 190 | PersistedData::RegisterPrefs(pref_->registry()); |
Jinho Bang | da4e428 | 2018-01-03 13:21:23 | [diff] [blame] | 191 | metadata_ = std::make_unique<PersistedData>(pref_.get(), nullptr); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | UpdateClientTest::~UpdateClientTest() { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void UpdateClientTest::RunThreads() { |
| 198 | runloop_.Run(); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 199 | scoped_task_environment_.RunUntilIdle(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | base::FilePath UpdateClientTest::TestFilePath(const char* file) { |
| 203 | base::FilePath path; |
Avi Drissman | f617d01 | 2018-05-02 18:48:53 | [diff] [blame] | 204 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 205 | return path.AppendASCII("components") |
| 206 | .AppendASCII("test") |
| 207 | .AppendASCII("data") |
| 208 | .AppendASCII("update_client") |
| 209 | .AppendASCII(file); |
| 210 | } |
| 211 | |
| 212 | // Tests the scenario where one update check is done for one CRX. The CRX |
| 213 | // has no update. |
| 214 | TEST_F(UpdateClientTest, OneCrxNoUpdate) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 215 | class DataCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 216 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 217 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 218 | const std::vector<std::string>& ids) { |
| 219 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 220 | crx->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 221 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 222 | crx->version = base::Version("0.9"); |
| 223 | crx->installer = base::MakeRefCounted<TestInstaller>(); |
| 224 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 225 | component.push_back(std::move(crx)); |
| 226 | return component; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 230 | class CompletionCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 231 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 232 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 233 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 234 | std::move(quit_closure).Run(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 235 | } |
| 236 | }; |
| 237 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 238 | class MockUpdateChecker : public UpdateChecker { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 239 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 240 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 241 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 242 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 243 | return std::make_unique<MockUpdateChecker>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 244 | } |
| 245 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 246 | void CheckForUpdates(const std::string& session_id, |
| 247 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 248 | const IdToComponentPtrMap& components, |
| 249 | const std::string& additional_attributes, |
| 250 | bool enabled_component_updates, |
| 251 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 252 | EXPECT_FALSE(session_id.empty()); |
sorin | 590921d | 2016-08-11 23:48:36 | [diff] [blame] | 253 | EXPECT_TRUE(enabled_component_updates); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 254 | EXPECT_EQ(1u, ids_to_check.size()); |
| 255 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 256 | EXPECT_EQ(id, ids_to_check.front()); |
| 257 | EXPECT_EQ(1u, components.count(id)); |
| 258 | |
| 259 | auto& component = components.at(id); |
| 260 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 261 | EXPECT_TRUE(component->is_foreground()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 262 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 263 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 264 | result.extension_id = id; |
| 265 | result.status = "noupdate"; |
| 266 | component->SetParseResult(result); |
| 267 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 268 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 269 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 270 | } |
| 271 | }; |
| 272 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 273 | class MockCrxDownloader : public CrxDownloader { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 274 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 275 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 276 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 277 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 278 | return std::make_unique<MockCrxDownloader>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 279 | } |
| 280 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 281 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 282 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 283 | private: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 284 | void DoStartDownload(const GURL& url) override { EXPECT_TRUE(false); } |
| 285 | }; |
| 286 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 287 | class MockPingManager : public MockPingManagerImpl { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 288 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 289 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 290 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 291 | |
| 292 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 293 | ~MockPingManager() override { EXPECT_TRUE(ping_data().empty()); } |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 294 | }; |
| 295 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 296 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 297 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 298 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 299 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 300 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 301 | MockObserver observer; |
| 302 | InSequence seq; |
| 303 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 304 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 305 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 306 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 307 | |
| 308 | update_client->AddObserver(&observer); |
| 309 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 310 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf"}; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 311 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 312 | ids, base::BindOnce(&DataCallbackMock::Callback), true, |
| 313 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 314 | |
| 315 | RunThreads(); |
| 316 | |
| 317 | update_client->RemoveObserver(&observer); |
| 318 | } |
| 319 | |
| 320 | // Tests the scenario where two CRXs are checked for updates. On CRX has |
| 321 | // an update, the other CRX does not. |
| 322 | TEST_F(UpdateClientTest, TwoCrxUpdateNoUpdate) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 323 | class DataCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 324 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 325 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 326 | const std::vector<std::string>& ids) { |
| 327 | std::unique_ptr<CrxComponent> crx1 = std::make_unique<CrxComponent>(); |
| 328 | crx1->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 329 | crx1->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 330 | crx1->version = base::Version("0.9"); |
| 331 | crx1->installer = base::MakeRefCounted<TestInstaller>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 332 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 333 | std::unique_ptr<CrxComponent> crx2 = std::make_unique<CrxComponent>(); |
| 334 | crx2->name = "test_abag"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 335 | crx2->pk_hash.assign(abag_hash, abag_hash + base::size(abag_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 336 | crx2->version = base::Version("2.2"); |
| 337 | crx2->installer = base::MakeRefCounted<TestInstaller>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 338 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 339 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 340 | component.push_back(std::move(crx1)); |
| 341 | component.push_back(std::move(crx2)); |
| 342 | return component; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 343 | } |
| 344 | }; |
| 345 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 346 | class CompletionCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 347 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 348 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 349 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 350 | std::move(quit_closure).Run(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 351 | } |
| 352 | }; |
| 353 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 354 | class MockUpdateChecker : public UpdateChecker { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 355 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 356 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 357 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 358 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 359 | return std::make_unique<MockUpdateChecker>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 360 | } |
| 361 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 362 | void CheckForUpdates(const std::string& session_id, |
| 363 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 364 | const IdToComponentPtrMap& components, |
| 365 | const std::string& additional_attributes, |
| 366 | bool enabled_component_updates, |
| 367 | UpdateCheckCallback update_check_callback) override { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 368 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 369 | Mock the following response: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 370 | |
| 371 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 372 | <response protocol='3.1'> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 373 | <app appid='jebgalgnebhfojomionfpkfelancnnkf'> |
| 374 | <updatecheck status='ok'> |
| 375 | <urls> |
| 376 | <url codebase='http://localhost/download/'/> |
| 377 | </urls> |
| 378 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 379 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 380 | <package name='jebgalgnebhfojomionfpkfelancnnkf.crx' |
| 381 | hash_sha256='6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd |
| 382 | 7c9b12cb7cc067667bde87'/> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 383 | </packages> |
| 384 | </manifest> |
| 385 | </updatecheck> |
| 386 | </app> |
| 387 | </response> |
| 388 | */ |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 389 | EXPECT_FALSE(session_id.empty()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 390 | EXPECT_TRUE(enabled_component_updates); |
| 391 | EXPECT_EQ(2u, ids_to_check.size()); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 392 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 393 | { |
| 394 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 395 | EXPECT_EQ(id, ids_to_check[0]); |
| 396 | EXPECT_EQ(1u, components.count(id)); |
| 397 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 398 | ProtocolParser::Result::Manifest::Package package; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 399 | package.name = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 400 | package.hash_sha256 = |
| 401 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
| 402 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 403 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 404 | result.extension_id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 405 | result.status = "ok"; |
| 406 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 407 | result.manifest.version = "1.0"; |
| 408 | result.manifest.browser_min_version = "11.0.1.0"; |
| 409 | result.manifest.packages.push_back(package); |
| 410 | |
| 411 | auto& component = components.at(id); |
| 412 | component->SetParseResult(result); |
| 413 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 414 | EXPECT_FALSE(component->is_foreground()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | { |
| 418 | const std::string id = "abagagagagagagagagagagagagagagag"; |
| 419 | EXPECT_EQ(id, ids_to_check[1]); |
| 420 | EXPECT_EQ(1u, components.count(id)); |
| 421 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 422 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 423 | result.extension_id = id; |
| 424 | result.status = "noupdate"; |
| 425 | |
| 426 | auto& component = components.at(id); |
| 427 | component->SetParseResult(result); |
| 428 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 429 | EXPECT_FALSE(component->is_foreground()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 430 | } |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 431 | |
| 432 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 433 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 434 | } |
| 435 | }; |
| 436 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 437 | class MockCrxDownloader : public CrxDownloader { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 438 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 439 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 440 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 441 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 442 | return std::make_unique<MockCrxDownloader>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 443 | } |
| 444 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 445 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 446 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 447 | private: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 448 | void DoStartDownload(const GURL& url) override { |
| 449 | DownloadMetrics download_metrics; |
| 450 | download_metrics.url = url; |
| 451 | download_metrics.downloader = DownloadMetrics::kNone; |
| 452 | download_metrics.error = 0; |
| 453 | download_metrics.downloaded_bytes = 1843; |
| 454 | download_metrics.total_bytes = 1843; |
| 455 | download_metrics.download_time_ms = 1000; |
| 456 | |
| 457 | FilePath path; |
| 458 | EXPECT_TRUE(MakeTestFile( |
| 459 | TestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), &path)); |
| 460 | |
| 461 | Result result; |
| 462 | result.error = 0; |
| 463 | result.response = path; |
| 464 | result.downloaded_bytes = 1843; |
| 465 | result.total_bytes = 1843; |
| 466 | |
| 467 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 468 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 469 | base::Unretained(this), result)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 470 | |
| 471 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 472 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 473 | base::Unretained(this), true, result, |
| 474 | download_metrics)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 475 | } |
| 476 | }; |
| 477 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 478 | class MockPingManager : public MockPingManagerImpl { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 479 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 480 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 481 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 482 | |
| 483 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 484 | ~MockPingManager() override { |
| 485 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 486 | EXPECT_EQ(1u, ping_data.size()); |
| 487 | EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_data[0].id); |
| 488 | EXPECT_EQ(base::Version("0.9"), ping_data[0].previous_version); |
| 489 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
| 490 | EXPECT_EQ(0, ping_data[0].error_category); |
| 491 | EXPECT_EQ(0, ping_data[0].error_code); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 492 | } |
| 493 | }; |
| 494 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 495 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 496 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 497 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 498 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 499 | |
| 500 | MockObserver observer; |
| 501 | { |
| 502 | InSequence seq; |
| 503 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 504 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 505 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 506 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 507 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 508 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 509 | .Times(AtLeast(1)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 510 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 511 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 512 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 513 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 514 | } |
| 515 | { |
| 516 | InSequence seq; |
| 517 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 518 | "abagagagagagagagagagagagagagagag")).Times(1); |
| 519 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 520 | "abagagagagagagagagagagagagagagag")).Times(1); |
| 521 | } |
| 522 | |
| 523 | update_client->AddObserver(&observer); |
| 524 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 525 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf", |
| 526 | "abagagagagagagagagagagagagagagag"}; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 527 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 528 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 529 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 530 | |
| 531 | RunThreads(); |
| 532 | |
| 533 | update_client->RemoveObserver(&observer); |
| 534 | } |
| 535 | |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 536 | // Tests the update check for two CRXs scenario when the second CRX does not |
| 537 | // provide a CrxComponent instance. In this case, the update is handled as |
| 538 | // if only one component were provided as an argument to the |Update| call |
| 539 | // with the exception that the second component still fires an event such as |
| 540 | // |COMPONENT_UPDATE_ERROR|. |
| 541 | TEST_F(UpdateClientTest, TwoCrxUpdateNoCrxComponentData) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 542 | class DataCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 543 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 544 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 545 | const std::vector<std::string>& ids) { |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 546 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 547 | crx->name = "test_jebg"; |
| 548 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
| 549 | crx->version = base::Version("0.9"); |
| 550 | crx->installer = base::MakeRefCounted<TestInstaller>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 551 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 552 | std::vector<std::unique_ptr<CrxComponent>> component; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 553 | component.push_back(std::move(crx)); |
| 554 | component.push_back(std::move(nullptr)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 555 | return component; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 556 | } |
| 557 | }; |
| 558 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 559 | class CompletionCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 560 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 561 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 562 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 563 | std::move(quit_closure).Run(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 564 | } |
| 565 | }; |
| 566 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 567 | class MockUpdateChecker : public UpdateChecker { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 568 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 569 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 570 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 571 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 572 | return std::make_unique<MockUpdateChecker>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 573 | } |
| 574 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 575 | void CheckForUpdates(const std::string& session_id, |
| 576 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 577 | const IdToComponentPtrMap& components, |
| 578 | const std::string& additional_attributes, |
| 579 | bool enabled_component_updates, |
| 580 | UpdateCheckCallback update_check_callback) override { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 581 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 582 | Mock the following response: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 583 | |
| 584 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 585 | <response protocol='3.1'> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 586 | <app appid='jebgalgnebhfojomionfpkfelancnnkf'> |
| 587 | <updatecheck status='ok'> |
| 588 | <urls> |
| 589 | <url codebase='http://localhost/download/'/> |
| 590 | </urls> |
| 591 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 592 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 593 | <package name='jebgalgnebhfojomionfpkfelancnnkf.crx' |
| 594 | hash_sha256='6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd |
| 595 | 7c9b12cb7cc067667bde87'/> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 596 | </packages> |
| 597 | </manifest> |
| 598 | </updatecheck> |
| 599 | </app> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 600 | </response> |
| 601 | */ |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 602 | EXPECT_FALSE(session_id.empty()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 603 | EXPECT_TRUE(enabled_component_updates); |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 604 | EXPECT_EQ(1u, ids_to_check.size()); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 605 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 606 | { |
| 607 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 608 | EXPECT_EQ(id, ids_to_check[0]); |
| 609 | EXPECT_EQ(1u, components.count(id)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 610 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 611 | ProtocolParser::Result::Manifest::Package package; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 612 | package.name = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 613 | package.hash_sha256 = |
| 614 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 615 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 616 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 617 | result.extension_id = id; |
| 618 | result.status = "ok"; |
| 619 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 620 | result.manifest.version = "1.0"; |
| 621 | result.manifest.browser_min_version = "11.0.1.0"; |
| 622 | result.manifest.packages.push_back(package); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 623 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 624 | auto& component = components.at(id); |
| 625 | component->SetParseResult(result); |
| 626 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 627 | EXPECT_FALSE(component->is_foreground()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 628 | } |
| 629 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 630 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 631 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 632 | } |
| 633 | }; |
| 634 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 635 | class MockCrxDownloader : public CrxDownloader { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 636 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 637 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 638 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 639 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 640 | return std::make_unique<MockCrxDownloader>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 641 | } |
| 642 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 643 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 644 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 645 | private: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 646 | void DoStartDownload(const GURL& url) override { |
| 647 | DownloadMetrics download_metrics; |
| 648 | FilePath path; |
| 649 | Result result; |
| 650 | if (url.path() == "/download/jebgalgnebhfojomionfpkfelancnnkf.crx") { |
| 651 | download_metrics.url = url; |
| 652 | download_metrics.downloader = DownloadMetrics::kNone; |
| 653 | download_metrics.error = 0; |
| 654 | download_metrics.downloaded_bytes = 1843; |
| 655 | download_metrics.total_bytes = 1843; |
| 656 | download_metrics.download_time_ms = 1000; |
| 657 | |
| 658 | EXPECT_TRUE(MakeTestFile( |
| 659 | TestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), &path)); |
| 660 | |
| 661 | result.error = 0; |
| 662 | result.response = path; |
| 663 | result.downloaded_bytes = 1843; |
| 664 | result.total_bytes = 1843; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 665 | } else { |
| 666 | NOTREACHED(); |
| 667 | } |
| 668 | |
| 669 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 670 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 671 | base::Unretained(this), result)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 672 | |
| 673 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 674 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 675 | base::Unretained(this), true, result, |
| 676 | download_metrics)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 677 | } |
| 678 | }; |
| 679 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 680 | class MockPingManager : public MockPingManagerImpl { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 681 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 682 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 683 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 684 | |
| 685 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 686 | ~MockPingManager() override { |
| 687 | const auto ping_data = MockPingManagerImpl::ping_data(); |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 688 | EXPECT_EQ(1u, ping_data.size()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 689 | EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_data[0].id); |
| 690 | EXPECT_EQ(base::Version("0.9"), ping_data[0].previous_version); |
| 691 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
| 692 | EXPECT_EQ(0, ping_data[0].error_category); |
| 693 | EXPECT_EQ(0, ping_data[0].error_code); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 694 | } |
| 695 | }; |
| 696 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 697 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 698 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 699 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 700 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 701 | |
| 702 | MockObserver observer; |
| 703 | { |
| 704 | InSequence seq; |
| 705 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 706 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 707 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 708 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 709 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 710 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 711 | .Times(AtLeast(1)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 712 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 713 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 714 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 715 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 716 | } |
| 717 | { |
| 718 | InSequence seq; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 719 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 720 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 721 | .Times(1); |
| 722 | } |
| 723 | |
| 724 | update_client->AddObserver(&observer); |
| 725 | |
| 726 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf", |
| 727 | "ihfokbkgjpifnbbojhneepfflplebdkc"}; |
| 728 | update_client->Update( |
| 729 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 730 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
| 731 | |
| 732 | RunThreads(); |
| 733 | |
| 734 | update_client->RemoveObserver(&observer); |
| 735 | } |
| 736 | |
| 737 | // Tests the update check for two CRXs scenario when no CrxComponent data is |
| 738 | // provided for either component. In this case, no update check occurs, and |
| 739 | // |COMPONENT_UPDATE_ERROR| event fires for both components. |
| 740 | TEST_F(UpdateClientTest, TwoCrxUpdateNoCrxComponentDataAtAll) { |
| 741 | class DataCallbackMock { |
| 742 | public: |
| 743 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 744 | const std::vector<std::string>& ids) { |
| 745 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 746 | component.push_back(std::move(nullptr)); |
| 747 | component.push_back(std::move(nullptr)); |
| 748 | return component; |
| 749 | } |
| 750 | }; |
| 751 | |
| 752 | class CompletionCallbackMock { |
| 753 | public: |
| 754 | static void Callback(base::OnceClosure quit_closure, Error error) { |
| 755 | EXPECT_EQ(Error::NONE, error); |
| 756 | std::move(quit_closure).Run(); |
| 757 | } |
| 758 | }; |
| 759 | |
| 760 | class MockUpdateChecker : public UpdateChecker { |
| 761 | public: |
| 762 | static std::unique_ptr<UpdateChecker> Create( |
| 763 | scoped_refptr<Configurator> config, |
| 764 | PersistedData* metadata) { |
| 765 | return std::make_unique<MockUpdateChecker>(); |
| 766 | } |
| 767 | |
| 768 | void CheckForUpdates(const std::string& session_id, |
| 769 | const std::vector<std::string>& ids_to_check, |
| 770 | const IdToComponentPtrMap& components, |
| 771 | const std::string& additional_attributes, |
| 772 | bool enabled_component_updates, |
| 773 | UpdateCheckCallback update_check_callback) override { |
| 774 | NOTREACHED(); |
| 775 | } |
| 776 | }; |
| 777 | |
| 778 | class MockCrxDownloader : public CrxDownloader { |
| 779 | public: |
| 780 | static std::unique_ptr<CrxDownloader> Create( |
| 781 | bool is_background_download, |
| 782 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
| 783 | return std::make_unique<MockCrxDownloader>(); |
| 784 | } |
| 785 | |
| 786 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
| 787 | |
| 788 | private: |
| 789 | void DoStartDownload(const GURL& url) override { NOTREACHED(); } |
| 790 | }; |
| 791 | |
| 792 | class MockPingManager : public MockPingManagerImpl { |
| 793 | public: |
| 794 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 795 | : MockPingManagerImpl(config) {} |
| 796 | |
| 797 | protected: |
| 798 | ~MockPingManager() override { |
| 799 | EXPECT_EQ(0u, MockPingManagerImpl::ping_data().size()); |
| 800 | } |
| 801 | }; |
| 802 | |
| 803 | scoped_refptr<UpdateClient> update_client = |
| 804 | base::MakeRefCounted<UpdateClientImpl>( |
| 805 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 806 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
| 807 | |
| 808 | MockObserver observer; |
| 809 | { |
| 810 | InSequence seq; |
| 811 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
| 812 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 813 | .Times(1); |
| 814 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
| 815 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 816 | .Times(1); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | update_client->AddObserver(&observer); |
| 820 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 821 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf", |
| 822 | "ihfokbkgjpifnbbojhneepfflplebdkc"}; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 823 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 824 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 825 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 826 | |
| 827 | RunThreads(); |
| 828 | |
| 829 | update_client->RemoveObserver(&observer); |
| 830 | } |
| 831 | |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 832 | // Tests the scenario where there is a download timeout for the first |
| 833 | // CRX. The update for the first CRX fails. The update client waits before |
| 834 | // attempting the update for the second CRX. This update succeeds. |
| 835 | TEST_F(UpdateClientTest, TwoCrxUpdateDownloadTimeout) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 836 | class DataCallbackMock { |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 837 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 838 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 839 | const std::vector<std::string>& ids) { |
| 840 | std::unique_ptr<CrxComponent> crx1 = std::make_unique<CrxComponent>(); |
| 841 | crx1->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 842 | crx1->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 843 | crx1->version = base::Version("0.9"); |
| 844 | crx1->installer = base::MakeRefCounted<TestInstaller>(); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 845 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 846 | std::unique_ptr<CrxComponent> crx2 = std::make_unique<CrxComponent>(); |
| 847 | crx2->name = "test_ihfo"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 848 | crx2->pk_hash.assign(ihfo_hash, ihfo_hash + base::size(ihfo_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 849 | crx2->version = base::Version("0.8"); |
| 850 | crx2->installer = base::MakeRefCounted<TestInstaller>(); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 851 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 852 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 853 | component.push_back(std::move(crx1)); |
| 854 | component.push_back(std::move(crx2)); |
| 855 | return component; |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 856 | } |
| 857 | }; |
| 858 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 859 | class CompletionCallbackMock { |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 860 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 861 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 862 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 863 | std::move(quit_closure).Run(); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 864 | } |
| 865 | }; |
| 866 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 867 | class MockUpdateChecker : public UpdateChecker { |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 868 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 869 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 870 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 871 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 872 | return std::make_unique<MockUpdateChecker>(); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 873 | } |
| 874 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 875 | void CheckForUpdates(const std::string& session_id, |
| 876 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 877 | const IdToComponentPtrMap& components, |
| 878 | const std::string& additional_attributes, |
| 879 | bool enabled_component_updates, |
| 880 | UpdateCheckCallback update_check_callback) override { |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 881 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 882 | Mock the following response: |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 883 | |
| 884 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 885 | <response protocol='3.1'> |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 886 | <app appid='jebgalgnebhfojomionfpkfelancnnkf'> |
| 887 | <updatecheck status='ok'> |
| 888 | <urls> |
| 889 | <url codebase='http://localhost/download/'/> |
| 890 | </urls> |
| 891 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 892 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 893 | <package name='jebgalgnebhfojomionfpkfelancnnkf.crx' |
| 894 | hash_sha256='6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd |
| 895 | 7c9b12cb7cc067667bde87'/> |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 896 | </packages> |
| 897 | </manifest> |
| 898 | </updatecheck> |
| 899 | </app> |
| 900 | <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'> |
| 901 | <updatecheck status='ok'> |
| 902 | <urls> |
| 903 | <url codebase='http://localhost/download/'/> |
| 904 | </urls> |
| 905 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 906 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 907 | <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx' |
| 908 | hash_sha256='813c59747e139a608b3b5fc49633affc6db574373f |
| 909 | 309f156ea6d27229c0b3f9'/> |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 910 | </packages> |
| 911 | </manifest> |
| 912 | </updatecheck> |
| 913 | </app> |
| 914 | </response> |
| 915 | */ |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 916 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 917 | EXPECT_FALSE(session_id.empty()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 918 | EXPECT_TRUE(enabled_component_updates); |
| 919 | EXPECT_EQ(2u, ids_to_check.size()); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 920 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 921 | { |
| 922 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 923 | EXPECT_EQ(id, ids_to_check[0]); |
| 924 | EXPECT_EQ(1u, components.count(id)); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 925 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 926 | ProtocolParser::Result::Manifest::Package package; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 927 | package.name = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 928 | package.hash_sha256 = |
| 929 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 930 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 931 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 932 | result.extension_id = id; |
| 933 | result.status = "ok"; |
| 934 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 935 | result.manifest.version = "1.0"; |
| 936 | result.manifest.browser_min_version = "11.0.1.0"; |
| 937 | result.manifest.packages.push_back(package); |
| 938 | |
| 939 | auto& component = components.at(id); |
| 940 | component->SetParseResult(result); |
| 941 | } |
| 942 | |
| 943 | { |
| 944 | const std::string id = "ihfokbkgjpifnbbojhneepfflplebdkc"; |
| 945 | EXPECT_EQ(id, ids_to_check[1]); |
| 946 | EXPECT_EQ(1u, components.count(id)); |
| 947 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 948 | ProtocolParser::Result::Manifest::Package package; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 949 | package.name = "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"; |
| 950 | package.hash_sha256 = |
| 951 | "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"; |
| 952 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 953 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 954 | result.extension_id = id; |
| 955 | result.status = "ok"; |
| 956 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 957 | result.manifest.version = "1.0"; |
| 958 | result.manifest.browser_min_version = "11.0.1.0"; |
| 959 | result.manifest.packages.push_back(package); |
| 960 | |
| 961 | auto& component = components.at(id); |
| 962 | component->SetParseResult(result); |
| 963 | } |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 964 | |
| 965 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 966 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 967 | } |
| 968 | }; |
| 969 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 970 | class MockCrxDownloader : public CrxDownloader { |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 971 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 972 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 973 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 974 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 975 | return std::make_unique<MockCrxDownloader>(); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 976 | } |
| 977 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 978 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 979 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 980 | private: |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 981 | void DoStartDownload(const GURL& url) override { |
| 982 | DownloadMetrics download_metrics; |
| 983 | FilePath path; |
| 984 | Result result; |
| 985 | if (url.path() == "/download/jebgalgnebhfojomionfpkfelancnnkf.crx") { |
| 986 | download_metrics.url = url; |
| 987 | download_metrics.downloader = DownloadMetrics::kNone; |
| 988 | download_metrics.error = -118; |
| 989 | download_metrics.downloaded_bytes = 0; |
| 990 | download_metrics.total_bytes = 0; |
| 991 | download_metrics.download_time_ms = 1000; |
| 992 | |
Sorin Jianu | 52de5fa | 2017-10-09 23:19:33 | [diff] [blame] | 993 | // The result must not include a file path in the case of errors. |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 994 | result.error = -118; |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 995 | result.downloaded_bytes = 0; |
| 996 | result.total_bytes = 0; |
| 997 | } else if (url.path() == |
| 998 | "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") { |
| 999 | download_metrics.url = url; |
| 1000 | download_metrics.downloader = DownloadMetrics::kNone; |
| 1001 | download_metrics.error = 0; |
| 1002 | download_metrics.downloaded_bytes = 53638; |
| 1003 | download_metrics.total_bytes = 53638; |
| 1004 | download_metrics.download_time_ms = 2000; |
| 1005 | |
| 1006 | EXPECT_TRUE(MakeTestFile( |
| 1007 | TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path)); |
| 1008 | |
| 1009 | result.error = 0; |
| 1010 | result.response = path; |
| 1011 | result.downloaded_bytes = 53638; |
| 1012 | result.total_bytes = 53638; |
| 1013 | } else { |
| 1014 | NOTREACHED(); |
| 1015 | } |
| 1016 | |
| 1017 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1018 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 1019 | base::Unretained(this), result)); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1020 | |
| 1021 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1022 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 1023 | base::Unretained(this), true, result, |
| 1024 | download_metrics)); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1025 | } |
| 1026 | }; |
| 1027 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1028 | class MockPingManager : public MockPingManagerImpl { |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1029 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1030 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 1031 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 1032 | |
| 1033 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1034 | ~MockPingManager() override { |
| 1035 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1036 | EXPECT_EQ(2u, ping_data.size()); |
| 1037 | EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_data[0].id); |
| 1038 | EXPECT_EQ(base::Version("0.9"), ping_data[0].previous_version); |
| 1039 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
| 1040 | EXPECT_EQ(1, ping_data[0].error_category); |
| 1041 | EXPECT_EQ(-118, ping_data[0].error_code); |
| 1042 | EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_data[1].id); |
| 1043 | EXPECT_EQ(base::Version("0.8"), ping_data[1].previous_version); |
| 1044 | EXPECT_EQ(base::Version("1.0"), ping_data[1].next_version); |
| 1045 | EXPECT_EQ(0, ping_data[1].error_category); |
| 1046 | EXPECT_EQ(0, ping_data[1].error_code); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1047 | } |
| 1048 | }; |
| 1049 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1050 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 1051 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1052 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 1053 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1054 | |
| 1055 | MockObserver observer; |
| 1056 | { |
| 1057 | InSequence seq; |
| 1058 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 1059 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 1060 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 1061 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 1062 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1063 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 1064 | .Times(AtLeast(1)); |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 1065 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
| 1066 | "jebgalgnebhfojomionfpkfelancnnkf")) |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 1067 | .Times(1) |
| 1068 | .WillOnce(Invoke([&update_client](Events event, const std::string& id) { |
| 1069 | CrxUpdateItem item; |
| 1070 | update_client->GetCrxUpdateState(id, &item); |
| 1071 | EXPECT_EQ(ComponentState::kUpdateError, item.state); |
| 1072 | EXPECT_EQ(1, item.error_category); |
| 1073 | EXPECT_EQ(-118, item.error_code); |
| 1074 | EXPECT_EQ(0, item.extra_code1); |
| 1075 | })); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1076 | } |
| 1077 | { |
| 1078 | InSequence seq; |
| 1079 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 1080 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1081 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 1082 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1083 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_WAIT, |
| 1084 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1085 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1086 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 1087 | .Times(AtLeast(1)); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1088 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 1089 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1090 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 1091 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1092 | } |
| 1093 | |
| 1094 | update_client->AddObserver(&observer); |
| 1095 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1096 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf", |
| 1097 | "ihfokbkgjpifnbbojhneepfflplebdkc"}; |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1098 | |
| 1099 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1100 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 1101 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1102 | |
| 1103 | RunThreads(); |
| 1104 | |
| 1105 | update_client->RemoveObserver(&observer); |
sorin | 6bb8de4 | 2015-06-03 00:23:27 | [diff] [blame] | 1106 | } |
| 1107 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1108 | // Tests the differential update scenario for one CRX. |
| 1109 | TEST_F(UpdateClientTest, OneCrxDiffUpdate) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1110 | class DataCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1111 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1112 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 1113 | const std::vector<std::string>& ids) { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1114 | static int num_calls = 0; |
| 1115 | |
| 1116 | // Must use the same stateful installer object. |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1117 | static scoped_refptr<CrxInstaller> installer = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 1118 | base::MakeRefCounted<VersionedTestInstaller>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1119 | |
| 1120 | ++num_calls; |
| 1121 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1122 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 1123 | crx->name = "test_ihfo"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 1124 | crx->pk_hash.assign(ihfo_hash, ihfo_hash + base::size(ihfo_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1125 | crx->installer = installer; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1126 | if (num_calls == 1) { |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1127 | crx->version = base::Version("0.8"); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1128 | } else if (num_calls == 2) { |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1129 | crx->version = base::Version("1.0"); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1130 | } else { |
| 1131 | NOTREACHED(); |
| 1132 | } |
| 1133 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1134 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 1135 | component.push_back(std::move(crx)); |
| 1136 | return component; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1137 | } |
| 1138 | }; |
| 1139 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1140 | class CompletionCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1141 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1142 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 1143 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1144 | std::move(quit_closure).Run(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1145 | } |
| 1146 | }; |
| 1147 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1148 | class MockUpdateChecker : public UpdateChecker { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1149 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 1150 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 1151 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 1152 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1153 | return std::make_unique<MockUpdateChecker>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1154 | } |
| 1155 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 1156 | void CheckForUpdates(const std::string& session_id, |
| 1157 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1158 | const IdToComponentPtrMap& components, |
| 1159 | const std::string& additional_attributes, |
| 1160 | bool enabled_component_updates, |
| 1161 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 1162 | EXPECT_FALSE(session_id.empty()); |
| 1163 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1164 | static int num_call = 0; |
| 1165 | ++num_call; |
| 1166 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1167 | ProtocolParser::Results results; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1168 | |
| 1169 | if (num_call == 1) { |
| 1170 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1171 | Mock the following response: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1172 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 1173 | <response protocol='3.1'> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1174 | <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'> |
| 1175 | <updatecheck status='ok'> |
| 1176 | <urls> |
| 1177 | <url codebase='http://localhost/download/'/> |
| 1178 | </urls> |
| 1179 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 1180 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1181 | <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx' |
| 1182 | hash_sha256='813c59747e139a608b3b5fc49633affc6db57437 |
| 1183 | 3f309f156ea6d27229c0b3f9'/> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1184 | </packages> |
| 1185 | </manifest> |
| 1186 | </updatecheck> |
| 1187 | </app> |
| 1188 | </response> |
| 1189 | */ |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1190 | const std::string id = "ihfokbkgjpifnbbojhneepfflplebdkc"; |
| 1191 | EXPECT_EQ(id, ids_to_check[0]); |
| 1192 | EXPECT_EQ(1u, components.count(id)); |
| 1193 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1194 | ProtocolParser::Result::Manifest::Package package; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1195 | package.name = "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1196 | package.hash_sha256 = |
| 1197 | "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1198 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1199 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1200 | result.extension_id = id; |
sorin | 9ef301c | 2017-02-16 20:29:17 | [diff] [blame] | 1201 | result.status = "ok"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1202 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 1203 | result.manifest.version = "1.0"; |
| 1204 | result.manifest.browser_min_version = "11.0.1.0"; |
| 1205 | result.manifest.packages.push_back(package); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1206 | |
| 1207 | auto& component = components.at(id); |
| 1208 | component->SetParseResult(result); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1209 | } else if (num_call == 2) { |
| 1210 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1211 | Mock the following response: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1212 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 1213 | <response protocol='3.1'> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1214 | <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'> |
| 1215 | <updatecheck status='ok'> |
| 1216 | <urls> |
| 1217 | <url codebase='http://localhost/download/'/> |
| 1218 | <url codebasediff='http://localhost/download/'/> |
| 1219 | </urls> |
| 1220 | <manifest version='2.0' prodversionmin='11.0.1.0'> |
| 1221 | <packages> |
| 1222 | <package name='ihfokbkgjpifnbbojhneepfflplebdkc_2.crx' |
| 1223 | namediff='ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx' |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1224 | hash_sha256='1af337fbd19c72db0f870753bcd7711c3ae9dcaa |
| 1225 | 0ecde26c262bad942b112990' |
| 1226 | fp='22' |
| 1227 | hashdiff_sha256='73c6e2d4f783fc4ca5481e89e0b8bfce7aec |
sorin | 46bdd0b3 | 2016-08-30 22:42:26 | [diff] [blame] | 1228 | 8ead3686290c94792658ec06f2f2'/> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1229 | </packages> |
| 1230 | </manifest> |
| 1231 | </updatecheck> |
| 1232 | </app> |
| 1233 | </response> |
| 1234 | */ |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1235 | const std::string id = "ihfokbkgjpifnbbojhneepfflplebdkc"; |
| 1236 | EXPECT_EQ(id, ids_to_check[0]); |
| 1237 | EXPECT_EQ(1u, components.count(id)); |
| 1238 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1239 | ProtocolParser::Result::Manifest::Package package; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1240 | package.name = "ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"; |
| 1241 | package.namediff = "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1242 | package.hash_sha256 = |
| 1243 | "1af337fbd19c72db0f870753bcd7711c3ae9dcaa0ecde26c262bad942b112990"; |
| 1244 | package.hashdiff_sha256 = |
| 1245 | "73c6e2d4f783fc4ca5481e89e0b8bfce7aec8ead3686290c94792658ec06f2f2"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1246 | package.fingerprint = "22"; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1247 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1248 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1249 | result.extension_id = id; |
sorin | 9ef301c | 2017-02-16 20:29:17 | [diff] [blame] | 1250 | result.status = "ok"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1251 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 1252 | result.crx_diffurls.push_back(GURL("http://localhost/download/")); |
| 1253 | result.manifest.version = "2.0"; |
| 1254 | result.manifest.browser_min_version = "11.0.1.0"; |
| 1255 | result.manifest.packages.push_back(package); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1256 | |
| 1257 | auto& component = components.at(id); |
| 1258 | component->SetParseResult(result); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1259 | } else { |
| 1260 | NOTREACHED(); |
| 1261 | } |
| 1262 | |
| 1263 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1264 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1265 | } |
| 1266 | }; |
| 1267 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1268 | class MockCrxDownloader : public CrxDownloader { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1269 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 1270 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1271 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 1272 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1273 | return std::make_unique<MockCrxDownloader>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1274 | } |
| 1275 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1276 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1277 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1278 | private: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1279 | void DoStartDownload(const GURL& url) override { |
| 1280 | DownloadMetrics download_metrics; |
| 1281 | FilePath path; |
| 1282 | Result result; |
| 1283 | if (url.path() == "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") { |
| 1284 | download_metrics.url = url; |
| 1285 | download_metrics.downloader = DownloadMetrics::kNone; |
| 1286 | download_metrics.error = 0; |
| 1287 | download_metrics.downloaded_bytes = 53638; |
| 1288 | download_metrics.total_bytes = 53638; |
| 1289 | download_metrics.download_time_ms = 2000; |
| 1290 | |
| 1291 | EXPECT_TRUE(MakeTestFile( |
| 1292 | TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path)); |
| 1293 | |
| 1294 | result.error = 0; |
| 1295 | result.response = path; |
| 1296 | result.downloaded_bytes = 53638; |
| 1297 | result.total_bytes = 53638; |
| 1298 | } else if (url.path() == |
| 1299 | "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx") { |
| 1300 | download_metrics.url = url; |
| 1301 | download_metrics.downloader = DownloadMetrics::kNone; |
| 1302 | download_metrics.error = 0; |
| 1303 | download_metrics.downloaded_bytes = 2105; |
| 1304 | download_metrics.total_bytes = 2105; |
| 1305 | download_metrics.download_time_ms = 1000; |
| 1306 | |
| 1307 | EXPECT_TRUE(MakeTestFile( |
| 1308 | TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"), &path)); |
| 1309 | |
| 1310 | result.error = 0; |
| 1311 | result.response = path; |
| 1312 | result.downloaded_bytes = 2105; |
| 1313 | result.total_bytes = 2105; |
| 1314 | } else { |
| 1315 | NOTREACHED(); |
| 1316 | } |
| 1317 | |
| 1318 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1319 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 1320 | base::Unretained(this), result)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1321 | |
| 1322 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1323 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 1324 | base::Unretained(this), true, result, |
| 1325 | download_metrics)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1326 | } |
| 1327 | }; |
| 1328 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1329 | class MockPingManager : public MockPingManagerImpl { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1330 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1331 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 1332 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 1333 | |
| 1334 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1335 | ~MockPingManager() override { |
| 1336 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1337 | EXPECT_EQ(2u, ping_data.size()); |
| 1338 | EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_data[0].id); |
| 1339 | EXPECT_EQ(base::Version("0.8"), ping_data[0].previous_version); |
| 1340 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
| 1341 | EXPECT_EQ(0, ping_data[0].error_category); |
| 1342 | EXPECT_EQ(0, ping_data[0].error_code); |
| 1343 | EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_data[1].id); |
| 1344 | EXPECT_EQ(base::Version("1.0"), ping_data[1].previous_version); |
| 1345 | EXPECT_EQ(base::Version("2.0"), ping_data[1].next_version); |
| 1346 | EXPECT_FALSE(ping_data[1].diff_update_failed); |
| 1347 | EXPECT_EQ(0, ping_data[1].diff_error_category); |
| 1348 | EXPECT_EQ(0, ping_data[1].diff_error_code); |
| 1349 | EXPECT_EQ(0, ping_data[1].error_category); |
| 1350 | EXPECT_EQ(0, ping_data[1].error_code); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1351 | } |
| 1352 | }; |
| 1353 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1354 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 1355 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1356 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 1357 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1358 | |
| 1359 | MockObserver observer; |
| 1360 | { |
| 1361 | InSequence seq; |
| 1362 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 1363 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1364 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 1365 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1366 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1367 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 1368 | .Times(AtLeast(1)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1369 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 1370 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1371 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 1372 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1373 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 1374 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1375 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 1376 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1377 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1378 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 1379 | .Times(AtLeast(1)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1380 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 1381 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1382 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 1383 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1384 | } |
| 1385 | |
| 1386 | update_client->AddObserver(&observer); |
| 1387 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1388 | const std::vector<std::string> ids = {"ihfokbkgjpifnbbojhneepfflplebdkc"}; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1389 | { |
| 1390 | base::RunLoop runloop; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1391 | update_client->Update(ids, base::BindOnce(&DataCallbackMock::Callback), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 1392 | false, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1393 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1394 | runloop.QuitClosure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1395 | runloop.Run(); |
| 1396 | } |
| 1397 | |
| 1398 | { |
| 1399 | base::RunLoop runloop; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1400 | update_client->Update(ids, base::BindOnce(&DataCallbackMock::Callback), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 1401 | false, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1402 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1403 | runloop.QuitClosure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1404 | runloop.Run(); |
| 1405 | } |
| 1406 | |
| 1407 | update_client->RemoveObserver(&observer); |
| 1408 | } |
| 1409 | |
| 1410 | // Tests the update scenario for one CRX where the CRX installer returns |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 1411 | // an error. Tests that the |unpack_path| argument refers to a valid path |
| 1412 | // then |Install| is called, then tests that the |unpack| path is deleted |
| 1413 | // by the |update_client| code before the test ends. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1414 | TEST_F(UpdateClientTest, OneCrxInstallError) { |
| 1415 | class MockInstaller : public CrxInstaller { |
| 1416 | public: |
| 1417 | MOCK_METHOD1(OnUpdateError, void(int error)); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1418 | MOCK_METHOD2(DoInstall, |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 1419 | void(const base::FilePath& unpack_path, |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 1420 | const Callback& callback)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1421 | MOCK_METHOD2(GetInstalledFile, |
| 1422 | bool(const std::string& file, base::FilePath* installed_file)); |
| 1423 | MOCK_METHOD0(Uninstall, bool()); |
| 1424 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1425 | void Install(const base::FilePath& unpack_path, |
| 1426 | const std::string& public_key, |
Daniel Cheng | 6784852 | 2018-04-27 22:04:41 | [diff] [blame] | 1427 | Callback callback) override { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1428 | DoInstall(unpack_path, std::move(callback)); |
| 1429 | |
Sorin Jianu | 23f70f75 | 2017-05-30 16:21:58 | [diff] [blame] | 1430 | unpack_path_ = unpack_path; |
| 1431 | EXPECT_TRUE(base::DirectoryExists(unpack_path_)); |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 1432 | base::PostTaskWithTraits( |
| 1433 | FROM_HERE, {base::MayBlock()}, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1434 | base::BindOnce(std::move(callback), |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 1435 | CrxInstaller::Result(InstallError::GENERIC_ERROR))); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | protected: |
Sorin Jianu | 23f70f75 | 2017-05-30 16:21:58 | [diff] [blame] | 1439 | ~MockInstaller() override { |
| 1440 | // The unpack path is deleted unconditionally by the component state code, |
| 1441 | // which is driving this installer. Therefore, the unpack path must not |
| 1442 | // exist when this object is destroyed. |
| 1443 | if (!unpack_path_.empty()) |
| 1444 | EXPECT_FALSE(base::DirectoryExists(unpack_path_)); |
| 1445 | } |
| 1446 | |
| 1447 | private: |
| 1448 | // Contains the |unpack_path| argument of the Install call. |
| 1449 | base::FilePath unpack_path_; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1450 | }; |
| 1451 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1452 | class DataCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1453 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1454 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 1455 | const std::vector<std::string>& ids) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1456 | scoped_refptr<MockInstaller> installer = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 1457 | base::MakeRefCounted<MockInstaller>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1458 | |
| 1459 | EXPECT_CALL(*installer, OnUpdateError(_)).Times(0); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1460 | EXPECT_CALL(*installer, DoInstall(_, _)).Times(1); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1461 | EXPECT_CALL(*installer, GetInstalledFile(_, _)).Times(0); |
| 1462 | EXPECT_CALL(*installer, Uninstall()).Times(0); |
| 1463 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1464 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 1465 | crx->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 1466 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1467 | crx->version = base::Version("0.9"); |
| 1468 | crx->installer = installer; |
| 1469 | |
| 1470 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 1471 | component.push_back(std::move(crx)); |
| 1472 | return component; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1473 | } |
| 1474 | }; |
| 1475 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1476 | class CompletionCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1477 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1478 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 1479 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1480 | std::move(quit_closure).Run(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1481 | } |
| 1482 | }; |
| 1483 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1484 | class MockUpdateChecker : public UpdateChecker { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1485 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 1486 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 1487 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 1488 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1489 | return std::make_unique<MockUpdateChecker>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1490 | } |
| 1491 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 1492 | void CheckForUpdates(const std::string& session_id, |
| 1493 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1494 | const IdToComponentPtrMap& components, |
| 1495 | const std::string& additional_attributes, |
| 1496 | bool enabled_component_updates, |
| 1497 | UpdateCheckCallback update_check_callback) override { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1498 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1499 | Mock the following response: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1500 | |
| 1501 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 1502 | <response protocol='3.1'> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1503 | <app appid='jebgalgnebhfojomionfpkfelancnnkf'> |
| 1504 | <updatecheck status='ok'> |
| 1505 | <urls> |
| 1506 | <url codebase='http://localhost/download/'/> |
| 1507 | </urls> |
| 1508 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 1509 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1510 | <package name='jebgalgnebhfojomionfpkfelancnnkf.crx' |
| 1511 | hash_sha256='6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd |
| 1512 | 7c9b12cb7cc067667bde87'/> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1513 | </packages> |
| 1514 | </manifest> |
| 1515 | </updatecheck> |
| 1516 | </app> |
| 1517 | </response> |
| 1518 | */ |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 1519 | EXPECT_FALSE(session_id.empty()); |
| 1520 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1521 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 1522 | EXPECT_EQ(id, ids_to_check[0]); |
| 1523 | EXPECT_EQ(1u, components.count(id)); |
| 1524 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1525 | ProtocolParser::Result::Manifest::Package package; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1526 | package.name = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1527 | package.hash_sha256 = |
| 1528 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1529 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1530 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1531 | result.extension_id = id; |
sorin | 9ef301c | 2017-02-16 20:29:17 | [diff] [blame] | 1532 | result.status = "ok"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1533 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 1534 | result.manifest.version = "1.0"; |
| 1535 | result.manifest.browser_min_version = "11.0.1.0"; |
| 1536 | result.manifest.packages.push_back(package); |
| 1537 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1538 | auto& component = components.at(id); |
| 1539 | component->SetParseResult(result); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1540 | |
| 1541 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1542 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1543 | } |
| 1544 | }; |
| 1545 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1546 | class MockCrxDownloader : public CrxDownloader { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1547 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 1548 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1549 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 1550 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1551 | return std::make_unique<MockCrxDownloader>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1552 | } |
| 1553 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1554 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1555 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1556 | private: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1557 | void DoStartDownload(const GURL& url) override { |
| 1558 | DownloadMetrics download_metrics; |
| 1559 | download_metrics.url = url; |
| 1560 | download_metrics.downloader = DownloadMetrics::kNone; |
| 1561 | download_metrics.error = 0; |
| 1562 | download_metrics.downloaded_bytes = 1843; |
| 1563 | download_metrics.total_bytes = 1843; |
| 1564 | download_metrics.download_time_ms = 1000; |
| 1565 | |
| 1566 | FilePath path; |
| 1567 | EXPECT_TRUE(MakeTestFile( |
| 1568 | TestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), &path)); |
| 1569 | |
| 1570 | Result result; |
| 1571 | result.error = 0; |
| 1572 | result.response = path; |
| 1573 | result.downloaded_bytes = 1843; |
| 1574 | result.total_bytes = 1843; |
| 1575 | |
| 1576 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1577 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1578 | base::Unretained(this), result)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1579 | |
| 1580 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1581 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 1582 | base::Unretained(this), true, result, |
| 1583 | download_metrics)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1584 | } |
| 1585 | }; |
| 1586 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1587 | class MockPingManager : public MockPingManagerImpl { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1588 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1589 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 1590 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 1591 | |
| 1592 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1593 | ~MockPingManager() override { |
| 1594 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1595 | EXPECT_EQ(1u, ping_data.size()); |
| 1596 | EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_data[0].id); |
| 1597 | EXPECT_EQ(base::Version("0.9"), ping_data[0].previous_version); |
| 1598 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 1599 | EXPECT_EQ(3, ping_data[0].error_category); // kInstall. |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1600 | EXPECT_EQ(9, ping_data[0].error_code); // kInstallerError. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1601 | } |
| 1602 | }; |
| 1603 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1604 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 1605 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1606 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 1607 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1608 | |
| 1609 | MockObserver observer; |
| 1610 | { |
| 1611 | InSequence seq; |
| 1612 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 1613 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 1614 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 1615 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 1616 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1617 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 1618 | .Times(AtLeast(1)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1619 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 1620 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 1621 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
| 1622 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 1623 | .Times(1); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | update_client->AddObserver(&observer); |
| 1627 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1628 | std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf"}; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1629 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1630 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 1631 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1632 | |
| 1633 | RunThreads(); |
| 1634 | |
| 1635 | update_client->RemoveObserver(&observer); |
| 1636 | } |
| 1637 | |
| 1638 | // Tests the fallback from differential to full update scenario for one CRX. |
| 1639 | TEST_F(UpdateClientTest, OneCrxDiffUpdateFailsFullUpdateSucceeds) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1640 | class DataCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1641 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1642 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 1643 | const std::vector<std::string>& ids) { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1644 | static int num_calls = 0; |
| 1645 | |
| 1646 | // Must use the same stateful installer object. |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1647 | static scoped_refptr<CrxInstaller> installer = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 1648 | base::MakeRefCounted<VersionedTestInstaller>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1649 | |
| 1650 | ++num_calls; |
| 1651 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1652 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 1653 | crx->name = "test_ihfo"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 1654 | crx->pk_hash.assign(ihfo_hash, ihfo_hash + base::size(ihfo_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1655 | crx->installer = installer; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1656 | if (num_calls == 1) { |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1657 | crx->version = base::Version("0.8"); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1658 | } else if (num_calls == 2) { |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1659 | crx->version = base::Version("1.0"); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1660 | } else { |
| 1661 | NOTREACHED(); |
| 1662 | } |
| 1663 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1664 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 1665 | component.push_back(std::move(crx)); |
| 1666 | return component; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1667 | } |
| 1668 | }; |
| 1669 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1670 | class CompletionCallbackMock { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1671 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1672 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 1673 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1674 | std::move(quit_closure).Run(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1675 | } |
| 1676 | }; |
| 1677 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1678 | class MockUpdateChecker : public UpdateChecker { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1679 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 1680 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 1681 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 1682 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1683 | return std::make_unique<MockUpdateChecker>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1684 | } |
| 1685 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 1686 | void CheckForUpdates(const std::string& session_id, |
| 1687 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1688 | const IdToComponentPtrMap& components, |
| 1689 | const std::string& additional_attributes, |
| 1690 | bool enabled_component_updates, |
| 1691 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 1692 | EXPECT_FALSE(session_id.empty()); |
| 1693 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1694 | static int num_call = 0; |
| 1695 | ++num_call; |
| 1696 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1697 | ProtocolParser::Results results; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1698 | |
| 1699 | if (num_call == 1) { |
| 1700 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1701 | Mock the following response: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1702 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 1703 | <response protocol='3.1'> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1704 | <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'> |
| 1705 | <updatecheck status='ok'> |
| 1706 | <urls> |
| 1707 | <url codebase='http://localhost/download/'/> |
| 1708 | </urls> |
| 1709 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 1710 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1711 | <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx' |
| 1712 | hash_sha256='813c59747e139a608b3b5fc49633affc6db57437 |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1713 | 3f309f156ea6d27229c0b3f9' |
| 1714 | fp='1'/> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1715 | </packages> |
| 1716 | </manifest> |
| 1717 | </updatecheck> |
| 1718 | </app> |
| 1719 | </response> |
| 1720 | */ |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1721 | const std::string id = "ihfokbkgjpifnbbojhneepfflplebdkc"; |
| 1722 | EXPECT_EQ(id, ids_to_check[0]); |
| 1723 | EXPECT_EQ(1u, components.count(id)); |
| 1724 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1725 | ProtocolParser::Result::Manifest::Package package; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1726 | package.name = "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1727 | package.hash_sha256 = |
| 1728 | "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1729 | package.fingerprint = "1"; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1730 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1731 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1732 | result.extension_id = id; |
sorin | 9ef301c | 2017-02-16 20:29:17 | [diff] [blame] | 1733 | result.status = "ok"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1734 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 1735 | result.manifest.version = "1.0"; |
| 1736 | result.manifest.browser_min_version = "11.0.1.0"; |
| 1737 | result.manifest.packages.push_back(package); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1738 | |
| 1739 | auto& component = components.at(id); |
| 1740 | component->SetParseResult(result); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1741 | } else if (num_call == 2) { |
| 1742 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1743 | Mock the following response: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1744 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 1745 | <response protocol='3.1'> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1746 | <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'> |
| 1747 | <updatecheck status='ok'> |
| 1748 | <urls> |
| 1749 | <url codebase='http://localhost/download/'/> |
| 1750 | <url codebasediff='http://localhost/download/'/> |
| 1751 | </urls> |
| 1752 | <manifest version='2.0' prodversionmin='11.0.1.0'> |
| 1753 | <packages> |
| 1754 | <package name='ihfokbkgjpifnbbojhneepfflplebdkc_2.crx' |
| 1755 | namediff='ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx' |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1756 | hash_sha256='1af337fbd19c72db0f870753bcd7711c3ae9dcaa |
| 1757 | 0ecde26c262bad942b112990' |
| 1758 | fp='22' |
| 1759 | hashdiff_sha256='73c6e2d4f783fc4ca5481e89e0b8bfce7aec |
sorin | 46bdd0b3 | 2016-08-30 22:42:26 | [diff] [blame] | 1760 | 8ead3686290c94792658ec06f2f2'/> |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1761 | </packages> |
| 1762 | </manifest> |
| 1763 | </updatecheck> |
| 1764 | </app> |
| 1765 | </response> |
| 1766 | */ |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1767 | const std::string id = "ihfokbkgjpifnbbojhneepfflplebdkc"; |
| 1768 | EXPECT_EQ(id, ids_to_check[0]); |
| 1769 | EXPECT_EQ(1u, components.count(id)); |
| 1770 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1771 | ProtocolParser::Result::Manifest::Package package; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1772 | package.name = "ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"; |
| 1773 | package.namediff = "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 1774 | package.hash_sha256 = |
| 1775 | "1af337fbd19c72db0f870753bcd7711c3ae9dcaa0ecde26c262bad942b112990"; |
| 1776 | package.hashdiff_sha256 = |
| 1777 | "73c6e2d4f783fc4ca5481e89e0b8bfce7aec8ead3686290c94792658ec06f2f2"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1778 | package.fingerprint = "22"; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1779 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 1780 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1781 | result.extension_id = id; |
sorin | 9ef301c | 2017-02-16 20:29:17 | [diff] [blame] | 1782 | result.status = "ok"; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1783 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 1784 | result.crx_diffurls.push_back(GURL("http://localhost/download/")); |
| 1785 | result.manifest.version = "2.0"; |
| 1786 | result.manifest.browser_min_version = "11.0.1.0"; |
| 1787 | result.manifest.packages.push_back(package); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1788 | |
| 1789 | auto& component = components.at(id); |
| 1790 | component->SetParseResult(result); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1791 | } else { |
| 1792 | NOTREACHED(); |
| 1793 | } |
| 1794 | |
| 1795 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1796 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1797 | } |
| 1798 | }; |
| 1799 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1800 | class MockCrxDownloader : public CrxDownloader { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1801 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 1802 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1803 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 1804 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1805 | return std::make_unique<MockCrxDownloader>(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1806 | } |
| 1807 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1808 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1809 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1810 | private: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1811 | void DoStartDownload(const GURL& url) override { |
| 1812 | DownloadMetrics download_metrics; |
| 1813 | FilePath path; |
| 1814 | Result result; |
| 1815 | if (url.path() == "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") { |
| 1816 | download_metrics.url = url; |
| 1817 | download_metrics.downloader = DownloadMetrics::kNone; |
| 1818 | download_metrics.error = 0; |
| 1819 | download_metrics.downloaded_bytes = 53638; |
| 1820 | download_metrics.total_bytes = 53638; |
| 1821 | download_metrics.download_time_ms = 2000; |
| 1822 | |
| 1823 | EXPECT_TRUE(MakeTestFile( |
| 1824 | TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path)); |
| 1825 | |
| 1826 | result.error = 0; |
| 1827 | result.response = path; |
| 1828 | result.downloaded_bytes = 53638; |
| 1829 | result.total_bytes = 53638; |
| 1830 | } else if (url.path() == |
| 1831 | "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx") { |
| 1832 | // A download error is injected on this execution path. |
| 1833 | download_metrics.url = url; |
| 1834 | download_metrics.downloader = DownloadMetrics::kNone; |
| 1835 | download_metrics.error = -1; |
| 1836 | download_metrics.downloaded_bytes = 0; |
| 1837 | download_metrics.total_bytes = 2105; |
| 1838 | download_metrics.download_time_ms = 1000; |
| 1839 | |
Sorin Jianu | 52de5fa | 2017-10-09 23:19:33 | [diff] [blame] | 1840 | // The response must not include a file path in the case of errors. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1841 | result.error = -1; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1842 | result.downloaded_bytes = 0; |
| 1843 | result.total_bytes = 2105; |
| 1844 | } else if (url.path() == |
| 1845 | "/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx") { |
| 1846 | download_metrics.url = url; |
| 1847 | download_metrics.downloader = DownloadMetrics::kNone; |
| 1848 | download_metrics.error = 0; |
| 1849 | download_metrics.downloaded_bytes = 53855; |
| 1850 | download_metrics.total_bytes = 53855; |
| 1851 | download_metrics.download_time_ms = 1000; |
| 1852 | |
| 1853 | EXPECT_TRUE(MakeTestFile( |
| 1854 | TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"), &path)); |
| 1855 | |
| 1856 | result.error = 0; |
| 1857 | result.response = path; |
| 1858 | result.downloaded_bytes = 53855; |
| 1859 | result.total_bytes = 53855; |
| 1860 | } |
| 1861 | |
| 1862 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1863 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 1864 | base::Unretained(this), result)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1865 | |
| 1866 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1867 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 1868 | base::Unretained(this), true, result, |
| 1869 | download_metrics)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1870 | } |
| 1871 | }; |
| 1872 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1873 | class MockPingManager : public MockPingManagerImpl { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1874 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1875 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 1876 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 1877 | |
| 1878 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1879 | ~MockPingManager() override { |
| 1880 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1881 | EXPECT_EQ(2u, ping_data.size()); |
| 1882 | EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_data[0].id); |
| 1883 | EXPECT_EQ(base::Version("0.8"), ping_data[0].previous_version); |
| 1884 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
| 1885 | EXPECT_EQ(0, ping_data[0].error_category); |
| 1886 | EXPECT_EQ(0, ping_data[0].error_code); |
| 1887 | EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_data[1].id); |
| 1888 | EXPECT_EQ(base::Version("1.0"), ping_data[1].previous_version); |
| 1889 | EXPECT_EQ(base::Version("2.0"), ping_data[1].next_version); |
| 1890 | EXPECT_EQ(0, ping_data[1].error_category); |
| 1891 | EXPECT_EQ(0, ping_data[1].error_code); |
| 1892 | EXPECT_TRUE(ping_data[1].diff_update_failed); |
| 1893 | EXPECT_EQ(1, ping_data[1].diff_error_category); // kNetworkError. |
| 1894 | EXPECT_EQ(-1, ping_data[1].diff_error_code); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1895 | } |
| 1896 | }; |
| 1897 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1898 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 1899 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1900 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 1901 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1902 | |
| 1903 | MockObserver observer; |
| 1904 | { |
| 1905 | InSequence seq; |
| 1906 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 1907 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1908 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 1909 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1910 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1911 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 1912 | .Times(AtLeast(1)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1913 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 1914 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1915 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 1916 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1917 | |
| 1918 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 1919 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1920 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 1921 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1922 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1923 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 1924 | .Times(AtLeast(1)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1925 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 1926 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1927 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 1928 | "ihfokbkgjpifnbbojhneepfflplebdkc")).Times(1); |
| 1929 | } |
| 1930 | |
| 1931 | update_client->AddObserver(&observer); |
| 1932 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 1933 | const std::vector<std::string> ids = {"ihfokbkgjpifnbbojhneepfflplebdkc"}; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1934 | |
| 1935 | { |
| 1936 | base::RunLoop runloop; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1937 | update_client->Update(ids, base::BindOnce(&DataCallbackMock::Callback), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 1938 | false, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1939 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1940 | runloop.QuitClosure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1941 | runloop.Run(); |
| 1942 | } |
| 1943 | |
| 1944 | { |
| 1945 | base::RunLoop runloop; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1946 | update_client->Update(ids, base::BindOnce(&DataCallbackMock::Callback), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 1947 | false, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1948 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1949 | runloop.QuitClosure())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 1950 | runloop.Run(); |
| 1951 | } |
| 1952 | |
| 1953 | update_client->RemoveObserver(&observer); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | // Tests the queuing of update checks. In this scenario, two update checks are |
| 1957 | // done for one CRX. The second update check call is queued up and will run |
| 1958 | // after the first check has completed. The CRX has no updates. |
| 1959 | TEST_F(UpdateClientTest, OneCrxNoUpdateQueuedCall) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1960 | class DataCallbackMock { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1961 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1962 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 1963 | const std::vector<std::string>& ids) { |
| 1964 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 1965 | crx->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 1966 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 1967 | crx->version = base::Version("0.9"); |
| 1968 | crx->installer = base::MakeRefCounted<TestInstaller>(); |
| 1969 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 1970 | component.push_back(std::move(crx)); |
| 1971 | return component; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1972 | } |
| 1973 | }; |
| 1974 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1975 | class CompletionCallbackMock { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1976 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1977 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1978 | static int num_call = 0; |
| 1979 | ++num_call; |
| 1980 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 1981 | EXPECT_EQ(Error::NONE, error); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1982 | |
| 1983 | if (num_call == 2) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1984 | std::move(quit_closure).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1985 | } |
| 1986 | }; |
| 1987 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1988 | class MockUpdateChecker : public UpdateChecker { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1989 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 1990 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 1991 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 1992 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 1993 | return std::make_unique<MockUpdateChecker>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 1994 | } |
| 1995 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 1996 | void CheckForUpdates(const std::string& session_id, |
| 1997 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 1998 | const IdToComponentPtrMap& components, |
| 1999 | const std::string& additional_attributes, |
| 2000 | bool enabled_component_updates, |
| 2001 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2002 | EXPECT_FALSE(session_id.empty()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2003 | EXPECT_TRUE(enabled_component_updates); |
| 2004 | EXPECT_EQ(1u, ids_to_check.size()); |
| 2005 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 2006 | EXPECT_EQ(id, ids_to_check.front()); |
| 2007 | EXPECT_EQ(1u, components.count(id)); |
| 2008 | |
| 2009 | auto& component = components.at(id); |
| 2010 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 2011 | EXPECT_FALSE(component->is_foreground()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2012 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2013 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2014 | result.extension_id = id; |
| 2015 | result.status = "noupdate"; |
| 2016 | component->SetParseResult(result); |
| 2017 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2018 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2019 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2020 | } |
| 2021 | }; |
| 2022 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2023 | class MockCrxDownloader : public CrxDownloader { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2024 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2025 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2026 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 2027 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2028 | return std::make_unique<MockCrxDownloader>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2029 | } |
| 2030 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2031 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2032 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2033 | private: |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2034 | void DoStartDownload(const GURL& url) override { EXPECT_TRUE(false); } |
| 2035 | }; |
| 2036 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2037 | class MockPingManager : public MockPingManagerImpl { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2038 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2039 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2040 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2041 | |
| 2042 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2043 | ~MockPingManager() override { EXPECT_TRUE(ping_data().empty()); } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2044 | }; |
| 2045 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2046 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 2047 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2048 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 2049 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2050 | |
| 2051 | MockObserver observer; |
| 2052 | InSequence seq; |
| 2053 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2054 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2055 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 2056 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2057 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2058 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2059 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 2060 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2061 | |
| 2062 | update_client->AddObserver(&observer); |
| 2063 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2064 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf"}; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2065 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2066 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 2067 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2068 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2069 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 2070 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2071 | |
| 2072 | RunThreads(); |
| 2073 | |
| 2074 | update_client->RemoveObserver(&observer); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | // Tests the install of one CRX. |
| 2078 | TEST_F(UpdateClientTest, OneCrxInstall) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2079 | class DataCallbackMock { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2080 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2081 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 2082 | const std::vector<std::string>& ids) { |
| 2083 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 2084 | crx->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 2085 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2086 | crx->version = base::Version("0.0"); |
| 2087 | crx->installer = base::MakeRefCounted<TestInstaller>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2088 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2089 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 2090 | component.push_back(std::move(crx)); |
| 2091 | return component; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2092 | } |
| 2093 | }; |
| 2094 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2095 | class CompletionCallbackMock { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2096 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2097 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2098 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2099 | std::move(quit_closure).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2100 | } |
| 2101 | }; |
| 2102 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2103 | class MockUpdateChecker : public UpdateChecker { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2104 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2105 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 2106 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 2107 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2108 | return std::make_unique<MockUpdateChecker>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2109 | } |
| 2110 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2111 | void CheckForUpdates(const std::string& session_id, |
| 2112 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2113 | const IdToComponentPtrMap& components, |
| 2114 | const std::string& additional_attributes, |
| 2115 | bool enabled_component_updates, |
| 2116 | UpdateCheckCallback update_check_callback) override { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2117 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2118 | Mock the following response: |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2119 | |
| 2120 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 2121 | <response protocol='3.1'> |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2122 | <app appid='jebgalgnebhfojomionfpkfelancnnkf'> |
| 2123 | <updatecheck status='ok'> |
| 2124 | <urls> |
| 2125 | <url codebase='http://localhost/download/'/> |
| 2126 | </urls> |
| 2127 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 2128 | <packages> |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 2129 | <package name='jebgalgnebhfojomionfpkfelancnnkf.crx' |
| 2130 | hash_sha256='6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd |
| 2131 | 7c9b12cb7cc067667bde87'/> |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2132 | </packages> |
| 2133 | </manifest> |
| 2134 | </updatecheck> |
| 2135 | </app> |
| 2136 | </response> |
| 2137 | */ |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2138 | EXPECT_FALSE(session_id.empty()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2139 | EXPECT_TRUE(enabled_component_updates); |
| 2140 | EXPECT_EQ(1u, ids_to_check.size()); |
| 2141 | |
| 2142 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 2143 | EXPECT_EQ(id, ids_to_check[0]); |
| 2144 | EXPECT_EQ(1u, components.count(id)); |
| 2145 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2146 | ProtocolParser::Result::Manifest::Package package; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2147 | package.name = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 2148 | package.hash_sha256 = |
| 2149 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2150 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2151 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2152 | result.extension_id = id; |
sorin | 9ef301c | 2017-02-16 20:29:17 | [diff] [blame] | 2153 | result.status = "ok"; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2154 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 2155 | result.manifest.version = "1.0"; |
| 2156 | result.manifest.browser_min_version = "11.0.1.0"; |
| 2157 | result.manifest.packages.push_back(package); |
| 2158 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2159 | auto& component = components.at(id); |
| 2160 | component->SetParseResult(result); |
| 2161 | |
| 2162 | // Verify that calling Install sets ondemand. |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 2163 | EXPECT_TRUE(component->is_foreground()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2164 | |
| 2165 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2166 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2167 | } |
| 2168 | }; |
| 2169 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2170 | class MockCrxDownloader : public CrxDownloader { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2171 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2172 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2173 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 2174 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2175 | return std::make_unique<MockCrxDownloader>(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2176 | } |
| 2177 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2178 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2179 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2180 | private: |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2181 | void DoStartDownload(const GURL& url) override { |
| 2182 | DownloadMetrics download_metrics; |
| 2183 | FilePath path; |
| 2184 | Result result; |
| 2185 | if (url.path() == "/download/jebgalgnebhfojomionfpkfelancnnkf.crx") { |
| 2186 | download_metrics.url = url; |
| 2187 | download_metrics.downloader = DownloadMetrics::kNone; |
| 2188 | download_metrics.error = 0; |
| 2189 | download_metrics.downloaded_bytes = 1843; |
| 2190 | download_metrics.total_bytes = 1843; |
| 2191 | download_metrics.download_time_ms = 1000; |
| 2192 | |
| 2193 | EXPECT_TRUE(MakeTestFile( |
| 2194 | TestFilePath("jebgalgnebhfojomionfpkfelancnnkf.crx"), &path)); |
| 2195 | |
| 2196 | result.error = 0; |
| 2197 | result.response = path; |
| 2198 | result.downloaded_bytes = 1843; |
| 2199 | result.total_bytes = 1843; |
| 2200 | } else { |
| 2201 | NOTREACHED(); |
| 2202 | } |
| 2203 | |
| 2204 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2205 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 2206 | base::Unretained(this), result)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2207 | |
| 2208 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2209 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 2210 | base::Unretained(this), true, result, |
| 2211 | download_metrics)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2212 | } |
| 2213 | }; |
| 2214 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2215 | class MockPingManager : public MockPingManagerImpl { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2216 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2217 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2218 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2219 | |
| 2220 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2221 | ~MockPingManager() override { |
| 2222 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2223 | EXPECT_EQ(1u, ping_data.size()); |
| 2224 | EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_data[0].id); |
| 2225 | EXPECT_EQ(base::Version("0.0"), ping_data[0].previous_version); |
| 2226 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
| 2227 | EXPECT_EQ(0, ping_data[0].error_category); |
| 2228 | EXPECT_EQ(0, ping_data[0].error_code); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2229 | } |
| 2230 | }; |
| 2231 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2232 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 2233 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2234 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 2235 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2236 | |
| 2237 | MockObserver observer; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2238 | InSequence seq; |
| 2239 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2240 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2241 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 2242 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2243 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2244 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2245 | .Times(AtLeast(1)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2246 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 2247 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2248 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 2249 | "jebgalgnebhfojomionfpkfelancnnkf")).Times(1); |
| 2250 | |
| 2251 | update_client->AddObserver(&observer); |
| 2252 | |
| 2253 | update_client->Install( |
| 2254 | std::string("jebgalgnebhfojomionfpkfelancnnkf"), |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2255 | base::BindOnce(&DataCallbackMock::Callback), |
| 2256 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 2257 | |
| 2258 | RunThreads(); |
| 2259 | |
| 2260 | update_client->RemoveObserver(&observer); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 2261 | } |
| 2262 | |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 2263 | // Tests the install of one CRX when no component data is provided. This |
| 2264 | // results in an install error. |
| 2265 | TEST_F(UpdateClientTest, OneCrxInstallNoCrxComponentData) { |
| 2266 | class DataCallbackMock { |
| 2267 | public: |
| 2268 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 2269 | const std::vector<std::string>& ids) { |
| 2270 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 2271 | component.push_back(nullptr); |
| 2272 | return component; |
| 2273 | } |
| 2274 | }; |
| 2275 | |
| 2276 | class CompletionCallbackMock { |
| 2277 | public: |
| 2278 | static void Callback(base::OnceClosure quit_closure, Error error) { |
| 2279 | EXPECT_EQ(Error::NONE, error); |
| 2280 | std::move(quit_closure).Run(); |
| 2281 | } |
| 2282 | }; |
| 2283 | |
| 2284 | class MockUpdateChecker : public UpdateChecker { |
| 2285 | public: |
| 2286 | static std::unique_ptr<UpdateChecker> Create( |
| 2287 | scoped_refptr<Configurator> config, |
| 2288 | PersistedData* metadata) { |
| 2289 | return std::make_unique<MockUpdateChecker>(); |
| 2290 | } |
| 2291 | |
| 2292 | void CheckForUpdates(const std::string& session_id, |
| 2293 | const std::vector<std::string>& ids_to_check, |
| 2294 | const IdToComponentPtrMap& components, |
| 2295 | const std::string& additional_attributes, |
| 2296 | bool enabled_component_updates, |
| 2297 | UpdateCheckCallback update_check_callback) override { |
| 2298 | NOTREACHED(); |
| 2299 | } |
| 2300 | }; |
| 2301 | |
| 2302 | class MockCrxDownloader : public CrxDownloader { |
| 2303 | public: |
| 2304 | static std::unique_ptr<CrxDownloader> Create( |
| 2305 | bool is_background_download, |
| 2306 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
| 2307 | return std::make_unique<MockCrxDownloader>(); |
| 2308 | } |
| 2309 | |
| 2310 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
| 2311 | |
| 2312 | private: |
| 2313 | void DoStartDownload(const GURL& url) override { NOTREACHED(); } |
| 2314 | }; |
| 2315 | |
| 2316 | class MockPingManager : public MockPingManagerImpl { |
| 2317 | public: |
| 2318 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2319 | : MockPingManagerImpl(config) {} |
| 2320 | |
| 2321 | protected: |
| 2322 | ~MockPingManager() override { |
| 2323 | EXPECT_EQ(0u, MockPingManagerImpl::ping_data().size()); |
| 2324 | } |
| 2325 | }; |
| 2326 | |
| 2327 | scoped_refptr<UpdateClient> update_client = |
| 2328 | base::MakeRefCounted<UpdateClientImpl>( |
| 2329 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 2330 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
| 2331 | |
| 2332 | MockObserver observer; |
| 2333 | InSequence seq; |
| 2334 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
| 2335 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2336 | .Times(1); |
| 2337 | |
| 2338 | update_client->AddObserver(&observer); |
| 2339 | |
| 2340 | update_client->Install( |
| 2341 | std::string("jebgalgnebhfojomionfpkfelancnnkf"), |
| 2342 | base::BindOnce(&DataCallbackMock::Callback), |
| 2343 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
| 2344 | |
| 2345 | RunThreads(); |
| 2346 | |
| 2347 | update_client->RemoveObserver(&observer); |
| 2348 | } |
| 2349 | |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2350 | // Tests that overlapping installs of the same CRX result in an error. |
| 2351 | TEST_F(UpdateClientTest, ConcurrentInstallSameCRX) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2352 | class DataCallbackMock { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2353 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2354 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 2355 | const std::vector<std::string>& ids) { |
| 2356 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 2357 | crx->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 2358 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2359 | crx->version = base::Version("0.0"); |
| 2360 | crx->installer = base::MakeRefCounted<TestInstaller>(); |
| 2361 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 2362 | component.push_back(std::move(crx)); |
| 2363 | return component; |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2364 | } |
| 2365 | }; |
| 2366 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2367 | class CompletionCallbackMock { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2368 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2369 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2370 | static int num_call = 0; |
| 2371 | ++num_call; |
| 2372 | |
| 2373 | EXPECT_LE(num_call, 2); |
| 2374 | |
| 2375 | if (num_call == 1) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2376 | EXPECT_EQ(Error::UPDATE_IN_PROGRESS, error); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2377 | return; |
| 2378 | } |
| 2379 | if (num_call == 2) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2380 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2381 | std::move(quit_closure).Run(); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | }; |
| 2385 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2386 | class MockUpdateChecker : public UpdateChecker { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2387 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2388 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 2389 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 2390 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2391 | return std::make_unique<MockUpdateChecker>(); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2392 | } |
| 2393 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2394 | void CheckForUpdates(const std::string& session_id, |
| 2395 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2396 | const IdToComponentPtrMap& components, |
| 2397 | const std::string& additional_attributes, |
| 2398 | bool enabled_component_updates, |
| 2399 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2400 | EXPECT_FALSE(session_id.empty()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2401 | EXPECT_TRUE(enabled_component_updates); |
| 2402 | EXPECT_EQ(1u, ids_to_check.size()); |
| 2403 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 2404 | EXPECT_EQ(id, ids_to_check.front()); |
| 2405 | EXPECT_EQ(1u, components.count(id)); |
| 2406 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2407 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2408 | result.extension_id = id; |
| 2409 | result.status = "noupdate"; |
| 2410 | |
| 2411 | auto& component = components.at(id); |
| 2412 | component->SetParseResult(result); |
| 2413 | |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 2414 | // Verify that calling Install sets |is_foreground| for the component. |
| 2415 | EXPECT_TRUE(component->is_foreground()); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2416 | |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2417 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2418 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2419 | } |
| 2420 | }; |
| 2421 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2422 | class MockCrxDownloader : public CrxDownloader { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2423 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2424 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2425 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 2426 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2427 | return std::make_unique<MockCrxDownloader>(); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2428 | } |
| 2429 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2430 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2431 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2432 | private: |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2433 | void DoStartDownload(const GURL& url) override { EXPECT_TRUE(false); } |
| 2434 | }; |
| 2435 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2436 | class MockPingManager : public MockPingManagerImpl { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2437 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2438 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2439 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2440 | |
| 2441 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2442 | ~MockPingManager() override { EXPECT_TRUE(ping_data().empty()); } |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2443 | }; |
| 2444 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2445 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 2446 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2447 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 2448 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2449 | |
| 2450 | MockObserver observer; |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2451 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2452 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2453 | .Times(1); |
| 2454 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 2455 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2456 | .Times(1); |
| 2457 | |
| 2458 | update_client->AddObserver(&observer); |
| 2459 | |
| 2460 | update_client->Install( |
| 2461 | std::string("jebgalgnebhfojomionfpkfelancnnkf"), |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2462 | base::BindOnce(&DataCallbackMock::Callback), |
| 2463 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2464 | |
| 2465 | update_client->Install( |
| 2466 | std::string("jebgalgnebhfojomionfpkfelancnnkf"), |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2467 | base::BindOnce(&DataCallbackMock::Callback), |
| 2468 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2469 | |
| 2470 | RunThreads(); |
| 2471 | |
| 2472 | update_client->RemoveObserver(&observer); |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 2473 | } |
| 2474 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2475 | // Tests that UpdateClient::Update returns Error::INVALID_ARGUMENT when |
| 2476 | // the |ids| parameter is empty. |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2477 | TEST_F(UpdateClientTest, EmptyIdList) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2478 | class DataCallbackMock { |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2479 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2480 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 2481 | const std::vector<std::string>& ids) { |
| 2482 | return {}; |
| 2483 | } |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2484 | }; |
| 2485 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2486 | class CompletionCallbackMock { |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2487 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2488 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2489 | DCHECK_EQ(Error::INVALID_ARGUMENT, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2490 | std::move(quit_closure).Run(); |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2491 | } |
| 2492 | }; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2493 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2494 | class MockUpdateChecker : public UpdateChecker { |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2495 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2496 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 2497 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 2498 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2499 | return std::make_unique<MockUpdateChecker>(); |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2500 | } |
| 2501 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2502 | void CheckForUpdates(const std::string& session_id, |
| 2503 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2504 | const IdToComponentPtrMap& components, |
| 2505 | const std::string& additional_attributes, |
| 2506 | bool enabled_component_updates, |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2507 | UpdateCheckCallback update_check_callback) override { |
| 2508 | NOTREACHED(); |
| 2509 | } |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2510 | }; |
| 2511 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2512 | class MockCrxDownloader : public CrxDownloader { |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2513 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2514 | static std::unique_ptr<CrxDownloader> Create( |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2515 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 2516 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2517 | return std::make_unique<MockCrxDownloader>(); |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2518 | } |
| 2519 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2520 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2521 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2522 | private: |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2523 | void DoStartDownload(const GURL& url) override { EXPECT_TRUE(false); } |
| 2524 | }; |
| 2525 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2526 | class MockPingManager : public MockPingManagerImpl { |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2527 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2528 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2529 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2530 | |
| 2531 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2532 | ~MockPingManager() override { EXPECT_TRUE(ping_data().empty()); } |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2533 | }; |
| 2534 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2535 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 2536 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2537 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 2538 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2539 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2540 | const std::vector<std::string> empty_id_list; |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2541 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2542 | empty_id_list, base::BindOnce(&DataCallbackMock::Callback), false, |
| 2543 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2544 | RunThreads(); |
asargent | e90363b | 2015-09-09 22:40:07 | [diff] [blame] | 2545 | } |
| 2546 | |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2547 | TEST_F(UpdateClientTest, SendUninstallPing) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2548 | class CompletionCallbackMock { |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 2549 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2550 | static void Callback(base::OnceClosure quit_closure, Error error) { |
| 2551 | std::move(quit_closure).Run(); |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 2552 | } |
| 2553 | }; |
| 2554 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2555 | class MockUpdateChecker : public UpdateChecker { |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2556 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2557 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 2558 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 2559 | PersistedData* metadata) { |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2560 | return nullptr; |
| 2561 | } |
| 2562 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2563 | void CheckForUpdates(const std::string& session_id, |
| 2564 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2565 | const IdToComponentPtrMap& components, |
| 2566 | const std::string& additional_attributes, |
| 2567 | bool enabled_component_updates, |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2568 | UpdateCheckCallback update_check_callback) override { |
| 2569 | NOTREACHED(); |
| 2570 | } |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2571 | }; |
| 2572 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2573 | class MockCrxDownloader : public CrxDownloader { |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2574 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2575 | static std::unique_ptr<CrxDownloader> Create( |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2576 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 2577 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2578 | return nullptr; |
| 2579 | } |
| 2580 | |
| 2581 | private: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2582 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
| 2583 | ~MockCrxDownloader() override {} |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2584 | |
| 2585 | void DoStartDownload(const GURL& url) override {} |
| 2586 | }; |
| 2587 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2588 | class MockPingManager : public MockPingManagerImpl { |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2589 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2590 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2591 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2592 | |
| 2593 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2594 | ~MockPingManager() override { |
| 2595 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2596 | EXPECT_EQ(1u, ping_data.size()); |
| 2597 | EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_data[0].id); |
Minh X. Nguyen | 01d3586d | 2018-01-03 21:53:39 | [diff] [blame] | 2598 | EXPECT_EQ(base::Version("1.2.3.4"), ping_data[0].previous_version); |
| 2599 | EXPECT_EQ(base::Version("0"), ping_data[0].next_version); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2600 | EXPECT_EQ(10, ping_data[0].extra_code1); |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2601 | } |
| 2602 | }; |
| 2603 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2604 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 2605 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2606 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 2607 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2608 | |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 2609 | update_client->SendUninstallPing( |
Minh X. Nguyen | 01d3586d | 2018-01-03 21:53:39 | [diff] [blame] | 2610 | "jebgalgnebhfojomionfpkfelancnnkf", base::Version("1.2.3.4"), 10, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2611 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 2612 | |
| 2613 | RunThreads(); |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 2614 | } |
| 2615 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2616 | TEST_F(UpdateClientTest, RetryAfter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2617 | class DataCallbackMock { |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2618 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2619 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 2620 | const std::vector<std::string>& ids) { |
| 2621 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 2622 | crx->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 2623 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2624 | crx->version = base::Version("0.9"); |
| 2625 | crx->installer = base::MakeRefCounted<TestInstaller>(); |
| 2626 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 2627 | component.push_back(std::move(crx)); |
| 2628 | return component; |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2629 | } |
| 2630 | }; |
| 2631 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2632 | class CompletionCallbackMock { |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2633 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2634 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2635 | static int num_call = 0; |
| 2636 | ++num_call; |
| 2637 | |
| 2638 | EXPECT_LE(num_call, 4); |
| 2639 | |
| 2640 | if (num_call == 1) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2641 | EXPECT_EQ(Error::NONE, error); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2642 | } else if (num_call == 2) { |
| 2643 | // This request is throttled since the update engine received a |
| 2644 | // positive |retry_after_sec| value in the update check response. |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2645 | EXPECT_EQ(Error::RETRY_LATER, error); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2646 | } else if (num_call == 3) { |
| 2647 | // This request is a foreground Install, which is never throttled. |
| 2648 | // The update engine received a |retry_after_sec| value of 0, which |
| 2649 | // resets the throttling. |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2650 | EXPECT_EQ(Error::NONE, error); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2651 | } else if (num_call == 4) { |
| 2652 | // This request succeeds since there is no throttling in effect. |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2653 | EXPECT_EQ(Error::NONE, error); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2654 | } |
| 2655 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2656 | std::move(quit_closure).Run(); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2657 | } |
| 2658 | }; |
| 2659 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2660 | class MockUpdateChecker : public UpdateChecker { |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2661 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2662 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 2663 | scoped_refptr<Configurator> config, |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 2664 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2665 | return std::make_unique<MockUpdateChecker>(); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2666 | } |
| 2667 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2668 | void CheckForUpdates(const std::string& session_id, |
| 2669 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2670 | const IdToComponentPtrMap& components, |
| 2671 | const std::string& additional_attributes, |
| 2672 | bool enabled_component_updates, |
| 2673 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2674 | EXPECT_FALSE(session_id.empty()); |
| 2675 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2676 | static int num_call = 0; |
| 2677 | ++num_call; |
| 2678 | |
| 2679 | EXPECT_LE(num_call, 3); |
| 2680 | |
| 2681 | int retry_after_sec(0); |
| 2682 | if (num_call == 1) { |
| 2683 | // Throttle the next call. |
| 2684 | retry_after_sec = 60 * 60; // 1 hour. |
| 2685 | } |
| 2686 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2687 | EXPECT_EQ(1u, ids_to_check.size()); |
| 2688 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 2689 | EXPECT_EQ(id, ids_to_check.front()); |
| 2690 | EXPECT_EQ(1u, components.count(id)); |
| 2691 | |
| 2692 | auto& component = components.at(id); |
| 2693 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2694 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2695 | result.extension_id = id; |
| 2696 | result.status = "noupdate"; |
| 2697 | component->SetParseResult(result); |
| 2698 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2699 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2700 | FROM_HERE, |
| 2701 | base::BindOnce(std::move(update_check_callback), 0, retry_after_sec)); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2702 | } |
| 2703 | }; |
| 2704 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2705 | class MockCrxDownloader : public CrxDownloader { |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2706 | public: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 2707 | static std::unique_ptr<CrxDownloader> Create( |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2708 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 2709 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2710 | return std::make_unique<MockCrxDownloader>(); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2711 | } |
| 2712 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2713 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2714 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2715 | private: |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2716 | void DoStartDownload(const GURL& url) override { EXPECT_TRUE(false); } |
| 2717 | }; |
| 2718 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2719 | class MockPingManager : public MockPingManagerImpl { |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2720 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2721 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2722 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2723 | |
| 2724 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2725 | ~MockPingManager() override { EXPECT_TRUE(ping_data().empty()); } |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2726 | }; |
| 2727 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2728 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 2729 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2730 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 2731 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2732 | |
| 2733 | MockObserver observer; |
| 2734 | |
| 2735 | InSequence seq; |
| 2736 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2737 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2738 | .Times(1); |
| 2739 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 2740 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2741 | .Times(1); |
| 2742 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2743 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2744 | .Times(1); |
| 2745 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 2746 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2747 | .Times(1); |
| 2748 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 2749 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2750 | .Times(1); |
| 2751 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED, |
| 2752 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 2753 | .Times(1); |
| 2754 | |
| 2755 | update_client->AddObserver(&observer); |
| 2756 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2757 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf"}; |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2758 | { |
| 2759 | // The engine handles this Update call but responds with a valid |
| 2760 | // |retry_after_sec|, which causes subsequent calls to fail. |
| 2761 | base::RunLoop runloop; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2762 | update_client->Update(ids, base::BindOnce(&DataCallbackMock::Callback), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 2763 | false, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2764 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2765 | runloop.QuitClosure())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2766 | runloop.Run(); |
| 2767 | } |
| 2768 | |
| 2769 | { |
| 2770 | // This call will result in a completion callback invoked with |
| 2771 | // Error::ERROR_UPDATE_RETRY_LATER. |
| 2772 | base::RunLoop runloop; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2773 | update_client->Update(ids, base::BindOnce(&DataCallbackMock::Callback), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 2774 | false, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2775 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2776 | runloop.QuitClosure())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2777 | runloop.Run(); |
| 2778 | } |
| 2779 | |
| 2780 | { |
| 2781 | // The Install call is handled, and the throttling is reset due to |
| 2782 | // the value of |retry_after_sec| in the completion callback. |
| 2783 | base::RunLoop runloop; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2784 | update_client->Install(std::string("jebgalgnebhfojomionfpkfelancnnkf"), |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2785 | base::BindOnce(&DataCallbackMock::Callback), |
| 2786 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2787 | runloop.QuitClosure())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2788 | runloop.Run(); |
| 2789 | } |
| 2790 | |
| 2791 | { |
| 2792 | // This call succeeds. |
| 2793 | base::RunLoop runloop; |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2794 | update_client->Update(ids, base::BindOnce(&DataCallbackMock::Callback), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 2795 | false, |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2796 | base::BindOnce(&CompletionCallbackMock::Callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2797 | runloop.QuitClosure())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 2798 | runloop.Run(); |
| 2799 | } |
| 2800 | |
| 2801 | update_client->RemoveObserver(&observer); |
| 2802 | } |
| 2803 | |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2804 | // Tests the update check for two CRXs scenario. The first component supports |
| 2805 | // the group policy to enable updates, and has its updates disabled. The second |
| 2806 | // component has an update. The server does not honor the "updatedisabled" |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2807 | // attribute and returns updates for both components. However, the update for |
| 2808 | // the first component is not apply and the client responds with a |
| 2809 | // (SERVICE_ERROR, UPDATE_DISABLED) |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2810 | TEST_F(UpdateClientTest, TwoCrxUpdateOneUpdateDisabled) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2811 | class DataCallbackMock { |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2812 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2813 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 2814 | const std::vector<std::string>& ids) { |
| 2815 | std::unique_ptr<CrxComponent> crx1 = std::make_unique<CrxComponent>(); |
| 2816 | crx1->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 2817 | crx1->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2818 | crx1->version = base::Version("0.9"); |
| 2819 | crx1->installer = base::MakeRefCounted<TestInstaller>(); |
| 2820 | crx1->supports_group_policy_enable_component_updates = true; |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2821 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2822 | std::unique_ptr<CrxComponent> crx2 = std::make_unique<CrxComponent>(); |
| 2823 | crx2->name = "test_ihfo"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 2824 | crx2->pk_hash.assign(ihfo_hash, ihfo_hash + base::size(ihfo_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2825 | crx2->version = base::Version("0.8"); |
| 2826 | crx2->installer = base::MakeRefCounted<TestInstaller>(); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2827 | |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 2828 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 2829 | component.push_back(std::move(crx1)); |
| 2830 | component.push_back(std::move(crx2)); |
| 2831 | return component; |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2832 | } |
| 2833 | }; |
| 2834 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2835 | class CompletionCallbackMock { |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2836 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2837 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 2838 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2839 | std::move(quit_closure).Run(); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2840 | } |
| 2841 | }; |
| 2842 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2843 | class MockUpdateChecker : public UpdateChecker { |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2844 | public: |
| 2845 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 2846 | scoped_refptr<Configurator> config, |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2847 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2848 | return std::make_unique<MockUpdateChecker>(); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2849 | } |
| 2850 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2851 | void CheckForUpdates(const std::string& session_id, |
| 2852 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2853 | const IdToComponentPtrMap& components, |
| 2854 | const std::string& additional_attributes, |
| 2855 | bool enabled_component_updates, |
| 2856 | UpdateCheckCallback update_check_callback) override { |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2857 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2858 | Mock the following response: |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2859 | |
| 2860 | <?xml version='1.0' encoding='UTF-8'?> |
sorin | 3985280 | 2017-05-01 22:46:28 | [diff] [blame] | 2861 | <response protocol='3.1'> |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2862 | <app appid='jebgalgnebhfojomionfpkfelancnnkf'> |
| 2863 | <updatecheck status='ok'> |
| 2864 | <urls> |
| 2865 | <url codebase='http://localhost/download/'/> |
| 2866 | </urls> |
| 2867 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 2868 | <packages> |
| 2869 | <package name='jebgalgnebhfojomionfpkfelancnnkf.crx' |
| 2870 | hash_sha256='6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd |
| 2871 | 7c9b12cb7cc067667bde87'/> |
| 2872 | </packages> |
| 2873 | </manifest> |
| 2874 | </updatecheck> |
| 2875 | </app> |
| 2876 | <app appid='ihfokbkgjpifnbbojhneepfflplebdkc'> |
| 2877 | <updatecheck status='ok'> |
| 2878 | <urls> |
| 2879 | <url codebase='http://localhost/download/'/> |
| 2880 | </urls> |
| 2881 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 2882 | <packages> |
| 2883 | <package name='ihfokbkgjpifnbbojhneepfflplebdkc_1.crx' |
| 2884 | hash_sha256='813c59747e139a608b3b5fc49633affc6db574373f |
| 2885 | 309f156ea6d27229c0b3f9'/> |
| 2886 | </packages> |
| 2887 | </manifest> |
| 2888 | </updatecheck> |
| 2889 | </app> |
| 2890 | </response> |
| 2891 | */ |
sorin | 590921d | 2016-08-11 23:48:36 | [diff] [blame] | 2892 | |
| 2893 | // UpdateClient reads the state of |enabled_component_updates| from the |
| 2894 | // configurator instance, persists its value in the corresponding |
| 2895 | // update context, and propagates it down to each of the update actions, |
| 2896 | // and further down to the UpdateChecker instance. |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 2897 | EXPECT_FALSE(session_id.empty()); |
sorin | 590921d | 2016-08-11 23:48:36 | [diff] [blame] | 2898 | EXPECT_FALSE(enabled_component_updates); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2899 | EXPECT_EQ(2u, ids_to_check.size()); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2900 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2901 | { |
| 2902 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 2903 | EXPECT_EQ(id, ids_to_check[0]); |
| 2904 | EXPECT_EQ(1u, components.count(id)); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2905 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2906 | ProtocolParser::Result::Manifest::Package package; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2907 | package.name = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 2908 | package.hash_sha256 = |
| 2909 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2910 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2911 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2912 | result.extension_id = id; |
| 2913 | result.status = "ok"; |
| 2914 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 2915 | result.manifest.version = "1.0"; |
| 2916 | result.manifest.browser_min_version = "11.0.1.0"; |
| 2917 | result.manifest.packages.push_back(package); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2918 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2919 | auto& component = components.at(id); |
| 2920 | component->SetParseResult(result); |
| 2921 | } |
| 2922 | |
| 2923 | { |
| 2924 | const std::string id = "ihfokbkgjpifnbbojhneepfflplebdkc"; |
| 2925 | EXPECT_EQ(id, ids_to_check[1]); |
| 2926 | EXPECT_EQ(1u, components.count(id)); |
| 2927 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2928 | ProtocolParser::Result::Manifest::Package package; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2929 | package.name = "ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"; |
| 2930 | package.hash_sha256 = |
| 2931 | "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"; |
| 2932 | |
sorin | 7cff6e5 | 2017-05-17 16:37:23 | [diff] [blame] | 2933 | ProtocolParser::Result result; |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2934 | result.extension_id = id; |
| 2935 | result.status = "ok"; |
| 2936 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 2937 | result.manifest.version = "1.0"; |
| 2938 | result.manifest.browser_min_version = "11.0.1.0"; |
| 2939 | result.manifest.packages.push_back(package); |
| 2940 | |
| 2941 | auto& component = components.at(id); |
| 2942 | component->SetParseResult(result); |
| 2943 | } |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2944 | |
| 2945 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 2946 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2947 | } |
| 2948 | }; |
| 2949 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2950 | class MockCrxDownloader : public CrxDownloader { |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2951 | public: |
| 2952 | static std::unique_ptr<CrxDownloader> Create( |
| 2953 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 2954 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2955 | return std::make_unique<MockCrxDownloader>(); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2956 | } |
| 2957 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2958 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2959 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 2960 | private: |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2961 | void DoStartDownload(const GURL& url) override { |
| 2962 | DownloadMetrics download_metrics; |
| 2963 | FilePath path; |
| 2964 | Result result; |
| 2965 | if (url.path() == "/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx") { |
| 2966 | download_metrics.url = url; |
| 2967 | download_metrics.downloader = DownloadMetrics::kNone; |
| 2968 | download_metrics.error = 0; |
| 2969 | download_metrics.downloaded_bytes = 53638; |
| 2970 | download_metrics.total_bytes = 53638; |
| 2971 | download_metrics.download_time_ms = 2000; |
| 2972 | |
| 2973 | EXPECT_TRUE(MakeTestFile( |
| 2974 | TestFilePath("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"), &path)); |
| 2975 | |
| 2976 | result.error = 0; |
| 2977 | result.response = path; |
| 2978 | result.downloaded_bytes = 53638; |
| 2979 | result.total_bytes = 53638; |
| 2980 | } else { |
| 2981 | NOTREACHED(); |
| 2982 | } |
| 2983 | |
| 2984 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2985 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadProgress, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 2986 | base::Unretained(this), result)); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2987 | |
| 2988 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2989 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 2990 | base::Unretained(this), true, result, |
| 2991 | download_metrics)); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2992 | } |
| 2993 | }; |
| 2994 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2995 | class MockPingManager : public MockPingManagerImpl { |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 2996 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 2997 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 2998 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 2999 | |
| 3000 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3001 | ~MockPingManager() override { |
| 3002 | const auto ping_data = MockPingManagerImpl::ping_data(); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 3003 | EXPECT_EQ(2u, ping_data.size()); |
| 3004 | EXPECT_EQ("jebgalgnebhfojomionfpkfelancnnkf", ping_data[0].id); |
| 3005 | EXPECT_EQ(base::Version("0.9"), ping_data[0].previous_version); |
| 3006 | EXPECT_EQ(base::Version("1.0"), ping_data[0].next_version); |
| 3007 | EXPECT_EQ(4, ping_data[0].error_category); |
| 3008 | EXPECT_EQ(2, ping_data[0].error_code); |
| 3009 | EXPECT_EQ("ihfokbkgjpifnbbojhneepfflplebdkc", ping_data[1].id); |
| 3010 | EXPECT_EQ(base::Version("0.8"), ping_data[1].previous_version); |
| 3011 | EXPECT_EQ(base::Version("1.0"), ping_data[1].next_version); |
| 3012 | EXPECT_EQ(0, ping_data[1].error_category); |
| 3013 | EXPECT_EQ(0, ping_data[1].error_code); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 3014 | } |
| 3015 | }; |
| 3016 | |
| 3017 | // Disables updates for the components declaring support for the group policy. |
| 3018 | config()->SetEnabledComponentUpdates(false); |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 3019 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 3020 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3021 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 3022 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 3023 | |
| 3024 | MockObserver observer; |
| 3025 | { |
| 3026 | InSequence seq; |
| 3027 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 3028 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 3029 | .Times(1); |
| 3030 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 3031 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 3032 | .Times(1); |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 3033 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 3034 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 3035 | .Times(1); |
| 3036 | } |
| 3037 | { |
| 3038 | InSequence seq; |
| 3039 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 3040 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 3041 | .Times(1); |
| 3042 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND, |
| 3043 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 3044 | .Times(1); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 3045 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING, |
| 3046 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 3047 | .Times(AtLeast(1)); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 3048 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY, |
| 3049 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 3050 | .Times(1); |
| 3051 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED, |
| 3052 | "ihfokbkgjpifnbbojhneepfflplebdkc")) |
| 3053 | .Times(1); |
| 3054 | } |
| 3055 | |
| 3056 | update_client->AddObserver(&observer); |
| 3057 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 3058 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf", |
| 3059 | "ihfokbkgjpifnbbojhneepfflplebdkc"}; |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 3060 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3061 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 3062 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | e84ff70 | 2016-08-04 01:22:02 | [diff] [blame] | 3063 | |
| 3064 | RunThreads(); |
| 3065 | |
| 3066 | update_client->RemoveObserver(&observer); |
| 3067 | } |
| 3068 | |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3069 | // Tests the scenario where the update check fails. |
| 3070 | TEST_F(UpdateClientTest, OneCrxUpdateCheckFails) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3071 | class DataCallbackMock { |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3072 | public: |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3073 | static std::vector<std::unique_ptr<CrxComponent>> Callback( |
| 3074 | const std::vector<std::string>& ids) { |
| 3075 | std::unique_ptr<CrxComponent> crx = std::make_unique<CrxComponent>(); |
| 3076 | crx->name = "test_jebg"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 3077 | crx->pk_hash.assign(jebg_hash, jebg_hash + base::size(jebg_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3078 | crx->version = base::Version("0.9"); |
| 3079 | crx->installer = base::MakeRefCounted<TestInstaller>(); |
| 3080 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 3081 | component.push_back(std::move(crx)); |
| 3082 | return component; |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3083 | } |
| 3084 | }; |
| 3085 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3086 | class CompletionCallbackMock { |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3087 | public: |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3088 | static void Callback(base::OnceClosure quit_closure, Error error) { |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3089 | EXPECT_EQ(Error::UPDATE_CHECK_ERROR, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3090 | std::move(quit_closure).Run(); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3091 | } |
| 3092 | }; |
| 3093 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3094 | class MockUpdateChecker : public UpdateChecker { |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3095 | public: |
| 3096 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 3097 | scoped_refptr<Configurator> config, |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3098 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3099 | return std::make_unique<MockUpdateChecker>(); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3100 | } |
| 3101 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 3102 | void CheckForUpdates(const std::string& session_id, |
| 3103 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3104 | const IdToComponentPtrMap& components, |
| 3105 | const std::string& additional_attributes, |
| 3106 | bool enabled_component_updates, |
| 3107 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 3108 | EXPECT_FALSE(session_id.empty()); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3109 | EXPECT_TRUE(enabled_component_updates); |
| 3110 | EXPECT_EQ(1u, ids_to_check.size()); |
| 3111 | const std::string id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 3112 | EXPECT_EQ(id, ids_to_check.front()); |
| 3113 | EXPECT_EQ(1u, components.count(id)); |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 3114 | constexpr int update_check_error = -1; |
| 3115 | components.at(id)->set_update_check_error(update_check_error); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3116 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 3117 | FROM_HERE, base::BindOnce(std::move(update_check_callback), |
| 3118 | update_check_error, 0)); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3119 | } |
| 3120 | }; |
| 3121 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3122 | class MockCrxDownloader : public CrxDownloader { |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3123 | public: |
| 3124 | static std::unique_ptr<CrxDownloader> Create( |
| 3125 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 3126 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3127 | return std::make_unique<MockCrxDownloader>(); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3128 | } |
| 3129 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3130 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3131 | |
| 3132 | private: |
| 3133 | void DoStartDownload(const GURL& url) override { EXPECT_TRUE(false); } |
| 3134 | }; |
| 3135 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3136 | class MockPingManager : public MockPingManagerImpl { |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3137 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3138 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 3139 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 3140 | |
| 3141 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3142 | ~MockPingManager() override { EXPECT_TRUE(ping_data().empty()); } |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3143 | }; |
| 3144 | |
| 3145 | scoped_refptr<UpdateClient> update_client = |
Taiju Tsuiki | 36c517d | 2017-05-18 06:45:43 | [diff] [blame] | 3146 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3147 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 3148 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3149 | |
| 3150 | MockObserver observer; |
| 3151 | InSequence seq; |
| 3152 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_CHECKING_FOR_UPDATES, |
| 3153 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 3154 | .Times(1); |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 3155 | EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_ERROR, |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3156 | "jebgalgnebhfojomionfpkfelancnnkf")) |
| 3157 | .Times(1) |
| 3158 | .WillOnce(Invoke([&update_client](Events event, const std::string& id) { |
| 3159 | CrxUpdateItem item; |
| 3160 | update_client->GetCrxUpdateState(id, &item); |
| 3161 | EXPECT_EQ(ComponentState::kUpdateError, item.state); |
Sorin Jianu | afcb70dd | 2018-05-16 20:14:15 | [diff] [blame] | 3162 | EXPECT_EQ(5, item.error_category); |
| 3163 | EXPECT_EQ(-1, item.error_code); |
| 3164 | EXPECT_EQ(0, item.extra_code1); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3165 | })); |
| 3166 | |
| 3167 | update_client->AddObserver(&observer); |
| 3168 | |
| 3169 | const std::vector<std::string> ids = {"jebgalgnebhfojomionfpkfelancnnkf"}; |
| 3170 | update_client->Update( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3171 | ids, base::BindOnce(&DataCallbackMock::Callback), false, |
| 3172 | base::BindOnce(&CompletionCallbackMock::Callback, quit_closure())); |
sorin | 03ec5b7 | 2017-05-01 23:14:24 | [diff] [blame] | 3173 | |
| 3174 | RunThreads(); |
| 3175 | |
| 3176 | update_client->RemoveObserver(&observer); |
| 3177 | } |
| 3178 | |
Sorin Jianu | 7b00a13 | 2017-06-19 21:56:54 | [diff] [blame] | 3179 | #if defined(OS_WIN) // ActionRun is only implemented on Windows. |
| 3180 | |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3181 | // Tests that a run action in invoked in the CRX install scenario. |
| 3182 | TEST_F(UpdateClientTest, ActionRun_Install) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3183 | class MockUpdateChecker : public UpdateChecker { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3184 | public: |
| 3185 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 3186 | scoped_refptr<Configurator> config, |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3187 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3188 | return std::make_unique<MockUpdateChecker>(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3189 | } |
| 3190 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 3191 | void CheckForUpdates(const std::string& session_id, |
| 3192 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3193 | const IdToComponentPtrMap& components, |
| 3194 | const std::string& additional_attributes, |
| 3195 | bool enabled_component_updates, |
| 3196 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3197 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3198 | Mock the following response: |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3199 | |
| 3200 | <?xml version='1.0' encoding='UTF-8'?> |
| 3201 | <response protocol='3.1'> |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3202 | <app appid='gjpmebpgbhcamgdgjcmnjfhggjpgcimm'> |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3203 | <updatecheck status='ok'> |
| 3204 | <urls> |
| 3205 | <url codebase='http://localhost/download/'/> |
| 3206 | </urls> |
| 3207 | <manifest version='1.0' prodversionmin='11.0.1.0'> |
| 3208 | <packages> |
| 3209 | <package name='runaction_test_win.crx3' |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3210 | hash_sha256='89290a0d2ff21ca5b45e109c6cc859ab5fe294e19c102d54acd321429c372cea'/> |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3211 | </packages> |
| 3212 | </manifest> |
| 3213 | <actions>" |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3214 | <action run='ChromeRecovery.crx3'/>" |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3215 | </actions>" |
| 3216 | </updatecheck> |
| 3217 | </app> |
| 3218 | </response> |
| 3219 | */ |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 3220 | EXPECT_FALSE(session_id.empty()); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3221 | EXPECT_TRUE(enabled_component_updates); |
| 3222 | EXPECT_EQ(1u, ids_to_check.size()); |
| 3223 | |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3224 | const std::string id = "gjpmebpgbhcamgdgjcmnjfhggjpgcimm"; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3225 | EXPECT_EQ(id, ids_to_check[0]); |
| 3226 | EXPECT_EQ(1u, components.count(id)); |
| 3227 | |
| 3228 | ProtocolParser::Result::Manifest::Package package; |
| 3229 | package.name = "runaction_test_win.crx3"; |
| 3230 | package.hash_sha256 = |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3231 | "89290a0d2ff21ca5b45e109c6cc859ab5fe294e19c102d54acd321429c372cea"; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3232 | |
| 3233 | ProtocolParser::Result result; |
| 3234 | result.extension_id = id; |
| 3235 | result.status = "ok"; |
| 3236 | result.crx_urls.push_back(GURL("http://localhost/download/")); |
| 3237 | result.manifest.version = "1.0"; |
| 3238 | result.manifest.browser_min_version = "11.0.1.0"; |
| 3239 | result.manifest.packages.push_back(package); |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3240 | result.action_run = "ChromeRecovery.crx3"; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3241 | |
| 3242 | auto& component = components.at(id); |
| 3243 | component->SetParseResult(result); |
| 3244 | |
| 3245 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3246 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3247 | } |
| 3248 | }; |
| 3249 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3250 | class MockCrxDownloader : public CrxDownloader { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3251 | public: |
| 3252 | static std::unique_ptr<CrxDownloader> Create( |
| 3253 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 3254 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3255 | return std::make_unique<MockCrxDownloader>(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3256 | } |
| 3257 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3258 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3259 | |
| 3260 | private: |
| 3261 | void DoStartDownload(const GURL& url) override { |
| 3262 | DownloadMetrics download_metrics; |
| 3263 | FilePath path; |
| 3264 | Result result; |
| 3265 | if (url.path() == "/download/runaction_test_win.crx3") { |
| 3266 | download_metrics.url = url; |
| 3267 | download_metrics.downloader = DownloadMetrics::kNone; |
| 3268 | download_metrics.error = 0; |
| 3269 | download_metrics.downloaded_bytes = 1843; |
| 3270 | download_metrics.total_bytes = 1843; |
| 3271 | download_metrics.download_time_ms = 1000; |
| 3272 | |
| 3273 | EXPECT_TRUE( |
| 3274 | MakeTestFile(TestFilePath("runaction_test_win.crx3"), &path)); |
| 3275 | |
| 3276 | result.error = 0; |
| 3277 | result.response = path; |
| 3278 | result.downloaded_bytes = 1843; |
| 3279 | result.total_bytes = 1843; |
| 3280 | } else { |
| 3281 | NOTREACHED(); |
| 3282 | } |
| 3283 | |
| 3284 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3285 | FROM_HERE, base::BindOnce(&MockCrxDownloader::OnDownloadComplete, |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 3286 | base::Unretained(this), true, result, |
| 3287 | download_metrics)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3288 | } |
| 3289 | }; |
| 3290 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3291 | class MockPingManager : public MockPingManagerImpl { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3292 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3293 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 3294 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 3295 | |
| 3296 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3297 | ~MockPingManager() override { |
| 3298 | const auto& events = MockPingManagerImpl::events(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3299 | EXPECT_EQ(3u, events.size()); |
| 3300 | EXPECT_STREQ( |
| 3301 | "<event eventtype=\"14\" eventresult=\"1\" downloader=\"unknown\" " |
| 3302 | "url=\"http://localhost/download/" |
| 3303 | "runaction_test_win.crx3\" downloaded=\"1843\" " |
Minh X. Nguyen | 01d3586d | 2018-01-03 21:53:39 | [diff] [blame] | 3304 | "total=\"1843\" download_time_ms=\"1000\" previousversion=\"0.0\" " |
| 3305 | "nextversion=\"1.0\"/>", |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3306 | events[0].c_str()); |
| 3307 | EXPECT_STREQ( |
Sorin Jianu | 7b00a13 | 2017-06-19 21:56:54 | [diff] [blame] | 3308 | "<event eventtype=\"42\" eventresult=\"1\" " |
| 3309 | "errorcode=\"1877345072\"/>", |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3310 | events[1].c_str()); |
Minh X. Nguyen | 01d3586d | 2018-01-03 21:53:39 | [diff] [blame] | 3311 | EXPECT_STREQ( |
| 3312 | "<event eventtype=\"3\" eventresult=\"1\" previousversion=\"0.0\" " |
| 3313 | "nextversion=\"1.0\"/>", |
| 3314 | events[2].c_str()); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3315 | } |
| 3316 | }; |
| 3317 | |
| 3318 | scoped_refptr<UpdateClient> update_client = |
| 3319 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3320 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 3321 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3322 | |
Sorin Jianu | 7b00a13 | 2017-06-19 21:56:54 | [diff] [blame] | 3323 | // The action is a program which returns 1877345072 as a hardcoded value. |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3324 | update_client->Install( |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3325 | std::string("gjpmebpgbhcamgdgjcmnjfhggjpgcimm"), |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3326 | base::BindOnce([](const std::vector<std::string>& ids) { |
| 3327 | auto crx = std::make_unique<CrxComponent>(); |
| 3328 | crx->name = "test_niea"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 3329 | crx->pk_hash.assign(gjpm_hash, gjpm_hash + base::size(gjpm_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3330 | crx->version = base::Version("0.0"); |
| 3331 | crx->installer = base::MakeRefCounted<VersionedTestInstaller>(); |
| 3332 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 3333 | component.push_back(std::move(crx)); |
| 3334 | return component; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3335 | }), |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3336 | base::BindOnce( |
| 3337 | [](base::OnceClosure quit_closure, Error error) { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3338 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3339 | std::move(quit_closure).Run(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3340 | }, |
| 3341 | quit_closure())); |
| 3342 | |
| 3343 | RunThreads(); |
| 3344 | } |
| 3345 | |
| 3346 | // Tests that a run action is invoked in an update scenario when there was |
| 3347 | // no update. |
| 3348 | TEST_F(UpdateClientTest, ActionRun_NoUpdate) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3349 | class MockUpdateChecker : public UpdateChecker { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3350 | public: |
| 3351 | static std::unique_ptr<UpdateChecker> Create( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 3352 | scoped_refptr<Configurator> config, |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3353 | PersistedData* metadata) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3354 | return std::make_unique<MockUpdateChecker>(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3355 | } |
| 3356 | |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 3357 | void CheckForUpdates(const std::string& session_id, |
| 3358 | const std::vector<std::string>& ids_to_check, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3359 | const IdToComponentPtrMap& components, |
| 3360 | const std::string& additional_attributes, |
| 3361 | bool enabled_component_updates, |
| 3362 | UpdateCheckCallback update_check_callback) override { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3363 | /* |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3364 | Mock the following response: |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3365 | |
| 3366 | <?xml version='1.0' encoding='UTF-8'?> |
| 3367 | <response protocol='3.1'> |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3368 | <app appid='gjpmebpgbhcamgdgjcmnjfhggjpgcimm'> |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3369 | <updatecheck status='noupdate'> |
| 3370 | <actions>" |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3371 | <action run=ChromeRecovery.crx3'/>" |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3372 | </actions>" |
| 3373 | </updatecheck> |
| 3374 | </app> |
| 3375 | </response> |
| 3376 | */ |
Sorin Jianu | d69d437 | 2018-02-07 19:44:22 | [diff] [blame] | 3377 | EXPECT_FALSE(session_id.empty()); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3378 | EXPECT_EQ(1u, ids_to_check.size()); |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3379 | const std::string id = "gjpmebpgbhcamgdgjcmnjfhggjpgcimm"; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3380 | EXPECT_EQ(id, ids_to_check[0]); |
| 3381 | EXPECT_EQ(1u, components.count(id)); |
| 3382 | |
| 3383 | auto& component = components.at(id); |
| 3384 | |
| 3385 | ProtocolParser::Result result; |
| 3386 | result.extension_id = id; |
| 3387 | result.status = "noupdate"; |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3388 | result.action_run = "ChromeRecovery.crx3"; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3389 | |
| 3390 | component->SetParseResult(result); |
| 3391 | |
| 3392 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3393 | FROM_HERE, base::BindOnce(std::move(update_check_callback), 0, 0)); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3394 | } |
| 3395 | }; |
| 3396 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3397 | class MockCrxDownloader : public CrxDownloader { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3398 | public: |
| 3399 | static std::unique_ptr<CrxDownloader> Create( |
| 3400 | bool is_background_download, |
Sorin Jianu | 556147d | 2018-01-19 21:55:21 | [diff] [blame] | 3401 | scoped_refptr<net::URLRequestContextGetter> context_getter) { |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3402 | return std::make_unique<MockCrxDownloader>(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3403 | } |
| 3404 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3405 | MockCrxDownloader() : CrxDownloader(nullptr) {} |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3406 | |
| 3407 | private: |
| 3408 | void DoStartDownload(const GURL& url) override { EXPECT_TRUE(false); } |
| 3409 | }; |
| 3410 | |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3411 | class MockPingManager : public MockPingManagerImpl { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3412 | public: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3413 | explicit MockPingManager(scoped_refptr<Configurator> config) |
| 3414 | : MockPingManagerImpl(config) {} |
Sorin Jianu | 560d502 | 2018-02-12 23:11:54 | [diff] [blame] | 3415 | |
| 3416 | protected: |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3417 | ~MockPingManager() override { |
| 3418 | const auto& events = MockPingManagerImpl::events(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3419 | EXPECT_EQ(1u, events.size()); |
| 3420 | EXPECT_STREQ( |
Sorin Jianu | 7b00a13 | 2017-06-19 21:56:54 | [diff] [blame] | 3421 | "<event eventtype=\"42\" eventresult=\"1\" " |
| 3422 | "errorcode=\"1877345072\"/>", |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3423 | events[0].c_str()); |
| 3424 | } |
| 3425 | }; |
| 3426 | |
| 3427 | // Unpack the CRX to mock an existing install to be updated. The payload to |
| 3428 | // run is going to be picked up from this directory. |
| 3429 | base::FilePath unpack_path; |
| 3430 | { |
| 3431 | base::RunLoop runloop; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3432 | base::OnceClosure quit_closure = runloop.QuitClosure(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3433 | |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 3434 | auto config = base::MakeRefCounted<TestConfigurator>(); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3435 | auto component_unpacker = base::MakeRefCounted<ComponentUnpacker>( |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3436 | std::vector<uint8_t>(std::begin(gjpm_hash), std::end(gjpm_hash)), |
Joshua Pawlicki | 6d76442 | 2018-02-21 15:23:10 | [diff] [blame] | 3437 | TestFilePath("runaction_test_win.crx3"), nullptr, |
| 3438 | config->CreateServiceManagerConnector()); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3439 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3440 | component_unpacker->Unpack(base::BindOnce( |
| 3441 | [](base::FilePath* unpack_path, base::OnceClosure quit_closure, |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3442 | const ComponentUnpacker::Result& result) { |
| 3443 | EXPECT_EQ(UnpackerError::kNone, result.error); |
| 3444 | EXPECT_EQ(0, result.extended_error); |
| 3445 | *unpack_path = result.unpack_path; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3446 | std::move(quit_closure).Run(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3447 | }, |
| 3448 | &unpack_path, runloop.QuitClosure())); |
| 3449 | |
| 3450 | runloop.Run(); |
| 3451 | } |
| 3452 | |
| 3453 | EXPECT_FALSE(unpack_path.empty()); |
| 3454 | EXPECT_TRUE(base::DirectoryExists(unpack_path)); |
| 3455 | int64_t file_size = 0; |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3456 | EXPECT_TRUE(base::GetFileSize(unpack_path.AppendASCII("ChromeRecovery.crx3"), |
| 3457 | &file_size)); |
| 3458 | EXPECT_EQ(44582, file_size); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3459 | |
| 3460 | base::ScopedTempDir unpack_path_owner; |
| 3461 | EXPECT_TRUE(unpack_path_owner.Set(unpack_path)); |
| 3462 | |
| 3463 | scoped_refptr<UpdateClient> update_client = |
| 3464 | base::MakeRefCounted<UpdateClientImpl>( |
Sorin Jianu | a8926bf | 2018-03-09 21:02:53 | [diff] [blame] | 3465 | config(), base::MakeRefCounted<MockPingManager>(config()), |
| 3466 | &MockUpdateChecker::Create, &MockCrxDownloader::Create); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3467 | |
Sorin Jianu | 7b00a13 | 2017-06-19 21:56:54 | [diff] [blame] | 3468 | // The action is a program which returns 1877345072 as a hardcoded value. |
Sorin Jianu | e5bfb5b | 2017-06-20 21:30:56 | [diff] [blame] | 3469 | const std::vector<std::string> ids = {"gjpmebpgbhcamgdgjcmnjfhggjpgcimm"}; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3470 | update_client->Update( |
| 3471 | ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3472 | base::BindOnce( |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3473 | [](const base::FilePath& unpack_path, |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3474 | const std::vector<std::string>& ids) { |
| 3475 | auto crx = std::make_unique<CrxComponent>(); |
| 3476 | crx->name = "test_niea"; |
Sorin Jianu | cb4431a | 2018-04-30 20:59:24 | [diff] [blame] | 3477 | crx->pk_hash.assign(gjpm_hash, gjpm_hash + base::size(gjpm_hash)); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3478 | crx->version = base::Version("1.0"); |
| 3479 | crx->installer = |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3480 | base::MakeRefCounted<ReadOnlyTestInstaller>(unpack_path); |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 3481 | std::vector<std::unique_ptr<CrxComponent>> component; |
| 3482 | component.push_back(std::move(crx)); |
| 3483 | return component; |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3484 | }, |
| 3485 | unpack_path), |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 3486 | false, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3487 | base::BindOnce( |
| 3488 | [](base::OnceClosure quit_closure, Error error) { |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3489 | EXPECT_EQ(Error::NONE, error); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 3490 | std::move(quit_closure).Run(); |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 3491 | }, |
| 3492 | quit_closure())); |
| 3493 | |
| 3494 | RunThreads(); |
| 3495 | } |
| 3496 | |
Sorin Jianu | 7b00a13 | 2017-06-19 21:56:54 | [diff] [blame] | 3497 | #endif // OS_WIN |
| 3498 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 3499 | } // namespace update_client |