blob: 7b676e25649975b27b98677c5db22ae9dfcbf0f5 [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"
Jae Hoon Kim051627a2019-09-03 12:56:32 -070056#include "update_engine/omaha_utils.h"
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080057#include "update_engine/update_manager/rollback_prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070058
Darin Petkov1cbd78f2010-07-29 12:38:34 -070059using base::Time;
60using base::TimeDelta;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080061using chromeos_update_manager::kRollforwardInfinity;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070062using std::pair;
Darin Petkov6a5b3222010-07-13 14:55:28 -070063using std::string;
64using std::vector;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080065using testing::_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070066using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070067using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070068using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070069using testing::Ge;
70using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080071using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070072using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070073using testing::ReturnPointee;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080074using testing::ReturnRef;
Gilad Arnold74b5f552014-10-07 08:17:16 -070075using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070076using testing::SetArgPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070077
Alex Deymo8e18f932015-03-27 16:16:59 -070078namespace {
79
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080080static_assert(kRollforwardInfinity == 0xfffffffe,
81 "Don't change the value of kRollforward infinity unless its "
82 "size has been changed in firmware.");
83
Xiaochu Liu6310be62018-10-11 15:09:03 -070084const char kCurrentVersion[] = "0.1.0.0";
Alex Deymo85616652015-10-15 18:48:31 -070085const char kTestAppId[] = "test-app-id";
Aaron Wood7dcdedf2017-09-06 17:17:41 -070086const char kTestAppId2[] = "test-app2-id";
Xiaochu Liu6310be62018-10-11 15:09:03 -070087const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck";
Alex Deymo85616652015-10-15 18:48:31 -070088
Alex Deymo8e18f932015-03-27 16:16:59 -070089// This is a helper struct to allow unit tests build an update response with the
90// values they care about.
91struct FakeUpdateResponse {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070092 string GetRollbackVersionAttributes() const {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070093 string num_milestones;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070094 num_milestones = base::NumberToString(rollback_allowed_milestones);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070095 const string rollback_version =
96 " _firmware_version_" + num_milestones + "=\"" +
97 past_rollback_key_version.first + "\"" + " _kernel_version_" +
98 num_milestones + "=\"" + past_rollback_key_version.second + "\"";
99
100 return (rollback ? " _rollback=\"true\"" : "") + rollback_version +
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700101 (!rollback_firmware_version.empty()
102 ? " _firmware_version=\"" + rollback_firmware_version + "\""
103 : "") +
104 (!rollback_kernel_version.empty()
105 ? " _kernel_version=\"" + rollback_kernel_version + "\""
106 : "");
107 }
108
Alex Deymo8e18f932015-03-27 16:16:59 -0700109 string GetNoUpdateResponse() const {
110 string entity_str;
111 if (include_entity)
112 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Sen Jiang81259682017-03-30 15:11:30 -0700113 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str +
114 "<response protocol=\"3.0\">"
115 "<daystart elapsed_seconds=\"100\"/>"
116 "<app appid=\"" +
117 app_id + "\" " +
118 (include_cohorts
119 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
120 "\" cohortname=\"" + cohortname + "\" "
121 : "") +
122 " status=\"ok\">"
123 "<ping status=\"ok\"/>"
124 "<updatecheck status=\"noupdate\"/></app>" +
125 (multi_app_no_update
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700126 ? "<app appid=\"" + app_id2 +
127 "\"><updatecheck status=\"noupdate\"/></app>"
Sen Jiang81259682017-03-30 15:11:30 -0700128 : "") +
129 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700130 }
131
132 string GetUpdateResponse() const {
Sen Jiang2703ef42017-03-16 13:36:21 -0700133 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
134 "protocol=\"3.0\">"
135 "<daystart elapsed_seconds=\"100\"" +
136 (elapsed_days.empty() ? ""
137 : (" elapsed_days=\"" + elapsed_days + "\"")) +
138 "/>"
139 "<app appid=\"" +
140 app_id + "\" " +
141 (include_cohorts
142 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
143 "\" cohortname=\"" + cohortname + "\" "
144 : "") +
145 " status=\"ok\">"
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700146 "<ping status=\"ok\"/><updatecheck status=\"ok\"" +
147 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" +
Sen Jiang2703ef42017-03-16 13:36:21 -0700148 codebase +
149 "\"/></urls>"
150 "<manifest version=\"" +
151 version +
152 "\">"
153 "<packages><package hash=\"not-used\" name=\"" +
154 filename + "\" size=\"" + base::Int64ToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800155 "\" hash_sha256=\"" + hash + "\"/>" +
156 (multi_package ? "<package name=\"package2\" size=\"222\" "
157 "hash_sha256=\"hash2\"/>"
158 : "") +
159 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700160 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700161 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
162 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700163 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700164 "IsDeltaPayload=\"true" +
165 (multi_package ? ":false" : "") +
166 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700167 "MaxDaysToScatter=\"" +
168 max_days_to_scatter +
169 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700170 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700171 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
172 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
173 : "") +
174 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700175 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700176 "/></actions></manifest></updatecheck></app>" +
177 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700178 ? "<app appid=\"" + app_id2 + "\"" +
179 (include_cohorts ? " cohort=\"cohort2\"" : "") +
180 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700181 codebase2 + "\"/></urls><manifest version=\"" + version2 +
182 "\"><packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700183 "<package name=\"package3\" size=\"333\" "
184 "hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700185 "<actions><action event=\"postinstall\" " +
186 (multi_app_self_update
187 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
188 : "IsDeltaPayload=\"false\" ") +
189 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700190 "</manifest></updatecheck></app>"
191 : "") +
192 (multi_app_no_update
193 ? "<app><updatecheck status=\"noupdate\"/></app>"
194 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700195 (multi_app_skip_updatecheck
196 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
197 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700198 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700199 }
200
201 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800202 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700203
Alex Deymo85616652015-10-15 18:48:31 -0700204 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700205 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700206 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
207 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700208 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700209 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700210 string more_info_url = "http://more/info";
211 string prompt = "true";
212 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700213 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700214 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700215 string hash = "4841534831323334";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800216 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700217 string deadline = "";
218 string max_days_to_scatter = "7";
219 string elapsed_days = "42";
220
221 // P2P setting defaults to allowed.
222 bool disable_p2p_for_downloading = false;
223 bool disable_p2p_for_sharing = false;
224
Sen Jiangfe284402018-03-21 14:03:50 -0700225 bool powerwash = false;
226
Alex Deymo8e18f932015-03-27 16:16:59 -0700227 // Omaha cohorts settings.
228 bool include_cohorts = false;
229 string cohort = "";
230 string cohorthint = "";
231 string cohortname = "";
232
233 // Whether to include the CrOS <!ENTITY> in the XML response.
234 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800235
Sen Jiang81259682017-03-30 15:11:30 -0700236 // Whether to include more than one app.
237 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700238 // Whether to include an app with noupdate="true".
239 bool multi_app_self_update = false;
240 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700241 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700242 // Whether to include an additional app with no updatecheck tag.
243 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700244 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800245 bool multi_package = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700246
247 // Whether the payload is a rollback.
248 bool rollback = false;
249 // The verified boot firmware key version for the rollback image.
250 string rollback_firmware_version = "";
251 // The verified boot kernel key version for the rollback image.
252 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700253 // The number of milestones back that the verified boot key version has been
254 // supplied.
255 uint32_t rollback_allowed_milestones = 0;
256 // The verified boot key version for the
257 // |current - rollback_allowed_milestones| most recent release.
258 // The pair contains <firmware_key_version, kernel_key_version> each
259 // of which is in the form "key_version.version".
260 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700261};
262
263} // namespace
264
Darin Petkov6a5b3222010-07-13 14:55:28 -0700265namespace chromeos_update_engine {
266
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700267class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
268 public:
269 OmahaRequestActionTestProcessorDelegate()
270 : expected_code_(ErrorCode::kSuccess),
271 interactive_(false),
272 test_http_fetcher_headers_(false) {}
273 ~OmahaRequestActionTestProcessorDelegate() override = default;
274
275 void ProcessingDone(const ActionProcessor* processor,
276 ErrorCode code) override {
277 brillo::MessageLoop::current()->BreakLoop();
278 }
279
280 void ActionCompleted(ActionProcessor* processor,
281 AbstractAction* action,
282 ErrorCode code) override {
283 // Make sure actions always succeed.
284 if (action->Type() == OmahaRequestAction::StaticType()) {
285 EXPECT_EQ(expected_code_, code);
286 // Check that the headers were set in the fetcher during the action. Note
287 // that we set this request as "interactive".
288 auto fetcher = static_cast<const MockHttpFetcher*>(
289 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
290
291 if (test_http_fetcher_headers_) {
292 EXPECT_EQ(interactive_ ? "fg" : "bg",
293 fetcher->GetHeader("X-Goog-Update-Interactivity"));
294 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
295 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
296 }
297 post_data_ = fetcher->post_data();
298 } else if (action->Type() ==
299 ObjectCollectorAction<OmahaResponse>::StaticType()) {
300 EXPECT_EQ(ErrorCode::kSuccess, code);
301 auto collector_action =
302 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
303 omaha_response_.reset(new OmahaResponse(collector_action->object()));
304 EXPECT_TRUE(omaha_response_);
305 } else {
306 EXPECT_EQ(ErrorCode::kSuccess, code);
307 }
308 }
309 ErrorCode expected_code_;
310 brillo::Blob post_data_;
311 bool interactive_;
312 bool test_http_fetcher_headers_;
313 std::unique_ptr<OmahaResponse> omaha_response_;
314};
315
Amin Hassani41ac04b2019-03-29 11:31:03 -0700316struct TestUpdateCheckParams {
317 string http_response;
318 int fail_http_response_code;
319 bool ping_only;
320 bool is_consumer_device;
321 int rollback_allowed_milestones;
322 bool is_policy_loaded;
323 ErrorCode expected_code;
324 metrics::CheckResult expected_check_result;
325 metrics::CheckReaction expected_check_reaction;
326 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700327 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700328};
329
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700330class OmahaRequestActionTest : public ::testing::Test {
331 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800332 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200333 request_params_.set_os_sp("service_pack");
334 request_params_.set_os_board("x86-generic");
335 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700336 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200337 request_params_.set_app_lang("en-US");
338 request_params_.set_current_channel("unittest");
339 request_params_.set_target_channel("unittest");
340 request_params_.set_hwid("OEM MODEL 09235 7471");
341 request_params_.set_fw_version("ChromeOSFirmware.1.0");
342 request_params_.set_ec_version("0X0A1");
343 request_params_.set_delta_okay(true);
344 request_params_.set_interactive(false);
345 request_params_.set_update_url("http://url");
346 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200347 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200348 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700349 request_params_.set_is_install(false);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800350 request_params_.set_dlc_module_ids({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200351
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700352 fake_system_state_.set_request_params(&request_params_);
353 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700354
355 // Setting the default update check params. Lookup |TestUpdateCheck()|.
356 tuc_params_ = {
357 .http_response = "",
358 .fail_http_response_code = -1,
359 .ping_only = false,
360 .is_consumer_device = true,
361 .rollback_allowed_milestones = 0,
362 .is_policy_loaded = false,
363 .expected_code = ErrorCode::kSuccess,
364 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
365 .expected_check_reaction = metrics::CheckReaction::kUpdating,
366 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
367 };
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700368 }
369
Amin Hassani41ac04b2019-03-29 11:31:03 -0700370 // This function uses the paramets in |tuc_params_| to do an update check. It
371 // will fill out |post_str| with the result data and |response| with
372 // |OmahaResponse|. Returns true iff an output response was obtained from the
373 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
374 // transfer will fail with that code. |ping_only| is passed through to the
375 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700376 //
377 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700378 // |expected_error_code| parameters are for checking expectations about
379 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
380 // statistics. Use the appropriate ::kUnset value to specify that the given
381 // metric should not be reported.
382 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700383
Amin Hassani41ac04b2019-03-29 11:31:03 -0700384 // Tests events using |event| and |https_response|. It will fill up |post_str|
385 // with the result data.
386 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700387
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800388 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700389 // only a ping or also an updatecheck.
390 void PingTest(bool ping_only);
391
392 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800393 bool InstallDateParseHelper(const string& elapsed_days,
394 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700395
396 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800397 void P2PTest(bool initial_allow_p2p_for_downloading,
398 bool initial_allow_p2p_for_sharing,
399 bool omaha_disable_p2p_for_downloading,
400 bool omaha_disable_p2p_for_sharing,
401 bool payload_state_allow_p2p_attempt,
402 bool expect_p2p_client_lookup,
403 const string& p2p_client_result_url,
404 bool expected_allow_p2p_for_downloading,
405 bool expected_allow_p2p_for_sharing,
406 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700407
408 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700409 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200410 // Used by all tests.
411 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700412
413 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700414
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700415 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700416
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700417 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700418
419 TestUpdateCheckParams tuc_params_;
420
421 // TODO(ahassani): Add trailing _ to these two variables.
422 OmahaResponse response;
423 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700424};
425
Amin Hassani41ac04b2019-03-29 11:31:03 -0700426bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700427 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700428 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700429 auto fetcher =
430 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
431 tuc_params_.http_response.size(),
432 nullptr);
433 if (tuc_params_.fail_http_response_code >= 0) {
434 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700435 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200436 // This ensures the tests didn't forget to update fake_system_state_ if they
437 // are not using the default request_params_.
438 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
439
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700440 auto omaha_request_action =
441 std::make_unique<OmahaRequestAction>(&fake_system_state_,
442 nullptr,
443 std::move(fetcher),
444 tuc_params_.ping_only,
445 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700446
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800447 auto mock_policy_provider =
448 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
449 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700450 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800451
452 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700453 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800454
455 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700456 const bool get_allowed_milestone_succeeds =
457 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800458 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700459 .WillRepeatedly(
460 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
461 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800462
463 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
464 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700465 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800466
Amin Hassani41ac04b2019-03-29 11:31:03 -0700467 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700468 delegate_.interactive_ = request_params_.interactive();
469 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700470 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700471 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700472
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700473 auto collector_action =
474 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
475 BondActions(omaha_request_action.get(), collector_action.get());
476 processor.EnqueueAction(std::move(omaha_request_action));
477 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700478
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700479 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
480 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800481 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700482
Amin Hassani41ac04b2019-03-29 11:31:03 -0700483 EXPECT_CALL(
484 *fake_system_state_.mock_metrics_reporter(),
485 ReportUpdateCheckMetrics(_,
486 tuc_params_.expected_check_result,
487 tuc_params_.expected_check_reaction,
488 tuc_params_.expected_download_error_code))
489 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800490
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700491 loop.PostTask(base::Bind(
492 [](ActionProcessor* processor) { processor->StartProcessing(); },
493 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700494 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700495 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700496 if (delegate_.omaha_response_)
497 response = *delegate_.omaha_response_;
498 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700499 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700500}
501
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700502// Tests Event requests -- they should always succeed. |out_post_data| may be
503// null; if non-null, the post-data received by the mock HttpFetcher is
504// returned.
505void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700506 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700507 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700508 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700509
510 auto action = std::make_unique<OmahaRequestAction>(
511 &fake_system_state_,
512 event,
513 std::make_unique<MockHttpFetcher>(
514 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700515 false,
516 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700517 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700518 processor.set_delegate(&delegate_);
519 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700520
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700521 loop.PostTask(base::Bind(
522 [](ActionProcessor* processor) { processor->StartProcessing(); },
523 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700524 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700525 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700526
Amin Hassani41ac04b2019-03-29 11:31:03 -0700527 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700528}
529
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700530TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700531 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700532 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
533 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
534 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
535 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
536
537 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400538 EXPECT_FALSE(response.update_exists);
539}
540
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700541TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700542 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
543 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
544 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
545
546 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700547 EXPECT_FALSE(response.update_exists);
548}
549
Sen Jiang81259682017-03-30 15:11:30 -0700550TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700551 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700552 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
553 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
554 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
555
556 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700557 EXPECT_FALSE(response.update_exists);
558}
559
Sen Jiang00adf7b2017-06-26 15:57:29 -0700560TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700561 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700562 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
563 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
564 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
565
566 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700567 EXPECT_FALSE(response.update_exists);
568}
569
570TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700571 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700572 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
573 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700574 "</manifest></updatecheck></app></response>";
575 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
576 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
577
578 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700579 EXPECT_FALSE(response.update_exists);
580}
581
Alex Deymo8e18f932015-03-27 16:16:59 -0700582// Test that all the values in the response are parsed in a normal update
583// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700584TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700585 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700586 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
587
588 ASSERT_TRUE(TestUpdateCheck());
589
Darin Petkov6a5b3222010-07-13 14:55:28 -0700590 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700591 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700592 EXPECT_EQ("", response.system_version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800593 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
594 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700595 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800596 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
597 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700598 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700599 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
600 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700601 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700602 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700603 // persisted.
604 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
605 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
606 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700607}
608
Sen Jiang0affc2c2017-02-10 15:55:05 -0800609TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800610 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700611 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
612
613 ASSERT_TRUE(TestUpdateCheck());
614
Sen Jiang0affc2c2017-02-10 15:55:05 -0800615 EXPECT_TRUE(response.update_exists);
616 EXPECT_EQ(fake_update_response_.version, response.version);
617 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
618 response.packages[0].payload_urls[0]);
619 EXPECT_EQ(fake_update_response_.codebase + "package2",
620 response.packages[1].payload_urls[0]);
621 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
622 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700623 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700624 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800625 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700626 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800627 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700628 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700629 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700630}
631
632TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700633 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700634 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
635
636 ASSERT_TRUE(TestUpdateCheck());
637
Sen Jiang81259682017-03-30 15:11:30 -0700638 EXPECT_TRUE(response.update_exists);
639 EXPECT_EQ(fake_update_response_.version, response.version);
640 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
641 response.packages[0].payload_urls[0]);
642 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
643 response.packages[1].payload_urls[0]);
644 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
645 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
646 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700647 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700648 ASSERT_EQ(2u, response.packages.size());
649 EXPECT_EQ(string("hash3"), response.packages[1].hash);
650 EXPECT_EQ(333u, response.packages[1].size);
651 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700652 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700653}
654
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700655TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) {
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700656 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700657 // Trigger the lining up of the app and system versions.
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700658 request_params_.set_system_app_id(fake_update_response_.app_id2);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700659 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700660
Amin Hassani41ac04b2019-03-29 11:31:03 -0700661 ASSERT_TRUE(TestUpdateCheck());
662
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700663 EXPECT_TRUE(response.update_exists);
664 EXPECT_EQ(fake_update_response_.version, response.version);
665 EXPECT_EQ(fake_update_response_.version2, response.system_version);
666 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
667 response.packages[0].payload_urls[0]);
668 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
669 response.packages[1].payload_urls[0]);
670 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
671 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
672 EXPECT_EQ(11u, response.packages[0].metadata_size);
673 EXPECT_EQ(true, response.packages[0].is_delta);
674 ASSERT_EQ(2u, response.packages.size());
675 EXPECT_EQ(string("hash3"), response.packages[1].hash);
676 EXPECT_EQ(333u, response.packages[1].size);
677 EXPECT_EQ(33u, response.packages[1].metadata_size);
678 EXPECT_EQ(false, response.packages[1].is_delta);
679}
680
Sen Jiang81259682017-03-30 15:11:30 -0700681TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700682 fake_update_response_.multi_app = true;
683 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700684 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
685
686 ASSERT_TRUE(TestUpdateCheck());
687
Sen Jiang81259682017-03-30 15:11:30 -0700688 EXPECT_TRUE(response.update_exists);
689 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700690 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700691 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
692 response.packages[0].payload_urls[0]);
693 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
694 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
695 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700696 ASSERT_EQ(2u, response.packages.size());
697 EXPECT_EQ(string("hash3"), response.packages[1].hash);
698 EXPECT_EQ(333u, response.packages[1].size);
699 EXPECT_EQ(33u, response.packages[1].metadata_size);
700 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700701}
702
703TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700704 fake_update_response_.multi_app = true;
705 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700706 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
707
708 ASSERT_TRUE(TestUpdateCheck());
709
Sen Jiang81259682017-03-30 15:11:30 -0700710 EXPECT_TRUE(response.update_exists);
711 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700712 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700713 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
714 response.packages[0].payload_urls[0]);
715 EXPECT_EQ(fake_update_response_.codebase + "package2",
716 response.packages[1].payload_urls[0]);
717 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
718 response.packages[2].payload_urls[0]);
719 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
720 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
721 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700722 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700723 ASSERT_EQ(3u, response.packages.size());
724 EXPECT_EQ(string("hash2"), response.packages[1].hash);
725 EXPECT_EQ(222u, response.packages[1].size);
726 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700727 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700728 EXPECT_EQ(string("hash3"), response.packages[2].hash);
729 EXPECT_EQ(333u, response.packages[2].size);
730 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700731 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800732}
733
Sen Jiangfe284402018-03-21 14:03:50 -0700734TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700735 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700736 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
737
738 ASSERT_TRUE(TestUpdateCheck());
739
Sen Jiangfe284402018-03-21 14:03:50 -0700740 EXPECT_TRUE(response.update_exists);
741 EXPECT_TRUE(response.powerwash_required);
742}
743
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700744TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700745 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700746 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700747 tuc_params_.http_response = "invalid xml>";
748 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
749 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
750 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
751
752 ASSERT_FALSE(TestUpdateCheck());
753
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700754 EXPECT_FALSE(response.update_exists);
755}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700756
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700757TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700758 request_params_.set_interactive(false);
759 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700760 tuc_params_.http_response = "invalid xml>";
761 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
762 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
763 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
764
765 ASSERT_FALSE(TestUpdateCheck());
766
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700767 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700768}
769
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700770TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700771 // Set up a connection manager that doesn't allow a valid update over
772 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700773 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700774 fake_system_state_.set_connection_manager(&mock_cm);
775
Alex Deymo30534502015-07-20 15:06:33 -0700776 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800777 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
778 SetArgPointee<1>(ConnectionTethering::kUnknown),
779 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700780 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
781 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700782
Amin Hassani41ac04b2019-03-29 11:31:03 -0700783 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
784 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
785 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
786
787 ASSERT_FALSE(TestUpdateCheck());
788
Chris Sosa77f79e82014-06-02 18:16:24 -0700789 EXPECT_FALSE(response.update_exists);
790}
791
Weidong Guo421ff332017-04-17 10:08:38 -0700792TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
793 // This test tests that update over cellular is allowed as device policy
794 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700795 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700796 fake_system_state_.set_connection_manager(&mock_cm);
797
798 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
799 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
800 SetArgPointee<1>(ConnectionTethering::kUnknown),
801 Return(true)));
802 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
803 .WillRepeatedly(Return(true));
804 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
805 .WillRepeatedly(Return(true));
806
Amin Hassani41ac04b2019-03-29 11:31:03 -0700807 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
808
809 ASSERT_TRUE(TestUpdateCheck());
810
Weidong Guo421ff332017-04-17 10:08:38 -0700811 EXPECT_TRUE(response.update_exists);
812}
813
814TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
815 // This test tests that update over cellular is blocked as device policy
816 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700817 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700818 fake_system_state_.set_connection_manager(&mock_cm);
819
820 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
821 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
822 SetArgPointee<1>(ConnectionTethering::kUnknown),
823 Return(true)));
824 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
825 .WillRepeatedly(Return(true));
826 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
827 .WillRepeatedly(Return(false));
828
Amin Hassani41ac04b2019-03-29 11:31:03 -0700829 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
830 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
831 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
832
833 ASSERT_FALSE(TestUpdateCheck());
834
Weidong Guo421ff332017-04-17 10:08:38 -0700835 EXPECT_FALSE(response.update_exists);
836}
837
838TEST_F(OmahaRequestActionTest,
839 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
840 // This test tests that, when device policy is not set, update over cellular
841 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700842 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700843 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
844 fake_system_state_.set_connection_manager(&mock_cm);
845
846 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
847 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
848 SetArgPointee<1>(ConnectionTethering::kUnknown),
849 Return(true)));
850 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
851 .WillRepeatedly(Return(false));
852 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
853 .WillRepeatedly(Return(true));
854
Amin Hassani41ac04b2019-03-29 11:31:03 -0700855 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
856
857 ASSERT_TRUE(TestUpdateCheck());
858
Weidong Guo421ff332017-04-17 10:08:38 -0700859 EXPECT_TRUE(response.update_exists);
860}
861
862TEST_F(OmahaRequestActionTest,
863 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
864 // This test tests that, when device policy is not set and permission for
865 // update over cellular is set to false or does not exist, update over
866 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700867 MockConnectionManager mock_cm;
868 // A version different from the version in omaha response.
869 string diff_version = "99.99.99";
870 // A size different from the size in omaha response.
871 int64_t diff_size = 999;
872
873 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
874 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
875 // This test tests cellular (3G) being the only connection type being allowed.
876 fake_system_state_.set_connection_manager(&mock_cm);
877
878 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
879 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
880 SetArgPointee<1>(ConnectionTethering::kUnknown),
881 Return(true)));
882 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
883 .WillRepeatedly(Return(false));
884 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
885 .WillRepeatedly(Return(true));
886
Amin Hassani41ac04b2019-03-29 11:31:03 -0700887 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
888 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
889 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
890
891 ASSERT_FALSE(TestUpdateCheck());
892
Weidong Guo421ff332017-04-17 10:08:38 -0700893 EXPECT_FALSE(response.update_exists);
894}
895
896TEST_F(OmahaRequestActionTest,
897 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
898 // This test tests that, when device policy is not set and permission for
899 // update over cellular is set to false or does not exist, update over
900 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700901 MockConnectionManager mock_cm;
902 // A version same as the version in omaha response.
903 string new_version = fake_update_response_.version;
904 // A size same as the size in omaha response.
905 int64_t new_size = fake_update_response_.size;
906
907 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
908 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
909 fake_system_state_.set_connection_manager(&mock_cm);
910
911 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
912 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
913 SetArgPointee<1>(ConnectionTethering::kUnknown),
914 Return(true)));
915 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
916 .WillRepeatedly(Return(false));
917 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
918 .WillRepeatedly(Return(true));
919
Amin Hassani41ac04b2019-03-29 11:31:03 -0700920 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
921
922 ASSERT_TRUE(TestUpdateCheck());
923
Weidong Guo421ff332017-04-17 10:08:38 -0700924 EXPECT_TRUE(response.update_exists);
925}
926
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700927TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700928 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700929 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700930 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700931 fake_update_response_.version = rollback_version;
932 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
933 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
934 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700935
Chris Sosa77f79e82014-06-02 18:16:24 -0700936 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800937 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700938
Amin Hassani41ac04b2019-03-29 11:31:03 -0700939 ASSERT_FALSE(TestUpdateCheck());
940
Chris Sosa77f79e82014-06-02 18:16:24 -0700941 EXPECT_FALSE(response.update_exists);
942}
943
Marton Hunyadyc2882062018-05-14 17:28:25 +0200944// Verify that update checks called during OOBE will not try to download an
945// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700946TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200947 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700948 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
949 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
950 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
951 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -0700952
Sen Jiang8cd42342018-01-31 12:06:59 -0800953 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700954 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700955 ASSERT_FALSE(TestUpdateCheck());
956
Kevin Cernekee2494e282016-03-29 18:03:53 -0700957 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +0200958}
Kevin Cernekee2494e282016-03-29 18:03:53 -0700959
Marton Hunyadyc2882062018-05-14 17:28:25 +0200960// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
961// enabled.
962TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200963 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700964 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700965 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700966
Amin Hassani41ac04b2019-03-29 11:31:03 -0700967 ASSERT_TRUE(TestUpdateCheck());
968
Kevin Cernekee2494e282016-03-29 18:03:53 -0700969 EXPECT_TRUE(response.update_exists);
970}
971
Marton Hunyadyc2882062018-05-14 17:28:25 +0200972// Verify that update checks called during OOBE will still try to download an
973// update if the response includes the deadline field.
974TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200975 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
976 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700977 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +0200978
Amin Hassani41ac04b2019-03-29 11:31:03 -0700979 ASSERT_TRUE(TestUpdateCheck());
980
Marton Hunyadyc2882062018-05-14 17:28:25 +0200981 EXPECT_TRUE(response.update_exists);
982}
983
984// Verify that update checks called during OOBE will not try to download an
985// update if a rollback happened, even when the response includes the deadline
986// field.
987TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200988 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
989 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700990 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
991 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
992 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
993 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
994
Marton Hunyadyc2882062018-05-14 17:28:25 +0200995 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
996 .WillOnce(Return(true));
997
Amin Hassani41ac04b2019-03-29 11:31:03 -0700998 ASSERT_FALSE(TestUpdateCheck());
999
Marton Hunyadyc2882062018-05-14 17:28:25 +02001000 EXPECT_FALSE(response.update_exists);
1001}
1002
Toni Barzic61544e62018-10-11 14:37:30 -07001003// Verify that non-critical updates are skipped by reporting the
1004// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1005// i.e. when the update would need user permission. Note that reporting
1006// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1007// in OOBE (warning the user about an update that will be skipped).
1008TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001009 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1010
1011 MockConnectionManager mock_cm;
1012 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001013 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1014 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1015 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1016 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001017
1018 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1019 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1020 SetArgPointee<1>(ConnectionTethering::kUnknown),
1021 Return(true)));
1022 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1023 .WillRepeatedly(Return(false));
1024
Amin Hassani41ac04b2019-03-29 11:31:03 -07001025 ASSERT_FALSE(TestUpdateCheck());
1026
Toni Barzic61544e62018-10-11 14:37:30 -07001027 EXPECT_FALSE(response.update_exists);
1028}
1029
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001030TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001031 request_params_.set_wall_clock_based_wait_enabled(true);
1032 request_params_.set_update_check_count_wait_enabled(false);
1033 request_params_.set_waiting_period(TimeDelta::FromDays(2));
May Lippert60aa3ca2018-08-15 16:55:29 -07001034 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001035 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1036 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1037 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001038
Amin Hassani41ac04b2019-03-29 11:31:03 -07001039 ASSERT_FALSE(TestUpdateCheck());
1040
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001041 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001042}
1043
1044TEST_F(OmahaRequestActionTest,
1045 WallClockBasedWaitAloneCausesScatteringInteractive) {
1046 request_params_.set_wall_clock_based_wait_enabled(true);
1047 request_params_.set_update_check_count_wait_enabled(false);
1048 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1049 request_params_.set_interactive(true);
1050 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1051 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001052
1053 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001054 ASSERT_TRUE(TestUpdateCheck());
1055
Chris Sosa968d0572013-08-23 14:46:02 -07001056 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001057}
1058
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001059TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001060 request_params_.set_wall_clock_based_wait_enabled(false);
1061 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1062 request_params_.set_update_check_count_wait_enabled(true);
1063 request_params_.set_min_update_checks_needed(1);
1064 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001065 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001066
Amin Hassani41ac04b2019-03-29 11:31:03 -07001067 ASSERT_TRUE(TestUpdateCheck());
1068
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001069 EXPECT_TRUE(response.update_exists);
1070}
1071
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001072TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001073 request_params_.set_wall_clock_based_wait_enabled(true);
1074 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1075 request_params_.set_update_check_count_wait_enabled(true);
1076 request_params_.set_min_update_checks_needed(1);
1077 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001078 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001079 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1080
1081 ASSERT_TRUE(TestUpdateCheck());
1082
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001083 EXPECT_TRUE(response.update_exists);
1084}
1085
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001086TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001087 request_params_.set_wall_clock_based_wait_enabled(true);
1088 request_params_.set_waiting_period(TimeDelta());
1089 request_params_.set_update_check_count_wait_enabled(true);
1090 request_params_.set_min_update_checks_needed(0);
1091 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001092 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001093 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001094
Amin Hassani41ac04b2019-03-29 11:31:03 -07001095 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001096
Ben Chan9abb7632014-08-07 00:10:53 -07001097 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001098 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001099 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001100 EXPECT_TRUE(response.update_exists);
1101}
1102
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001103TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001104 request_params_.set_wall_clock_based_wait_enabled(true);
1105 request_params_.set_waiting_period(TimeDelta());
1106 request_params_.set_update_check_count_wait_enabled(true);
1107 request_params_.set_min_update_checks_needed(1);
1108 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001109 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001110 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1111 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1112 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001113
Amin Hassani41ac04b2019-03-29 11:31:03 -07001114 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001115
Ben Chan9abb7632014-08-07 00:10:53 -07001116 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001117 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001118 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001119 EXPECT_FALSE(response.update_exists);
1120}
1121
Amin Hassani41ac04b2019-03-29 11:31:03 -07001122TEST_F(OmahaRequestActionTest,
1123 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001124 request_params_.set_wall_clock_based_wait_enabled(true);
1125 request_params_.set_waiting_period(TimeDelta());
1126 request_params_.set_update_check_count_wait_enabled(true);
1127 request_params_.set_min_update_checks_needed(1);
1128 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001129 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001130 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001131 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1132
1133 // Verify if we are interactive check we don't defer.
1134 ASSERT_TRUE(TestUpdateCheck());
1135
1136 EXPECT_TRUE(response.update_exists);
1137}
1138
1139TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1140 request_params_.set_wall_clock_based_wait_enabled(true);
1141 request_params_.set_waiting_period(TimeDelta());
1142 request_params_.set_update_check_count_wait_enabled(true);
1143 request_params_.set_min_update_checks_needed(1);
1144 request_params_.set_max_update_checks_allowed(8);
1145 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1146 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1147 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1148 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001149
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001150 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001151 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001152
Ben Chan9abb7632014-08-07 00:10:53 -07001153 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001154 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001155 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001156 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001157 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001158 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001159}
1160
1161TEST_F(OmahaRequestActionTest,
1162 ExistingUpdateCheckCountCausesScatteringInteractive) {
1163 request_params_.set_wall_clock_based_wait_enabled(true);
1164 request_params_.set_waiting_period(TimeDelta());
1165 request_params_.set_update_check_count_wait_enabled(true);
1166 request_params_.set_min_update_checks_needed(1);
1167 request_params_.set_max_update_checks_allowed(8);
1168 request_params_.set_interactive(true);
1169 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1170 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1171
1172 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001173
1174 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001175 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001176 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001177}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001178
Adolfo Victoria497044c2018-07-18 07:51:42 -07001179TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1180 // If staging is on, the value for max days to scatter should be ignored, and
1181 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001182 request_params_.set_wall_clock_based_wait_enabled(true);
1183 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1184 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001185 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1186
Adolfo Victoria497044c2018-07-18 07:51:42 -07001187 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1188 // This should not prevent scattering due to staging.
1189 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001190 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1191 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1192 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1193
1194 ASSERT_FALSE(TestUpdateCheck());
1195
Adolfo Victoria497044c2018-07-18 07:51:42 -07001196 EXPECT_FALSE(response.update_exists);
1197
1198 // Interactive updates should not be affected.
1199 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001200 tuc_params_.expected_code = ErrorCode::kSuccess;
1201 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1202
1203 ASSERT_TRUE(TestUpdateCheck());
1204
Adolfo Victoria497044c2018-07-18 07:51:42 -07001205 EXPECT_TRUE(response.update_exists);
1206}
1207
Alex Deymo8e18f932015-03-27 16:16:59 -07001208TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001209 fake_update_response_.include_cohorts = true;
1210 fake_update_response_.cohort = "s/154454/8479665";
1211 fake_update_response_.cohorthint = "please-put-me-on-beta";
1212 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001213 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001214
Amin Hassani41ac04b2019-03-29 11:31:03 -07001215 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001216
1217 string value;
1218 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1219 EXPECT_EQ(fake_update_response_.cohort, value);
1220
1221 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1222 EXPECT_EQ(fake_update_response_.cohorthint, value);
1223
1224 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1225 EXPECT_EQ(fake_update_response_.cohortname, value);
1226}
1227
1228TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001229 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1230 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1231 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1232 fake_update_response_.include_cohorts = true;
1233 fake_update_response_.cohort = "s/154454/8479665";
1234 fake_update_response_.cohorthint = "please-put-me-on-beta";
1235 fake_update_response_.cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001236 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001237
Amin Hassani41ac04b2019-03-29 11:31:03 -07001238 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001239
1240 string value;
1241 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1242 EXPECT_EQ(fake_update_response_.cohort, value);
1243
1244 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1245 EXPECT_EQ(fake_update_response_.cohorthint, value);
1246
1247 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1248}
1249
1250TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001251 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001252
Amin Hassani41ac04b2019-03-29 11:31:03 -07001253 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1254 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001255
1256 string value;
1257 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1258 EXPECT_EQ("old_value", value);
1259
1260 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1261 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1262}
1263
Alex Deymo00d79ac2015-06-29 15:41:49 -07001264TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001265 fake_update_response_.include_cohorts = true;
1266 fake_update_response_.cohort = "s/154454/8479665";
1267 fake_update_response_.cohorthint = "please-put-me-on-beta";
1268 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001269 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1270 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1271 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001272
Amin Hassani41ac04b2019-03-29 11:31:03 -07001273 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001274
1275 string value;
1276 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1277 EXPECT_EQ(fake_update_response_.cohort, value);
1278
1279 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1280 EXPECT_EQ(fake_update_response_.cohorthint, value);
1281
1282 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1283 EXPECT_EQ(fake_update_response_.cohortname, value);
1284}
1285
Sen Jiangb1e063a2017-09-15 17:44:31 -07001286TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001287 fake_update_response_.multi_app = true;
1288 fake_update_response_.include_cohorts = true;
1289 fake_update_response_.cohort = "s/154454/8479665";
1290 fake_update_response_.cohorthint = "please-put-me-on-beta";
1291 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001292 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001293
Amin Hassani41ac04b2019-03-29 11:31:03 -07001294 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001295
1296 string value;
1297 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1298 EXPECT_EQ(fake_update_response_.cohort, value);
1299
1300 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1301 EXPECT_EQ(fake_update_response_.cohorthint, value);
1302
1303 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1304 EXPECT_EQ(fake_update_response_.cohortname, value);
1305}
1306
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001307TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001308 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001309 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001310 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001311
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001312 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001313 &fake_system_state_,
1314 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001315 std::make_unique<MockHttpFetcher>(
1316 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001317 false,
1318 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001319 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001320 processor.set_delegate(&delegate_);
1321 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001322
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001323 loop.PostTask(base::Bind(
1324 [](ActionProcessor* processor) { processor->StartProcessing(); },
1325 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001326 loop.Run();
1327 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001328 EXPECT_FALSE(processor.IsRunning());
1329}
1330
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001331TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001332 tuc_params_.http_response = "invalid xml>";
1333 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1334 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1335 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1336
1337 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001338 EXPECT_FALSE(response.update_exists);
1339}
1340
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001341TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001342 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1343 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1344 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1345
1346 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001347 EXPECT_FALSE(response.update_exists);
1348}
1349
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001350TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001351 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001352 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1353 "<daystart elapsed_seconds=\"100\"/>"
1354 "<app appid=\"foo\" status=\"ok\">"
1355 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001356 "<updatecheck/></app></response>";
1357 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1358 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1359 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1360
1361 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001362 EXPECT_FALSE(response.update_exists);
1363}
1364
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001365TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001366 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001367 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1368 "<daystart elapsed_seconds=\"100\"/>"
1369 "<app appid=\"foo\" status=\"ok\">"
1370 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001371 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1372 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1373 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1374 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1375
1376 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001377 EXPECT_FALSE(response.update_exists);
1378}
1379
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001380TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001381 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001382 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1383 "<daystart elapsed_seconds=\"100\"/>"
1384 "<app appid=\"foo\" status=\"ok\">"
1385 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001386 "</app></response>";
1387 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1388 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1389 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1390
1391 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001392 EXPECT_FALSE(response.update_exists);
1393}
1394
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001395TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001396 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001397 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1398 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001399 // the appid needs to match that in the request params
1400 "<app appid=\"" +
1401 fake_update_response_.app_id +
1402 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001403 "<updatecheck status=\"ok\">"
1404 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001405 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001406 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001407 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001408 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001409 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001410 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001411 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001412 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001413
Amin Hassani41ac04b2019-03-29 11:31:03 -07001414 ASSERT_TRUE(TestUpdateCheck());
1415
Darin Petkov6a5b3222010-07-13 14:55:28 -07001416 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001417 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001418 EXPECT_EQ("http://missing/field/test/f",
1419 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001420 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001421 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1422 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001423 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001424 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001425}
1426
1427namespace {
1428class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1429 public:
1430 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001431 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001432 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001433};
1434
Alex Deymo60ca1a72015-06-18 18:19:15 -07001435void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001436 processor->StartProcessing();
1437 CHECK(processor->IsRunning());
1438 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001439}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001440} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001441
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001442TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001443 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001444 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001445
Alex Deymo60ca1a72015-06-18 18:19:15 -07001446 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001447 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001448 &fake_system_state_,
1449 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001450 std::make_unique<MockHttpFetcher>(
1451 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001452 false,
1453 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001454 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001455 ActionProcessor processor;
1456 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001457 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001458
Alex Deymo60ca1a72015-06-18 18:19:15 -07001459 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1460 loop.Run();
1461 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001462}
1463
Alex Deymob0d74eb2015-03-30 17:59:17 -07001464TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001465 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001466 request_params_.set_os_sp("testtheservice_pack>");
1467 request_params_.set_os_board("x86 generic<id");
1468 request_params_.set_current_channel("unittest_track&lt;");
1469 request_params_.set_target_channel("unittest_track&lt;");
1470 request_params_.set_hwid("<OEM MODEL>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001471 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1472 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001473 fake_prefs_.SetString(
1474 kPrefsOmahaCohortName,
1475 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001476 tuc_params_.http_response = "invalid xml>";
1477 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1478 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1479 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1480
1481 ASSERT_FALSE(TestUpdateCheck());
1482
Alex Deymo8e18f932015-03-27 16:16:59 -07001483 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1484 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1485 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1486 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1487 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1488 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1489 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1490 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1491 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1492 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1493 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1494 // Values from Prefs that are too big are removed from the XML instead of
1495 // encoded.
1496 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001497}
1498
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001499TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001500 fake_update_response_.deadline = "&lt;20110101";
1501 fake_update_response_.more_info_url = "testthe&lt;url";
1502 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001503 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1504
1505 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001506
Sen Jiang0affc2c2017-02-10 15:55:05 -08001507 EXPECT_EQ("testthe<url", response.more_info_url);
1508 EXPECT_EQ("testthe&codebase/file.signed",
1509 response.packages[0].payload_urls[0]);
1510 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001511}
1512
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001513TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001514 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001515 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001516 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001517
Amin Hassani41ac04b2019-03-29 11:31:03 -07001518 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001519 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001520}
1521
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001522TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001523 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001524 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001525 tuc_params_.http_response = "invalid xml>";
1526 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1527 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1528 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001529
Darin Petkov95508da2011-01-05 12:42:29 -08001530 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001531 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001532 // An existing but empty previous version means that we didn't reboot to a new
1533 // update, therefore, no need to update the previous version.
1534 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001535 ASSERT_FALSE(TestUpdateCheck());
1536
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001537 EXPECT_NE(
1538 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1539 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001540 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001541 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1542 string::npos);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001543 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos);
1544 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001545 // No <event> tag should be sent if we didn't reboot to an update.
1546 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001547}
1548
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001549TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001550 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001551 "invalid xml>");
1552
Alex Vakulenko75039d72014-03-25 12:36:28 -07001553 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001554 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001555 OmahaEvent::kTypeUpdateDownloadStarted,
1556 OmahaEvent::kResultSuccess);
1557 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001558 EXPECT_EQ(post_str.find("ping"), string::npos);
1559 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001560}
1561
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001562TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001563 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001564 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001565 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001566 "invalid xml>");
1567
Alex Vakulenko75039d72014-03-25 12:36:28 -07001568 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001569 " <event eventtype=\"%d\" eventresult=\"%d\" "
1570 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001571 OmahaEvent::kTypeDownloadComplete,
1572 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001573 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001574 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001575 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001576}
1577
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001578TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001579 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001580 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001581 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001582 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001583 std::make_unique<MockHttpFetcher>(
1584 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001585 false,
1586 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001587 EXPECT_FALSE(update_check_action.IsEvent());
1588
1589 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001590 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001591 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001592 std::make_unique<MockHttpFetcher>(
1593 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001594 false,
1595 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001596 EXPECT_TRUE(event_action.IsEvent());
1597}
1598
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001599TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001600 tuc_params_.http_response = "invalid xml>";
1601 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1602 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1603 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1604
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001605 for (int i = 0; i < 2; i++) {
1606 bool delta_okay = i == 1;
1607 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001608 request_params_.set_delta_okay(delta_okay);
1609
Amin Hassani41ac04b2019-03-29 11:31:03 -07001610 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001611 EXPECT_NE(
1612 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1613 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001614 << "i = " << i;
1615 }
1616}
1617
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001618TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001619 tuc_params_.http_response = "invalid xml>";
1620 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1621 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1622 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1623
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001624 for (int i = 0; i < 2; i++) {
1625 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001626 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001627 request_params_.set_interactive(interactive);
1628
Amin Hassani41ac04b2019-03-29 11:31:03 -07001629 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001630 EXPECT_NE(post_str.find(
1631 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001632 string::npos)
1633 << "i = " << i;
1634 }
1635}
1636
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001637TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001638 tuc_params_.http_response = "invalid xml>";
1639 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1640 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1641 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1642
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001643 for (int i = 0; i < 2; i++) {
1644 bool target_version_set = i == 1;
1645 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001646 request_params_.set_target_version_prefix(target_version_prefix);
1647
Amin Hassani41ac04b2019-03-29 11:31:03 -07001648 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001649 if (target_version_set) {
1650 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1651 string::npos)
1652 << "i = " << i;
1653 } else {
1654 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1655 << "i = " << i;
1656 }
1657 }
1658}
1659
1660TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001661 tuc_params_.http_response = "invalid xml>";
1662 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1663 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1664 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1665
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001666 for (int i = 0; i < 4; i++) {
1667 bool rollback_allowed = i / 2 == 0;
1668 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001669 request_params_.set_target_version_prefix(target_version_set ? "10032."
1670 : "");
1671 request_params_.set_rollback_allowed(rollback_allowed);
1672
Amin Hassani41ac04b2019-03-29 11:31:03 -07001673 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001674 if (rollback_allowed && target_version_set) {
1675 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1676 << "i = " << i;
1677 } else {
1678 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1679 }
1680 }
1681}
1682
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001683TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001684 OmahaEvent default_event;
1685 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1686 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001687 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001688
1689 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1690 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1691 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001692 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001693
1694 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1695 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001696 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001697 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1698 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001699 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001700}
1701
Askar Aitzhan570ca872019-04-24 11:16:12 +02001702TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
Askar Aitzhan18fff842019-06-21 23:24:37 +02001703 // If DeviceQuickFixBuildToken value is set it takes precedence over pref
1704 // value.
1705 constexpr char autoupdate_token[] = "autoupdate_token>";
1706 constexpr char xml_encoded_autoupdate_token[] = "autoupdate_token&gt;";
1707 constexpr char omaha_cohort_hint[] = "cohort_hint";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001708
1709 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1710 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1711 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1712 request_params_.set_autoupdate_token(autoupdate_token);
Askar Aitzhan18fff842019-06-21 23:24:37 +02001713 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan570ca872019-04-24 11:16:12 +02001714
1715 ASSERT_TRUE(TestUpdateCheck());
1716
Askar Aitzhan18fff842019-06-21 23:24:37 +02001717 EXPECT_NE(string::npos,
1718 post_str.find("cohorthint=\"" +
1719 string(xml_encoded_autoupdate_token) + "\""));
1720 EXPECT_EQ(string::npos, post_str.find(autoupdate_token));
1721 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
1722}
1723
1724TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsNotSetTest) {
1725 // If DeviceQuickFixBuildToken is not set, pref value will be provided in
1726 // cohorthint attribute.
1727 constexpr char omaha_cohort_hint[] = "evil_string>";
1728 constexpr char xml_encoded_cohort_hint[] = "evil_string&gt;";
1729
1730 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1731 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1732 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1733 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
1734
1735 ASSERT_TRUE(TestUpdateCheck());
1736
1737 EXPECT_NE(
1738 string::npos,
1739 post_str.find("cohorthint=\"" + string(xml_encoded_cohort_hint) + "\""));
1740 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001741}
1742
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001743void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001744 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001745 fake_system_state_.set_prefs(&prefs);
1746 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001747 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001748 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1749 // Add a few hours to the day difference to test no rounding, etc.
1750 int64_t five_days_ago =
1751 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1752 int64_t six_days_ago =
1753 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1754 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001755 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001756 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001757 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001758 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001759 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001760
1761 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1762 tuc_params_.ping_only = ping_only;
1763 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1764 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1765
1766 ASSERT_TRUE(TestUpdateCheck());
1767
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001768 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1769 string::npos);
1770 if (ping_only) {
1771 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1772 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1773 } else {
1774 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1775 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001776 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001777}
1778
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001779TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001780 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001781}
1782
1783TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001784 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001785}
1786
1787TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001788 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001789 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001790 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001791 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001792 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001793 int64_t three_days_ago =
1794 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1795 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001796 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001797 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001798 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001799 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001800 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001801 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001802
1803 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1804 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1805 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1806
1807 ASSERT_TRUE(TestUpdateCheck());
1808
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001809 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001810}
1811
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001812TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001813 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001814 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001815 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001816 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001817 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001818 int64_t four_days_ago =
1819 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1820 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001821 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001822 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001823 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001824 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001825 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001826 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001827
1828 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1829 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1830 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1831
1832 ASSERT_TRUE(TestUpdateCheck());
1833
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001834 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001835 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001836}
1837
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001838TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001839 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001840 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001841 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001842 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001843 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001844 int64_t one_hour_ago =
1845 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001846 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001847 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001848 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001849 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001850 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001851 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001852 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1853 // send a ping.
1854 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1855 .WillOnce(Return(true));
1856 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1857 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001858
1859 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1860 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1861 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1862
1863 ASSERT_TRUE(TestUpdateCheck());
1864
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001865 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001866}
1867
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001868TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001869 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001870 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001871 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001872 int64_t now = Time::Now().ToInternalValue();
1873 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001874 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001875 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001876 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001877 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1878 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001879
1880 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1881 tuc_params_.ping_only = true;
1882 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
1883 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1884
1885 EXPECT_TRUE(TestUpdateCheck());
1886 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07001887}
1888
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001889TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001890 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001891 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001892 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001893 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001894 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001895 int64_t future =
1896 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001897 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001898 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001899 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001900 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001901 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001902 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001903 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1904 .WillOnce(Return(true));
1905 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1906 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001907
1908 tuc_params_.http_response =
1909 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1910 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
1911 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1912 "<updatecheck status=\"noupdate\"/></app></response>";
1913 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1914 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1915
1916 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001917 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001918}
1919
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001920TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001921 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001922 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001923 // may fail if it runs for longer than 5 seconds. It shouldn't run
1924 // that long though.
1925 int64_t midnight =
1926 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1927 int64_t midnight_slack =
1928 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001929 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001930 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001931 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1932 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001933 EXPECT_CALL(prefs,
1934 SetInt64(kPrefsLastActivePingDay,
1935 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001936 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001937 EXPECT_CALL(prefs,
1938 SetInt64(kPrefsLastRollCallPingDay,
1939 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001940 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001941
1942 tuc_params_.http_response =
1943 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1944 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
1945 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1946 "<updatecheck status=\"noupdate\"/></app></response>";
1947 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1948 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1949
1950 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001951}
1952
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001953TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001954 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001955 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001956 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1957 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001958 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1959 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001960
1961 tuc_params_.http_response =
1962 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1963 "protocol=\"3.0\"><daystart blah=\"200\"/>"
1964 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1965 "<updatecheck status=\"noupdate\"/></app></response>";
1966 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1967 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1968
1969 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001970}
1971
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001972TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001973 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001974 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001975 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1976 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001977 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1978 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001979
1980 tuc_params_.http_response =
1981 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1982 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
1983 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1984 "<updatecheck status=\"noupdate\"/></app></response>";
1985 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1986 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1987
1988 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001989}
1990
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001991TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001992 tuc_params_.http_response = "invalid xml>";
1993 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1994 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1995 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1996
1997 ASSERT_FALSE(TestUpdateCheck());
1998
Darin Petkov84c763c2010-07-29 16:27:58 -07001999 EXPECT_EQ(post_str.find("machineid="), string::npos);
2000 EXPECT_EQ(post_str.find("userid="), string::npos);
2001}
2002
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002003TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002004 const int http_error_code =
2005 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002006 tuc_params_.fail_http_response_code = 501;
2007 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2008 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2009 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2010 tuc_params_.expected_download_error_code =
2011 static_cast<metrics::DownloadErrorCode>(501);
2012
2013 ASSERT_FALSE(TestUpdateCheck());
2014
Darin Petkovedc522e2010-11-05 09:35:17 -07002015 EXPECT_FALSE(response.update_exists);
2016}
2017
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002018TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002019 const int http_error_code =
2020 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002021
2022 tuc_params_.fail_http_response_code = 1500;
2023 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2024 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2025 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2026 tuc_params_.expected_download_error_code =
2027 metrics::DownloadErrorCode::kHttpStatusOther;
2028
2029 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002030 EXPECT_FALSE(response.update_exists);
2031}
2032
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002033TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002034 request_params_.set_wall_clock_based_wait_enabled(true);
2035 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2036 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002037
Sen Jiang7c1171e2016-06-23 11:35:40 -07002038 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002039 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002040 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002041
2042 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2043 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2044 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2045
2046 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002047
Ben Chan9abb7632014-08-07 00:10:53 -07002048 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002049 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002050 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002051 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002052
2053 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002054 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002055 tuc_params_.expected_code = ErrorCode::kSuccess;
2056 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2057
2058 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002059 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002060}
2061
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002062TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002063 request_params_.set_wall_clock_based_wait_enabled(true);
2064 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2065 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002066
Sen Jiang7c1171e2016-06-23 11:35:40 -07002067 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002068 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2069 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002070 ASSERT_TRUE(
2071 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2072 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002073
Amin Hassani41ac04b2019-03-29 11:31:03 -07002074 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2075
2076 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002077 EXPECT_TRUE(response.update_exists);
2078
2079 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002080 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002081 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002082 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2083}
2084
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002085TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002086 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002087 base::ScopedTempDir tempdir;
2088 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002089
Marton Hunyady2abda312018-04-24 18:21:49 +02002090 request_params_.set_root(tempdir.GetPath().value());
2091 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2092 request_params_.set_app_version("1.2.3.4");
2093 request_params_.set_product_components("o.bundle=1");
2094 request_params_.set_current_channel("canary-channel");
2095 EXPECT_TRUE(
2096 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2097 request_params_.UpdateDownloadChannel();
2098 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002099
2100 tuc_params_.http_response = "invalid xml>";
2101 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2102 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2103 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2104
2105 ASSERT_FALSE(TestUpdateCheck());
2106
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002107 EXPECT_NE(
2108 string::npos,
2109 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2110 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2111 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002112 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002113}
2114
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002115TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002116 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002117 base::ScopedTempDir tempdir;
2118 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002119
Marton Hunyady2abda312018-04-24 18:21:49 +02002120 request_params_.set_root(tempdir.GetPath().value());
2121 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2122 request_params_.set_app_version("5.6.7.8");
2123 request_params_.set_product_components("o.bundle=1");
2124 request_params_.set_current_channel("stable-channel");
2125 EXPECT_TRUE(
2126 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2127 request_params_.UpdateDownloadChannel();
2128 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002129
2130 tuc_params_.http_response = "invalid xml>";
2131 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2132 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2133 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2134
2135 ASSERT_FALSE(TestUpdateCheck());
2136
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002137 EXPECT_NE(
2138 string::npos,
2139 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2140 "version=\"5.6.7.8\" "
2141 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002142 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002143 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002144}
2145
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002146// Checks that the initial ping with a=-1 r=-1 is not send when the device
2147// was powerwashed.
2148TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2149 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2150
2151 // Flag that the device was powerwashed in the past.
2152 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002153 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2154 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2155 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002156
Amin Hassani41ac04b2019-03-29 11:31:03 -07002157 ASSERT_TRUE(TestUpdateCheck());
2158
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002159 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002160 EXPECT_EQ(string::npos, post_str.find("<ping"));
2161}
2162
Amin Hassani1677e812017-06-21 13:36:36 -07002163// Checks that the initial ping with a=-1 r=-1 is not send when the device
2164// first_active_omaha_ping_sent is set.
2165TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2166 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2167
2168 // Flag that the device was not powerwashed in the past.
2169 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2170
2171 // Flag that the device has sent first active ping in the past.
2172 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2173
Amin Hassani41ac04b2019-03-29 11:31:03 -07002174 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2175 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2176 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2177
2178 ASSERT_TRUE(TestUpdateCheck());
2179
Amin Hassani1677e812017-06-21 13:36:36 -07002180 // We shouldn't send a ping in this case since
2181 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002182 EXPECT_EQ(string::npos, post_str.find("<ping"));
2183}
2184
Alex Deymo9fded1e2015-11-05 12:31:19 -08002185// Checks that the event 54 is sent on a reboot to a new update.
2186TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2187 // Flag that the device was updated in a previous boot.
2188 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2189
Amin Hassani41ac04b2019-03-29 11:31:03 -07002190 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2191 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2192 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2193
2194 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002195
2196 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002197 EXPECT_NE(
2198 string::npos,
2199 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2200 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002201 EXPECT_NE(string::npos,
2202 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2203
2204 // The previous version flag should have been removed.
2205 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2206 string prev_version;
2207 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2208 EXPECT_TRUE(prev_version.empty());
2209}
2210
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002211void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2212 bool initial_allow_p2p_for_sharing,
2213 bool omaha_disable_p2p_for_downloading,
2214 bool omaha_disable_p2p_for_sharing,
2215 bool payload_state_allow_p2p_attempt,
2216 bool expect_p2p_client_lookup,
2217 const string& p2p_client_result_url,
2218 bool expected_allow_p2p_for_downloading,
2219 bool expected_allow_p2p_for_sharing,
2220 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002221 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2222 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2223 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002224
2225 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002226 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002227 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2228 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002229 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2230 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2231 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2232 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2233 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2234 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2235 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2236 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2237 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2238 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2239
David Zeuthen8f191b22013-08-06 12:27:50 -07002240 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002241 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002242 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2243
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002244 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2245 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002246 .Times(expect_p2p_client_lookup ? 1 : 0);
2247
Alex Deymo8e18f932015-03-27 16:16:59 -07002248 fake_update_response_.disable_p2p_for_downloading =
2249 omaha_disable_p2p_for_downloading;
2250 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002251
2252 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2253 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2254
2255 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002256 EXPECT_TRUE(response.update_exists);
2257
Gilad Arnold74b5f552014-10-07 08:17:16 -07002258 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2259 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002260 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002261
Gilad Arnold74b5f552014-10-07 08:17:16 -07002262 EXPECT_EQ(expected_allow_p2p_for_downloading,
2263 actual_allow_p2p_for_downloading);
2264 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2265 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002266}
2267
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002268TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002269 P2PTest(true, // initial_allow_p2p_for_downloading
2270 true, // initial_allow_p2p_for_sharing
2271 false, // omaha_disable_p2p_for_downloading
2272 false, // omaha_disable_p2p_for_sharing
2273 true, // payload_state_allow_p2p_attempt
2274 true, // expect_p2p_client_lookup
2275 "http://1.3.5.7/p2p", // p2p_client_result_url
2276 true, // expected_allow_p2p_for_downloading
2277 true, // expected_allow_p2p_for_sharing
2278 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002279}
2280
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002281TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002282 P2PTest(true, // initial_allow_p2p_for_downloading
2283 true, // initial_allow_p2p_for_sharing
2284 false, // omaha_disable_p2p_for_downloading
2285 false, // omaha_disable_p2p_for_sharing
2286 true, // payload_state_allow_p2p_attempt
2287 true, // expect_p2p_client_lookup
2288 "", // p2p_client_result_url
2289 false, // expected_allow_p2p_for_downloading
2290 true, // expected_allow_p2p_for_sharing
2291 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002292}
2293
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002294TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002295 P2PTest(false, // initial_allow_p2p_for_downloading
2296 true, // initial_allow_p2p_for_sharing
2297 false, // omaha_disable_p2p_for_downloading
2298 false, // omaha_disable_p2p_for_sharing
2299 true, // payload_state_allow_p2p_attempt
2300 false, // expect_p2p_client_lookup
2301 "unset", // p2p_client_result_url
2302 false, // expected_allow_p2p_for_downloading
2303 true, // expected_allow_p2p_for_sharing
2304 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002305}
2306
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002307TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002308 P2PTest(true, // initial_allow_p2p_for_downloading
2309 true, // initial_allow_p2p_for_sharing
2310 true, // omaha_disable_p2p_for_downloading
2311 false, // omaha_disable_p2p_for_sharing
2312 true, // payload_state_allow_p2p_attempt
2313 false, // expect_p2p_client_lookup
2314 "unset", // p2p_client_result_url
2315 false, // expected_allow_p2p_for_downloading
2316 true, // expected_allow_p2p_for_sharing
2317 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002318}
2319
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002320TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002321 P2PTest(true, // initial_allow_p2p_for_downloading
2322 true, // initial_allow_p2p_for_sharing
2323 false, // omaha_disable_p2p_for_downloading
2324 true, // omaha_disable_p2p_for_sharing
2325 true, // payload_state_allow_p2p_attempt
2326 true, // expect_p2p_client_lookup
2327 "http://1.3.5.7/p2p", // p2p_client_result_url
2328 true, // expected_allow_p2p_for_downloading
2329 false, // expected_allow_p2p_for_sharing
2330 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002331}
2332
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002333TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002334 P2PTest(true, // initial_allow_p2p_for_downloading
2335 true, // initial_allow_p2p_for_sharing
2336 true, // omaha_disable_p2p_for_downloading
2337 true, // omaha_disable_p2p_for_sharing
2338 true, // payload_state_allow_p2p_attempt
2339 false, // expect_p2p_client_lookup
2340 "unset", // p2p_client_result_url
2341 false, // expected_allow_p2p_for_downloading
2342 false, // expected_allow_p2p_for_sharing
2343 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002344}
2345
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002346bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2347 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002348 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002349 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2350
2351 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002352}
2353
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002354TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002355 // Simulate a successful update check that happens during OOBE. The
2356 // deadline in the response is needed to force the update attempt to
2357 // occur; responses without a deadline seen during OOBE will normally
2358 // return ErrorCode::kNonCriticalUpdateInOOBE.
2359 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2360 fake_update_response_.deadline = "20101020";
2361
David Zeuthen639aa362014-02-03 16:23:44 -08002362 // Check that we parse elapsed_days in the Omaha Response correctly.
2363 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002364 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2365 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002366 EXPECT_TRUE(response.update_exists);
2367 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002368 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002369 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002370 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002371 EXPECT_EQ(prefs_days, 42);
2372
2373 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002374 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002375 EXPECT_TRUE(response.update_exists);
2376 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002377 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002378 EXPECT_EQ(prefs_days, 42);
2379
2380 // Note that elapsed_days is not necessarily divisible by 7 so check
2381 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002382 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2383 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002384 EXPECT_TRUE(response.update_exists);
2385 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002386 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002387 EXPECT_EQ(prefs_days, 21);
2388
2389 // Check that we correctly handle elapsed_days not being included in
2390 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002391 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002392 EXPECT_TRUE(response.update_exists);
2393 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002394}
2395
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002396// If there is no prefs and OOBE is not complete, we should not
2397// report anything to Omaha.
2398TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002399 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002400 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2401 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2402}
David Zeuthen639aa362014-02-03 16:23:44 -08002403
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002404// If OOBE is complete and happened on a valid date (e.g. after Jan
2405// 1 2007 0:00 PST), that date should be used and written to
2406// prefs. However, first try with an invalid date and check we do
2407// nothing.
2408TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2409 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2410 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2411 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2412 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2413}
David Zeuthen639aa362014-02-03 16:23:44 -08002414
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002415// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2416// should yield an InstallDate of 14.
2417TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2418 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2419 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2420 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2421 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002422
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002423 int64_t prefs_days;
2424 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2425 EXPECT_EQ(prefs_days, 14);
2426}
David Zeuthen639aa362014-02-03 16:23:44 -08002427
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002428// Now that we have a valid date in prefs, check that we keep using
2429// that even if OOBE date reports something else. The date Jan 30,
2430// 2007 0:00 PST should yield an InstallDate of 28... but since
2431// there's a prefs file, we should still get 14.
2432TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2433 // Set a valid date in the prefs first.
2434 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002435
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002436 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2437 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2438 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002439
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002440 int64_t prefs_days;
2441 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2442 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002443
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002444 // If we delete the prefs file, we should get 28 days.
2445 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2446 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2447 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2448 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002449}
2450
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002451// Verifies that a device with no device policy, and is not a consumer
2452// device sets the max kernel key version to the current version.
2453// ie. the same behavior as if rollback is enabled.
2454TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2455 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2456
2457 // Setup and verify some initial default values for the kernel TPM
2458 // values that control verified boot and rollback.
2459 const int min_kernel_version = 4;
2460 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2461 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2462 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2463 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2464
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002465 EXPECT_CALL(
2466 *fake_system_state_.mock_metrics_reporter(),
2467 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2468 .Times(1);
2469
Amin Hassani41ac04b2019-03-29 11:31:03 -07002470 fake_update_response_.deadline = "20101020";
2471 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2472 tuc_params_.is_consumer_device = false;
2473 tuc_params_.rollback_allowed_milestones = 3;
2474
2475 EXPECT_TRUE(TestUpdateCheck());
2476 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002477
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002478 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002479 // kernel key version. This has the effect of freezing roll
2480 // forwards indefinitely. This will hold the rollback window
2481 // open until a future change will be able to move this forward
2482 // relative the configured window.
2483 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2484 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2485}
2486
2487// Verifies that a conmsumer device with no device policy sets the
2488// max kernel key version to the current version. ie. the same
2489// behavior as if rollback is enabled.
2490TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2491 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2492
2493 // Setup and verify some initial default values for the kernel TPM
2494 // values that control verified boot and rollback.
2495 const int min_kernel_version = 3;
2496 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2497 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2498 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2499 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2500
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002501 EXPECT_CALL(
2502 *fake_system_state_.mock_metrics_reporter(),
2503 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2504 .Times(1);
2505
Amin Hassani41ac04b2019-03-29 11:31:03 -07002506 fake_update_response_.deadline = "20101020";
2507 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2508 tuc_params_.is_consumer_device = true;
2509 tuc_params_.rollback_allowed_milestones = 3;
2510
2511 EXPECT_TRUE(TestUpdateCheck());
2512 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002513
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002514 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002515 // was set to logical infinity. This is the expected behavior for
2516 // consumer devices and matches the existing behavior prior to the
2517 // rollback features.
2518 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2519 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2520}
2521
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002522// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002523// in the TPM to prevent roll forward.
2524TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2525 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2526
2527 // Setup and verify some initial default values for the kernel TPM
2528 // values that control verified boot and rollback.
2529 const int allowed_milestones = 4;
2530 const int min_kernel_version = 3;
2531 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2532 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2533 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2534 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2535
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002536 EXPECT_CALL(
2537 *fake_system_state_.mock_metrics_reporter(),
2538 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2539 .Times(1);
2540
Amin Hassani41ac04b2019-03-29 11:31:03 -07002541 fake_update_response_.deadline = "20101020";
2542 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2543 tuc_params_.is_consumer_device = false;
2544 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2545 tuc_params_.is_policy_loaded = true;
2546
2547 EXPECT_TRUE(TestUpdateCheck());
2548 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002549
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002550 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002551 // was set to the current minimum kernel key version. This has
2552 // the effect of freezing roll forwards indefinitely. This will
2553 // hold the rollback window open until a future change will
2554 // be able to move this forward relative the configured window.
2555 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2556 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2557}
2558
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002559// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002560// in the TPM to logical infinity, to allow roll forward.
2561TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2562 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2563
2564 // Setup and verify some initial default values for the kernel TPM
2565 // values that control verified boot and rollback.
2566 const int allowed_milestones = 0;
2567 const int min_kernel_version = 3;
2568 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2569 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2570 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2571 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2572
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002573 EXPECT_CALL(
2574 *fake_system_state_.mock_metrics_reporter(),
2575 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2576 .Times(1);
2577
Amin Hassani41ac04b2019-03-29 11:31:03 -07002578 fake_update_response_.deadline = "20101020";
2579 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2580 tuc_params_.is_consumer_device = false;
2581 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2582 tuc_params_.is_policy_loaded = true;
2583
2584 EXPECT_TRUE(TestUpdateCheck());
2585 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002586
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002587 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002588 // was set to logical infinity.
2589 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2590 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2591}
2592
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002593TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002594 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002595 fake_update_response_.deadline = "20101020";
2596 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2597 tuc_params_.is_consumer_device = false;
2598 tuc_params_.rollback_allowed_milestones = 4;
2599 tuc_params_.is_policy_loaded = true;
2600
2601 EXPECT_TRUE(TestUpdateCheck());
2602 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002603 EXPECT_TRUE(response.is_rollback);
2604}
2605
2606TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002607 fake_update_response_.rollback_firmware_version = "1.2";
2608 fake_update_response_.rollback_kernel_version = "3.4";
2609 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002610 fake_update_response_.deadline = "20101020";
2611 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2612 tuc_params_.is_consumer_device = false;
2613 tuc_params_.rollback_allowed_milestones = 4;
2614 tuc_params_.is_policy_loaded = true;
2615
2616 EXPECT_TRUE(TestUpdateCheck());
2617 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002618 EXPECT_TRUE(response.is_rollback);
2619 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2620 EXPECT_EQ(2, response.rollback_key_version.firmware);
2621 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2622 EXPECT_EQ(4, response.rollback_key_version.kernel);
2623}
2624
May Lippert60aa3ca2018-08-15 16:55:29 -07002625TEST_F(OmahaRequestActionTest,
2626 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2627 FakeClock fake_clock;
2628 Time now = Time::Now();
2629 fake_clock.SetWallclockTime(now);
2630 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002631 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002632
Amin Hassani41ac04b2019-03-29 11:31:03 -07002633 ASSERT_TRUE(TestUpdateCheck());
2634
May Lippert60aa3ca2018-08-15 16:55:29 -07002635 EXPECT_TRUE(response.update_exists);
2636 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2637
2638 int64_t stored_first_seen_at_time;
2639 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2640 &stored_first_seen_at_time));
2641 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2642}
2643
2644TEST_F(OmahaRequestActionTest,
2645 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2646 FakeClock fake_clock;
2647 Time now = Time::Now();
2648 fake_clock.SetWallclockTime(now);
2649 fake_system_state_.set_clock(&fake_clock);
2650
Amin Hassani41ac04b2019-03-29 11:31:03 -07002651 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2652 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2653 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2654
2655 ASSERT_TRUE(TestUpdateCheck());
2656
May Lippert60aa3ca2018-08-15 16:55:29 -07002657 EXPECT_FALSE(response.update_exists);
2658 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2659}
2660
Xiaochu Liu88d90382018-08-29 16:09:11 -07002661TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002662 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002663 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002664 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2665
2666 ASSERT_TRUE(TestUpdateCheck());
2667
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002668 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) {
Xiaochu Liu88d90382018-08-29 16:09:11 -07002669 EXPECT_NE(string::npos,
Xiaochu Liu6310be62018-10-11 15:09:03 -07002670 post_str.find("appid=\"" + fake_update_response_.app_id + "_" +
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002671 dlc_module_id + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002672 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002673 EXPECT_NE(string::npos,
2674 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2675
2676 // Count number of updatecheck tag in response.
2677 int updatecheck_count = 0;
2678 size_t pos = 0;
2679 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2680 updatecheck_count++;
2681 pos++;
2682 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002683 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002684}
2685
2686TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2687 fake_update_response_.multi_app_skip_updatecheck = true;
2688 fake_update_response_.multi_app_no_update = false;
2689 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002690 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002691 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002692 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2693
2694 ASSERT_TRUE(TestUpdateCheck());
2695
Xiaochu Liu6310be62018-10-11 15:09:03 -07002696 EXPECT_TRUE(response.update_exists);
2697 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002698}
2699
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002700TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002701 fake_update_response_.rollback = true;
2702 fake_update_response_.rollback_allowed_milestones = 4;
2703 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002704 fake_update_response_.deadline = "20101020";
2705 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2706 tuc_params_.is_consumer_device = false;
2707 tuc_params_.rollback_allowed_milestones = 4;
2708 tuc_params_.is_policy_loaded = true;
2709
2710 EXPECT_TRUE(TestUpdateCheck());
2711 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002712 EXPECT_TRUE(response.is_rollback);
2713 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2714 response.past_rollback_key_version.firmware_key);
2715 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2716 response.past_rollback_key_version.firmware);
2717 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2718 response.past_rollback_key_version.kernel_key);
2719 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2720 response.past_rollback_key_version.kernel);
2721}
2722
2723TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002724 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002725 fake_update_response_.rollback = true;
2726 fake_update_response_.rollback_allowed_milestones = 4;
2727 fake_update_response_.rollback_firmware_version = "4.3";
2728 fake_update_response_.rollback_kernel_version = "2.1";
2729 fake_update_response_.past_rollback_key_version =
2730 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002731 fake_update_response_.deadline = "20101020";
2732 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2733 tuc_params_.is_consumer_device = false;
2734 tuc_params_.rollback_allowed_milestones = 4;
2735 tuc_params_.is_policy_loaded = true;
2736
2737 EXPECT_TRUE(TestUpdateCheck());
2738 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002739 EXPECT_TRUE(response.is_rollback);
2740 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2741 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2742 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2743 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
2744}
2745
2746TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002747 fake_update_response_.rollback = true;
2748 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002749 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002750 request_params_.set_rollback_allowed_milestones(4);
2751
2752 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
2753 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
2754 // to 2, OmahaRequestAction will look for the key values of N-4 version but
2755 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07002756 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2757 tuc_params_.is_consumer_device = false;
2758 tuc_params_.rollback_allowed_milestones = 2;
2759 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002760
Amin Hassani41ac04b2019-03-29 11:31:03 -07002761 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(std::numeric_limits<uint16_t>::max(),
2765 response.past_rollback_key_version.firmware_key);
2766 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2767 response.past_rollback_key_version.firmware);
2768 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2769 response.past_rollback_key_version.kernel_key);
2770 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2771 response.past_rollback_key_version.kernel);
2772}
2773
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04002774TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
2775 request_params_.set_device_requisition("remora");
2776 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2777 ASSERT_TRUE(TestUpdateCheck());
2778 EXPECT_NE(string::npos, post_str.find("requisition=\"remora\""));
2779}
2780
2781TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
2782 request_params_.set_device_requisition("");
2783 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2784 ASSERT_TRUE(TestUpdateCheck());
2785 EXPECT_EQ(string::npos, post_str.find("requisition"));
2786}
2787
Amin Hassani2cbb0692019-10-30 13:36:17 -07002788TEST_F(OmahaRequestActionTest, PersistEolDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002789 tuc_params_.http_response =
2790 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2791 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2792 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07002793 "_eol_date=\"200\" _foo=\"bar\"/></app></response>";
2794 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2795 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2796
2797 ASSERT_TRUE(TestUpdateCheck());
2798
2799 string eol_date;
2800 EXPECT_TRUE(
2801 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
2802 EXPECT_EQ("200", eol_date);
2803}
2804
2805TEST_F(OmahaRequestActionTest, PersistEolMissingDateTest) {
2806 tuc_params_.http_response =
2807 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2808 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2809 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002810 "_foo=\"bar\"/></app></response>";
2811 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2812 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2813
Jae Hoon Kimb3146742019-11-07 13:24:24 -08002814 const string kDate = "123";
2815 fake_system_state_.prefs()->SetString(kPrefsOmahaEolDate, kDate);
2816
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002817 ASSERT_TRUE(TestUpdateCheck());
2818
Amin Hassani2cbb0692019-10-30 13:36:17 -07002819 string eol_date;
Jae Hoon Kimb3146742019-11-07 13:24:24 -08002820 EXPECT_TRUE(
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002821 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
Jae Hoon Kimb3146742019-11-07 13:24:24 -08002822 EXPECT_EQ(kDate, eol_date);
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002823}
2824
Amin Hassani2cbb0692019-10-30 13:36:17 -07002825TEST_F(OmahaRequestActionTest, PersistEolBadDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002826 tuc_params_.http_response =
2827 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2828 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2829 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07002830 "_eol_date=\"bad\" foo=\"bar\"/></app></response>";
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002831 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2832 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2833
2834 ASSERT_TRUE(TestUpdateCheck());
2835
Amin Hassani2cbb0692019-10-30 13:36:17 -07002836 string eol_date;
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002837 EXPECT_TRUE(
2838 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
2839 EXPECT_EQ(kEolDateInvalid, StringToEolDate(eol_date));
2840}
2841
Darin Petkov6a5b3222010-07-13 14:55:28 -07002842} // namespace chromeos_update_engine