blob: 8d94195bea60d6de5c0e1c7dfcc7a37bcb05d861 [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 {
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800138 chromeos_update_engine::OmahaRequestParams request_params{nullptr};
139 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=\"" +
Qijiang Fan0b6501c2020-05-21 05:19:12 +0900161 filename + "\" size=\"" + base::NumberToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800162 "\" hash_sha256=\"" + hash + "\"/>" +
163 (multi_package ? "<package name=\"package2\" size=\"222\" "
164 "hash_sha256=\"hash2\"/>"
165 : "") +
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>"
Sen Jiang81259682017-03-30 15:11:30 -0700190 "<package name=\"package3\" size=\"333\" "
191 "hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700192 "<actions><action event=\"postinstall\" " +
193 (multi_app_self_update
194 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
195 : "IsDeltaPayload=\"false\" ") +
196 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700197 "</manifest></updatecheck></app>"
198 : "") +
199 (multi_app_no_update
200 ? "<app><updatecheck status=\"noupdate\"/></app>"
201 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700202 (multi_app_skip_updatecheck
203 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
204 : "") +
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800205 (dlc_app_update
206 ? "<app appid=\"" + request_params.GetDlcAppId(kDlcId1) +
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800207 "\" " +
208 (include_dlc_cohorts
209 ? "cohort=\"" + dlc_cohort + "\" cohorthint=\"" +
210 dlc_cohorthint + "\" cohortname=\"" +
211 dlc_cohortname + "\" "
212 : "") +
213 "status=\"ok\">"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800214 "<updatecheck status=\"ok\"><urls><url codebase=\"" +
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700215 codebase + "\"/><url codebase=\"" + codebase2 +
216 "\"/></urls><manifest version=\"" + version +
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800217 "\"><packages><package name=\"package3\" size=\"333\" "
218 "hash_sha256=\"hash3\"/></packages><actions>"
219 "<action event=\"install\" run=\".signed\"/>"
220 "<action event=\"postinstall\" MetadataSize=\"33\"/>"
221 "</actions></manifest></updatecheck></app>"
222 : "") +
223 (dlc_app_no_update
224 ? "<app appid=\"" + request_params.GetDlcAppId(kDlcId2) +
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800225 +"\" " +
226 (include_dlc_cohorts
227 ? "cohort=\"" + dlc_cohort + "\" cohorthint=\"" +
228 dlc_cohorthint + "\" cohortname=\"" +
229 dlc_cohortname + "\" "
230 : "") +
231 "><updatecheck status=\"noupdate\"/></app>"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800232 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700233 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700234 }
235
236 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800237 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700238
Alex Deymo85616652015-10-15 18:48:31 -0700239 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700240 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700241 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
242 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700243 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700244 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700245 string more_info_url = "http://more/info";
246 string prompt = "true";
247 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700248 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700249 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700250 string hash = "4841534831323334";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800251 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700252 string deadline = "";
253 string max_days_to_scatter = "7";
254 string elapsed_days = "42";
255
256 // P2P setting defaults to allowed.
257 bool disable_p2p_for_downloading = false;
258 bool disable_p2p_for_sharing = false;
259
Sen Jiangfe284402018-03-21 14:03:50 -0700260 bool powerwash = false;
261
Alex Deymo8e18f932015-03-27 16:16:59 -0700262 // Omaha cohorts settings.
263 bool include_cohorts = false;
264 string cohort = "";
265 string cohorthint = "";
266 string cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800267 // Whether to include Omaha cohorts for DLC apps.
268 bool include_dlc_cohorts = false;
269 string dlc_cohort = "";
270 string dlc_cohorthint = "";
271 string dlc_cohortname = "";
Alex Deymo8e18f932015-03-27 16:16:59 -0700272
273 // Whether to include the CrOS <!ENTITY> in the XML response.
274 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800275
Sen Jiang81259682017-03-30 15:11:30 -0700276 // Whether to include more than one app.
277 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700278 // Whether to include an app with noupdate="true".
279 bool multi_app_self_update = false;
280 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700281 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700282 // Whether to include an additional app with no updatecheck tag.
283 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700284 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800285 bool multi_package = false;
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800286 // Whether to include a DLC app with updatecheck tag.
287 bool dlc_app_update = false;
288 // Whether to include a DLC app with no updatecheck tag.
289 bool dlc_app_no_update = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700290
291 // Whether the payload is a rollback.
292 bool rollback = false;
293 // The verified boot firmware key version for the rollback image.
294 string rollback_firmware_version = "";
295 // The verified boot kernel key version for the rollback image.
296 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700297 // The number of milestones back that the verified boot key version has been
298 // supplied.
299 uint32_t rollback_allowed_milestones = 0;
300 // The verified boot key version for the
301 // |current - rollback_allowed_milestones| most recent release.
302 // The pair contains <firmware_key_version, kernel_key_version> each
303 // of which is in the form "key_version.version".
304 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700305};
306
307} // namespace
308
Darin Petkov6a5b3222010-07-13 14:55:28 -0700309namespace chromeos_update_engine {
310
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700311class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
312 public:
313 OmahaRequestActionTestProcessorDelegate()
314 : expected_code_(ErrorCode::kSuccess),
315 interactive_(false),
316 test_http_fetcher_headers_(false) {}
317 ~OmahaRequestActionTestProcessorDelegate() override = default;
318
319 void ProcessingDone(const ActionProcessor* processor,
320 ErrorCode code) override {
321 brillo::MessageLoop::current()->BreakLoop();
322 }
323
324 void ActionCompleted(ActionProcessor* processor,
325 AbstractAction* action,
326 ErrorCode code) override {
327 // Make sure actions always succeed.
328 if (action->Type() == OmahaRequestAction::StaticType()) {
329 EXPECT_EQ(expected_code_, code);
330 // Check that the headers were set in the fetcher during the action. Note
331 // that we set this request as "interactive".
332 auto fetcher = static_cast<const MockHttpFetcher*>(
333 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
334
335 if (test_http_fetcher_headers_) {
336 EXPECT_EQ(interactive_ ? "fg" : "bg",
337 fetcher->GetHeader("X-Goog-Update-Interactivity"));
338 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
339 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
340 }
341 post_data_ = fetcher->post_data();
342 } else if (action->Type() ==
343 ObjectCollectorAction<OmahaResponse>::StaticType()) {
344 EXPECT_EQ(ErrorCode::kSuccess, code);
345 auto collector_action =
346 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
347 omaha_response_.reset(new OmahaResponse(collector_action->object()));
348 EXPECT_TRUE(omaha_response_);
349 } else {
350 EXPECT_EQ(ErrorCode::kSuccess, code);
351 }
352 }
353 ErrorCode expected_code_;
354 brillo::Blob post_data_;
355 bool interactive_;
356 bool test_http_fetcher_headers_;
357 std::unique_ptr<OmahaResponse> omaha_response_;
358};
359
Amin Hassani41ac04b2019-03-29 11:31:03 -0700360struct TestUpdateCheckParams {
361 string http_response;
362 int fail_http_response_code;
363 bool ping_only;
364 bool is_consumer_device;
365 int rollback_allowed_milestones;
366 bool is_policy_loaded;
367 ErrorCode expected_code;
368 metrics::CheckResult expected_check_result;
369 metrics::CheckReaction expected_check_reaction;
370 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700371 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700372};
373
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700374class OmahaRequestActionTest : public ::testing::Test {
375 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800376 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200377 request_params_.set_os_sp("service_pack");
378 request_params_.set_os_board("x86-generic");
379 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700380 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200381 request_params_.set_app_lang("en-US");
382 request_params_.set_current_channel("unittest");
383 request_params_.set_target_channel("unittest");
384 request_params_.set_hwid("OEM MODEL 09235 7471");
Marton Hunyady2abda312018-04-24 18:21:49 +0200385 request_params_.set_delta_okay(true);
386 request_params_.set_interactive(false);
387 request_params_.set_update_url("http://url");
388 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200389 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200390 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700391 request_params_.set_is_install(false);
Andrewe045aef2020-01-08 16:29:22 -0800392 request_params_.set_dlc_apps_params({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200393
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700394 fake_system_state_.set_request_params(&request_params_);
395 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700396
397 // Setting the default update check params. Lookup |TestUpdateCheck()|.
398 tuc_params_ = {
399 .http_response = "",
400 .fail_http_response_code = -1,
401 .ping_only = false,
402 .is_consumer_device = true,
403 .rollback_allowed_milestones = 0,
404 .is_policy_loaded = false,
405 .expected_code = ErrorCode::kSuccess,
406 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
407 .expected_check_reaction = metrics::CheckReaction::kUpdating,
408 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
409 };
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700410
411 ON_CALL(*fake_system_state_.mock_update_attempter(), GetExcluder())
412 .WillByDefault(Return(&mock_excluder_));
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700413 }
414
Andrewe045aef2020-01-08 16:29:22 -0800415 // This function uses the parameters in |tuc_params_| to do an update check.
416 // It will fill out |post_str| with the result data and |response| with
Amin Hassani41ac04b2019-03-29 11:31:03 -0700417 // |OmahaResponse|. Returns true iff an output response was obtained from the
418 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
419 // transfer will fail with that code. |ping_only| is passed through to the
420 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700421 //
422 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700423 // |expected_error_code| parameters are for checking expectations about
424 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
425 // statistics. Use the appropriate ::kUnset value to specify that the given
426 // metric should not be reported.
427 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700428
Amin Hassani41ac04b2019-03-29 11:31:03 -0700429 // Tests events using |event| and |https_response|. It will fill up |post_str|
430 // with the result data.
431 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700432
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800433 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700434 // only a ping or also an updatecheck.
435 void PingTest(bool ping_only);
436
437 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800438 bool InstallDateParseHelper(const string& elapsed_days,
439 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700440
441 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800442 void P2PTest(bool initial_allow_p2p_for_downloading,
443 bool initial_allow_p2p_for_sharing,
444 bool omaha_disable_p2p_for_downloading,
445 bool omaha_disable_p2p_for_sharing,
446 bool payload_state_allow_p2p_attempt,
447 bool expect_p2p_client_lookup,
448 const string& p2p_client_result_url,
449 bool expected_allow_p2p_for_downloading,
450 bool expected_allow_p2p_for_sharing,
451 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700452
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700453 StrictMock<MockExcluder> mock_excluder_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700454 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700455 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200456 // Used by all tests.
457 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700458
459 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700460
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700461 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700462
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700463 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700464
465 TestUpdateCheckParams tuc_params_;
466
467 // TODO(ahassani): Add trailing _ to these two variables.
468 OmahaResponse response;
469 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700470};
471
Andrew065d78d2020-04-07 15:43:07 -0700472class OmahaRequestActionDlcPingTest : public OmahaRequestActionTest {
473 protected:
474 void SetUp() override {
475 OmahaRequestActionTest::SetUp();
476 dlc_id_ = "dlc0";
477 active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700478 {kDlcPrefsSubDir, dlc_id_, kPrefsPingActive});
Andrew065d78d2020-04-07 15:43:07 -0700479 last_active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700480 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastActive});
Andrew065d78d2020-04-07 15:43:07 -0700481 last_rollcall_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700482 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastRollcall});
Andrew065d78d2020-04-07 15:43:07 -0700483
484 tuc_params_.http_response =
485 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
486 "protocol=\"3.0\"><daystart elapsed_days=\"4763\" "
487 "elapsed_seconds=\"36540\"/><app appid=\"test-app-id\" status=\"ok\">\""
488 "<updatecheck status=\"noupdate\"/></app><app "
489 "appid=\"test-app-id_dlc0\" "
490 "status=\"ok\"><ping status=\"ok\"/><updatecheck status=\"noupdate\"/>"
491 "</app></response>";
492 tuc_params_.expected_check_result =
493 metrics::CheckResult::kNoUpdateAvailable;
494 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
495 }
496
497 std::string dlc_id_;
498 std::string active_key_;
499 std::string last_active_key_;
500 std::string last_rollcall_key_;
501};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700502bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700503 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700504 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700505 auto fetcher =
506 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
507 tuc_params_.http_response.size(),
508 nullptr);
509 if (tuc_params_.fail_http_response_code >= 0) {
510 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700511 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200512 // This ensures the tests didn't forget to update fake_system_state_ if they
513 // are not using the default request_params_.
514 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
515
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700516 auto omaha_request_action =
517 std::make_unique<OmahaRequestAction>(&fake_system_state_,
518 nullptr,
519 std::move(fetcher),
520 tuc_params_.ping_only,
521 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700522
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800523 auto mock_policy_provider =
524 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
525 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700526 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800527
528 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700529 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800530
531 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700532 const bool get_allowed_milestone_succeeds =
533 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800534 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700535 .WillRepeatedly(
536 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
537 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800538
539 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
540 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700541 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800542
Amin Hassani41ac04b2019-03-29 11:31:03 -0700543 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700544 delegate_.interactive_ = request_params_.interactive();
545 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700546 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700547 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700548
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700549 auto collector_action =
550 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
551 BondActions(omaha_request_action.get(), collector_action.get());
552 processor.EnqueueAction(std::move(omaha_request_action));
553 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700554
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700555 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
556 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800557 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700558
Amin Hassani41ac04b2019-03-29 11:31:03 -0700559 EXPECT_CALL(
560 *fake_system_state_.mock_metrics_reporter(),
561 ReportUpdateCheckMetrics(_,
562 tuc_params_.expected_check_result,
563 tuc_params_.expected_check_reaction,
564 tuc_params_.expected_download_error_code))
565 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800566
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700567 loop.PostTask(base::Bind(
568 [](ActionProcessor* processor) { processor->StartProcessing(); },
569 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700570 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700571 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700572 if (delegate_.omaha_response_)
573 response = *delegate_.omaha_response_;
574 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700575 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700576}
577
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700578// Tests Event requests -- they should always succeed. |out_post_data| may be
579// null; if non-null, the post-data received by the mock HttpFetcher is
580// returned.
581void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700582 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700583 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700584 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700585
586 auto action = std::make_unique<OmahaRequestAction>(
587 &fake_system_state_,
588 event,
589 std::make_unique<MockHttpFetcher>(
590 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700591 false,
592 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700593 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700594 processor.set_delegate(&delegate_);
595 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700596
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700597 loop.PostTask(base::Bind(
598 [](ActionProcessor* processor) { processor->StartProcessing(); },
599 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700600 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700601 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700602
Amin Hassani41ac04b2019-03-29 11:31:03 -0700603 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700604}
605
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700606TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700607 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700608 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
609 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
610 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
611 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
612
613 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400614 EXPECT_FALSE(response.update_exists);
615}
616
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700617TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700618 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
619 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
620 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
621
622 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700623 EXPECT_FALSE(response.update_exists);
624}
625
Sen Jiang81259682017-03-30 15:11:30 -0700626TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700627 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700628 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
629 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
630 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
631
632 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700633 EXPECT_FALSE(response.update_exists);
634}
635
Sen Jiang00adf7b2017-06-26 15:57:29 -0700636TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700637 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700638 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
639 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
640 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
641
642 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700643 EXPECT_FALSE(response.update_exists);
644}
645
646TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700647 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700648 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
649 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700650 "</manifest></updatecheck></app></response>";
651 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
652 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
653
654 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700655 EXPECT_FALSE(response.update_exists);
656}
657
Alex Deymo8e18f932015-03-27 16:16:59 -0700658// Test that all the values in the response are parsed in a normal update
659// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700660TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700661 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700662 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
663
664 ASSERT_TRUE(TestUpdateCheck());
665
Darin Petkov6a5b3222010-07-13 14:55:28 -0700666 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700667 EXPECT_EQ(fake_update_response_.version, response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800668 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
669 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700670 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800671 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
672 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700673 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700674 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
675 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700676 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700677 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700678 // persisted.
679 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
680 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
681 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700682}
683
Sen Jiang0affc2c2017-02-10 15:55:05 -0800684TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800685 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700686 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
687
688 ASSERT_TRUE(TestUpdateCheck());
689
Sen Jiang0affc2c2017-02-10 15:55:05 -0800690 EXPECT_TRUE(response.update_exists);
691 EXPECT_EQ(fake_update_response_.version, response.version);
692 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
693 response.packages[0].payload_urls[0]);
694 EXPECT_EQ(fake_update_response_.codebase + "package2",
695 response.packages[1].payload_urls[0]);
696 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
697 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700698 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700699 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800700 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700701 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800702 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700703 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700704 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700705}
706
707TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700708 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700709 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
710
711 ASSERT_TRUE(TestUpdateCheck());
712
Sen Jiang81259682017-03-30 15:11:30 -0700713 EXPECT_TRUE(response.update_exists);
714 EXPECT_EQ(fake_update_response_.version, response.version);
715 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
716 response.packages[0].payload_urls[0]);
717 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
718 response.packages[1].payload_urls[0]);
719 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
720 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
721 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700722 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700723 ASSERT_EQ(2u, response.packages.size());
724 EXPECT_EQ(string("hash3"), response.packages[1].hash);
725 EXPECT_EQ(333u, response.packages[1].size);
726 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700727 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700728}
729
730TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700731 fake_update_response_.multi_app = true;
732 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700733 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
734
735 ASSERT_TRUE(TestUpdateCheck());
736
Sen Jiang81259682017-03-30 15:11:30 -0700737 EXPECT_TRUE(response.update_exists);
738 EXPECT_EQ(fake_update_response_.version, response.version);
739 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
740 response.packages[0].payload_urls[0]);
741 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
742 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
743 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700744 ASSERT_EQ(2u, response.packages.size());
745 EXPECT_EQ(string("hash3"), response.packages[1].hash);
746 EXPECT_EQ(333u, response.packages[1].size);
747 EXPECT_EQ(33u, response.packages[1].metadata_size);
748 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700749}
750
751TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700752 fake_update_response_.multi_app = true;
753 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700754 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
755
756 ASSERT_TRUE(TestUpdateCheck());
757
Sen Jiang81259682017-03-30 15:11:30 -0700758 EXPECT_TRUE(response.update_exists);
759 EXPECT_EQ(fake_update_response_.version, response.version);
760 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
761 response.packages[0].payload_urls[0]);
762 EXPECT_EQ(fake_update_response_.codebase + "package2",
763 response.packages[1].payload_urls[0]);
764 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
765 response.packages[2].payload_urls[0]);
766 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
767 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
768 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700769 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700770 ASSERT_EQ(3u, response.packages.size());
771 EXPECT_EQ(string("hash2"), response.packages[1].hash);
772 EXPECT_EQ(222u, response.packages[1].size);
773 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700774 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700775 EXPECT_EQ(string("hash3"), response.packages[2].hash);
776 EXPECT_EQ(333u, response.packages[2].size);
777 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700778 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800779}
780
Sen Jiangfe284402018-03-21 14:03:50 -0700781TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700782 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700783 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
784
785 ASSERT_TRUE(TestUpdateCheck());
786
Sen Jiangfe284402018-03-21 14:03:50 -0700787 EXPECT_TRUE(response.update_exists);
788 EXPECT_TRUE(response.powerwash_required);
789}
790
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700791TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700792 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700793 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700794 tuc_params_.http_response = "invalid xml>";
795 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
796 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
797 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
798
799 ASSERT_FALSE(TestUpdateCheck());
800
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700801 EXPECT_FALSE(response.update_exists);
802}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700803
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700804TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700805 request_params_.set_interactive(false);
806 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 Hassanid3f4bea2018-04-30 14:52:40 -0700814 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700815}
816
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700817TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700818 // Set up a connection manager that doesn't allow a valid update over
819 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700820 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700821 fake_system_state_.set_connection_manager(&mock_cm);
822
Alex Deymo30534502015-07-20 15:06:33 -0700823 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800824 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
825 SetArgPointee<1>(ConnectionTethering::kUnknown),
826 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700827 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
828 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700829
Amin Hassani41ac04b2019-03-29 11:31:03 -0700830 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
831 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
832 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
833
834 ASSERT_FALSE(TestUpdateCheck());
835
Chris Sosa77f79e82014-06-02 18:16:24 -0700836 EXPECT_FALSE(response.update_exists);
837}
838
Weidong Guo421ff332017-04-17 10:08:38 -0700839TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
840 // This test tests that update over cellular is allowed as device policy
841 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700842 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700843 fake_system_state_.set_connection_manager(&mock_cm);
844
845 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
846 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
847 SetArgPointee<1>(ConnectionTethering::kUnknown),
848 Return(true)));
849 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
850 .WillRepeatedly(Return(true));
851 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
852 .WillRepeatedly(Return(true));
853
Amin Hassani41ac04b2019-03-29 11:31:03 -0700854 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
855
856 ASSERT_TRUE(TestUpdateCheck());
857
Weidong Guo421ff332017-04-17 10:08:38 -0700858 EXPECT_TRUE(response.update_exists);
859}
860
861TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
862 // This test tests that update over cellular is blocked as device policy
863 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700864 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700865 fake_system_state_.set_connection_manager(&mock_cm);
866
867 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
868 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
869 SetArgPointee<1>(ConnectionTethering::kUnknown),
870 Return(true)));
871 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
872 .WillRepeatedly(Return(true));
873 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
874 .WillRepeatedly(Return(false));
875
Amin Hassani41ac04b2019-03-29 11:31:03 -0700876 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
877 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
878 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
879
880 ASSERT_FALSE(TestUpdateCheck());
881
Weidong Guo421ff332017-04-17 10:08:38 -0700882 EXPECT_FALSE(response.update_exists);
883}
884
885TEST_F(OmahaRequestActionTest,
886 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
887 // This test tests that, when device policy is not set, update over cellular
888 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700889 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700890 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
891 fake_system_state_.set_connection_manager(&mock_cm);
892
893 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
894 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
895 SetArgPointee<1>(ConnectionTethering::kUnknown),
896 Return(true)));
897 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
898 .WillRepeatedly(Return(false));
899 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
900 .WillRepeatedly(Return(true));
901
Amin Hassani41ac04b2019-03-29 11:31:03 -0700902 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
903
904 ASSERT_TRUE(TestUpdateCheck());
905
Weidong Guo421ff332017-04-17 10:08:38 -0700906 EXPECT_TRUE(response.update_exists);
907}
908
909TEST_F(OmahaRequestActionTest,
910 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
911 // This test tests that, when device policy is not set and permission for
912 // update over cellular is set to false or does not exist, update over
913 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700914 MockConnectionManager mock_cm;
915 // A version different from the version in omaha response.
916 string diff_version = "99.99.99";
917 // A size different from the size in omaha response.
918 int64_t diff_size = 999;
919
920 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
921 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
922 // This test tests cellular (3G) being the only connection type being allowed.
923 fake_system_state_.set_connection_manager(&mock_cm);
924
925 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
926 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
927 SetArgPointee<1>(ConnectionTethering::kUnknown),
928 Return(true)));
929 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
930 .WillRepeatedly(Return(false));
931 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
932 .WillRepeatedly(Return(true));
933
Amin Hassani41ac04b2019-03-29 11:31:03 -0700934 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
935 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
936 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
937
938 ASSERT_FALSE(TestUpdateCheck());
939
Weidong Guo421ff332017-04-17 10:08:38 -0700940 EXPECT_FALSE(response.update_exists);
941}
942
943TEST_F(OmahaRequestActionTest,
944 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
945 // This test tests that, when device policy is not set and permission for
946 // update over cellular is set to false or does not exist, update over
947 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700948 MockConnectionManager mock_cm;
949 // A version same as the version in omaha response.
950 string new_version = fake_update_response_.version;
951 // A size same as the size in omaha response.
952 int64_t new_size = fake_update_response_.size;
953
954 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
955 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
956 fake_system_state_.set_connection_manager(&mock_cm);
957
958 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
959 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
960 SetArgPointee<1>(ConnectionTethering::kUnknown),
961 Return(true)));
962 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
963 .WillRepeatedly(Return(false));
964 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
965 .WillRepeatedly(Return(true));
966
Amin Hassani41ac04b2019-03-29 11:31:03 -0700967 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
968
969 ASSERT_TRUE(TestUpdateCheck());
970
Weidong Guo421ff332017-04-17 10:08:38 -0700971 EXPECT_TRUE(response.update_exists);
972}
973
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700974TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700975 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700976 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700977 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700978 fake_update_response_.version = rollback_version;
979 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
980 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
981 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700982
Chris Sosa77f79e82014-06-02 18:16:24 -0700983 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800984 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700985
Amin Hassani41ac04b2019-03-29 11:31:03 -0700986 ASSERT_FALSE(TestUpdateCheck());
987
Chris Sosa77f79e82014-06-02 18:16:24 -0700988 EXPECT_FALSE(response.update_exists);
989}
990
Marton Hunyadyc2882062018-05-14 17:28:25 +0200991// Verify that update checks called during OOBE will not try to download an
992// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700993TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200994 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700995 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
996 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
997 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
998 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -0700999
Sen Jiang8cd42342018-01-31 12:06:59 -08001000 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001001 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001002 ASSERT_FALSE(TestUpdateCheck());
1003
Kevin Cernekee2494e282016-03-29 18:03:53 -07001004 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001005}
Kevin Cernekee2494e282016-03-29 18:03:53 -07001006
Marton Hunyadyc2882062018-05-14 17:28:25 +02001007// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
1008// enabled.
1009TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +02001010 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -07001011 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001012 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -07001013
Amin Hassani41ac04b2019-03-29 11:31:03 -07001014 ASSERT_TRUE(TestUpdateCheck());
1015
Kevin Cernekee2494e282016-03-29 18:03:53 -07001016 EXPECT_TRUE(response.update_exists);
1017}
1018
Marton Hunyadyc2882062018-05-14 17:28:25 +02001019// Verify that update checks called during OOBE will still try to download an
1020// update if the response includes the deadline field.
1021TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +02001022 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1023 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001024 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001025
Amin Hassani41ac04b2019-03-29 11:31:03 -07001026 ASSERT_TRUE(TestUpdateCheck());
1027
Marton Hunyadyc2882062018-05-14 17:28:25 +02001028 EXPECT_TRUE(response.update_exists);
1029}
1030
1031// Verify that update checks called during OOBE will not try to download an
1032// update if a rollback happened, even when the response includes the deadline
1033// field.
1034TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +02001035 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1036 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001037 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1038 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1039 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1040 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1041
Marton Hunyadyc2882062018-05-14 17:28:25 +02001042 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
1043 .WillOnce(Return(true));
1044
Amin Hassani41ac04b2019-03-29 11:31:03 -07001045 ASSERT_FALSE(TestUpdateCheck());
1046
Marton Hunyadyc2882062018-05-14 17:28:25 +02001047 EXPECT_FALSE(response.update_exists);
1048}
1049
Toni Barzic61544e62018-10-11 14:37:30 -07001050// Verify that non-critical updates are skipped by reporting the
1051// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1052// i.e. when the update would need user permission. Note that reporting
1053// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1054// in OOBE (warning the user about an update that will be skipped).
1055TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001056 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1057
1058 MockConnectionManager mock_cm;
1059 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001060 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1061 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1062 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1063 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001064
1065 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1066 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1067 SetArgPointee<1>(ConnectionTethering::kUnknown),
1068 Return(true)));
1069 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1070 .WillRepeatedly(Return(false));
1071
Amin Hassani41ac04b2019-03-29 11:31:03 -07001072 ASSERT_FALSE(TestUpdateCheck());
1073
Toni Barzic61544e62018-10-11 14:37:30 -07001074 EXPECT_FALSE(response.update_exists);
1075}
1076
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001077TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001078 request_params_.set_wall_clock_based_wait_enabled(true);
1079 request_params_.set_update_check_count_wait_enabled(false);
1080 request_params_.set_waiting_period(TimeDelta::FromDays(2));
May Lippert60aa3ca2018-08-15 16:55:29 -07001081 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001082 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1083 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1084 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001085
Amin Hassani41ac04b2019-03-29 11:31:03 -07001086 ASSERT_FALSE(TestUpdateCheck());
1087
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001088 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001089}
1090
1091TEST_F(OmahaRequestActionTest,
1092 WallClockBasedWaitAloneCausesScatteringInteractive) {
1093 request_params_.set_wall_clock_based_wait_enabled(true);
1094 request_params_.set_update_check_count_wait_enabled(false);
1095 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1096 request_params_.set_interactive(true);
1097 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1098 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001099
1100 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001101 ASSERT_TRUE(TestUpdateCheck());
1102
Chris Sosa968d0572013-08-23 14:46:02 -07001103 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001104}
1105
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001106TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001107 request_params_.set_wall_clock_based_wait_enabled(false);
1108 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1109 request_params_.set_update_check_count_wait_enabled(true);
1110 request_params_.set_min_update_checks_needed(1);
1111 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001112 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001113
Amin Hassani41ac04b2019-03-29 11:31:03 -07001114 ASSERT_TRUE(TestUpdateCheck());
1115
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001116 EXPECT_TRUE(response.update_exists);
1117}
1118
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001119TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001120 request_params_.set_wall_clock_based_wait_enabled(true);
1121 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1122 request_params_.set_update_check_count_wait_enabled(true);
1123 request_params_.set_min_update_checks_needed(1);
1124 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001125 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001126 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1127
1128 ASSERT_TRUE(TestUpdateCheck());
1129
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001130 EXPECT_TRUE(response.update_exists);
1131}
1132
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001133TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001134 request_params_.set_wall_clock_based_wait_enabled(true);
1135 request_params_.set_waiting_period(TimeDelta());
1136 request_params_.set_update_check_count_wait_enabled(true);
1137 request_params_.set_min_update_checks_needed(0);
1138 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001139 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001140 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001141
Amin Hassani41ac04b2019-03-29 11:31:03 -07001142 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001143
Ben Chan9abb7632014-08-07 00:10:53 -07001144 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001145 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001146 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001147 EXPECT_TRUE(response.update_exists);
1148}
1149
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001150TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001151 request_params_.set_wall_clock_based_wait_enabled(true);
1152 request_params_.set_waiting_period(TimeDelta());
1153 request_params_.set_update_check_count_wait_enabled(true);
1154 request_params_.set_min_update_checks_needed(1);
1155 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001156 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001157 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1158 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1159 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001160
Amin Hassani41ac04b2019-03-29 11:31:03 -07001161 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001162
Ben Chan9abb7632014-08-07 00:10:53 -07001163 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001164 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001165 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001166 EXPECT_FALSE(response.update_exists);
1167}
1168
Amin Hassani41ac04b2019-03-29 11:31:03 -07001169TEST_F(OmahaRequestActionTest,
1170 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001171 request_params_.set_wall_clock_based_wait_enabled(true);
1172 request_params_.set_waiting_period(TimeDelta());
1173 request_params_.set_update_check_count_wait_enabled(true);
1174 request_params_.set_min_update_checks_needed(1);
1175 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001176 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001177 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001178 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1179
1180 // Verify if we are interactive check we don't defer.
1181 ASSERT_TRUE(TestUpdateCheck());
1182
1183 EXPECT_TRUE(response.update_exists);
1184}
1185
1186TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1187 request_params_.set_wall_clock_based_wait_enabled(true);
1188 request_params_.set_waiting_period(TimeDelta());
1189 request_params_.set_update_check_count_wait_enabled(true);
1190 request_params_.set_min_update_checks_needed(1);
1191 request_params_.set_max_update_checks_allowed(8);
1192 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1193 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1194 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1195 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001196
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001197 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001198 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001199
Ben Chan9abb7632014-08-07 00:10:53 -07001200 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001201 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001202 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001203 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001204 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001205 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001206}
1207
1208TEST_F(OmahaRequestActionTest,
1209 ExistingUpdateCheckCountCausesScatteringInteractive) {
1210 request_params_.set_wall_clock_based_wait_enabled(true);
1211 request_params_.set_waiting_period(TimeDelta());
1212 request_params_.set_update_check_count_wait_enabled(true);
1213 request_params_.set_min_update_checks_needed(1);
1214 request_params_.set_max_update_checks_allowed(8);
1215 request_params_.set_interactive(true);
1216 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1217 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1218
1219 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001220
1221 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001222 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001223 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001224}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001225
Adolfo Victoria497044c2018-07-18 07:51:42 -07001226TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1227 // If staging is on, the value for max days to scatter should be ignored, and
1228 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001229 request_params_.set_wall_clock_based_wait_enabled(true);
1230 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1231 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001232 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1233
Adolfo Victoria497044c2018-07-18 07:51:42 -07001234 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1235 // This should not prevent scattering due to staging.
1236 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001237 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1238 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1239 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1240
1241 ASSERT_FALSE(TestUpdateCheck());
1242
Adolfo Victoria497044c2018-07-18 07:51:42 -07001243 EXPECT_FALSE(response.update_exists);
1244
1245 // Interactive updates should not be affected.
1246 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001247 tuc_params_.expected_code = ErrorCode::kSuccess;
1248 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1249
1250 ASSERT_TRUE(TestUpdateCheck());
1251
Adolfo Victoria497044c2018-07-18 07:51:42 -07001252 EXPECT_TRUE(response.update_exists);
1253}
1254
Alex Deymo8e18f932015-03-27 16:16:59 -07001255TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001256 fake_update_response_.include_cohorts = true;
1257 fake_update_response_.cohort = "s/154454/8479665";
1258 fake_update_response_.cohorthint = "please-put-me-on-beta";
1259 fake_update_response_.cohortname = "stable";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001260 request_params_.set_dlc_apps_params(
1261 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
1262 fake_update_response_.dlc_app_update = true;
1263 fake_update_response_.include_dlc_cohorts = true;
1264 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1265 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1266 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001267 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001268
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001269 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001270 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001271
1272 string value;
1273 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1274 EXPECT_EQ(fake_update_response_.cohort, value);
1275
1276 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1277 EXPECT_EQ(fake_update_response_.cohorthint, value);
1278
1279 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1280 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001281
1282 EXPECT_TRUE(fake_prefs_.GetString(
1283 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
1284 &value));
1285 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1286
1287 EXPECT_TRUE(fake_prefs_.GetString(
1288 fake_prefs_.CreateSubKey(
1289 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1290 &value));
1291 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1292
1293 EXPECT_TRUE(fake_prefs_.GetString(
1294 fake_prefs_.CreateSubKey(
1295 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1296 &value));
1297 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Alex Deymo8e18f932015-03-27 16:16:59 -07001298}
1299
1300TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001301 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1302 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1303 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001304 const string dlc_cohort_key =
1305 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1306 const string dlc_cohort_hint_key = fake_prefs_.CreateSubKey(
1307 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint});
1308 const string dlc_cohort_name_key = fake_prefs_.CreateSubKey(
1309 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName});
1310 request_params_.set_dlc_apps_params(
1311 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
1312 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_key, "old_value_dlc"));
1313 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_hint_key, "old_hint_dlc"));
1314 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_name_key, "old_name_dlc"));
Alex Deymo8e18f932015-03-27 16:16:59 -07001315 fake_update_response_.include_cohorts = true;
1316 fake_update_response_.cohort = "s/154454/8479665";
1317 fake_update_response_.cohorthint = "please-put-me-on-beta";
1318 fake_update_response_.cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001319 fake_update_response_.dlc_app_update = true;
1320 fake_update_response_.include_dlc_cohorts = true;
1321 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1322 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1323 fake_update_response_.dlc_cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001324 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001325
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001326 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001327 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001328
1329 string value;
1330 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1331 EXPECT_EQ(fake_update_response_.cohort, value);
1332
1333 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1334 EXPECT_EQ(fake_update_response_.cohorthint, value);
1335
1336 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001337
1338 EXPECT_TRUE(fake_prefs_.GetString(dlc_cohort_key, &value));
1339 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1340
1341 EXPECT_TRUE(fake_prefs_.GetString(dlc_cohort_hint_key, &value));
1342 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1343
1344 EXPECT_FALSE(fake_prefs_.GetString(dlc_cohort_name_key, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001345}
1346
1347TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001348 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001349
Amin Hassani41ac04b2019-03-29 11:31:03 -07001350 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001351 const string dlc_cohort_key =
1352 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1353 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_key, "old_value_dlc"));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001354 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001355
1356 string value;
1357 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1358 EXPECT_EQ("old_value", value);
1359
1360 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1361 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001362
1363 EXPECT_TRUE(fake_prefs_.GetString(dlc_cohort_key, &value));
1364 EXPECT_EQ("old_value_dlc", value);
1365
1366 EXPECT_FALSE(fake_prefs_.GetString(
1367 fake_prefs_.CreateSubKey(
1368 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1369 &value));
1370 EXPECT_FALSE(fake_prefs_.GetString(
1371 fake_prefs_.CreateSubKey(
1372 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1373 &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001374}
1375
Alex Deymo00d79ac2015-06-29 15:41:49 -07001376TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001377 fake_update_response_.include_cohorts = true;
1378 fake_update_response_.cohort = "s/154454/8479665";
1379 fake_update_response_.cohorthint = "please-put-me-on-beta";
1380 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001381 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1382 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1383 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001384
Amin Hassani41ac04b2019-03-29 11:31:03 -07001385 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001386
1387 string value;
1388 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1389 EXPECT_EQ(fake_update_response_.cohort, value);
1390
1391 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1392 EXPECT_EQ(fake_update_response_.cohorthint, value);
1393
1394 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1395 EXPECT_EQ(fake_update_response_.cohortname, value);
1396}
1397
Sen Jiangb1e063a2017-09-15 17:44:31 -07001398TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001399 fake_update_response_.multi_app = true;
1400 fake_update_response_.include_cohorts = true;
1401 fake_update_response_.cohort = "s/154454/8479665";
1402 fake_update_response_.cohorthint = "please-put-me-on-beta";
1403 fake_update_response_.cohortname = "stable";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001404 request_params_.set_dlc_apps_params(
1405 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
1406 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
1407 fake_update_response_.dlc_app_update = true;
1408 fake_update_response_.dlc_app_no_update = true;
1409 fake_update_response_.include_dlc_cohorts = true;
1410 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1411 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1412 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001413 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001414
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001415 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001416 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001417
1418 string value;
1419 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1420 EXPECT_EQ(fake_update_response_.cohort, value);
1421
1422 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1423 EXPECT_EQ(fake_update_response_.cohorthint, value);
1424
1425 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1426 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001427
1428 EXPECT_TRUE(fake_prefs_.GetString(
1429 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
1430 &value));
1431 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1432 EXPECT_TRUE(fake_prefs_.GetString(
1433 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohort}),
1434 &value));
1435 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1436
1437 EXPECT_TRUE(fake_prefs_.GetString(
1438 fake_prefs_.CreateSubKey(
1439 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1440 &value));
1441 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1442 EXPECT_TRUE(fake_prefs_.GetString(
1443 fake_prefs_.CreateSubKey(
1444 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortHint}),
1445 &value));
1446 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1447
1448 EXPECT_TRUE(fake_prefs_.GetString(
1449 fake_prefs_.CreateSubKey(
1450 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1451 &value));
1452 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
1453 EXPECT_TRUE(fake_prefs_.GetString(
1454 fake_prefs_.CreateSubKey(
1455 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortName}),
1456 &value));
1457 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Sen Jiangb1e063a2017-09-15 17:44:31 -07001458}
1459
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001460TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001461 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001462 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001463 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001464
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001465 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001466 &fake_system_state_,
1467 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001468 std::make_unique<MockHttpFetcher>(
1469 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001470 false,
1471 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001472 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001473 processor.set_delegate(&delegate_);
1474 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001475
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001476 loop.PostTask(base::Bind(
1477 [](ActionProcessor* processor) { processor->StartProcessing(); },
1478 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001479 loop.Run();
1480 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001481 EXPECT_FALSE(processor.IsRunning());
1482}
1483
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001484TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001485 tuc_params_.http_response = "invalid xml>";
1486 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1487 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1488 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1489
1490 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001491 EXPECT_FALSE(response.update_exists);
1492}
1493
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001494TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001495 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1496 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1497 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1498
1499 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001500 EXPECT_FALSE(response.update_exists);
1501}
1502
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001503TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001504 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001505 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1506 "<daystart elapsed_seconds=\"100\"/>"
1507 "<app appid=\"foo\" status=\"ok\">"
1508 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001509 "<updatecheck/></app></response>";
1510 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1511 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1512 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1513
1514 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001515 EXPECT_FALSE(response.update_exists);
1516}
1517
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001518TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001519 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001520 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1521 "<daystart elapsed_seconds=\"100\"/>"
1522 "<app appid=\"foo\" status=\"ok\">"
1523 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001524 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1525 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1526 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1527 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1528
1529 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001530 EXPECT_FALSE(response.update_exists);
1531}
1532
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001533TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001534 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001535 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1536 "<daystart elapsed_seconds=\"100\"/>"
1537 "<app appid=\"foo\" status=\"ok\">"
1538 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001539 "</app></response>";
1540 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1541 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1542 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1543
1544 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001545 EXPECT_FALSE(response.update_exists);
1546}
1547
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001548TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001549 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001550 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1551 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001552 // the appid needs to match that in the request params
1553 "<app appid=\"" +
1554 fake_update_response_.app_id +
1555 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001556 "<updatecheck status=\"ok\">"
1557 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001558 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001559 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001560 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001561 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001562 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001563 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001564 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001565 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001566
Amin Hassani41ac04b2019-03-29 11:31:03 -07001567 ASSERT_TRUE(TestUpdateCheck());
1568
Darin Petkov6a5b3222010-07-13 14:55:28 -07001569 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001570 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001571 EXPECT_EQ("http://missing/field/test/f",
1572 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001573 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001574 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1575 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001576 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001577 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001578}
1579
1580namespace {
1581class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1582 public:
1583 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001584 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001585 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001586};
1587
Alex Deymo60ca1a72015-06-18 18:19:15 -07001588void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001589 processor->StartProcessing();
1590 CHECK(processor->IsRunning());
1591 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001592}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001593} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001594
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001595TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001596 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001597 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001598
Alex Deymo60ca1a72015-06-18 18:19:15 -07001599 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001600 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001601 &fake_system_state_,
1602 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001603 std::make_unique<MockHttpFetcher>(
1604 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001605 false,
1606 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001607 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001608 ActionProcessor processor;
1609 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001610 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001611
Alex Deymo60ca1a72015-06-18 18:19:15 -07001612 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1613 loop.Run();
1614 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001615}
1616
Alex Deymob0d74eb2015-03-30 17:59:17 -07001617TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001618 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001619 request_params_.set_os_sp("testtheservice_pack>");
1620 request_params_.set_os_board("x86 generic<id");
1621 request_params_.set_current_channel("unittest_track&lt;");
1622 request_params_.set_target_channel("unittest_track&lt;");
Amin Hassani37b67232020-08-13 09:29:48 -07001623 request_params_.set_lts_tag("unittest_hint&lt;");
Marton Hunyady2abda312018-04-24 18:21:49 +02001624 request_params_.set_hwid("<OEM MODEL>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001625 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1626 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001627 fake_prefs_.SetString(
1628 kPrefsOmahaCohortName,
1629 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001630 tuc_params_.http_response = "invalid xml>";
1631 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1632 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1633 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1634
1635 ASSERT_FALSE(TestUpdateCheck());
1636
Alex Deymo8e18f932015-03-27 16:16:59 -07001637 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1638 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1639 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1640 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1641 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1642 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
Amin Hassani37b67232020-08-13 09:29:48 -07001643 EXPECT_NE(string::npos, post_str.find("unittest_hint&amp;lt;"));
1644 EXPECT_EQ(string::npos, post_str.find("unittest_hint&lt;"));
Alex Deymo8e18f932015-03-27 16:16:59 -07001645 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1646 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1647 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1648 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1649 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1650 // Values from Prefs that are too big are removed from the XML instead of
1651 // encoded.
1652 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001653}
1654
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001655TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001656 fake_update_response_.deadline = "&lt;20110101";
1657 fake_update_response_.more_info_url = "testthe&lt;url";
1658 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001659 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1660
1661 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001662
Sen Jiang0affc2c2017-02-10 15:55:05 -08001663 EXPECT_EQ("testthe<url", response.more_info_url);
1664 EXPECT_EQ("testthe&codebase/file.signed",
1665 response.packages[0].payload_urls[0]);
1666 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001667}
1668
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001669TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001670 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001671 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001672 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001673
Amin Hassani41ac04b2019-03-29 11:31:03 -07001674 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001675 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001676}
1677
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001678TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001679 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001680 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001681 tuc_params_.http_response = "invalid xml>";
1682 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1683 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1684 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001685
Darin Petkov95508da2011-01-05 12:42:29 -08001686 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001687 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001688 // An existing but empty previous version means that we didn't reboot to a new
1689 // update, therefore, no need to update the previous version.
1690 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001691 ASSERT_FALSE(TestUpdateCheck());
1692
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001693 EXPECT_NE(
1694 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1695 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001696 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001697 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1698 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001699 // No <event> tag should be sent if we didn't reboot to an update.
1700 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001701}
1702
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001703TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001704 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001705 "invalid xml>");
1706
Alex Vakulenko75039d72014-03-25 12:36:28 -07001707 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001708 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001709 OmahaEvent::kTypeUpdateDownloadStarted,
1710 OmahaEvent::kResultSuccess);
1711 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001712 EXPECT_EQ(post_str.find("ping"), string::npos);
1713 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001714}
1715
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001716TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001717 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001718 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001719 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001720 "invalid xml>");
1721
Alex Vakulenko75039d72014-03-25 12:36:28 -07001722 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001723 " <event eventtype=\"%d\" eventresult=\"%d\" "
1724 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001725 OmahaEvent::kTypeDownloadComplete,
1726 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001727 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001728 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001729 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001730}
1731
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001732TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001733 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001734 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001735 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001736 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001737 std::make_unique<MockHttpFetcher>(
1738 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001739 false,
1740 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001741 EXPECT_FALSE(update_check_action.IsEvent());
1742
1743 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001744 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001745 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001746 std::make_unique<MockHttpFetcher>(
1747 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001748 false,
1749 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001750 EXPECT_TRUE(event_action.IsEvent());
1751}
1752
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001753TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001754 tuc_params_.http_response = "invalid xml>";
1755 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1756 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1757 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1758
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001759 for (int i = 0; i < 2; i++) {
1760 bool delta_okay = i == 1;
1761 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001762 request_params_.set_delta_okay(delta_okay);
1763
Amin Hassani41ac04b2019-03-29 11:31:03 -07001764 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001765 EXPECT_NE(
1766 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1767 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001768 << "i = " << i;
1769 }
1770}
1771
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001772TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001773 tuc_params_.http_response = "invalid xml>";
1774 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1775 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1776 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1777
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001778 for (int i = 0; i < 2; i++) {
1779 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001780 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001781 request_params_.set_interactive(interactive);
1782
Amin Hassani41ac04b2019-03-29 11:31:03 -07001783 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001784 EXPECT_NE(post_str.find(
1785 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001786 string::npos)
1787 << "i = " << i;
1788 }
1789}
1790
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001791TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001792 tuc_params_.http_response = "invalid xml>";
1793 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1794 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1795 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1796
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001797 for (int i = 0; i < 2; i++) {
1798 bool target_version_set = i == 1;
1799 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001800 request_params_.set_target_version_prefix(target_version_prefix);
1801
Amin Hassani41ac04b2019-03-29 11:31:03 -07001802 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001803 if (target_version_set) {
1804 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1805 string::npos)
1806 << "i = " << i;
1807 } else {
1808 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1809 << "i = " << i;
1810 }
1811 }
1812}
1813
1814TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001815 tuc_params_.http_response = "invalid xml>";
1816 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1817 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1818 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1819
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001820 for (int i = 0; i < 4; i++) {
1821 bool rollback_allowed = i / 2 == 0;
1822 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001823 request_params_.set_target_version_prefix(target_version_set ? "10032."
1824 : "");
1825 request_params_.set_rollback_allowed(rollback_allowed);
1826
Amin Hassani41ac04b2019-03-29 11:31:03 -07001827 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001828 if (rollback_allowed && target_version_set) {
1829 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1830 << "i = " << i;
1831 } else {
1832 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1833 }
1834 }
1835}
1836
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001837TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001838 OmahaEvent default_event;
1839 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1840 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001841 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001842
1843 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1844 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1845 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001846 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001847
1848 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1849 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001850 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001851 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1852 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001853 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001854}
1855
Askar Aitzhan570ca872019-04-24 11:16:12 +02001856TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
Askar Aitzhan18fff842019-06-21 23:24:37 +02001857 // If DeviceQuickFixBuildToken value is set it takes precedence over pref
1858 // value.
1859 constexpr char autoupdate_token[] = "autoupdate_token>";
1860 constexpr char xml_encoded_autoupdate_token[] = "autoupdate_token&gt;";
1861 constexpr char omaha_cohort_hint[] = "cohort_hint";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001862
1863 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1864 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1865 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1866 request_params_.set_autoupdate_token(autoupdate_token);
Askar Aitzhan18fff842019-06-21 23:24:37 +02001867 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan570ca872019-04-24 11:16:12 +02001868
1869 ASSERT_TRUE(TestUpdateCheck());
1870
Askar Aitzhan18fff842019-06-21 23:24:37 +02001871 EXPECT_NE(string::npos,
1872 post_str.find("cohorthint=\"" +
1873 string(xml_encoded_autoupdate_token) + "\""));
1874 EXPECT_EQ(string::npos, post_str.find(autoupdate_token));
1875 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
1876}
1877
1878TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsNotSetTest) {
1879 // If DeviceQuickFixBuildToken is not set, pref value will be provided in
1880 // cohorthint attribute.
1881 constexpr char omaha_cohort_hint[] = "evil_string>";
1882 constexpr char xml_encoded_cohort_hint[] = "evil_string&gt;";
1883
1884 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1885 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1886 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1887 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
1888
1889 ASSERT_TRUE(TestUpdateCheck());
1890
1891 EXPECT_NE(
1892 string::npos,
1893 post_str.find("cohorthint=\"" + string(xml_encoded_cohort_hint) + "\""));
1894 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001895}
1896
Amin Hassani37b67232020-08-13 09:29:48 -07001897TEST_F(OmahaRequestActionTest, TargetChannelHintTest) {
1898 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1899 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1900 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1901 request_params_.set_lts_tag("hint>");
1902
1903 ASSERT_TRUE(TestUpdateCheck());
1904
1905 EXPECT_NE(string::npos, post_str.find("ltstag=\"hint&gt;\""));
1906}
1907
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001908void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001909 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001910 fake_system_state_.set_prefs(&prefs);
1911 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001912 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001913 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1914 // Add a few hours to the day difference to test no rounding, etc.
1915 int64_t five_days_ago =
1916 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1917 int64_t six_days_ago =
1918 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1919 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001920 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001921 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001922 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001923 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001924 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001925
1926 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1927 tuc_params_.ping_only = ping_only;
1928 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1929 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1930
1931 ASSERT_TRUE(TestUpdateCheck());
1932
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001933 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1934 string::npos);
1935 if (ping_only) {
1936 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1937 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1938 } else {
1939 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1940 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001941 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001942}
1943
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001944TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001945 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001946}
1947
1948TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001949 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001950}
1951
1952TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001953 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001954 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001955 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001956 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001957 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001958 int64_t three_days_ago =
1959 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1960 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001961 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001962 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001963 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001964 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001965 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001966 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001967
1968 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1969 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1970 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1971
1972 ASSERT_TRUE(TestUpdateCheck());
1973
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001974 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001975}
1976
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001977TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001978 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001979 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001980 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001981 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001982 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001983 int64_t four_days_ago =
1984 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1985 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001986 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001987 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001988 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001989 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001990 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001991 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001992
1993 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1994 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1995 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1996
1997 ASSERT_TRUE(TestUpdateCheck());
1998
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001999 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07002000 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002001}
2002
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002003TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002004 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002005 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002006 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002007 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002008 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002009 int64_t one_hour_ago =
2010 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002011 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002012 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002013 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002014 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002015 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002016 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002017 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
2018 // send a ping.
2019 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2020 .WillOnce(Return(true));
2021 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2022 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002023
2024 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2025 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2026 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2027
2028 ASSERT_TRUE(TestUpdateCheck());
2029
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002030 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002031}
2032
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002033TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07002034 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08002035 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002036 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07002037 int64_t now = Time::Now().ToInternalValue();
2038 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002039 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002040 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002041 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002042 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2043 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002044
2045 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2046 tuc_params_.ping_only = true;
2047 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
2048 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2049
2050 EXPECT_TRUE(TestUpdateCheck());
2051 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07002052}
2053
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002054TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002055 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002056 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002057 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002058 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002059 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002060 int64_t future =
2061 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002062 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002063 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002064 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002065 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002066 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002067 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002068 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2069 .WillOnce(Return(true));
2070 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2071 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002072
2073 tuc_params_.http_response =
2074 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2075 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
2076 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2077 "<updatecheck status=\"noupdate\"/></app></response>";
2078 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2079 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2080
2081 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002082 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002083}
2084
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002085TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002086 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07002087 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002088 // may fail if it runs for longer than 5 seconds. It shouldn't run
2089 // that long though.
2090 int64_t midnight =
2091 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
2092 int64_t midnight_slack =
2093 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08002094 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002095 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002096 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2097 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002098 EXPECT_CALL(prefs,
2099 SetInt64(kPrefsLastActivePingDay,
2100 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002101 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002102 EXPECT_CALL(prefs,
2103 SetInt64(kPrefsLastRollCallPingDay,
2104 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002105 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002106
2107 tuc_params_.http_response =
2108 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2109 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
2110 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2111 "<updatecheck status=\"noupdate\"/></app></response>";
2112 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2113 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2114
2115 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002116}
2117
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002118TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002119 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002120 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002121 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2122 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002123 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2124 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002125
2126 tuc_params_.http_response =
2127 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2128 "protocol=\"3.0\"><daystart blah=\"200\"/>"
2129 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2130 "<updatecheck status=\"noupdate\"/></app></response>";
2131 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2132 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2133
2134 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002135}
2136
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002137TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002138 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002139 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002140 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2141 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002142 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2143 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002144
2145 tuc_params_.http_response =
2146 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2147 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
2148 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2149 "<updatecheck status=\"noupdate\"/></app></response>";
2150 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2151 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2152
2153 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002154}
2155
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002156TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07002157 tuc_params_.http_response = "invalid xml>";
2158 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2159 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2160 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2161
2162 ASSERT_FALSE(TestUpdateCheck());
2163
Darin Petkov84c763c2010-07-29 16:27:58 -07002164 EXPECT_EQ(post_str.find("machineid="), string::npos);
2165 EXPECT_EQ(post_str.find("userid="), string::npos);
2166}
2167
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002168TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002169 const int http_error_code =
2170 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002171 tuc_params_.fail_http_response_code = 501;
2172 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2173 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2174 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2175 tuc_params_.expected_download_error_code =
2176 static_cast<metrics::DownloadErrorCode>(501);
2177
2178 ASSERT_FALSE(TestUpdateCheck());
2179
Darin Petkovedc522e2010-11-05 09:35:17 -07002180 EXPECT_FALSE(response.update_exists);
2181}
2182
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002183TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002184 const int http_error_code =
2185 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002186
2187 tuc_params_.fail_http_response_code = 1500;
2188 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2189 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2190 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2191 tuc_params_.expected_download_error_code =
2192 metrics::DownloadErrorCode::kHttpStatusOther;
2193
2194 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002195 EXPECT_FALSE(response.update_exists);
2196}
2197
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002198TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002199 request_params_.set_wall_clock_based_wait_enabled(true);
2200 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2201 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002202
Sen Jiang7c1171e2016-06-23 11:35:40 -07002203 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002204 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002205 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002206
2207 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2208 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2209 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2210
2211 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002212
Ben Chan9abb7632014-08-07 00:10:53 -07002213 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002214 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002215 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002216 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002217
2218 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002219 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002220 tuc_params_.expected_code = ErrorCode::kSuccess;
2221 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2222
2223 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002224 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002225}
2226
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002227TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002228 request_params_.set_wall_clock_based_wait_enabled(true);
2229 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2230 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002231
Sen Jiang7c1171e2016-06-23 11:35:40 -07002232 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002233 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2234 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002235 ASSERT_TRUE(
2236 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2237 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002238
Amin Hassani41ac04b2019-03-29 11:31:03 -07002239 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2240
2241 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002242 EXPECT_TRUE(response.update_exists);
2243
2244 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002245 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002246 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002247 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2248}
2249
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002250TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002251 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002252 base::ScopedTempDir tempdir;
2253 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002254
Marton Hunyady2abda312018-04-24 18:21:49 +02002255 request_params_.set_root(tempdir.GetPath().value());
2256 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2257 request_params_.set_app_version("1.2.3.4");
2258 request_params_.set_product_components("o.bundle=1");
2259 request_params_.set_current_channel("canary-channel");
2260 EXPECT_TRUE(
2261 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2262 request_params_.UpdateDownloadChannel();
2263 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002264
2265 tuc_params_.http_response = "invalid xml>";
2266 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2267 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2268 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2269
2270 ASSERT_FALSE(TestUpdateCheck());
2271
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002272 EXPECT_NE(
2273 string::npos,
2274 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2275 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2276 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002277 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002278}
2279
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002280TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002281 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002282 base::ScopedTempDir tempdir;
2283 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002284
Marton Hunyady2abda312018-04-24 18:21:49 +02002285 request_params_.set_root(tempdir.GetPath().value());
2286 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2287 request_params_.set_app_version("5.6.7.8");
2288 request_params_.set_product_components("o.bundle=1");
2289 request_params_.set_current_channel("stable-channel");
2290 EXPECT_TRUE(
2291 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2292 request_params_.UpdateDownloadChannel();
2293 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002294
2295 tuc_params_.http_response = "invalid xml>";
2296 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2297 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2298 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2299
2300 ASSERT_FALSE(TestUpdateCheck());
2301
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002302 EXPECT_NE(
2303 string::npos,
2304 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2305 "version=\"5.6.7.8\" "
2306 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002307 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002308 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002309}
2310
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002311// Checks that the initial ping with a=-1 r=-1 is not send when the device
2312// was powerwashed.
2313TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2314 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2315
2316 // Flag that the device was powerwashed in the past.
2317 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002318 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2319 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2320 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002321
Amin Hassani41ac04b2019-03-29 11:31:03 -07002322 ASSERT_TRUE(TestUpdateCheck());
2323
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002324 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002325 EXPECT_EQ(string::npos, post_str.find("<ping"));
2326}
2327
Amin Hassani1677e812017-06-21 13:36:36 -07002328// Checks that the initial ping with a=-1 r=-1 is not send when the device
2329// first_active_omaha_ping_sent is set.
2330TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2331 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2332
2333 // Flag that the device was not powerwashed in the past.
2334 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2335
2336 // Flag that the device has sent first active ping in the past.
2337 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2338
Amin Hassani41ac04b2019-03-29 11:31:03 -07002339 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2340 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2341 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2342
2343 ASSERT_TRUE(TestUpdateCheck());
2344
Amin Hassani1677e812017-06-21 13:36:36 -07002345 // We shouldn't send a ping in this case since
2346 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002347 EXPECT_EQ(string::npos, post_str.find("<ping"));
2348}
2349
Alex Deymo9fded1e2015-11-05 12:31:19 -08002350// Checks that the event 54 is sent on a reboot to a new update.
2351TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2352 // Flag that the device was updated in a previous boot.
2353 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2354
Amin Hassani41ac04b2019-03-29 11:31:03 -07002355 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2356 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2357 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2358
2359 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002360
2361 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002362 EXPECT_NE(
2363 string::npos,
2364 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2365 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002366 EXPECT_NE(string::npos,
2367 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2368
2369 // The previous version flag should have been removed.
2370 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2371 string prev_version;
2372 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2373 EXPECT_TRUE(prev_version.empty());
2374}
2375
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002376void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2377 bool initial_allow_p2p_for_sharing,
2378 bool omaha_disable_p2p_for_downloading,
2379 bool omaha_disable_p2p_for_sharing,
2380 bool payload_state_allow_p2p_attempt,
2381 bool expect_p2p_client_lookup,
2382 const string& p2p_client_result_url,
2383 bool expected_allow_p2p_for_downloading,
2384 bool expected_allow_p2p_for_sharing,
2385 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002386 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2387 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2388 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002389
2390 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002391 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002392 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2393 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002394 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2395 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2396 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2397 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2398 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2399 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2400 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2401 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2402 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2403 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2404
David Zeuthen8f191b22013-08-06 12:27:50 -07002405 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002406 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002407 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2408
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002409 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2410 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002411 .Times(expect_p2p_client_lookup ? 1 : 0);
2412
Alex Deymo8e18f932015-03-27 16:16:59 -07002413 fake_update_response_.disable_p2p_for_downloading =
2414 omaha_disable_p2p_for_downloading;
2415 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002416
2417 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2418 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2419
2420 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002421 EXPECT_TRUE(response.update_exists);
2422
Gilad Arnold74b5f552014-10-07 08:17:16 -07002423 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2424 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002425 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002426
Gilad Arnold74b5f552014-10-07 08:17:16 -07002427 EXPECT_EQ(expected_allow_p2p_for_downloading,
2428 actual_allow_p2p_for_downloading);
2429 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2430 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002431}
2432
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002433TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002434 P2PTest(true, // initial_allow_p2p_for_downloading
2435 true, // initial_allow_p2p_for_sharing
2436 false, // omaha_disable_p2p_for_downloading
2437 false, // omaha_disable_p2p_for_sharing
2438 true, // payload_state_allow_p2p_attempt
2439 true, // expect_p2p_client_lookup
2440 "http://1.3.5.7/p2p", // p2p_client_result_url
2441 true, // expected_allow_p2p_for_downloading
2442 true, // expected_allow_p2p_for_sharing
2443 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002444}
2445
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002446TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002447 P2PTest(true, // initial_allow_p2p_for_downloading
2448 true, // initial_allow_p2p_for_sharing
2449 false, // omaha_disable_p2p_for_downloading
2450 false, // omaha_disable_p2p_for_sharing
2451 true, // payload_state_allow_p2p_attempt
2452 true, // expect_p2p_client_lookup
2453 "", // p2p_client_result_url
2454 false, // expected_allow_p2p_for_downloading
2455 true, // expected_allow_p2p_for_sharing
2456 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002457}
2458
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002459TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002460 P2PTest(false, // initial_allow_p2p_for_downloading
2461 true, // initial_allow_p2p_for_sharing
2462 false, // omaha_disable_p2p_for_downloading
2463 false, // omaha_disable_p2p_for_sharing
2464 true, // payload_state_allow_p2p_attempt
2465 false, // expect_p2p_client_lookup
2466 "unset", // p2p_client_result_url
2467 false, // expected_allow_p2p_for_downloading
2468 true, // expected_allow_p2p_for_sharing
2469 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002470}
2471
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002472TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002473 P2PTest(true, // initial_allow_p2p_for_downloading
2474 true, // initial_allow_p2p_for_sharing
2475 true, // omaha_disable_p2p_for_downloading
2476 false, // omaha_disable_p2p_for_sharing
2477 true, // payload_state_allow_p2p_attempt
2478 false, // expect_p2p_client_lookup
2479 "unset", // p2p_client_result_url
2480 false, // expected_allow_p2p_for_downloading
2481 true, // expected_allow_p2p_for_sharing
2482 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002483}
2484
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002485TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002486 P2PTest(true, // initial_allow_p2p_for_downloading
2487 true, // initial_allow_p2p_for_sharing
2488 false, // omaha_disable_p2p_for_downloading
2489 true, // omaha_disable_p2p_for_sharing
2490 true, // payload_state_allow_p2p_attempt
2491 true, // expect_p2p_client_lookup
2492 "http://1.3.5.7/p2p", // p2p_client_result_url
2493 true, // expected_allow_p2p_for_downloading
2494 false, // expected_allow_p2p_for_sharing
2495 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002496}
2497
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002498TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002499 P2PTest(true, // initial_allow_p2p_for_downloading
2500 true, // initial_allow_p2p_for_sharing
2501 true, // omaha_disable_p2p_for_downloading
2502 true, // omaha_disable_p2p_for_sharing
2503 true, // payload_state_allow_p2p_attempt
2504 false, // expect_p2p_client_lookup
2505 "unset", // p2p_client_result_url
2506 false, // expected_allow_p2p_for_downloading
2507 false, // expected_allow_p2p_for_sharing
2508 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002509}
2510
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002511bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2512 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002513 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002514 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2515
2516 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002517}
2518
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002519TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002520 // Simulate a successful update check that happens during OOBE. The
2521 // deadline in the response is needed to force the update attempt to
2522 // occur; responses without a deadline seen during OOBE will normally
2523 // return ErrorCode::kNonCriticalUpdateInOOBE.
2524 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2525 fake_update_response_.deadline = "20101020";
2526
David Zeuthen639aa362014-02-03 16:23:44 -08002527 // Check that we parse elapsed_days in the Omaha Response correctly.
2528 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002529 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2530 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002531 EXPECT_TRUE(response.update_exists);
2532 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002533 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002534 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002535 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002536 EXPECT_EQ(prefs_days, 42);
2537
2538 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002539 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002540 EXPECT_TRUE(response.update_exists);
2541 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002542 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002543 EXPECT_EQ(prefs_days, 42);
2544
2545 // Note that elapsed_days is not necessarily divisible by 7 so check
2546 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002547 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2548 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002549 EXPECT_TRUE(response.update_exists);
2550 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002551 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002552 EXPECT_EQ(prefs_days, 21);
2553
2554 // Check that we correctly handle elapsed_days not being included in
2555 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002556 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002557 EXPECT_TRUE(response.update_exists);
2558 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002559}
2560
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002561// If there is no prefs and OOBE is not complete, we should not
2562// report anything to Omaha.
2563TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002564 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002565 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2566 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2567}
David Zeuthen639aa362014-02-03 16:23:44 -08002568
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002569// If OOBE is complete and happened on a valid date (e.g. after Jan
2570// 1 2007 0:00 PST), that date should be used and written to
2571// prefs. However, first try with an invalid date and check we do
2572// nothing.
2573TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2574 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2575 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2576 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2577 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2578}
David Zeuthen639aa362014-02-03 16:23:44 -08002579
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002580// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2581// should yield an InstallDate of 14.
2582TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2583 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2584 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2585 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2586 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002587
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002588 int64_t prefs_days;
2589 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2590 EXPECT_EQ(prefs_days, 14);
2591}
David Zeuthen639aa362014-02-03 16:23:44 -08002592
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002593// Now that we have a valid date in prefs, check that we keep using
2594// that even if OOBE date reports something else. The date Jan 30,
2595// 2007 0:00 PST should yield an InstallDate of 28... but since
2596// there's a prefs file, we should still get 14.
2597TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2598 // Set a valid date in the prefs first.
2599 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002600
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002601 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2602 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2603 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002604
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002605 int64_t prefs_days;
2606 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2607 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002608
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002609 // If we delete the prefs file, we should get 28 days.
2610 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2611 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2612 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2613 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002614}
2615
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002616// Verifies that a device with no device policy, and is not a consumer
2617// device sets the max kernel key version to the current version.
2618// ie. the same behavior as if rollback is enabled.
2619TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2620 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2621
2622 // Setup and verify some initial default values for the kernel TPM
2623 // values that control verified boot and rollback.
2624 const int min_kernel_version = 4;
2625 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2626 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2627 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2628 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2629
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002630 EXPECT_CALL(
2631 *fake_system_state_.mock_metrics_reporter(),
2632 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2633 .Times(1);
2634
Amin Hassani41ac04b2019-03-29 11:31:03 -07002635 fake_update_response_.deadline = "20101020";
2636 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2637 tuc_params_.is_consumer_device = false;
2638 tuc_params_.rollback_allowed_milestones = 3;
2639
2640 EXPECT_TRUE(TestUpdateCheck());
2641 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002642
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002643 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002644 // kernel key version. This has the effect of freezing roll
2645 // forwards indefinitely. This will hold the rollback window
2646 // open until a future change will be able to move this forward
2647 // relative the configured window.
2648 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2649 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2650}
2651
2652// Verifies that a conmsumer device with no device policy sets the
2653// max kernel key version to the current version. ie. the same
2654// behavior as if rollback is enabled.
2655TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2656 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2657
2658 // Setup and verify some initial default values for the kernel TPM
2659 // values that control verified boot and rollback.
2660 const int min_kernel_version = 3;
2661 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2662 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2663 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2664 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2665
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002666 EXPECT_CALL(
2667 *fake_system_state_.mock_metrics_reporter(),
2668 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2669 .Times(1);
2670
Amin Hassani41ac04b2019-03-29 11:31:03 -07002671 fake_update_response_.deadline = "20101020";
2672 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2673 tuc_params_.is_consumer_device = true;
2674 tuc_params_.rollback_allowed_milestones = 3;
2675
2676 EXPECT_TRUE(TestUpdateCheck());
2677 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002678
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002679 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002680 // was set to logical infinity. This is the expected behavior for
2681 // consumer devices and matches the existing behavior prior to the
2682 // rollback features.
2683 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2684 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2685}
2686
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002687// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002688// in the TPM to prevent roll forward.
2689TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2690 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2691
2692 // Setup and verify some initial default values for the kernel TPM
2693 // values that control verified boot and rollback.
2694 const int allowed_milestones = 4;
2695 const int min_kernel_version = 3;
2696 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2697 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2698 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2699 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2700
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002701 EXPECT_CALL(
2702 *fake_system_state_.mock_metrics_reporter(),
2703 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2704 .Times(1);
2705
Amin Hassani41ac04b2019-03-29 11:31:03 -07002706 fake_update_response_.deadline = "20101020";
2707 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2708 tuc_params_.is_consumer_device = false;
2709 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2710 tuc_params_.is_policy_loaded = true;
2711
2712 EXPECT_TRUE(TestUpdateCheck());
2713 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002714
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002715 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002716 // was set to the current minimum kernel key version. This has
2717 // the effect of freezing roll forwards indefinitely. This will
2718 // hold the rollback window open until a future change will
2719 // be able to move this forward relative the configured window.
2720 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2721 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2722}
2723
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002724// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002725// in the TPM to logical infinity, to allow roll forward.
2726TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2727 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2728
2729 // Setup and verify some initial default values for the kernel TPM
2730 // values that control verified boot and rollback.
2731 const int allowed_milestones = 0;
2732 const int min_kernel_version = 3;
2733 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2734 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2735 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2736 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2737
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002738 EXPECT_CALL(
2739 *fake_system_state_.mock_metrics_reporter(),
2740 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2741 .Times(1);
2742
Amin Hassani41ac04b2019-03-29 11:31:03 -07002743 fake_update_response_.deadline = "20101020";
2744 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2745 tuc_params_.is_consumer_device = false;
2746 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2747 tuc_params_.is_policy_loaded = true;
2748
2749 EXPECT_TRUE(TestUpdateCheck());
2750 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002751
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002752 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002753 // was set to logical infinity.
2754 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2755 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2756}
2757
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002758TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002759 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002760 fake_update_response_.deadline = "20101020";
2761 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2762 tuc_params_.is_consumer_device = false;
2763 tuc_params_.rollback_allowed_milestones = 4;
2764 tuc_params_.is_policy_loaded = true;
2765
2766 EXPECT_TRUE(TestUpdateCheck());
2767 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002768 EXPECT_TRUE(response.is_rollback);
2769}
2770
2771TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002772 fake_update_response_.rollback_firmware_version = "1.2";
2773 fake_update_response_.rollback_kernel_version = "3.4";
2774 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002775 fake_update_response_.deadline = "20101020";
2776 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2777 tuc_params_.is_consumer_device = false;
2778 tuc_params_.rollback_allowed_milestones = 4;
2779 tuc_params_.is_policy_loaded = true;
2780
2781 EXPECT_TRUE(TestUpdateCheck());
2782 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002783 EXPECT_TRUE(response.is_rollback);
2784 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2785 EXPECT_EQ(2, response.rollback_key_version.firmware);
2786 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2787 EXPECT_EQ(4, response.rollback_key_version.kernel);
2788}
2789
May Lippert60aa3ca2018-08-15 16:55:29 -07002790TEST_F(OmahaRequestActionTest,
2791 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2792 FakeClock fake_clock;
2793 Time now = Time::Now();
2794 fake_clock.SetWallclockTime(now);
2795 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002796 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002797
Amin Hassani41ac04b2019-03-29 11:31:03 -07002798 ASSERT_TRUE(TestUpdateCheck());
2799
May Lippert60aa3ca2018-08-15 16:55:29 -07002800 EXPECT_TRUE(response.update_exists);
2801 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2802
2803 int64_t stored_first_seen_at_time;
2804 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2805 &stored_first_seen_at_time));
2806 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2807}
2808
2809TEST_F(OmahaRequestActionTest,
2810 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2811 FakeClock fake_clock;
2812 Time now = Time::Now();
2813 fake_clock.SetWallclockTime(now);
2814 fake_system_state_.set_clock(&fake_clock);
2815
Amin Hassani41ac04b2019-03-29 11:31:03 -07002816 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2817 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2818 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2819
2820 ASSERT_TRUE(TestUpdateCheck());
2821
May Lippert60aa3ca2018-08-15 16:55:29 -07002822 EXPECT_FALSE(response.update_exists);
2823 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2824}
2825
Xiaochu Liu88d90382018-08-29 16:09:11 -07002826TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002827 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002828 request_params_.set_dlc_apps_params(
2829 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2830 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002831 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2832
2833 ASSERT_TRUE(TestUpdateCheck());
2834
Andrewe045aef2020-01-08 16:29:22 -08002835 for (const auto& it : request_params_.dlc_apps_params()) {
2836 EXPECT_NE(string::npos, post_str.find("appid=\"" + it.first + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002837 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002838 EXPECT_NE(string::npos,
2839 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2840
2841 // Count number of updatecheck tag in response.
2842 int updatecheck_count = 0;
2843 size_t pos = 0;
2844 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2845 updatecheck_count++;
2846 pos++;
2847 }
Andrewe045aef2020-01-08 16:29:22 -08002848 EXPECT_EQ(request_params_.dlc_apps_params().size(), updatecheck_count);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002849 EXPECT_TRUE(response.update_exists);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002850}
2851
2852TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2853 fake_update_response_.multi_app_skip_updatecheck = true;
2854 fake_update_response_.multi_app_no_update = false;
2855 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002856 request_params_.set_dlc_apps_params(
2857 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2858 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002859 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002860 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2861
2862 ASSERT_TRUE(TestUpdateCheck());
2863
Xiaochu Liu6310be62018-10-11 15:09:03 -07002864 EXPECT_TRUE(response.update_exists);
2865 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002866}
2867
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002868TEST_F(OmahaRequestActionTest, UpdateWithDlcTest) {
2869 request_params_.set_dlc_apps_params(
2870 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
2871 fake_update_response_.dlc_app_update = true;
2872 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002873 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002874 ASSERT_TRUE(TestUpdateCheck());
2875
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002876 EXPECT_EQ(response.packages.size(), 2u);
2877 // Two candidate URLs.
2878 EXPECT_EQ(response.packages[1].payload_urls.size(), 2u);
2879 EXPECT_TRUE(response.update_exists);
2880}
2881
2882TEST_F(OmahaRequestActionTest, UpdateWithPartiallyExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002883 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2884 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002885 fake_update_response_.dlc_app_update = true;
2886 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2887 // The first DLC candidate URL is excluded.
2888 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2889 .WillOnce(Return(true))
2890 .WillOnce(Return(false));
2891 ASSERT_TRUE(TestUpdateCheck());
2892
2893 EXPECT_EQ(response.packages.size(), 2u);
2894 // One candidate URL.
2895 EXPECT_EQ(response.packages[1].payload_urls.size(), 1u);
2896 EXPECT_TRUE(response.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002897 EXPECT_TRUE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002898}
2899
2900TEST_F(OmahaRequestActionTest, UpdateWithExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002901 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2902 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002903 fake_update_response_.dlc_app_update = true;
2904 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2905 // Both DLC candidate URLs are excluded.
2906 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2907 .WillOnce(Return(true))
2908 .WillOnce(Return(true));
2909 ASSERT_TRUE(TestUpdateCheck());
2910
2911 EXPECT_EQ(response.packages.size(), 1u);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002912 EXPECT_TRUE(response.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002913 EXPECT_FALSE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002914}
2915
2916TEST_F(OmahaRequestActionTest, UpdateWithDeprecatedDlcTest) {
2917 request_params_.set_dlc_apps_params(
2918 {{request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2919 fake_update_response_.dlc_app_no_update = true;
2920 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002921 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002922 ASSERT_TRUE(TestUpdateCheck());
2923
2924 EXPECT_TRUE(response.update_exists);
2925}
2926
2927TEST_F(OmahaRequestActionTest, UpdateWithDlcAndDeprecatedDlcTest) {
2928 request_params_.set_dlc_apps_params(
2929 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
2930 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2931 fake_update_response_.dlc_app_update = true;
2932 fake_update_response_.dlc_app_no_update = true;
2933 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002934 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002935 ASSERT_TRUE(TestUpdateCheck());
2936
2937 EXPECT_TRUE(response.update_exists);
2938}
2939
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002940TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002941 fake_update_response_.rollback = true;
2942 fake_update_response_.rollback_allowed_milestones = 4;
2943 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002944 fake_update_response_.deadline = "20101020";
2945 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2946 tuc_params_.is_consumer_device = false;
2947 tuc_params_.rollback_allowed_milestones = 4;
2948 tuc_params_.is_policy_loaded = true;
2949
2950 EXPECT_TRUE(TestUpdateCheck());
2951 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002952 EXPECT_TRUE(response.is_rollback);
2953 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2954 response.past_rollback_key_version.firmware_key);
2955 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2956 response.past_rollback_key_version.firmware);
2957 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2958 response.past_rollback_key_version.kernel_key);
2959 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2960 response.past_rollback_key_version.kernel);
2961}
2962
2963TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002964 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002965 fake_update_response_.rollback = true;
2966 fake_update_response_.rollback_allowed_milestones = 4;
2967 fake_update_response_.rollback_firmware_version = "4.3";
2968 fake_update_response_.rollback_kernel_version = "2.1";
2969 fake_update_response_.past_rollback_key_version =
2970 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002971 fake_update_response_.deadline = "20101020";
2972 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2973 tuc_params_.is_consumer_device = false;
2974 tuc_params_.rollback_allowed_milestones = 4;
2975 tuc_params_.is_policy_loaded = true;
2976
2977 EXPECT_TRUE(TestUpdateCheck());
2978 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002979 EXPECT_TRUE(response.is_rollback);
2980 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2981 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2982 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2983 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
2984}
2985
2986TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002987 fake_update_response_.rollback = true;
2988 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002989 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002990 request_params_.set_rollback_allowed_milestones(4);
2991
2992 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
2993 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
2994 // to 2, OmahaRequestAction will look for the key values of N-4 version but
2995 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07002996 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2997 tuc_params_.is_consumer_device = false;
2998 tuc_params_.rollback_allowed_milestones = 2;
2999 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003000
Amin Hassani41ac04b2019-03-29 11:31:03 -07003001 EXPECT_TRUE(TestUpdateCheck());
3002 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003003 EXPECT_TRUE(response.is_rollback);
3004 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3005 response.past_rollback_key_version.firmware_key);
3006 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3007 response.past_rollback_key_version.firmware);
3008 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3009 response.past_rollback_key_version.kernel_key);
3010 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3011 response.past_rollback_key_version.kernel);
3012}
3013
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003014TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
3015 request_params_.set_device_requisition("remora");
3016 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3017 ASSERT_TRUE(TestUpdateCheck());
3018 EXPECT_NE(string::npos, post_str.find("requisition=\"remora\""));
3019}
3020
3021TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
3022 request_params_.set_device_requisition("");
3023 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3024 ASSERT_TRUE(TestUpdateCheck());
3025 EXPECT_EQ(string::npos, post_str.find("requisition"));
3026}
3027
Amin Hassani2cbb0692019-10-30 13:36:17 -07003028TEST_F(OmahaRequestActionTest, PersistEolDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003029 tuc_params_.http_response =
3030 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3031 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3032 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003033 "_eol_date=\"200\" _foo=\"bar\"/></app></response>";
3034 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3035 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3036
3037 ASSERT_TRUE(TestUpdateCheck());
3038
3039 string eol_date;
3040 EXPECT_TRUE(
3041 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
3042 EXPECT_EQ("200", eol_date);
3043}
3044
3045TEST_F(OmahaRequestActionTest, PersistEolMissingDateTest) {
3046 tuc_params_.http_response =
3047 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3048 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3049 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003050 "_foo=\"bar\"/></app></response>";
3051 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3052 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3053
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003054 const string kDate = "123";
3055 fake_system_state_.prefs()->SetString(kPrefsOmahaEolDate, kDate);
3056
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003057 ASSERT_TRUE(TestUpdateCheck());
3058
Amin Hassani2cbb0692019-10-30 13:36:17 -07003059 string eol_date;
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003060 EXPECT_TRUE(
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003061 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003062 EXPECT_EQ(kDate, eol_date);
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003063}
3064
Amin Hassani2cbb0692019-10-30 13:36:17 -07003065TEST_F(OmahaRequestActionTest, PersistEolBadDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003066 tuc_params_.http_response =
3067 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3068 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3069 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003070 "_eol_date=\"bad\" foo=\"bar\"/></app></response>";
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003071 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3072 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3073
3074 ASSERT_TRUE(TestUpdateCheck());
3075
Amin Hassani2cbb0692019-10-30 13:36:17 -07003076 string eol_date;
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003077 EXPECT_TRUE(
3078 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
3079 EXPECT_EQ(kEolDateInvalid, StringToEolDate(eol_date));
3080}
3081
Andrew065d78d2020-04-07 15:43:07 -07003082TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyNoPing) {
3083 OmahaRequestParams::AppParams app_param = {.name = dlc_id_};
Andrewe045aef2020-01-08 16:29:22 -08003084 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003085 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003086
3087 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003088
3089 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003090 // If there was no ping, the metadata files shouldn't exist yet.
Andrew065d78d2020-04-07 15:43:07 -07003091 EXPECT_FALSE(fake_prefs_.GetInt64(active_key_, &temp_int));
3092 EXPECT_FALSE(fake_prefs_.GetInt64(last_active_key_, &temp_int));
3093 EXPECT_FALSE(fake_prefs_.GetInt64(last_rollcall_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003094}
3095
Andrew065d78d2020-04-07 15:43:07 -07003096TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyActiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003097 // Create Active value
Andrew065d78d2020-04-07 15:43:07 -07003098 fake_prefs_.SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003099
3100 OmahaRequestParams::AppParams app_param = {
3101 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003102 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003103 .ping_active = 1,
3104 .send_ping = true};
3105 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003106 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003107
Andrew065d78d2020-04-07 15:43:07 -07003108 int64_t temp_int;
3109 string temp_str;
Andrewe045aef2020-01-08 16:29:22 -08003110 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003111 EXPECT_TRUE(fake_prefs_.GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003112 EXPECT_EQ(temp_int, kPingInactiveValue);
Andrew065d78d2020-04-07 15:43:07 -07003113 EXPECT_TRUE(fake_prefs_.GetString(last_active_key_, &temp_str));
3114 EXPECT_EQ(temp_str, "4763");
3115 EXPECT_TRUE(fake_prefs_.GetString(last_rollcall_key_, &temp_str));
3116 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003117}
3118
Andrew065d78d2020-04-07 15:43:07 -07003119TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyInactiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003120 // Create Active value
Andrew065d78d2020-04-07 15:43:07 -07003121 fake_prefs_.SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003122
3123 OmahaRequestParams::AppParams app_param = {
3124 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003125 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003126 .ping_active = 0,
3127 .send_ping = true};
3128 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003129 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003130
3131 // Set the previous active value to an older value than 4763.
Andrew065d78d2020-04-07 15:43:07 -07003132 fake_prefs_.SetString(last_active_key_, "555");
Andrewe045aef2020-01-08 16:29:22 -08003133
Andrew065d78d2020-04-07 15:43:07 -07003134 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003135 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003136 EXPECT_TRUE(fake_prefs_.GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003137 EXPECT_EQ(temp_int, kPingInactiveValue);
Andrew065d78d2020-04-07 15:43:07 -07003138 string temp_str;
3139 EXPECT_TRUE(fake_prefs_.GetString(last_active_key_, &temp_str));
3140 EXPECT_EQ(temp_str, "555");
3141 EXPECT_TRUE(fake_prefs_.GetString(last_rollcall_key_, &temp_str));
3142 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003143}
3144
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07003145TEST_F(OmahaRequestActionTest, OmahaResponseUpdateCanExcludeCheck) {
3146 request_params_.set_dlc_apps_params(
3147 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3148 fake_update_response_.dlc_app_update = true;
3149 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3150
3151 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3152 ASSERT_TRUE(TestUpdateCheck());
3153 ASSERT_TRUE(delegate_.omaha_response_);
3154 const auto& packages = delegate_.omaha_response_->packages;
3155 ASSERT_EQ(packages.size(), 2);
3156
3157 EXPECT_FALSE(packages[0].can_exclude);
3158 EXPECT_TRUE(packages[1].can_exclude);
3159}
3160
3161TEST_F(OmahaRequestActionTest, OmahaResponseInstallCannotExcludeCheck) {
3162 request_params_.set_is_install(true);
3163 request_params_.set_dlc_apps_params(
3164 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3165 fake_update_response_.dlc_app_update = true;
3166 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3167
3168 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3169 ASSERT_TRUE(TestUpdateCheck());
3170 ASSERT_TRUE(delegate_.omaha_response_);
3171 const auto& packages = delegate_.omaha_response_->packages;
3172 ASSERT_EQ(packages.size(), 2);
3173
3174 EXPECT_FALSE(packages[0].can_exclude);
3175 EXPECT_FALSE(packages[1].can_exclude);
3176}
3177
Darin Petkov6a5b3222010-07-13 14:55:28 -07003178} // namespace chromeos_update_engine