blob: 9f9c75f4162163e00fbc7c84ae3ecff2d61900d4 [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=\"" +
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800161 filename + "\" size=\"" + base::NumberToString(size) + "\" fp=\"" +
162 fp + "\" hash_sha256=\"" + hash + "\"/>" +
163 (multi_package ? "<package name=\"package2\" size=\"222\" fp=\"" +
164 fp2 + "\" hash_sha256=\"hash2\"/>"
Sen Jiang0affc2c2017-02-10 15:55:05 -0800165 : "") +
166 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700167 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700168 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
169 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700170 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700171 "IsDeltaPayload=\"true" +
172 (multi_package ? ":false" : "") +
173 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700174 "MaxDaysToScatter=\"" +
175 max_days_to_scatter +
176 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700177 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700178 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
179 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
180 : "") +
181 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700182 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700183 "/></actions></manifest></updatecheck></app>" +
184 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700185 ? "<app appid=\"" + app_id2 + "\"" +
186 (include_cohorts ? " cohort=\"cohort2\"" : "") +
187 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700188 codebase2 + "\"/></urls><manifest version=\"" + version2 +
189 "\"><packages>"
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800190 "<package name=\"package3\" size=\"333\" fp=\"" +
191 fp2 +
192 "\" hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700193 "<actions><action event=\"postinstall\" " +
194 (multi_app_self_update
195 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
196 : "IsDeltaPayload=\"false\" ") +
197 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700198 "</manifest></updatecheck></app>"
199 : "") +
200 (multi_app_no_update
201 ? "<app><updatecheck status=\"noupdate\"/></app>"
202 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700203 (multi_app_skip_updatecheck
204 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
205 : "") +
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800206 (dlc_app_update
207 ? "<app appid=\"" + request_params.GetDlcAppId(kDlcId1) +
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800208 "\" " +
209 (include_dlc_cohorts
210 ? "cohort=\"" + dlc_cohort + "\" cohorthint=\"" +
211 dlc_cohorthint + "\" cohortname=\"" +
212 dlc_cohortname + "\" "
213 : "") +
214 "status=\"ok\">"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800215 "<updatecheck status=\"ok\"><urls><url codebase=\"" +
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700216 codebase + "\"/><url codebase=\"" + codebase2 +
217 "\"/></urls><manifest version=\"" + version +
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800218 "\"><packages><package name=\"package3\" size=\"333\" "
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800219 "fp=\"" +
220 fp2 +
221 "\" hash_sha256=\"hash3\"/></packages>"
222 "<actions><action event=\"install\" run=\".signed\"/>"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800223 "<action event=\"postinstall\" MetadataSize=\"33\"/>"
224 "</actions></manifest></updatecheck></app>"
225 : "") +
226 (dlc_app_no_update
227 ? "<app appid=\"" + request_params.GetDlcAppId(kDlcId2) +
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800228 +"\" " +
229 (include_dlc_cohorts
230 ? "cohort=\"" + dlc_cohort + "\" cohorthint=\"" +
231 dlc_cohorthint + "\" cohortname=\"" +
232 dlc_cohortname + "\" "
233 : "") +
234 "><updatecheck status=\"noupdate\"/></app>"
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800235 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700236 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700237 }
238
239 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800240 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700241
Alex Deymo85616652015-10-15 18:48:31 -0700242 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700243 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700244 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
245 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700246 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700247 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700248 string more_info_url = "http://more/info";
249 string prompt = "true";
250 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700251 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700252 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700253 string hash = "4841534831323334";
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800254 string fp = "3.98ba213e";
255 string fp2 = "3.755aff78e";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800256 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700257 string deadline = "";
258 string max_days_to_scatter = "7";
259 string elapsed_days = "42";
260
261 // P2P setting defaults to allowed.
262 bool disable_p2p_for_downloading = false;
263 bool disable_p2p_for_sharing = false;
264
Sen Jiangfe284402018-03-21 14:03:50 -0700265 bool powerwash = false;
266
Alex Deymo8e18f932015-03-27 16:16:59 -0700267 // Omaha cohorts settings.
268 bool include_cohorts = false;
269 string cohort = "";
270 string cohorthint = "";
271 string cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -0800272 // Whether to include Omaha cohorts for DLC apps.
273 bool include_dlc_cohorts = false;
274 string dlc_cohort = "";
275 string dlc_cohorthint = "";
276 string dlc_cohortname = "";
Alex Deymo8e18f932015-03-27 16:16:59 -0700277
278 // Whether to include the CrOS <!ENTITY> in the XML response.
279 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800280
Sen Jiang81259682017-03-30 15:11:30 -0700281 // Whether to include more than one app.
282 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700283 // Whether to include an app with noupdate="true".
284 bool multi_app_self_update = false;
285 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700286 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700287 // Whether to include an additional app with no updatecheck tag.
288 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700289 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800290 bool multi_package = false;
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -0800291 // Whether to include a DLC app with updatecheck tag.
292 bool dlc_app_update = false;
293 // Whether to include a DLC app with no updatecheck tag.
294 bool dlc_app_no_update = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700295
296 // Whether the payload is a rollback.
297 bool rollback = false;
298 // The verified boot firmware key version for the rollback image.
299 string rollback_firmware_version = "";
300 // The verified boot kernel key version for the rollback image.
301 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700302 // The number of milestones back that the verified boot key version has been
303 // supplied.
304 uint32_t rollback_allowed_milestones = 0;
305 // The verified boot key version for the
306 // |current - rollback_allowed_milestones| most recent release.
307 // The pair contains <firmware_key_version, kernel_key_version> each
308 // of which is in the form "key_version.version".
309 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700310};
311
312} // namespace
313
Darin Petkov6a5b3222010-07-13 14:55:28 -0700314namespace chromeos_update_engine {
315
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700316class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
317 public:
318 OmahaRequestActionTestProcessorDelegate()
319 : expected_code_(ErrorCode::kSuccess),
320 interactive_(false),
321 test_http_fetcher_headers_(false) {}
322 ~OmahaRequestActionTestProcessorDelegate() override = default;
323
324 void ProcessingDone(const ActionProcessor* processor,
325 ErrorCode code) override {
326 brillo::MessageLoop::current()->BreakLoop();
327 }
328
329 void ActionCompleted(ActionProcessor* processor,
330 AbstractAction* action,
331 ErrorCode code) override {
332 // Make sure actions always succeed.
333 if (action->Type() == OmahaRequestAction::StaticType()) {
334 EXPECT_EQ(expected_code_, code);
335 // Check that the headers were set in the fetcher during the action. Note
336 // that we set this request as "interactive".
337 auto fetcher = static_cast<const MockHttpFetcher*>(
338 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
339
340 if (test_http_fetcher_headers_) {
341 EXPECT_EQ(interactive_ ? "fg" : "bg",
342 fetcher->GetHeader("X-Goog-Update-Interactivity"));
343 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
344 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
345 }
346 post_data_ = fetcher->post_data();
347 } else if (action->Type() ==
348 ObjectCollectorAction<OmahaResponse>::StaticType()) {
349 EXPECT_EQ(ErrorCode::kSuccess, code);
350 auto collector_action =
351 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
352 omaha_response_.reset(new OmahaResponse(collector_action->object()));
353 EXPECT_TRUE(omaha_response_);
354 } else {
355 EXPECT_EQ(ErrorCode::kSuccess, code);
356 }
357 }
358 ErrorCode expected_code_;
359 brillo::Blob post_data_;
360 bool interactive_;
361 bool test_http_fetcher_headers_;
362 std::unique_ptr<OmahaResponse> omaha_response_;
363};
364
Amin Hassani41ac04b2019-03-29 11:31:03 -0700365struct TestUpdateCheckParams {
366 string http_response;
367 int fail_http_response_code;
368 bool ping_only;
369 bool is_consumer_device;
370 int rollback_allowed_milestones;
371 bool is_policy_loaded;
372 ErrorCode expected_code;
373 metrics::CheckResult expected_check_result;
374 metrics::CheckReaction expected_check_reaction;
375 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700376 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700377};
378
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700379class OmahaRequestActionTest : public ::testing::Test {
380 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800381 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200382 request_params_.set_os_sp("service_pack");
383 request_params_.set_os_board("x86-generic");
384 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700385 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200386 request_params_.set_app_lang("en-US");
387 request_params_.set_current_channel("unittest");
388 request_params_.set_target_channel("unittest");
389 request_params_.set_hwid("OEM MODEL 09235 7471");
Marton Hunyady2abda312018-04-24 18:21:49 +0200390 request_params_.set_delta_okay(true);
391 request_params_.set_interactive(false);
392 request_params_.set_update_url("http://url");
393 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200394 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200395 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700396 request_params_.set_is_install(false);
Andrewe045aef2020-01-08 16:29:22 -0800397 request_params_.set_dlc_apps_params({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200398
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700399 fake_system_state_.set_request_params(&request_params_);
400 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700401
402 // Setting the default update check params. Lookup |TestUpdateCheck()|.
403 tuc_params_ = {
404 .http_response = "",
405 .fail_http_response_code = -1,
406 .ping_only = false,
407 .is_consumer_device = true,
408 .rollback_allowed_milestones = 0,
409 .is_policy_loaded = false,
410 .expected_code = ErrorCode::kSuccess,
411 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
412 .expected_check_reaction = metrics::CheckReaction::kUpdating,
413 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
414 };
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700415
416 ON_CALL(*fake_system_state_.mock_update_attempter(), GetExcluder())
417 .WillByDefault(Return(&mock_excluder_));
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700418 }
419
Andrewe045aef2020-01-08 16:29:22 -0800420 // This function uses the parameters in |tuc_params_| to do an update check.
421 // It will fill out |post_str| with the result data and |response| with
Amin Hassani41ac04b2019-03-29 11:31:03 -0700422 // |OmahaResponse|. Returns true iff an output response was obtained from the
423 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
424 // transfer will fail with that code. |ping_only| is passed through to the
425 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700426 //
427 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700428 // |expected_error_code| parameters are for checking expectations about
429 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
430 // statistics. Use the appropriate ::kUnset value to specify that the given
431 // metric should not be reported.
432 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700433
Amin Hassani41ac04b2019-03-29 11:31:03 -0700434 // Tests events using |event| and |https_response|. It will fill up |post_str|
435 // with the result data.
436 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700437
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800438 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700439 // only a ping or also an updatecheck.
440 void PingTest(bool ping_only);
441
442 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800443 bool InstallDateParseHelper(const string& elapsed_days,
444 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700445
446 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800447 void P2PTest(bool initial_allow_p2p_for_downloading,
448 bool initial_allow_p2p_for_sharing,
449 bool omaha_disable_p2p_for_downloading,
450 bool omaha_disable_p2p_for_sharing,
451 bool payload_state_allow_p2p_attempt,
452 bool expect_p2p_client_lookup,
453 const string& p2p_client_result_url,
454 bool expected_allow_p2p_for_downloading,
455 bool expected_allow_p2p_for_sharing,
456 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700457
Jae Hoon Kim694eeb02020-06-01 14:24:08 -0700458 StrictMock<MockExcluder> mock_excluder_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700459 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700460 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200461 // Used by all tests.
462 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700463
464 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700465
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700466 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700467
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700468 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700469
470 TestUpdateCheckParams tuc_params_;
471
472 // TODO(ahassani): Add trailing _ to these two variables.
473 OmahaResponse response;
474 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700475};
476
Andrew065d78d2020-04-07 15:43:07 -0700477class OmahaRequestActionDlcPingTest : public OmahaRequestActionTest {
478 protected:
479 void SetUp() override {
480 OmahaRequestActionTest::SetUp();
481 dlc_id_ = "dlc0";
482 active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700483 {kDlcPrefsSubDir, dlc_id_, kPrefsPingActive});
Andrew065d78d2020-04-07 15:43:07 -0700484 last_active_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700485 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastActive});
Andrew065d78d2020-04-07 15:43:07 -0700486 last_rollcall_key_ = PrefsInterface::CreateSubKey(
Jae Hoon Kimc1f36922020-05-11 18:20:18 -0700487 {kDlcPrefsSubDir, dlc_id_, kPrefsPingLastRollcall});
Andrew065d78d2020-04-07 15:43:07 -0700488
489 tuc_params_.http_response =
490 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
491 "protocol=\"3.0\"><daystart elapsed_days=\"4763\" "
492 "elapsed_seconds=\"36540\"/><app appid=\"test-app-id\" status=\"ok\">\""
493 "<updatecheck status=\"noupdate\"/></app><app "
494 "appid=\"test-app-id_dlc0\" "
495 "status=\"ok\"><ping status=\"ok\"/><updatecheck status=\"noupdate\"/>"
496 "</app></response>";
497 tuc_params_.expected_check_result =
498 metrics::CheckResult::kNoUpdateAvailable;
499 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
500 }
501
502 std::string dlc_id_;
503 std::string active_key_;
504 std::string last_active_key_;
505 std::string last_rollcall_key_;
506};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700507bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700508 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700509 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700510 auto fetcher =
511 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
512 tuc_params_.http_response.size(),
513 nullptr);
514 if (tuc_params_.fail_http_response_code >= 0) {
515 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700516 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200517 // This ensures the tests didn't forget to update fake_system_state_ if they
518 // are not using the default request_params_.
519 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
520
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700521 auto omaha_request_action =
522 std::make_unique<OmahaRequestAction>(&fake_system_state_,
523 nullptr,
524 std::move(fetcher),
525 tuc_params_.ping_only,
526 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700527
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800528 auto mock_policy_provider =
529 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
530 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700531 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800532
533 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700534 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800535
536 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700537 const bool get_allowed_milestone_succeeds =
538 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800539 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700540 .WillRepeatedly(
541 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
542 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800543
544 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
545 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700546 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800547
Amin Hassani41ac04b2019-03-29 11:31:03 -0700548 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700549 delegate_.interactive_ = request_params_.interactive();
550 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700551 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700552 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700553
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700554 auto collector_action =
555 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
556 BondActions(omaha_request_action.get(), collector_action.get());
557 processor.EnqueueAction(std::move(omaha_request_action));
558 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700559
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700560 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
561 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800562 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700563
Amin Hassani41ac04b2019-03-29 11:31:03 -0700564 EXPECT_CALL(
565 *fake_system_state_.mock_metrics_reporter(),
566 ReportUpdateCheckMetrics(_,
567 tuc_params_.expected_check_result,
568 tuc_params_.expected_check_reaction,
569 tuc_params_.expected_download_error_code))
570 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800571
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700572 loop.PostTask(base::Bind(
573 [](ActionProcessor* processor) { processor->StartProcessing(); },
574 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700575 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700576 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700577 if (delegate_.omaha_response_)
578 response = *delegate_.omaha_response_;
579 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700580 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700581}
582
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700583// Tests Event requests -- they should always succeed. |out_post_data| may be
584// null; if non-null, the post-data received by the mock HttpFetcher is
585// returned.
586void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700587 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700588 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700589 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700590
591 auto action = std::make_unique<OmahaRequestAction>(
592 &fake_system_state_,
593 event,
594 std::make_unique<MockHttpFetcher>(
595 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700596 false,
597 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700598 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700599 processor.set_delegate(&delegate_);
600 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700601
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700602 loop.PostTask(base::Bind(
603 [](ActionProcessor* processor) { processor->StartProcessing(); },
604 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700605 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700606 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700607
Amin Hassani41ac04b2019-03-29 11:31:03 -0700608 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700609}
610
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700611TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700612 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700613 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
614 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
615 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
616 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
617
618 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400619 EXPECT_FALSE(response.update_exists);
620}
621
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700622TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700623 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
624 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
625 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
626
627 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700628 EXPECT_FALSE(response.update_exists);
629}
630
Sen Jiang81259682017-03-30 15:11:30 -0700631TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700632 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700633 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
634 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
635 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
636
637 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700638 EXPECT_FALSE(response.update_exists);
639}
640
Sen Jiang00adf7b2017-06-26 15:57:29 -0700641TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700642 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700643 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
644 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
645 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
646
647 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700648 EXPECT_FALSE(response.update_exists);
649}
650
651TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700652 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700653 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
654 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700655 "</manifest></updatecheck></app></response>";
656 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
657 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
658
659 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700660 EXPECT_FALSE(response.update_exists);
661}
662
Alex Deymo8e18f932015-03-27 16:16:59 -0700663// Test that all the values in the response are parsed in a normal update
664// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700665TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700666 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700667 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
668
669 ASSERT_TRUE(TestUpdateCheck());
670
Darin Petkov6a5b3222010-07-13 14:55:28 -0700671 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700672 EXPECT_EQ(fake_update_response_.version, response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800673 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
674 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700675 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800676 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
677 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800678 EXPECT_EQ(fake_update_response_.fp, response.packages[0].fp);
Sen Jiangcdd52062017-05-18 15:33:10 -0700679 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700680 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
681 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700682 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700683 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700684 // persisted.
685 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
686 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
687 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700688}
689
Sen Jiang0affc2c2017-02-10 15:55:05 -0800690TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800691 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700692 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
693
694 ASSERT_TRUE(TestUpdateCheck());
695
Sen Jiang0affc2c2017-02-10 15:55:05 -0800696 EXPECT_TRUE(response.update_exists);
697 EXPECT_EQ(fake_update_response_.version, response.version);
698 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
699 response.packages[0].payload_urls[0]);
700 EXPECT_EQ(fake_update_response_.codebase + "package2",
701 response.packages[1].payload_urls[0]);
702 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
703 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800704 EXPECT_EQ(fake_update_response_.fp, response.packages[0].fp);
Sen Jiangcdd52062017-05-18 15:33:10 -0700705 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700706 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800707 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700708 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800709 EXPECT_EQ(222u, response.packages[1].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800710 EXPECT_EQ(fake_update_response_.fp2, response.packages[1].fp);
Sen Jiang81259682017-03-30 15:11:30 -0700711 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700712 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700713}
714
715TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700716 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700717 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
718
719 ASSERT_TRUE(TestUpdateCheck());
720
Sen Jiang81259682017-03-30 15:11:30 -0700721 EXPECT_TRUE(response.update_exists);
722 EXPECT_EQ(fake_update_response_.version, response.version);
723 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
724 response.packages[0].payload_urls[0]);
725 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
726 response.packages[1].payload_urls[0]);
727 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
728 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800729 EXPECT_EQ(fake_update_response_.fp, response.packages[0].fp);
Sen Jiang81259682017-03-30 15:11:30 -0700730 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700731 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700732 ASSERT_EQ(2u, response.packages.size());
733 EXPECT_EQ(string("hash3"), response.packages[1].hash);
734 EXPECT_EQ(333u, response.packages[1].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800735 EXPECT_EQ(fake_update_response_.fp2, response.packages[1].fp);
Sen Jiang81259682017-03-30 15:11:30 -0700736 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700737 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700738}
739
740TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700741 fake_update_response_.multi_app = true;
742 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700743 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
744
745 ASSERT_TRUE(TestUpdateCheck());
746
Sen Jiang81259682017-03-30 15:11:30 -0700747 EXPECT_TRUE(response.update_exists);
748 EXPECT_EQ(fake_update_response_.version, response.version);
749 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
750 response.packages[0].payload_urls[0]);
751 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
752 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800753 EXPECT_EQ(fake_update_response_.fp, response.packages[0].fp);
Sen Jiang81259682017-03-30 15:11:30 -0700754 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700755 ASSERT_EQ(2u, response.packages.size());
756 EXPECT_EQ(string("hash3"), response.packages[1].hash);
757 EXPECT_EQ(333u, response.packages[1].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800758 EXPECT_EQ(fake_update_response_.fp2, response.packages[1].fp);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700759 EXPECT_EQ(33u, response.packages[1].metadata_size);
760 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700761}
762
763TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700764 fake_update_response_.multi_app = true;
765 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700766 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
767
768 ASSERT_TRUE(TestUpdateCheck());
769
Sen Jiang81259682017-03-30 15:11:30 -0700770 EXPECT_TRUE(response.update_exists);
771 EXPECT_EQ(fake_update_response_.version, response.version);
772 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
773 response.packages[0].payload_urls[0]);
774 EXPECT_EQ(fake_update_response_.codebase + "package2",
775 response.packages[1].payload_urls[0]);
776 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
777 response.packages[2].payload_urls[0]);
778 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
779 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800780 EXPECT_EQ(fake_update_response_.fp, response.packages[0].fp);
Sen Jiang81259682017-03-30 15:11:30 -0700781 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700782 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700783 ASSERT_EQ(3u, response.packages.size());
784 EXPECT_EQ(string("hash2"), response.packages[1].hash);
785 EXPECT_EQ(222u, response.packages[1].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800786 EXPECT_EQ(fake_update_response_.fp2, response.packages[1].fp);
Sen Jiang81259682017-03-30 15:11:30 -0700787 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700788 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700789 EXPECT_EQ(string("hash3"), response.packages[2].hash);
790 EXPECT_EQ(333u, response.packages[2].size);
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800791 EXPECT_EQ(fake_update_response_.fp2, response.packages[2].fp);
Sen Jiang81259682017-03-30 15:11:30 -0700792 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700793 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800794}
795
Sen Jiangfe284402018-03-21 14:03:50 -0700796TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700797 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700798 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
799
800 ASSERT_TRUE(TestUpdateCheck());
801
Sen Jiangfe284402018-03-21 14:03:50 -0700802 EXPECT_TRUE(response.update_exists);
803 EXPECT_TRUE(response.powerwash_required);
804}
805
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700806TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700807 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700808 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700809 tuc_params_.http_response = "invalid xml>";
810 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
811 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
812 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
813
814 ASSERT_FALSE(TestUpdateCheck());
815
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700816 EXPECT_FALSE(response.update_exists);
817}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700818
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700819TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700820 request_params_.set_interactive(false);
821 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700822 tuc_params_.http_response = "invalid xml>";
823 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
824 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
825 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
826
827 ASSERT_FALSE(TestUpdateCheck());
828
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700829 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700830}
831
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700832TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700833 // Set up a connection manager that doesn't allow a valid update over
834 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700835 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700836 fake_system_state_.set_connection_manager(&mock_cm);
837
Alex Deymo30534502015-07-20 15:06:33 -0700838 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800839 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
840 SetArgPointee<1>(ConnectionTethering::kUnknown),
841 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700842 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
843 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700844
Amin Hassani41ac04b2019-03-29 11:31:03 -0700845 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
846 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
847 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
848
849 ASSERT_FALSE(TestUpdateCheck());
850
Chris Sosa77f79e82014-06-02 18:16:24 -0700851 EXPECT_FALSE(response.update_exists);
852}
853
Weidong Guo421ff332017-04-17 10:08:38 -0700854TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
855 // This test tests that update over cellular is allowed as device policy
856 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700857 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700858 fake_system_state_.set_connection_manager(&mock_cm);
859
860 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
861 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
862 SetArgPointee<1>(ConnectionTethering::kUnknown),
863 Return(true)));
864 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
865 .WillRepeatedly(Return(true));
866 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
867 .WillRepeatedly(Return(true));
868
Amin Hassani41ac04b2019-03-29 11:31:03 -0700869 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
870
871 ASSERT_TRUE(TestUpdateCheck());
872
Weidong Guo421ff332017-04-17 10:08:38 -0700873 EXPECT_TRUE(response.update_exists);
874}
875
876TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
877 // This test tests that update over cellular is blocked as device policy
878 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700879 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700880 fake_system_state_.set_connection_manager(&mock_cm);
881
882 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
883 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
884 SetArgPointee<1>(ConnectionTethering::kUnknown),
885 Return(true)));
886 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
887 .WillRepeatedly(Return(true));
888 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
889 .WillRepeatedly(Return(false));
890
Amin Hassani41ac04b2019-03-29 11:31:03 -0700891 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
892 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
893 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
894
895 ASSERT_FALSE(TestUpdateCheck());
896
Weidong Guo421ff332017-04-17 10:08:38 -0700897 EXPECT_FALSE(response.update_exists);
898}
899
900TEST_F(OmahaRequestActionTest,
901 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
902 // This test tests that, when device policy is not set, update over cellular
903 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700904 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700905 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
906 fake_system_state_.set_connection_manager(&mock_cm);
907
908 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
909 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
910 SetArgPointee<1>(ConnectionTethering::kUnknown),
911 Return(true)));
912 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
913 .WillRepeatedly(Return(false));
914 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
915 .WillRepeatedly(Return(true));
916
Amin Hassani41ac04b2019-03-29 11:31:03 -0700917 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
918
919 ASSERT_TRUE(TestUpdateCheck());
920
Weidong Guo421ff332017-04-17 10:08:38 -0700921 EXPECT_TRUE(response.update_exists);
922}
923
924TEST_F(OmahaRequestActionTest,
925 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
926 // This test tests that, when device policy is not set and permission for
927 // update over cellular is set to false or does not exist, update over
928 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700929 MockConnectionManager mock_cm;
930 // A version different from the version in omaha response.
931 string diff_version = "99.99.99";
932 // A size different from the size in omaha response.
933 int64_t diff_size = 999;
934
935 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
936 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
937 // This test tests cellular (3G) being the only connection type being allowed.
938 fake_system_state_.set_connection_manager(&mock_cm);
939
940 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
941 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
942 SetArgPointee<1>(ConnectionTethering::kUnknown),
943 Return(true)));
944 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
945 .WillRepeatedly(Return(false));
946 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
947 .WillRepeatedly(Return(true));
948
Amin Hassani41ac04b2019-03-29 11:31:03 -0700949 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
950 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
951 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
952
953 ASSERT_FALSE(TestUpdateCheck());
954
Weidong Guo421ff332017-04-17 10:08:38 -0700955 EXPECT_FALSE(response.update_exists);
956}
957
958TEST_F(OmahaRequestActionTest,
959 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
960 // This test tests that, when device policy is not set and permission for
961 // update over cellular is set to false or does not exist, update over
962 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700963 MockConnectionManager mock_cm;
964 // A version same as the version in omaha response.
965 string new_version = fake_update_response_.version;
966 // A size same as the size in omaha response.
967 int64_t new_size = fake_update_response_.size;
968
969 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
970 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
971 fake_system_state_.set_connection_manager(&mock_cm);
972
973 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
974 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
975 SetArgPointee<1>(ConnectionTethering::kUnknown),
976 Return(true)));
977 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
978 .WillRepeatedly(Return(false));
979 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
980 .WillRepeatedly(Return(true));
981
Amin Hassani41ac04b2019-03-29 11:31:03 -0700982 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
983
984 ASSERT_TRUE(TestUpdateCheck());
985
Weidong Guo421ff332017-04-17 10:08:38 -0700986 EXPECT_TRUE(response.update_exists);
987}
988
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700989TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700990 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700991 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700992 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700993 fake_update_response_.version = rollback_version;
994 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
995 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
996 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700997
Chris Sosa77f79e82014-06-02 18:16:24 -0700998 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800999 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -07001000
Amin Hassani41ac04b2019-03-29 11:31:03 -07001001 ASSERT_FALSE(TestUpdateCheck());
1002
Chris Sosa77f79e82014-06-02 18:16:24 -07001003 EXPECT_FALSE(response.update_exists);
1004}
1005
Marton Hunyadyc2882062018-05-14 17:28:25 +02001006// Verify that update checks called during OOBE will not try to download an
1007// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -07001008TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +02001009 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -07001010 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1011 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1012 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1013 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -07001014
Sen Jiang8cd42342018-01-31 12:06:59 -08001015 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001016 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001017 ASSERT_FALSE(TestUpdateCheck());
1018
Kevin Cernekee2494e282016-03-29 18:03:53 -07001019 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001020}
Kevin Cernekee2494e282016-03-29 18:03:53 -07001021
Marton Hunyadyc2882062018-05-14 17:28:25 +02001022// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
1023// enabled.
1024TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +02001025 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -07001026 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001027 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -07001028
Amin Hassani41ac04b2019-03-29 11:31:03 -07001029 ASSERT_TRUE(TestUpdateCheck());
1030
Kevin Cernekee2494e282016-03-29 18:03:53 -07001031 EXPECT_TRUE(response.update_exists);
1032}
1033
Marton Hunyadyc2882062018-05-14 17:28:25 +02001034// Verify that update checks called during OOBE will still try to download an
1035// update if the response includes the deadline field.
1036TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +02001037 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1038 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001039 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +02001040
Amin Hassani41ac04b2019-03-29 11:31:03 -07001041 ASSERT_TRUE(TestUpdateCheck());
1042
Marton Hunyadyc2882062018-05-14 17:28:25 +02001043 EXPECT_TRUE(response.update_exists);
1044}
1045
1046// Verify that update checks called during OOBE will not try to download an
1047// update if a rollback happened, even when the response includes the deadline
1048// field.
1049TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +02001050 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1051 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001052 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1053 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1054 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1055 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1056
Marton Hunyadyc2882062018-05-14 17:28:25 +02001057 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
1058 .WillOnce(Return(true));
1059
Amin Hassani41ac04b2019-03-29 11:31:03 -07001060 ASSERT_FALSE(TestUpdateCheck());
1061
Marton Hunyadyc2882062018-05-14 17:28:25 +02001062 EXPECT_FALSE(response.update_exists);
1063}
1064
Toni Barzic61544e62018-10-11 14:37:30 -07001065// Verify that non-critical updates are skipped by reporting the
1066// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1067// i.e. when the update would need user permission. Note that reporting
1068// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1069// in OOBE (warning the user about an update that will be skipped).
1070TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001071 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1072
1073 MockConnectionManager mock_cm;
1074 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001075 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1076 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1077 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1078 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001079
1080 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1081 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1082 SetArgPointee<1>(ConnectionTethering::kUnknown),
1083 Return(true)));
1084 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1085 .WillRepeatedly(Return(false));
1086
Amin Hassani41ac04b2019-03-29 11:31:03 -07001087 ASSERT_FALSE(TestUpdateCheck());
1088
Toni Barzic61544e62018-10-11 14:37:30 -07001089 EXPECT_FALSE(response.update_exists);
1090}
1091
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001092TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001093 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));
May Lippert60aa3ca2018-08-15 16:55:29 -07001096 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001097 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1098 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1099 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001100
Amin Hassani41ac04b2019-03-29 11:31:03 -07001101 ASSERT_FALSE(TestUpdateCheck());
1102
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001103 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001104}
1105
1106TEST_F(OmahaRequestActionTest,
1107 WallClockBasedWaitAloneCausesScatteringInteractive) {
1108 request_params_.set_wall_clock_based_wait_enabled(true);
1109 request_params_.set_update_check_count_wait_enabled(false);
1110 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1111 request_params_.set_interactive(true);
1112 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1113 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001114
1115 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001116 ASSERT_TRUE(TestUpdateCheck());
1117
Chris Sosa968d0572013-08-23 14:46:02 -07001118 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001119}
1120
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001121TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001122 request_params_.set_wall_clock_based_wait_enabled(false);
1123 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1124 request_params_.set_update_check_count_wait_enabled(true);
1125 request_params_.set_min_update_checks_needed(1);
1126 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001127 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001128
Amin Hassani41ac04b2019-03-29 11:31:03 -07001129 ASSERT_TRUE(TestUpdateCheck());
1130
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001131 EXPECT_TRUE(response.update_exists);
1132}
1133
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001134TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001135 request_params_.set_wall_clock_based_wait_enabled(true);
1136 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1137 request_params_.set_update_check_count_wait_enabled(true);
1138 request_params_.set_min_update_checks_needed(1);
1139 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001140 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001141 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1142
1143 ASSERT_TRUE(TestUpdateCheck());
1144
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001145 EXPECT_TRUE(response.update_exists);
1146}
1147
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001148TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001149 request_params_.set_wall_clock_based_wait_enabled(true);
1150 request_params_.set_waiting_period(TimeDelta());
1151 request_params_.set_update_check_count_wait_enabled(true);
1152 request_params_.set_min_update_checks_needed(0);
1153 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001154 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001155 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001156
Amin Hassani41ac04b2019-03-29 11:31:03 -07001157 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001158
Ben Chan9abb7632014-08-07 00:10:53 -07001159 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001160 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001161 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001162 EXPECT_TRUE(response.update_exists);
1163}
1164
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001165TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001166 request_params_.set_wall_clock_based_wait_enabled(true);
1167 request_params_.set_waiting_period(TimeDelta());
1168 request_params_.set_update_check_count_wait_enabled(true);
1169 request_params_.set_min_update_checks_needed(1);
1170 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001171 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001172 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1173 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1174 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001175
Amin Hassani41ac04b2019-03-29 11:31:03 -07001176 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001177
Ben Chan9abb7632014-08-07 00:10:53 -07001178 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001179 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001180 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001181 EXPECT_FALSE(response.update_exists);
1182}
1183
Amin Hassani41ac04b2019-03-29 11:31:03 -07001184TEST_F(OmahaRequestActionTest,
1185 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001186 request_params_.set_wall_clock_based_wait_enabled(true);
1187 request_params_.set_waiting_period(TimeDelta());
1188 request_params_.set_update_check_count_wait_enabled(true);
1189 request_params_.set_min_update_checks_needed(1);
1190 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001191 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001192 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001193 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1194
1195 // Verify if we are interactive check we don't defer.
1196 ASSERT_TRUE(TestUpdateCheck());
1197
1198 EXPECT_TRUE(response.update_exists);
1199}
1200
1201TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1202 request_params_.set_wall_clock_based_wait_enabled(true);
1203 request_params_.set_waiting_period(TimeDelta());
1204 request_params_.set_update_check_count_wait_enabled(true);
1205 request_params_.set_min_update_checks_needed(1);
1206 request_params_.set_max_update_checks_allowed(8);
1207 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1208 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1209 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1210 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001211
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001212 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001213 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001214
Ben Chan9abb7632014-08-07 00:10:53 -07001215 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001216 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001217 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001218 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001219 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001220 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001221}
1222
1223TEST_F(OmahaRequestActionTest,
1224 ExistingUpdateCheckCountCausesScatteringInteractive) {
1225 request_params_.set_wall_clock_based_wait_enabled(true);
1226 request_params_.set_waiting_period(TimeDelta());
1227 request_params_.set_update_check_count_wait_enabled(true);
1228 request_params_.set_min_update_checks_needed(1);
1229 request_params_.set_max_update_checks_allowed(8);
1230 request_params_.set_interactive(true);
1231 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1232 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1233
1234 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001235
1236 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001237 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001238 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001239}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001240
Adolfo Victoria497044c2018-07-18 07:51:42 -07001241TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1242 // If staging is on, the value for max days to scatter should be ignored, and
1243 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001244 request_params_.set_wall_clock_based_wait_enabled(true);
1245 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1246 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001247 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1248
Adolfo Victoria497044c2018-07-18 07:51:42 -07001249 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1250 // This should not prevent scattering due to staging.
1251 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001252 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1253 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1254 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1255
1256 ASSERT_FALSE(TestUpdateCheck());
1257
Adolfo Victoria497044c2018-07-18 07:51:42 -07001258 EXPECT_FALSE(response.update_exists);
1259
1260 // Interactive updates should not be affected.
1261 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001262 tuc_params_.expected_code = ErrorCode::kSuccess;
1263 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1264
1265 ASSERT_TRUE(TestUpdateCheck());
1266
Adolfo Victoria497044c2018-07-18 07:51:42 -07001267 EXPECT_TRUE(response.update_exists);
1268}
1269
Alex Deymo8e18f932015-03-27 16:16:59 -07001270TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001271 fake_update_response_.include_cohorts = true;
1272 fake_update_response_.cohort = "s/154454/8479665";
1273 fake_update_response_.cohorthint = "please-put-me-on-beta";
1274 fake_update_response_.cohortname = "stable";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001275 request_params_.set_dlc_apps_params(
1276 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
1277 fake_update_response_.dlc_app_update = true;
1278 fake_update_response_.include_dlc_cohorts = true;
1279 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1280 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1281 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001282 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001283
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001284 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001285 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001286
1287 string value;
1288 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1289 EXPECT_EQ(fake_update_response_.cohort, value);
1290
1291 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1292 EXPECT_EQ(fake_update_response_.cohorthint, value);
1293
1294 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1295 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001296
1297 EXPECT_TRUE(fake_prefs_.GetString(
1298 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
1299 &value));
1300 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1301
1302 EXPECT_TRUE(fake_prefs_.GetString(
1303 fake_prefs_.CreateSubKey(
1304 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1305 &value));
1306 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1307
1308 EXPECT_TRUE(fake_prefs_.GetString(
1309 fake_prefs_.CreateSubKey(
1310 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1311 &value));
1312 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Alex Deymo8e18f932015-03-27 16:16:59 -07001313}
1314
1315TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001316 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1317 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1318 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001319 const string dlc_cohort_key =
1320 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1321 const string dlc_cohort_hint_key = fake_prefs_.CreateSubKey(
1322 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint});
1323 const string dlc_cohort_name_key = fake_prefs_.CreateSubKey(
1324 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName});
1325 request_params_.set_dlc_apps_params(
1326 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
1327 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_key, "old_value_dlc"));
1328 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_hint_key, "old_hint_dlc"));
1329 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_name_key, "old_name_dlc"));
Alex Deymo8e18f932015-03-27 16:16:59 -07001330 fake_update_response_.include_cohorts = true;
1331 fake_update_response_.cohort = "s/154454/8479665";
1332 fake_update_response_.cohorthint = "please-put-me-on-beta";
1333 fake_update_response_.cohortname = "";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001334 fake_update_response_.dlc_app_update = true;
1335 fake_update_response_.include_dlc_cohorts = true;
1336 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1337 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1338 fake_update_response_.dlc_cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001339 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001340
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001341 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001342 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001343
1344 string value;
1345 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1346 EXPECT_EQ(fake_update_response_.cohort, value);
1347
1348 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1349 EXPECT_EQ(fake_update_response_.cohorthint, value);
1350
1351 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001352
1353 EXPECT_TRUE(fake_prefs_.GetString(dlc_cohort_key, &value));
1354 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1355
1356 EXPECT_TRUE(fake_prefs_.GetString(dlc_cohort_hint_key, &value));
1357 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1358
1359 EXPECT_FALSE(fake_prefs_.GetString(dlc_cohort_name_key, &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001360}
1361
1362TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001363 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001364
Amin Hassani41ac04b2019-03-29 11:31:03 -07001365 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001366 const string dlc_cohort_key =
1367 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort});
1368 EXPECT_TRUE(fake_prefs_.SetString(dlc_cohort_key, "old_value_dlc"));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001369 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001370
1371 string value;
1372 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1373 EXPECT_EQ("old_value", value);
1374
1375 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1376 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001377
1378 EXPECT_TRUE(fake_prefs_.GetString(dlc_cohort_key, &value));
1379 EXPECT_EQ("old_value_dlc", value);
1380
1381 EXPECT_FALSE(fake_prefs_.GetString(
1382 fake_prefs_.CreateSubKey(
1383 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1384 &value));
1385 EXPECT_FALSE(fake_prefs_.GetString(
1386 fake_prefs_.CreateSubKey(
1387 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1388 &value));
Alex Deymo8e18f932015-03-27 16:16:59 -07001389}
1390
Alex Deymo00d79ac2015-06-29 15:41:49 -07001391TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001392 fake_update_response_.include_cohorts = true;
1393 fake_update_response_.cohort = "s/154454/8479665";
1394 fake_update_response_.cohorthint = "please-put-me-on-beta";
1395 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001396 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1397 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1398 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001399
Amin Hassani41ac04b2019-03-29 11:31:03 -07001400 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001401
1402 string value;
1403 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1404 EXPECT_EQ(fake_update_response_.cohort, value);
1405
1406 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1407 EXPECT_EQ(fake_update_response_.cohorthint, value);
1408
1409 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1410 EXPECT_EQ(fake_update_response_.cohortname, value);
1411}
1412
Sen Jiangb1e063a2017-09-15 17:44:31 -07001413TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001414 fake_update_response_.multi_app = true;
1415 fake_update_response_.include_cohorts = true;
1416 fake_update_response_.cohort = "s/154454/8479665";
1417 fake_update_response_.cohorthint = "please-put-me-on-beta";
1418 fake_update_response_.cohortname = "stable";
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001419 request_params_.set_dlc_apps_params(
1420 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
1421 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
1422 fake_update_response_.dlc_app_update = true;
1423 fake_update_response_.dlc_app_no_update = true;
1424 fake_update_response_.include_dlc_cohorts = true;
1425 fake_update_response_.dlc_cohort = "s/154454/8479665/dlc";
1426 fake_update_response_.dlc_cohorthint = "please-put-me-on-beta-dlc";
1427 fake_update_response_.dlc_cohortname = "stable-dlc";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001428 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001429
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001430 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001431 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001432
1433 string value;
1434 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1435 EXPECT_EQ(fake_update_response_.cohort, value);
1436
1437 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1438 EXPECT_EQ(fake_update_response_.cohorthint, value);
1439
1440 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1441 EXPECT_EQ(fake_update_response_.cohortname, value);
Jae Hoon Kime2cac612020-11-02 18:30:29 -08001442
1443 EXPECT_TRUE(fake_prefs_.GetString(
1444 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohort}),
1445 &value));
1446 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1447 EXPECT_TRUE(fake_prefs_.GetString(
1448 fake_prefs_.CreateSubKey({kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohort}),
1449 &value));
1450 EXPECT_EQ(fake_update_response_.dlc_cohort, value);
1451
1452 EXPECT_TRUE(fake_prefs_.GetString(
1453 fake_prefs_.CreateSubKey(
1454 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortHint}),
1455 &value));
1456 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1457 EXPECT_TRUE(fake_prefs_.GetString(
1458 fake_prefs_.CreateSubKey(
1459 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortHint}),
1460 &value));
1461 EXPECT_EQ(fake_update_response_.dlc_cohorthint, value);
1462
1463 EXPECT_TRUE(fake_prefs_.GetString(
1464 fake_prefs_.CreateSubKey(
1465 {kDlcPrefsSubDir, kDlcId1, kPrefsOmahaCohortName}),
1466 &value));
1467 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
1468 EXPECT_TRUE(fake_prefs_.GetString(
1469 fake_prefs_.CreateSubKey(
1470 {kDlcPrefsSubDir, kDlcId2, kPrefsOmahaCohortName}),
1471 &value));
1472 EXPECT_EQ(fake_update_response_.dlc_cohortname, value);
Sen Jiangb1e063a2017-09-15 17:44:31 -07001473}
1474
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001475TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001476 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001477 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001478 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001479
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001480 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001481 &fake_system_state_,
1482 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001483 std::make_unique<MockHttpFetcher>(
1484 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001485 false,
1486 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001487 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001488 processor.set_delegate(&delegate_);
1489 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001490
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001491 loop.PostTask(base::Bind(
1492 [](ActionProcessor* processor) { processor->StartProcessing(); },
1493 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001494 loop.Run();
1495 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001496 EXPECT_FALSE(processor.IsRunning());
1497}
1498
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001499TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001500 tuc_params_.http_response = "invalid xml>";
1501 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1502 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1503 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1504
1505 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001506 EXPECT_FALSE(response.update_exists);
1507}
1508
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001509TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001510 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
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, MissingStatusTest) {
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/></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, InvalidStatusTest) {
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 "<updatecheck status=\"InvalidStatusTest\"/></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, MissingNodesetTest) {
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\"/>"
1552 "<app appid=\"foo\" status=\"ok\">"
1553 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001554 "</app></response>";
1555 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1556 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1557 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1558
1559 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001560 EXPECT_FALSE(response.update_exists);
1561}
1562
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001563TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001564 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001565 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1566 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001567 // the appid needs to match that in the request params
1568 "<app appid=\"" +
1569 fake_update_response_.app_id +
1570 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001571 "<updatecheck status=\"ok\">"
1572 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001573 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001574 "<packages><package hash=\"not-used\" name=\"f\" "
Vyshu Khota4c5413d2020-11-04 16:17:25 -08001575 "size=\"587\" fp=\"3.789\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001576 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001577 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001578 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001579 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001580 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001581
Amin Hassani41ac04b2019-03-29 11:31:03 -07001582 ASSERT_TRUE(TestUpdateCheck());
1583
Darin Petkov6a5b3222010-07-13 14:55:28 -07001584 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001585 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001586 EXPECT_EQ("http://missing/field/test/f",
1587 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001588 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001589 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
Vyshu Khota4c5413d2020-11-04 16:17:25 -08001590 EXPECT_EQ(string("3.789"), response.packages[0].fp);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001591 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001592 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001593 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001594}
1595
1596namespace {
1597class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1598 public:
1599 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001600 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001601 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001602};
1603
Alex Deymo60ca1a72015-06-18 18:19:15 -07001604void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001605 processor->StartProcessing();
1606 CHECK(processor->IsRunning());
1607 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001608}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001609} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001610
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001611TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001612 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001613 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001614
Alex Deymo60ca1a72015-06-18 18:19:15 -07001615 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001616 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001617 &fake_system_state_,
1618 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001619 std::make_unique<MockHttpFetcher>(
1620 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001621 false,
1622 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001623 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001624 ActionProcessor processor;
1625 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001626 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001627
Alex Deymo60ca1a72015-06-18 18:19:15 -07001628 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1629 loop.Run();
1630 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001631}
1632
Alex Deymob0d74eb2015-03-30 17:59:17 -07001633TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001634 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001635 request_params_.set_os_sp("testtheservice_pack>");
1636 request_params_.set_os_board("x86 generic<id");
1637 request_params_.set_current_channel("unittest_track&lt;");
1638 request_params_.set_target_channel("unittest_track&lt;");
Amin Hassani37b67232020-08-13 09:29:48 -07001639 request_params_.set_lts_tag("unittest_hint&lt;");
Marton Hunyady2abda312018-04-24 18:21:49 +02001640 request_params_.set_hwid("<OEM MODEL>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001641 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1642 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001643 fake_prefs_.SetString(
1644 kPrefsOmahaCohortName,
1645 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001646 tuc_params_.http_response = "invalid xml>";
1647 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1648 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1649 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1650
1651 ASSERT_FALSE(TestUpdateCheck());
1652
Alex Deymo8e18f932015-03-27 16:16:59 -07001653 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1654 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1655 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1656 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1657 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1658 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
Amin Hassani37b67232020-08-13 09:29:48 -07001659 EXPECT_NE(string::npos, post_str.find("unittest_hint&amp;lt;"));
1660 EXPECT_EQ(string::npos, post_str.find("unittest_hint&lt;"));
Alex Deymo8e18f932015-03-27 16:16:59 -07001661 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1662 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1663 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1664 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1665 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1666 // Values from Prefs that are too big are removed from the XML instead of
1667 // encoded.
1668 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001669}
1670
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001671TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001672 fake_update_response_.deadline = "&lt;20110101";
1673 fake_update_response_.more_info_url = "testthe&lt;url";
1674 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001675 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1676
1677 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001678
Sen Jiang0affc2c2017-02-10 15:55:05 -08001679 EXPECT_EQ("testthe<url", response.more_info_url);
1680 EXPECT_EQ("testthe&codebase/file.signed",
1681 response.packages[0].payload_urls[0]);
1682 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001683}
1684
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001685TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001686 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001687 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001688 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001689
Amin Hassani41ac04b2019-03-29 11:31:03 -07001690 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001691 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001692}
1693
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001694TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001695 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001696 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001697 tuc_params_.http_response = "invalid xml>";
1698 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1699 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1700 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001701
Darin Petkov95508da2011-01-05 12:42:29 -08001702 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001703 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001704 // An existing but empty previous version means that we didn't reboot to a new
1705 // update, therefore, no need to update the previous version.
1706 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001707 ASSERT_FALSE(TestUpdateCheck());
1708
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001709 EXPECT_NE(
1710 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1711 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001712 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001713 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1714 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001715 // No <event> tag should be sent if we didn't reboot to an update.
1716 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001717}
1718
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001719TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001720 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001721 "invalid xml>");
1722
Alex Vakulenko75039d72014-03-25 12:36:28 -07001723 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001724 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001725 OmahaEvent::kTypeUpdateDownloadStarted,
1726 OmahaEvent::kResultSuccess);
1727 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001728 EXPECT_EQ(post_str.find("ping"), string::npos);
1729 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001730}
1731
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001732TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001733 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001734 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001735 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001736 "invalid xml>");
1737
Alex Vakulenko75039d72014-03-25 12:36:28 -07001738 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001739 " <event eventtype=\"%d\" eventresult=\"%d\" "
1740 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001741 OmahaEvent::kTypeDownloadComplete,
1742 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001743 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001744 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001745 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001746}
1747
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001748TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001749 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001750 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001751 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001752 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001753 std::make_unique<MockHttpFetcher>(
1754 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001755 false,
1756 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001757 EXPECT_FALSE(update_check_action.IsEvent());
1758
1759 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001760 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001761 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001762 std::make_unique<MockHttpFetcher>(
1763 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001764 false,
1765 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001766 EXPECT_TRUE(event_action.IsEvent());
1767}
1768
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001769TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001770 tuc_params_.http_response = "invalid xml>";
1771 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1772 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1773 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1774
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001775 for (int i = 0; i < 2; i++) {
1776 bool delta_okay = i == 1;
1777 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001778 request_params_.set_delta_okay(delta_okay);
1779
Amin Hassani41ac04b2019-03-29 11:31:03 -07001780 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001781 EXPECT_NE(
1782 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1783 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001784 << "i = " << i;
1785 }
1786}
1787
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001788TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001789 tuc_params_.http_response = "invalid xml>";
1790 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1791 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1792 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1793
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001794 for (int i = 0; i < 2; i++) {
1795 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001796 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001797 request_params_.set_interactive(interactive);
1798
Amin Hassani41ac04b2019-03-29 11:31:03 -07001799 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001800 EXPECT_NE(post_str.find(
1801 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001802 string::npos)
1803 << "i = " << i;
1804 }
1805}
1806
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001807TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001808 tuc_params_.http_response = "invalid xml>";
1809 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1810 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1811 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1812
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001813 for (int i = 0; i < 2; i++) {
1814 bool target_version_set = i == 1;
1815 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001816 request_params_.set_target_version_prefix(target_version_prefix);
1817
Amin Hassani41ac04b2019-03-29 11:31:03 -07001818 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001819 if (target_version_set) {
1820 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1821 string::npos)
1822 << "i = " << i;
1823 } else {
1824 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1825 << "i = " << i;
1826 }
1827 }
1828}
1829
1830TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001831 tuc_params_.http_response = "invalid xml>";
1832 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1833 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1834 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1835
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001836 for (int i = 0; i < 4; i++) {
1837 bool rollback_allowed = i / 2 == 0;
1838 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001839 request_params_.set_target_version_prefix(target_version_set ? "10032."
1840 : "");
1841 request_params_.set_rollback_allowed(rollback_allowed);
1842
Amin Hassani41ac04b2019-03-29 11:31:03 -07001843 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001844 if (rollback_allowed && target_version_set) {
1845 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1846 << "i = " << i;
1847 } else {
1848 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1849 }
1850 }
1851}
1852
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001853TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001854 OmahaEvent default_event;
1855 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1856 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001857 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001858
1859 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1860 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1861 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001862 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001863
1864 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1865 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001866 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001867 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1868 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001869 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001870}
1871
Askar Aitzhan570ca872019-04-24 11:16:12 +02001872TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
Askar Aitzhan18fff842019-06-21 23:24:37 +02001873 // If DeviceQuickFixBuildToken value is set it takes precedence over pref
1874 // value.
1875 constexpr char autoupdate_token[] = "autoupdate_token>";
1876 constexpr char xml_encoded_autoupdate_token[] = "autoupdate_token&gt;";
1877 constexpr char omaha_cohort_hint[] = "cohort_hint";
Askar Aitzhan570ca872019-04-24 11:16:12 +02001878
1879 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1880 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1881 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1882 request_params_.set_autoupdate_token(autoupdate_token);
Askar Aitzhan18fff842019-06-21 23:24:37 +02001883 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
Askar Aitzhan570ca872019-04-24 11:16:12 +02001884
1885 ASSERT_TRUE(TestUpdateCheck());
1886
Askar Aitzhan18fff842019-06-21 23:24:37 +02001887 EXPECT_NE(string::npos,
1888 post_str.find("cohorthint=\"" +
1889 string(xml_encoded_autoupdate_token) + "\""));
1890 EXPECT_EQ(string::npos, post_str.find(autoupdate_token));
1891 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
1892}
1893
1894TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsNotSetTest) {
1895 // If DeviceQuickFixBuildToken is not set, pref value will be provided in
1896 // cohorthint attribute.
1897 constexpr char omaha_cohort_hint[] = "evil_string>";
1898 constexpr char xml_encoded_cohort_hint[] = "evil_string&gt;";
1899
1900 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1901 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1902 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1903 fake_prefs_.SetString(kPrefsOmahaCohortHint, omaha_cohort_hint);
1904
1905 ASSERT_TRUE(TestUpdateCheck());
1906
1907 EXPECT_NE(
1908 string::npos,
1909 post_str.find("cohorthint=\"" + string(xml_encoded_cohort_hint) + "\""));
1910 EXPECT_EQ(string::npos, post_str.find(omaha_cohort_hint));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001911}
1912
Amin Hassani37b67232020-08-13 09:29:48 -07001913TEST_F(OmahaRequestActionTest, TargetChannelHintTest) {
1914 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1915 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1916 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1917 request_params_.set_lts_tag("hint>");
1918
1919 ASSERT_TRUE(TestUpdateCheck());
1920
1921 EXPECT_NE(string::npos, post_str.find("ltstag=\"hint&gt;\""));
1922}
1923
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001924void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001925 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001926 fake_system_state_.set_prefs(&prefs);
1927 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001928 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001929 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1930 // Add a few hours to the day difference to test no rounding, etc.
1931 int64_t five_days_ago =
1932 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1933 int64_t six_days_ago =
1934 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1935 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001936 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001937 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001938 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001939 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001940 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001941
1942 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1943 tuc_params_.ping_only = ping_only;
1944 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1945 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1946
1947 ASSERT_TRUE(TestUpdateCheck());
1948
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001949 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1950 string::npos);
1951 if (ping_only) {
1952 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1953 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1954 } else {
1955 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1956 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001957 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001958}
1959
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001960TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001961 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001962}
1963
1964TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001965 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001966}
1967
1968TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001969 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001970 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001971 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001972 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001973 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001974 int64_t three_days_ago =
1975 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1976 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001977 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001978 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001979 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001980 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001981 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001982 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001983
1984 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1985 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1986 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1987
1988 ASSERT_TRUE(TestUpdateCheck());
1989
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001990 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001991}
1992
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001993TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001994 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001995 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001996 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001997 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001998 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001999 int64_t four_days_ago =
2000 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
2001 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002002 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002003 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002004 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002005 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002006 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002007 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002008
2009 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2010 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2011 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2012
2013 ASSERT_TRUE(TestUpdateCheck());
2014
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002015 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07002016 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002017}
2018
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002019TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002020 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002021 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002022 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002023 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002024 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002025 int64_t one_hour_ago =
2026 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002027 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002028 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002029 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002030 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002031 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002032 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002033 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
2034 // send a ping.
2035 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2036 .WillOnce(Return(true));
2037 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2038 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002039
2040 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2041 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2042 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2043
2044 ASSERT_TRUE(TestUpdateCheck());
2045
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002046 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002047}
2048
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002049TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07002050 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08002051 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002052 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07002053 int64_t now = Time::Now().ToInternalValue();
2054 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002055 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002056 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002057 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002058 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2059 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002060
2061 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2062 tuc_params_.ping_only = true;
2063 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
2064 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2065
2066 EXPECT_TRUE(TestUpdateCheck());
2067 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07002068}
2069
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002070TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002071 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002072 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002073 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002074 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002075 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002076 int64_t future =
2077 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002078 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002079 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002080 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002081 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002082 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002083 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002084 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2085 .WillOnce(Return(true));
2086 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2087 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002088
2089 tuc_params_.http_response =
2090 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2091 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
2092 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2093 "<updatecheck status=\"noupdate\"/></app></response>";
2094 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2095 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2096
2097 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002098 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002099}
2100
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002101TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002102 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07002103 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002104 // may fail if it runs for longer than 5 seconds. It shouldn't run
2105 // that long though.
2106 int64_t midnight =
2107 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
2108 int64_t midnight_slack =
2109 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08002110 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002111 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002112 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2113 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002114 EXPECT_CALL(prefs,
2115 SetInt64(kPrefsLastActivePingDay,
2116 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002117 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002118 EXPECT_CALL(prefs,
2119 SetInt64(kPrefsLastRollCallPingDay,
2120 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002121 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07002122
2123 tuc_params_.http_response =
2124 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2125 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
2126 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2127 "<updatecheck status=\"noupdate\"/></app></response>";
2128 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2129 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2130
2131 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002132}
2133
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002134TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002135 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002136 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002137 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2138 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002139 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2140 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002141
2142 tuc_params_.http_response =
2143 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2144 "protocol=\"3.0\"><daystart blah=\"200\"/>"
2145 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2146 "<updatecheck status=\"noupdate\"/></app></response>";
2147 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2148 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2149
2150 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002151}
2152
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002153TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002154 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002155 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002156 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2157 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002158 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2159 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002160
2161 tuc_params_.http_response =
2162 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
2163 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
2164 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
2165 "<updatecheck status=\"noupdate\"/></app></response>";
2166 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2167 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2168
2169 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002170}
2171
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002172TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07002173 tuc_params_.http_response = "invalid xml>";
2174 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2175 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2176 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2177
2178 ASSERT_FALSE(TestUpdateCheck());
2179
Darin Petkov84c763c2010-07-29 16:27:58 -07002180 EXPECT_EQ(post_str.find("machineid="), string::npos);
2181 EXPECT_EQ(post_str.find("userid="), string::npos);
2182}
2183
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002184TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002185 const int http_error_code =
2186 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002187 tuc_params_.fail_http_response_code = 501;
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 static_cast<metrics::DownloadErrorCode>(501);
2193
2194 ASSERT_FALSE(TestUpdateCheck());
2195
Darin Petkovedc522e2010-11-05 09:35:17 -07002196 EXPECT_FALSE(response.update_exists);
2197}
2198
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002199TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002200 const int http_error_code =
2201 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002202
2203 tuc_params_.fail_http_response_code = 1500;
2204 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2205 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2206 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2207 tuc_params_.expected_download_error_code =
2208 metrics::DownloadErrorCode::kHttpStatusOther;
2209
2210 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002211 EXPECT_FALSE(response.update_exists);
2212}
2213
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002214TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002215 request_params_.set_wall_clock_based_wait_enabled(true);
2216 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2217 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002218
Sen Jiang7c1171e2016-06-23 11:35:40 -07002219 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002220 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002221 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002222
2223 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2224 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2225 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2226
2227 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002228
Ben Chan9abb7632014-08-07 00:10:53 -07002229 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002230 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002231 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002232 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002233
2234 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002235 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002236 tuc_params_.expected_code = ErrorCode::kSuccess;
2237 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2238
2239 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002240 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002241}
2242
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002243TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002244 request_params_.set_wall_clock_based_wait_enabled(true);
2245 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2246 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002247
Sen Jiang7c1171e2016-06-23 11:35:40 -07002248 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002249 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2250 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002251 ASSERT_TRUE(
2252 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2253 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002254
Amin Hassani41ac04b2019-03-29 11:31:03 -07002255 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2256
2257 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002258 EXPECT_TRUE(response.update_exists);
2259
2260 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002261 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002262 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002263 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2264}
2265
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002266TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002267 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002268 base::ScopedTempDir tempdir;
2269 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002270
Marton Hunyady2abda312018-04-24 18:21:49 +02002271 request_params_.set_root(tempdir.GetPath().value());
2272 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2273 request_params_.set_app_version("1.2.3.4");
2274 request_params_.set_product_components("o.bundle=1");
2275 request_params_.set_current_channel("canary-channel");
2276 EXPECT_TRUE(
2277 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2278 request_params_.UpdateDownloadChannel();
2279 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002280
2281 tuc_params_.http_response = "invalid xml>";
2282 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2283 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2284 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2285
2286 ASSERT_FALSE(TestUpdateCheck());
2287
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002288 EXPECT_NE(
2289 string::npos,
2290 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2291 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2292 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002293 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002294}
2295
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002296TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002297 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002298 base::ScopedTempDir tempdir;
2299 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002300
Marton Hunyady2abda312018-04-24 18:21:49 +02002301 request_params_.set_root(tempdir.GetPath().value());
2302 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2303 request_params_.set_app_version("5.6.7.8");
2304 request_params_.set_product_components("o.bundle=1");
2305 request_params_.set_current_channel("stable-channel");
2306 EXPECT_TRUE(
2307 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2308 request_params_.UpdateDownloadChannel();
2309 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002310
2311 tuc_params_.http_response = "invalid xml>";
2312 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2313 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2314 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2315
2316 ASSERT_FALSE(TestUpdateCheck());
2317
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002318 EXPECT_NE(
2319 string::npos,
2320 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2321 "version=\"5.6.7.8\" "
2322 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002323 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002324 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002325}
2326
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002327// Checks that the initial ping with a=-1 r=-1 is not send when the device
2328// was powerwashed.
2329TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2330 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2331
2332 // Flag that the device was powerwashed in the past.
2333 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002334 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2335 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2336 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002337
Amin Hassani41ac04b2019-03-29 11:31:03 -07002338 ASSERT_TRUE(TestUpdateCheck());
2339
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002340 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002341 EXPECT_EQ(string::npos, post_str.find("<ping"));
2342}
2343
Amin Hassani1677e812017-06-21 13:36:36 -07002344// Checks that the initial ping with a=-1 r=-1 is not send when the device
2345// first_active_omaha_ping_sent is set.
2346TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2347 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2348
2349 // Flag that the device was not powerwashed in the past.
2350 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2351
2352 // Flag that the device has sent first active ping in the past.
2353 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
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());
2360
Amin Hassani1677e812017-06-21 13:36:36 -07002361 // We shouldn't send a ping in this case since
2362 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002363 EXPECT_EQ(string::npos, post_str.find("<ping"));
2364}
2365
Alex Deymo9fded1e2015-11-05 12:31:19 -08002366// Checks that the event 54 is sent on a reboot to a new update.
2367TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2368 // Flag that the device was updated in a previous boot.
2369 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2370
Amin Hassani41ac04b2019-03-29 11:31:03 -07002371 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2372 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2373 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2374
2375 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002376
2377 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002378 EXPECT_NE(
2379 string::npos,
2380 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2381 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002382 EXPECT_NE(string::npos,
2383 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2384
2385 // The previous version flag should have been removed.
2386 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2387 string prev_version;
2388 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2389 EXPECT_TRUE(prev_version.empty());
2390}
2391
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002392void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2393 bool initial_allow_p2p_for_sharing,
2394 bool omaha_disable_p2p_for_downloading,
2395 bool omaha_disable_p2p_for_sharing,
2396 bool payload_state_allow_p2p_attempt,
2397 bool expect_p2p_client_lookup,
2398 const string& p2p_client_result_url,
2399 bool expected_allow_p2p_for_downloading,
2400 bool expected_allow_p2p_for_sharing,
2401 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002402 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2403 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2404 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002405
2406 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002407 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002408 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2409 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002410 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2411 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2412 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2413 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2414 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2415 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2416 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2417 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2418 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2419 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2420
David Zeuthen8f191b22013-08-06 12:27:50 -07002421 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002422 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002423 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2424
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002425 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2426 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002427 .Times(expect_p2p_client_lookup ? 1 : 0);
2428
Alex Deymo8e18f932015-03-27 16:16:59 -07002429 fake_update_response_.disable_p2p_for_downloading =
2430 omaha_disable_p2p_for_downloading;
2431 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002432
2433 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2434 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2435
2436 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002437 EXPECT_TRUE(response.update_exists);
2438
Gilad Arnold74b5f552014-10-07 08:17:16 -07002439 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2440 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002441 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002442
Gilad Arnold74b5f552014-10-07 08:17:16 -07002443 EXPECT_EQ(expected_allow_p2p_for_downloading,
2444 actual_allow_p2p_for_downloading);
2445 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2446 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002447}
2448
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002449TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002450 P2PTest(true, // initial_allow_p2p_for_downloading
2451 true, // initial_allow_p2p_for_sharing
2452 false, // omaha_disable_p2p_for_downloading
2453 false, // omaha_disable_p2p_for_sharing
2454 true, // payload_state_allow_p2p_attempt
2455 true, // expect_p2p_client_lookup
2456 "http://1.3.5.7/p2p", // p2p_client_result_url
2457 true, // expected_allow_p2p_for_downloading
2458 true, // expected_allow_p2p_for_sharing
2459 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002460}
2461
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002462TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002463 P2PTest(true, // initial_allow_p2p_for_downloading
2464 true, // initial_allow_p2p_for_sharing
2465 false, // omaha_disable_p2p_for_downloading
2466 false, // omaha_disable_p2p_for_sharing
2467 true, // payload_state_allow_p2p_attempt
2468 true, // expect_p2p_client_lookup
2469 "", // p2p_client_result_url
2470 false, // expected_allow_p2p_for_downloading
2471 true, // expected_allow_p2p_for_sharing
2472 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002473}
2474
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002475TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002476 P2PTest(false, // initial_allow_p2p_for_downloading
2477 true, // initial_allow_p2p_for_sharing
2478 false, // omaha_disable_p2p_for_downloading
2479 false, // omaha_disable_p2p_for_sharing
2480 true, // payload_state_allow_p2p_attempt
2481 false, // expect_p2p_client_lookup
2482 "unset", // p2p_client_result_url
2483 false, // expected_allow_p2p_for_downloading
2484 true, // expected_allow_p2p_for_sharing
2485 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002486}
2487
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002488TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002489 P2PTest(true, // initial_allow_p2p_for_downloading
2490 true, // initial_allow_p2p_for_sharing
2491 true, // omaha_disable_p2p_for_downloading
2492 false, // omaha_disable_p2p_for_sharing
2493 true, // payload_state_allow_p2p_attempt
2494 false, // expect_p2p_client_lookup
2495 "unset", // p2p_client_result_url
2496 false, // expected_allow_p2p_for_downloading
2497 true, // expected_allow_p2p_for_sharing
2498 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002499}
2500
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002501TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002502 P2PTest(true, // initial_allow_p2p_for_downloading
2503 true, // initial_allow_p2p_for_sharing
2504 false, // omaha_disable_p2p_for_downloading
2505 true, // omaha_disable_p2p_for_sharing
2506 true, // payload_state_allow_p2p_attempt
2507 true, // expect_p2p_client_lookup
2508 "http://1.3.5.7/p2p", // p2p_client_result_url
2509 true, // expected_allow_p2p_for_downloading
2510 false, // expected_allow_p2p_for_sharing
2511 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002512}
2513
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002514TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002515 P2PTest(true, // initial_allow_p2p_for_downloading
2516 true, // initial_allow_p2p_for_sharing
2517 true, // omaha_disable_p2p_for_downloading
2518 true, // omaha_disable_p2p_for_sharing
2519 true, // payload_state_allow_p2p_attempt
2520 false, // expect_p2p_client_lookup
2521 "unset", // p2p_client_result_url
2522 false, // expected_allow_p2p_for_downloading
2523 false, // expected_allow_p2p_for_sharing
2524 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002525}
2526
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002527bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2528 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002529 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002530 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2531
2532 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002533}
2534
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002535TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002536 // Simulate a successful update check that happens during OOBE. The
2537 // deadline in the response is needed to force the update attempt to
2538 // occur; responses without a deadline seen during OOBE will normally
2539 // return ErrorCode::kNonCriticalUpdateInOOBE.
2540 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2541 fake_update_response_.deadline = "20101020";
2542
David Zeuthen639aa362014-02-03 16:23:44 -08002543 // Check that we parse elapsed_days in the Omaha Response correctly.
2544 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002545 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2546 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002547 EXPECT_TRUE(response.update_exists);
2548 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002549 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002550 int64_t prefs_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, 42);
2553
2554 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002555 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002556 EXPECT_TRUE(response.update_exists);
2557 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002558 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002559 EXPECT_EQ(prefs_days, 42);
2560
2561 // Note that elapsed_days is not necessarily divisible by 7 so check
2562 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002563 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2564 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002565 EXPECT_TRUE(response.update_exists);
2566 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002567 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002568 EXPECT_EQ(prefs_days, 21);
2569
2570 // Check that we correctly handle elapsed_days not being included in
2571 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002572 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002573 EXPECT_TRUE(response.update_exists);
2574 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002575}
2576
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002577// If there is no prefs and OOBE is not complete, we should not
2578// report anything to Omaha.
2579TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002580 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002581 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2582 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2583}
David Zeuthen639aa362014-02-03 16:23:44 -08002584
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002585// If OOBE is complete and happened on a valid date (e.g. after Jan
2586// 1 2007 0:00 PST), that date should be used and written to
2587// prefs. However, first try with an invalid date and check we do
2588// nothing.
2589TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2590 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2591 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2592 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2593 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2594}
David Zeuthen639aa362014-02-03 16:23:44 -08002595
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002596// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2597// should yield an InstallDate of 14.
2598TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2599 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2600 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2601 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2602 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002603
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002604 int64_t prefs_days;
2605 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2606 EXPECT_EQ(prefs_days, 14);
2607}
David Zeuthen639aa362014-02-03 16:23:44 -08002608
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002609// Now that we have a valid date in prefs, check that we keep using
2610// that even if OOBE date reports something else. The date Jan 30,
2611// 2007 0:00 PST should yield an InstallDate of 28... but since
2612// there's a prefs file, we should still get 14.
2613TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2614 // Set a valid date in the prefs first.
2615 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002616
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002617 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2618 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2619 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002620
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002621 int64_t prefs_days;
2622 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2623 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002624
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002625 // If we delete the prefs file, we should get 28 days.
2626 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2627 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2628 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2629 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002630}
2631
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002632// Verifies that a device with no device policy, and is not a consumer
2633// device sets the max kernel key version to the current version.
2634// ie. the same behavior as if rollback is enabled.
2635TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2636 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2637
2638 // Setup and verify some initial default values for the kernel TPM
2639 // values that control verified boot and rollback.
2640 const int min_kernel_version = 4;
2641 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2642 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2643 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2644 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2645
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002646 EXPECT_CALL(
2647 *fake_system_state_.mock_metrics_reporter(),
2648 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2649 .Times(1);
2650
Amin Hassani41ac04b2019-03-29 11:31:03 -07002651 fake_update_response_.deadline = "20101020";
2652 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2653 tuc_params_.is_consumer_device = false;
2654 tuc_params_.rollback_allowed_milestones = 3;
2655
2656 EXPECT_TRUE(TestUpdateCheck());
2657 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002658
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002659 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002660 // kernel key version. This has the effect of freezing roll
2661 // forwards indefinitely. This will hold the rollback window
2662 // open until a future change will be able to move this forward
2663 // relative the configured window.
2664 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2665 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2666}
2667
2668// Verifies that a conmsumer device with no device policy sets the
2669// max kernel key version to the current version. ie. the same
2670// behavior as if rollback is enabled.
2671TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2672 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2673
2674 // Setup and verify some initial default values for the kernel TPM
2675 // values that control verified boot and rollback.
2676 const int min_kernel_version = 3;
2677 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2678 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2679 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2680 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2681
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002682 EXPECT_CALL(
2683 *fake_system_state_.mock_metrics_reporter(),
2684 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2685 .Times(1);
2686
Amin Hassani41ac04b2019-03-29 11:31:03 -07002687 fake_update_response_.deadline = "20101020";
2688 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2689 tuc_params_.is_consumer_device = true;
2690 tuc_params_.rollback_allowed_milestones = 3;
2691
2692 EXPECT_TRUE(TestUpdateCheck());
2693 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002694
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002695 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002696 // was set to logical infinity. This is the expected behavior for
2697 // consumer devices and matches the existing behavior prior to the
2698 // rollback features.
2699 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2700 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2701}
2702
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002703// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002704// in the TPM to prevent roll forward.
2705TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2706 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2707
2708 // Setup and verify some initial default values for the kernel TPM
2709 // values that control verified boot and rollback.
2710 const int allowed_milestones = 4;
2711 const int min_kernel_version = 3;
2712 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2713 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2714 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2715 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2716
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002717 EXPECT_CALL(
2718 *fake_system_state_.mock_metrics_reporter(),
2719 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2720 .Times(1);
2721
Amin Hassani41ac04b2019-03-29 11:31:03 -07002722 fake_update_response_.deadline = "20101020";
2723 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2724 tuc_params_.is_consumer_device = false;
2725 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2726 tuc_params_.is_policy_loaded = true;
2727
2728 EXPECT_TRUE(TestUpdateCheck());
2729 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002730
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002731 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002732 // was set to the current minimum kernel key version. This has
2733 // the effect of freezing roll forwards indefinitely. This will
2734 // hold the rollback window open until a future change will
2735 // be able to move this forward relative the configured window.
2736 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2737 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2738}
2739
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002740// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002741// in the TPM to logical infinity, to allow roll forward.
2742TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2743 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2744
2745 // Setup and verify some initial default values for the kernel TPM
2746 // values that control verified boot and rollback.
2747 const int allowed_milestones = 0;
2748 const int min_kernel_version = 3;
2749 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2750 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2751 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2752 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2753
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002754 EXPECT_CALL(
2755 *fake_system_state_.mock_metrics_reporter(),
2756 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2757 .Times(1);
2758
Amin Hassani41ac04b2019-03-29 11:31:03 -07002759 fake_update_response_.deadline = "20101020";
2760 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2761 tuc_params_.is_consumer_device = false;
2762 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2763 tuc_params_.is_policy_loaded = true;
2764
2765 EXPECT_TRUE(TestUpdateCheck());
2766 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002767
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002768 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002769 // was set to logical infinity.
2770 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2771 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2772}
2773
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002774TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002775 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002776 fake_update_response_.deadline = "20101020";
2777 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2778 tuc_params_.is_consumer_device = false;
2779 tuc_params_.rollback_allowed_milestones = 4;
2780 tuc_params_.is_policy_loaded = true;
2781
2782 EXPECT_TRUE(TestUpdateCheck());
2783 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002784 EXPECT_TRUE(response.is_rollback);
2785}
2786
2787TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002788 fake_update_response_.rollback_firmware_version = "1.2";
2789 fake_update_response_.rollback_kernel_version = "3.4";
2790 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002791 fake_update_response_.deadline = "20101020";
2792 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2793 tuc_params_.is_consumer_device = false;
2794 tuc_params_.rollback_allowed_milestones = 4;
2795 tuc_params_.is_policy_loaded = true;
2796
2797 EXPECT_TRUE(TestUpdateCheck());
2798 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002799 EXPECT_TRUE(response.is_rollback);
2800 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2801 EXPECT_EQ(2, response.rollback_key_version.firmware);
2802 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2803 EXPECT_EQ(4, response.rollback_key_version.kernel);
2804}
2805
May Lippert60aa3ca2018-08-15 16:55:29 -07002806TEST_F(OmahaRequestActionTest,
2807 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2808 FakeClock fake_clock;
2809 Time now = Time::Now();
2810 fake_clock.SetWallclockTime(now);
2811 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002812 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002813
Amin Hassani41ac04b2019-03-29 11:31:03 -07002814 ASSERT_TRUE(TestUpdateCheck());
2815
May Lippert60aa3ca2018-08-15 16:55:29 -07002816 EXPECT_TRUE(response.update_exists);
2817 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2818
2819 int64_t stored_first_seen_at_time;
2820 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2821 &stored_first_seen_at_time));
2822 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2823}
2824
2825TEST_F(OmahaRequestActionTest,
2826 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2827 FakeClock fake_clock;
2828 Time now = Time::Now();
2829 fake_clock.SetWallclockTime(now);
2830 fake_system_state_.set_clock(&fake_clock);
2831
Amin Hassani41ac04b2019-03-29 11:31:03 -07002832 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2833 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2834 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2835
2836 ASSERT_TRUE(TestUpdateCheck());
2837
May Lippert60aa3ca2018-08-15 16:55:29 -07002838 EXPECT_FALSE(response.update_exists);
2839 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2840}
2841
Xiaochu Liu88d90382018-08-29 16:09:11 -07002842TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002843 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002844 request_params_.set_dlc_apps_params(
2845 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2846 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002847 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2848
2849 ASSERT_TRUE(TestUpdateCheck());
2850
Andrewe045aef2020-01-08 16:29:22 -08002851 for (const auto& it : request_params_.dlc_apps_params()) {
2852 EXPECT_NE(string::npos, post_str.find("appid=\"" + it.first + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002853 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002854 EXPECT_NE(string::npos,
2855 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2856
2857 // Count number of updatecheck tag in response.
2858 int updatecheck_count = 0;
2859 size_t pos = 0;
2860 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2861 updatecheck_count++;
2862 pos++;
2863 }
Andrewe045aef2020-01-08 16:29:22 -08002864 EXPECT_EQ(request_params_.dlc_apps_params().size(), updatecheck_count);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002865 EXPECT_TRUE(response.update_exists);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002866}
2867
2868TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2869 fake_update_response_.multi_app_skip_updatecheck = true;
2870 fake_update_response_.multi_app_no_update = false;
2871 request_params_.set_is_install(true);
Andrewe045aef2020-01-08 16:29:22 -08002872 request_params_.set_dlc_apps_params(
2873 {{request_params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
2874 {request_params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002875 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002876 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2877
2878 ASSERT_TRUE(TestUpdateCheck());
2879
Xiaochu Liu6310be62018-10-11 15:09:03 -07002880 EXPECT_TRUE(response.update_exists);
2881 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002882}
2883
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002884TEST_F(OmahaRequestActionTest, UpdateWithDlcTest) {
2885 request_params_.set_dlc_apps_params(
2886 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
2887 fake_update_response_.dlc_app_update = true;
2888 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002889 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002890 ASSERT_TRUE(TestUpdateCheck());
2891
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002892 EXPECT_EQ(response.packages.size(), 2u);
2893 // Two candidate URLs.
2894 EXPECT_EQ(response.packages[1].payload_urls.size(), 2u);
2895 EXPECT_TRUE(response.update_exists);
2896}
2897
2898TEST_F(OmahaRequestActionTest, UpdateWithPartiallyExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002899 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2900 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002901 fake_update_response_.dlc_app_update = true;
2902 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2903 // The first DLC candidate URL is excluded.
2904 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2905 .WillOnce(Return(true))
2906 .WillOnce(Return(false));
2907 ASSERT_TRUE(TestUpdateCheck());
2908
2909 EXPECT_EQ(response.packages.size(), 2u);
2910 // One candidate URL.
2911 EXPECT_EQ(response.packages[1].payload_urls.size(), 1u);
2912 EXPECT_TRUE(response.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002913 EXPECT_TRUE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002914}
2915
2916TEST_F(OmahaRequestActionTest, UpdateWithExcludedDlcTest) {
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002917 const string kDlcAppId = request_params_.GetDlcAppId(kDlcId1);
2918 request_params_.set_dlc_apps_params({{kDlcAppId, {.name = kDlcId1}}});
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002919 fake_update_response_.dlc_app_update = true;
2920 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2921 // Both DLC candidate URLs are excluded.
2922 EXPECT_CALL(mock_excluder_, IsExcluded(_))
2923 .WillOnce(Return(true))
2924 .WillOnce(Return(true));
2925 ASSERT_TRUE(TestUpdateCheck());
2926
2927 EXPECT_EQ(response.packages.size(), 1u);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002928 EXPECT_TRUE(response.update_exists);
Jae Hoon Kim3e69b4c2020-06-16 09:23:39 -07002929 EXPECT_FALSE(request_params_.dlc_apps_params().at(kDlcAppId).updated);
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002930}
2931
2932TEST_F(OmahaRequestActionTest, UpdateWithDeprecatedDlcTest) {
2933 request_params_.set_dlc_apps_params(
2934 {{request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2935 fake_update_response_.dlc_app_no_update = true;
2936 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002937 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002938 ASSERT_TRUE(TestUpdateCheck());
2939
2940 EXPECT_TRUE(response.update_exists);
2941}
2942
2943TEST_F(OmahaRequestActionTest, UpdateWithDlcAndDeprecatedDlcTest) {
2944 request_params_.set_dlc_apps_params(
2945 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}},
2946 {request_params_.GetDlcAppId(kDlcId2), {.name = kDlcId2}}});
2947 fake_update_response_.dlc_app_update = true;
2948 fake_update_response_.dlc_app_no_update = true;
2949 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07002950 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
Jae Hoon Kim5e9cd712020-02-12 15:28:28 -08002951 ASSERT_TRUE(TestUpdateCheck());
2952
2953 EXPECT_TRUE(response.update_exists);
2954}
2955
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002956TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002957 fake_update_response_.rollback = true;
2958 fake_update_response_.rollback_allowed_milestones = 4;
2959 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002960 fake_update_response_.deadline = "20101020";
2961 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2962 tuc_params_.is_consumer_device = false;
2963 tuc_params_.rollback_allowed_milestones = 4;
2964 tuc_params_.is_policy_loaded = true;
2965
2966 EXPECT_TRUE(TestUpdateCheck());
2967 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002968 EXPECT_TRUE(response.is_rollback);
2969 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2970 response.past_rollback_key_version.firmware_key);
2971 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2972 response.past_rollback_key_version.firmware);
2973 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2974 response.past_rollback_key_version.kernel_key);
2975 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2976 response.past_rollback_key_version.kernel);
2977}
2978
2979TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002980 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002981 fake_update_response_.rollback = true;
2982 fake_update_response_.rollback_allowed_milestones = 4;
2983 fake_update_response_.rollback_firmware_version = "4.3";
2984 fake_update_response_.rollback_kernel_version = "2.1";
2985 fake_update_response_.past_rollback_key_version =
2986 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002987 fake_update_response_.deadline = "20101020";
2988 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2989 tuc_params_.is_consumer_device = false;
2990 tuc_params_.rollback_allowed_milestones = 4;
2991 tuc_params_.is_policy_loaded = true;
2992
2993 EXPECT_TRUE(TestUpdateCheck());
2994 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002995 EXPECT_TRUE(response.is_rollback);
2996 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2997 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2998 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2999 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
3000}
3001
3002TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003003 fake_update_response_.rollback = true;
3004 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07003005 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003006 request_params_.set_rollback_allowed_milestones(4);
3007
3008 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
3009 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
3010 // to 2, OmahaRequestAction will look for the key values of N-4 version but
3011 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07003012 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3013 tuc_params_.is_consumer_device = false;
3014 tuc_params_.rollback_allowed_milestones = 2;
3015 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003016
Amin Hassani41ac04b2019-03-29 11:31:03 -07003017 EXPECT_TRUE(TestUpdateCheck());
3018 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07003019 EXPECT_TRUE(response.is_rollback);
3020 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3021 response.past_rollback_key_version.firmware_key);
3022 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3023 response.past_rollback_key_version.firmware);
3024 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3025 response.past_rollback_key_version.kernel_key);
3026 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
3027 response.past_rollback_key_version.kernel);
3028}
3029
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04003030TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
3031 request_params_.set_device_requisition("remora");
3032 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3033 ASSERT_TRUE(TestUpdateCheck());
3034 EXPECT_NE(string::npos, post_str.find("requisition=\"remora\""));
3035}
3036
3037TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
3038 request_params_.set_device_requisition("");
3039 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3040 ASSERT_TRUE(TestUpdateCheck());
3041 EXPECT_EQ(string::npos, post_str.find("requisition"));
3042}
3043
Amin Hassani2cbb0692019-10-30 13:36:17 -07003044TEST_F(OmahaRequestActionTest, PersistEolDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003045 tuc_params_.http_response =
3046 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3047 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3048 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003049 "_eol_date=\"200\" _foo=\"bar\"/></app></response>";
3050 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3051 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3052
3053 ASSERT_TRUE(TestUpdateCheck());
3054
3055 string eol_date;
3056 EXPECT_TRUE(
3057 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
3058 EXPECT_EQ("200", eol_date);
3059}
3060
3061TEST_F(OmahaRequestActionTest, PersistEolMissingDateTest) {
3062 tuc_params_.http_response =
3063 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3064 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3065 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003066 "_foo=\"bar\"/></app></response>";
3067 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3068 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3069
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003070 const string kDate = "123";
3071 fake_system_state_.prefs()->SetString(kPrefsOmahaEolDate, kDate);
3072
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003073 ASSERT_TRUE(TestUpdateCheck());
3074
Amin Hassani2cbb0692019-10-30 13:36:17 -07003075 string eol_date;
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003076 EXPECT_TRUE(
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003077 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
Jae Hoon Kimb3146742019-11-07 13:24:24 -08003078 EXPECT_EQ(kDate, eol_date);
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003079}
3080
Amin Hassani2cbb0692019-10-30 13:36:17 -07003081TEST_F(OmahaRequestActionTest, PersistEolBadDateTest) {
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003082 tuc_params_.http_response =
3083 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
3084 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
3085 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
Amin Hassani2cbb0692019-10-30 13:36:17 -07003086 "_eol_date=\"bad\" foo=\"bar\"/></app></response>";
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003087 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
3088 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
3089
3090 ASSERT_TRUE(TestUpdateCheck());
3091
Amin Hassani2cbb0692019-10-30 13:36:17 -07003092 string eol_date;
Jae Hoon Kim051627a2019-09-03 12:56:32 -07003093 EXPECT_TRUE(
3094 fake_system_state_.prefs()->GetString(kPrefsOmahaEolDate, &eol_date));
3095 EXPECT_EQ(kEolDateInvalid, StringToEolDate(eol_date));
3096}
3097
Andrew065d78d2020-04-07 15:43:07 -07003098TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyNoPing) {
3099 OmahaRequestParams::AppParams app_param = {.name = dlc_id_};
Andrewe045aef2020-01-08 16:29:22 -08003100 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003101 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003102
3103 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003104
3105 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003106 // If there was no ping, the metadata files shouldn't exist yet.
Andrew065d78d2020-04-07 15:43:07 -07003107 EXPECT_FALSE(fake_prefs_.GetInt64(active_key_, &temp_int));
3108 EXPECT_FALSE(fake_prefs_.GetInt64(last_active_key_, &temp_int));
3109 EXPECT_FALSE(fake_prefs_.GetInt64(last_rollcall_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003110}
3111
Andrew065d78d2020-04-07 15:43:07 -07003112TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyActiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003113 // Create Active value
Andrew065d78d2020-04-07 15:43:07 -07003114 fake_prefs_.SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003115
3116 OmahaRequestParams::AppParams app_param = {
3117 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003118 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003119 .ping_active = 1,
3120 .send_ping = true};
3121 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003122 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003123
Andrew065d78d2020-04-07 15:43:07 -07003124 int64_t temp_int;
3125 string temp_str;
Andrewe045aef2020-01-08 16:29:22 -08003126 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003127 EXPECT_TRUE(fake_prefs_.GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003128 EXPECT_EQ(temp_int, kPingInactiveValue);
Andrew065d78d2020-04-07 15:43:07 -07003129 EXPECT_TRUE(fake_prefs_.GetString(last_active_key_, &temp_str));
3130 EXPECT_EQ(temp_str, "4763");
3131 EXPECT_TRUE(fake_prefs_.GetString(last_rollcall_key_, &temp_str));
3132 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003133}
3134
Andrew065d78d2020-04-07 15:43:07 -07003135TEST_F(OmahaRequestActionDlcPingTest, StorePingReplyInactiveTest) {
Andrewe045aef2020-01-08 16:29:22 -08003136 // Create Active value
Andrew065d78d2020-04-07 15:43:07 -07003137 fake_prefs_.SetInt64(active_key_, 0);
Andrewe045aef2020-01-08 16:29:22 -08003138
3139 OmahaRequestParams::AppParams app_param = {
3140 .active_counting_type = OmahaRequestParams::kDateBased,
Andrew065d78d2020-04-07 15:43:07 -07003141 .name = dlc_id_,
Andrewe045aef2020-01-08 16:29:22 -08003142 .ping_active = 0,
3143 .send_ping = true};
3144 request_params_.set_dlc_apps_params(
Andrew065d78d2020-04-07 15:43:07 -07003145 {{request_params_.GetDlcAppId(dlc_id_), app_param}});
Andrewe045aef2020-01-08 16:29:22 -08003146
3147 // Set the previous active value to an older value than 4763.
Andrew065d78d2020-04-07 15:43:07 -07003148 fake_prefs_.SetString(last_active_key_, "555");
Andrewe045aef2020-01-08 16:29:22 -08003149
Andrew065d78d2020-04-07 15:43:07 -07003150 int64_t temp_int;
Andrewe045aef2020-01-08 16:29:22 -08003151 ASSERT_TRUE(TestUpdateCheck());
Andrew065d78d2020-04-07 15:43:07 -07003152 EXPECT_TRUE(fake_prefs_.GetInt64(active_key_, &temp_int));
Andrewe045aef2020-01-08 16:29:22 -08003153 EXPECT_EQ(temp_int, kPingInactiveValue);
Andrew065d78d2020-04-07 15:43:07 -07003154 string temp_str;
3155 EXPECT_TRUE(fake_prefs_.GetString(last_active_key_, &temp_str));
3156 EXPECT_EQ(temp_str, "555");
3157 EXPECT_TRUE(fake_prefs_.GetString(last_rollcall_key_, &temp_str));
3158 EXPECT_EQ(temp_str, "4763");
Andrewe045aef2020-01-08 16:29:22 -08003159}
3160
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07003161TEST_F(OmahaRequestActionTest, OmahaResponseUpdateCanExcludeCheck) {
3162 request_params_.set_dlc_apps_params(
3163 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3164 fake_update_response_.dlc_app_update = true;
3165 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3166
3167 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3168 ASSERT_TRUE(TestUpdateCheck());
3169 ASSERT_TRUE(delegate_.omaha_response_);
3170 const auto& packages = delegate_.omaha_response_->packages;
3171 ASSERT_EQ(packages.size(), 2);
3172
3173 EXPECT_FALSE(packages[0].can_exclude);
3174 EXPECT_TRUE(packages[1].can_exclude);
3175}
3176
3177TEST_F(OmahaRequestActionTest, OmahaResponseInstallCannotExcludeCheck) {
3178 request_params_.set_is_install(true);
3179 request_params_.set_dlc_apps_params(
3180 {{request_params_.GetDlcAppId(kDlcId1), {.name = kDlcId1}}});
3181 fake_update_response_.dlc_app_update = true;
3182 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
3183
3184 EXPECT_CALL(mock_excluder_, IsExcluded(_)).WillRepeatedly(Return(false));
3185 ASSERT_TRUE(TestUpdateCheck());
3186 ASSERT_TRUE(delegate_.omaha_response_);
3187 const auto& packages = delegate_.omaha_response_->packages;
3188 ASSERT_EQ(packages.size(), 2);
3189
3190 EXPECT_FALSE(packages[0].can_exclude);
3191 EXPECT_FALSE(packages[1].can_exclude);
3192}
3193
Darin Petkov6a5b3222010-07-13 14:55:28 -07003194} // namespace chromeos_update_engine