blob: 8008e008a0216db1d7d375233e756a243ad1569b [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;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700330 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700331};
332
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700333class OmahaRequestActionTest : public ::testing::Test {
334 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800335 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200336 request_params_.set_os_sp("service_pack");
337 request_params_.set_os_board("x86-generic");
338 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700339 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200340 request_params_.set_app_lang("en-US");
341 request_params_.set_current_channel("unittest");
342 request_params_.set_target_channel("unittest");
343 request_params_.set_hwid("OEM MODEL 09235 7471");
344 request_params_.set_fw_version("ChromeOSFirmware.1.0");
345 request_params_.set_ec_version("0X0A1");
346 request_params_.set_delta_okay(true);
347 request_params_.set_interactive(false);
348 request_params_.set_update_url("http://url");
349 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200350 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200351 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700352 request_params_.set_is_install(false);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800353 request_params_.set_dlc_module_ids({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200354
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700355 fake_system_state_.set_request_params(&request_params_);
356 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700357
358 // Setting the default update check params. Lookup |TestUpdateCheck()|.
359 tuc_params_ = {
360 .http_response = "",
361 .fail_http_response_code = -1,
362 .ping_only = false,
363 .is_consumer_device = true,
364 .rollback_allowed_milestones = 0,
365 .is_policy_loaded = false,
366 .expected_code = ErrorCode::kSuccess,
367 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
368 .expected_check_reaction = metrics::CheckReaction::kUpdating,
369 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
370 };
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700371 }
372
Amin Hassani41ac04b2019-03-29 11:31:03 -0700373 // This function uses the paramets in |tuc_params_| to do an update check. It
374 // will fill out |post_str| with the result data and |response| with
375 // |OmahaResponse|. Returns true iff an output response was obtained from the
376 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
377 // transfer will fail with that code. |ping_only| is passed through to the
378 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700379 //
380 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700381 // |expected_error_code| parameters are for checking expectations about
382 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
383 // statistics. Use the appropriate ::kUnset value to specify that the given
384 // metric should not be reported.
385 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700386
Amin Hassani41ac04b2019-03-29 11:31:03 -0700387 // Tests events using |event| and |https_response|. It will fill up |post_str|
388 // with the result data.
389 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700390
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800391 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700392 // only a ping or also an updatecheck.
393 void PingTest(bool ping_only);
394
395 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800396 bool InstallDateParseHelper(const string& elapsed_days,
397 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700398
399 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800400 void P2PTest(bool initial_allow_p2p_for_downloading,
401 bool initial_allow_p2p_for_sharing,
402 bool omaha_disable_p2p_for_downloading,
403 bool omaha_disable_p2p_for_sharing,
404 bool payload_state_allow_p2p_attempt,
405 bool expect_p2p_client_lookup,
406 const string& p2p_client_result_url,
407 bool expected_allow_p2p_for_downloading,
408 bool expected_allow_p2p_for_sharing,
409 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700410
411 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700412 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200413 // Used by all tests.
414 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700415
416 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700417
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700418 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700419
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700420 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700421
422 TestUpdateCheckParams tuc_params_;
423
424 // TODO(ahassani): Add trailing _ to these two variables.
425 OmahaResponse response;
426 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700427};
428
Amin Hassani41ac04b2019-03-29 11:31:03 -0700429bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700430 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700431 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700432 auto fetcher =
433 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
434 tuc_params_.http_response.size(),
435 nullptr);
436 if (tuc_params_.fail_http_response_code >= 0) {
437 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700438 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200439 // This ensures the tests didn't forget to update fake_system_state_ if they
440 // are not using the default request_params_.
441 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
442
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700443 auto omaha_request_action =
444 std::make_unique<OmahaRequestAction>(&fake_system_state_,
445 nullptr,
446 std::move(fetcher),
447 tuc_params_.ping_only,
448 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700449
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800450 auto mock_policy_provider =
451 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
452 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700453 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800454
455 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700456 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800457
458 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700459 const bool get_allowed_milestone_succeeds =
460 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800461 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700462 .WillRepeatedly(
463 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
464 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800465
466 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
467 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700468 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800469
Amin Hassani41ac04b2019-03-29 11:31:03 -0700470 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700471 delegate_.interactive_ = request_params_.interactive();
472 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700473 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700474 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700475
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700476 auto collector_action =
477 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
478 BondActions(omaha_request_action.get(), collector_action.get());
479 processor.EnqueueAction(std::move(omaha_request_action));
480 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700481
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700482 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
483 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800484 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700485
Amin Hassani41ac04b2019-03-29 11:31:03 -0700486 EXPECT_CALL(
487 *fake_system_state_.mock_metrics_reporter(),
488 ReportUpdateCheckMetrics(_,
489 tuc_params_.expected_check_result,
490 tuc_params_.expected_check_reaction,
491 tuc_params_.expected_download_error_code))
492 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800493
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700494 loop.PostTask(base::Bind(
495 [](ActionProcessor* processor) { processor->StartProcessing(); },
496 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700497 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700498 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700499 if (delegate_.omaha_response_)
500 response = *delegate_.omaha_response_;
501 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700502 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700503}
504
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700505// Tests Event requests -- they should always succeed. |out_post_data| may be
506// null; if non-null, the post-data received by the mock HttpFetcher is
507// returned.
508void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700509 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700510 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700511 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700512
513 auto action = std::make_unique<OmahaRequestAction>(
514 &fake_system_state_,
515 event,
516 std::make_unique<MockHttpFetcher>(
517 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700518 false,
519 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700520 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700521 processor.set_delegate(&delegate_);
522 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700523
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700524 loop.PostTask(base::Bind(
525 [](ActionProcessor* processor) { processor->StartProcessing(); },
526 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700527 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700528 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700529
Amin Hassani41ac04b2019-03-29 11:31:03 -0700530 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700531}
532
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700533TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700534 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700535 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
536 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
537 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
538 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
539
540 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400541 EXPECT_FALSE(response.update_exists);
542}
543
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700544TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700545 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
546 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
547 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
548
549 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700550 EXPECT_FALSE(response.update_exists);
551}
552
Sen Jiang81259682017-03-30 15:11:30 -0700553TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700554 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700555 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
556 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
557 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
558
559 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700560 EXPECT_FALSE(response.update_exists);
561}
562
Sen Jiang00adf7b2017-06-26 15:57:29 -0700563TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700564 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700565 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
566 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
567 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
568
569 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700570 EXPECT_FALSE(response.update_exists);
571}
572
573TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700574 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700575 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
576 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700577 "</manifest></updatecheck></app></response>";
578 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
579 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
580
581 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700582 EXPECT_FALSE(response.update_exists);
583}
584
Alex Deymo8e18f932015-03-27 16:16:59 -0700585// Test that all the values in the response are parsed in a normal update
586// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700587TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700588 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700589 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
590
591 ASSERT_TRUE(TestUpdateCheck());
592
Darin Petkov6a5b3222010-07-13 14:55:28 -0700593 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700594 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700595 EXPECT_EQ("", response.system_version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800596 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
597 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700598 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800599 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
600 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700601 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700602 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
603 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700604 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700605 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700606 // persisted.
607 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
608 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
609 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700610}
611
Sen Jiang0affc2c2017-02-10 15:55:05 -0800612TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800613 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700614 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
615
616 ASSERT_TRUE(TestUpdateCheck());
617
Sen Jiang0affc2c2017-02-10 15:55:05 -0800618 EXPECT_TRUE(response.update_exists);
619 EXPECT_EQ(fake_update_response_.version, response.version);
620 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
621 response.packages[0].payload_urls[0]);
622 EXPECT_EQ(fake_update_response_.codebase + "package2",
623 response.packages[1].payload_urls[0]);
624 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
625 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700626 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700627 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800628 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700629 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800630 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700631 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700632 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700633}
634
635TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700636 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700637 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
638
639 ASSERT_TRUE(TestUpdateCheck());
640
Sen Jiang81259682017-03-30 15:11:30 -0700641 EXPECT_TRUE(response.update_exists);
642 EXPECT_EQ(fake_update_response_.version, response.version);
643 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
644 response.packages[0].payload_urls[0]);
645 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
646 response.packages[1].payload_urls[0]);
647 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
648 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
649 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700650 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700651 ASSERT_EQ(2u, response.packages.size());
652 EXPECT_EQ(string("hash3"), response.packages[1].hash);
653 EXPECT_EQ(333u, response.packages[1].size);
654 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700655 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700656}
657
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700658TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) {
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700659 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700660 // Trigger the lining up of the app and system versions.
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700661 request_params_.set_system_app_id(fake_update_response_.app_id2);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700662 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700663
Amin Hassani41ac04b2019-03-29 11:31:03 -0700664 ASSERT_TRUE(TestUpdateCheck());
665
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700666 EXPECT_TRUE(response.update_exists);
667 EXPECT_EQ(fake_update_response_.version, response.version);
668 EXPECT_EQ(fake_update_response_.version2, response.system_version);
669 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
670 response.packages[0].payload_urls[0]);
671 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
672 response.packages[1].payload_urls[0]);
673 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
674 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
675 EXPECT_EQ(11u, response.packages[0].metadata_size);
676 EXPECT_EQ(true, response.packages[0].is_delta);
677 ASSERT_EQ(2u, response.packages.size());
678 EXPECT_EQ(string("hash3"), response.packages[1].hash);
679 EXPECT_EQ(333u, response.packages[1].size);
680 EXPECT_EQ(33u, response.packages[1].metadata_size);
681 EXPECT_EQ(false, response.packages[1].is_delta);
682}
683
Sen Jiang81259682017-03-30 15:11:30 -0700684TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700685 fake_update_response_.multi_app = true;
686 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700687 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
688
689 ASSERT_TRUE(TestUpdateCheck());
690
Sen Jiang81259682017-03-30 15:11:30 -0700691 EXPECT_TRUE(response.update_exists);
692 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700693 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700694 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
695 response.packages[0].payload_urls[0]);
696 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
697 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
698 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700699 ASSERT_EQ(2u, response.packages.size());
700 EXPECT_EQ(string("hash3"), response.packages[1].hash);
701 EXPECT_EQ(333u, response.packages[1].size);
702 EXPECT_EQ(33u, response.packages[1].metadata_size);
703 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700704}
705
706TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700707 fake_update_response_.multi_app = true;
708 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700709 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
710
711 ASSERT_TRUE(TestUpdateCheck());
712
Sen Jiang81259682017-03-30 15:11:30 -0700713 EXPECT_TRUE(response.update_exists);
714 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700715 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700716 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
717 response.packages[0].payload_urls[0]);
718 EXPECT_EQ(fake_update_response_.codebase + "package2",
719 response.packages[1].payload_urls[0]);
720 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
721 response.packages[2].payload_urls[0]);
722 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
723 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
724 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700725 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700726 ASSERT_EQ(3u, response.packages.size());
727 EXPECT_EQ(string("hash2"), response.packages[1].hash);
728 EXPECT_EQ(222u, response.packages[1].size);
729 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700730 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700731 EXPECT_EQ(string("hash3"), response.packages[2].hash);
732 EXPECT_EQ(333u, response.packages[2].size);
733 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700734 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800735}
736
Sen Jiangfe284402018-03-21 14:03:50 -0700737TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700738 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700739 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
740
741 ASSERT_TRUE(TestUpdateCheck());
742
Sen Jiangfe284402018-03-21 14:03:50 -0700743 EXPECT_TRUE(response.update_exists);
744 EXPECT_TRUE(response.powerwash_required);
745}
746
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700747TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700748 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700749 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700750 tuc_params_.http_response = "invalid xml>";
751 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
752 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
753 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
754
755 ASSERT_FALSE(TestUpdateCheck());
756
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700757 EXPECT_FALSE(response.update_exists);
758}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700759
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700760TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700761 request_params_.set_interactive(false);
762 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700763 tuc_params_.http_response = "invalid xml>";
764 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
765 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
766 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
767
768 ASSERT_FALSE(TestUpdateCheck());
769
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700770 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700771}
772
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700773TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700774 // Set up a connection manager that doesn't allow a valid update over
775 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700776 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700777 fake_system_state_.set_connection_manager(&mock_cm);
778
Alex Deymo30534502015-07-20 15:06:33 -0700779 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800780 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
781 SetArgPointee<1>(ConnectionTethering::kUnknown),
782 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700783 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
784 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700785
Amin Hassani41ac04b2019-03-29 11:31:03 -0700786 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
787 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
788 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
789
790 ASSERT_FALSE(TestUpdateCheck());
791
Chris Sosa77f79e82014-06-02 18:16:24 -0700792 EXPECT_FALSE(response.update_exists);
793}
794
Weidong Guo421ff332017-04-17 10:08:38 -0700795TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
796 // This test tests that update over cellular is allowed as device policy
797 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700798 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700799 fake_system_state_.set_connection_manager(&mock_cm);
800
801 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
802 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
803 SetArgPointee<1>(ConnectionTethering::kUnknown),
804 Return(true)));
805 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
806 .WillRepeatedly(Return(true));
807 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
808 .WillRepeatedly(Return(true));
809
Amin Hassani41ac04b2019-03-29 11:31:03 -0700810 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
811
812 ASSERT_TRUE(TestUpdateCheck());
813
Weidong Guo421ff332017-04-17 10:08:38 -0700814 EXPECT_TRUE(response.update_exists);
815}
816
817TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
818 // This test tests that update over cellular is blocked as device policy
819 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700820 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700821 fake_system_state_.set_connection_manager(&mock_cm);
822
823 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
824 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
825 SetArgPointee<1>(ConnectionTethering::kUnknown),
826 Return(true)));
827 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
828 .WillRepeatedly(Return(true));
829 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
830 .WillRepeatedly(Return(false));
831
Amin Hassani41ac04b2019-03-29 11:31:03 -0700832 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
833 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
834 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
835
836 ASSERT_FALSE(TestUpdateCheck());
837
Weidong Guo421ff332017-04-17 10:08:38 -0700838 EXPECT_FALSE(response.update_exists);
839}
840
841TEST_F(OmahaRequestActionTest,
842 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
843 // This test tests that, when device policy is not set, update over cellular
844 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700845 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700846 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
847 fake_system_state_.set_connection_manager(&mock_cm);
848
849 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
850 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
851 SetArgPointee<1>(ConnectionTethering::kUnknown),
852 Return(true)));
853 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
854 .WillRepeatedly(Return(false));
855 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
856 .WillRepeatedly(Return(true));
857
Amin Hassani41ac04b2019-03-29 11:31:03 -0700858 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
859
860 ASSERT_TRUE(TestUpdateCheck());
861
Weidong Guo421ff332017-04-17 10:08:38 -0700862 EXPECT_TRUE(response.update_exists);
863}
864
865TEST_F(OmahaRequestActionTest,
866 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
867 // This test tests that, when device policy is not set and permission for
868 // update over cellular is set to false or does not exist, update over
869 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700870 MockConnectionManager mock_cm;
871 // A version different from the version in omaha response.
872 string diff_version = "99.99.99";
873 // A size different from the size in omaha response.
874 int64_t diff_size = 999;
875
876 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
877 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
878 // This test tests cellular (3G) being the only connection type being allowed.
879 fake_system_state_.set_connection_manager(&mock_cm);
880
881 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
882 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
883 SetArgPointee<1>(ConnectionTethering::kUnknown),
884 Return(true)));
885 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
886 .WillRepeatedly(Return(false));
887 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
888 .WillRepeatedly(Return(true));
889
Amin Hassani41ac04b2019-03-29 11:31:03 -0700890 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
891 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
892 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
893
894 ASSERT_FALSE(TestUpdateCheck());
895
Weidong Guo421ff332017-04-17 10:08:38 -0700896 EXPECT_FALSE(response.update_exists);
897}
898
899TEST_F(OmahaRequestActionTest,
900 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
901 // This test tests that, when device policy is not set and permission for
902 // update over cellular is set to false or does not exist, update over
903 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700904 MockConnectionManager mock_cm;
905 // A version same as the version in omaha response.
906 string new_version = fake_update_response_.version;
907 // A size same as the size in omaha response.
908 int64_t new_size = fake_update_response_.size;
909
910 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
911 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
912 fake_system_state_.set_connection_manager(&mock_cm);
913
914 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
915 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
916 SetArgPointee<1>(ConnectionTethering::kUnknown),
917 Return(true)));
918 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
919 .WillRepeatedly(Return(false));
920 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
921 .WillRepeatedly(Return(true));
922
Amin Hassani41ac04b2019-03-29 11:31:03 -0700923 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
924
925 ASSERT_TRUE(TestUpdateCheck());
926
Weidong Guo421ff332017-04-17 10:08:38 -0700927 EXPECT_TRUE(response.update_exists);
928}
929
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700930TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700931 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700932 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700933 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700934 fake_update_response_.version = rollback_version;
935 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
936 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
937 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700938
Chris Sosa77f79e82014-06-02 18:16:24 -0700939 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800940 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700941
Amin Hassani41ac04b2019-03-29 11:31:03 -0700942 ASSERT_FALSE(TestUpdateCheck());
943
Chris Sosa77f79e82014-06-02 18:16:24 -0700944 EXPECT_FALSE(response.update_exists);
945}
946
Marton Hunyadyc2882062018-05-14 17:28:25 +0200947// Verify that update checks called during OOBE will not try to download an
948// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700949TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200950 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700951 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
952 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
953 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
954 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -0700955
Sen Jiang8cd42342018-01-31 12:06:59 -0800956 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700957 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700958 ASSERT_FALSE(TestUpdateCheck());
959
Kevin Cernekee2494e282016-03-29 18:03:53 -0700960 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +0200961}
Kevin Cernekee2494e282016-03-29 18:03:53 -0700962
Marton Hunyadyc2882062018-05-14 17:28:25 +0200963// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
964// enabled.
965TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200966 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700967 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700968 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700969
Amin Hassani41ac04b2019-03-29 11:31:03 -0700970 ASSERT_TRUE(TestUpdateCheck());
971
Kevin Cernekee2494e282016-03-29 18:03:53 -0700972 EXPECT_TRUE(response.update_exists);
973}
974
Marton Hunyadyc2882062018-05-14 17:28:25 +0200975// Verify that update checks called during OOBE will still try to download an
976// update if the response includes the deadline field.
977TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200978 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
979 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700980 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +0200981
Amin Hassani41ac04b2019-03-29 11:31:03 -0700982 ASSERT_TRUE(TestUpdateCheck());
983
Marton Hunyadyc2882062018-05-14 17:28:25 +0200984 EXPECT_TRUE(response.update_exists);
985}
986
987// Verify that update checks called during OOBE will not try to download an
988// update if a rollback happened, even when the response includes the deadline
989// field.
990TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200991 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
992 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700993 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
994 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
995 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
996 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
997
Marton Hunyadyc2882062018-05-14 17:28:25 +0200998 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
999 .WillOnce(Return(true));
1000
Amin Hassani41ac04b2019-03-29 11:31:03 -07001001 ASSERT_FALSE(TestUpdateCheck());
1002
Marton Hunyadyc2882062018-05-14 17:28:25 +02001003 EXPECT_FALSE(response.update_exists);
1004}
1005
Toni Barzic61544e62018-10-11 14:37:30 -07001006// Verify that non-critical updates are skipped by reporting the
1007// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1008// i.e. when the update would need user permission. Note that reporting
1009// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1010// in OOBE (warning the user about an update that will be skipped).
1011TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001012 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1013
1014 MockConnectionManager mock_cm;
1015 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001016 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1017 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1018 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1019 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001020
1021 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1022 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1023 SetArgPointee<1>(ConnectionTethering::kUnknown),
1024 Return(true)));
1025 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1026 .WillRepeatedly(Return(false));
1027
Amin Hassani41ac04b2019-03-29 11:31:03 -07001028 ASSERT_FALSE(TestUpdateCheck());
1029
Toni Barzic61544e62018-10-11 14:37:30 -07001030 EXPECT_FALSE(response.update_exists);
1031}
1032
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001033TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001034 request_params_.set_wall_clock_based_wait_enabled(true);
1035 request_params_.set_update_check_count_wait_enabled(false);
1036 request_params_.set_waiting_period(TimeDelta::FromDays(2));
May Lippert60aa3ca2018-08-15 16:55:29 -07001037 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001038 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1039 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1040 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001041
Amin Hassani41ac04b2019-03-29 11:31:03 -07001042 ASSERT_FALSE(TestUpdateCheck());
1043
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001044 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001045}
1046
1047TEST_F(OmahaRequestActionTest,
1048 WallClockBasedWaitAloneCausesScatteringInteractive) {
1049 request_params_.set_wall_clock_based_wait_enabled(true);
1050 request_params_.set_update_check_count_wait_enabled(false);
1051 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1052 request_params_.set_interactive(true);
1053 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1054 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001055
1056 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001057 ASSERT_TRUE(TestUpdateCheck());
1058
Chris Sosa968d0572013-08-23 14:46:02 -07001059 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001060}
1061
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001062TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001063 request_params_.set_wall_clock_based_wait_enabled(false);
1064 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1065 request_params_.set_update_check_count_wait_enabled(true);
1066 request_params_.set_min_update_checks_needed(1);
1067 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001068 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001069
Amin Hassani41ac04b2019-03-29 11:31:03 -07001070 ASSERT_TRUE(TestUpdateCheck());
1071
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001072 EXPECT_TRUE(response.update_exists);
1073}
1074
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001075TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001076 request_params_.set_wall_clock_based_wait_enabled(true);
1077 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1078 request_params_.set_update_check_count_wait_enabled(true);
1079 request_params_.set_min_update_checks_needed(1);
1080 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001081 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001082 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1083
1084 ASSERT_TRUE(TestUpdateCheck());
1085
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001086 EXPECT_TRUE(response.update_exists);
1087}
1088
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001089TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001090 request_params_.set_wall_clock_based_wait_enabled(true);
1091 request_params_.set_waiting_period(TimeDelta());
1092 request_params_.set_update_check_count_wait_enabled(true);
1093 request_params_.set_min_update_checks_needed(0);
1094 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001095 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001096 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001097
Amin Hassani41ac04b2019-03-29 11:31:03 -07001098 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001099
Ben Chan9abb7632014-08-07 00:10:53 -07001100 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001101 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001102 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001103 EXPECT_TRUE(response.update_exists);
1104}
1105
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001106TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001107 request_params_.set_wall_clock_based_wait_enabled(true);
1108 request_params_.set_waiting_period(TimeDelta());
1109 request_params_.set_update_check_count_wait_enabled(true);
1110 request_params_.set_min_update_checks_needed(1);
1111 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001112 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001113 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1114 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1115 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001116
Amin Hassani41ac04b2019-03-29 11:31:03 -07001117 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001118
Ben Chan9abb7632014-08-07 00:10:53 -07001119 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001120 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001121 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001122 EXPECT_FALSE(response.update_exists);
1123}
1124
Amin Hassani41ac04b2019-03-29 11:31:03 -07001125TEST_F(OmahaRequestActionTest,
1126 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001127 request_params_.set_wall_clock_based_wait_enabled(true);
1128 request_params_.set_waiting_period(TimeDelta());
1129 request_params_.set_update_check_count_wait_enabled(true);
1130 request_params_.set_min_update_checks_needed(1);
1131 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001132 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001133 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001134 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1135
1136 // Verify if we are interactive check we don't defer.
1137 ASSERT_TRUE(TestUpdateCheck());
1138
1139 EXPECT_TRUE(response.update_exists);
1140}
1141
1142TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1143 request_params_.set_wall_clock_based_wait_enabled(true);
1144 request_params_.set_waiting_period(TimeDelta());
1145 request_params_.set_update_check_count_wait_enabled(true);
1146 request_params_.set_min_update_checks_needed(1);
1147 request_params_.set_max_update_checks_allowed(8);
1148 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1149 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1150 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1151 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001152
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001153 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001154 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001155
Ben Chan9abb7632014-08-07 00:10:53 -07001156 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001157 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001158 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001159 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001160 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001161 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001162}
1163
1164TEST_F(OmahaRequestActionTest,
1165 ExistingUpdateCheckCountCausesScatteringInteractive) {
1166 request_params_.set_wall_clock_based_wait_enabled(true);
1167 request_params_.set_waiting_period(TimeDelta());
1168 request_params_.set_update_check_count_wait_enabled(true);
1169 request_params_.set_min_update_checks_needed(1);
1170 request_params_.set_max_update_checks_allowed(8);
1171 request_params_.set_interactive(true);
1172 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1173 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1174
1175 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001176
1177 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001178 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001179 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001180}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001181
Adolfo Victoria497044c2018-07-18 07:51:42 -07001182TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1183 // If staging is on, the value for max days to scatter should be ignored, and
1184 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001185 request_params_.set_wall_clock_based_wait_enabled(true);
1186 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1187 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001188 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1189
Adolfo Victoria497044c2018-07-18 07:51:42 -07001190 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1191 // This should not prevent scattering due to staging.
1192 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001193 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1194 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1195 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1196
1197 ASSERT_FALSE(TestUpdateCheck());
1198
Adolfo Victoria497044c2018-07-18 07:51:42 -07001199 EXPECT_FALSE(response.update_exists);
1200
1201 // Interactive updates should not be affected.
1202 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001203 tuc_params_.expected_code = ErrorCode::kSuccess;
1204 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1205
1206 ASSERT_TRUE(TestUpdateCheck());
1207
Adolfo Victoria497044c2018-07-18 07:51:42 -07001208 EXPECT_TRUE(response.update_exists);
1209}
1210
Alex Deymo8e18f932015-03-27 16:16:59 -07001211TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001212 fake_update_response_.include_cohorts = true;
1213 fake_update_response_.cohort = "s/154454/8479665";
1214 fake_update_response_.cohorthint = "please-put-me-on-beta";
1215 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001216 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001217
Amin Hassani41ac04b2019-03-29 11:31:03 -07001218 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001219
1220 string value;
1221 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1222 EXPECT_EQ(fake_update_response_.cohort, value);
1223
1224 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1225 EXPECT_EQ(fake_update_response_.cohorthint, value);
1226
1227 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1228 EXPECT_EQ(fake_update_response_.cohortname, value);
1229}
1230
1231TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001232 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1233 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1234 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1235 fake_update_response_.include_cohorts = true;
1236 fake_update_response_.cohort = "s/154454/8479665";
1237 fake_update_response_.cohorthint = "please-put-me-on-beta";
1238 fake_update_response_.cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001239 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001240
Amin Hassani41ac04b2019-03-29 11:31:03 -07001241 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001242
1243 string value;
1244 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1245 EXPECT_EQ(fake_update_response_.cohort, value);
1246
1247 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1248 EXPECT_EQ(fake_update_response_.cohorthint, value);
1249
1250 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1251}
1252
1253TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001254 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001255
Amin Hassani41ac04b2019-03-29 11:31:03 -07001256 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1257 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001258
1259 string value;
1260 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1261 EXPECT_EQ("old_value", value);
1262
1263 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1264 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1265}
1266
Alex Deymo00d79ac2015-06-29 15:41:49 -07001267TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001268 fake_update_response_.include_cohorts = true;
1269 fake_update_response_.cohort = "s/154454/8479665";
1270 fake_update_response_.cohorthint = "please-put-me-on-beta";
1271 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001272 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1273 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1274 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001275
Amin Hassani41ac04b2019-03-29 11:31:03 -07001276 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001277
1278 string value;
1279 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1280 EXPECT_EQ(fake_update_response_.cohort, value);
1281
1282 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1283 EXPECT_EQ(fake_update_response_.cohorthint, value);
1284
1285 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1286 EXPECT_EQ(fake_update_response_.cohortname, value);
1287}
1288
Sen Jiangb1e063a2017-09-15 17:44:31 -07001289TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001290 fake_update_response_.multi_app = true;
1291 fake_update_response_.include_cohorts = true;
1292 fake_update_response_.cohort = "s/154454/8479665";
1293 fake_update_response_.cohorthint = "please-put-me-on-beta";
1294 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001295 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001296
Amin Hassani41ac04b2019-03-29 11:31:03 -07001297 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001298
1299 string value;
1300 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1301 EXPECT_EQ(fake_update_response_.cohort, value);
1302
1303 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1304 EXPECT_EQ(fake_update_response_.cohorthint, value);
1305
1306 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1307 EXPECT_EQ(fake_update_response_.cohortname, value);
1308}
1309
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001310TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001311 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001312 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001313 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001314
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001315 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001316 &fake_system_state_,
1317 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001318 std::make_unique<MockHttpFetcher>(
1319 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001320 false,
1321 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001322 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001323 processor.set_delegate(&delegate_);
1324 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001325
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001326 loop.PostTask(base::Bind(
1327 [](ActionProcessor* processor) { processor->StartProcessing(); },
1328 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001329 loop.Run();
1330 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001331 EXPECT_FALSE(processor.IsRunning());
1332}
1333
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001334TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001335 tuc_params_.http_response = "invalid xml>";
1336 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1337 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1338 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1339
1340 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001341 EXPECT_FALSE(response.update_exists);
1342}
1343
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001344TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001345 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1346 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1347 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1348
1349 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001350 EXPECT_FALSE(response.update_exists);
1351}
1352
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001353TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001354 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001355 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1356 "<daystart elapsed_seconds=\"100\"/>"
1357 "<app appid=\"foo\" status=\"ok\">"
1358 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001359 "<updatecheck/></app></response>";
1360 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1361 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1362 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1363
1364 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001365 EXPECT_FALSE(response.update_exists);
1366}
1367
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001368TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001369 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001370 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1371 "<daystart elapsed_seconds=\"100\"/>"
1372 "<app appid=\"foo\" status=\"ok\">"
1373 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001374 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1375 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1376 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1377 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1378
1379 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001380 EXPECT_FALSE(response.update_exists);
1381}
1382
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001383TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001384 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001385 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1386 "<daystart elapsed_seconds=\"100\"/>"
1387 "<app appid=\"foo\" status=\"ok\">"
1388 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001389 "</app></response>";
1390 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1391 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1392 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1393
1394 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001395 EXPECT_FALSE(response.update_exists);
1396}
1397
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001398TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001399 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001400 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1401 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001402 // the appid needs to match that in the request params
1403 "<app appid=\"" +
1404 fake_update_response_.app_id +
1405 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001406 "<updatecheck status=\"ok\">"
1407 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001408 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001409 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001410 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001411 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001412 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001413 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001414 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001415 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001416
Amin Hassani41ac04b2019-03-29 11:31:03 -07001417 ASSERT_TRUE(TestUpdateCheck());
1418
Darin Petkov6a5b3222010-07-13 14:55:28 -07001419 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001420 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001421 EXPECT_EQ("http://missing/field/test/f",
1422 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001423 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001424 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1425 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001426 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001427 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001428}
1429
1430namespace {
1431class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1432 public:
1433 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001434 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001435 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001436};
1437
Alex Deymo60ca1a72015-06-18 18:19:15 -07001438void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001439 processor->StartProcessing();
1440 CHECK(processor->IsRunning());
1441 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001442}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001443} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001444
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001445TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001446 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001447 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001448
Alex Deymo60ca1a72015-06-18 18:19:15 -07001449 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001450 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001451 &fake_system_state_,
1452 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001453 std::make_unique<MockHttpFetcher>(
1454 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001455 false,
1456 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001457 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001458 ActionProcessor processor;
1459 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001460 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001461
Alex Deymo60ca1a72015-06-18 18:19:15 -07001462 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1463 loop.Run();
1464 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001465}
1466
Alex Deymob0d74eb2015-03-30 17:59:17 -07001467TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001468 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001469 request_params_.set_os_sp("testtheservice_pack>");
1470 request_params_.set_os_board("x86 generic<id");
1471 request_params_.set_current_channel("unittest_track&lt;");
1472 request_params_.set_target_channel("unittest_track&lt;");
1473 request_params_.set_hwid("<OEM MODEL>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001474 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1475 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001476 fake_prefs_.SetString(
1477 kPrefsOmahaCohortName,
1478 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001479 tuc_params_.http_response = "invalid xml>";
1480 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1481 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1482 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1483
1484 ASSERT_FALSE(TestUpdateCheck());
1485
Alex Deymo8e18f932015-03-27 16:16:59 -07001486 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1487 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1488 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1489 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1490 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1491 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1492 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1493 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1494 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1495 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1496 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1497 // Values from Prefs that are too big are removed from the XML instead of
1498 // encoded.
1499 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001500}
1501
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001502TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001503 fake_update_response_.deadline = "&lt;20110101";
1504 fake_update_response_.more_info_url = "testthe&lt;url";
1505 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001506 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1507
1508 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001509
Sen Jiang0affc2c2017-02-10 15:55:05 -08001510 EXPECT_EQ("testthe<url", response.more_info_url);
1511 EXPECT_EQ("testthe&codebase/file.signed",
1512 response.packages[0].payload_urls[0]);
1513 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001514}
1515
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001516TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001517 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001518 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001519 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001520
Amin Hassani41ac04b2019-03-29 11:31:03 -07001521 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001522 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001523}
1524
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001525TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001526 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001527 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001528 tuc_params_.http_response = "invalid xml>";
1529 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1530 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1531 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001532
Darin Petkov95508da2011-01-05 12:42:29 -08001533 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001534 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001535 // An existing but empty previous version means that we didn't reboot to a new
1536 // update, therefore, no need to update the previous version.
1537 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001538 ASSERT_FALSE(TestUpdateCheck());
1539
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001540 EXPECT_NE(
1541 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1542 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001543 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001544 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1545 string::npos);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001546 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos);
1547 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001548 // No <event> tag should be sent if we didn't reboot to an update.
1549 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001550}
1551
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001552TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001553 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001554 "invalid xml>");
1555
Alex Vakulenko75039d72014-03-25 12:36:28 -07001556 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001557 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001558 OmahaEvent::kTypeUpdateDownloadStarted,
1559 OmahaEvent::kResultSuccess);
1560 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001561 EXPECT_EQ(post_str.find("ping"), string::npos);
1562 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001563}
1564
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001565TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001566 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001567 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001568 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001569 "invalid xml>");
1570
Alex Vakulenko75039d72014-03-25 12:36:28 -07001571 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001572 " <event eventtype=\"%d\" eventresult=\"%d\" "
1573 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001574 OmahaEvent::kTypeDownloadComplete,
1575 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001576 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001577 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001578 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001579}
1580
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001581TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001582 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001583 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001584 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001585 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001586 std::make_unique<MockHttpFetcher>(
1587 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001588 false,
1589 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001590 EXPECT_FALSE(update_check_action.IsEvent());
1591
1592 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001593 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001594 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001595 std::make_unique<MockHttpFetcher>(
1596 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001597 false,
1598 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001599 EXPECT_TRUE(event_action.IsEvent());
1600}
1601
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001602TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001603 tuc_params_.http_response = "invalid xml>";
1604 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1605 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1606 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1607
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001608 for (int i = 0; i < 2; i++) {
1609 bool delta_okay = i == 1;
1610 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001611 request_params_.set_delta_okay(delta_okay);
1612
Amin Hassani41ac04b2019-03-29 11:31:03 -07001613 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001614 EXPECT_NE(
1615 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1616 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001617 << "i = " << i;
1618 }
1619}
1620
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001621TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001622 tuc_params_.http_response = "invalid xml>";
1623 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1624 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1625 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1626
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001627 for (int i = 0; i < 2; i++) {
1628 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001629 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001630 request_params_.set_interactive(interactive);
1631
Amin Hassani41ac04b2019-03-29 11:31:03 -07001632 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001633 EXPECT_NE(post_str.find(
1634 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001635 string::npos)
1636 << "i = " << i;
1637 }
1638}
1639
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001640TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001641 tuc_params_.http_response = "invalid xml>";
1642 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1643 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1644 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1645
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001646 for (int i = 0; i < 2; i++) {
1647 bool target_version_set = i == 1;
1648 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001649 request_params_.set_target_version_prefix(target_version_prefix);
1650
Amin Hassani41ac04b2019-03-29 11:31:03 -07001651 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001652 if (target_version_set) {
1653 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1654 string::npos)
1655 << "i = " << i;
1656 } else {
1657 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1658 << "i = " << i;
1659 }
1660 }
1661}
1662
1663TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001664 tuc_params_.http_response = "invalid xml>";
1665 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1666 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1667 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1668
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001669 for (int i = 0; i < 4; i++) {
1670 bool rollback_allowed = i / 2 == 0;
1671 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001672 request_params_.set_target_version_prefix(target_version_set ? "10032."
1673 : "");
1674 request_params_.set_rollback_allowed(rollback_allowed);
1675
Amin Hassani41ac04b2019-03-29 11:31:03 -07001676 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001677 if (rollback_allowed && target_version_set) {
1678 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1679 << "i = " << i;
1680 } else {
1681 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1682 }
1683 }
1684}
1685
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001686TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001687 OmahaEvent default_event;
1688 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1689 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001690 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001691
1692 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1693 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1694 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001695 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001696
1697 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1698 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001699 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001700 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1701 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001702 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001703}
1704
Askar Aitzhan570ca872019-04-24 11:16:12 +02001705TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
Askar Aitzhan18fff842019-06-21 23:24:37 +02001706 // If DeviceQuickFixBuildToken value is set it takes precedence over pref
1707 // value.
1708 constexpr char autoupdate_token[] = "autoupdate_token>";
1709 constexpr char xml_encoded_autoupdate_token[] = "autoupdate_token&gt;";
1710 constexpr char omaha_cohort_hint[] = "cohort_hint";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001711
1712 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1713 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1714 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1715 request_params_.set_autoupdate_token(autoupdate_token);
Askar Aitzhan18fff842019-06-21 23:24:37 +02001716 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan570ca872019-04-24 11:16:12 +02001717
1718 ASSERT_TRUE(TestUpdateCheck());
1719
Askar Aitzhan18fff842019-06-21 23:24:37 +02001720 EXPECT_NE(string::npos,
1721 post_str.find("cohorthint=\"" +
1722 string(xml_encoded_autoupdate_token) + "\""));
1723 EXPECT_EQ(string::npos, post_str.find(autoupdate_token));
1724 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
1725}
1726
1727TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsNotSetTest) {
1728 // If DeviceQuickFixBuildToken is not set, pref value will be provided in
1729 // cohorthint attribute.
1730 constexpr char omaha_cohort_hint[] = "evil_string>";
1731 constexpr char xml_encoded_cohort_hint[] = "evil_string&gt;";
1732
1733 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1734 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1735 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1736 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
1737
1738 ASSERT_TRUE(TestUpdateCheck());
1739
1740 EXPECT_NE(
1741 string::npos,
1742 post_str.find("cohorthint=\"" + string(xml_encoded_cohort_hint) + "\""));
1743 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001744}
1745
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001746void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001747 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001748 fake_system_state_.set_prefs(&prefs);
1749 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001750 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001751 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1752 // Add a few hours to the day difference to test no rounding, etc.
1753 int64_t five_days_ago =
1754 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1755 int64_t six_days_ago =
1756 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1757 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001758 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001759 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001760 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001761 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001762 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001763
1764 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1765 tuc_params_.ping_only = ping_only;
1766 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1767 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1768
1769 ASSERT_TRUE(TestUpdateCheck());
1770
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001771 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1772 string::npos);
1773 if (ping_only) {
1774 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1775 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1776 } else {
1777 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1778 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001779 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001780}
1781
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001782TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001783 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001784}
1785
1786TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001787 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001788}
1789
1790TEST_F(OmahaRequestActionTest, ActivePingTest) {
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 three_days_ago =
1797 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1798 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001799 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001800 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001801 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001802 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001803 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001804 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001805
1806 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1807 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1808 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1809
1810 ASSERT_TRUE(TestUpdateCheck());
1811
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001812 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001813}
1814
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001815TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001816 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001817 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001818 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001819 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001820 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001821 int64_t four_days_ago =
1822 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1823 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001824 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001825 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001826 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001827 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001828 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001829 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001830
1831 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1832 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1833 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1834
1835 ASSERT_TRUE(TestUpdateCheck());
1836
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001837 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001838 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001839}
1840
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001841TEST_F(OmahaRequestActionTest, NoPingTest) {
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 one_hour_ago =
1848 (Time::Now() - TimeDelta::FromHours(1)).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>(one_hour_ago), 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>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001855 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1856 // send a ping.
1857 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1858 .WillOnce(Return(true));
1859 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1860 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001861
1862 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1863 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1864 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1865
1866 ASSERT_TRUE(TestUpdateCheck());
1867
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001868 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001869}
1870
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001871TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001872 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001873 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001874 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001875 int64_t now = Time::Now().ToInternalValue();
1876 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001877 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001878 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001879 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001880 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1881 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001882
1883 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1884 tuc_params_.ping_only = true;
1885 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
1886 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1887
1888 EXPECT_TRUE(TestUpdateCheck());
1889 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07001890}
1891
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001892TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001893 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001894 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001895 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001896 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001897 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001898 int64_t future =
1899 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001900 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001901 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001902 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001903 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001904 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001905 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001906 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1907 .WillOnce(Return(true));
1908 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1909 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001910
1911 tuc_params_.http_response =
1912 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1913 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
1914 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1915 "<updatecheck status=\"noupdate\"/></app></response>";
1916 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1917 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1918
1919 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001920 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001921}
1922
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001923TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001924 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001925 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001926 // may fail if it runs for longer than 5 seconds. It shouldn't run
1927 // that long though.
1928 int64_t midnight =
1929 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1930 int64_t midnight_slack =
1931 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001932 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001933 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001934 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1935 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001936 EXPECT_CALL(prefs,
1937 SetInt64(kPrefsLastActivePingDay,
1938 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001939 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001940 EXPECT_CALL(prefs,
1941 SetInt64(kPrefsLastRollCallPingDay,
1942 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001943 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001944
1945 tuc_params_.http_response =
1946 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1947 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
1948 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1949 "<updatecheck status=\"noupdate\"/></app></response>";
1950 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1951 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1952
1953 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001954}
1955
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001956TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001957 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001958 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001959 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1960 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001961 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1962 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001963
1964 tuc_params_.http_response =
1965 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1966 "protocol=\"3.0\"><daystart blah=\"200\"/>"
1967 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1968 "<updatecheck status=\"noupdate\"/></app></response>";
1969 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1970 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1971
1972 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001973}
1974
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001975TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001976 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001977 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001978 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1979 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001980 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1981 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001982
1983 tuc_params_.http_response =
1984 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1985 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
1986 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1987 "<updatecheck status=\"noupdate\"/></app></response>";
1988 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1989 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1990
1991 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001992}
1993
Alex Deymob3fa53b2016-04-18 19:57:58 -07001994TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1995 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1996 // the "eol" attribute.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001997 tuc_params_.http_response =
1998 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1999 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2000 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
2001 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/></app></response>";
2002 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2003 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2004
2005 ASSERT_TRUE(TestUpdateCheck());
2006
Alex Deymob3fa53b2016-04-18 19:57:58 -07002007 string eol_pref;
2008 EXPECT_TRUE(
2009 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
2010 // Note that the eol="eol" attribute should be ignored and the _eol should be
2011 // used instead.
2012 EXPECT_EQ("security-only", eol_pref);
2013}
2014
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002015TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07002016 tuc_params_.http_response = "invalid xml>";
2017 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2018 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2019 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2020
2021 ASSERT_FALSE(TestUpdateCheck());
2022
Darin Petkov84c763c2010-07-29 16:27:58 -07002023 EXPECT_EQ(post_str.find("machineid="), string::npos);
2024 EXPECT_EQ(post_str.find("userid="), string::npos);
2025}
2026
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002027TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002028 const int http_error_code =
2029 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002030 tuc_params_.fail_http_response_code = 501;
2031 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2032 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2033 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2034 tuc_params_.expected_download_error_code =
2035 static_cast<metrics::DownloadErrorCode>(501);
2036
2037 ASSERT_FALSE(TestUpdateCheck());
2038
Darin Petkovedc522e2010-11-05 09:35:17 -07002039 EXPECT_FALSE(response.update_exists);
2040}
2041
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002042TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002043 const int http_error_code =
2044 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002045
2046 tuc_params_.fail_http_response_code = 1500;
2047 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2048 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2049 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2050 tuc_params_.expected_download_error_code =
2051 metrics::DownloadErrorCode::kHttpStatusOther;
2052
2053 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002054 EXPECT_FALSE(response.update_exists);
2055}
2056
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002057TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002058 request_params_.set_wall_clock_based_wait_enabled(true);
2059 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2060 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002061
Sen Jiang7c1171e2016-06-23 11:35:40 -07002062 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002063 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002064 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002065
2066 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2067 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2068 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2069
2070 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002071
Ben Chan9abb7632014-08-07 00:10:53 -07002072 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002073 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002074 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002075 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002076
2077 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002078 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002079 tuc_params_.expected_code = ErrorCode::kSuccess;
2080 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2081
2082 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002083 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002084}
2085
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002086TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002087 request_params_.set_wall_clock_based_wait_enabled(true);
2088 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2089 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002090
Sen Jiang7c1171e2016-06-23 11:35:40 -07002091 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002092 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2093 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002094 ASSERT_TRUE(
2095 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2096 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002097
Amin Hassani41ac04b2019-03-29 11:31:03 -07002098 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2099
2100 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002101 EXPECT_TRUE(response.update_exists);
2102
2103 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002104 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002105 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002106 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2107}
2108
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002109TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002110 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002111 base::ScopedTempDir tempdir;
2112 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002113
Marton Hunyady2abda312018-04-24 18:21:49 +02002114 request_params_.set_root(tempdir.GetPath().value());
2115 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2116 request_params_.set_app_version("1.2.3.4");
2117 request_params_.set_product_components("o.bundle=1");
2118 request_params_.set_current_channel("canary-channel");
2119 EXPECT_TRUE(
2120 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2121 request_params_.UpdateDownloadChannel();
2122 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002123
2124 tuc_params_.http_response = "invalid xml>";
2125 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2126 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2127 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2128
2129 ASSERT_FALSE(TestUpdateCheck());
2130
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002131 EXPECT_NE(
2132 string::npos,
2133 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2134 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2135 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002136 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002137}
2138
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002139TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002140 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002141 base::ScopedTempDir tempdir;
2142 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002143
Marton Hunyady2abda312018-04-24 18:21:49 +02002144 request_params_.set_root(tempdir.GetPath().value());
2145 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2146 request_params_.set_app_version("5.6.7.8");
2147 request_params_.set_product_components("o.bundle=1");
2148 request_params_.set_current_channel("stable-channel");
2149 EXPECT_TRUE(
2150 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2151 request_params_.UpdateDownloadChannel();
2152 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002153
2154 tuc_params_.http_response = "invalid xml>";
2155 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2156 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2157 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2158
2159 ASSERT_FALSE(TestUpdateCheck());
2160
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002161 EXPECT_NE(
2162 string::npos,
2163 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2164 "version=\"5.6.7.8\" "
2165 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002166 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002167 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002168}
2169
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002170// Checks that the initial ping with a=-1 r=-1 is not send when the device
2171// was powerwashed.
2172TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2173 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2174
2175 // Flag that the device was powerwashed in the past.
2176 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002177 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2178 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2179 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002180
Amin Hassani41ac04b2019-03-29 11:31:03 -07002181 ASSERT_TRUE(TestUpdateCheck());
2182
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002183 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002184 EXPECT_EQ(string::npos, post_str.find("<ping"));
2185}
2186
Amin Hassani1677e812017-06-21 13:36:36 -07002187// Checks that the initial ping with a=-1 r=-1 is not send when the device
2188// first_active_omaha_ping_sent is set.
2189TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2190 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2191
2192 // Flag that the device was not powerwashed in the past.
2193 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2194
2195 // Flag that the device has sent first active ping in the past.
2196 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2197
Amin Hassani41ac04b2019-03-29 11:31:03 -07002198 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2199 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2200 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2201
2202 ASSERT_TRUE(TestUpdateCheck());
2203
Amin Hassani1677e812017-06-21 13:36:36 -07002204 // We shouldn't send a ping in this case since
2205 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002206 EXPECT_EQ(string::npos, post_str.find("<ping"));
2207}
2208
Alex Deymo9fded1e2015-11-05 12:31:19 -08002209// Checks that the event 54 is sent on a reboot to a new update.
2210TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2211 // Flag that the device was updated in a previous boot.
2212 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2213
Amin Hassani41ac04b2019-03-29 11:31:03 -07002214 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2215 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2216 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2217
2218 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002219
2220 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002221 EXPECT_NE(
2222 string::npos,
2223 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2224 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002225 EXPECT_NE(string::npos,
2226 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2227
2228 // The previous version flag should have been removed.
2229 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2230 string prev_version;
2231 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2232 EXPECT_TRUE(prev_version.empty());
2233}
2234
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002235void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2236 bool initial_allow_p2p_for_sharing,
2237 bool omaha_disable_p2p_for_downloading,
2238 bool omaha_disable_p2p_for_sharing,
2239 bool payload_state_allow_p2p_attempt,
2240 bool expect_p2p_client_lookup,
2241 const string& p2p_client_result_url,
2242 bool expected_allow_p2p_for_downloading,
2243 bool expected_allow_p2p_for_sharing,
2244 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002245 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2246 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2247 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002248
2249 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002250 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002251 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2252 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002253 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2254 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2255 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2256 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2257 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2258 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2259 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2260 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2261 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2262 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2263
David Zeuthen8f191b22013-08-06 12:27:50 -07002264 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002265 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002266 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2267
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002268 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2269 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002270 .Times(expect_p2p_client_lookup ? 1 : 0);
2271
Alex Deymo8e18f932015-03-27 16:16:59 -07002272 fake_update_response_.disable_p2p_for_downloading =
2273 omaha_disable_p2p_for_downloading;
2274 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002275
2276 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2277 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2278
2279 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002280 EXPECT_TRUE(response.update_exists);
2281
Gilad Arnold74b5f552014-10-07 08:17:16 -07002282 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2283 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002284 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002285
Gilad Arnold74b5f552014-10-07 08:17:16 -07002286 EXPECT_EQ(expected_allow_p2p_for_downloading,
2287 actual_allow_p2p_for_downloading);
2288 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2289 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002290}
2291
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002292TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002293 P2PTest(true, // initial_allow_p2p_for_downloading
2294 true, // initial_allow_p2p_for_sharing
2295 false, // omaha_disable_p2p_for_downloading
2296 false, // omaha_disable_p2p_for_sharing
2297 true, // payload_state_allow_p2p_attempt
2298 true, // expect_p2p_client_lookup
2299 "http://1.3.5.7/p2p", // p2p_client_result_url
2300 true, // expected_allow_p2p_for_downloading
2301 true, // expected_allow_p2p_for_sharing
2302 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002303}
2304
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002305TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002306 P2PTest(true, // initial_allow_p2p_for_downloading
2307 true, // initial_allow_p2p_for_sharing
2308 false, // omaha_disable_p2p_for_downloading
2309 false, // omaha_disable_p2p_for_sharing
2310 true, // payload_state_allow_p2p_attempt
2311 true, // expect_p2p_client_lookup
2312 "", // p2p_client_result_url
2313 false, // expected_allow_p2p_for_downloading
2314 true, // expected_allow_p2p_for_sharing
2315 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002316}
2317
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002318TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002319 P2PTest(false, // initial_allow_p2p_for_downloading
2320 true, // initial_allow_p2p_for_sharing
2321 false, // omaha_disable_p2p_for_downloading
2322 false, // omaha_disable_p2p_for_sharing
2323 true, // payload_state_allow_p2p_attempt
2324 false, // expect_p2p_client_lookup
2325 "unset", // p2p_client_result_url
2326 false, // expected_allow_p2p_for_downloading
2327 true, // expected_allow_p2p_for_sharing
2328 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002329}
2330
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002331TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002332 P2PTest(true, // initial_allow_p2p_for_downloading
2333 true, // initial_allow_p2p_for_sharing
2334 true, // omaha_disable_p2p_for_downloading
2335 false, // omaha_disable_p2p_for_sharing
2336 true, // payload_state_allow_p2p_attempt
2337 false, // expect_p2p_client_lookup
2338 "unset", // p2p_client_result_url
2339 false, // expected_allow_p2p_for_downloading
2340 true, // expected_allow_p2p_for_sharing
2341 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002342}
2343
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002344TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002345 P2PTest(true, // initial_allow_p2p_for_downloading
2346 true, // initial_allow_p2p_for_sharing
2347 false, // omaha_disable_p2p_for_downloading
2348 true, // omaha_disable_p2p_for_sharing
2349 true, // payload_state_allow_p2p_attempt
2350 true, // expect_p2p_client_lookup
2351 "http://1.3.5.7/p2p", // p2p_client_result_url
2352 true, // expected_allow_p2p_for_downloading
2353 false, // expected_allow_p2p_for_sharing
2354 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002355}
2356
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002357TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002358 P2PTest(true, // initial_allow_p2p_for_downloading
2359 true, // initial_allow_p2p_for_sharing
2360 true, // omaha_disable_p2p_for_downloading
2361 true, // omaha_disable_p2p_for_sharing
2362 true, // payload_state_allow_p2p_attempt
2363 false, // expect_p2p_client_lookup
2364 "unset", // p2p_client_result_url
2365 false, // expected_allow_p2p_for_downloading
2366 false, // expected_allow_p2p_for_sharing
2367 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002368}
2369
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002370bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2371 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002372 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002373 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2374
2375 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002376}
2377
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002378TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002379 // Simulate a successful update check that happens during OOBE. The
2380 // deadline in the response is needed to force the update attempt to
2381 // occur; responses without a deadline seen during OOBE will normally
2382 // return ErrorCode::kNonCriticalUpdateInOOBE.
2383 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2384 fake_update_response_.deadline = "20101020";
2385
David Zeuthen639aa362014-02-03 16:23:44 -08002386 // Check that we parse elapsed_days in the Omaha Response correctly.
2387 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002388 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2389 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002390 EXPECT_TRUE(response.update_exists);
2391 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002392 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002393 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002394 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002395 EXPECT_EQ(prefs_days, 42);
2396
2397 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002398 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002399 EXPECT_TRUE(response.update_exists);
2400 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002401 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002402 EXPECT_EQ(prefs_days, 42);
2403
2404 // Note that elapsed_days is not necessarily divisible by 7 so check
2405 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002406 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2407 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002408 EXPECT_TRUE(response.update_exists);
2409 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002410 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002411 EXPECT_EQ(prefs_days, 21);
2412
2413 // Check that we correctly handle elapsed_days not being included in
2414 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002415 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002416 EXPECT_TRUE(response.update_exists);
2417 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002418}
2419
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002420// If there is no prefs and OOBE is not complete, we should not
2421// report anything to Omaha.
2422TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002423 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002424 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2425 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2426}
David Zeuthen639aa362014-02-03 16:23:44 -08002427
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002428// If OOBE is complete and happened on a valid date (e.g. after Jan
2429// 1 2007 0:00 PST), that date should be used and written to
2430// prefs. However, first try with an invalid date and check we do
2431// nothing.
2432TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2433 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2434 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2435 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2436 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2437}
David Zeuthen639aa362014-02-03 16:23:44 -08002438
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002439// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2440// should yield an InstallDate of 14.
2441TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2442 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2443 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2444 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2445 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002446
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002447 int64_t prefs_days;
2448 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2449 EXPECT_EQ(prefs_days, 14);
2450}
David Zeuthen639aa362014-02-03 16:23:44 -08002451
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002452// Now that we have a valid date in prefs, check that we keep using
2453// that even if OOBE date reports something else. The date Jan 30,
2454// 2007 0:00 PST should yield an InstallDate of 28... but since
2455// there's a prefs file, we should still get 14.
2456TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2457 // Set a valid date in the prefs first.
2458 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002459
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002460 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2461 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2462 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002463
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002464 int64_t prefs_days;
2465 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2466 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002467
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002468 // If we delete the prefs file, we should get 28 days.
2469 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2470 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2471 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2472 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002473}
2474
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002475// Verifies that a device with no device policy, and is not a consumer
2476// device sets the max kernel key version to the current version.
2477// ie. the same behavior as if rollback is enabled.
2478TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2479 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2480
2481 // Setup and verify some initial default values for the kernel TPM
2482 // values that control verified boot and rollback.
2483 const int min_kernel_version = 4;
2484 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2485 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2486 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2487 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2488
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002489 EXPECT_CALL(
2490 *fake_system_state_.mock_metrics_reporter(),
2491 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2492 .Times(1);
2493
Amin Hassani41ac04b2019-03-29 11:31:03 -07002494 fake_update_response_.deadline = "20101020";
2495 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2496 tuc_params_.is_consumer_device = false;
2497 tuc_params_.rollback_allowed_milestones = 3;
2498
2499 EXPECT_TRUE(TestUpdateCheck());
2500 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002501
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002502 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002503 // kernel key version. This has the effect of freezing roll
2504 // forwards indefinitely. This will hold the rollback window
2505 // open until a future change will be able to move this forward
2506 // relative the configured window.
2507 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2508 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2509}
2510
2511// Verifies that a conmsumer device with no device policy sets the
2512// max kernel key version to the current version. ie. the same
2513// behavior as if rollback is enabled.
2514TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2515 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2516
2517 // Setup and verify some initial default values for the kernel TPM
2518 // values that control verified boot and rollback.
2519 const int min_kernel_version = 3;
2520 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2521 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2522 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2523 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2524
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002525 EXPECT_CALL(
2526 *fake_system_state_.mock_metrics_reporter(),
2527 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2528 .Times(1);
2529
Amin Hassani41ac04b2019-03-29 11:31:03 -07002530 fake_update_response_.deadline = "20101020";
2531 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2532 tuc_params_.is_consumer_device = true;
2533 tuc_params_.rollback_allowed_milestones = 3;
2534
2535 EXPECT_TRUE(TestUpdateCheck());
2536 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002537
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002538 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002539 // was set to logical infinity. This is the expected behavior for
2540 // consumer devices and matches the existing behavior prior to the
2541 // rollback features.
2542 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2543 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2544}
2545
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002546// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002547// in the TPM to prevent roll forward.
2548TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2549 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2550
2551 // Setup and verify some initial default values for the kernel TPM
2552 // values that control verified boot and rollback.
2553 const int allowed_milestones = 4;
2554 const int min_kernel_version = 3;
2555 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2556 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2557 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2558 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2559
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002560 EXPECT_CALL(
2561 *fake_system_state_.mock_metrics_reporter(),
2562 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2563 .Times(1);
2564
Amin Hassani41ac04b2019-03-29 11:31:03 -07002565 fake_update_response_.deadline = "20101020";
2566 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2567 tuc_params_.is_consumer_device = false;
2568 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2569 tuc_params_.is_policy_loaded = true;
2570
2571 EXPECT_TRUE(TestUpdateCheck());
2572 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002573
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002574 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002575 // was set to the current minimum kernel key version. This has
2576 // the effect of freezing roll forwards indefinitely. This will
2577 // hold the rollback window open until a future change will
2578 // be able to move this forward relative the configured window.
2579 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2580 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2581}
2582
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002583// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002584// in the TPM to logical infinity, to allow roll forward.
2585TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2586 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2587
2588 // Setup and verify some initial default values for the kernel TPM
2589 // values that control verified boot and rollback.
2590 const int allowed_milestones = 0;
2591 const int min_kernel_version = 3;
2592 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2593 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2594 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2595 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2596
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002597 EXPECT_CALL(
2598 *fake_system_state_.mock_metrics_reporter(),
2599 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2600 .Times(1);
2601
Amin Hassani41ac04b2019-03-29 11:31:03 -07002602 fake_update_response_.deadline = "20101020";
2603 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2604 tuc_params_.is_consumer_device = false;
2605 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2606 tuc_params_.is_policy_loaded = true;
2607
2608 EXPECT_TRUE(TestUpdateCheck());
2609 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002610
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002611 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002612 // was set to logical infinity.
2613 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2614 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2615}
2616
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002617TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002618 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002619 fake_update_response_.deadline = "20101020";
2620 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2621 tuc_params_.is_consumer_device = false;
2622 tuc_params_.rollback_allowed_milestones = 4;
2623 tuc_params_.is_policy_loaded = true;
2624
2625 EXPECT_TRUE(TestUpdateCheck());
2626 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002627 EXPECT_TRUE(response.is_rollback);
2628}
2629
2630TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002631 fake_update_response_.rollback_firmware_version = "1.2";
2632 fake_update_response_.rollback_kernel_version = "3.4";
2633 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002634 fake_update_response_.deadline = "20101020";
2635 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2636 tuc_params_.is_consumer_device = false;
2637 tuc_params_.rollback_allowed_milestones = 4;
2638 tuc_params_.is_policy_loaded = true;
2639
2640 EXPECT_TRUE(TestUpdateCheck());
2641 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002642 EXPECT_TRUE(response.is_rollback);
2643 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2644 EXPECT_EQ(2, response.rollback_key_version.firmware);
2645 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2646 EXPECT_EQ(4, response.rollback_key_version.kernel);
2647}
2648
May Lippert60aa3ca2018-08-15 16:55:29 -07002649TEST_F(OmahaRequestActionTest,
2650 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2651 FakeClock fake_clock;
2652 Time now = Time::Now();
2653 fake_clock.SetWallclockTime(now);
2654 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002655 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002656
Amin Hassani41ac04b2019-03-29 11:31:03 -07002657 ASSERT_TRUE(TestUpdateCheck());
2658
May Lippert60aa3ca2018-08-15 16:55:29 -07002659 EXPECT_TRUE(response.update_exists);
2660 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2661
2662 int64_t stored_first_seen_at_time;
2663 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2664 &stored_first_seen_at_time));
2665 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2666}
2667
2668TEST_F(OmahaRequestActionTest,
2669 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2670 FakeClock fake_clock;
2671 Time now = Time::Now();
2672 fake_clock.SetWallclockTime(now);
2673 fake_system_state_.set_clock(&fake_clock);
2674
Amin Hassani41ac04b2019-03-29 11:31:03 -07002675 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2676 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2677 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2678
2679 ASSERT_TRUE(TestUpdateCheck());
2680
May Lippert60aa3ca2018-08-15 16:55:29 -07002681 EXPECT_FALSE(response.update_exists);
2682 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2683}
2684
Xiaochu Liu88d90382018-08-29 16:09:11 -07002685TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002686 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002687 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002688 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2689
2690 ASSERT_TRUE(TestUpdateCheck());
2691
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002692 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) {
Xiaochu Liu88d90382018-08-29 16:09:11 -07002693 EXPECT_NE(string::npos,
Xiaochu Liu6310be62018-10-11 15:09:03 -07002694 post_str.find("appid=\"" + fake_update_response_.app_id + "_" +
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002695 dlc_module_id + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002696 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002697 EXPECT_NE(string::npos,
2698 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2699
2700 // Count number of updatecheck tag in response.
2701 int updatecheck_count = 0;
2702 size_t pos = 0;
2703 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2704 updatecheck_count++;
2705 pos++;
2706 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002707 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002708}
2709
2710TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2711 fake_update_response_.multi_app_skip_updatecheck = true;
2712 fake_update_response_.multi_app_no_update = false;
2713 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002714 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002715 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002716 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2717
2718 ASSERT_TRUE(TestUpdateCheck());
2719
Xiaochu Liu6310be62018-10-11 15:09:03 -07002720 EXPECT_TRUE(response.update_exists);
2721 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002722}
2723
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002724TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002725 fake_update_response_.rollback = true;
2726 fake_update_response_.rollback_allowed_milestones = 4;
2727 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002728 fake_update_response_.deadline = "20101020";
2729 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2730 tuc_params_.is_consumer_device = false;
2731 tuc_params_.rollback_allowed_milestones = 4;
2732 tuc_params_.is_policy_loaded = true;
2733
2734 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
2747TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002748 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002749 fake_update_response_.rollback = true;
2750 fake_update_response_.rollback_allowed_milestones = 4;
2751 fake_update_response_.rollback_firmware_version = "4.3";
2752 fake_update_response_.rollback_kernel_version = "2.1";
2753 fake_update_response_.past_rollback_key_version =
2754 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002755 fake_update_response_.deadline = "20101020";
2756 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2757 tuc_params_.is_consumer_device = false;
2758 tuc_params_.rollback_allowed_milestones = 4;
2759 tuc_params_.is_policy_loaded = true;
2760
2761 EXPECT_TRUE(TestUpdateCheck());
2762 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002763 EXPECT_TRUE(response.is_rollback);
2764 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2765 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2766 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2767 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
2768}
2769
2770TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002771 fake_update_response_.rollback = true;
2772 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002773 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002774 request_params_.set_rollback_allowed_milestones(4);
2775
2776 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
2777 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
2778 // to 2, OmahaRequestAction will look for the key values of N-4 version but
2779 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07002780 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2781 tuc_params_.is_consumer_device = false;
2782 tuc_params_.rollback_allowed_milestones = 2;
2783 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002784
Amin Hassani41ac04b2019-03-29 11:31:03 -07002785 EXPECT_TRUE(TestUpdateCheck());
2786 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002787 EXPECT_TRUE(response.is_rollback);
2788 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2789 response.past_rollback_key_version.firmware_key);
2790 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2791 response.past_rollback_key_version.firmware);
2792 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2793 response.past_rollback_key_version.kernel_key);
2794 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2795 response.past_rollback_key_version.kernel);
2796}
2797
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04002798TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
2799 request_params_.set_device_requisition("remora");
2800 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2801 ASSERT_TRUE(TestUpdateCheck());
2802 EXPECT_NE(string::npos, post_str.find("requisition=\"remora\""));
2803}
2804
2805TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
2806 request_params_.set_device_requisition("");
2807 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2808 ASSERT_TRUE(TestUpdateCheck());
2809 EXPECT_EQ(string::npos, post_str.find("requisition"));
2810}
2811
Darin Petkov6a5b3222010-07-13 14:55:28 -07002812} // namespace chromeos_update_engine