blob: b66375f2ce8e97488329a09e1238d8cac9889cec [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov6a5b3222010-07-13 14:55:28 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_action.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070021#include <limits>
Ben Chanab5a0af2017-10-12 14:57:50 -070022#include <memory>
Darin Petkov6a5b3222010-07-13 14:55:28 -070023#include <string>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080024#include <utility>
Darin Petkov6a5b3222010-07-13 14:55:28 -070025#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070026
Alex Deymo60ca1a72015-06-18 18:19:15 -070027#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070028#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070029#include <base/files/scoped_temp_dir.h>
Ben Chan5c02c132017-06-27 07:10:36 -070030#include <base/memory/ptr_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070031#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070032#include <base/strings/string_util.h>
33#include <base/strings/stringprintf.h>
34#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070035#include <brillo/message_loops/fake_message_loop.h>
36#include <brillo/message_loops/message_loop.h>
37#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070038#include <gtest/gtest.h>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080039#include <policy/libpolicy.h>
40#include <policy/mock_libpolicy.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070041
Alex Deymo39910dc2015-11-09 17:04:30 -080042#include "update_engine/common/action_pipe.h"
43#include "update_engine/common/constants.h"
44#include "update_engine/common/fake_prefs.h"
45#include "update_engine/common/hash_calculator.h"
46#include "update_engine/common/mock_http_fetcher.h"
47#include "update_engine/common/platform_constants.h"
48#include "update_engine/common/prefs.h"
49#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030050#include "update_engine/fake_system_state.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070051#include "update_engine/metrics_reporter_interface.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070052#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070053#include "update_engine/mock_payload_state.h"
Amin Hassani7fca2862019-03-28 16:09:22 -070054#include "update_engine/omaha_request_builder_xml.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055#include "update_engine/omaha_request_params.h"
Jae Hoon Kim051627a2019-09-03 12:56:32 -070056#include "update_engine/omaha_utils.h"
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080057#include "update_engine/update_manager/rollback_prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070058
Darin Petkov1cbd78f2010-07-29 12:38:34 -070059using base::Time;
60using base::TimeDelta;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080061using chromeos_update_manager::kRollforwardInfinity;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070062using std::pair;
Darin Petkov6a5b3222010-07-13 14:55:28 -070063using std::string;
64using std::vector;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080065using testing::_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070066using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070067using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070068using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070069using testing::Ge;
70using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080071using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070072using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070073using testing::ReturnPointee;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080074using testing::ReturnRef;
Gilad Arnold74b5f552014-10-07 08:17:16 -070075using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070076using testing::SetArgPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070077
Alex Deymo8e18f932015-03-27 16:16:59 -070078namespace {
79
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080080static_assert(kRollforwardInfinity == 0xfffffffe,
81 "Don't change the value of kRollforward infinity unless its "
82 "size has been changed in firmware.");
83
Xiaochu Liu6310be62018-10-11 15:09:03 -070084const char kCurrentVersion[] = "0.1.0.0";
Alex Deymo85616652015-10-15 18:48:31 -070085const char kTestAppId[] = "test-app-id";
Aaron Wood7dcdedf2017-09-06 17:17:41 -070086const char kTestAppId2[] = "test-app2-id";
Xiaochu Liu6310be62018-10-11 15:09:03 -070087const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck";
Alex Deymo85616652015-10-15 18:48:31 -070088
Alex Deymo8e18f932015-03-27 16:16:59 -070089// This is a helper struct to allow unit tests build an update response with the
90// values they care about.
91struct FakeUpdateResponse {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070092 string GetRollbackVersionAttributes() const {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070093 string num_milestones;
94#if BASE_VER < 576279
95 num_milestones = base::IntToString(rollback_allowed_milestones);
96#else
97 num_milestones = base::NumberToString(rollback_allowed_milestones);
98#endif
99 const string rollback_version =
100 " _firmware_version_" + num_milestones + "=\"" +
101 past_rollback_key_version.first + "\"" + " _kernel_version_" +
102 num_milestones + "=\"" + past_rollback_key_version.second + "\"";
103
104 return (rollback ? " _rollback=\"true\"" : "") + rollback_version +
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700105 (!rollback_firmware_version.empty()
106 ? " _firmware_version=\"" + rollback_firmware_version + "\""
107 : "") +
108 (!rollback_kernel_version.empty()
109 ? " _kernel_version=\"" + rollback_kernel_version + "\""
110 : "");
111 }
112
Alex Deymo8e18f932015-03-27 16:16:59 -0700113 string GetNoUpdateResponse() const {
114 string entity_str;
115 if (include_entity)
116 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Sen Jiang81259682017-03-30 15:11:30 -0700117 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str +
118 "<response protocol=\"3.0\">"
119 "<daystart elapsed_seconds=\"100\"/>"
120 "<app appid=\"" +
121 app_id + "\" " +
122 (include_cohorts
123 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
124 "\" cohortname=\"" + cohortname + "\" "
125 : "") +
126 " status=\"ok\">"
127 "<ping status=\"ok\"/>"
128 "<updatecheck status=\"noupdate\"/></app>" +
129 (multi_app_no_update
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700130 ? "<app appid=\"" + app_id2 +
131 "\"><updatecheck status=\"noupdate\"/></app>"
Sen Jiang81259682017-03-30 15:11:30 -0700132 : "") +
133 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700134 }
135
136 string GetUpdateResponse() const {
Sen Jiang2703ef42017-03-16 13:36:21 -0700137 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
138 "protocol=\"3.0\">"
139 "<daystart elapsed_seconds=\"100\"" +
140 (elapsed_days.empty() ? ""
141 : (" elapsed_days=\"" + elapsed_days + "\"")) +
142 "/>"
143 "<app appid=\"" +
144 app_id + "\" " +
145 (include_cohorts
146 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
147 "\" cohortname=\"" + cohortname + "\" "
148 : "") +
149 " status=\"ok\">"
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700150 "<ping status=\"ok\"/><updatecheck status=\"ok\"" +
151 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" +
Sen Jiang2703ef42017-03-16 13:36:21 -0700152 codebase +
153 "\"/></urls>"
154 "<manifest version=\"" +
155 version +
156 "\">"
157 "<packages><package hash=\"not-used\" name=\"" +
158 filename + "\" size=\"" + base::Int64ToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800159 "\" hash_sha256=\"" + hash + "\"/>" +
160 (multi_package ? "<package name=\"package2\" size=\"222\" "
161 "hash_sha256=\"hash2\"/>"
162 : "") +
163 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700164 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700165 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
166 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700167 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700168 "IsDeltaPayload=\"true" +
169 (multi_package ? ":false" : "") +
170 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700171 "MaxDaysToScatter=\"" +
172 max_days_to_scatter +
173 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700174 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700175 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
176 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
177 : "") +
178 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700179 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700180 "/></actions></manifest></updatecheck></app>" +
181 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700182 ? "<app appid=\"" + app_id2 + "\"" +
183 (include_cohorts ? " cohort=\"cohort2\"" : "") +
184 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700185 codebase2 + "\"/></urls><manifest version=\"" + version2 +
186 "\"><packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700187 "<package name=\"package3\" size=\"333\" "
188 "hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700189 "<actions><action event=\"postinstall\" " +
190 (multi_app_self_update
191 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
192 : "IsDeltaPayload=\"false\" ") +
193 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700194 "</manifest></updatecheck></app>"
195 : "") +
196 (multi_app_no_update
197 ? "<app><updatecheck status=\"noupdate\"/></app>"
198 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700199 (multi_app_skip_updatecheck
200 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
201 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700202 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700203 }
204
205 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800206 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700207
Alex Deymo85616652015-10-15 18:48:31 -0700208 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700209 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700210 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
211 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700212 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700213 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700214 string more_info_url = "http://more/info";
215 string prompt = "true";
216 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700217 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700218 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700219 string hash = "4841534831323334";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800220 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700221 string deadline = "";
222 string max_days_to_scatter = "7";
223 string elapsed_days = "42";
224
225 // P2P setting defaults to allowed.
226 bool disable_p2p_for_downloading = false;
227 bool disable_p2p_for_sharing = false;
228
Sen Jiangfe284402018-03-21 14:03:50 -0700229 bool powerwash = false;
230
Alex Deymo8e18f932015-03-27 16:16:59 -0700231 // Omaha cohorts settings.
232 bool include_cohorts = false;
233 string cohort = "";
234 string cohorthint = "";
235 string cohortname = "";
236
237 // Whether to include the CrOS <!ENTITY> in the XML response.
238 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800239
Sen Jiang81259682017-03-30 15:11:30 -0700240 // Whether to include more than one app.
241 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700242 // Whether to include an app with noupdate="true".
243 bool multi_app_self_update = false;
244 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700245 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700246 // Whether to include an additional app with no updatecheck tag.
247 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700248 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800249 bool multi_package = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700250
251 // Whether the payload is a rollback.
252 bool rollback = false;
253 // The verified boot firmware key version for the rollback image.
254 string rollback_firmware_version = "";
255 // The verified boot kernel key version for the rollback image.
256 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700257 // The number of milestones back that the verified boot key version has been
258 // supplied.
259 uint32_t rollback_allowed_milestones = 0;
260 // The verified boot key version for the
261 // |current - rollback_allowed_milestones| most recent release.
262 // The pair contains <firmware_key_version, kernel_key_version> each
263 // of which is in the form "key_version.version".
264 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700265};
266
267} // namespace
268
Darin Petkov6a5b3222010-07-13 14:55:28 -0700269namespace chromeos_update_engine {
270
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700271class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
272 public:
273 OmahaRequestActionTestProcessorDelegate()
274 : expected_code_(ErrorCode::kSuccess),
275 interactive_(false),
276 test_http_fetcher_headers_(false) {}
277 ~OmahaRequestActionTestProcessorDelegate() override = default;
278
279 void ProcessingDone(const ActionProcessor* processor,
280 ErrorCode code) override {
281 brillo::MessageLoop::current()->BreakLoop();
282 }
283
284 void ActionCompleted(ActionProcessor* processor,
285 AbstractAction* action,
286 ErrorCode code) override {
287 // Make sure actions always succeed.
288 if (action->Type() == OmahaRequestAction::StaticType()) {
289 EXPECT_EQ(expected_code_, code);
290 // Check that the headers were set in the fetcher during the action. Note
291 // that we set this request as "interactive".
292 auto fetcher = static_cast<const MockHttpFetcher*>(
293 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
294
295 if (test_http_fetcher_headers_) {
296 EXPECT_EQ(interactive_ ? "fg" : "bg",
297 fetcher->GetHeader("X-Goog-Update-Interactivity"));
298 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
299 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
300 }
301 post_data_ = fetcher->post_data();
302 } else if (action->Type() ==
303 ObjectCollectorAction<OmahaResponse>::StaticType()) {
304 EXPECT_EQ(ErrorCode::kSuccess, code);
305 auto collector_action =
306 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
307 omaha_response_.reset(new OmahaResponse(collector_action->object()));
308 EXPECT_TRUE(omaha_response_);
309 } else {
310 EXPECT_EQ(ErrorCode::kSuccess, code);
311 }
312 }
313 ErrorCode expected_code_;
314 brillo::Blob post_data_;
315 bool interactive_;
316 bool test_http_fetcher_headers_;
317 std::unique_ptr<OmahaResponse> omaha_response_;
318};
319
Amin Hassani41ac04b2019-03-29 11:31:03 -0700320struct TestUpdateCheckParams {
321 string http_response;
322 int fail_http_response_code;
323 bool ping_only;
324 bool is_consumer_device;
325 int rollback_allowed_milestones;
326 bool is_policy_loaded;
327 ErrorCode expected_code;
328 metrics::CheckResult expected_check_result;
329 metrics::CheckReaction expected_check_reaction;
330 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700331 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700332};
333
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700334class OmahaRequestActionTest : public ::testing::Test {
335 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800336 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200337 request_params_.set_os_sp("service_pack");
338 request_params_.set_os_board("x86-generic");
339 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700340 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200341 request_params_.set_app_lang("en-US");
342 request_params_.set_current_channel("unittest");
343 request_params_.set_target_channel("unittest");
344 request_params_.set_hwid("OEM MODEL 09235 7471");
345 request_params_.set_fw_version("ChromeOSFirmware.1.0");
346 request_params_.set_ec_version("0X0A1");
347 request_params_.set_delta_okay(true);
348 request_params_.set_interactive(false);
349 request_params_.set_update_url("http://url");
350 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200351 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200352 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700353 request_params_.set_is_install(false);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800354 request_params_.set_dlc_module_ids({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200355
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700356 fake_system_state_.set_request_params(&request_params_);
357 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700358
359 // Setting the default update check params. Lookup |TestUpdateCheck()|.
360 tuc_params_ = {
361 .http_response = "",
362 .fail_http_response_code = -1,
363 .ping_only = false,
364 .is_consumer_device = true,
365 .rollback_allowed_milestones = 0,
366 .is_policy_loaded = false,
367 .expected_code = ErrorCode::kSuccess,
368 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
369 .expected_check_reaction = metrics::CheckReaction::kUpdating,
370 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
371 };
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700372 }
373
Amin Hassani41ac04b2019-03-29 11:31:03 -0700374 // This function uses the paramets in |tuc_params_| to do an update check. It
375 // will fill out |post_str| with the result data and |response| with
376 // |OmahaResponse|. Returns true iff an output response was obtained from the
377 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
378 // transfer will fail with that code. |ping_only| is passed through to the
379 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700380 //
381 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700382 // |expected_error_code| parameters are for checking expectations about
383 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
384 // statistics. Use the appropriate ::kUnset value to specify that the given
385 // metric should not be reported.
386 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700387
Amin Hassani41ac04b2019-03-29 11:31:03 -0700388 // Tests events using |event| and |https_response|. It will fill up |post_str|
389 // with the result data.
390 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700391
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800392 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700393 // only a ping or also an updatecheck.
394 void PingTest(bool ping_only);
395
396 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800397 bool InstallDateParseHelper(const string& elapsed_days,
398 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700399
400 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800401 void P2PTest(bool initial_allow_p2p_for_downloading,
402 bool initial_allow_p2p_for_sharing,
403 bool omaha_disable_p2p_for_downloading,
404 bool omaha_disable_p2p_for_sharing,
405 bool payload_state_allow_p2p_attempt,
406 bool expect_p2p_client_lookup,
407 const string& p2p_client_result_url,
408 bool expected_allow_p2p_for_downloading,
409 bool expected_allow_p2p_for_sharing,
410 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700411
412 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700413 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200414 // Used by all tests.
415 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700416
417 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700418
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700419 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700420
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700421 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700422
423 TestUpdateCheckParams tuc_params_;
424
425 // TODO(ahassani): Add trailing _ to these two variables.
426 OmahaResponse response;
427 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700428};
429
Amin Hassani41ac04b2019-03-29 11:31:03 -0700430bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700431 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700432 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700433 auto fetcher =
434 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
435 tuc_params_.http_response.size(),
436 nullptr);
437 if (tuc_params_.fail_http_response_code >= 0) {
438 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700439 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200440 // This ensures the tests didn't forget to update fake_system_state_ if they
441 // are not using the default request_params_.
442 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
443
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700444 auto omaha_request_action =
445 std::make_unique<OmahaRequestAction>(&fake_system_state_,
446 nullptr,
447 std::move(fetcher),
448 tuc_params_.ping_only,
449 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700450
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800451 auto mock_policy_provider =
452 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
453 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700454 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800455
456 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700457 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800458
459 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700460 const bool get_allowed_milestone_succeeds =
461 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800462 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700463 .WillRepeatedly(
464 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
465 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800466
467 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
468 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700469 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800470
Amin Hassani41ac04b2019-03-29 11:31:03 -0700471 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700472 delegate_.interactive_ = request_params_.interactive();
473 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700474 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700475 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700476
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700477 auto collector_action =
478 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
479 BondActions(omaha_request_action.get(), collector_action.get());
480 processor.EnqueueAction(std::move(omaha_request_action));
481 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700482
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700483 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
484 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800485 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700486
Amin Hassani41ac04b2019-03-29 11:31:03 -0700487 EXPECT_CALL(
488 *fake_system_state_.mock_metrics_reporter(),
489 ReportUpdateCheckMetrics(_,
490 tuc_params_.expected_check_result,
491 tuc_params_.expected_check_reaction,
492 tuc_params_.expected_download_error_code))
493 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800494
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700495 loop.PostTask(base::Bind(
496 [](ActionProcessor* processor) { processor->StartProcessing(); },
497 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700498 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700499 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700500 if (delegate_.omaha_response_)
501 response = *delegate_.omaha_response_;
502 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700503 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700504}
505
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700506// Tests Event requests -- they should always succeed. |out_post_data| may be
507// null; if non-null, the post-data received by the mock HttpFetcher is
508// returned.
509void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700510 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700511 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700512 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700513
514 auto action = std::make_unique<OmahaRequestAction>(
515 &fake_system_state_,
516 event,
517 std::make_unique<MockHttpFetcher>(
518 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700519 false,
520 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700521 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700522 processor.set_delegate(&delegate_);
523 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700524
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700525 loop.PostTask(base::Bind(
526 [](ActionProcessor* processor) { processor->StartProcessing(); },
527 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700528 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700529 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700530
Amin Hassani41ac04b2019-03-29 11:31:03 -0700531 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700532}
533
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700534TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700535 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700536 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
537 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
538 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
539 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
540
541 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400542 EXPECT_FALSE(response.update_exists);
543}
544
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700545TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700546 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
547 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
548 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
549
550 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700551 EXPECT_FALSE(response.update_exists);
552}
553
Sen Jiang81259682017-03-30 15:11:30 -0700554TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700555 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700556 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
557 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
558 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
559
560 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700561 EXPECT_FALSE(response.update_exists);
562}
563
Sen Jiang00adf7b2017-06-26 15:57:29 -0700564TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700565 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700566 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
567 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
568 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
569
570 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700571 EXPECT_FALSE(response.update_exists);
572}
573
574TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700575 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700576 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
577 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700578 "</manifest></updatecheck></app></response>";
579 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
580 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
581
582 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700583 EXPECT_FALSE(response.update_exists);
584}
585
Alex Deymo8e18f932015-03-27 16:16:59 -0700586// Test that all the values in the response are parsed in a normal update
587// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700588TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700589 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700590 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
591
592 ASSERT_TRUE(TestUpdateCheck());
593
Darin Petkov6a5b3222010-07-13 14:55:28 -0700594 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700595 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700596 EXPECT_EQ("", response.system_version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800597 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
598 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700599 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800600 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
601 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700602 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700603 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
604 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700605 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700606 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700607 // persisted.
608 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
609 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
610 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700611}
612
Sen Jiang0affc2c2017-02-10 15:55:05 -0800613TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800614 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700615 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
616
617 ASSERT_TRUE(TestUpdateCheck());
618
Sen Jiang0affc2c2017-02-10 15:55:05 -0800619 EXPECT_TRUE(response.update_exists);
620 EXPECT_EQ(fake_update_response_.version, response.version);
621 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
622 response.packages[0].payload_urls[0]);
623 EXPECT_EQ(fake_update_response_.codebase + "package2",
624 response.packages[1].payload_urls[0]);
625 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
626 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700627 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700628 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800629 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700630 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800631 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700632 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700633 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700634}
635
636TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700637 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700638 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
639
640 ASSERT_TRUE(TestUpdateCheck());
641
Sen Jiang81259682017-03-30 15:11:30 -0700642 EXPECT_TRUE(response.update_exists);
643 EXPECT_EQ(fake_update_response_.version, response.version);
644 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
645 response.packages[0].payload_urls[0]);
646 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
647 response.packages[1].payload_urls[0]);
648 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
649 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
650 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700651 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700652 ASSERT_EQ(2u, response.packages.size());
653 EXPECT_EQ(string("hash3"), response.packages[1].hash);
654 EXPECT_EQ(333u, response.packages[1].size);
655 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700656 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700657}
658
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700659TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) {
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700660 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700661 // Trigger the lining up of the app and system versions.
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700662 request_params_.set_system_app_id(fake_update_response_.app_id2);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700663 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700664
Amin Hassani41ac04b2019-03-29 11:31:03 -0700665 ASSERT_TRUE(TestUpdateCheck());
666
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700667 EXPECT_TRUE(response.update_exists);
668 EXPECT_EQ(fake_update_response_.version, response.version);
669 EXPECT_EQ(fake_update_response_.version2, response.system_version);
670 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
671 response.packages[0].payload_urls[0]);
672 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
673 response.packages[1].payload_urls[0]);
674 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
675 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
676 EXPECT_EQ(11u, response.packages[0].metadata_size);
677 EXPECT_EQ(true, response.packages[0].is_delta);
678 ASSERT_EQ(2u, response.packages.size());
679 EXPECT_EQ(string("hash3"), response.packages[1].hash);
680 EXPECT_EQ(333u, response.packages[1].size);
681 EXPECT_EQ(33u, response.packages[1].metadata_size);
682 EXPECT_EQ(false, response.packages[1].is_delta);
683}
684
Sen Jiang81259682017-03-30 15:11:30 -0700685TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700686 fake_update_response_.multi_app = true;
687 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700688 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
689
690 ASSERT_TRUE(TestUpdateCheck());
691
Sen Jiang81259682017-03-30 15:11:30 -0700692 EXPECT_TRUE(response.update_exists);
693 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700694 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700695 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
696 response.packages[0].payload_urls[0]);
697 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
698 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
699 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700700 ASSERT_EQ(2u, response.packages.size());
701 EXPECT_EQ(string("hash3"), response.packages[1].hash);
702 EXPECT_EQ(333u, response.packages[1].size);
703 EXPECT_EQ(33u, response.packages[1].metadata_size);
704 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700705}
706
707TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700708 fake_update_response_.multi_app = true;
709 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700710 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
711
712 ASSERT_TRUE(TestUpdateCheck());
713
Sen Jiang81259682017-03-30 15:11:30 -0700714 EXPECT_TRUE(response.update_exists);
715 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700716 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700717 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
718 response.packages[0].payload_urls[0]);
719 EXPECT_EQ(fake_update_response_.codebase + "package2",
720 response.packages[1].payload_urls[0]);
721 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
722 response.packages[2].payload_urls[0]);
723 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
724 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
725 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700726 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700727 ASSERT_EQ(3u, response.packages.size());
728 EXPECT_EQ(string("hash2"), response.packages[1].hash);
729 EXPECT_EQ(222u, response.packages[1].size);
730 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700731 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700732 EXPECT_EQ(string("hash3"), response.packages[2].hash);
733 EXPECT_EQ(333u, response.packages[2].size);
734 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700735 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800736}
737
Sen Jiangfe284402018-03-21 14:03:50 -0700738TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700739 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700740 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
741
742 ASSERT_TRUE(TestUpdateCheck());
743
Sen Jiangfe284402018-03-21 14:03:50 -0700744 EXPECT_TRUE(response.update_exists);
745 EXPECT_TRUE(response.powerwash_required);
746}
747
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700748TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700749 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700750 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700751 tuc_params_.http_response = "invalid xml>";
752 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
753 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
754 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
755
756 ASSERT_FALSE(TestUpdateCheck());
757
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700758 EXPECT_FALSE(response.update_exists);
759}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700760
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700761TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700762 request_params_.set_interactive(false);
763 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700764 tuc_params_.http_response = "invalid xml>";
765 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
766 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
767 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
768
769 ASSERT_FALSE(TestUpdateCheck());
770
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700771 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700772}
773
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700774TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700775 // Set up a connection manager that doesn't allow a valid update over
776 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700777 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700778 fake_system_state_.set_connection_manager(&mock_cm);
779
Alex Deymo30534502015-07-20 15:06:33 -0700780 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800781 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
782 SetArgPointee<1>(ConnectionTethering::kUnknown),
783 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700784 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
785 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700786
Amin Hassani41ac04b2019-03-29 11:31:03 -0700787 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
788 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
789 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
790
791 ASSERT_FALSE(TestUpdateCheck());
792
Chris Sosa77f79e82014-06-02 18:16:24 -0700793 EXPECT_FALSE(response.update_exists);
794}
795
Weidong Guo421ff332017-04-17 10:08:38 -0700796TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
797 // This test tests that update over cellular is allowed as device policy
798 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700799 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700800 fake_system_state_.set_connection_manager(&mock_cm);
801
802 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
803 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
804 SetArgPointee<1>(ConnectionTethering::kUnknown),
805 Return(true)));
806 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
807 .WillRepeatedly(Return(true));
808 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
809 .WillRepeatedly(Return(true));
810
Amin Hassani41ac04b2019-03-29 11:31:03 -0700811 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
812
813 ASSERT_TRUE(TestUpdateCheck());
814
Weidong Guo421ff332017-04-17 10:08:38 -0700815 EXPECT_TRUE(response.update_exists);
816}
817
818TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
819 // This test tests that update over cellular is blocked as device policy
820 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700821 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700822 fake_system_state_.set_connection_manager(&mock_cm);
823
824 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
825 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
826 SetArgPointee<1>(ConnectionTethering::kUnknown),
827 Return(true)));
828 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
829 .WillRepeatedly(Return(true));
830 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
831 .WillRepeatedly(Return(false));
832
Amin Hassani41ac04b2019-03-29 11:31:03 -0700833 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
834 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
835 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
836
837 ASSERT_FALSE(TestUpdateCheck());
838
Weidong Guo421ff332017-04-17 10:08:38 -0700839 EXPECT_FALSE(response.update_exists);
840}
841
842TEST_F(OmahaRequestActionTest,
843 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
844 // This test tests that, when device policy is not set, update over cellular
845 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700846 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700847 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
848 fake_system_state_.set_connection_manager(&mock_cm);
849
850 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
851 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
852 SetArgPointee<1>(ConnectionTethering::kUnknown),
853 Return(true)));
854 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
855 .WillRepeatedly(Return(false));
856 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
857 .WillRepeatedly(Return(true));
858
Amin Hassani41ac04b2019-03-29 11:31:03 -0700859 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
860
861 ASSERT_TRUE(TestUpdateCheck());
862
Weidong Guo421ff332017-04-17 10:08:38 -0700863 EXPECT_TRUE(response.update_exists);
864}
865
866TEST_F(OmahaRequestActionTest,
867 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
868 // This test tests that, when device policy is not set and permission for
869 // update over cellular is set to false or does not exist, update over
870 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700871 MockConnectionManager mock_cm;
872 // A version different from the version in omaha response.
873 string diff_version = "99.99.99";
874 // A size different from the size in omaha response.
875 int64_t diff_size = 999;
876
877 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
878 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
879 // This test tests cellular (3G) being the only connection type being allowed.
880 fake_system_state_.set_connection_manager(&mock_cm);
881
882 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
883 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
884 SetArgPointee<1>(ConnectionTethering::kUnknown),
885 Return(true)));
886 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
887 .WillRepeatedly(Return(false));
888 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
889 .WillRepeatedly(Return(true));
890
Amin Hassani41ac04b2019-03-29 11:31:03 -0700891 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
892 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
893 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
894
895 ASSERT_FALSE(TestUpdateCheck());
896
Weidong Guo421ff332017-04-17 10:08:38 -0700897 EXPECT_FALSE(response.update_exists);
898}
899
900TEST_F(OmahaRequestActionTest,
901 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
902 // This test tests that, when device policy is not set and permission for
903 // update over cellular is set to false or does not exist, update over
904 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700905 MockConnectionManager mock_cm;
906 // A version same as the version in omaha response.
907 string new_version = fake_update_response_.version;
908 // A size same as the size in omaha response.
909 int64_t new_size = fake_update_response_.size;
910
911 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
912 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
913 fake_system_state_.set_connection_manager(&mock_cm);
914
915 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
916 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
917 SetArgPointee<1>(ConnectionTethering::kUnknown),
918 Return(true)));
919 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
920 .WillRepeatedly(Return(false));
921 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
922 .WillRepeatedly(Return(true));
923
Amin Hassani41ac04b2019-03-29 11:31:03 -0700924 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
925
926 ASSERT_TRUE(TestUpdateCheck());
927
Weidong Guo421ff332017-04-17 10:08:38 -0700928 EXPECT_TRUE(response.update_exists);
929}
930
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700931TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700932 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700933 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700934 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700935 fake_update_response_.version = rollback_version;
936 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
937 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
938 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700939
Chris Sosa77f79e82014-06-02 18:16:24 -0700940 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800941 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700942
Amin Hassani41ac04b2019-03-29 11:31:03 -0700943 ASSERT_FALSE(TestUpdateCheck());
944
Chris Sosa77f79e82014-06-02 18:16:24 -0700945 EXPECT_FALSE(response.update_exists);
946}
947
Marton Hunyadyc2882062018-05-14 17:28:25 +0200948// Verify that update checks called during OOBE will not try to download an
949// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700950TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200951 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700952 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
953 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
954 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
955 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -0700956
Sen Jiang8cd42342018-01-31 12:06:59 -0800957 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700958 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700959 ASSERT_FALSE(TestUpdateCheck());
960
Kevin Cernekee2494e282016-03-29 18:03:53 -0700961 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +0200962}
Kevin Cernekee2494e282016-03-29 18:03:53 -0700963
Marton Hunyadyc2882062018-05-14 17:28:25 +0200964// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
965// enabled.
966TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200967 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700968 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700969 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700970
Amin Hassani41ac04b2019-03-29 11:31:03 -0700971 ASSERT_TRUE(TestUpdateCheck());
972
Kevin Cernekee2494e282016-03-29 18:03:53 -0700973 EXPECT_TRUE(response.update_exists);
974}
975
Marton Hunyadyc2882062018-05-14 17:28:25 +0200976// Verify that update checks called during OOBE will still try to download an
977// update if the response includes the deadline field.
978TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200979 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
980 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700981 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +0200982
Amin Hassani41ac04b2019-03-29 11:31:03 -0700983 ASSERT_TRUE(TestUpdateCheck());
984
Marton Hunyadyc2882062018-05-14 17:28:25 +0200985 EXPECT_TRUE(response.update_exists);
986}
987
988// Verify that update checks called during OOBE will not try to download an
989// update if a rollback happened, even when the response includes the deadline
990// field.
991TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200992 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
993 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700994 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
995 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
996 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
997 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
998
Marton Hunyadyc2882062018-05-14 17:28:25 +0200999 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
1000 .WillOnce(Return(true));
1001
Amin Hassani41ac04b2019-03-29 11:31:03 -07001002 ASSERT_FALSE(TestUpdateCheck());
1003
Marton Hunyadyc2882062018-05-14 17:28:25 +02001004 EXPECT_FALSE(response.update_exists);
1005}
1006
Toni Barzic61544e62018-10-11 14:37:30 -07001007// Verify that non-critical updates are skipped by reporting the
1008// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1009// i.e. when the update would need user permission. Note that reporting
1010// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1011// in OOBE (warning the user about an update that will be skipped).
1012TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001013 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1014
1015 MockConnectionManager mock_cm;
1016 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001017 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1018 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1019 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1020 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001021
1022 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1023 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1024 SetArgPointee<1>(ConnectionTethering::kUnknown),
1025 Return(true)));
1026 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1027 .WillRepeatedly(Return(false));
1028
Amin Hassani41ac04b2019-03-29 11:31:03 -07001029 ASSERT_FALSE(TestUpdateCheck());
1030
Toni Barzic61544e62018-10-11 14:37:30 -07001031 EXPECT_FALSE(response.update_exists);
1032}
1033
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001034TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001035 request_params_.set_wall_clock_based_wait_enabled(true);
1036 request_params_.set_update_check_count_wait_enabled(false);
1037 request_params_.set_waiting_period(TimeDelta::FromDays(2));
May Lippert60aa3ca2018-08-15 16:55:29 -07001038 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001039 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1040 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1041 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001042
Amin Hassani41ac04b2019-03-29 11:31:03 -07001043 ASSERT_FALSE(TestUpdateCheck());
1044
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001045 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001046}
1047
1048TEST_F(OmahaRequestActionTest,
1049 WallClockBasedWaitAloneCausesScatteringInteractive) {
1050 request_params_.set_wall_clock_based_wait_enabled(true);
1051 request_params_.set_update_check_count_wait_enabled(false);
1052 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1053 request_params_.set_interactive(true);
1054 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1055 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001056
1057 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001058 ASSERT_TRUE(TestUpdateCheck());
1059
Chris Sosa968d0572013-08-23 14:46:02 -07001060 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001061}
1062
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001063TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001064 request_params_.set_wall_clock_based_wait_enabled(false);
1065 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1066 request_params_.set_update_check_count_wait_enabled(true);
1067 request_params_.set_min_update_checks_needed(1);
1068 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001069 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001070
Amin Hassani41ac04b2019-03-29 11:31:03 -07001071 ASSERT_TRUE(TestUpdateCheck());
1072
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001073 EXPECT_TRUE(response.update_exists);
1074}
1075
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001076TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001077 request_params_.set_wall_clock_based_wait_enabled(true);
1078 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1079 request_params_.set_update_check_count_wait_enabled(true);
1080 request_params_.set_min_update_checks_needed(1);
1081 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001082 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001083 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1084
1085 ASSERT_TRUE(TestUpdateCheck());
1086
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001087 EXPECT_TRUE(response.update_exists);
1088}
1089
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001090TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001091 request_params_.set_wall_clock_based_wait_enabled(true);
1092 request_params_.set_waiting_period(TimeDelta());
1093 request_params_.set_update_check_count_wait_enabled(true);
1094 request_params_.set_min_update_checks_needed(0);
1095 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001096 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001097 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001098
Amin Hassani41ac04b2019-03-29 11:31:03 -07001099 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001100
Ben Chan9abb7632014-08-07 00:10:53 -07001101 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001102 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001103 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001104 EXPECT_TRUE(response.update_exists);
1105}
1106
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001107TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001108 request_params_.set_wall_clock_based_wait_enabled(true);
1109 request_params_.set_waiting_period(TimeDelta());
1110 request_params_.set_update_check_count_wait_enabled(true);
1111 request_params_.set_min_update_checks_needed(1);
1112 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001113 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001114 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1115 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1116 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001117
Amin Hassani41ac04b2019-03-29 11:31:03 -07001118 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001119
Ben Chan9abb7632014-08-07 00:10:53 -07001120 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001121 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001122 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001123 EXPECT_FALSE(response.update_exists);
1124}
1125
Amin Hassani41ac04b2019-03-29 11:31:03 -07001126TEST_F(OmahaRequestActionTest,
1127 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001128 request_params_.set_wall_clock_based_wait_enabled(true);
1129 request_params_.set_waiting_period(TimeDelta());
1130 request_params_.set_update_check_count_wait_enabled(true);
1131 request_params_.set_min_update_checks_needed(1);
1132 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001133 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001134 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001135 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1136
1137 // Verify if we are interactive check we don't defer.
1138 ASSERT_TRUE(TestUpdateCheck());
1139
1140 EXPECT_TRUE(response.update_exists);
1141}
1142
1143TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1144 request_params_.set_wall_clock_based_wait_enabled(true);
1145 request_params_.set_waiting_period(TimeDelta());
1146 request_params_.set_update_check_count_wait_enabled(true);
1147 request_params_.set_min_update_checks_needed(1);
1148 request_params_.set_max_update_checks_allowed(8);
1149 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1150 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1151 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1152 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001153
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001154 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001155 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001156
Ben Chan9abb7632014-08-07 00:10:53 -07001157 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001158 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001159 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001160 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001161 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001162 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001163}
1164
1165TEST_F(OmahaRequestActionTest,
1166 ExistingUpdateCheckCountCausesScatteringInteractive) {
1167 request_params_.set_wall_clock_based_wait_enabled(true);
1168 request_params_.set_waiting_period(TimeDelta());
1169 request_params_.set_update_check_count_wait_enabled(true);
1170 request_params_.set_min_update_checks_needed(1);
1171 request_params_.set_max_update_checks_allowed(8);
1172 request_params_.set_interactive(true);
1173 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1174 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1175
1176 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001177
1178 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001179 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001180 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001181}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001182
Adolfo Victoria497044c2018-07-18 07:51:42 -07001183TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1184 // If staging is on, the value for max days to scatter should be ignored, and
1185 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001186 request_params_.set_wall_clock_based_wait_enabled(true);
1187 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1188 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001189 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1190
Adolfo Victoria497044c2018-07-18 07:51:42 -07001191 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1192 // This should not prevent scattering due to staging.
1193 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001194 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1195 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1196 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1197
1198 ASSERT_FALSE(TestUpdateCheck());
1199
Adolfo Victoria497044c2018-07-18 07:51:42 -07001200 EXPECT_FALSE(response.update_exists);
1201
1202 // Interactive updates should not be affected.
1203 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001204 tuc_params_.expected_code = ErrorCode::kSuccess;
1205 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1206
1207 ASSERT_TRUE(TestUpdateCheck());
1208
Adolfo Victoria497044c2018-07-18 07:51:42 -07001209 EXPECT_TRUE(response.update_exists);
1210}
1211
Alex Deymo8e18f932015-03-27 16:16:59 -07001212TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001213 fake_update_response_.include_cohorts = true;
1214 fake_update_response_.cohort = "s/154454/8479665";
1215 fake_update_response_.cohorthint = "please-put-me-on-beta";
1216 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001217 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001218
Amin Hassani41ac04b2019-03-29 11:31:03 -07001219 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001220
1221 string value;
1222 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1223 EXPECT_EQ(fake_update_response_.cohort, value);
1224
1225 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1226 EXPECT_EQ(fake_update_response_.cohorthint, value);
1227
1228 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1229 EXPECT_EQ(fake_update_response_.cohortname, value);
1230}
1231
1232TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001233 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1234 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1235 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1236 fake_update_response_.include_cohorts = true;
1237 fake_update_response_.cohort = "s/154454/8479665";
1238 fake_update_response_.cohorthint = "please-put-me-on-beta";
1239 fake_update_response_.cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001240 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001241
Amin Hassani41ac04b2019-03-29 11:31:03 -07001242 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001243
1244 string value;
1245 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1246 EXPECT_EQ(fake_update_response_.cohort, value);
1247
1248 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1249 EXPECT_EQ(fake_update_response_.cohorthint, value);
1250
1251 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1252}
1253
1254TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001255 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001256
Amin Hassani41ac04b2019-03-29 11:31:03 -07001257 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1258 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001259
1260 string value;
1261 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1262 EXPECT_EQ("old_value", value);
1263
1264 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1265 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1266}
1267
Alex Deymo00d79ac2015-06-29 15:41:49 -07001268TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001269 fake_update_response_.include_cohorts = true;
1270 fake_update_response_.cohort = "s/154454/8479665";
1271 fake_update_response_.cohorthint = "please-put-me-on-beta";
1272 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001273 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1274 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1275 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001276
Amin Hassani41ac04b2019-03-29 11:31:03 -07001277 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001278
1279 string value;
1280 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1281 EXPECT_EQ(fake_update_response_.cohort, value);
1282
1283 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1284 EXPECT_EQ(fake_update_response_.cohorthint, value);
1285
1286 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1287 EXPECT_EQ(fake_update_response_.cohortname, value);
1288}
1289
Sen Jiangb1e063a2017-09-15 17:44:31 -07001290TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001291 fake_update_response_.multi_app = true;
1292 fake_update_response_.include_cohorts = true;
1293 fake_update_response_.cohort = "s/154454/8479665";
1294 fake_update_response_.cohorthint = "please-put-me-on-beta";
1295 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001296 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001297
Amin Hassani41ac04b2019-03-29 11:31:03 -07001298 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001299
1300 string value;
1301 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1302 EXPECT_EQ(fake_update_response_.cohort, value);
1303
1304 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1305 EXPECT_EQ(fake_update_response_.cohorthint, value);
1306
1307 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1308 EXPECT_EQ(fake_update_response_.cohortname, value);
1309}
1310
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001311TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001312 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001313 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001314 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001315
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001316 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001317 &fake_system_state_,
1318 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001319 std::make_unique<MockHttpFetcher>(
1320 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001321 false,
1322 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001323 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001324 processor.set_delegate(&delegate_);
1325 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001326
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001327 loop.PostTask(base::Bind(
1328 [](ActionProcessor* processor) { processor->StartProcessing(); },
1329 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001330 loop.Run();
1331 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001332 EXPECT_FALSE(processor.IsRunning());
1333}
1334
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001335TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001336 tuc_params_.http_response = "invalid xml>";
1337 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1338 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1339 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1340
1341 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001342 EXPECT_FALSE(response.update_exists);
1343}
1344
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001345TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001346 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1347 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1348 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1349
1350 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001351 EXPECT_FALSE(response.update_exists);
1352}
1353
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001354TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001355 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001356 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1357 "<daystart elapsed_seconds=\"100\"/>"
1358 "<app appid=\"foo\" status=\"ok\">"
1359 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001360 "<updatecheck/></app></response>";
1361 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1362 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1363 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1364
1365 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001366 EXPECT_FALSE(response.update_exists);
1367}
1368
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001369TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001370 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001371 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1372 "<daystart elapsed_seconds=\"100\"/>"
1373 "<app appid=\"foo\" status=\"ok\">"
1374 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001375 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1376 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1377 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1378 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1379
1380 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001381 EXPECT_FALSE(response.update_exists);
1382}
1383
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001384TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001385 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001386 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1387 "<daystart elapsed_seconds=\"100\"/>"
1388 "<app appid=\"foo\" status=\"ok\">"
1389 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001390 "</app></response>";
1391 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1392 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1393 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1394
1395 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001396 EXPECT_FALSE(response.update_exists);
1397}
1398
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001399TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001400 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001401 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1402 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001403 // the appid needs to match that in the request params
1404 "<app appid=\"" +
1405 fake_update_response_.app_id +
1406 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001407 "<updatecheck status=\"ok\">"
1408 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001409 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001410 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001411 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001412 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001413 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001414 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001415 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001416 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001417
Amin Hassani41ac04b2019-03-29 11:31:03 -07001418 ASSERT_TRUE(TestUpdateCheck());
1419
Darin Petkov6a5b3222010-07-13 14:55:28 -07001420 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001421 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001422 EXPECT_EQ("http://missing/field/test/f",
1423 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001424 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001425 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1426 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001427 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001428 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001429}
1430
1431namespace {
1432class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1433 public:
1434 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001435 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001436 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001437};
1438
Alex Deymo60ca1a72015-06-18 18:19:15 -07001439void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001440 processor->StartProcessing();
1441 CHECK(processor->IsRunning());
1442 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001443}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001444} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001445
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001446TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001447 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001448 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001449
Alex Deymo60ca1a72015-06-18 18:19:15 -07001450 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001451 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001452 &fake_system_state_,
1453 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001454 std::make_unique<MockHttpFetcher>(
1455 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001456 false,
1457 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001458 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001459 ActionProcessor processor;
1460 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001461 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001462
Alex Deymo60ca1a72015-06-18 18:19:15 -07001463 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1464 loop.Run();
1465 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001466}
1467
Alex Deymob0d74eb2015-03-30 17:59:17 -07001468TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001469 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001470 request_params_.set_os_sp("testtheservice_pack>");
1471 request_params_.set_os_board("x86 generic<id");
1472 request_params_.set_current_channel("unittest_track&lt;");
1473 request_params_.set_target_channel("unittest_track&lt;");
1474 request_params_.set_hwid("<OEM MODEL>");
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="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001501}
1502
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001503TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001504 fake_update_response_.deadline = "&lt;20110101";
1505 fake_update_response_.more_info_url = "testthe&lt;url";
1506 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001507 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1508
1509 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001510
Sen Jiang0affc2c2017-02-10 15:55:05 -08001511 EXPECT_EQ("testthe<url", response.more_info_url);
1512 EXPECT_EQ("testthe&codebase/file.signed",
1513 response.packages[0].payload_urls[0]);
1514 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001515}
1516
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001517TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001518 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001519 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001520 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001521
Amin Hassani41ac04b2019-03-29 11:31:03 -07001522 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001523 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001524}
1525
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001526TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001527 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001528 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001529 tuc_params_.http_response = "invalid xml>";
1530 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1531 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1532 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001533
Darin Petkov95508da2011-01-05 12:42:29 -08001534 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001535 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001536 // An existing but empty previous version means that we didn't reboot to a new
1537 // update, therefore, no need to update the previous version.
1538 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001539 ASSERT_FALSE(TestUpdateCheck());
1540
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001541 EXPECT_NE(
1542 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1543 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001544 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001545 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1546 string::npos);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001547 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos);
1548 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001549 // No <event> tag should be sent if we didn't reboot to an update.
1550 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001551}
1552
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001553TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001554 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001555 "invalid xml>");
1556
Alex Vakulenko75039d72014-03-25 12:36:28 -07001557 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001558 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001559 OmahaEvent::kTypeUpdateDownloadStarted,
1560 OmahaEvent::kResultSuccess);
1561 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001562 EXPECT_EQ(post_str.find("ping"), string::npos);
1563 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001564}
1565
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001566TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001567 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001568 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001569 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001570 "invalid xml>");
1571
Alex Vakulenko75039d72014-03-25 12:36:28 -07001572 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001573 " <event eventtype=\"%d\" eventresult=\"%d\" "
1574 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001575 OmahaEvent::kTypeDownloadComplete,
1576 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001577 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001578 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001579 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001580}
1581
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001582TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001583 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001584 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001585 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001586 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001587 std::make_unique<MockHttpFetcher>(
1588 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001589 false,
1590 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001591 EXPECT_FALSE(update_check_action.IsEvent());
1592
1593 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001594 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001595 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001596 std::make_unique<MockHttpFetcher>(
1597 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001598 false,
1599 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001600 EXPECT_TRUE(event_action.IsEvent());
1601}
1602
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001603TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001604 tuc_params_.http_response = "invalid xml>";
1605 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1606 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1607 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1608
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001609 for (int i = 0; i < 2; i++) {
1610 bool delta_okay = i == 1;
1611 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001612 request_params_.set_delta_okay(delta_okay);
1613
Amin Hassani41ac04b2019-03-29 11:31:03 -07001614 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001615 EXPECT_NE(
1616 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1617 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001618 << "i = " << i;
1619 }
1620}
1621
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001622TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001623 tuc_params_.http_response = "invalid xml>";
1624 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1625 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1626 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1627
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001628 for (int i = 0; i < 2; i++) {
1629 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001630 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001631 request_params_.set_interactive(interactive);
1632
Amin Hassani41ac04b2019-03-29 11:31:03 -07001633 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001634 EXPECT_NE(post_str.find(
1635 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001636 string::npos)
1637 << "i = " << i;
1638 }
1639}
1640
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001641TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001642 tuc_params_.http_response = "invalid xml>";
1643 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1644 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1645 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1646
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001647 for (int i = 0; i < 2; i++) {
1648 bool target_version_set = i == 1;
1649 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001650 request_params_.set_target_version_prefix(target_version_prefix);
1651
Amin Hassani41ac04b2019-03-29 11:31:03 -07001652 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001653 if (target_version_set) {
1654 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1655 string::npos)
1656 << "i = " << i;
1657 } else {
1658 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1659 << "i = " << i;
1660 }
1661 }
1662}
1663
1664TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001665 tuc_params_.http_response = "invalid xml>";
1666 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1667 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1668 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1669
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001670 for (int i = 0; i < 4; i++) {
1671 bool rollback_allowed = i / 2 == 0;
1672 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001673 request_params_.set_target_version_prefix(target_version_set ? "10032."
1674 : "");
1675 request_params_.set_rollback_allowed(rollback_allowed);
1676
Amin Hassani41ac04b2019-03-29 11:31:03 -07001677 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001678 if (rollback_allowed && target_version_set) {
1679 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1680 << "i = " << i;
1681 } else {
1682 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1683 }
1684 }
1685}
1686
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001687TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001688 OmahaEvent default_event;
1689 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1690 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001691 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001692
1693 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1694 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1695 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001696 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001697
1698 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1699 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001700 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001701 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1702 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001703 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001704}
1705
Askar Aitzhan570ca872019-04-24 11:16:12 +02001706TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
Askar Aitzhan18fff842019-06-21 23:24:37 +02001707 // If DeviceQuickFixBuildToken value is set it takes precedence over pref
1708 // value.
1709 constexpr char autoupdate_token[] = "autoupdate_token>";
1710 constexpr char xml_encoded_autoupdate_token[] = "autoupdate_token&gt;";
1711 constexpr char omaha_cohort_hint[] = "cohort_hint";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001712
1713 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1714 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1715 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1716 request_params_.set_autoupdate_token(autoupdate_token);
Askar Aitzhan18fff842019-06-21 23:24:37 +02001717 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan570ca872019-04-24 11:16:12 +02001718
1719 ASSERT_TRUE(TestUpdateCheck());
1720
Askar Aitzhan18fff842019-06-21 23:24:37 +02001721 EXPECT_NE(string::npos,
1722 post_str.find("cohorthint=\"" +
1723 string(xml_encoded_autoupdate_token) + "\""));
1724 EXPECT_EQ(string::npos, post_str.find(autoupdate_token));
1725 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
1726}
1727
1728TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsNotSetTest) {
1729 // If DeviceQuickFixBuildToken is not set, pref value will be provided in
1730 // cohorthint attribute.
1731 constexpr char omaha_cohort_hint[] = "evil_string>";
1732 constexpr char xml_encoded_cohort_hint[] = "evil_string&gt;";
1733
1734 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1735 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1736 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1737 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
1738
1739 ASSERT_TRUE(TestUpdateCheck());
1740
1741 EXPECT_NE(
1742 string::npos,
1743 post_str.find("cohorthint=\"" + string(xml_encoded_cohort_hint) + "\""));
1744 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001745}
1746
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001747void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001748 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001749 fake_system_state_.set_prefs(&prefs);
1750 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001751 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001752 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1753 // Add a few hours to the day difference to test no rounding, etc.
1754 int64_t five_days_ago =
1755 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1756 int64_t six_days_ago =
1757 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1758 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001759 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001760 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001761 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001762 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001763 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001764
1765 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1766 tuc_params_.ping_only = ping_only;
1767 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1768 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1769
1770 ASSERT_TRUE(TestUpdateCheck());
1771
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001772 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1773 string::npos);
1774 if (ping_only) {
1775 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1776 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1777 } else {
1778 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1779 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001780 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001781}
1782
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001783TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001784 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001785}
1786
1787TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001788 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001789}
1790
1791TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001792 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001793 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001794 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001795 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001796 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001797 int64_t three_days_ago =
1798 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1799 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001800 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001801 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001802 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001803 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001804 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001805 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001806
1807 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1808 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1809 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1810
1811 ASSERT_TRUE(TestUpdateCheck());
1812
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001813 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001814}
1815
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001816TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001817 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001818 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001819 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001820 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001821 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001822 int64_t four_days_ago =
1823 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1824 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001825 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001826 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001827 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001828 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001829 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001830 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001831
1832 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1833 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1834 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1835
1836 ASSERT_TRUE(TestUpdateCheck());
1837
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001838 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001839 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001840}
1841
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001842TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001843 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001844 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001845 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001846 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001847 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001848 int64_t one_hour_ago =
1849 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001850 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001851 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001852 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001853 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001854 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001855 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001856 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1857 // send a ping.
1858 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1859 .WillOnce(Return(true));
1860 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1861 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001862
1863 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1864 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1865 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1866
1867 ASSERT_TRUE(TestUpdateCheck());
1868
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001869 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001870}
1871
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001872TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001873 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001874 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001875 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001876 int64_t now = Time::Now().ToInternalValue();
1877 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001878 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001879 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001880 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001881 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1882 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001883
1884 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1885 tuc_params_.ping_only = true;
1886 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
1887 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1888
1889 EXPECT_TRUE(TestUpdateCheck());
1890 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07001891}
1892
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001893TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001894 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001895 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001896 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001897 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001898 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001899 int64_t future =
1900 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001901 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001902 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001903 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001904 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001905 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001906 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001907 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1908 .WillOnce(Return(true));
1909 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1910 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001911
1912 tuc_params_.http_response =
1913 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1914 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
1915 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1916 "<updatecheck status=\"noupdate\"/></app></response>";
1917 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1918 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1919
1920 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001921 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001922}
1923
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001924TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001925 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001926 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001927 // may fail if it runs for longer than 5 seconds. It shouldn't run
1928 // that long though.
1929 int64_t midnight =
1930 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1931 int64_t midnight_slack =
1932 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001933 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001934 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001935 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1936 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001937 EXPECT_CALL(prefs,
1938 SetInt64(kPrefsLastActivePingDay,
1939 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001940 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001941 EXPECT_CALL(prefs,
1942 SetInt64(kPrefsLastRollCallPingDay,
1943 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001944 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001945
1946 tuc_params_.http_response =
1947 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1948 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
1949 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1950 "<updatecheck status=\"noupdate\"/></app></response>";
1951 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1952 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1953
1954 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001955}
1956
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001957TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001958 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001959 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001960 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1961 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001962 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1963 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001964
1965 tuc_params_.http_response =
1966 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1967 "protocol=\"3.0\"><daystart blah=\"200\"/>"
1968 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1969 "<updatecheck status=\"noupdate\"/></app></response>";
1970 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1971 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1972
1973 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001974}
1975
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001976TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001977 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001978 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001979 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1980 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001981 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1982 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001983
1984 tuc_params_.http_response =
1985 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1986 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
1987 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1988 "<updatecheck status=\"noupdate\"/></app></response>";
1989 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1990 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1991
1992 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001993}
1994
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001995TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001996 tuc_params_.http_response = "invalid xml>";
1997 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1998 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1999 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2000
2001 ASSERT_FALSE(TestUpdateCheck());
2002
Darin Petkov84c763c2010-07-29 16:27:58 -07002003 EXPECT_EQ(post_str.find("machineid="), string::npos);
2004 EXPECT_EQ(post_str.find("userid="), string::npos);
2005}
2006
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002007TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002008 const int http_error_code =
2009 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002010 tuc_params_.fail_http_response_code = 501;
2011 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2012 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2013 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2014 tuc_params_.expected_download_error_code =
2015 static_cast<metrics::DownloadErrorCode>(501);
2016
2017 ASSERT_FALSE(TestUpdateCheck());
2018
Darin Petkovedc522e2010-11-05 09:35:17 -07002019 EXPECT_FALSE(response.update_exists);
2020}
2021
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002022TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002023 const int http_error_code =
2024 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002025
2026 tuc_params_.fail_http_response_code = 1500;
2027 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2028 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2029 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2030 tuc_params_.expected_download_error_code =
2031 metrics::DownloadErrorCode::kHttpStatusOther;
2032
2033 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002034 EXPECT_FALSE(response.update_exists);
2035}
2036
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002037TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002038 request_params_.set_wall_clock_based_wait_enabled(true);
2039 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2040 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002041
Sen Jiang7c1171e2016-06-23 11:35:40 -07002042 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002043 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002044 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002045
2046 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2047 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2048 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2049
2050 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002051
Ben Chan9abb7632014-08-07 00:10:53 -07002052 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002053 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002054 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002055 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002056
2057 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002058 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002059 tuc_params_.expected_code = ErrorCode::kSuccess;
2060 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2061
2062 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002063 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002064}
2065
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002066TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002067 request_params_.set_wall_clock_based_wait_enabled(true);
2068 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2069 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002070
Sen Jiang7c1171e2016-06-23 11:35:40 -07002071 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002072 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2073 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002074 ASSERT_TRUE(
2075 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2076 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002077
Amin Hassani41ac04b2019-03-29 11:31:03 -07002078 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2079
2080 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002081 EXPECT_TRUE(response.update_exists);
2082
2083 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002084 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002085 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002086 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2087}
2088
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002089TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002090 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002091 base::ScopedTempDir tempdir;
2092 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002093
Marton Hunyady2abda312018-04-24 18:21:49 +02002094 request_params_.set_root(tempdir.GetPath().value());
2095 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2096 request_params_.set_app_version("1.2.3.4");
2097 request_params_.set_product_components("o.bundle=1");
2098 request_params_.set_current_channel("canary-channel");
2099 EXPECT_TRUE(
2100 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2101 request_params_.UpdateDownloadChannel();
2102 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002103
2104 tuc_params_.http_response = "invalid xml>";
2105 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2106 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2107 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2108
2109 ASSERT_FALSE(TestUpdateCheck());
2110
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002111 EXPECT_NE(
2112 string::npos,
2113 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2114 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2115 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002116 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002117}
2118
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002119TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002120 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002121 base::ScopedTempDir tempdir;
2122 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002123
Marton Hunyady2abda312018-04-24 18:21:49 +02002124 request_params_.set_root(tempdir.GetPath().value());
2125 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2126 request_params_.set_app_version("5.6.7.8");
2127 request_params_.set_product_components("o.bundle=1");
2128 request_params_.set_current_channel("stable-channel");
2129 EXPECT_TRUE(
2130 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2131 request_params_.UpdateDownloadChannel();
2132 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002133
2134 tuc_params_.http_response = "invalid xml>";
2135 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2136 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2137 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2138
2139 ASSERT_FALSE(TestUpdateCheck());
2140
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002141 EXPECT_NE(
2142 string::npos,
2143 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2144 "version=\"5.6.7.8\" "
2145 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002146 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002147 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002148}
2149
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002150// Checks that the initial ping with a=-1 r=-1 is not send when the device
2151// was powerwashed.
2152TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2153 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2154
2155 // Flag that the device was powerwashed in the past.
2156 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002157 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2158 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2159 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002160
Amin Hassani41ac04b2019-03-29 11:31:03 -07002161 ASSERT_TRUE(TestUpdateCheck());
2162
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002163 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002164 EXPECT_EQ(string::npos, post_str.find("<ping"));
2165}
2166
Amin Hassani1677e812017-06-21 13:36:36 -07002167// Checks that the initial ping with a=-1 r=-1 is not send when the device
2168// first_active_omaha_ping_sent is set.
2169TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2170 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2171
2172 // Flag that the device was not powerwashed in the past.
2173 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2174
2175 // Flag that the device has sent first active ping in the past.
2176 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2177
Amin Hassani41ac04b2019-03-29 11:31:03 -07002178 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2179 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2180 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2181
2182 ASSERT_TRUE(TestUpdateCheck());
2183
Amin Hassani1677e812017-06-21 13:36:36 -07002184 // We shouldn't send a ping in this case since
2185 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002186 EXPECT_EQ(string::npos, post_str.find("<ping"));
2187}
2188
Alex Deymo9fded1e2015-11-05 12:31:19 -08002189// Checks that the event 54 is sent on a reboot to a new update.
2190TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2191 // Flag that the device was updated in a previous boot.
2192 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2193
Amin Hassani41ac04b2019-03-29 11:31:03 -07002194 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2195 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2196 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2197
2198 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002199
2200 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002201 EXPECT_NE(
2202 string::npos,
2203 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2204 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002205 EXPECT_NE(string::npos,
2206 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2207
2208 // The previous version flag should have been removed.
2209 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2210 string prev_version;
2211 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2212 EXPECT_TRUE(prev_version.empty());
2213}
2214
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002215void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2216 bool initial_allow_p2p_for_sharing,
2217 bool omaha_disable_p2p_for_downloading,
2218 bool omaha_disable_p2p_for_sharing,
2219 bool payload_state_allow_p2p_attempt,
2220 bool expect_p2p_client_lookup,
2221 const string& p2p_client_result_url,
2222 bool expected_allow_p2p_for_downloading,
2223 bool expected_allow_p2p_for_sharing,
2224 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002225 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2226 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2227 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002228
2229 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002230 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002231 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2232 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002233 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2234 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2235 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2236 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2237 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2238 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2239 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2240 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2241 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2242 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2243
David Zeuthen8f191b22013-08-06 12:27:50 -07002244 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002245 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002246 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2247
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002248 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2249 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002250 .Times(expect_p2p_client_lookup ? 1 : 0);
2251
Alex Deymo8e18f932015-03-27 16:16:59 -07002252 fake_update_response_.disable_p2p_for_downloading =
2253 omaha_disable_p2p_for_downloading;
2254 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002255
2256 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2257 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2258
2259 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002260 EXPECT_TRUE(response.update_exists);
2261
Gilad Arnold74b5f552014-10-07 08:17:16 -07002262 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2263 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002264 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002265
Gilad Arnold74b5f552014-10-07 08:17:16 -07002266 EXPECT_EQ(expected_allow_p2p_for_downloading,
2267 actual_allow_p2p_for_downloading);
2268 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2269 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002270}
2271
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002272TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002273 P2PTest(true, // initial_allow_p2p_for_downloading
2274 true, // initial_allow_p2p_for_sharing
2275 false, // omaha_disable_p2p_for_downloading
2276 false, // omaha_disable_p2p_for_sharing
2277 true, // payload_state_allow_p2p_attempt
2278 true, // expect_p2p_client_lookup
2279 "http://1.3.5.7/p2p", // p2p_client_result_url
2280 true, // expected_allow_p2p_for_downloading
2281 true, // expected_allow_p2p_for_sharing
2282 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002283}
2284
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002285TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002286 P2PTest(true, // initial_allow_p2p_for_downloading
2287 true, // initial_allow_p2p_for_sharing
2288 false, // omaha_disable_p2p_for_downloading
2289 false, // omaha_disable_p2p_for_sharing
2290 true, // payload_state_allow_p2p_attempt
2291 true, // expect_p2p_client_lookup
2292 "", // p2p_client_result_url
2293 false, // expected_allow_p2p_for_downloading
2294 true, // expected_allow_p2p_for_sharing
2295 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002296}
2297
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002298TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002299 P2PTest(false, // initial_allow_p2p_for_downloading
2300 true, // initial_allow_p2p_for_sharing
2301 false, // omaha_disable_p2p_for_downloading
2302 false, // omaha_disable_p2p_for_sharing
2303 true, // payload_state_allow_p2p_attempt
2304 false, // expect_p2p_client_lookup
2305 "unset", // p2p_client_result_url
2306 false, // expected_allow_p2p_for_downloading
2307 true, // expected_allow_p2p_for_sharing
2308 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002309}
2310
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002311TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002312 P2PTest(true, // initial_allow_p2p_for_downloading
2313 true, // initial_allow_p2p_for_sharing
2314 true, // omaha_disable_p2p_for_downloading
2315 false, // omaha_disable_p2p_for_sharing
2316 true, // payload_state_allow_p2p_attempt
2317 false, // expect_p2p_client_lookup
2318 "unset", // p2p_client_result_url
2319 false, // expected_allow_p2p_for_downloading
2320 true, // expected_allow_p2p_for_sharing
2321 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002322}
2323
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002324TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002325 P2PTest(true, // initial_allow_p2p_for_downloading
2326 true, // initial_allow_p2p_for_sharing
2327 false, // omaha_disable_p2p_for_downloading
2328 true, // omaha_disable_p2p_for_sharing
2329 true, // payload_state_allow_p2p_attempt
2330 true, // expect_p2p_client_lookup
2331 "http://1.3.5.7/p2p", // p2p_client_result_url
2332 true, // expected_allow_p2p_for_downloading
2333 false, // expected_allow_p2p_for_sharing
2334 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002335}
2336
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002337TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002338 P2PTest(true, // initial_allow_p2p_for_downloading
2339 true, // initial_allow_p2p_for_sharing
2340 true, // omaha_disable_p2p_for_downloading
2341 true, // omaha_disable_p2p_for_sharing
2342 true, // payload_state_allow_p2p_attempt
2343 false, // expect_p2p_client_lookup
2344 "unset", // p2p_client_result_url
2345 false, // expected_allow_p2p_for_downloading
2346 false, // expected_allow_p2p_for_sharing
2347 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002348}
2349
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002350bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2351 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002352 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002353 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2354
2355 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002356}
2357
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002358TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002359 // Simulate a successful update check that happens during OOBE. The
2360 // deadline in the response is needed to force the update attempt to
2361 // occur; responses without a deadline seen during OOBE will normally
2362 // return ErrorCode::kNonCriticalUpdateInOOBE.
2363 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2364 fake_update_response_.deadline = "20101020";
2365
David Zeuthen639aa362014-02-03 16:23:44 -08002366 // Check that we parse elapsed_days in the Omaha Response correctly.
2367 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002368 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2369 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002370 EXPECT_TRUE(response.update_exists);
2371 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002372 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002373 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002374 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002375 EXPECT_EQ(prefs_days, 42);
2376
2377 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002378 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002379 EXPECT_TRUE(response.update_exists);
2380 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002381 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002382 EXPECT_EQ(prefs_days, 42);
2383
2384 // Note that elapsed_days is not necessarily divisible by 7 so check
2385 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002386 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2387 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002388 EXPECT_TRUE(response.update_exists);
2389 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002390 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002391 EXPECT_EQ(prefs_days, 21);
2392
2393 // Check that we correctly handle elapsed_days not being included in
2394 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002395 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002396 EXPECT_TRUE(response.update_exists);
2397 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002398}
2399
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002400// If there is no prefs and OOBE is not complete, we should not
2401// report anything to Omaha.
2402TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002403 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002404 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2405 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2406}
David Zeuthen639aa362014-02-03 16:23:44 -08002407
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002408// If OOBE is complete and happened on a valid date (e.g. after Jan
2409// 1 2007 0:00 PST), that date should be used and written to
2410// prefs. However, first try with an invalid date and check we do
2411// nothing.
2412TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2413 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2414 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2415 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2416 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2417}
David Zeuthen639aa362014-02-03 16:23:44 -08002418
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002419// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2420// should yield an InstallDate of 14.
2421TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2422 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2423 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2424 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2425 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002426
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002427 int64_t prefs_days;
2428 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2429 EXPECT_EQ(prefs_days, 14);
2430}
David Zeuthen639aa362014-02-03 16:23:44 -08002431
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002432// Now that we have a valid date in prefs, check that we keep using
2433// that even if OOBE date reports something else. The date Jan 30,
2434// 2007 0:00 PST should yield an InstallDate of 28... but since
2435// there's a prefs file, we should still get 14.
2436TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2437 // Set a valid date in the prefs first.
2438 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002439
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002440 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2441 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2442 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002443
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002444 int64_t prefs_days;
2445 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2446 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002447
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002448 // If we delete the prefs file, we should get 28 days.
2449 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2450 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2451 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2452 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002453}
2454
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002455// Verifies that a device with no device policy, and is not a consumer
2456// device sets the max kernel key version to the current version.
2457// ie. the same behavior as if rollback is enabled.
2458TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2459 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2460
2461 // Setup and verify some initial default values for the kernel TPM
2462 // values that control verified boot and rollback.
2463 const int min_kernel_version = 4;
2464 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2465 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2466 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2467 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2468
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002469 EXPECT_CALL(
2470 *fake_system_state_.mock_metrics_reporter(),
2471 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2472 .Times(1);
2473
Amin Hassani41ac04b2019-03-29 11:31:03 -07002474 fake_update_response_.deadline = "20101020";
2475 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2476 tuc_params_.is_consumer_device = false;
2477 tuc_params_.rollback_allowed_milestones = 3;
2478
2479 EXPECT_TRUE(TestUpdateCheck());
2480 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002481
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002482 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002483 // kernel key version. This has the effect of freezing roll
2484 // forwards indefinitely. This will hold the rollback window
2485 // open until a future change will be able to move this forward
2486 // relative the configured window.
2487 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2488 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2489}
2490
2491// Verifies that a conmsumer device with no device policy sets the
2492// max kernel key version to the current version. ie. the same
2493// behavior as if rollback is enabled.
2494TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2495 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2496
2497 // Setup and verify some initial default values for the kernel TPM
2498 // values that control verified boot and rollback.
2499 const int min_kernel_version = 3;
2500 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2501 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2502 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2503 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2504
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002505 EXPECT_CALL(
2506 *fake_system_state_.mock_metrics_reporter(),
2507 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2508 .Times(1);
2509
Amin Hassani41ac04b2019-03-29 11:31:03 -07002510 fake_update_response_.deadline = "20101020";
2511 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2512 tuc_params_.is_consumer_device = true;
2513 tuc_params_.rollback_allowed_milestones = 3;
2514
2515 EXPECT_TRUE(TestUpdateCheck());
2516 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002517
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002518 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002519 // was set to logical infinity. This is the expected behavior for
2520 // consumer devices and matches the existing behavior prior to the
2521 // rollback features.
2522 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2523 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2524}
2525
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002526// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002527// in the TPM to prevent roll forward.
2528TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2529 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2530
2531 // Setup and verify some initial default values for the kernel TPM
2532 // values that control verified boot and rollback.
2533 const int allowed_milestones = 4;
2534 const int min_kernel_version = 3;
2535 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2536 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2537 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2538 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2539
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002540 EXPECT_CALL(
2541 *fake_system_state_.mock_metrics_reporter(),
2542 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2543 .Times(1);
2544
Amin Hassani41ac04b2019-03-29 11:31:03 -07002545 fake_update_response_.deadline = "20101020";
2546 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2547 tuc_params_.is_consumer_device = false;
2548 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2549 tuc_params_.is_policy_loaded = true;
2550
2551 EXPECT_TRUE(TestUpdateCheck());
2552 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002553
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002554 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002555 // was set to the current minimum kernel key version. This has
2556 // the effect of freezing roll forwards indefinitely. This will
2557 // hold the rollback window open until a future change will
2558 // be able to move this forward relative the configured window.
2559 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2560 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2561}
2562
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002563// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002564// in the TPM to logical infinity, to allow roll forward.
2565TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2566 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2567
2568 // Setup and verify some initial default values for the kernel TPM
2569 // values that control verified boot and rollback.
2570 const int allowed_milestones = 0;
2571 const int min_kernel_version = 3;
2572 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2573 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2574 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2575 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2576
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002577 EXPECT_CALL(
2578 *fake_system_state_.mock_metrics_reporter(),
2579 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2580 .Times(1);
2581
Amin Hassani41ac04b2019-03-29 11:31:03 -07002582 fake_update_response_.deadline = "20101020";
2583 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2584 tuc_params_.is_consumer_device = false;
2585 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2586 tuc_params_.is_policy_loaded = true;
2587
2588 EXPECT_TRUE(TestUpdateCheck());
2589 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002590
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002591 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002592 // was set to logical infinity.
2593 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2594 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2595}
2596
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002597TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002598 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002599 fake_update_response_.deadline = "20101020";
2600 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2601 tuc_params_.is_consumer_device = false;
2602 tuc_params_.rollback_allowed_milestones = 4;
2603 tuc_params_.is_policy_loaded = true;
2604
2605 EXPECT_TRUE(TestUpdateCheck());
2606 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002607 EXPECT_TRUE(response.is_rollback);
2608}
2609
2610TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002611 fake_update_response_.rollback_firmware_version = "1.2";
2612 fake_update_response_.rollback_kernel_version = "3.4";
2613 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002614 fake_update_response_.deadline = "20101020";
2615 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2616 tuc_params_.is_consumer_device = false;
2617 tuc_params_.rollback_allowed_milestones = 4;
2618 tuc_params_.is_policy_loaded = true;
2619
2620 EXPECT_TRUE(TestUpdateCheck());
2621 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002622 EXPECT_TRUE(response.is_rollback);
2623 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2624 EXPECT_EQ(2, response.rollback_key_version.firmware);
2625 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2626 EXPECT_EQ(4, response.rollback_key_version.kernel);
2627}
2628
May Lippert60aa3ca2018-08-15 16:55:29 -07002629TEST_F(OmahaRequestActionTest,
2630 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2631 FakeClock fake_clock;
2632 Time now = Time::Now();
2633 fake_clock.SetWallclockTime(now);
2634 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002635 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002636
Amin Hassani41ac04b2019-03-29 11:31:03 -07002637 ASSERT_TRUE(TestUpdateCheck());
2638
May Lippert60aa3ca2018-08-15 16:55:29 -07002639 EXPECT_TRUE(response.update_exists);
2640 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2641
2642 int64_t stored_first_seen_at_time;
2643 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2644 &stored_first_seen_at_time));
2645 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2646}
2647
2648TEST_F(OmahaRequestActionTest,
2649 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2650 FakeClock fake_clock;
2651 Time now = Time::Now();
2652 fake_clock.SetWallclockTime(now);
2653 fake_system_state_.set_clock(&fake_clock);
2654
Amin Hassani41ac04b2019-03-29 11:31:03 -07002655 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2656 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2657 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2658
2659 ASSERT_TRUE(TestUpdateCheck());
2660
May Lippert60aa3ca2018-08-15 16:55:29 -07002661 EXPECT_FALSE(response.update_exists);
2662 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2663}
2664
Xiaochu Liu88d90382018-08-29 16:09:11 -07002665TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002666 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002667 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002668 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2669
2670 ASSERT_TRUE(TestUpdateCheck());
2671
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002672 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) {
Xiaochu Liu88d90382018-08-29 16:09:11 -07002673 EXPECT_NE(string::npos,
Xiaochu Liu6310be62018-10-11 15:09:03 -07002674 post_str.find("appid=\"" + fake_update_response_.app_id + "_" +
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002675 dlc_module_id + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002676 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002677 EXPECT_NE(string::npos,
2678 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2679
2680 // Count number of updatecheck tag in response.
2681 int updatecheck_count = 0;
2682 size_t pos = 0;
2683 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2684 updatecheck_count++;
2685 pos++;
2686 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002687 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002688}
2689
2690TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2691 fake_update_response_.multi_app_skip_updatecheck = true;
2692 fake_update_response_.multi_app_no_update = false;
2693 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002694 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002695 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002696 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2697
2698 ASSERT_TRUE(TestUpdateCheck());
2699
Xiaochu Liu6310be62018-10-11 15:09:03 -07002700 EXPECT_TRUE(response.update_exists);
2701 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002702}
2703
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002704TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002705 fake_update_response_.rollback = true;
2706 fake_update_response_.rollback_allowed_milestones = 4;
2707 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002708 fake_update_response_.deadline = "20101020";
2709 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2710 tuc_params_.is_consumer_device = false;
2711 tuc_params_.rollback_allowed_milestones = 4;
2712 tuc_params_.is_policy_loaded = true;
2713
2714 EXPECT_TRUE(TestUpdateCheck());
2715 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002716 EXPECT_TRUE(response.is_rollback);
2717 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2718 response.past_rollback_key_version.firmware_key);
2719 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2720 response.past_rollback_key_version.firmware);
2721 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2722 response.past_rollback_key_version.kernel_key);
2723 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2724 response.past_rollback_key_version.kernel);
2725}
2726
2727TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002728 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002729 fake_update_response_.rollback = true;
2730 fake_update_response_.rollback_allowed_milestones = 4;
2731 fake_update_response_.rollback_firmware_version = "4.3";
2732 fake_update_response_.rollback_kernel_version = "2.1";
2733 fake_update_response_.past_rollback_key_version =
2734 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002735 fake_update_response_.deadline = "20101020";
2736 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2737 tuc_params_.is_consumer_device = false;
2738 tuc_params_.rollback_allowed_milestones = 4;
2739 tuc_params_.is_policy_loaded = true;
2740
2741 EXPECT_TRUE(TestUpdateCheck());
2742 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002743 EXPECT_TRUE(response.is_rollback);
2744 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2745 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2746 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2747 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
2748}
2749
2750TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002751 fake_update_response_.rollback = true;
2752 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002753 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002754 request_params_.set_rollback_allowed_milestones(4);
2755
2756 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
2757 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
2758 // to 2, OmahaRequestAction will look for the key values of N-4 version but
2759 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07002760 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2761 tuc_params_.is_consumer_device = false;
2762 tuc_params_.rollback_allowed_milestones = 2;
2763 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002764
Amin Hassani41ac04b2019-03-29 11:31:03 -07002765 EXPECT_TRUE(TestUpdateCheck());
2766 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002767 EXPECT_TRUE(response.is_rollback);
2768 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2769 response.past_rollback_key_version.firmware_key);
2770 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2771 response.past_rollback_key_version.firmware);
2772 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2773 response.past_rollback_key_version.kernel_key);
2774 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2775 response.past_rollback_key_version.kernel);
2776}
2777
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04002778TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
2779 request_params_.set_device_requisition("remora");
2780 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2781 ASSERT_TRUE(TestUpdateCheck());
2782 EXPECT_NE(string::npos, post_str.find("requisition=\"remora\""));
2783}
2784
2785TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
2786 request_params_.set_device_requisition("");
2787 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2788 ASSERT_TRUE(TestUpdateCheck());
2789 EXPECT_EQ(string::npos, post_str.find("requisition"));
2790}
2791
Amin Hassani2cbb0692019-10-30 13:36:17 -07002792TEST_F(OmahaRequestActionTest, PersistEolDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002793 tuc_params_.http_response =
2794 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2795 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2796 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07002797 "_eol_date=\"200\" _foo=\"bar\"/></app></response>";
2798 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2799 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2800
2801 ASSERT_TRUE(TestUpdateCheck());
2802
2803 string eol_date;
2804 EXPECT_TRUE(
2805 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
2806 EXPECT_EQ("200", eol_date);
2807}
2808
2809TEST_F(OmahaRequestActionTest, PersistEolMissingDateTest) {
2810 tuc_params_.http_response =
2811 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2812 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2813 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002814 "_foo=\"bar\"/></app></response>";
2815 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2816 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2817
2818 ASSERT_TRUE(TestUpdateCheck());
2819
Amin Hassani2cbb0692019-10-30 13:36:17 -07002820 string eol_date;
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002821 EXPECT_FALSE(
2822 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
2823}
2824
Amin Hassani2cbb0692019-10-30 13:36:17 -07002825TEST_F(OmahaRequestActionTest, PersistEolBadDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002826 tuc_params_.http_response =
2827 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2828 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2829 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07002830 "_eol_date=\"bad\" foo=\"bar\"/></app></response>";
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002831 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2832 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2833
2834 ASSERT_TRUE(TestUpdateCheck());
2835
Amin Hassani2cbb0692019-10-30 13:36:17 -07002836 string eol_date;
Jae Hoon Kim051627a2019-09-03 12:56:32 -07002837 EXPECT_TRUE(
2838 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
2839 EXPECT_EQ(kEolDateInvalid, StringToEolDate(eol_date));
2840}
2841
Darin Petkov6a5b3222010-07-13 14:55:28 -07002842} // namespace chromeos_update_engine