blob: ec0833352a0ca1d45ae6ce9cb414cff7f24c83ed [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov6a5b3222010-07-13 14:55:28 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_action.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070021#include <limits>
Ben Chanab5a0af2017-10-12 14:57:50 -070022#include <memory>
Darin Petkov6a5b3222010-07-13 14:55:28 -070023#include <string>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080024#include <utility>
Darin Petkov6a5b3222010-07-13 14:55:28 -070025#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070026
Alex Deymo60ca1a72015-06-18 18:19:15 -070027#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070028#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070029#include <base/files/scoped_temp_dir.h>
Ben Chan5c02c132017-06-27 07:10:36 -070030#include <base/memory/ptr_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070031#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070032#include <base/strings/string_util.h>
33#include <base/strings/stringprintf.h>
34#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070035#include <brillo/message_loops/fake_message_loop.h>
36#include <brillo/message_loops/message_loop.h>
37#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070038#include <gtest/gtest.h>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080039#include <policy/libpolicy.h>
40#include <policy/mock_libpolicy.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070041
Alex Deymo39910dc2015-11-09 17:04:30 -080042#include "update_engine/common/action_pipe.h"
43#include "update_engine/common/constants.h"
44#include "update_engine/common/fake_prefs.h"
45#include "update_engine/common/hash_calculator.h"
46#include "update_engine/common/mock_http_fetcher.h"
47#include "update_engine/common/platform_constants.h"
48#include "update_engine/common/prefs.h"
49#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030050#include "update_engine/fake_system_state.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070051#include "update_engine/metrics_reporter_interface.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070052#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070053#include "update_engine/mock_payload_state.h"
Amin Hassani7fca2862019-03-28 16:09:22 -070054#include "update_engine/omaha_request_builder_xml.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055#include "update_engine/omaha_request_params.h"
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080056#include "update_engine/update_manager/rollback_prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070057
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058using base::Time;
59using base::TimeDelta;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080060using chromeos_update_manager::kRollforwardInfinity;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070061using std::pair;
Darin Petkov6a5b3222010-07-13 14:55:28 -070062using std::string;
63using std::vector;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080064using testing::_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070066using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070067using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070068using testing::Ge;
69using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080070using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070071using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070072using testing::ReturnPointee;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080073using testing::ReturnRef;
Gilad Arnold74b5f552014-10-07 08:17:16 -070074using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070075using testing::SetArgPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070076
Alex Deymo8e18f932015-03-27 16:16:59 -070077namespace {
78
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080079static_assert(kRollforwardInfinity == 0xfffffffe,
80 "Don't change the value of kRollforward infinity unless its "
81 "size has been changed in firmware.");
82
Xiaochu Liu6310be62018-10-11 15:09:03 -070083const char kCurrentVersion[] = "0.1.0.0";
Alex Deymo85616652015-10-15 18:48:31 -070084const char kTestAppId[] = "test-app-id";
Aaron Wood7dcdedf2017-09-06 17:17:41 -070085const char kTestAppId2[] = "test-app2-id";
Xiaochu Liu6310be62018-10-11 15:09:03 -070086const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck";
Alex Deymo85616652015-10-15 18:48:31 -070087
Alex Deymo8e18f932015-03-27 16:16:59 -070088// This is a helper struct to allow unit tests build an update response with the
89// values they care about.
90struct FakeUpdateResponse {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070091 string GetRollbackVersionAttributes() const {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070092 string num_milestones;
93#if BASE_VER < 576279
94 num_milestones = base::IntToString(rollback_allowed_milestones);
95#else
96 num_milestones = base::NumberToString(rollback_allowed_milestones);
97#endif
98 const string rollback_version =
99 " _firmware_version_" + num_milestones + "=\"" +
100 past_rollback_key_version.first + "\"" + " _kernel_version_" +
101 num_milestones + "=\"" + past_rollback_key_version.second + "\"";
102
103 return (rollback ? " _rollback=\"true\"" : "") + rollback_version +
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700104 (!rollback_firmware_version.empty()
105 ? " _firmware_version=\"" + rollback_firmware_version + "\""
106 : "") +
107 (!rollback_kernel_version.empty()
108 ? " _kernel_version=\"" + rollback_kernel_version + "\""
109 : "");
110 }
111
Alex Deymo8e18f932015-03-27 16:16:59 -0700112 string GetNoUpdateResponse() const {
113 string entity_str;
114 if (include_entity)
115 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Sen Jiang81259682017-03-30 15:11:30 -0700116 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str +
117 "<response protocol=\"3.0\">"
118 "<daystart elapsed_seconds=\"100\"/>"
119 "<app appid=\"" +
120 app_id + "\" " +
121 (include_cohorts
122 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
123 "\" cohortname=\"" + cohortname + "\" "
124 : "") +
125 " status=\"ok\">"
126 "<ping status=\"ok\"/>"
127 "<updatecheck status=\"noupdate\"/></app>" +
128 (multi_app_no_update
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700129 ? "<app appid=\"" + app_id2 +
130 "\"><updatecheck status=\"noupdate\"/></app>"
Sen Jiang81259682017-03-30 15:11:30 -0700131 : "") +
132 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700133 }
134
135 string GetUpdateResponse() const {
Sen Jiang2703ef42017-03-16 13:36:21 -0700136 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
137 "protocol=\"3.0\">"
138 "<daystart elapsed_seconds=\"100\"" +
139 (elapsed_days.empty() ? ""
140 : (" elapsed_days=\"" + elapsed_days + "\"")) +
141 "/>"
142 "<app appid=\"" +
143 app_id + "\" " +
144 (include_cohorts
145 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
146 "\" cohortname=\"" + cohortname + "\" "
147 : "") +
148 " status=\"ok\">"
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700149 "<ping status=\"ok\"/><updatecheck status=\"ok\"" +
150 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" +
Sen Jiang2703ef42017-03-16 13:36:21 -0700151 codebase +
152 "\"/></urls>"
153 "<manifest version=\"" +
154 version +
155 "\">"
156 "<packages><package hash=\"not-used\" name=\"" +
157 filename + "\" size=\"" + base::Int64ToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800158 "\" hash_sha256=\"" + hash + "\"/>" +
159 (multi_package ? "<package name=\"package2\" size=\"222\" "
160 "hash_sha256=\"hash2\"/>"
161 : "") +
162 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700163 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700164 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
165 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700166 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700167 "IsDeltaPayload=\"true" +
168 (multi_package ? ":false" : "") +
169 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700170 "MaxDaysToScatter=\"" +
171 max_days_to_scatter +
172 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700173 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700174 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
175 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
176 : "") +
177 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700178 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700179 "/></actions></manifest></updatecheck></app>" +
180 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700181 ? "<app appid=\"" + app_id2 + "\"" +
182 (include_cohorts ? " cohort=\"cohort2\"" : "") +
183 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700184 codebase2 + "\"/></urls><manifest version=\"" + version2 +
185 "\"><packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700186 "<package name=\"package3\" size=\"333\" "
187 "hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700188 "<actions><action event=\"postinstall\" " +
189 (multi_app_self_update
190 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
191 : "IsDeltaPayload=\"false\" ") +
192 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700193 "</manifest></updatecheck></app>"
194 : "") +
195 (multi_app_no_update
196 ? "<app><updatecheck status=\"noupdate\"/></app>"
197 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700198 (multi_app_skip_updatecheck
199 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
200 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700201 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700202 }
203
204 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800205 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700206
Alex Deymo85616652015-10-15 18:48:31 -0700207 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700208 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700209 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
210 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700211 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700212 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700213 string more_info_url = "http://more/info";
214 string prompt = "true";
215 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700216 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700217 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700218 string hash = "4841534831323334";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800219 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700220 string deadline = "";
221 string max_days_to_scatter = "7";
222 string elapsed_days = "42";
223
224 // P2P setting defaults to allowed.
225 bool disable_p2p_for_downloading = false;
226 bool disable_p2p_for_sharing = false;
227
Sen Jiangfe284402018-03-21 14:03:50 -0700228 bool powerwash = false;
229
Alex Deymo8e18f932015-03-27 16:16:59 -0700230 // Omaha cohorts settings.
231 bool include_cohorts = false;
232 string cohort = "";
233 string cohorthint = "";
234 string cohortname = "";
235
236 // Whether to include the CrOS <!ENTITY> in the XML response.
237 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800238
Sen Jiang81259682017-03-30 15:11:30 -0700239 // Whether to include more than one app.
240 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700241 // Whether to include an app with noupdate="true".
242 bool multi_app_self_update = false;
243 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700244 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700245 // Whether to include an additional app with no updatecheck tag.
246 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700247 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800248 bool multi_package = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700249
250 // Whether the payload is a rollback.
251 bool rollback = false;
252 // The verified boot firmware key version for the rollback image.
253 string rollback_firmware_version = "";
254 // The verified boot kernel key version for the rollback image.
255 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700256 // The number of milestones back that the verified boot key version has been
257 // supplied.
258 uint32_t rollback_allowed_milestones = 0;
259 // The verified boot key version for the
260 // |current - rollback_allowed_milestones| most recent release.
261 // The pair contains <firmware_key_version, kernel_key_version> each
262 // of which is in the form "key_version.version".
263 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700264};
265
266} // namespace
267
Darin Petkov6a5b3222010-07-13 14:55:28 -0700268namespace chromeos_update_engine {
269
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700270class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
271 public:
272 OmahaRequestActionTestProcessorDelegate()
273 : expected_code_(ErrorCode::kSuccess),
274 interactive_(false),
275 test_http_fetcher_headers_(false) {}
276 ~OmahaRequestActionTestProcessorDelegate() override = default;
277
278 void ProcessingDone(const ActionProcessor* processor,
279 ErrorCode code) override {
280 brillo::MessageLoop::current()->BreakLoop();
281 }
282
283 void ActionCompleted(ActionProcessor* processor,
284 AbstractAction* action,
285 ErrorCode code) override {
286 // Make sure actions always succeed.
287 if (action->Type() == OmahaRequestAction::StaticType()) {
288 EXPECT_EQ(expected_code_, code);
289 // Check that the headers were set in the fetcher during the action. Note
290 // that we set this request as "interactive".
291 auto fetcher = static_cast<const MockHttpFetcher*>(
292 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
293
294 if (test_http_fetcher_headers_) {
295 EXPECT_EQ(interactive_ ? "fg" : "bg",
296 fetcher->GetHeader("X-Goog-Update-Interactivity"));
297 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
298 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
299 }
300 post_data_ = fetcher->post_data();
301 } else if (action->Type() ==
302 ObjectCollectorAction<OmahaResponse>::StaticType()) {
303 EXPECT_EQ(ErrorCode::kSuccess, code);
304 auto collector_action =
305 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
306 omaha_response_.reset(new OmahaResponse(collector_action->object()));
307 EXPECT_TRUE(omaha_response_);
308 } else {
309 EXPECT_EQ(ErrorCode::kSuccess, code);
310 }
311 }
312 ErrorCode expected_code_;
313 brillo::Blob post_data_;
314 bool interactive_;
315 bool test_http_fetcher_headers_;
316 std::unique_ptr<OmahaResponse> omaha_response_;
317};
318
Amin Hassani41ac04b2019-03-29 11:31:03 -0700319struct TestUpdateCheckParams {
320 string http_response;
321 int fail_http_response_code;
322 bool ping_only;
323 bool is_consumer_device;
324 int rollback_allowed_milestones;
325 bool is_policy_loaded;
326 ErrorCode expected_code;
327 metrics::CheckResult expected_check_result;
328 metrics::CheckReaction expected_check_reaction;
329 metrics::DownloadErrorCode expected_download_error_code;
330};
331
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700332class OmahaRequestActionTest : public ::testing::Test {
333 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800334 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200335 request_params_.set_os_sp("service_pack");
336 request_params_.set_os_board("x86-generic");
337 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700338 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200339 request_params_.set_app_lang("en-US");
340 request_params_.set_current_channel("unittest");
341 request_params_.set_target_channel("unittest");
342 request_params_.set_hwid("OEM MODEL 09235 7471");
343 request_params_.set_fw_version("ChromeOSFirmware.1.0");
344 request_params_.set_ec_version("0X0A1");
345 request_params_.set_delta_okay(true);
346 request_params_.set_interactive(false);
347 request_params_.set_update_url("http://url");
348 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200349 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200350 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700351 request_params_.set_is_install(false);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800352 request_params_.set_dlc_module_ids({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200353
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700354 fake_system_state_.set_request_params(&request_params_);
355 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700356
357 // Setting the default update check params. Lookup |TestUpdateCheck()|.
358 tuc_params_ = {
359 .http_response = "",
360 .fail_http_response_code = -1,
361 .ping_only = false,
362 .is_consumer_device = true,
363 .rollback_allowed_milestones = 0,
364 .is_policy_loaded = false,
365 .expected_code = ErrorCode::kSuccess,
366 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
367 .expected_check_reaction = metrics::CheckReaction::kUpdating,
368 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
369 };
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700370 }
371
Amin Hassani41ac04b2019-03-29 11:31:03 -0700372 // This function uses the paramets in |tuc_params_| to do an update check. It
373 // will fill out |post_str| with the result data and |response| with
374 // |OmahaResponse|. Returns true iff an output response was obtained from the
375 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
376 // transfer will fail with that code. |ping_only| is passed through to the
377 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700378 //
379 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700380 // |expected_error_code| parameters are for checking expectations about
381 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
382 // statistics. Use the appropriate ::kUnset value to specify that the given
383 // metric should not be reported.
384 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700385
Amin Hassani41ac04b2019-03-29 11:31:03 -0700386 // Tests events using |event| and |https_response|. It will fill up |post_str|
387 // with the result data.
388 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700389
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800390 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700391 // only a ping or also an updatecheck.
392 void PingTest(bool ping_only);
393
394 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800395 bool InstallDateParseHelper(const string& elapsed_days,
396 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700397
398 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800399 void P2PTest(bool initial_allow_p2p_for_downloading,
400 bool initial_allow_p2p_for_sharing,
401 bool omaha_disable_p2p_for_downloading,
402 bool omaha_disable_p2p_for_sharing,
403 bool payload_state_allow_p2p_attempt,
404 bool expect_p2p_client_lookup,
405 const string& p2p_client_result_url,
406 bool expected_allow_p2p_for_downloading,
407 bool expected_allow_p2p_for_sharing,
408 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700409
410 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700411 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200412 // Used by all tests.
413 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700414
415 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700416
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700417 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700418
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700419 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700420
421 TestUpdateCheckParams tuc_params_;
422
423 // TODO(ahassani): Add trailing _ to these two variables.
424 OmahaResponse response;
425 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700426};
427
Amin Hassani41ac04b2019-03-29 11:31:03 -0700428bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700429 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700430 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700431 auto fetcher =
432 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
433 tuc_params_.http_response.size(),
434 nullptr);
435 if (tuc_params_.fail_http_response_code >= 0) {
436 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700437 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200438 // This ensures the tests didn't forget to update fake_system_state_ if they
439 // are not using the default request_params_.
440 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
441
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700442 auto omaha_request_action = std::make_unique<OmahaRequestAction>(
Amin Hassani41ac04b2019-03-29 11:31:03 -0700443 &fake_system_state_, nullptr, std::move(fetcher), tuc_params_.ping_only);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700444
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800445 auto mock_policy_provider =
446 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
447 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700448 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800449
450 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700451 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800452
453 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700454 const bool get_allowed_milestone_succeeds =
455 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800456 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700457 .WillRepeatedly(
458 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
459 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800460
461 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
462 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700463 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800464
Amin Hassani41ac04b2019-03-29 11:31:03 -0700465 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700466 delegate_.interactive_ = request_params_.interactive();
467 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700468 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700469 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700470
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700471 auto collector_action =
472 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
473 BondActions(omaha_request_action.get(), collector_action.get());
474 processor.EnqueueAction(std::move(omaha_request_action));
475 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700476
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700477 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
478 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800479 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700480
Amin Hassani41ac04b2019-03-29 11:31:03 -0700481 EXPECT_CALL(
482 *fake_system_state_.mock_metrics_reporter(),
483 ReportUpdateCheckMetrics(_,
484 tuc_params_.expected_check_result,
485 tuc_params_.expected_check_reaction,
486 tuc_params_.expected_download_error_code))
487 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800488
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700489 loop.PostTask(base::Bind(
490 [](ActionProcessor* processor) { processor->StartProcessing(); },
491 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700492 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700493 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700494 if (delegate_.omaha_response_)
495 response = *delegate_.omaha_response_;
496 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700497 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700498}
499
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700500// Tests Event requests -- they should always succeed. |out_post_data| may be
501// null; if non-null, the post-data received by the mock HttpFetcher is
502// returned.
503void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700504 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700505 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700506 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700507
508 auto action = std::make_unique<OmahaRequestAction>(
509 &fake_system_state_,
510 event,
511 std::make_unique<MockHttpFetcher>(
512 http_response.data(), http_response.size(), nullptr),
513 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700514 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700515 processor.set_delegate(&delegate_);
516 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700517
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700518 loop.PostTask(base::Bind(
519 [](ActionProcessor* processor) { processor->StartProcessing(); },
520 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700521 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700522 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700523
Amin Hassani41ac04b2019-03-29 11:31:03 -0700524 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700525}
526
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700527TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700528 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700529 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
530 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
531 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
532 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
533
534 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400535 EXPECT_FALSE(response.update_exists);
536}
537
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700538TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700539 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
540 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
541 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
542
543 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700544 EXPECT_FALSE(response.update_exists);
545}
546
Sen Jiang81259682017-03-30 15:11:30 -0700547TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700548 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700549 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
550 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
551 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
552
553 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700554 EXPECT_FALSE(response.update_exists);
555}
556
Sen Jiang00adf7b2017-06-26 15:57:29 -0700557TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700558 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700559 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
560 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
561 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
562
563 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700564 EXPECT_FALSE(response.update_exists);
565}
566
567TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700568 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700569 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
570 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700571 "</manifest></updatecheck></app></response>";
572 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
573 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
574
575 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700576 EXPECT_FALSE(response.update_exists);
577}
578
Alex Deymo8e18f932015-03-27 16:16:59 -0700579// Test that all the values in the response are parsed in a normal update
580// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700581TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700582 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700583 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
584
585 ASSERT_TRUE(TestUpdateCheck());
586
Darin Petkov6a5b3222010-07-13 14:55:28 -0700587 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700588 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700589 EXPECT_EQ("", response.system_version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800590 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
591 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700592 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800593 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
594 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700595 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700596 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
597 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700598 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700599 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700600 // persisted.
601 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
602 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
603 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700604}
605
Sen Jiang0affc2c2017-02-10 15:55:05 -0800606TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800607 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700608 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
609
610 ASSERT_TRUE(TestUpdateCheck());
611
Sen Jiang0affc2c2017-02-10 15:55:05 -0800612 EXPECT_TRUE(response.update_exists);
613 EXPECT_EQ(fake_update_response_.version, response.version);
614 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
615 response.packages[0].payload_urls[0]);
616 EXPECT_EQ(fake_update_response_.codebase + "package2",
617 response.packages[1].payload_urls[0]);
618 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
619 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700620 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700621 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800622 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700623 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800624 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700625 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700626 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700627}
628
629TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700630 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700631 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
632
633 ASSERT_TRUE(TestUpdateCheck());
634
Sen Jiang81259682017-03-30 15:11:30 -0700635 EXPECT_TRUE(response.update_exists);
636 EXPECT_EQ(fake_update_response_.version, response.version);
637 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
638 response.packages[0].payload_urls[0]);
639 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
640 response.packages[1].payload_urls[0]);
641 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
642 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
643 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700644 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700645 ASSERT_EQ(2u, response.packages.size());
646 EXPECT_EQ(string("hash3"), response.packages[1].hash);
647 EXPECT_EQ(333u, response.packages[1].size);
648 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700649 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700650}
651
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700652TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) {
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700653 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700654 // Trigger the lining up of the app and system versions.
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700655 request_params_.set_system_app_id(fake_update_response_.app_id2);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700656 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700657
Amin Hassani41ac04b2019-03-29 11:31:03 -0700658 ASSERT_TRUE(TestUpdateCheck());
659
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700660 EXPECT_TRUE(response.update_exists);
661 EXPECT_EQ(fake_update_response_.version, response.version);
662 EXPECT_EQ(fake_update_response_.version2, response.system_version);
663 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
664 response.packages[0].payload_urls[0]);
665 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
666 response.packages[1].payload_urls[0]);
667 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
668 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
669 EXPECT_EQ(11u, response.packages[0].metadata_size);
670 EXPECT_EQ(true, response.packages[0].is_delta);
671 ASSERT_EQ(2u, response.packages.size());
672 EXPECT_EQ(string("hash3"), response.packages[1].hash);
673 EXPECT_EQ(333u, response.packages[1].size);
674 EXPECT_EQ(33u, response.packages[1].metadata_size);
675 EXPECT_EQ(false, response.packages[1].is_delta);
676}
677
Sen Jiang81259682017-03-30 15:11:30 -0700678TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700679 fake_update_response_.multi_app = true;
680 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700681 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
682
683 ASSERT_TRUE(TestUpdateCheck());
684
Sen Jiang81259682017-03-30 15:11:30 -0700685 EXPECT_TRUE(response.update_exists);
686 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700687 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700688 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
689 response.packages[0].payload_urls[0]);
690 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
691 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
692 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700693 ASSERT_EQ(2u, response.packages.size());
694 EXPECT_EQ(string("hash3"), response.packages[1].hash);
695 EXPECT_EQ(333u, response.packages[1].size);
696 EXPECT_EQ(33u, response.packages[1].metadata_size);
697 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700698}
699
700TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700701 fake_update_response_.multi_app = true;
702 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700703 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
704
705 ASSERT_TRUE(TestUpdateCheck());
706
Sen Jiang81259682017-03-30 15:11:30 -0700707 EXPECT_TRUE(response.update_exists);
708 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700709 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700710 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
711 response.packages[0].payload_urls[0]);
712 EXPECT_EQ(fake_update_response_.codebase + "package2",
713 response.packages[1].payload_urls[0]);
714 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
715 response.packages[2].payload_urls[0]);
716 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
717 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
718 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700719 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700720 ASSERT_EQ(3u, response.packages.size());
721 EXPECT_EQ(string("hash2"), response.packages[1].hash);
722 EXPECT_EQ(222u, response.packages[1].size);
723 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700724 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700725 EXPECT_EQ(string("hash3"), response.packages[2].hash);
726 EXPECT_EQ(333u, response.packages[2].size);
727 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700728 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800729}
730
Sen Jiangfe284402018-03-21 14:03:50 -0700731TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700732 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700733 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
734
735 ASSERT_TRUE(TestUpdateCheck());
736
Sen Jiangfe284402018-03-21 14:03:50 -0700737 EXPECT_TRUE(response.update_exists);
738 EXPECT_TRUE(response.powerwash_required);
739}
740
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700741TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700742 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700743 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700744 tuc_params_.http_response = "invalid xml>";
745 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
746 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
747 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
748
749 ASSERT_FALSE(TestUpdateCheck());
750
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700751 EXPECT_FALSE(response.update_exists);
752}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700753
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700754TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700755 request_params_.set_interactive(false);
756 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700757 tuc_params_.http_response = "invalid xml>";
758 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
759 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
760 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
761
762 ASSERT_FALSE(TestUpdateCheck());
763
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700764 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700765}
766
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700767TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700768 // Set up a connection manager that doesn't allow a valid update over
769 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700770 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700771 fake_system_state_.set_connection_manager(&mock_cm);
772
Alex Deymo30534502015-07-20 15:06:33 -0700773 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800774 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
775 SetArgPointee<1>(ConnectionTethering::kUnknown),
776 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700777 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
778 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700779
Amin Hassani41ac04b2019-03-29 11:31:03 -0700780 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
781 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
782 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
783
784 ASSERT_FALSE(TestUpdateCheck());
785
Chris Sosa77f79e82014-06-02 18:16:24 -0700786 EXPECT_FALSE(response.update_exists);
787}
788
Weidong Guo421ff332017-04-17 10:08:38 -0700789TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
790 // This test tests that update over cellular is allowed as device policy
791 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700792 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700793 fake_system_state_.set_connection_manager(&mock_cm);
794
795 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
796 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
797 SetArgPointee<1>(ConnectionTethering::kUnknown),
798 Return(true)));
799 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
800 .WillRepeatedly(Return(true));
801 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
802 .WillRepeatedly(Return(true));
803
Amin Hassani41ac04b2019-03-29 11:31:03 -0700804 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
805
806 ASSERT_TRUE(TestUpdateCheck());
807
Weidong Guo421ff332017-04-17 10:08:38 -0700808 EXPECT_TRUE(response.update_exists);
809}
810
811TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
812 // This test tests that update over cellular is blocked as device policy
813 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700814 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700815 fake_system_state_.set_connection_manager(&mock_cm);
816
817 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
818 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
819 SetArgPointee<1>(ConnectionTethering::kUnknown),
820 Return(true)));
821 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
822 .WillRepeatedly(Return(true));
823 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
824 .WillRepeatedly(Return(false));
825
Amin Hassani41ac04b2019-03-29 11:31:03 -0700826 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
827 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
828 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
829
830 ASSERT_FALSE(TestUpdateCheck());
831
Weidong Guo421ff332017-04-17 10:08:38 -0700832 EXPECT_FALSE(response.update_exists);
833}
834
835TEST_F(OmahaRequestActionTest,
836 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
837 // This test tests that, when device policy is not set, update over cellular
838 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700839 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700840 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
841 fake_system_state_.set_connection_manager(&mock_cm);
842
843 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
844 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
845 SetArgPointee<1>(ConnectionTethering::kUnknown),
846 Return(true)));
847 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
848 .WillRepeatedly(Return(false));
849 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
850 .WillRepeatedly(Return(true));
851
Amin Hassani41ac04b2019-03-29 11:31:03 -0700852 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
853
854 ASSERT_TRUE(TestUpdateCheck());
855
Weidong Guo421ff332017-04-17 10:08:38 -0700856 EXPECT_TRUE(response.update_exists);
857}
858
859TEST_F(OmahaRequestActionTest,
860 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
861 // This test tests that, when device policy is not set and permission for
862 // update over cellular is set to false or does not exist, update over
863 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700864 MockConnectionManager mock_cm;
865 // A version different from the version in omaha response.
866 string diff_version = "99.99.99";
867 // A size different from the size in omaha response.
868 int64_t diff_size = 999;
869
870 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
871 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
872 // This test tests cellular (3G) being the only connection type being allowed.
873 fake_system_state_.set_connection_manager(&mock_cm);
874
875 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
876 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
877 SetArgPointee<1>(ConnectionTethering::kUnknown),
878 Return(true)));
879 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
880 .WillRepeatedly(Return(false));
881 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
882 .WillRepeatedly(Return(true));
883
Amin Hassani41ac04b2019-03-29 11:31:03 -0700884 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
885 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
886 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
887
888 ASSERT_FALSE(TestUpdateCheck());
889
Weidong Guo421ff332017-04-17 10:08:38 -0700890 EXPECT_FALSE(response.update_exists);
891}
892
893TEST_F(OmahaRequestActionTest,
894 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
895 // This test tests that, when device policy is not set and permission for
896 // update over cellular is set to false or does not exist, update over
897 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700898 MockConnectionManager mock_cm;
899 // A version same as the version in omaha response.
900 string new_version = fake_update_response_.version;
901 // A size same as the size in omaha response.
902 int64_t new_size = fake_update_response_.size;
903
904 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
905 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
906 fake_system_state_.set_connection_manager(&mock_cm);
907
908 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
909 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
910 SetArgPointee<1>(ConnectionTethering::kUnknown),
911 Return(true)));
912 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
913 .WillRepeatedly(Return(false));
914 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
915 .WillRepeatedly(Return(true));
916
Amin Hassani41ac04b2019-03-29 11:31:03 -0700917 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
918
919 ASSERT_TRUE(TestUpdateCheck());
920
Weidong Guo421ff332017-04-17 10:08:38 -0700921 EXPECT_TRUE(response.update_exists);
922}
923
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700924TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700925 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700926 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700927 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700928 fake_update_response_.version = rollback_version;
929 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
930 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
931 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700932
Chris Sosa77f79e82014-06-02 18:16:24 -0700933 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800934 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700935
Amin Hassani41ac04b2019-03-29 11:31:03 -0700936 ASSERT_FALSE(TestUpdateCheck());
937
Chris Sosa77f79e82014-06-02 18:16:24 -0700938 EXPECT_FALSE(response.update_exists);
939}
940
Marton Hunyadyc2882062018-05-14 17:28:25 +0200941// Verify that update checks called during OOBE will not try to download an
942// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700943TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200944 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700945 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
946 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
947 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
948 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -0700949
Sen Jiang8cd42342018-01-31 12:06:59 -0800950 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700951 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700952 ASSERT_FALSE(TestUpdateCheck());
953
Kevin Cernekee2494e282016-03-29 18:03:53 -0700954 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +0200955}
Kevin Cernekee2494e282016-03-29 18:03:53 -0700956
Marton Hunyadyc2882062018-05-14 17:28:25 +0200957// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
958// enabled.
959TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200960 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700961 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700962 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700963
Amin Hassani41ac04b2019-03-29 11:31:03 -0700964 ASSERT_TRUE(TestUpdateCheck());
965
Kevin Cernekee2494e282016-03-29 18:03:53 -0700966 EXPECT_TRUE(response.update_exists);
967}
968
Marton Hunyadyc2882062018-05-14 17:28:25 +0200969// Verify that update checks called during OOBE will still try to download an
970// update if the response includes the deadline field.
971TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200972 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
973 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700974 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +0200975
Amin Hassani41ac04b2019-03-29 11:31:03 -0700976 ASSERT_TRUE(TestUpdateCheck());
977
Marton Hunyadyc2882062018-05-14 17:28:25 +0200978 EXPECT_TRUE(response.update_exists);
979}
980
981// Verify that update checks called during OOBE will not try to download an
982// update if a rollback happened, even when the response includes the deadline
983// field.
984TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200985 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
986 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700987 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
988 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
989 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
990 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
991
Marton Hunyadyc2882062018-05-14 17:28:25 +0200992 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
993 .WillOnce(Return(true));
994
Amin Hassani41ac04b2019-03-29 11:31:03 -0700995 ASSERT_FALSE(TestUpdateCheck());
996
Marton Hunyadyc2882062018-05-14 17:28:25 +0200997 EXPECT_FALSE(response.update_exists);
998}
999
Toni Barzic61544e62018-10-11 14:37:30 -07001000// Verify that non-critical updates are skipped by reporting the
1001// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1002// i.e. when the update would need user permission. Note that reporting
1003// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1004// in OOBE (warning the user about an update that will be skipped).
1005TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001006 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1007
1008 MockConnectionManager mock_cm;
1009 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001010 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1011 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1012 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1013 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001014
1015 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1016 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1017 SetArgPointee<1>(ConnectionTethering::kUnknown),
1018 Return(true)));
1019 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1020 .WillRepeatedly(Return(false));
1021
Amin Hassani41ac04b2019-03-29 11:31:03 -07001022 ASSERT_FALSE(TestUpdateCheck());
1023
Toni Barzic61544e62018-10-11 14:37:30 -07001024 EXPECT_FALSE(response.update_exists);
1025}
1026
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001027TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001028 request_params_.set_wall_clock_based_wait_enabled(true);
1029 request_params_.set_update_check_count_wait_enabled(false);
1030 request_params_.set_waiting_period(TimeDelta::FromDays(2));
May Lippert60aa3ca2018-08-15 16:55:29 -07001031 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001032 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1033 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1034 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001035
Amin Hassani41ac04b2019-03-29 11:31:03 -07001036 ASSERT_FALSE(TestUpdateCheck());
1037
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001038 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001039}
1040
1041TEST_F(OmahaRequestActionTest,
1042 WallClockBasedWaitAloneCausesScatteringInteractive) {
1043 request_params_.set_wall_clock_based_wait_enabled(true);
1044 request_params_.set_update_check_count_wait_enabled(false);
1045 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1046 request_params_.set_interactive(true);
1047 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1048 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001049
1050 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001051 ASSERT_TRUE(TestUpdateCheck());
1052
Chris Sosa968d0572013-08-23 14:46:02 -07001053 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001054}
1055
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001056TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001057 request_params_.set_wall_clock_based_wait_enabled(false);
1058 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1059 request_params_.set_update_check_count_wait_enabled(true);
1060 request_params_.set_min_update_checks_needed(1);
1061 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001062 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001063
Amin Hassani41ac04b2019-03-29 11:31:03 -07001064 ASSERT_TRUE(TestUpdateCheck());
1065
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001066 EXPECT_TRUE(response.update_exists);
1067}
1068
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001069TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001070 request_params_.set_wall_clock_based_wait_enabled(true);
1071 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1072 request_params_.set_update_check_count_wait_enabled(true);
1073 request_params_.set_min_update_checks_needed(1);
1074 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001075 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001076 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1077
1078 ASSERT_TRUE(TestUpdateCheck());
1079
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001080 EXPECT_TRUE(response.update_exists);
1081}
1082
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001083TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001084 request_params_.set_wall_clock_based_wait_enabled(true);
1085 request_params_.set_waiting_period(TimeDelta());
1086 request_params_.set_update_check_count_wait_enabled(true);
1087 request_params_.set_min_update_checks_needed(0);
1088 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001089 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001090 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001091
Amin Hassani41ac04b2019-03-29 11:31:03 -07001092 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001093
Ben Chan9abb7632014-08-07 00:10:53 -07001094 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001095 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001096 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001097 EXPECT_TRUE(response.update_exists);
1098}
1099
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001100TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001101 request_params_.set_wall_clock_based_wait_enabled(true);
1102 request_params_.set_waiting_period(TimeDelta());
1103 request_params_.set_update_check_count_wait_enabled(true);
1104 request_params_.set_min_update_checks_needed(1);
1105 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001106 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001107 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1108 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1109 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001110
Amin Hassani41ac04b2019-03-29 11:31:03 -07001111 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001112
Ben Chan9abb7632014-08-07 00:10:53 -07001113 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001114 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001115 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001116 EXPECT_FALSE(response.update_exists);
1117}
1118
Amin Hassani41ac04b2019-03-29 11:31:03 -07001119TEST_F(OmahaRequestActionTest,
1120 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001121 request_params_.set_wall_clock_based_wait_enabled(true);
1122 request_params_.set_waiting_period(TimeDelta());
1123 request_params_.set_update_check_count_wait_enabled(true);
1124 request_params_.set_min_update_checks_needed(1);
1125 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001126 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001127 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001128 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1129
1130 // Verify if we are interactive check we don't defer.
1131 ASSERT_TRUE(TestUpdateCheck());
1132
1133 EXPECT_TRUE(response.update_exists);
1134}
1135
1136TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1137 request_params_.set_wall_clock_based_wait_enabled(true);
1138 request_params_.set_waiting_period(TimeDelta());
1139 request_params_.set_update_check_count_wait_enabled(true);
1140 request_params_.set_min_update_checks_needed(1);
1141 request_params_.set_max_update_checks_allowed(8);
1142 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1143 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1144 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1145 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001146
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001147 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001148 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001149
Ben Chan9abb7632014-08-07 00:10:53 -07001150 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001151 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001152 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001153 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001154 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001155 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001156}
1157
1158TEST_F(OmahaRequestActionTest,
1159 ExistingUpdateCheckCountCausesScatteringInteractive) {
1160 request_params_.set_wall_clock_based_wait_enabled(true);
1161 request_params_.set_waiting_period(TimeDelta());
1162 request_params_.set_update_check_count_wait_enabled(true);
1163 request_params_.set_min_update_checks_needed(1);
1164 request_params_.set_max_update_checks_allowed(8);
1165 request_params_.set_interactive(true);
1166 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1167 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1168
1169 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001170
1171 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001172 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001173 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001174}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001175
Adolfo Victoria497044c2018-07-18 07:51:42 -07001176TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1177 // If staging is on, the value for max days to scatter should be ignored, and
1178 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001179 request_params_.set_wall_clock_based_wait_enabled(true);
1180 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1181 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001182 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1183
Adolfo Victoria497044c2018-07-18 07:51:42 -07001184 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1185 // This should not prevent scattering due to staging.
1186 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001187 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1188 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1189 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1190
1191 ASSERT_FALSE(TestUpdateCheck());
1192
Adolfo Victoria497044c2018-07-18 07:51:42 -07001193 EXPECT_FALSE(response.update_exists);
1194
1195 // Interactive updates should not be affected.
1196 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001197 tuc_params_.expected_code = ErrorCode::kSuccess;
1198 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1199
1200 ASSERT_TRUE(TestUpdateCheck());
1201
Adolfo Victoria497044c2018-07-18 07:51:42 -07001202 EXPECT_TRUE(response.update_exists);
1203}
1204
Alex Deymo8e18f932015-03-27 16:16:59 -07001205TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001206 fake_update_response_.include_cohorts = true;
1207 fake_update_response_.cohort = "s/154454/8479665";
1208 fake_update_response_.cohorthint = "please-put-me-on-beta";
1209 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001210 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001211
Amin Hassani41ac04b2019-03-29 11:31:03 -07001212 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001213
1214 string value;
1215 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1216 EXPECT_EQ(fake_update_response_.cohort, value);
1217
1218 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1219 EXPECT_EQ(fake_update_response_.cohorthint, value);
1220
1221 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1222 EXPECT_EQ(fake_update_response_.cohortname, value);
1223}
1224
1225TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001226 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1227 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1228 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1229 fake_update_response_.include_cohorts = true;
1230 fake_update_response_.cohort = "s/154454/8479665";
1231 fake_update_response_.cohorthint = "please-put-me-on-beta";
1232 fake_update_response_.cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001233 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001234
Amin Hassani41ac04b2019-03-29 11:31:03 -07001235 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001236
1237 string value;
1238 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1239 EXPECT_EQ(fake_update_response_.cohort, value);
1240
1241 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1242 EXPECT_EQ(fake_update_response_.cohorthint, value);
1243
1244 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1245}
1246
1247TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001248 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001249
Amin Hassani41ac04b2019-03-29 11:31:03 -07001250 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1251 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001252
1253 string value;
1254 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1255 EXPECT_EQ("old_value", value);
1256
1257 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1258 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1259}
1260
Alex Deymo00d79ac2015-06-29 15:41:49 -07001261TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001262 fake_update_response_.include_cohorts = true;
1263 fake_update_response_.cohort = "s/154454/8479665";
1264 fake_update_response_.cohorthint = "please-put-me-on-beta";
1265 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001266 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1267 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1268 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001269
Amin Hassani41ac04b2019-03-29 11:31:03 -07001270 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001271
1272 string value;
1273 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1274 EXPECT_EQ(fake_update_response_.cohort, value);
1275
1276 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1277 EXPECT_EQ(fake_update_response_.cohorthint, value);
1278
1279 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1280 EXPECT_EQ(fake_update_response_.cohortname, value);
1281}
1282
Sen Jiangb1e063a2017-09-15 17:44:31 -07001283TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001284 fake_update_response_.multi_app = true;
1285 fake_update_response_.include_cohorts = true;
1286 fake_update_response_.cohort = "s/154454/8479665";
1287 fake_update_response_.cohorthint = "please-put-me-on-beta";
1288 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001289 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001290
Amin Hassani41ac04b2019-03-29 11:31:03 -07001291 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001292
1293 string value;
1294 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1295 EXPECT_EQ(fake_update_response_.cohort, value);
1296
1297 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1298 EXPECT_EQ(fake_update_response_.cohorthint, value);
1299
1300 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1301 EXPECT_EQ(fake_update_response_.cohortname, value);
1302}
1303
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001304TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001305 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001306 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001307 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001308
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001309 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001310 &fake_system_state_,
1311 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001312 std::make_unique<MockHttpFetcher>(
1313 http_response.data(), http_response.size(), nullptr),
Ben Chan5c02c132017-06-27 07:10:36 -07001314 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001315 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001316 processor.set_delegate(&delegate_);
1317 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001318
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001319 loop.PostTask(base::Bind(
1320 [](ActionProcessor* processor) { processor->StartProcessing(); },
1321 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001322 loop.Run();
1323 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001324 EXPECT_FALSE(processor.IsRunning());
1325}
1326
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001327TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001328 tuc_params_.http_response = "invalid xml>";
1329 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1330 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1331 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1332
1333 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001334 EXPECT_FALSE(response.update_exists);
1335}
1336
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001337TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001338 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1339 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1340 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1341
1342 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001343 EXPECT_FALSE(response.update_exists);
1344}
1345
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001346TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001347 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001348 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1349 "<daystart elapsed_seconds=\"100\"/>"
1350 "<app appid=\"foo\" status=\"ok\">"
1351 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001352 "<updatecheck/></app></response>";
1353 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1354 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1355 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1356
1357 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001358 EXPECT_FALSE(response.update_exists);
1359}
1360
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001361TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001362 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001363 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1364 "<daystart elapsed_seconds=\"100\"/>"
1365 "<app appid=\"foo\" status=\"ok\">"
1366 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001367 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1368 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1369 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1370 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1371
1372 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001373 EXPECT_FALSE(response.update_exists);
1374}
1375
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001376TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001377 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001378 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1379 "<daystart elapsed_seconds=\"100\"/>"
1380 "<app appid=\"foo\" status=\"ok\">"
1381 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001382 "</app></response>";
1383 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1384 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1385 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1386
1387 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001388 EXPECT_FALSE(response.update_exists);
1389}
1390
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001391TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001392 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001393 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1394 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001395 // the appid needs to match that in the request params
1396 "<app appid=\"" +
1397 fake_update_response_.app_id +
1398 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001399 "<updatecheck status=\"ok\">"
1400 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001401 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001402 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001403 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001404 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001405 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001406 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001407 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001408 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001409
Amin Hassani41ac04b2019-03-29 11:31:03 -07001410 ASSERT_TRUE(TestUpdateCheck());
1411
Darin Petkov6a5b3222010-07-13 14:55:28 -07001412 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001413 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001414 EXPECT_EQ("http://missing/field/test/f",
1415 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001416 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001417 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1418 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001419 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001420 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001421}
1422
1423namespace {
1424class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1425 public:
1426 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001427 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001428 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001429};
1430
Alex Deymo60ca1a72015-06-18 18:19:15 -07001431void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001432 processor->StartProcessing();
1433 CHECK(processor->IsRunning());
1434 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001435}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001436} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001437
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001438TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001439 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001440 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001441
Alex Deymo60ca1a72015-06-18 18:19:15 -07001442 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001443 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001444 &fake_system_state_,
1445 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001446 std::make_unique<MockHttpFetcher>(
1447 http_response.data(), http_response.size(), nullptr),
Ben Chan5c02c132017-06-27 07:10:36 -07001448 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001449 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001450 ActionProcessor processor;
1451 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001452 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001453
Alex Deymo60ca1a72015-06-18 18:19:15 -07001454 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1455 loop.Run();
1456 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001457}
1458
Alex Deymob0d74eb2015-03-30 17:59:17 -07001459TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001460 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001461 request_params_.set_os_sp("testtheservice_pack>");
1462 request_params_.set_os_board("x86 generic<id");
1463 request_params_.set_current_channel("unittest_track&lt;");
1464 request_params_.set_target_channel("unittest_track&lt;");
1465 request_params_.set_hwid("<OEM MODEL>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001466 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1467 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001468 fake_prefs_.SetString(
1469 kPrefsOmahaCohortName,
1470 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001471 tuc_params_.http_response = "invalid xml>";
1472 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1473 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1474 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1475
1476 ASSERT_FALSE(TestUpdateCheck());
1477
Alex Deymo8e18f932015-03-27 16:16:59 -07001478 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1479 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1480 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1481 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1482 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1483 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1484 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1485 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1486 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1487 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1488 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1489 // Values from Prefs that are too big are removed from the XML instead of
1490 // encoded.
1491 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001492}
1493
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001494TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001495 fake_update_response_.deadline = "&lt;20110101";
1496 fake_update_response_.more_info_url = "testthe&lt;url";
1497 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001498 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1499
1500 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001501
Sen Jiang0affc2c2017-02-10 15:55:05 -08001502 EXPECT_EQ("testthe<url", response.more_info_url);
1503 EXPECT_EQ("testthe&codebase/file.signed",
1504 response.packages[0].payload_urls[0]);
1505 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001506}
1507
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001508TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001509 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001510 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001511 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001512
Amin Hassani41ac04b2019-03-29 11:31:03 -07001513 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001514 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001515}
1516
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001517TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001518 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001519 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001520 tuc_params_.http_response = "invalid xml>";
1521 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1522 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1523 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001524
Darin Petkov95508da2011-01-05 12:42:29 -08001525 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001526 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001527 // An existing but empty previous version means that we didn't reboot to a new
1528 // update, therefore, no need to update the previous version.
1529 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001530 ASSERT_FALSE(TestUpdateCheck());
1531
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001532 EXPECT_NE(
1533 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1534 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001535 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001536 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1537 string::npos);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001538 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos);
1539 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001540 // No <event> tag should be sent if we didn't reboot to an update.
1541 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001542}
1543
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001544TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001545 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001546 "invalid xml>");
1547
Alex Vakulenko75039d72014-03-25 12:36:28 -07001548 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001549 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001550 OmahaEvent::kTypeUpdateDownloadStarted,
1551 OmahaEvent::kResultSuccess);
1552 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001553 EXPECT_EQ(post_str.find("ping"), string::npos);
1554 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001555}
1556
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001557TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001558 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001559 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001560 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001561 "invalid xml>");
1562
Alex Vakulenko75039d72014-03-25 12:36:28 -07001563 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001564 " <event eventtype=\"%d\" eventresult=\"%d\" "
1565 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001566 OmahaEvent::kTypeDownloadComplete,
1567 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001568 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001569 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001570 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001571}
1572
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001573TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001574 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001575 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001576 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001577 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001578 std::make_unique<MockHttpFetcher>(
1579 http_response.data(), http_response.size(), nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001580 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001581 EXPECT_FALSE(update_check_action.IsEvent());
1582
1583 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001584 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001585 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001586 std::make_unique<MockHttpFetcher>(
1587 http_response.data(), http_response.size(), nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001588 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001589 EXPECT_TRUE(event_action.IsEvent());
1590}
1591
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001592TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001593 tuc_params_.http_response = "invalid xml>";
1594 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1595 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1596 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1597
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001598 for (int i = 0; i < 2; i++) {
1599 bool delta_okay = i == 1;
1600 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001601 request_params_.set_delta_okay(delta_okay);
1602
Amin Hassani41ac04b2019-03-29 11:31:03 -07001603 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001604 EXPECT_NE(
1605 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1606 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001607 << "i = " << i;
1608 }
1609}
1610
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001611TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001612 tuc_params_.http_response = "invalid xml>";
1613 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1614 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1615 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1616
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001617 for (int i = 0; i < 2; i++) {
1618 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001619 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001620 request_params_.set_interactive(interactive);
1621
Amin Hassani41ac04b2019-03-29 11:31:03 -07001622 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001623 EXPECT_NE(post_str.find(
1624 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001625 string::npos)
1626 << "i = " << i;
1627 }
1628}
1629
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001630TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001631 tuc_params_.http_response = "invalid xml>";
1632 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1633 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1634 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1635
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001636 for (int i = 0; i < 2; i++) {
1637 bool target_version_set = i == 1;
1638 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001639 request_params_.set_target_version_prefix(target_version_prefix);
1640
Amin Hassani41ac04b2019-03-29 11:31:03 -07001641 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001642 if (target_version_set) {
1643 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1644 string::npos)
1645 << "i = " << i;
1646 } else {
1647 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1648 << "i = " << i;
1649 }
1650 }
1651}
1652
1653TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001654 tuc_params_.http_response = "invalid xml>";
1655 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1656 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1657 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1658
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001659 for (int i = 0; i < 4; i++) {
1660 bool rollback_allowed = i / 2 == 0;
1661 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001662 request_params_.set_target_version_prefix(target_version_set ? "10032."
1663 : "");
1664 request_params_.set_rollback_allowed(rollback_allowed);
1665
Amin Hassani41ac04b2019-03-29 11:31:03 -07001666 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001667 if (rollback_allowed && target_version_set) {
1668 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1669 << "i = " << i;
1670 } else {
1671 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1672 }
1673 }
1674}
1675
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001676TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001677 OmahaEvent default_event;
1678 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1679 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001680 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001681
1682 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1683 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1684 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001685 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001686
1687 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1688 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001689 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001690 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1691 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001692 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001693}
1694
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001695void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001696 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001697 fake_system_state_.set_prefs(&prefs);
1698 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001699 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001700 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1701 // Add a few hours to the day difference to test no rounding, etc.
1702 int64_t five_days_ago =
1703 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1704 int64_t six_days_ago =
1705 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1706 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001707 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001708 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001709 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001710 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001711 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001712
1713 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1714 tuc_params_.ping_only = ping_only;
1715 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1716 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1717
1718 ASSERT_TRUE(TestUpdateCheck());
1719
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001720 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1721 string::npos);
1722 if (ping_only) {
1723 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1724 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1725 } else {
1726 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1727 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001728 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001729}
1730
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001731TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001732 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001733}
1734
1735TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001736 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001737}
1738
1739TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001740 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001741 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001742 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001743 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001744 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001745 int64_t three_days_ago =
1746 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1747 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001748 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001749 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001750 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001751 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001752 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001753 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001754
1755 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1756 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1757 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1758
1759 ASSERT_TRUE(TestUpdateCheck());
1760
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001761 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001762}
1763
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001764TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001765 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001766 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001767 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001768 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001769 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001770 int64_t four_days_ago =
1771 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1772 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001773 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001774 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001775 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001776 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001777 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001778 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001779
1780 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1781 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1782 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1783
1784 ASSERT_TRUE(TestUpdateCheck());
1785
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001786 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001787 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001788}
1789
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001790TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001791 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001792 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001793 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001794 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001795 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001796 int64_t one_hour_ago =
1797 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001798 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001799 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001800 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001801 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001802 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001803 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001804 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1805 // send a ping.
1806 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1807 .WillOnce(Return(true));
1808 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1809 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001810
1811 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1812 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1813 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1814
1815 ASSERT_TRUE(TestUpdateCheck());
1816
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001817 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001818}
1819
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001820TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001821 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001822 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001823 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001824 int64_t now = Time::Now().ToInternalValue();
1825 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001826 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001827 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001828 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001829 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1830 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001831
1832 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1833 tuc_params_.ping_only = true;
1834 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
1835 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1836
1837 EXPECT_TRUE(TestUpdateCheck());
1838 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07001839}
1840
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001841TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001842 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001843 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001844 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001845 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001846 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001847 int64_t future =
1848 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001849 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001850 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001851 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001852 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001853 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001854 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001855 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1856 .WillOnce(Return(true));
1857 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1858 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001859
1860 tuc_params_.http_response =
1861 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1862 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
1863 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1864 "<updatecheck status=\"noupdate\"/></app></response>";
1865 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1866 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1867
1868 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001869 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001870}
1871
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001872TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001873 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001874 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001875 // may fail if it runs for longer than 5 seconds. It shouldn't run
1876 // that long though.
1877 int64_t midnight =
1878 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1879 int64_t midnight_slack =
1880 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001881 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001882 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001883 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1884 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001885 EXPECT_CALL(prefs,
1886 SetInt64(kPrefsLastActivePingDay,
1887 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001888 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001889 EXPECT_CALL(prefs,
1890 SetInt64(kPrefsLastRollCallPingDay,
1891 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001892 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001893
1894 tuc_params_.http_response =
1895 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1896 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
1897 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1898 "<updatecheck status=\"noupdate\"/></app></response>";
1899 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1900 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1901
1902 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001903}
1904
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001905TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001906 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001907 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001908 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1909 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001910 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1911 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001912
1913 tuc_params_.http_response =
1914 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1915 "protocol=\"3.0\"><daystart blah=\"200\"/>"
1916 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1917 "<updatecheck status=\"noupdate\"/></app></response>";
1918 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1919 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1920
1921 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001922}
1923
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001924TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001925 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001926 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001927 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1928 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001929 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1930 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001931
1932 tuc_params_.http_response =
1933 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1934 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
1935 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1936 "<updatecheck status=\"noupdate\"/></app></response>";
1937 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1938 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1939
1940 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001941}
1942
Alex Deymob3fa53b2016-04-18 19:57:58 -07001943TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1944 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1945 // the "eol" attribute.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001946 tuc_params_.http_response =
1947 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1948 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1949 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1950 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/></app></response>";
1951 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1952 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1953
1954 ASSERT_TRUE(TestUpdateCheck());
1955
Alex Deymob3fa53b2016-04-18 19:57:58 -07001956 string eol_pref;
1957 EXPECT_TRUE(
1958 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1959 // Note that the eol="eol" attribute should be ignored and the _eol should be
1960 // used instead.
1961 EXPECT_EQ("security-only", eol_pref);
1962}
1963
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001964TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001965 tuc_params_.http_response = "invalid xml>";
1966 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1967 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1968 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1969
1970 ASSERT_FALSE(TestUpdateCheck());
1971
Darin Petkov84c763c2010-07-29 16:27:58 -07001972 EXPECT_EQ(post_str.find("machineid="), string::npos);
1973 EXPECT_EQ(post_str.find("userid="), string::npos);
1974}
1975
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001976TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001977 const int http_error_code =
1978 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001979 tuc_params_.fail_http_response_code = 501;
1980 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
1981 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
1982 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1983 tuc_params_.expected_download_error_code =
1984 static_cast<metrics::DownloadErrorCode>(501);
1985
1986 ASSERT_FALSE(TestUpdateCheck());
1987
Darin Petkovedc522e2010-11-05 09:35:17 -07001988 EXPECT_FALSE(response.update_exists);
1989}
1990
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001991TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001992 const int http_error_code =
1993 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001994
1995 tuc_params_.fail_http_response_code = 1500;
1996 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
1997 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
1998 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1999 tuc_params_.expected_download_error_code =
2000 metrics::DownloadErrorCode::kHttpStatusOther;
2001
2002 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002003 EXPECT_FALSE(response.update_exists);
2004}
2005
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002006TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002007 request_params_.set_wall_clock_based_wait_enabled(true);
2008 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2009 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002010
Sen Jiang7c1171e2016-06-23 11:35:40 -07002011 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002012 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002013 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002014
2015 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2016 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2017 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2018
2019 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002020
Ben Chan9abb7632014-08-07 00:10:53 -07002021 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002022 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002023 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002024 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002025
2026 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002027 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002028 tuc_params_.expected_code = ErrorCode::kSuccess;
2029 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2030
2031 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002032 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002033}
2034
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002035TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002036 request_params_.set_wall_clock_based_wait_enabled(true);
2037 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2038 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002039
Sen Jiang7c1171e2016-06-23 11:35:40 -07002040 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002041 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2042 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002043 ASSERT_TRUE(
2044 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2045 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002046
Amin Hassani41ac04b2019-03-29 11:31:03 -07002047 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2048
2049 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002050 EXPECT_TRUE(response.update_exists);
2051
2052 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002053 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002054 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002055 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2056}
2057
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002058TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002059 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002060 base::ScopedTempDir tempdir;
2061 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002062
Marton Hunyady2abda312018-04-24 18:21:49 +02002063 request_params_.set_root(tempdir.GetPath().value());
2064 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2065 request_params_.set_app_version("1.2.3.4");
2066 request_params_.set_product_components("o.bundle=1");
2067 request_params_.set_current_channel("canary-channel");
2068 EXPECT_TRUE(
2069 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2070 request_params_.UpdateDownloadChannel();
2071 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002072
2073 tuc_params_.http_response = "invalid xml>";
2074 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2075 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2076 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2077
2078 ASSERT_FALSE(TestUpdateCheck());
2079
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002080 EXPECT_NE(
2081 string::npos,
2082 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2083 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2084 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002085 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002086}
2087
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002088TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002089 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002090 base::ScopedTempDir tempdir;
2091 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002092
Marton Hunyady2abda312018-04-24 18:21:49 +02002093 request_params_.set_root(tempdir.GetPath().value());
2094 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2095 request_params_.set_app_version("5.6.7.8");
2096 request_params_.set_product_components("o.bundle=1");
2097 request_params_.set_current_channel("stable-channel");
2098 EXPECT_TRUE(
2099 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2100 request_params_.UpdateDownloadChannel();
2101 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002102
2103 tuc_params_.http_response = "invalid xml>";
2104 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2105 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2106 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2107
2108 ASSERT_FALSE(TestUpdateCheck());
2109
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002110 EXPECT_NE(
2111 string::npos,
2112 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2113 "version=\"5.6.7.8\" "
2114 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002115 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002116 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002117}
2118
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002119// Checks that the initial ping with a=-1 r=-1 is not send when the device
2120// was powerwashed.
2121TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2122 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2123
2124 // Flag that the device was powerwashed in the past.
2125 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002126 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2127 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2128 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002129
Amin Hassani41ac04b2019-03-29 11:31:03 -07002130 ASSERT_TRUE(TestUpdateCheck());
2131
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002132 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002133 EXPECT_EQ(string::npos, post_str.find("<ping"));
2134}
2135
Amin Hassani1677e812017-06-21 13:36:36 -07002136// Checks that the initial ping with a=-1 r=-1 is not send when the device
2137// first_active_omaha_ping_sent is set.
2138TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2139 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2140
2141 // Flag that the device was not powerwashed in the past.
2142 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2143
2144 // Flag that the device has sent first active ping in the past.
2145 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2146
Amin Hassani41ac04b2019-03-29 11:31:03 -07002147 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2148 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2149 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2150
2151 ASSERT_TRUE(TestUpdateCheck());
2152
Amin Hassani1677e812017-06-21 13:36:36 -07002153 // We shouldn't send a ping in this case since
2154 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002155 EXPECT_EQ(string::npos, post_str.find("<ping"));
2156}
2157
Alex Deymo9fded1e2015-11-05 12:31:19 -08002158// Checks that the event 54 is sent on a reboot to a new update.
2159TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2160 // Flag that the device was updated in a previous boot.
2161 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2162
Amin Hassani41ac04b2019-03-29 11:31:03 -07002163 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2164 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2165 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2166
2167 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002168
2169 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002170 EXPECT_NE(
2171 string::npos,
2172 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2173 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002174 EXPECT_NE(string::npos,
2175 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2176
2177 // The previous version flag should have been removed.
2178 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2179 string prev_version;
2180 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2181 EXPECT_TRUE(prev_version.empty());
2182}
2183
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002184void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2185 bool initial_allow_p2p_for_sharing,
2186 bool omaha_disable_p2p_for_downloading,
2187 bool omaha_disable_p2p_for_sharing,
2188 bool payload_state_allow_p2p_attempt,
2189 bool expect_p2p_client_lookup,
2190 const string& p2p_client_result_url,
2191 bool expected_allow_p2p_for_downloading,
2192 bool expected_allow_p2p_for_sharing,
2193 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002194 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2195 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2196 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002197
2198 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002199 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002200 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2201 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002202 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2203 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2204 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2205 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2206 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2207 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2208 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2209 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2210 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2211 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2212
David Zeuthen8f191b22013-08-06 12:27:50 -07002213 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002214 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002215 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2216
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002217 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2218 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002219 .Times(expect_p2p_client_lookup ? 1 : 0);
2220
Alex Deymo8e18f932015-03-27 16:16:59 -07002221 fake_update_response_.disable_p2p_for_downloading =
2222 omaha_disable_p2p_for_downloading;
2223 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002224
2225 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2226 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2227
2228 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002229 EXPECT_TRUE(response.update_exists);
2230
Gilad Arnold74b5f552014-10-07 08:17:16 -07002231 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2232 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002233 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002234
Gilad Arnold74b5f552014-10-07 08:17:16 -07002235 EXPECT_EQ(expected_allow_p2p_for_downloading,
2236 actual_allow_p2p_for_downloading);
2237 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2238 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002239}
2240
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002241TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002242 P2PTest(true, // initial_allow_p2p_for_downloading
2243 true, // initial_allow_p2p_for_sharing
2244 false, // omaha_disable_p2p_for_downloading
2245 false, // omaha_disable_p2p_for_sharing
2246 true, // payload_state_allow_p2p_attempt
2247 true, // expect_p2p_client_lookup
2248 "http://1.3.5.7/p2p", // p2p_client_result_url
2249 true, // expected_allow_p2p_for_downloading
2250 true, // expected_allow_p2p_for_sharing
2251 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002252}
2253
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002254TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002255 P2PTest(true, // initial_allow_p2p_for_downloading
2256 true, // initial_allow_p2p_for_sharing
2257 false, // omaha_disable_p2p_for_downloading
2258 false, // omaha_disable_p2p_for_sharing
2259 true, // payload_state_allow_p2p_attempt
2260 true, // expect_p2p_client_lookup
2261 "", // p2p_client_result_url
2262 false, // expected_allow_p2p_for_downloading
2263 true, // expected_allow_p2p_for_sharing
2264 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002265}
2266
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002267TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002268 P2PTest(false, // initial_allow_p2p_for_downloading
2269 true, // initial_allow_p2p_for_sharing
2270 false, // omaha_disable_p2p_for_downloading
2271 false, // omaha_disable_p2p_for_sharing
2272 true, // payload_state_allow_p2p_attempt
2273 false, // expect_p2p_client_lookup
2274 "unset", // p2p_client_result_url
2275 false, // expected_allow_p2p_for_downloading
2276 true, // expected_allow_p2p_for_sharing
2277 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002278}
2279
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002280TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002281 P2PTest(true, // initial_allow_p2p_for_downloading
2282 true, // initial_allow_p2p_for_sharing
2283 true, // omaha_disable_p2p_for_downloading
2284 false, // omaha_disable_p2p_for_sharing
2285 true, // payload_state_allow_p2p_attempt
2286 false, // expect_p2p_client_lookup
2287 "unset", // p2p_client_result_url
2288 false, // expected_allow_p2p_for_downloading
2289 true, // expected_allow_p2p_for_sharing
2290 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002291}
2292
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002293TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002294 P2PTest(true, // initial_allow_p2p_for_downloading
2295 true, // initial_allow_p2p_for_sharing
2296 false, // omaha_disable_p2p_for_downloading
2297 true, // omaha_disable_p2p_for_sharing
2298 true, // payload_state_allow_p2p_attempt
2299 true, // expect_p2p_client_lookup
2300 "http://1.3.5.7/p2p", // p2p_client_result_url
2301 true, // expected_allow_p2p_for_downloading
2302 false, // expected_allow_p2p_for_sharing
2303 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002304}
2305
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002306TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002307 P2PTest(true, // initial_allow_p2p_for_downloading
2308 true, // initial_allow_p2p_for_sharing
2309 true, // omaha_disable_p2p_for_downloading
2310 true, // omaha_disable_p2p_for_sharing
2311 true, // payload_state_allow_p2p_attempt
2312 false, // expect_p2p_client_lookup
2313 "unset", // p2p_client_result_url
2314 false, // expected_allow_p2p_for_downloading
2315 false, // expected_allow_p2p_for_sharing
2316 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002317}
2318
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002319bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2320 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002321 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002322 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2323
2324 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002325}
2326
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002327TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002328 // Simulate a successful update check that happens during OOBE. The
2329 // deadline in the response is needed to force the update attempt to
2330 // occur; responses without a deadline seen during OOBE will normally
2331 // return ErrorCode::kNonCriticalUpdateInOOBE.
2332 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2333 fake_update_response_.deadline = "20101020";
2334
David Zeuthen639aa362014-02-03 16:23:44 -08002335 // Check that we parse elapsed_days in the Omaha Response correctly.
2336 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002337 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2338 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002339 EXPECT_TRUE(response.update_exists);
2340 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002341 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002342 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002343 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002344 EXPECT_EQ(prefs_days, 42);
2345
2346 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002347 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002348 EXPECT_TRUE(response.update_exists);
2349 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002350 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002351 EXPECT_EQ(prefs_days, 42);
2352
2353 // Note that elapsed_days is not necessarily divisible by 7 so check
2354 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002355 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2356 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002357 EXPECT_TRUE(response.update_exists);
2358 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002359 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002360 EXPECT_EQ(prefs_days, 21);
2361
2362 // Check that we correctly handle elapsed_days not being included in
2363 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002364 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002365 EXPECT_TRUE(response.update_exists);
2366 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002367}
2368
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002369// If there is no prefs and OOBE is not complete, we should not
2370// report anything to Omaha.
2371TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002372 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002373 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2374 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2375}
David Zeuthen639aa362014-02-03 16:23:44 -08002376
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002377// If OOBE is complete and happened on a valid date (e.g. after Jan
2378// 1 2007 0:00 PST), that date should be used and written to
2379// prefs. However, first try with an invalid date and check we do
2380// nothing.
2381TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2382 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2383 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2384 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2385 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2386}
David Zeuthen639aa362014-02-03 16:23:44 -08002387
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002388// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2389// should yield an InstallDate of 14.
2390TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2391 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2392 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2393 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2394 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002395
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002396 int64_t prefs_days;
2397 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2398 EXPECT_EQ(prefs_days, 14);
2399}
David Zeuthen639aa362014-02-03 16:23:44 -08002400
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002401// Now that we have a valid date in prefs, check that we keep using
2402// that even if OOBE date reports something else. The date Jan 30,
2403// 2007 0:00 PST should yield an InstallDate of 28... but since
2404// there's a prefs file, we should still get 14.
2405TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2406 // Set a valid date in the prefs first.
2407 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002408
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002409 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2410 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2411 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002412
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002413 int64_t prefs_days;
2414 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2415 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002416
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002417 // If we delete the prefs file, we should get 28 days.
2418 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2419 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2420 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2421 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002422}
2423
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002424// Verifies that a device with no device policy, and is not a consumer
2425// device sets the max kernel key version to the current version.
2426// ie. the same behavior as if rollback is enabled.
2427TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2428 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2429
2430 // Setup and verify some initial default values for the kernel TPM
2431 // values that control verified boot and rollback.
2432 const int min_kernel_version = 4;
2433 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2434 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2435 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2436 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2437
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002438 EXPECT_CALL(
2439 *fake_system_state_.mock_metrics_reporter(),
2440 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2441 .Times(1);
2442
Amin Hassani41ac04b2019-03-29 11:31:03 -07002443 fake_update_response_.deadline = "20101020";
2444 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2445 tuc_params_.is_consumer_device = false;
2446 tuc_params_.rollback_allowed_milestones = 3;
2447
2448 EXPECT_TRUE(TestUpdateCheck());
2449 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002450
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002451 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002452 // kernel key version. This has the effect of freezing roll
2453 // forwards indefinitely. This will hold the rollback window
2454 // open until a future change will be able to move this forward
2455 // relative the configured window.
2456 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2457 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2458}
2459
2460// Verifies that a conmsumer device with no device policy sets the
2461// max kernel key version to the current version. ie. the same
2462// behavior as if rollback is enabled.
2463TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2464 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2465
2466 // Setup and verify some initial default values for the kernel TPM
2467 // values that control verified boot and rollback.
2468 const int min_kernel_version = 3;
2469 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2470 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2471 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2472 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2473
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002474 EXPECT_CALL(
2475 *fake_system_state_.mock_metrics_reporter(),
2476 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2477 .Times(1);
2478
Amin Hassani41ac04b2019-03-29 11:31:03 -07002479 fake_update_response_.deadline = "20101020";
2480 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2481 tuc_params_.is_consumer_device = true;
2482 tuc_params_.rollback_allowed_milestones = 3;
2483
2484 EXPECT_TRUE(TestUpdateCheck());
2485 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002486
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002487 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002488 // was set to logical infinity. This is the expected behavior for
2489 // consumer devices and matches the existing behavior prior to the
2490 // rollback features.
2491 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2492 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2493}
2494
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002495// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002496// in the TPM to prevent roll forward.
2497TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2498 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2499
2500 // Setup and verify some initial default values for the kernel TPM
2501 // values that control verified boot and rollback.
2502 const int allowed_milestones = 4;
2503 const int min_kernel_version = 3;
2504 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2505 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2506 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2507 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2508
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002509 EXPECT_CALL(
2510 *fake_system_state_.mock_metrics_reporter(),
2511 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2512 .Times(1);
2513
Amin Hassani41ac04b2019-03-29 11:31:03 -07002514 fake_update_response_.deadline = "20101020";
2515 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2516 tuc_params_.is_consumer_device = false;
2517 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2518 tuc_params_.is_policy_loaded = true;
2519
2520 EXPECT_TRUE(TestUpdateCheck());
2521 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002522
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002523 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002524 // was set to the current minimum kernel key version. This has
2525 // the effect of freezing roll forwards indefinitely. This will
2526 // hold the rollback window open until a future change will
2527 // be able to move this forward relative the configured window.
2528 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2529 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2530}
2531
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002532// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002533// in the TPM to logical infinity, to allow roll forward.
2534TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2535 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2536
2537 // Setup and verify some initial default values for the kernel TPM
2538 // values that control verified boot and rollback.
2539 const int allowed_milestones = 0;
2540 const int min_kernel_version = 3;
2541 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2542 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2543 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2544 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2545
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002546 EXPECT_CALL(
2547 *fake_system_state_.mock_metrics_reporter(),
2548 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2549 .Times(1);
2550
Amin Hassani41ac04b2019-03-29 11:31:03 -07002551 fake_update_response_.deadline = "20101020";
2552 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2553 tuc_params_.is_consumer_device = false;
2554 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2555 tuc_params_.is_policy_loaded = true;
2556
2557 EXPECT_TRUE(TestUpdateCheck());
2558 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002559
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002560 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002561 // was set to logical infinity.
2562 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2563 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2564}
2565
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002566TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002567 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002568 fake_update_response_.deadline = "20101020";
2569 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2570 tuc_params_.is_consumer_device = false;
2571 tuc_params_.rollback_allowed_milestones = 4;
2572 tuc_params_.is_policy_loaded = true;
2573
2574 EXPECT_TRUE(TestUpdateCheck());
2575 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002576 EXPECT_TRUE(response.is_rollback);
2577}
2578
2579TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002580 fake_update_response_.rollback_firmware_version = "1.2";
2581 fake_update_response_.rollback_kernel_version = "3.4";
2582 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002583 fake_update_response_.deadline = "20101020";
2584 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2585 tuc_params_.is_consumer_device = false;
2586 tuc_params_.rollback_allowed_milestones = 4;
2587 tuc_params_.is_policy_loaded = true;
2588
2589 EXPECT_TRUE(TestUpdateCheck());
2590 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002591 EXPECT_TRUE(response.is_rollback);
2592 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2593 EXPECT_EQ(2, response.rollback_key_version.firmware);
2594 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2595 EXPECT_EQ(4, response.rollback_key_version.kernel);
2596}
2597
May Lippert60aa3ca2018-08-15 16:55:29 -07002598TEST_F(OmahaRequestActionTest,
2599 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2600 FakeClock fake_clock;
2601 Time now = Time::Now();
2602 fake_clock.SetWallclockTime(now);
2603 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002604 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002605
Amin Hassani41ac04b2019-03-29 11:31:03 -07002606 ASSERT_TRUE(TestUpdateCheck());
2607
May Lippert60aa3ca2018-08-15 16:55:29 -07002608 EXPECT_TRUE(response.update_exists);
2609 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2610
2611 int64_t stored_first_seen_at_time;
2612 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2613 &stored_first_seen_at_time));
2614 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2615}
2616
2617TEST_F(OmahaRequestActionTest,
2618 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2619 FakeClock fake_clock;
2620 Time now = Time::Now();
2621 fake_clock.SetWallclockTime(now);
2622 fake_system_state_.set_clock(&fake_clock);
2623
Amin Hassani41ac04b2019-03-29 11:31:03 -07002624 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2625 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2626 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2627
2628 ASSERT_TRUE(TestUpdateCheck());
2629
May Lippert60aa3ca2018-08-15 16:55:29 -07002630 EXPECT_FALSE(response.update_exists);
2631 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2632}
2633
Xiaochu Liu88d90382018-08-29 16:09:11 -07002634TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002635 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002636 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002637 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2638
2639 ASSERT_TRUE(TestUpdateCheck());
2640
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002641 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) {
Xiaochu Liu88d90382018-08-29 16:09:11 -07002642 EXPECT_NE(string::npos,
Xiaochu Liu6310be62018-10-11 15:09:03 -07002643 post_str.find("appid=\"" + fake_update_response_.app_id + "_" +
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002644 dlc_module_id + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002645 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002646 EXPECT_NE(string::npos,
2647 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2648
2649 // Count number of updatecheck tag in response.
2650 int updatecheck_count = 0;
2651 size_t pos = 0;
2652 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2653 updatecheck_count++;
2654 pos++;
2655 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002656 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002657}
2658
2659TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2660 fake_update_response_.multi_app_skip_updatecheck = true;
2661 fake_update_response_.multi_app_no_update = false;
2662 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002663 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002664 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002665 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2666
2667 ASSERT_TRUE(TestUpdateCheck());
2668
Xiaochu Liu6310be62018-10-11 15:09:03 -07002669 EXPECT_TRUE(response.update_exists);
2670 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002671}
2672
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002673TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002674 fake_update_response_.rollback = true;
2675 fake_update_response_.rollback_allowed_milestones = 4;
2676 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002677 fake_update_response_.deadline = "20101020";
2678 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2679 tuc_params_.is_consumer_device = false;
2680 tuc_params_.rollback_allowed_milestones = 4;
2681 tuc_params_.is_policy_loaded = true;
2682
2683 EXPECT_TRUE(TestUpdateCheck());
2684 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002685 EXPECT_TRUE(response.is_rollback);
2686 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2687 response.past_rollback_key_version.firmware_key);
2688 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2689 response.past_rollback_key_version.firmware);
2690 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2691 response.past_rollback_key_version.kernel_key);
2692 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2693 response.past_rollback_key_version.kernel);
2694}
2695
2696TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002697 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002698 fake_update_response_.rollback = true;
2699 fake_update_response_.rollback_allowed_milestones = 4;
2700 fake_update_response_.rollback_firmware_version = "4.3";
2701 fake_update_response_.rollback_kernel_version = "2.1";
2702 fake_update_response_.past_rollback_key_version =
2703 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002704 fake_update_response_.deadline = "20101020";
2705 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2706 tuc_params_.is_consumer_device = false;
2707 tuc_params_.rollback_allowed_milestones = 4;
2708 tuc_params_.is_policy_loaded = true;
2709
2710 EXPECT_TRUE(TestUpdateCheck());
2711 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002712 EXPECT_TRUE(response.is_rollback);
2713 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2714 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2715 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2716 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
2717}
2718
2719TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002720 fake_update_response_.rollback = true;
2721 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002722 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002723 request_params_.set_rollback_allowed_milestones(4);
2724
2725 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
2726 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
2727 // to 2, OmahaRequestAction will look for the key values of N-4 version but
2728 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07002729 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2730 tuc_params_.is_consumer_device = false;
2731 tuc_params_.rollback_allowed_milestones = 2;
2732 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002733
Amin Hassani41ac04b2019-03-29 11:31:03 -07002734 EXPECT_TRUE(TestUpdateCheck());
2735 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002736 EXPECT_TRUE(response.is_rollback);
2737 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2738 response.past_rollback_key_version.firmware_key);
2739 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2740 response.past_rollback_key_version.firmware);
2741 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2742 response.past_rollback_key_version.kernel_key);
2743 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2744 response.past_rollback_key_version.kernel);
2745}
2746
Darin Petkov6a5b3222010-07-13 14:55:28 -07002747} // namespace chromeos_update_engine