blob: 89648eb396052e1f5e759b6b4438688cea898690 [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;
245 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700246 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700247 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700248 string more_info_url = "http://more/info";
249 string prompt = "true";
250 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700251 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700252 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700253 string hash = "4841534831323334";
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800254 string fp = "3.98ba213e";
255 string fp2 = "3.755aff78e";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800256 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700257 string deadline = "";
258 string max_days_to_scatter = "7";
259 string elapsed_days = "42";
260
261 // P2P setting defaults to allowed.
262 bool disable_p2p_for_downloading = false;
263 bool disable_p2p_for_sharing = false;
264
Sen Jiangfe284402018-03-21 14:03:50 -0700265 bool powerwash = false;
266
Alex Deymo8e18f932015-03-27 16:16:59 -0700267 // Omaha cohorts settings.
268 bool include_cohorts = false;
269 string cohort = "";
270 string cohorthint = "";
271 string cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800272 // Whether to include Omaha cohorts for DLC apps.
273 bool include_dlc_cohorts = false;
274 string dlc_cohort = "";
275 string dlc_cohorthint = "";
276 string dlc_cohortname = "";
Alex Deymo8e18f932015-03-27 16:16:59 -0700277
278 // Whether to include the CrOS <!ENTITY> in the XML response.
279 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800280
Sen Jiang81259682017-03-30 15:11:30 -0700281 // Whether to include more than one app.
282 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700283 // Whether to include an app with noupdate="true".
284 bool multi_app_self_update = false;
285 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700286 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700287 // Whether to include an additional app with no updatecheck tag.
288 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700289 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800290 bool multi_package = false;
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800291 // Whether to include a DLC app with updatecheck tag.
292 bool dlc_app_update = false;
293 // Whether to include a DLC app with no updatecheck tag.
294 bool dlc_app_no_update = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700295
296 // Whether the payload is a rollback.
297 bool rollback = false;
298 // The verified boot firmware key version for the rollback image.
299 string rollback_firmware_version = "";
300 // The verified boot kernel key version for the rollback image.
301 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700302 // The number of milestones back that the verified boot key version has been
303 // supplied.
304 uint32_t rollback_allowed_milestones = 0;
305 // The verified boot key version for the
306 // |current - rollback_allowed_milestones| most recent release.
307 // The pair contains <firmware_key_version, kernel_key_version> each
308 // of which is in the form "key_version.version".
309 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700310};
311
312} // namespace
313
Darin Petkov6a5b3222010-07-13 14:55:28 -0700314namespace chromeos_update_engine {
315
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700316class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
317 public:
318 OmahaRequestActionTestProcessorDelegate()
319 : expected_code_(ErrorCode::kSuccess),
320 interactive_(false),
321 test_http_fetcher_headers_(false) {}
322 ~OmahaRequestActionTestProcessorDelegate() override = default;
323
324 void ProcessingDone(const ActionProcessor* processor,
325 ErrorCode code) override {
326 brillo::MessageLoop::current()->BreakLoop();
327 }
328
329 void ActionCompleted(ActionProcessor* processor,
330 AbstractAction* action,
331 ErrorCode code) override {
332 // Make sure actions always succeed.
333 if (action->Type() == OmahaRequestAction::StaticType()) {
334 EXPECT_EQ(expected_code_, code);
335 // Check that the headers were set in the fetcher during the action. Note
336 // that we set this request as "interactive".
337 auto fetcher = static_cast<const MockHttpFetcher*>(
338 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
339
340 if (test_http_fetcher_headers_) {
341 EXPECT_EQ(interactive_ ? "fg" : "bg",
342 fetcher->GetHeader("X-Goog-Update-Interactivity"));
343 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
344 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
345 }
346 post_data_ = fetcher->post_data();
347 } else if (action->Type() ==
348 ObjectCollectorAction<OmahaResponse>::StaticType()) {
349 EXPECT_EQ(ErrorCode::kSuccess, code);
350 auto collector_action =
351 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
352 omaha_response_.reset(new OmahaResponse(collector_action->object()));
353 EXPECT_TRUE(omaha_response_);
354 } else {
355 EXPECT_EQ(ErrorCode::kSuccess, code);
356 }
357 }
358 ErrorCode expected_code_;
359 brillo::Blob post_data_;
360 bool interactive_;
361 bool test_http_fetcher_headers_;
362 std::unique_ptr<OmahaResponse> omaha_response_;
363};
364
Amin Hassani41ac04b2019-03-29 11:31:03 -0700365struct TestUpdateCheckParams {
366 string http_response;
367 int fail_http_response_code;
368 bool ping_only;
369 bool is_consumer_device;
370 int rollback_allowed_milestones;
371 bool is_policy_loaded;
372 ErrorCode expected_code;
373 metrics::CheckResult expected_check_result;
374 metrics::CheckReaction expected_check_reaction;
375 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700376 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700377};
378
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700379class OmahaRequestActionTest : public ::testing::Test {
380 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800381 void SetUp() override {
Amin Hassani538bd592020-11-04 20:46:08 -0800382 FakeSystemState::CreateInstance();
383
Marton Hunyady2abda312018-04-24 18:21:49 +0200384 request_params_.set_os_sp("service_pack");
385 request_params_.set_os_board("x86-generic");
386 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700387 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200388 request_params_.set_app_lang("en-US");
389 request_params_.set_current_channel("unittest");
390 request_params_.set_target_channel("unittest");
391 request_params_.set_hwid("OEM MODEL 09235 7471");
Marton Hunyady2abda312018-04-24 18:21:49 +0200392 request_params_.set_delta_okay(true);
393 request_params_.set_interactive(false);
394 request_params_.set_update_url("http://url");
395 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200396 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200397 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700398 request_params_.set_is_install(false);
Andrewe045aef2020-01-08 16:29:22 -0800399 request_params_.set_dlc_apps_params({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200400
Amin Hassani538bd592020-11-04 20:46:08 -0800401 FakeSystemState::Get()->set_request_params(&request_params_);
Amin Hassani90e9f192020-11-18 14:20:56 -0800402 fake_prefs_ = FakeSystemState::Get()->fake_prefs();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700403
404 // Setting the default update check params. Lookup |TestUpdateCheck()|.
405 tuc_params_ = {
406 .http_response = "",
407 .fail_http_response_code = -1,
408 .ping_only = false,
409 .is_consumer_device = true,
410 .rollback_allowed_milestones = 0,
411 .is_policy_loaded = false,
412 .expected_code = ErrorCode::kSuccess,
413 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
414 .expected_check_reaction = metrics::CheckReaction::kUpdating,
415 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
416 };
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700417
Amin Hassani538bd592020-11-04 20:46:08 -0800418 ON_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetExcluder())
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700419 .WillByDefault(Return(&mock_excluder_));
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700420 }
421
Andrewe045aef2020-01-08 16:29:22 -0800422 // This function uses the parameters in |tuc_params_| to do an update check.
Amin Hassania859c922020-11-13 15:30:38 -0800423 // It will fill out |post_str_| with the result data and |response| with
Amin Hassani41ac04b2019-03-29 11:31:03 -0700424 // |OmahaResponse|. Returns true iff an output response was obtained from the
425 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
426 // transfer will fail with that code. |ping_only| is passed through to the
427 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700428 //
429 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700430 // |expected_error_code| parameters are for checking expectations about
431 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
432 // statistics. Use the appropriate ::kUnset value to specify that the given
433 // metric should not be reported.
434 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700435
Amin Hassania859c922020-11-13 15:30:38 -0800436 // Tests events using |event| and |https_response|. It will fill up
437 // |post_str_| with the result data.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700438 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700439
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800440 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700441 // only a ping or also an updatecheck.
442 void PingTest(bool ping_only);
443
444 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800445 bool InstallDateParseHelper(const string& elapsed_days,
446 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700447
448 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800449 void P2PTest(bool initial_allow_p2p_for_downloading,
450 bool initial_allow_p2p_for_sharing,
451 bool omaha_disable_p2p_for_downloading,
452 bool omaha_disable_p2p_for_sharing,
453 bool payload_state_allow_p2p_attempt,
454 bool expect_p2p_client_lookup,
455 const string& p2p_client_result_url,
456 bool expected_allow_p2p_for_downloading,
457 bool expected_allow_p2p_for_sharing,
458 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700459
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700460 StrictMock<MockExcluder> mock_excluder_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700461 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200462 // Used by all tests.
Amin Hassani538bd592020-11-04 20:46:08 -0800463 OmahaRequestParams request_params_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700464
Amin Hassani90e9f192020-11-18 14:20:56 -0800465 FakePrefs* fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700466
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700467 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700468
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700469 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700470
471 TestUpdateCheckParams tuc_params_;
472
Amin Hassania859c922020-11-13 15:30:38 -0800473 OmahaResponse response_;
474 string post_str_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700475};
476
Andrew065d78d2020-04-07 15:43:07 -0700477class OmahaRequestActionDlcPingTest : public OmahaRequestActionTest {
478 protected:
479 void SetUp() override {
480 OmahaRequestActionTest::SetUp();
481 dlc_id_ = "dlc0";
482 active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700483 {kDlcPrefsSubDir, dlc_id_, kPrefsPingActive});
Andrew065d78d2020-04-07 15:43:07 -0700484 last_active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700485 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastActive});
Andrew065d78d2020-04-07 15:43:07 -0700486 last_rollcall_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700487 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastRollcall});
Andrew065d78d2020-04-07 15:43:07 -0700488
489 tuc_params_.http_response =
490 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
491 "protocol=\"3.0\"><daystart elapsed_days=\"4763\" "
492 "elapsed_seconds=\"36540\"/><app appid=\"test-app-id\" status=\"ok\">\""
493 "<updatecheck status=\"noupdate\"/></app><app "
494 "appid=\"test-app-id_dlc0\" "
495 "status=\"ok\"><ping status=\"ok\"/><updatecheck status=\"noupdate\"/>"
496 "</app></response>";
497 tuc_params_.expected_check_result =
498 metrics::CheckResult::kNoUpdateAvailable;
499 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
500 }
501
502 std::string dlc_id_;
503 std::string active_key_;
504 std::string last_active_key_;
505 std::string last_rollcall_key_;
506};
Amin Hassani90e9f192020-11-18 14:20:56 -0800507
Amin Hassani41ac04b2019-03-29 11:31:03 -0700508bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700509 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700510 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700511 auto fetcher =
512 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
513 tuc_params_.http_response.size(),
514 nullptr);
515 if (tuc_params_.fail_http_response_code >= 0) {
516 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700517 }
Amin Hassani538bd592020-11-04 20:46:08 -0800518 // This ensures the tests didn't forget to update |FakeSystemState| if they
Amin Hassani61789032020-11-13 16:20:12 -0800519 // are not using the default |request_params_|.
Amin Hassani538bd592020-11-04 20:46:08 -0800520 EXPECT_EQ(&request_params_, FakeSystemState::Get()->request_params());
Marton Hunyady2abda312018-04-24 18:21:49 +0200521
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700522 auto omaha_request_action =
Amin Hassani538bd592020-11-04 20:46:08 -0800523 std::make_unique<OmahaRequestAction>(nullptr,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700524 std::move(fetcher),
525 tuc_params_.ping_only,
526 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700527
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800528 auto mock_policy_provider =
529 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
530 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700531 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800532
533 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700534 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800535
536 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700537 const bool get_allowed_milestone_succeeds =
538 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800539 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700540 .WillRepeatedly(
541 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
542 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800543
544 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
545 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700546 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800547
Amin Hassani41ac04b2019-03-29 11:31:03 -0700548 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700549 delegate_.interactive_ = request_params_.interactive();
550 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700551 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700552 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700553
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700554 auto collector_action =
555 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
556 BondActions(omaha_request_action.get(), collector_action.get());
557 processor.EnqueueAction(std::move(omaha_request_action));
558 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700559
Amin Hassani538bd592020-11-04 20:46:08 -0800560 EXPECT_CALL(*FakeSystemState::Get()->mock_metrics_reporter(),
561 ReportUpdateCheckMetrics(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800562 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700563
Amin Hassani41ac04b2019-03-29 11:31:03 -0700564 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -0800565 *FakeSystemState::Get()->mock_metrics_reporter(),
566 ReportUpdateCheckMetrics(tuc_params_.expected_check_result,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700567 tuc_params_.expected_check_reaction,
568 tuc_params_.expected_download_error_code))
569 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800570
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700571 loop.PostTask(base::Bind(
572 [](ActionProcessor* processor) { processor->StartProcessing(); },
573 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700574 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700575 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700576 if (delegate_.omaha_response_)
Amin Hassania859c922020-11-13 15:30:38 -0800577 response_ = *delegate_.omaha_response_;
578 post_str_ = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700579 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700580}
581
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700582// Tests Event requests -- they should always succeed. |out_post_data| may be
583// null; if non-null, the post-data received by the mock HttpFetcher is
584// returned.
585void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700586 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700587 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700588 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700589
590 auto action = std::make_unique<OmahaRequestAction>(
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700591 event,
592 std::make_unique<MockHttpFetcher>(
593 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700594 false,
595 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700596 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700597 processor.set_delegate(&delegate_);
598 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700599
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700600 loop.PostTask(base::Bind(
601 [](ActionProcessor* processor) { processor->StartProcessing(); },
602 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700603 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700604 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700605
Amin Hassania859c922020-11-13 15:30:38 -0800606 post_str_ = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700607}
608
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700609TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700610 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700611 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
612 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
613 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
614 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
615
616 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800617 EXPECT_FALSE(response_.update_exists);
David Zeuthenf3e28012014-08-26 18:23:52 -0400618}
619
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700620TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700621 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
622 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
623 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
624
625 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800626 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700627}
628
Sen Jiang81259682017-03-30 15:11:30 -0700629TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700630 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700631 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
632 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
633 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
634
635 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800636 EXPECT_FALSE(response_.update_exists);
Sen Jiang81259682017-03-30 15:11:30 -0700637}
638
Sen Jiang00adf7b2017-06-26 15:57:29 -0700639TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700640 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700641 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
642 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
643 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
644
645 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800646 EXPECT_FALSE(response_.update_exists);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700647}
648
649TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700650 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700651 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
652 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700653 "</manifest></updatecheck></app></response>";
654 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
655 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
656
657 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -0800658 EXPECT_FALSE(response_.update_exists);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700659}
660
Alex Deymo8e18f932015-03-27 16:16:59 -0700661// Test that all the values in the response are parsed in a normal update
Amin Hassania859c922020-11-13 15:30:38 -0800662// response_.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700663TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700664 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700665 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
666
667 ASSERT_TRUE(TestUpdateCheck());
668
Amin Hassania859c922020-11-13 15:30:38 -0800669 EXPECT_TRUE(response_.update_exists);
670 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800671 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800672 response_.packages[0].payload_urls[0]);
673 EXPECT_EQ(fake_update_response_.more_info_url, response_.more_info_url);
674 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
675 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
676 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
677 EXPECT_EQ(true, response_.packages[0].is_delta);
678 EXPECT_EQ(fake_update_response_.prompt == "true", response_.prompt);
679 EXPECT_EQ(fake_update_response_.deadline, response_.deadline);
680 EXPECT_FALSE(response_.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700681 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700682 // persisted.
Amin Hassani90e9f192020-11-18 14:20:56 -0800683 EXPECT_FALSE(fake_prefs_->Exists(kPrefsOmahaCohort));
684 EXPECT_FALSE(fake_prefs_->Exists(kPrefsOmahaCohortHint));
685 EXPECT_FALSE(fake_prefs_->Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700686}
687
Sen Jiang0affc2c2017-02-10 15:55:05 -0800688TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800689 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700690 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
691
692 ASSERT_TRUE(TestUpdateCheck());
693
Amin Hassania859c922020-11-13 15:30:38 -0800694 EXPECT_TRUE(response_.update_exists);
695 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800696 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800697 response_.packages[0].payload_urls[0]);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800698 EXPECT_EQ(fake_update_response_.codebase + "package2",
Amin Hassania859c922020-11-13 15:30:38 -0800699 response_.packages[1].payload_urls[0]);
700 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
701 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
702 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
703 EXPECT_EQ(true, response_.packages[0].is_delta);
704 EXPECT_EQ(11u, response_.packages[0].metadata_size);
705 ASSERT_EQ(2u, response_.packages.size());
706 EXPECT_EQ(string("hash2"), response_.packages[1].hash);
707 EXPECT_EQ(222u, response_.packages[1].size);
708 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
709 EXPECT_EQ(22u, response_.packages[1].metadata_size);
710 EXPECT_EQ(false, response_.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700711}
712
713TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700714 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700715 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
716
717 ASSERT_TRUE(TestUpdateCheck());
718
Amin Hassania859c922020-11-13 15:30:38 -0800719 EXPECT_TRUE(response_.update_exists);
720 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang81259682017-03-30 15:11:30 -0700721 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800722 response_.packages[0].payload_urls[0]);
Sen Jiang81259682017-03-30 15:11:30 -0700723 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
Amin Hassania859c922020-11-13 15:30:38 -0800724 response_.packages[1].payload_urls[0]);
725 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
726 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
727 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
728 EXPECT_EQ(11u, response_.packages[0].metadata_size);
729 EXPECT_EQ(true, response_.packages[0].is_delta);
730 ASSERT_EQ(2u, response_.packages.size());
731 EXPECT_EQ(string("hash3"), response_.packages[1].hash);
732 EXPECT_EQ(333u, response_.packages[1].size);
733 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
734 EXPECT_EQ(33u, response_.packages[1].metadata_size);
735 EXPECT_EQ(false, response_.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700736}
737
738TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700739 fake_update_response_.multi_app = true;
740 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700741 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
742
743 ASSERT_TRUE(TestUpdateCheck());
744
Amin Hassania859c922020-11-13 15:30:38 -0800745 EXPECT_TRUE(response_.update_exists);
746 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang81259682017-03-30 15:11:30 -0700747 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800748 response_.packages[0].payload_urls[0]);
749 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
750 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
751 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
752 EXPECT_EQ(11u, response_.packages[0].metadata_size);
753 ASSERT_EQ(2u, response_.packages.size());
754 EXPECT_EQ(string("hash3"), response_.packages[1].hash);
755 EXPECT_EQ(333u, response_.packages[1].size);
756 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
757 EXPECT_EQ(33u, response_.packages[1].metadata_size);
758 EXPECT_EQ(true, response_.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700759}
760
761TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700762 fake_update_response_.multi_app = true;
763 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700764 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
765
766 ASSERT_TRUE(TestUpdateCheck());
767
Amin Hassania859c922020-11-13 15:30:38 -0800768 EXPECT_TRUE(response_.update_exists);
769 EXPECT_EQ(fake_update_response_.version, response_.version);
Sen Jiang81259682017-03-30 15:11:30 -0700770 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
Amin Hassania859c922020-11-13 15:30:38 -0800771 response_.packages[0].payload_urls[0]);
Sen Jiang81259682017-03-30 15:11:30 -0700772 EXPECT_EQ(fake_update_response_.codebase + "package2",
Amin Hassania859c922020-11-13 15:30:38 -0800773 response_.packages[1].payload_urls[0]);
Sen Jiang81259682017-03-30 15:11:30 -0700774 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
Amin Hassania859c922020-11-13 15:30:38 -0800775 response_.packages[2].payload_urls[0]);
776 EXPECT_EQ(fake_update_response_.hash, response_.packages[0].hash);
777 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
778 EXPECT_EQ(fake_update_response_.fp, response_.packages[0].fp);
779 EXPECT_EQ(11u, response_.packages[0].metadata_size);
780 EXPECT_EQ(true, response_.packages[0].is_delta);
781 ASSERT_EQ(3u, response_.packages.size());
782 EXPECT_EQ(string("hash2"), response_.packages[1].hash);
783 EXPECT_EQ(222u, response_.packages[1].size);
784 EXPECT_EQ(fake_update_response_.fp2, response_.packages[1].fp);
785 EXPECT_EQ(22u, response_.packages[1].metadata_size);
786 EXPECT_EQ(false, response_.packages[1].is_delta);
787 EXPECT_EQ(string("hash3"), response_.packages[2].hash);
788 EXPECT_EQ(333u, response_.packages[2].size);
789 EXPECT_EQ(fake_update_response_.fp2, response_.packages[2].fp);
790 EXPECT_EQ(33u, response_.packages[2].metadata_size);
791 EXPECT_EQ(false, response_.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800792}
793
Sen Jiangfe284402018-03-21 14:03:50 -0700794TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700795 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700796 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
797
798 ASSERT_TRUE(TestUpdateCheck());
799
Amin Hassania859c922020-11-13 15:30:38 -0800800 EXPECT_TRUE(response_.update_exists);
801 EXPECT_TRUE(response_.powerwash_required);
Sen Jiangfe284402018-03-21 14:03:50 -0700802}
803
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700804TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700805 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700806 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700807 tuc_params_.http_response = "invalid xml>";
808 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
809 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
810 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
811
812 ASSERT_FALSE(TestUpdateCheck());
813
Amin Hassania859c922020-11-13 15:30:38 -0800814 EXPECT_FALSE(response_.update_exists);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700815}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700816
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700817TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700818 request_params_.set_interactive(false);
819 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700820 tuc_params_.http_response = "invalid xml>";
821 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
822 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
823 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
824
825 ASSERT_FALSE(TestUpdateCheck());
826
Amin Hassania859c922020-11-13 15:30:38 -0800827 EXPECT_FALSE(response_.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700828}
829
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700830TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700831 // Set up a connection manager that doesn't allow a valid update over
832 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700833 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -0800834 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700835
Alex Deymo30534502015-07-20 15:06:33 -0700836 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800837 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
838 SetArgPointee<1>(ConnectionTethering::kUnknown),
839 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700840 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
841 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700842
Amin Hassani41ac04b2019-03-29 11:31:03 -0700843 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
844 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
845 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
846
847 ASSERT_FALSE(TestUpdateCheck());
848
Amin Hassania859c922020-11-13 15:30:38 -0800849 EXPECT_FALSE(response_.update_exists);
Chris Sosa77f79e82014-06-02 18:16:24 -0700850}
851
Weidong Guo421ff332017-04-17 10:08:38 -0700852TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
853 // This test tests that update over cellular is allowed as device policy
854 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700855 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -0800856 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700857
858 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
859 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
860 SetArgPointee<1>(ConnectionTethering::kUnknown),
861 Return(true)));
862 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
863 .WillRepeatedly(Return(true));
864 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
865 .WillRepeatedly(Return(true));
866
Amin Hassani41ac04b2019-03-29 11:31:03 -0700867 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
868
869 ASSERT_TRUE(TestUpdateCheck());
870
Amin Hassania859c922020-11-13 15:30:38 -0800871 EXPECT_TRUE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700872}
873
874TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
875 // This test tests that update over cellular is blocked as device policy
876 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700877 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -0800878 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700879
880 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
881 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
882 SetArgPointee<1>(ConnectionTethering::kUnknown),
883 Return(true)));
884 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
885 .WillRepeatedly(Return(true));
886 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
887 .WillRepeatedly(Return(false));
888
Amin Hassani41ac04b2019-03-29 11:31:03 -0700889 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
890 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
891 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
892
893 ASSERT_FALSE(TestUpdateCheck());
894
Amin Hassania859c922020-11-13 15:30:38 -0800895 EXPECT_FALSE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700896}
897
898TEST_F(OmahaRequestActionTest,
899 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
900 // This test tests that, when device policy is not set, update over cellular
901 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700902 MockConnectionManager mock_cm;
Amin Hassani90e9f192020-11-18 14:20:56 -0800903 fake_prefs_->SetBoolean(kPrefsUpdateOverCellularPermission, true);
Amin Hassani538bd592020-11-04 20:46:08 -0800904 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700905
906 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
907 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
908 SetArgPointee<1>(ConnectionTethering::kUnknown),
909 Return(true)));
910 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
911 .WillRepeatedly(Return(false));
912 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
913 .WillRepeatedly(Return(true));
914
Amin Hassani41ac04b2019-03-29 11:31:03 -0700915 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
916
917 ASSERT_TRUE(TestUpdateCheck());
918
Amin Hassania859c922020-11-13 15:30:38 -0800919 EXPECT_TRUE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700920}
921
922TEST_F(OmahaRequestActionTest,
923 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
924 // This test tests that, when device policy is not set and permission for
925 // update over cellular is set to false or does not exist, update over
926 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700927 MockConnectionManager mock_cm;
928 // A version different from the version in omaha response.
929 string diff_version = "99.99.99";
930 // A size different from the size in omaha response.
931 int64_t diff_size = 999;
932
Amin Hassani90e9f192020-11-18 14:20:56 -0800933 fake_prefs_->SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
934 fake_prefs_->SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
Weidong Guo421ff332017-04-17 10:08:38 -0700935 // This test tests cellular (3G) being the only connection type being allowed.
Amin Hassani538bd592020-11-04 20:46:08 -0800936 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700937
938 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
939 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
940 SetArgPointee<1>(ConnectionTethering::kUnknown),
941 Return(true)));
942 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
943 .WillRepeatedly(Return(false));
944 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
945 .WillRepeatedly(Return(true));
946
Amin Hassani41ac04b2019-03-29 11:31:03 -0700947 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
948 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
949 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
950
951 ASSERT_FALSE(TestUpdateCheck());
952
Amin Hassania859c922020-11-13 15:30:38 -0800953 EXPECT_FALSE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700954}
955
956TEST_F(OmahaRequestActionTest,
957 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
958 // This test tests that, when device policy is not set and permission for
959 // update over cellular is set to false or does not exist, update over
960 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700961 MockConnectionManager mock_cm;
962 // A version same as the version in omaha response.
963 string new_version = fake_update_response_.version;
964 // A size same as the size in omaha response.
965 int64_t new_size = fake_update_response_.size;
966
Amin Hassani90e9f192020-11-18 14:20:56 -0800967 fake_prefs_->SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
968 fake_prefs_->SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
Amin Hassani538bd592020-11-04 20:46:08 -0800969 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Weidong Guo421ff332017-04-17 10:08:38 -0700970
971 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
972 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
973 SetArgPointee<1>(ConnectionTethering::kUnknown),
974 Return(true)));
975 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
976 .WillRepeatedly(Return(false));
977 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
978 .WillRepeatedly(Return(true));
979
Amin Hassani41ac04b2019-03-29 11:31:03 -0700980 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
981
982 ASSERT_TRUE(TestUpdateCheck());
983
Amin Hassania859c922020-11-13 15:30:38 -0800984 EXPECT_TRUE(response_.update_exists);
Weidong Guo421ff332017-04-17 10:08:38 -0700985}
986
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700987TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700988 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700989 MockPayloadState mock_payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -0800990 FakeSystemState::Get()->set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700991 fake_update_response_.version = rollback_version;
992 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
993 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
994 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700995
Chris Sosa77f79e82014-06-02 18:16:24 -0700996 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800997 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700998
Amin Hassani41ac04b2019-03-29 11:31:03 -0700999 ASSERT_FALSE(TestUpdateCheck());
1000
Amin Hassania859c922020-11-13 15:30:38 -08001001 EXPECT_FALSE(response_.update_exists);
Chris Sosa77f79e82014-06-02 18:16:24 -07001002}
1003
Marton Hunyadyc2882062018-05-14 17:28:25 +02001004// Verify that update checks called during OOBE will not try to download an
1005// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -07001006TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Amin Hassani538bd592020-11-04 20:46:08 -08001007 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -07001008 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1009 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1010 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1011 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -07001012
Sen Jiang8cd42342018-01-31 12:06:59 -08001013 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001014 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001015 ASSERT_FALSE(TestUpdateCheck());
1016
Amin Hassania859c922020-11-13 15:30:38 -08001017 EXPECT_FALSE(response_.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001018}
Kevin Cernekee2494e282016-03-29 18:03:53 -07001019
Marton Hunyadyc2882062018-05-14 17:28:25 +02001020// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
1021// enabled.
1022TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Amin Hassani538bd592020-11-04 20:46:08 -08001023 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
1024 FakeSystemState::Get()->fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001025 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -07001026
Amin Hassani41ac04b2019-03-29 11:31:03 -07001027 ASSERT_TRUE(TestUpdateCheck());
1028
Amin Hassania859c922020-11-13 15:30:38 -08001029 EXPECT_TRUE(response_.update_exists);
Kevin Cernekee2494e282016-03-29 18:03:53 -07001030}
1031
Marton Hunyadyc2882062018-05-14 17:28:25 +02001032// Verify that update checks called during OOBE will still try to download an
1033// update if the response includes the deadline field.
1034TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Amin Hassani538bd592020-11-04 20:46:08 -08001035 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001036 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001037 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001038
Amin Hassani41ac04b2019-03-29 11:31:03 -07001039 ASSERT_TRUE(TestUpdateCheck());
1040
Amin Hassania859c922020-11-13 15:30:38 -08001041 EXPECT_TRUE(response_.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001042}
1043
1044// Verify that update checks called during OOBE will not try to download an
1045// update if a rollback happened, even when the response includes the deadline
1046// field.
1047TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08001048 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001049 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001050 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1051 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1052 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1053 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1054
Amin Hassani538bd592020-11-04 20:46:08 -08001055 EXPECT_CALL(*(FakeSystemState::Get()->mock_payload_state()),
1056 GetRollbackHappened())
Marton Hunyadyc2882062018-05-14 17:28:25 +02001057 .WillOnce(Return(true));
1058
Amin Hassani41ac04b2019-03-29 11:31:03 -07001059 ASSERT_FALSE(TestUpdateCheck());
1060
Amin Hassania859c922020-11-13 15:30:38 -08001061 EXPECT_FALSE(response_.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001062}
1063
Toni Barzic61544e62018-10-11 14:37:30 -07001064// Verify that non-critical updates are skipped by reporting the
1065// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1066// i.e. when the update would need user permission. Note that reporting
1067// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1068// in OOBE (warning the user about an update that will be skipped).
1069TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Amin Hassani538bd592020-11-04 20:46:08 -08001070 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Toni Barzic61544e62018-10-11 14:37:30 -07001071
1072 MockConnectionManager mock_cm;
Amin Hassani538bd592020-11-04 20:46:08 -08001073 FakeSystemState::Get()->set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001074 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1075 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1076 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1077 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001078
1079 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1080 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1081 SetArgPointee<1>(ConnectionTethering::kUnknown),
1082 Return(true)));
1083 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1084 .WillRepeatedly(Return(false));
1085
Amin Hassani41ac04b2019-03-29 11:31:03 -07001086 ASSERT_FALSE(TestUpdateCheck());
1087
Amin Hassania859c922020-11-13 15:30:38 -08001088 EXPECT_FALSE(response_.update_exists);
Toni Barzic61544e62018-10-11 14:37:30 -07001089}
1090
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001091TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001092 request_params_.set_wall_clock_based_wait_enabled(true);
1093 request_params_.set_update_check_count_wait_enabled(false);
1094 request_params_.set_waiting_period(TimeDelta::FromDays(2));
Amin Hassani538bd592020-11-04 20:46:08 -08001095 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001096 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1097 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1098 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001099
Amin Hassani41ac04b2019-03-29 11:31:03 -07001100 ASSERT_FALSE(TestUpdateCheck());
1101
Amin Hassania859c922020-11-13 15:30:38 -08001102 EXPECT_FALSE(response_.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001103}
1104
1105TEST_F(OmahaRequestActionTest,
1106 WallClockBasedWaitAloneCausesScatteringInteractive) {
1107 request_params_.set_wall_clock_based_wait_enabled(true);
1108 request_params_.set_update_check_count_wait_enabled(false);
1109 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1110 request_params_.set_interactive(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001111 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001112 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001113
1114 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001115 ASSERT_TRUE(TestUpdateCheck());
1116
Amin Hassania859c922020-11-13 15:30:38 -08001117 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001118}
1119
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001120TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001121 request_params_.set_wall_clock_based_wait_enabled(false);
1122 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1123 request_params_.set_update_check_count_wait_enabled(true);
1124 request_params_.set_min_update_checks_needed(1);
1125 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001126 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001127
Amin Hassani41ac04b2019-03-29 11:31:03 -07001128 ASSERT_TRUE(TestUpdateCheck());
1129
Amin Hassania859c922020-11-13 15:30:38 -08001130 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001131}
1132
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001133TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001134 request_params_.set_wall_clock_based_wait_enabled(true);
1135 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1136 request_params_.set_update_check_count_wait_enabled(true);
1137 request_params_.set_min_update_checks_needed(1);
1138 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001139 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001140 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1141
1142 ASSERT_TRUE(TestUpdateCheck());
1143
Amin Hassania859c922020-11-13 15:30:38 -08001144 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001145}
1146
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001147TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001148 request_params_.set_wall_clock_based_wait_enabled(true);
1149 request_params_.set_waiting_period(TimeDelta());
1150 request_params_.set_update_check_count_wait_enabled(true);
1151 request_params_.set_min_update_checks_needed(0);
1152 request_params_.set_max_update_checks_allowed(0);
Amin Hassani538bd592020-11-04 20:46:08 -08001153 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001154 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001155
Amin Hassani41ac04b2019-03-29 11:31:03 -07001156 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001157
Ben Chan9abb7632014-08-07 00:10:53 -07001158 int64_t count;
Amin Hassani90e9f192020-11-18 14:20:56 -08001159 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001160 ASSERT_EQ(count, 0);
Amin Hassania859c922020-11-13 15:30:38 -08001161 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001162}
1163
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001164TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001165 request_params_.set_wall_clock_based_wait_enabled(true);
1166 request_params_.set_waiting_period(TimeDelta());
1167 request_params_.set_update_check_count_wait_enabled(true);
1168 request_params_.set_min_update_checks_needed(1);
1169 request_params_.set_max_update_checks_allowed(8);
Amin Hassani538bd592020-11-04 20:46:08 -08001170 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001171 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1172 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1173 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001174
Amin Hassani41ac04b2019-03-29 11:31:03 -07001175 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001176
Ben Chan9abb7632014-08-07 00:10:53 -07001177 int64_t count;
Amin Hassani90e9f192020-11-18 14:20:56 -08001178 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001179 ASSERT_GT(count, 0);
Amin Hassania859c922020-11-13 15:30:38 -08001180 EXPECT_FALSE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001181}
1182
Amin Hassani41ac04b2019-03-29 11:31:03 -07001183TEST_F(OmahaRequestActionTest,
1184 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001185 request_params_.set_wall_clock_based_wait_enabled(true);
1186 request_params_.set_waiting_period(TimeDelta());
1187 request_params_.set_update_check_count_wait_enabled(true);
1188 request_params_.set_min_update_checks_needed(1);
1189 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001190 request_params_.set_interactive(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001191 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001192 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1193
1194 // Verify if we are interactive check we don't defer.
1195 ASSERT_TRUE(TestUpdateCheck());
1196
Amin Hassania859c922020-11-13 15:30:38 -08001197 EXPECT_TRUE(response_.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001198}
1199
1200TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1201 request_params_.set_wall_clock_based_wait_enabled(true);
1202 request_params_.set_waiting_period(TimeDelta());
1203 request_params_.set_update_check_count_wait_enabled(true);
1204 request_params_.set_min_update_checks_needed(1);
1205 request_params_.set_max_update_checks_allowed(8);
Amin Hassani538bd592020-11-04 20:46:08 -08001206 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001207 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1208 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1209 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001210
Amin Hassani90e9f192020-11-18 14:20:56 -08001211 ASSERT_TRUE(fake_prefs_->SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001212 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001213
Ben Chan9abb7632014-08-07 00:10:53 -07001214 int64_t count;
Amin Hassani90e9f192020-11-18 14:20:56 -08001215 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001216 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001217 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001218 ASSERT_EQ(count, 5);
Amin Hassania859c922020-11-13 15:30:38 -08001219 EXPECT_FALSE(response_.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001220}
1221
1222TEST_F(OmahaRequestActionTest,
1223 ExistingUpdateCheckCountCausesScatteringInteractive) {
1224 request_params_.set_wall_clock_based_wait_enabled(true);
1225 request_params_.set_waiting_period(TimeDelta());
1226 request_params_.set_update_check_count_wait_enabled(true);
1227 request_params_.set_min_update_checks_needed(1);
1228 request_params_.set_max_update_checks_allowed(8);
1229 request_params_.set_interactive(true);
Amin Hassani538bd592020-11-04 20:46:08 -08001230 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001231 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1232
Amin Hassani90e9f192020-11-18 14:20:56 -08001233 ASSERT_TRUE(fake_prefs_->SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001234
1235 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001236 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001237 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001238}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001239
Adolfo Victoria497044c2018-07-18 07:51:42 -07001240TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1241 // If staging is on, the value for max days to scatter should be ignored, and
1242 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001243 request_params_.set_wall_clock_based_wait_enabled(true);
1244 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1245 request_params_.set_update_check_count_wait_enabled(false);
Amin Hassani538bd592020-11-04 20:46:08 -08001246 FakeSystemState::Get()->fake_clock()->SetWallclockTime(Time::Now());
May Lippert60aa3ca2018-08-15 16:55:29 -07001247
Amin Hassani90e9f192020-11-18 14:20:56 -08001248 ASSERT_TRUE(fake_prefs_->SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
Adolfo Victoria497044c2018-07-18 07:51:42 -07001249 // This should not prevent scattering due to staging.
1250 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001251 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1252 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1253 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1254
1255 ASSERT_FALSE(TestUpdateCheck());
1256
Amin Hassania859c922020-11-13 15:30:38 -08001257 EXPECT_FALSE(response_.update_exists);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001258
1259 // Interactive updates should not be affected.
1260 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001261 tuc_params_.expected_code = ErrorCode::kSuccess;
1262 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1263
1264 ASSERT_TRUE(TestUpdateCheck());
1265
Amin Hassania859c922020-11-13 15:30:38 -08001266 EXPECT_TRUE(response_.update_exists);
Adolfo Victoria497044c2018-07-18 07:51:42 -07001267}
1268
Alex Deymo8e18f932015-03-27 16:16:59 -07001269TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001270 fake_update_response_.include_cohorts = true;
1271 fake_update_response_.cohort = "s/154454/8479665";
1272 fake_update_response_.cohorthint = "please-put-me-on-beta";
1273 fake_update_response_.cohortname = "stable";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001274 request_params_.set_dlc_apps_params(
1275 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
1276 fake_update_response_.dlc_app_update = true;
1277 fake_update_response_.include_dlc_cohorts = true;
1278 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1279 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1280 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001281 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001282
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001283 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001284 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001285
1286 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001287 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001288 EXPECT_EQ(fake_update_response_.cohort, value);
1289
Amin Hassani90e9f192020-11-18 14:20:56 -08001290 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001291 EXPECT_EQ(fake_update_response_.cohorthint, value);
1292
Amin Hassani90e9f192020-11-18 14:20:56 -08001293 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001294 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001295
Amin Hassani90e9f192020-11-18 14:20:56 -08001296 EXPECT_TRUE(fake_prefs_->GetString(
1297 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001298 &value));
1299 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1300
Amin Hassani90e9f192020-11-18 14:20:56 -08001301 EXPECT_TRUE(fake_prefs_->GetString(
1302 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001303 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1304 &value));
1305 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1306
Amin Hassani90e9f192020-11-18 14:20:56 -08001307 EXPECT_TRUE(fake_prefs_->GetString(
1308 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001309 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1310 &value));
1311 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Alex Deymo8e18f932015-03-27 16:16:59 -07001312}
1313
1314TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Amin Hassani90e9f192020-11-18 14:20:56 -08001315 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohort, "old_value"));
1316 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohortHint, "old_hint"));
1317 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohortName, "old_name"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001318 const string dlc_cohort_key =
Amin Hassani90e9f192020-11-18 14:20:56 -08001319 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1320 const string dlc_cohort_hint_key = fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001321 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint});
Amin Hassani90e9f192020-11-18 14:20:56 -08001322 const string dlc_cohort_name_key = fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001323 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName});
1324 request_params_.set_dlc_apps_params(
1325 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
Amin Hassani90e9f192020-11-18 14:20:56 -08001326 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_key, "old_value_dlc"));
1327 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_hint_key, "old_hint_dlc"));
1328 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_name_key, "old_name_dlc"));
Alex Deymo8e18f932015-03-27 16:16:59 -07001329 fake_update_response_.include_cohorts = true;
1330 fake_update_response_.cohort = "s/154454/8479665";
1331 fake_update_response_.cohorthint = "please-put-me-on-beta";
1332 fake_update_response_.cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001333 fake_update_response_.dlc_app_update = true;
1334 fake_update_response_.include_dlc_cohorts = true;
1335 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1336 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1337 fake_update_response_.dlc_cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001338 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001339
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001340 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001341 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001342
1343 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001344 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001345 EXPECT_EQ(fake_update_response_.cohort, value);
1346
Amin Hassani90e9f192020-11-18 14:20:56 -08001347 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001348 EXPECT_EQ(fake_update_response_.cohorthint, value);
1349
Amin Hassani90e9f192020-11-18 14:20:56 -08001350 EXPECT_FALSE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001351
Amin Hassani90e9f192020-11-18 14:20:56 -08001352 EXPECT_TRUE(fake_prefs_->GetString(dlc_cohort_key, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001353 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1354
Amin Hassani90e9f192020-11-18 14:20:56 -08001355 EXPECT_TRUE(fake_prefs_->GetString(dlc_cohort_hint_key, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001356 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1357
Amin Hassani90e9f192020-11-18 14:20:56 -08001358 EXPECT_FALSE(fake_prefs_->GetString(dlc_cohort_name_key, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001359}
1360
1361TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001362 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001363
Amin Hassani90e9f192020-11-18 14:20:56 -08001364 EXPECT_TRUE(fake_prefs_->SetString(kPrefsOmahaCohort, "old_value"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001365 const string dlc_cohort_key =
Amin Hassani90e9f192020-11-18 14:20:56 -08001366 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1367 EXPECT_TRUE(fake_prefs_->SetString(dlc_cohort_key, "old_value_dlc"));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001368 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001369
1370 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001371 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001372 EXPECT_EQ("old_value", value);
1373
Amin Hassani90e9f192020-11-18 14:20:56 -08001374 EXPECT_FALSE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
1375 EXPECT_FALSE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001376
Amin Hassani90e9f192020-11-18 14:20:56 -08001377 EXPECT_TRUE(fake_prefs_->GetString(dlc_cohort_key, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001378 EXPECT_EQ("old_value_dlc", value);
1379
Amin Hassani90e9f192020-11-18 14:20:56 -08001380 EXPECT_FALSE(fake_prefs_->GetString(
1381 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001382 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1383 &value));
Amin Hassani90e9f192020-11-18 14:20:56 -08001384 EXPECT_FALSE(fake_prefs_->GetString(
1385 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001386 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1387 &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001388}
1389
Alex Deymo00d79ac2015-06-29 15:41:49 -07001390TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001391 fake_update_response_.include_cohorts = true;
1392 fake_update_response_.cohort = "s/154454/8479665";
1393 fake_update_response_.cohorthint = "please-put-me-on-beta";
1394 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001395 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1396 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1397 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001398
Amin Hassani41ac04b2019-03-29 11:31:03 -07001399 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001400
1401 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001402 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Alex Deymo00d79ac2015-06-29 15:41:49 -07001403 EXPECT_EQ(fake_update_response_.cohort, value);
1404
Amin Hassani90e9f192020-11-18 14:20:56 -08001405 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Alex Deymo00d79ac2015-06-29 15:41:49 -07001406 EXPECT_EQ(fake_update_response_.cohorthint, value);
1407
Amin Hassani90e9f192020-11-18 14:20:56 -08001408 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Alex Deymo00d79ac2015-06-29 15:41:49 -07001409 EXPECT_EQ(fake_update_response_.cohortname, value);
1410}
1411
Sen Jiangb1e063a2017-09-15 17:44:31 -07001412TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001413 fake_update_response_.multi_app = true;
1414 fake_update_response_.include_cohorts = true;
1415 fake_update_response_.cohort = "s/154454/8479665";
1416 fake_update_response_.cohorthint = "please-put-me-on-beta";
1417 fake_update_response_.cohortname = "stable";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001418 request_params_.set_dlc_apps_params(
1419 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
1420 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
1421 fake_update_response_.dlc_app_update = true;
1422 fake_update_response_.dlc_app_no_update = true;
1423 fake_update_response_.include_dlc_cohorts = true;
1424 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1425 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1426 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001427 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001428
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001429 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001430 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001431
1432 string value;
Amin Hassani90e9f192020-11-18 14:20:56 -08001433 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohort, &value));
Sen Jiangb1e063a2017-09-15 17:44:31 -07001434 EXPECT_EQ(fake_update_response_.cohort, value);
1435
Amin Hassani90e9f192020-11-18 14:20:56 -08001436 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortHint, &value));
Sen Jiangb1e063a2017-09-15 17:44:31 -07001437 EXPECT_EQ(fake_update_response_.cohorthint, value);
1438
Amin Hassani90e9f192020-11-18 14:20:56 -08001439 EXPECT_TRUE(fake_prefs_->GetString(kPrefsOmahaCohortName, &value));
Sen Jiangb1e063a2017-09-15 17:44:31 -07001440 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001441
Amin Hassani90e9f192020-11-18 14:20:56 -08001442 EXPECT_TRUE(fake_prefs_->GetString(
1443 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001444 &value));
1445 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
Amin Hassani90e9f192020-11-18 14:20:56 -08001446 EXPECT_TRUE(fake_prefs_->GetString(
1447 fake_prefs_->CreateSubKey({kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohort}),
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001448 &value));
1449 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1450
Amin Hassani90e9f192020-11-18 14:20:56 -08001451 EXPECT_TRUE(fake_prefs_->GetString(
1452 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001453 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1454 &value));
1455 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
Amin Hassani90e9f192020-11-18 14:20:56 -08001456 EXPECT_TRUE(fake_prefs_->GetString(
1457 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001458 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortHint}),
1459 &value));
1460 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1461
Amin Hassani90e9f192020-11-18 14:20:56 -08001462 EXPECT_TRUE(fake_prefs_->GetString(
1463 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001464 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1465 &value));
1466 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Amin Hassani90e9f192020-11-18 14:20:56 -08001467 EXPECT_TRUE(fake_prefs_->GetString(
1468 fake_prefs_->CreateSubKey(
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001469 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortName}),
1470 &value));
1471 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Sen Jiangb1e063a2017-09-15 17:44:31 -07001472}
1473
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001474TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001475 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001476 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001477 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001478
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001479 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001480 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001481 std::make_unique<MockHttpFetcher>(
1482 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001483 false,
1484 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001485 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001486 processor.set_delegate(&delegate_);
1487 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001488
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001489 loop.PostTask(base::Bind(
1490 [](ActionProcessor* processor) { processor->StartProcessing(); },
1491 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001492 loop.Run();
1493 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001494 EXPECT_FALSE(processor.IsRunning());
1495}
1496
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001497TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001498 tuc_params_.http_response = "invalid xml>";
1499 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1500 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1501 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1502
1503 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001504 EXPECT_FALSE(response_.update_exists);
Darin Petkovedc522e2010-11-05 09:35:17 -07001505}
1506
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001507TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001508 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1509 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1510 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1511
1512 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001513 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001514}
1515
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001516TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001517 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001518 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1519 "<daystart elapsed_seconds=\"100\"/>"
1520 "<app appid=\"foo\" status=\"ok\">"
1521 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001522 "<updatecheck/></app></response>";
1523 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1524 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1525 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1526
1527 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001528 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001529}
1530
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001531TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001532 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001533 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1534 "<daystart elapsed_seconds=\"100\"/>"
1535 "<app appid=\"foo\" status=\"ok\">"
1536 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001537 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1538 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1539 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1540 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1541
1542 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001543 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001544}
1545
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001546TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001547 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001548 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1549 "<daystart elapsed_seconds=\"100\"/>"
1550 "<app appid=\"foo\" status=\"ok\">"
1551 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001552 "</app></response>";
1553 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1554 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1555 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1556
1557 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001558 EXPECT_FALSE(response_.update_exists);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001559}
1560
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001561TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001562 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001563 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1564 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001565 // the appid needs to match that in the request params
1566 "<app appid=\"" +
1567 fake_update_response_.app_id +
1568 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001569 "<updatecheck status=\"ok\">"
1570 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001571 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001572 "<packages><package hash=\"not-used\" name=\"f\" "
Vyshu Khota4c5413d2020-11-04 16:17:25 -08001573 "size=\"587\" fp=\"3.789\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001574 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001575 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001576 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001577 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001578 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001579
Amin Hassani41ac04b2019-03-29 11:31:03 -07001580 ASSERT_TRUE(TestUpdateCheck());
1581
Amin Hassania859c922020-11-13 15:30:38 -08001582 EXPECT_TRUE(response_.update_exists);
1583 EXPECT_EQ("10.2.3.4", response_.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001584 EXPECT_EQ("http://missing/field/test/f",
Amin Hassania859c922020-11-13 15:30:38 -08001585 response_.packages[0].payload_urls[0]);
1586 EXPECT_EQ("", response_.more_info_url);
1587 EXPECT_EQ("lkq34j5345", response_.packages[0].hash);
1588 EXPECT_EQ(string("3.789"), response_.packages[0].fp);
1589 EXPECT_EQ(587u, response_.packages[0].size);
1590 EXPECT_FALSE(response_.prompt);
1591 EXPECT_TRUE(response_.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001592}
1593
1594namespace {
1595class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1596 public:
1597 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001598 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001599 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001600};
1601
Alex Deymo60ca1a72015-06-18 18:19:15 -07001602void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001603 processor->StartProcessing();
1604 CHECK(processor->IsRunning());
1605 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001606}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001607} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001608
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001609TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001610 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001611 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001612
Alex Deymo60ca1a72015-06-18 18:19:15 -07001613 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001614 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001615 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001616 std::make_unique<MockHttpFetcher>(
1617 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001618 false,
1619 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001620 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001621 ActionProcessor processor;
1622 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001623 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001624
Alex Deymo60ca1a72015-06-18 18:19:15 -07001625 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1626 loop.Run();
1627 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001628}
1629
Alex Deymob0d74eb2015-03-30 17:59:17 -07001630TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001631 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001632 request_params_.set_os_sp("testtheservice_pack>");
1633 request_params_.set_os_board("x86 generic<id");
1634 request_params_.set_current_channel("unittest_track&lt;");
1635 request_params_.set_target_channel("unittest_track&lt;");
Amin Hassani37b67232020-08-13 09:29:48 -07001636 request_params_.set_lts_tag("unittest_hint&lt;");
Marton Hunyady2abda312018-04-24 18:21:49 +02001637 request_params_.set_hwid("<OEM MODEL>");
Amin Hassani90e9f192020-11-18 14:20:56 -08001638 fake_prefs_->SetString(kPrefsOmahaCohort, "evil\nstring");
1639 fake_prefs_->SetString(kPrefsOmahaCohortHint, "evil&string\\");
1640 fake_prefs_->SetString(
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001641 kPrefsOmahaCohortName,
1642 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001643 tuc_params_.http_response = "invalid xml>";
1644 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1645 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1646 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1647
1648 ASSERT_FALSE(TestUpdateCheck());
1649
Amin Hassania859c922020-11-13 15:30:38 -08001650 EXPECT_NE(string::npos, post_str_.find("testtheservice_pack&gt;"));
1651 EXPECT_EQ(string::npos, post_str_.find("testtheservice_pack>"));
1652 EXPECT_NE(string::npos, post_str_.find("x86 generic&lt;id"));
1653 EXPECT_EQ(string::npos, post_str_.find("x86 generic<id"));
1654 EXPECT_NE(string::npos, post_str_.find("unittest_track&amp;lt;"));
1655 EXPECT_EQ(string::npos, post_str_.find("unittest_track&lt;"));
1656 EXPECT_NE(string::npos, post_str_.find("unittest_hint&amp;lt;"));
1657 EXPECT_EQ(string::npos, post_str_.find("unittest_hint&lt;"));
1658 EXPECT_NE(string::npos, post_str_.find("&lt;OEM MODEL&gt;"));
1659 EXPECT_EQ(string::npos, post_str_.find("<OEM MODEL>"));
1660 EXPECT_NE(string::npos, post_str_.find("cohort=\"evil\nstring\""));
1661 EXPECT_EQ(string::npos, post_str_.find("cohorthint=\"evil&string\\\""));
1662 EXPECT_NE(string::npos, post_str_.find("cohorthint=\"evil&amp;string\\\""));
Alex Deymo8e18f932015-03-27 16:16:59 -07001663 // Values from Prefs that are too big are removed from the XML instead of
1664 // encoded.
Amin Hassania859c922020-11-13 15:30:38 -08001665 EXPECT_EQ(string::npos, post_str_.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001666}
1667
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001668TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001669 fake_update_response_.deadline = "&lt;20110101";
1670 fake_update_response_.more_info_url = "testthe&lt;url";
1671 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001672 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1673
1674 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001675
Amin Hassania859c922020-11-13 15:30:38 -08001676 EXPECT_EQ("testthe<url", response_.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001677 EXPECT_EQ("testthe&codebase/file.signed",
Amin Hassania859c922020-11-13 15:30:38 -08001678 response_.packages[0].payload_urls[0]);
1679 EXPECT_EQ("<20110101", response_.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001680}
1681
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001682TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001683 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001684 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001685 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001686
Amin Hassani41ac04b2019-03-29 11:31:03 -07001687 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001688 EXPECT_EQ(fake_update_response_.size, response_.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001689}
1690
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001691TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001692 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001693 FakeSystemState::Get()->set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001694 tuc_params_.http_response = "invalid xml>";
1695 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1696 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1697 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001698
Darin Petkov95508da2011-01-05 12:42:29 -08001699 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001700 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001701 // An existing but empty previous version means that we didn't reboot to a new
1702 // update, therefore, no need to update the previous version.
1703 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001704 ASSERT_FALSE(TestUpdateCheck());
1705
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001706 EXPECT_NE(
Amin Hassania859c922020-11-13 15:30:38 -08001707 post_str_.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1708 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001709 string::npos);
Amin Hassania859c922020-11-13 15:30:38 -08001710 EXPECT_NE(post_str_.find("hardware_class=\"OEM MODEL 09235 7471\""),
Darin Petkovfbb40092010-07-29 17:05:50 -07001711 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001712 // No <event> tag should be sent if we didn't reboot to an update.
Amin Hassania859c922020-11-13 15:30:38 -08001713 EXPECT_EQ(post_str_.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001714}
1715
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001716TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001717 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001718 "invalid xml>");
1719
Alex Vakulenko75039d72014-03-25 12:36:28 -07001720 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001721 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001722 OmahaEvent::kTypeUpdateDownloadStarted,
1723 OmahaEvent::kResultSuccess);
Amin Hassania859c922020-11-13 15:30:38 -08001724 EXPECT_NE(post_str_.find(expected_event), string::npos);
1725 EXPECT_EQ(post_str_.find("ping"), string::npos);
1726 EXPECT_EQ(post_str_.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001727}
1728
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001729TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001730 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001731 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001732 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001733 "invalid xml>");
1734
Alex Vakulenko75039d72014-03-25 12:36:28 -07001735 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001736 " <event eventtype=\"%d\" eventresult=\"%d\" "
1737 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001738 OmahaEvent::kTypeDownloadComplete,
1739 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001740 static_cast<int>(ErrorCode::kError));
Amin Hassania859c922020-11-13 15:30:38 -08001741 EXPECT_NE(post_str_.find(expected_event), string::npos);
1742 EXPECT_EQ(post_str_.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001743}
1744
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001745TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001746 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001747 OmahaRequestAction update_check_action(
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001748 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001749 std::make_unique<MockHttpFetcher>(
1750 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001751 false,
1752 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001753 EXPECT_FALSE(update_check_action.IsEvent());
1754
1755 OmahaRequestAction event_action(
Darin Petkove17f86b2010-07-20 09:12:01 -07001756 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001757 std::make_unique<MockHttpFetcher>(
1758 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001759 false,
1760 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001761 EXPECT_TRUE(event_action.IsEvent());
1762}
1763
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001764TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001765 tuc_params_.http_response = "invalid xml>";
1766 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1767 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1768 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1769
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001770 for (int i = 0; i < 2; i++) {
1771 bool delta_okay = i == 1;
1772 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001773 request_params_.set_delta_okay(delta_okay);
1774
Amin Hassani41ac04b2019-03-29 11:31:03 -07001775 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001776 EXPECT_NE(post_str_.find(
1777 base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1778 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001779 << "i = " << i;
1780 }
1781}
1782
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001783TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001784 tuc_params_.http_response = "invalid xml>";
1785 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1786 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1787 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1788
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001789 for (int i = 0; i < 2; i++) {
1790 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001791 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001792 request_params_.set_interactive(interactive);
1793
Amin Hassani41ac04b2019-03-29 11:31:03 -07001794 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08001795 EXPECT_NE(post_str_.find(
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001796 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001797 string::npos)
1798 << "i = " << i;
1799 }
1800}
1801
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001802TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001803 tuc_params_.http_response = "invalid xml>";
1804 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1805 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1806 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1807
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001808 for (int i = 0; i < 2; i++) {
1809 bool target_version_set = i == 1;
1810 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001811 request_params_.set_target_version_prefix(target_version_prefix);
1812
Amin Hassani41ac04b2019-03-29 11:31:03 -07001813 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001814 if (target_version_set) {
Amin Hassania859c922020-11-13 15:30:38 -08001815 EXPECT_NE(post_str_.find("<updatecheck targetversionprefix=\"10032.\">"),
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001816 string::npos)
1817 << "i = " << i;
1818 } else {
Amin Hassania859c922020-11-13 15:30:38 -08001819 EXPECT_EQ(post_str_.find("targetversionprefix"), string::npos)
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001820 << "i = " << i;
1821 }
1822 }
1823}
1824
1825TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001826 tuc_params_.http_response = "invalid xml>";
1827 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1828 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1829 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1830
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001831 for (int i = 0; i < 4; i++) {
1832 bool rollback_allowed = i / 2 == 0;
1833 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001834 request_params_.set_target_version_prefix(target_version_set ? "10032."
1835 : "");
1836 request_params_.set_rollback_allowed(rollback_allowed);
1837
Amin Hassani41ac04b2019-03-29 11:31:03 -07001838 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001839 if (rollback_allowed && target_version_set) {
Amin Hassania859c922020-11-13 15:30:38 -08001840 EXPECT_NE(post_str_.find("rollback_allowed=\"true\""), string::npos)
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001841 << "i = " << i;
1842 } else {
Amin Hassania859c922020-11-13 15:30:38 -08001843 EXPECT_EQ(post_str_.find("rollback_allowed"), string::npos)
1844 << "i = " << i;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001845 }
1846 }
1847}
1848
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001849TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001850 OmahaEvent default_event;
1851 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1852 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001853 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001854
1855 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1856 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1857 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001858 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001859
1860 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1861 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001862 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001863 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1864 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001865 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001866}
1867
Askar Aitzhan570ca872019-04-24 11:16:12 +02001868TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
Askar Aitzhan18fff842019-06-21 23:24:37 +02001869 // If DeviceQuickFixBuildToken value is set it takes precedence over pref
1870 // value.
1871 constexpr char autoupdate_token[] = "autoupdate_token>";
1872 constexpr char xml_encoded_autoupdate_token[] = "autoupdate_token&gt;";
1873 constexpr char omaha_cohort_hint[] = "cohort_hint";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001874
1875 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1876 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1877 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1878 request_params_.set_autoupdate_token(autoupdate_token);
Amin Hassani90e9f192020-11-18 14:20:56 -08001879 fake_prefs_->SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan570ca872019-04-24 11:16:12 +02001880
1881 ASSERT_TRUE(TestUpdateCheck());
1882
Askar Aitzhan18fff842019-06-21 23:24:37 +02001883 EXPECT_NE(string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08001884 post_str_.find("cohorthint=\"" +
1885 string(xml_encoded_autoupdate_token) + "\""));
1886 EXPECT_EQ(string::npos, post_str_.find(autoupdate_token));
1887 EXPECT_EQ(string::npos, post_str_.find(omaha_cohort_hint));
Askar Aitzhan18fff842019-06-21 23:24:37 +02001888}
1889
1890TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsNotSetTest) {
1891 // If DeviceQuickFixBuildToken is not set, pref value will be provided in
1892 // cohorthint attribute.
1893 constexpr char omaha_cohort_hint[] = "evil_string>";
1894 constexpr char xml_encoded_cohort_hint[] = "evil_string&gt;";
1895
1896 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1897 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1898 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Amin Hassani90e9f192020-11-18 14:20:56 -08001899 fake_prefs_->SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan18fff842019-06-21 23:24:37 +02001900
1901 ASSERT_TRUE(TestUpdateCheck());
1902
1903 EXPECT_NE(
1904 string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08001905 post_str_.find("cohorthint=\"" + string(xml_encoded_cohort_hint) + "\""));
1906 EXPECT_EQ(string::npos, post_str_.find(omaha_cohort_hint));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001907}
1908
Amin Hassani37b67232020-08-13 09:29:48 -07001909TEST_F(OmahaRequestActionTest, TargetChannelHintTest) {
1910 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1911 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1912 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1913 request_params_.set_lts_tag("hint>");
1914
1915 ASSERT_TRUE(TestUpdateCheck());
1916
Amin Hassania859c922020-11-13 15:30:38 -08001917 EXPECT_NE(string::npos, post_str_.find("ltstag=\"hint&gt;\""));
Amin Hassani37b67232020-08-13 09:29:48 -07001918}
1919
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001920void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001921 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001922 FakeSystemState::Get()->set_prefs(&prefs);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001923 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001924 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001925 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1926 // Add a few hours to the day difference to test no rounding, etc.
1927 int64_t five_days_ago =
1928 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1929 int64_t six_days_ago =
1930 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1931 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001932 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001933 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001934 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001935 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001936 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001937
1938 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1939 tuc_params_.ping_only = ping_only;
1940 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1941 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1942
1943 ASSERT_TRUE(TestUpdateCheck());
1944
Amin Hassania859c922020-11-13 15:30:38 -08001945 EXPECT_NE(post_str_.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001946 string::npos);
1947 if (ping_only) {
Amin Hassania859c922020-11-13 15:30:38 -08001948 EXPECT_EQ(post_str_.find("updatecheck"), string::npos);
1949 EXPECT_EQ(post_str_.find("previousversion"), string::npos);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001950 } else {
Amin Hassania859c922020-11-13 15:30:38 -08001951 EXPECT_NE(post_str_.find("updatecheck"), string::npos);
1952 EXPECT_NE(post_str_.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001953 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001954}
1955
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001956TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001957 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001958}
1959
1960TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001961 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001962}
1963
1964TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001965 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001966 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001967 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001968 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001969 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001970 int64_t three_days_ago =
1971 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1972 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001973 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001974 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001975 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001976 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001977 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001978 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001979
1980 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1981 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1982 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1983
1984 ASSERT_TRUE(TestUpdateCheck());
1985
Amin Hassania859c922020-11-13 15:30:38 -08001986 EXPECT_NE(post_str_.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001987}
1988
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001989TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001990 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08001991 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001992 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001993 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001994 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001995 int64_t four_days_ago =
1996 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1997 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001998 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001999 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002000 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002001 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002002 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002003 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002004
2005 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2006 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2007 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2008
2009 ASSERT_TRUE(TestUpdateCheck());
2010
Amin Hassania859c922020-11-13 15:30:38 -08002011 EXPECT_NE(post_str_.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07002012 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002013}
2014
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002015TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002016 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002017 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002018 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002019 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002020 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002021 int64_t one_hour_ago =
2022 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002023 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002024 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002025 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002026 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002027 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002028 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002029 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
2030 // send a ping.
2031 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2032 .WillOnce(Return(true));
2033 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2034 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002035
2036 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2037 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2038 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2039
2040 ASSERT_TRUE(TestUpdateCheck());
2041
Amin Hassania859c922020-11-13 15:30:38 -08002042 EXPECT_EQ(post_str_.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002043}
2044
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002045TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07002046 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08002047 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002048 FakeSystemState::Get()->set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07002049 int64_t now = Time::Now().ToInternalValue();
2050 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002051 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002052 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002053 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002054 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2055 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002056
2057 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2058 tuc_params_.ping_only = true;
2059 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
2060 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2061
2062 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002063 EXPECT_TRUE(post_str_.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07002064}
2065
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002066TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002067 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002068 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002069 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002070 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002071 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002072 int64_t future =
2073 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002074 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002075 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002076 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002077 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002078 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002079 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002080 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2081 .WillOnce(Return(true));
2082 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2083 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002084
2085 tuc_params_.http_response =
2086 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2087 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
2088 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2089 "<updatecheck status=\"noupdate\"/></app></response>";
2090 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2091 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2092
2093 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002094 EXPECT_EQ(post_str_.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002095}
2096
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002097TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002098 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07002099 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002100 // may fail if it runs for longer than 5 seconds. It shouldn't run
2101 // that long though.
2102 int64_t midnight =
2103 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
2104 int64_t midnight_slack =
2105 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08002106 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002107 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002108 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2109 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002110 EXPECT_CALL(prefs,
2111 SetInt64(kPrefsLastActivePingDay,
2112 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002113 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002114 EXPECT_CALL(prefs,
2115 SetInt64(kPrefsLastRollCallPingDay,
2116 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002117 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002118
2119 tuc_params_.http_response =
2120 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2121 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
2122 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2123 "<updatecheck status=\"noupdate\"/></app></response>";
2124 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2125 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2126
2127 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002128}
2129
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002130TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002131 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002132 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002133 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2134 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002135 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2136 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002137
2138 tuc_params_.http_response =
2139 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2140 "protocol=\"3.0\"><daystart blah=\"200\"/>"
2141 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2142 "<updatecheck status=\"noupdate\"/></app></response>";
2143 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2144 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2145
2146 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002147}
2148
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002149TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002150 NiceMock<MockPrefs> prefs;
Amin Hassani538bd592020-11-04 20:46:08 -08002151 FakeSystemState::Get()->set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002152 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2153 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002154 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2155 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002156
2157 tuc_params_.http_response =
2158 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2159 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
2160 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2161 "<updatecheck status=\"noupdate\"/></app></response>";
2162 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2163 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2164
2165 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002166}
2167
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002168TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07002169 tuc_params_.http_response = "invalid xml>";
2170 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2171 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2172 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2173
2174 ASSERT_FALSE(TestUpdateCheck());
2175
Amin Hassania859c922020-11-13 15:30:38 -08002176 EXPECT_EQ(post_str_.find("machineid="), string::npos);
2177 EXPECT_EQ(post_str_.find("userid="), string::npos);
Darin Petkov84c763c2010-07-29 16:27:58 -07002178}
2179
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002180TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002181 const int http_error_code =
2182 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002183 tuc_params_.fail_http_response_code = 501;
2184 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2185 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2186 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2187 tuc_params_.expected_download_error_code =
2188 static_cast<metrics::DownloadErrorCode>(501);
2189
2190 ASSERT_FALSE(TestUpdateCheck());
2191
Amin Hassania859c922020-11-13 15:30:38 -08002192 EXPECT_FALSE(response_.update_exists);
Darin Petkovedc522e2010-11-05 09:35:17 -07002193}
2194
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002195TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002196 const int http_error_code =
2197 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002198
2199 tuc_params_.fail_http_response_code = 1500;
2200 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2201 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2202 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2203 tuc_params_.expected_download_error_code =
2204 metrics::DownloadErrorCode::kHttpStatusOther;
2205
2206 ASSERT_FALSE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002207 EXPECT_FALSE(response_.update_exists);
Darin Petkovedc522e2010-11-05 09:35:17 -07002208}
2209
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002210TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002211 request_params_.set_wall_clock_based_wait_enabled(true);
2212 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2213 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002214
Sen Jiang7c1171e2016-06-23 11:35:40 -07002215 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002216 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Amin Hassani538bd592020-11-04 20:46:08 -08002217 FakeSystemState::Get()->fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002218
2219 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2220 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2221 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2222
2223 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002224
Ben Chan9abb7632014-08-07 00:10:53 -07002225 int64_t timestamp = 0;
Amin Hassani90e9f192020-11-18 14:20:56 -08002226 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002227 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Amin Hassania859c922020-11-13 15:30:38 -08002228 EXPECT_FALSE(response_.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002229
2230 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002231 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002232 tuc_params_.expected_code = ErrorCode::kSuccess;
2233 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2234
2235 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002236 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002237}
2238
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002239TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002240 request_params_.set_wall_clock_based_wait_enabled(true);
2241 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2242 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002243
Sen Jiang7c1171e2016-06-23 11:35:40 -07002244 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002245 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2246 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002247 ASSERT_TRUE(
Amin Hassani90e9f192020-11-18 14:20:56 -08002248 fake_prefs_->SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Amin Hassani538bd592020-11-04 20:46:08 -08002249 FakeSystemState::Get()->fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002250
Amin Hassani41ac04b2019-03-29 11:31:03 -07002251 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2252
2253 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002254 EXPECT_TRUE(response_.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002255
2256 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002257 int64_t timestamp = 0;
Amin Hassani90e9f192020-11-18 14:20:56 -08002258 ASSERT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002259 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2260}
2261
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002262TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002263 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002264 base::ScopedTempDir tempdir;
2265 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002266
Marton Hunyady2abda312018-04-24 18:21:49 +02002267 request_params_.set_root(tempdir.GetPath().value());
2268 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2269 request_params_.set_app_version("1.2.3.4");
2270 request_params_.set_product_components("o.bundle=1");
2271 request_params_.set_current_channel("canary-channel");
2272 EXPECT_TRUE(
2273 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2274 request_params_.UpdateDownloadChannel();
2275 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002276
2277 tuc_params_.http_response = "invalid xml>";
2278 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2279 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2280 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2281
2282 ASSERT_FALSE(TestUpdateCheck());
2283
Amin Hassania859c922020-11-13 15:30:38 -08002284 EXPECT_NE(string::npos,
2285 post_str_.find(
2286 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
2287 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2288 "track=\"stable-channel\" from_track=\"canary-channel\" "));
2289 EXPECT_EQ(string::npos, post_str_.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002290}
2291
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002292TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002293 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002294 base::ScopedTempDir tempdir;
2295 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002296
Marton Hunyady2abda312018-04-24 18:21:49 +02002297 request_params_.set_root(tempdir.GetPath().value());
2298 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2299 request_params_.set_app_version("5.6.7.8");
2300 request_params_.set_product_components("o.bundle=1");
2301 request_params_.set_current_channel("stable-channel");
2302 EXPECT_TRUE(
2303 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2304 request_params_.UpdateDownloadChannel();
2305 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002306
2307 tuc_params_.http_response = "invalid xml>";
2308 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2309 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2310 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2311
2312 ASSERT_FALSE(TestUpdateCheck());
2313
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002314 EXPECT_NE(
2315 string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002316 post_str_.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2317 "version=\"5.6.7.8\" "
2318 "track=\"canary-channel\" from_track=\"stable-channel\""));
2319 EXPECT_EQ(string::npos, post_str_.find("from_version"));
2320 EXPECT_NE(string::npos, post_str_.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002321}
2322
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002323// Checks that the initial ping with a=-1 r=-1 is not send when the device
2324// was powerwashed.
2325TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
Amin Hassani90e9f192020-11-18 14:20:56 -08002326 fake_prefs_->SetString(kPrefsPreviousVersion, "");
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002327
2328 // Flag that the device was powerwashed in the past.
Amin Hassani538bd592020-11-04 20:46:08 -08002329 FakeSystemState::Get()->fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002330 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2331 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2332 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002333
Amin Hassani41ac04b2019-03-29 11:31:03 -07002334 ASSERT_TRUE(TestUpdateCheck());
2335
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002336 // We shouldn't send a ping in this case since powerwash > 0.
Amin Hassania859c922020-11-13 15:30:38 -08002337 EXPECT_EQ(string::npos, post_str_.find("<ping"));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002338}
2339
Amin Hassani1677e812017-06-21 13:36:36 -07002340// Checks that the initial ping with a=-1 r=-1 is not send when the device
2341// first_active_omaha_ping_sent is set.
2342TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
Amin Hassani90e9f192020-11-18 14:20:56 -08002343 fake_prefs_->SetString(kPrefsPreviousVersion, "");
Amin Hassani1677e812017-06-21 13:36:36 -07002344
2345 // Flag that the device was not powerwashed in the past.
Amin Hassani538bd592020-11-04 20:46:08 -08002346 FakeSystemState::Get()->fake_hardware()->SetPowerwashCount(0);
Amin Hassani1677e812017-06-21 13:36:36 -07002347
2348 // Flag that the device has sent first active ping in the past.
Amin Hassani538bd592020-11-04 20:46:08 -08002349 FakeSystemState::Get()->fake_hardware()->SetFirstActiveOmahaPingSent();
Amin Hassani1677e812017-06-21 13:36:36 -07002350
Amin Hassani41ac04b2019-03-29 11:31:03 -07002351 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2352 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2353 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2354
2355 ASSERT_TRUE(TestUpdateCheck());
2356
Amin Hassani1677e812017-06-21 13:36:36 -07002357 // We shouldn't send a ping in this case since
2358 // first_active_omaha_ping_sent=true
Amin Hassania859c922020-11-13 15:30:38 -08002359 EXPECT_EQ(string::npos, post_str_.find("<ping"));
Amin Hassani1677e812017-06-21 13:36:36 -07002360}
2361
Alex Deymo9fded1e2015-11-05 12:31:19 -08002362// Checks that the event 54 is sent on a reboot to a new update.
2363TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2364 // Flag that the device was updated in a previous boot.
Amin Hassani90e9f192020-11-18 14:20:56 -08002365 fake_prefs_->SetString(kPrefsPreviousVersion, "1.2.3.4");
Alex Deymo9fded1e2015-11-05 12:31:19 -08002366
Amin Hassani41ac04b2019-03-29 11:31:03 -07002367 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2368 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2369 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2370
2371 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002372
2373 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002374 EXPECT_NE(
2375 string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002376 post_str_.find(base::StringPrintf("<event eventtype=\"%d\"",
2377 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002378 EXPECT_NE(string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002379 post_str_.find("previousversion=\"1.2.3.4\"></event>"));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002380
2381 // The previous version flag should have been removed.
Amin Hassani90e9f192020-11-18 14:20:56 -08002382 EXPECT_TRUE(fake_prefs_->Exists(kPrefsPreviousVersion));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002383 string prev_version;
Amin Hassani90e9f192020-11-18 14:20:56 -08002384 EXPECT_TRUE(fake_prefs_->GetString(kPrefsPreviousVersion, &prev_version));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002385 EXPECT_TRUE(prev_version.empty());
2386}
2387
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002388void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2389 bool initial_allow_p2p_for_sharing,
2390 bool omaha_disable_p2p_for_downloading,
2391 bool omaha_disable_p2p_for_sharing,
2392 bool payload_state_allow_p2p_attempt,
2393 bool expect_p2p_client_lookup,
2394 const string& p2p_client_result_url,
2395 bool expected_allow_p2p_for_downloading,
2396 bool expected_allow_p2p_for_sharing,
2397 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002398 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2399 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2400 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002401
2402 MockPayloadState mock_payload_state;
Amin Hassani538bd592020-11-04 20:46:08 -08002403 FakeSystemState::Get()->set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002404 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2405 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002406 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2407 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2408 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2409 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2410 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2411 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2412 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2413 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2414 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2415 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2416
David Zeuthen8f191b22013-08-06 12:27:50 -07002417 MockP2PManager mock_p2p_manager;
Amin Hassani538bd592020-11-04 20:46:08 -08002418 FakeSystemState::Get()->set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002419 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2420
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002421 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2422 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002423 .Times(expect_p2p_client_lookup ? 1 : 0);
2424
Alex Deymo8e18f932015-03-27 16:16:59 -07002425 fake_update_response_.disable_p2p_for_downloading =
2426 omaha_disable_p2p_for_downloading;
2427 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002428
2429 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2430 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2431
2432 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002433 EXPECT_TRUE(response_.update_exists);
David Zeuthen8f191b22013-08-06 12:27:50 -07002434
Gilad Arnold74b5f552014-10-07 08:17:16 -07002435 EXPECT_EQ(omaha_disable_p2p_for_downloading,
Amin Hassania859c922020-11-13 15:30:38 -08002436 response_.disable_p2p_for_downloading);
2437 EXPECT_EQ(omaha_disable_p2p_for_sharing, response_.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002438
Gilad Arnold74b5f552014-10-07 08:17:16 -07002439 EXPECT_EQ(expected_allow_p2p_for_downloading,
2440 actual_allow_p2p_for_downloading);
2441 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2442 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002443}
2444
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002445TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002446 P2PTest(true, // initial_allow_p2p_for_downloading
2447 true, // initial_allow_p2p_for_sharing
2448 false, // omaha_disable_p2p_for_downloading
2449 false, // omaha_disable_p2p_for_sharing
2450 true, // payload_state_allow_p2p_attempt
2451 true, // expect_p2p_client_lookup
2452 "http://1.3.5.7/p2p", // p2p_client_result_url
2453 true, // expected_allow_p2p_for_downloading
2454 true, // expected_allow_p2p_for_sharing
2455 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002456}
2457
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002458TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002459 P2PTest(true, // initial_allow_p2p_for_downloading
2460 true, // initial_allow_p2p_for_sharing
2461 false, // omaha_disable_p2p_for_downloading
2462 false, // omaha_disable_p2p_for_sharing
2463 true, // payload_state_allow_p2p_attempt
2464 true, // expect_p2p_client_lookup
2465 "", // p2p_client_result_url
2466 false, // expected_allow_p2p_for_downloading
2467 true, // expected_allow_p2p_for_sharing
2468 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002469}
2470
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002471TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002472 P2PTest(false, // initial_allow_p2p_for_downloading
2473 true, // initial_allow_p2p_for_sharing
2474 false, // omaha_disable_p2p_for_downloading
2475 false, // omaha_disable_p2p_for_sharing
2476 true, // payload_state_allow_p2p_attempt
2477 false, // expect_p2p_client_lookup
2478 "unset", // p2p_client_result_url
2479 false, // expected_allow_p2p_for_downloading
2480 true, // expected_allow_p2p_for_sharing
2481 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002482}
2483
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002484TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002485 P2PTest(true, // initial_allow_p2p_for_downloading
2486 true, // initial_allow_p2p_for_sharing
2487 true, // omaha_disable_p2p_for_downloading
2488 false, // omaha_disable_p2p_for_sharing
2489 true, // payload_state_allow_p2p_attempt
2490 false, // expect_p2p_client_lookup
2491 "unset", // p2p_client_result_url
2492 false, // expected_allow_p2p_for_downloading
2493 true, // expected_allow_p2p_for_sharing
2494 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002495}
2496
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002497TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002498 P2PTest(true, // initial_allow_p2p_for_downloading
2499 true, // initial_allow_p2p_for_sharing
2500 false, // omaha_disable_p2p_for_downloading
2501 true, // omaha_disable_p2p_for_sharing
2502 true, // payload_state_allow_p2p_attempt
2503 true, // expect_p2p_client_lookup
2504 "http://1.3.5.7/p2p", // p2p_client_result_url
2505 true, // expected_allow_p2p_for_downloading
2506 false, // expected_allow_p2p_for_sharing
2507 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002508}
2509
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002510TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002511 P2PTest(true, // initial_allow_p2p_for_downloading
2512 true, // initial_allow_p2p_for_sharing
2513 true, // omaha_disable_p2p_for_downloading
2514 true, // omaha_disable_p2p_for_sharing
2515 true, // payload_state_allow_p2p_attempt
2516 false, // expect_p2p_client_lookup
2517 "unset", // p2p_client_result_url
2518 false, // expected_allow_p2p_for_downloading
2519 false, // expected_allow_p2p_for_sharing
2520 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002521}
2522
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002523bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2524 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002525 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002526 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2527
2528 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002529}
2530
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002531TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002532 // Simulate a successful update check that happens during OOBE. The
2533 // deadline in the response is needed to force the update attempt to
2534 // occur; responses without a deadline seen during OOBE will normally
2535 // return ErrorCode::kNonCriticalUpdateInOOBE.
Amin Hassani538bd592020-11-04 20:46:08 -08002536 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
Kevin Cernekee2494e282016-03-29 18:03:53 -07002537 fake_update_response_.deadline = "20101020";
2538
David Zeuthen639aa362014-02-03 16:23:44 -08002539 // Check that we parse elapsed_days in the Omaha Response correctly.
2540 // and that the kPrefsInstallDateDays value is written to.
Amin Hassani90e9f192020-11-18 14:20:56 -08002541 EXPECT_FALSE(fake_prefs_->Exists(kPrefsInstallDateDays));
Amin Hassania859c922020-11-13 15:30:38 -08002542 EXPECT_TRUE(InstallDateParseHelper("42", &response_));
2543 EXPECT_TRUE(response_.update_exists);
2544 EXPECT_EQ(42, response_.install_date_days);
Amin Hassani90e9f192020-11-18 14:20:56 -08002545 EXPECT_TRUE(fake_prefs_->Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002546 int64_t prefs_days;
Amin Hassani90e9f192020-11-18 14:20:56 -08002547 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002548 EXPECT_EQ(prefs_days, 42);
2549
2550 // If there already is a value set, we shouldn't do anything.
Amin Hassania859c922020-11-13 15:30:38 -08002551 EXPECT_TRUE(InstallDateParseHelper("7", &response_));
2552 EXPECT_TRUE(response_.update_exists);
2553 EXPECT_EQ(7, response_.install_date_days);
Amin Hassani90e9f192020-11-18 14:20:56 -08002554 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002555 EXPECT_EQ(prefs_days, 42);
2556
2557 // Note that elapsed_days is not necessarily divisible by 7 so check
2558 // that we round down correctly when populating kPrefsInstallDateDays.
Amin Hassani90e9f192020-11-18 14:20:56 -08002559 EXPECT_TRUE(fake_prefs_->Delete(kPrefsInstallDateDays));
Amin Hassania859c922020-11-13 15:30:38 -08002560 EXPECT_TRUE(InstallDateParseHelper("23", &response_));
2561 EXPECT_TRUE(response_.update_exists);
2562 EXPECT_EQ(23, response_.install_date_days);
Amin Hassani90e9f192020-11-18 14:20:56 -08002563 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002564 EXPECT_EQ(prefs_days, 21);
2565
2566 // Check that we correctly handle elapsed_days not being included in
Amin Hassania859c922020-11-13 15:30:38 -08002567 // the Omaha Response_.
2568 EXPECT_TRUE(InstallDateParseHelper("", &response_));
2569 EXPECT_TRUE(response_.update_exists);
2570 EXPECT_EQ(-1, response_.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002571}
2572
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002573// If there is no prefs and OOBE is not complete, we should not
2574// report anything to Omaha.
2575TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Amin Hassani538bd592020-11-04 20:46:08 -08002576 FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
2577 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), -1);
Amin Hassani90e9f192020-11-18 14:20:56 -08002578 EXPECT_FALSE(fake_prefs_->Exists(kPrefsInstallDateDays));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002579}
David Zeuthen639aa362014-02-03 16:23:44 -08002580
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002581// If OOBE is complete and happened on a valid date (e.g. after Jan
2582// 1 2007 0:00 PST), that date should be used and written to
2583// prefs. However, first try with an invalid date and check we do
2584// nothing.
2585TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2586 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
Amin Hassani538bd592020-11-04 20:46:08 -08002587 FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(oobe_date);
2588 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), -1);
Amin Hassani90e9f192020-11-18 14:20:56 -08002589 EXPECT_FALSE(fake_prefs_->Exists(kPrefsInstallDateDays));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002590}
David Zeuthen639aa362014-02-03 16:23:44 -08002591
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002592// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2593// should yield an InstallDate of 14.
2594TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2595 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
Amin Hassani538bd592020-11-04 20:46:08 -08002596 FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(oobe_date);
2597 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), 14);
Amin Hassani90e9f192020-11-18 14:20:56 -08002598 EXPECT_TRUE(fake_prefs_->Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002599
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002600 int64_t prefs_days;
Amin Hassani90e9f192020-11-18 14:20:56 -08002601 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002602 EXPECT_EQ(prefs_days, 14);
2603}
David Zeuthen639aa362014-02-03 16:23:44 -08002604
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002605// Now that we have a valid date in prefs, check that we keep using
2606// that even if OOBE date reports something else. The date Jan 30,
2607// 2007 0:00 PST should yield an InstallDate of 28... but since
2608// there's a prefs file, we should still get 14.
2609TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2610 // Set a valid date in the prefs first.
Amin Hassani90e9f192020-11-18 14:20:56 -08002611 EXPECT_TRUE(fake_prefs_->SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002612
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002613 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
Amin Hassani538bd592020-11-04 20:46:08 -08002614 FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(oobe_date);
2615 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002616
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002617 int64_t prefs_days;
Amin Hassani90e9f192020-11-18 14:20:56 -08002618 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002619 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002620
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002621 // If we delete the prefs file, we should get 28 days.
Amin Hassani90e9f192020-11-18 14:20:56 -08002622 EXPECT_TRUE(fake_prefs_->Delete(kPrefsInstallDateDays));
Amin Hassani538bd592020-11-04 20:46:08 -08002623 EXPECT_EQ(OmahaRequestAction::GetInstallDate(), 28);
Amin Hassani90e9f192020-11-18 14:20:56 -08002624 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsInstallDateDays, &prefs_days));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002625 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002626}
2627
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002628// Verifies that a device with no device policy, and is not a consumer
2629// device sets the max kernel key version to the current version.
2630// ie. the same behavior as if rollback is enabled.
2631TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002632 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002633
2634 // Setup and verify some initial default values for the kernel TPM
2635 // values that control verified boot and rollback.
2636 const int min_kernel_version = 4;
2637 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2638 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2639 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2640 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2641
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002642 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002643 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002644 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2645 .Times(1);
2646
Amin Hassani41ac04b2019-03-29 11:31:03 -07002647 fake_update_response_.deadline = "20101020";
2648 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2649 tuc_params_.is_consumer_device = false;
2650 tuc_params_.rollback_allowed_milestones = 3;
2651
2652 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002653 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002654
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002655 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002656 // kernel key version. This has the effect of freezing roll
2657 // forwards indefinitely. This will hold the rollback window
2658 // open until a future change will be able to move this forward
2659 // relative the configured window.
2660 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2661 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2662}
2663
2664// Verifies that a conmsumer device with no device policy sets the
2665// max kernel key version to the current version. ie. the same
2666// behavior as if rollback is enabled.
2667TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002668 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002669
2670 // Setup and verify some initial default values for the kernel TPM
2671 // values that control verified boot and rollback.
2672 const int min_kernel_version = 3;
2673 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2674 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2675 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2676 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2677
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002678 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002679 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002680 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2681 .Times(1);
2682
Amin Hassani41ac04b2019-03-29 11:31:03 -07002683 fake_update_response_.deadline = "20101020";
2684 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2685 tuc_params_.is_consumer_device = true;
2686 tuc_params_.rollback_allowed_milestones = 3;
2687
2688 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002689 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002690
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002691 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002692 // was set to logical infinity. This is the expected behavior for
2693 // consumer devices and matches the existing behavior prior to the
2694 // rollback features.
2695 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2696 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2697}
2698
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002699// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002700// in the TPM to prevent roll forward.
2701TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002702 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002703
2704 // Setup and verify some initial default values for the kernel TPM
2705 // values that control verified boot and rollback.
2706 const int allowed_milestones = 4;
2707 const int min_kernel_version = 3;
2708 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2709 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2710 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2711 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2712
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002713 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002714 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002715 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2716 .Times(1);
2717
Amin Hassani41ac04b2019-03-29 11:31:03 -07002718 fake_update_response_.deadline = "20101020";
2719 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2720 tuc_params_.is_consumer_device = false;
2721 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2722 tuc_params_.is_policy_loaded = true;
2723
2724 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002725 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002726
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002727 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002728 // was set to the current minimum kernel key version. This has
2729 // the effect of freezing roll forwards indefinitely. This will
2730 // hold the rollback window open until a future change will
2731 // be able to move this forward relative the configured window.
2732 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2733 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2734}
2735
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002736// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002737// in the TPM to logical infinity, to allow roll forward.
2738TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
Amin Hassani538bd592020-11-04 20:46:08 -08002739 FakeHardware* fake_hw = FakeSystemState::Get()->fake_hardware();
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002740
2741 // Setup and verify some initial default values for the kernel TPM
2742 // values that control verified boot and rollback.
2743 const int allowed_milestones = 0;
2744 const int min_kernel_version = 3;
2745 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2746 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2747 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2748 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2749
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002750 EXPECT_CALL(
Amin Hassani538bd592020-11-04 20:46:08 -08002751 *FakeSystemState::Get()->mock_metrics_reporter(),
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002752 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2753 .Times(1);
2754
Amin Hassani41ac04b2019-03-29 11:31:03 -07002755 fake_update_response_.deadline = "20101020";
2756 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2757 tuc_params_.is_consumer_device = false;
2758 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2759 tuc_params_.is_policy_loaded = true;
2760
2761 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002762 EXPECT_TRUE(response_.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002763
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002764 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002765 // was set to logical infinity.
2766 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2767 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2768}
2769
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002770TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002771 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002772 fake_update_response_.deadline = "20101020";
2773 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2774 tuc_params_.is_consumer_device = false;
2775 tuc_params_.rollback_allowed_milestones = 4;
2776 tuc_params_.is_policy_loaded = true;
2777
2778 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002779 EXPECT_TRUE(response_.update_exists);
2780 EXPECT_TRUE(response_.is_rollback);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002781}
2782
2783TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002784 fake_update_response_.rollback_firmware_version = "1.2";
2785 fake_update_response_.rollback_kernel_version = "3.4";
2786 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002787 fake_update_response_.deadline = "20101020";
2788 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2789 tuc_params_.is_consumer_device = false;
2790 tuc_params_.rollback_allowed_milestones = 4;
2791 tuc_params_.is_policy_loaded = true;
2792
2793 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002794 EXPECT_TRUE(response_.update_exists);
2795 EXPECT_TRUE(response_.is_rollback);
2796 EXPECT_EQ(1, response_.rollback_key_version.firmware_key);
2797 EXPECT_EQ(2, response_.rollback_key_version.firmware);
2798 EXPECT_EQ(3, response_.rollback_key_version.kernel_key);
2799 EXPECT_EQ(4, response_.rollback_key_version.kernel);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002800}
2801
May Lippert60aa3ca2018-08-15 16:55:29 -07002802TEST_F(OmahaRequestActionTest,
2803 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2804 FakeClock fake_clock;
2805 Time now = Time::Now();
2806 fake_clock.SetWallclockTime(now);
Amin Hassani538bd592020-11-04 20:46:08 -08002807 FakeSystemState::Get()->set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002808 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002809
Amin Hassani41ac04b2019-03-29 11:31:03 -07002810 ASSERT_TRUE(TestUpdateCheck());
2811
Amin Hassania859c922020-11-13 15:30:38 -08002812 EXPECT_TRUE(response_.update_exists);
Amin Hassani90e9f192020-11-18 14:20:56 -08002813 EXPECT_TRUE(fake_prefs_->Exists(kPrefsUpdateFirstSeenAt));
May Lippert60aa3ca2018-08-15 16:55:29 -07002814
2815 int64_t stored_first_seen_at_time;
Amin Hassani90e9f192020-11-18 14:20:56 -08002816 EXPECT_TRUE(fake_prefs_->GetInt64(kPrefsUpdateFirstSeenAt,
2817 &stored_first_seen_at_time));
May Lippert60aa3ca2018-08-15 16:55:29 -07002818 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2819}
2820
2821TEST_F(OmahaRequestActionTest,
2822 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2823 FakeClock fake_clock;
2824 Time now = Time::Now();
2825 fake_clock.SetWallclockTime(now);
Amin Hassani538bd592020-11-04 20:46:08 -08002826 FakeSystemState::Get()->set_clock(&fake_clock);
May Lippert60aa3ca2018-08-15 16:55:29 -07002827
Amin Hassani41ac04b2019-03-29 11:31:03 -07002828 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2829 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2830 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2831
2832 ASSERT_TRUE(TestUpdateCheck());
2833
Amin Hassania859c922020-11-13 15:30:38 -08002834 EXPECT_FALSE(response_.update_exists);
Amin Hassani90e9f192020-11-18 14:20:56 -08002835 EXPECT_FALSE(fake_prefs_->Exists(kPrefsUpdateFirstSeenAt));
May Lippert60aa3ca2018-08-15 16:55:29 -07002836}
2837
Xiaochu Liu88d90382018-08-29 16:09:11 -07002838TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002839 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002840 request_params_.set_dlc_apps_params(
2841 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2842 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002843 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2844
2845 ASSERT_TRUE(TestUpdateCheck());
2846
Andrewe045aef2020-01-08 16:29:22 -08002847 for (const auto& it : request_params_.dlc_apps_params()) {
Amin Hassania859c922020-11-13 15:30:38 -08002848 EXPECT_NE(string::npos, post_str_.find("appid=\"" + it.first + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002849 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002850 EXPECT_NE(string::npos,
Amin Hassania859c922020-11-13 15:30:38 -08002851 post_str_.find("appid=\"" + fake_update_response_.app_id + "\""));
Xiaochu Liu6310be62018-10-11 15:09:03 -07002852
Amin Hassania859c922020-11-13 15:30:38 -08002853 // Count number of updatecheck tag in response_.
Xiaochu Liu6310be62018-10-11 15:09:03 -07002854 int updatecheck_count = 0;
2855 size_t pos = 0;
Amin Hassania859c922020-11-13 15:30:38 -08002856 while ((pos = post_str_.find("<updatecheck", pos)) != string::npos) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002857 updatecheck_count++;
2858 pos++;
2859 }
Andrewe045aef2020-01-08 16:29:22 -08002860 EXPECT_EQ(request_params_.dlc_apps_params().size(), updatecheck_count);
Amin Hassania859c922020-11-13 15:30:38 -08002861 EXPECT_TRUE(response_.update_exists);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002862}
2863
2864TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2865 fake_update_response_.multi_app_skip_updatecheck = true;
2866 fake_update_response_.multi_app_no_update = false;
2867 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002868 request_params_.set_dlc_apps_params(
2869 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2870 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002871 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002872 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2873
2874 ASSERT_TRUE(TestUpdateCheck());
2875
Amin Hassania859c922020-11-13 15:30:38 -08002876 EXPECT_TRUE(response_.update_exists);
2877 EXPECT_EQ(fake_update_response_.current_version, response_.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002878}
2879
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002880TEST_F(OmahaRequestActionTest, UpdateWithDlcTest) {
2881 request_params_.set_dlc_apps_params(
2882 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
2883 fake_update_response_.dlc_app_update = true;
2884 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002885 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002886 ASSERT_TRUE(TestUpdateCheck());
2887
Amin Hassania859c922020-11-13 15:30:38 -08002888 EXPECT_EQ(response_.packages.size(), 2u);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002889 // Two candidate URLs.
Amin Hassania859c922020-11-13 15:30:38 -08002890 EXPECT_EQ(response_.packages[1].payload_urls.size(), 2u);
2891 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002892}
2893
2894TEST_F(OmahaRequestActionTest, UpdateWithPartiallyExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002895 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2896 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002897 fake_update_response_.dlc_app_update = true;
2898 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2899 // The first DLC candidate URL is excluded.
2900 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2901 .WillOnce(Return(true))
2902 .WillOnce(Return(false));
2903 ASSERT_TRUE(TestUpdateCheck());
2904
Amin Hassania859c922020-11-13 15:30:38 -08002905 EXPECT_EQ(response_.packages.size(), 2u);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002906 // One candidate URL.
Amin Hassania859c922020-11-13 15:30:38 -08002907 EXPECT_EQ(response_.packages[1].payload_urls.size(), 1u);
2908 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002909 EXPECT_TRUE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002910}
2911
2912TEST_F(OmahaRequestActionTest, UpdateWithExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002913 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2914 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002915 fake_update_response_.dlc_app_update = true;
2916 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2917 // Both DLC candidate URLs are excluded.
2918 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2919 .WillOnce(Return(true))
2920 .WillOnce(Return(true));
2921 ASSERT_TRUE(TestUpdateCheck());
2922
Amin Hassania859c922020-11-13 15:30:38 -08002923 EXPECT_EQ(response_.packages.size(), 1u);
2924 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002925 EXPECT_FALSE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002926}
2927
2928TEST_F(OmahaRequestActionTest, UpdateWithDeprecatedDlcTest) {
2929 request_params_.set_dlc_apps_params(
2930 {{request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2931 fake_update_response_.dlc_app_no_update = true;
2932 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002933 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002934 ASSERT_TRUE(TestUpdateCheck());
2935
Amin Hassania859c922020-11-13 15:30:38 -08002936 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002937}
2938
2939TEST_F(OmahaRequestActionTest, UpdateWithDlcAndDeprecatedDlcTest) {
2940 request_params_.set_dlc_apps_params(
2941 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
2942 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2943 fake_update_response_.dlc_app_update = true;
2944 fake_update_response_.dlc_app_no_update = true;
2945 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002946 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002947 ASSERT_TRUE(TestUpdateCheck());
2948
Amin Hassania859c922020-11-13 15:30:38 -08002949 EXPECT_TRUE(response_.update_exists);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002950}
2951
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002952TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002953 fake_update_response_.rollback = true;
2954 fake_update_response_.rollback_allowed_milestones = 4;
2955 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002956 fake_update_response_.deadline = "20101020";
2957 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2958 tuc_params_.is_consumer_device = false;
2959 tuc_params_.rollback_allowed_milestones = 4;
2960 tuc_params_.is_policy_loaded = true;
2961
2962 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002963 EXPECT_TRUE(response_.update_exists);
2964 EXPECT_TRUE(response_.is_rollback);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002965 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002966 response_.past_rollback_key_version.firmware_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002967 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002968 response_.past_rollback_key_version.firmware);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002969 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002970 response_.past_rollback_key_version.kernel_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002971 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08002972 response_.past_rollback_key_version.kernel);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002973}
2974
2975TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002976 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002977 fake_update_response_.rollback = true;
2978 fake_update_response_.rollback_allowed_milestones = 4;
2979 fake_update_response_.rollback_firmware_version = "4.3";
2980 fake_update_response_.rollback_kernel_version = "2.1";
2981 fake_update_response_.past_rollback_key_version =
2982 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002983 fake_update_response_.deadline = "20101020";
2984 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2985 tuc_params_.is_consumer_device = false;
2986 tuc_params_.rollback_allowed_milestones = 4;
2987 tuc_params_.is_policy_loaded = true;
2988
2989 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08002990 EXPECT_TRUE(response_.update_exists);
2991 EXPECT_TRUE(response_.is_rollback);
2992 EXPECT_EQ(16, response_.past_rollback_key_version.firmware_key);
2993 EXPECT_EQ(15, response_.past_rollback_key_version.firmware);
2994 EXPECT_EQ(14, response_.past_rollback_key_version.kernel_key);
2995 EXPECT_EQ(13, response_.past_rollback_key_version.kernel);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002996}
2997
2998TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002999 fake_update_response_.rollback = true;
3000 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07003001 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003002 request_params_.set_rollback_allowed_milestones(4);
3003
3004 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
3005 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
3006 // to 2, OmahaRequestAction will look for the key values of N-4 version but
3007 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07003008 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3009 tuc_params_.is_consumer_device = false;
3010 tuc_params_.rollback_allowed_milestones = 2;
3011 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003012
Amin Hassani41ac04b2019-03-29 11:31:03 -07003013 EXPECT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08003014 EXPECT_TRUE(response_.update_exists);
3015 EXPECT_TRUE(response_.is_rollback);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003016 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003017 response_.past_rollback_key_version.firmware_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003018 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003019 response_.past_rollback_key_version.firmware);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003020 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003021 response_.past_rollback_key_version.kernel_key);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003022 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
Amin Hassania859c922020-11-13 15:30:38 -08003023 response_.past_rollback_key_version.kernel);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003024}
3025
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003026TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
3027 request_params_.set_device_requisition("remora");
3028 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3029 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08003030 EXPECT_NE(string::npos, post_str_.find("requisition=\"remora\""));
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003031}
3032
3033TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
3034 request_params_.set_device_requisition("");
3035 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3036 ASSERT_TRUE(TestUpdateCheck());
Amin Hassania859c922020-11-13 15:30:38 -08003037 EXPECT_EQ(string::npos, post_str_.find("requisition"));
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003038}
3039
Amin Hassani2cbb0692019-10-30 13:36:17 -07003040TEST_F(OmahaRequestActionTest, PersistEolDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003041 tuc_params_.http_response =
3042 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3043 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3044 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003045 "_eol_date=\"200\" _foo=\"bar\"/></app></response>";
3046 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3047 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3048
3049 ASSERT_TRUE(TestUpdateCheck());
3050
3051 string eol_date;
Amin Hassani538bd592020-11-04 20:46:08 -08003052 EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetString(kPrefsOmahaEolDate,
3053 &eol_date));
Amin Hassani2cbb0692019-10-30 13:36:17 -07003054 EXPECT_EQ("200", eol_date);
3055}
3056
3057TEST_F(OmahaRequestActionTest, PersistEolMissingDateTest) {
3058 tuc_params_.http_response =
3059 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3060 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3061 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003062 "_foo=\"bar\"/></app></response>";
3063 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3064 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3065
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003066 const string kDate = "123";
Amin Hassani538bd592020-11-04 20:46:08 -08003067 FakeSystemState::Get()->prefs()->SetString(kPrefsOmahaEolDate, kDate);
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003068
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003069 ASSERT_TRUE(TestUpdateCheck());
3070
Amin Hassani2cbb0692019-10-30 13:36:17 -07003071 string eol_date;
Amin Hassani538bd592020-11-04 20:46:08 -08003072 EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetString(kPrefsOmahaEolDate,
3073 &eol_date));
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003074 EXPECT_EQ(kDate, eol_date);
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003075}
3076
Amin Hassani2cbb0692019-10-30 13:36:17 -07003077TEST_F(OmahaRequestActionTest, PersistEolBadDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003078 tuc_params_.http_response =
3079 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3080 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3081 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003082 "_eol_date=\"bad\" foo=\"bar\"/></app></response>";
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003083 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3084 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3085
3086 ASSERT_TRUE(TestUpdateCheck());
3087
Amin Hassani2cbb0692019-10-30 13:36:17 -07003088 string eol_date;
Amin Hassani538bd592020-11-04 20:46:08 -08003089 EXPECT_TRUE(FakeSystemState::Get()->prefs()->GetString(kPrefsOmahaEolDate,
3090 &eol_date));
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003091 EXPECT_EQ(kEolDateInvalid, StringToEolDate(eol_date));
3092}
3093
Andrew065d78d2020-04-07 15:43:07 -07003094TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyNoPing) {
3095 OmahaRequestParams::AppParams app_param = {.name = dlc_id_};
Andrewe045aef2020-01-08 16:29:22 -08003096 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003097 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003098
3099 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003100
3101 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003102 // If there was no ping, the metadata files shouldn't exist yet.
Amin Hassani90e9f192020-11-18 14:20:56 -08003103 EXPECT_FALSE(fake_prefs_->GetInt64(active_key_, &temp_int));
3104 EXPECT_FALSE(fake_prefs_->GetInt64(last_active_key_, &temp_int));
3105 EXPECT_FALSE(fake_prefs_->GetInt64(last_rollcall_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003106}
3107
Andrew065d78d2020-04-07 15:43:07 -07003108TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyActiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003109 // Create Active value
Amin Hassani90e9f192020-11-18 14:20:56 -08003110 fake_prefs_->SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003111
3112 OmahaRequestParams::AppParams app_param = {
3113 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003114 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003115 .ping_active = 1,
3116 .send_ping = true};
3117 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003118 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003119
Andrew065d78d2020-04-07 15:43:07 -07003120 int64_t temp_int;
3121 string temp_str;
Andrewe045aef2020-01-08 16:29:22 -08003122 ASSERT_TRUE(TestUpdateCheck());
Amin Hassani90e9f192020-11-18 14:20:56 -08003123 EXPECT_TRUE(fake_prefs_->GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003124 EXPECT_EQ(temp_int, kPingInactiveValue);
Amin Hassani90e9f192020-11-18 14:20:56 -08003125 EXPECT_TRUE(fake_prefs_->GetString(last_active_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003126 EXPECT_EQ(temp_str, "4763");
Amin Hassani90e9f192020-11-18 14:20:56 -08003127 EXPECT_TRUE(fake_prefs_->GetString(last_rollcall_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003128 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003129}
3130
Andrew065d78d2020-04-07 15:43:07 -07003131TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyInactiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003132 // Create Active value
Amin Hassani90e9f192020-11-18 14:20:56 -08003133 fake_prefs_->SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003134
3135 OmahaRequestParams::AppParams app_param = {
3136 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003137 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003138 .ping_active = 0,
3139 .send_ping = true};
3140 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003141 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003142
3143 // Set the previous active value to an older value than 4763.
Amin Hassani90e9f192020-11-18 14:20:56 -08003144 fake_prefs_->SetString(last_active_key_, "555");
Andrewe045aef2020-01-08 16:29:22 -08003145
Andrew065d78d2020-04-07 15:43:07 -07003146 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003147 ASSERT_TRUE(TestUpdateCheck());
Amin Hassani90e9f192020-11-18 14:20:56 -08003148 EXPECT_TRUE(fake_prefs_->GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003149 EXPECT_EQ(temp_int, kPingInactiveValue);
Andrew065d78d2020-04-07 15:43:07 -07003150 string temp_str;
Amin Hassani90e9f192020-11-18 14:20:56 -08003151 EXPECT_TRUE(fake_prefs_->GetString(last_active_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003152 EXPECT_EQ(temp_str, "555");
Amin Hassani90e9f192020-11-18 14:20:56 -08003153 EXPECT_TRUE(fake_prefs_->GetString(last_rollcall_key_, &temp_str));
Andrew065d78d2020-04-07 15:43:07 -07003154 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003155}
3156
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07003157TEST_F(OmahaRequestActionTest, OmahaResponseUpdateCanExcludeCheck) {
3158 request_params_.set_dlc_apps_params(
3159 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3160 fake_update_response_.dlc_app_update = true;
3161 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3162
3163 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3164 ASSERT_TRUE(TestUpdateCheck());
3165 ASSERT_TRUE(delegate_.omaha_response_);
3166 const auto& packages = delegate_.omaha_response_->packages;
3167 ASSERT_EQ(packages.size(), 2);
3168
3169 EXPECT_FALSE(packages[0].can_exclude);
3170 EXPECT_TRUE(packages[1].can_exclude);
3171}
3172
3173TEST_F(OmahaRequestActionTest, OmahaResponseInstallCannotExcludeCheck) {
3174 request_params_.set_is_install(true);
3175 request_params_.set_dlc_apps_params(
3176 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3177 fake_update_response_.dlc_app_update = true;
3178 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3179
3180 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3181 ASSERT_TRUE(TestUpdateCheck());
3182 ASSERT_TRUE(delegate_.omaha_response_);
3183 const auto& packages = delegate_.omaha_response_->packages;
3184 ASSERT_EQ(packages.size(), 2);
3185
3186 EXPECT_FALSE(packages[0].can_exclude);
3187 EXPECT_FALSE(packages[1].can_exclude);
3188}
3189
Darin Petkov6a5b3222010-07-13 14:55:28 -07003190} // namespace chromeos_update_engine