blob: e13d10e9da90832d7334546b3da0fcf0ade36e50 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov6a5b3222010-07-13 14:55:28 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_action.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070021#include <limits>
Ben Chanab5a0af2017-10-12 14:57:50 -070022#include <memory>
Darin Petkov6a5b3222010-07-13 14:55:28 -070023#include <string>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080024#include <utility>
Darin Petkov6a5b3222010-07-13 14:55:28 -070025#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070026
Alex Deymo60ca1a72015-06-18 18:19:15 -070027#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070028#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070029#include <base/files/scoped_temp_dir.h>
Ben Chan5c02c132017-06-27 07:10:36 -070030#include <base/memory/ptr_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070031#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070032#include <base/strings/string_util.h>
33#include <base/strings/stringprintf.h>
34#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070035#include <brillo/message_loops/fake_message_loop.h>
36#include <brillo/message_loops/message_loop.h>
37#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070038#include <gtest/gtest.h>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080039#include <policy/libpolicy.h>
40#include <policy/mock_libpolicy.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070041
Alex Deymo39910dc2015-11-09 17:04:30 -080042#include "update_engine/common/action_pipe.h"
43#include "update_engine/common/constants.h"
44#include "update_engine/common/fake_prefs.h"
45#include "update_engine/common/hash_calculator.h"
46#include "update_engine/common/mock_http_fetcher.h"
47#include "update_engine/common/platform_constants.h"
48#include "update_engine/common/prefs.h"
49#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030050#include "update_engine/fake_system_state.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070051#include "update_engine/metrics_reporter_interface.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070052#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070053#include "update_engine/mock_payload_state.h"
Amin Hassani7fca2862019-03-28 16:09:22 -070054#include "update_engine/omaha_request_builder_xml.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055#include "update_engine/omaha_request_params.h"
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080056#include "update_engine/update_manager/rollback_prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070057
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058using base::Time;
59using base::TimeDelta;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080060using chromeos_update_manager::kRollforwardInfinity;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070061using std::pair;
Darin Petkov6a5b3222010-07-13 14:55:28 -070062using std::string;
63using std::vector;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080064using testing::_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070066using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070067using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070068using testing::Ge;
69using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080070using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070071using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070072using testing::ReturnPointee;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080073using testing::ReturnRef;
Gilad Arnold74b5f552014-10-07 08:17:16 -070074using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070075using testing::SetArgPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070076
Alex Deymo8e18f932015-03-27 16:16:59 -070077namespace {
78
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080079static_assert(kRollforwardInfinity == 0xfffffffe,
80 "Don't change the value of kRollforward infinity unless its "
81 "size has been changed in firmware.");
82
Xiaochu Liu6310be62018-10-11 15:09:03 -070083const char kCurrentVersion[] = "0.1.0.0";
Alex Deymo85616652015-10-15 18:48:31 -070084const char kTestAppId[] = "test-app-id";
Aaron Wood7dcdedf2017-09-06 17:17:41 -070085const char kTestAppId2[] = "test-app2-id";
Xiaochu Liu6310be62018-10-11 15:09:03 -070086const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck";
Jae Hoon Kimedb65502019-06-14 11:52:17 -070087const char kTestSessionId[] = "12341234-1234-1234-1234-1234123412341234";
Alex Deymo85616652015-10-15 18:48:31 -070088
Alex Deymo8e18f932015-03-27 16:16:59 -070089// This is a helper struct to allow unit tests build an update response with the
90// values they care about.
91struct FakeUpdateResponse {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070092 string GetRollbackVersionAttributes() const {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070093 string num_milestones;
94#if BASE_VER < 576279
95 num_milestones = base::IntToString(rollback_allowed_milestones);
96#else
97 num_milestones = base::NumberToString(rollback_allowed_milestones);
98#endif
99 const string rollback_version =
100 " _firmware_version_" + num_milestones + "=\"" +
101 past_rollback_key_version.first + "\"" + " _kernel_version_" +
102 num_milestones + "=\"" + past_rollback_key_version.second + "\"";
103
104 return (rollback ? " _rollback=\"true\"" : "") + rollback_version +
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700105 (!rollback_firmware_version.empty()
106 ? " _firmware_version=\"" + rollback_firmware_version + "\""
107 : "") +
108 (!rollback_kernel_version.empty()
109 ? " _kernel_version=\"" + rollback_kernel_version + "\""
110 : "");
111 }
112
Alex Deymo8e18f932015-03-27 16:16:59 -0700113 string GetNoUpdateResponse() const {
114 string entity_str;
115 if (include_entity)
116 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Sen Jiang81259682017-03-30 15:11:30 -0700117 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str +
118 "<response protocol=\"3.0\">"
119 "<daystart elapsed_seconds=\"100\"/>"
120 "<app appid=\"" +
121 app_id + "\" " +
122 (include_cohorts
123 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
124 "\" cohortname=\"" + cohortname + "\" "
125 : "") +
126 " status=\"ok\">"
127 "<ping status=\"ok\"/>"
128 "<updatecheck status=\"noupdate\"/></app>" +
129 (multi_app_no_update
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700130 ? "<app appid=\"" + app_id2 +
131 "\"><updatecheck status=\"noupdate\"/></app>"
Sen Jiang81259682017-03-30 15:11:30 -0700132 : "") +
133 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700134 }
135
136 string GetUpdateResponse() const {
Sen Jiang2703ef42017-03-16 13:36:21 -0700137 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
138 "protocol=\"3.0\">"
139 "<daystart elapsed_seconds=\"100\"" +
140 (elapsed_days.empty() ? ""
141 : (" elapsed_days=\"" + elapsed_days + "\"")) +
142 "/>"
143 "<app appid=\"" +
144 app_id + "\" " +
145 (include_cohorts
146 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
147 "\" cohortname=\"" + cohortname + "\" "
148 : "") +
149 " status=\"ok\">"
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700150 "<ping status=\"ok\"/><updatecheck status=\"ok\"" +
151 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" +
Sen Jiang2703ef42017-03-16 13:36:21 -0700152 codebase +
153 "\"/></urls>"
154 "<manifest version=\"" +
155 version +
156 "\">"
157 "<packages><package hash=\"not-used\" name=\"" +
158 filename + "\" size=\"" + base::Int64ToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800159 "\" hash_sha256=\"" + hash + "\"/>" +
160 (multi_package ? "<package name=\"package2\" size=\"222\" "
161 "hash_sha256=\"hash2\"/>"
162 : "") +
163 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700164 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700165 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
166 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700167 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700168 "IsDeltaPayload=\"true" +
169 (multi_package ? ":false" : "") +
170 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700171 "MaxDaysToScatter=\"" +
172 max_days_to_scatter +
173 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700174 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700175 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
176 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
177 : "") +
178 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700179 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700180 "/></actions></manifest></updatecheck></app>" +
181 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700182 ? "<app appid=\"" + app_id2 + "\"" +
183 (include_cohorts ? " cohort=\"cohort2\"" : "") +
184 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700185 codebase2 + "\"/></urls><manifest version=\"" + version2 +
186 "\"><packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700187 "<package name=\"package3\" size=\"333\" "
188 "hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700189 "<actions><action event=\"postinstall\" " +
190 (multi_app_self_update
191 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
192 : "IsDeltaPayload=\"false\" ") +
193 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700194 "</manifest></updatecheck></app>"
195 : "") +
196 (multi_app_no_update
197 ? "<app><updatecheck status=\"noupdate\"/></app>"
198 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700199 (multi_app_skip_updatecheck
200 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
201 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700202 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700203 }
204
205 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800206 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700207
Alex Deymo85616652015-10-15 18:48:31 -0700208 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700209 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700210 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
211 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700212 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700213 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700214 string more_info_url = "http://more/info";
215 string prompt = "true";
216 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700217 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700218 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700219 string hash = "4841534831323334";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800220 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700221 string deadline = "";
222 string max_days_to_scatter = "7";
223 string elapsed_days = "42";
224
225 // P2P setting defaults to allowed.
226 bool disable_p2p_for_downloading = false;
227 bool disable_p2p_for_sharing = false;
228
Sen Jiangfe284402018-03-21 14:03:50 -0700229 bool powerwash = false;
230
Alex Deymo8e18f932015-03-27 16:16:59 -0700231 // Omaha cohorts settings.
232 bool include_cohorts = false;
233 string cohort = "";
234 string cohorthint = "";
235 string cohortname = "";
236
237 // Whether to include the CrOS <!ENTITY> in the XML response.
238 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800239
Sen Jiang81259682017-03-30 15:11:30 -0700240 // Whether to include more than one app.
241 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700242 // Whether to include an app with noupdate="true".
243 bool multi_app_self_update = false;
244 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700245 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700246 // Whether to include an additional app with no updatecheck tag.
247 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700248 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800249 bool multi_package = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700250
251 // Whether the payload is a rollback.
252 bool rollback = false;
253 // The verified boot firmware key version for the rollback image.
254 string rollback_firmware_version = "";
255 // The verified boot kernel key version for the rollback image.
256 string rollback_kernel_version = "";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700257 // The number of milestones back that the verified boot key version has been
258 // supplied.
259 uint32_t rollback_allowed_milestones = 0;
260 // The verified boot key version for the
261 // |current - rollback_allowed_milestones| most recent release.
262 // The pair contains <firmware_key_version, kernel_key_version> each
263 // of which is in the form "key_version.version".
264 pair<string, string> past_rollback_key_version;
Alex Deymo8e18f932015-03-27 16:16:59 -0700265};
266
267} // namespace
268
Darin Petkov6a5b3222010-07-13 14:55:28 -0700269namespace chromeos_update_engine {
270
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700271class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
272 public:
273 OmahaRequestActionTestProcessorDelegate()
274 : expected_code_(ErrorCode::kSuccess),
275 interactive_(false),
276 test_http_fetcher_headers_(false) {}
277 ~OmahaRequestActionTestProcessorDelegate() override = default;
278
279 void ProcessingDone(const ActionProcessor* processor,
280 ErrorCode code) override {
281 brillo::MessageLoop::current()->BreakLoop();
282 }
283
284 void ActionCompleted(ActionProcessor* processor,
285 AbstractAction* action,
286 ErrorCode code) override {
287 // Make sure actions always succeed.
288 if (action->Type() == OmahaRequestAction::StaticType()) {
289 EXPECT_EQ(expected_code_, code);
290 // Check that the headers were set in the fetcher during the action. Note
291 // that we set this request as "interactive".
292 auto fetcher = static_cast<const MockHttpFetcher*>(
293 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
294
295 if (test_http_fetcher_headers_) {
296 EXPECT_EQ(interactive_ ? "fg" : "bg",
297 fetcher->GetHeader("X-Goog-Update-Interactivity"));
298 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
299 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700300 EXPECT_EQ(kTestSessionId,
301 fetcher->GetHeader("X-Goog-Update-SessionId"));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700302 }
303 post_data_ = fetcher->post_data();
304 } else if (action->Type() ==
305 ObjectCollectorAction<OmahaResponse>::StaticType()) {
306 EXPECT_EQ(ErrorCode::kSuccess, code);
307 auto collector_action =
308 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
309 omaha_response_.reset(new OmahaResponse(collector_action->object()));
310 EXPECT_TRUE(omaha_response_);
311 } else {
312 EXPECT_EQ(ErrorCode::kSuccess, code);
313 }
314 }
315 ErrorCode expected_code_;
316 brillo::Blob post_data_;
317 bool interactive_;
318 bool test_http_fetcher_headers_;
319 std::unique_ptr<OmahaResponse> omaha_response_;
320};
321
Amin Hassani41ac04b2019-03-29 11:31:03 -0700322struct TestUpdateCheckParams {
323 string http_response;
324 int fail_http_response_code;
325 bool ping_only;
326 bool is_consumer_device;
327 int rollback_allowed_milestones;
328 bool is_policy_loaded;
329 ErrorCode expected_code;
330 metrics::CheckResult expected_check_result;
331 metrics::CheckReaction expected_check_reaction;
332 metrics::DownloadErrorCode expected_download_error_code;
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700333 string session_id;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700334};
335
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700336class OmahaRequestActionTest : public ::testing::Test {
337 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800338 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200339 request_params_.set_os_sp("service_pack");
340 request_params_.set_os_board("x86-generic");
341 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700342 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200343 request_params_.set_app_lang("en-US");
344 request_params_.set_current_channel("unittest");
345 request_params_.set_target_channel("unittest");
346 request_params_.set_hwid("OEM MODEL 09235 7471");
347 request_params_.set_fw_version("ChromeOSFirmware.1.0");
348 request_params_.set_ec_version("0X0A1");
349 request_params_.set_delta_okay(true);
350 request_params_.set_interactive(false);
351 request_params_.set_update_url("http://url");
352 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200353 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200354 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700355 request_params_.set_is_install(false);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800356 request_params_.set_dlc_module_ids({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200357
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700358 fake_system_state_.set_request_params(&request_params_);
359 fake_system_state_.set_prefs(&fake_prefs_);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700360
361 // Setting the default update check params. Lookup |TestUpdateCheck()|.
362 tuc_params_ = {
363 .http_response = "",
364 .fail_http_response_code = -1,
365 .ping_only = false,
366 .is_consumer_device = true,
367 .rollback_allowed_milestones = 0,
368 .is_policy_loaded = false,
369 .expected_code = ErrorCode::kSuccess,
370 .expected_check_result = metrics::CheckResult::kUpdateAvailable,
371 .expected_check_reaction = metrics::CheckReaction::kUpdating,
372 .expected_download_error_code = metrics::DownloadErrorCode::kUnset,
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700373 .session_id = kTestSessionId,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700374 };
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700375 }
376
Amin Hassani41ac04b2019-03-29 11:31:03 -0700377 // This function uses the paramets in |tuc_params_| to do an update check. It
378 // will fill out |post_str| with the result data and |response| with
379 // |OmahaResponse|. Returns true iff an output response was obtained from the
380 // |OmahaRequestAction|. If |fail_http_response_code| is non-negative, the
381 // transfer will fail with that code. |ping_only| is passed through to the
382 // |OmahaRequestAction| constructor.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700383 //
384 // The |expected_check_result|, |expected_check_reaction| and
Amin Hassani41ac04b2019-03-29 11:31:03 -0700385 // |expected_error_code| parameters are for checking expectations about
386 // reporting UpdateEngine.Check.{Result,Reaction,DownloadError} UMA
387 // statistics. Use the appropriate ::kUnset value to specify that the given
388 // metric should not be reported.
389 bool TestUpdateCheck();
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700390
Amin Hassani41ac04b2019-03-29 11:31:03 -0700391 // Tests events using |event| and |https_response|. It will fill up |post_str|
392 // with the result data.
393 void TestEvent(OmahaEvent* event, const string& http_response);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700394
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800395 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700396 // only a ping or also an updatecheck.
397 void PingTest(bool ping_only);
398
399 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800400 bool InstallDateParseHelper(const string& elapsed_days,
401 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700402
403 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800404 void P2PTest(bool initial_allow_p2p_for_downloading,
405 bool initial_allow_p2p_for_sharing,
406 bool omaha_disable_p2p_for_downloading,
407 bool omaha_disable_p2p_for_sharing,
408 bool payload_state_allow_p2p_attempt,
409 bool expect_p2p_client_lookup,
410 const string& p2p_client_result_url,
411 bool expected_allow_p2p_for_downloading,
412 bool expected_allow_p2p_for_sharing,
413 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700414
415 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700416 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200417 // Used by all tests.
418 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700419
420 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700421
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700422 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700423
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700424 bool test_http_fetcher_headers_{false};
Amin Hassani41ac04b2019-03-29 11:31:03 -0700425
426 TestUpdateCheckParams tuc_params_;
427
428 // TODO(ahassani): Add trailing _ to these two variables.
429 OmahaResponse response;
430 string post_str;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700431};
432
Amin Hassani41ac04b2019-03-29 11:31:03 -0700433bool OmahaRequestActionTest::TestUpdateCheck() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700434 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700435 loop.SetAsCurrent();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700436 auto fetcher =
437 std::make_unique<MockHttpFetcher>(tuc_params_.http_response.data(),
438 tuc_params_.http_response.size(),
439 nullptr);
440 if (tuc_params_.fail_http_response_code >= 0) {
441 fetcher->FailTransfer(tuc_params_.fail_http_response_code);
Darin Petkovedc522e2010-11-05 09:35:17 -0700442 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200443 // This ensures the tests didn't forget to update fake_system_state_ if they
444 // are not using the default request_params_.
445 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
446
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700447 auto omaha_request_action =
448 std::make_unique<OmahaRequestAction>(&fake_system_state_,
449 nullptr,
450 std::move(fetcher),
451 tuc_params_.ping_only,
452 tuc_params_.session_id);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700453
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800454 auto mock_policy_provider =
455 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
456 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700457 .WillRepeatedly(Return(tuc_params_.is_consumer_device));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800458
459 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
Amin Hassani41ac04b2019-03-29 11:31:03 -0700460 .WillRepeatedly(Return(tuc_params_.is_policy_loaded));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800461
462 const policy::MockDevicePolicy device_policy;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700463 const bool get_allowed_milestone_succeeds =
464 tuc_params_.rollback_allowed_milestones >= 0;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800465 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
Amin Hassani41ac04b2019-03-29 11:31:03 -0700466 .WillRepeatedly(
467 DoAll(SetArgPointee<0>(tuc_params_.rollback_allowed_milestones),
468 Return(get_allowed_milestone_succeeds)));
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800469
470 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
471 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700472 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800473
Amin Hassani41ac04b2019-03-29 11:31:03 -0700474 delegate_.expected_code_ = tuc_params_.expected_code;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700475 delegate_.interactive_ = request_params_.interactive();
476 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700477 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700478 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700479
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700480 auto collector_action =
481 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
482 BondActions(omaha_request_action.get(), collector_action.get());
483 processor.EnqueueAction(std::move(omaha_request_action));
484 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700485
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700486 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
487 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800488 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700489
Amin Hassani41ac04b2019-03-29 11:31:03 -0700490 EXPECT_CALL(
491 *fake_system_state_.mock_metrics_reporter(),
492 ReportUpdateCheckMetrics(_,
493 tuc_params_.expected_check_result,
494 tuc_params_.expected_check_reaction,
495 tuc_params_.expected_download_error_code))
496 .Times(tuc_params_.ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800497
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700498 loop.PostTask(base::Bind(
499 [](ActionProcessor* processor) { processor->StartProcessing(); },
500 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700501 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700502 EXPECT_FALSE(loop.PendingTasks());
Amin Hassani41ac04b2019-03-29 11:31:03 -0700503 if (delegate_.omaha_response_)
504 response = *delegate_.omaha_response_;
505 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700506 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700507}
508
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700509// Tests Event requests -- they should always succeed. |out_post_data| may be
510// null; if non-null, the post-data received by the mock HttpFetcher is
511// returned.
512void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
Amin Hassani41ac04b2019-03-29 11:31:03 -0700513 const string& http_response) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700514 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700515 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700516
517 auto action = std::make_unique<OmahaRequestAction>(
518 &fake_system_state_,
519 event,
520 std::make_unique<MockHttpFetcher>(
521 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -0700522 false,
523 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700524 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700525 processor.set_delegate(&delegate_);
526 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700527
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700528 loop.PostTask(base::Bind(
529 [](ActionProcessor* processor) { processor->StartProcessing(); },
530 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700531 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700532 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700533
Amin Hassani41ac04b2019-03-29 11:31:03 -0700534 post_str = string(delegate_.post_data_.begin(), delegate_.post_data_.end());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700535}
536
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700537TEST_F(OmahaRequestActionTest, RejectEntities) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700538 fake_update_response_.include_entity = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700539 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
540 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLHasEntityDecl;
541 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
542 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
543
544 ASSERT_FALSE(TestUpdateCheck());
David Zeuthenf3e28012014-08-26 18:23:52 -0400545 EXPECT_FALSE(response.update_exists);
546}
547
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700548TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700549 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
550 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
551 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
552
553 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700554 EXPECT_FALSE(response.update_exists);
555}
556
Sen Jiang81259682017-03-30 15:11:30 -0700557TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700558 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700559 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
560 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
561 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
562
563 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang81259682017-03-30 15:11:30 -0700564 EXPECT_FALSE(response.update_exists);
565}
566
Sen Jiang00adf7b2017-06-26 15:57:29 -0700567TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700568 fake_update_response_.multi_app_no_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700569 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
570 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
571 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
572
573 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700574 EXPECT_FALSE(response.update_exists);
575}
576
577TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -0700578 tuc_params_.http_response =
Sen Jiang00adf7b2017-06-26 15:57:29 -0700579 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
580 "event=\"postinstall\" noupdate=\"true\"/></actions>"
Amin Hassani41ac04b2019-03-29 11:31:03 -0700581 "</manifest></updatecheck></app></response>";
582 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
583 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
584
585 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang00adf7b2017-06-26 15:57:29 -0700586 EXPECT_FALSE(response.update_exists);
587}
588
Alex Deymo8e18f932015-03-27 16:16:59 -0700589// Test that all the values in the response are parsed in a normal update
590// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700591TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700592 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700593 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
594
595 ASSERT_TRUE(TestUpdateCheck());
596
Darin Petkov6a5b3222010-07-13 14:55:28 -0700597 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700598 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700599 EXPECT_EQ("", response.system_version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800600 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
601 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700602 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800603 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
604 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700605 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700606 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
607 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700608 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700609 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700610 // persisted.
611 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
612 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
613 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700614}
615
Sen Jiang0affc2c2017-02-10 15:55:05 -0800616TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800617 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700618 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
619
620 ASSERT_TRUE(TestUpdateCheck());
621
Sen Jiang0affc2c2017-02-10 15:55:05 -0800622 EXPECT_TRUE(response.update_exists);
623 EXPECT_EQ(fake_update_response_.version, response.version);
624 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
625 response.packages[0].payload_urls[0]);
626 EXPECT_EQ(fake_update_response_.codebase + "package2",
627 response.packages[1].payload_urls[0]);
628 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
629 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700630 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700631 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800632 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700633 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800634 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700635 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700636 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700637}
638
639TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700640 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700641 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
642
643 ASSERT_TRUE(TestUpdateCheck());
644
Sen Jiang81259682017-03-30 15:11:30 -0700645 EXPECT_TRUE(response.update_exists);
646 EXPECT_EQ(fake_update_response_.version, response.version);
647 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
648 response.packages[0].payload_urls[0]);
649 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
650 response.packages[1].payload_urls[0]);
651 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
652 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
653 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700654 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700655 ASSERT_EQ(2u, response.packages.size());
656 EXPECT_EQ(string("hash3"), response.packages[1].hash);
657 EXPECT_EQ(333u, response.packages[1].size);
658 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700659 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700660}
661
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700662TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) {
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700663 fake_update_response_.multi_app = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700664 // Trigger the lining up of the app and system versions.
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700665 request_params_.set_system_app_id(fake_update_response_.app_id2);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700666 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700667
Amin Hassani41ac04b2019-03-29 11:31:03 -0700668 ASSERT_TRUE(TestUpdateCheck());
669
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700670 EXPECT_TRUE(response.update_exists);
671 EXPECT_EQ(fake_update_response_.version, response.version);
672 EXPECT_EQ(fake_update_response_.version2, response.system_version);
673 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
674 response.packages[0].payload_urls[0]);
675 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
676 response.packages[1].payload_urls[0]);
677 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
678 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
679 EXPECT_EQ(11u, response.packages[0].metadata_size);
680 EXPECT_EQ(true, response.packages[0].is_delta);
681 ASSERT_EQ(2u, response.packages.size());
682 EXPECT_EQ(string("hash3"), response.packages[1].hash);
683 EXPECT_EQ(333u, response.packages[1].size);
684 EXPECT_EQ(33u, response.packages[1].metadata_size);
685 EXPECT_EQ(false, response.packages[1].is_delta);
686}
687
Sen Jiang81259682017-03-30 15:11:30 -0700688TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
Sen Jiang00adf7b2017-06-26 15:57:29 -0700689 fake_update_response_.multi_app = true;
690 fake_update_response_.multi_app_self_update = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700691 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
692
693 ASSERT_TRUE(TestUpdateCheck());
694
Sen Jiang81259682017-03-30 15:11:30 -0700695 EXPECT_TRUE(response.update_exists);
696 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700697 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700698 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
699 response.packages[0].payload_urls[0]);
700 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
701 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
702 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700703 ASSERT_EQ(2u, response.packages.size());
704 EXPECT_EQ(string("hash3"), response.packages[1].hash);
705 EXPECT_EQ(333u, response.packages[1].size);
706 EXPECT_EQ(33u, response.packages[1].metadata_size);
707 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700708}
709
710TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
Sen Jiang81259682017-03-30 15:11:30 -0700711 fake_update_response_.multi_app = true;
712 fake_update_response_.multi_package = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700713 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
714
715 ASSERT_TRUE(TestUpdateCheck());
716
Sen Jiang81259682017-03-30 15:11:30 -0700717 EXPECT_TRUE(response.update_exists);
718 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700719 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700720 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
721 response.packages[0].payload_urls[0]);
722 EXPECT_EQ(fake_update_response_.codebase + "package2",
723 response.packages[1].payload_urls[0]);
724 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
725 response.packages[2].payload_urls[0]);
726 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
727 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
728 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700729 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700730 ASSERT_EQ(3u, response.packages.size());
731 EXPECT_EQ(string("hash2"), response.packages[1].hash);
732 EXPECT_EQ(222u, response.packages[1].size);
733 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700734 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700735 EXPECT_EQ(string("hash3"), response.packages[2].hash);
736 EXPECT_EQ(333u, response.packages[2].size);
737 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700738 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800739}
740
Sen Jiangfe284402018-03-21 14:03:50 -0700741TEST_F(OmahaRequestActionTest, PowerwashTest) {
Sen Jiangfe284402018-03-21 14:03:50 -0700742 fake_update_response_.powerwash = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700743 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
744
745 ASSERT_TRUE(TestUpdateCheck());
746
Sen Jiangfe284402018-03-21 14:03:50 -0700747 EXPECT_TRUE(response.update_exists);
748 EXPECT_TRUE(response.powerwash_required);
749}
750
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700751TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
Alex Deymo14ad88e2016-06-29 12:30:14 -0700752 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700753 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700754 tuc_params_.http_response = "invalid xml>";
755 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
756 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
757 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
758
759 ASSERT_FALSE(TestUpdateCheck());
760
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700761 EXPECT_FALSE(response.update_exists);
762}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700763
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700764TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700765 request_params_.set_interactive(false);
766 test_http_fetcher_headers_ = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -0700767 tuc_params_.http_response = "invalid xml>";
768 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
769 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
770 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
771
772 ASSERT_FALSE(TestUpdateCheck());
773
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700774 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700775}
776
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700777TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700778 // Set up a connection manager that doesn't allow a valid update over
779 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700780 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700781 fake_system_state_.set_connection_manager(&mock_cm);
782
Alex Deymo30534502015-07-20 15:06:33 -0700783 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800784 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
785 SetArgPointee<1>(ConnectionTethering::kUnknown),
786 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700787 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
788 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700789
Amin Hassani41ac04b2019-03-29 11:31:03 -0700790 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
791 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
792 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
793
794 ASSERT_FALSE(TestUpdateCheck());
795
Chris Sosa77f79e82014-06-02 18:16:24 -0700796 EXPECT_FALSE(response.update_exists);
797}
798
Weidong Guo421ff332017-04-17 10:08:38 -0700799TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
800 // This test tests that update over cellular is allowed as device policy
801 // says yes.
Weidong Guo421ff332017-04-17 10:08:38 -0700802 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700803 fake_system_state_.set_connection_manager(&mock_cm);
804
805 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
806 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
807 SetArgPointee<1>(ConnectionTethering::kUnknown),
808 Return(true)));
809 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
810 .WillRepeatedly(Return(true));
811 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
812 .WillRepeatedly(Return(true));
813
Amin Hassani41ac04b2019-03-29 11:31:03 -0700814 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
815
816 ASSERT_TRUE(TestUpdateCheck());
817
Weidong Guo421ff332017-04-17 10:08:38 -0700818 EXPECT_TRUE(response.update_exists);
819}
820
821TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
822 // This test tests that update over cellular is blocked as device policy
823 // says no.
Weidong Guo421ff332017-04-17 10:08:38 -0700824 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700825 fake_system_state_.set_connection_manager(&mock_cm);
826
827 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
828 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
829 SetArgPointee<1>(ConnectionTethering::kUnknown),
830 Return(true)));
831 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
832 .WillRepeatedly(Return(true));
833 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
834 .WillRepeatedly(Return(false));
835
Amin Hassani41ac04b2019-03-29 11:31:03 -0700836 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
837 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
838 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
839
840 ASSERT_FALSE(TestUpdateCheck());
841
Weidong Guo421ff332017-04-17 10:08:38 -0700842 EXPECT_FALSE(response.update_exists);
843}
844
845TEST_F(OmahaRequestActionTest,
846 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
847 // This test tests that, when device policy is not set, update over cellular
848 // is allowed as permission for update over cellular is set to true.
Weidong Guo421ff332017-04-17 10:08:38 -0700849 MockConnectionManager mock_cm;
Weidong Guo421ff332017-04-17 10:08:38 -0700850 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
851 fake_system_state_.set_connection_manager(&mock_cm);
852
853 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
854 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
855 SetArgPointee<1>(ConnectionTethering::kUnknown),
856 Return(true)));
857 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
858 .WillRepeatedly(Return(false));
859 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
860 .WillRepeatedly(Return(true));
861
Amin Hassani41ac04b2019-03-29 11:31:03 -0700862 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
863
864 ASSERT_TRUE(TestUpdateCheck());
865
Weidong Guo421ff332017-04-17 10:08:38 -0700866 EXPECT_TRUE(response.update_exists);
867}
868
869TEST_F(OmahaRequestActionTest,
870 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
871 // This test tests that, when device policy is not set and permission for
872 // update over cellular is set to false or does not exist, update over
873 // cellular is blocked as update target does not match the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700874 MockConnectionManager mock_cm;
875 // A version different from the version in omaha response.
876 string diff_version = "99.99.99";
877 // A size different from the size in omaha response.
878 int64_t diff_size = 999;
879
880 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
881 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
882 // This test tests cellular (3G) being the only connection type being allowed.
883 fake_system_state_.set_connection_manager(&mock_cm);
884
885 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
886 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
887 SetArgPointee<1>(ConnectionTethering::kUnknown),
888 Return(true)));
889 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
890 .WillRepeatedly(Return(false));
891 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
892 .WillRepeatedly(Return(true));
893
Amin Hassani41ac04b2019-03-29 11:31:03 -0700894 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
895 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredOverCellular;
896 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
897
898 ASSERT_FALSE(TestUpdateCheck());
899
Weidong Guo421ff332017-04-17 10:08:38 -0700900 EXPECT_FALSE(response.update_exists);
901}
902
903TEST_F(OmahaRequestActionTest,
904 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
905 // This test tests that, when device policy is not set and permission for
906 // update over cellular is set to false or does not exist, update over
907 // cellular is allowed as update target matches the omaha response.
Weidong Guo421ff332017-04-17 10:08:38 -0700908 MockConnectionManager mock_cm;
909 // A version same as the version in omaha response.
910 string new_version = fake_update_response_.version;
911 // A size same as the size in omaha response.
912 int64_t new_size = fake_update_response_.size;
913
914 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
915 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
916 fake_system_state_.set_connection_manager(&mock_cm);
917
918 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
919 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
920 SetArgPointee<1>(ConnectionTethering::kUnknown),
921 Return(true)));
922 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
923 .WillRepeatedly(Return(false));
924 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
925 .WillRepeatedly(Return(true));
926
Amin Hassani41ac04b2019-03-29 11:31:03 -0700927 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
928
929 ASSERT_TRUE(TestUpdateCheck());
930
Weidong Guo421ff332017-04-17 10:08:38 -0700931 EXPECT_TRUE(response.update_exists);
932}
933
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700934TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700935 string rollback_version = "1234.0.0";
Chris Sosa77f79e82014-06-02 18:16:24 -0700936 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700937 fake_system_state_.set_payload_state(&mock_payload_state);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700938 fake_update_response_.version = rollback_version;
939 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
940 tuc_params_.expected_code = ErrorCode::kOmahaUpdateIgnoredPerPolicy;
941 tuc_params_.expected_check_reaction = metrics::CheckReaction::kIgnored;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700942
Chris Sosa77f79e82014-06-02 18:16:24 -0700943 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800944 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -0700945
Amin Hassani41ac04b2019-03-29 11:31:03 -0700946 ASSERT_FALSE(TestUpdateCheck());
947
Chris Sosa77f79e82014-06-02 18:16:24 -0700948 EXPECT_FALSE(response.update_exists);
949}
950
Marton Hunyadyc2882062018-05-14 17:28:25 +0200951// Verify that update checks called during OOBE will not try to download an
952// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700953TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200954 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Amin Hassani41ac04b2019-03-29 11:31:03 -0700955 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
956 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
957 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
958 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Kevin Cernekee2494e282016-03-29 18:03:53 -0700959
Sen Jiang8cd42342018-01-31 12:06:59 -0800960 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700961 // OmahaRequestAction::ActionCompleted.
Amin Hassani41ac04b2019-03-29 11:31:03 -0700962 ASSERT_FALSE(TestUpdateCheck());
963
Kevin Cernekee2494e282016-03-29 18:03:53 -0700964 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +0200965}
Kevin Cernekee2494e282016-03-29 18:03:53 -0700966
Marton Hunyadyc2882062018-05-14 17:28:25 +0200967// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
968// enabled.
969TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200970 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700971 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Amin Hassani41ac04b2019-03-29 11:31:03 -0700972 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo46a9aae2016-05-04 20:20:11 -0700973
Amin Hassani41ac04b2019-03-29 11:31:03 -0700974 ASSERT_TRUE(TestUpdateCheck());
975
Kevin Cernekee2494e282016-03-29 18:03:53 -0700976 EXPECT_TRUE(response.update_exists);
977}
978
Marton Hunyadyc2882062018-05-14 17:28:25 +0200979// Verify that update checks called during OOBE will still try to download an
980// update if the response includes the deadline field.
981TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200982 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
983 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700984 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Marton Hunyadyc2882062018-05-14 17:28:25 +0200985
Amin Hassani41ac04b2019-03-29 11:31:03 -0700986 ASSERT_TRUE(TestUpdateCheck());
987
Marton Hunyadyc2882062018-05-14 17:28:25 +0200988 EXPECT_TRUE(response.update_exists);
989}
990
991// Verify that update checks called during OOBE will not try to download an
992// update if a rollback happened, even when the response includes the deadline
993// field.
994TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
Marton Hunyadyc2882062018-05-14 17:28:25 +0200995 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
996 fake_update_response_.deadline = "20101020";
Amin Hassani41ac04b2019-03-29 11:31:03 -0700997 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
998 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
999 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1000 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1001
Marton Hunyadyc2882062018-05-14 17:28:25 +02001002 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
1003 .WillOnce(Return(true));
1004
Amin Hassani41ac04b2019-03-29 11:31:03 -07001005 ASSERT_FALSE(TestUpdateCheck());
1006
Marton Hunyadyc2882062018-05-14 17:28:25 +02001007 EXPECT_FALSE(response.update_exists);
1008}
1009
Toni Barzic61544e62018-10-11 14:37:30 -07001010// Verify that non-critical updates are skipped by reporting the
1011// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1012// i.e. when the update would need user permission. Note that reporting
1013// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1014// in OOBE (warning the user about an update that will be skipped).
1015TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
Toni Barzic61544e62018-10-11 14:37:30 -07001016 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1017
1018 MockConnectionManager mock_cm;
1019 fake_system_state_.set_connection_manager(&mock_cm);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001020 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1021 tuc_params_.expected_code = ErrorCode::kNonCriticalUpdateInOOBE;
1022 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1023 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Toni Barzic61544e62018-10-11 14:37:30 -07001024
1025 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1026 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1027 SetArgPointee<1>(ConnectionTethering::kUnknown),
1028 Return(true)));
1029 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1030 .WillRepeatedly(Return(false));
1031
Amin Hassani41ac04b2019-03-29 11:31:03 -07001032 ASSERT_FALSE(TestUpdateCheck());
1033
Toni Barzic61544e62018-10-11 14:37:30 -07001034 EXPECT_FALSE(response.update_exists);
1035}
1036
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001037TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001038 request_params_.set_wall_clock_based_wait_enabled(true);
1039 request_params_.set_update_check_count_wait_enabled(false);
1040 request_params_.set_waiting_period(TimeDelta::FromDays(2));
May Lippert60aa3ca2018-08-15 16:55:29 -07001041 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001042 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1043 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1044 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001045
Amin Hassani41ac04b2019-03-29 11:31:03 -07001046 ASSERT_FALSE(TestUpdateCheck());
1047
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001048 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001049}
1050
1051TEST_F(OmahaRequestActionTest,
1052 WallClockBasedWaitAloneCausesScatteringInteractive) {
1053 request_params_.set_wall_clock_based_wait_enabled(true);
1054 request_params_.set_update_check_count_wait_enabled(false);
1055 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1056 request_params_.set_interactive(true);
1057 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1058 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Chris Sosa968d0572013-08-23 14:46:02 -07001059
1060 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001061 ASSERT_TRUE(TestUpdateCheck());
1062
Chris Sosa968d0572013-08-23 14:46:02 -07001063 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001064}
1065
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001066TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001067 request_params_.set_wall_clock_based_wait_enabled(false);
1068 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1069 request_params_.set_update_check_count_wait_enabled(true);
1070 request_params_.set_min_update_checks_needed(1);
1071 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001072 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001073
Amin Hassani41ac04b2019-03-29 11:31:03 -07001074 ASSERT_TRUE(TestUpdateCheck());
1075
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001076 EXPECT_TRUE(response.update_exists);
1077}
1078
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001079TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001080 request_params_.set_wall_clock_based_wait_enabled(true);
1081 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1082 request_params_.set_update_check_count_wait_enabled(true);
1083 request_params_.set_min_update_checks_needed(1);
1084 request_params_.set_max_update_checks_allowed(8);
Alex Deymo8e18f932015-03-27 16:16:59 -07001085 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001086 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1087
1088 ASSERT_TRUE(TestUpdateCheck());
1089
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001090 EXPECT_TRUE(response.update_exists);
1091}
1092
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001093TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001094 request_params_.set_wall_clock_based_wait_enabled(true);
1095 request_params_.set_waiting_period(TimeDelta());
1096 request_params_.set_update_check_count_wait_enabled(true);
1097 request_params_.set_min_update_checks_needed(0);
1098 request_params_.set_max_update_checks_allowed(0);
May Lippert60aa3ca2018-08-15 16:55:29 -07001099 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001100 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07001101
Amin Hassani41ac04b2019-03-29 11:31:03 -07001102 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001103
Ben Chan9abb7632014-08-07 00:10:53 -07001104 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001105 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001106 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001107 EXPECT_TRUE(response.update_exists);
1108}
1109
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001110TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001111 request_params_.set_wall_clock_based_wait_enabled(true);
1112 request_params_.set_waiting_period(TimeDelta());
1113 request_params_.set_update_check_count_wait_enabled(true);
1114 request_params_.set_min_update_checks_needed(1);
1115 request_params_.set_max_update_checks_allowed(8);
May Lippert60aa3ca2018-08-15 16:55:29 -07001116 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001117 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1118 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1119 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001120
Amin Hassani41ac04b2019-03-29 11:31:03 -07001121 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001122
Ben Chan9abb7632014-08-07 00:10:53 -07001123 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001124 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001125 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001126 EXPECT_FALSE(response.update_exists);
1127}
1128
Amin Hassani41ac04b2019-03-29 11:31:03 -07001129TEST_F(OmahaRequestActionTest,
1130 NonZeroUpdateCheckCountCausesScatteringInteractive) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001131 request_params_.set_wall_clock_based_wait_enabled(true);
1132 request_params_.set_waiting_period(TimeDelta());
1133 request_params_.set_update_check_count_wait_enabled(true);
1134 request_params_.set_min_update_checks_needed(1);
1135 request_params_.set_max_update_checks_allowed(8);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001136 request_params_.set_interactive(true);
May Lippert60aa3ca2018-08-15 16:55:29 -07001137 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
Amin Hassani41ac04b2019-03-29 11:31:03 -07001138 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1139
1140 // Verify if we are interactive check we don't defer.
1141 ASSERT_TRUE(TestUpdateCheck());
1142
1143 EXPECT_TRUE(response.update_exists);
1144}
1145
1146TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
1147 request_params_.set_wall_clock_based_wait_enabled(true);
1148 request_params_.set_waiting_period(TimeDelta());
1149 request_params_.set_update_check_count_wait_enabled(true);
1150 request_params_.set_min_update_checks_needed(1);
1151 request_params_.set_max_update_checks_allowed(8);
1152 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1153 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1154 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1155 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
May Lippert60aa3ca2018-08-15 16:55:29 -07001156
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001157 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001158 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001159
Ben Chan9abb7632014-08-07 00:10:53 -07001160 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001161 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001162 // |count| remains the same, as the decrementing happens in update_attempter
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001163 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001164 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001165 EXPECT_FALSE(response.update_exists);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001166}
1167
1168TEST_F(OmahaRequestActionTest,
1169 ExistingUpdateCheckCountCausesScatteringInteractive) {
1170 request_params_.set_wall_clock_based_wait_enabled(true);
1171 request_params_.set_waiting_period(TimeDelta());
1172 request_params_.set_update_check_count_wait_enabled(true);
1173 request_params_.set_min_update_checks_needed(1);
1174 request_params_.set_max_update_checks_allowed(8);
1175 request_params_.set_interactive(true);
1176 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1177 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1178
1179 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Chris Sosa968d0572013-08-23 14:46:02 -07001180
1181 // Verify if we are interactive check we don't defer.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001182 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07001183 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001184}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001185
Adolfo Victoria497044c2018-07-18 07:51:42 -07001186TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1187 // If staging is on, the value for max days to scatter should be ignored, and
1188 // staging's scatter value should be used.
Adolfo Victoria497044c2018-07-18 07:51:42 -07001189 request_params_.set_wall_clock_based_wait_enabled(true);
1190 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1191 request_params_.set_update_check_count_wait_enabled(false);
May Lippert60aa3ca2018-08-15 16:55:29 -07001192 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1193
Adolfo Victoria497044c2018-07-18 07:51:42 -07001194 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1195 // This should not prevent scattering due to staging.
1196 fake_update_response_.max_days_to_scatter = "0";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001197 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1198 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
1199 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
1200
1201 ASSERT_FALSE(TestUpdateCheck());
1202
Adolfo Victoria497044c2018-07-18 07:51:42 -07001203 EXPECT_FALSE(response.update_exists);
1204
1205 // Interactive updates should not be affected.
1206 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001207 tuc_params_.expected_code = ErrorCode::kSuccess;
1208 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
1209
1210 ASSERT_TRUE(TestUpdateCheck());
1211
Adolfo Victoria497044c2018-07-18 07:51:42 -07001212 EXPECT_TRUE(response.update_exists);
1213}
1214
Alex Deymo8e18f932015-03-27 16:16:59 -07001215TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001216 fake_update_response_.include_cohorts = true;
1217 fake_update_response_.cohort = "s/154454/8479665";
1218 fake_update_response_.cohorthint = "please-put-me-on-beta";
1219 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001220 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001221
Amin Hassani41ac04b2019-03-29 11:31:03 -07001222 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001223
1224 string value;
1225 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1226 EXPECT_EQ(fake_update_response_.cohort, value);
1227
1228 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1229 EXPECT_EQ(fake_update_response_.cohorthint, value);
1230
1231 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1232 EXPECT_EQ(fake_update_response_.cohortname, value);
1233}
1234
1235TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001236 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1237 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1238 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1239 fake_update_response_.include_cohorts = true;
1240 fake_update_response_.cohort = "s/154454/8479665";
1241 fake_update_response_.cohorthint = "please-put-me-on-beta";
1242 fake_update_response_.cohortname = "";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001243 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001244
Amin Hassani41ac04b2019-03-29 11:31:03 -07001245 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001246
1247 string value;
1248 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1249 EXPECT_EQ(fake_update_response_.cohort, value);
1250
1251 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1252 EXPECT_EQ(fake_update_response_.cohorthint, value);
1253
1254 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1255}
1256
1257TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001258 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Alex Deymo8e18f932015-03-27 16:16:59 -07001259
Amin Hassani41ac04b2019-03-29 11:31:03 -07001260 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1261 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo8e18f932015-03-27 16:16:59 -07001262
1263 string value;
1264 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1265 EXPECT_EQ("old_value", value);
1266
1267 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1268 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1269}
1270
Alex Deymo00d79ac2015-06-29 15:41:49 -07001271TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
Alex Deymo00d79ac2015-06-29 15:41:49 -07001272 fake_update_response_.include_cohorts = true;
1273 fake_update_response_.cohort = "s/154454/8479665";
1274 fake_update_response_.cohorthint = "please-put-me-on-beta";
1275 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001276 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1277 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1278 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001279
Amin Hassani41ac04b2019-03-29 11:31:03 -07001280 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo00d79ac2015-06-29 15:41:49 -07001281
1282 string value;
1283 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1284 EXPECT_EQ(fake_update_response_.cohort, value);
1285
1286 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1287 EXPECT_EQ(fake_update_response_.cohorthint, value);
1288
1289 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1290 EXPECT_EQ(fake_update_response_.cohortname, value);
1291}
1292
Sen Jiangb1e063a2017-09-15 17:44:31 -07001293TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
Sen Jiangb1e063a2017-09-15 17:44:31 -07001294 fake_update_response_.multi_app = true;
1295 fake_update_response_.include_cohorts = true;
1296 fake_update_response_.cohort = "s/154454/8479665";
1297 fake_update_response_.cohorthint = "please-put-me-on-beta";
1298 fake_update_response_.cohortname = "stable";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001299 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Sen Jiangb1e063a2017-09-15 17:44:31 -07001300
Amin Hassani41ac04b2019-03-29 11:31:03 -07001301 ASSERT_TRUE(TestUpdateCheck());
Sen Jiangb1e063a2017-09-15 17:44:31 -07001302
1303 string value;
1304 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1305 EXPECT_EQ(fake_update_response_.cohort, value);
1306
1307 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1308 EXPECT_EQ(fake_update_response_.cohorthint, value);
1309
1310 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1311 EXPECT_EQ(fake_update_response_.cohortname, value);
1312}
1313
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001314TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001315 const string http_response(fake_update_response_.GetNoUpdateResponse());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001316 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001317 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001318
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001319 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001320 &fake_system_state_,
1321 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001322 std::make_unique<MockHttpFetcher>(
1323 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001324 false,
1325 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001326 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001327 processor.set_delegate(&delegate_);
1328 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001329
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001330 loop.PostTask(base::Bind(
1331 [](ActionProcessor* processor) { processor->StartProcessing(); },
1332 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001333 loop.Run();
1334 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001335 EXPECT_FALSE(processor.IsRunning());
1336}
1337
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001338TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001339 tuc_params_.http_response = "invalid xml>";
1340 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1341 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1342 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1343
1344 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07001345 EXPECT_FALSE(response.update_exists);
1346}
1347
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001348TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001349 tuc_params_.expected_code = ErrorCode::kOmahaRequestEmptyResponseError;
1350 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1351 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1352
1353 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001354 EXPECT_FALSE(response.update_exists);
1355}
1356
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001357TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001358 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001359 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1360 "<daystart elapsed_seconds=\"100\"/>"
1361 "<app appid=\"foo\" status=\"ok\">"
1362 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001363 "<updatecheck/></app></response>";
1364 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1365 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1366 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1367
1368 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001369 EXPECT_FALSE(response.update_exists);
1370}
1371
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001372TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001373 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001374 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1375 "<daystart elapsed_seconds=\"100\"/>"
1376 "<app appid=\"foo\" status=\"ok\">"
1377 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001378 "<updatecheck status=\"InvalidStatusTest\"/></app></response>";
1379 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1380 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1381 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1382
1383 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001384 EXPECT_FALSE(response.update_exists);
1385}
1386
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001387TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001388 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001389 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1390 "<daystart elapsed_seconds=\"100\"/>"
1391 "<app appid=\"foo\" status=\"ok\">"
1392 "<ping status=\"ok\"/>"
Amin Hassani41ac04b2019-03-29 11:31:03 -07001393 "</app></response>";
1394 tuc_params_.expected_code = ErrorCode::kOmahaResponseInvalid;
1395 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1396 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1397
1398 ASSERT_FALSE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001399 EXPECT_FALSE(response.update_exists);
1400}
1401
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001402TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001403 tuc_params_.http_response =
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001404 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1405 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001406 // the appid needs to match that in the request params
1407 "<app appid=\"" +
1408 fake_update_response_.app_id +
1409 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001410 "<updatecheck status=\"ok\">"
1411 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001412 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001413 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001414 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001415 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001416 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001417 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001418 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001419 "/></actions></manifest></updatecheck></app></response>";
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001420
Amin Hassani41ac04b2019-03-29 11:31:03 -07001421 ASSERT_TRUE(TestUpdateCheck());
1422
Darin Petkov6a5b3222010-07-13 14:55:28 -07001423 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001424 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001425 EXPECT_EQ("http://missing/field/test/f",
1426 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001427 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001428 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1429 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001430 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001431 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001432}
1433
1434namespace {
1435class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1436 public:
1437 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001438 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001439 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001440};
1441
Alex Deymo60ca1a72015-06-18 18:19:15 -07001442void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001443 processor->StartProcessing();
1444 CHECK(processor->IsRunning());
1445 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001446}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001447} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001448
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001449TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001450 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001451 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001452
Alex Deymo60ca1a72015-06-18 18:19:15 -07001453 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001454 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001455 &fake_system_state_,
1456 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001457 std::make_unique<MockHttpFetcher>(
1458 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001459 false,
1460 "");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001461 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001462 ActionProcessor processor;
1463 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001464 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001465
Alex Deymo60ca1a72015-06-18 18:19:15 -07001466 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1467 loop.Run();
1468 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001469}
1470
Alex Deymob0d74eb2015-03-30 17:59:17 -07001471TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001472 // Make sure XML Encode is being called on the params.
Marton Hunyady2abda312018-04-24 18:21:49 +02001473 request_params_.set_os_sp("testtheservice_pack>");
1474 request_params_.set_os_board("x86 generic<id");
1475 request_params_.set_current_channel("unittest_track&lt;");
1476 request_params_.set_target_channel("unittest_track&lt;");
1477 request_params_.set_hwid("<OEM MODEL>");
Askar Aitzhan570ca872019-04-24 11:16:12 +02001478 request_params_.set_autoupdate_token("autoupdate_token>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001479 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1480 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001481 fake_prefs_.SetString(
1482 kPrefsOmahaCohortName,
1483 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001484 tuc_params_.http_response = "invalid xml>";
1485 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1486 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1487 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1488
1489 ASSERT_FALSE(TestUpdateCheck());
1490
Alex Deymo8e18f932015-03-27 16:16:59 -07001491 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1492 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1493 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1494 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1495 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1496 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1497 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1498 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1499 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1500 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1501 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1502 // Values from Prefs that are too big are removed from the XML instead of
1503 // encoded.
1504 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Askar Aitzhan570ca872019-04-24 11:16:12 +02001505 EXPECT_NE(string::npos, post_str.find("autoupdate_token&gt;"));
1506 EXPECT_EQ(string::npos, post_str.find("autoupdate_token>"));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001507}
1508
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001509TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001510 fake_update_response_.deadline = "&lt;20110101";
1511 fake_update_response_.more_info_url = "testthe&lt;url";
1512 fake_update_response_.codebase = "testthe&amp;codebase/";
Amin Hassani41ac04b2019-03-29 11:31:03 -07001513 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
1514
1515 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001516
Sen Jiang0affc2c2017-02-10 15:55:05 -08001517 EXPECT_EQ("testthe<url", response.more_info_url);
1518 EXPECT_EQ("testthe&codebase/file.signed",
1519 response.packages[0].payload_urls[0]);
1520 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001521}
1522
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001523TEST_F(OmahaRequestActionTest, ParseIntTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001524 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001525 fake_update_response_.size = 123123123123123ull;
Amin Hassani41ac04b2019-03-29 11:31:03 -07001526 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001527
Amin Hassani41ac04b2019-03-29 11:31:03 -07001528 ASSERT_TRUE(TestUpdateCheck());
Sen Jiang0affc2c2017-02-10 15:55:05 -08001529 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001530}
1531
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001532TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001533 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001534 fake_system_state_.set_prefs(&prefs);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001535 tuc_params_.http_response = "invalid xml>";
1536 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1537 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1538 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001539
Darin Petkov95508da2011-01-05 12:42:29 -08001540 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001541 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001542 // An existing but empty previous version means that we didn't reboot to a new
1543 // update, therefore, no need to update the previous version.
1544 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001545 ASSERT_FALSE(TestUpdateCheck());
1546
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001547 EXPECT_NE(
1548 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1549 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001550 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001551 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1552 string::npos);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001553 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos);
1554 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001555 // No <event> tag should be sent if we didn't reboot to an update.
1556 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001557}
1558
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001559TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001560 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001561 "invalid xml>");
1562
Alex Vakulenko75039d72014-03-25 12:36:28 -07001563 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001564 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001565 OmahaEvent::kTypeUpdateDownloadStarted,
1566 OmahaEvent::kResultSuccess);
1567 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001568 EXPECT_EQ(post_str.find("ping"), string::npos);
1569 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001570}
1571
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001572TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Marton Hunyady2abda312018-04-24 18:21:49 +02001573 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001574 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001575 ErrorCode::kError),
Amin Hassani41ac04b2019-03-29 11:31:03 -07001576 "invalid xml>");
1577
Alex Vakulenko75039d72014-03-25 12:36:28 -07001578 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001579 " <event eventtype=\"%d\" eventresult=\"%d\" "
1580 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001581 OmahaEvent::kTypeDownloadComplete,
1582 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001583 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001584 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001585 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001586}
1587
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001588TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001589 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001590 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001591 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001592 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001593 std::make_unique<MockHttpFetcher>(
1594 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001595 false,
1596 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001597 EXPECT_FALSE(update_check_action.IsEvent());
1598
1599 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001600 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001601 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001602 std::make_unique<MockHttpFetcher>(
1603 http_response.data(), http_response.size(), nullptr),
Jae Hoon Kimedb65502019-06-14 11:52:17 -07001604 false,
1605 "");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001606 EXPECT_TRUE(event_action.IsEvent());
1607}
1608
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001609TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001610 tuc_params_.http_response = "invalid xml>";
1611 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1612 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1613 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1614
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001615 for (int i = 0; i < 2; i++) {
1616 bool delta_okay = i == 1;
1617 const char* delta_okay_str = delta_okay ? "true" : "false";
Marton Hunyady2abda312018-04-24 18:21:49 +02001618 request_params_.set_delta_okay(delta_okay);
1619
Amin Hassani41ac04b2019-03-29 11:31:03 -07001620 ASSERT_FALSE(TestUpdateCheck());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001621 EXPECT_NE(
1622 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1623 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001624 << "i = " << i;
1625 }
1626}
1627
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001628TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001629 tuc_params_.http_response = "invalid xml>";
1630 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1631 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1632 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1633
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001634 for (int i = 0; i < 2; i++) {
1635 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001636 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Marton Hunyady2abda312018-04-24 18:21:49 +02001637 request_params_.set_interactive(interactive);
1638
Amin Hassani41ac04b2019-03-29 11:31:03 -07001639 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001640 EXPECT_NE(post_str.find(
1641 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001642 string::npos)
1643 << "i = " << i;
1644 }
1645}
1646
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001647TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001648 tuc_params_.http_response = "invalid xml>";
1649 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1650 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1651 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1652
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001653 for (int i = 0; i < 2; i++) {
1654 bool target_version_set = i == 1;
1655 const char* target_version_prefix = target_version_set ? "10032." : "";
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001656 request_params_.set_target_version_prefix(target_version_prefix);
1657
Amin Hassani41ac04b2019-03-29 11:31:03 -07001658 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001659 if (target_version_set) {
1660 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
1661 string::npos)
1662 << "i = " << i;
1663 } else {
1664 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
1665 << "i = " << i;
1666 }
1667 }
1668}
1669
1670TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001671 tuc_params_.http_response = "invalid xml>";
1672 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1673 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
1674 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1675
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001676 for (int i = 0; i < 4; i++) {
1677 bool rollback_allowed = i / 2 == 0;
1678 bool target_version_set = i % 2 == 0;
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001679 request_params_.set_target_version_prefix(target_version_set ? "10032."
1680 : "");
1681 request_params_.set_rollback_allowed(rollback_allowed);
1682
Amin Hassani41ac04b2019-03-29 11:31:03 -07001683 ASSERT_FALSE(TestUpdateCheck());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001684 if (rollback_allowed && target_version_set) {
1685 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
1686 << "i = " << i;
1687 } else {
1688 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
1689 }
1690 }
1691}
1692
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001693TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001694 OmahaEvent default_event;
1695 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1696 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001697 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001698
1699 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1700 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1701 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001702 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001703
1704 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1705 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001706 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001707 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1708 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001709 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001710}
1711
Askar Aitzhan570ca872019-04-24 11:16:12 +02001712TEST_F(OmahaRequestActionTest, DeviceQuickFixBuildTokenIsSetTest) {
1713 constexpr char autoupdate_token[] = "autoupdate_token";
1714
1715 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1716 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1717 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1718 request_params_.set_autoupdate_token(autoupdate_token);
1719
1720 ASSERT_TRUE(TestUpdateCheck());
1721
1722 EXPECT_NE(post_str.find(" <updatecheck token=\"" +
1723 string(autoupdate_token) + "\"></updatecheck>\n"),
1724 string::npos);
1725}
1726
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001727void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001728 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001729 fake_system_state_.set_prefs(&prefs);
1730 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001731 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001732 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1733 // Add a few hours to the day difference to test no rounding, etc.
1734 int64_t five_days_ago =
1735 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1736 int64_t six_days_ago =
1737 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1738 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001739 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001740 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001741 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001742 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001743 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001744
1745 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1746 tuc_params_.ping_only = ping_only;
1747 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1748 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1749
1750 ASSERT_TRUE(TestUpdateCheck());
1751
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001752 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1753 string::npos);
1754 if (ping_only) {
1755 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1756 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1757 } else {
1758 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1759 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001760 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001761}
1762
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001763TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001764 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001765}
1766
1767TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001768 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001769}
1770
1771TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001772 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001773 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001774 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001775 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001776 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001777 int64_t three_days_ago =
1778 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1779 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001780 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001781 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001782 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001783 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001784 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001785 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001786
1787 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1788 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1789 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1790
1791 ASSERT_TRUE(TestUpdateCheck());
1792
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001793 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001794}
1795
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001796TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001797 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001798 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001799 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001800 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001801 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001802 int64_t four_days_ago =
1803 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1804 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001805 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001806 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001807 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001808 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001809 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001810 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001811
1812 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1813 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1814 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1815
1816 ASSERT_TRUE(TestUpdateCheck());
1817
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001818 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001819 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001820}
1821
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001822TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001823 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001824 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001825 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001826 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001827 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001828 int64_t one_hour_ago =
1829 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001830 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001831 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001832 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001833 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001834 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001835 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001836 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1837 // send a ping.
1838 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1839 .WillOnce(Return(true));
1840 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1841 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001842
1843 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1844 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1845 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1846
1847 ASSERT_TRUE(TestUpdateCheck());
1848
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001849 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001850}
1851
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001852TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001853 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001854 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001855 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001856 int64_t now = Time::Now().ToInternalValue();
1857 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001858 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001859 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001860 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001861 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1862 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001863
1864 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
1865 tuc_params_.ping_only = true;
1866 tuc_params_.expected_check_result = metrics::CheckResult::kUnset;
1867 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1868
1869 EXPECT_TRUE(TestUpdateCheck());
1870 EXPECT_TRUE(post_str.empty());
Thieu Leb44e9e82011-06-06 14:34:04 -07001871}
1872
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001873TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001874 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001875 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001876 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001877 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08001878 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001879 int64_t future =
1880 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001881 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001882 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001883 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001884 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001885 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001886 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001887 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1888 .WillOnce(Return(true));
1889 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1890 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001891
1892 tuc_params_.http_response =
1893 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1894 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
1895 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1896 "<updatecheck status=\"noupdate\"/></app></response>";
1897 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1898 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1899
1900 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001901 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001902}
1903
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001904TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001905 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001906 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001907 // may fail if it runs for longer than 5 seconds. It shouldn't run
1908 // that long though.
1909 int64_t midnight =
1910 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1911 int64_t midnight_slack =
1912 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001913 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001914 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001915 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1916 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001917 EXPECT_CALL(prefs,
1918 SetInt64(kPrefsLastActivePingDay,
1919 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001920 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001921 EXPECT_CALL(prefs,
1922 SetInt64(kPrefsLastRollCallPingDay,
1923 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001924 .WillOnce(Return(true));
Amin Hassani41ac04b2019-03-29 11:31:03 -07001925
1926 tuc_params_.http_response =
1927 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1928 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
1929 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1930 "<updatecheck status=\"noupdate\"/></app></response>";
1931 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1932 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1933
1934 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001935}
1936
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001937TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001938 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001939 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001940 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1941 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001942 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1943 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001944
1945 tuc_params_.http_response =
1946 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1947 "protocol=\"3.0\"><daystart blah=\"200\"/>"
1948 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1949 "<updatecheck status=\"noupdate\"/></app></response>";
1950 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1951 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1952
1953 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001954}
1955
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001956TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001957 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001958 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001959 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1960 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001961 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1962 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Amin Hassani41ac04b2019-03-29 11:31:03 -07001963
1964 tuc_params_.http_response =
1965 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1966 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
1967 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
1968 "<updatecheck status=\"noupdate\"/></app></response>";
1969 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1970 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1971
1972 ASSERT_TRUE(TestUpdateCheck());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001973}
1974
Alex Deymob3fa53b2016-04-18 19:57:58 -07001975TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1976 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1977 // the "eol" attribute.
Amin Hassani41ac04b2019-03-29 11:31:03 -07001978 tuc_params_.http_response =
1979 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1980 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1981 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1982 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/></app></response>";
1983 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
1984 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
1985
1986 ASSERT_TRUE(TestUpdateCheck());
1987
Alex Deymob3fa53b2016-04-18 19:57:58 -07001988 string eol_pref;
1989 EXPECT_TRUE(
1990 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1991 // Note that the eol="eol" attribute should be ignored and the _eol should be
1992 // used instead.
1993 EXPECT_EQ("security-only", eol_pref);
1994}
1995
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001996TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Amin Hassani41ac04b2019-03-29 11:31:03 -07001997 tuc_params_.http_response = "invalid xml>";
1998 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
1999 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2000 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2001
2002 ASSERT_FALSE(TestUpdateCheck());
2003
Darin Petkov84c763c2010-07-29 16:27:58 -07002004 EXPECT_EQ(post_str.find("machineid="), string::npos);
2005 EXPECT_EQ(post_str.find("userid="), string::npos);
2006}
2007
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002008TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002009 const int http_error_code =
2010 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002011 tuc_params_.fail_http_response_code = 501;
2012 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2013 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2014 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2015 tuc_params_.expected_download_error_code =
2016 static_cast<metrics::DownloadErrorCode>(501);
2017
2018 ASSERT_FALSE(TestUpdateCheck());
2019
Darin Petkovedc522e2010-11-05 09:35:17 -07002020 EXPECT_FALSE(response.update_exists);
2021}
2022
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002023TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002024 const int http_error_code =
2025 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002026
2027 tuc_params_.fail_http_response_code = 1500;
2028 tuc_params_.expected_code = static_cast<ErrorCode>(http_error_code);
2029 tuc_params_.expected_check_result = metrics::CheckResult::kDownloadError;
2030 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2031 tuc_params_.expected_download_error_code =
2032 metrics::DownloadErrorCode::kHttpStatusOther;
2033
2034 ASSERT_FALSE(TestUpdateCheck());
Darin Petkovedc522e2010-11-05 09:35:17 -07002035 EXPECT_FALSE(response.update_exists);
2036}
2037
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002038TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002039 request_params_.set_wall_clock_based_wait_enabled(true);
2040 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2041 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002042
Sen Jiang7c1171e2016-06-23 11:35:40 -07002043 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002044 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002045 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002046
2047 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2048 tuc_params_.expected_code = ErrorCode::kOmahaUpdateDeferredPerPolicy;
2049 tuc_params_.expected_check_reaction = metrics::CheckReaction::kDeferring;
2050
2051 ASSERT_FALSE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002052
Ben Chan9abb7632014-08-07 00:10:53 -07002053 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002054 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002055 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002056 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002057
2058 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002059 request_params_.set_interactive(true);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002060 tuc_params_.expected_code = ErrorCode::kSuccess;
2061 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUpdating;
2062
2063 ASSERT_TRUE(TestUpdateCheck());
Chris Sosa968d0572013-08-23 14:46:02 -07002064 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002065}
2066
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002067TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Marton Hunyady2abda312018-04-24 18:21:49 +02002068 request_params_.set_wall_clock_based_wait_enabled(true);
2069 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2070 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002071
Sen Jiang7c1171e2016-06-23 11:35:40 -07002072 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002073 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2074 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002075 ASSERT_TRUE(
2076 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2077 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002078
Amin Hassani41ac04b2019-03-29 11:31:03 -07002079 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2080
2081 ASSERT_TRUE(TestUpdateCheck());
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002082 EXPECT_TRUE(response.update_exists);
2083
2084 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002085 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002086 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002087 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2088}
2089
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002090TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002091 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002092 base::ScopedTempDir tempdir;
2093 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002094
Marton Hunyady2abda312018-04-24 18:21:49 +02002095 request_params_.set_root(tempdir.GetPath().value());
2096 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2097 request_params_.set_app_version("1.2.3.4");
2098 request_params_.set_product_components("o.bundle=1");
2099 request_params_.set_current_channel("canary-channel");
2100 EXPECT_TRUE(
2101 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2102 request_params_.UpdateDownloadChannel();
2103 EXPECT_TRUE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002104
2105 tuc_params_.http_response = "invalid xml>";
2106 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2107 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2108 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2109
2110 ASSERT_FALSE(TestUpdateCheck());
2111
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002112 EXPECT_NE(
2113 string::npos,
2114 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2115 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2116 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002117 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002118}
2119
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002120TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002121 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002122 base::ScopedTempDir tempdir;
2123 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002124
Marton Hunyady2abda312018-04-24 18:21:49 +02002125 request_params_.set_root(tempdir.GetPath().value());
2126 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2127 request_params_.set_app_version("5.6.7.8");
2128 request_params_.set_product_components("o.bundle=1");
2129 request_params_.set_current_channel("stable-channel");
2130 EXPECT_TRUE(
2131 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2132 request_params_.UpdateDownloadChannel();
2133 EXPECT_FALSE(request_params_.ShouldPowerwash());
Amin Hassani41ac04b2019-03-29 11:31:03 -07002134
2135 tuc_params_.http_response = "invalid xml>";
2136 tuc_params_.expected_code = ErrorCode::kOmahaRequestXMLParseError;
2137 tuc_params_.expected_check_result = metrics::CheckResult::kParsingError;
2138 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2139
2140 ASSERT_FALSE(TestUpdateCheck());
2141
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002142 EXPECT_NE(
2143 string::npos,
2144 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2145 "version=\"5.6.7.8\" "
2146 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002147 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002148 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002149}
2150
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002151// Checks that the initial ping with a=-1 r=-1 is not send when the device
2152// was powerwashed.
2153TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2154 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2155
2156 // Flag that the device was powerwashed in the past.
2157 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002158 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2159 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2160 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002161
Amin Hassani41ac04b2019-03-29 11:31:03 -07002162 ASSERT_TRUE(TestUpdateCheck());
2163
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002164 // We shouldn't send a ping in this case since powerwash > 0.
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002165 EXPECT_EQ(string::npos, post_str.find("<ping"));
2166}
2167
Amin Hassani1677e812017-06-21 13:36:36 -07002168// Checks that the initial ping with a=-1 r=-1 is not send when the device
2169// first_active_omaha_ping_sent is set.
2170TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2171 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2172
2173 // Flag that the device was not powerwashed in the past.
2174 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2175
2176 // Flag that the device has sent first active ping in the past.
2177 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2178
Amin Hassani41ac04b2019-03-29 11:31:03 -07002179 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2180 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2181 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2182
2183 ASSERT_TRUE(TestUpdateCheck());
2184
Amin Hassani1677e812017-06-21 13:36:36 -07002185 // We shouldn't send a ping in this case since
2186 // first_active_omaha_ping_sent=true
Amin Hassani1677e812017-06-21 13:36:36 -07002187 EXPECT_EQ(string::npos, post_str.find("<ping"));
2188}
2189
Alex Deymo9fded1e2015-11-05 12:31:19 -08002190// Checks that the event 54 is sent on a reboot to a new update.
2191TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2192 // Flag that the device was updated in a previous boot.
2193 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2194
Amin Hassani41ac04b2019-03-29 11:31:03 -07002195 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2196 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2197 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2198
2199 ASSERT_TRUE(TestUpdateCheck());
Alex Deymo9fded1e2015-11-05 12:31:19 -08002200
2201 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002202 EXPECT_NE(
2203 string::npos,
2204 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2205 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002206 EXPECT_NE(string::npos,
2207 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2208
2209 // The previous version flag should have been removed.
2210 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2211 string prev_version;
2212 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2213 EXPECT_TRUE(prev_version.empty());
2214}
2215
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002216void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2217 bool initial_allow_p2p_for_sharing,
2218 bool omaha_disable_p2p_for_downloading,
2219 bool omaha_disable_p2p_for_sharing,
2220 bool payload_state_allow_p2p_attempt,
2221 bool expect_p2p_client_lookup,
2222 const string& p2p_client_result_url,
2223 bool expected_allow_p2p_for_downloading,
2224 bool expected_allow_p2p_for_sharing,
2225 const string& expected_p2p_url) {
Gilad Arnold74b5f552014-10-07 08:17:16 -07002226 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2227 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2228 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002229
2230 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002231 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002232 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2233 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002234 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2235 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2236 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2237 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2238 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2239 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2240 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2241 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2242 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2243 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2244
David Zeuthen8f191b22013-08-06 12:27:50 -07002245 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002246 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002247 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2248
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002249 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2250 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002251 .Times(expect_p2p_client_lookup ? 1 : 0);
2252
Alex Deymo8e18f932015-03-27 16:16:59 -07002253 fake_update_response_.disable_p2p_for_downloading =
2254 omaha_disable_p2p_for_downloading;
2255 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002256
2257 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2258 tuc_params_.expected_check_result = metrics::CheckResult::kUpdateAvailable;
2259
2260 ASSERT_TRUE(TestUpdateCheck());
David Zeuthen8f191b22013-08-06 12:27:50 -07002261 EXPECT_TRUE(response.update_exists);
2262
Gilad Arnold74b5f552014-10-07 08:17:16 -07002263 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2264 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002265 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002266
Gilad Arnold74b5f552014-10-07 08:17:16 -07002267 EXPECT_EQ(expected_allow_p2p_for_downloading,
2268 actual_allow_p2p_for_downloading);
2269 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2270 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002271}
2272
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002273TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002274 P2PTest(true, // initial_allow_p2p_for_downloading
2275 true, // initial_allow_p2p_for_sharing
2276 false, // omaha_disable_p2p_for_downloading
2277 false, // omaha_disable_p2p_for_sharing
2278 true, // payload_state_allow_p2p_attempt
2279 true, // expect_p2p_client_lookup
2280 "http://1.3.5.7/p2p", // p2p_client_result_url
2281 true, // expected_allow_p2p_for_downloading
2282 true, // expected_allow_p2p_for_sharing
2283 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002284}
2285
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002286TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002287 P2PTest(true, // initial_allow_p2p_for_downloading
2288 true, // initial_allow_p2p_for_sharing
2289 false, // omaha_disable_p2p_for_downloading
2290 false, // omaha_disable_p2p_for_sharing
2291 true, // payload_state_allow_p2p_attempt
2292 true, // expect_p2p_client_lookup
2293 "", // p2p_client_result_url
2294 false, // expected_allow_p2p_for_downloading
2295 true, // expected_allow_p2p_for_sharing
2296 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002297}
2298
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002299TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002300 P2PTest(false, // initial_allow_p2p_for_downloading
2301 true, // initial_allow_p2p_for_sharing
2302 false, // omaha_disable_p2p_for_downloading
2303 false, // omaha_disable_p2p_for_sharing
2304 true, // payload_state_allow_p2p_attempt
2305 false, // expect_p2p_client_lookup
2306 "unset", // p2p_client_result_url
2307 false, // expected_allow_p2p_for_downloading
2308 true, // expected_allow_p2p_for_sharing
2309 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002310}
2311
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002312TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002313 P2PTest(true, // initial_allow_p2p_for_downloading
2314 true, // initial_allow_p2p_for_sharing
2315 true, // omaha_disable_p2p_for_downloading
2316 false, // omaha_disable_p2p_for_sharing
2317 true, // payload_state_allow_p2p_attempt
2318 false, // expect_p2p_client_lookup
2319 "unset", // p2p_client_result_url
2320 false, // expected_allow_p2p_for_downloading
2321 true, // expected_allow_p2p_for_sharing
2322 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002323}
2324
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002325TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002326 P2PTest(true, // initial_allow_p2p_for_downloading
2327 true, // initial_allow_p2p_for_sharing
2328 false, // omaha_disable_p2p_for_downloading
2329 true, // omaha_disable_p2p_for_sharing
2330 true, // payload_state_allow_p2p_attempt
2331 true, // expect_p2p_client_lookup
2332 "http://1.3.5.7/p2p", // p2p_client_result_url
2333 true, // expected_allow_p2p_for_downloading
2334 false, // expected_allow_p2p_for_sharing
2335 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002336}
2337
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002338TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002339 P2PTest(true, // initial_allow_p2p_for_downloading
2340 true, // initial_allow_p2p_for_sharing
2341 true, // omaha_disable_p2p_for_downloading
2342 true, // omaha_disable_p2p_for_sharing
2343 true, // payload_state_allow_p2p_attempt
2344 false, // expect_p2p_client_lookup
2345 "unset", // p2p_client_result_url
2346 false, // expected_allow_p2p_for_downloading
2347 false, // expected_allow_p2p_for_sharing
2348 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002349}
2350
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002351bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2352 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002353 fake_update_response_.elapsed_days = elapsed_days;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002354 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2355
2356 return TestUpdateCheck();
David Zeuthen639aa362014-02-03 16:23:44 -08002357}
2358
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002359TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002360 // Simulate a successful update check that happens during OOBE. The
2361 // deadline in the response is needed to force the update attempt to
2362 // occur; responses without a deadline seen during OOBE will normally
2363 // return ErrorCode::kNonCriticalUpdateInOOBE.
2364 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2365 fake_update_response_.deadline = "20101020";
2366
David Zeuthen639aa362014-02-03 16:23:44 -08002367 // Check that we parse elapsed_days in the Omaha Response correctly.
2368 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002369 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2370 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002371 EXPECT_TRUE(response.update_exists);
2372 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002373 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002374 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002375 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002376 EXPECT_EQ(prefs_days, 42);
2377
2378 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002379 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002380 EXPECT_TRUE(response.update_exists);
2381 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002382 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002383 EXPECT_EQ(prefs_days, 42);
2384
2385 // Note that elapsed_days is not necessarily divisible by 7 so check
2386 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002387 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2388 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002389 EXPECT_TRUE(response.update_exists);
2390 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002391 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002392 EXPECT_EQ(prefs_days, 21);
2393
2394 // Check that we correctly handle elapsed_days not being included in
2395 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002396 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002397 EXPECT_TRUE(response.update_exists);
2398 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002399}
2400
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002401// If there is no prefs and OOBE is not complete, we should not
2402// report anything to Omaha.
2403TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002404 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002405 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2406 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2407}
David Zeuthen639aa362014-02-03 16:23:44 -08002408
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002409// If OOBE is complete and happened on a valid date (e.g. after Jan
2410// 1 2007 0:00 PST), that date should be used and written to
2411// prefs. However, first try with an invalid date and check we do
2412// nothing.
2413TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2414 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2415 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2416 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2417 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2418}
David Zeuthen639aa362014-02-03 16:23:44 -08002419
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002420// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2421// should yield an InstallDate of 14.
2422TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2423 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2424 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2425 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2426 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002427
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002428 int64_t prefs_days;
2429 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2430 EXPECT_EQ(prefs_days, 14);
2431}
David Zeuthen639aa362014-02-03 16:23:44 -08002432
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002433// Now that we have a valid date in prefs, check that we keep using
2434// that even if OOBE date reports something else. The date Jan 30,
2435// 2007 0:00 PST should yield an InstallDate of 28... but since
2436// there's a prefs file, we should still get 14.
2437TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2438 // Set a valid date in the prefs first.
2439 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002440
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002441 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2442 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2443 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002444
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002445 int64_t prefs_days;
2446 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2447 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002448
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002449 // If we delete the prefs file, we should get 28 days.
2450 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2451 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2452 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2453 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002454}
2455
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002456// Verifies that a device with no device policy, and is not a consumer
2457// device sets the max kernel key version to the current version.
2458// ie. the same behavior as if rollback is enabled.
2459TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2460 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2461
2462 // Setup and verify some initial default values for the kernel TPM
2463 // values that control verified boot and rollback.
2464 const int min_kernel_version = 4;
2465 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2466 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2467 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2468 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2469
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002470 EXPECT_CALL(
2471 *fake_system_state_.mock_metrics_reporter(),
2472 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2473 .Times(1);
2474
Amin Hassani41ac04b2019-03-29 11:31:03 -07002475 fake_update_response_.deadline = "20101020";
2476 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2477 tuc_params_.is_consumer_device = false;
2478 tuc_params_.rollback_allowed_milestones = 3;
2479
2480 EXPECT_TRUE(TestUpdateCheck());
2481 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002482
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002483 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002484 // kernel key version. This has the effect of freezing roll
2485 // forwards indefinitely. This will hold the rollback window
2486 // open until a future change will be able to move this forward
2487 // relative the configured window.
2488 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2489 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2490}
2491
2492// Verifies that a conmsumer device with no device policy sets the
2493// max kernel key version to the current version. ie. the same
2494// behavior as if rollback is enabled.
2495TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2496 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2497
2498 // Setup and verify some initial default values for the kernel TPM
2499 // values that control verified boot and rollback.
2500 const int min_kernel_version = 3;
2501 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2502 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2503 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2504 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2505
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002506 EXPECT_CALL(
2507 *fake_system_state_.mock_metrics_reporter(),
2508 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2509 .Times(1);
2510
Amin Hassani41ac04b2019-03-29 11:31:03 -07002511 fake_update_response_.deadline = "20101020";
2512 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2513 tuc_params_.is_consumer_device = true;
2514 tuc_params_.rollback_allowed_milestones = 3;
2515
2516 EXPECT_TRUE(TestUpdateCheck());
2517 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002518
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002519 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002520 // was set to logical infinity. This is the expected behavior for
2521 // consumer devices and matches the existing behavior prior to the
2522 // rollback features.
2523 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2524 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2525}
2526
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002527// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002528// in the TPM to prevent roll forward.
2529TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2530 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2531
2532 // Setup and verify some initial default values for the kernel TPM
2533 // values that control verified boot and rollback.
2534 const int allowed_milestones = 4;
2535 const int min_kernel_version = 3;
2536 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2537 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2538 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2539 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2540
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002541 EXPECT_CALL(
2542 *fake_system_state_.mock_metrics_reporter(),
2543 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2544 .Times(1);
2545
Amin Hassani41ac04b2019-03-29 11:31:03 -07002546 fake_update_response_.deadline = "20101020";
2547 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2548 tuc_params_.is_consumer_device = false;
2549 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2550 tuc_params_.is_policy_loaded = true;
2551
2552 EXPECT_TRUE(TestUpdateCheck());
2553 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002554
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002555 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002556 // was set to the current minimum kernel key version. This has
2557 // the effect of freezing roll forwards indefinitely. This will
2558 // hold the rollback window open until a future change will
2559 // be able to move this forward relative the configured window.
2560 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2561 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2562}
2563
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002564// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002565// in the TPM to logical infinity, to allow roll forward.
2566TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2567 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2568
2569 // Setup and verify some initial default values for the kernel TPM
2570 // values that control verified boot and rollback.
2571 const int allowed_milestones = 0;
2572 const int min_kernel_version = 3;
2573 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2574 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2575 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2576 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2577
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002578 EXPECT_CALL(
2579 *fake_system_state_.mock_metrics_reporter(),
2580 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2581 .Times(1);
2582
Amin Hassani41ac04b2019-03-29 11:31:03 -07002583 fake_update_response_.deadline = "20101020";
2584 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2585 tuc_params_.is_consumer_device = false;
2586 tuc_params_.rollback_allowed_milestones = allowed_milestones;
2587 tuc_params_.is_policy_loaded = true;
2588
2589 EXPECT_TRUE(TestUpdateCheck());
2590 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002591
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002592 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002593 // was set to logical infinity.
2594 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2595 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2596}
2597
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002598TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002599 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002600 fake_update_response_.deadline = "20101020";
2601 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2602 tuc_params_.is_consumer_device = false;
2603 tuc_params_.rollback_allowed_milestones = 4;
2604 tuc_params_.is_policy_loaded = true;
2605
2606 EXPECT_TRUE(TestUpdateCheck());
2607 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002608 EXPECT_TRUE(response.is_rollback);
2609}
2610
2611TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002612 fake_update_response_.rollback_firmware_version = "1.2";
2613 fake_update_response_.rollback_kernel_version = "3.4";
2614 fake_update_response_.rollback = true;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002615 fake_update_response_.deadline = "20101020";
2616 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2617 tuc_params_.is_consumer_device = false;
2618 tuc_params_.rollback_allowed_milestones = 4;
2619 tuc_params_.is_policy_loaded = true;
2620
2621 EXPECT_TRUE(TestUpdateCheck());
2622 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002623 EXPECT_TRUE(response.is_rollback);
2624 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
2625 EXPECT_EQ(2, response.rollback_key_version.firmware);
2626 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
2627 EXPECT_EQ(4, response.rollback_key_version.kernel);
2628}
2629
May Lippert60aa3ca2018-08-15 16:55:29 -07002630TEST_F(OmahaRequestActionTest,
2631 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
2632 FakeClock fake_clock;
2633 Time now = Time::Now();
2634 fake_clock.SetWallclockTime(now);
2635 fake_system_state_.set_clock(&fake_clock);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002636 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
May Lippert60aa3ca2018-08-15 16:55:29 -07002637
Amin Hassani41ac04b2019-03-29 11:31:03 -07002638 ASSERT_TRUE(TestUpdateCheck());
2639
May Lippert60aa3ca2018-08-15 16:55:29 -07002640 EXPECT_TRUE(response.update_exists);
2641 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2642
2643 int64_t stored_first_seen_at_time;
2644 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
2645 &stored_first_seen_at_time));
2646 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
2647}
2648
2649TEST_F(OmahaRequestActionTest,
2650 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
2651 FakeClock fake_clock;
2652 Time now = Time::Now();
2653 fake_clock.SetWallclockTime(now);
2654 fake_system_state_.set_clock(&fake_clock);
2655
Amin Hassani41ac04b2019-03-29 11:31:03 -07002656 tuc_params_.http_response = fake_update_response_.GetNoUpdateResponse();
2657 tuc_params_.expected_check_result = metrics::CheckResult::kNoUpdateAvailable;
2658 tuc_params_.expected_check_reaction = metrics::CheckReaction::kUnset;
2659
2660 ASSERT_TRUE(TestUpdateCheck());
2661
May Lippert60aa3ca2018-08-15 16:55:29 -07002662 EXPECT_FALSE(response.update_exists);
2663 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
2664}
2665
Xiaochu Liu88d90382018-08-29 16:09:11 -07002666TEST_F(OmahaRequestActionTest, InstallTest) {
Xiaochu Liu6310be62018-10-11 15:09:03 -07002667 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002668 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Amin Hassani41ac04b2019-03-29 11:31:03 -07002669 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2670
2671 ASSERT_TRUE(TestUpdateCheck());
2672
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002673 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) {
Xiaochu Liu88d90382018-08-29 16:09:11 -07002674 EXPECT_NE(string::npos,
Xiaochu Liu6310be62018-10-11 15:09:03 -07002675 post_str.find("appid=\"" + fake_update_response_.app_id + "_" +
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002676 dlc_module_id + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002677 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07002678 EXPECT_NE(string::npos,
2679 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
2680
2681 // Count number of updatecheck tag in response.
2682 int updatecheck_count = 0;
2683 size_t pos = 0;
2684 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
2685 updatecheck_count++;
2686 pos++;
2687 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002688 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count);
Xiaochu Liu6310be62018-10-11 15:09:03 -07002689}
2690
2691TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
2692 fake_update_response_.multi_app_skip_updatecheck = true;
2693 fake_update_response_.multi_app_no_update = false;
2694 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08002695 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu6310be62018-10-11 15:09:03 -07002696 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002697 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2698
2699 ASSERT_TRUE(TestUpdateCheck());
2700
Xiaochu Liu6310be62018-10-11 15:09:03 -07002701 EXPECT_TRUE(response.update_exists);
2702 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07002703}
2704
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002705TEST_F(OmahaRequestActionTest, PastRollbackVersionsNoEntries) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002706 fake_update_response_.rollback = true;
2707 fake_update_response_.rollback_allowed_milestones = 4;
2708 request_params_.set_rollback_allowed_milestones(4);
Amin Hassani41ac04b2019-03-29 11:31:03 -07002709 fake_update_response_.deadline = "20101020";
2710 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2711 tuc_params_.is_consumer_device = false;
2712 tuc_params_.rollback_allowed_milestones = 4;
2713 tuc_params_.is_policy_loaded = true;
2714
2715 EXPECT_TRUE(TestUpdateCheck());
2716 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002717 EXPECT_TRUE(response.is_rollback);
2718 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2719 response.past_rollback_key_version.firmware_key);
2720 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2721 response.past_rollback_key_version.firmware);
2722 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2723 response.past_rollback_key_version.kernel_key);
2724 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2725 response.past_rollback_key_version.kernel);
2726}
2727
2728TEST_F(OmahaRequestActionTest, PastRollbackVersionsValidEntries) {
Bailey Berrof3ce47f2019-02-25 18:22:17 -08002729 request_params_.set_rollback_allowed_milestones(4);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002730 fake_update_response_.rollback = true;
2731 fake_update_response_.rollback_allowed_milestones = 4;
2732 fake_update_response_.rollback_firmware_version = "4.3";
2733 fake_update_response_.rollback_kernel_version = "2.1";
2734 fake_update_response_.past_rollback_key_version =
2735 std::make_pair("16.15", "14.13");
Amin Hassani41ac04b2019-03-29 11:31:03 -07002736 fake_update_response_.deadline = "20101020";
2737 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2738 tuc_params_.is_consumer_device = false;
2739 tuc_params_.rollback_allowed_milestones = 4;
2740 tuc_params_.is_policy_loaded = true;
2741
2742 EXPECT_TRUE(TestUpdateCheck());
2743 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002744 EXPECT_TRUE(response.is_rollback);
2745 EXPECT_EQ(16, response.past_rollback_key_version.firmware_key);
2746 EXPECT_EQ(15, response.past_rollback_key_version.firmware);
2747 EXPECT_EQ(14, response.past_rollback_key_version.kernel_key);
2748 EXPECT_EQ(13, response.past_rollback_key_version.kernel);
2749}
2750
2751TEST_F(OmahaRequestActionTest, MismatchNumberOfVersions) {
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002752 fake_update_response_.rollback = true;
2753 fake_update_response_.rollback_allowed_milestones = 2;
Amin Hassani41ac04b2019-03-29 11:31:03 -07002754 fake_update_response_.deadline = "20101020";
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002755 request_params_.set_rollback_allowed_milestones(4);
2756
2757 // Since |request_params_.rollback_allowed_milestones| is 4 but the response
2758 // is constructed with |fake_update_response_.rollback_allowed_milestones| set
2759 // to 2, OmahaRequestAction will look for the key values of N-4 version but
2760 // only the N-2 version will exist.
Amin Hassani41ac04b2019-03-29 11:31:03 -07002761 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2762 tuc_params_.is_consumer_device = false;
2763 tuc_params_.rollback_allowed_milestones = 2;
2764 tuc_params_.is_policy_loaded = true;
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002765
Amin Hassani41ac04b2019-03-29 11:31:03 -07002766 EXPECT_TRUE(TestUpdateCheck());
2767 EXPECT_TRUE(response.update_exists);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -07002768 EXPECT_TRUE(response.is_rollback);
2769 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2770 response.past_rollback_key_version.firmware_key);
2771 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2772 response.past_rollback_key_version.firmware);
2773 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2774 response.past_rollback_key_version.kernel_key);
2775 EXPECT_EQ(std::numeric_limits<uint16_t>::max(),
2776 response.past_rollback_key_version.kernel);
2777}
2778
Matt Ziegelbaumaa8e1a42019-05-09 21:41:58 -04002779TEST_F(OmahaRequestActionTest, IncludeRequisitionTest) {
2780 request_params_.set_device_requisition("remora");
2781 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2782 ASSERT_TRUE(TestUpdateCheck());
2783 EXPECT_NE(string::npos, post_str.find("requisition=\"remora\""));
2784}
2785
2786TEST_F(OmahaRequestActionTest, NoIncludeRequisitionTest) {
2787 request_params_.set_device_requisition("");
2788 tuc_params_.http_response = fake_update_response_.GetUpdateResponse();
2789 ASSERT_TRUE(TestUpdateCheck());
2790 EXPECT_EQ(string::npos, post_str.find("requisition"));
2791}
2792
Darin Petkov6a5b3222010-07-13 14:55:28 -07002793} // namespace chromeos_update_engine