blob: 01be1a82a6f92f13e41308312c042fc60d654c3f [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
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/omaha_request_action.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080018
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>
Andrewe045aef2020-01-08 16:29:22 -080038#include <expat.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070039#include <gtest/gtest.h>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080040#include <policy/libpolicy.h>
41#include <policy/mock_libpolicy.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070042
Alex Deymo39910dc2015-11-09 17:04:30 -080043#include "update_engine/common/action_pipe.h"
44#include "update_engine/common/constants.h"
45#include "update_engine/common/fake_prefs.h"
46#include "update_engine/common/hash_calculator.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070047#include "update_engine/common/metrics_reporter_interface.h"
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070048#include "update_engine/common/mock_excluder.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080049#include "update_engine/common/mock_http_fetcher.h"
50#include "update_engine/common/platform_constants.h"
51#include "update_engine/common/prefs.h"
52#include "update_engine/common/test_utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070053#include "update_engine/cros/fake_system_state.h"
54#include "update_engine/cros/mock_connection_manager.h"
55#include "update_engine/cros/mock_payload_state.h"
56#include "update_engine/cros/omaha_request_builder_xml.h"
57#include "update_engine/cros/omaha_request_params.h"
58#include "update_engine/cros/omaha_utils.h"
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080059#include "update_engine/update_manager/rollback_prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070060
Darin Petkov1cbd78f2010-07-29 12:38:34 -070061using base::Time;
62using base::TimeDelta;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080063using chromeos_update_manager::kRollforwardInfinity;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070064using std::pair;
Darin Petkov6a5b3222010-07-13 14:55:28 -070065using std::string;
66using std::vector;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080067using testing::_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070068using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070069using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070070using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070071using testing::Ge;
72using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080073using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070074using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070075using testing::ReturnPointee;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080076using testing::ReturnRef;
Gilad Arnold74b5f552014-10-07 08:17:16 -070077using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070078using testing::SetArgPointee;
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070079using testing::StrictMock;
Darin Petkov6a5b3222010-07-13 14:55:28 -070080
Alex Deymo8e18f932015-03-27 16:16:59 -070081namespace {
82
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080083static_assert(kRollforwardInfinity == 0xfffffffe,
84 "Don't change the value of kRollforward infinity unless its "
85 "size has been changed in firmware.");
86
Xiaochu Liu6310be62018-10-11 15:09:03 -070087const char kCurrentVersion[] = "0.1.0.0";
Alex Deymo85616652015-10-15 18:48:31 -070088const char kTestAppId[] = "test-app-id";
Aaron Wood7dcdedf2017-09-06 17:17:41 -070089const char kTestAppId2[] = "test-app2-id";
Xiaochu Liu6310be62018-10-11 15:09:03 -070090const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck";
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -080091const char kDlcId1[] = "dlc-id-1";
92const char kDlcId2[] = "dlc-id-2";
Alex Deymo85616652015-10-15 18:48:31 -070093
Alex Deymo8e18f932015-03-27 16:16:59 -070094// This is a helper struct to allow unit tests build an update response with the
95// values they care about.
96struct FakeUpdateResponse {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070097 string GetRollbackVersionAttributes() const {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070098 string num_milestones;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070099 num_milestones = base::NumberToString(rollback_allowed_milestones);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700100 const string rollback_version =
101 " _firmware_version_" + num_milestones + "=\"" +
102 past_rollback_key_version.first + "\"" + " _kernel_version_" +
103 num_milestones + "=\"" + past_rollback_key_version.second + "\"";
104
105 return (rollback ? " _rollback=\"true\"" : "") + rollback_version +
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700106 (!rollback_firmware_version.empty()
107 ? " _firmware_version=\"" + rollback_firmware_version + "\""
108 : "") +
109 (!rollback_kernel_version.empty()
110 ? " _kernel_version=\"" + rollback_kernel_version + "\""
111 : "");
112 }
113
Alex Deymo8e18f932015-03-27 16:16:59 -0700114 string GetNoUpdateResponse() const {
115 string entity_str;
116 if (include_entity)
117 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Sen Jiang81259682017-03-30 15:11:30 -0700118 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str +
119 "<response protocol=\"3.0\">"
120 "<daystart elapsed_seconds=\"100\"/>"
121 "<app appid=\"" +
122 app_id + "\" " +
123 (include_cohorts
124 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
125 "\" cohortname=\"" + cohortname + "\" "
126 : "") +
127 " status=\"ok\">"
128 "<ping status=\"ok\"/>"
129 "<updatecheck status=\"noupdate\"/></app>" +
130 (multi_app_no_update
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700131 ? "<app appid=\"" + app_id2 +
132 "\"><updatecheck status=\"noupdate\"/></app>"
Sen Jiang81259682017-03-30 15:11:30 -0700133 : "") +
134 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700135 }
136
137 string GetUpdateResponse() const {
Amin Hassani538bd592020-11-04 20:46:08 -0800138 chromeos_update_engine::OmahaRequestParams request_params;
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800139 request_params.set_app_id(app_id);
Sen Jiang2703ef42017-03-16 13:36:21 -0700140 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
141 "protocol=\"3.0\">"
142 "<daystart elapsed_seconds=\"100\"" +
143 (elapsed_days.empty() ? ""
144 : (" elapsed_days=\"" + elapsed_days + "\"")) +
145 "/>"
146 "<app appid=\"" +
147 app_id + "\" " +
148 (include_cohorts
149 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
150 "\" cohortname=\"" + cohortname + "\" "
151 : "") +
152 " status=\"ok\">"
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700153 "<ping status=\"ok\"/><updatecheck status=\"ok\"" +
154 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" +
Sen Jiang2703ef42017-03-16 13:36:21 -0700155 codebase +
156 "\"/></urls>"
157 "<manifest version=\"" +
158 version +
159 "\">"
160 "<packages><package hash=\"not-used\" name=\"" +
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800161 filename + "\" size=\"" + base::NumberToString(size) + "\" fp=\"" +
162 fp + "\" hash_sha256=\"" + hash + "\"/>" +
163 (multi_package ? "<package name=\"package2\" size=\"222\" fp=\"" +
164 fp2 + "\" hash_sha256=\"hash2\"/>"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800165 : "") +
166 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700167 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700168 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
169 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700170 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700171 "IsDeltaPayload=\"true" +
172 (multi_package ? ":false" : "") +
173 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700174 "MaxDaysToScatter=\"" +
175 max_days_to_scatter +
176 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700177 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700178 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
179 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
180 : "") +
181 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700182 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700183 "/></actions></manifest></updatecheck></app>" +
184 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700185 ? "<app appid=\"" + app_id2 + "\"" +
186 (include_cohorts ? " cohort=\"cohort2\"" : "") +
187 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700188 codebase2 + "\"/></urls><manifest version=\"" + version2 +
189 "\"><packages>"
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800190 "<package name=\"package3\" size=\"333\" fp=\"" +
191 fp2 +
192 "\" hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700193 "<actions><action event=\"postinstall\" " +
194 (multi_app_self_update
195 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
196 : "IsDeltaPayload=\"false\" ") +
197 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700198 "</manifest></updatecheck></app>"
199 : "") +
200 (multi_app_no_update
201 ? "<app><updatecheck status=\"noupdate\"/></app>"
202 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700203 (multi_app_skip_updatecheck
204 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
205 : "") +
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800206 (dlc_app_update
207 ? "<app appid=\"" + request_params.GetDlcAppId(kDlcId1) +
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800208 "\" " +
209 (include_dlc_cohorts
210 ? "cohort=\"" + dlc_cohort + "\" cohorthint=\"" +
211 dlc_cohorthint + "\" cohortname=\"" +
212 dlc_cohortname + "\" "
213 : "") +
214 "status=\"ok\">"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800215 "<updatecheck status=\"ok\"><urls><url codebase=\"" +
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700216 codebase + "\"/><url codebase=\"" + codebase2 +
217 "\"/></urls><manifest version=\"" + version +
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800218 "\"><packages><package name=\"package3\" size=\"333\" "
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800219 "fp=\"" +
220 fp2 +
221 "\" hash_sha256=\"hash3\"/></packages>"
222 "<actions><action event=\"install\" run=\".signed\"/>"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800223 "<action event=\"postinstall\" MetadataSize=\"33\"/>"
224 "</actions></manifest></updatecheck></app>"
225 : "") +
226 (dlc_app_no_update
227 ? "<app appid=\"" + request_params.GetDlcAppId(kDlcId2) +
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800228 +"\" " +
229 (include_dlc_cohorts
230 ? "cohort=\"" + dlc_cohort + "\" cohorthint=\"" +
231 dlc_cohorthint + "\" cohortname=\"" +
232 dlc_cohortname + "\" "
233 : "") +
234 "><updatecheck status=\"noupdate\"/></app>"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800235 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700236 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700237 }
238
239 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800240 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700241
Alex Deymo85616652015-10-15 18:48:31 -0700242 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700243 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700244 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
Alex Deymo8e18f932015-03-27 16:16:59 -0700245 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700246 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700247 string more_info_url = "http://more/info";
248 string prompt = "true";
249 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700250 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700251 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700252 string hash = "4841534831323334";
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800253 string fp = "3.98ba213e";
254 string fp2 = "3.755aff78e";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800255 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700256 string deadline = "";
257 string max_days_to_scatter = "7";
258 string elapsed_days = "42";
259
260 // P2P setting defaults to allowed.
261 bool disable_p2p_for_downloading = false;
262 bool disable_p2p_for_sharing = false;
263
Sen Jiangfe284402018-03-21 14:03:50 -0700264 bool powerwash = false;
265
Alex Deymo8e18f932015-03-27 16:16:59 -0700266 // Omaha cohorts settings.
267 bool include_cohorts = false;
268 string cohort = "";
269 string cohorthint = "";
270 string cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800271 // Whether to include Omaha cohorts for DLC apps.
272 bool include_dlc_cohorts = false;
273 string dlc_cohort = "";
274 string dlc_cohorthint = "";
275 string dlc_cohortname = "";
Alex Deymo8e18f932015-03-27 16:16:59 -0700276
277 // Whether to include the CrOS <!ENTITY> in the XML response.
278 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800279
Sen Jiang81259682017-03-30 15:11:30 -0700280 // Whether to include more than one app.
281 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700282 // Whether to include an app with noupdate="true".
283 bool multi_app_self_update = false;
284 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700285 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700286 // Whether to include an additional app with no updatecheck tag.
287 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700288 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800289 bool multi_package = false;
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800290 // Whether to include a DLC app with updatecheck tag.
291 bool dlc_app_update = false;
292 // Whether to include a DLC app with no updatecheck tag.
293 bool dlc_app_no_update = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700294
295 // Whether the payload is a rollback.
296 bool rollback = false;
297 // The verified boot firmware key version for the rollback image.
298 string rollback_firmware_version = "";
299 // The verified boot kernel key version for the rollback image.
300 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700301 // The number of milestones back that the verified boot key version has been
302 // supplied.
303 uint32_t rollback_allowed_milestones = 0;
304 // The verified boot key version for the
305 // |current - rollback_allowed_milestones| most recent release.
306 // The pair contains <firmware_key_version, kernel_key_version> each
307 // of which is in the form "key_version.version".
308 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700309};
310
311} // namespace
312
Darin Petkov6a5b3222010-07-13 14:55:28 -0700313namespace chromeos_update_engine {
314
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700315class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
316 public:
317 OmahaRequestActionTestProcessorDelegate()
318 : expected_code_(ErrorCode::kSuccess),
319 interactive_(false),
320 test_http_fetcher_headers_(false) {}
321 ~OmahaRequestActionTestProcessorDelegate() override = default;
322
323 void ProcessingDone(const ActionProcessor* processor,
324 ErrorCode code) override {
325 brillo::MessageLoop::current()->BreakLoop();
326 }
327
328 void ActionCompleted(ActionProcessor* processor,
329 AbstractAction* action,
330 ErrorCode code) override {
331 // Make sure actions always succeed.
332 if (action->Type() == OmahaRequestAction::StaticType()) {
333 EXPECT_EQ(expected_code_, code);
334 // Check that the headers were set in the fetcher during the action. Note
335 // that we set this request as "interactive".
336 auto fetcher = static_cast<const MockHttpFetcher*>(
337 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
338
339 if (test_http_fetcher_headers_) {
340 EXPECT_EQ(interactive_ ? "fg" : "bg",
341 fetcher->GetHeader("X-Goog-Update-Interactivity"));
342 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
343 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
344 }
345 post_data_ = fetcher->post_data();
346 } else if (action->Type() ==
347 ObjectCollectorAction<OmahaResponse>::StaticType()) {
348 EXPECT_EQ(ErrorCode::kSuccess, code);
349 auto collector_action =
350 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
351 omaha_response_.reset(new OmahaResponse(collector_action->object()));
352 EXPECT_TRUE(omaha_response_);
353 } else {
354 EXPECT_EQ(ErrorCode::kSuccess, code);
355 }
356 }
357 ErrorCode expected_code_;
358 brillo::Blob post_data_;
359 bool interactive_;
360 bool test_http_fetcher_headers_;
361 std::unique_ptr<OmahaResponse> omaha_response_;
362};
363
Amin Hassani41ac04b2019-03-29 11:31:03 -0700364struct TestUpdateCheckParams {
365 string http_response;
366 int fail_http_response_code;
367 bool ping_only;
368 bool is_consumer_device;
369 int rollback_allowed_milestones;
370 bool is_policy_loaded;
371 ErrorCode expected_code;
372 metrics::CheckResult expected_check_result;
373 metrics::CheckReaction expected_check_reaction;
374 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700375 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700376};
377
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700378class OmahaRequestActionTest : public ::testing::Test {
379 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800380 void SetUp() override {
Amin Hassani538bd592020-11-04 20:46:08 -0800381 FakeSystemState::CreateInstance();
382
Marton Hunyady2abda312018-04-24 18:21:49 +0200383 request_params_.set_os_sp("service_pack");
384 request_params_.set_os_board("x86-generic");
385 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700386 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200387 request_params_.set_app_lang("en-US");
388 request_params_.set_current_channel("unittest");
389 request_params_.set_target_channel("unittest");
390 request_params_.set_hwid("OEM MODEL 09235 7471");
Marton Hunyady2abda312018-04-24 18:21:49 +0200391 request_params_.set_delta_okay(true);
392 request_params_.set_interactive(false);
393 request_params_.set_update_url("http://url");
394 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200395 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200396 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700397 request_params_.set_is_install(false);
Andrewe045aef2020-01-08 16:29:22 -0800398 request_params_.set_dlc_apps_params({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200399
Amin Hassani538bd592020-11-04 20:46:08 -0800400 FakeSystemState::Get()->set_request_params(&request_params_);
Amin Hassani90e9f192020-11-18 14:20:56 -0800401 fake_prefs_ = FakeSystemState::Get()->fake_prefs();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700402
403 // Setting the default update check params. Lookup |TestUpdateCheck()|.
404 tuc_params_ = {
405 .http_response = "",
406 .fail_http_response_code = -1,
407 .ping_only = false,
408 .is_consumer_device = true,
409 .rollback_allowed_milestones = 0,
410 .is_policy_loaded = false,
411 .expected_code = ErrorCode::kSuccess,
412 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
413 .expected_check_reaction = metrics::CheckReaction::kUpdating,
414 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
415 };
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700416
Amin Hassani538bd592020-11-04 20:46:08 -0800417 ON_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700418 .WillByDefault(Return(&mock_excluder_));
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700419 }
420
Andrewe045aef2020-01-08 16:29:22 -0800421 // This function uses the parameters in |tuc_params_| to do an update check.
Amin Hassania859c922020-11-13 15:30:38 -0800422 // It will fill out |post_str_| with the result data and |response| with
Amin Hassani41ac04b2019-03-29 11:31:03 -0700423 // |OmahaResponse|. Returns true iff an output response was obtained from the
424 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
425 // transfer will fail with that code. |ping_only| is passed through to the
426 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700427 //
428 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700429 // |expected_error_code| parameters are for checking expectations about
430 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
431 // statistics. Use the appropriate ::kUnset value to specify that the given
432 // metric should not be reported.
433 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700434
Amin Hassania859c922020-11-13 15:30:38 -0800435 // Tests events using |event| and |https_response|. It will fill up
436 // |post_str_| with the result data.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700437 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700438
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800439 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700440 // only a ping or also an updatecheck.
441 void PingTest(bool ping_only);
442
443 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800444 bool InstallDateParseHelper(const string& elapsed_days,
445 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700446
447 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800448 void P2PTest(bool initial_allow_p2p_for_downloading,
449 bool initial_allow_p2p_for_sharing,
450 bool omaha_disable_p2p_for_downloading,
451 bool omaha_disable_p2p_for_sharing,
452 bool payload_state_allow_p2p_attempt,
453 bool expect_p2p_client_lookup,
454 const string& p2p_client_result_url,
455 bool expected_allow_p2p_for_downloading,
456 bool expected_allow_p2p_for_sharing,
457 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700458
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700459 StrictMock<MockExcluder> mock_excluder_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700460 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200461 // Used by all tests.
Amin Hassani538bd592020-11-04 20:46:08 -0800462 OmahaRequestParams request_params_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700463
Amin Hassani90e9f192020-11-18 14:20:56 -0800464 FakePrefs* fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700465
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700466 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700467
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700468 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700469
470 TestUpdateCheckParams tuc_params_;
471
Amin Hassania859c922020-11-13 15:30:38 -0800472 OmahaResponse response_;
473 string post_str_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700474};
475
Andrew065d78d2020-04-07 15:43:07 -0700476class OmahaRequestActionDlcPingTest : public OmahaRequestActionTest {
477 protected:
478 void SetUp() override {
479 OmahaRequestActionTest::SetUp();
480 dlc_id_ = "dlc0";
481 active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700482 {kDlcPrefsSubDir, dlc_id_, kPrefsPingActive});
Andrew065d78d2020-04-07 15:43:07 -0700483 last_active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700484 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastActive});
Andrew065d78d2020-04-07 15:43:07 -0700485 last_rollcall_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700486 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastRollcall});
Andrew065d78d2020-04-07 15:43:07 -0700487
488 tuc_params_.http_response =
489 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
490 "protocol=\"3.0\"><daystart elapsed_days=\"4763\" "
491 "elapsed_seconds=\"36540\"/><app appid=\"test-app-id\" status=\"ok\">\""
492 "<updatecheck status=\"noupdate\"/></app><app "
493 "appid=\"test-app-id_dlc0\" "
494 "status=\"ok\"><ping status=\"ok\"/><updatecheck status=\"noupdate\"/>"
495 "</app></response>";
496 tuc_params_.expected_check_result =
497 metrics::CheckResult::kNoUpdateAvailable;
498 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
499 }
500
501 std::string dlc_id_;
502 std::string active_key_;
503 std::string last_active_key_;
504 std::string last_rollcall_key_;
505};
Amin Hassani90e9f192020-11-18 14:20:56 -0800506
Amin Hassani41ac04b2019-03-29 11:31:03 -0700507bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700508 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700509 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700510 auto fetcher =
511 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
512 tuc_params_.http_response.size(),
513 nullptr);
514 if (tuc_params_.fail_http_response_code >= 0) {
515 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700516 }
Amin Hassani538bd592020-11-04 20:46:08 -0800517 // This ensures the tests didn't forget to update |FakeSystemState| if they
Amin Hassani61789032020-11-13 16:20:12 -0800518 // are not using the default |request_params_|.
Amin Hassani538bd592020-11-04 20:46:08 -0800519 EXPECT_EQ(&request_params_, FakeSystemState::Get()->request_params());
Marton Hunyady2abda312018-04-24 18:21:49 +0200520
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700521 auto omaha_request_action =
Amin Hassani538bd592020-11-04 20:46:08 -0800522 std::make_unique<OmahaRequestAction>(nullptr,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700523 std::move(fetcher),
524 tuc_params_.ping_only,
525 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700526
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800527 auto mock_policy_provider =
528 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
529 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700530 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800531
532 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700533 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800534
535 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700536 const bool get_allowed_milestone_succeeds =
537 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800538 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700539 .WillRepeatedly(
540 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
541 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800542
543 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
544 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700545 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800546
Amin Hassani41ac04b2019-03-29 11:31:03 -0700547 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700548 delegate_.interactive_ = request_params_.interactive();
549 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700550 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700551 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700552
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700553 auto collector_action =
554 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
555 BondActions(omaha_request_action.get(), collector_action.get());
556 processor.EnqueueAction(std::move(omaha_request_action));
557 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700558
Amin Hassani538bd592020-11-04 20:46:08 -0800559 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
560 ReportUpdateCheckMetrics(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800561 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700562
Amin Hassani41ac04b2019-03-29 11:31:03 -0700563 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -0800564 *FakeSystemState::Get()->mock_metrics_reporter(),
565 ReportUpdateCheckMetrics(tuc_params_.expected_check_result,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700566 tuc_params_.expected_check_reaction,
567 tuc_params_.expected_download_error_code))
568 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800569
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700570 loop.PostTask(base::Bind(
571 [](ActionProcessor* processor) { processor->StartProcessing(); },
572 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700573 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700574 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700575 if (delegate_.omaha_response_)
Amin Hassania859c922020-11-13 15:30:38 -0800576 response_ = *delegate_.omaha_response_;
577 post_str_ = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700578 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700579}
580
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700581// Tests Event requests -- they should always succeed. |out_post_data| may be
582// null; if non-null, the post-data received by the mock HttpFetcher is
583// returned.
584void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700585 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700586 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700587 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700588
589 auto action = std::make_unique<OmahaRequestAction>(
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700590 event,
591 std::make_unique<MockHttpFetcher>(
592 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700593 false,
594 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700595 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700596 processor.set_delegate(&delegate_);
597 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700598
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700599 loop.PostTask(base::Bind(
600 [](ActionProcessor* processor) { processor->StartProcessing(); },
601 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700602 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700603 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700604
Amin Hassania859c922020-11-13 15:30:38 -0800605 post_str_ = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700606}
607
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700608TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700609 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700610 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
611 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
612 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
613 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
614
615 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800616 EXPECT_FALSE(response_.update_exists);
David Zeuthenf3e28012014-08-26 18:23:52 -0400617}
618
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700619TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700620 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
621 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
622 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
623
624 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800625 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700626}
627
Sen Jiang81259682017-03-30 15:11:30 -0700628TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700629 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700630 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
631 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
632 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
633
634 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800635 EXPECT_FALSE(response_.update_exists);
Sen Jiang81259682017-03-30 15:11:30 -0700636}
637
Sen Jiang00adf7b2017-06-26 15:57:29 -0700638TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700639 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700640 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
641 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
642 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
643
644 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800645 EXPECT_FALSE(response_.update_exists);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700646}
647
648TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700649 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700650 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
651 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700652 "</manifest></updatecheck></app></response>";
653 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
654 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
655
656 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800657 EXPECT_FALSE(response_.update_exists);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700658}
659
Alex Deymo8e18f932015-03-27 16:16:59 -0700660// Test that all the values in the response are parsed in a normal update
Amin Hassania859c922020-11-13 15:30:38 -0800661// response_.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700662TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700663 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700664 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
665
666 ASSERT_TRUE(TestUpdateCheck());
667
Amin Hassania859c922020-11-13 15:30:38 -0800668 EXPECT_TRUE(response_.update_exists);
669 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800670 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800671 response_.packages[0].payload_urls[0]);
672 EXPECT_EQ(fake_update_response_.more_info_url, response_.more_info_url);
673 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
674 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
675 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
676 EXPECT_EQ(true, response_.packages[0].is_delta);
677 EXPECT_EQ(fake_update_response_.prompt == "true", response_.prompt);
678 EXPECT_EQ(fake_update_response_.deadline, response_.deadline);
679 EXPECT_FALSE(response_.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700680 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700681 // persisted.
Amin Hassani90e9f192020-11-18 14:20:56 -0800682 EXPECT_FALSE(fake_prefs_->Exists(kPrefsOmahaCohort));
683 EXPECT_FALSE(fake_prefs_->Exists(kPrefsOmahaCohortHint));
684 EXPECT_FALSE(fake_prefs_->Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700685}
686
Sen Jiang0affc2c2017-02-10 15:55:05 -0800687TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800688 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700689 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
690
691 ASSERT_TRUE(TestUpdateCheck());
692
Amin Hassania859c922020-11-13 15:30:38 -0800693 EXPECT_TRUE(response_.update_exists);
694 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800695 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800696 response_.packages[0].payload_urls[0]);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800697 EXPECT_EQ(fake_update_response_.codebase + "package2",
Amin Hassania859c922020-11-13 15:30:38 -0800698 response_.packages[1].payload_urls[0]);
699 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
700 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
701 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
702 EXPECT_EQ(true, response_.packages[0].is_delta);
703 EXPECT_EQ(11u, response_.packages[0].metadata_size);
704 ASSERT_EQ(2u, response_.packages.size());
705 EXPECT_EQ(string("hash2"), response_.packages[1].hash);
706 EXPECT_EQ(222u, response_.packages[1].size);
707 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
708 EXPECT_EQ(22u, response_.packages[1].metadata_size);
709 EXPECT_EQ(false, response_.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700710}
711
712TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700713 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700714 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
715
716 ASSERT_TRUE(TestUpdateCheck());
717
Amin Hassania859c922020-11-13 15:30:38 -0800718 EXPECT_TRUE(response_.update_exists);
719 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang81259682017-03-30 15:11:30 -0700720 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800721 response_.packages[0].payload_urls[0]);
Sen Jiang81259682017-03-30 15:11:30 -0700722 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
Amin Hassania859c922020-11-13 15:30:38 -0800723 response_.packages[1].payload_urls[0]);
724 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
725 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
726 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
727 EXPECT_EQ(11u, response_.packages[0].metadata_size);
728 EXPECT_EQ(true, response_.packages[0].is_delta);
729 ASSERT_EQ(2u, response_.packages.size());
730 EXPECT_EQ(string("hash3"), response_.packages[1].hash);
731 EXPECT_EQ(333u, response_.packages[1].size);
732 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
733 EXPECT_EQ(33u, response_.packages[1].metadata_size);
734 EXPECT_EQ(false, response_.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700735}
736
737TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700738 fake_update_response_.multi_app = true;
739 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700740 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
741
742 ASSERT_TRUE(TestUpdateCheck());
743
Amin Hassania859c922020-11-13 15:30:38 -0800744 EXPECT_TRUE(response_.update_exists);
745 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang81259682017-03-30 15:11:30 -0700746 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800747 response_.packages[0].payload_urls[0]);
748 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
749 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
750 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
751 EXPECT_EQ(11u, response_.packages[0].metadata_size);
752 ASSERT_EQ(2u, response_.packages.size());
753 EXPECT_EQ(string("hash3"), response_.packages[1].hash);
754 EXPECT_EQ(333u, response_.packages[1].size);
755 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
756 EXPECT_EQ(33u, response_.packages[1].metadata_size);
757 EXPECT_EQ(true, response_.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700758}
759
760TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700761 fake_update_response_.multi_app = true;
762 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700763 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
764
765 ASSERT_TRUE(TestUpdateCheck());
766
Amin Hassania859c922020-11-13 15:30:38 -0800767 EXPECT_TRUE(response_.update_exists);
768 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang81259682017-03-30 15:11:30 -0700769 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800770 response_.packages[0].payload_urls[0]);
Sen Jiang81259682017-03-30 15:11:30 -0700771 EXPECT_EQ(fake_update_response_.codebase + "package2",
Amin Hassania859c922020-11-13 15:30:38 -0800772 response_.packages[1].payload_urls[0]);
Sen Jiang81259682017-03-30 15:11:30 -0700773 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
Amin Hassania859c922020-11-13 15:30:38 -0800774 response_.packages[2].payload_urls[0]);
775 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
776 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
777 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
778 EXPECT_EQ(11u, response_.packages[0].metadata_size);
779 EXPECT_EQ(true, response_.packages[0].is_delta);
780 ASSERT_EQ(3u, response_.packages.size());
781 EXPECT_EQ(string("hash2"), response_.packages[1].hash);
782 EXPECT_EQ(222u, response_.packages[1].size);
783 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
784 EXPECT_EQ(22u, response_.packages[1].metadata_size);
785 EXPECT_EQ(false, response_.packages[1].is_delta);
786 EXPECT_EQ(string("hash3"), response_.packages[2].hash);
787 EXPECT_EQ(333u, response_.packages[2].size);
788 EXPECT_EQ(fake_update_response_.fp2, response_.packages[2].fp);
789 EXPECT_EQ(33u, response_.packages[2].metadata_size);
790 EXPECT_EQ(false, response_.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800791}
792
Sen Jiangfe284402018-03-21 14:03:50 -0700793TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700794 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700795 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
796
797 ASSERT_TRUE(TestUpdateCheck());
798
Amin Hassania859c922020-11-13 15:30:38 -0800799 EXPECT_TRUE(response_.update_exists);
800 EXPECT_TRUE(response_.powerwash_required);
Sen Jiangfe284402018-03-21 14:03:50 -0700801}
802
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700803TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700804 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700805 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700806 tuc_params_.http_response = "invalid xml>";
807 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
808 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
809 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
810
811 ASSERT_FALSE(TestUpdateCheck());
812
Amin Hassania859c922020-11-13 15:30:38 -0800813 EXPECT_FALSE(response_.update_exists);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700814}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700815
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700816TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700817 request_params_.set_interactive(false);
818 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700819 tuc_params_.http_response = "invalid xml>";
820 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
821 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
822 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
823
824 ASSERT_FALSE(TestUpdateCheck());
825
Amin Hassania859c922020-11-13 15:30:38 -0800826 EXPECT_FALSE(response_.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700827}
828
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700829TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700830 // Set up a connection manager that doesn't allow a valid update over
831 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700832 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -0800833 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700834
Alex Deymo30534502015-07-20 15:06:33 -0700835 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800836 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
837 SetArgPointee<1>(ConnectionTethering::kUnknown),
838 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700839 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
840 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700841
Amin Hassani41ac04b2019-03-29 11:31:03 -0700842 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
843 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
844 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
845
846 ASSERT_FALSE(TestUpdateCheck());
847
Amin Hassania859c922020-11-13 15:30:38 -0800848 EXPECT_FALSE(response_.update_exists);
Chris Sosa77f79e82014-06-02 18:16:24 -0700849}
850
Weidong Guo421ff332017-04-17 10:08:38 -0700851TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
852 // This test tests that update over cellular is allowed as device policy
853 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700854 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -0800855 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700856
857 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
858 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
859 SetArgPointee<1>(ConnectionTethering::kUnknown),
860 Return(true)));
861 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
862 .WillRepeatedly(Return(true));
863 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
864 .WillRepeatedly(Return(true));
865
Amin Hassani41ac04b2019-03-29 11:31:03 -0700866 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
867
868 ASSERT_TRUE(TestUpdateCheck());
869
Amin Hassania859c922020-11-13 15:30:38 -0800870 EXPECT_TRUE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700871}
872
873TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
874 // This test tests that update over cellular is blocked as device policy
875 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700876 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -0800877 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700878
879 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
880 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
881 SetArgPointee<1>(ConnectionTethering::kUnknown),
882 Return(true)));
883 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
884 .WillRepeatedly(Return(true));
885 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
886 .WillRepeatedly(Return(false));
887
Amin Hassani41ac04b2019-03-29 11:31:03 -0700888 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
889 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
890 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
891
892 ASSERT_FALSE(TestUpdateCheck());
893
Amin Hassania859c922020-11-13 15:30:38 -0800894 EXPECT_FALSE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700895}
896
897TEST_F(OmahaRequestActionTest,
898 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
899 // This test tests that, when device policy is not set, update over cellular
900 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700901 MockConnectionManager mock_cm;
Amin Hassani90e9f192020-11-18 14:20:56 -0800902 fake_prefs_->SetBoolean(kPrefsUpdateOverCellularPermission, true);
Amin Hassani538bd592020-11-04 20:46:08 -0800903 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700904
905 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
906 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
907 SetArgPointee<1>(ConnectionTethering::kUnknown),
908 Return(true)));
909 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
910 .WillRepeatedly(Return(false));
911 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
912 .WillRepeatedly(Return(true));
913
Amin Hassani41ac04b2019-03-29 11:31:03 -0700914 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
915
916 ASSERT_TRUE(TestUpdateCheck());
917
Amin Hassania859c922020-11-13 15:30:38 -0800918 EXPECT_TRUE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700919}
920
921TEST_F(OmahaRequestActionTest,
922 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
923 // This test tests that, when device policy is not set and permission for
924 // update over cellular is set to false or does not exist, update over
925 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700926 MockConnectionManager mock_cm;
927 // A version different from the version in omaha response.
928 string diff_version = "99.99.99";
929 // A size different from the size in omaha response.
930 int64_t diff_size = 999;
931
Amin Hassani90e9f192020-11-18 14:20:56 -0800932 fake_prefs_->SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
933 fake_prefs_->SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
Weidong Guo421ff332017-04-17 10:08:38 -0700934 // This test tests cellular (3G) being the only connection type being allowed.
Amin Hassani538bd592020-11-04 20:46:08 -0800935 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700936
937 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
938 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
939 SetArgPointee<1>(ConnectionTethering::kUnknown),
940 Return(true)));
941 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
942 .WillRepeatedly(Return(false));
943 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
944 .WillRepeatedly(Return(true));
945
Amin Hassani41ac04b2019-03-29 11:31:03 -0700946 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
947 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
948 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
949
950 ASSERT_FALSE(TestUpdateCheck());
951
Amin Hassania859c922020-11-13 15:30:38 -0800952 EXPECT_FALSE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700953}
954
955TEST_F(OmahaRequestActionTest,
956 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
957 // This test tests that, when device policy is not set and permission for
958 // update over cellular is set to false or does not exist, update over
959 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700960 MockConnectionManager mock_cm;
961 // A version same as the version in omaha response.
962 string new_version = fake_update_response_.version;
963 // A size same as the size in omaha response.
964 int64_t new_size = fake_update_response_.size;
965
Amin Hassani90e9f192020-11-18 14:20:56 -0800966 fake_prefs_->SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
967 fake_prefs_->SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
Amin Hassani538bd592020-11-04 20:46:08 -0800968 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700969
970 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
971 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
972 SetArgPointee<1>(ConnectionTethering::kUnknown),
973 Return(true)));
974 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
975 .WillRepeatedly(Return(false));
976 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
977 .WillRepeatedly(Return(true));
978
Amin Hassani41ac04b2019-03-29 11:31:03 -0700979 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
980
981 ASSERT_TRUE(TestUpdateCheck());
982
Amin Hassania859c922020-11-13 15:30:38 -0800983 EXPECT_TRUE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700984}
985
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700986TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700987 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700988 MockPayloadState mock_payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800989 FakeSystemState::Get()->set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700990 fake_update_response_.version = rollback_version;
991 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
992 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
993 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700994
Chris Sosa77f79e82014-06-02 18:16:24 -0700995 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800996 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700997
Amin Hassani41ac04b2019-03-29 11:31:03 -0700998 ASSERT_FALSE(TestUpdateCheck());
999
Amin Hassania859c922020-11-13 15:30:38 -08001000 EXPECT_FALSE(response_.update_exists);
Chris Sosa77f79e82014-06-02 18:16:24 -07001001}
1002
Marton Hunyadyc2882062018-05-14 17:28:25 +02001003// Verify that update checks called during OOBE will not try to download an
1004// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -07001005TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Amin Hassani538bd592020-11-04 20:46:08 -08001006 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -07001007 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1008 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1009 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1010 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -07001011
Sen Jiang8cd42342018-01-31 12:06:59 -08001012 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001013 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001014 ASSERT_FALSE(TestUpdateCheck());
1015
Amin Hassania859c922020-11-13 15:30:38 -08001016 EXPECT_FALSE(response_.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001017}
Kevin Cernekee2494e282016-03-29 18:03:53 -07001018
Marton Hunyadyc2882062018-05-14 17:28:25 +02001019// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
1020// enabled.
1021TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Amin Hassani538bd592020-11-04 20:46:08 -08001022 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
1023 FakeSystemState::Get()->fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001024 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -07001025
Amin Hassani41ac04b2019-03-29 11:31:03 -07001026 ASSERT_TRUE(TestUpdateCheck());
1027
Amin Hassania859c922020-11-13 15:30:38 -08001028 EXPECT_TRUE(response_.update_exists);
Kevin Cernekee2494e282016-03-29 18:03:53 -07001029}
1030
Marton Hunyadyc2882062018-05-14 17:28:25 +02001031// Verify that update checks called during OOBE will still try to download an
1032// update if the response includes the deadline field.
1033TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Amin Hassani538bd592020-11-04 20:46:08 -08001034 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001035 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001036 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001037
Amin Hassani41ac04b2019-03-29 11:31:03 -07001038 ASSERT_TRUE(TestUpdateCheck());
1039
Amin Hassania859c922020-11-13 15:30:38 -08001040 EXPECT_TRUE(response_.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001041}
1042
1043// Verify that update checks called during OOBE will not try to download an
1044// update if a rollback happened, even when the response includes the deadline
1045// field.
1046TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08001047 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001048 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001049 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1050 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1051 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1052 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1053
Amin Hassani538bd592020-11-04 20:46:08 -08001054 EXPECT_CALL(*(FakeSystemState::Get()->mock_payload_state()),
1055 GetRollbackHappened())
Marton Hunyadyc2882062018-05-14 17:28:25 +02001056 .WillOnce(Return(true));
1057
Amin Hassani41ac04b2019-03-29 11:31:03 -07001058 ASSERT_FALSE(TestUpdateCheck());
1059
Amin Hassania859c922020-11-13 15:30:38 -08001060 EXPECT_FALSE(response_.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001061}
1062
Toni Barzic61544e62018-10-11 14:37:30 -07001063// Verify that non-critical updates are skipped by reporting the
1064// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1065// i.e. when the update would need user permission. Note that reporting
1066// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1067// in OOBE (warning the user about an update that will be skipped).
1068TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Amin Hassani538bd592020-11-04 20:46:08 -08001069 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Toni Barzic61544e62018-10-11 14:37:30 -07001070
1071 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -08001072 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001073 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1074 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1075 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1076 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001077
1078 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1079 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1080 SetArgPointee<1>(ConnectionTethering::kUnknown),
1081 Return(true)));
1082 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1083 .WillRepeatedly(Return(false));
1084
Amin Hassani41ac04b2019-03-29 11:31:03 -07001085 ASSERT_FALSE(TestUpdateCheck());
1086
Amin Hassania859c922020-11-13 15:30:38 -08001087 EXPECT_FALSE(response_.update_exists);
Toni Barzic61544e62018-10-11 14:37:30 -07001088}
1089
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001090TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001091 request_params_.set_wall_clock_based_wait_enabled(true);
1092 request_params_.set_update_check_count_wait_enabled(false);
1093 request_params_.set_waiting_period(TimeDelta::FromDays(2));
Amin Hassani538bd592020-11-04 20:46:08 -08001094 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001095 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1096 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1097 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001098
Amin Hassani41ac04b2019-03-29 11:31:03 -07001099 ASSERT_FALSE(TestUpdateCheck());
1100
Amin Hassania859c922020-11-13 15:30:38 -08001101 EXPECT_FALSE(response_.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001102}
1103
1104TEST_F(OmahaRequestActionTest,
1105 WallClockBasedWaitAloneCausesScatteringInteractive) {
1106 request_params_.set_wall_clock_based_wait_enabled(true);
1107 request_params_.set_update_check_count_wait_enabled(false);
1108 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1109 request_params_.set_interactive(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001110 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001111 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001112
1113 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001114 ASSERT_TRUE(TestUpdateCheck());
1115
Amin Hassania859c922020-11-13 15:30:38 -08001116 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001117}
1118
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001119TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001120 request_params_.set_wall_clock_based_wait_enabled(false);
1121 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1122 request_params_.set_update_check_count_wait_enabled(true);
1123 request_params_.set_min_update_checks_needed(1);
1124 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001125 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001126
Amin Hassani41ac04b2019-03-29 11:31:03 -07001127 ASSERT_TRUE(TestUpdateCheck());
1128
Amin Hassania859c922020-11-13 15:30:38 -08001129 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001130}
1131
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001132TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001133 request_params_.set_wall_clock_based_wait_enabled(true);
1134 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1135 request_params_.set_update_check_count_wait_enabled(true);
1136 request_params_.set_min_update_checks_needed(1);
1137 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001138 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001139 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1140
1141 ASSERT_TRUE(TestUpdateCheck());
1142
Amin Hassania859c922020-11-13 15:30:38 -08001143 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001144}
1145
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001146TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001147 request_params_.set_wall_clock_based_wait_enabled(true);
1148 request_params_.set_waiting_period(TimeDelta());
1149 request_params_.set_update_check_count_wait_enabled(true);
1150 request_params_.set_min_update_checks_needed(0);
1151 request_params_.set_max_update_checks_allowed(0);
Amin Hassani538bd592020-11-04 20:46:08 -08001152 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001153 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001154
Amin Hassani41ac04b2019-03-29 11:31:03 -07001155 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001156
Ben Chan9abb7632014-08-07 00:10:53 -07001157 int64_t count;
Amin Hassani90e9f192020-11-18 14:20:56 -08001158 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001159 ASSERT_EQ(count, 0);
Amin Hassania859c922020-11-13 15:30:38 -08001160 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001161}
1162
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001163TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001164 request_params_.set_wall_clock_based_wait_enabled(true);
1165 request_params_.set_waiting_period(TimeDelta());
1166 request_params_.set_update_check_count_wait_enabled(true);
1167 request_params_.set_min_update_checks_needed(1);
1168 request_params_.set_max_update_checks_allowed(8);
Amin Hassani538bd592020-11-04 20:46:08 -08001169 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001170 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1171 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1172 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001173
Amin Hassani41ac04b2019-03-29 11:31:03 -07001174 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001175
Ben Chan9abb7632014-08-07 00:10:53 -07001176 int64_t count;
Amin Hassani90e9f192020-11-18 14:20:56 -08001177 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001178 ASSERT_GT(count, 0);
Amin Hassania859c922020-11-13 15:30:38 -08001179 EXPECT_FALSE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001180}
1181
Amin Hassani41ac04b2019-03-29 11:31:03 -07001182TEST_F(OmahaRequestActionTest,
1183 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001184 request_params_.set_wall_clock_based_wait_enabled(true);
1185 request_params_.set_waiting_period(TimeDelta());
1186 request_params_.set_update_check_count_wait_enabled(true);
1187 request_params_.set_min_update_checks_needed(1);
1188 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001189 request_params_.set_interactive(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001190 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001191 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1192
1193 // Verify if we are interactive check we don't defer.
1194 ASSERT_TRUE(TestUpdateCheck());
1195
Amin Hassania859c922020-11-13 15:30:38 -08001196 EXPECT_TRUE(response_.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001197}
1198
1199TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1200 request_params_.set_wall_clock_based_wait_enabled(true);
1201 request_params_.set_waiting_period(TimeDelta());
1202 request_params_.set_update_check_count_wait_enabled(true);
1203 request_params_.set_min_update_checks_needed(1);
1204 request_params_.set_max_update_checks_allowed(8);
Amin Hassani538bd592020-11-04 20:46:08 -08001205 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001206 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1207 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1208 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001209
Amin Hassani90e9f192020-11-18 14:20:56 -08001210 ASSERT_TRUE(fake_prefs_->SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001211 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001212
Ben Chan9abb7632014-08-07 00:10:53 -07001213 int64_t count;
Amin Hassani90e9f192020-11-18 14:20:56 -08001214 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001215 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001216 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001217 ASSERT_EQ(count, 5);
Amin Hassania859c922020-11-13 15:30:38 -08001218 EXPECT_FALSE(response_.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001219}
1220
1221TEST_F(OmahaRequestActionTest,
1222 ExistingUpdateCheckCountCausesScatteringInteractive) {
1223 request_params_.set_wall_clock_based_wait_enabled(true);
1224 request_params_.set_waiting_period(TimeDelta());
1225 request_params_.set_update_check_count_wait_enabled(true);
1226 request_params_.set_min_update_checks_needed(1);
1227 request_params_.set_max_update_checks_allowed(8);
1228 request_params_.set_interactive(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001229 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001230 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1231
Amin Hassani90e9f192020-11-18 14:20:56 -08001232 ASSERT_TRUE(fake_prefs_->SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001233
1234 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001235 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001236 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001237}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001238
Adolfo Victoria497044c2018-07-18 07:51:42 -07001239TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1240 // If staging is on, the value for max days to scatter should be ignored, and
1241 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001242 request_params_.set_wall_clock_based_wait_enabled(true);
1243 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1244 request_params_.set_update_check_count_wait_enabled(false);
Amin Hassani538bd592020-11-04 20:46:08 -08001245 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
May Lippert60aa3ca2018-08-15 16:55:29 -07001246
Amin Hassani90e9f192020-11-18 14:20:56 -08001247 ASSERT_TRUE(fake_prefs_->SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
Adolfo Victoria497044c2018-07-18 07:51:42 -07001248 // This should not prevent scattering due to staging.
1249 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001250 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1251 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1252 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1253
1254 ASSERT_FALSE(TestUpdateCheck());
1255
Amin Hassania859c922020-11-13 15:30:38 -08001256 EXPECT_FALSE(response_.update_exists);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001257
1258 // Interactive updates should not be affected.
1259 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001260 tuc_params_.expected_code = ErrorCode::kSuccess;
1261 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1262
1263 ASSERT_TRUE(TestUpdateCheck());
1264
Amin Hassania859c922020-11-13 15:30:38 -08001265 EXPECT_TRUE(response_.update_exists);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001266}
1267
Alex Deymo8e18f932015-03-27 16:16:59 -07001268TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -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";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001273 request_params_.set_dlc_apps_params(
1274 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
1275 fake_update_response_.dlc_app_update = true;
1276 fake_update_response_.include_dlc_cohorts = true;
1277 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1278 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1279 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001280 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001281
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001282 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001283 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001284
1285 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001286 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001287 EXPECT_EQ(fake_update_response_.cohort, value);
1288
Amin Hassani90e9f192020-11-18 14:20:56 -08001289 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001290 EXPECT_EQ(fake_update_response_.cohorthint, value);
1291
Amin Hassani90e9f192020-11-18 14:20:56 -08001292 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001293 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001294
Amin Hassani90e9f192020-11-18 14:20:56 -08001295 EXPECT_TRUE(fake_prefs_->GetString(
1296 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001297 &value));
1298 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1299
Amin Hassani90e9f192020-11-18 14:20:56 -08001300 EXPECT_TRUE(fake_prefs_->GetString(
1301 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001302 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1303 &value));
1304 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1305
Amin Hassani90e9f192020-11-18 14:20:56 -08001306 EXPECT_TRUE(fake_prefs_->GetString(
1307 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001308 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1309 &value));
1310 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Alex Deymo8e18f932015-03-27 16:16:59 -07001311}
1312
1313TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Amin Hassani90e9f192020-11-18 14:20:56 -08001314 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohort, "old_value"));
1315 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohortHint, "old_hint"));
1316 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohortName, "old_name"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001317 const string dlc_cohort_key =
Amin Hassani90e9f192020-11-18 14:20:56 -08001318 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1319 const string dlc_cohort_hint_key = fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001320 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint});
Amin Hassani90e9f192020-11-18 14:20:56 -08001321 const string dlc_cohort_name_key = fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001322 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName});
1323 request_params_.set_dlc_apps_params(
1324 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
Amin Hassani90e9f192020-11-18 14:20:56 -08001325 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_key, "old_value_dlc"));
1326 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_hint_key, "old_hint_dlc"));
1327 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_name_key, "old_name_dlc"));
Alex Deymo8e18f932015-03-27 16:16:59 -07001328 fake_update_response_.include_cohorts = true;
1329 fake_update_response_.cohort = "s/154454/8479665";
1330 fake_update_response_.cohorthint = "please-put-me-on-beta";
1331 fake_update_response_.cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001332 fake_update_response_.dlc_app_update = true;
1333 fake_update_response_.include_dlc_cohorts = true;
1334 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1335 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1336 fake_update_response_.dlc_cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001337 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001338
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001339 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001340 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001341
1342 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001343 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001344 EXPECT_EQ(fake_update_response_.cohort, value);
1345
Amin Hassani90e9f192020-11-18 14:20:56 -08001346 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001347 EXPECT_EQ(fake_update_response_.cohorthint, value);
1348
Amin Hassani90e9f192020-11-18 14:20:56 -08001349 EXPECT_FALSE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001350
Amin Hassani90e9f192020-11-18 14:20:56 -08001351 EXPECT_TRUE(fake_prefs_->GetString(dlc_cohort_key, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001352 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1353
Amin Hassani90e9f192020-11-18 14:20:56 -08001354 EXPECT_TRUE(fake_prefs_->GetString(dlc_cohort_hint_key, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001355 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1356
Amin Hassani90e9f192020-11-18 14:20:56 -08001357 EXPECT_FALSE(fake_prefs_->GetString(dlc_cohort_name_key, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001358}
1359
1360TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001361 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001362
Amin Hassani90e9f192020-11-18 14:20:56 -08001363 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohort, "old_value"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001364 const string dlc_cohort_key =
Amin Hassani90e9f192020-11-18 14:20:56 -08001365 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1366 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_key, "old_value_dlc"));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001367 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001368
1369 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001370 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001371 EXPECT_EQ("old_value", value);
1372
Amin Hassani90e9f192020-11-18 14:20:56 -08001373 EXPECT_FALSE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
1374 EXPECT_FALSE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001375
Amin Hassani90e9f192020-11-18 14:20:56 -08001376 EXPECT_TRUE(fake_prefs_->GetString(dlc_cohort_key, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001377 EXPECT_EQ("old_value_dlc", value);
1378
Amin Hassani90e9f192020-11-18 14:20:56 -08001379 EXPECT_FALSE(fake_prefs_->GetString(
1380 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001381 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1382 &value));
Amin Hassani90e9f192020-11-18 14:20:56 -08001383 EXPECT_FALSE(fake_prefs_->GetString(
1384 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001385 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1386 &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001387}
1388
Alex Deymo00d79ac2015-06-29 15:41:49 -07001389TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001390 fake_update_response_.include_cohorts = true;
1391 fake_update_response_.cohort = "s/154454/8479665";
1392 fake_update_response_.cohorthint = "please-put-me-on-beta";
1393 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001394 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1395 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1396 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001397
Amin Hassani41ac04b2019-03-29 11:31:03 -07001398 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001399
1400 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001401 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo00d79ac2015-06-29 15:41:49 -07001402 EXPECT_EQ(fake_update_response_.cohort, value);
1403
Amin Hassani90e9f192020-11-18 14:20:56 -08001404 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Alex Deymo00d79ac2015-06-29 15:41:49 -07001405 EXPECT_EQ(fake_update_response_.cohorthint, value);
1406
Amin Hassani90e9f192020-11-18 14:20:56 -08001407 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Alex Deymo00d79ac2015-06-29 15:41:49 -07001408 EXPECT_EQ(fake_update_response_.cohortname, value);
1409}
1410
Sen Jiangb1e063a2017-09-15 17:44:31 -07001411TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001412 fake_update_response_.multi_app = true;
1413 fake_update_response_.include_cohorts = true;
1414 fake_update_response_.cohort = "s/154454/8479665";
1415 fake_update_response_.cohorthint = "please-put-me-on-beta";
1416 fake_update_response_.cohortname = "stable";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001417 request_params_.set_dlc_apps_params(
1418 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
1419 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
1420 fake_update_response_.dlc_app_update = true;
1421 fake_update_response_.dlc_app_no_update = true;
1422 fake_update_response_.include_dlc_cohorts = true;
1423 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1424 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1425 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001426 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001427
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001428 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001429 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001430
1431 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001432 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Sen Jiangb1e063a2017-09-15 17:44:31 -07001433 EXPECT_EQ(fake_update_response_.cohort, value);
1434
Amin Hassani90e9f192020-11-18 14:20:56 -08001435 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Sen Jiangb1e063a2017-09-15 17:44:31 -07001436 EXPECT_EQ(fake_update_response_.cohorthint, value);
1437
Amin Hassani90e9f192020-11-18 14:20:56 -08001438 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Sen Jiangb1e063a2017-09-15 17:44:31 -07001439 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001440
Amin Hassani90e9f192020-11-18 14:20:56 -08001441 EXPECT_TRUE(fake_prefs_->GetString(
1442 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001443 &value));
1444 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
Amin Hassani90e9f192020-11-18 14:20:56 -08001445 EXPECT_TRUE(fake_prefs_->GetString(
1446 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohort}),
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001447 &value));
1448 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1449
Amin Hassani90e9f192020-11-18 14:20:56 -08001450 EXPECT_TRUE(fake_prefs_->GetString(
1451 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001452 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1453 &value));
1454 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
Amin Hassani90e9f192020-11-18 14:20:56 -08001455 EXPECT_TRUE(fake_prefs_->GetString(
1456 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001457 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortHint}),
1458 &value));
1459 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1460
Amin Hassani90e9f192020-11-18 14:20:56 -08001461 EXPECT_TRUE(fake_prefs_->GetString(
1462 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001463 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1464 &value));
1465 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Amin Hassani90e9f192020-11-18 14:20:56 -08001466 EXPECT_TRUE(fake_prefs_->GetString(
1467 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001468 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortName}),
1469 &value));
1470 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Sen Jiangb1e063a2017-09-15 17:44:31 -07001471}
1472
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001473TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001474 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001475 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001476 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001477
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001478 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001479 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001480 std::make_unique<MockHttpFetcher>(
1481 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001482 false,
1483 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001484 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001485 processor.set_delegate(&delegate_);
1486 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001487
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001488 loop.PostTask(base::Bind(
1489 [](ActionProcessor* processor) { processor->StartProcessing(); },
1490 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001491 loop.Run();
1492 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001493 EXPECT_FALSE(processor.IsRunning());
1494}
1495
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001496TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001497 tuc_params_.http_response = "invalid xml>";
1498 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1499 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1500 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1501
1502 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001503 EXPECT_FALSE(response_.update_exists);
Darin Petkovedc522e2010-11-05 09:35:17 -07001504}
1505
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001506TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001507 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1508 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1509 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1510
1511 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001512 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001513}
1514
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001515TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001516 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001517 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1518 "<daystart elapsed_seconds=\"100\"/>"
1519 "<app appid=\"foo\" status=\"ok\">"
1520 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001521 "<updatecheck/></app></response>";
1522 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1523 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1524 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1525
1526 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001527 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001528}
1529
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001530TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001531 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001532 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1533 "<daystart elapsed_seconds=\"100\"/>"
1534 "<app appid=\"foo\" status=\"ok\">"
1535 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001536 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1537 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1538 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1539 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1540
1541 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001542 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001543}
1544
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001545TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001546 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001547 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1548 "<daystart elapsed_seconds=\"100\"/>"
1549 "<app appid=\"foo\" status=\"ok\">"
1550 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001551 "</app></response>";
1552 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1553 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1554 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1555
1556 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001557 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001558}
1559
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001560TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001561 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001562 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1563 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001564 // the appid needs to match that in the request params
1565 "<app appid=\"" +
1566 fake_update_response_.app_id +
1567 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001568 "<updatecheck status=\"ok\">"
1569 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001570 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001571 "<packages><package hash=\"not-used\" name=\"f\" "
Vyshu Khota4c5413d2020-11-04 16:17:25 -08001572 "size=\"587\" fp=\"3.789\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001573 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001574 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001575 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001576 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001577 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001578
Amin Hassani41ac04b2019-03-29 11:31:03 -07001579 ASSERT_TRUE(TestUpdateCheck());
1580
Amin Hassania859c922020-11-13 15:30:38 -08001581 EXPECT_TRUE(response_.update_exists);
1582 EXPECT_EQ("10.2.3.4", response_.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001583 EXPECT_EQ("http://missing/field/test/f",
Amin Hassania859c922020-11-13 15:30:38 -08001584 response_.packages[0].payload_urls[0]);
1585 EXPECT_EQ("", response_.more_info_url);
1586 EXPECT_EQ("lkq34j5345", response_.packages[0].hash);
1587 EXPECT_EQ(string("3.789"), response_.packages[0].fp);
1588 EXPECT_EQ(587u, response_.packages[0].size);
1589 EXPECT_FALSE(response_.prompt);
1590 EXPECT_TRUE(response_.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001591}
1592
1593namespace {
1594class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1595 public:
1596 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001597 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001598 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001599};
1600
Alex Deymo60ca1a72015-06-18 18:19:15 -07001601void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001602 processor->StartProcessing();
1603 CHECK(processor->IsRunning());
1604 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001605}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001606} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001607
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001608TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001609 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001610 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001611
Alex Deymo60ca1a72015-06-18 18:19:15 -07001612 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001613 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001614 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001615 std::make_unique<MockHttpFetcher>(
1616 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001617 false,
1618 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001619 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001620 ActionProcessor processor;
1621 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001622 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001623
Alex Deymo60ca1a72015-06-18 18:19:15 -07001624 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1625 loop.Run();
1626 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001627}
1628
Alex Deymob0d74eb2015-03-30 17:59:17 -07001629TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001630 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001631 request_params_.set_os_sp("testtheservice_pack>");
1632 request_params_.set_os_board("x86 generic<id");
1633 request_params_.set_current_channel("unittest_track&lt;");
1634 request_params_.set_target_channel("unittest_track&lt;");
Amin Hassani37b67232020-08-13 09:29:48 -07001635 request_params_.set_lts_tag("unittest_hint&lt;");
Marton Hunyady2abda312018-04-24 18:21:49 +02001636 request_params_.set_hwid("<OEM MODEL>");
Amin Hassani90e9f192020-11-18 14:20:56 -08001637 fake_prefs_->SetString(kPrefsOmahaCohort, "evil\nstring");
1638 fake_prefs_->SetString(kPrefsOmahaCohortHint, "evil&string\\");
1639 fake_prefs_->SetString(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001640 kPrefsOmahaCohortName,
1641 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
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
1647 ASSERT_FALSE(TestUpdateCheck());
1648
Amin Hassania859c922020-11-13 15:30:38 -08001649 EXPECT_NE(string::npos, post_str_.find("testtheservice_pack&gt;"));
1650 EXPECT_EQ(string::npos, post_str_.find("testtheservice_pack>"));
1651 EXPECT_NE(string::npos, post_str_.find("x86 generic&lt;id"));
1652 EXPECT_EQ(string::npos, post_str_.find("x86 generic<id"));
1653 EXPECT_NE(string::npos, post_str_.find("unittest_track&amp;lt;"));
1654 EXPECT_EQ(string::npos, post_str_.find("unittest_track&lt;"));
1655 EXPECT_NE(string::npos, post_str_.find("unittest_hint&amp;lt;"));
1656 EXPECT_EQ(string::npos, post_str_.find("unittest_hint&lt;"));
1657 EXPECT_NE(string::npos, post_str_.find("&lt;OEM MODEL&gt;"));
1658 EXPECT_EQ(string::npos, post_str_.find("<OEM MODEL>"));
1659 EXPECT_NE(string::npos, post_str_.find("cohort=\"evil\nstring\""));
1660 EXPECT_EQ(string::npos, post_str_.find("cohorthint=\"evil&string\\\""));
1661 EXPECT_NE(string::npos, post_str_.find("cohorthint=\"evil&amp;string\\\""));
Alex Deymo8e18f932015-03-27 16:16:59 -07001662 // Values from Prefs that are too big are removed from the XML instead of
1663 // encoded.
Amin Hassania859c922020-11-13 15:30:38 -08001664 EXPECT_EQ(string::npos, post_str_.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001665}
1666
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001667TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001668 fake_update_response_.deadline = "&lt;20110101";
1669 fake_update_response_.more_info_url = "testthe&lt;url";
1670 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001671 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1672
1673 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001674
Amin Hassania859c922020-11-13 15:30:38 -08001675 EXPECT_EQ("testthe<url", response_.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001676 EXPECT_EQ("testthe&codebase/file.signed",
Amin Hassania859c922020-11-13 15:30:38 -08001677 response_.packages[0].payload_urls[0]);
1678 EXPECT_EQ("<20110101", response_.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001679}
1680
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001681TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001682 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001683 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001684 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001685
Amin Hassani41ac04b2019-03-29 11:31:03 -07001686 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001687 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001688}
1689
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001690TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001691 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001692 FakeSystemState::Get()->set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001693 tuc_params_.http_response = "invalid xml>";
1694 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1695 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1696 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001697
Darin Petkov95508da2011-01-05 12:42:29 -08001698 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001699 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001700 // An existing but empty previous version means that we didn't reboot to a new
1701 // update, therefore, no need to update the previous version.
1702 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001703 ASSERT_FALSE(TestUpdateCheck());
1704
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001705 EXPECT_NE(
Amin Hassania859c922020-11-13 15:30:38 -08001706 post_str_.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1707 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001708 string::npos);
Amin Hassania859c922020-11-13 15:30:38 -08001709 EXPECT_NE(post_str_.find("hardware_class=\"OEM MODEL 09235 7471\""),
Darin Petkovfbb40092010-07-29 17:05:50 -07001710 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001711 // No <event> tag should be sent if we didn't reboot to an update.
Amin Hassania859c922020-11-13 15:30:38 -08001712 EXPECT_EQ(post_str_.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001713}
1714
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001715TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001716 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001717 "invalid xml>");
1718
Alex Vakulenko75039d72014-03-25 12:36:28 -07001719 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001720 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001721 OmahaEvent::kTypeUpdateDownloadStarted,
1722 OmahaEvent::kResultSuccess);
Amin Hassania859c922020-11-13 15:30:38 -08001723 EXPECT_NE(post_str_.find(expected_event), string::npos);
1724 EXPECT_EQ(post_str_.find("ping"), string::npos);
1725 EXPECT_EQ(post_str_.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001726}
1727
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001728TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001729 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001730 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001731 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001732 "invalid xml>");
1733
Alex Vakulenko75039d72014-03-25 12:36:28 -07001734 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001735 " <event eventtype=\"%d\" eventresult=\"%d\" "
1736 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001737 OmahaEvent::kTypeDownloadComplete,
1738 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001739 static_cast<int>(ErrorCode::kError));
Amin Hassania859c922020-11-13 15:30:38 -08001740 EXPECT_NE(post_str_.find(expected_event), string::npos);
1741 EXPECT_EQ(post_str_.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001742}
1743
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001744TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001745 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001746 OmahaRequestAction update_check_action(
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001747 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001748 std::make_unique<MockHttpFetcher>(
1749 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001750 false,
1751 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001752 EXPECT_FALSE(update_check_action.IsEvent());
1753
1754 OmahaRequestAction event_action(
Darin Petkove17f86b2010-07-20 09:12:01 -07001755 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001756 std::make_unique<MockHttpFetcher>(
1757 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001758 false,
1759 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001760 EXPECT_TRUE(event_action.IsEvent());
1761}
1762
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001763TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001764 tuc_params_.http_response = "invalid xml>";
1765 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1766 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1767 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1768
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001769 for (int i = 0; i < 2; i++) {
1770 bool delta_okay = i == 1;
1771 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001772 request_params_.set_delta_okay(delta_okay);
1773
Amin Hassani41ac04b2019-03-29 11:31:03 -07001774 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001775 EXPECT_NE(post_str_.find(
1776 base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1777 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001778 << "i = " << i;
1779 }
1780}
1781
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001782TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001783 tuc_params_.http_response = "invalid xml>";
1784 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1785 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1786 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1787
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001788 for (int i = 0; i < 2; i++) {
1789 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001790 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001791 request_params_.set_interactive(interactive);
1792
Amin Hassani41ac04b2019-03-29 11:31:03 -07001793 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001794 EXPECT_NE(post_str_.find(
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001795 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001796 string::npos)
1797 << "i = " << i;
1798 }
1799}
1800
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001801TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001802 tuc_params_.http_response = "invalid xml>";
1803 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1804 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1805 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1806
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001807 for (int i = 0; i < 2; i++) {
1808 bool target_version_set = i == 1;
1809 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001810 request_params_.set_target_version_prefix(target_version_prefix);
1811
Amin Hassani41ac04b2019-03-29 11:31:03 -07001812 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001813 if (target_version_set) {
Amin Hassania859c922020-11-13 15:30:38 -08001814 EXPECT_NE(post_str_.find("<updatecheck targetversionprefix=\"10032.\">"),
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001815 string::npos)
1816 << "i = " << i;
1817 } else {
Amin Hassania859c922020-11-13 15:30:38 -08001818 EXPECT_EQ(post_str_.find("targetversionprefix"), string::npos)
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001819 << "i = " << i;
1820 }
1821 }
1822}
1823
1824TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001825 tuc_params_.http_response = "invalid xml>";
1826 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1827 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1828 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1829
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001830 for (int i = 0; i < 4; i++) {
1831 bool rollback_allowed = i / 2 == 0;
1832 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001833 request_params_.set_target_version_prefix(target_version_set ? "10032."
1834 : "");
1835 request_params_.set_rollback_allowed(rollback_allowed);
1836
Amin Hassani41ac04b2019-03-29 11:31:03 -07001837 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001838 if (rollback_allowed && target_version_set) {
Amin Hassania859c922020-11-13 15:30:38 -08001839 EXPECT_NE(post_str_.find("rollback_allowed=\"true\""), string::npos)
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001840 << "i = " << i;
1841 } else {
Amin Hassania859c922020-11-13 15:30:38 -08001842 EXPECT_EQ(post_str_.find("rollback_allowed"), string::npos)
1843 << "i = " << i;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001844 }
1845 }
1846}
1847
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001848TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001849 OmahaEvent default_event;
1850 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1851 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001852 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001853
1854 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1855 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1856 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001857 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001858
1859 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1860 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001861 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001862 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1863 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001864 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001865}
1866
Askar Aitzhan570ca872019-04-24 11:16:12 +02001867TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
Askar Aitzhan18fff842019-06-21 23:24:37 +02001868 // If DeviceQuickFixBuildToken value is set it takes precedence over pref
1869 // value.
1870 constexpr char autoupdate_token[] = "autoupdate_token>";
1871 constexpr char xml_encoded_autoupdate_token[] = "autoupdate_token&gt;";
1872 constexpr char omaha_cohort_hint[] = "cohort_hint";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001873
1874 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1875 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1876 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1877 request_params_.set_autoupdate_token(autoupdate_token);
Amin Hassani90e9f192020-11-18 14:20:56 -08001878 fake_prefs_->SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan570ca872019-04-24 11:16:12 +02001879
1880 ASSERT_TRUE(TestUpdateCheck());
1881
Askar Aitzhan18fff842019-06-21 23:24:37 +02001882 EXPECT_NE(string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08001883 post_str_.find("cohorthint=\"" +
1884 string(xml_encoded_autoupdate_token) + "\""));
1885 EXPECT_EQ(string::npos, post_str_.find(autoupdate_token));
1886 EXPECT_EQ(string::npos, post_str_.find(omaha_cohort_hint));
Askar Aitzhan18fff842019-06-21 23:24:37 +02001887}
1888
1889TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsNotSetTest) {
1890 // If DeviceQuickFixBuildToken is not set, pref value will be provided in
1891 // cohorthint attribute.
1892 constexpr char omaha_cohort_hint[] = "evil_string>";
1893 constexpr char xml_encoded_cohort_hint[] = "evil_string&gt;";
1894
1895 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1896 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1897 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Amin Hassani90e9f192020-11-18 14:20:56 -08001898 fake_prefs_->SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan18fff842019-06-21 23:24:37 +02001899
1900 ASSERT_TRUE(TestUpdateCheck());
1901
1902 EXPECT_NE(
1903 string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08001904 post_str_.find("cohorthint=\"" + string(xml_encoded_cohort_hint) + "\""));
1905 EXPECT_EQ(string::npos, post_str_.find(omaha_cohort_hint));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001906}
1907
Amin Hassani37b67232020-08-13 09:29:48 -07001908TEST_F(OmahaRequestActionTest, TargetChannelHintTest) {
1909 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1910 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1911 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1912 request_params_.set_lts_tag("hint>");
1913
1914 ASSERT_TRUE(TestUpdateCheck());
1915
Amin Hassania859c922020-11-13 15:30:38 -08001916 EXPECT_NE(string::npos, post_str_.find("ltstag=\"hint&gt;\""));
Amin Hassani37b67232020-08-13 09:29:48 -07001917}
1918
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001919void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001920 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001921 FakeSystemState::Get()->set_prefs(&prefs);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001922 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001923 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001924 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1925 // Add a few hours to the day difference to test no rounding, etc.
1926 int64_t five_days_ago =
1927 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1928 int64_t six_days_ago =
1929 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1930 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001931 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001932 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001933 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001934 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001935 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001936
1937 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1938 tuc_params_.ping_only = ping_only;
1939 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1940 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1941
1942 ASSERT_TRUE(TestUpdateCheck());
1943
Amin Hassania859c922020-11-13 15:30:38 -08001944 EXPECT_NE(post_str_.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001945 string::npos);
1946 if (ping_only) {
Amin Hassania859c922020-11-13 15:30:38 -08001947 EXPECT_EQ(post_str_.find("updatecheck"), string::npos);
1948 EXPECT_EQ(post_str_.find("previousversion"), string::npos);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001949 } else {
Amin Hassania859c922020-11-13 15:30:38 -08001950 EXPECT_NE(post_str_.find("updatecheck"), string::npos);
1951 EXPECT_NE(post_str_.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001952 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001953}
1954
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001955TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001956 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001957}
1958
1959TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001960 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001961}
1962
1963TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001964 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001965 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001966 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001967 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001968 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001969 int64_t three_days_ago =
1970 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1971 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001972 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001973 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001974 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001975 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001976 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001977 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001978
1979 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1980 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1981 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1982
1983 ASSERT_TRUE(TestUpdateCheck());
1984
Amin Hassania859c922020-11-13 15:30:38 -08001985 EXPECT_NE(post_str_.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001986}
1987
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001988TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001989 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001990 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001991 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001992 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001993 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001994 int64_t four_days_ago =
1995 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1996 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001997 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001998 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001999 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002000 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002001 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002002 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002003
2004 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2005 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2006 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2007
2008 ASSERT_TRUE(TestUpdateCheck());
2009
Amin Hassania859c922020-11-13 15:30:38 -08002010 EXPECT_NE(post_str_.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07002011 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002012}
2013
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002014TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002015 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002016 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002017 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002018 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002019 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002020 int64_t one_hour_ago =
2021 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002022 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002023 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002024 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002025 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002026 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002027 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002028 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
2029 // send a ping.
2030 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2031 .WillOnce(Return(true));
2032 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2033 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002034
2035 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2036 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2037 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2038
2039 ASSERT_TRUE(TestUpdateCheck());
2040
Amin Hassania859c922020-11-13 15:30:38 -08002041 EXPECT_EQ(post_str_.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002042}
2043
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002044TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07002045 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08002046 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002047 FakeSystemState::Get()->set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07002048 int64_t now = Time::Now().ToInternalValue();
2049 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002050 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002051 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002052 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002053 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2054 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002055
2056 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2057 tuc_params_.ping_only = true;
2058 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
2059 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2060
2061 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002062 EXPECT_TRUE(post_str_.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07002063}
2064
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002065TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002066 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002067 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002068 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002069 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002070 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002071 int64_t future =
2072 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002073 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002074 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002075 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002076 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002077 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002078 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002079 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2080 .WillOnce(Return(true));
2081 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2082 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002083
2084 tuc_params_.http_response =
2085 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2086 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
2087 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2088 "<updatecheck status=\"noupdate\"/></app></response>";
2089 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2090 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2091
2092 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002093 EXPECT_EQ(post_str_.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002094}
2095
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002096TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002097 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07002098 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002099 // may fail if it runs for longer than 5 seconds. It shouldn't run
2100 // that long though.
2101 int64_t midnight =
2102 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
2103 int64_t midnight_slack =
2104 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08002105 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002106 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002107 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2108 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002109 EXPECT_CALL(prefs,
2110 SetInt64(kPrefsLastActivePingDay,
2111 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002112 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002113 EXPECT_CALL(prefs,
2114 SetInt64(kPrefsLastRollCallPingDay,
2115 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002116 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002117
2118 tuc_params_.http_response =
2119 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2120 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
2121 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2122 "<updatecheck status=\"noupdate\"/></app></response>";
2123 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2124 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2125
2126 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002127}
2128
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002129TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002130 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002131 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002132 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2133 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002134 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2135 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002136
2137 tuc_params_.http_response =
2138 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2139 "protocol=\"3.0\"><daystart blah=\"200\"/>"
2140 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2141 "<updatecheck status=\"noupdate\"/></app></response>";
2142 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2143 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2144
2145 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002146}
2147
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002148TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002149 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002150 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002151 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2152 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002153 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2154 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002155
2156 tuc_params_.http_response =
2157 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2158 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
2159 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2160 "<updatecheck status=\"noupdate\"/></app></response>";
2161 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2162 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2163
2164 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002165}
2166
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002167TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07002168 tuc_params_.http_response = "invalid xml>";
2169 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2170 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2171 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2172
2173 ASSERT_FALSE(TestUpdateCheck());
2174
Amin Hassania859c922020-11-13 15:30:38 -08002175 EXPECT_EQ(post_str_.find("machineid="), string::npos);
2176 EXPECT_EQ(post_str_.find("userid="), string::npos);
Darin Petkov84c763c2010-07-29 16:27:58 -07002177}
2178
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002179TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002180 const int http_error_code =
2181 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002182 tuc_params_.fail_http_response_code = 501;
2183 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2184 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2185 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2186 tuc_params_.expected_download_error_code =
2187 static_cast<metrics::DownloadErrorCode>(501);
2188
2189 ASSERT_FALSE(TestUpdateCheck());
2190
Amin Hassania859c922020-11-13 15:30:38 -08002191 EXPECT_FALSE(response_.update_exists);
Darin Petkovedc522e2010-11-05 09:35:17 -07002192}
2193
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002194TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002195 const int http_error_code =
2196 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002197
2198 tuc_params_.fail_http_response_code = 1500;
2199 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2200 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2201 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2202 tuc_params_.expected_download_error_code =
2203 metrics::DownloadErrorCode::kHttpStatusOther;
2204
2205 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002206 EXPECT_FALSE(response_.update_exists);
Darin Petkovedc522e2010-11-05 09:35:17 -07002207}
2208
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002209TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002210 request_params_.set_wall_clock_based_wait_enabled(true);
2211 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2212 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002213
Sen Jiang7c1171e2016-06-23 11:35:40 -07002214 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002215 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Amin Hassani538bd592020-11-04 20:46:08 -08002216 FakeSystemState::Get()->fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002217
2218 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2219 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2220 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2221
2222 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002223
Ben Chan9abb7632014-08-07 00:10:53 -07002224 int64_t timestamp = 0;
Amin Hassani90e9f192020-11-18 14:20:56 -08002225 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002226 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Amin Hassania859c922020-11-13 15:30:38 -08002227 EXPECT_FALSE(response_.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002228
2229 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002230 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002231 tuc_params_.expected_code = ErrorCode::kSuccess;
2232 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2233
2234 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002235 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002236}
2237
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002238TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002239 request_params_.set_wall_clock_based_wait_enabled(true);
2240 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2241 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002242
Sen Jiang7c1171e2016-06-23 11:35:40 -07002243 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002244 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2245 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002246 ASSERT_TRUE(
Amin Hassani90e9f192020-11-18 14:20:56 -08002247 fake_prefs_->SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Amin Hassani538bd592020-11-04 20:46:08 -08002248 FakeSystemState::Get()->fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002249
Amin Hassani41ac04b2019-03-29 11:31:03 -07002250 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2251
2252 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002253 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002254
2255 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002256 int64_t timestamp = 0;
Amin Hassani90e9f192020-11-18 14:20:56 -08002257 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002258 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2259}
2260
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002261TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002262 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002263 base::ScopedTempDir tempdir;
2264 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002265
Marton Hunyady2abda312018-04-24 18:21:49 +02002266 request_params_.set_root(tempdir.GetPath().value());
2267 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2268 request_params_.set_app_version("1.2.3.4");
2269 request_params_.set_product_components("o.bundle=1");
2270 request_params_.set_current_channel("canary-channel");
2271 EXPECT_TRUE(
2272 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2273 request_params_.UpdateDownloadChannel();
2274 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002275
2276 tuc_params_.http_response = "invalid xml>";
2277 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2278 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2279 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2280
2281 ASSERT_FALSE(TestUpdateCheck());
2282
Amin Hassania859c922020-11-13 15:30:38 -08002283 EXPECT_NE(string::npos,
2284 post_str_.find(
2285 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
2286 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2287 "track=\"stable-channel\" from_track=\"canary-channel\" "));
2288 EXPECT_EQ(string::npos, post_str_.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002289}
2290
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002291TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002292 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002293 base::ScopedTempDir tempdir;
2294 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002295
Marton Hunyady2abda312018-04-24 18:21:49 +02002296 request_params_.set_root(tempdir.GetPath().value());
2297 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2298 request_params_.set_app_version("5.6.7.8");
2299 request_params_.set_product_components("o.bundle=1");
2300 request_params_.set_current_channel("stable-channel");
2301 EXPECT_TRUE(
2302 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2303 request_params_.UpdateDownloadChannel();
2304 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002305
2306 tuc_params_.http_response = "invalid xml>";
2307 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2308 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2309 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2310
2311 ASSERT_FALSE(TestUpdateCheck());
2312
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002313 EXPECT_NE(
2314 string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002315 post_str_.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2316 "version=\"5.6.7.8\" "
2317 "track=\"canary-channel\" from_track=\"stable-channel\""));
2318 EXPECT_EQ(string::npos, post_str_.find("from_version"));
2319 EXPECT_NE(string::npos, post_str_.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002320}
2321
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002322// Checks that the initial ping with a=-1 r=-1 is not send when the device
2323// was powerwashed.
2324TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
Amin Hassani90e9f192020-11-18 14:20:56 -08002325 fake_prefs_->SetString(kPrefsPreviousVersion, "");
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002326
2327 // Flag that the device was powerwashed in the past.
Amin Hassani538bd592020-11-04 20:46:08 -08002328 FakeSystemState::Get()->fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002329 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2330 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2331 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002332
Amin Hassani41ac04b2019-03-29 11:31:03 -07002333 ASSERT_TRUE(TestUpdateCheck());
2334
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002335 // We shouldn't send a ping in this case since powerwash > 0.
Amin Hassania859c922020-11-13 15:30:38 -08002336 EXPECT_EQ(string::npos, post_str_.find("<ping"));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002337}
2338
Amin Hassani1677e812017-06-21 13:36:36 -07002339// Checks that the initial ping with a=-1 r=-1 is not send when the device
2340// first_active_omaha_ping_sent is set.
2341TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
Amin Hassani90e9f192020-11-18 14:20:56 -08002342 fake_prefs_->SetString(kPrefsPreviousVersion, "");
Amin Hassani1677e812017-06-21 13:36:36 -07002343
2344 // Flag that the device was not powerwashed in the past.
Amin Hassani538bd592020-11-04 20:46:08 -08002345 FakeSystemState::Get()->fake_hardware()->SetPowerwashCount(0);
Amin Hassani1677e812017-06-21 13:36:36 -07002346
2347 // Flag that the device has sent first active ping in the past.
Amin Hassani538bd592020-11-04 20:46:08 -08002348 FakeSystemState::Get()->fake_hardware()->SetFirstActiveOmahaPingSent();
Amin Hassani1677e812017-06-21 13:36:36 -07002349
Amin Hassani41ac04b2019-03-29 11:31:03 -07002350 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2351 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2352 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2353
2354 ASSERT_TRUE(TestUpdateCheck());
2355
Amin Hassani1677e812017-06-21 13:36:36 -07002356 // We shouldn't send a ping in this case since
2357 // first_active_omaha_ping_sent=true
Amin Hassania859c922020-11-13 15:30:38 -08002358 EXPECT_EQ(string::npos, post_str_.find("<ping"));
Amin Hassani1677e812017-06-21 13:36:36 -07002359}
2360
Alex Deymo9fded1e2015-11-05 12:31:19 -08002361// Checks that the event 54 is sent on a reboot to a new update.
2362TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2363 // Flag that the device was updated in a previous boot.
Amin Hassani90e9f192020-11-18 14:20:56 -08002364 fake_prefs_->SetString(kPrefsPreviousVersion, "1.2.3.4");
Alex Deymo9fded1e2015-11-05 12:31:19 -08002365
Amin Hassani41ac04b2019-03-29 11:31:03 -07002366 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2367 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2368 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2369
2370 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002371
2372 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002373 EXPECT_NE(
2374 string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002375 post_str_.find(base::StringPrintf("<event eventtype=\"%d\"",
2376 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002377 EXPECT_NE(string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002378 post_str_.find("previousversion=\"1.2.3.4\"></event>"));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002379
2380 // The previous version flag should have been removed.
Amin Hassani90e9f192020-11-18 14:20:56 -08002381 EXPECT_TRUE(fake_prefs_->Exists(kPrefsPreviousVersion));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002382 string prev_version;
Amin Hassani90e9f192020-11-18 14:20:56 -08002383 EXPECT_TRUE(fake_prefs_->GetString(kPrefsPreviousVersion, &prev_version));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002384 EXPECT_TRUE(prev_version.empty());
2385}
2386
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002387void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2388 bool initial_allow_p2p_for_sharing,
2389 bool omaha_disable_p2p_for_downloading,
2390 bool omaha_disable_p2p_for_sharing,
2391 bool payload_state_allow_p2p_attempt,
2392 bool expect_p2p_client_lookup,
2393 const string& p2p_client_result_url,
2394 bool expected_allow_p2p_for_downloading,
2395 bool expected_allow_p2p_for_sharing,
2396 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002397 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2398 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2399 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002400
2401 MockPayloadState mock_payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08002402 FakeSystemState::Get()->set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002403 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2404 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002405 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2406 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2407 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2408 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2409 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2410 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2411 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2412 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2413 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2414 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2415
David Zeuthen8f191b22013-08-06 12:27:50 -07002416 MockP2PManager mock_p2p_manager;
Amin Hassani538bd592020-11-04 20:46:08 -08002417 FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002418 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2419
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002420 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2421 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002422 .Times(expect_p2p_client_lookup ? 1 : 0);
2423
Alex Deymo8e18f932015-03-27 16:16:59 -07002424 fake_update_response_.disable_p2p_for_downloading =
2425 omaha_disable_p2p_for_downloading;
2426 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002427
2428 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2429 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2430
2431 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002432 EXPECT_TRUE(response_.update_exists);
David Zeuthen8f191b22013-08-06 12:27:50 -07002433
Gilad Arnold74b5f552014-10-07 08:17:16 -07002434 EXPECT_EQ(omaha_disable_p2p_for_downloading,
Amin Hassania859c922020-11-13 15:30:38 -08002435 response_.disable_p2p_for_downloading);
2436 EXPECT_EQ(omaha_disable_p2p_for_sharing, response_.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002437
Gilad Arnold74b5f552014-10-07 08:17:16 -07002438 EXPECT_EQ(expected_allow_p2p_for_downloading,
2439 actual_allow_p2p_for_downloading);
2440 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2441 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002442}
2443
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002444TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002445 P2PTest(true, // initial_allow_p2p_for_downloading
2446 true, // initial_allow_p2p_for_sharing
2447 false, // omaha_disable_p2p_for_downloading
2448 false, // omaha_disable_p2p_for_sharing
2449 true, // payload_state_allow_p2p_attempt
2450 true, // expect_p2p_client_lookup
2451 "http://1.3.5.7/p2p", // p2p_client_result_url
2452 true, // expected_allow_p2p_for_downloading
2453 true, // expected_allow_p2p_for_sharing
2454 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002455}
2456
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002457TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002458 P2PTest(true, // initial_allow_p2p_for_downloading
2459 true, // initial_allow_p2p_for_sharing
2460 false, // omaha_disable_p2p_for_downloading
2461 false, // omaha_disable_p2p_for_sharing
2462 true, // payload_state_allow_p2p_attempt
2463 true, // expect_p2p_client_lookup
2464 "", // p2p_client_result_url
2465 false, // expected_allow_p2p_for_downloading
2466 true, // expected_allow_p2p_for_sharing
2467 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002468}
2469
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002470TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002471 P2PTest(false, // initial_allow_p2p_for_downloading
2472 true, // initial_allow_p2p_for_sharing
2473 false, // omaha_disable_p2p_for_downloading
2474 false, // omaha_disable_p2p_for_sharing
2475 true, // payload_state_allow_p2p_attempt
2476 false, // expect_p2p_client_lookup
2477 "unset", // p2p_client_result_url
2478 false, // expected_allow_p2p_for_downloading
2479 true, // expected_allow_p2p_for_sharing
2480 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002481}
2482
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002483TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002484 P2PTest(true, // initial_allow_p2p_for_downloading
2485 true, // initial_allow_p2p_for_sharing
2486 true, // omaha_disable_p2p_for_downloading
2487 false, // omaha_disable_p2p_for_sharing
2488 true, // payload_state_allow_p2p_attempt
2489 false, // expect_p2p_client_lookup
2490 "unset", // p2p_client_result_url
2491 false, // expected_allow_p2p_for_downloading
2492 true, // expected_allow_p2p_for_sharing
2493 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002494}
2495
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002496TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002497 P2PTest(true, // initial_allow_p2p_for_downloading
2498 true, // initial_allow_p2p_for_sharing
2499 false, // omaha_disable_p2p_for_downloading
2500 true, // omaha_disable_p2p_for_sharing
2501 true, // payload_state_allow_p2p_attempt
2502 true, // expect_p2p_client_lookup
2503 "http://1.3.5.7/p2p", // p2p_client_result_url
2504 true, // expected_allow_p2p_for_downloading
2505 false, // expected_allow_p2p_for_sharing
2506 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002507}
2508
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002509TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002510 P2PTest(true, // initial_allow_p2p_for_downloading
2511 true, // initial_allow_p2p_for_sharing
2512 true, // omaha_disable_p2p_for_downloading
2513 true, // omaha_disable_p2p_for_sharing
2514 true, // payload_state_allow_p2p_attempt
2515 false, // expect_p2p_client_lookup
2516 "unset", // p2p_client_result_url
2517 false, // expected_allow_p2p_for_downloading
2518 false, // expected_allow_p2p_for_sharing
2519 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002520}
2521
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002522bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2523 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002524 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002525 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2526
2527 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002528}
2529
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002530TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002531 // Simulate a successful update check that happens during OOBE. The
2532 // deadline in the response is needed to force the update attempt to
2533 // occur; responses without a deadline seen during OOBE will normally
2534 // return ErrorCode::kNonCriticalUpdateInOOBE.
Amin Hassani538bd592020-11-04 20:46:08 -08002535 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Kevin Cernekee2494e282016-03-29 18:03:53 -07002536 fake_update_response_.deadline = "20101020";
2537
David Zeuthen639aa362014-02-03 16:23:44 -08002538 // Check that we parse elapsed_days in the Omaha Response correctly.
2539 // and that the kPrefsInstallDateDays value is written to.
Amin Hassani90e9f192020-11-18 14:20:56 -08002540 EXPECT_FALSE(fake_prefs_->Exists(kPrefsInstallDateDays));
Amin Hassania859c922020-11-13 15:30:38 -08002541 EXPECT_TRUE(InstallDateParseHelper("42", &response_));
2542 EXPECT_TRUE(response_.update_exists);
2543 EXPECT_EQ(42, response_.install_date_days);
Amin Hassani90e9f192020-11-18 14:20:56 -08002544 EXPECT_TRUE(fake_prefs_->Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002545 int64_t prefs_days;
Amin Hassani90e9f192020-11-18 14:20:56 -08002546 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002547 EXPECT_EQ(prefs_days, 42);
2548
2549 // If there already is a value set, we shouldn't do anything.
Amin Hassania859c922020-11-13 15:30:38 -08002550 EXPECT_TRUE(InstallDateParseHelper("7", &response_));
2551 EXPECT_TRUE(response_.update_exists);
2552 EXPECT_EQ(7, response_.install_date_days);
Amin Hassani90e9f192020-11-18 14:20:56 -08002553 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002554 EXPECT_EQ(prefs_days, 42);
2555
2556 // Note that elapsed_days is not necessarily divisible by 7 so check
2557 // that we round down correctly when populating kPrefsInstallDateDays.
Amin Hassani90e9f192020-11-18 14:20:56 -08002558 EXPECT_TRUE(fake_prefs_->Delete(kPrefsInstallDateDays));
Amin Hassania859c922020-11-13 15:30:38 -08002559 EXPECT_TRUE(InstallDateParseHelper("23", &response_));
2560 EXPECT_TRUE(response_.update_exists);
2561 EXPECT_EQ(23, response_.install_date_days);
Amin Hassani90e9f192020-11-18 14:20:56 -08002562 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002563 EXPECT_EQ(prefs_days, 21);
2564
2565 // Check that we correctly handle elapsed_days not being included in
Amin Hassania859c922020-11-13 15:30:38 -08002566 // the Omaha Response_.
2567 EXPECT_TRUE(InstallDateParseHelper("", &response_));
2568 EXPECT_TRUE(response_.update_exists);
2569 EXPECT_EQ(-1, response_.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002570}
2571
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002572// If there is no prefs and OOBE is not complete, we should not
2573// report anything to Omaha.
2574TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Amin Hassani538bd592020-11-04 20:46:08 -08002575 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
2576 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), -1);
Amin Hassani90e9f192020-11-18 14:20:56 -08002577 EXPECT_FALSE(fake_prefs_->Exists(kPrefsInstallDateDays));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002578}
David Zeuthen639aa362014-02-03 16:23:44 -08002579
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002580// If OOBE is complete and happened on a valid date (e.g. after Jan
2581// 1 2007 0:00 PST), that date should be used and written to
2582// prefs. However, first try with an invalid date and check we do
2583// nothing.
2584TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2585 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
Amin Hassani538bd592020-11-04 20:46:08 -08002586 FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(oobe_date);
2587 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), -1);
Amin Hassani90e9f192020-11-18 14:20:56 -08002588 EXPECT_FALSE(fake_prefs_->Exists(kPrefsInstallDateDays));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002589}
David Zeuthen639aa362014-02-03 16:23:44 -08002590
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002591// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2592// should yield an InstallDate of 14.
2593TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2594 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
Amin Hassani538bd592020-11-04 20:46:08 -08002595 FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(oobe_date);
2596 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), 14);
Amin Hassani90e9f192020-11-18 14:20:56 -08002597 EXPECT_TRUE(fake_prefs_->Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002598
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002599 int64_t prefs_days;
Amin Hassani90e9f192020-11-18 14:20:56 -08002600 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002601 EXPECT_EQ(prefs_days, 14);
2602}
David Zeuthen639aa362014-02-03 16:23:44 -08002603
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002604// Now that we have a valid date in prefs, check that we keep using
2605// that even if OOBE date reports something else. The date Jan 30,
2606// 2007 0:00 PST should yield an InstallDate of 28... but since
2607// there's a prefs file, we should still get 14.
2608TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2609 // Set a valid date in the prefs first.
Amin Hassani90e9f192020-11-18 14:20:56 -08002610 EXPECT_TRUE(fake_prefs_->SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002611
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002612 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
Amin Hassani538bd592020-11-04 20:46:08 -08002613 FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(oobe_date);
2614 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002615
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002616 int64_t prefs_days;
Amin Hassani90e9f192020-11-18 14:20:56 -08002617 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002618 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002619
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002620 // If we delete the prefs file, we should get 28 days.
Amin Hassani90e9f192020-11-18 14:20:56 -08002621 EXPECT_TRUE(fake_prefs_->Delete(kPrefsInstallDateDays));
Amin Hassani538bd592020-11-04 20:46:08 -08002622 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), 28);
Amin Hassani90e9f192020-11-18 14:20:56 -08002623 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002624 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002625}
2626
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002627// Verifies that a device with no device policy, and is not a consumer
2628// device sets the max kernel key version to the current version.
2629// ie. the same behavior as if rollback is enabled.
2630TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002631 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002632
2633 // Setup and verify some initial default values for the kernel TPM
2634 // values that control verified boot and rollback.
2635 const int min_kernel_version = 4;
2636 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2637 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2638 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2639 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2640
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002641 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002642 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002643 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2644 .Times(1);
2645
Amin Hassani41ac04b2019-03-29 11:31:03 -07002646 fake_update_response_.deadline = "20101020";
2647 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2648 tuc_params_.is_consumer_device = false;
2649 tuc_params_.rollback_allowed_milestones = 3;
2650
2651 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002652 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002653
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002654 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002655 // kernel key version. This has the effect of freezing roll
2656 // forwards indefinitely. This will hold the rollback window
2657 // open until a future change will be able to move this forward
2658 // relative the configured window.
2659 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2660 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2661}
2662
2663// Verifies that a conmsumer device with no device policy sets the
2664// max kernel key version to the current version. ie. the same
2665// behavior as if rollback is enabled.
2666TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002667 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002668
2669 // Setup and verify some initial default values for the kernel TPM
2670 // values that control verified boot and rollback.
2671 const int min_kernel_version = 3;
2672 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2673 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2674 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2675 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2676
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002677 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002678 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002679 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2680 .Times(1);
2681
Amin Hassani41ac04b2019-03-29 11:31:03 -07002682 fake_update_response_.deadline = "20101020";
2683 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2684 tuc_params_.is_consumer_device = true;
2685 tuc_params_.rollback_allowed_milestones = 3;
2686
2687 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002688 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002689
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002690 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002691 // was set to logical infinity. This is the expected behavior for
2692 // consumer devices and matches the existing behavior prior to the
2693 // rollback features.
2694 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2695 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2696}
2697
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002698// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002699// in the TPM to prevent roll forward.
2700TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002701 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002702
2703 // Setup and verify some initial default values for the kernel TPM
2704 // values that control verified boot and rollback.
2705 const int allowed_milestones = 4;
2706 const int min_kernel_version = 3;
2707 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2708 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2709 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2710 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2711
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002712 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002713 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002714 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2715 .Times(1);
2716
Amin Hassani41ac04b2019-03-29 11:31:03 -07002717 fake_update_response_.deadline = "20101020";
2718 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2719 tuc_params_.is_consumer_device = false;
2720 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2721 tuc_params_.is_policy_loaded = true;
2722
2723 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002724 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002725
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002726 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002727 // was set to the current minimum kernel key version. This has
2728 // the effect of freezing roll forwards indefinitely. This will
2729 // hold the rollback window open until a future change will
2730 // be able to move this forward relative the configured window.
2731 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2732 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2733}
2734
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002735// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002736// in the TPM to logical infinity, to allow roll forward.
2737TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002738 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002739
2740 // Setup and verify some initial default values for the kernel TPM
2741 // values that control verified boot and rollback.
2742 const int allowed_milestones = 0;
2743 const int min_kernel_version = 3;
2744 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2745 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2746 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2747 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2748
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002749 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002750 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002751 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2752 .Times(1);
2753
Amin Hassani41ac04b2019-03-29 11:31:03 -07002754 fake_update_response_.deadline = "20101020";
2755 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2756 tuc_params_.is_consumer_device = false;
2757 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2758 tuc_params_.is_policy_loaded = true;
2759
2760 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002761 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002762
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002763 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002764 // was set to logical infinity.
2765 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2766 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2767}
2768
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002769TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002770 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002771 fake_update_response_.deadline = "20101020";
2772 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2773 tuc_params_.is_consumer_device = false;
2774 tuc_params_.rollback_allowed_milestones = 4;
2775 tuc_params_.is_policy_loaded = true;
2776
2777 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002778 EXPECT_TRUE(response_.update_exists);
2779 EXPECT_TRUE(response_.is_rollback);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002780}
2781
2782TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002783 fake_update_response_.rollback_firmware_version = "1.2";
2784 fake_update_response_.rollback_kernel_version = "3.4";
2785 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002786 fake_update_response_.deadline = "20101020";
2787 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2788 tuc_params_.is_consumer_device = false;
2789 tuc_params_.rollback_allowed_milestones = 4;
2790 tuc_params_.is_policy_loaded = true;
2791
2792 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002793 EXPECT_TRUE(response_.update_exists);
2794 EXPECT_TRUE(response_.is_rollback);
2795 EXPECT_EQ(1, response_.rollback_key_version.firmware_key);
2796 EXPECT_EQ(2, response_.rollback_key_version.firmware);
2797 EXPECT_EQ(3, response_.rollback_key_version.kernel_key);
2798 EXPECT_EQ(4, response_.rollback_key_version.kernel);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002799}
2800
May Lippert60aa3ca2018-08-15 16:55:29 -07002801TEST_F(OmahaRequestActionTest,
2802 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2803 FakeClock fake_clock;
2804 Time now = Time::Now();
2805 fake_clock.SetWallclockTime(now);
Amin Hassani538bd592020-11-04 20:46:08 -08002806 FakeSystemState::Get()->set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002807 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002808
Amin Hassani41ac04b2019-03-29 11:31:03 -07002809 ASSERT_TRUE(TestUpdateCheck());
2810
Amin Hassania859c922020-11-13 15:30:38 -08002811 EXPECT_TRUE(response_.update_exists);
Amin Hassani90e9f192020-11-18 14:20:56 -08002812 EXPECT_TRUE(fake_prefs_->Exists(kPrefsUpdateFirstSeenAt));
May Lippert60aa3ca2018-08-15 16:55:29 -07002813
2814 int64_t stored_first_seen_at_time;
Amin Hassani90e9f192020-11-18 14:20:56 -08002815 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateFirstSeenAt,
2816 &stored_first_seen_at_time));
May Lippert60aa3ca2018-08-15 16:55:29 -07002817 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2818}
2819
2820TEST_F(OmahaRequestActionTest,
2821 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2822 FakeClock fake_clock;
2823 Time now = Time::Now();
2824 fake_clock.SetWallclockTime(now);
Amin Hassani538bd592020-11-04 20:46:08 -08002825 FakeSystemState::Get()->set_clock(&fake_clock);
May Lippert60aa3ca2018-08-15 16:55:29 -07002826
Amin Hassani41ac04b2019-03-29 11:31:03 -07002827 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2828 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2829 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2830
2831 ASSERT_TRUE(TestUpdateCheck());
2832
Amin Hassania859c922020-11-13 15:30:38 -08002833 EXPECT_FALSE(response_.update_exists);
Amin Hassani90e9f192020-11-18 14:20:56 -08002834 EXPECT_FALSE(fake_prefs_->Exists(kPrefsUpdateFirstSeenAt));
May Lippert60aa3ca2018-08-15 16:55:29 -07002835}
2836
Xiaochu Liu88d90382018-08-29 16:09:11 -07002837TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002838 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002839 request_params_.set_dlc_apps_params(
2840 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2841 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002842 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2843
2844 ASSERT_TRUE(TestUpdateCheck());
2845
Andrewe045aef2020-01-08 16:29:22 -08002846 for (const auto& it : request_params_.dlc_apps_params()) {
Amin Hassania859c922020-11-13 15:30:38 -08002847 EXPECT_NE(string::npos, post_str_.find("appid=\"" + it.first + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002848 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002849 EXPECT_NE(string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002850 post_str_.find("appid=\"" + fake_update_response_.app_id + "\""));
Xiaochu Liu6310be62018-10-11 15:09:03 -07002851
Amin Hassania859c922020-11-13 15:30:38 -08002852 // Count number of updatecheck tag in response_.
Xiaochu Liu6310be62018-10-11 15:09:03 -07002853 int updatecheck_count = 0;
2854 size_t pos = 0;
Amin Hassania859c922020-11-13 15:30:38 -08002855 while ((pos = post_str_.find("<updatecheck", pos)) != string::npos) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002856 updatecheck_count++;
2857 pos++;
2858 }
Andrewe045aef2020-01-08 16:29:22 -08002859 EXPECT_EQ(request_params_.dlc_apps_params().size(), updatecheck_count);
Amin Hassania859c922020-11-13 15:30:38 -08002860 EXPECT_TRUE(response_.update_exists);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002861}
2862
2863TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2864 fake_update_response_.multi_app_skip_updatecheck = true;
2865 fake_update_response_.multi_app_no_update = false;
2866 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002867 request_params_.set_dlc_apps_params(
2868 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2869 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002870 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002871 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2872
2873 ASSERT_TRUE(TestUpdateCheck());
2874
Amin Hassania859c922020-11-13 15:30:38 -08002875 EXPECT_TRUE(response_.update_exists);
Amin Hassani606abc92020-11-16 12:51:37 -08002876 EXPECT_EQ(fake_update_response_.version, response_.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002877}
2878
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002879TEST_F(OmahaRequestActionTest, UpdateWithDlcTest) {
2880 request_params_.set_dlc_apps_params(
2881 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
2882 fake_update_response_.dlc_app_update = true;
2883 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002884 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002885 ASSERT_TRUE(TestUpdateCheck());
2886
Amin Hassania859c922020-11-13 15:30:38 -08002887 EXPECT_EQ(response_.packages.size(), 2u);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002888 // Two candidate URLs.
Amin Hassania859c922020-11-13 15:30:38 -08002889 EXPECT_EQ(response_.packages[1].payload_urls.size(), 2u);
2890 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002891}
2892
2893TEST_F(OmahaRequestActionTest, UpdateWithPartiallyExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002894 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2895 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002896 fake_update_response_.dlc_app_update = true;
2897 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2898 // The first DLC candidate URL is excluded.
2899 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2900 .WillOnce(Return(true))
2901 .WillOnce(Return(false));
2902 ASSERT_TRUE(TestUpdateCheck());
2903
Amin Hassania859c922020-11-13 15:30:38 -08002904 EXPECT_EQ(response_.packages.size(), 2u);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002905 // One candidate URL.
Amin Hassania859c922020-11-13 15:30:38 -08002906 EXPECT_EQ(response_.packages[1].payload_urls.size(), 1u);
2907 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002908 EXPECT_TRUE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002909}
2910
2911TEST_F(OmahaRequestActionTest, UpdateWithExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002912 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2913 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002914 fake_update_response_.dlc_app_update = true;
2915 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2916 // Both DLC candidate URLs are excluded.
2917 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2918 .WillOnce(Return(true))
2919 .WillOnce(Return(true));
2920 ASSERT_TRUE(TestUpdateCheck());
2921
Amin Hassania859c922020-11-13 15:30:38 -08002922 EXPECT_EQ(response_.packages.size(), 1u);
2923 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002924 EXPECT_FALSE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002925}
2926
2927TEST_F(OmahaRequestActionTest, UpdateWithDeprecatedDlcTest) {
2928 request_params_.set_dlc_apps_params(
2929 {{request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2930 fake_update_response_.dlc_app_no_update = true;
2931 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002932 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002933 ASSERT_TRUE(TestUpdateCheck());
2934
Amin Hassania859c922020-11-13 15:30:38 -08002935 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002936}
2937
2938TEST_F(OmahaRequestActionTest, UpdateWithDlcAndDeprecatedDlcTest) {
2939 request_params_.set_dlc_apps_params(
2940 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
2941 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2942 fake_update_response_.dlc_app_update = true;
2943 fake_update_response_.dlc_app_no_update = true;
2944 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002945 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002946 ASSERT_TRUE(TestUpdateCheck());
2947
Amin Hassania859c922020-11-13 15:30:38 -08002948 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002949}
2950
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002951TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002952 fake_update_response_.rollback = true;
2953 fake_update_response_.rollback_allowed_milestones = 4;
2954 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002955 fake_update_response_.deadline = "20101020";
2956 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2957 tuc_params_.is_consumer_device = false;
2958 tuc_params_.rollback_allowed_milestones = 4;
2959 tuc_params_.is_policy_loaded = true;
2960
2961 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002962 EXPECT_TRUE(response_.update_exists);
2963 EXPECT_TRUE(response_.is_rollback);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002964 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002965 response_.past_rollback_key_version.firmware_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002966 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002967 response_.past_rollback_key_version.firmware);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002968 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002969 response_.past_rollback_key_version.kernel_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002970 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002971 response_.past_rollback_key_version.kernel);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002972}
2973
2974TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002975 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002976 fake_update_response_.rollback = true;
2977 fake_update_response_.rollback_allowed_milestones = 4;
2978 fake_update_response_.rollback_firmware_version = "4.3";
2979 fake_update_response_.rollback_kernel_version = "2.1";
2980 fake_update_response_.past_rollback_key_version =
2981 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002982 fake_update_response_.deadline = "20101020";
2983 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2984 tuc_params_.is_consumer_device = false;
2985 tuc_params_.rollback_allowed_milestones = 4;
2986 tuc_params_.is_policy_loaded = true;
2987
2988 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002989 EXPECT_TRUE(response_.update_exists);
2990 EXPECT_TRUE(response_.is_rollback);
2991 EXPECT_EQ(16, response_.past_rollback_key_version.firmware_key);
2992 EXPECT_EQ(15, response_.past_rollback_key_version.firmware);
2993 EXPECT_EQ(14, response_.past_rollback_key_version.kernel_key);
2994 EXPECT_EQ(13, response_.past_rollback_key_version.kernel);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002995}
2996
2997TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002998 fake_update_response_.rollback = true;
2999 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07003000 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003001 request_params_.set_rollback_allowed_milestones(4);
3002
3003 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
3004 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
3005 // to 2, OmahaRequestAction will look for the key values of N-4 version but
3006 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07003007 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3008 tuc_params_.is_consumer_device = false;
3009 tuc_params_.rollback_allowed_milestones = 2;
3010 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003011
Amin Hassani41ac04b2019-03-29 11:31:03 -07003012 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08003013 EXPECT_TRUE(response_.update_exists);
3014 EXPECT_TRUE(response_.is_rollback);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003015 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003016 response_.past_rollback_key_version.firmware_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003017 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003018 response_.past_rollback_key_version.firmware);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003019 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003020 response_.past_rollback_key_version.kernel_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003021 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003022 response_.past_rollback_key_version.kernel);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003023}
3024
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003025TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
3026 request_params_.set_device_requisition("remora");
3027 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3028 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08003029 EXPECT_NE(string::npos, post_str_.find("requisition=\"remora\""));
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003030}
3031
3032TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
3033 request_params_.set_device_requisition("");
3034 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3035 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08003036 EXPECT_EQ(string::npos, post_str_.find("requisition"));
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003037}
3038
Amin Hassani2cbb0692019-10-30 13:36:17 -07003039TEST_F(OmahaRequestActionTest, PersistEolDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003040 tuc_params_.http_response =
3041 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Amin Hassani606abc92020-11-16 12:51:37 -08003042 "protocol=\"3.0\"><app appid=\"test-app-id\" status=\"ok\">"
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003043 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003044 "_eol_date=\"200\" _foo=\"bar\"/></app></response>";
3045 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3046 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3047
3048 ASSERT_TRUE(TestUpdateCheck());
3049
3050 string eol_date;
Amin Hassani538bd592020-11-04 20:46:08 -08003051 EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetString(kPrefsOmahaEolDate,
3052 &eol_date));
Amin Hassani2cbb0692019-10-30 13:36:17 -07003053 EXPECT_EQ("200", eol_date);
3054}
3055
3056TEST_F(OmahaRequestActionTest, PersistEolMissingDateTest) {
3057 tuc_params_.http_response =
3058 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Amin Hassani606abc92020-11-16 12:51:37 -08003059 "protocol=\"3.0\"><app appid=\"test-app-id\" status=\"ok\">"
Amin Hassani2cbb0692019-10-30 13:36:17 -07003060 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003061 "_foo=\"bar\"/></app></response>";
3062 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3063 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3064
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003065 const string kDate = "123";
Amin Hassani538bd592020-11-04 20:46:08 -08003066 FakeSystemState::Get()->prefs()->SetString(kPrefsOmahaEolDate, kDate);
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003067
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003068 ASSERT_TRUE(TestUpdateCheck());
3069
Amin Hassani2cbb0692019-10-30 13:36:17 -07003070 string eol_date;
Amin Hassani538bd592020-11-04 20:46:08 -08003071 EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetString(kPrefsOmahaEolDate,
3072 &eol_date));
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003073 EXPECT_EQ(kDate, eol_date);
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003074}
3075
Amin Hassani2cbb0692019-10-30 13:36:17 -07003076TEST_F(OmahaRequestActionTest, PersistEolBadDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003077 tuc_params_.http_response =
3078 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Amin Hassani606abc92020-11-16 12:51:37 -08003079 "protocol=\"3.0\"><app appid=\"test-app-id\" status=\"ok\">"
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003080 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003081 "_eol_date=\"bad\" foo=\"bar\"/></app></response>";
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003082 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3083 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3084
3085 ASSERT_TRUE(TestUpdateCheck());
3086
Amin Hassani2cbb0692019-10-30 13:36:17 -07003087 string eol_date;
Amin Hassani538bd592020-11-04 20:46:08 -08003088 EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetString(kPrefsOmahaEolDate,
3089 &eol_date));
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003090 EXPECT_EQ(kEolDateInvalid, StringToEolDate(eol_date));
3091}
3092
Andrew065d78d2020-04-07 15:43:07 -07003093TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyNoPing) {
3094 OmahaRequestParams::AppParams app_param = {.name = dlc_id_};
Andrewe045aef2020-01-08 16:29:22 -08003095 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003096 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003097
3098 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003099
3100 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003101 // If there was no ping, the metadata files shouldn't exist yet.
Amin Hassani90e9f192020-11-18 14:20:56 -08003102 EXPECT_FALSE(fake_prefs_->GetInt64(active_key_, &temp_int));
3103 EXPECT_FALSE(fake_prefs_->GetInt64(last_active_key_, &temp_int));
3104 EXPECT_FALSE(fake_prefs_->GetInt64(last_rollcall_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003105}
3106
Andrew065d78d2020-04-07 15:43:07 -07003107TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyActiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003108 // Create Active value
Amin Hassani90e9f192020-11-18 14:20:56 -08003109 fake_prefs_->SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003110
3111 OmahaRequestParams::AppParams app_param = {
3112 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003113 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003114 .ping_active = 1,
3115 .send_ping = true};
3116 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003117 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003118
Andrew065d78d2020-04-07 15:43:07 -07003119 int64_t temp_int;
3120 string temp_str;
Andrewe045aef2020-01-08 16:29:22 -08003121 ASSERT_TRUE(TestUpdateCheck());
Amin Hassani90e9f192020-11-18 14:20:56 -08003122 EXPECT_TRUE(fake_prefs_->GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003123 EXPECT_EQ(temp_int, kPingInactiveValue);
Amin Hassani90e9f192020-11-18 14:20:56 -08003124 EXPECT_TRUE(fake_prefs_->GetString(last_active_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003125 EXPECT_EQ(temp_str, "4763");
Amin Hassani90e9f192020-11-18 14:20:56 -08003126 EXPECT_TRUE(fake_prefs_->GetString(last_rollcall_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003127 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003128}
3129
Andrew065d78d2020-04-07 15:43:07 -07003130TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyInactiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003131 // Create Active value
Amin Hassani90e9f192020-11-18 14:20:56 -08003132 fake_prefs_->SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003133
3134 OmahaRequestParams::AppParams app_param = {
3135 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003136 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003137 .ping_active = 0,
3138 .send_ping = true};
3139 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003140 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003141
3142 // Set the previous active value to an older value than 4763.
Amin Hassani90e9f192020-11-18 14:20:56 -08003143 fake_prefs_->SetString(last_active_key_, "555");
Andrewe045aef2020-01-08 16:29:22 -08003144
Andrew065d78d2020-04-07 15:43:07 -07003145 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003146 ASSERT_TRUE(TestUpdateCheck());
Amin Hassani90e9f192020-11-18 14:20:56 -08003147 EXPECT_TRUE(fake_prefs_->GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003148 EXPECT_EQ(temp_int, kPingInactiveValue);
Andrew065d78d2020-04-07 15:43:07 -07003149 string temp_str;
Amin Hassani90e9f192020-11-18 14:20:56 -08003150 EXPECT_TRUE(fake_prefs_->GetString(last_active_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003151 EXPECT_EQ(temp_str, "555");
Amin Hassani90e9f192020-11-18 14:20:56 -08003152 EXPECT_TRUE(fake_prefs_->GetString(last_rollcall_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003153 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003154}
3155
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07003156TEST_F(OmahaRequestActionTest, OmahaResponseUpdateCanExcludeCheck) {
3157 request_params_.set_dlc_apps_params(
3158 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3159 fake_update_response_.dlc_app_update = true;
3160 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3161
3162 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3163 ASSERT_TRUE(TestUpdateCheck());
3164 ASSERT_TRUE(delegate_.omaha_response_);
3165 const auto& packages = delegate_.omaha_response_->packages;
3166 ASSERT_EQ(packages.size(), 2);
3167
3168 EXPECT_FALSE(packages[0].can_exclude);
3169 EXPECT_TRUE(packages[1].can_exclude);
3170}
3171
3172TEST_F(OmahaRequestActionTest, OmahaResponseInstallCannotExcludeCheck) {
3173 request_params_.set_is_install(true);
3174 request_params_.set_dlc_apps_params(
3175 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3176 fake_update_response_.dlc_app_update = true;
3177 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3178
3179 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3180 ASSERT_TRUE(TestUpdateCheck());
3181 ASSERT_TRUE(delegate_.omaha_response_);
3182 const auto& packages = delegate_.omaha_response_->packages;
3183 ASSERT_EQ(packages.size(), 2);
3184
3185 EXPECT_FALSE(packages[0].can_exclude);
3186 EXPECT_FALSE(packages[1].can_exclude);
3187}
3188
Darin Petkov6a5b3222010-07-13 14:55:28 -07003189} // namespace chromeos_update_engine