blob: 41b2520b06c3f4d77889a06be1644e0fd3c35694 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov6a5b3222010-07-13 14:55:28 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_action.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070021#include <limits>
Ben Chanab5a0af2017-10-12 14:57:50 -070022#include <memory>
Darin Petkov6a5b3222010-07-13 14:55:28 -070023#include <string>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080024#include <utility>
Darin Petkov6a5b3222010-07-13 14:55:28 -070025#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070026
Alex Deymo60ca1a72015-06-18 18:19:15 -070027#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070028#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070029#include <base/files/scoped_temp_dir.h>
Ben Chan5c02c132017-06-27 07:10:36 -070030#include <base/memory/ptr_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070031#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070032#include <base/strings/string_util.h>
33#include <base/strings/stringprintf.h>
34#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070035#include <brillo/message_loops/fake_message_loop.h>
36#include <brillo/message_loops/message_loop.h>
37#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070038#include <gtest/gtest.h>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080039#include <policy/libpolicy.h>
40#include <policy/mock_libpolicy.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070041
Alex Deymo39910dc2015-11-09 17:04:30 -080042#include "update_engine/common/action_pipe.h"
43#include "update_engine/common/constants.h"
44#include "update_engine/common/fake_prefs.h"
45#include "update_engine/common/hash_calculator.h"
46#include "update_engine/common/mock_http_fetcher.h"
47#include "update_engine/common/platform_constants.h"
48#include "update_engine/common/prefs.h"
49#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030050#include "update_engine/fake_system_state.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070051#include "update_engine/metrics_reporter_interface.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070052#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070053#include "update_engine/mock_payload_state.h"
Amin Hassani7fca2862019-03-28 16:09:22 -070054#include "update_engine/omaha_request_builder_xml.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055#include "update_engine/omaha_request_params.h"
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080056#include "update_engine/update_manager/rollback_prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070057
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058using base::Time;
59using base::TimeDelta;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080060using chromeos_update_manager::kRollforwardInfinity;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070061using std::pair;
Darin Petkov6a5b3222010-07-13 14:55:28 -070062using std::string;
63using std::vector;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080064using testing::_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070066using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070067using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070068using testing::Ge;
69using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080070using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070071using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070072using testing::ReturnPointee;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080073using testing::ReturnRef;
Gilad Arnold74b5f552014-10-07 08:17:16 -070074using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070075using testing::SetArgPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070076
Alex Deymo8e18f932015-03-27 16:16:59 -070077namespace {
78
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080079static_assert(kRollforwardInfinity == 0xfffffffe,
80 "Don't change the value of kRollforward infinity unless its "
81 "size has been changed in firmware.");
82
Xiaochu Liu6310be62018-10-11 15:09:03 -070083const char kCurrentVersion[] = "0.1.0.0";
Alex Deymo85616652015-10-15 18:48:31 -070084const char kTestAppId[] = "test-app-id";
Aaron Wood7dcdedf2017-09-06 17:17:41 -070085const char kTestAppId2[] = "test-app2-id";
Xiaochu Liu6310be62018-10-11 15:09:03 -070086const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck";
Alex Deymo85616652015-10-15 18:48:31 -070087
Alex Deymo8e18f932015-03-27 16:16:59 -070088// This is a helper struct to allow unit tests build an update response with the
89// values they care about.
90struct FakeUpdateResponse {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070091 string GetRollbackVersionAttributes() const {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070092 string num_milestones;
93#if BASE_VER < 576279
94 num_milestones = base::IntToString(rollback_allowed_milestones);
95#else
96 num_milestones = base::NumberToString(rollback_allowed_milestones);
97#endif
98 const string rollback_version =
99 " _firmware_version_" + num_milestones + "=\"" +
100 past_rollback_key_version.first + "\"" + " _kernel_version_" +
101 num_milestones + "=\"" + past_rollback_key_version.second + "\"";
102
103 return (rollback ? " _rollback=\"true\"" : "") + rollback_version +
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700104 (!rollback_firmware_version.empty()
105 ? " _firmware_version=\"" + rollback_firmware_version + "\""
106 : "") +
107 (!rollback_kernel_version.empty()
108 ? " _kernel_version=\"" + rollback_kernel_version + "\""
109 : "");
110 }
111
Alex Deymo8e18f932015-03-27 16:16:59 -0700112 string GetNoUpdateResponse() const {
113 string entity_str;
114 if (include_entity)
115 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Sen Jiang81259682017-03-30 15:11:30 -0700116 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str +
117 "<response protocol=\"3.0\">"
118 "<daystart elapsed_seconds=\"100\"/>"
119 "<app appid=\"" +
120 app_id + "\" " +
121 (include_cohorts
122 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
123 "\" cohortname=\"" + cohortname + "\" "
124 : "") +
125 " status=\"ok\">"
126 "<ping status=\"ok\"/>"
127 "<updatecheck status=\"noupdate\"/></app>" +
128 (multi_app_no_update
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700129 ? "<app appid=\"" + app_id2 +
130 "\"><updatecheck status=\"noupdate\"/></app>"
Sen Jiang81259682017-03-30 15:11:30 -0700131 : "") +
132 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700133 }
134
135 string GetUpdateResponse() const {
Sen Jiang2703ef42017-03-16 13:36:21 -0700136 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
137 "protocol=\"3.0\">"
138 "<daystart elapsed_seconds=\"100\"" +
139 (elapsed_days.empty() ? ""
140 : (" elapsed_days=\"" + elapsed_days + "\"")) +
141 "/>"
142 "<app appid=\"" +
143 app_id + "\" " +
144 (include_cohorts
145 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
146 "\" cohortname=\"" + cohortname + "\" "
147 : "") +
148 " status=\"ok\">"
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700149 "<ping status=\"ok\"/><updatecheck status=\"ok\"" +
150 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" +
Sen Jiang2703ef42017-03-16 13:36:21 -0700151 codebase +
152 "\"/></urls>"
153 "<manifest version=\"" +
154 version +
155 "\">"
156 "<packages><package hash=\"not-used\" name=\"" +
157 filename + "\" size=\"" + base::Int64ToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800158 "\" hash_sha256=\"" + hash + "\"/>" +
159 (multi_package ? "<package name=\"package2\" size=\"222\" "
160 "hash_sha256=\"hash2\"/>"
161 : "") +
162 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700163 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700164 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
165 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700166 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700167 "IsDeltaPayload=\"true" +
168 (multi_package ? ":false" : "") +
169 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700170 "MaxDaysToScatter=\"" +
171 max_days_to_scatter +
172 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700173 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700174 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
175 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
176 : "") +
177 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700178 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700179 "/></actions></manifest></updatecheck></app>" +
180 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700181 ? "<app appid=\"" + app_id2 + "\"" +
182 (include_cohorts ? " cohort=\"cohort2\"" : "") +
183 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700184 codebase2 + "\"/></urls><manifest version=\"" + version2 +
185 "\"><packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700186 "<package name=\"package3\" size=\"333\" "
187 "hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700188 "<actions><action event=\"postinstall\" " +
189 (multi_app_self_update
190 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
191 : "IsDeltaPayload=\"false\" ") +
192 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700193 "</manifest></updatecheck></app>"
194 : "") +
195 (multi_app_no_update
196 ? "<app><updatecheck status=\"noupdate\"/></app>"
197 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700198 (multi_app_skip_updatecheck
199 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
200 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700201 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700202 }
203
204 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800205 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700206
Alex Deymo85616652015-10-15 18:48:31 -0700207 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700208 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700209 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
210 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700211 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700212 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700213 string more_info_url = "http://more/info";
214 string prompt = "true";
215 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700216 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700217 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700218 string hash = "4841534831323334";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800219 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700220 string deadline = "";
221 string max_days_to_scatter = "7";
222 string elapsed_days = "42";
223
224 // P2P setting defaults to allowed.
225 bool disable_p2p_for_downloading = false;
226 bool disable_p2p_for_sharing = false;
227
Sen Jiangfe284402018-03-21 14:03:50 -0700228 bool powerwash = false;
229
Alex Deymo8e18f932015-03-27 16:16:59 -0700230 // Omaha cohorts settings.
231 bool include_cohorts = false;
232 string cohort = "";
233 string cohorthint = "";
234 string cohortname = "";
235
236 // Whether to include the CrOS <!ENTITY> in the XML response.
237 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800238
Sen Jiang81259682017-03-30 15:11:30 -0700239 // Whether to include more than one app.
240 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700241 // Whether to include an app with noupdate="true".
242 bool multi_app_self_update = false;
243 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700244 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700245 // Whether to include an additional app with no updatecheck tag.
246 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700247 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800248 bool multi_package = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700249
250 // Whether the payload is a rollback.
251 bool rollback = false;
252 // The verified boot firmware key version for the rollback image.
253 string rollback_firmware_version = "";
254 // The verified boot kernel key version for the rollback image.
255 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700256 // The number of milestones back that the verified boot key version has been
257 // supplied.
258 uint32_t rollback_allowed_milestones = 0;
259 // The verified boot key version for the
260 // |current - rollback_allowed_milestones| most recent release.
261 // The pair contains <firmware_key_version, kernel_key_version> each
262 // of which is in the form "key_version.version".
263 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700264};
265
266} // namespace
267
Darin Petkov6a5b3222010-07-13 14:55:28 -0700268namespace chromeos_update_engine {
269
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700270class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
271 public:
272 OmahaRequestActionTestProcessorDelegate()
273 : expected_code_(ErrorCode::kSuccess),
274 interactive_(false),
275 test_http_fetcher_headers_(false) {}
276 ~OmahaRequestActionTestProcessorDelegate() override = default;
277
278 void ProcessingDone(const ActionProcessor* processor,
279 ErrorCode code) override {
280 brillo::MessageLoop::current()->BreakLoop();
281 }
282
283 void ActionCompleted(ActionProcessor* processor,
284 AbstractAction* action,
285 ErrorCode code) override {
286 // Make sure actions always succeed.
287 if (action->Type() == OmahaRequestAction::StaticType()) {
288 EXPECT_EQ(expected_code_, code);
289 // Check that the headers were set in the fetcher during the action. Note
290 // that we set this request as "interactive".
291 auto fetcher = static_cast<const MockHttpFetcher*>(
292 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
293
294 if (test_http_fetcher_headers_) {
295 EXPECT_EQ(interactive_ ? "fg" : "bg",
296 fetcher->GetHeader("X-Goog-Update-Interactivity"));
297 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
298 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
299 }
300 post_data_ = fetcher->post_data();
301 } else if (action->Type() ==
302 ObjectCollectorAction<OmahaResponse>::StaticType()) {
303 EXPECT_EQ(ErrorCode::kSuccess, code);
304 auto collector_action =
305 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
306 omaha_response_.reset(new OmahaResponse(collector_action->object()));
307 EXPECT_TRUE(omaha_response_);
308 } else {
309 EXPECT_EQ(ErrorCode::kSuccess, code);
310 }
311 }
312 ErrorCode expected_code_;
313 brillo::Blob post_data_;
314 bool interactive_;
315 bool test_http_fetcher_headers_;
316 std::unique_ptr<OmahaResponse> omaha_response_;
317};
318
Amin Hassani41ac04b2019-03-29 11:31:03 -0700319struct TestUpdateCheckParams {
320 string http_response;
321 int fail_http_response_code;
322 bool ping_only;
323 bool is_consumer_device;
324 int rollback_allowed_milestones;
325 bool is_policy_loaded;
326 ErrorCode expected_code;
327 metrics::CheckResult expected_check_result;
328 metrics::CheckReaction expected_check_reaction;
329 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700330 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700331};
332
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700333class OmahaRequestActionTest : public ::testing::Test {
334 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800335 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200336 request_params_.set_os_sp("service_pack");
337 request_params_.set_os_board("x86-generic");
338 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700339 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200340 request_params_.set_app_lang("en-US");
341 request_params_.set_current_channel("unittest");
342 request_params_.set_target_channel("unittest");
343 request_params_.set_hwid("OEM MODEL 09235 7471");
344 request_params_.set_fw_version("ChromeOSFirmware.1.0");
345 request_params_.set_ec_version("0X0A1");
346 request_params_.set_delta_okay(true);
347 request_params_.set_interactive(false);
348 request_params_.set_update_url("http://url");
349 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200350 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200351 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700352 request_params_.set_is_install(false);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800353 request_params_.set_dlc_module_ids({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200354
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700355 fake_system_state_.set_request_params(&request_params_);
356 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700357
358 // Setting the default update check params. Lookup |TestUpdateCheck()|.
359 tuc_params_ = {
360 .http_response = "",
361 .fail_http_response_code = -1,
362 .ping_only = false,
363 .is_consumer_device = true,
364 .rollback_allowed_milestones = 0,
365 .is_policy_loaded = false,
366 .expected_code = ErrorCode::kSuccess,
367 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
368 .expected_check_reaction = metrics::CheckReaction::kUpdating,
369 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
370 };
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700371 }
372
Amin Hassani41ac04b2019-03-29 11:31:03 -0700373 // This function uses the paramets in |tuc_params_| to do an update check. It
374 // will fill out |post_str| with the result data and |response| with
375 // |OmahaResponse|. Returns true iff an output response was obtained from the
376 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
377 // transfer will fail with that code. |ping_only| is passed through to the
378 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700379 //
380 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700381 // |expected_error_code| parameters are for checking expectations about
382 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
383 // statistics. Use the appropriate ::kUnset value to specify that the given
384 // metric should not be reported.
385 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700386
Amin Hassani41ac04b2019-03-29 11:31:03 -0700387 // Tests events using |event| and |https_response|. It will fill up |post_str|
388 // with the result data.
389 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700390
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800391 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700392 // only a ping or also an updatecheck.
393 void PingTest(bool ping_only);
394
395 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800396 bool InstallDateParseHelper(const string& elapsed_days,
397 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700398
399 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800400 void P2PTest(bool initial_allow_p2p_for_downloading,
401 bool initial_allow_p2p_for_sharing,
402 bool omaha_disable_p2p_for_downloading,
403 bool omaha_disable_p2p_for_sharing,
404 bool payload_state_allow_p2p_attempt,
405 bool expect_p2p_client_lookup,
406 const string& p2p_client_result_url,
407 bool expected_allow_p2p_for_downloading,
408 bool expected_allow_p2p_for_sharing,
409 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700410
411 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700412 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200413 // Used by all tests.
414 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700415
416 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700417
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700418 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700419
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700420 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700421
422 TestUpdateCheckParams tuc_params_;
423
424 // TODO(ahassani): Add trailing _ to these two variables.
425 OmahaResponse response;
426 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700427};
428
Amin Hassani41ac04b2019-03-29 11:31:03 -0700429bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700430 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700431 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700432 auto fetcher =
433 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
434 tuc_params_.http_response.size(),
435 nullptr);
436 if (tuc_params_.fail_http_response_code >= 0) {
437 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700438 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200439 // This ensures the tests didn't forget to update fake_system_state_ if they
440 // are not using the default request_params_.
441 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
442
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700443 auto omaha_request_action =
444 std::make_unique<OmahaRequestAction>(&fake_system_state_,
445 nullptr,
446 std::move(fetcher),
447 tuc_params_.ping_only,
448 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700449
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800450 auto mock_policy_provider =
451 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
452 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700453 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800454
455 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700456 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800457
458 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700459 const bool get_allowed_milestone_succeeds =
460 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800461 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700462 .WillRepeatedly(
463 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
464 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800465
466 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
467 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700468 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800469
Amin Hassani41ac04b2019-03-29 11:31:03 -0700470 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700471 delegate_.interactive_ = request_params_.interactive();
472 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700473 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700474 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700475
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700476 auto collector_action =
477 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
478 BondActions(omaha_request_action.get(), collector_action.get());
479 processor.EnqueueAction(std::move(omaha_request_action));
480 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700481
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700482 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
483 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800484 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700485
Amin Hassani41ac04b2019-03-29 11:31:03 -0700486 EXPECT_CALL(
487 *fake_system_state_.mock_metrics_reporter(),
488 ReportUpdateCheckMetrics(_,
489 tuc_params_.expected_check_result,
490 tuc_params_.expected_check_reaction,
491 tuc_params_.expected_download_error_code))
492 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800493
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700494 loop.PostTask(base::Bind(
495 [](ActionProcessor* processor) { processor->StartProcessing(); },
496 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700497 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700498 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700499 if (delegate_.omaha_response_)
500 response = *delegate_.omaha_response_;
501 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700502 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700503}
504
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700505// Tests Event requests -- they should always succeed. |out_post_data| may be
506// null; if non-null, the post-data received by the mock HttpFetcher is
507// returned.
508void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700509 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700510 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700511 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700512
513 auto action = std::make_unique<OmahaRequestAction>(
514 &fake_system_state_,
515 event,
516 std::make_unique<MockHttpFetcher>(
517 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700518 false,
519 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700520 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700521 processor.set_delegate(&delegate_);
522 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700523
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700524 loop.PostTask(base::Bind(
525 [](ActionProcessor* processor) { processor->StartProcessing(); },
526 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700527 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700528 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700529
Amin Hassani41ac04b2019-03-29 11:31:03 -0700530 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700531}
532
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700533TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700534 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700535 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
536 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
537 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
538 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
539
540 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400541 EXPECT_FALSE(response.update_exists);
542}
543
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700544TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700545 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
546 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
547 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
548
549 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700550 EXPECT_FALSE(response.update_exists);
551}
552
Sen Jiang81259682017-03-30 15:11:30 -0700553TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700554 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700555 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
556 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
557 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
558
559 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700560 EXPECT_FALSE(response.update_exists);
561}
562
Sen Jiang00adf7b2017-06-26 15:57:29 -0700563TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700564 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700565 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
566 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
567 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
568
569 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700570 EXPECT_FALSE(response.update_exists);
571}
572
573TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700574 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700575 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
576 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700577 "</manifest></updatecheck></app></response>";
578 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
579 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
580
581 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700582 EXPECT_FALSE(response.update_exists);
583}
584
Alex Deymo8e18f932015-03-27 16:16:59 -0700585// Test that all the values in the response are parsed in a normal update
586// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700587TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700588 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700589 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
590
591 ASSERT_TRUE(TestUpdateCheck());
592
Darin Petkov6a5b3222010-07-13 14:55:28 -0700593 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700594 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700595 EXPECT_EQ("", response.system_version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800596 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
597 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700598 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800599 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
600 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700601 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700602 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
603 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700604 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700605 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700606 // persisted.
607 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
608 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
609 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700610}
611
Sen Jiang0affc2c2017-02-10 15:55:05 -0800612TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800613 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700614 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
615
616 ASSERT_TRUE(TestUpdateCheck());
617
Sen Jiang0affc2c2017-02-10 15:55:05 -0800618 EXPECT_TRUE(response.update_exists);
619 EXPECT_EQ(fake_update_response_.version, response.version);
620 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
621 response.packages[0].payload_urls[0]);
622 EXPECT_EQ(fake_update_response_.codebase + "package2",
623 response.packages[1].payload_urls[0]);
624 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
625 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700626 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700627 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800628 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700629 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800630 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700631 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700632 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700633}
634
635TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700636 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700637 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
638
639 ASSERT_TRUE(TestUpdateCheck());
640
Sen Jiang81259682017-03-30 15:11:30 -0700641 EXPECT_TRUE(response.update_exists);
642 EXPECT_EQ(fake_update_response_.version, response.version);
643 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
644 response.packages[0].payload_urls[0]);
645 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
646 response.packages[1].payload_urls[0]);
647 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
648 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
649 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700650 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700651 ASSERT_EQ(2u, response.packages.size());
652 EXPECT_EQ(string("hash3"), response.packages[1].hash);
653 EXPECT_EQ(333u, response.packages[1].size);
654 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700655 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700656}
657
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700658TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) {
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700659 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700660 // Trigger the lining up of the app and system versions.
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700661 request_params_.set_system_app_id(fake_update_response_.app_id2);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700662 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700663
Amin Hassani41ac04b2019-03-29 11:31:03 -0700664 ASSERT_TRUE(TestUpdateCheck());
665
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700666 EXPECT_TRUE(response.update_exists);
667 EXPECT_EQ(fake_update_response_.version, response.version);
668 EXPECT_EQ(fake_update_response_.version2, response.system_version);
669 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
670 response.packages[0].payload_urls[0]);
671 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
672 response.packages[1].payload_urls[0]);
673 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
674 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
675 EXPECT_EQ(11u, response.packages[0].metadata_size);
676 EXPECT_EQ(true, response.packages[0].is_delta);
677 ASSERT_EQ(2u, response.packages.size());
678 EXPECT_EQ(string("hash3"), response.packages[1].hash);
679 EXPECT_EQ(333u, response.packages[1].size);
680 EXPECT_EQ(33u, response.packages[1].metadata_size);
681 EXPECT_EQ(false, response.packages[1].is_delta);
682}
683
Sen Jiang81259682017-03-30 15:11:30 -0700684TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700685 fake_update_response_.multi_app = true;
686 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700687 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
688
689 ASSERT_TRUE(TestUpdateCheck());
690
Sen Jiang81259682017-03-30 15:11:30 -0700691 EXPECT_TRUE(response.update_exists);
692 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700693 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700694 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
695 response.packages[0].payload_urls[0]);
696 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
697 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
698 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700699 ASSERT_EQ(2u, response.packages.size());
700 EXPECT_EQ(string("hash3"), response.packages[1].hash);
701 EXPECT_EQ(333u, response.packages[1].size);
702 EXPECT_EQ(33u, response.packages[1].metadata_size);
703 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700704}
705
706TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700707 fake_update_response_.multi_app = true;
708 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700709 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
710
711 ASSERT_TRUE(TestUpdateCheck());
712
Sen Jiang81259682017-03-30 15:11:30 -0700713 EXPECT_TRUE(response.update_exists);
714 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700715 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700716 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
717 response.packages[0].payload_urls[0]);
718 EXPECT_EQ(fake_update_response_.codebase + "package2",
719 response.packages[1].payload_urls[0]);
720 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
721 response.packages[2].payload_urls[0]);
722 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
723 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
724 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700725 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700726 ASSERT_EQ(3u, response.packages.size());
727 EXPECT_EQ(string("hash2"), response.packages[1].hash);
728 EXPECT_EQ(222u, response.packages[1].size);
729 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700730 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700731 EXPECT_EQ(string("hash3"), response.packages[2].hash);
732 EXPECT_EQ(333u, response.packages[2].size);
733 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700734 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800735}
736
Sen Jiangfe284402018-03-21 14:03:50 -0700737TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700738 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700739 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
740
741 ASSERT_TRUE(TestUpdateCheck());
742
Sen Jiangfe284402018-03-21 14:03:50 -0700743 EXPECT_TRUE(response.update_exists);
744 EXPECT_TRUE(response.powerwash_required);
745}
746
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700747TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700748 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700749 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700750 tuc_params_.http_response = "invalid xml>";
751 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
752 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
753 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
754
755 ASSERT_FALSE(TestUpdateCheck());
756
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700757 EXPECT_FALSE(response.update_exists);
758}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700759
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700760TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700761 request_params_.set_interactive(false);
762 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700763 tuc_params_.http_response = "invalid xml>";
764 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
765 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
766 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
767
768 ASSERT_FALSE(TestUpdateCheck());
769
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700770 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700771}
772
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700773TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700774 // Set up a connection manager that doesn't allow a valid update over
775 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700776 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700777 fake_system_state_.set_connection_manager(&mock_cm);
778
Alex Deymo30534502015-07-20 15:06:33 -0700779 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800780 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
781 SetArgPointee<1>(ConnectionTethering::kUnknown),
782 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700783 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
784 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700785
Amin Hassani41ac04b2019-03-29 11:31:03 -0700786 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
787 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
788 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
789
790 ASSERT_FALSE(TestUpdateCheck());
791
Chris Sosa77f79e82014-06-02 18:16:24 -0700792 EXPECT_FALSE(response.update_exists);
793}
794
Weidong Guo421ff332017-04-17 10:08:38 -0700795TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
796 // This test tests that update over cellular is allowed as device policy
797 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700798 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700799 fake_system_state_.set_connection_manager(&mock_cm);
800
801 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
802 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
803 SetArgPointee<1>(ConnectionTethering::kUnknown),
804 Return(true)));
805 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
806 .WillRepeatedly(Return(true));
807 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
808 .WillRepeatedly(Return(true));
809
Amin Hassani41ac04b2019-03-29 11:31:03 -0700810 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
811
812 ASSERT_TRUE(TestUpdateCheck());
813
Weidong Guo421ff332017-04-17 10:08:38 -0700814 EXPECT_TRUE(response.update_exists);
815}
816
817TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
818 // This test tests that update over cellular is blocked as device policy
819 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700820 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700821 fake_system_state_.set_connection_manager(&mock_cm);
822
823 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
824 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
825 SetArgPointee<1>(ConnectionTethering::kUnknown),
826 Return(true)));
827 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
828 .WillRepeatedly(Return(true));
829 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
830 .WillRepeatedly(Return(false));
831
Amin Hassani41ac04b2019-03-29 11:31:03 -0700832 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
833 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
834 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
835
836 ASSERT_FALSE(TestUpdateCheck());
837
Weidong Guo421ff332017-04-17 10:08:38 -0700838 EXPECT_FALSE(response.update_exists);
839}
840
841TEST_F(OmahaRequestActionTest,
842 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
843 // This test tests that, when device policy is not set, update over cellular
844 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700845 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700846 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
847 fake_system_state_.set_connection_manager(&mock_cm);
848
849 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
850 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
851 SetArgPointee<1>(ConnectionTethering::kUnknown),
852 Return(true)));
853 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
854 .WillRepeatedly(Return(false));
855 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
856 .WillRepeatedly(Return(true));
857
Amin Hassani41ac04b2019-03-29 11:31:03 -0700858 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
859
860 ASSERT_TRUE(TestUpdateCheck());
861
Weidong Guo421ff332017-04-17 10:08:38 -0700862 EXPECT_TRUE(response.update_exists);
863}
864
865TEST_F(OmahaRequestActionTest,
866 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
867 // This test tests that, when device policy is not set and permission for
868 // update over cellular is set to false or does not exist, update over
869 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700870 MockConnectionManager mock_cm;
871 // A version different from the version in omaha response.
872 string diff_version = "99.99.99";
873 // A size different from the size in omaha response.
874 int64_t diff_size = 999;
875
876 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
877 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
878 // This test tests cellular (3G) being the only connection type being allowed.
879 fake_system_state_.set_connection_manager(&mock_cm);
880
881 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
882 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
883 SetArgPointee<1>(ConnectionTethering::kUnknown),
884 Return(true)));
885 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
886 .WillRepeatedly(Return(false));
887 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
888 .WillRepeatedly(Return(true));
889
Amin Hassani41ac04b2019-03-29 11:31:03 -0700890 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
891 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
892 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
893
894 ASSERT_FALSE(TestUpdateCheck());
895
Weidong Guo421ff332017-04-17 10:08:38 -0700896 EXPECT_FALSE(response.update_exists);
897}
898
899TEST_F(OmahaRequestActionTest,
900 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
901 // This test tests that, when device policy is not set and permission for
902 // update over cellular is set to false or does not exist, update over
903 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700904 MockConnectionManager mock_cm;
905 // A version same as the version in omaha response.
906 string new_version = fake_update_response_.version;
907 // A size same as the size in omaha response.
908 int64_t new_size = fake_update_response_.size;
909
910 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
911 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
912 fake_system_state_.set_connection_manager(&mock_cm);
913
914 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
915 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
916 SetArgPointee<1>(ConnectionTethering::kUnknown),
917 Return(true)));
918 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
919 .WillRepeatedly(Return(false));
920 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
921 .WillRepeatedly(Return(true));
922
Amin Hassani41ac04b2019-03-29 11:31:03 -0700923 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
924
925 ASSERT_TRUE(TestUpdateCheck());
926
Weidong Guo421ff332017-04-17 10:08:38 -0700927 EXPECT_TRUE(response.update_exists);
928}
929
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700930TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700931 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700932 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700933 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700934 fake_update_response_.version = rollback_version;
935 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
936 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
937 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700938
Chris Sosa77f79e82014-06-02 18:16:24 -0700939 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800940 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700941
Amin Hassani41ac04b2019-03-29 11:31:03 -0700942 ASSERT_FALSE(TestUpdateCheck());
943
Chris Sosa77f79e82014-06-02 18:16:24 -0700944 EXPECT_FALSE(response.update_exists);
945}
946
Marton Hunyadyc2882062018-05-14 17:28:25 +0200947// Verify that update checks called during OOBE will not try to download an
948// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700949TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200950 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700951 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
952 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
953 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
954 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -0700955
Sen Jiang8cd42342018-01-31 12:06:59 -0800956 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700957 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700958 ASSERT_FALSE(TestUpdateCheck());
959
Kevin Cernekee2494e282016-03-29 18:03:53 -0700960 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +0200961}
Kevin Cernekee2494e282016-03-29 18:03:53 -0700962
Marton Hunyadyc2882062018-05-14 17:28:25 +0200963// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
964// enabled.
965TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200966 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700967 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700968 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700969
Amin Hassani41ac04b2019-03-29 11:31:03 -0700970 ASSERT_TRUE(TestUpdateCheck());
971
Kevin Cernekee2494e282016-03-29 18:03:53 -0700972 EXPECT_TRUE(response.update_exists);
973}
974
Marton Hunyadyc2882062018-05-14 17:28:25 +0200975// Verify that update checks called during OOBE will still try to download an
976// update if the response includes the deadline field.
977TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200978 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
979 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700980 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +0200981
Amin Hassani41ac04b2019-03-29 11:31:03 -0700982 ASSERT_TRUE(TestUpdateCheck());
983
Marton Hunyadyc2882062018-05-14 17:28:25 +0200984 EXPECT_TRUE(response.update_exists);
985}
986
987// Verify that update checks called during OOBE will not try to download an
988// update if a rollback happened, even when the response includes the deadline
989// field.
990TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200991 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
992 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700993 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
994 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
995 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
996 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
997
Marton Hunyadyc2882062018-05-14 17:28:25 +0200998 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
999 .WillOnce(Return(true));
1000
Amin Hassani41ac04b2019-03-29 11:31:03 -07001001 ASSERT_FALSE(TestUpdateCheck());
1002
Marton Hunyadyc2882062018-05-14 17:28:25 +02001003 EXPECT_FALSE(response.update_exists);
1004}
1005
Toni Barzic61544e62018-10-11 14:37:30 -07001006// Verify that non-critical updates are skipped by reporting the
1007// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1008// i.e. when the update would need user permission. Note that reporting
1009// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1010// in OOBE (warning the user about an update that will be skipped).
1011TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001012 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1013
1014 MockConnectionManager mock_cm;
1015 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001016 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1017 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1018 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1019 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001020
1021 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1022 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1023 SetArgPointee<1>(ConnectionTethering::kUnknown),
1024 Return(true)));
1025 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1026 .WillRepeatedly(Return(false));
1027
Amin Hassani41ac04b2019-03-29 11:31:03 -07001028 ASSERT_FALSE(TestUpdateCheck());
1029
Toni Barzic61544e62018-10-11 14:37:30 -07001030 EXPECT_FALSE(response.update_exists);
1031}
1032
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001033TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001034 request_params_.set_wall_clock_based_wait_enabled(true);
1035 request_params_.set_update_check_count_wait_enabled(false);
1036 request_params_.set_waiting_period(TimeDelta::FromDays(2));
May Lippert60aa3ca2018-08-15 16:55:29 -07001037 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001038 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1039 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1040 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001041
Amin Hassani41ac04b2019-03-29 11:31:03 -07001042 ASSERT_FALSE(TestUpdateCheck());
1043
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001044 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001045}
1046
1047TEST_F(OmahaRequestActionTest,
1048 WallClockBasedWaitAloneCausesScatteringInteractive) {
1049 request_params_.set_wall_clock_based_wait_enabled(true);
1050 request_params_.set_update_check_count_wait_enabled(false);
1051 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1052 request_params_.set_interactive(true);
1053 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1054 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001055
1056 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001057 ASSERT_TRUE(TestUpdateCheck());
1058
Chris Sosa968d0572013-08-23 14:46:02 -07001059 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001060}
1061
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001062TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001063 request_params_.set_wall_clock_based_wait_enabled(false);
1064 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1065 request_params_.set_update_check_count_wait_enabled(true);
1066 request_params_.set_min_update_checks_needed(1);
1067 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001068 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001069
Amin Hassani41ac04b2019-03-29 11:31:03 -07001070 ASSERT_TRUE(TestUpdateCheck());
1071
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001072 EXPECT_TRUE(response.update_exists);
1073}
1074
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001075TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001076 request_params_.set_wall_clock_based_wait_enabled(true);
1077 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1078 request_params_.set_update_check_count_wait_enabled(true);
1079 request_params_.set_min_update_checks_needed(1);
1080 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001081 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001082 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1083
1084 ASSERT_TRUE(TestUpdateCheck());
1085
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001086 EXPECT_TRUE(response.update_exists);
1087}
1088
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001089TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001090 request_params_.set_wall_clock_based_wait_enabled(true);
1091 request_params_.set_waiting_period(TimeDelta());
1092 request_params_.set_update_check_count_wait_enabled(true);
1093 request_params_.set_min_update_checks_needed(0);
1094 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001095 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001096 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001097
Amin Hassani41ac04b2019-03-29 11:31:03 -07001098 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001099
Ben Chan9abb7632014-08-07 00:10:53 -07001100 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001101 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001102 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001103 EXPECT_TRUE(response.update_exists);
1104}
1105
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001106TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001107 request_params_.set_wall_clock_based_wait_enabled(true);
1108 request_params_.set_waiting_period(TimeDelta());
1109 request_params_.set_update_check_count_wait_enabled(true);
1110 request_params_.set_min_update_checks_needed(1);
1111 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001112 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001113 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1114 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1115 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001116
Amin Hassani41ac04b2019-03-29 11:31:03 -07001117 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001118
Ben Chan9abb7632014-08-07 00:10:53 -07001119 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001120 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001121 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001122 EXPECT_FALSE(response.update_exists);
1123}
1124
Amin Hassani41ac04b2019-03-29 11:31:03 -07001125TEST_F(OmahaRequestActionTest,
1126 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001127 request_params_.set_wall_clock_based_wait_enabled(true);
1128 request_params_.set_waiting_period(TimeDelta());
1129 request_params_.set_update_check_count_wait_enabled(true);
1130 request_params_.set_min_update_checks_needed(1);
1131 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001132 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001133 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001134 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1135
1136 // Verify if we are interactive check we don't defer.
1137 ASSERT_TRUE(TestUpdateCheck());
1138
1139 EXPECT_TRUE(response.update_exists);
1140}
1141
1142TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1143 request_params_.set_wall_clock_based_wait_enabled(true);
1144 request_params_.set_waiting_period(TimeDelta());
1145 request_params_.set_update_check_count_wait_enabled(true);
1146 request_params_.set_min_update_checks_needed(1);
1147 request_params_.set_max_update_checks_allowed(8);
1148 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1149 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1150 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1151 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001152
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001153 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001154 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001155
Ben Chan9abb7632014-08-07 00:10:53 -07001156 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001157 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001158 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001159 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001160 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001161 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001162}
1163
1164TEST_F(OmahaRequestActionTest,
1165 ExistingUpdateCheckCountCausesScatteringInteractive) {
1166 request_params_.set_wall_clock_based_wait_enabled(true);
1167 request_params_.set_waiting_period(TimeDelta());
1168 request_params_.set_update_check_count_wait_enabled(true);
1169 request_params_.set_min_update_checks_needed(1);
1170 request_params_.set_max_update_checks_allowed(8);
1171 request_params_.set_interactive(true);
1172 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1173 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1174
1175 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001176
1177 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001178 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001179 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001180}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001181
Adolfo Victoria497044c2018-07-18 07:51:42 -07001182TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1183 // If staging is on, the value for max days to scatter should be ignored, and
1184 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001185 request_params_.set_wall_clock_based_wait_enabled(true);
1186 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1187 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001188 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1189
Adolfo Victoria497044c2018-07-18 07:51:42 -07001190 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1191 // This should not prevent scattering due to staging.
1192 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001193 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1194 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1195 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1196
1197 ASSERT_FALSE(TestUpdateCheck());
1198
Adolfo Victoria497044c2018-07-18 07:51:42 -07001199 EXPECT_FALSE(response.update_exists);
1200
1201 // Interactive updates should not be affected.
1202 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001203 tuc_params_.expected_code = ErrorCode::kSuccess;
1204 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1205
1206 ASSERT_TRUE(TestUpdateCheck());
1207
Adolfo Victoria497044c2018-07-18 07:51:42 -07001208 EXPECT_TRUE(response.update_exists);
1209}
1210
Alex Deymo8e18f932015-03-27 16:16:59 -07001211TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001212 fake_update_response_.include_cohorts = true;
1213 fake_update_response_.cohort = "s/154454/8479665";
1214 fake_update_response_.cohorthint = "please-put-me-on-beta";
1215 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001216 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001217
Amin Hassani41ac04b2019-03-29 11:31:03 -07001218 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001219
1220 string value;
1221 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1222 EXPECT_EQ(fake_update_response_.cohort, value);
1223
1224 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1225 EXPECT_EQ(fake_update_response_.cohorthint, value);
1226
1227 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1228 EXPECT_EQ(fake_update_response_.cohortname, value);
1229}
1230
1231TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001232 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1233 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1234 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1235 fake_update_response_.include_cohorts = true;
1236 fake_update_response_.cohort = "s/154454/8479665";
1237 fake_update_response_.cohorthint = "please-put-me-on-beta";
1238 fake_update_response_.cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001239 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001240
Amin Hassani41ac04b2019-03-29 11:31:03 -07001241 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001242
1243 string value;
1244 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1245 EXPECT_EQ(fake_update_response_.cohort, value);
1246
1247 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1248 EXPECT_EQ(fake_update_response_.cohorthint, value);
1249
1250 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1251}
1252
1253TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001254 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001255
Amin Hassani41ac04b2019-03-29 11:31:03 -07001256 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1257 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001258
1259 string value;
1260 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1261 EXPECT_EQ("old_value", value);
1262
1263 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1264 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1265}
1266
Alex Deymo00d79ac2015-06-29 15:41:49 -07001267TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001268 fake_update_response_.include_cohorts = true;
1269 fake_update_response_.cohort = "s/154454/8479665";
1270 fake_update_response_.cohorthint = "please-put-me-on-beta";
1271 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001272 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1273 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1274 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001275
Amin Hassani41ac04b2019-03-29 11:31:03 -07001276 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001277
1278 string value;
1279 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1280 EXPECT_EQ(fake_update_response_.cohort, value);
1281
1282 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1283 EXPECT_EQ(fake_update_response_.cohorthint, value);
1284
1285 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1286 EXPECT_EQ(fake_update_response_.cohortname, value);
1287}
1288
Sen Jiangb1e063a2017-09-15 17:44:31 -07001289TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001290 fake_update_response_.multi_app = true;
1291 fake_update_response_.include_cohorts = true;
1292 fake_update_response_.cohort = "s/154454/8479665";
1293 fake_update_response_.cohorthint = "please-put-me-on-beta";
1294 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001295 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001296
Amin Hassani41ac04b2019-03-29 11:31:03 -07001297 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001298
1299 string value;
1300 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1301 EXPECT_EQ(fake_update_response_.cohort, value);
1302
1303 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1304 EXPECT_EQ(fake_update_response_.cohorthint, value);
1305
1306 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1307 EXPECT_EQ(fake_update_response_.cohortname, value);
1308}
1309
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001310TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001311 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001312 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001313 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001314
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001315 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001316 &fake_system_state_,
1317 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001318 std::make_unique<MockHttpFetcher>(
1319 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001320 false,
1321 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001322 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001323 processor.set_delegate(&delegate_);
1324 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001325
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001326 loop.PostTask(base::Bind(
1327 [](ActionProcessor* processor) { processor->StartProcessing(); },
1328 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001329 loop.Run();
1330 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001331 EXPECT_FALSE(processor.IsRunning());
1332}
1333
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001334TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001335 tuc_params_.http_response = "invalid xml>";
1336 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1337 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1338 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1339
1340 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001341 EXPECT_FALSE(response.update_exists);
1342}
1343
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001344TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001345 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1346 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1347 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1348
1349 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001350 EXPECT_FALSE(response.update_exists);
1351}
1352
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001353TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001354 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001355 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1356 "<daystart elapsed_seconds=\"100\"/>"
1357 "<app appid=\"foo\" status=\"ok\">"
1358 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001359 "<updatecheck/></app></response>";
1360 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1361 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1362 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1363
1364 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001365 EXPECT_FALSE(response.update_exists);
1366}
1367
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001368TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001369 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001370 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1371 "<daystart elapsed_seconds=\"100\"/>"
1372 "<app appid=\"foo\" status=\"ok\">"
1373 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001374 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1375 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1376 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1377 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1378
1379 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001380 EXPECT_FALSE(response.update_exists);
1381}
1382
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001383TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001384 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001385 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1386 "<daystart elapsed_seconds=\"100\"/>"
1387 "<app appid=\"foo\" status=\"ok\">"
1388 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001389 "</app></response>";
1390 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1391 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1392 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1393
1394 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001395 EXPECT_FALSE(response.update_exists);
1396}
1397
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001398TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001399 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001400 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1401 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001402 // the appid needs to match that in the request params
1403 "<app appid=\"" +
1404 fake_update_response_.app_id +
1405 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001406 "<updatecheck status=\"ok\">"
1407 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001408 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001409 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001410 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001411 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001412 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001413 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001414 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001415 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001416
Amin Hassani41ac04b2019-03-29 11:31:03 -07001417 ASSERT_TRUE(TestUpdateCheck());
1418
Darin Petkov6a5b3222010-07-13 14:55:28 -07001419 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001420 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001421 EXPECT_EQ("http://missing/field/test/f",
1422 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001423 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001424 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1425 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001426 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001427 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001428}
1429
1430namespace {
1431class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1432 public:
1433 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001434 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001435 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001436};
1437
Alex Deymo60ca1a72015-06-18 18:19:15 -07001438void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001439 processor->StartProcessing();
1440 CHECK(processor->IsRunning());
1441 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001442}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001443} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001444
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001445TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001446 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001447 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001448
Alex Deymo60ca1a72015-06-18 18:19:15 -07001449 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001450 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001451 &fake_system_state_,
1452 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001453 std::make_unique<MockHttpFetcher>(
1454 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001455 false,
1456 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001457 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001458 ActionProcessor processor;
1459 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001460 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001461
Alex Deymo60ca1a72015-06-18 18:19:15 -07001462 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1463 loop.Run();
1464 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001465}
1466
Alex Deymob0d74eb2015-03-30 17:59:17 -07001467TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001468 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001469 request_params_.set_os_sp("testtheservice_pack>");
1470 request_params_.set_os_board("x86 generic<id");
1471 request_params_.set_current_channel("unittest_track&lt;");
1472 request_params_.set_target_channel("unittest_track&lt;");
1473 request_params_.set_hwid("<OEM MODEL>");
Askar Aitzhan570ca872019-04-24 11:16:12 +02001474 request_params_.set_autoupdate_token("autoupdate_token>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001475 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1476 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001477 fake_prefs_.SetString(
1478 kPrefsOmahaCohortName,
1479 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001480 tuc_params_.http_response = "invalid xml>";
1481 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1482 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1483 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1484
1485 ASSERT_FALSE(TestUpdateCheck());
1486
Alex Deymo8e18f932015-03-27 16:16:59 -07001487 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1488 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1489 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1490 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1491 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1492 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1493 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1494 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1495 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1496 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1497 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1498 // Values from Prefs that are too big are removed from the XML instead of
1499 // encoded.
1500 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001501 EXPECT_NE(string::npos, post_str.find("autoupdate_token&gt;"));
1502 EXPECT_EQ(string::npos, post_str.find("autoupdate_token>"));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001503}
1504
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001505TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001506 fake_update_response_.deadline = "&lt;20110101";
1507 fake_update_response_.more_info_url = "testthe&lt;url";
1508 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001509 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1510
1511 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001512
Sen Jiang0affc2c2017-02-10 15:55:05 -08001513 EXPECT_EQ("testthe<url", response.more_info_url);
1514 EXPECT_EQ("testthe&codebase/file.signed",
1515 response.packages[0].payload_urls[0]);
1516 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001517}
1518
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001519TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001520 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001521 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001522 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001523
Amin Hassani41ac04b2019-03-29 11:31:03 -07001524 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001525 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001526}
1527
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001528TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001529 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001530 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001531 tuc_params_.http_response = "invalid xml>";
1532 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1533 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1534 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001535
Darin Petkov95508da2011-01-05 12:42:29 -08001536 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001537 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001538 // An existing but empty previous version means that we didn't reboot to a new
1539 // update, therefore, no need to update the previous version.
1540 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001541 ASSERT_FALSE(TestUpdateCheck());
1542
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001543 EXPECT_NE(
1544 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1545 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001546 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001547 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1548 string::npos);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001549 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos);
1550 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001551 // No <event> tag should be sent if we didn't reboot to an update.
1552 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001553}
1554
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001555TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001556 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001557 "invalid xml>");
1558
Alex Vakulenko75039d72014-03-25 12:36:28 -07001559 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001560 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001561 OmahaEvent::kTypeUpdateDownloadStarted,
1562 OmahaEvent::kResultSuccess);
1563 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001564 EXPECT_EQ(post_str.find("ping"), string::npos);
1565 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001566}
1567
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001568TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001569 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001570 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001571 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001572 "invalid xml>");
1573
Alex Vakulenko75039d72014-03-25 12:36:28 -07001574 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001575 " <event eventtype=\"%d\" eventresult=\"%d\" "
1576 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001577 OmahaEvent::kTypeDownloadComplete,
1578 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001579 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001580 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001581 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001582}
1583
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001584TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001585 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001586 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001587 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001588 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001589 std::make_unique<MockHttpFetcher>(
1590 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001591 false,
1592 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001593 EXPECT_FALSE(update_check_action.IsEvent());
1594
1595 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001596 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001597 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001598 std::make_unique<MockHttpFetcher>(
1599 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001600 false,
1601 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001602 EXPECT_TRUE(event_action.IsEvent());
1603}
1604
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001605TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001606 tuc_params_.http_response = "invalid xml>";
1607 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1608 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1609 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1610
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001611 for (int i = 0; i < 2; i++) {
1612 bool delta_okay = i == 1;
1613 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001614 request_params_.set_delta_okay(delta_okay);
1615
Amin Hassani41ac04b2019-03-29 11:31:03 -07001616 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001617 EXPECT_NE(
1618 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1619 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001620 << "i = " << i;
1621 }
1622}
1623
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001624TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001625 tuc_params_.http_response = "invalid xml>";
1626 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1627 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1628 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1629
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001630 for (int i = 0; i < 2; i++) {
1631 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001632 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001633 request_params_.set_interactive(interactive);
1634
Amin Hassani41ac04b2019-03-29 11:31:03 -07001635 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001636 EXPECT_NE(post_str.find(
1637 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001638 string::npos)
1639 << "i = " << i;
1640 }
1641}
1642
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001643TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001644 tuc_params_.http_response = "invalid xml>";
1645 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1646 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1647 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1648
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001649 for (int i = 0; i < 2; i++) {
1650 bool target_version_set = i == 1;
1651 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001652 request_params_.set_target_version_prefix(target_version_prefix);
1653
Amin Hassani41ac04b2019-03-29 11:31:03 -07001654 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001655 if (target_version_set) {
1656 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1657 string::npos)
1658 << "i = " << i;
1659 } else {
1660 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1661 << "i = " << i;
1662 }
1663 }
1664}
1665
1666TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001667 tuc_params_.http_response = "invalid xml>";
1668 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1669 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1670 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1671
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001672 for (int i = 0; i < 4; i++) {
1673 bool rollback_allowed = i / 2 == 0;
1674 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001675 request_params_.set_target_version_prefix(target_version_set ? "10032."
1676 : "");
1677 request_params_.set_rollback_allowed(rollback_allowed);
1678
Amin Hassani41ac04b2019-03-29 11:31:03 -07001679 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001680 if (rollback_allowed && target_version_set) {
1681 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1682 << "i = " << i;
1683 } else {
1684 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1685 }
1686 }
1687}
1688
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001689TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001690 OmahaEvent default_event;
1691 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1692 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001693 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001694
1695 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1696 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1697 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001698 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001699
1700 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1701 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001702 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001703 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1704 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001705 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001706}
1707
Askar Aitzhan570ca872019-04-24 11:16:12 +02001708TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
1709 constexpr char autoupdate_token[] = "autoupdate_token";
1710
1711 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1712 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1713 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1714 request_params_.set_autoupdate_token(autoupdate_token);
1715
1716 ASSERT_TRUE(TestUpdateCheck());
1717
1718 EXPECT_NE(post_str.find(" <updatecheck token=\"" +
1719 string(autoupdate_token) + "\"></updatecheck>\n"),
1720 string::npos);
1721}
1722
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001723void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001724 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001725 fake_system_state_.set_prefs(&prefs);
1726 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001727 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001728 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1729 // Add a few hours to the day difference to test no rounding, etc.
1730 int64_t five_days_ago =
1731 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1732 int64_t six_days_ago =
1733 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1734 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001735 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001736 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001737 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001738 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001739 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001740
1741 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1742 tuc_params_.ping_only = ping_only;
1743 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1744 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1745
1746 ASSERT_TRUE(TestUpdateCheck());
1747
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001748 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1749 string::npos);
1750 if (ping_only) {
1751 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1752 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1753 } else {
1754 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1755 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001756 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001757}
1758
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001759TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001760 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001761}
1762
1763TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001764 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001765}
1766
1767TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001768 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001769 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001770 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001771 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001772 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001773 int64_t three_days_ago =
1774 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1775 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001776 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001777 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001778 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001779 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001780 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001781 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001782
1783 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1784 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1785 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1786
1787 ASSERT_TRUE(TestUpdateCheck());
1788
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001789 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001790}
1791
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001792TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001793 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001794 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001795 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001796 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001797 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001798 int64_t four_days_ago =
1799 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1800 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001801 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001802 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001803 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001804 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001805 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001806 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001807
1808 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1809 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1810 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1811
1812 ASSERT_TRUE(TestUpdateCheck());
1813
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001814 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001815 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001816}
1817
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001818TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001819 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001820 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001821 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001822 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001823 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001824 int64_t one_hour_ago =
1825 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001826 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001827 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001828 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001829 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001830 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001831 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001832 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1833 // send a ping.
1834 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1835 .WillOnce(Return(true));
1836 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1837 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001838
1839 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1840 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1841 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1842
1843 ASSERT_TRUE(TestUpdateCheck());
1844
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001845 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001846}
1847
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001848TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001849 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001850 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001851 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001852 int64_t now = Time::Now().ToInternalValue();
1853 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001854 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001855 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001856 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001857 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1858 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001859
1860 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1861 tuc_params_.ping_only = true;
1862 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
1863 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1864
1865 EXPECT_TRUE(TestUpdateCheck());
1866 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07001867}
1868
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001869TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
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);
David Zeuthen33bae492014-02-25 16:16:18 -08001872 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001873 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001874 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001875 int64_t future =
1876 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001877 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001878 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001879 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001880 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001881 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001882 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001883 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1884 .WillOnce(Return(true));
1885 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1886 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001887
1888 tuc_params_.http_response =
1889 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1890 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
1891 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1892 "<updatecheck status=\"noupdate\"/></app></response>";
1893 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1894 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1895
1896 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001897 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001898}
1899
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001900TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001901 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001902 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001903 // may fail if it runs for longer than 5 seconds. It shouldn't run
1904 // that long though.
1905 int64_t midnight =
1906 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1907 int64_t midnight_slack =
1908 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001909 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001910 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001911 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1912 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001913 EXPECT_CALL(prefs,
1914 SetInt64(kPrefsLastActivePingDay,
1915 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001916 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001917 EXPECT_CALL(prefs,
1918 SetInt64(kPrefsLastRollCallPingDay,
1919 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001920 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001921
1922 tuc_params_.http_response =
1923 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1924 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
1925 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1926 "<updatecheck status=\"noupdate\"/></app></response>";
1927 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1928 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1929
1930 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001931}
1932
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001933TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001934 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001935 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001936 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1937 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001938 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1939 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001940
1941 tuc_params_.http_response =
1942 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1943 "protocol=\"3.0\"><daystart blah=\"200\"/>"
1944 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1945 "<updatecheck status=\"noupdate\"/></app></response>";
1946 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1947 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1948
1949 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001950}
1951
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001952TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001953 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001954 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001955 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1956 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001957 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1958 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001959
1960 tuc_params_.http_response =
1961 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1962 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
1963 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1964 "<updatecheck status=\"noupdate\"/></app></response>";
1965 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1966 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1967
1968 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001969}
1970
Alex Deymob3fa53b2016-04-18 19:57:58 -07001971TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1972 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1973 // the "eol" attribute.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001974 tuc_params_.http_response =
1975 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1976 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1977 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1978 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/></app></response>";
1979 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1980 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1981
1982 ASSERT_TRUE(TestUpdateCheck());
1983
Alex Deymob3fa53b2016-04-18 19:57:58 -07001984 string eol_pref;
1985 EXPECT_TRUE(
1986 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1987 // Note that the eol="eol" attribute should be ignored and the _eol should be
1988 // used instead.
1989 EXPECT_EQ("security-only", eol_pref);
1990}
1991
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001992TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001993 tuc_params_.http_response = "invalid xml>";
1994 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1995 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1996 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1997
1998 ASSERT_FALSE(TestUpdateCheck());
1999
Darin Petkov84c763c2010-07-29 16:27:58 -07002000 EXPECT_EQ(post_str.find("machineid="), string::npos);
2001 EXPECT_EQ(post_str.find("userid="), string::npos);
2002}
2003
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002004TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002005 const int http_error_code =
2006 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002007 tuc_params_.fail_http_response_code = 501;
2008 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2009 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2010 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2011 tuc_params_.expected_download_error_code =
2012 static_cast<metrics::DownloadErrorCode>(501);
2013
2014 ASSERT_FALSE(TestUpdateCheck());
2015
Darin Petkovedc522e2010-11-05 09:35:17 -07002016 EXPECT_FALSE(response.update_exists);
2017}
2018
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002019TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002020 const int http_error_code =
2021 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002022
2023 tuc_params_.fail_http_response_code = 1500;
2024 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2025 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2026 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2027 tuc_params_.expected_download_error_code =
2028 metrics::DownloadErrorCode::kHttpStatusOther;
2029
2030 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002031 EXPECT_FALSE(response.update_exists);
2032}
2033
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002034TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002035 request_params_.set_wall_clock_based_wait_enabled(true);
2036 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2037 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002038
Sen Jiang7c1171e2016-06-23 11:35:40 -07002039 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002040 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002041 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002042
2043 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2044 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2045 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2046
2047 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002048
Ben Chan9abb7632014-08-07 00:10:53 -07002049 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002050 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002051 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002052 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002053
2054 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002055 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002056 tuc_params_.expected_code = ErrorCode::kSuccess;
2057 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2058
2059 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002060 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002061}
2062
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002063TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002064 request_params_.set_wall_clock_based_wait_enabled(true);
2065 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2066 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002067
Sen Jiang7c1171e2016-06-23 11:35:40 -07002068 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002069 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2070 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002071 ASSERT_TRUE(
2072 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2073 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002074
Amin Hassani41ac04b2019-03-29 11:31:03 -07002075 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2076
2077 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002078 EXPECT_TRUE(response.update_exists);
2079
2080 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002081 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002082 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002083 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2084}
2085
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002086TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002087 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002088 base::ScopedTempDir tempdir;
2089 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002090
Marton Hunyady2abda312018-04-24 18:21:49 +02002091 request_params_.set_root(tempdir.GetPath().value());
2092 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2093 request_params_.set_app_version("1.2.3.4");
2094 request_params_.set_product_components("o.bundle=1");
2095 request_params_.set_current_channel("canary-channel");
2096 EXPECT_TRUE(
2097 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2098 request_params_.UpdateDownloadChannel();
2099 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002100
2101 tuc_params_.http_response = "invalid xml>";
2102 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2103 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2104 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2105
2106 ASSERT_FALSE(TestUpdateCheck());
2107
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002108 EXPECT_NE(
2109 string::npos,
2110 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2111 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2112 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002113 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002114}
2115
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002116TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002117 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002118 base::ScopedTempDir tempdir;
2119 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002120
Marton Hunyady2abda312018-04-24 18:21:49 +02002121 request_params_.set_root(tempdir.GetPath().value());
2122 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2123 request_params_.set_app_version("5.6.7.8");
2124 request_params_.set_product_components("o.bundle=1");
2125 request_params_.set_current_channel("stable-channel");
2126 EXPECT_TRUE(
2127 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2128 request_params_.UpdateDownloadChannel();
2129 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002130
2131 tuc_params_.http_response = "invalid xml>";
2132 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2133 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2134 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2135
2136 ASSERT_FALSE(TestUpdateCheck());
2137
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002138 EXPECT_NE(
2139 string::npos,
2140 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2141 "version=\"5.6.7.8\" "
2142 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002143 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002144 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002145}
2146
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002147// Checks that the initial ping with a=-1 r=-1 is not send when the device
2148// was powerwashed.
2149TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2150 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2151
2152 // Flag that the device was powerwashed in the past.
2153 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002154 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2155 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2156 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002157
Amin Hassani41ac04b2019-03-29 11:31:03 -07002158 ASSERT_TRUE(TestUpdateCheck());
2159
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002160 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002161 EXPECT_EQ(string::npos, post_str.find("<ping"));
2162}
2163
Amin Hassani1677e812017-06-21 13:36:36 -07002164// Checks that the initial ping with a=-1 r=-1 is not send when the device
2165// first_active_omaha_ping_sent is set.
2166TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2167 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2168
2169 // Flag that the device was not powerwashed in the past.
2170 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2171
2172 // Flag that the device has sent first active ping in the past.
2173 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2174
Amin Hassani41ac04b2019-03-29 11:31:03 -07002175 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2176 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2177 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2178
2179 ASSERT_TRUE(TestUpdateCheck());
2180
Amin Hassani1677e812017-06-21 13:36:36 -07002181 // We shouldn't send a ping in this case since
2182 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002183 EXPECT_EQ(string::npos, post_str.find("<ping"));
2184}
2185
Alex Deymo9fded1e2015-11-05 12:31:19 -08002186// Checks that the event 54 is sent on a reboot to a new update.
2187TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2188 // Flag that the device was updated in a previous boot.
2189 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2190
Amin Hassani41ac04b2019-03-29 11:31:03 -07002191 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2192 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2193 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2194
2195 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002196
2197 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002198 EXPECT_NE(
2199 string::npos,
2200 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2201 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002202 EXPECT_NE(string::npos,
2203 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2204
2205 // The previous version flag should have been removed.
2206 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2207 string prev_version;
2208 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2209 EXPECT_TRUE(prev_version.empty());
2210}
2211
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002212void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2213 bool initial_allow_p2p_for_sharing,
2214 bool omaha_disable_p2p_for_downloading,
2215 bool omaha_disable_p2p_for_sharing,
2216 bool payload_state_allow_p2p_attempt,
2217 bool expect_p2p_client_lookup,
2218 const string& p2p_client_result_url,
2219 bool expected_allow_p2p_for_downloading,
2220 bool expected_allow_p2p_for_sharing,
2221 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002222 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2223 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2224 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002225
2226 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002227 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002228 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2229 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002230 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2231 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2232 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2233 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2234 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2235 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2236 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2237 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2238 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2239 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2240
David Zeuthen8f191b22013-08-06 12:27:50 -07002241 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002242 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002243 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2244
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002245 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2246 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002247 .Times(expect_p2p_client_lookup ? 1 : 0);
2248
Alex Deymo8e18f932015-03-27 16:16:59 -07002249 fake_update_response_.disable_p2p_for_downloading =
2250 omaha_disable_p2p_for_downloading;
2251 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002252
2253 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2254 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2255
2256 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002257 EXPECT_TRUE(response.update_exists);
2258
Gilad Arnold74b5f552014-10-07 08:17:16 -07002259 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2260 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002261 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002262
Gilad Arnold74b5f552014-10-07 08:17:16 -07002263 EXPECT_EQ(expected_allow_p2p_for_downloading,
2264 actual_allow_p2p_for_downloading);
2265 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2266 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002267}
2268
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002269TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002270 P2PTest(true, // initial_allow_p2p_for_downloading
2271 true, // initial_allow_p2p_for_sharing
2272 false, // omaha_disable_p2p_for_downloading
2273 false, // omaha_disable_p2p_for_sharing
2274 true, // payload_state_allow_p2p_attempt
2275 true, // expect_p2p_client_lookup
2276 "http://1.3.5.7/p2p", // p2p_client_result_url
2277 true, // expected_allow_p2p_for_downloading
2278 true, // expected_allow_p2p_for_sharing
2279 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002280}
2281
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002282TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002283 P2PTest(true, // initial_allow_p2p_for_downloading
2284 true, // initial_allow_p2p_for_sharing
2285 false, // omaha_disable_p2p_for_downloading
2286 false, // omaha_disable_p2p_for_sharing
2287 true, // payload_state_allow_p2p_attempt
2288 true, // expect_p2p_client_lookup
2289 "", // p2p_client_result_url
2290 false, // expected_allow_p2p_for_downloading
2291 true, // expected_allow_p2p_for_sharing
2292 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002293}
2294
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002295TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002296 P2PTest(false, // initial_allow_p2p_for_downloading
2297 true, // initial_allow_p2p_for_sharing
2298 false, // omaha_disable_p2p_for_downloading
2299 false, // omaha_disable_p2p_for_sharing
2300 true, // payload_state_allow_p2p_attempt
2301 false, // expect_p2p_client_lookup
2302 "unset", // p2p_client_result_url
2303 false, // expected_allow_p2p_for_downloading
2304 true, // expected_allow_p2p_for_sharing
2305 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002306}
2307
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002308TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002309 P2PTest(true, // initial_allow_p2p_for_downloading
2310 true, // initial_allow_p2p_for_sharing
2311 true, // omaha_disable_p2p_for_downloading
2312 false, // omaha_disable_p2p_for_sharing
2313 true, // payload_state_allow_p2p_attempt
2314 false, // expect_p2p_client_lookup
2315 "unset", // p2p_client_result_url
2316 false, // expected_allow_p2p_for_downloading
2317 true, // expected_allow_p2p_for_sharing
2318 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002319}
2320
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002321TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002322 P2PTest(true, // initial_allow_p2p_for_downloading
2323 true, // initial_allow_p2p_for_sharing
2324 false, // omaha_disable_p2p_for_downloading
2325 true, // omaha_disable_p2p_for_sharing
2326 true, // payload_state_allow_p2p_attempt
2327 true, // expect_p2p_client_lookup
2328 "http://1.3.5.7/p2p", // p2p_client_result_url
2329 true, // expected_allow_p2p_for_downloading
2330 false, // expected_allow_p2p_for_sharing
2331 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002332}
2333
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002334TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002335 P2PTest(true, // initial_allow_p2p_for_downloading
2336 true, // initial_allow_p2p_for_sharing
2337 true, // omaha_disable_p2p_for_downloading
2338 true, // omaha_disable_p2p_for_sharing
2339 true, // payload_state_allow_p2p_attempt
2340 false, // expect_p2p_client_lookup
2341 "unset", // p2p_client_result_url
2342 false, // expected_allow_p2p_for_downloading
2343 false, // expected_allow_p2p_for_sharing
2344 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002345}
2346
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002347bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2348 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002349 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002350 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2351
2352 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002353}
2354
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002355TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002356 // Simulate a successful update check that happens during OOBE. The
2357 // deadline in the response is needed to force the update attempt to
2358 // occur; responses without a deadline seen during OOBE will normally
2359 // return ErrorCode::kNonCriticalUpdateInOOBE.
2360 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2361 fake_update_response_.deadline = "20101020";
2362
David Zeuthen639aa362014-02-03 16:23:44 -08002363 // Check that we parse elapsed_days in the Omaha Response correctly.
2364 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002365 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2366 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002367 EXPECT_TRUE(response.update_exists);
2368 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002369 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002370 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002371 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002372 EXPECT_EQ(prefs_days, 42);
2373
2374 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002375 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002376 EXPECT_TRUE(response.update_exists);
2377 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002378 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002379 EXPECT_EQ(prefs_days, 42);
2380
2381 // Note that elapsed_days is not necessarily divisible by 7 so check
2382 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002383 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2384 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002385 EXPECT_TRUE(response.update_exists);
2386 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002387 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002388 EXPECT_EQ(prefs_days, 21);
2389
2390 // Check that we correctly handle elapsed_days not being included in
2391 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002392 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002393 EXPECT_TRUE(response.update_exists);
2394 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002395}
2396
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002397// If there is no prefs and OOBE is not complete, we should not
2398// report anything to Omaha.
2399TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002400 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002401 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2402 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2403}
David Zeuthen639aa362014-02-03 16:23:44 -08002404
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002405// If OOBE is complete and happened on a valid date (e.g. after Jan
2406// 1 2007 0:00 PST), that date should be used and written to
2407// prefs. However, first try with an invalid date and check we do
2408// nothing.
2409TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2410 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2411 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2412 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2413 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2414}
David Zeuthen639aa362014-02-03 16:23:44 -08002415
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002416// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2417// should yield an InstallDate of 14.
2418TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2419 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2420 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2421 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2422 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002423
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002424 int64_t prefs_days;
2425 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2426 EXPECT_EQ(prefs_days, 14);
2427}
David Zeuthen639aa362014-02-03 16:23:44 -08002428
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002429// Now that we have a valid date in prefs, check that we keep using
2430// that even if OOBE date reports something else. The date Jan 30,
2431// 2007 0:00 PST should yield an InstallDate of 28... but since
2432// there's a prefs file, we should still get 14.
2433TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2434 // Set a valid date in the prefs first.
2435 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002436
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002437 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2438 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2439 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002440
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002441 int64_t prefs_days;
2442 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2443 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002444
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002445 // If we delete the prefs file, we should get 28 days.
2446 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2447 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2448 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2449 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002450}
2451
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002452// Verifies that a device with no device policy, and is not a consumer
2453// device sets the max kernel key version to the current version.
2454// ie. the same behavior as if rollback is enabled.
2455TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2456 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2457
2458 // Setup and verify some initial default values for the kernel TPM
2459 // values that control verified boot and rollback.
2460 const int min_kernel_version = 4;
2461 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2462 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2463 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2464 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2465
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002466 EXPECT_CALL(
2467 *fake_system_state_.mock_metrics_reporter(),
2468 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2469 .Times(1);
2470
Amin Hassani41ac04b2019-03-29 11:31:03 -07002471 fake_update_response_.deadline = "20101020";
2472 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2473 tuc_params_.is_consumer_device = false;
2474 tuc_params_.rollback_allowed_milestones = 3;
2475
2476 EXPECT_TRUE(TestUpdateCheck());
2477 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002478
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002479 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002480 // kernel key version. This has the effect of freezing roll
2481 // forwards indefinitely. This will hold the rollback window
2482 // open until a future change will be able to move this forward
2483 // relative the configured window.
2484 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2485 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2486}
2487
2488// Verifies that a conmsumer device with no device policy sets the
2489// max kernel key version to the current version. ie. the same
2490// behavior as if rollback is enabled.
2491TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2492 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2493
2494 // Setup and verify some initial default values for the kernel TPM
2495 // values that control verified boot and rollback.
2496 const int min_kernel_version = 3;
2497 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2498 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2499 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2500 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2501
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002502 EXPECT_CALL(
2503 *fake_system_state_.mock_metrics_reporter(),
2504 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2505 .Times(1);
2506
Amin Hassani41ac04b2019-03-29 11:31:03 -07002507 fake_update_response_.deadline = "20101020";
2508 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2509 tuc_params_.is_consumer_device = true;
2510 tuc_params_.rollback_allowed_milestones = 3;
2511
2512 EXPECT_TRUE(TestUpdateCheck());
2513 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002514
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002515 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002516 // was set to logical infinity. This is the expected behavior for
2517 // consumer devices and matches the existing behavior prior to the
2518 // rollback features.
2519 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2520 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2521}
2522
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002523// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002524// in the TPM to prevent roll forward.
2525TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2526 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2527
2528 // Setup and verify some initial default values for the kernel TPM
2529 // values that control verified boot and rollback.
2530 const int allowed_milestones = 4;
2531 const int min_kernel_version = 3;
2532 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2533 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2534 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2535 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2536
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002537 EXPECT_CALL(
2538 *fake_system_state_.mock_metrics_reporter(),
2539 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2540 .Times(1);
2541
Amin Hassani41ac04b2019-03-29 11:31:03 -07002542 fake_update_response_.deadline = "20101020";
2543 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2544 tuc_params_.is_consumer_device = false;
2545 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2546 tuc_params_.is_policy_loaded = true;
2547
2548 EXPECT_TRUE(TestUpdateCheck());
2549 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002550
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002551 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002552 // was set to the current minimum kernel key version. This has
2553 // the effect of freezing roll forwards indefinitely. This will
2554 // hold the rollback window open until a future change will
2555 // be able to move this forward relative the configured window.
2556 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2557 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2558}
2559
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002560// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002561// in the TPM to logical infinity, to allow roll forward.
2562TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2563 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2564
2565 // Setup and verify some initial default values for the kernel TPM
2566 // values that control verified boot and rollback.
2567 const int allowed_milestones = 0;
2568 const int min_kernel_version = 3;
2569 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2570 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2571 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2572 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2573
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002574 EXPECT_CALL(
2575 *fake_system_state_.mock_metrics_reporter(),
2576 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2577 .Times(1);
2578
Amin Hassani41ac04b2019-03-29 11:31:03 -07002579 fake_update_response_.deadline = "20101020";
2580 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2581 tuc_params_.is_consumer_device = false;
2582 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2583 tuc_params_.is_policy_loaded = true;
2584
2585 EXPECT_TRUE(TestUpdateCheck());
2586 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002587
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002588 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002589 // was set to logical infinity.
2590 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2591 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2592}
2593
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002594TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002595 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002596 fake_update_response_.deadline = "20101020";
2597 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2598 tuc_params_.is_consumer_device = false;
2599 tuc_params_.rollback_allowed_milestones = 4;
2600 tuc_params_.is_policy_loaded = true;
2601
2602 EXPECT_TRUE(TestUpdateCheck());
2603 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002604 EXPECT_TRUE(response.is_rollback);
2605}
2606
2607TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002608 fake_update_response_.rollback_firmware_version = "1.2";
2609 fake_update_response_.rollback_kernel_version = "3.4";
2610 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002611 fake_update_response_.deadline = "20101020";
2612 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2613 tuc_params_.is_consumer_device = false;
2614 tuc_params_.rollback_allowed_milestones = 4;
2615 tuc_params_.is_policy_loaded = true;
2616
2617 EXPECT_TRUE(TestUpdateCheck());
2618 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002619 EXPECT_TRUE(response.is_rollback);
2620 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2621 EXPECT_EQ(2, response.rollback_key_version.firmware);
2622 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2623 EXPECT_EQ(4, response.rollback_key_version.kernel);
2624}
2625
May Lippert60aa3ca2018-08-15 16:55:29 -07002626TEST_F(OmahaRequestActionTest,
2627 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2628 FakeClock fake_clock;
2629 Time now = Time::Now();
2630 fake_clock.SetWallclockTime(now);
2631 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002632 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002633
Amin Hassani41ac04b2019-03-29 11:31:03 -07002634 ASSERT_TRUE(TestUpdateCheck());
2635
May Lippert60aa3ca2018-08-15 16:55:29 -07002636 EXPECT_TRUE(response.update_exists);
2637 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2638
2639 int64_t stored_first_seen_at_time;
2640 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2641 &stored_first_seen_at_time));
2642 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2643}
2644
2645TEST_F(OmahaRequestActionTest,
2646 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2647 FakeClock fake_clock;
2648 Time now = Time::Now();
2649 fake_clock.SetWallclockTime(now);
2650 fake_system_state_.set_clock(&fake_clock);
2651
Amin Hassani41ac04b2019-03-29 11:31:03 -07002652 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2653 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2654 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2655
2656 ASSERT_TRUE(TestUpdateCheck());
2657
May Lippert60aa3ca2018-08-15 16:55:29 -07002658 EXPECT_FALSE(response.update_exists);
2659 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2660}
2661
Xiaochu Liu88d90382018-08-29 16:09:11 -07002662TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002663 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002664 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002665 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2666
2667 ASSERT_TRUE(TestUpdateCheck());
2668
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002669 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) {
Xiaochu Liu88d90382018-08-29 16:09:11 -07002670 EXPECT_NE(string::npos,
Xiaochu Liu6310be62018-10-11 15:09:03 -07002671 post_str.find("appid=\"" + fake_update_response_.app_id + "_" +
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002672 dlc_module_id + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002673 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002674 EXPECT_NE(string::npos,
2675 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2676
2677 // Count number of updatecheck tag in response.
2678 int updatecheck_count = 0;
2679 size_t pos = 0;
2680 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2681 updatecheck_count++;
2682 pos++;
2683 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002684 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002685}
2686
2687TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2688 fake_update_response_.multi_app_skip_updatecheck = true;
2689 fake_update_response_.multi_app_no_update = false;
2690 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002691 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002692 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002693 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2694
2695 ASSERT_TRUE(TestUpdateCheck());
2696
Xiaochu Liu6310be62018-10-11 15:09:03 -07002697 EXPECT_TRUE(response.update_exists);
2698 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002699}
2700
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002701TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002702 fake_update_response_.rollback = true;
2703 fake_update_response_.rollback_allowed_milestones = 4;
2704 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002705 fake_update_response_.deadline = "20101020";
2706 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2707 tuc_params_.is_consumer_device = false;
2708 tuc_params_.rollback_allowed_milestones = 4;
2709 tuc_params_.is_policy_loaded = true;
2710
2711 EXPECT_TRUE(TestUpdateCheck());
2712 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002713 EXPECT_TRUE(response.is_rollback);
2714 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2715 response.past_rollback_key_version.firmware_key);
2716 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2717 response.past_rollback_key_version.firmware);
2718 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2719 response.past_rollback_key_version.kernel_key);
2720 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2721 response.past_rollback_key_version.kernel);
2722}
2723
2724TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002725 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002726 fake_update_response_.rollback = true;
2727 fake_update_response_.rollback_allowed_milestones = 4;
2728 fake_update_response_.rollback_firmware_version = "4.3";
2729 fake_update_response_.rollback_kernel_version = "2.1";
2730 fake_update_response_.past_rollback_key_version =
2731 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002732 fake_update_response_.deadline = "20101020";
2733 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2734 tuc_params_.is_consumer_device = false;
2735 tuc_params_.rollback_allowed_milestones = 4;
2736 tuc_params_.is_policy_loaded = true;
2737
2738 EXPECT_TRUE(TestUpdateCheck());
2739 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002740 EXPECT_TRUE(response.is_rollback);
2741 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2742 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2743 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2744 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
2745}
2746
2747TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002748 fake_update_response_.rollback = true;
2749 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002750 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002751 request_params_.set_rollback_allowed_milestones(4);
2752
2753 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
2754 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
2755 // to 2, OmahaRequestAction will look for the key values of N-4 version but
2756 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07002757 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2758 tuc_params_.is_consumer_device = false;
2759 tuc_params_.rollback_allowed_milestones = 2;
2760 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002761
Amin Hassani41ac04b2019-03-29 11:31:03 -07002762 EXPECT_TRUE(TestUpdateCheck());
2763 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002764 EXPECT_TRUE(response.is_rollback);
2765 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2766 response.past_rollback_key_version.firmware_key);
2767 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2768 response.past_rollback_key_version.firmware);
2769 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2770 response.past_rollback_key_version.kernel_key);
2771 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2772 response.past_rollback_key_version.kernel);
2773}
2774
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04002775TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
2776 request_params_.set_device_requisition("remora");
2777 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2778 ASSERT_TRUE(TestUpdateCheck());
2779 EXPECT_NE(string::npos, post_str.find("requisition=\"remora\""));
2780}
2781
2782TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
2783 request_params_.set_device_requisition("");
2784 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2785 ASSERT_TRUE(TestUpdateCheck());
2786 EXPECT_EQ(string::npos, post_str.find("requisition"));
2787}
2788
Darin Petkov6a5b3222010-07-13 14:55:28 -07002789} // namespace chromeos_update_engine