blob: a7abdeceab902bfa3ca07987091d7f7dd427d230 [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>
Alex Deymo8e18f932015-03-27 16:16:59 -070027#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_util.h>
29#include <base/strings/stringprintf.h>
30#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/bind_lambda.h>
Alex Deymoc1c17b42015-11-23 03:53:15 -030032#include <brillo/make_unique_ptr.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070033#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,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300324 brillo::make_unique_ptr(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
Alex Deymo60ca1a72015-06-18 18:19:15 -0700355 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700356 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700357 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700358 if (collector_action.has_input_object_ && out_response)
359 *out_response = collector_action.omaha_response_;
360 if (out_post_data)
361 *out_post_data = fetcher->post_data();
362 return collector_action.has_input_object_;
363}
364
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700365// Tests Event requests -- they should always succeed. |out_post_data|
366// may be null; if non-null, the post-data received by the mock
367// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700368void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700369 OmahaEvent* event,
370 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700371 brillo::Blob* out_post_data) {
372 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700373 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700374 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800375 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700376 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700377 FakeSystemState fake_system_state;
378 fake_system_state.set_request_params(&params);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300379 OmahaRequestAction action(&fake_system_state,
380 event,
381 brillo::make_unique_ptr(fetcher),
382 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700383 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700384 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700385 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700386 processor.EnqueueAction(&action);
387
Alex Deymo60ca1a72015-06-18 18:19:15 -0700388 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
389 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700390 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700391
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700392 if (out_post_data)
393 *out_post_data = fetcher->post_data();
394}
395
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700396TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400397 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700398 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400399 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700400 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700401 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400402 -1,
403 false, // ping_only
404 ErrorCode::kOmahaRequestXMLHasEntityDecl,
405 metrics::CheckResult::kParsingError,
406 metrics::CheckReaction::kUnset,
407 metrics::DownloadErrorCode::kUnset,
408 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700409 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400410 EXPECT_FALSE(response.update_exists);
411}
412
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700413TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700414 OmahaResponse response;
415 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700416 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700417 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700418 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700419 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700420 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800421 metrics::CheckResult::kNoUpdateAvailable,
422 metrics::CheckReaction::kUnset,
423 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700424 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700425 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700426 EXPECT_FALSE(response.update_exists);
427}
428
Alex Deymo8e18f932015-03-27 16:16:59 -0700429// Test that all the values in the response are parsed in a normal update
430// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700431TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700432 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700433 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700434 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700435 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700436 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700437 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700438 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700439 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800440 metrics::CheckResult::kUpdateAvailable,
441 metrics::CheckReaction::kUpdating,
442 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700443 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700444 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700445 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700446 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700447 EXPECT_EQ(fake_update_response_.version, response.version);
448 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
449 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
450 EXPECT_EQ(fake_update_response_.hash, response.hash);
451 EXPECT_EQ(fake_update_response_.size, response.size);
452 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
453 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
454 // Omaha cohort attribets are not set in the response, so they should not be
455 // persisted.
456 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
457 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
458 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700459}
460
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700461TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700462 OmahaResponse response;
463 // Set up a connection manager that doesn't allow a valid update over
464 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700465 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700466 fake_system_state_.set_connection_manager(&mock_cm);
467
Alex Deymo30534502015-07-20 15:06:33 -0700468 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
469 .WillRepeatedly(
470 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet),
471 SetArgumentPointee<1>(NetworkTethering::kUnknown),
472 Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700473 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _))
Chris Sosa77f79e82014-06-02 18:16:24 -0700474 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700475
476 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700477 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700478 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700479 -1,
480 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700481 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700482 metrics::CheckResult::kUpdateAvailable,
483 metrics::CheckReaction::kIgnored,
484 metrics::DownloadErrorCode::kUnset,
485 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700486 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700487 EXPECT_FALSE(response.update_exists);
488}
489
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700490TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700491 string rollback_version = "1234.0.0";
492 OmahaResponse response;
493
494 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700495 fake_system_state_.set_payload_state(&mock_payload_state);
496
Chris Sosa77f79e82014-06-02 18:16:24 -0700497 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
498 .WillRepeatedly(Return(rollback_version));
499
Alex Deymo8e18f932015-03-27 16:16:59 -0700500 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700501 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700502 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700503 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700504 -1,
505 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700506 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700507 metrics::CheckResult::kUpdateAvailable,
508 metrics::CheckReaction::kIgnored,
509 metrics::DownloadErrorCode::kUnset,
510 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700511 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700512 EXPECT_FALSE(response.update_exists);
513}
514
Kevin Cernekee2494e282016-03-29 18:03:53 -0700515// Verify that update checks called during OOBE will only try to download
516// an update if the response includes a non-empty deadline field.
517TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
518 OmahaResponse response;
519
520 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
521 ASSERT_FALSE(
522 TestUpdateCheck(nullptr, // request_params
523 fake_update_response_.GetUpdateResponse(),
524 -1,
525 false, // ping_only
526 ErrorCode::kNonCriticalUpdateInOOBE,
527 metrics::CheckResult::kUnset,
528 metrics::CheckReaction::kUnset,
529 metrics::DownloadErrorCode::kUnset,
530 &response,
531 nullptr));
532 EXPECT_FALSE(response.update_exists);
533
534 fake_update_response_.deadline = "20101020";
535 ASSERT_TRUE(
536 TestUpdateCheck(nullptr, // request_params
537 fake_update_response_.GetUpdateResponse(),
538 -1,
539 false, // ping_only
540 ErrorCode::kSuccess,
541 metrics::CheckResult::kUpdateAvailable,
542 metrics::CheckReaction::kUpdating,
543 metrics::DownloadErrorCode::kUnset,
544 &response,
545 nullptr));
546 EXPECT_TRUE(response.update_exists);
547}
548
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700549TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700550 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700551 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700552 params.set_wall_clock_based_wait_enabled(true);
553 params.set_update_check_count_wait_enabled(false);
554 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700555
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700556 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700557 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700558 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700559 -1,
560 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700561 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800562 metrics::CheckResult::kUpdateAvailable,
563 metrics::CheckReaction::kDeferring,
564 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700565 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700566 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700567 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700568
569 // Verify if we are interactive check we don't defer.
570 params.set_interactive(true);
571 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700572 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700573 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700574 -1,
575 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700576 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800577 metrics::CheckResult::kUpdateAvailable,
578 metrics::CheckReaction::kUpdating,
579 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700580 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700581 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700582 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700583}
584
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700585TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700586 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700587 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700588 params.set_wall_clock_based_wait_enabled(false);
589 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700590
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700591 params.set_update_check_count_wait_enabled(true);
592 params.set_min_update_checks_needed(1);
593 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700594
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700595 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700596 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700597 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700598 -1,
599 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700600 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800601 metrics::CheckResult::kUpdateAvailable,
602 metrics::CheckReaction::kUpdating,
603 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700604 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700605 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700606 EXPECT_TRUE(response.update_exists);
607}
608
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700609TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700610 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700611 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700612 params.set_wall_clock_based_wait_enabled(true);
613 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700614
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700615 params.set_update_check_count_wait_enabled(true);
616 params.set_min_update_checks_needed(1);
617 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700618
Alex Deymo8e18f932015-03-27 16:16:59 -0700619 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700620 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700621 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700622 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700623 -1,
624 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700625 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800626 metrics::CheckResult::kUpdateAvailable,
627 metrics::CheckReaction::kUpdating,
628 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700629 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700630 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700631 EXPECT_TRUE(response.update_exists);
632}
633
634
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700635TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700636 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700637 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700638 params.set_wall_clock_based_wait_enabled(true);
639 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700640
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700641 params.set_update_check_count_wait_enabled(true);
642 params.set_min_update_checks_needed(0);
643 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700644
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700645 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700646 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700647 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700648 -1,
649 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700650 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800651 metrics::CheckResult::kUpdateAvailable,
652 metrics::CheckReaction::kUpdating,
653 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700654 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700655 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700656
Ben Chan9abb7632014-08-07 00:10:53 -0700657 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700658 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700659 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700660 EXPECT_TRUE(response.update_exists);
661}
662
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700663TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700664 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700665 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700666 params.set_wall_clock_based_wait_enabled(true);
667 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700668
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700669 params.set_update_check_count_wait_enabled(true);
670 params.set_min_update_checks_needed(1);
671 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700672
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700673 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700674 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700675 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700676 -1,
677 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700678 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800679 metrics::CheckResult::kUpdateAvailable,
680 metrics::CheckReaction::kDeferring,
681 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700682 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700683 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700684
Ben Chan9abb7632014-08-07 00:10:53 -0700685 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700686 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700687 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700688 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700689
690 // Verify if we are interactive check we don't defer.
691 params.set_interactive(true);
692 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700693 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700694 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700695 -1,
696 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700697 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800698 metrics::CheckResult::kUpdateAvailable,
699 metrics::CheckReaction::kUpdating,
700 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700701 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700702 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700703 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700704}
705
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700706TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700707 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700708 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700709 params.set_wall_clock_based_wait_enabled(true);
710 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700711
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700712 params.set_update_check_count_wait_enabled(true);
713 params.set_min_update_checks_needed(1);
714 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700715
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700716 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700717
718 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700719 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700720 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700721 -1,
722 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700723 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800724 metrics::CheckResult::kUpdateAvailable,
725 metrics::CheckReaction::kDeferring,
726 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700727 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700728 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700729
Ben Chan9abb7632014-08-07 00:10:53 -0700730 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700731 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700732 // count remains the same, as the decrementing happens in update_attempter
733 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700734 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700735 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700736
737 // Verify if we are interactive check we don't defer.
738 params.set_interactive(true);
739 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700740 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700741 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700742 -1,
743 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700744 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800745 metrics::CheckResult::kUpdateAvailable,
746 metrics::CheckReaction::kUpdating,
747 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700748 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700749 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700750 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700751}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700752
Alex Deymo8e18f932015-03-27 16:16:59 -0700753TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
754 OmahaResponse response;
755 OmahaRequestParams params = request_params_;
756 fake_update_response_.include_cohorts = true;
757 fake_update_response_.cohort = "s/154454/8479665";
758 fake_update_response_.cohorthint = "please-put-me-on-beta";
759 fake_update_response_.cohortname = "stable";
760
761 ASSERT_TRUE(TestUpdateCheck(&params,
762 fake_update_response_.GetUpdateResponse(),
763 -1,
764 false, // ping_only
765 ErrorCode::kSuccess,
766 metrics::CheckResult::kUpdateAvailable,
767 metrics::CheckReaction::kUpdating,
768 metrics::DownloadErrorCode::kUnset,
769 &response,
770 nullptr));
771
772 string value;
773 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
774 EXPECT_EQ(fake_update_response_.cohort, value);
775
776 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
777 EXPECT_EQ(fake_update_response_.cohorthint, value);
778
779 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
780 EXPECT_EQ(fake_update_response_.cohortname, value);
781}
782
783TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
784 OmahaResponse response;
785 OmahaRequestParams params = request_params_;
786 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
787 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
788 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
789 fake_update_response_.include_cohorts = true;
790 fake_update_response_.cohort = "s/154454/8479665";
791 fake_update_response_.cohorthint = "please-put-me-on-beta";
792 fake_update_response_.cohortname = "";
793
794 ASSERT_TRUE(TestUpdateCheck(&params,
795 fake_update_response_.GetUpdateResponse(),
796 -1,
797 false, // ping_only
798 ErrorCode::kSuccess,
799 metrics::CheckResult::kUpdateAvailable,
800 metrics::CheckReaction::kUpdating,
801 metrics::DownloadErrorCode::kUnset,
802 &response,
803 nullptr));
804
805 string value;
806 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
807 EXPECT_EQ(fake_update_response_.cohort, value);
808
809 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
810 EXPECT_EQ(fake_update_response_.cohorthint, value);
811
812 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
813}
814
815TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
816 OmahaResponse response;
817 OmahaRequestParams params = request_params_;
818 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
819
820 ASSERT_TRUE(TestUpdateCheck(&params,
821 fake_update_response_.GetUpdateResponse(),
822 -1,
823 false, // ping_only
824 ErrorCode::kSuccess,
825 metrics::CheckResult::kUpdateAvailable,
826 metrics::CheckReaction::kUpdating,
827 metrics::DownloadErrorCode::kUnset,
828 &response,
829 nullptr));
830
831 string value;
832 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
833 EXPECT_EQ("old_value", value);
834
835 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
836 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
837}
838
Alex Deymo00d79ac2015-06-29 15:41:49 -0700839TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
840 OmahaResponse response;
841 OmahaRequestParams params = request_params_;
842 fake_update_response_.include_cohorts = true;
843 fake_update_response_.cohort = "s/154454/8479665";
844 fake_update_response_.cohorthint = "please-put-me-on-beta";
845 fake_update_response_.cohortname = "stable";
846
847 ASSERT_TRUE(TestUpdateCheck(&params,
848 fake_update_response_.GetNoUpdateResponse(),
849 -1,
850 false, // ping_only
851 ErrorCode::kSuccess,
852 metrics::CheckResult::kNoUpdateAvailable,
853 metrics::CheckReaction::kUnset,
854 metrics::DownloadErrorCode::kUnset,
855 &response,
856 nullptr));
857
858 string value;
859 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
860 EXPECT_EQ(fake_update_response_.cohort, value);
861
862 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
863 EXPECT_EQ(fake_update_response_.cohorthint, value);
864
865 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
866 EXPECT_EQ(fake_update_response_.cohortname, value);
867}
868
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700869TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700870 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700871
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700872 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700873 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700874
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700875 OmahaRequestParams params = request_params_;
876 fake_system_state_.set_request_params(&params);
877 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300878 brillo::make_unique_ptr(
879 new MockHttpFetcher(http_response.data(),
880 http_response.size(),
881 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -0700882 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700883 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700884 ActionProcessor processor;
885 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700886 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700887
Alex Deymo60ca1a72015-06-18 18:19:15 -0700888 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
889 loop.Run();
890 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700891 EXPECT_FALSE(processor.IsRunning());
892}
893
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700894TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700895 OmahaResponse response;
896 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700897 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700898 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700899 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700900 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700901 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800902 metrics::CheckResult::kParsingError,
903 metrics::CheckReaction::kUnset,
904 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700905 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700906 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -0700907 EXPECT_FALSE(response.update_exists);
908}
909
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700910TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700911 OmahaResponse response;
912 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700913 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -0700914 "",
915 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700916 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700917 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800918 metrics::CheckResult::kParsingError,
919 metrics::CheckReaction::kUnset,
920 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700921 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700922 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700923 EXPECT_FALSE(response.update_exists);
924}
925
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700926TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700927 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700928 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700929 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700930 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
931 "<daystart elapsed_seconds=\"100\"/>"
932 "<app appid=\"foo\" status=\"ok\">"
933 "<ping status=\"ok\"/>"
934 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700935 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700936 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700937 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800938 metrics::CheckResult::kParsingError,
939 metrics::CheckReaction::kUnset,
940 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700941 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700942 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700943 EXPECT_FALSE(response.update_exists);
944}
945
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700946TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700947 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700948 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700949 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700950 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
951 "<daystart elapsed_seconds=\"100\"/>"
952 "<app appid=\"foo\" status=\"ok\">"
953 "<ping status=\"ok\"/>"
954 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700955 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700956 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700957 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800958 metrics::CheckResult::kParsingError,
959 metrics::CheckReaction::kUnset,
960 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700961 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700962 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700963 EXPECT_FALSE(response.update_exists);
964}
965
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700966TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700967 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700968 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700969 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700970 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
971 "<daystart elapsed_seconds=\"100\"/>"
972 "<app appid=\"foo\" status=\"ok\">"
973 "<ping status=\"ok\"/>"
974 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700975 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700976 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700977 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800978 metrics::CheckResult::kParsingError,
979 metrics::CheckReaction::kUnset,
980 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700981 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700982 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700983 EXPECT_FALSE(response.update_exists);
984}
985
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700986TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700987 string input_response =
988 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
989 "<daystart elapsed_seconds=\"100\"/>"
990 "<app appid=\"xyz\" status=\"ok\">"
991 "<updatecheck status=\"ok\">"
992 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700993 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700994 "<packages><package hash=\"not-used\" name=\"f\" "
995 "size=\"587\"/></packages>"
996 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700997 "ChromeOSVersion=\"10.2.3.4\" "
998 "Prompt=\"false\" "
999 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001000 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001001 "sha256=\"lkq34j5345\" "
1002 "needsadmin=\"true\" "
1003 "/></actions></manifest></updatecheck></app></response>";
1004 LOG(INFO) << "Input Response = " << input_response;
1005
Darin Petkov6a5b3222010-07-13 14:55:28 -07001006 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001007 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001008 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -07001009 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001010 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001011 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001012 metrics::CheckResult::kUpdateAvailable,
1013 metrics::CheckReaction::kUpdating,
1014 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001015 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001016 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001017 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001018 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001019 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001020 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001021 EXPECT_EQ("lkq34j5345", response.hash);
1022 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001023 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001024 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001025}
1026
1027namespace {
1028class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1029 public:
1030 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001031 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001032 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001033};
1034
Alex Deymo60ca1a72015-06-18 18:19:15 -07001035void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001036 processor->StartProcessing();
1037 CHECK(processor->IsRunning());
1038 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001039}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001040} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001041
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001042TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001043 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001044 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001045
Alex Deymo60ca1a72015-06-18 18:19:15 -07001046 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001047 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001048 brillo::make_unique_ptr(
1049 new MockHttpFetcher(http_response.data(),
1050 http_response.size(),
1051 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001052 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001053 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001054 ActionProcessor processor;
1055 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001056 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001057
Alex Deymo60ca1a72015-06-18 18:19:15 -07001058 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1059 loop.Run();
1060 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001061}
1062
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001063TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001064 string output;
1065 EXPECT_TRUE(XmlEncode("ab", &output));
1066 EXPECT_EQ("ab", output);
1067 EXPECT_TRUE(XmlEncode("a<b", &output));
1068 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001069 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1070 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001071 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1072 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001073 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001074 EXPECT_FALSE(XmlEncode("\xc2", &output));
1075 // Fail with invalid ASCII-7 chars.
1076 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1077}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001078
Alex Deymob0d74eb2015-03-30 17:59:17 -07001079TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1080 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1081 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1082}
1083
1084TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001085 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001086
1087 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001088 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001089 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001090 OmahaRequestParams::kOsVersion,
1091 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001092 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001093 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001094 "0.1.0.0",
1095 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001096 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001097 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001098 "ChromeOSFirmware.1.0",
1099 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001100 false, // delta okay
1101 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001102 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001103 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001104 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1105 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1106 fake_prefs_.SetString(kPrefsOmahaCohortName,
Alex Vakulenko0103c362016-01-20 07:56:15 -08001107 base::JoinString(
1108 vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001109 OmahaResponse response;
1110 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001111 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001112 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001113 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001114 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001115 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001116 metrics::CheckResult::kParsingError,
1117 metrics::CheckReaction::kUnset,
1118 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001119 &response,
1120 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001121 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001122 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001123 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1124 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1125 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1126 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1127 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1128 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1129 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1130 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1131 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1132 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1133 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1134 // Values from Prefs that are too big are removed from the XML instead of
1135 // encoded.
1136 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001137}
1138
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001139TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001140 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001141 fake_update_response_.deadline = "&lt;20110101";
1142 fake_update_response_.more_info_url = "testthe&lt;url";
1143 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001144 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001145 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001146 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001147 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001148 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001149 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001150 metrics::CheckResult::kUpdateAvailable,
1151 metrics::CheckReaction::kUpdating,
1152 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001153 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001154 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001155
1156 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001157 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001158 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001159}
1160
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001161TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001162 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001163 // overflows int32_t:
1164 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001165 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001166 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001167 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001168 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001169 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001170 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001171 metrics::CheckResult::kUpdateAvailable,
1172 metrics::CheckReaction::kUpdating,
1173 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001174 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001175 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001176
1177 EXPECT_EQ(response.size, 123123123123123ll);
1178}
1179
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001180TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001181 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001182 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001183 fake_system_state_.set_prefs(&prefs);
1184
Darin Petkov95508da2011-01-05 12:42:29 -08001185 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1186 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001187 // An existing but empty previous version means that we didn't reboot to a new
1188 // update, therefore, no need to update the previous version.
1189 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001190 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001191 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001192 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001193 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001194 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001195 metrics::CheckResult::kParsingError,
1196 metrics::CheckReaction::kUnset,
1197 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001198 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001199 &post_data));
1200 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001201 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001202 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001203 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001204 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001205 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001206 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1207 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001208 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1209 string::npos);
1210 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1211 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001212 // No <event> tag should be sent if we didn't reboot to an update.
1213 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001214}
1215
Jay Srinivasan0a708742012-03-20 11:26:12 -07001216
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001217TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001218 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001219 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001220 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1221 "invalid xml>",
1222 &post_data);
1223 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001224 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001225 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001226 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001227 OmahaEvent::kTypeUpdateDownloadStarted,
1228 OmahaEvent::kResultSuccess);
1229 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001230 EXPECT_EQ(post_str.find("ping"), string::npos);
1231 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001232}
1233
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001234TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001235 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001236 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001237 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1238 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001239 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001240 "invalid xml>",
1241 &post_data);
1242 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001243 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001244 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001245 " <event eventtype=\"%d\" eventresult=\"%d\" "
1246 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001247 OmahaEvent::kTypeDownloadComplete,
1248 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001249 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001250 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001251 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001252}
1253
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001254TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001255 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001256 // Create a copy of the OmahaRequestParams to reuse it later.
1257 OmahaRequestParams params = request_params_;
1258 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001259 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001260 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001261 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001262 brillo::make_unique_ptr(
1263 new MockHttpFetcher(http_response.data(),
1264 http_response.size(),
1265 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001266 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001267 EXPECT_FALSE(update_check_action.IsEvent());
1268
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001269 params = request_params_;
1270 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001271 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001272 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001273 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001274 brillo::make_unique_ptr(
1275 new MockHttpFetcher(http_response.data(),
1276 http_response.size(),
1277 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001278 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001279 EXPECT_TRUE(event_action.IsEvent());
1280}
1281
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001282TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001283 for (int i = 0; i < 2; i++) {
1284 bool delta_okay = i == 1;
1285 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001286 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001287 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001288 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001289 OmahaRequestParams::kOsVersion,
1290 "service_pack",
1291 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001292 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001293 "0.1.0.0",
1294 "en-US",
1295 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001296 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001297 "ChromeOSFirmware.1.0",
1298 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001299 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001300 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001301 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001302 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001303 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001304 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001305 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001306 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001307 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001308 metrics::CheckResult::kParsingError,
1309 metrics::CheckReaction::kUnset,
1310 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001311 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001312 &post_data));
1313 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001314 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001315 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1316 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001317 string::npos)
1318 << "i = " << i;
1319 }
1320}
1321
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001322TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001323 for (int i = 0; i < 2; i++) {
1324 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001325 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001326 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001327 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001328 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001329 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001330 OmahaRequestParams::kOsVersion,
1331 "service_pack",
1332 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001333 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001334 "0.1.0.0",
1335 "en-US",
1336 "unittest_track",
1337 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001338 "ChromeOSFirmware.1.0",
1339 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001340 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001341 interactive,
1342 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001343 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001344 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001345 "invalid xml>",
1346 -1,
1347 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001348 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001349 metrics::CheckResult::kParsingError,
1350 metrics::CheckReaction::kUnset,
1351 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001352 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001353 &post_data));
1354 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001355 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001356 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1357 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001358 string::npos)
1359 << "i = " << i;
1360 }
1361}
1362
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001363TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001364 OmahaEvent default_event;
1365 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1366 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001367 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001368
1369 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1370 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1371 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001372 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001373
1374 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1375 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001376 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001377 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1378 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001379 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001380}
1381
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001382void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001383 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001384 fake_system_state_.set_prefs(&prefs);
1385 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1386 .Times(AnyNumber());
1387 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1388 // Add a few hours to the day difference to test no rounding, etc.
1389 int64_t five_days_ago =
1390 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1391 int64_t six_days_ago =
1392 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1393 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1394 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1395 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1396 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1397 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1398 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001399 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001400 ASSERT_TRUE(
1401 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001402 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001403 -1,
1404 ping_only,
1405 ErrorCode::kSuccess,
1406 metrics::CheckResult::kUnset,
1407 metrics::CheckReaction::kUnset,
1408 metrics::DownloadErrorCode::kUnset,
1409 nullptr,
1410 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001411 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001412 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1413 string::npos);
1414 if (ping_only) {
1415 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1416 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1417 } else {
1418 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1419 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001420 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001421}
1422
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001423TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1424 PingTest(true /* ping_only */);
1425}
1426
1427TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1428 PingTest(false /* ping_only */);
1429}
1430
1431TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001432 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001433 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001434 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1435 .Times(AnyNumber());
1436 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001437 int64_t three_days_ago =
1438 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1439 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001440 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1441 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001442 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1443 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1444 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1445 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001446 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001447 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001448 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001449 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001450 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001451 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001452 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001453 metrics::CheckResult::kNoUpdateAvailable,
1454 metrics::CheckReaction::kUnset,
1455 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001456 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001457 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001458 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001459 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001460 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001461}
1462
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001463TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001464 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001465 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001466 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1467 .Times(AnyNumber());
1468 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001469 int64_t four_days_ago =
1470 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1471 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001472 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1473 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001474 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1475 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1476 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1477 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001478 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001479 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001480 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001481 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001482 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001483 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001484 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001485 metrics::CheckResult::kNoUpdateAvailable,
1486 metrics::CheckReaction::kUnset,
1487 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001488 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001489 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001490 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001491 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001492 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001493}
1494
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001495TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001496 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001497 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001498 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1499 .Times(AnyNumber());
1500 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001501 int64_t one_hour_ago =
1502 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001503 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1504 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001505 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1506 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1507 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1508 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001509 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1510 // send a ping.
1511 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1512 .WillOnce(Return(true));
1513 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1514 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001515 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001516 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001517 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001518 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001519 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001520 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001521 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001522 metrics::CheckResult::kNoUpdateAvailable,
1523 metrics::CheckReaction::kUnset,
1524 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001525 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001526 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001527 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001528 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001529}
1530
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001531TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001532 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001533 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001534 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001535 int64_t now = Time::Now().ToInternalValue();
1536 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1537 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1538 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1539 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1540 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1541 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001542 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001543 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001544 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001545 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001546 -1,
1547 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001548 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001549 metrics::CheckResult::kUnset,
1550 metrics::CheckReaction::kUnset,
1551 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001552 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001553 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08001554 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07001555}
1556
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001557TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001558 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001559 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001560 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1561 .Times(AnyNumber());
1562 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001563 int64_t future =
1564 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001565 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1566 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001567 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1568 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1569 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1570 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1571 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1572 .WillOnce(Return(true));
1573 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1574 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001575 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001576 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001577 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001578 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1579 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001580 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001581 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001582 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001583 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001584 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001585 metrics::CheckResult::kNoUpdateAvailable,
1586 metrics::CheckReaction::kUnset,
1587 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001588 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001589 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001590 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001591 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001592}
1593
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001594TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001595 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001596 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001597 // may fail if it runs for longer than 5 seconds. It shouldn't run
1598 // that long though.
1599 int64_t midnight =
1600 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1601 int64_t midnight_slack =
1602 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001603 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001604 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001605 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1606 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001607 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1608 AllOf(Ge(midnight), Le(midnight_slack))))
1609 .WillOnce(Return(true));
1610 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1611 AllOf(Ge(midnight), Le(midnight_slack))))
1612 .WillOnce(Return(true));
1613 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001614 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001615 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1616 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001617 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001618 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001619 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001620 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001621 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001622 metrics::CheckResult::kNoUpdateAvailable,
1623 metrics::CheckReaction::kUnset,
1624 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001625 nullptr,
1626 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001627}
1628
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001629TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001630 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001631 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001632 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1633 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001634 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1635 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1636 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001637 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001638 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1639 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001640 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001641 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001642 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001643 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001644 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001645 metrics::CheckResult::kNoUpdateAvailable,
1646 metrics::CheckReaction::kUnset,
1647 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001648 nullptr,
1649 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001650}
1651
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001652TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001653 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001654 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001655 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1656 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001657 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1658 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1659 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001660 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001661 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1662 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001663 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001664 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001665 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001666 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001667 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001668 metrics::CheckResult::kNoUpdateAvailable,
1669 metrics::CheckReaction::kUnset,
1670 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001671 nullptr,
1672 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001673}
1674
Alex Deymob3fa53b2016-04-18 19:57:58 -07001675TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1676 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1677 // the "eol" attribute.
1678 ASSERT_TRUE(
1679 TestUpdateCheck(nullptr, // request_params
1680 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1681 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1682 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1683 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/>"
1684 "</app></response>",
1685 -1,
1686 false, // ping_only
1687 ErrorCode::kSuccess,
1688 metrics::CheckResult::kNoUpdateAvailable,
1689 metrics::CheckReaction::kUnset,
1690 metrics::DownloadErrorCode::kUnset,
1691 nullptr,
1692 nullptr));
1693 string eol_pref;
1694 EXPECT_TRUE(
1695 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1696 // Note that the eol="eol" attribute should be ignored and the _eol should be
1697 // used instead.
1698 EXPECT_EQ("security-only", eol_pref);
1699}
1700
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001701TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001702 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001703 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001704 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001705 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001706 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001707 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001708 metrics::CheckResult::kParsingError,
1709 metrics::CheckReaction::kUnset,
1710 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001711 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001712 &post_data));
1713 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001714 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001715 EXPECT_EQ(post_str.find("machineid="), string::npos);
1716 EXPECT_EQ(post_str.find("userid="), string::npos);
1717}
1718
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001719TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001720 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001721 const int http_error_code =
1722 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001723 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001724 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001725 "",
1726 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001727 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001728 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001729 metrics::CheckResult::kDownloadError,
1730 metrics::CheckReaction::kUnset,
1731 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001732 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001733 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001734 EXPECT_FALSE(response.update_exists);
1735}
1736
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001737TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001738 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001739 const int http_error_code =
1740 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001741 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001742 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001743 "",
1744 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001745 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001746 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001747 metrics::CheckResult::kDownloadError,
1748 metrics::CheckReaction::kUnset,
1749 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001750 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001751 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001752 EXPECT_FALSE(response.update_exists);
1753}
1754
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001755TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001756 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001757 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001758 params.set_wall_clock_based_wait_enabled(true);
1759 params.set_waiting_period(TimeDelta().FromDays(1));
1760 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001761
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001762 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001763 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001764 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001765 -1,
1766 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001767 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001768 metrics::CheckResult::kUpdateAvailable,
1769 metrics::CheckReaction::kDeferring,
1770 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001771 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001772 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001773
Ben Chan9abb7632014-08-07 00:10:53 -07001774 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001775 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001776 ASSERT_GT(timestamp, 0);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001777 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001778
1779 // Verify if we are interactive check we don't defer.
1780 params.set_interactive(true);
1781 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001782 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001783 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -07001784 -1,
1785 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001786 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001787 metrics::CheckResult::kUpdateAvailable,
1788 metrics::CheckReaction::kUpdating,
1789 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001790 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001791 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001792 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001793}
1794
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001795TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001796 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001797 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001798 params.set_wall_clock_based_wait_enabled(true);
1799 params.set_waiting_period(TimeDelta().FromDays(1));
1800 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001801
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001802 // Set the timestamp to a very old value such that it exceeds the
1803 // waiting period set above.
1804 Time t1;
1805 Time::FromString("1/1/2012", &t1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001806 ASSERT_TRUE(fake_prefs_.SetInt64(
1807 kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001808 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001809 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001810 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001811 -1,
1812 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001813 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001814 metrics::CheckResult::kUpdateAvailable,
1815 metrics::CheckReaction::kUpdating,
1816 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001817 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001818 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001819
1820 EXPECT_TRUE(response.update_exists);
1821
1822 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07001823 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001824 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001825 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1826}
1827
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001828TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001829 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07001830 base::ScopedTempDir tempdir;
1831 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001832
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001833 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07001834 OmahaRequestParams params(&fake_system_state_);
1835 params.set_root(tempdir.path().value());
1836 params.set_app_id("{22222222-2222-2222-2222-222222222222}");
1837 params.set_app_version("1.2.3.4");
1838 params.set_current_channel("canary-channel");
1839 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
1840 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001841 EXPECT_TRUE(params.to_more_stable_channel());
1842 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001843 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001844 "invalid xml>",
1845 -1,
1846 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001847 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001848 metrics::CheckResult::kParsingError,
1849 metrics::CheckReaction::kUnset,
1850 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001851 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001852 &post_data));
1853 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001854 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001855 EXPECT_NE(string::npos, post_str.find(
1856 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1857 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1858 "track=\"stable-channel\" from_track=\"canary-channel\" "));
1859}
1860
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001861TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001862 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07001863 base::ScopedTempDir tempdir;
1864 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001865
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001866 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07001867 OmahaRequestParams params(&fake_system_state_);
1868 params.set_root(tempdir.path().value());
1869 params.set_app_id("{11111111-1111-1111-1111-111111111111}");
1870 params.set_app_version("5.6.7.8");
1871 params.set_current_channel("stable-channel");
1872 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
1873 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001874 EXPECT_FALSE(params.to_more_stable_channel());
1875 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001876 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001877 "invalid xml>",
1878 -1,
1879 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001880 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001881 metrics::CheckResult::kParsingError,
1882 metrics::CheckReaction::kUnset,
1883 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001884 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001885 &post_data));
1886 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001887 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001888 EXPECT_NE(string::npos, post_str.find(
1889 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1890 "version=\"5.6.7.8\" "
1891 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001892 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001893}
1894
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001895// Checks that the initial ping with a=-1 r=-1 is not send when the device
1896// was powerwashed.
1897TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
1898 fake_prefs_.SetString(kPrefsPreviousVersion, "");
1899
1900 // Flag that the device was powerwashed in the past.
1901 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
1902
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001903 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001904 ASSERT_TRUE(
1905 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001906 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001907 -1,
1908 false, // ping_only
1909 ErrorCode::kSuccess,
1910 metrics::CheckResult::kNoUpdateAvailable,
1911 metrics::CheckReaction::kUnset,
1912 metrics::DownloadErrorCode::kUnset,
1913 nullptr,
1914 &post_data));
1915 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001916 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001917 EXPECT_EQ(string::npos, post_str.find("<ping"));
1918}
1919
Alex Deymo9fded1e2015-11-05 12:31:19 -08001920// Checks that the event 54 is sent on a reboot to a new update.
1921TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
1922 // Flag that the device was updated in a previous boot.
1923 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
1924
1925 brillo::Blob post_data;
1926 ASSERT_TRUE(
1927 TestUpdateCheck(nullptr, // request_params
1928 fake_update_response_.GetNoUpdateResponse(),
1929 -1,
1930 false, // ping_only
1931 ErrorCode::kSuccess,
1932 metrics::CheckResult::kNoUpdateAvailable,
1933 metrics::CheckReaction::kUnset,
1934 metrics::DownloadErrorCode::kUnset,
1935 nullptr,
1936 &post_data));
1937 string post_str(post_data.begin(), post_data.end());
1938
1939 // An event 54 is included and has the right version.
1940 EXPECT_NE(string::npos,
1941 post_str.find(base::StringPrintf(
1942 "<event eventtype=\"%d\"",
1943 OmahaEvent::kTypeRebootedAfterUpdate)));
1944 EXPECT_NE(string::npos,
1945 post_str.find("previousversion=\"1.2.3.4\"></event>"));
1946
1947 // The previous version flag should have been removed.
1948 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
1949 string prev_version;
1950 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
1951 EXPECT_TRUE(prev_version.empty());
1952}
1953
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001954void OmahaRequestActionTest::P2PTest(
1955 bool initial_allow_p2p_for_downloading,
1956 bool initial_allow_p2p_for_sharing,
1957 bool omaha_disable_p2p_for_downloading,
1958 bool omaha_disable_p2p_for_sharing,
1959 bool payload_state_allow_p2p_attempt,
1960 bool expect_p2p_client_lookup,
1961 const string& p2p_client_result_url,
1962 bool expected_allow_p2p_for_downloading,
1963 bool expected_allow_p2p_for_sharing,
1964 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001965 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001966 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07001967 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
1968 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
1969 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07001970
1971 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001972 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07001973 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1974 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07001975 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
1976 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
1977 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
1978 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
1979 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
1980 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
1981 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
1982 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
1983 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
1984 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
1985
David Zeuthen8f191b22013-08-06 12:27:50 -07001986 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001987 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001988 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1989
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001990 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
1991 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07001992 .Times(expect_p2p_client_lookup ? 1 : 0);
1993
Alex Deymo8e18f932015-03-27 16:16:59 -07001994 fake_update_response_.disable_p2p_for_downloading =
1995 omaha_disable_p2p_for_downloading;
1996 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07001997 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001998 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001999 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07002000 -1,
2001 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002002 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002003 metrics::CheckResult::kUpdateAvailable,
2004 metrics::CheckReaction::kUpdating,
2005 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07002006 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002007 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07002008 EXPECT_TRUE(response.update_exists);
2009
Gilad Arnold74b5f552014-10-07 08:17:16 -07002010 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2011 response.disable_p2p_for_downloading);
2012 EXPECT_EQ(omaha_disable_p2p_for_sharing,
2013 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002014
Gilad Arnold74b5f552014-10-07 08:17:16 -07002015 EXPECT_EQ(expected_allow_p2p_for_downloading,
2016 actual_allow_p2p_for_downloading);
2017 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2018 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002019}
2020
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002021TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002022 P2PTest(true, // initial_allow_p2p_for_downloading
2023 true, // initial_allow_p2p_for_sharing
2024 false, // omaha_disable_p2p_for_downloading
2025 false, // omaha_disable_p2p_for_sharing
2026 true, // payload_state_allow_p2p_attempt
2027 true, // expect_p2p_client_lookup
2028 "http://1.3.5.7/p2p", // p2p_client_result_url
2029 true, // expected_allow_p2p_for_downloading
2030 true, // expected_allow_p2p_for_sharing
2031 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002032}
2033
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002034TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002035 P2PTest(true, // initial_allow_p2p_for_downloading
2036 true, // initial_allow_p2p_for_sharing
2037 false, // omaha_disable_p2p_for_downloading
2038 false, // omaha_disable_p2p_for_sharing
2039 true, // payload_state_allow_p2p_attempt
2040 true, // expect_p2p_client_lookup
2041 "", // p2p_client_result_url
2042 false, // expected_allow_p2p_for_downloading
2043 true, // expected_allow_p2p_for_sharing
2044 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002045}
2046
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002047TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002048 P2PTest(false, // initial_allow_p2p_for_downloading
2049 true, // initial_allow_p2p_for_sharing
2050 false, // omaha_disable_p2p_for_downloading
2051 false, // omaha_disable_p2p_for_sharing
2052 true, // payload_state_allow_p2p_attempt
2053 false, // expect_p2p_client_lookup
2054 "unset", // p2p_client_result_url
2055 false, // expected_allow_p2p_for_downloading
2056 true, // expected_allow_p2p_for_sharing
2057 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002058}
2059
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002060TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002061 P2PTest(true, // initial_allow_p2p_for_downloading
2062 true, // initial_allow_p2p_for_sharing
2063 true, // omaha_disable_p2p_for_downloading
2064 false, // omaha_disable_p2p_for_sharing
2065 true, // payload_state_allow_p2p_attempt
2066 false, // expect_p2p_client_lookup
2067 "unset", // p2p_client_result_url
2068 false, // expected_allow_p2p_for_downloading
2069 true, // expected_allow_p2p_for_sharing
2070 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002071}
2072
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002073TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002074 P2PTest(true, // initial_allow_p2p_for_downloading
2075 true, // initial_allow_p2p_for_sharing
2076 false, // omaha_disable_p2p_for_downloading
2077 true, // omaha_disable_p2p_for_sharing
2078 true, // payload_state_allow_p2p_attempt
2079 true, // expect_p2p_client_lookup
2080 "http://1.3.5.7/p2p", // p2p_client_result_url
2081 true, // expected_allow_p2p_for_downloading
2082 false, // expected_allow_p2p_for_sharing
2083 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002084}
2085
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002086TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002087 P2PTest(true, // initial_allow_p2p_for_downloading
2088 true, // initial_allow_p2p_for_sharing
2089 true, // omaha_disable_p2p_for_downloading
2090 true, // omaha_disable_p2p_for_sharing
2091 true, // payload_state_allow_p2p_attempt
2092 false, // expect_p2p_client_lookup
2093 "unset", // p2p_client_result_url
2094 false, // expected_allow_p2p_for_downloading
2095 false, // expected_allow_p2p_for_sharing
2096 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002097}
2098
Alex Deymof329b932014-10-30 01:37:48 -07002099bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2100 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002101 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002102 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002103 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002104 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002105 -1,
2106 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002107 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002108 metrics::CheckResult::kUpdateAvailable,
2109 metrics::CheckReaction::kUpdating,
2110 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002111 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002112 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002113}
2114
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002115TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002116 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002117
Kevin Cernekee2494e282016-03-29 18:03:53 -07002118 // Simulate a successful update check that happens during OOBE. The
2119 // deadline in the response is needed to force the update attempt to
2120 // occur; responses without a deadline seen during OOBE will normally
2121 // return ErrorCode::kNonCriticalUpdateInOOBE.
2122 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2123 fake_update_response_.deadline = "20101020";
2124
David Zeuthen639aa362014-02-03 16:23:44 -08002125 // Check that we parse elapsed_days in the Omaha Response correctly.
2126 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002127 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2128 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002129 EXPECT_TRUE(response.update_exists);
2130 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002131 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002132 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002133 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002134 EXPECT_EQ(prefs_days, 42);
2135
2136 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002137 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002138 EXPECT_TRUE(response.update_exists);
2139 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002140 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002141 EXPECT_EQ(prefs_days, 42);
2142
2143 // Note that elapsed_days is not necessarily divisible by 7 so check
2144 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002145 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2146 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002147 EXPECT_TRUE(response.update_exists);
2148 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002149 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002150 EXPECT_EQ(prefs_days, 21);
2151
2152 // Check that we correctly handle elapsed_days not being included in
2153 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002154 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002155 EXPECT_TRUE(response.update_exists);
2156 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002157}
2158
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002159// If there is no prefs and OOBE is not complete, we should not
2160// report anything to Omaha.
2161TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002162 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002163 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2164 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2165}
David Zeuthen639aa362014-02-03 16:23:44 -08002166
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002167// If OOBE is complete and happened on a valid date (e.g. after Jan
2168// 1 2007 0:00 PST), that date should be used and written to
2169// prefs. However, first try with an invalid date and check we do
2170// nothing.
2171TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2172 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2173 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2174 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2175 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2176}
David Zeuthen639aa362014-02-03 16:23:44 -08002177
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002178// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2179// should yield an InstallDate of 14.
2180TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2181 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2182 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2183 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2184 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002185
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002186 int64_t prefs_days;
2187 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2188 EXPECT_EQ(prefs_days, 14);
2189}
David Zeuthen639aa362014-02-03 16:23:44 -08002190
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002191// Now that we have a valid date in prefs, check that we keep using
2192// that even if OOBE date reports something else. The date Jan 30,
2193// 2007 0:00 PST should yield an InstallDate of 28... but since
2194// there's a prefs file, we should still get 14.
2195TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2196 // Set a valid date in the prefs first.
2197 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002198
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002199 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2200 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2201 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002202
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002203 int64_t prefs_days;
2204 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2205 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002206
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002207 // If we delete the prefs file, we should get 28 days.
2208 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2209 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2210 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2211 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002212}
2213
Darin Petkov6a5b3222010-07-13 14:55:28 -07002214} // namespace chromeos_update_engine