blob: 1786bcc223c216f07c6b33ad82c58c70be30795a [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
Ben Chanab5a0af2017-10-12 14:57:50 -070021#include <memory>
Darin Petkov6a5b3222010-07-13 14:55:28 -070022#include <string>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080023#include <utility>
Darin Petkov6a5b3222010-07-13 14:55:28 -070024#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070025
Alex Deymo60ca1a72015-06-18 18:19:15 -070026#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070027#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070028#include <base/files/scoped_temp_dir.h>
Ben Chan5c02c132017-06-27 07:10:36 -070029#include <base/memory/ptr_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070030#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070031#include <base/strings/string_util.h>
32#include <base/strings/stringprintf.h>
33#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070034#include <brillo/message_loops/fake_message_loop.h>
35#include <brillo/message_loops/message_loop.h>
36#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070037#include <gtest/gtest.h>
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080038#include <policy/libpolicy.h>
39#include <policy/mock_libpolicy.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070040
Alex Deymo39910dc2015-11-09 17:04:30 -080041#include "update_engine/common/action_pipe.h"
42#include "update_engine/common/constants.h"
43#include "update_engine/common/fake_prefs.h"
44#include "update_engine/common/hash_calculator.h"
45#include "update_engine/common/mock_http_fetcher.h"
46#include "update_engine/common/platform_constants.h"
47#include "update_engine/common/prefs.h"
48#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030049#include "update_engine/fake_system_state.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070050#include "update_engine/metrics_reporter_interface.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070051#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070052#include "update_engine/mock_payload_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070053#include "update_engine/omaha_request_params.h"
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080054#include "update_engine/update_manager/rollback_prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070055
Darin Petkov1cbd78f2010-07-29 12:38:34 -070056using base::Time;
57using base::TimeDelta;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080058using chromeos_update_manager::kRollforwardInfinity;
Darin Petkov6a5b3222010-07-13 14:55:28 -070059using std::string;
60using std::vector;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080061using testing::_;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070062using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070063using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070064using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065using testing::Ge;
66using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080067using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070068using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070069using testing::ReturnPointee;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080070using testing::ReturnRef;
Gilad Arnold74b5f552014-10-07 08:17:16 -070071using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070072using testing::SetArgPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070073
Alex Deymo8e18f932015-03-27 16:16:59 -070074namespace {
75
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -080076static_assert(kRollforwardInfinity == 0xfffffffe,
77 "Don't change the value of kRollforward infinity unless its "
78 "size has been changed in firmware.");
79
Xiaochu Liu6310be62018-10-11 15:09:03 -070080const char kCurrentVersion[] = "0.1.0.0";
Alex Deymo85616652015-10-15 18:48:31 -070081const char kTestAppId[] = "test-app-id";
Aaron Wood7dcdedf2017-09-06 17:17:41 -070082const char kTestAppId2[] = "test-app2-id";
Xiaochu Liu6310be62018-10-11 15:09:03 -070083const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck";
Alex Deymo85616652015-10-15 18:48:31 -070084
Alex Deymo8e18f932015-03-27 16:16:59 -070085// This is a helper struct to allow unit tests build an update response with the
86// values they care about.
87struct FakeUpdateResponse {
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070088 string GetRollbackVersionAttributes() const {
89 return (rollback ? " _rollback=\"true\"" : "") +
90 (!rollback_firmware_version.empty()
91 ? " _firmware_version=\"" + rollback_firmware_version + "\""
92 : "") +
93 (!rollback_kernel_version.empty()
94 ? " _kernel_version=\"" + rollback_kernel_version + "\""
95 : "");
96 }
97
Alex Deymo8e18f932015-03-27 16:16:59 -070098 string GetNoUpdateResponse() const {
99 string entity_str;
100 if (include_entity)
101 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Sen Jiang81259682017-03-30 15:11:30 -0700102 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str +
103 "<response protocol=\"3.0\">"
104 "<daystart elapsed_seconds=\"100\"/>"
105 "<app appid=\"" +
106 app_id + "\" " +
107 (include_cohorts
108 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
109 "\" cohortname=\"" + cohortname + "\" "
110 : "") +
111 " status=\"ok\">"
112 "<ping status=\"ok\"/>"
113 "<updatecheck status=\"noupdate\"/></app>" +
114 (multi_app_no_update
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700115 ? "<app appid=\"" + app_id2 +
116 "\"><updatecheck status=\"noupdate\"/></app>"
Sen Jiang81259682017-03-30 15:11:30 -0700117 : "") +
118 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700119 }
120
121 string GetUpdateResponse() const {
Sen Jiang2703ef42017-03-16 13:36:21 -0700122 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
123 "protocol=\"3.0\">"
124 "<daystart elapsed_seconds=\"100\"" +
125 (elapsed_days.empty() ? ""
126 : (" elapsed_days=\"" + elapsed_days + "\"")) +
127 "/>"
128 "<app appid=\"" +
129 app_id + "\" " +
130 (include_cohorts
131 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
132 "\" cohortname=\"" + cohortname + "\" "
133 : "") +
134 " status=\"ok\">"
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700135 "<ping status=\"ok\"/><updatecheck status=\"ok\"" +
136 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" +
Sen Jiang2703ef42017-03-16 13:36:21 -0700137 codebase +
138 "\"/></urls>"
139 "<manifest version=\"" +
140 version +
141 "\">"
142 "<packages><package hash=\"not-used\" name=\"" +
143 filename + "\" size=\"" + base::Int64ToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800144 "\" hash_sha256=\"" + hash + "\"/>" +
145 (multi_package ? "<package name=\"package2\" size=\"222\" "
146 "hash_sha256=\"hash2\"/>"
147 : "") +
148 "</packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700149 "<actions><action event=\"postinstall\" MetadataSize=\"11" +
Amin Hassanic482bbd2018-09-21 16:07:20 -0700150 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url +
151 "\" Prompt=\"" + prompt +
Sen Jiang2703ef42017-03-16 13:36:21 -0700152 "\" "
Sen Jiangcdd52062017-05-18 15:33:10 -0700153 "IsDeltaPayload=\"true" +
154 (multi_package ? ":false" : "") +
155 "\" "
Sen Jiang2703ef42017-03-16 13:36:21 -0700156 "MaxDaysToScatter=\"" +
157 max_days_to_scatter +
158 "\" "
Amin Hassanic482bbd2018-09-21 16:07:20 -0700159 "sha256=\"not-used\" " +
Sen Jiang2703ef42017-03-16 13:36:21 -0700160 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
161 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
162 : "") +
163 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Sen Jiangfe284402018-03-21 14:03:50 -0700164 (powerwash ? "Powerwash=\"true\" " : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700165 "/></actions></manifest></updatecheck></app>" +
166 (multi_app
Sen Jiangb1e063a2017-09-15 17:44:31 -0700167 ? "<app appid=\"" + app_id2 + "\"" +
168 (include_cohorts ? " cohort=\"cohort2\"" : "") +
169 "><updatecheck status=\"ok\"><urls><url codebase=\"" +
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700170 codebase2 + "\"/></urls><manifest version=\"" + version2 +
171 "\"><packages>"
Sen Jiang81259682017-03-30 15:11:30 -0700172 "<package name=\"package3\" size=\"333\" "
173 "hash_sha256=\"hash3\"/></packages>"
Sen Jiang00adf7b2017-06-26 15:57:29 -0700174 "<actions><action event=\"postinstall\" " +
175 (multi_app_self_update
176 ? "noupdate=\"true\" IsDeltaPayload=\"true\" "
177 : "IsDeltaPayload=\"false\" ") +
178 "MetadataSize=\"33\"/></actions>"
Sen Jiang81259682017-03-30 15:11:30 -0700179 "</manifest></updatecheck></app>"
180 : "") +
181 (multi_app_no_update
182 ? "<app><updatecheck status=\"noupdate\"/></app>"
183 : "") +
Xiaochu Liu6310be62018-10-11 15:09:03 -0700184 (multi_app_skip_updatecheck
185 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>"
186 : "") +
Sen Jiang81259682017-03-30 15:11:30 -0700187 "</response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700188 }
189
190 // Return the payload URL, which is split in two fields in the XML response.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800191 string GetPayloadUrl() { return codebase + filename; }
Alex Deymo8e18f932015-03-27 16:16:59 -0700192
Alex Deymo85616652015-10-15 18:48:31 -0700193 string app_id = kTestAppId;
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700194 string app_id2 = kTestAppId2;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700195 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck;
196 string current_version = kCurrentVersion;
Alex Deymo8e18f932015-03-27 16:16:59 -0700197 string version = "1.2.3.4";
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700198 string version2 = "2.3.4.5";
Alex Deymo8e18f932015-03-27 16:16:59 -0700199 string more_info_url = "http://more/info";
200 string prompt = "true";
201 string codebase = "http://code/base/";
Sen Jiang81259682017-03-30 15:11:30 -0700202 string codebase2 = "http://code/base/2/";
Alex Deymo8e18f932015-03-27 16:16:59 -0700203 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700204 string hash = "4841534831323334";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800205 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700206 string deadline = "";
207 string max_days_to_scatter = "7";
208 string elapsed_days = "42";
209
210 // P2P setting defaults to allowed.
211 bool disable_p2p_for_downloading = false;
212 bool disable_p2p_for_sharing = false;
213
Sen Jiangfe284402018-03-21 14:03:50 -0700214 bool powerwash = false;
215
Alex Deymo8e18f932015-03-27 16:16:59 -0700216 // Omaha cohorts settings.
217 bool include_cohorts = false;
218 string cohort = "";
219 string cohorthint = "";
220 string cohortname = "";
221
222 // Whether to include the CrOS <!ENTITY> in the XML response.
223 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800224
Sen Jiang81259682017-03-30 15:11:30 -0700225 // Whether to include more than one app.
226 bool multi_app = false;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700227 // Whether to include an app with noupdate="true".
228 bool multi_app_self_update = false;
229 // Whether to include an additional app with status="noupdate".
Sen Jiang81259682017-03-30 15:11:30 -0700230 bool multi_app_no_update = false;
Xiaochu Liu6310be62018-10-11 15:09:03 -0700231 // Whether to include an additional app with no updatecheck tag.
232 bool multi_app_skip_updatecheck = false;
Sen Jiang81259682017-03-30 15:11:30 -0700233 // Whether to include more than one package in an app.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800234 bool multi_package = false;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700235
236 // Whether the payload is a rollback.
237 bool rollback = false;
238 // The verified boot firmware key version for the rollback image.
239 string rollback_firmware_version = "";
240 // The verified boot kernel key version for the rollback image.
241 string rollback_kernel_version = "";
Alex Deymo8e18f932015-03-27 16:16:59 -0700242};
243
244} // namespace
245
Darin Petkov6a5b3222010-07-13 14:55:28 -0700246namespace chromeos_update_engine {
247
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700248class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
249 public:
250 OmahaRequestActionTestProcessorDelegate()
251 : expected_code_(ErrorCode::kSuccess),
252 interactive_(false),
253 test_http_fetcher_headers_(false) {}
254 ~OmahaRequestActionTestProcessorDelegate() override = default;
255
256 void ProcessingDone(const ActionProcessor* processor,
257 ErrorCode code) override {
258 brillo::MessageLoop::current()->BreakLoop();
259 }
260
261 void ActionCompleted(ActionProcessor* processor,
262 AbstractAction* action,
263 ErrorCode code) override {
264 // Make sure actions always succeed.
265 if (action->Type() == OmahaRequestAction::StaticType()) {
266 EXPECT_EQ(expected_code_, code);
267 // Check that the headers were set in the fetcher during the action. Note
268 // that we set this request as "interactive".
269 auto fetcher = static_cast<const MockHttpFetcher*>(
270 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get());
271
272 if (test_http_fetcher_headers_) {
273 EXPECT_EQ(interactive_ ? "fg" : "bg",
274 fetcher->GetHeader("X-Goog-Update-Interactivity"));
275 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId"));
276 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater"));
277 }
278 post_data_ = fetcher->post_data();
279 } else if (action->Type() ==
280 ObjectCollectorAction<OmahaResponse>::StaticType()) {
281 EXPECT_EQ(ErrorCode::kSuccess, code);
282 auto collector_action =
283 static_cast<ObjectCollectorAction<OmahaResponse>*>(action);
284 omaha_response_.reset(new OmahaResponse(collector_action->object()));
285 EXPECT_TRUE(omaha_response_);
286 } else {
287 EXPECT_EQ(ErrorCode::kSuccess, code);
288 }
289 }
290 ErrorCode expected_code_;
291 brillo::Blob post_data_;
292 bool interactive_;
293 bool test_http_fetcher_headers_;
294 std::unique_ptr<OmahaResponse> omaha_response_;
295};
296
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700297class OmahaRequestActionTest : public ::testing::Test {
298 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800299 void SetUp() override {
Marton Hunyady2abda312018-04-24 18:21:49 +0200300 request_params_.set_os_sp("service_pack");
301 request_params_.set_os_board("x86-generic");
302 request_params_.set_app_id(kTestAppId);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700303 request_params_.set_app_version(kCurrentVersion);
Marton Hunyady2abda312018-04-24 18:21:49 +0200304 request_params_.set_app_lang("en-US");
305 request_params_.set_current_channel("unittest");
306 request_params_.set_target_channel("unittest");
307 request_params_.set_hwid("OEM MODEL 09235 7471");
308 request_params_.set_fw_version("ChromeOSFirmware.1.0");
309 request_params_.set_ec_version("0X0A1");
310 request_params_.set_delta_okay(true);
311 request_params_.set_interactive(false);
312 request_params_.set_update_url("http://url");
313 request_params_.set_target_version_prefix("");
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200314 request_params_.set_rollback_allowed(false);
Marton Hunyady2abda312018-04-24 18:21:49 +0200315 request_params_.set_is_powerwash_allowed(false);
Xiaochu Liu6310be62018-10-11 15:09:03 -0700316 request_params_.set_is_install(false);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -0800317 request_params_.set_dlc_module_ids({});
Marton Hunyady2abda312018-04-24 18:21:49 +0200318
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700319 fake_system_state_.set_request_params(&request_params_);
320 fake_system_state_.set_prefs(&fake_prefs_);
321 }
322
323 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800324 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700325 // is used. |payload_state| may be null, in which case a local mock is used.
326 // |p2p_manager| may be null, in which case a local mock is used.
327 // |connection_manager| may be null, in which case a local mock is used.
328 // out_response may be null. If |fail_http_response_code| is non-negative,
329 // the transfer will fail with that code. |ping_only| is passed through to the
330 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
331 // post-data received by the mock HttpFetcher is returned.
332 //
333 // The |expected_check_result|, |expected_check_reaction| and
334 // |expected_error_code| parameters are for checking expectations
335 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
336 // UMA statistics. Use the appropriate ::kUnset value to specify that
337 // the given metric should not be reported.
Marton Hunyady2abda312018-04-24 18:21:49 +0200338 bool TestUpdateCheck(const string& http_response,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700339 int fail_http_response_code,
340 bool ping_only,
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800341 bool is_consumer_device,
342 int rollback_allowed_milestones,
343 bool is_policy_loaded,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700344 ErrorCode expected_code,
345 metrics::CheckResult expected_check_result,
346 metrics::CheckReaction expected_check_reaction,
347 metrics::DownloadErrorCode expected_download_error_code,
348 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700349 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700350
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800351 // Overload of TestUpdateCheck that does not supply |is_consumer_device| or
352 // |rollback_allowed_milestones| which are only required for rollback tests.
353 bool TestUpdateCheck(const string& http_response,
354 int fail_http_response_code,
355 bool ping_only,
356 ErrorCode expected_code,
357 metrics::CheckResult expected_check_result,
358 metrics::CheckReaction expected_check_reaction,
359 metrics::DownloadErrorCode expected_download_error_code,
360 OmahaResponse* out_response,
361 brillo::Blob* out_post_data);
362
363 void TestRollbackCheck(bool is_consumer_device,
364 int rollback_allowed_milestones,
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700365 bool is_policy_loaded,
366 OmahaResponse* out_response);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800367
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700368 void TestEvent(OmahaEvent* event,
369 const string& http_response,
370 brillo::Blob* out_post_data);
371
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800372 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700373 // only a ping or also an updatecheck.
374 void PingTest(bool ping_only);
375
376 // InstallDate test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800377 bool InstallDateParseHelper(const string& elapsed_days,
378 OmahaResponse* response);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700379
380 // P2P test helper function.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800381 void P2PTest(bool initial_allow_p2p_for_downloading,
382 bool initial_allow_p2p_for_sharing,
383 bool omaha_disable_p2p_for_downloading,
384 bool omaha_disable_p2p_for_sharing,
385 bool payload_state_allow_p2p_attempt,
386 bool expect_p2p_client_lookup,
387 const string& p2p_client_result_url,
388 bool expected_allow_p2p_for_downloading,
389 bool expected_allow_p2p_for_sharing,
390 const string& expected_p2p_url);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700391
392 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700393 FakeUpdateResponse fake_update_response_;
Marton Hunyady2abda312018-04-24 18:21:49 +0200394 // Used by all tests.
395 OmahaRequestParams request_params_{&fake_system_state_};
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700396
397 FakePrefs fake_prefs_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700398
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700399 OmahaRequestActionTestProcessorDelegate delegate_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700400
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700401 bool test_http_fetcher_headers_{false};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700402};
403
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700404bool OmahaRequestActionTest::TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700405 const string& http_response,
406 int fail_http_response_code,
407 bool ping_only,
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800408 bool is_consumer_device,
409 int rollback_allowed_milestones,
410 bool is_policy_loaded,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700411 ErrorCode expected_code,
412 metrics::CheckResult expected_check_result,
413 metrics::CheckReaction expected_check_reaction,
414 metrics::DownloadErrorCode expected_download_error_code,
415 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700416 brillo::Blob* out_post_data) {
417 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700418 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700419 auto fetcher = std::make_unique<MockHttpFetcher>(
420 http_response.data(), http_response.size(), nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700421 if (fail_http_response_code >= 0) {
422 fetcher->FailTransfer(fail_http_response_code);
423 }
Marton Hunyady2abda312018-04-24 18:21:49 +0200424 // This ensures the tests didn't forget to update fake_system_state_ if they
425 // are not using the default request_params_.
426 EXPECT_EQ(&request_params_, fake_system_state_.request_params());
427
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700428 auto omaha_request_action = std::make_unique<OmahaRequestAction>(
429 &fake_system_state_, nullptr, std::move(fetcher), ping_only);
430
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800431 auto mock_policy_provider =
432 std::make_unique<NiceMock<policy::MockPolicyProvider>>();
433 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice())
434 .WillRepeatedly(Return(is_consumer_device));
435
436 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded())
437 .WillRepeatedly(Return(is_policy_loaded));
438
439 const policy::MockDevicePolicy device_policy;
440 const bool get_allowed_milestone_succeeds = rollback_allowed_milestones >= 0;
441 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_))
442 .WillRepeatedly(DoAll(SetArgPointee<0>(rollback_allowed_milestones),
443 Return(get_allowed_milestone_succeeds)));
444
445 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy())
446 .WillRepeatedly(ReturnRef(device_policy));
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700447 omaha_request_action->policy_provider_ = std::move(mock_policy_provider);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800448
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700449 delegate_.expected_code_ = expected_code;
450 delegate_.interactive_ = request_params_.interactive();
451 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700452 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700453 processor.set_delegate(&delegate_);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700454
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700455 auto collector_action =
456 std::make_unique<ObjectCollectorAction<OmahaResponse>>();
457 BondActions(omaha_request_action.get(), collector_action.get());
458 processor.EnqueueAction(std::move(omaha_request_action));
459 processor.EnqueueAction(std::move(collector_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700460
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700461 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
462 ReportUpdateCheckMetrics(_, _, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800463 .Times(AnyNumber());
Tianjie Xu282aa1f2017-09-05 13:42:45 -0700464
465 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(),
466 ReportUpdateCheckMetrics(_,
467 expected_check_result,
468 expected_check_reaction,
469 expected_download_error_code))
470 .Times(ping_only ? 0 : 1);
David Zeuthen33bae492014-02-25 16:16:18 -0800471
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700472 loop.PostTask(base::Bind(
473 [](ActionProcessor* processor) { processor->StartProcessing(); },
474 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700475 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700476 EXPECT_FALSE(loop.PendingTasks());
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700477 if (delegate_.omaha_response_ && out_response)
478 *out_response = *delegate_.omaha_response_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700479 if (out_post_data)
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700480 *out_post_data = delegate_.post_data_;
481 return delegate_.omaha_response_ != nullptr;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700482}
483
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800484bool OmahaRequestActionTest::TestUpdateCheck(
485 const string& http_response,
486 int fail_http_response_code,
487 bool ping_only,
488 ErrorCode expected_code,
489 metrics::CheckResult expected_check_result,
490 metrics::CheckReaction expected_check_reaction,
491 metrics::DownloadErrorCode expected_download_error_code,
492 OmahaResponse* out_response,
493 brillo::Blob* out_post_data) {
494 return TestUpdateCheck(http_response,
495 fail_http_response_code,
496 ping_only,
497 true, // is_consumer_device
498 0, // rollback_allowed_milestones
499 false, // is_policy_loaded
500 expected_code,
501 expected_check_result,
502 expected_check_reaction,
503 expected_download_error_code,
504 out_response,
505 out_post_data);
506}
507
508void OmahaRequestActionTest::TestRollbackCheck(bool is_consumer_device,
509 int rollback_allowed_milestones,
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700510 bool is_policy_loaded,
511 OmahaResponse* out_response) {
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800512 fake_update_response_.deadline = "20101020";
513 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
514 -1,
515 false, // ping_only
516 is_consumer_device,
517 rollback_allowed_milestones,
518 is_policy_loaded,
519 ErrorCode::kSuccess,
520 metrics::CheckResult::kUpdateAvailable,
521 metrics::CheckReaction::kUpdating,
522 metrics::DownloadErrorCode::kUnset,
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700523 out_response,
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800524 nullptr));
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700525 ASSERT_TRUE(out_response->update_exists);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -0800526}
527
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700528// Tests Event requests -- they should always succeed. |out_post_data| may be
529// null; if non-null, the post-data received by the mock HttpFetcher is
530// returned.
531void OmahaRequestActionTest::TestEvent(OmahaEvent* event,
532 const string& http_response,
533 brillo::Blob* out_post_data) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700534 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700535 loop.SetAsCurrent();
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700536
537 auto action = std::make_unique<OmahaRequestAction>(
538 &fake_system_state_,
539 event,
540 std::make_unique<MockHttpFetcher>(
541 http_response.data(), http_response.size(), nullptr),
542 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700543 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700544 processor.set_delegate(&delegate_);
545 processor.EnqueueAction(std::move(action));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700546
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700547 loop.PostTask(base::Bind(
548 [](ActionProcessor* processor) { processor->StartProcessing(); },
549 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700550 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700551 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700552
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700553 if (out_post_data)
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700554 *out_post_data = delegate_.post_data_;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700555}
556
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700557TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400558 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700559 fake_update_response_.include_entity = true;
Marton Hunyady2abda312018-04-24 18:21:49 +0200560 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
561 -1,
562 false, // ping_only
563 ErrorCode::kOmahaRequestXMLHasEntityDecl,
564 metrics::CheckResult::kParsingError,
565 metrics::CheckReaction::kUnset,
566 metrics::DownloadErrorCode::kUnset,
567 &response,
568 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400569 EXPECT_FALSE(response.update_exists);
570}
571
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700572TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700573 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +0200574 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
575 -1,
576 false, // ping_only
577 ErrorCode::kSuccess,
578 metrics::CheckResult::kNoUpdateAvailable,
579 metrics::CheckReaction::kUnset,
580 metrics::DownloadErrorCode::kUnset,
581 &response,
582 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700583 EXPECT_FALSE(response.update_exists);
584}
585
Sen Jiang81259682017-03-30 15:11:30 -0700586TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) {
587 OmahaResponse response;
588 fake_update_response_.multi_app_no_update = true;
Marton Hunyady2abda312018-04-24 18:21:49 +0200589 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
Sen Jiang81259682017-03-30 15:11:30 -0700590 -1,
591 false, // ping_only
592 ErrorCode::kSuccess,
593 metrics::CheckResult::kNoUpdateAvailable,
594 metrics::CheckReaction::kUnset,
595 metrics::DownloadErrorCode::kUnset,
596 &response,
597 nullptr));
598 EXPECT_FALSE(response.update_exists);
599}
600
Sen Jiang00adf7b2017-06-26 15:57:29 -0700601TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) {
602 OmahaResponse response;
603 fake_update_response_.multi_app_no_update = true;
Marton Hunyady2abda312018-04-24 18:21:49 +0200604 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang00adf7b2017-06-26 15:57:29 -0700605 -1,
606 false, // ping_only
607 ErrorCode::kSuccess,
608 metrics::CheckResult::kNoUpdateAvailable,
609 metrics::CheckReaction::kUnset,
610 metrics::DownloadErrorCode::kUnset,
611 &response,
612 nullptr));
613 EXPECT_FALSE(response.update_exists);
614}
615
616TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) {
617 OmahaResponse response;
618 ASSERT_TRUE(TestUpdateCheck(
Sen Jiang00adf7b2017-06-26 15:57:29 -0700619 "<response><app><updatecheck status=\"ok\"><manifest><actions><action "
620 "event=\"postinstall\" noupdate=\"true\"/></actions>"
621 "</manifest></updatecheck></app></response>",
622 -1,
623 false, // ping_only
624 ErrorCode::kSuccess,
625 metrics::CheckResult::kNoUpdateAvailable,
626 metrics::CheckReaction::kUnset,
627 metrics::DownloadErrorCode::kUnset,
628 &response,
629 nullptr));
630 EXPECT_FALSE(response.update_exists);
631}
632
Alex Deymo8e18f932015-03-27 16:16:59 -0700633// Test that all the values in the response are parsed in a normal update
634// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700635TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700636 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700637 fake_update_response_.deadline = "20101020";
Marton Hunyady2abda312018-04-24 18:21:49 +0200638 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
639 -1,
640 false, // ping_only
641 ErrorCode::kSuccess,
642 metrics::CheckResult::kUpdateAvailable,
643 metrics::CheckReaction::kUpdating,
644 metrics::DownloadErrorCode::kUnset,
645 &response,
646 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700647 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700648 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700649 EXPECT_EQ("", response.system_version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800650 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
651 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700652 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800653 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
654 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700655 EXPECT_EQ(true, response.packages[0].is_delta);
Alex Deymo8e18f932015-03-27 16:16:59 -0700656 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
657 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
Sen Jiangfe284402018-03-21 14:03:50 -0700658 EXPECT_FALSE(response.powerwash_required);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700659 // Omaha cohort attributes are not set in the response, so they should not be
Alex Deymo8e18f932015-03-27 16:16:59 -0700660 // persisted.
661 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
662 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
663 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700664}
665
Sen Jiang0affc2c2017-02-10 15:55:05 -0800666TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
667 OmahaResponse response;
668 fake_update_response_.multi_package = true;
Marton Hunyady2abda312018-04-24 18:21:49 +0200669 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang0affc2c2017-02-10 15:55:05 -0800670 -1,
671 false, // ping_only
672 ErrorCode::kSuccess,
673 metrics::CheckResult::kUpdateAvailable,
674 metrics::CheckReaction::kUpdating,
675 metrics::DownloadErrorCode::kUnset,
676 &response,
677 nullptr));
678 EXPECT_TRUE(response.update_exists);
679 EXPECT_EQ(fake_update_response_.version, response.version);
680 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
681 response.packages[0].payload_urls[0]);
682 EXPECT_EQ(fake_update_response_.codebase + "package2",
683 response.packages[1].payload_urls[0]);
684 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
685 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700686 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700687 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800688 ASSERT_EQ(2u, response.packages.size());
Sen Jiang81259682017-03-30 15:11:30 -0700689 EXPECT_EQ(string("hash2"), response.packages[1].hash);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800690 EXPECT_EQ(222u, response.packages[1].size);
Sen Jiang81259682017-03-30 15:11:30 -0700691 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700692 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700693}
694
695TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) {
696 OmahaResponse response;
697 fake_update_response_.multi_app = true;
Marton Hunyady2abda312018-04-24 18:21:49 +0200698 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang81259682017-03-30 15:11:30 -0700699 -1,
700 false, // ping_only
701 ErrorCode::kSuccess,
702 metrics::CheckResult::kUpdateAvailable,
703 metrics::CheckReaction::kUpdating,
704 metrics::DownloadErrorCode::kUnset,
705 &response,
706 nullptr));
707 EXPECT_TRUE(response.update_exists);
708 EXPECT_EQ(fake_update_response_.version, response.version);
709 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
710 response.packages[0].payload_urls[0]);
711 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
712 response.packages[1].payload_urls[0]);
713 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
714 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
715 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700716 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700717 ASSERT_EQ(2u, response.packages.size());
718 EXPECT_EQ(string("hash3"), response.packages[1].hash);
719 EXPECT_EQ(333u, response.packages[1].size);
720 EXPECT_EQ(33u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700721 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700722}
723
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700724TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) {
725 OmahaResponse response;
726 fake_update_response_.multi_app = true;
727 // trigger the lining up of the app and system versions
728 request_params_.set_system_app_id(fake_update_response_.app_id2);
729
Marton Hunyady2abda312018-04-24 18:21:49 +0200730 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700731 -1,
732 false, // ping_only
733 ErrorCode::kSuccess,
734 metrics::CheckResult::kUpdateAvailable,
735 metrics::CheckReaction::kUpdating,
736 metrics::DownloadErrorCode::kUnset,
737 &response,
738 nullptr));
739 EXPECT_TRUE(response.update_exists);
740 EXPECT_EQ(fake_update_response_.version, response.version);
741 EXPECT_EQ(fake_update_response_.version2, response.system_version);
742 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
743 response.packages[0].payload_urls[0]);
744 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
745 response.packages[1].payload_urls[0]);
746 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
747 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
748 EXPECT_EQ(11u, response.packages[0].metadata_size);
749 EXPECT_EQ(true, response.packages[0].is_delta);
750 ASSERT_EQ(2u, response.packages.size());
751 EXPECT_EQ(string("hash3"), response.packages[1].hash);
752 EXPECT_EQ(333u, response.packages[1].size);
753 EXPECT_EQ(33u, response.packages[1].metadata_size);
754 EXPECT_EQ(false, response.packages[1].is_delta);
755}
756
Sen Jiang81259682017-03-30 15:11:30 -0700757TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) {
758 OmahaResponse response;
Sen Jiang00adf7b2017-06-26 15:57:29 -0700759 fake_update_response_.multi_app = true;
760 fake_update_response_.multi_app_self_update = true;
Marton Hunyady2abda312018-04-24 18:21:49 +0200761 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang81259682017-03-30 15:11:30 -0700762 -1,
763 false, // ping_only
764 ErrorCode::kSuccess,
765 metrics::CheckResult::kUpdateAvailable,
766 metrics::CheckReaction::kUpdating,
767 metrics::DownloadErrorCode::kUnset,
768 &response,
769 nullptr));
770 EXPECT_TRUE(response.update_exists);
771 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700772 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700773 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
774 response.packages[0].payload_urls[0]);
775 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
776 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
777 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiang00adf7b2017-06-26 15:57:29 -0700778 ASSERT_EQ(2u, response.packages.size());
779 EXPECT_EQ(string("hash3"), response.packages[1].hash);
780 EXPECT_EQ(333u, response.packages[1].size);
781 EXPECT_EQ(33u, response.packages[1].metadata_size);
782 EXPECT_EQ(true, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700783}
784
785TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) {
786 OmahaResponse response;
787 fake_update_response_.multi_app = true;
788 fake_update_response_.multi_package = true;
Marton Hunyady2abda312018-04-24 18:21:49 +0200789 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang81259682017-03-30 15:11:30 -0700790 -1,
791 false, // ping_only
792 ErrorCode::kSuccess,
793 metrics::CheckResult::kUpdateAvailable,
794 metrics::CheckReaction::kUpdating,
795 metrics::DownloadErrorCode::kUnset,
796 &response,
797 nullptr));
798 EXPECT_TRUE(response.update_exists);
799 EXPECT_EQ(fake_update_response_.version, response.version);
Aaron Wood7dcdedf2017-09-06 17:17:41 -0700800 EXPECT_EQ("", response.system_version);
Sen Jiang81259682017-03-30 15:11:30 -0700801 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
802 response.packages[0].payload_urls[0]);
803 EXPECT_EQ(fake_update_response_.codebase + "package2",
804 response.packages[1].payload_urls[0]);
805 EXPECT_EQ(fake_update_response_.codebase2 + "package3",
806 response.packages[2].payload_urls[0]);
807 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
808 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
809 EXPECT_EQ(11u, response.packages[0].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700810 EXPECT_EQ(true, response.packages[0].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700811 ASSERT_EQ(3u, response.packages.size());
812 EXPECT_EQ(string("hash2"), response.packages[1].hash);
813 EXPECT_EQ(222u, response.packages[1].size);
814 EXPECT_EQ(22u, response.packages[1].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700815 EXPECT_EQ(false, response.packages[1].is_delta);
Sen Jiang81259682017-03-30 15:11:30 -0700816 EXPECT_EQ(string("hash3"), response.packages[2].hash);
817 EXPECT_EQ(333u, response.packages[2].size);
818 EXPECT_EQ(33u, response.packages[2].metadata_size);
Sen Jiangcdd52062017-05-18 15:33:10 -0700819 EXPECT_EQ(false, response.packages[2].is_delta);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800820}
821
Sen Jiangfe284402018-03-21 14:03:50 -0700822TEST_F(OmahaRequestActionTest, PowerwashTest) {
823 OmahaResponse response;
824 fake_update_response_.powerwash = true;
Amin Hassani3538a882018-05-31 14:11:30 -0700825 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiangfe284402018-03-21 14:03:50 -0700826 -1,
827 false, // ping_only
828 ErrorCode::kSuccess,
829 metrics::CheckResult::kUpdateAvailable,
830 metrics::CheckReaction::kUpdating,
831 metrics::DownloadErrorCode::kUnset,
832 &response,
833 nullptr));
834 EXPECT_TRUE(response.update_exists);
835 EXPECT_TRUE(response.powerwash_required);
836}
837
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700838TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) {
839 OmahaResponse response;
Alex Deymo14ad88e2016-06-29 12:30:14 -0700840 request_params_.set_interactive(true);
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700841 test_http_fetcher_headers_ = true;
842 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
843 -1,
844 false, // ping_only
845 ErrorCode::kOmahaRequestXMLParseError,
846 metrics::CheckResult::kParsingError,
847 metrics::CheckReaction::kUnset,
848 metrics::DownloadErrorCode::kUnset,
849 &response,
850 nullptr));
851 EXPECT_FALSE(response.update_exists);
852}
Alex Deymo14ad88e2016-06-29 12:30:14 -0700853
Amin Hassanid3f4bea2018-04-30 14:52:40 -0700854TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) {
855 OmahaResponse response;
856 request_params_.set_interactive(false);
857 test_http_fetcher_headers_ = true;
858 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
859 -1,
860 false, // ping_only
861 ErrorCode::kOmahaRequestXMLParseError,
862 metrics::CheckResult::kParsingError,
863 metrics::CheckReaction::kUnset,
864 metrics::DownloadErrorCode::kUnset,
865 &response,
866 nullptr));
867 EXPECT_FALSE(response.update_exists);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700868}
869
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700870TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700871 OmahaResponse response;
872 // Set up a connection manager that doesn't allow a valid update over
873 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700874 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700875 fake_system_state_.set_connection_manager(&mock_cm);
876
Alex Deymo30534502015-07-20 15:06:33 -0700877 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800878 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
879 SetArgPointee<1>(ConnectionTethering::kUnknown),
880 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700881 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
882 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700883
Marton Hunyady2abda312018-04-24 18:21:49 +0200884 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
885 -1,
886 false, // ping_only
887 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
888 metrics::CheckResult::kUpdateAvailable,
889 metrics::CheckReaction::kIgnored,
890 metrics::DownloadErrorCode::kUnset,
891 &response,
892 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700893 EXPECT_FALSE(response.update_exists);
894}
895
Weidong Guo421ff332017-04-17 10:08:38 -0700896TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
897 // This test tests that update over cellular is allowed as device policy
898 // says yes.
899 OmahaResponse response;
900 MockConnectionManager mock_cm;
901
902 fake_system_state_.set_connection_manager(&mock_cm);
903
904 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
905 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
906 SetArgPointee<1>(ConnectionTethering::kUnknown),
907 Return(true)));
908 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
909 .WillRepeatedly(Return(true));
910 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
911 .WillRepeatedly(Return(true));
912
Marton Hunyady2abda312018-04-24 18:21:49 +0200913 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Weidong Guo421ff332017-04-17 10:08:38 -0700914 -1,
915 false, // ping_only
916 ErrorCode::kSuccess,
917 metrics::CheckResult::kUpdateAvailable,
918 metrics::CheckReaction::kUpdating,
919 metrics::DownloadErrorCode::kUnset,
920 &response,
921 nullptr));
922 EXPECT_TRUE(response.update_exists);
923}
924
925TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
926 // This test tests that update over cellular is blocked as device policy
927 // says no.
928 OmahaResponse response;
929 MockConnectionManager mock_cm;
930
931 fake_system_state_.set_connection_manager(&mock_cm);
932
933 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
934 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
935 SetArgPointee<1>(ConnectionTethering::kUnknown),
936 Return(true)));
937 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
938 .WillRepeatedly(Return(true));
939 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
940 .WillRepeatedly(Return(false));
941
Marton Hunyady2abda312018-04-24 18:21:49 +0200942 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Weidong Guo421ff332017-04-17 10:08:38 -0700943 -1,
944 false, // ping_only
945 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
946 metrics::CheckResult::kUpdateAvailable,
947 metrics::CheckReaction::kIgnored,
948 metrics::DownloadErrorCode::kUnset,
949 &response,
950 nullptr));
951 EXPECT_FALSE(response.update_exists);
952}
953
954TEST_F(OmahaRequestActionTest,
955 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
956 // This test tests that, when device policy is not set, update over cellular
957 // is allowed as permission for update over cellular is set to true.
958 OmahaResponse response;
959 MockConnectionManager mock_cm;
960
961 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
962 fake_system_state_.set_connection_manager(&mock_cm);
963
964 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
965 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
966 SetArgPointee<1>(ConnectionTethering::kUnknown),
967 Return(true)));
968 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
969 .WillRepeatedly(Return(false));
970 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
971 .WillRepeatedly(Return(true));
972
Marton Hunyady2abda312018-04-24 18:21:49 +0200973 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Weidong Guo421ff332017-04-17 10:08:38 -0700974 -1,
975 false, // ping_only
976 ErrorCode::kSuccess,
977 metrics::CheckResult::kUpdateAvailable,
978 metrics::CheckReaction::kUpdating,
979 metrics::DownloadErrorCode::kUnset,
980 &response,
981 nullptr));
982 EXPECT_TRUE(response.update_exists);
983}
984
985TEST_F(OmahaRequestActionTest,
986 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
987 // This test tests that, when device policy is not set and permission for
988 // update over cellular is set to false or does not exist, update over
989 // cellular is blocked as update target does not match the omaha response.
990 OmahaResponse response;
991 MockConnectionManager mock_cm;
992 // A version different from the version in omaha response.
993 string diff_version = "99.99.99";
994 // A size different from the size in omaha response.
995 int64_t diff_size = 999;
996
997 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
998 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
999 // This test tests cellular (3G) being the only connection type being allowed.
1000 fake_system_state_.set_connection_manager(&mock_cm);
1001
1002 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1003 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1004 SetArgPointee<1>(ConnectionTethering::kUnknown),
1005 Return(true)));
1006 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1007 .WillRepeatedly(Return(false));
1008 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
1009 .WillRepeatedly(Return(true));
1010
Marton Hunyady2abda312018-04-24 18:21:49 +02001011 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Weidong Guo421ff332017-04-17 10:08:38 -07001012 -1,
1013 false, // ping_only
1014 ErrorCode::kOmahaUpdateIgnoredOverCellular,
1015 metrics::CheckResult::kUpdateAvailable,
1016 metrics::CheckReaction::kIgnored,
1017 metrics::DownloadErrorCode::kUnset,
1018 &response,
1019 nullptr));
1020 EXPECT_FALSE(response.update_exists);
1021}
1022
1023TEST_F(OmahaRequestActionTest,
1024 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
1025 // This test tests that, when device policy is not set and permission for
1026 // update over cellular is set to false or does not exist, update over
1027 // cellular is allowed as update target matches the omaha response.
1028 OmahaResponse response;
1029 MockConnectionManager mock_cm;
1030 // A version same as the version in omaha response.
1031 string new_version = fake_update_response_.version;
1032 // A size same as the size in omaha response.
1033 int64_t new_size = fake_update_response_.size;
1034
1035 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
1036 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
1037 fake_system_state_.set_connection_manager(&mock_cm);
1038
1039 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1040 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1041 SetArgPointee<1>(ConnectionTethering::kUnknown),
1042 Return(true)));
1043 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1044 .WillRepeatedly(Return(false));
1045 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
1046 .WillRepeatedly(Return(true));
1047
Marton Hunyady2abda312018-04-24 18:21:49 +02001048 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Weidong Guo421ff332017-04-17 10:08:38 -07001049 -1,
1050 false, // ping_only
1051 ErrorCode::kSuccess,
1052 metrics::CheckResult::kUpdateAvailable,
1053 metrics::CheckReaction::kUpdating,
1054 metrics::DownloadErrorCode::kUnset,
1055 &response,
1056 nullptr));
1057 EXPECT_TRUE(response.update_exists);
1058}
1059
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001060TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -07001061 string rollback_version = "1234.0.0";
1062 OmahaResponse response;
1063
1064 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001065 fake_system_state_.set_payload_state(&mock_payload_state);
1066
Chris Sosa77f79e82014-06-02 18:16:24 -07001067 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001068 .WillRepeatedly(Return(rollback_version));
Chris Sosa77f79e82014-06-02 18:16:24 -07001069
Alex Deymo8e18f932015-03-27 16:16:59 -07001070 fake_update_response_.version = rollback_version;
Marton Hunyady2abda312018-04-24 18:21:49 +02001071 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1072 -1,
1073 false, // ping_only
1074 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
1075 metrics::CheckResult::kUpdateAvailable,
1076 metrics::CheckReaction::kIgnored,
1077 metrics::DownloadErrorCode::kUnset,
1078 &response,
1079 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -07001080 EXPECT_FALSE(response.update_exists);
1081}
1082
Marton Hunyadyc2882062018-05-14 17:28:25 +02001083// Verify that update checks called during OOBE will not try to download an
1084// update if the response doesn't include the deadline field.
Kevin Cernekee2494e282016-03-29 18:03:53 -07001085TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
1086 OmahaResponse response;
Marton Hunyadyc2882062018-05-14 17:28:25 +02001087 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Kevin Cernekee2494e282016-03-29 18:03:53 -07001088
Sen Jiang8cd42342018-01-31 12:06:59 -08001089 // TODO(senj): set better default value for metrics::checkresult in
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001090 // OmahaRequestAction::ActionCompleted.
Marton Hunyady2abda312018-04-24 18:21:49 +02001091 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07001092 -1,
1093 false, // ping_only
1094 ErrorCode::kNonCriticalUpdateInOOBE,
1095 metrics::CheckResult::kParsingError,
1096 metrics::CheckReaction::kUnset,
1097 metrics::DownloadErrorCode::kUnset,
1098 &response,
1099 nullptr));
Kevin Cernekee2494e282016-03-29 18:03:53 -07001100 EXPECT_FALSE(response.update_exists);
Marton Hunyadyc2882062018-05-14 17:28:25 +02001101}
Kevin Cernekee2494e282016-03-29 18:03:53 -07001102
Marton Hunyadyc2882062018-05-14 17:28:25 +02001103// Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not
1104// enabled.
1105TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) {
1106 OmahaResponse response;
1107 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymo46a9aae2016-05-04 20:20:11 -07001108 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
Alex Deymo46a9aae2016-05-04 20:20:11 -07001109
Marton Hunyady2abda312018-04-24 18:21:49 +02001110 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1111 -1,
1112 false, // ping_only
1113 ErrorCode::kSuccess,
1114 metrics::CheckResult::kUpdateAvailable,
1115 metrics::CheckReaction::kUpdating,
1116 metrics::DownloadErrorCode::kUnset,
1117 &response,
1118 nullptr));
Kevin Cernekee2494e282016-03-29 18:03:53 -07001119 EXPECT_TRUE(response.update_exists);
1120}
1121
Marton Hunyadyc2882062018-05-14 17:28:25 +02001122// Verify that update checks called during OOBE will still try to download an
1123// update if the response includes the deadline field.
1124TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) {
1125 OmahaResponse response;
1126 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1127 fake_update_response_.deadline = "20101020";
1128
1129 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1130 -1,
1131 false, // ping_only
1132 ErrorCode::kSuccess,
1133 metrics::CheckResult::kUpdateAvailable,
1134 metrics::CheckReaction::kUpdating,
1135 metrics::DownloadErrorCode::kUnset,
1136 &response,
1137 nullptr));
1138 EXPECT_TRUE(response.update_exists);
1139}
1140
1141// Verify that update checks called during OOBE will not try to download an
1142// update if a rollback happened, even when the response includes the deadline
1143// field.
1144TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) {
1145 OmahaResponse response;
1146 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1147 fake_update_response_.deadline = "20101020";
1148 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened())
1149 .WillOnce(Return(true));
1150
1151 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1152 -1,
1153 false, // ping_only
1154 ErrorCode::kNonCriticalUpdateInOOBE,
1155 metrics::CheckResult::kParsingError,
1156 metrics::CheckReaction::kUnset,
1157 metrics::DownloadErrorCode::kUnset,
1158 &response,
1159 nullptr));
1160 EXPECT_FALSE(response.update_exists);
1161}
1162
Toni Barzic61544e62018-10-11 14:37:30 -07001163// Verify that non-critical updates are skipped by reporting the
1164// kNonCriticalUpdateInOOBE error code when attempted over cellular network -
1165// i.e. when the update would need user permission. Note that reporting
1166// kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX
1167// in OOBE (warning the user about an update that will be skipped).
1168TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) {
1169 OmahaResponse response;
1170 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
1171
1172 MockConnectionManager mock_cm;
1173 fake_system_state_.set_connection_manager(&mock_cm);
1174
1175 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
1176 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular),
1177 SetArgPointee<1>(ConnectionTethering::kUnknown),
1178 Return(true)));
1179 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
1180 .WillRepeatedly(Return(false));
1181
1182 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1183 -1,
1184 false, // ping_only
1185 ErrorCode::kNonCriticalUpdateInOOBE,
1186 metrics::CheckResult::kParsingError,
1187 metrics::CheckReaction::kUnset,
1188 metrics::DownloadErrorCode::kUnset,
1189 &response,
1190 nullptr));
1191 EXPECT_FALSE(response.update_exists);
1192}
1193
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001194TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001195 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001196 request_params_.set_wall_clock_based_wait_enabled(true);
1197 request_params_.set_update_check_count_wait_enabled(false);
1198 request_params_.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001199
May Lippert60aa3ca2018-08-15 16:55:29 -07001200 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1201
Marton Hunyady2abda312018-04-24 18:21:49 +02001202 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1203 -1,
1204 false, // ping_only
1205 ErrorCode::kOmahaUpdateDeferredPerPolicy,
1206 metrics::CheckResult::kUpdateAvailable,
1207 metrics::CheckReaction::kDeferring,
1208 metrics::DownloadErrorCode::kUnset,
1209 &response,
1210 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001211 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001212
1213 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02001214 request_params_.set_interactive(true);
1215 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1216 -1,
1217 false, // ping_only
1218 ErrorCode::kSuccess,
1219 metrics::CheckResult::kUpdateAvailable,
1220 metrics::CheckReaction::kUpdating,
1221 metrics::DownloadErrorCode::kUnset,
1222 &response,
1223 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001224 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001225}
1226
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001227TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001228 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001229 request_params_.set_wall_clock_based_wait_enabled(false);
1230 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1231 request_params_.set_update_check_count_wait_enabled(true);
1232 request_params_.set_min_update_checks_needed(1);
1233 request_params_.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001234
Marton Hunyady2abda312018-04-24 18:21:49 +02001235 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1236 -1,
1237 false, // ping_only
1238 ErrorCode::kSuccess,
1239 metrics::CheckResult::kUpdateAvailable,
1240 metrics::CheckReaction::kUpdating,
1241 metrics::DownloadErrorCode::kUnset,
1242 &response,
1243 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001244 EXPECT_TRUE(response.update_exists);
1245}
1246
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001247TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001248 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001249 request_params_.set_wall_clock_based_wait_enabled(true);
1250 request_params_.set_waiting_period(TimeDelta::FromDays(2));
1251 request_params_.set_update_check_count_wait_enabled(true);
1252 request_params_.set_min_update_checks_needed(1);
1253 request_params_.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001254
Alex Deymo8e18f932015-03-27 16:16:59 -07001255 fake_update_response_.max_days_to_scatter = "0";
Marton Hunyady2abda312018-04-24 18:21:49 +02001256 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1257 -1,
1258 false, // ping_only
1259 ErrorCode::kSuccess,
1260 metrics::CheckResult::kUpdateAvailable,
1261 metrics::CheckReaction::kUpdating,
1262 metrics::DownloadErrorCode::kUnset,
1263 &response,
1264 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001265 EXPECT_TRUE(response.update_exists);
1266}
1267
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001268TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001269 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001270 request_params_.set_wall_clock_based_wait_enabled(true);
1271 request_params_.set_waiting_period(TimeDelta());
1272 request_params_.set_update_check_count_wait_enabled(true);
1273 request_params_.set_min_update_checks_needed(0);
1274 request_params_.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001275
May Lippert60aa3ca2018-08-15 16:55:29 -07001276 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1277
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001278 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1279 -1,
1280 false, // ping_only
1281 ErrorCode::kSuccess,
1282 metrics::CheckResult::kUpdateAvailable,
1283 metrics::CheckReaction::kUpdating,
1284 metrics::DownloadErrorCode::kUnset,
1285 &response,
1286 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001287
Ben Chan9abb7632014-08-07 00:10:53 -07001288 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001289 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001290 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001291 EXPECT_TRUE(response.update_exists);
1292}
1293
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001294TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001295 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001296 request_params_.set_wall_clock_based_wait_enabled(true);
1297 request_params_.set_waiting_period(TimeDelta());
1298 request_params_.set_update_check_count_wait_enabled(true);
1299 request_params_.set_min_update_checks_needed(1);
1300 request_params_.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001301
May Lippert60aa3ca2018-08-15 16:55:29 -07001302 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1303
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001304 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1305 -1,
1306 false, // ping_only
1307 ErrorCode::kOmahaUpdateDeferredPerPolicy,
1308 metrics::CheckResult::kUpdateAvailable,
1309 metrics::CheckReaction::kDeferring,
1310 metrics::DownloadErrorCode::kUnset,
1311 &response,
1312 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001313
Ben Chan9abb7632014-08-07 00:10:53 -07001314 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001315 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001316 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001317 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001318
1319 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02001320 request_params_.set_interactive(true);
1321 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1322 -1,
1323 false, // ping_only
1324 ErrorCode::kSuccess,
1325 metrics::CheckResult::kUpdateAvailable,
1326 metrics::CheckReaction::kUpdating,
1327 metrics::DownloadErrorCode::kUnset,
1328 &response,
1329 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001330 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001331}
1332
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001333TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001334 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001335 request_params_.set_wall_clock_based_wait_enabled(true);
1336 request_params_.set_waiting_period(TimeDelta());
1337 request_params_.set_update_check_count_wait_enabled(true);
1338 request_params_.set_min_update_checks_needed(1);
1339 request_params_.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001340
May Lippert60aa3ca2018-08-15 16:55:29 -07001341 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1342
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001343 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001344
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001345 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1346 -1,
1347 false, // ping_only
1348 ErrorCode::kOmahaUpdateDeferredPerPolicy,
1349 metrics::CheckResult::kUpdateAvailable,
1350 metrics::CheckReaction::kDeferring,
1351 metrics::DownloadErrorCode::kUnset,
1352 &response,
1353 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001354
Ben Chan9abb7632014-08-07 00:10:53 -07001355 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001356 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001357 // count remains the same, as the decrementing happens in update_attempter
1358 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001359 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001360 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001361
1362 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02001363 request_params_.set_interactive(true);
1364 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1365 -1,
1366 false, // ping_only
1367 ErrorCode::kSuccess,
1368 metrics::CheckResult::kUpdateAvailable,
1369 metrics::CheckReaction::kUpdating,
1370 metrics::DownloadErrorCode::kUnset,
1371 &response,
1372 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001373 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001374}
Jay Srinivasan0a708742012-03-20 11:26:12 -07001375
Adolfo Victoria497044c2018-07-18 07:51:42 -07001376TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) {
1377 // If staging is on, the value for max days to scatter should be ignored, and
1378 // staging's scatter value should be used.
1379 OmahaResponse response;
1380 request_params_.set_wall_clock_based_wait_enabled(true);
1381 request_params_.set_waiting_period(TimeDelta::FromDays(6));
1382 request_params_.set_update_check_count_wait_enabled(false);
1383
May Lippert60aa3ca2018-08-15 16:55:29 -07001384 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now());
1385
Adolfo Victoria497044c2018-07-18 07:51:42 -07001386 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6));
1387 // This should not prevent scattering due to staging.
1388 fake_update_response_.max_days_to_scatter = "0";
1389 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1390 -1,
1391 false, // ping_only
1392 ErrorCode::kOmahaUpdateDeferredPerPolicy,
1393 metrics::CheckResult::kUpdateAvailable,
1394 metrics::CheckReaction::kDeferring,
1395 metrics::DownloadErrorCode::kUnset,
1396 &response,
1397 nullptr));
1398 EXPECT_FALSE(response.update_exists);
1399
1400 // Interactive updates should not be affected.
1401 request_params_.set_interactive(true);
1402 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1403 -1,
1404 false, // ping_only
1405 ErrorCode::kSuccess,
1406 metrics::CheckResult::kUpdateAvailable,
1407 metrics::CheckReaction::kUpdating,
1408 metrics::DownloadErrorCode::kUnset,
1409 &response,
1410 nullptr));
1411 EXPECT_TRUE(response.update_exists);
1412}
1413
Alex Deymo8e18f932015-03-27 16:16:59 -07001414TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
1415 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001416 fake_update_response_.include_cohorts = true;
1417 fake_update_response_.cohort = "s/154454/8479665";
1418 fake_update_response_.cohorthint = "please-put-me-on-beta";
1419 fake_update_response_.cohortname = "stable";
1420
Marton Hunyady2abda312018-04-24 18:21:49 +02001421 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Alex Deymo8e18f932015-03-27 16:16:59 -07001422 -1,
1423 false, // ping_only
1424 ErrorCode::kSuccess,
1425 metrics::CheckResult::kUpdateAvailable,
1426 metrics::CheckReaction::kUpdating,
1427 metrics::DownloadErrorCode::kUnset,
1428 &response,
1429 nullptr));
1430
1431 string value;
1432 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1433 EXPECT_EQ(fake_update_response_.cohort, value);
1434
1435 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1436 EXPECT_EQ(fake_update_response_.cohorthint, value);
1437
1438 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1439 EXPECT_EQ(fake_update_response_.cohortname, value);
1440}
1441
1442TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
1443 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001444 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1445 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1446 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1447 fake_update_response_.include_cohorts = true;
1448 fake_update_response_.cohort = "s/154454/8479665";
1449 fake_update_response_.cohorthint = "please-put-me-on-beta";
1450 fake_update_response_.cohortname = "";
1451
Marton Hunyady2abda312018-04-24 18:21:49 +02001452 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Alex Deymo8e18f932015-03-27 16:16:59 -07001453 -1,
1454 false, // ping_only
1455 ErrorCode::kSuccess,
1456 metrics::CheckResult::kUpdateAvailable,
1457 metrics::CheckReaction::kUpdating,
1458 metrics::DownloadErrorCode::kUnset,
1459 &response,
1460 nullptr));
1461
1462 string value;
1463 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1464 EXPECT_EQ(fake_update_response_.cohort, value);
1465
1466 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1467 EXPECT_EQ(fake_update_response_.cohorthint, value);
1468
1469 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1470}
1471
1472TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
1473 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001474 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1475
Marton Hunyady2abda312018-04-24 18:21:49 +02001476 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Alex Deymo8e18f932015-03-27 16:16:59 -07001477 -1,
1478 false, // ping_only
1479 ErrorCode::kSuccess,
1480 metrics::CheckResult::kUpdateAvailable,
1481 metrics::CheckReaction::kUpdating,
1482 metrics::DownloadErrorCode::kUnset,
1483 &response,
1484 nullptr));
1485
1486 string value;
1487 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1488 EXPECT_EQ("old_value", value);
1489
1490 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1491 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1492}
1493
Alex Deymo00d79ac2015-06-29 15:41:49 -07001494TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
1495 OmahaResponse response;
Alex Deymo00d79ac2015-06-29 15:41:49 -07001496 fake_update_response_.include_cohorts = true;
1497 fake_update_response_.cohort = "s/154454/8479665";
1498 fake_update_response_.cohorthint = "please-put-me-on-beta";
1499 fake_update_response_.cohortname = "stable";
1500
Marton Hunyady2abda312018-04-24 18:21:49 +02001501 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
Alex Deymo00d79ac2015-06-29 15:41:49 -07001502 -1,
1503 false, // ping_only
1504 ErrorCode::kSuccess,
1505 metrics::CheckResult::kNoUpdateAvailable,
1506 metrics::CheckReaction::kUnset,
1507 metrics::DownloadErrorCode::kUnset,
1508 &response,
1509 nullptr));
1510
1511 string value;
1512 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1513 EXPECT_EQ(fake_update_response_.cohort, value);
1514
1515 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1516 EXPECT_EQ(fake_update_response_.cohorthint, value);
1517
1518 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1519 EXPECT_EQ(fake_update_response_.cohortname, value);
1520}
1521
Sen Jiangb1e063a2017-09-15 17:44:31 -07001522TEST_F(OmahaRequestActionTest, MultiAppCohortTest) {
1523 OmahaResponse response;
Sen Jiangb1e063a2017-09-15 17:44:31 -07001524 fake_update_response_.multi_app = true;
1525 fake_update_response_.include_cohorts = true;
1526 fake_update_response_.cohort = "s/154454/8479665";
1527 fake_update_response_.cohorthint = "please-put-me-on-beta";
1528 fake_update_response_.cohortname = "stable";
1529
Marton Hunyady2abda312018-04-24 18:21:49 +02001530 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiangb1e063a2017-09-15 17:44:31 -07001531 -1,
1532 false, // ping_only
1533 ErrorCode::kSuccess,
1534 metrics::CheckResult::kUpdateAvailable,
1535 metrics::CheckReaction::kUpdating,
1536 metrics::DownloadErrorCode::kUnset,
1537 &response,
1538 nullptr));
1539
1540 string value;
1541 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1542 EXPECT_EQ(fake_update_response_.cohort, value);
1543
1544 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1545 EXPECT_EQ(fake_update_response_.cohorthint, value);
1546
1547 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1548 EXPECT_EQ(fake_update_response_.cohortname, value);
1549}
1550
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001551TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001552 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001553
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001554 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001555 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001556
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001557 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001558 &fake_system_state_,
1559 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001560 std::make_unique<MockHttpFetcher>(
1561 http_response.data(), http_response.size(), nullptr),
Ben Chan5c02c132017-06-27 07:10:36 -07001562 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001563 ActionProcessor processor;
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001564 processor.set_delegate(&delegate_);
1565 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001566
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001567 loop.PostTask(base::Bind(
1568 [](ActionProcessor* processor) { processor->StartProcessing(); },
1569 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001570 loop.Run();
1571 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001572 EXPECT_FALSE(processor.IsRunning());
1573}
1574
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001575TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001576 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001577 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
1578 -1,
1579 false, // ping_only
1580 ErrorCode::kOmahaRequestXMLParseError,
1581 metrics::CheckResult::kParsingError,
1582 metrics::CheckReaction::kUnset,
1583 metrics::DownloadErrorCode::kUnset,
1584 &response,
1585 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001586 EXPECT_FALSE(response.update_exists);
1587}
1588
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001589TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001590 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001591 ASSERT_FALSE(TestUpdateCheck("",
1592 -1,
1593 false, // ping_only
1594 ErrorCode::kOmahaRequestEmptyResponseError,
1595 metrics::CheckResult::kParsingError,
1596 metrics::CheckReaction::kUnset,
1597 metrics::DownloadErrorCode::kUnset,
1598 &response,
1599 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001600 EXPECT_FALSE(response.update_exists);
1601}
1602
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001603TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001604 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001605 ASSERT_FALSE(TestUpdateCheck(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001606 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1607 "<daystart elapsed_seconds=\"100\"/>"
1608 "<app appid=\"foo\" status=\"ok\">"
1609 "<ping status=\"ok\"/>"
1610 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001611 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001612 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001613 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001614 metrics::CheckResult::kParsingError,
1615 metrics::CheckReaction::kUnset,
1616 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001617 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001618 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001619 EXPECT_FALSE(response.update_exists);
1620}
1621
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001622TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001623 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001624 ASSERT_FALSE(TestUpdateCheck(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001625 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1626 "<daystart elapsed_seconds=\"100\"/>"
1627 "<app appid=\"foo\" status=\"ok\">"
1628 "<ping status=\"ok\"/>"
1629 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001630 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001631 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001632 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001633 metrics::CheckResult::kParsingError,
1634 metrics::CheckReaction::kUnset,
1635 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001636 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001637 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001638 EXPECT_FALSE(response.update_exists);
1639}
1640
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001641TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001642 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001643 ASSERT_FALSE(TestUpdateCheck(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001644 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1645 "<daystart elapsed_seconds=\"100\"/>"
1646 "<app appid=\"foo\" status=\"ok\">"
1647 "<ping status=\"ok\"/>"
1648 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001649 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001650 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001651 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001652 metrics::CheckResult::kParsingError,
1653 metrics::CheckReaction::kUnset,
1654 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001655 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001656 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001657 EXPECT_FALSE(response.update_exists);
1658}
1659
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001660TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001661 string input_response =
1662 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1663 "<daystart elapsed_seconds=\"100\"/>"
Aaron Wood7dcdedf2017-09-06 17:17:41 -07001664 // the appid needs to match that in the request params
1665 "<app appid=\"" +
1666 fake_update_response_.app_id +
1667 "\" status=\"ok\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001668 "<updatecheck status=\"ok\">"
1669 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001670 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001671 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001672 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001673 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001674 "Prompt=\"false\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001675 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001676 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001677 "/></actions></manifest></updatecheck></app></response>";
1678 LOG(INFO) << "Input Response = " << input_response;
1679
Darin Petkov6a5b3222010-07-13 14:55:28 -07001680 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001681 ASSERT_TRUE(TestUpdateCheck(input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -07001682 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001683 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001684 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001685 metrics::CheckResult::kUpdateAvailable,
1686 metrics::CheckReaction::kUpdating,
1687 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001688 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001689 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001690 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001691 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001692 EXPECT_EQ("http://missing/field/test/f",
1693 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001694 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001695 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1696 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001697 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001698 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001699}
1700
1701namespace {
1702class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1703 public:
1704 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001705 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001706 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001707};
1708
Alex Deymo60ca1a72015-06-18 18:19:15 -07001709void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001710 processor->StartProcessing();
1711 CHECK(processor->IsRunning());
1712 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001713}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001714} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001715
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001716TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001717 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001718 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001719
Alex Deymo60ca1a72015-06-18 18:19:15 -07001720 string http_response("doesn't matter");
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001721 auto action = std::make_unique<OmahaRequestAction>(
Ben Chan5c02c132017-06-27 07:10:36 -07001722 &fake_system_state_,
1723 nullptr,
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001724 std::make_unique<MockHttpFetcher>(
1725 http_response.data(), http_response.size(), nullptr),
Ben Chan5c02c132017-06-27 07:10:36 -07001726 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001727 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001728 ActionProcessor processor;
1729 processor.set_delegate(&delegate);
Amin Hassanid3f4bea2018-04-30 14:52:40 -07001730 processor.EnqueueAction(std::move(action));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001731
Alex Deymo60ca1a72015-06-18 18:19:15 -07001732 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1733 loop.Run();
1734 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001735}
1736
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001737TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001738 string output;
1739 EXPECT_TRUE(XmlEncode("ab", &output));
1740 EXPECT_EQ("ab", output);
1741 EXPECT_TRUE(XmlEncode("a<b", &output));
1742 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001743 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1744 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001745 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1746 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001747 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001748 EXPECT_FALSE(XmlEncode("\xc2", &output));
1749 // Fail with invalid ASCII-7 chars.
1750 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1751}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001752
Alex Deymob0d74eb2015-03-30 17:59:17 -07001753TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1754 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1755 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1756}
1757
1758TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001759 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001760
1761 // Make sure XML Encode is being called on the params
Marton Hunyady2abda312018-04-24 18:21:49 +02001762 request_params_.set_os_sp("testtheservice_pack>");
1763 request_params_.set_os_board("x86 generic<id");
1764 request_params_.set_current_channel("unittest_track&lt;");
1765 request_params_.set_target_channel("unittest_track&lt;");
1766 request_params_.set_hwid("<OEM MODEL>");
Alex Deymo8e18f932015-03-27 16:16:59 -07001767 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1768 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001769 fake_prefs_.SetString(
1770 kPrefsOmahaCohortName,
1771 base::JoinString(vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001772 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02001773 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
1774 -1,
1775 false, // ping_only
1776 ErrorCode::kOmahaRequestXMLParseError,
1777 metrics::CheckResult::kParsingError,
1778 metrics::CheckReaction::kUnset,
1779 metrics::DownloadErrorCode::kUnset,
1780 &response,
1781 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001782 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001783 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001784 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1785 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1786 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1787 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1788 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1789 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1790 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1791 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1792 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1793 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1794 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1795 // Values from Prefs that are too big are removed from the XML instead of
1796 // encoded.
1797 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001798}
1799
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001800TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001801 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001802 fake_update_response_.deadline = "&lt;20110101";
1803 fake_update_response_.more_info_url = "testthe&lt;url";
1804 fake_update_response_.codebase = "testthe&amp;codebase/";
Marton Hunyady2abda312018-04-24 18:21:49 +02001805 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1806 -1,
1807 false, // ping_only
1808 ErrorCode::kSuccess,
1809 metrics::CheckResult::kUpdateAvailable,
1810 metrics::CheckReaction::kUpdating,
1811 metrics::DownloadErrorCode::kUnset,
1812 &response,
1813 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001814
Sen Jiang0affc2c2017-02-10 15:55:05 -08001815 EXPECT_EQ("testthe<url", response.more_info_url);
1816 EXPECT_EQ("testthe&codebase/file.signed",
1817 response.packages[0].payload_urls[0]);
1818 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001819}
1820
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001821TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001822 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001823 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001824 fake_update_response_.size = 123123123123123ull;
Marton Hunyady2abda312018-04-24 18:21:49 +02001825 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
1826 -1,
1827 false, // ping_only
1828 ErrorCode::kSuccess,
1829 metrics::CheckResult::kUpdateAvailable,
1830 metrics::CheckReaction::kUpdating,
1831 metrics::DownloadErrorCode::kUnset,
1832 &response,
1833 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001834
Sen Jiang0affc2c2017-02-10 15:55:05 -08001835 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001836}
1837
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001838TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001839 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001840 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001841 fake_system_state_.set_prefs(&prefs);
1842
Darin Petkov95508da2011-01-05 12:42:29 -08001843 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001844 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001845 // An existing but empty previous version means that we didn't reboot to a new
1846 // update, therefore, no need to update the previous version.
1847 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Marton Hunyady2abda312018-04-24 18:21:49 +02001848 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001849 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001850 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001851 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001852 metrics::CheckResult::kParsingError,
1853 metrics::CheckReaction::kUnset,
1854 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001855 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001856 &post_data));
1857 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001858 string post_str(post_data.begin(), post_data.end());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001859 EXPECT_NE(
1860 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
1861 " <updatecheck></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001862 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001863 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1864 string::npos);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001865 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos);
1866 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001867 // No <event> tag should be sent if we didn't reboot to an update.
1868 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001869}
1870
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001871TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001872 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02001873 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkove17f86b2010-07-20 09:12:01 -07001874 "invalid xml>",
1875 &post_data);
1876 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001877 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001878 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001879 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001880 OmahaEvent::kTypeUpdateDownloadStarted,
1881 OmahaEvent::kResultSuccess);
1882 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001883 EXPECT_EQ(post_str.find("ping"), string::npos);
1884 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001885}
1886
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001887TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001888 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02001889 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
Darin Petkove17f86b2010-07-20 09:12:01 -07001890 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001891 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001892 "invalid xml>",
1893 &post_data);
1894 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001895 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001896 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001897 " <event eventtype=\"%d\" eventresult=\"%d\" "
1898 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001899 OmahaEvent::kTypeDownloadComplete,
1900 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001901 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001902 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001903 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001904}
1905
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001906TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001907 string http_response("doesn't matter");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001908 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001909 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001910 nullptr,
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001911 std::make_unique<MockHttpFetcher>(
1912 http_response.data(), http_response.size(), nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001913 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001914 EXPECT_FALSE(update_check_action.IsEvent());
1915
1916 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001917 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001918 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001919 std::make_unique<MockHttpFetcher>(
1920 http_response.data(), http_response.size(), nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001921 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001922 EXPECT_TRUE(event_action.IsEvent());
1923}
1924
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001925TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001926 for (int i = 0; i < 2; i++) {
1927 bool delta_okay = i == 1;
1928 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001929 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02001930
1931 request_params_.set_delta_okay(delta_okay);
1932
1933 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001934 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001935 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001936 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001937 metrics::CheckResult::kParsingError,
1938 metrics::CheckReaction::kUnset,
1939 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001940 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001941 &post_data));
1942 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001943 string post_str(post_data.begin(), post_data.end());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08001944 EXPECT_NE(
1945 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1946 string::npos)
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001947 << "i = " << i;
1948 }
1949}
1950
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001951TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001952 for (int i = 0; i < 2; i++) {
1953 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001954 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001955 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001956 FakeSystemState fake_system_state;
Marton Hunyady2abda312018-04-24 18:21:49 +02001957
1958 request_params_.set_interactive(interactive);
1959
1960 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001961 -1,
1962 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001963 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001964 metrics::CheckResult::kParsingError,
1965 metrics::CheckReaction::kUnset,
1966 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001967 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001968 &post_data));
1969 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001970 string post_str(post_data.begin(), post_data.end());
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001971 EXPECT_NE(post_str.find(
1972 base::StringPrintf("installsource=\"%s\"", interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001973 string::npos)
1974 << "i = " << i;
1975 }
1976}
1977
Marton Hunyadyba51c3f2018-04-25 15:18:10 +02001978TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) {
1979 for (int i = 0; i < 2; i++) {
1980 bool target_version_set = i == 1;
1981 const char* target_version_prefix = target_version_set ? "10032." : "";
1982 brillo::Blob post_data;
1983 FakeSystemState fake_system_state;
1984
1985 request_params_.set_target_version_prefix(target_version_prefix);
1986
1987 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
1988 -1,
1989 false, // ping_only
1990 ErrorCode::kOmahaRequestXMLParseError,
1991 metrics::CheckResult::kParsingError,
1992 metrics::CheckReaction::kUnset,
1993 metrics::DownloadErrorCode::kUnset,
1994 nullptr,
1995 &post_data));
1996 // convert post_data to string
1997 string post_str(post_data.begin(), post_data.end());
1998 if (target_version_set) {
1999 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"),
2000 string::npos)
2001 << "i = " << i;
2002 } else {
2003 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos)
2004 << "i = " << i;
2005 }
2006 }
2007}
2008
2009TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) {
2010 for (int i = 0; i < 4; i++) {
2011 bool rollback_allowed = i / 2 == 0;
2012 bool target_version_set = i % 2 == 0;
2013 brillo::Blob post_data;
2014 FakeSystemState fake_system_state;
2015
2016 request_params_.set_target_version_prefix(target_version_set ? "10032."
2017 : "");
2018 request_params_.set_rollback_allowed(rollback_allowed);
2019
2020 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
2021 -1,
2022 false, // ping_only
2023 ErrorCode::kOmahaRequestXMLParseError,
2024 metrics::CheckResult::kParsingError,
2025 metrics::CheckReaction::kUnset,
2026 metrics::DownloadErrorCode::kUnset,
2027 nullptr,
2028 &post_data));
2029 // convert post_data to string
2030 string post_str(post_data.begin(), post_data.end());
2031 if (rollback_allowed && target_version_set) {
2032 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos)
2033 << "i = " << i;
2034 } else {
2035 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i;
2036 }
2037 }
2038}
2039
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002040TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07002041 OmahaEvent default_event;
2042 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
2043 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002044 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07002045
2046 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
2047 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
2048 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002049 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07002050
2051 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
2052 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002053 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07002054 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
2055 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002056 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07002057}
2058
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002059void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002060 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002061 fake_system_state_.set_prefs(&prefs);
2062 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002063 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002064 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
2065 // Add a few hours to the day difference to test no rounding, etc.
2066 int64_t five_days_ago =
2067 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
2068 int64_t six_days_ago =
2069 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
2070 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002071 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002072 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002073 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002074 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002075 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002076 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002077 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
Tianjie Xu282aa1f2017-09-05 13:42:45 -07002078 -1,
2079 ping_only,
2080 ErrorCode::kSuccess,
2081 metrics::CheckResult::kNoUpdateAvailable,
2082 metrics::CheckReaction::kUnset,
2083 metrics::DownloadErrorCode::kUnset,
2084 nullptr,
2085 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002086 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002087 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
2088 string::npos);
2089 if (ping_only) {
2090 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
2091 EXPECT_EQ(post_str.find("previousversion"), string::npos);
2092 } else {
2093 EXPECT_NE(post_str.find("updatecheck"), string::npos);
2094 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07002095 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002096}
2097
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002098TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002099 PingTest(true /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002100}
2101
2102TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002103 PingTest(false /* ping_only */);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002104}
2105
2106TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002107 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002108 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002109 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002110 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002111 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002112 int64_t three_days_ago =
2113 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
2114 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002115 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002116 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002117 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002118 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002119 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002120 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002121 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002122 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
2123 -1,
2124 false, // ping_only
2125 ErrorCode::kSuccess,
2126 metrics::CheckResult::kNoUpdateAvailable,
2127 metrics::CheckReaction::kUnset,
2128 metrics::DownloadErrorCode::kUnset,
2129 nullptr,
2130 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002131 string post_str(post_data.begin(), post_data.end());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002132 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002133}
2134
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002135TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002136 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002137 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002138 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002139 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002140 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002141 int64_t four_days_ago =
2142 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
2143 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002144 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002145 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002146 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002147 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002148 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002149 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002150 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002151 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
2152 -1,
2153 false, // ping_only
2154 ErrorCode::kSuccess,
2155 metrics::CheckResult::kNoUpdateAvailable,
2156 metrics::CheckReaction::kUnset,
2157 metrics::DownloadErrorCode::kUnset,
2158 nullptr,
2159 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002160 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002161 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07002162 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002163}
2164
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002165TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002166 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002167 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002168 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002169 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002170 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002171 int64_t one_hour_ago =
2172 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002173 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002174 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002175 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002176 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002177 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002178 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002179 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
2180 // send a ping.
2181 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2182 .WillOnce(Return(true));
2183 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2184 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002185 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002186 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
2187 -1,
2188 false, // ping_only
2189 ErrorCode::kSuccess,
2190 metrics::CheckResult::kNoUpdateAvailable,
2191 metrics::CheckReaction::kUnset,
2192 metrics::DownloadErrorCode::kUnset,
2193 nullptr,
2194 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002195 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002196 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002197}
2198
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002199TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07002200 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08002201 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002202 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07002203 int64_t now = Time::Now().ToInternalValue();
2204 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002205 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002206 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002207 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07002208 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2209 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002210 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002211 EXPECT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
2212 -1,
2213 true, // ping_only
2214 ErrorCode::kSuccess,
2215 metrics::CheckResult::kUnset,
2216 metrics::CheckReaction::kUnset,
2217 metrics::DownloadErrorCode::kUnset,
2218 nullptr,
2219 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08002220 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07002221}
2222
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002223TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002224 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002225 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002226 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002227 .Times(AnyNumber());
David Zeuthen33bae492014-02-25 16:16:18 -08002228 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002229 int64_t future =
2230 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08002231 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002232 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002233 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002234 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002235 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07002236 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002237 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
2238 .WillOnce(Return(true));
2239 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
2240 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002241 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002242 ASSERT_TRUE(
Marton Hunyady2abda312018-04-24 18:21:49 +02002243 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002244 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002245 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002246 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07002247 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07002248 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002249 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002250 metrics::CheckResult::kNoUpdateAvailable,
2251 metrics::CheckReaction::kUnset,
2252 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002253 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002254 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002255 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002256 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002257}
2258
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002259TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002260 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07002261 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002262 // may fail if it runs for longer than 5 seconds. It shouldn't run
2263 // that long though.
2264 int64_t midnight =
2265 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
2266 int64_t midnight_slack =
2267 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08002268 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002269 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002270 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2271 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002272 EXPECT_CALL(prefs,
2273 SetInt64(kPrefsLastActivePingDay,
2274 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002275 .WillOnce(Return(true));
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002276 EXPECT_CALL(prefs,
2277 SetInt64(kPrefsLastRollCallPingDay,
2278 AllOf(Ge(midnight), Le(midnight_slack))))
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002279 .WillOnce(Return(true));
2280 ASSERT_TRUE(
Marton Hunyady2abda312018-04-24 18:21:49 +02002281 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002282 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002283 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002284 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07002285 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07002286 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002287 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002288 metrics::CheckResult::kNoUpdateAvailable,
2289 metrics::CheckReaction::kUnset,
2290 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002291 nullptr,
2292 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002293}
2294
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002295TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002296 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002297 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002298 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2299 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002300 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2301 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
2302 ASSERT_TRUE(
Marton Hunyady2abda312018-04-24 18:21:49 +02002303 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002304 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002305 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002306 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07002307 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07002308 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002309 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002310 metrics::CheckResult::kNoUpdateAvailable,
2311 metrics::CheckReaction::kUnset,
2312 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002313 nullptr,
2314 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002315}
2316
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002317TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08002318 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002319 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08002320 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
2321 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002322 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
2323 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
2324 ASSERT_TRUE(
Marton Hunyady2abda312018-04-24 18:21:49 +02002325 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002326 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002327 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07002328 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07002329 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07002330 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002331 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002332 metrics::CheckResult::kNoUpdateAvailable,
2333 metrics::CheckReaction::kUnset,
2334 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002335 nullptr,
2336 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07002337}
2338
Alex Deymob3fa53b2016-04-18 19:57:58 -07002339TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
2340 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
2341 // the "eol" attribute.
2342 ASSERT_TRUE(
Marton Hunyady2abda312018-04-24 18:21:49 +02002343 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
Alex Deymob3fa53b2016-04-18 19:57:58 -07002344 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
2345 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
2346 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/>"
2347 "</app></response>",
2348 -1,
2349 false, // ping_only
2350 ErrorCode::kSuccess,
2351 metrics::CheckResult::kNoUpdateAvailable,
2352 metrics::CheckReaction::kUnset,
2353 metrics::DownloadErrorCode::kUnset,
2354 nullptr,
2355 nullptr));
2356 string eol_pref;
2357 EXPECT_TRUE(
2358 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
2359 // Note that the eol="eol" attribute should be ignored and the _eol should be
2360 // used instead.
2361 EXPECT_EQ("security-only", eol_pref);
2362}
2363
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002364TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002365 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002366 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07002367 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07002368 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002369 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002370 metrics::CheckResult::kParsingError,
2371 metrics::CheckReaction::kUnset,
2372 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002373 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07002374 &post_data));
2375 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002376 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07002377 EXPECT_EQ(post_str.find("machineid="), string::npos);
2378 EXPECT_EQ(post_str.find("userid="), string::npos);
2379}
2380
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002381TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07002382 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002383 const int http_error_code =
2384 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Marton Hunyady2abda312018-04-24 18:21:49 +02002385 ASSERT_FALSE(TestUpdateCheck("",
2386 501,
2387 false, // ping_only
2388 static_cast<ErrorCode>(http_error_code),
2389 metrics::CheckResult::kDownloadError,
2390 metrics::CheckReaction::kUnset,
2391 static_cast<metrics::DownloadErrorCode>(501),
2392 &response,
2393 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07002394 EXPECT_FALSE(response.update_exists);
2395}
2396
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002397TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07002398 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002399 const int http_error_code =
2400 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Marton Hunyady2abda312018-04-24 18:21:49 +02002401 ASSERT_FALSE(TestUpdateCheck("",
2402 1500,
2403 false, // ping_only
2404 static_cast<ErrorCode>(http_error_code),
2405 metrics::CheckResult::kDownloadError,
2406 metrics::CheckReaction::kUnset,
2407 metrics::DownloadErrorCode::kHttpStatusOther,
2408 &response,
2409 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07002410 EXPECT_FALSE(response.update_exists);
2411}
2412
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002413TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002414 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02002415 request_params_.set_wall_clock_based_wait_enabled(true);
2416 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2417 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002418
Sen Jiang7c1171e2016-06-23 11:35:40 -07002419 Time arbitrary_date;
Eric Caruso761be2c2018-05-22 16:23:33 -07002420 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002421 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
Marton Hunyady2abda312018-04-24 18:21:49 +02002422 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang7c1171e2016-06-23 11:35:40 -07002423 -1,
2424 false, // ping_only
2425 ErrorCode::kOmahaUpdateDeferredPerPolicy,
2426 metrics::CheckResult::kUpdateAvailable,
2427 metrics::CheckReaction::kDeferring,
2428 metrics::DownloadErrorCode::kUnset,
2429 &response,
2430 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002431
Ben Chan9abb7632014-08-07 00:10:53 -07002432 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002433 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002434 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002435 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002436
2437 // Verify if we are interactive check we don't defer.
Marton Hunyady2abda312018-04-24 18:21:49 +02002438 request_params_.set_interactive(true);
2439 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang7c1171e2016-06-23 11:35:40 -07002440 -1,
2441 false, // ping_only
2442 ErrorCode::kSuccess,
2443 metrics::CheckResult::kUpdateAvailable,
2444 metrics::CheckReaction::kUpdating,
2445 metrics::DownloadErrorCode::kUnset,
2446 &response,
2447 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07002448 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002449}
2450
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002451TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002452 OmahaResponse response;
Marton Hunyady2abda312018-04-24 18:21:49 +02002453 request_params_.set_wall_clock_based_wait_enabled(true);
2454 request_params_.set_waiting_period(TimeDelta().FromDays(1));
2455 request_params_.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002456
Sen Jiang7c1171e2016-06-23 11:35:40 -07002457 Time t1, t2;
Eric Caruso761be2c2018-05-22 16:23:33 -07002458 ASSERT_TRUE(Time::FromString("1/1/2012", &t1));
2459 ASSERT_TRUE(Time::FromString("1/3/2012", &t2));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002460 ASSERT_TRUE(
2461 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2462 fake_system_state_.fake_clock()->SetWallclockTime(t2);
Marton Hunyady2abda312018-04-24 18:21:49 +02002463 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
Sen Jiang7c1171e2016-06-23 11:35:40 -07002464 -1,
2465 false, // ping_only
2466 ErrorCode::kSuccess,
2467 metrics::CheckResult::kUpdateAvailable,
2468 metrics::CheckReaction::kUpdating,
2469 metrics::DownloadErrorCode::kUnset,
2470 &response,
2471 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002472
2473 EXPECT_TRUE(response.update_exists);
2474
2475 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002476 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002477 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002478 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2479}
2480
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002481TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002482 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002483 base::ScopedTempDir tempdir;
2484 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002485
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002486 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002487 request_params_.set_root(tempdir.GetPath().value());
2488 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}");
2489 request_params_.set_app_version("1.2.3.4");
2490 request_params_.set_product_components("o.bundle=1");
2491 request_params_.set_current_channel("canary-channel");
2492 EXPECT_TRUE(
2493 request_params_.SetTargetChannel("stable-channel", true, nullptr));
2494 request_params_.UpdateDownloadChannel();
2495 EXPECT_TRUE(request_params_.ShouldPowerwash());
2496 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002497 -1,
2498 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002499 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002500 metrics::CheckResult::kParsingError,
2501 metrics::CheckReaction::kUnset,
2502 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002503 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002504 &post_data));
2505 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002506 string post_str(post_data.begin(), post_data.end());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002507 EXPECT_NE(
2508 string::npos,
2509 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" "
2510 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2511 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Sen Jiang8cd42342018-01-31 12:06:59 -08002512 EXPECT_EQ(string::npos, post_str.find("o.bundle"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002513}
2514
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002515TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002516 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002517 base::ScopedTempDir tempdir;
2518 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002519
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002520 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002521 request_params_.set_root(tempdir.GetPath().value());
2522 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}");
2523 request_params_.set_app_version("5.6.7.8");
2524 request_params_.set_product_components("o.bundle=1");
2525 request_params_.set_current_channel("stable-channel");
2526 EXPECT_TRUE(
2527 request_params_.SetTargetChannel("canary-channel", false, nullptr));
2528 request_params_.UpdateDownloadChannel();
2529 EXPECT_FALSE(request_params_.ShouldPowerwash());
2530 ASSERT_FALSE(TestUpdateCheck("invalid xml>",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002531 -1,
2532 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002533 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002534 metrics::CheckResult::kParsingError,
2535 metrics::CheckReaction::kUnset,
2536 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002537 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002538 &post_data));
Xiaochu Liu88d90382018-08-29 16:09:11 -07002539 // Convert post_data to string.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002540 string post_str(post_data.begin(), post_data.end());
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002541 EXPECT_NE(
2542 string::npos,
2543 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" "
2544 "version=\"5.6.7.8\" "
2545 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002546 EXPECT_EQ(string::npos, post_str.find("from_version"));
Sen Jiang8cd42342018-01-31 12:06:59 -08002547 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\""));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002548}
2549
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002550// Checks that the initial ping with a=-1 r=-1 is not send when the device
2551// was powerwashed.
2552TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2553 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2554
2555 // Flag that the device was powerwashed in the past.
2556 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
2557
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002558 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002559 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
2560 -1,
2561 false, // ping_only
2562 ErrorCode::kSuccess,
2563 metrics::CheckResult::kNoUpdateAvailable,
2564 metrics::CheckReaction::kUnset,
2565 metrics::DownloadErrorCode::kUnset,
2566 nullptr,
2567 &post_data));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002568 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002569 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002570 EXPECT_EQ(string::npos, post_str.find("<ping"));
2571}
2572
Amin Hassani1677e812017-06-21 13:36:36 -07002573// Checks that the initial ping with a=-1 r=-1 is not send when the device
2574// first_active_omaha_ping_sent is set.
2575TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2576 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2577
2578 // Flag that the device was not powerwashed in the past.
2579 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2580
2581 // Flag that the device has sent first active ping in the past.
2582 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2583
2584 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002585 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
2586 -1,
2587 false, // ping_only
2588 ErrorCode::kSuccess,
2589 metrics::CheckResult::kNoUpdateAvailable,
2590 metrics::CheckReaction::kUnset,
2591 metrics::DownloadErrorCode::kUnset,
2592 nullptr,
2593 &post_data));
Amin Hassani1677e812017-06-21 13:36:36 -07002594 // We shouldn't send a ping in this case since
2595 // first_active_omaha_ping_sent=true
2596 string post_str(post_data.begin(), post_data.end());
2597 EXPECT_EQ(string::npos, post_str.find("<ping"));
2598}
2599
Alex Deymo9fded1e2015-11-05 12:31:19 -08002600// Checks that the event 54 is sent on a reboot to a new update.
2601TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2602 // Flag that the device was updated in a previous boot.
2603 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2604
2605 brillo::Blob post_data;
Marton Hunyady2abda312018-04-24 18:21:49 +02002606 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
2607 -1,
2608 false, // ping_only
2609 ErrorCode::kSuccess,
2610 metrics::CheckResult::kNoUpdateAvailable,
2611 metrics::CheckReaction::kUnset,
2612 metrics::DownloadErrorCode::kUnset,
2613 nullptr,
2614 &post_data));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002615 string post_str(post_data.begin(), post_data.end());
2616
2617 // An event 54 is included and has the right version.
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002618 EXPECT_NE(
2619 string::npos,
2620 post_str.find(base::StringPrintf("<event eventtype=\"%d\"",
2621 OmahaEvent::kTypeRebootedAfterUpdate)));
Alex Deymo9fded1e2015-11-05 12:31:19 -08002622 EXPECT_NE(string::npos,
2623 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2624
2625 // The previous version flag should have been removed.
2626 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2627 string prev_version;
2628 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2629 EXPECT_TRUE(prev_version.empty());
2630}
2631
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002632void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading,
2633 bool initial_allow_p2p_for_sharing,
2634 bool omaha_disable_p2p_for_downloading,
2635 bool omaha_disable_p2p_for_sharing,
2636 bool payload_state_allow_p2p_attempt,
2637 bool expect_p2p_client_lookup,
2638 const string& p2p_client_result_url,
2639 bool expected_allow_p2p_for_downloading,
2640 bool expected_allow_p2p_for_sharing,
2641 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07002642 OmahaResponse response;
Gilad Arnold74b5f552014-10-07 08:17:16 -07002643 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2644 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2645 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002646
2647 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002648 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002649 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2650 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002651 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2652 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2653 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2654 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2655 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2656 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2657 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2658 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2659 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2660 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2661
David Zeuthen8f191b22013-08-06 12:27:50 -07002662 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002663 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002664 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2665
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002666 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2667 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002668 .Times(expect_p2p_client_lookup ? 1 : 0);
2669
Alex Deymo8e18f932015-03-27 16:16:59 -07002670 fake_update_response_.disable_p2p_for_downloading =
2671 omaha_disable_p2p_for_downloading;
2672 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
Marton Hunyady2abda312018-04-24 18:21:49 +02002673 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
2674 -1,
2675 false, // ping_only
2676 ErrorCode::kSuccess,
2677 metrics::CheckResult::kUpdateAvailable,
2678 metrics::CheckReaction::kUpdating,
2679 metrics::DownloadErrorCode::kUnset,
2680 &response,
2681 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07002682 EXPECT_TRUE(response.update_exists);
2683
Gilad Arnold74b5f552014-10-07 08:17:16 -07002684 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2685 response.disable_p2p_for_downloading);
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002686 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002687
Gilad Arnold74b5f552014-10-07 08:17:16 -07002688 EXPECT_EQ(expected_allow_p2p_for_downloading,
2689 actual_allow_p2p_for_downloading);
2690 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2691 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002692}
2693
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002694TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002695 P2PTest(true, // initial_allow_p2p_for_downloading
2696 true, // initial_allow_p2p_for_sharing
2697 false, // omaha_disable_p2p_for_downloading
2698 false, // omaha_disable_p2p_for_sharing
2699 true, // payload_state_allow_p2p_attempt
2700 true, // expect_p2p_client_lookup
2701 "http://1.3.5.7/p2p", // p2p_client_result_url
2702 true, // expected_allow_p2p_for_downloading
2703 true, // expected_allow_p2p_for_sharing
2704 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002705}
2706
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002707TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002708 P2PTest(true, // initial_allow_p2p_for_downloading
2709 true, // initial_allow_p2p_for_sharing
2710 false, // omaha_disable_p2p_for_downloading
2711 false, // omaha_disable_p2p_for_sharing
2712 true, // payload_state_allow_p2p_attempt
2713 true, // expect_p2p_client_lookup
2714 "", // p2p_client_result_url
2715 false, // expected_allow_p2p_for_downloading
2716 true, // expected_allow_p2p_for_sharing
2717 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002718}
2719
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002720TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002721 P2PTest(false, // initial_allow_p2p_for_downloading
2722 true, // initial_allow_p2p_for_sharing
2723 false, // omaha_disable_p2p_for_downloading
2724 false, // omaha_disable_p2p_for_sharing
2725 true, // payload_state_allow_p2p_attempt
2726 false, // expect_p2p_client_lookup
2727 "unset", // p2p_client_result_url
2728 false, // expected_allow_p2p_for_downloading
2729 true, // expected_allow_p2p_for_sharing
2730 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002731}
2732
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002733TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002734 P2PTest(true, // initial_allow_p2p_for_downloading
2735 true, // initial_allow_p2p_for_sharing
2736 true, // omaha_disable_p2p_for_downloading
2737 false, // omaha_disable_p2p_for_sharing
2738 true, // payload_state_allow_p2p_attempt
2739 false, // expect_p2p_client_lookup
2740 "unset", // p2p_client_result_url
2741 false, // expected_allow_p2p_for_downloading
2742 true, // expected_allow_p2p_for_sharing
2743 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002744}
2745
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002746TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002747 P2PTest(true, // initial_allow_p2p_for_downloading
2748 true, // initial_allow_p2p_for_sharing
2749 false, // omaha_disable_p2p_for_downloading
2750 true, // omaha_disable_p2p_for_sharing
2751 true, // payload_state_allow_p2p_attempt
2752 true, // expect_p2p_client_lookup
2753 "http://1.3.5.7/p2p", // p2p_client_result_url
2754 true, // expected_allow_p2p_for_downloading
2755 false, // expected_allow_p2p_for_sharing
2756 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002757}
2758
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002759TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002760 P2PTest(true, // initial_allow_p2p_for_downloading
2761 true, // initial_allow_p2p_for_sharing
2762 true, // omaha_disable_p2p_for_downloading
2763 true, // omaha_disable_p2p_for_sharing
2764 true, // payload_state_allow_p2p_attempt
2765 false, // expect_p2p_client_lookup
2766 "unset", // p2p_client_result_url
2767 false, // expected_allow_p2p_for_downloading
2768 false, // expected_allow_p2p_for_sharing
2769 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002770}
2771
Amin Hassani7cc8bb02019-01-14 16:29:47 -08002772bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days,
2773 OmahaResponse* response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002774 fake_update_response_.elapsed_days = elapsed_days;
Marton Hunyady2abda312018-04-24 18:21:49 +02002775 return TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
2776 -1,
2777 false, // ping_only
2778 ErrorCode::kSuccess,
2779 metrics::CheckResult::kUpdateAvailable,
2780 metrics::CheckReaction::kUpdating,
2781 metrics::DownloadErrorCode::kUnset,
2782 response,
2783 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002784}
2785
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002786TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002787 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002788
Kevin Cernekee2494e282016-03-29 18:03:53 -07002789 // Simulate a successful update check that happens during OOBE. The
2790 // deadline in the response is needed to force the update attempt to
2791 // occur; responses without a deadline seen during OOBE will normally
2792 // return ErrorCode::kNonCriticalUpdateInOOBE.
2793 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2794 fake_update_response_.deadline = "20101020";
2795
David Zeuthen639aa362014-02-03 16:23:44 -08002796 // Check that we parse elapsed_days in the Omaha Response correctly.
2797 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002798 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2799 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002800 EXPECT_TRUE(response.update_exists);
2801 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002802 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002803 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002804 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002805 EXPECT_EQ(prefs_days, 42);
2806
2807 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002808 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002809 EXPECT_TRUE(response.update_exists);
2810 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002811 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002812 EXPECT_EQ(prefs_days, 42);
2813
2814 // Note that elapsed_days is not necessarily divisible by 7 so check
2815 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002816 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2817 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002818 EXPECT_TRUE(response.update_exists);
2819 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002820 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002821 EXPECT_EQ(prefs_days, 21);
2822
2823 // Check that we correctly handle elapsed_days not being included in
2824 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002825 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002826 EXPECT_TRUE(response.update_exists);
2827 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002828}
2829
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002830// If there is no prefs and OOBE is not complete, we should not
2831// report anything to Omaha.
2832TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002833 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002834 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2835 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2836}
David Zeuthen639aa362014-02-03 16:23:44 -08002837
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002838// If OOBE is complete and happened on a valid date (e.g. after Jan
2839// 1 2007 0:00 PST), that date should be used and written to
2840// prefs. However, first try with an invalid date and check we do
2841// nothing.
2842TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2843 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2844 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2845 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2846 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2847}
David Zeuthen639aa362014-02-03 16:23:44 -08002848
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002849// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2850// should yield an InstallDate of 14.
2851TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2852 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2853 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2854 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2855 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002856
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002857 int64_t prefs_days;
2858 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2859 EXPECT_EQ(prefs_days, 14);
2860}
David Zeuthen639aa362014-02-03 16:23:44 -08002861
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002862// Now that we have a valid date in prefs, check that we keep using
2863// that even if OOBE date reports something else. The date Jan 30,
2864// 2007 0:00 PST should yield an InstallDate of 28... but since
2865// there's a prefs file, we should still get 14.
2866TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2867 // Set a valid date in the prefs first.
2868 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002869
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002870 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2871 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2872 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002873
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002874 int64_t prefs_days;
2875 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2876 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002877
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002878 // If we delete the prefs file, we should get 28 days.
2879 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2880 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2881 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2882 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002883}
2884
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002885// Verifies that a device with no device policy, and is not a consumer
2886// device sets the max kernel key version to the current version.
2887// ie. the same behavior as if rollback is enabled.
2888TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) {
2889 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2890
2891 // Setup and verify some initial default values for the kernel TPM
2892 // values that control verified boot and rollback.
2893 const int min_kernel_version = 4;
2894 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2895 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2896 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2897 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2898
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002899 EXPECT_CALL(
2900 *fake_system_state_.mock_metrics_reporter(),
2901 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2902 .Times(1);
2903
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002904 OmahaResponse response;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002905 TestRollbackCheck(false /* is_consumer_device */,
2906 3 /* rollback_allowed_milestones */,
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002907 false /* is_policy_loaded */,
2908 &response);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002909
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002910 // Verify kernel_max_rollforward was set to the current minimum
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002911 // kernel key version. This has the effect of freezing roll
2912 // forwards indefinitely. This will hold the rollback window
2913 // open until a future change will be able to move this forward
2914 // relative the configured window.
2915 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2916 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2917}
2918
2919// Verifies that a conmsumer device with no device policy sets the
2920// max kernel key version to the current version. ie. the same
2921// behavior as if rollback is enabled.
2922TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) {
2923 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2924
2925 // Setup and verify some initial default values for the kernel TPM
2926 // values that control verified boot and rollback.
2927 const int min_kernel_version = 3;
2928 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2929 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2930 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2931 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2932
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002933 EXPECT_CALL(
2934 *fake_system_state_.mock_metrics_reporter(),
2935 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
2936 .Times(1);
2937
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002938 OmahaResponse response;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002939 TestRollbackCheck(true /* is_consumer_device */,
2940 3 /* rollback_allowed_milestones */,
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002941 false /* is_policy_loaded */,
2942 &response);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002943
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002944 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002945 // was set to logical infinity. This is the expected behavior for
2946 // consumer devices and matches the existing behavior prior to the
2947 // rollback features.
2948 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2949 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2950}
2951
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002952// Verifies that a device with rollback enabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002953// in the TPM to prevent roll forward.
2954TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) {
2955 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2956
2957 // Setup and verify some initial default values for the kernel TPM
2958 // values that control verified boot and rollback.
2959 const int allowed_milestones = 4;
2960 const int min_kernel_version = 3;
2961 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2962 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2963 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2964 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2965
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02002966 EXPECT_CALL(
2967 *fake_system_state_.mock_metrics_reporter(),
2968 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true))
2969 .Times(1);
2970
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002971 OmahaResponse response;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002972 TestRollbackCheck(false /* is_consumer_device */,
2973 allowed_milestones,
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07002974 true /* is_policy_loaded */,
2975 &response);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002976
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002977 // Verify that with rollback enabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002978 // was set to the current minimum kernel key version. This has
2979 // the effect of freezing roll forwards indefinitely. This will
2980 // hold the rollback window open until a future change will
2981 // be able to move this forward relative the configured window.
2982 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2983 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward());
2984}
2985
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07002986// Verifies that a device with rollback disabled sets kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08002987// in the TPM to logical infinity, to allow roll forward.
2988TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) {
2989 FakeHardware* fake_hw = fake_system_state_.fake_hardware();
2990
2991 // Setup and verify some initial default values for the kernel TPM
2992 // values that control verified boot and rollback.
2993 const int allowed_milestones = 0;
2994 const int min_kernel_version = 3;
2995 fake_hw->SetMinKernelKeyVersion(min_kernel_version);
2996 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity);
2997 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
2998 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
2999
Marton Hunyadyffbfdfb2018-05-30 13:03:29 +02003000 EXPECT_CALL(
3001 *fake_system_state_.mock_metrics_reporter(),
3002 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true))
3003 .Times(1);
3004
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07003005 OmahaResponse response;
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08003006 TestRollbackCheck(false /* is_consumer_device */,
3007 allowed_milestones,
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07003008 true /* is_policy_loaded */,
3009 &response);
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08003010
Zentaro Kavanagh5d956152018-05-15 09:40:33 -07003011 // Verify that with rollback disabled that kernel_max_rollforward
Zentaro Kavanagh1f899d52018-02-27 15:02:47 -08003012 // was set to logical infinity.
3013 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion());
3014 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward());
3015}
3016
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07003017TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) {
3018 OmahaResponse response;
3019 fake_update_response_.rollback = true;
3020 TestRollbackCheck(false /* is_consumer_device */,
3021 4 /* rollback_allowed_milestones */,
3022 true /* is_policy_loaded */,
3023 &response);
3024 EXPECT_TRUE(response.is_rollback);
3025}
3026
3027TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) {
3028 OmahaResponse response;
3029 fake_update_response_.rollback_firmware_version = "1.2";
3030 fake_update_response_.rollback_kernel_version = "3.4";
3031 fake_update_response_.rollback = true;
3032 TestRollbackCheck(false /* is_consumer_device */,
3033 4 /* rollback_allowed_milestones */,
3034 true /* is_policy_loaded */,
3035 &response);
3036 EXPECT_TRUE(response.is_rollback);
3037 EXPECT_EQ(1, response.rollback_key_version.firmware_key);
3038 EXPECT_EQ(2, response.rollback_key_version.firmware);
3039 EXPECT_EQ(3, response.rollback_key_version.kernel_key);
3040 EXPECT_EQ(4, response.rollback_key_version.kernel);
3041}
3042
May Lippert60aa3ca2018-08-15 16:55:29 -07003043TEST_F(OmahaRequestActionTest,
3044 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) {
3045 FakeClock fake_clock;
3046 Time now = Time::Now();
3047 fake_clock.SetWallclockTime(now);
3048 fake_system_state_.set_clock(&fake_clock);
3049
3050 OmahaResponse response;
3051 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
3052 -1,
3053 false, // ping_only
3054 ErrorCode::kSuccess,
3055 metrics::CheckResult::kUpdateAvailable,
3056 metrics::CheckReaction::kUpdating,
3057 metrics::DownloadErrorCode::kUnset,
3058 &response,
3059 nullptr));
3060 EXPECT_TRUE(response.update_exists);
3061 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
3062
3063 int64_t stored_first_seen_at_time;
3064 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt,
3065 &stored_first_seen_at_time));
3066 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time);
3067}
3068
3069TEST_F(OmahaRequestActionTest,
3070 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) {
3071 FakeClock fake_clock;
3072 Time now = Time::Now();
3073 fake_clock.SetWallclockTime(now);
3074 fake_system_state_.set_clock(&fake_clock);
3075
3076 OmahaResponse response;
3077 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(),
3078 -1,
3079 false, // ping_only
3080 ErrorCode::kSuccess,
3081 metrics::CheckResult::kNoUpdateAvailable,
3082 metrics::CheckReaction::kUnset,
3083 metrics::DownloadErrorCode::kUnset,
3084 &response,
3085 nullptr));
3086 EXPECT_FALSE(response.update_exists);
3087 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt));
3088}
3089
Xiaochu Liu88d90382018-08-29 16:09:11 -07003090TEST_F(OmahaRequestActionTest, InstallTest) {
3091 OmahaResponse response;
Xiaochu Liu6310be62018-10-11 15:09:03 -07003092 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08003093 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu88d90382018-08-29 16:09:11 -07003094 brillo::Blob post_data;
3095 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
3096 -1,
3097 false, // ping_only
3098 true, // is_consumer_device
3099 0, // rollback_allowed_milestones
3100 false, // is_policy_loaded
3101 ErrorCode::kSuccess,
3102 metrics::CheckResult::kUpdateAvailable,
3103 metrics::CheckReaction::kUpdating,
3104 metrics::DownloadErrorCode::kUnset,
3105 &response,
3106 &post_data));
3107 // Convert post_data to string.
3108 string post_str(post_data.begin(), post_data.end());
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08003109 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) {
Xiaochu Liu88d90382018-08-29 16:09:11 -07003110 EXPECT_NE(string::npos,
Xiaochu Liu6310be62018-10-11 15:09:03 -07003111 post_str.find("appid=\"" + fake_update_response_.app_id + "_" +
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08003112 dlc_module_id + "\""));
Xiaochu Liu88d90382018-08-29 16:09:11 -07003113 }
Xiaochu Liu6310be62018-10-11 15:09:03 -07003114 EXPECT_NE(string::npos,
3115 post_str.find("appid=\"" + fake_update_response_.app_id + "\""));
3116
3117 // Count number of updatecheck tag in response.
3118 int updatecheck_count = 0;
3119 size_t pos = 0;
3120 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) {
3121 updatecheck_count++;
3122 pos++;
3123 }
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08003124 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count);
Xiaochu Liu6310be62018-10-11 15:09:03 -07003125}
3126
3127TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) {
3128 fake_update_response_.multi_app_skip_updatecheck = true;
3129 fake_update_response_.multi_app_no_update = false;
3130 request_params_.set_is_install(true);
Xiaochu Liuf53a5d32018-11-26 13:48:59 -08003131 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"});
Xiaochu Liu6310be62018-10-11 15:09:03 -07003132 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck);
3133 OmahaResponse response;
3134 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(),
3135 -1,
3136 false, // ping_only
3137 ErrorCode::kSuccess,
3138 metrics::CheckResult::kUpdateAvailable,
3139 metrics::CheckReaction::kUpdating,
3140 metrics::DownloadErrorCode::kUnset,
3141 &response,
3142 nullptr));
3143 EXPECT_TRUE(response.update_exists);
3144 EXPECT_EQ(fake_update_response_.current_version, response.version);
Xiaochu Liu88d90382018-08-29 16:09:11 -07003145}
3146
Darin Petkov6a5b3222010-07-13 14:55:28 -07003147} // namespace chromeos_update_engine