blob: 3f455f339ebe746dcee46c4dbba23397fe128d3e [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>
Alex Deymo8e18f932015-03-27 16:16:59 -070026#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070027#include <base/strings/string_util.h>
28#include <base/strings/stringprintf.h>
29#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/bind_lambda.h>
Alex Deymoc1c17b42015-11-23 03:53:15 -030031#include <brillo/make_unique_ptr.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070032#include <brillo/message_loops/fake_message_loop.h>
33#include <brillo/message_loops/message_loop.h>
34#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070035#include <gtest/gtest.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070036
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/action_pipe.h"
38#include "update_engine/common/constants.h"
39#include "update_engine/common/fake_prefs.h"
40#include "update_engine/common/hash_calculator.h"
41#include "update_engine/common/mock_http_fetcher.h"
42#include "update_engine/common/platform_constants.h"
43#include "update_engine/common/prefs.h"
44#include "update_engine/common/test_utils.h"
45#include "update_engine/common/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;
Alex Deymo10875d92014-11-10 21:52:57 -080054using chromeos_update_engine::test_utils::System;
55using chromeos_update_engine::test_utils::WriteFileString;
Darin Petkov6a5b3222010-07-13 14:55:28 -070056using std::string;
57using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070059using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070060using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070061using testing::Ge;
62using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080063using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070064using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070065using testing::ReturnPointee;
66using testing::SaveArg;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070067using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070068using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070069
Alex Deymo8e18f932015-03-27 16:16:59 -070070namespace {
71
Alex Deymo85616652015-10-15 18:48:31 -070072const char kTestAppId[] = "test-app-id";
73
Alex Deymo8e18f932015-03-27 16:16:59 -070074// This is a helper struct to allow unit tests build an update response with the
75// values they care about.
76struct FakeUpdateResponse {
77 string GetNoUpdateResponse() const {
78 string entity_str;
79 if (include_entity)
80 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070081 return
82 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
83 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070084 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070085 "<app appid=\"" + app_id + "\" " +
86 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
87 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
88 " status=\"ok\">"
89 "<ping status=\"ok\"/>"
90 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070091 }
92
93 string GetUpdateResponse() const {
94 return
95 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
96 "protocol=\"3.0\">"
97 "<daystart elapsed_seconds=\"100\"" +
98 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
99 + "/>"
100 "<app appid=\"" + app_id + "\" " +
101 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
102 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
103 " status=\"ok\">"
104 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
105 "<urls><url codebase=\"" + codebase + "\"/></urls>"
106 "<manifest version=\"" + version + "\">"
107 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
108 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
109 "<actions><action event=\"postinstall\" "
110 "ChromeOSVersion=\"" + version + "\" "
111 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
112 "IsDelta=\"true\" "
113 "IsDeltaPayload=\"true\" "
114 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
115 "sha256=\"" + hash + "\" "
116 "needsadmin=\"" + needsadmin + "\" " +
117 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
118 (disable_p2p_for_downloading ?
119 "DisableP2PForDownloading=\"true\" " : "") +
120 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
121 "/></actions></manifest></updatecheck></app></response>";
122 }
123
124 // Return the payload URL, which is split in two fields in the XML response.
125 string GetPayloadUrl() {
126 return codebase + filename;
127 }
128
Alex Deymo85616652015-10-15 18:48:31 -0700129 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700130 string version = "1.2.3.4";
131 string more_info_url = "http://more/info";
132 string prompt = "true";
133 string codebase = "http://code/base/";
134 string filename = "file.signed";
135 string hash = "HASH1234=";
136 string needsadmin = "false";
137 int64_t size = 123;
138 string deadline = "";
139 string max_days_to_scatter = "7";
140 string elapsed_days = "42";
141
142 // P2P setting defaults to allowed.
143 bool disable_p2p_for_downloading = false;
144 bool disable_p2p_for_sharing = false;
145
146 // Omaha cohorts settings.
147 bool include_cohorts = false;
148 string cohort = "";
149 string cohorthint = "";
150 string cohortname = "";
151
152 // Whether to include the CrOS <!ENTITY> in the XML response.
153 bool include_entity = false;
154};
155
156} // namespace
157
Darin Petkov6a5b3222010-07-13 14:55:28 -0700158namespace chromeos_update_engine {
159
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700160class OmahaRequestActionTest : public ::testing::Test {
161 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800162 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700163 fake_system_state_.set_request_params(&request_params_);
164 fake_system_state_.set_prefs(&fake_prefs_);
165 }
166
167 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800168 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700169 // is used. |payload_state| may be null, in which case a local mock is used.
170 // |p2p_manager| may be null, in which case a local mock is used.
171 // |connection_manager| may be null, in which case a local mock is used.
172 // out_response may be null. If |fail_http_response_code| is non-negative,
173 // the transfer will fail with that code. |ping_only| is passed through to the
174 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
175 // post-data received by the mock HttpFetcher is returned.
176 //
177 // The |expected_check_result|, |expected_check_reaction| and
178 // |expected_error_code| parameters are for checking expectations
179 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
180 // UMA statistics. Use the appropriate ::kUnset value to specify that
181 // the given metric should not be reported.
182 bool TestUpdateCheck(OmahaRequestParams* request_params,
183 const string& http_response,
184 int fail_http_response_code,
185 bool ping_only,
186 ErrorCode expected_code,
187 metrics::CheckResult expected_check_result,
188 metrics::CheckReaction expected_check_reaction,
189 metrics::DownloadErrorCode expected_download_error_code,
190 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700191 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700192
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800193 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700194 // only a ping or also an updatecheck.
195 void PingTest(bool ping_only);
196
197 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700198 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700199 OmahaResponse *response);
200
201 // P2P test helper function.
202 void P2PTest(
203 bool initial_allow_p2p_for_downloading,
204 bool initial_allow_p2p_for_sharing,
205 bool omaha_disable_p2p_for_downloading,
206 bool omaha_disable_p2p_for_sharing,
207 bool payload_state_allow_p2p_attempt,
208 bool expect_p2p_client_lookup,
209 const string& p2p_client_result_url,
210 bool expected_allow_p2p_for_downloading,
211 bool expected_allow_p2p_for_sharing,
212 const string& expected_p2p_url);
213
214 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700215 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700216
217 // By default, all tests use these objects unless they replace them in the
218 // fake_system_state_.
219 OmahaRequestParams request_params_ = OmahaRequestParams{
220 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700221 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700222 OmahaRequestParams::kOsVersion,
223 "service_pack",
224 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700225 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700226 "0.1.0.0",
227 "en-US",
228 "unittest",
229 "OEM MODEL 09235 7471",
230 "ChromeOSFirmware.1.0",
231 "0X0A1",
232 false, // delta okay
233 false, // interactive
234 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700235 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700236
237 FakePrefs fake_prefs_;
238};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700239
240namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700241class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
242 public:
243 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700244 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800245 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700246 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800247 void ProcessingDone(const ActionProcessor* processor,
248 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700249 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700250 }
251
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800252 void ActionCompleted(ActionProcessor* processor,
253 AbstractAction* action,
254 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700255 // make sure actions always succeed
256 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700257 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700258 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700259 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260 }
David Zeuthena99981f2013-04-29 13:42:47 -0700261 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700263} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700264
265class OutputObjectCollectorAction;
266
267template<>
268class ActionTraits<OutputObjectCollectorAction> {
269 public:
270 // Does not take an object for input
271 typedef OmahaResponse InputObjectType;
272 // On success, puts the output path on output
273 typedef NoneType OutputObjectType;
274};
275
276class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
277 public:
278 OutputObjectCollectorAction() : has_input_object_(false) {}
279 void PerformAction() {
280 // copy input object
281 has_input_object_ = HasInputObject();
282 if (has_input_object_)
283 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700284 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700285 }
286 // Should never be called
287 void TerminateProcessing() {
288 CHECK(false);
289 }
290 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700291 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700292 return "OutputObjectCollectorAction";
293 }
Alex Deymof329b932014-10-30 01:37:48 -0700294 string Type() const { return StaticType(); }
Alex Vakulenko0057daa2016-01-23 16:22:50 -0800295 using InputObjectType =
296 ActionTraits<OutputObjectCollectorAction>::InputObjectType;
297 using OutputObjectType =
298 ActionTraits<OutputObjectCollectorAction>::OutputObjectType;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700299 bool has_input_object_;
300 OmahaResponse omaha_response_;
301};
302
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700303bool OmahaRequestActionTest::TestUpdateCheck(
304 OmahaRequestParams* request_params,
305 const string& http_response,
306 int fail_http_response_code,
307 bool ping_only,
308 ErrorCode expected_code,
309 metrics::CheckResult expected_check_result,
310 metrics::CheckReaction expected_check_reaction,
311 metrics::DownloadErrorCode expected_download_error_code,
312 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700313 brillo::Blob* out_post_data) {
314 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700315 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700316 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800317 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700318 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700319 if (fail_http_response_code >= 0) {
320 fetcher->FailTransfer(fail_http_response_code);
321 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700322 if (request_params)
323 fake_system_state_.set_request_params(request_params);
324 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700325 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300326 brillo::make_unique_ptr(fetcher),
Darin Petkov265f2902011-05-09 15:17:40 -0700327 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700328 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700329 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700330
Darin Petkov6a5b3222010-07-13 14:55:28 -0700331 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700332 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700333 processor.EnqueueAction(&action);
334
335 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700336 BondActions(&action, &collector_action);
337 processor.EnqueueAction(&collector_action);
338
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700339 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800340 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700341 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800342 SendEnumToUMA(metrics::kMetricCheckResult,
343 static_cast<int>(expected_check_result),
344 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
345 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700346 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800347 SendEnumToUMA(metrics::kMetricCheckReaction,
348 static_cast<int>(expected_check_reaction),
349 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
350 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700351 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700352 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
353 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800354 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
355 ? 0 : 1);
356
Alex Deymo60ca1a72015-06-18 18:19:15 -0700357 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
358 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
359 loop.Run();
360 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
361 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700362 if (collector_action.has_input_object_ && out_response)
363 *out_response = collector_action.omaha_response_;
364 if (out_post_data)
365 *out_post_data = fetcher->post_data();
366 return collector_action.has_input_object_;
367}
368
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700369// Tests Event requests -- they should always succeed. |out_post_data|
370// may be null; if non-null, the post-data received by the mock
371// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700372void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700373 OmahaEvent* event,
374 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700375 brillo::Blob* out_post_data) {
376 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700377 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700378 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800379 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700380 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700381 FakeSystemState fake_system_state;
382 fake_system_state.set_request_params(&params);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300383 OmahaRequestAction action(&fake_system_state,
384 event,
385 brillo::make_unique_ptr(fetcher),
386 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700387 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700388 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700389 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700390 processor.EnqueueAction(&action);
391
Alex Deymo60ca1a72015-06-18 18:19:15 -0700392 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
393 loop.Run();
394
395 // This test should schedule a callback to notify the crash reporter if
396 // the passed event is an error.
397 EXPECT_EQ(event->result == OmahaEvent::kResultError, loop.PendingTasks());
398
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700399 if (out_post_data)
400 *out_post_data = fetcher->post_data();
401}
402
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700403TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400404 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700405 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400406 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700407 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700408 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400409 -1,
410 false, // ping_only
411 ErrorCode::kOmahaRequestXMLHasEntityDecl,
412 metrics::CheckResult::kParsingError,
413 metrics::CheckReaction::kUnset,
414 metrics::DownloadErrorCode::kUnset,
415 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700416 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400417 EXPECT_FALSE(response.update_exists);
418}
419
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700420TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700421 OmahaResponse response;
422 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700423 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700424 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700425 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700426 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700427 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800428 metrics::CheckResult::kNoUpdateAvailable,
429 metrics::CheckReaction::kUnset,
430 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700431 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700432 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700433 EXPECT_FALSE(response.update_exists);
434}
435
Alex Deymo8e18f932015-03-27 16:16:59 -0700436// Test that all the values in the response are parsed in a normal update
437// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700438TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700439 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700440 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700441 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700442 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700443 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700444 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700445 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700446 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800447 metrics::CheckResult::kUpdateAvailable,
448 metrics::CheckReaction::kUpdating,
449 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700450 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700451 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700452 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700453 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700454 EXPECT_EQ(fake_update_response_.version, response.version);
455 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
456 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
457 EXPECT_EQ(fake_update_response_.hash, response.hash);
458 EXPECT_EQ(fake_update_response_.size, response.size);
459 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
460 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
461 // Omaha cohort attribets are not set in the response, so they should not be
462 // persisted.
463 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
464 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
465 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700466}
467
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700468TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700469 OmahaResponse response;
470 // Set up a connection manager that doesn't allow a valid update over
471 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700472 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700473 fake_system_state_.set_connection_manager(&mock_cm);
474
Alex Deymo30534502015-07-20 15:06:33 -0700475 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
476 .WillRepeatedly(
477 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet),
478 SetArgumentPointee<1>(NetworkTethering::kUnknown),
479 Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700480 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _))
Chris Sosa77f79e82014-06-02 18:16:24 -0700481 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700482
483 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700484 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700485 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700486 -1,
487 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700488 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700489 metrics::CheckResult::kUpdateAvailable,
490 metrics::CheckReaction::kIgnored,
491 metrics::DownloadErrorCode::kUnset,
492 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700493 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700494 EXPECT_FALSE(response.update_exists);
495}
496
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700497TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700498 string rollback_version = "1234.0.0";
499 OmahaResponse response;
500
501 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700502 fake_system_state_.set_payload_state(&mock_payload_state);
503
Chris Sosa77f79e82014-06-02 18:16:24 -0700504 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
505 .WillRepeatedly(Return(rollback_version));
506
Alex Deymo8e18f932015-03-27 16:16:59 -0700507 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700508 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700509 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700510 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700511 -1,
512 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700513 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700514 metrics::CheckResult::kUpdateAvailable,
515 metrics::CheckReaction::kIgnored,
516 metrics::DownloadErrorCode::kUnset,
517 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700518 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700519 EXPECT_FALSE(response.update_exists);
520}
521
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700522TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700523 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700524 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700525 params.set_wall_clock_based_wait_enabled(true);
526 params.set_update_check_count_wait_enabled(false);
527 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700528
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700529 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700530 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700531 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700532 -1,
533 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700534 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800535 metrics::CheckResult::kUpdateAvailable,
536 metrics::CheckReaction::kDeferring,
537 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700538 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700539 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700540 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700541
542 // Verify if we are interactive check we don't defer.
543 params.set_interactive(true);
544 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700545 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700546 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700547 -1,
548 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700549 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800550 metrics::CheckResult::kUpdateAvailable,
551 metrics::CheckReaction::kUpdating,
552 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700553 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700554 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700555 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700556}
557
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700558TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700559 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700560 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700561 params.set_wall_clock_based_wait_enabled(false);
562 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700563
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700564 params.set_update_check_count_wait_enabled(true);
565 params.set_min_update_checks_needed(1);
566 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700567
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700568 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700569 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700570 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700571 -1,
572 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700573 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800574 metrics::CheckResult::kUpdateAvailable,
575 metrics::CheckReaction::kUpdating,
576 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700577 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700578 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700579 EXPECT_TRUE(response.update_exists);
580}
581
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700582TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700583 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700584 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700585 params.set_wall_clock_based_wait_enabled(true);
586 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700587
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700588 params.set_update_check_count_wait_enabled(true);
589 params.set_min_update_checks_needed(1);
590 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700591
Alex Deymo8e18f932015-03-27 16:16:59 -0700592 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700593 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700594 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700595 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700596 -1,
597 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700598 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800599 metrics::CheckResult::kUpdateAvailable,
600 metrics::CheckReaction::kUpdating,
601 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700602 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700603 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700604 EXPECT_TRUE(response.update_exists);
605}
606
607
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700608TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700609 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700610 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700611 params.set_wall_clock_based_wait_enabled(true);
612 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700613
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700614 params.set_update_check_count_wait_enabled(true);
615 params.set_min_update_checks_needed(0);
616 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700617
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700618 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700619 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700620 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700621 -1,
622 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700623 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800624 metrics::CheckResult::kUpdateAvailable,
625 metrics::CheckReaction::kUpdating,
626 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700627 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700628 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700629
Ben Chan9abb7632014-08-07 00:10:53 -0700630 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700631 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700632 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700633 EXPECT_TRUE(response.update_exists);
634}
635
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700636TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700637 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700638 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700639 params.set_wall_clock_based_wait_enabled(true);
640 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700641
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700642 params.set_update_check_count_wait_enabled(true);
643 params.set_min_update_checks_needed(1);
644 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700645
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700646 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700647 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700648 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700649 -1,
650 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700651 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800652 metrics::CheckResult::kUpdateAvailable,
653 metrics::CheckReaction::kDeferring,
654 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700655 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700656 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700657
Ben Chan9abb7632014-08-07 00:10:53 -0700658 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700659 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700660 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700661 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700662
663 // Verify if we are interactive check we don't defer.
664 params.set_interactive(true);
665 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700666 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700667 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700668 -1,
669 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700670 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800671 metrics::CheckResult::kUpdateAvailable,
672 metrics::CheckReaction::kUpdating,
673 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700674 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700675 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700676 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700677}
678
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700679TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700680 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700681 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700682 params.set_wall_clock_based_wait_enabled(true);
683 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700684
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700685 params.set_update_check_count_wait_enabled(true);
686 params.set_min_update_checks_needed(1);
687 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700688
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700689 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700690
691 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700692 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700693 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700694 -1,
695 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700696 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800697 metrics::CheckResult::kUpdateAvailable,
698 metrics::CheckReaction::kDeferring,
699 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700700 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700701 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700702
Ben Chan9abb7632014-08-07 00:10:53 -0700703 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700704 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700705 // count remains the same, as the decrementing happens in update_attempter
706 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700707 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700708 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700709
710 // Verify if we are interactive check we don't defer.
711 params.set_interactive(true);
712 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700713 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700714 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700715 -1,
716 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700717 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800718 metrics::CheckResult::kUpdateAvailable,
719 metrics::CheckReaction::kUpdating,
720 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700721 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700722 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700723 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700724}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700725
Alex Deymo8e18f932015-03-27 16:16:59 -0700726TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
727 OmahaResponse response;
728 OmahaRequestParams params = request_params_;
729 fake_update_response_.include_cohorts = true;
730 fake_update_response_.cohort = "s/154454/8479665";
731 fake_update_response_.cohorthint = "please-put-me-on-beta";
732 fake_update_response_.cohortname = "stable";
733
734 ASSERT_TRUE(TestUpdateCheck(&params,
735 fake_update_response_.GetUpdateResponse(),
736 -1,
737 false, // ping_only
738 ErrorCode::kSuccess,
739 metrics::CheckResult::kUpdateAvailable,
740 metrics::CheckReaction::kUpdating,
741 metrics::DownloadErrorCode::kUnset,
742 &response,
743 nullptr));
744
745 string value;
746 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
747 EXPECT_EQ(fake_update_response_.cohort, value);
748
749 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
750 EXPECT_EQ(fake_update_response_.cohorthint, value);
751
752 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
753 EXPECT_EQ(fake_update_response_.cohortname, value);
754}
755
756TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
757 OmahaResponse response;
758 OmahaRequestParams params = request_params_;
759 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
760 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
761 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
762 fake_update_response_.include_cohorts = true;
763 fake_update_response_.cohort = "s/154454/8479665";
764 fake_update_response_.cohorthint = "please-put-me-on-beta";
765 fake_update_response_.cohortname = "";
766
767 ASSERT_TRUE(TestUpdateCheck(&params,
768 fake_update_response_.GetUpdateResponse(),
769 -1,
770 false, // ping_only
771 ErrorCode::kSuccess,
772 metrics::CheckResult::kUpdateAvailable,
773 metrics::CheckReaction::kUpdating,
774 metrics::DownloadErrorCode::kUnset,
775 &response,
776 nullptr));
777
778 string value;
779 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
780 EXPECT_EQ(fake_update_response_.cohort, value);
781
782 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
783 EXPECT_EQ(fake_update_response_.cohorthint, value);
784
785 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
786}
787
788TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
789 OmahaResponse response;
790 OmahaRequestParams params = request_params_;
791 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
792
793 ASSERT_TRUE(TestUpdateCheck(&params,
794 fake_update_response_.GetUpdateResponse(),
795 -1,
796 false, // ping_only
797 ErrorCode::kSuccess,
798 metrics::CheckResult::kUpdateAvailable,
799 metrics::CheckReaction::kUpdating,
800 metrics::DownloadErrorCode::kUnset,
801 &response,
802 nullptr));
803
804 string value;
805 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
806 EXPECT_EQ("old_value", value);
807
808 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
809 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
810}
811
Alex Deymo00d79ac2015-06-29 15:41:49 -0700812TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
813 OmahaResponse response;
814 OmahaRequestParams params = request_params_;
815 fake_update_response_.include_cohorts = true;
816 fake_update_response_.cohort = "s/154454/8479665";
817 fake_update_response_.cohorthint = "please-put-me-on-beta";
818 fake_update_response_.cohortname = "stable";
819
820 ASSERT_TRUE(TestUpdateCheck(&params,
821 fake_update_response_.GetNoUpdateResponse(),
822 -1,
823 false, // ping_only
824 ErrorCode::kSuccess,
825 metrics::CheckResult::kNoUpdateAvailable,
826 metrics::CheckReaction::kUnset,
827 metrics::DownloadErrorCode::kUnset,
828 &response,
829 nullptr));
830
831 string value;
832 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
833 EXPECT_EQ(fake_update_response_.cohort, value);
834
835 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
836 EXPECT_EQ(fake_update_response_.cohorthint, value);
837
838 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
839 EXPECT_EQ(fake_update_response_.cohortname, value);
840}
841
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700842TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700843 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700844
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700845 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700846 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700847
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700848 OmahaRequestParams params = request_params_;
849 fake_system_state_.set_request_params(&params);
850 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300851 brillo::make_unique_ptr(
852 new MockHttpFetcher(http_response.data(),
853 http_response.size(),
854 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -0700855 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700856 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700857 ActionProcessor processor;
858 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700859 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700860
Alex Deymo60ca1a72015-06-18 18:19:15 -0700861 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
862 loop.Run();
863 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700864 EXPECT_FALSE(processor.IsRunning());
865}
866
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700867TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700868 OmahaResponse response;
869 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700870 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700871 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700872 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700873 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700874 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800875 metrics::CheckResult::kParsingError,
876 metrics::CheckReaction::kUnset,
877 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700878 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700879 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -0700880 EXPECT_FALSE(response.update_exists);
881}
882
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700883TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700884 OmahaResponse response;
885 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700886 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -0700887 "",
888 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700889 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700890 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800891 metrics::CheckResult::kParsingError,
892 metrics::CheckReaction::kUnset,
893 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700894 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700895 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700896 EXPECT_FALSE(response.update_exists);
897}
898
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700899TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700900 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700901 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700902 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700903 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
904 "<daystart elapsed_seconds=\"100\"/>"
905 "<app appid=\"foo\" status=\"ok\">"
906 "<ping status=\"ok\"/>"
907 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700908 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700909 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700910 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800911 metrics::CheckResult::kParsingError,
912 metrics::CheckReaction::kUnset,
913 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700914 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700915 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700916 EXPECT_FALSE(response.update_exists);
917}
918
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700919TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700920 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700921 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700922 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700923 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
924 "<daystart elapsed_seconds=\"100\"/>"
925 "<app appid=\"foo\" status=\"ok\">"
926 "<ping status=\"ok\"/>"
927 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700928 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700929 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700930 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800931 metrics::CheckResult::kParsingError,
932 metrics::CheckReaction::kUnset,
933 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700934 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700935 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700936 EXPECT_FALSE(response.update_exists);
937}
938
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700939TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700940 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700941 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700942 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700943 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
944 "<daystart elapsed_seconds=\"100\"/>"
945 "<app appid=\"foo\" status=\"ok\">"
946 "<ping status=\"ok\"/>"
947 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700948 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700949 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700950 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800951 metrics::CheckResult::kParsingError,
952 metrics::CheckReaction::kUnset,
953 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700954 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700955 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700956 EXPECT_FALSE(response.update_exists);
957}
958
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700959TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700960 string input_response =
961 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
962 "<daystart elapsed_seconds=\"100\"/>"
963 "<app appid=\"xyz\" status=\"ok\">"
964 "<updatecheck status=\"ok\">"
965 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700966 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700967 "<packages><package hash=\"not-used\" name=\"f\" "
968 "size=\"587\"/></packages>"
969 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700970 "ChromeOSVersion=\"10.2.3.4\" "
971 "Prompt=\"false\" "
972 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800973 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700974 "sha256=\"lkq34j5345\" "
975 "needsadmin=\"true\" "
976 "/></actions></manifest></updatecheck></app></response>";
977 LOG(INFO) << "Input Response = " << input_response;
978
Darin Petkov6a5b3222010-07-13 14:55:28 -0700979 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700980 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700981 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700982 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700983 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700984 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800985 metrics::CheckResult::kUpdateAvailable,
986 metrics::CheckReaction::kUpdating,
987 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700988 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700989 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700990 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700991 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800992 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700993 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700994 EXPECT_EQ("lkq34j5345", response.hash);
995 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700996 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700997 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700998}
999
1000namespace {
1001class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1002 public:
1003 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001004 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001005 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001006};
1007
Alex Deymo60ca1a72015-06-18 18:19:15 -07001008void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001009 processor->StartProcessing();
1010 CHECK(processor->IsRunning());
1011 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001012}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001013} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001014
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001015TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001016 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001017 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001018
Alex Deymo60ca1a72015-06-18 18:19:15 -07001019 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001020 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001021 brillo::make_unique_ptr(
1022 new MockHttpFetcher(http_response.data(),
1023 http_response.size(),
1024 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001025 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001026 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001027 ActionProcessor processor;
1028 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001029 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001030
Alex Deymo60ca1a72015-06-18 18:19:15 -07001031 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1032 loop.Run();
1033 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001034}
1035
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001036TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001037 string output;
1038 EXPECT_TRUE(XmlEncode("ab", &output));
1039 EXPECT_EQ("ab", output);
1040 EXPECT_TRUE(XmlEncode("a<b", &output));
1041 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001042 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1043 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001044 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1045 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001046 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001047 EXPECT_FALSE(XmlEncode("\xc2", &output));
1048 // Fail with invalid ASCII-7 chars.
1049 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1050}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001051
Alex Deymob0d74eb2015-03-30 17:59:17 -07001052TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1053 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1054 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1055}
1056
1057TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001058 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001059
1060 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001061 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001062 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001063 OmahaRequestParams::kOsVersion,
1064 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001065 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001066 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001067 "0.1.0.0",
1068 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001069 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001070 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001071 "ChromeOSFirmware.1.0",
1072 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001073 false, // delta okay
1074 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001075 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001076 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001077 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1078 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1079 fake_prefs_.SetString(kPrefsOmahaCohortName,
Alex Vakulenko0103c362016-01-20 07:56:15 -08001080 base::JoinString(
1081 vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001082 OmahaResponse response;
1083 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001084 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001085 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001086 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001087 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001088 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001089 metrics::CheckResult::kParsingError,
1090 metrics::CheckReaction::kUnset,
1091 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001092 &response,
1093 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001094 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001095 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001096 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1097 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1098 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1099 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1100 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1101 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1102 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1103 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1104 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1105 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1106 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1107 // Values from Prefs that are too big are removed from the XML instead of
1108 // encoded.
1109 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001110}
1111
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001112TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001113 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001114 fake_update_response_.deadline = "&lt;20110101";
1115 fake_update_response_.more_info_url = "testthe&lt;url";
1116 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001117 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001118 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001119 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001120 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001121 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001122 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001123 metrics::CheckResult::kUpdateAvailable,
1124 metrics::CheckReaction::kUpdating,
1125 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001126 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001127 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001128
1129 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001130 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001131 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001132}
1133
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001134TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001135 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001136 // overflows int32_t:
1137 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001138 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001139 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001140 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001141 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001142 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001143 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001144 metrics::CheckResult::kUpdateAvailable,
1145 metrics::CheckReaction::kUpdating,
1146 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001147 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001148 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001149
1150 EXPECT_EQ(response.size, 123123123123123ll);
1151}
1152
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001153TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001154 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001155 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001156 fake_system_state_.set_prefs(&prefs);
1157
Darin Petkov95508da2011-01-05 12:42:29 -08001158 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1159 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001160 // An existing but empty previous version means that we didn't reboot to a new
1161 // update, therefore, no need to update the previous version.
1162 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001163 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001164 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001165 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001166 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001167 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001168 metrics::CheckResult::kParsingError,
1169 metrics::CheckReaction::kUnset,
1170 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001171 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001172 &post_data));
1173 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001174 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001175 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001176 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001177 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001178 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001179 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1180 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001181 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1182 string::npos);
1183 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1184 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001185 // No <event> tag should be sent if we didn't reboot to an update.
1186 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001187}
1188
Jay Srinivasan0a708742012-03-20 11:26:12 -07001189
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001190TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001191 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001192 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001193 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1194 "invalid xml>",
1195 &post_data);
1196 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001197 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001198 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001199 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001200 OmahaEvent::kTypeUpdateDownloadStarted,
1201 OmahaEvent::kResultSuccess);
1202 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001203 EXPECT_EQ(post_str.find("ping"), string::npos);
1204 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001205}
1206
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001207TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001208 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001209 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001210 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1211 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001212 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001213 "invalid xml>",
1214 &post_data);
1215 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001216 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001217 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001218 " <event eventtype=\"%d\" eventresult=\"%d\" "
1219 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001220 OmahaEvent::kTypeDownloadComplete,
1221 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001222 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001223 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001224 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001225}
1226
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001227TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001228 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001229 // Create a copy of the OmahaRequestParams to reuse it later.
1230 OmahaRequestParams params = request_params_;
1231 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001232 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001233 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001234 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001235 brillo::make_unique_ptr(
1236 new MockHttpFetcher(http_response.data(),
1237 http_response.size(),
1238 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001239 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001240 EXPECT_FALSE(update_check_action.IsEvent());
1241
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001242 params = request_params_;
1243 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001244 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001245 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001246 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001247 brillo::make_unique_ptr(
1248 new MockHttpFetcher(http_response.data(),
1249 http_response.size(),
1250 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001251 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001252 EXPECT_TRUE(event_action.IsEvent());
1253}
1254
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001255TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001256 for (int i = 0; i < 2; i++) {
1257 bool delta_okay = i == 1;
1258 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001259 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001260 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001261 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001262 OmahaRequestParams::kOsVersion,
1263 "service_pack",
1264 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001265 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001266 "0.1.0.0",
1267 "en-US",
1268 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001269 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001270 "ChromeOSFirmware.1.0",
1271 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001272 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001273 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001274 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001275 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001276 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001277 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001278 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001279 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001280 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001281 metrics::CheckResult::kParsingError,
1282 metrics::CheckReaction::kUnset,
1283 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001284 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001285 &post_data));
1286 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001287 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001288 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1289 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001290 string::npos)
1291 << "i = " << i;
1292 }
1293}
1294
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001295TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001296 for (int i = 0; i < 2; i++) {
1297 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001298 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001299 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001300 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001301 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001302 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001303 OmahaRequestParams::kOsVersion,
1304 "service_pack",
1305 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001306 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001307 "0.1.0.0",
1308 "en-US",
1309 "unittest_track",
1310 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001311 "ChromeOSFirmware.1.0",
1312 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001313 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001314 interactive,
1315 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001316 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001317 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001318 "invalid xml>",
1319 -1,
1320 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001321 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001322 metrics::CheckResult::kParsingError,
1323 metrics::CheckReaction::kUnset,
1324 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001325 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001326 &post_data));
1327 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001328 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001329 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1330 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001331 string::npos)
1332 << "i = " << i;
1333 }
1334}
1335
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001336TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001337 OmahaEvent default_event;
1338 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1339 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001340 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001341
1342 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1343 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1344 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001345 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001346
1347 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1348 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001349 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001350 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1351 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001352 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001353}
1354
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001355void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001356 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001357 fake_system_state_.set_prefs(&prefs);
1358 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1359 .Times(AnyNumber());
1360 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1361 // Add a few hours to the day difference to test no rounding, etc.
1362 int64_t five_days_ago =
1363 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1364 int64_t six_days_ago =
1365 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1366 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1367 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1368 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1369 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1370 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1371 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001372 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001373 ASSERT_TRUE(
1374 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001375 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001376 -1,
1377 ping_only,
1378 ErrorCode::kSuccess,
1379 metrics::CheckResult::kUnset,
1380 metrics::CheckReaction::kUnset,
1381 metrics::DownloadErrorCode::kUnset,
1382 nullptr,
1383 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001384 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001385 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1386 string::npos);
1387 if (ping_only) {
1388 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1389 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1390 } else {
1391 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1392 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001393 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001394}
1395
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001396TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1397 PingTest(true /* ping_only */);
1398}
1399
1400TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1401 PingTest(false /* ping_only */);
1402}
1403
1404TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001405 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001406 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001407 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1408 .Times(AnyNumber());
1409 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001410 int64_t three_days_ago =
1411 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1412 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001413 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1414 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001415 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1416 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1417 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1418 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001419 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001420 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001421 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001422 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001423 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001424 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001425 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001426 metrics::CheckResult::kNoUpdateAvailable,
1427 metrics::CheckReaction::kUnset,
1428 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001429 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001430 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001431 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001432 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001433 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001434}
1435
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001436TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001437 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001438 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001439 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1440 .Times(AnyNumber());
1441 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001442 int64_t four_days_ago =
1443 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1444 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001445 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1446 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001447 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1448 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1449 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1450 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001451 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001452 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001453 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001454 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001455 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001456 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001457 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001458 metrics::CheckResult::kNoUpdateAvailable,
1459 metrics::CheckReaction::kUnset,
1460 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001461 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001462 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001463 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001464 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001465 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001466}
1467
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001468TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001469 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001470 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001471 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1472 .Times(AnyNumber());
1473 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001474 int64_t one_hour_ago =
1475 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001476 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1477 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001478 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1479 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1480 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1481 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001482 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1483 // send a ping.
1484 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1485 .WillOnce(Return(true));
1486 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1487 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001488 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001489 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001490 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001491 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001492 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001493 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001494 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001495 metrics::CheckResult::kNoUpdateAvailable,
1496 metrics::CheckReaction::kUnset,
1497 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001498 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001499 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001500 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001501 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001502}
1503
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001504TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001505 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001506 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001507 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001508 int64_t now = Time::Now().ToInternalValue();
1509 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1510 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1511 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1512 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1513 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1514 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001515 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001516 EXPECT_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(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001519 -1,
1520 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001521 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001522 metrics::CheckResult::kUnset,
1523 metrics::CheckReaction::kUnset,
1524 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001525 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001526 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08001527 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07001528}
1529
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001530TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001531 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001532 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001533 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1534 .Times(AnyNumber());
1535 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001536 int64_t future =
1537 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001538 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1539 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001540 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1541 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1542 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1543 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1544 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1545 .WillOnce(Return(true));
1546 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1547 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001548 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001549 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001550 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001551 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1552 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001553 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001554 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001555 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001556 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001557 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001558 metrics::CheckResult::kNoUpdateAvailable,
1559 metrics::CheckReaction::kUnset,
1560 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001561 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001562 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001563 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001564 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001565}
1566
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001567TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001568 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001569 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001570 // may fail if it runs for longer than 5 seconds. It shouldn't run
1571 // that long though.
1572 int64_t midnight =
1573 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1574 int64_t midnight_slack =
1575 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001576 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001577 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001578 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1579 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001580 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1581 AllOf(Ge(midnight), Le(midnight_slack))))
1582 .WillOnce(Return(true));
1583 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1584 AllOf(Ge(midnight), Le(midnight_slack))))
1585 .WillOnce(Return(true));
1586 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001587 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001588 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1589 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001590 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001591 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001592 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001593 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001594 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001595 metrics::CheckResult::kNoUpdateAvailable,
1596 metrics::CheckReaction::kUnset,
1597 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001598 nullptr,
1599 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001600}
1601
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001602TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
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, _)).Times(0);
1608 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1609 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001610 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001611 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1612 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001613 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001614 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001615 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001616 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001617 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001618 metrics::CheckResult::kNoUpdateAvailable,
1619 metrics::CheckReaction::kUnset,
1620 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001621 nullptr,
1622 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001623}
1624
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001625TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001626 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001627 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001628 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1629 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001630 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1631 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1632 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001633 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001634 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1635 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001636 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001637 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001638 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001639 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001640 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001641 metrics::CheckResult::kNoUpdateAvailable,
1642 metrics::CheckReaction::kUnset,
1643 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001644 nullptr,
1645 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001646}
1647
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001648TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001649 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001650 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001651 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001652 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001653 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001654 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001655 metrics::CheckResult::kParsingError,
1656 metrics::CheckReaction::kUnset,
1657 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001658 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001659 &post_data));
1660 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001661 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001662 EXPECT_EQ(post_str.find("machineid="), string::npos);
1663 EXPECT_EQ(post_str.find("userid="), string::npos);
1664}
1665
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001666TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001667 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001668 const int http_error_code =
1669 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001670 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001671 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001672 "",
1673 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001674 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001675 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001676 metrics::CheckResult::kDownloadError,
1677 metrics::CheckReaction::kUnset,
1678 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001679 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001680 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001681 EXPECT_FALSE(response.update_exists);
1682}
1683
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001684TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001685 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001686 const int http_error_code =
1687 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001688 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001689 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001690 "",
1691 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001692 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001693 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001694 metrics::CheckResult::kDownloadError,
1695 metrics::CheckReaction::kUnset,
1696 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001697 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001698 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001699 EXPECT_FALSE(response.update_exists);
1700}
1701
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001702TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001703 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001704 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001705 params.set_wall_clock_based_wait_enabled(true);
1706 params.set_waiting_period(TimeDelta().FromDays(1));
1707 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001708
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001709 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001710 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001711 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001712 -1,
1713 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001714 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001715 metrics::CheckResult::kUpdateAvailable,
1716 metrics::CheckReaction::kDeferring,
1717 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001718 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001719 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001720
Ben Chan9abb7632014-08-07 00:10:53 -07001721 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001722 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001723 ASSERT_GT(timestamp, 0);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001724 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001725
1726 // Verify if we are interactive check we don't defer.
1727 params.set_interactive(true);
1728 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001729 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001730 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -07001731 -1,
1732 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001733 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001734 metrics::CheckResult::kUpdateAvailable,
1735 metrics::CheckReaction::kUpdating,
1736 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001737 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001738 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001739 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001740}
1741
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001742TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001743 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001744 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001745 params.set_wall_clock_based_wait_enabled(true);
1746 params.set_waiting_period(TimeDelta().FromDays(1));
1747 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001748
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001749 // Set the timestamp to a very old value such that it exceeds the
1750 // waiting period set above.
1751 Time t1;
1752 Time::FromString("1/1/2012", &t1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001753 ASSERT_TRUE(fake_prefs_.SetInt64(
1754 kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001755 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001756 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001757 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001758 -1,
1759 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001760 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001761 metrics::CheckResult::kUpdateAvailable,
1762 metrics::CheckReaction::kUpdating,
1763 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001764 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001765 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001766
1767 EXPECT_TRUE(response.update_exists);
1768
1769 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07001770 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001771 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001772 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1773}
1774
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001775TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001776 // Create a uniquely named test directory.
1777 string test_dir;
1778 ASSERT_TRUE(utils::MakeTempDirectory(
1779 "omaha_request_action-test-XXXXXX", &test_dir));
1780
1781 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1782 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001783 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001784 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001785 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001786 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001787 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001788 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001789 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1790 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1791 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1792 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001793 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001794 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
1795 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001796 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001797 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001798 params.Init("1.2.3.4", "", 0);
1799 EXPECT_EQ("canary-channel", params.current_channel());
1800 EXPECT_EQ("stable-channel", params.target_channel());
1801 EXPECT_TRUE(params.to_more_stable_channel());
1802 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001803 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001804 "invalid xml>",
1805 -1,
1806 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001807 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001808 metrics::CheckResult::kParsingError,
1809 metrics::CheckReaction::kUnset,
1810 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001811 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001812 &post_data));
1813 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001814 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001815 EXPECT_NE(string::npos, post_str.find(
1816 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1817 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1818 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001819
Alex Deymo110e0302015-10-19 20:35:21 -07001820 ASSERT_TRUE(base::DeleteFile(base::FilePath(test_dir), true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001821}
1822
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001823TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001824 // Create a uniquely named test directory.
1825 string test_dir;
1826 ASSERT_TRUE(utils::MakeTempDirectory(
1827 "omaha_request_action-test-XXXXXX", &test_dir));
1828
1829 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1830 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001831 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001832 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001833 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001834 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001835 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001836 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001837 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1838 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1839 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1840 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001841 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001842 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001843 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001844 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001845 params.Init("5.6.7.8", "", 0);
1846 EXPECT_EQ("stable-channel", params.current_channel());
1847 EXPECT_EQ("canary-channel", params.target_channel());
1848 EXPECT_FALSE(params.to_more_stable_channel());
1849 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001850 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001851 "invalid xml>",
1852 -1,
1853 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001854 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001855 metrics::CheckResult::kParsingError,
1856 metrics::CheckReaction::kUnset,
1857 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001858 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001859 &post_data));
1860 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001861 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001862 EXPECT_NE(string::npos, post_str.find(
1863 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1864 "version=\"5.6.7.8\" "
1865 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001866 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001867}
1868
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001869// Checks that the initial ping with a=-1 r=-1 is not send when the device
1870// was powerwashed.
1871TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
1872 fake_prefs_.SetString(kPrefsPreviousVersion, "");
1873
1874 // Flag that the device was powerwashed in the past.
1875 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
1876
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001877 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001878 ASSERT_TRUE(
1879 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001880 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001881 -1,
1882 false, // ping_only
1883 ErrorCode::kSuccess,
1884 metrics::CheckResult::kNoUpdateAvailable,
1885 metrics::CheckReaction::kUnset,
1886 metrics::DownloadErrorCode::kUnset,
1887 nullptr,
1888 &post_data));
1889 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001890 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001891 EXPECT_EQ(string::npos, post_str.find("<ping"));
1892}
1893
Alex Deymo9fded1e2015-11-05 12:31:19 -08001894// Checks that the event 54 is sent on a reboot to a new update.
1895TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
1896 // Flag that the device was updated in a previous boot.
1897 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
1898
1899 brillo::Blob post_data;
1900 ASSERT_TRUE(
1901 TestUpdateCheck(nullptr, // request_params
1902 fake_update_response_.GetNoUpdateResponse(),
1903 -1,
1904 false, // ping_only
1905 ErrorCode::kSuccess,
1906 metrics::CheckResult::kNoUpdateAvailable,
1907 metrics::CheckReaction::kUnset,
1908 metrics::DownloadErrorCode::kUnset,
1909 nullptr,
1910 &post_data));
1911 string post_str(post_data.begin(), post_data.end());
1912
1913 // An event 54 is included and has the right version.
1914 EXPECT_NE(string::npos,
1915 post_str.find(base::StringPrintf(
1916 "<event eventtype=\"%d\"",
1917 OmahaEvent::kTypeRebootedAfterUpdate)));
1918 EXPECT_NE(string::npos,
1919 post_str.find("previousversion=\"1.2.3.4\"></event>"));
1920
1921 // The previous version flag should have been removed.
1922 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
1923 string prev_version;
1924 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
1925 EXPECT_TRUE(prev_version.empty());
1926}
1927
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001928void OmahaRequestActionTest::P2PTest(
1929 bool initial_allow_p2p_for_downloading,
1930 bool initial_allow_p2p_for_sharing,
1931 bool omaha_disable_p2p_for_downloading,
1932 bool omaha_disable_p2p_for_sharing,
1933 bool payload_state_allow_p2p_attempt,
1934 bool expect_p2p_client_lookup,
1935 const string& p2p_client_result_url,
1936 bool expected_allow_p2p_for_downloading,
1937 bool expected_allow_p2p_for_sharing,
1938 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001939 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001940 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07001941 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
1942 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
1943 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07001944
1945 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001946 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07001947 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1948 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07001949 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
1950 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
1951 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
1952 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
1953 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
1954 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
1955 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
1956 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
1957 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
1958 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
1959
David Zeuthen8f191b22013-08-06 12:27:50 -07001960 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001961 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001962 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1963
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001964 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
1965 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07001966 .Times(expect_p2p_client_lookup ? 1 : 0);
1967
Alex Deymo8e18f932015-03-27 16:16:59 -07001968 fake_update_response_.disable_p2p_for_downloading =
1969 omaha_disable_p2p_for_downloading;
1970 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07001971 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001972 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001973 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07001974 -1,
1975 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001976 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001977 metrics::CheckResult::kUpdateAvailable,
1978 metrics::CheckReaction::kUpdating,
1979 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07001980 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001981 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07001982 EXPECT_TRUE(response.update_exists);
1983
Gilad Arnold74b5f552014-10-07 08:17:16 -07001984 EXPECT_EQ(omaha_disable_p2p_for_downloading,
1985 response.disable_p2p_for_downloading);
1986 EXPECT_EQ(omaha_disable_p2p_for_sharing,
1987 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07001988
Gilad Arnold74b5f552014-10-07 08:17:16 -07001989 EXPECT_EQ(expected_allow_p2p_for_downloading,
1990 actual_allow_p2p_for_downloading);
1991 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
1992 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07001993}
1994
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001995TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001996 P2PTest(true, // initial_allow_p2p_for_downloading
1997 true, // initial_allow_p2p_for_sharing
1998 false, // omaha_disable_p2p_for_downloading
1999 false, // omaha_disable_p2p_for_sharing
2000 true, // payload_state_allow_p2p_attempt
2001 true, // expect_p2p_client_lookup
2002 "http://1.3.5.7/p2p", // p2p_client_result_url
2003 true, // expected_allow_p2p_for_downloading
2004 true, // expected_allow_p2p_for_sharing
2005 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002006}
2007
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002008TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002009 P2PTest(true, // initial_allow_p2p_for_downloading
2010 true, // initial_allow_p2p_for_sharing
2011 false, // omaha_disable_p2p_for_downloading
2012 false, // omaha_disable_p2p_for_sharing
2013 true, // payload_state_allow_p2p_attempt
2014 true, // expect_p2p_client_lookup
2015 "", // p2p_client_result_url
2016 false, // expected_allow_p2p_for_downloading
2017 true, // expected_allow_p2p_for_sharing
2018 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002019}
2020
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002021TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002022 P2PTest(false, // 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 false, // expect_p2p_client_lookup
2028 "unset", // p2p_client_result_url
2029 false, // expected_allow_p2p_for_downloading
2030 true, // expected_allow_p2p_for_sharing
2031 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002032}
2033
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002034TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002035 P2PTest(true, // initial_allow_p2p_for_downloading
2036 true, // initial_allow_p2p_for_sharing
2037 true, // omaha_disable_p2p_for_downloading
2038 false, // omaha_disable_p2p_for_sharing
2039 true, // payload_state_allow_p2p_attempt
2040 false, // expect_p2p_client_lookup
2041 "unset", // 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, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002048 P2PTest(true, // initial_allow_p2p_for_downloading
2049 true, // initial_allow_p2p_for_sharing
2050 false, // omaha_disable_p2p_for_downloading
2051 true, // omaha_disable_p2p_for_sharing
2052 true, // payload_state_allow_p2p_attempt
2053 true, // expect_p2p_client_lookup
2054 "http://1.3.5.7/p2p", // p2p_client_result_url
2055 true, // expected_allow_p2p_for_downloading
2056 false, // expected_allow_p2p_for_sharing
2057 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002058}
2059
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002060TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
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 true, // 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 false, // expected_allow_p2p_for_sharing
2070 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002071}
2072
Alex Deymof329b932014-10-30 01:37:48 -07002073bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2074 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002075 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002076 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002077 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002078 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002079 -1,
2080 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002081 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002082 metrics::CheckResult::kUpdateAvailable,
2083 metrics::CheckReaction::kUpdating,
2084 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002085 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002086 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002087}
2088
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002089TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002090 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002091
2092 // Check that we parse elapsed_days in the Omaha Response correctly.
2093 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002094 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2095 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002096 EXPECT_TRUE(response.update_exists);
2097 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002098 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002099 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002100 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002101 EXPECT_EQ(prefs_days, 42);
2102
2103 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002104 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002105 EXPECT_TRUE(response.update_exists);
2106 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002107 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002108 EXPECT_EQ(prefs_days, 42);
2109
2110 // Note that elapsed_days is not necessarily divisible by 7 so check
2111 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002112 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2113 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002114 EXPECT_TRUE(response.update_exists);
2115 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002116 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002117 EXPECT_EQ(prefs_days, 21);
2118
2119 // Check that we correctly handle elapsed_days not being included in
2120 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002121 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002122 EXPECT_TRUE(response.update_exists);
2123 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002124}
2125
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002126// If there is no prefs and OOBE is not complete, we should not
2127// report anything to Omaha.
2128TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
2129 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2130 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2131}
David Zeuthen639aa362014-02-03 16:23:44 -08002132
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002133// If OOBE is complete and happened on a valid date (e.g. after Jan
2134// 1 2007 0:00 PST), that date should be used and written to
2135// prefs. However, first try with an invalid date and check we do
2136// nothing.
2137TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2138 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2139 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2140 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2141 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2142}
David Zeuthen639aa362014-02-03 16:23:44 -08002143
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002144// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2145// should yield an InstallDate of 14.
2146TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2147 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2148 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2149 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2150 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002151
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002152 int64_t prefs_days;
2153 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2154 EXPECT_EQ(prefs_days, 14);
2155}
David Zeuthen639aa362014-02-03 16:23:44 -08002156
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002157// Now that we have a valid date in prefs, check that we keep using
2158// that even if OOBE date reports something else. The date Jan 30,
2159// 2007 0:00 PST should yield an InstallDate of 28... but since
2160// there's a prefs file, we should still get 14.
2161TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2162 // Set a valid date in the prefs first.
2163 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002164
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002165 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2166 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2167 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002168
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002169 int64_t prefs_days;
2170 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2171 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002172
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002173 // If we delete the prefs file, we should get 28 days.
2174 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2175 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2176 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2177 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002178}
2179
Darin Petkov6a5b3222010-07-13 14:55:28 -07002180} // namespace chromeos_update_engine