blob: c7ef0c724ec3a1963fa0268b4e0e2495612fb526 [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
Darin Petkov6a5b3222010-07-13 14:55:28 -070021#include <string>
22#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070023
Alex Deymo60ca1a72015-06-18 18:19:15 -070024#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070025#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070026#include <base/files/scoped_temp_dir.h>
Ben Chan5c02c132017-06-27 07:10:36 -070027#include <base/memory/ptr_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070028#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070029#include <base/strings/string_util.h>
30#include <base/strings/stringprintf.h>
31#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070032#include <brillo/bind_lambda.h>
33#include <brillo/message_loops/fake_message_loop.h>
34#include <brillo/message_loops/message_loop.h>
35#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070036#include <gtest/gtest.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070037
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/action_pipe.h"
39#include "update_engine/common/constants.h"
40#include "update_engine/common/fake_prefs.h"
41#include "update_engine/common/hash_calculator.h"
42#include "update_engine/common/mock_http_fetcher.h"
43#include "update_engine/common/platform_constants.h"
44#include "update_engine/common/prefs.h"
45#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030046#include "update_engine/fake_system_state.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080047#include "update_engine/metrics.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070048#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070049#include "update_engine/mock_payload_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070050#include "update_engine/omaha_request_params.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070051
Darin Petkov1cbd78f2010-07-29 12:38:34 -070052using base::Time;
53using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070054using std::string;
55using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070056using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070057using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070058using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070059using testing::Ge;
60using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080061using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070062using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070063using testing::ReturnPointee;
64using testing::SaveArg;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070066using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070067
Alex Deymo8e18f932015-03-27 16:16:59 -070068namespace {
69
Alex Deymo85616652015-10-15 18:48:31 -070070const char kTestAppId[] = "test-app-id";
71
Alex Deymo8e18f932015-03-27 16:16:59 -070072// This is a helper struct to allow unit tests build an update response with the
73// values they care about.
74struct FakeUpdateResponse {
75 string GetNoUpdateResponse() const {
76 string entity_str;
77 if (include_entity)
78 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070079 return
80 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
81 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070082 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070083 "<app appid=\"" + app_id + "\" " +
84 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
85 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
86 " status=\"ok\">"
87 "<ping status=\"ok\"/>"
88 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070089 }
90
91 string GetUpdateResponse() const {
92 return
93 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
94 "protocol=\"3.0\">"
95 "<daystart elapsed_seconds=\"100\"" +
96 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
97 + "/>"
98 "<app appid=\"" + app_id + "\" " +
99 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
100 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
101 " status=\"ok\">"
102 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
103 "<urls><url codebase=\"" + codebase + "\"/></urls>"
104 "<manifest version=\"" + version + "\">"
105 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
106 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
107 "<actions><action event=\"postinstall\" "
108 "ChromeOSVersion=\"" + version + "\" "
109 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
110 "IsDelta=\"true\" "
111 "IsDeltaPayload=\"true\" "
112 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
113 "sha256=\"" + hash + "\" "
114 "needsadmin=\"" + needsadmin + "\" " +
115 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
116 (disable_p2p_for_downloading ?
117 "DisableP2PForDownloading=\"true\" " : "") +
118 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
119 "/></actions></manifest></updatecheck></app></response>";
120 }
121
122 // Return the payload URL, which is split in two fields in the XML response.
123 string GetPayloadUrl() {
124 return codebase + filename;
125 }
126
Alex Deymo85616652015-10-15 18:48:31 -0700127 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700128 string version = "1.2.3.4";
129 string more_info_url = "http://more/info";
130 string prompt = "true";
131 string codebase = "http://code/base/";
132 string filename = "file.signed";
133 string hash = "HASH1234=";
134 string needsadmin = "false";
135 int64_t size = 123;
136 string deadline = "";
137 string max_days_to_scatter = "7";
138 string elapsed_days = "42";
139
140 // P2P setting defaults to allowed.
141 bool disable_p2p_for_downloading = false;
142 bool disable_p2p_for_sharing = false;
143
144 // Omaha cohorts settings.
145 bool include_cohorts = false;
146 string cohort = "";
147 string cohorthint = "";
148 string cohortname = "";
149
150 // Whether to include the CrOS <!ENTITY> in the XML response.
151 bool include_entity = false;
152};
153
154} // namespace
155
Darin Petkov6a5b3222010-07-13 14:55:28 -0700156namespace chromeos_update_engine {
157
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700158class OmahaRequestActionTest : public ::testing::Test {
159 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800160 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700161 fake_system_state_.set_request_params(&request_params_);
162 fake_system_state_.set_prefs(&fake_prefs_);
163 }
164
165 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800166 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700167 // is used. |payload_state| may be null, in which case a local mock is used.
168 // |p2p_manager| may be null, in which case a local mock is used.
169 // |connection_manager| may be null, in which case a local mock is used.
170 // out_response may be null. If |fail_http_response_code| is non-negative,
171 // the transfer will fail with that code. |ping_only| is passed through to the
172 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
173 // post-data received by the mock HttpFetcher is returned.
174 //
175 // The |expected_check_result|, |expected_check_reaction| and
176 // |expected_error_code| parameters are for checking expectations
177 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
178 // UMA statistics. Use the appropriate ::kUnset value to specify that
179 // the given metric should not be reported.
180 bool TestUpdateCheck(OmahaRequestParams* request_params,
181 const string& http_response,
182 int fail_http_response_code,
183 bool ping_only,
184 ErrorCode expected_code,
185 metrics::CheckResult expected_check_result,
186 metrics::CheckReaction expected_check_reaction,
187 metrics::DownloadErrorCode expected_download_error_code,
188 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700189 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700190
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800191 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700192 // only a ping or also an updatecheck.
193 void PingTest(bool ping_only);
194
195 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700196 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700197 OmahaResponse *response);
198
199 // P2P test helper function.
200 void P2PTest(
201 bool initial_allow_p2p_for_downloading,
202 bool initial_allow_p2p_for_sharing,
203 bool omaha_disable_p2p_for_downloading,
204 bool omaha_disable_p2p_for_sharing,
205 bool payload_state_allow_p2p_attempt,
206 bool expect_p2p_client_lookup,
207 const string& p2p_client_result_url,
208 bool expected_allow_p2p_for_downloading,
209 bool expected_allow_p2p_for_sharing,
210 const string& expected_p2p_url);
211
212 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700213 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700214
215 // By default, all tests use these objects unless they replace them in the
216 // fake_system_state_.
217 OmahaRequestParams request_params_ = OmahaRequestParams{
218 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700219 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700220 OmahaRequestParams::kOsVersion,
221 "service_pack",
222 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700223 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700224 "0.1.0.0",
225 "en-US",
226 "unittest",
227 "OEM MODEL 09235 7471",
228 "ChromeOSFirmware.1.0",
229 "0X0A1",
230 false, // delta okay
231 false, // interactive
232 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700233 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700234
235 FakePrefs fake_prefs_;
236};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700237
238namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700239class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
240 public:
241 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700242 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800243 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700244 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800245 void ProcessingDone(const ActionProcessor* processor,
246 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700247 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700248 }
249
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800250 void ActionCompleted(ActionProcessor* processor,
251 AbstractAction* action,
252 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700253 // make sure actions always succeed
254 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700255 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700256 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700257 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700258 }
David Zeuthena99981f2013-04-29 13:42:47 -0700259 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700261} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262
263class OutputObjectCollectorAction;
264
265template<>
266class ActionTraits<OutputObjectCollectorAction> {
267 public:
268 // Does not take an object for input
269 typedef OmahaResponse InputObjectType;
270 // On success, puts the output path on output
271 typedef NoneType OutputObjectType;
272};
273
274class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
275 public:
276 OutputObjectCollectorAction() : has_input_object_(false) {}
277 void PerformAction() {
278 // copy input object
279 has_input_object_ = HasInputObject();
280 if (has_input_object_)
281 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700282 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700283 }
284 // Should never be called
285 void TerminateProcessing() {
286 CHECK(false);
287 }
288 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700289 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700290 return "OutputObjectCollectorAction";
291 }
Alex Deymof329b932014-10-30 01:37:48 -0700292 string Type() const { return StaticType(); }
Alex Vakulenko0057daa2016-01-23 16:22:50 -0800293 using InputObjectType =
294 ActionTraits<OutputObjectCollectorAction>::InputObjectType;
295 using OutputObjectType =
296 ActionTraits<OutputObjectCollectorAction>::OutputObjectType;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700297 bool has_input_object_;
298 OmahaResponse omaha_response_;
299};
300
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700301bool OmahaRequestActionTest::TestUpdateCheck(
302 OmahaRequestParams* request_params,
303 const string& http_response,
304 int fail_http_response_code,
305 bool ping_only,
306 ErrorCode expected_code,
307 metrics::CheckResult expected_check_result,
308 metrics::CheckReaction expected_check_reaction,
309 metrics::DownloadErrorCode expected_download_error_code,
310 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700311 brillo::Blob* out_post_data) {
312 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700313 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700314 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800315 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700316 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700317 if (fail_http_response_code >= 0) {
318 fetcher->FailTransfer(fail_http_response_code);
319 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700320 if (request_params)
321 fake_system_state_.set_request_params(request_params);
322 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700323 nullptr,
Ben Chan5c02c132017-06-27 07:10:36 -0700324 base::WrapUnique(fetcher),
Darin Petkov265f2902011-05-09 15:17:40 -0700325 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700326 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700327 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700328
Darin Petkov6a5b3222010-07-13 14:55:28 -0700329 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700330 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700331 processor.EnqueueAction(&action);
332
333 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700334 BondActions(&action, &collector_action);
335 processor.EnqueueAction(&collector_action);
336
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700337 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800338 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700339 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800340 SendEnumToUMA(metrics::kMetricCheckResult,
341 static_cast<int>(expected_check_result),
342 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
343 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700344 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800345 SendEnumToUMA(metrics::kMetricCheckReaction,
346 static_cast<int>(expected_check_reaction),
347 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
348 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700349 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700350 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
351 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800352 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
353 ? 0 : 1);
354
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700355 loop.PostTask(base::Bind(
356 [](ActionProcessor* processor) { processor->StartProcessing(); },
357 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700358 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700359 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700360 if (collector_action.has_input_object_ && out_response)
361 *out_response = collector_action.omaha_response_;
362 if (out_post_data)
363 *out_post_data = fetcher->post_data();
364 return collector_action.has_input_object_;
365}
366
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700367// Tests Event requests -- they should always succeed. |out_post_data|
368// may be null; if non-null, the post-data received by the mock
369// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700370void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700371 OmahaEvent* event,
372 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700373 brillo::Blob* out_post_data) {
374 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700375 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700376 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800377 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700378 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700379 FakeSystemState fake_system_state;
380 fake_system_state.set_request_params(&params);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300381 OmahaRequestAction action(&fake_system_state,
382 event,
Ben Chan5c02c132017-06-27 07:10:36 -0700383 base::WrapUnique(fetcher),
Alex Deymoc1c17b42015-11-23 03:53:15 -0300384 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700385 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700386 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700387 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700388 processor.EnqueueAction(&action);
389
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700390 loop.PostTask(base::Bind(
391 [](ActionProcessor* processor) { processor->StartProcessing(); },
392 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700393 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700394 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700395
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700396 if (out_post_data)
397 *out_post_data = fetcher->post_data();
398}
399
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700400TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400401 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700402 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400403 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700404 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700405 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400406 -1,
407 false, // ping_only
408 ErrorCode::kOmahaRequestXMLHasEntityDecl,
409 metrics::CheckResult::kParsingError,
410 metrics::CheckReaction::kUnset,
411 metrics::DownloadErrorCode::kUnset,
412 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700413 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400414 EXPECT_FALSE(response.update_exists);
415}
416
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700417TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700418 OmahaResponse response;
419 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700420 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700421 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700422 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700423 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700424 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800425 metrics::CheckResult::kNoUpdateAvailable,
426 metrics::CheckReaction::kUnset,
427 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700428 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700429 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700430 EXPECT_FALSE(response.update_exists);
431}
432
Alex Deymo8e18f932015-03-27 16:16:59 -0700433// Test that all the values in the response are parsed in a normal update
434// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700435TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700436 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700437 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700438 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700439 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700440 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700441 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700442 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700443 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800444 metrics::CheckResult::kUpdateAvailable,
445 metrics::CheckReaction::kUpdating,
446 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700447 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700448 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700449 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700450 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700451 EXPECT_EQ(fake_update_response_.version, response.version);
452 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
453 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
454 EXPECT_EQ(fake_update_response_.hash, response.hash);
455 EXPECT_EQ(fake_update_response_.size, response.size);
456 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
457 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
458 // Omaha cohort attribets are not set in the response, so they should not be
459 // persisted.
460 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
461 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
462 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700463}
464
Alex Deymo14ad88e2016-06-29 12:30:14 -0700465TEST_F(OmahaRequestActionTest, ExtraHeadersSentTest) {
466 const string http_response = "<?xml invalid response";
467 request_params_.set_interactive(true);
468
469 brillo::FakeMessageLoop loop(nullptr);
470 loop.SetAsCurrent();
471
472 MockHttpFetcher* fetcher =
473 new MockHttpFetcher(http_response.data(), http_response.size(), nullptr);
Ben Chan5c02c132017-06-27 07:10:36 -0700474 OmahaRequestAction action(&fake_system_state_, nullptr,
475 base::WrapUnique(fetcher), false);
Alex Deymo14ad88e2016-06-29 12:30:14 -0700476 ActionProcessor processor;
477 processor.EnqueueAction(&action);
478
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700479 loop.PostTask(base::Bind(
480 [](ActionProcessor* processor) { processor->StartProcessing(); },
481 base::Unretained(&processor)));
Alex Deymo14ad88e2016-06-29 12:30:14 -0700482 loop.Run();
483 EXPECT_FALSE(loop.PendingTasks());
484
485 // Check that the headers were set in the fetcher during the action. Note that
486 // we set this request as "interactive".
487 EXPECT_EQ("fg", fetcher->GetHeader("X-GoogleUpdate-Interactivity"));
488 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-GoogleUpdate-AppId"));
489 EXPECT_NE("", fetcher->GetHeader("X-GoogleUpdate-Updater"));
490}
491
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700492TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700493 OmahaResponse response;
494 // Set up a connection manager that doesn't allow a valid update over
495 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700496 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700497 fake_system_state_.set_connection_manager(&mock_cm);
498
Alex Deymo30534502015-07-20 15:06:33 -0700499 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
500 .WillRepeatedly(
Sen Jiang255e22b2016-05-20 16:15:29 -0700501 DoAll(SetArgumentPointee<0>(ConnectionType::kEthernet),
502 SetArgumentPointee<1>(ConnectionTethering::kUnknown),
Alex Deymo30534502015-07-20 15:06:33 -0700503 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700504 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
505 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700506
507 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700508 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700509 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700510 -1,
511 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700512 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700513 metrics::CheckResult::kUpdateAvailable,
514 metrics::CheckReaction::kIgnored,
515 metrics::DownloadErrorCode::kUnset,
516 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700517 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700518 EXPECT_FALSE(response.update_exists);
519}
520
Weidong Guo4b0d6032017-04-17 10:08:38 -0700521TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
522 // This test tests that update over cellular is allowed as device policy
523 // says yes.
524 OmahaResponse response;
525 MockConnectionManager mock_cm;
526
527 fake_system_state_.set_connection_manager(&mock_cm);
528
529 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
530 .WillRepeatedly(
531 DoAll(SetArgumentPointee<0>(ConnectionType::kCellular),
532 SetArgumentPointee<1>(ConnectionTethering::kUnknown),
533 Return(true)));
534 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
535 .WillRepeatedly(Return(true));
536 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
537 .WillRepeatedly(Return(true));
538
539 ASSERT_TRUE(
540 TestUpdateCheck(nullptr, // request_params
541 fake_update_response_.GetUpdateResponse(),
542 -1,
543 false, // ping_only
544 ErrorCode::kSuccess,
545 metrics::CheckResult::kUpdateAvailable,
546 metrics::CheckReaction::kUpdating,
547 metrics::DownloadErrorCode::kUnset,
548 &response,
549 nullptr));
550 EXPECT_TRUE(response.update_exists);
551}
552
553TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
554 // This test tests that update over cellular is blocked as device policy
555 // says no.
556 OmahaResponse response;
557 MockConnectionManager mock_cm;
558
559 fake_system_state_.set_connection_manager(&mock_cm);
560
561 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
562 .WillRepeatedly(
563 DoAll(SetArgumentPointee<0>(ConnectionType::kCellular),
564 SetArgumentPointee<1>(ConnectionTethering::kUnknown),
565 Return(true)));
566 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
567 .WillRepeatedly(Return(true));
568 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
569 .WillRepeatedly(Return(false));
570
571 ASSERT_FALSE(
572 TestUpdateCheck(nullptr, // request_params
573 fake_update_response_.GetUpdateResponse(),
574 -1,
575 false, // ping_only
576 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
577 metrics::CheckResult::kUpdateAvailable,
578 metrics::CheckReaction::kIgnored,
579 metrics::DownloadErrorCode::kUnset,
580 &response,
581 nullptr));
582 EXPECT_FALSE(response.update_exists);
583}
584
585TEST_F(OmahaRequestActionTest,
586 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
587 // This test tests that, when device policy is not set, update over cellular
588 // is allowed as permission for update over cellular is set to true.
589 OmahaResponse response;
590 MockConnectionManager mock_cm;
591
592 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
593 fake_system_state_.set_connection_manager(&mock_cm);
594
595 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
596 .WillRepeatedly(
597 DoAll(SetArgumentPointee<0>(ConnectionType::kCellular),
598 SetArgumentPointee<1>(ConnectionTethering::kUnknown),
599 Return(true)));
600 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
601 .WillRepeatedly(Return(false));
602 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
603 .WillRepeatedly(Return(true));
604
605 ASSERT_TRUE(
606 TestUpdateCheck(nullptr, // request_params
607 fake_update_response_.GetUpdateResponse(),
608 -1,
609 false, // ping_only
610 ErrorCode::kSuccess,
611 metrics::CheckResult::kUpdateAvailable,
612 metrics::CheckReaction::kUpdating,
613 metrics::DownloadErrorCode::kUnset,
614 &response,
615 nullptr));
616 EXPECT_TRUE(response.update_exists);
617}
618
619TEST_F(OmahaRequestActionTest,
620 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
621 // This test tests that, when device policy is not set and permission for
622 // update over cellular is set to false or does not exist, update over
623 // cellular is blocked as update target does not match the omaha response.
624 OmahaResponse response;
625 MockConnectionManager mock_cm;
626 // A version different from the version in omaha response.
627 string diff_version = "99.99.99";
628 // A size different from the size in omaha response.
629 int64_t diff_size = 999;
630
631 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
632 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
633 // This test tests cellular (3G) being the only connection type being allowed.
634 fake_system_state_.set_connection_manager(&mock_cm);
635
636 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
637 .WillRepeatedly(
638 DoAll(SetArgumentPointee<0>(ConnectionType::kCellular),
639 SetArgumentPointee<1>(ConnectionTethering::kUnknown),
640 Return(true)));
641 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
642 .WillRepeatedly(Return(false));
643 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
644 .WillRepeatedly(Return(true));
645
646 ASSERT_FALSE(
647 TestUpdateCheck(nullptr, // request_params
648 fake_update_response_.GetUpdateResponse(),
649 -1,
650 false, // ping_only
651 ErrorCode::kOmahaUpdateIgnoredOverCellular,
652 metrics::CheckResult::kUpdateAvailable,
653 metrics::CheckReaction::kIgnored,
654 metrics::DownloadErrorCode::kUnset,
655 &response,
656 nullptr));
657 EXPECT_FALSE(response.update_exists);
658}
659
660TEST_F(OmahaRequestActionTest,
661 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
662 // This test tests that, when device policy is not set and permission for
663 // update over cellular is set to false or does not exist, update over
664 // cellular is allowed as update target matches the omaha response.
665 OmahaResponse response;
666 MockConnectionManager mock_cm;
667 // A version same as the version in omaha response.
668 string new_version = fake_update_response_.version;
669 // A size same as the size in omaha response.
670 int64_t new_size = fake_update_response_.size;
671
672 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
673 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
674 fake_system_state_.set_connection_manager(&mock_cm);
675
676 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
677 .WillRepeatedly(
678 DoAll(SetArgumentPointee<0>(ConnectionType::kCellular),
679 SetArgumentPointee<1>(ConnectionTethering::kUnknown),
680 Return(true)));
681 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
682 .WillRepeatedly(Return(false));
683 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _))
684 .WillRepeatedly(Return(true));
685
686 ASSERT_TRUE(
687 TestUpdateCheck(nullptr, // request_params
688 fake_update_response_.GetUpdateResponse(),
689 -1,
690 false, // ping_only
691 ErrorCode::kSuccess,
692 metrics::CheckResult::kUpdateAvailable,
693 metrics::CheckReaction::kUpdating,
694 metrics::DownloadErrorCode::kUnset,
695 &response,
696 nullptr));
697 EXPECT_TRUE(response.update_exists);
698}
699
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700700TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700701 string rollback_version = "1234.0.0";
702 OmahaResponse response;
703
704 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700705 fake_system_state_.set_payload_state(&mock_payload_state);
706
Chris Sosa77f79e82014-06-02 18:16:24 -0700707 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
708 .WillRepeatedly(Return(rollback_version));
709
Alex Deymo8e18f932015-03-27 16:16:59 -0700710 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700711 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700712 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700713 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700714 -1,
715 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700716 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700717 metrics::CheckResult::kUpdateAvailable,
718 metrics::CheckReaction::kIgnored,
719 metrics::DownloadErrorCode::kUnset,
720 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700721 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700722 EXPECT_FALSE(response.update_exists);
723}
724
Kevin Cernekee2494e282016-03-29 18:03:53 -0700725// Verify that update checks called during OOBE will only try to download
726// an update if the response includes a non-empty deadline field.
727TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
728 OmahaResponse response;
729
730 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
731 ASSERT_FALSE(
732 TestUpdateCheck(nullptr, // request_params
733 fake_update_response_.GetUpdateResponse(),
734 -1,
735 false, // ping_only
736 ErrorCode::kNonCriticalUpdateInOOBE,
737 metrics::CheckResult::kUnset,
738 metrics::CheckReaction::kUnset,
739 metrics::DownloadErrorCode::kUnset,
740 &response,
741 nullptr));
742 EXPECT_FALSE(response.update_exists);
743
Alex Deymo46a9aae2016-05-04 20:20:11 -0700744 // The IsOOBEComplete() value is ignored when the OOBE flow is not enabled.
745 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
746 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
747 fake_update_response_.GetUpdateResponse(),
748 -1,
749 false, // ping_only
750 ErrorCode::kSuccess,
751 metrics::CheckResult::kUpdateAvailable,
752 metrics::CheckReaction::kUpdating,
753 metrics::DownloadErrorCode::kUnset,
754 &response,
755 nullptr));
756 EXPECT_TRUE(response.update_exists);
757 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
758
759 // The payload is applied when a deadline was set in the response.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700760 fake_update_response_.deadline = "20101020";
761 ASSERT_TRUE(
762 TestUpdateCheck(nullptr, // request_params
763 fake_update_response_.GetUpdateResponse(),
764 -1,
765 false, // ping_only
766 ErrorCode::kSuccess,
767 metrics::CheckResult::kUpdateAvailable,
768 metrics::CheckReaction::kUpdating,
769 metrics::DownloadErrorCode::kUnset,
770 &response,
771 nullptr));
772 EXPECT_TRUE(response.update_exists);
773}
774
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700775TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700776 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700777 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700778 params.set_wall_clock_based_wait_enabled(true);
779 params.set_update_check_count_wait_enabled(false);
780 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700781
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700782 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700783 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700784 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700785 -1,
786 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700787 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800788 metrics::CheckResult::kUpdateAvailable,
789 metrics::CheckReaction::kDeferring,
790 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700791 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700792 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700793 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700794
795 // Verify if we are interactive check we don't defer.
796 params.set_interactive(true);
797 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700798 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700799 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700800 -1,
801 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700802 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800803 metrics::CheckResult::kUpdateAvailable,
804 metrics::CheckReaction::kUpdating,
805 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700806 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700807 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700808 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700809}
810
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700811TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700812 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700813 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700814 params.set_wall_clock_based_wait_enabled(false);
815 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700816
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700817 params.set_update_check_count_wait_enabled(true);
818 params.set_min_update_checks_needed(1);
819 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700820
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700821 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700822 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700823 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700824 -1,
825 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700826 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800827 metrics::CheckResult::kUpdateAvailable,
828 metrics::CheckReaction::kUpdating,
829 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700830 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700831 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700832 EXPECT_TRUE(response.update_exists);
833}
834
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700835TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700836 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700837 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700838 params.set_wall_clock_based_wait_enabled(true);
839 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700840
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700841 params.set_update_check_count_wait_enabled(true);
842 params.set_min_update_checks_needed(1);
843 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700844
Alex Deymo8e18f932015-03-27 16:16:59 -0700845 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700846 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700847 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700848 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700849 -1,
850 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700851 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800852 metrics::CheckResult::kUpdateAvailable,
853 metrics::CheckReaction::kUpdating,
854 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700855 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700856 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700857 EXPECT_TRUE(response.update_exists);
858}
859
860
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700861TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700862 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700863 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700864 params.set_wall_clock_based_wait_enabled(true);
865 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700866
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700867 params.set_update_check_count_wait_enabled(true);
868 params.set_min_update_checks_needed(0);
869 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700870
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700871 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700872 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700873 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700874 -1,
875 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700876 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800877 metrics::CheckResult::kUpdateAvailable,
878 metrics::CheckReaction::kUpdating,
879 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700880 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700881 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700882
Ben Chan9abb7632014-08-07 00:10:53 -0700883 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700884 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700885 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700886 EXPECT_TRUE(response.update_exists);
887}
888
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700889TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700890 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700891 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700892 params.set_wall_clock_based_wait_enabled(true);
893 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700894
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700895 params.set_update_check_count_wait_enabled(true);
896 params.set_min_update_checks_needed(1);
897 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700898
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700899 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700900 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700901 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700902 -1,
903 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700904 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800905 metrics::CheckResult::kUpdateAvailable,
906 metrics::CheckReaction::kDeferring,
907 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700908 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700909 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700910
Ben Chan9abb7632014-08-07 00:10:53 -0700911 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700912 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700913 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700914 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700915
916 // Verify if we are interactive check we don't defer.
917 params.set_interactive(true);
918 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700919 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700920 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700921 -1,
922 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700923 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800924 metrics::CheckResult::kUpdateAvailable,
925 metrics::CheckReaction::kUpdating,
926 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700927 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700928 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700929 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700930}
931
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700932TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700933 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700934 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700935 params.set_wall_clock_based_wait_enabled(true);
936 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700937
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700938 params.set_update_check_count_wait_enabled(true);
939 params.set_min_update_checks_needed(1);
940 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700941
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700942 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700943
944 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700945 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700946 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700947 -1,
948 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700949 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800950 metrics::CheckResult::kUpdateAvailable,
951 metrics::CheckReaction::kDeferring,
952 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700953 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700954 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700955
Ben Chan9abb7632014-08-07 00:10:53 -0700956 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700957 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700958 // count remains the same, as the decrementing happens in update_attempter
959 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700960 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700961 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700962
963 // Verify if we are interactive check we don't defer.
964 params.set_interactive(true);
965 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700966 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700967 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700968 -1,
969 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700970 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800971 metrics::CheckResult::kUpdateAvailable,
972 metrics::CheckReaction::kUpdating,
973 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700974 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700975 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700976 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700977}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700978
Alex Deymo8e18f932015-03-27 16:16:59 -0700979TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
980 OmahaResponse response;
981 OmahaRequestParams params = request_params_;
982 fake_update_response_.include_cohorts = true;
983 fake_update_response_.cohort = "s/154454/8479665";
984 fake_update_response_.cohorthint = "please-put-me-on-beta";
985 fake_update_response_.cohortname = "stable";
986
987 ASSERT_TRUE(TestUpdateCheck(&params,
988 fake_update_response_.GetUpdateResponse(),
989 -1,
990 false, // ping_only
991 ErrorCode::kSuccess,
992 metrics::CheckResult::kUpdateAvailable,
993 metrics::CheckReaction::kUpdating,
994 metrics::DownloadErrorCode::kUnset,
995 &response,
996 nullptr));
997
998 string value;
999 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1000 EXPECT_EQ(fake_update_response_.cohort, value);
1001
1002 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1003 EXPECT_EQ(fake_update_response_.cohorthint, value);
1004
1005 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1006 EXPECT_EQ(fake_update_response_.cohortname, value);
1007}
1008
1009TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
1010 OmahaResponse response;
1011 OmahaRequestParams params = request_params_;
1012 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1013 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
1014 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
1015 fake_update_response_.include_cohorts = true;
1016 fake_update_response_.cohort = "s/154454/8479665";
1017 fake_update_response_.cohorthint = "please-put-me-on-beta";
1018 fake_update_response_.cohortname = "";
1019
1020 ASSERT_TRUE(TestUpdateCheck(&params,
1021 fake_update_response_.GetUpdateResponse(),
1022 -1,
1023 false, // ping_only
1024 ErrorCode::kSuccess,
1025 metrics::CheckResult::kUpdateAvailable,
1026 metrics::CheckReaction::kUpdating,
1027 metrics::DownloadErrorCode::kUnset,
1028 &response,
1029 nullptr));
1030
1031 string value;
1032 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1033 EXPECT_EQ(fake_update_response_.cohort, value);
1034
1035 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1036 EXPECT_EQ(fake_update_response_.cohorthint, value);
1037
1038 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1039}
1040
1041TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
1042 OmahaResponse response;
1043 OmahaRequestParams params = request_params_;
1044 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1045
1046 ASSERT_TRUE(TestUpdateCheck(&params,
1047 fake_update_response_.GetUpdateResponse(),
1048 -1,
1049 false, // ping_only
1050 ErrorCode::kSuccess,
1051 metrics::CheckResult::kUpdateAvailable,
1052 metrics::CheckReaction::kUpdating,
1053 metrics::DownloadErrorCode::kUnset,
1054 &response,
1055 nullptr));
1056
1057 string value;
1058 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1059 EXPECT_EQ("old_value", value);
1060
1061 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1062 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1063}
1064
Alex Deymo00d79ac2015-06-29 15:41:49 -07001065TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
1066 OmahaResponse response;
1067 OmahaRequestParams params = request_params_;
1068 fake_update_response_.include_cohorts = true;
1069 fake_update_response_.cohort = "s/154454/8479665";
1070 fake_update_response_.cohorthint = "please-put-me-on-beta";
1071 fake_update_response_.cohortname = "stable";
1072
1073 ASSERT_TRUE(TestUpdateCheck(&params,
1074 fake_update_response_.GetNoUpdateResponse(),
1075 -1,
1076 false, // ping_only
1077 ErrorCode::kSuccess,
1078 metrics::CheckResult::kNoUpdateAvailable,
1079 metrics::CheckReaction::kUnset,
1080 metrics::DownloadErrorCode::kUnset,
1081 &response,
1082 nullptr));
1083
1084 string value;
1085 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1086 EXPECT_EQ(fake_update_response_.cohort, value);
1087
1088 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1089 EXPECT_EQ(fake_update_response_.cohorthint, value);
1090
1091 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1092 EXPECT_EQ(fake_update_response_.cohortname, value);
1093}
1094
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001095TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001096 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001097
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001098 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001099 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001100
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001101 OmahaRequestParams params = request_params_;
1102 fake_system_state_.set_request_params(&params);
Ben Chan5c02c132017-06-27 07:10:36 -07001103 OmahaRequestAction action(
1104 &fake_system_state_,
1105 nullptr,
1106 base::MakeUnique<MockHttpFetcher>(http_response.data(),
1107 http_response.size(),
1108 nullptr),
1109 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001110 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001111 ActionProcessor processor;
1112 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001113 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001114
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -07001115 loop.PostTask(base::Bind(
1116 [](ActionProcessor* processor) { processor->StartProcessing(); },
1117 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -07001118 loop.Run();
1119 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001120 EXPECT_FALSE(processor.IsRunning());
1121}
1122
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001123TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001124 OmahaResponse response;
1125 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001126 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001127 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001128 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001129 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001130 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001131 metrics::CheckResult::kParsingError,
1132 metrics::CheckReaction::kUnset,
1133 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -07001134 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001135 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001136 EXPECT_FALSE(response.update_exists);
1137}
1138
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001139TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001140 OmahaResponse response;
1141 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001142 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001143 "",
1144 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001145 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001146 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001147 metrics::CheckResult::kParsingError,
1148 metrics::CheckReaction::kUnset,
1149 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001150 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001151 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001152 EXPECT_FALSE(response.update_exists);
1153}
1154
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001155TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001156 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001157 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001158 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001159 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1160 "<daystart elapsed_seconds=\"100\"/>"
1161 "<app appid=\"foo\" status=\"ok\">"
1162 "<ping status=\"ok\"/>"
1163 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001164 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001165 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001166 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001167 metrics::CheckResult::kParsingError,
1168 metrics::CheckReaction::kUnset,
1169 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001170 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001171 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001172 EXPECT_FALSE(response.update_exists);
1173}
1174
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001175TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001176 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001177 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001178 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001179 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1180 "<daystart elapsed_seconds=\"100\"/>"
1181 "<app appid=\"foo\" status=\"ok\">"
1182 "<ping status=\"ok\"/>"
1183 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001184 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001185 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001186 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001187 metrics::CheckResult::kParsingError,
1188 metrics::CheckReaction::kUnset,
1189 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001190 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001191 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001192 EXPECT_FALSE(response.update_exists);
1193}
1194
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001195TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001196 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001197 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001198 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001199 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1200 "<daystart elapsed_seconds=\"100\"/>"
1201 "<app appid=\"foo\" status=\"ok\">"
1202 "<ping status=\"ok\"/>"
1203 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001204 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001205 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001206 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001207 metrics::CheckResult::kParsingError,
1208 metrics::CheckReaction::kUnset,
1209 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001210 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001211 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001212 EXPECT_FALSE(response.update_exists);
1213}
1214
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001215TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001216 string input_response =
1217 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1218 "<daystart elapsed_seconds=\"100\"/>"
1219 "<app appid=\"xyz\" status=\"ok\">"
1220 "<updatecheck status=\"ok\">"
1221 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001222 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001223 "<packages><package hash=\"not-used\" name=\"f\" "
1224 "size=\"587\"/></packages>"
1225 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001226 "ChromeOSVersion=\"10.2.3.4\" "
1227 "Prompt=\"false\" "
1228 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001229 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001230 "sha256=\"lkq34j5345\" "
1231 "needsadmin=\"true\" "
1232 "/></actions></manifest></updatecheck></app></response>";
1233 LOG(INFO) << "Input Response = " << input_response;
1234
Darin Petkov6a5b3222010-07-13 14:55:28 -07001235 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001236 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001237 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -07001238 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001239 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001240 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001241 metrics::CheckResult::kUpdateAvailable,
1242 metrics::CheckReaction::kUpdating,
1243 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001244 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001245 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001246 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001247 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001248 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001249 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001250 EXPECT_EQ("lkq34j5345", response.hash);
1251 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001252 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001253 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001254}
1255
1256namespace {
1257class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1258 public:
1259 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001260 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001261 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001262};
1263
Alex Deymo60ca1a72015-06-18 18:19:15 -07001264void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001265 processor->StartProcessing();
1266 CHECK(processor->IsRunning());
1267 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001268}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001269} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001270
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001271TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001272 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001273 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001274
Alex Deymo60ca1a72015-06-18 18:19:15 -07001275 string http_response("doesn't matter");
Ben Chan5c02c132017-06-27 07:10:36 -07001276 OmahaRequestAction action(
1277 &fake_system_state_,
1278 nullptr,
1279 base::MakeUnique<MockHttpFetcher>(http_response.data(),
1280 http_response.size(),
1281 nullptr),
1282 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001283 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001284 ActionProcessor processor;
1285 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001286 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001287
Alex Deymo60ca1a72015-06-18 18:19:15 -07001288 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1289 loop.Run();
1290 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001291}
1292
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001293TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001294 string output;
1295 EXPECT_TRUE(XmlEncode("ab", &output));
1296 EXPECT_EQ("ab", output);
1297 EXPECT_TRUE(XmlEncode("a<b", &output));
1298 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001299 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1300 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001301 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1302 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001303 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001304 EXPECT_FALSE(XmlEncode("\xc2", &output));
1305 // Fail with invalid ASCII-7 chars.
1306 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1307}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001308
Alex Deymob0d74eb2015-03-30 17:59:17 -07001309TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1310 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1311 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1312}
1313
1314TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001315 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001316
1317 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001318 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001319 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001320 OmahaRequestParams::kOsVersion,
1321 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001322 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001323 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001324 "0.1.0.0",
1325 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001326 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001327 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001328 "ChromeOSFirmware.1.0",
1329 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001330 false, // delta okay
1331 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001332 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001333 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001334 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1335 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1336 fake_prefs_.SetString(kPrefsOmahaCohortName,
Alex Vakulenko0103c362016-01-20 07:56:15 -08001337 base::JoinString(
1338 vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001339 OmahaResponse response;
1340 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001341 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001342 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001343 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001344 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001345 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001346 metrics::CheckResult::kParsingError,
1347 metrics::CheckReaction::kUnset,
1348 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001349 &response,
1350 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001351 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001352 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001353 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1354 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1355 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1356 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1357 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1358 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1359 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1360 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1361 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1362 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1363 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1364 // Values from Prefs that are too big are removed from the XML instead of
1365 // encoded.
1366 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001367}
1368
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001369TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001370 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001371 fake_update_response_.deadline = "&lt;20110101";
1372 fake_update_response_.more_info_url = "testthe&lt;url";
1373 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001374 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001375 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001376 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001377 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001378 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001379 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001380 metrics::CheckResult::kUpdateAvailable,
1381 metrics::CheckReaction::kUpdating,
1382 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001383 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001384 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001385
1386 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001387 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001388 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001389}
1390
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001391TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001392 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001393 // overflows int32_t:
1394 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001395 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001396 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001397 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001398 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001399 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001400 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001401 metrics::CheckResult::kUpdateAvailable,
1402 metrics::CheckReaction::kUpdating,
1403 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001404 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001405 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001406
1407 EXPECT_EQ(response.size, 123123123123123ll);
1408}
1409
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001410TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001411 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001412 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001413 fake_system_state_.set_prefs(&prefs);
1414
Darin Petkov95508da2011-01-05 12:42:29 -08001415 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1416 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001417 // An existing but empty previous version means that we didn't reboot to a new
1418 // update, therefore, no need to update the previous version.
1419 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001420 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001421 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001422 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001423 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001424 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001425 metrics::CheckResult::kParsingError,
1426 metrics::CheckReaction::kUnset,
1427 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001428 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001429 &post_data));
1430 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001431 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001432 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001433 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001434 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001435 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001436 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1437 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001438 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1439 string::npos);
1440 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1441 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001442 // No <event> tag should be sent if we didn't reboot to an update.
1443 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001444}
1445
Jay Srinivasan0a708742012-03-20 11:26:12 -07001446
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001447TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001448 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001449 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001450 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1451 "invalid xml>",
1452 &post_data);
1453 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001454 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001455 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001456 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001457 OmahaEvent::kTypeUpdateDownloadStarted,
1458 OmahaEvent::kResultSuccess);
1459 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001460 EXPECT_EQ(post_str.find("ping"), string::npos);
1461 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001462}
1463
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001464TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001465 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001466 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001467 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1468 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001469 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001470 "invalid xml>",
1471 &post_data);
1472 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001473 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001474 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001475 " <event eventtype=\"%d\" eventresult=\"%d\" "
1476 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001477 OmahaEvent::kTypeDownloadComplete,
1478 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001479 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001480 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001481 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001482}
1483
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001484TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001485 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001486 // Create a copy of the OmahaRequestParams to reuse it later.
1487 OmahaRequestParams params = request_params_;
1488 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001489 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001490 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001491 nullptr,
Ben Chan5c02c132017-06-27 07:10:36 -07001492 base::MakeUnique<MockHttpFetcher>(http_response.data(),
1493 http_response.size(),
1494 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001495 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001496 EXPECT_FALSE(update_check_action.IsEvent());
1497
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001498 params = request_params_;
1499 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001500 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001501 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001502 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Ben Chan5c02c132017-06-27 07:10:36 -07001503 base::MakeUnique<MockHttpFetcher>(http_response.data(),
1504 http_response.size(),
1505 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001506 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001507 EXPECT_TRUE(event_action.IsEvent());
1508}
1509
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001510TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001511 for (int i = 0; i < 2; i++) {
1512 bool delta_okay = i == 1;
1513 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001514 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001515 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001516 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001517 OmahaRequestParams::kOsVersion,
1518 "service_pack",
1519 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001520 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001521 "0.1.0.0",
1522 "en-US",
1523 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001524 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001525 "ChromeOSFirmware.1.0",
1526 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001527 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001528 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001529 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001530 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001531 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001532 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001533 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001534 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001535 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001536 metrics::CheckResult::kParsingError,
1537 metrics::CheckReaction::kUnset,
1538 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001539 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001540 &post_data));
1541 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001542 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001543 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1544 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001545 string::npos)
1546 << "i = " << i;
1547 }
1548}
1549
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001550TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001551 for (int i = 0; i < 2; i++) {
1552 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001553 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001554 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001555 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001556 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001557 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001558 OmahaRequestParams::kOsVersion,
1559 "service_pack",
1560 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001561 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001562 "0.1.0.0",
1563 "en-US",
1564 "unittest_track",
1565 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001566 "ChromeOSFirmware.1.0",
1567 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001568 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001569 interactive,
1570 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001571 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001572 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001573 "invalid xml>",
1574 -1,
1575 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001576 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001577 metrics::CheckResult::kParsingError,
1578 metrics::CheckReaction::kUnset,
1579 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001580 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001581 &post_data));
1582 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001583 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001584 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1585 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001586 string::npos)
1587 << "i = " << i;
1588 }
1589}
1590
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001591TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001592 OmahaEvent default_event;
1593 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1594 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001595 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001596
1597 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1598 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1599 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001600 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001601
1602 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1603 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001604 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001605 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1606 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001607 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001608}
1609
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001610void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001611 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001612 fake_system_state_.set_prefs(&prefs);
1613 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1614 .Times(AnyNumber());
1615 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1616 // Add a few hours to the day difference to test no rounding, etc.
1617 int64_t five_days_ago =
1618 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1619 int64_t six_days_ago =
1620 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1621 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1622 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1623 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1624 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1625 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1626 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001627 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001628 ASSERT_TRUE(
1629 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001630 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001631 -1,
1632 ping_only,
1633 ErrorCode::kSuccess,
1634 metrics::CheckResult::kUnset,
1635 metrics::CheckReaction::kUnset,
1636 metrics::DownloadErrorCode::kUnset,
1637 nullptr,
1638 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001639 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001640 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1641 string::npos);
1642 if (ping_only) {
1643 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1644 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1645 } else {
1646 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1647 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001648 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001649}
1650
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001651TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1652 PingTest(true /* ping_only */);
1653}
1654
1655TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1656 PingTest(false /* ping_only */);
1657}
1658
1659TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001660 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001661 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001662 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1663 .Times(AnyNumber());
1664 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001665 int64_t three_days_ago =
1666 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1667 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001668 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1669 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001670 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1671 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1672 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1673 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001674 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001675 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001676 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001677 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001678 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001679 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001680 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001681 metrics::CheckResult::kNoUpdateAvailable,
1682 metrics::CheckReaction::kUnset,
1683 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001684 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001685 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001686 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001687 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001688 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001689}
1690
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001691TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001692 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001693 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001694 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1695 .Times(AnyNumber());
1696 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001697 int64_t four_days_ago =
1698 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1699 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001700 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1701 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001702 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1703 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1704 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1705 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001706 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001707 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001708 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001709 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001710 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001711 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001712 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001713 metrics::CheckResult::kNoUpdateAvailable,
1714 metrics::CheckReaction::kUnset,
1715 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001716 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001717 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001718 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001719 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001720 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001721}
1722
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001723TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001724 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001725 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001726 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1727 .Times(AnyNumber());
1728 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001729 int64_t one_hour_ago =
1730 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001731 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1732 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001733 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1734 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1735 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1736 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001737 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1738 // send a ping.
1739 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1740 .WillOnce(Return(true));
1741 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1742 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001743 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001744 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001745 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001746 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001747 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001748 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001749 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001750 metrics::CheckResult::kNoUpdateAvailable,
1751 metrics::CheckReaction::kUnset,
1752 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001753 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001754 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001755 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001756 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001757}
1758
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001759TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001760 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001761 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001762 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001763 int64_t now = Time::Now().ToInternalValue();
1764 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1765 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1766 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1767 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1768 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1769 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001770 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001771 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001772 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001773 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001774 -1,
1775 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001776 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001777 metrics::CheckResult::kUnset,
1778 metrics::CheckReaction::kUnset,
1779 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001780 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001781 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08001782 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07001783}
1784
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001785TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001786 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001787 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001788 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1789 .Times(AnyNumber());
1790 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001791 int64_t future =
1792 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001793 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1794 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001795 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1796 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1797 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1798 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1799 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1800 .WillOnce(Return(true));
1801 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1802 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001803 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001804 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001805 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001806 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1807 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001808 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001809 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001810 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001811 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001812 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001813 metrics::CheckResult::kNoUpdateAvailable,
1814 metrics::CheckReaction::kUnset,
1815 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001816 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001817 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001818 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001819 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001820}
1821
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001822TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001823 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001824 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001825 // may fail if it runs for longer than 5 seconds. It shouldn't run
1826 // that long though.
1827 int64_t midnight =
1828 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1829 int64_t midnight_slack =
1830 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001831 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001832 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001833 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1834 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001835 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1836 AllOf(Ge(midnight), Le(midnight_slack))))
1837 .WillOnce(Return(true));
1838 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1839 AllOf(Ge(midnight), Le(midnight_slack))))
1840 .WillOnce(Return(true));
1841 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001842 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001843 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1844 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001845 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001846 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001847 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001848 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001849 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001850 metrics::CheckResult::kNoUpdateAvailable,
1851 metrics::CheckReaction::kUnset,
1852 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001853 nullptr,
1854 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001855}
1856
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001857TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001858 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001859 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001860 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1861 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001862 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1863 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1864 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001865 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001866 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1867 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001868 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001869 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001870 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001871 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001872 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001873 metrics::CheckResult::kNoUpdateAvailable,
1874 metrics::CheckReaction::kUnset,
1875 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001876 nullptr,
1877 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001878}
1879
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001880TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001881 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001882 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001883 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1884 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001885 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1886 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1887 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001888 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001889 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1890 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001891 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001892 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001893 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001894 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001895 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001896 metrics::CheckResult::kNoUpdateAvailable,
1897 metrics::CheckReaction::kUnset,
1898 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001899 nullptr,
1900 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001901}
1902
Alex Deymob3fa53b2016-04-18 19:57:58 -07001903TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1904 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1905 // the "eol" attribute.
1906 ASSERT_TRUE(
1907 TestUpdateCheck(nullptr, // request_params
1908 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1909 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1910 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1911 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/>"
1912 "</app></response>",
1913 -1,
1914 false, // ping_only
1915 ErrorCode::kSuccess,
1916 metrics::CheckResult::kNoUpdateAvailable,
1917 metrics::CheckReaction::kUnset,
1918 metrics::DownloadErrorCode::kUnset,
1919 nullptr,
1920 nullptr));
1921 string eol_pref;
1922 EXPECT_TRUE(
1923 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1924 // Note that the eol="eol" attribute should be ignored and the _eol should be
1925 // used instead.
1926 EXPECT_EQ("security-only", eol_pref);
1927}
1928
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001929TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001930 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001931 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001932 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001933 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001934 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001935 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001936 metrics::CheckResult::kParsingError,
1937 metrics::CheckReaction::kUnset,
1938 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001939 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001940 &post_data));
1941 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001942 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001943 EXPECT_EQ(post_str.find("machineid="), string::npos);
1944 EXPECT_EQ(post_str.find("userid="), string::npos);
1945}
1946
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001947TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001948 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001949 const int http_error_code =
1950 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001951 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001952 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001953 "",
1954 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001955 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001956 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001957 metrics::CheckResult::kDownloadError,
1958 metrics::CheckReaction::kUnset,
1959 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001960 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001961 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001962 EXPECT_FALSE(response.update_exists);
1963}
1964
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001965TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001966 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001967 const int http_error_code =
1968 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001969 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001970 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001971 "",
1972 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001973 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001974 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001975 metrics::CheckResult::kDownloadError,
1976 metrics::CheckReaction::kUnset,
1977 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001978 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001979 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001980 EXPECT_FALSE(response.update_exists);
1981}
1982
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001983TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001984 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001985 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001986 params.set_wall_clock_based_wait_enabled(true);
1987 params.set_waiting_period(TimeDelta().FromDays(1));
1988 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001989
Sen Jiang7c1171e2016-06-23 11:35:40 -07001990 Time arbitrary_date;
1991 Time::FromString("6/4/1989", &arbitrary_date);
1992 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
1993 ASSERT_FALSE(TestUpdateCheck(&params,
1994 fake_update_response_.GetUpdateResponse(),
1995 -1,
1996 false, // ping_only
1997 ErrorCode::kOmahaUpdateDeferredPerPolicy,
1998 metrics::CheckResult::kUpdateAvailable,
1999 metrics::CheckReaction::kDeferring,
2000 metrics::DownloadErrorCode::kUnset,
2001 &response,
2002 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002003
Ben Chan9abb7632014-08-07 00:10:53 -07002004 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002005 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07002006 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002007 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07002008
2009 // Verify if we are interactive check we don't defer.
2010 params.set_interactive(true);
Sen Jiang7c1171e2016-06-23 11:35:40 -07002011 ASSERT_TRUE(TestUpdateCheck(&params,
2012 fake_update_response_.GetUpdateResponse(),
2013 -1,
2014 false, // ping_only
2015 ErrorCode::kSuccess,
2016 metrics::CheckResult::kUpdateAvailable,
2017 metrics::CheckReaction::kUpdating,
2018 metrics::DownloadErrorCode::kUnset,
2019 &response,
2020 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07002021 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002022}
2023
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002024TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002025 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002026 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002027 params.set_wall_clock_based_wait_enabled(true);
2028 params.set_waiting_period(TimeDelta().FromDays(1));
2029 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002030
Sen Jiang7c1171e2016-06-23 11:35:40 -07002031 Time t1, t2;
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002032 Time::FromString("1/1/2012", &t1);
Sen Jiang7c1171e2016-06-23 11:35:40 -07002033 Time::FromString("1/3/2012", &t2);
2034 ASSERT_TRUE(
2035 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
2036 fake_system_state_.fake_clock()->SetWallclockTime(t2);
2037 ASSERT_TRUE(TestUpdateCheck(&params,
2038 fake_update_response_.GetUpdateResponse(),
2039 -1,
2040 false, // ping_only
2041 ErrorCode::kSuccess,
2042 metrics::CheckResult::kUpdateAvailable,
2043 metrics::CheckReaction::kUpdating,
2044 metrics::DownloadErrorCode::kUnset,
2045 &response,
2046 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002047
2048 EXPECT_TRUE(response.update_exists);
2049
2050 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002051 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002052 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002053 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2054}
2055
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002056TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002057 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002058 base::ScopedTempDir tempdir;
2059 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002060
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002061 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07002062 OmahaRequestParams params(&fake_system_state_);
2063 params.set_root(tempdir.path().value());
2064 params.set_app_id("{22222222-2222-2222-2222-222222222222}");
2065 params.set_app_version("1.2.3.4");
2066 params.set_current_channel("canary-channel");
2067 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
2068 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002069 EXPECT_TRUE(params.to_more_stable_channel());
2070 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002071 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002072 "invalid xml>",
2073 -1,
2074 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002075 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002076 metrics::CheckResult::kParsingError,
2077 metrics::CheckReaction::kUnset,
2078 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002079 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002080 &post_data));
2081 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002082 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002083 EXPECT_NE(string::npos, post_str.find(
2084 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
2085 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2086 "track=\"stable-channel\" from_track=\"canary-channel\" "));
2087}
2088
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002089TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002090 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002091 base::ScopedTempDir tempdir;
2092 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002093
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002094 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07002095 OmahaRequestParams params(&fake_system_state_);
2096 params.set_root(tempdir.path().value());
2097 params.set_app_id("{11111111-1111-1111-1111-111111111111}");
2098 params.set_app_version("5.6.7.8");
2099 params.set_current_channel("stable-channel");
2100 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
2101 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002102 EXPECT_FALSE(params.to_more_stable_channel());
2103 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002104 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002105 "invalid xml>",
2106 -1,
2107 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002108 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002109 metrics::CheckResult::kParsingError,
2110 metrics::CheckReaction::kUnset,
2111 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002112 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002113 &post_data));
2114 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002115 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002116 EXPECT_NE(string::npos, post_str.find(
2117 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
2118 "version=\"5.6.7.8\" "
2119 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002120 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002121}
2122
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002123// Checks that the initial ping with a=-1 r=-1 is not send when the device
2124// was powerwashed.
2125TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2126 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2127
2128 // Flag that the device was powerwashed in the past.
2129 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
2130
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002131 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002132 ASSERT_TRUE(
2133 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002134 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002135 -1,
2136 false, // ping_only
2137 ErrorCode::kSuccess,
2138 metrics::CheckResult::kNoUpdateAvailable,
2139 metrics::CheckReaction::kUnset,
2140 metrics::DownloadErrorCode::kUnset,
2141 nullptr,
2142 &post_data));
2143 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002144 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002145 EXPECT_EQ(string::npos, post_str.find("<ping"));
2146}
2147
Amin Hassani1677e812017-06-21 13:36:36 -07002148// Checks that the initial ping with a=-1 r=-1 is not send when the device
2149// first_active_omaha_ping_sent is set.
2150TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) {
2151 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2152
2153 // Flag that the device was not powerwashed in the past.
2154 fake_system_state_.fake_hardware()->SetPowerwashCount(0);
2155
2156 // Flag that the device has sent first active ping in the past.
2157 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent();
2158
2159 brillo::Blob post_data;
2160 ASSERT_TRUE(
2161 TestUpdateCheck(nullptr, // request_params
2162 fake_update_response_.GetNoUpdateResponse(),
2163 -1,
2164 false, // ping_only
2165 ErrorCode::kSuccess,
2166 metrics::CheckResult::kNoUpdateAvailable,
2167 metrics::CheckReaction::kUnset,
2168 metrics::DownloadErrorCode::kUnset,
2169 nullptr,
2170 &post_data));
2171 // We shouldn't send a ping in this case since
2172 // first_active_omaha_ping_sent=true
2173 string post_str(post_data.begin(), post_data.end());
2174 EXPECT_EQ(string::npos, post_str.find("<ping"));
2175}
2176
Alex Deymo9fded1e2015-11-05 12:31:19 -08002177// Checks that the event 54 is sent on a reboot to a new update.
2178TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2179 // Flag that the device was updated in a previous boot.
2180 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2181
2182 brillo::Blob post_data;
2183 ASSERT_TRUE(
2184 TestUpdateCheck(nullptr, // request_params
2185 fake_update_response_.GetNoUpdateResponse(),
2186 -1,
2187 false, // ping_only
2188 ErrorCode::kSuccess,
2189 metrics::CheckResult::kNoUpdateAvailable,
2190 metrics::CheckReaction::kUnset,
2191 metrics::DownloadErrorCode::kUnset,
2192 nullptr,
2193 &post_data));
2194 string post_str(post_data.begin(), post_data.end());
2195
2196 // An event 54 is included and has the right version.
2197 EXPECT_NE(string::npos,
2198 post_str.find(base::StringPrintf(
2199 "<event eventtype=\"%d\"",
2200 OmahaEvent::kTypeRebootedAfterUpdate)));
2201 EXPECT_NE(string::npos,
2202 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2203
2204 // The previous version flag should have been removed.
2205 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2206 string prev_version;
2207 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2208 EXPECT_TRUE(prev_version.empty());
2209}
2210
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002211void OmahaRequestActionTest::P2PTest(
2212 bool initial_allow_p2p_for_downloading,
2213 bool initial_allow_p2p_for_sharing,
2214 bool omaha_disable_p2p_for_downloading,
2215 bool omaha_disable_p2p_for_sharing,
2216 bool payload_state_allow_p2p_attempt,
2217 bool expect_p2p_client_lookup,
2218 const string& p2p_client_result_url,
2219 bool expected_allow_p2p_for_downloading,
2220 bool expected_allow_p2p_for_sharing,
2221 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07002222 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002223 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07002224 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2225 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2226 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002227
2228 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002229 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002230 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2231 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002232 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2233 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2234 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2235 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2236 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2237 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2238 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2239 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2240 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2241 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2242
David Zeuthen8f191b22013-08-06 12:27:50 -07002243 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002244 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002245 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2246
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002247 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2248 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002249 .Times(expect_p2p_client_lookup ? 1 : 0);
2250
Alex Deymo8e18f932015-03-27 16:16:59 -07002251 fake_update_response_.disable_p2p_for_downloading =
2252 omaha_disable_p2p_for_downloading;
2253 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07002254 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002255 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07002256 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07002257 -1,
2258 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002259 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002260 metrics::CheckResult::kUpdateAvailable,
2261 metrics::CheckReaction::kUpdating,
2262 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07002263 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002264 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07002265 EXPECT_TRUE(response.update_exists);
2266
Gilad Arnold74b5f552014-10-07 08:17:16 -07002267 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2268 response.disable_p2p_for_downloading);
2269 EXPECT_EQ(omaha_disable_p2p_for_sharing,
2270 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002271
Gilad Arnold74b5f552014-10-07 08:17:16 -07002272 EXPECT_EQ(expected_allow_p2p_for_downloading,
2273 actual_allow_p2p_for_downloading);
2274 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2275 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002276}
2277
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002278TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002279 P2PTest(true, // initial_allow_p2p_for_downloading
2280 true, // initial_allow_p2p_for_sharing
2281 false, // omaha_disable_p2p_for_downloading
2282 false, // omaha_disable_p2p_for_sharing
2283 true, // payload_state_allow_p2p_attempt
2284 true, // expect_p2p_client_lookup
2285 "http://1.3.5.7/p2p", // p2p_client_result_url
2286 true, // expected_allow_p2p_for_downloading
2287 true, // expected_allow_p2p_for_sharing
2288 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002289}
2290
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002291TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002292 P2PTest(true, // initial_allow_p2p_for_downloading
2293 true, // initial_allow_p2p_for_sharing
2294 false, // omaha_disable_p2p_for_downloading
2295 false, // omaha_disable_p2p_for_sharing
2296 true, // payload_state_allow_p2p_attempt
2297 true, // expect_p2p_client_lookup
2298 "", // p2p_client_result_url
2299 false, // expected_allow_p2p_for_downloading
2300 true, // expected_allow_p2p_for_sharing
2301 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002302}
2303
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002304TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002305 P2PTest(false, // initial_allow_p2p_for_downloading
2306 true, // initial_allow_p2p_for_sharing
2307 false, // omaha_disable_p2p_for_downloading
2308 false, // omaha_disable_p2p_for_sharing
2309 true, // payload_state_allow_p2p_attempt
2310 false, // expect_p2p_client_lookup
2311 "unset", // p2p_client_result_url
2312 false, // expected_allow_p2p_for_downloading
2313 true, // expected_allow_p2p_for_sharing
2314 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002315}
2316
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002317TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002318 P2PTest(true, // initial_allow_p2p_for_downloading
2319 true, // initial_allow_p2p_for_sharing
2320 true, // omaha_disable_p2p_for_downloading
2321 false, // omaha_disable_p2p_for_sharing
2322 true, // payload_state_allow_p2p_attempt
2323 false, // expect_p2p_client_lookup
2324 "unset", // p2p_client_result_url
2325 false, // expected_allow_p2p_for_downloading
2326 true, // expected_allow_p2p_for_sharing
2327 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002328}
2329
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002330TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002331 P2PTest(true, // initial_allow_p2p_for_downloading
2332 true, // initial_allow_p2p_for_sharing
2333 false, // omaha_disable_p2p_for_downloading
2334 true, // omaha_disable_p2p_for_sharing
2335 true, // payload_state_allow_p2p_attempt
2336 true, // expect_p2p_client_lookup
2337 "http://1.3.5.7/p2p", // p2p_client_result_url
2338 true, // expected_allow_p2p_for_downloading
2339 false, // expected_allow_p2p_for_sharing
2340 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002341}
2342
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002343TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002344 P2PTest(true, // initial_allow_p2p_for_downloading
2345 true, // initial_allow_p2p_for_sharing
2346 true, // omaha_disable_p2p_for_downloading
2347 true, // omaha_disable_p2p_for_sharing
2348 true, // payload_state_allow_p2p_attempt
2349 false, // expect_p2p_client_lookup
2350 "unset", // p2p_client_result_url
2351 false, // expected_allow_p2p_for_downloading
2352 false, // expected_allow_p2p_for_sharing
2353 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002354}
2355
Alex Deymof329b932014-10-30 01:37:48 -07002356bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2357 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002358 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002359 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002360 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002361 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002362 -1,
2363 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002364 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002365 metrics::CheckResult::kUpdateAvailable,
2366 metrics::CheckReaction::kUpdating,
2367 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002368 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002369 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002370}
2371
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002372TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002373 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002374
Kevin Cernekee2494e282016-03-29 18:03:53 -07002375 // Simulate a successful update check that happens during OOBE. The
2376 // deadline in the response is needed to force the update attempt to
2377 // occur; responses without a deadline seen during OOBE will normally
2378 // return ErrorCode::kNonCriticalUpdateInOOBE.
2379 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2380 fake_update_response_.deadline = "20101020";
2381
David Zeuthen639aa362014-02-03 16:23:44 -08002382 // Check that we parse elapsed_days in the Omaha Response correctly.
2383 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002384 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2385 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002386 EXPECT_TRUE(response.update_exists);
2387 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002388 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002389 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002390 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002391 EXPECT_EQ(prefs_days, 42);
2392
2393 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002394 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002395 EXPECT_TRUE(response.update_exists);
2396 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002397 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002398 EXPECT_EQ(prefs_days, 42);
2399
2400 // Note that elapsed_days is not necessarily divisible by 7 so check
2401 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002402 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2403 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002404 EXPECT_TRUE(response.update_exists);
2405 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002406 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002407 EXPECT_EQ(prefs_days, 21);
2408
2409 // Check that we correctly handle elapsed_days not being included in
2410 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002411 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002412 EXPECT_TRUE(response.update_exists);
2413 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002414}
2415
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002416// If there is no prefs and OOBE is not complete, we should not
2417// report anything to Omaha.
2418TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002419 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002420 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2421 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2422}
David Zeuthen639aa362014-02-03 16:23:44 -08002423
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002424// If OOBE is complete and happened on a valid date (e.g. after Jan
2425// 1 2007 0:00 PST), that date should be used and written to
2426// prefs. However, first try with an invalid date and check we do
2427// nothing.
2428TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2429 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2430 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2431 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2432 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2433}
David Zeuthen639aa362014-02-03 16:23:44 -08002434
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002435// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2436// should yield an InstallDate of 14.
2437TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2438 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2439 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2440 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2441 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002442
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002443 int64_t prefs_days;
2444 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2445 EXPECT_EQ(prefs_days, 14);
2446}
David Zeuthen639aa362014-02-03 16:23:44 -08002447
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002448// Now that we have a valid date in prefs, check that we keep using
2449// that even if OOBE date reports something else. The date Jan 30,
2450// 2007 0:00 PST should yield an InstallDate of 28... but since
2451// there's a prefs file, we should still get 14.
2452TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2453 // Set a valid date in the prefs first.
2454 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002455
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002456 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2457 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2458 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002459
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002460 int64_t prefs_days;
2461 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2462 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002463
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002464 // If we delete the prefs file, we should get 28 days.
2465 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2466 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2467 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2468 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002469}
2470
Darin Petkov6a5b3222010-07-13 14:55:28 -07002471} // namespace chromeos_update_engine