blob: c310e72a62e330786535889be3a39542363015e3 [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>
23#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070024
Alex Deymo60ca1a72015-06-18 18:19:15 -070025#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070026#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070027#include <base/files/scoped_temp_dir.h>
Ben Chan5c02c132017-06-27 07:10:36 -070028#include <base/memory/ptr_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070029#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070030#include <base/strings/string_util.h>
31#include <base/strings/stringprintf.h>
32#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070033#include <brillo/bind_lambda.h>
34#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>
Jay Srinivasand29695d2013-04-08 15:08:05 -070038
Alex Deymo39910dc2015-11-09 17:04:30 -080039#include "update_engine/common/action_pipe.h"
40#include "update_engine/common/constants.h"
41#include "update_engine/common/fake_prefs.h"
42#include "update_engine/common/hash_calculator.h"
43#include "update_engine/common/mock_http_fetcher.h"
44#include "update_engine/common/platform_constants.h"
45#include "update_engine/common/prefs.h"
46#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030047#include "update_engine/fake_system_state.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080048#include "update_engine/metrics.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070049#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070050#include "update_engine/mock_payload_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070051#include "update_engine/omaha_request_params.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070052
Darin Petkov1cbd78f2010-07-29 12:38:34 -070053using base::Time;
54using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070055using std::string;
56using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070057using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070058using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070059using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070060using testing::Ge;
61using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080062using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070063using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070064using testing::ReturnPointee;
65using testing::SaveArg;
Ben Chan672c1f52017-10-23 15:41:39 -070066using testing::SetArgPointee;
Alex Deymof329b932014-10-30 01:37:48 -070067using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070068
Alex Deymo8e18f932015-03-27 16:16:59 -070069namespace {
70
Alex Deymo85616652015-10-15 18:48:31 -070071const char kTestAppId[] = "test-app-id";
72
Alex Deymo8e18f932015-03-27 16:16:59 -070073// This is a helper struct to allow unit tests build an update response with the
74// values they care about.
75struct FakeUpdateResponse {
76 string GetNoUpdateResponse() const {
77 string entity_str;
78 if (include_entity)
79 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070080 return
81 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
82 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070083 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070084 "<app appid=\"" + app_id + "\" " +
85 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
86 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
87 " status=\"ok\">"
88 "<ping status=\"ok\"/>"
89 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070090 }
91
92 string GetUpdateResponse() const {
93 return
94 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
95 "protocol=\"3.0\">"
96 "<daystart elapsed_seconds=\"100\"" +
97 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
98 + "/>"
99 "<app appid=\"" + app_id + "\" " +
100 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
101 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
102 " status=\"ok\">"
103 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
104 "<urls><url codebase=\"" + codebase + "\"/></urls>"
105 "<manifest version=\"" + version + "\">"
106 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
107 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
108 "<actions><action event=\"postinstall\" "
109 "ChromeOSVersion=\"" + version + "\" "
110 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
111 "IsDelta=\"true\" "
112 "IsDeltaPayload=\"true\" "
113 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
114 "sha256=\"" + hash + "\" "
115 "needsadmin=\"" + needsadmin + "\" " +
116 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
117 (disable_p2p_for_downloading ?
118 "DisableP2PForDownloading=\"true\" " : "") +
119 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
120 "/></actions></manifest></updatecheck></app></response>";
121 }
122
123 // Return the payload URL, which is split in two fields in the XML response.
124 string GetPayloadUrl() {
125 return codebase + filename;
126 }
127
Alex Deymo85616652015-10-15 18:48:31 -0700128 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700129 string version = "1.2.3.4";
130 string more_info_url = "http://more/info";
131 string prompt = "true";
132 string codebase = "http://code/base/";
133 string filename = "file.signed";
134 string hash = "HASH1234=";
135 string needsadmin = "false";
136 int64_t size = 123;
137 string deadline = "";
138 string max_days_to_scatter = "7";
139 string elapsed_days = "42";
140
141 // P2P setting defaults to allowed.
142 bool disable_p2p_for_downloading = false;
143 bool disable_p2p_for_sharing = false;
144
145 // Omaha cohorts settings.
146 bool include_cohorts = false;
147 string cohort = "";
148 string cohorthint = "";
149 string cohortname = "";
150
151 // Whether to include the CrOS <!ENTITY> in the XML response.
152 bool include_entity = false;
153};
154
155} // namespace
156
Darin Petkov6a5b3222010-07-13 14:55:28 -0700157namespace chromeos_update_engine {
158
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700159class OmahaRequestActionTest : public ::testing::Test {
160 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800161 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700162 fake_system_state_.set_request_params(&request_params_);
163 fake_system_state_.set_prefs(&fake_prefs_);
164 }
165
166 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800167 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700168 // is used. |payload_state| may be null, in which case a local mock is used.
169 // |p2p_manager| may be null, in which case a local mock is used.
170 // |connection_manager| may be null, in which case a local mock is used.
171 // out_response may be null. If |fail_http_response_code| is non-negative,
172 // the transfer will fail with that code. |ping_only| is passed through to the
173 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
174 // post-data received by the mock HttpFetcher is returned.
175 //
176 // The |expected_check_result|, |expected_check_reaction| and
177 // |expected_error_code| parameters are for checking expectations
178 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
179 // UMA statistics. Use the appropriate ::kUnset value to specify that
180 // the given metric should not be reported.
181 bool TestUpdateCheck(OmahaRequestParams* request_params,
182 const string& http_response,
183 int fail_http_response_code,
184 bool ping_only,
185 ErrorCode expected_code,
186 metrics::CheckResult expected_check_result,
187 metrics::CheckReaction expected_check_reaction,
188 metrics::DownloadErrorCode expected_download_error_code,
189 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700190 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700191
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800192 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700193 // only a ping or also an updatecheck.
194 void PingTest(bool ping_only);
195
196 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700197 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700198 OmahaResponse *response);
199
200 // P2P test helper function.
201 void P2PTest(
202 bool initial_allow_p2p_for_downloading,
203 bool initial_allow_p2p_for_sharing,
204 bool omaha_disable_p2p_for_downloading,
205 bool omaha_disable_p2p_for_sharing,
206 bool payload_state_allow_p2p_attempt,
207 bool expect_p2p_client_lookup,
208 const string& p2p_client_result_url,
209 bool expected_allow_p2p_for_downloading,
210 bool expected_allow_p2p_for_sharing,
211 const string& expected_p2p_url);
212
213 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700214 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700215
216 // By default, all tests use these objects unless they replace them in the
217 // fake_system_state_.
218 OmahaRequestParams request_params_ = OmahaRequestParams{
219 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700220 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700221 OmahaRequestParams::kOsVersion,
222 "service_pack",
223 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700224 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700225 "0.1.0.0",
226 "en-US",
227 "unittest",
228 "OEM MODEL 09235 7471",
229 "ChromeOSFirmware.1.0",
230 "0X0A1",
231 false, // delta okay
232 false, // interactive
233 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700234 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700235
236 FakePrefs fake_prefs_;
237};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238
239namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700240class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
241 public:
242 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700243 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800244 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700245 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800246 void ProcessingDone(const ActionProcessor* processor,
247 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700248 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700249 }
250
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800251 void ActionCompleted(ActionProcessor* processor,
252 AbstractAction* action,
253 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700254 // make sure actions always succeed
255 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700256 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700257 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700258 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700259 }
David Zeuthena99981f2013-04-29 13:42:47 -0700260 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700261};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700262} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700263
264class OutputObjectCollectorAction;
265
266template<>
267class ActionTraits<OutputObjectCollectorAction> {
268 public:
269 // Does not take an object for input
270 typedef OmahaResponse InputObjectType;
271 // On success, puts the output path on output
272 typedef NoneType OutputObjectType;
273};
274
275class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
276 public:
277 OutputObjectCollectorAction() : has_input_object_(false) {}
278 void PerformAction() {
279 // copy input object
280 has_input_object_ = HasInputObject();
281 if (has_input_object_)
282 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700283 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700284 }
285 // Should never be called
286 void TerminateProcessing() {
287 CHECK(false);
288 }
289 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700290 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700291 return "OutputObjectCollectorAction";
292 }
Alex Deymof329b932014-10-30 01:37:48 -0700293 string Type() const { return StaticType(); }
Alex Vakulenko0057daa2016-01-23 16:22:50 -0800294 using InputObjectType =
295 ActionTraits<OutputObjectCollectorAction>::InputObjectType;
296 using OutputObjectType =
297 ActionTraits<OutputObjectCollectorAction>::OutputObjectType;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700298 bool has_input_object_;
299 OmahaResponse omaha_response_;
300};
301
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700302bool OmahaRequestActionTest::TestUpdateCheck(
303 OmahaRequestParams* request_params,
304 const string& http_response,
305 int fail_http_response_code,
306 bool ping_only,
307 ErrorCode expected_code,
308 metrics::CheckResult expected_check_result,
309 metrics::CheckReaction expected_check_reaction,
310 metrics::DownloadErrorCode expected_download_error_code,
311 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700312 brillo::Blob* out_post_data) {
313 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700314 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700315 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800316 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700317 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700318 if (fail_http_response_code >= 0) {
319 fetcher->FailTransfer(fail_http_response_code);
320 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700321 if (request_params)
322 fake_system_state_.set_request_params(request_params);
323 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700324 nullptr,
Ben Chan5c02c132017-06-27 07:10:36 -0700325 base::WrapUnique(fetcher),
Darin Petkov265f2902011-05-09 15:17:40 -0700326 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700327 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700328 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700329
Darin Petkov6a5b3222010-07-13 14:55:28 -0700330 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700331 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700332 processor.EnqueueAction(&action);
333
334 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700335 BondActions(&action, &collector_action);
336 processor.EnqueueAction(&collector_action);
337
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700338 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800339 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700340 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800341 SendEnumToUMA(metrics::kMetricCheckResult,
342 static_cast<int>(expected_check_result),
343 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
344 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700345 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800346 SendEnumToUMA(metrics::kMetricCheckReaction,
347 static_cast<int>(expected_check_reaction),
348 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
349 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700350 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700351 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
352 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800353 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
354 ? 0 : 1);
355
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700356 loop.PostTask(base::Bind(
357 [](ActionProcessor* processor) { processor->StartProcessing(); },
358 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700359 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700360 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700361 if (collector_action.has_input_object_ && out_response)
362 *out_response = collector_action.omaha_response_;
363 if (out_post_data)
364 *out_post_data = fetcher->post_data();
365 return collector_action.has_input_object_;
366}
367
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700368// Tests Event requests -- they should always succeed. |out_post_data|
369// may be null; if non-null, the post-data received by the mock
370// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700371void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700372 OmahaEvent* event,
373 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700374 brillo::Blob* out_post_data) {
375 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700376 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700377 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800378 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700379 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700380 FakeSystemState fake_system_state;
381 fake_system_state.set_request_params(&params);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300382 OmahaRequestAction action(&fake_system_state,
383 event,
Ben Chan5c02c132017-06-27 07:10:36 -0700384 base::WrapUnique(fetcher),
Alex Deymoc1c17b42015-11-23 03:53:15 -0300385 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700386 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700387 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700388 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700389 processor.EnqueueAction(&action);
390
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700391 loop.PostTask(base::Bind(
392 [](ActionProcessor* processor) { processor->StartProcessing(); },
393 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700394 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700395 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700396
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700397 if (out_post_data)
398 *out_post_data = fetcher->post_data();
399}
400
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700401TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400402 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700403 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400404 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700405 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700406 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400407 -1,
408 false, // ping_only
409 ErrorCode::kOmahaRequestXMLHasEntityDecl,
410 metrics::CheckResult::kParsingError,
411 metrics::CheckReaction::kUnset,
412 metrics::DownloadErrorCode::kUnset,
413 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700414 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400415 EXPECT_FALSE(response.update_exists);
416}
417
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700418TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700419 OmahaResponse response;
420 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700421 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700422 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700423 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700424 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700425 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800426 metrics::CheckResult::kNoUpdateAvailable,
427 metrics::CheckReaction::kUnset,
428 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700429 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700430 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700431 EXPECT_FALSE(response.update_exists);
432}
433
Alex Deymo8e18f932015-03-27 16:16:59 -0700434// Test that all the values in the response are parsed in a normal update
435// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700436TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700437 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700438 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700439 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700440 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700441 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700442 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700443 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700444 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800445 metrics::CheckResult::kUpdateAvailable,
446 metrics::CheckReaction::kUpdating,
447 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700448 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700449 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700450 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700451 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700452 EXPECT_EQ(fake_update_response_.version, response.version);
453 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
454 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
455 EXPECT_EQ(fake_update_response_.hash, response.hash);
456 EXPECT_EQ(fake_update_response_.size, response.size);
457 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
458 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
459 // Omaha cohort attribets are not set in the response, so they should not be
460 // persisted.
461 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
462 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
463 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700464}
465
Alex Deymo14ad88e2016-06-29 12:30:14 -0700466TEST_F(OmahaRequestActionTest, ExtraHeadersSentTest) {
467 const string http_response = "<?xml invalid response";
468 request_params_.set_interactive(true);
469
470 brillo::FakeMessageLoop loop(nullptr);
471 loop.SetAsCurrent();
472
473 MockHttpFetcher* fetcher =
474 new MockHttpFetcher(http_response.data(), http_response.size(), nullptr);
Ben Chan5c02c132017-06-27 07:10:36 -0700475 OmahaRequestAction action(&fake_system_state_, nullptr,
476 base::WrapUnique(fetcher), false);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700477 ActionProcessor processor;
478 processor.EnqueueAction(&action);
479
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700480 loop.PostTask(base::Bind(
481 [](ActionProcessor* processor) { processor->StartProcessing(); },
482 base::Unretained(&processor)));
Alex Deymo14ad88e2016-06-29 12:30:14 -0700483 loop.Run();
484 EXPECT_FALSE(loop.PendingTasks());
485
486 // Check that the headers were set in the fetcher during the action. Note that
487 // we set this request as "interactive".
488 EXPECT_EQ("fg", fetcher->GetHeader("X-GoogleUpdate-Interactivity"));
489 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-GoogleUpdate-AppId"));
490 EXPECT_NE("", fetcher->GetHeader("X-GoogleUpdate-Updater"));
491}
492
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700493TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700494 OmahaResponse response;
495 // Set up a connection manager that doesn't allow a valid update over
496 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700497 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700498 fake_system_state_.set_connection_manager(&mock_cm);
499
Alex Deymo30534502015-07-20 15:06:33 -0700500 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
501 .WillRepeatedly(
Ben Chan672c1f52017-10-23 15:41:39 -0700502 DoAll(SetArgPointee<0>(ConnectionType::kEthernet),
503 SetArgPointee<1>(ConnectionTethering::kUnknown),
Alex Deymo30534502015-07-20 15:06:33 -0700504 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700505 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
506 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700507
508 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700509 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700510 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700511 -1,
512 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700513 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700514 metrics::CheckResult::kUpdateAvailable,
515 metrics::CheckReaction::kIgnored,
516 metrics::DownloadErrorCode::kUnset,
517 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700518 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700519 EXPECT_FALSE(response.update_exists);
520}
521
Weidong Guo4b0d6032017-04-17 10:08:38 -0700522TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
523 // This test tests that update over cellular is allowed as device policy
524 // says yes.
525 OmahaResponse response;
526 MockConnectionManager mock_cm;
527
528 fake_system_state_.set_connection_manager(&mock_cm);
529
530 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
531 .WillRepeatedly(
Ben Chan672c1f52017-10-23 15:41:39 -0700532 DoAll(SetArgPointee<0>(ConnectionType::kCellular),
533 SetArgPointee<1>(ConnectionTethering::kUnknown),
Weidong Guo4b0d6032017-04-17 10:08:38 -0700534 Return(true)));
535 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
536 .WillRepeatedly(Return(true));
537 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
538 .WillRepeatedly(Return(true));
539
540 ASSERT_TRUE(
541 TestUpdateCheck(nullptr, // request_params
542 fake_update_response_.GetUpdateResponse(),
543 -1,
544 false, // ping_only
545 ErrorCode::kSuccess,
546 metrics::CheckResult::kUpdateAvailable,
547 metrics::CheckReaction::kUpdating,
548 metrics::DownloadErrorCode::kUnset,
549 &response,
550 nullptr));
551 EXPECT_TRUE(response.update_exists);
552}
553
554TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
555 // This test tests that update over cellular is blocked as device policy
556 // says no.
557 OmahaResponse response;
558 MockConnectionManager mock_cm;
559
560 fake_system_state_.set_connection_manager(&mock_cm);
561
562 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
563 .WillRepeatedly(
Ben Chan672c1f52017-10-23 15:41:39 -0700564 DoAll(SetArgPointee<0>(ConnectionType::kCellular),
565 SetArgPointee<1>(ConnectionTethering::kUnknown),
Weidong Guo4b0d6032017-04-17 10:08:38 -0700566 Return(true)));
567 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
568 .WillRepeatedly(Return(true));
569 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
570 .WillRepeatedly(Return(false));
571
572 ASSERT_FALSE(
573 TestUpdateCheck(nullptr, // request_params
574 fake_update_response_.GetUpdateResponse(),
575 -1,
576 false, // ping_only
577 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
578 metrics::CheckResult::kUpdateAvailable,
579 metrics::CheckReaction::kIgnored,
580 metrics::DownloadErrorCode::kUnset,
581 &response,
582 nullptr));
583 EXPECT_FALSE(response.update_exists);
584}
585
586TEST_F(OmahaRequestActionTest,
587 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
588 // This test tests that, when device policy is not set, update over cellular
589 // is allowed as permission for update over cellular is set to true.
590 OmahaResponse response;
591 MockConnectionManager mock_cm;
592
593 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
594 fake_system_state_.set_connection_manager(&mock_cm);
595
596 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
597 .WillRepeatedly(
Ben Chan672c1f52017-10-23 15:41:39 -0700598 DoAll(SetArgPointee<0>(ConnectionType::kCellular),
599 SetArgPointee<1>(ConnectionTethering::kUnknown),
Weidong Guo4b0d6032017-04-17 10:08:38 -0700600 Return(true)));
601 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
602 .WillRepeatedly(Return(false));
603 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
604 .WillRepeatedly(Return(true));
605
606 ASSERT_TRUE(
607 TestUpdateCheck(nullptr, // request_params
608 fake_update_response_.GetUpdateResponse(),
609 -1,
610 false, // ping_only
611 ErrorCode::kSuccess,
612 metrics::CheckResult::kUpdateAvailable,
613 metrics::CheckReaction::kUpdating,
614 metrics::DownloadErrorCode::kUnset,
615 &response,
616 nullptr));
617 EXPECT_TRUE(response.update_exists);
618}
619
620TEST_F(OmahaRequestActionTest,
621 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
622 // This test tests that, when device policy is not set and permission for
623 // update over cellular is set to false or does not exist, update over
624 // cellular is blocked as update target does not match the omaha response.
625 OmahaResponse response;
626 MockConnectionManager mock_cm;
627 // A version different from the version in omaha response.
628 string diff_version = "99.99.99";
629 // A size different from the size in omaha response.
630 int64_t diff_size = 999;
631
632 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
633 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
634 // This test tests cellular (3G) being the only connection type being allowed.
635 fake_system_state_.set_connection_manager(&mock_cm);
636
637 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
638 .WillRepeatedly(
Ben Chan672c1f52017-10-23 15:41:39 -0700639 DoAll(SetArgPointee<0>(ConnectionType::kCellular),
640 SetArgPointee<1>(ConnectionTethering::kUnknown),
Weidong Guo4b0d6032017-04-17 10:08:38 -0700641 Return(true)));
642 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
643 .WillRepeatedly(Return(false));
644 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
645 .WillRepeatedly(Return(true));
646
647 ASSERT_FALSE(
648 TestUpdateCheck(nullptr, // request_params
649 fake_update_response_.GetUpdateResponse(),
650 -1,
651 false, // ping_only
652 ErrorCode::kOmahaUpdateIgnoredOverCellular,
653 metrics::CheckResult::kUpdateAvailable,
654 metrics::CheckReaction::kIgnored,
655 metrics::DownloadErrorCode::kUnset,
656 &response,
657 nullptr));
658 EXPECT_FALSE(response.update_exists);
659}
660
661TEST_F(OmahaRequestActionTest,
662 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
663 // This test tests that, when device policy is not set and permission for
664 // update over cellular is set to false or does not exist, update over
665 // cellular is allowed as update target matches the omaha response.
666 OmahaResponse response;
667 MockConnectionManager mock_cm;
668 // A version same as the version in omaha response.
669 string new_version = fake_update_response_.version;
670 // A size same as the size in omaha response.
671 int64_t new_size = fake_update_response_.size;
672
673 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
674 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
675 fake_system_state_.set_connection_manager(&mock_cm);
676
677 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
678 .WillRepeatedly(
Ben Chan672c1f52017-10-23 15:41:39 -0700679 DoAll(SetArgPointee<0>(ConnectionType::kCellular),
680 SetArgPointee<1>(ConnectionTethering::kUnknown),
Weidong Guo4b0d6032017-04-17 10:08:38 -0700681 Return(true)));
682 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
683 .WillRepeatedly(Return(false));
684 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
685 .WillRepeatedly(Return(true));
686
687 ASSERT_TRUE(
688 TestUpdateCheck(nullptr, // request_params
689 fake_update_response_.GetUpdateResponse(),
690 -1,
691 false, // ping_only
692 ErrorCode::kSuccess,
693 metrics::CheckResult::kUpdateAvailable,
694 metrics::CheckReaction::kUpdating,
695 metrics::DownloadErrorCode::kUnset,
696 &response,
697 nullptr));
698 EXPECT_TRUE(response.update_exists);
699}
700
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700701TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700702 string rollback_version = "1234.0.0";
703 OmahaResponse response;
704
705 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700706 fake_system_state_.set_payload_state(&mock_payload_state);
707
Chris Sosa77f79e82014-06-02 18:16:24 -0700708 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
709 .WillRepeatedly(Return(rollback_version));
710
Alex Deymo8e18f932015-03-27 16:16:59 -0700711 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700712 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700713 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700714 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700715 -1,
716 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700717 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700718 metrics::CheckResult::kUpdateAvailable,
719 metrics::CheckReaction::kIgnored,
720 metrics::DownloadErrorCode::kUnset,
721 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700722 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700723 EXPECT_FALSE(response.update_exists);
724}
725
Kevin Cernekee2494e282016-03-29 18:03:53 -0700726// Verify that update checks called during OOBE will only try to download
727// an update if the response includes a non-empty deadline field.
728TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
729 OmahaResponse response;
730
731 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
732 ASSERT_FALSE(
733 TestUpdateCheck(nullptr, // request_params
734 fake_update_response_.GetUpdateResponse(),
735 -1,
736 false, // ping_only
737 ErrorCode::kNonCriticalUpdateInOOBE,
738 metrics::CheckResult::kUnset,
739 metrics::CheckReaction::kUnset,
740 metrics::DownloadErrorCode::kUnset,
741 &response,
742 nullptr));
743 EXPECT_FALSE(response.update_exists);
744
Alex Deymo46a9aae2016-05-04 20:20:11 -0700745 // The IsOOBEComplete() value is ignored when the OOBE flow is not enabled.
746 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
747 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
748 fake_update_response_.GetUpdateResponse(),
749 -1,
750 false, // ping_only
751 ErrorCode::kSuccess,
752 metrics::CheckResult::kUpdateAvailable,
753 metrics::CheckReaction::kUpdating,
754 metrics::DownloadErrorCode::kUnset,
755 &response,
756 nullptr));
757 EXPECT_TRUE(response.update_exists);
758 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
759
760 // The payload is applied when a deadline was set in the response.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700761 fake_update_response_.deadline = "20101020";
762 ASSERT_TRUE(
763 TestUpdateCheck(nullptr, // request_params
764 fake_update_response_.GetUpdateResponse(),
765 -1,
766 false, // ping_only
767 ErrorCode::kSuccess,
768 metrics::CheckResult::kUpdateAvailable,
769 metrics::CheckReaction::kUpdating,
770 metrics::DownloadErrorCode::kUnset,
771 &response,
772 nullptr));
773 EXPECT_TRUE(response.update_exists);
774}
775
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700776TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700777 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700778 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700779 params.set_wall_clock_based_wait_enabled(true);
780 params.set_update_check_count_wait_enabled(false);
781 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700782
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700783 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700784 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700785 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700786 -1,
787 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700788 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800789 metrics::CheckResult::kUpdateAvailable,
790 metrics::CheckReaction::kDeferring,
791 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700792 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700793 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700794 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700795
796 // Verify if we are interactive check we don't defer.
797 params.set_interactive(true);
798 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700799 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700800 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700801 -1,
802 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700803 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800804 metrics::CheckResult::kUpdateAvailable,
805 metrics::CheckReaction::kUpdating,
806 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700807 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700808 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700809 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700810}
811
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700812TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700813 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700814 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700815 params.set_wall_clock_based_wait_enabled(false);
816 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700817
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700818 params.set_update_check_count_wait_enabled(true);
819 params.set_min_update_checks_needed(1);
820 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700821
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700822 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700823 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700824 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700825 -1,
826 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700827 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800828 metrics::CheckResult::kUpdateAvailable,
829 metrics::CheckReaction::kUpdating,
830 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700831 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700832 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700833 EXPECT_TRUE(response.update_exists);
834}
835
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700836TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700837 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700838 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700839 params.set_wall_clock_based_wait_enabled(true);
840 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700841
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700842 params.set_update_check_count_wait_enabled(true);
843 params.set_min_update_checks_needed(1);
844 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700845
Alex Deymo8e18f932015-03-27 16:16:59 -0700846 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700847 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700848 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700849 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700850 -1,
851 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700852 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800853 metrics::CheckResult::kUpdateAvailable,
854 metrics::CheckReaction::kUpdating,
855 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700856 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700857 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700858 EXPECT_TRUE(response.update_exists);
859}
860
861
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700862TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700863 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700864 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700865 params.set_wall_clock_based_wait_enabled(true);
866 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700867
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700868 params.set_update_check_count_wait_enabled(true);
869 params.set_min_update_checks_needed(0);
870 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700871
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700872 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700873 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700874 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700875 -1,
876 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700877 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800878 metrics::CheckResult::kUpdateAvailable,
879 metrics::CheckReaction::kUpdating,
880 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700881 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700882 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700883
Ben Chan9abb7632014-08-07 00:10:53 -0700884 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700885 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700886 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700887 EXPECT_TRUE(response.update_exists);
888}
889
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700890TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700891 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700892 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700893 params.set_wall_clock_based_wait_enabled(true);
894 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700895
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700896 params.set_update_check_count_wait_enabled(true);
897 params.set_min_update_checks_needed(1);
898 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700899
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700900 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700901 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700902 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700903 -1,
904 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700905 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800906 metrics::CheckResult::kUpdateAvailable,
907 metrics::CheckReaction::kDeferring,
908 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700909 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700910 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700911
Ben Chan9abb7632014-08-07 00:10:53 -0700912 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700913 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700914 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700915 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700916
917 // Verify if we are interactive check we don't defer.
918 params.set_interactive(true);
919 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700920 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700921 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700922 -1,
923 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700924 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800925 metrics::CheckResult::kUpdateAvailable,
926 metrics::CheckReaction::kUpdating,
927 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700928 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700929 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700930 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700931}
932
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700933TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700934 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700935 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700936 params.set_wall_clock_based_wait_enabled(true);
937 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700938
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700939 params.set_update_check_count_wait_enabled(true);
940 params.set_min_update_checks_needed(1);
941 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700942
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700943 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700944
945 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700946 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700947 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700948 -1,
949 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700950 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800951 metrics::CheckResult::kUpdateAvailable,
952 metrics::CheckReaction::kDeferring,
953 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700954 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700955 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700956
Ben Chan9abb7632014-08-07 00:10:53 -0700957 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700958 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700959 // count remains the same, as the decrementing happens in update_attempter
960 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700961 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700962 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700963
964 // Verify if we are interactive check we don't defer.
965 params.set_interactive(true);
966 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700967 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700968 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700969 -1,
970 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700971 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800972 metrics::CheckResult::kUpdateAvailable,
973 metrics::CheckReaction::kUpdating,
974 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700975 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700976 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700977 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700978}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700979
Alex Deymo8e18f932015-03-27 16:16:59 -0700980TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
981 OmahaResponse response;
982 OmahaRequestParams params = request_params_;
983 fake_update_response_.include_cohorts = true;
984 fake_update_response_.cohort = "s/154454/8479665";
985 fake_update_response_.cohorthint = "please-put-me-on-beta";
986 fake_update_response_.cohortname = "stable";
987
988 ASSERT_TRUE(TestUpdateCheck(&params,
989 fake_update_response_.GetUpdateResponse(),
990 -1,
991 false, // ping_only
992 ErrorCode::kSuccess,
993 metrics::CheckResult::kUpdateAvailable,
994 metrics::CheckReaction::kUpdating,
995 metrics::DownloadErrorCode::kUnset,
996 &response,
997 nullptr));
998
999 string value;
1000 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1001 EXPECT_EQ(fake_update_response_.cohort, value);
1002
1003 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1004 EXPECT_EQ(fake_update_response_.cohorthint, value);
1005
1006 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1007 EXPECT_EQ(fake_update_response_.cohortname, value);
1008}
1009
1010TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
1011 OmahaResponse response;
1012 OmahaRequestParams params = request_params_;
1013 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1014 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1015 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1016 fake_update_response_.include_cohorts = true;
1017 fake_update_response_.cohort = "s/154454/8479665";
1018 fake_update_response_.cohorthint = "please-put-me-on-beta";
1019 fake_update_response_.cohortname = "";
1020
1021 ASSERT_TRUE(TestUpdateCheck(&params,
1022 fake_update_response_.GetUpdateResponse(),
1023 -1,
1024 false, // ping_only
1025 ErrorCode::kSuccess,
1026 metrics::CheckResult::kUpdateAvailable,
1027 metrics::CheckReaction::kUpdating,
1028 metrics::DownloadErrorCode::kUnset,
1029 &response,
1030 nullptr));
1031
1032 string value;
1033 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1034 EXPECT_EQ(fake_update_response_.cohort, value);
1035
1036 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1037 EXPECT_EQ(fake_update_response_.cohorthint, value);
1038
1039 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1040}
1041
1042TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
1043 OmahaResponse response;
1044 OmahaRequestParams params = request_params_;
1045 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1046
1047 ASSERT_TRUE(TestUpdateCheck(&params,
1048 fake_update_response_.GetUpdateResponse(),
1049 -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
1058 string value;
1059 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1060 EXPECT_EQ("old_value", value);
1061
1062 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1063 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1064}
1065
Alex Deymo00d79ac2015-06-29 15:41:49 -07001066TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
1067 OmahaResponse response;
1068 OmahaRequestParams params = request_params_;
1069 fake_update_response_.include_cohorts = true;
1070 fake_update_response_.cohort = "s/154454/8479665";
1071 fake_update_response_.cohorthint = "please-put-me-on-beta";
1072 fake_update_response_.cohortname = "stable";
1073
1074 ASSERT_TRUE(TestUpdateCheck(&params,
1075 fake_update_response_.GetNoUpdateResponse(),
1076 -1,
1077 false, // ping_only
1078 ErrorCode::kSuccess,
1079 metrics::CheckResult::kNoUpdateAvailable,
1080 metrics::CheckReaction::kUnset,
1081 metrics::DownloadErrorCode::kUnset,
1082 &response,
1083 nullptr));
1084
1085 string value;
1086 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1087 EXPECT_EQ(fake_update_response_.cohort, value);
1088
1089 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1090 EXPECT_EQ(fake_update_response_.cohorthint, value);
1091
1092 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1093 EXPECT_EQ(fake_update_response_.cohortname, value);
1094}
1095
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001096TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001097 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001098
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001099 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001100 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001101
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001102 OmahaRequestParams params = request_params_;
1103 fake_system_state_.set_request_params(&params);
Ben Chan5c02c132017-06-27 07:10:36 -07001104 OmahaRequestAction action(
1105 &fake_system_state_,
1106 nullptr,
Ben Chanab5a0af2017-10-12 14:57:50 -07001107 std::make_unique<MockHttpFetcher>(http_response.data(),
Ben Chan5c02c132017-06-27 07:10:36 -07001108 http_response.size(),
1109 nullptr),
1110 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001111 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001112 ActionProcessor processor;
1113 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001114 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001115
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001116 loop.PostTask(base::Bind(
1117 [](ActionProcessor* processor) { processor->StartProcessing(); },
1118 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001119 loop.Run();
1120 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001121 EXPECT_FALSE(processor.IsRunning());
1122}
1123
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001124TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001125 OmahaResponse response;
1126 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001127 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001128 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001129 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001130 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001131 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001132 metrics::CheckResult::kParsingError,
1133 metrics::CheckReaction::kUnset,
1134 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -07001135 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001136 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001137 EXPECT_FALSE(response.update_exists);
1138}
1139
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001140TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001141 OmahaResponse response;
1142 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001143 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001144 "",
1145 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001146 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001147 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001148 metrics::CheckResult::kParsingError,
1149 metrics::CheckReaction::kUnset,
1150 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001151 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001152 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001153 EXPECT_FALSE(response.update_exists);
1154}
1155
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001156TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001157 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001158 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001159 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001160 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1161 "<daystart elapsed_seconds=\"100\"/>"
1162 "<app appid=\"foo\" status=\"ok\">"
1163 "<ping status=\"ok\"/>"
1164 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001165 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001166 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001167 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001168 metrics::CheckResult::kParsingError,
1169 metrics::CheckReaction::kUnset,
1170 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001171 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001172 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001173 EXPECT_FALSE(response.update_exists);
1174}
1175
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001176TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001177 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001178 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001179 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001180 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1181 "<daystart elapsed_seconds=\"100\"/>"
1182 "<app appid=\"foo\" status=\"ok\">"
1183 "<ping status=\"ok\"/>"
1184 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001185 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001186 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001187 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001188 metrics::CheckResult::kParsingError,
1189 metrics::CheckReaction::kUnset,
1190 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001191 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001192 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001193 EXPECT_FALSE(response.update_exists);
1194}
1195
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001196TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001197 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001198 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001199 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001200 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1201 "<daystart elapsed_seconds=\"100\"/>"
1202 "<app appid=\"foo\" status=\"ok\">"
1203 "<ping status=\"ok\"/>"
1204 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001205 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001206 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001207 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001208 metrics::CheckResult::kParsingError,
1209 metrics::CheckReaction::kUnset,
1210 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001211 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001212 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001213 EXPECT_FALSE(response.update_exists);
1214}
1215
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001216TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001217 string input_response =
1218 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1219 "<daystart elapsed_seconds=\"100\"/>"
1220 "<app appid=\"xyz\" status=\"ok\">"
1221 "<updatecheck status=\"ok\">"
1222 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001223 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001224 "<packages><package hash=\"not-used\" name=\"f\" "
1225 "size=\"587\"/></packages>"
1226 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001227 "ChromeOSVersion=\"10.2.3.4\" "
1228 "Prompt=\"false\" "
1229 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001230 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001231 "sha256=\"lkq34j5345\" "
1232 "needsadmin=\"true\" "
1233 "/></actions></manifest></updatecheck></app></response>";
1234 LOG(INFO) << "Input Response = " << input_response;
1235
Darin Petkov6a5b3222010-07-13 14:55:28 -07001236 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001237 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001238 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -07001239 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001240 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001241 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001242 metrics::CheckResult::kUpdateAvailable,
1243 metrics::CheckReaction::kUpdating,
1244 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001245 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001246 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001247 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001248 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001249 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001250 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001251 EXPECT_EQ("lkq34j5345", response.hash);
1252 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001253 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001254 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001255}
1256
1257namespace {
1258class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1259 public:
1260 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001261 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001262 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001263};
1264
Alex Deymo60ca1a72015-06-18 18:19:15 -07001265void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001266 processor->StartProcessing();
1267 CHECK(processor->IsRunning());
1268 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001269}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001270} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001271
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001272TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001273 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001274 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001275
Alex Deymo60ca1a72015-06-18 18:19:15 -07001276 string http_response("doesn't matter");
Ben Chan5c02c132017-06-27 07:10:36 -07001277 OmahaRequestAction action(
1278 &fake_system_state_,
1279 nullptr,
Ben Chanab5a0af2017-10-12 14:57:50 -07001280 std::make_unique<MockHttpFetcher>(http_response.data(),
Ben Chan5c02c132017-06-27 07:10:36 -07001281 http_response.size(),
1282 nullptr),
1283 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001284 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001285 ActionProcessor processor;
1286 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001287 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001288
Alex Deymo60ca1a72015-06-18 18:19:15 -07001289 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1290 loop.Run();
1291 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001292}
1293
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001294TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001295 string output;
1296 EXPECT_TRUE(XmlEncode("ab", &output));
1297 EXPECT_EQ("ab", output);
1298 EXPECT_TRUE(XmlEncode("a<b", &output));
1299 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001300 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1301 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001302 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1303 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001304 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001305 EXPECT_FALSE(XmlEncode("\xc2", &output));
1306 // Fail with invalid ASCII-7 chars.
1307 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1308}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001309
Alex Deymob0d74eb2015-03-30 17:59:17 -07001310TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1311 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1312 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1313}
1314
1315TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001316 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001317
1318 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001319 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001320 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001321 OmahaRequestParams::kOsVersion,
1322 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001323 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001324 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001325 "0.1.0.0",
1326 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001327 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001328 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001329 "ChromeOSFirmware.1.0",
1330 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001331 false, // delta okay
1332 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001333 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001334 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001335 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1336 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1337 fake_prefs_.SetString(kPrefsOmahaCohortName,
Alex Vakulenko0103c362016-01-20 07:56:15 -08001338 base::JoinString(
1339 vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001340 OmahaResponse response;
1341 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001342 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001343 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001344 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001345 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001346 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001347 metrics::CheckResult::kParsingError,
1348 metrics::CheckReaction::kUnset,
1349 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001350 &response,
1351 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001352 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001353 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001354 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1355 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1356 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1357 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1358 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1359 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1360 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1361 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1362 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1363 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1364 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1365 // Values from Prefs that are too big are removed from the XML instead of
1366 // encoded.
1367 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001368}
1369
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001370TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001371 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001372 fake_update_response_.deadline = "&lt;20110101";
1373 fake_update_response_.more_info_url = "testthe&lt;url";
1374 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001375 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001376 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001377 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001378 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001379 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001380 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001381 metrics::CheckResult::kUpdateAvailable,
1382 metrics::CheckReaction::kUpdating,
1383 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001384 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001385 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001386
1387 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001388 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001389 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001390}
1391
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001392TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001393 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001394 // overflows int32_t:
1395 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001396 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001397 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001398 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001399 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001400 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001401 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001402 metrics::CheckResult::kUpdateAvailable,
1403 metrics::CheckReaction::kUpdating,
1404 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001405 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001406 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001407
1408 EXPECT_EQ(response.size, 123123123123123ll);
1409}
1410
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001411TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001412 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001413 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001414 fake_system_state_.set_prefs(&prefs);
1415
Darin Petkov95508da2011-01-05 12:42:29 -08001416 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001417 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001418 // An existing but empty previous version means that we didn't reboot to a new
1419 // update, therefore, no need to update the previous version.
1420 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001421 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001422 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001423 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001424 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001425 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001426 metrics::CheckResult::kParsingError,
1427 metrics::CheckReaction::kUnset,
1428 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001429 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001430 &post_data));
1431 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001432 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001433 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001434 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001435 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001436 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001437 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1438 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001439 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1440 string::npos);
1441 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1442 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001443 // No <event> tag should be sent if we didn't reboot to an update.
1444 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001445}
1446
Jay Srinivasan0a708742012-03-20 11:26:12 -07001447
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001448TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001449 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001450 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001451 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1452 "invalid xml>",
1453 &post_data);
1454 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001455 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001456 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001457 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001458 OmahaEvent::kTypeUpdateDownloadStarted,
1459 OmahaEvent::kResultSuccess);
1460 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001461 EXPECT_EQ(post_str.find("ping"), string::npos);
1462 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001463}
1464
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001465TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001466 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001467 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001468 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1469 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001470 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001471 "invalid xml>",
1472 &post_data);
1473 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001474 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001475 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001476 " <event eventtype=\"%d\" eventresult=\"%d\" "
1477 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001478 OmahaEvent::kTypeDownloadComplete,
1479 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001480 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001481 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001482 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001483}
1484
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001485TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001486 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001487 // Create a copy of the OmahaRequestParams to reuse it later.
1488 OmahaRequestParams params = request_params_;
1489 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001490 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001491 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001492 nullptr,
Ben Chanab5a0af2017-10-12 14:57:50 -07001493 std::make_unique<MockHttpFetcher>(http_response.data(),
Ben Chan5c02c132017-06-27 07:10:36 -07001494 http_response.size(),
1495 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001496 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001497 EXPECT_FALSE(update_check_action.IsEvent());
1498
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001499 params = request_params_;
1500 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001501 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001502 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001503 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Ben Chanab5a0af2017-10-12 14:57:50 -07001504 std::make_unique<MockHttpFetcher>(http_response.data(),
Ben Chan5c02c132017-06-27 07:10:36 -07001505 http_response.size(),
1506 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001507 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001508 EXPECT_TRUE(event_action.IsEvent());
1509}
1510
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001511TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001512 for (int i = 0; i < 2; i++) {
1513 bool delta_okay = i == 1;
1514 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001515 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001516 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001517 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001518 OmahaRequestParams::kOsVersion,
1519 "service_pack",
1520 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001521 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001522 "0.1.0.0",
1523 "en-US",
1524 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001525 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001526 "ChromeOSFirmware.1.0",
1527 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001528 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001529 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001530 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001531 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001532 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001533 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001534 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001535 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001536 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001537 metrics::CheckResult::kParsingError,
1538 metrics::CheckReaction::kUnset,
1539 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001540 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001541 &post_data));
1542 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001543 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001544 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1545 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001546 string::npos)
1547 << "i = " << i;
1548 }
1549}
1550
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001551TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001552 for (int i = 0; i < 2; i++) {
1553 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001554 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001555 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001556 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001557 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001558 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001559 OmahaRequestParams::kOsVersion,
1560 "service_pack",
1561 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001562 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001563 "0.1.0.0",
1564 "en-US",
1565 "unittest_track",
1566 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001567 "ChromeOSFirmware.1.0",
1568 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001569 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001570 interactive,
1571 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001572 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001573 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001574 "invalid xml>",
1575 -1,
1576 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001577 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001578 metrics::CheckResult::kParsingError,
1579 metrics::CheckReaction::kUnset,
1580 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001581 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001582 &post_data));
1583 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001584 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001585 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1586 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001587 string::npos)
1588 << "i = " << i;
1589 }
1590}
1591
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001592TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001593 OmahaEvent default_event;
1594 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1595 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001596 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001597
1598 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1599 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1600 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001601 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001602
1603 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1604 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001605 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001606 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1607 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001608 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001609}
1610
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001611void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001612 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001613 fake_system_state_.set_prefs(&prefs);
1614 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1615 .Times(AnyNumber());
1616 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1617 // Add a few hours to the day difference to test no rounding, etc.
1618 int64_t five_days_ago =
1619 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1620 int64_t six_days_ago =
1621 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1622 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001623 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001624 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001625 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true)));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001626 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001627 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001628 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001629 ASSERT_TRUE(
1630 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001631 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001632 -1,
1633 ping_only,
1634 ErrorCode::kSuccess,
1635 metrics::CheckResult::kUnset,
1636 metrics::CheckReaction::kUnset,
1637 metrics::DownloadErrorCode::kUnset,
1638 nullptr,
1639 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001640 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001641 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1642 string::npos);
1643 if (ping_only) {
1644 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1645 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1646 } else {
1647 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1648 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001649 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001650}
1651
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001652TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1653 PingTest(true /* ping_only */);
1654}
1655
1656TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1657 PingTest(false /* ping_only */);
1658}
1659
1660TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001661 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001662 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001663 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1664 .Times(AnyNumber());
1665 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001666 int64_t three_days_ago =
1667 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1668 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001669 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001670 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001671 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001672 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001673 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001674 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001675 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001676 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001677 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001678 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001679 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001680 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001681 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001682 metrics::CheckResult::kNoUpdateAvailable,
1683 metrics::CheckReaction::kUnset,
1684 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001685 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001686 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001687 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001688 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001689 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001690}
1691
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001692TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001693 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001694 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001695 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1696 .Times(AnyNumber());
1697 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001698 int64_t four_days_ago =
1699 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1700 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001701 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001702 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001703 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001704 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001705 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001706 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001707 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001708 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001709 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001710 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001711 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001712 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001713 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001714 metrics::CheckResult::kNoUpdateAvailable,
1715 metrics::CheckReaction::kUnset,
1716 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001717 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001718 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001719 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001720 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001721 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001722}
1723
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001724TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001725 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001726 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001727 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1728 .Times(AnyNumber());
1729 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001730 int64_t one_hour_ago =
1731 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001732 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001733 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001734 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001735 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001736 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001737 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001738 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1739 // send a ping.
1740 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1741 .WillOnce(Return(true));
1742 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1743 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001744 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001745 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001746 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001747 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001748 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001749 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001750 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001751 metrics::CheckResult::kNoUpdateAvailable,
1752 metrics::CheckReaction::kUnset,
1753 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001754 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001755 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001756 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001757 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001758}
1759
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001760TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001761 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001762 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001763 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001764 int64_t now = Time::Now().ToInternalValue();
1765 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001766 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001767 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001768 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true)));
Thieu Leb44e9e82011-06-06 14:34:04 -07001769 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1770 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001771 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001772 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001773 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001774 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001775 -1,
1776 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001777 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001778 metrics::CheckResult::kUnset,
1779 metrics::CheckReaction::kUnset,
1780 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001781 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001782 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08001783 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07001784}
1785
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001786TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001787 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001788 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001789 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1790 .Times(AnyNumber());
1791 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001792 int64_t future =
1793 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001794 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001795 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001796 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001797 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001798 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
Ben Chan672c1f52017-10-23 15:41:39 -07001799 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001800 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1801 .WillOnce(Return(true));
1802 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1803 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001804 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001805 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001806 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001807 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1808 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001809 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001810 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001811 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001812 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001813 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001814 metrics::CheckResult::kNoUpdateAvailable,
1815 metrics::CheckReaction::kUnset,
1816 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001817 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001818 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001819 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001820 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001821}
1822
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001823TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001824 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001825 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001826 // may fail if it runs for longer than 5 seconds. It shouldn't run
1827 // that long though.
1828 int64_t midnight =
1829 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1830 int64_t midnight_slack =
1831 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001832 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001833 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001834 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1835 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001836 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1837 AllOf(Ge(midnight), Le(midnight_slack))))
1838 .WillOnce(Return(true));
1839 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1840 AllOf(Ge(midnight), Le(midnight_slack))))
1841 .WillOnce(Return(true));
1842 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001843 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001844 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1845 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001846 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001847 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001848 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001849 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001850 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001851 metrics::CheckResult::kNoUpdateAvailable,
1852 metrics::CheckReaction::kUnset,
1853 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001854 nullptr,
1855 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001856}
1857
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001858TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001859 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001860 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001861 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1862 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001863 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1864 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1865 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001866 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001867 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1868 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001869 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001870 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001871 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001872 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001873 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001874 metrics::CheckResult::kNoUpdateAvailable,
1875 metrics::CheckReaction::kUnset,
1876 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001877 nullptr,
1878 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001879}
1880
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001881TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001882 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001883 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001884 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1885 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001886 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1887 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1888 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001889 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001890 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1891 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001892 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001893 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001894 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001895 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001896 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001897 metrics::CheckResult::kNoUpdateAvailable,
1898 metrics::CheckReaction::kUnset,
1899 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001900 nullptr,
1901 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001902}
1903
Alex Deymob3fa53b2016-04-18 19:57:58 -07001904TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1905 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1906 // the "eol" attribute.
1907 ASSERT_TRUE(
1908 TestUpdateCheck(nullptr, // request_params
1909 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1910 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1911 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1912 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/>"
1913 "</app></response>",
1914 -1,
1915 false, // ping_only
1916 ErrorCode::kSuccess,
1917 metrics::CheckResult::kNoUpdateAvailable,
1918 metrics::CheckReaction::kUnset,
1919 metrics::DownloadErrorCode::kUnset,
1920 nullptr,
1921 nullptr));
1922 string eol_pref;
1923 EXPECT_TRUE(
1924 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1925 // Note that the eol="eol" attribute should be ignored and the _eol should be
1926 // used instead.
1927 EXPECT_EQ("security-only", eol_pref);
1928}
1929
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001930TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001931 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001932 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001933 "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, // response
Darin Petkov84c763c2010-07-29 16:27:58 -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());
Darin Petkov84c763c2010-07-29 16:27:58 -07001944 EXPECT_EQ(post_str.find("machineid="), string::npos);
1945 EXPECT_EQ(post_str.find("userid="), string::npos);
1946}
1947
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001948TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001949 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001950 const int http_error_code =
1951 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001952 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001953 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001954 "",
1955 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001956 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001957 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001958 metrics::CheckResult::kDownloadError,
1959 metrics::CheckReaction::kUnset,
1960 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001961 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001962 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001963 EXPECT_FALSE(response.update_exists);
1964}
1965
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001966TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001967 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001968 const int http_error_code =
1969 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001970 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001971 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001972 "",
1973 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001974 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001975 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001976 metrics::CheckResult::kDownloadError,
1977 metrics::CheckReaction::kUnset,
1978 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001979 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001980 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001981 EXPECT_FALSE(response.update_exists);
1982}
1983
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001984TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001985 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001986 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001987 params.set_wall_clock_based_wait_enabled(true);
1988 params.set_waiting_period(TimeDelta().FromDays(1));
1989 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001990
Sen Jiang7c1171e2016-06-23 11:35:40 -07001991 Time arbitrary_date;
1992 Time::FromString("6/4/1989", &arbitrary_date);
1993 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
1994 ASSERT_FALSE(TestUpdateCheck(&params,
1995 fake_update_response_.GetUpdateResponse(),
1996 -1,
1997 false, // ping_only
1998 ErrorCode::kOmahaUpdateDeferredPerPolicy,
1999 metrics::CheckResult::kUpdateAvailable,
2000 metrics::CheckReaction::kDeferring,
2001 metrics::DownloadErrorCode::kUnset,
2002 &response,
2003 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002004
Ben Chan9abb7632014-08-07 00:10:53 -07002005 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002006 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002007 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002008 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002009
2010 // Verify if we are interactive check we don't defer.
2011 params.set_interactive(true);
Sen Jiang7c1171e2016-06-23 11:35:40 -07002012 ASSERT_TRUE(TestUpdateCheck(&params,
2013 fake_update_response_.GetUpdateResponse(),
2014 -1,
2015 false, // ping_only
2016 ErrorCode::kSuccess,
2017 metrics::CheckResult::kUpdateAvailable,
2018 metrics::CheckReaction::kUpdating,
2019 metrics::DownloadErrorCode::kUnset,
2020 &response,
2021 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07002022 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002023}
2024
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002025TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002026 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002027 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002028 params.set_wall_clock_based_wait_enabled(true);
2029 params.set_waiting_period(TimeDelta().FromDays(1));
2030 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002031
Sen Jiang7c1171e2016-06-23 11:35:40 -07002032 Time t1, t2;
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002033 Time::FromString("1/1/2012", &t1);
Sen Jiang7c1171e2016-06-23 11:35:40 -07002034 Time::FromString("1/3/2012", &t2);
2035 ASSERT_TRUE(
2036 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2037 fake_system_state_.fake_clock()->SetWallclockTime(t2);
2038 ASSERT_TRUE(TestUpdateCheck(&params,
2039 fake_update_response_.GetUpdateResponse(),
2040 -1,
2041 false, // ping_only
2042 ErrorCode::kSuccess,
2043 metrics::CheckResult::kUpdateAvailable,
2044 metrics::CheckReaction::kUpdating,
2045 metrics::DownloadErrorCode::kUnset,
2046 &response,
2047 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002048
2049 EXPECT_TRUE(response.update_exists);
2050
2051 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002052 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002053 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002054 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2055}
2056
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002057TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002058 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002059 base::ScopedTempDir tempdir;
2060 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002061
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002062 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07002063 OmahaRequestParams params(&fake_system_state_);
2064 params.set_root(tempdir.path().value());
2065 params.set_app_id("{22222222-2222-2222-2222-222222222222}");
2066 params.set_app_version("1.2.3.4");
2067 params.set_current_channel("canary-channel");
2068 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
2069 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002070 EXPECT_TRUE(params.to_more_stable_channel());
2071 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002072 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002073 "invalid xml>",
2074 -1,
2075 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002076 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002077 metrics::CheckResult::kParsingError,
2078 metrics::CheckReaction::kUnset,
2079 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002080 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002081 &post_data));
2082 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002083 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002084 EXPECT_NE(string::npos, post_str.find(
2085 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
2086 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2087 "track=\"stable-channel\" from_track=\"canary-channel\" "));
2088}
2089
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002090TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002091 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002092 base::ScopedTempDir tempdir;
2093 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002094
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002095 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07002096 OmahaRequestParams params(&fake_system_state_);
2097 params.set_root(tempdir.path().value());
2098 params.set_app_id("{11111111-1111-1111-1111-111111111111}");
2099 params.set_app_version("5.6.7.8");
2100 params.set_current_channel("stable-channel");
2101 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
2102 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002103 EXPECT_FALSE(params.to_more_stable_channel());
2104 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002105 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002106 "invalid xml>",
2107 -1,
2108 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002109 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002110 metrics::CheckResult::kParsingError,
2111 metrics::CheckReaction::kUnset,
2112 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002113 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002114 &post_data));
2115 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002116 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002117 EXPECT_NE(string::npos, post_str.find(
2118 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
2119 "version=\"5.6.7.8\" "
2120 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002121 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002122}
2123
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002124// Checks that the initial ping with a=-1 r=-1 is not send when the device
2125// was powerwashed.
2126TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2127 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2128
2129 // Flag that the device was powerwashed in the past.
2130 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
2131
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002132 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002133 ASSERT_TRUE(
2134 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002135 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002136 -1,
2137 false, // ping_only
2138 ErrorCode::kSuccess,
2139 metrics::CheckResult::kNoUpdateAvailable,
2140 metrics::CheckReaction::kUnset,
2141 metrics::DownloadErrorCode::kUnset,
2142 nullptr,
2143 &post_data));
2144 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002145 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002146 EXPECT_EQ(string::npos, post_str.find("<ping"));
2147}
2148
Amin Hassani1677e812017-06-21 13:36:36 -07002149// Checks that the initial ping with a=-1 r=-1 is not send when the device
2150// first_active_omaha_ping_sent is set.
2151TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2152 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2153
2154 // Flag that the device was not powerwashed in the past.
2155 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2156
2157 // Flag that the device has sent first active ping in the past.
2158 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2159
2160 brillo::Blob post_data;
2161 ASSERT_TRUE(
2162 TestUpdateCheck(nullptr, // request_params
2163 fake_update_response_.GetNoUpdateResponse(),
2164 -1,
2165 false, // ping_only
2166 ErrorCode::kSuccess,
2167 metrics::CheckResult::kNoUpdateAvailable,
2168 metrics::CheckReaction::kUnset,
2169 metrics::DownloadErrorCode::kUnset,
2170 nullptr,
2171 &post_data));
2172 // We shouldn't send a ping in this case since
2173 // first_active_omaha_ping_sent=true
2174 string post_str(post_data.begin(), post_data.end());
2175 EXPECT_EQ(string::npos, post_str.find("<ping"));
2176}
2177
Alex Deymo9fded1e2015-11-05 12:31:19 -08002178// Checks that the event 54 is sent on a reboot to a new update.
2179TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2180 // Flag that the device was updated in a previous boot.
2181 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2182
2183 brillo::Blob post_data;
2184 ASSERT_TRUE(
2185 TestUpdateCheck(nullptr, // request_params
2186 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));
2195 string post_str(post_data.begin(), post_data.end());
2196
2197 // An event 54 is included and has the right version.
2198 EXPECT_NE(string::npos,
2199 post_str.find(base::StringPrintf(
2200 "<event eventtype=\"%d\"",
2201 OmahaEvent::kTypeRebootedAfterUpdate)));
2202 EXPECT_NE(string::npos,
2203 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2204
2205 // The previous version flag should have been removed.
2206 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2207 string prev_version;
2208 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2209 EXPECT_TRUE(prev_version.empty());
2210}
2211
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002212void OmahaRequestActionTest::P2PTest(
2213 bool initial_allow_p2p_for_downloading,
2214 bool initial_allow_p2p_for_sharing,
2215 bool omaha_disable_p2p_for_downloading,
2216 bool omaha_disable_p2p_for_sharing,
2217 bool payload_state_allow_p2p_attempt,
2218 bool expect_p2p_client_lookup,
2219 const string& p2p_client_result_url,
2220 bool expected_allow_p2p_for_downloading,
2221 bool expected_allow_p2p_for_sharing,
2222 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07002223 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002224 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07002225 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2226 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2227 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002228
2229 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002230 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002231 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2232 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002233 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2234 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2235 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2236 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2237 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2238 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2239 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2240 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2241 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2242 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2243
David Zeuthen8f191b22013-08-06 12:27:50 -07002244 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002245 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002246 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2247
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002248 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2249 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002250 .Times(expect_p2p_client_lookup ? 1 : 0);
2251
Alex Deymo8e18f932015-03-27 16:16:59 -07002252 fake_update_response_.disable_p2p_for_downloading =
2253 omaha_disable_p2p_for_downloading;
2254 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07002255 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002256 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07002257 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07002258 -1,
2259 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002260 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002261 metrics::CheckResult::kUpdateAvailable,
2262 metrics::CheckReaction::kUpdating,
2263 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07002264 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002265 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07002266 EXPECT_TRUE(response.update_exists);
2267
Gilad Arnold74b5f552014-10-07 08:17:16 -07002268 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2269 response.disable_p2p_for_downloading);
2270 EXPECT_EQ(omaha_disable_p2p_for_sharing,
2271 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002272
Gilad Arnold74b5f552014-10-07 08:17:16 -07002273 EXPECT_EQ(expected_allow_p2p_for_downloading,
2274 actual_allow_p2p_for_downloading);
2275 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2276 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002277}
2278
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002279TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002280 P2PTest(true, // initial_allow_p2p_for_downloading
2281 true, // initial_allow_p2p_for_sharing
2282 false, // omaha_disable_p2p_for_downloading
2283 false, // omaha_disable_p2p_for_sharing
2284 true, // payload_state_allow_p2p_attempt
2285 true, // expect_p2p_client_lookup
2286 "http://1.3.5.7/p2p", // p2p_client_result_url
2287 true, // expected_allow_p2p_for_downloading
2288 true, // expected_allow_p2p_for_sharing
2289 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002290}
2291
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002292TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002293 P2PTest(true, // initial_allow_p2p_for_downloading
2294 true, // initial_allow_p2p_for_sharing
2295 false, // omaha_disable_p2p_for_downloading
2296 false, // omaha_disable_p2p_for_sharing
2297 true, // payload_state_allow_p2p_attempt
2298 true, // expect_p2p_client_lookup
2299 "", // p2p_client_result_url
2300 false, // expected_allow_p2p_for_downloading
2301 true, // expected_allow_p2p_for_sharing
2302 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002303}
2304
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002305TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002306 P2PTest(false, // initial_allow_p2p_for_downloading
2307 true, // initial_allow_p2p_for_sharing
2308 false, // omaha_disable_p2p_for_downloading
2309 false, // omaha_disable_p2p_for_sharing
2310 true, // payload_state_allow_p2p_attempt
2311 false, // expect_p2p_client_lookup
2312 "unset", // p2p_client_result_url
2313 false, // expected_allow_p2p_for_downloading
2314 true, // expected_allow_p2p_for_sharing
2315 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002316}
2317
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002318TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002319 P2PTest(true, // initial_allow_p2p_for_downloading
2320 true, // initial_allow_p2p_for_sharing
2321 true, // omaha_disable_p2p_for_downloading
2322 false, // omaha_disable_p2p_for_sharing
2323 true, // payload_state_allow_p2p_attempt
2324 false, // expect_p2p_client_lookup
2325 "unset", // p2p_client_result_url
2326 false, // expected_allow_p2p_for_downloading
2327 true, // expected_allow_p2p_for_sharing
2328 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002329}
2330
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002331TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002332 P2PTest(true, // initial_allow_p2p_for_downloading
2333 true, // initial_allow_p2p_for_sharing
2334 false, // omaha_disable_p2p_for_downloading
2335 true, // omaha_disable_p2p_for_sharing
2336 true, // payload_state_allow_p2p_attempt
2337 true, // expect_p2p_client_lookup
2338 "http://1.3.5.7/p2p", // p2p_client_result_url
2339 true, // expected_allow_p2p_for_downloading
2340 false, // expected_allow_p2p_for_sharing
2341 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002342}
2343
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002344TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002345 P2PTest(true, // initial_allow_p2p_for_downloading
2346 true, // initial_allow_p2p_for_sharing
2347 true, // omaha_disable_p2p_for_downloading
2348 true, // omaha_disable_p2p_for_sharing
2349 true, // payload_state_allow_p2p_attempt
2350 false, // expect_p2p_client_lookup
2351 "unset", // p2p_client_result_url
2352 false, // expected_allow_p2p_for_downloading
2353 false, // expected_allow_p2p_for_sharing
2354 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002355}
2356
Alex Deymof329b932014-10-30 01:37:48 -07002357bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2358 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002359 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002360 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002361 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002362 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002363 -1,
2364 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002365 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002366 metrics::CheckResult::kUpdateAvailable,
2367 metrics::CheckReaction::kUpdating,
2368 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002369 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002370 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002371}
2372
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002373TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002374 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002375
Kevin Cernekee2494e282016-03-29 18:03:53 -07002376 // Simulate a successful update check that happens during OOBE. The
2377 // deadline in the response is needed to force the update attempt to
2378 // occur; responses without a deadline seen during OOBE will normally
2379 // return ErrorCode::kNonCriticalUpdateInOOBE.
2380 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2381 fake_update_response_.deadline = "20101020";
2382
David Zeuthen639aa362014-02-03 16:23:44 -08002383 // Check that we parse elapsed_days in the Omaha Response correctly.
2384 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002385 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2386 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002387 EXPECT_TRUE(response.update_exists);
2388 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002389 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002390 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002391 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002392 EXPECT_EQ(prefs_days, 42);
2393
2394 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002395 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002396 EXPECT_TRUE(response.update_exists);
2397 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002398 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002399 EXPECT_EQ(prefs_days, 42);
2400
2401 // Note that elapsed_days is not necessarily divisible by 7 so check
2402 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002403 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2404 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002405 EXPECT_TRUE(response.update_exists);
2406 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002407 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002408 EXPECT_EQ(prefs_days, 21);
2409
2410 // Check that we correctly handle elapsed_days not being included in
2411 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002412 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002413 EXPECT_TRUE(response.update_exists);
2414 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002415}
2416
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002417// If there is no prefs and OOBE is not complete, we should not
2418// report anything to Omaha.
2419TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002420 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002421 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2422 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2423}
David Zeuthen639aa362014-02-03 16:23:44 -08002424
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002425// If OOBE is complete and happened on a valid date (e.g. after Jan
2426// 1 2007 0:00 PST), that date should be used and written to
2427// prefs. However, first try with an invalid date and check we do
2428// nothing.
2429TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2430 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2431 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2432 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2433 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2434}
David Zeuthen639aa362014-02-03 16:23:44 -08002435
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002436// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2437// should yield an InstallDate of 14.
2438TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2439 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2440 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2441 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2442 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002443
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002444 int64_t prefs_days;
2445 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2446 EXPECT_EQ(prefs_days, 14);
2447}
David Zeuthen639aa362014-02-03 16:23:44 -08002448
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002449// Now that we have a valid date in prefs, check that we keep using
2450// that even if OOBE date reports something else. The date Jan 30,
2451// 2007 0:00 PST should yield an InstallDate of 28... but since
2452// there's a prefs file, we should still get 14.
2453TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2454 // Set a valid date in the prefs first.
2455 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002456
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002457 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2458 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2459 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002460
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002461 int64_t prefs_days;
2462 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2463 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002464
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002465 // If we delete the prefs file, we should get 28 days.
2466 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2467 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2468 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2469 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002470}
2471
Darin Petkov6a5b3222010-07-13 14:55:28 -07002472} // namespace chromeos_update_engine