blob: a560bd201660f9375596014963ce3ac9094f8c99 [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 Deymo8e18f932015-03-27 16:16:59 -070025#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070026#include <base/strings/string_util.h>
27#include <base/strings/stringprintf.h>
28#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070029#include <brillo/bind_lambda.h>
30#include <brillo/message_loops/fake_message_loop.h>
31#include <brillo/message_loops/message_loop.h>
32#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070033#include <gtest/gtest.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070034
Darin Petkov6a5b3222010-07-13 14:55:28 -070035#include "update_engine/action_pipe.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070036#include "update_engine/constants.h"
Alex Deymoe1e3afe2014-10-30 13:02:49 -070037#include "update_engine/fake_prefs.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070038#include "update_engine/mock_connection_manager.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070039#include "update_engine/mock_http_fetcher.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070040#include "update_engine/mock_payload_state.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070041#include "update_engine/omaha_hash_calculator.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070042#include "update_engine/omaha_request_params.h"
Alex Deymoac41a822015-09-15 20:52:53 -070043#include "update_engine/platform_constants.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070044#include "update_engine/prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070045#include "update_engine/test_utils.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070046#include "update_engine/utils.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070047
Darin Petkov1cbd78f2010-07-29 12:38:34 -070048using base::Time;
49using base::TimeDelta;
Alex Deymo10875d92014-11-10 21:52:57 -080050using chromeos_update_engine::test_utils::System;
51using chromeos_update_engine::test_utils::WriteFileString;
Darin Petkov6a5b3222010-07-13 14:55:28 -070052using std::string;
53using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070054using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070055using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070056using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070057using testing::Ge;
58using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080059using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070060using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070061using testing::ReturnPointee;
62using testing::SaveArg;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070063using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070064using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070065
Alex Deymo8e18f932015-03-27 16:16:59 -070066namespace {
67
Alex Deymo85616652015-10-15 18:48:31 -070068const char kTestAppId[] = "test-app-id";
69
Alex Deymo8e18f932015-03-27 16:16:59 -070070// This is a helper struct to allow unit tests build an update response with the
71// values they care about.
72struct FakeUpdateResponse {
73 string GetNoUpdateResponse() const {
74 string entity_str;
75 if (include_entity)
76 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070077 return
78 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
79 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070080 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070081 "<app appid=\"" + app_id + "\" " +
82 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
83 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
84 " status=\"ok\">"
85 "<ping status=\"ok\"/>"
86 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070087 }
88
89 string GetUpdateResponse() const {
90 return
91 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
92 "protocol=\"3.0\">"
93 "<daystart elapsed_seconds=\"100\"" +
94 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
95 + "/>"
96 "<app appid=\"" + app_id + "\" " +
97 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
98 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
99 " status=\"ok\">"
100 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
101 "<urls><url codebase=\"" + codebase + "\"/></urls>"
102 "<manifest version=\"" + version + "\">"
103 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
104 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
105 "<actions><action event=\"postinstall\" "
106 "ChromeOSVersion=\"" + version + "\" "
107 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
108 "IsDelta=\"true\" "
109 "IsDeltaPayload=\"true\" "
110 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
111 "sha256=\"" + hash + "\" "
112 "needsadmin=\"" + needsadmin + "\" " +
113 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
114 (disable_p2p_for_downloading ?
115 "DisableP2PForDownloading=\"true\" " : "") +
116 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
117 "/></actions></manifest></updatecheck></app></response>";
118 }
119
120 // Return the payload URL, which is split in two fields in the XML response.
121 string GetPayloadUrl() {
122 return codebase + filename;
123 }
124
Alex Deymo85616652015-10-15 18:48:31 -0700125 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700126 string version = "1.2.3.4";
127 string more_info_url = "http://more/info";
128 string prompt = "true";
129 string codebase = "http://code/base/";
130 string filename = "file.signed";
131 string hash = "HASH1234=";
132 string needsadmin = "false";
133 int64_t size = 123;
134 string deadline = "";
135 string max_days_to_scatter = "7";
136 string elapsed_days = "42";
137
138 // P2P setting defaults to allowed.
139 bool disable_p2p_for_downloading = false;
140 bool disable_p2p_for_sharing = false;
141
142 // Omaha cohorts settings.
143 bool include_cohorts = false;
144 string cohort = "";
145 string cohorthint = "";
146 string cohortname = "";
147
148 // Whether to include the CrOS <!ENTITY> in the XML response.
149 bool include_entity = false;
150};
151
152} // namespace
153
Darin Petkov6a5b3222010-07-13 14:55:28 -0700154namespace chromeos_update_engine {
155
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700156class OmahaRequestActionTest : public ::testing::Test {
157 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800158 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700159 fake_system_state_.set_request_params(&request_params_);
160 fake_system_state_.set_prefs(&fake_prefs_);
161 }
162
163 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800164 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700165 // is used. |payload_state| may be null, in which case a local mock is used.
166 // |p2p_manager| may be null, in which case a local mock is used.
167 // |connection_manager| may be null, in which case a local mock is used.
168 // out_response may be null. If |fail_http_response_code| is non-negative,
169 // the transfer will fail with that code. |ping_only| is passed through to the
170 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
171 // post-data received by the mock HttpFetcher is returned.
172 //
173 // The |expected_check_result|, |expected_check_reaction| and
174 // |expected_error_code| parameters are for checking expectations
175 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
176 // UMA statistics. Use the appropriate ::kUnset value to specify that
177 // the given metric should not be reported.
178 bool TestUpdateCheck(OmahaRequestParams* request_params,
179 const string& http_response,
180 int fail_http_response_code,
181 bool ping_only,
182 ErrorCode expected_code,
183 metrics::CheckResult expected_check_result,
184 metrics::CheckReaction expected_check_reaction,
185 metrics::DownloadErrorCode expected_download_error_code,
186 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700187 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700188
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800189 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700190 // only a ping or also an updatecheck.
191 void PingTest(bool ping_only);
192
193 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700194 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700195 OmahaResponse *response);
196
197 // P2P test helper function.
198 void P2PTest(
199 bool initial_allow_p2p_for_downloading,
200 bool initial_allow_p2p_for_sharing,
201 bool omaha_disable_p2p_for_downloading,
202 bool omaha_disable_p2p_for_sharing,
203 bool payload_state_allow_p2p_attempt,
204 bool expect_p2p_client_lookup,
205 const string& p2p_client_result_url,
206 bool expected_allow_p2p_for_downloading,
207 bool expected_allow_p2p_for_sharing,
208 const string& expected_p2p_url);
209
210 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700211 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700212
213 // By default, all tests use these objects unless they replace them in the
214 // fake_system_state_.
215 OmahaRequestParams request_params_ = OmahaRequestParams{
216 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700217 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700218 OmahaRequestParams::kOsVersion,
219 "service_pack",
220 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700221 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700222 "0.1.0.0",
223 "en-US",
224 "unittest",
225 "OEM MODEL 09235 7471",
226 "ChromeOSFirmware.1.0",
227 "0X0A1",
228 false, // delta okay
229 false, // interactive
230 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700231 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700232
233 FakePrefs fake_prefs_;
234};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700235
236namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700237class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
238 public:
239 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700240 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800241 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700242 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800243 void ProcessingDone(const ActionProcessor* processor,
244 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700245 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700246 }
247
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800248 void ActionCompleted(ActionProcessor* processor,
249 AbstractAction* action,
250 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700251 // make sure actions always succeed
252 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700253 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700254 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700255 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700256 }
David Zeuthena99981f2013-04-29 13:42:47 -0700257 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700258};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700259} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260
261class OutputObjectCollectorAction;
262
263template<>
264class ActionTraits<OutputObjectCollectorAction> {
265 public:
266 // Does not take an object for input
267 typedef OmahaResponse InputObjectType;
268 // On success, puts the output path on output
269 typedef NoneType OutputObjectType;
270};
271
272class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
273 public:
274 OutputObjectCollectorAction() : has_input_object_(false) {}
275 void PerformAction() {
276 // copy input object
277 has_input_object_ = HasInputObject();
278 if (has_input_object_)
279 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700280 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700281 }
282 // Should never be called
283 void TerminateProcessing() {
284 CHECK(false);
285 }
286 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700287 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700288 return "OutputObjectCollectorAction";
289 }
Alex Deymof329b932014-10-30 01:37:48 -0700290 string Type() const { return StaticType(); }
Darin Petkov6a5b3222010-07-13 14:55:28 -0700291 bool has_input_object_;
292 OmahaResponse omaha_response_;
293};
294
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700295bool OmahaRequestActionTest::TestUpdateCheck(
296 OmahaRequestParams* request_params,
297 const string& http_response,
298 int fail_http_response_code,
299 bool ping_only,
300 ErrorCode expected_code,
301 metrics::CheckResult expected_check_result,
302 metrics::CheckReaction expected_check_reaction,
303 metrics::DownloadErrorCode expected_download_error_code,
304 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700305 brillo::Blob* out_post_data) {
306 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700307 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700308 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800309 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700310 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700311 if (fail_http_response_code >= 0) {
312 fetcher->FailTransfer(fail_http_response_code);
313 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700314 if (request_params)
315 fake_system_state_.set_request_params(request_params);
316 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700317 nullptr,
Thieu Le116fda32011-04-19 11:01:54 -0700318 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700319 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700320 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700321 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700322
Darin Petkov6a5b3222010-07-13 14:55:28 -0700323 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700324 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700325 processor.EnqueueAction(&action);
326
327 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700328 BondActions(&action, &collector_action);
329 processor.EnqueueAction(&collector_action);
330
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700331 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800332 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700333 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800334 SendEnumToUMA(metrics::kMetricCheckResult,
335 static_cast<int>(expected_check_result),
336 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
337 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700338 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800339 SendEnumToUMA(metrics::kMetricCheckReaction,
340 static_cast<int>(expected_check_reaction),
341 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
342 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700343 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700344 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
345 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800346 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
347 ? 0 : 1);
348
Alex Deymo60ca1a72015-06-18 18:19:15 -0700349 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
350 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
351 loop.Run();
352 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
353 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700354 if (collector_action.has_input_object_ && out_response)
355 *out_response = collector_action.omaha_response_;
356 if (out_post_data)
357 *out_post_data = fetcher->post_data();
358 return collector_action.has_input_object_;
359}
360
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700361// Tests Event requests -- they should always succeed. |out_post_data|
362// may be null; if non-null, the post-data received by the mock
363// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700364void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700365 OmahaEvent* event,
366 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700367 brillo::Blob* out_post_data) {
368 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700369 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700370 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800371 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700372 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700373 FakeSystemState fake_system_state;
374 fake_system_state.set_request_params(&params);
375 OmahaRequestAction action(&fake_system_state, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700376 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700377 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700378 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700379 processor.EnqueueAction(&action);
380
Alex Deymo60ca1a72015-06-18 18:19:15 -0700381 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
382 loop.Run();
383
384 // This test should schedule a callback to notify the crash reporter if
385 // the passed event is an error.
386 EXPECT_EQ(event->result == OmahaEvent::kResultError, loop.PendingTasks());
387
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700388 if (out_post_data)
389 *out_post_data = fetcher->post_data();
390}
391
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700392TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400393 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700394 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400395 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700396 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700397 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400398 -1,
399 false, // ping_only
400 ErrorCode::kOmahaRequestXMLHasEntityDecl,
401 metrics::CheckResult::kParsingError,
402 metrics::CheckReaction::kUnset,
403 metrics::DownloadErrorCode::kUnset,
404 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700405 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400406 EXPECT_FALSE(response.update_exists);
407}
408
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700409TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700410 OmahaResponse response;
411 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700412 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700413 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700414 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700415 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700416 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800417 metrics::CheckResult::kNoUpdateAvailable,
418 metrics::CheckReaction::kUnset,
419 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700420 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700421 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700422 EXPECT_FALSE(response.update_exists);
423}
424
Alex Deymo8e18f932015-03-27 16:16:59 -0700425// Test that all the values in the response are parsed in a normal update
426// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700427TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700428 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700429 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700430 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700431 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700432 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700433 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700434 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700435 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800436 metrics::CheckResult::kUpdateAvailable,
437 metrics::CheckReaction::kUpdating,
438 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700439 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700440 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700441 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700442 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700443 EXPECT_EQ(fake_update_response_.version, response.version);
444 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
445 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
446 EXPECT_EQ(fake_update_response_.hash, response.hash);
447 EXPECT_EQ(fake_update_response_.size, response.size);
448 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
449 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
450 // Omaha cohort attribets are not set in the response, so they should not be
451 // persisted.
452 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
453 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
454 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700455}
456
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700457TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700458 OmahaResponse response;
459 // Set up a connection manager that doesn't allow a valid update over
460 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700461 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700462 fake_system_state_.set_connection_manager(&mock_cm);
463
Alex Deymo30534502015-07-20 15:06:33 -0700464 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
465 .WillRepeatedly(
466 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet),
467 SetArgumentPointee<1>(NetworkTethering::kUnknown),
468 Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700469 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _))
Chris Sosa77f79e82014-06-02 18:16:24 -0700470 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700471
472 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700473 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700474 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700475 -1,
476 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700477 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700478 metrics::CheckResult::kUpdateAvailable,
479 metrics::CheckReaction::kIgnored,
480 metrics::DownloadErrorCode::kUnset,
481 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700482 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700483 EXPECT_FALSE(response.update_exists);
484}
485
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700486TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700487 string rollback_version = "1234.0.0";
488 OmahaResponse response;
489
490 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700491 fake_system_state_.set_payload_state(&mock_payload_state);
492
Chris Sosa77f79e82014-06-02 18:16:24 -0700493 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
494 .WillRepeatedly(Return(rollback_version));
495
Alex Deymo8e18f932015-03-27 16:16:59 -0700496 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700497 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700498 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700499 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700500 -1,
501 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700502 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700503 metrics::CheckResult::kUpdateAvailable,
504 metrics::CheckReaction::kIgnored,
505 metrics::DownloadErrorCode::kUnset,
506 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700507 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700508 EXPECT_FALSE(response.update_exists);
509}
510
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700511TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700512 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700513 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700514 params.set_wall_clock_based_wait_enabled(true);
515 params.set_update_check_count_wait_enabled(false);
516 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700517
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700518 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700519 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700520 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700521 -1,
522 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700523 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800524 metrics::CheckResult::kUpdateAvailable,
525 metrics::CheckReaction::kDeferring,
526 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700527 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700528 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700529 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700530
531 // Verify if we are interactive check we don't defer.
532 params.set_interactive(true);
533 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700534 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700535 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700536 -1,
537 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700538 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800539 metrics::CheckResult::kUpdateAvailable,
540 metrics::CheckReaction::kUpdating,
541 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700542 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700543 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700544 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700545}
546
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700547TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700548 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700549 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700550 params.set_wall_clock_based_wait_enabled(false);
551 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700552
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700553 params.set_update_check_count_wait_enabled(true);
554 params.set_min_update_checks_needed(1);
555 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700556
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700557 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700558 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700559 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700560 -1,
561 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700562 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800563 metrics::CheckResult::kUpdateAvailable,
564 metrics::CheckReaction::kUpdating,
565 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700566 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700567 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700568 EXPECT_TRUE(response.update_exists);
569}
570
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700571TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700572 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700573 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700574 params.set_wall_clock_based_wait_enabled(true);
575 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700576
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700577 params.set_update_check_count_wait_enabled(true);
578 params.set_min_update_checks_needed(1);
579 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700580
Alex Deymo8e18f932015-03-27 16:16:59 -0700581 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700582 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700583 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700584 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700585 -1,
586 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700587 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800588 metrics::CheckResult::kUpdateAvailable,
589 metrics::CheckReaction::kUpdating,
590 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700591 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700592 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700593 EXPECT_TRUE(response.update_exists);
594}
595
596
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700597TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700598 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700599 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700600 params.set_wall_clock_based_wait_enabled(true);
601 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700602
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700603 params.set_update_check_count_wait_enabled(true);
604 params.set_min_update_checks_needed(0);
605 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700606
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700607 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700608 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700609 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700610 -1,
611 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700612 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800613 metrics::CheckResult::kUpdateAvailable,
614 metrics::CheckReaction::kUpdating,
615 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700616 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700617 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700618
Ben Chan9abb7632014-08-07 00:10:53 -0700619 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700620 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700621 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700622 EXPECT_TRUE(response.update_exists);
623}
624
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700625TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700626 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700627 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700628 params.set_wall_clock_based_wait_enabled(true);
629 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700630
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700631 params.set_update_check_count_wait_enabled(true);
632 params.set_min_update_checks_needed(1);
633 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700634
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700635 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700636 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700637 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700638 -1,
639 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700640 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800641 metrics::CheckResult::kUpdateAvailable,
642 metrics::CheckReaction::kDeferring,
643 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700644 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700645 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700646
Ben Chan9abb7632014-08-07 00:10:53 -0700647 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700648 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700649 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700650 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700651
652 // Verify if we are interactive check we don't defer.
653 params.set_interactive(true);
654 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700655 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700656 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700657 -1,
658 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700659 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800660 metrics::CheckResult::kUpdateAvailable,
661 metrics::CheckReaction::kUpdating,
662 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700663 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700664 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700665 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700666}
667
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700668TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700669 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700670 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700671 params.set_wall_clock_based_wait_enabled(true);
672 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700673
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700674 params.set_update_check_count_wait_enabled(true);
675 params.set_min_update_checks_needed(1);
676 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700677
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700678 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700679
680 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700681 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700682 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700683 -1,
684 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700685 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800686 metrics::CheckResult::kUpdateAvailable,
687 metrics::CheckReaction::kDeferring,
688 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700689 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700690 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700691
Ben Chan9abb7632014-08-07 00:10:53 -0700692 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700693 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700694 // count remains the same, as the decrementing happens in update_attempter
695 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700696 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700697 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700698
699 // Verify if we are interactive check we don't defer.
700 params.set_interactive(true);
701 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700702 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700703 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700704 -1,
705 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700706 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800707 metrics::CheckResult::kUpdateAvailable,
708 metrics::CheckReaction::kUpdating,
709 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700710 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700711 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700712 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700713}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700714
Alex Deymo8e18f932015-03-27 16:16:59 -0700715TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
716 OmahaResponse response;
717 OmahaRequestParams params = request_params_;
718 fake_update_response_.include_cohorts = true;
719 fake_update_response_.cohort = "s/154454/8479665";
720 fake_update_response_.cohorthint = "please-put-me-on-beta";
721 fake_update_response_.cohortname = "stable";
722
723 ASSERT_TRUE(TestUpdateCheck(&params,
724 fake_update_response_.GetUpdateResponse(),
725 -1,
726 false, // ping_only
727 ErrorCode::kSuccess,
728 metrics::CheckResult::kUpdateAvailable,
729 metrics::CheckReaction::kUpdating,
730 metrics::DownloadErrorCode::kUnset,
731 &response,
732 nullptr));
733
734 string value;
735 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
736 EXPECT_EQ(fake_update_response_.cohort, value);
737
738 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
739 EXPECT_EQ(fake_update_response_.cohorthint, value);
740
741 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
742 EXPECT_EQ(fake_update_response_.cohortname, value);
743}
744
745TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
746 OmahaResponse response;
747 OmahaRequestParams params = request_params_;
748 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
749 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
750 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
751 fake_update_response_.include_cohorts = true;
752 fake_update_response_.cohort = "s/154454/8479665";
753 fake_update_response_.cohorthint = "please-put-me-on-beta";
754 fake_update_response_.cohortname = "";
755
756 ASSERT_TRUE(TestUpdateCheck(&params,
757 fake_update_response_.GetUpdateResponse(),
758 -1,
759 false, // ping_only
760 ErrorCode::kSuccess,
761 metrics::CheckResult::kUpdateAvailable,
762 metrics::CheckReaction::kUpdating,
763 metrics::DownloadErrorCode::kUnset,
764 &response,
765 nullptr));
766
767 string value;
768 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
769 EXPECT_EQ(fake_update_response_.cohort, value);
770
771 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
772 EXPECT_EQ(fake_update_response_.cohorthint, value);
773
774 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
775}
776
777TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
778 OmahaResponse response;
779 OmahaRequestParams params = request_params_;
780 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
781
782 ASSERT_TRUE(TestUpdateCheck(&params,
783 fake_update_response_.GetUpdateResponse(),
784 -1,
785 false, // ping_only
786 ErrorCode::kSuccess,
787 metrics::CheckResult::kUpdateAvailable,
788 metrics::CheckReaction::kUpdating,
789 metrics::DownloadErrorCode::kUnset,
790 &response,
791 nullptr));
792
793 string value;
794 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
795 EXPECT_EQ("old_value", value);
796
797 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
798 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
799}
800
Alex Deymo00d79ac2015-06-29 15:41:49 -0700801TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
802 OmahaResponse response;
803 OmahaRequestParams params = request_params_;
804 fake_update_response_.include_cohorts = true;
805 fake_update_response_.cohort = "s/154454/8479665";
806 fake_update_response_.cohorthint = "please-put-me-on-beta";
807 fake_update_response_.cohortname = "stable";
808
809 ASSERT_TRUE(TestUpdateCheck(&params,
810 fake_update_response_.GetNoUpdateResponse(),
811 -1,
812 false, // ping_only
813 ErrorCode::kSuccess,
814 metrics::CheckResult::kNoUpdateAvailable,
815 metrics::CheckReaction::kUnset,
816 metrics::DownloadErrorCode::kUnset,
817 &response,
818 nullptr));
819
820 string value;
821 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
822 EXPECT_EQ(fake_update_response_.cohort, value);
823
824 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
825 EXPECT_EQ(fake_update_response_.cohorthint, value);
826
827 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
828 EXPECT_EQ(fake_update_response_.cohortname, value);
829}
830
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700831TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700832 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700833
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700834 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700835 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700836
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700837 OmahaRequestParams params = request_params_;
838 fake_system_state_.set_request_params(&params);
839 OmahaRequestAction action(&fake_system_state_, nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700840 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800841 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700842 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -0700843 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700844 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700845 ActionProcessor processor;
846 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700847 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700848
Alex Deymo60ca1a72015-06-18 18:19:15 -0700849 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
850 loop.Run();
851 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700852 EXPECT_FALSE(processor.IsRunning());
853}
854
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700855TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700856 OmahaResponse response;
857 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700858 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700859 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700860 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700861 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700862 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800863 metrics::CheckResult::kParsingError,
864 metrics::CheckReaction::kUnset,
865 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700866 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700867 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -0700868 EXPECT_FALSE(response.update_exists);
869}
870
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700871TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700872 OmahaResponse response;
873 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700874 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -0700875 "",
876 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700877 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700878 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800879 metrics::CheckResult::kParsingError,
880 metrics::CheckReaction::kUnset,
881 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700882 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700883 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700884 EXPECT_FALSE(response.update_exists);
885}
886
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700887TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700888 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700889 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700890 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700891 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
892 "<daystart elapsed_seconds=\"100\"/>"
893 "<app appid=\"foo\" status=\"ok\">"
894 "<ping status=\"ok\"/>"
895 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700896 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700897 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700898 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800899 metrics::CheckResult::kParsingError,
900 metrics::CheckReaction::kUnset,
901 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700902 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700903 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700904 EXPECT_FALSE(response.update_exists);
905}
906
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700907TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700908 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700909 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700910 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700911 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
912 "<daystart elapsed_seconds=\"100\"/>"
913 "<app appid=\"foo\" status=\"ok\">"
914 "<ping status=\"ok\"/>"
915 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700916 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700917 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700918 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800919 metrics::CheckResult::kParsingError,
920 metrics::CheckReaction::kUnset,
921 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700922 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700923 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700924 EXPECT_FALSE(response.update_exists);
925}
926
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700927TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700928 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700929 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700930 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700931 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
932 "<daystart elapsed_seconds=\"100\"/>"
933 "<app appid=\"foo\" status=\"ok\">"
934 "<ping status=\"ok\"/>"
935 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700936 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700937 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700938 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800939 metrics::CheckResult::kParsingError,
940 metrics::CheckReaction::kUnset,
941 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700942 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700943 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700944 EXPECT_FALSE(response.update_exists);
945}
946
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700947TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700948 string input_response =
949 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
950 "<daystart elapsed_seconds=\"100\"/>"
951 "<app appid=\"xyz\" status=\"ok\">"
952 "<updatecheck status=\"ok\">"
953 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700954 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700955 "<packages><package hash=\"not-used\" name=\"f\" "
956 "size=\"587\"/></packages>"
957 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700958 "ChromeOSVersion=\"10.2.3.4\" "
959 "Prompt=\"false\" "
960 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800961 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700962 "sha256=\"lkq34j5345\" "
963 "needsadmin=\"true\" "
964 "/></actions></manifest></updatecheck></app></response>";
965 LOG(INFO) << "Input Response = " << input_response;
966
Darin Petkov6a5b3222010-07-13 14:55:28 -0700967 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700968 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700969 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700970 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700971 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700972 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800973 metrics::CheckResult::kUpdateAvailable,
974 metrics::CheckReaction::kUpdating,
975 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700976 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700977 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700978 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700979 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800980 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700981 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700982 EXPECT_EQ("lkq34j5345", response.hash);
983 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700984 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700985 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700986}
987
988namespace {
989class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
990 public:
991 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700992 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700993 }
Darin Petkov6a5b3222010-07-13 14:55:28 -0700994};
995
Alex Deymo60ca1a72015-06-18 18:19:15 -0700996void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700997 processor->StartProcessing();
998 CHECK(processor->IsRunning());
999 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001000}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001001} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001002
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001003TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001004 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001005 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001006
Alex Deymo60ca1a72015-06-18 18:19:15 -07001007 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001008 OmahaRequestAction action(&fake_system_state_, nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001009 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001010 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001011 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001012 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001013 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001014 ActionProcessor processor;
1015 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001016 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001017
Alex Deymo60ca1a72015-06-18 18:19:15 -07001018 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1019 loop.Run();
1020 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001021}
1022
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001023TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001024 string output;
1025 EXPECT_TRUE(XmlEncode("ab", &output));
1026 EXPECT_EQ("ab", output);
1027 EXPECT_TRUE(XmlEncode("a<b", &output));
1028 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001029 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1030 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001031 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1032 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001033 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001034 EXPECT_FALSE(XmlEncode("\xc2", &output));
1035 // Fail with invalid ASCII-7 chars.
1036 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1037}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001038
Alex Deymob0d74eb2015-03-30 17:59:17 -07001039TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1040 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1041 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1042}
1043
1044TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001045 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001046
1047 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001048 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001049 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001050 OmahaRequestParams::kOsVersion,
1051 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001052 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001053 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001054 "0.1.0.0",
1055 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001056 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001057 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001058 "ChromeOSFirmware.1.0",
1059 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001060 false, // delta okay
1061 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001062 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001063 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001064 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1065 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1066 fake_prefs_.SetString(kPrefsOmahaCohortName,
1067 JoinString(vector<string>(100, "My spoon is too big."),
1068 ' '));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001069 OmahaResponse response;
1070 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001071 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001072 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001073 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001074 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001075 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001076 metrics::CheckResult::kParsingError,
1077 metrics::CheckReaction::kUnset,
1078 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001079 &response,
1080 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001081 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001082 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001083 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1084 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1085 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1086 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1087 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1088 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1089 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1090 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1091 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1092 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1093 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1094 // Values from Prefs that are too big are removed from the XML instead of
1095 // encoded.
1096 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001097}
1098
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001099TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001100 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001101 fake_update_response_.deadline = "&lt;20110101";
1102 fake_update_response_.more_info_url = "testthe&lt;url";
1103 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001104 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001105 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001106 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001107 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001108 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001109 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001110 metrics::CheckResult::kUpdateAvailable,
1111 metrics::CheckReaction::kUpdating,
1112 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001113 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001114 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001115
1116 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001117 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001118 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001119}
1120
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001121TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001122 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001123 // overflows int32_t:
1124 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001125 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001126 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001127 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001128 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001129 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001130 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001131 metrics::CheckResult::kUpdateAvailable,
1132 metrics::CheckReaction::kUpdating,
1133 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001134 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001135 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001136
1137 EXPECT_EQ(response.size, 123123123123123ll);
1138}
1139
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001140TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001141 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001142 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001143 fake_system_state_.set_prefs(&prefs);
1144
Darin Petkov95508da2011-01-05 12:42:29 -08001145 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1146 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001147 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001148 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001149 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001150 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001151 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001152 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001153 metrics::CheckResult::kParsingError,
1154 metrics::CheckReaction::kUnset,
1155 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001156 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001157 &post_data));
1158 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001159 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001160 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001161 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001162 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001163 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001164 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1165 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001166 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1167 string::npos);
1168 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1169 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001170}
1171
Jay Srinivasan0a708742012-03-20 11:26:12 -07001172
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001173TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001174 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001175 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001176 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1177 "invalid xml>",
1178 &post_data);
1179 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001180 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001181 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001182 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001183 OmahaEvent::kTypeUpdateDownloadStarted,
1184 OmahaEvent::kResultSuccess);
1185 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001186 EXPECT_EQ(post_str.find("ping"), string::npos);
1187 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001188}
1189
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001190TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
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::kTypeDownloadComplete,
1194 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001195 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001196 "invalid xml>",
1197 &post_data);
1198 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001199 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001200 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001201 " <event eventtype=\"%d\" eventresult=\"%d\" "
1202 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001203 OmahaEvent::kTypeDownloadComplete,
1204 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001205 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001206 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001207 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001208}
1209
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001210TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001211 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001212 // Create a copy of the OmahaRequestParams to reuse it later.
1213 OmahaRequestParams params = request_params_;
1214 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001215 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001216 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001217 nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001218 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001219 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001220 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001221 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001222 EXPECT_FALSE(update_check_action.IsEvent());
1223
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001224 params = request_params_;
1225 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001226 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001227 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001228 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001229 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001230 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001231 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001232 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001233 EXPECT_TRUE(event_action.IsEvent());
1234}
1235
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001236TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001237 for (int i = 0; i < 2; i++) {
1238 bool delta_okay = i == 1;
1239 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001240 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001241 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001242 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001243 OmahaRequestParams::kOsVersion,
1244 "service_pack",
1245 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001246 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001247 "0.1.0.0",
1248 "en-US",
1249 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001250 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001251 "ChromeOSFirmware.1.0",
1252 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001253 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001254 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001255 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001256 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001257 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001258 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001259 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001260 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001261 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001262 metrics::CheckResult::kParsingError,
1263 metrics::CheckReaction::kUnset,
1264 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001265 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001266 &post_data));
1267 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001268 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001269 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1270 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001271 string::npos)
1272 << "i = " << i;
1273 }
1274}
1275
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001276TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001277 for (int i = 0; i < 2; i++) {
1278 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001279 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001280 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001281 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001282 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001283 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001284 OmahaRequestParams::kOsVersion,
1285 "service_pack",
1286 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001287 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001288 "0.1.0.0",
1289 "en-US",
1290 "unittest_track",
1291 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001292 "ChromeOSFirmware.1.0",
1293 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001294 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001295 interactive,
1296 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001297 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001298 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001299 "invalid xml>",
1300 -1,
1301 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001302 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001303 metrics::CheckResult::kParsingError,
1304 metrics::CheckReaction::kUnset,
1305 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001306 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001307 &post_data));
1308 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001309 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001310 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1311 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001312 string::npos)
1313 << "i = " << i;
1314 }
1315}
1316
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001317TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001318 OmahaEvent default_event;
1319 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1320 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001321 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001322
1323 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1324 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1325 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001326 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001327
1328 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1329 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001330 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001331 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1332 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001333 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001334}
1335
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001336void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001337 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001338 fake_system_state_.set_prefs(&prefs);
1339 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1340 .Times(AnyNumber());
1341 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1342 // Add a few hours to the day difference to test no rounding, etc.
1343 int64_t five_days_ago =
1344 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1345 int64_t six_days_ago =
1346 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1347 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1348 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1349 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1350 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1351 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1352 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001353 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001354 ASSERT_TRUE(
1355 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001356 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001357 -1,
1358 ping_only,
1359 ErrorCode::kSuccess,
1360 metrics::CheckResult::kUnset,
1361 metrics::CheckReaction::kUnset,
1362 metrics::DownloadErrorCode::kUnset,
1363 nullptr,
1364 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001365 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001366 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1367 string::npos);
1368 if (ping_only) {
1369 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1370 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1371 } else {
1372 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1373 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001374 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001375}
1376
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001377TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1378 PingTest(true /* ping_only */);
1379}
1380
1381TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1382 PingTest(false /* ping_only */);
1383}
1384
1385TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001386 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001387 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001388 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1389 .Times(AnyNumber());
1390 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001391 int64_t three_days_ago =
1392 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1393 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001394 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1395 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001396 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1397 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1398 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1399 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001400 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001401 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001402 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001403 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001404 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001405 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001406 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001407 metrics::CheckResult::kNoUpdateAvailable,
1408 metrics::CheckReaction::kUnset,
1409 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001410 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001411 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001412 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001413 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001414 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001415}
1416
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001417TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001418 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001419 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001420 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1421 .Times(AnyNumber());
1422 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001423 int64_t four_days_ago =
1424 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1425 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001426 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1427 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001428 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1429 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1430 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1431 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001432 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001433 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001434 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001435 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001436 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001437 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001438 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001439 metrics::CheckResult::kNoUpdateAvailable,
1440 metrics::CheckReaction::kUnset,
1441 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001442 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001443 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001444 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001445 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001446 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001447}
1448
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001449TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001450 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001451 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001452 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1453 .Times(AnyNumber());
1454 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001455 int64_t one_hour_ago =
1456 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001457 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1458 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001459 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1460 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1461 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1462 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001463 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1464 // send a ping.
1465 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1466 .WillOnce(Return(true));
1467 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1468 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001469 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001470 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001471 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001472 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001473 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001474 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001475 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001476 metrics::CheckResult::kNoUpdateAvailable,
1477 metrics::CheckReaction::kUnset,
1478 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001479 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001480 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001481 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001482 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001483}
1484
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001485TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001486 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001487 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001488 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001489 int64_t now = Time::Now().ToInternalValue();
1490 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1491 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1492 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1493 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1494 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1495 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001496 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001497 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001498 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001499 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001500 -1,
1501 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001502 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001503 metrics::CheckResult::kUnset,
1504 metrics::CheckReaction::kUnset,
1505 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001506 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001507 &post_data));
1508 EXPECT_EQ(post_data.size(), 0);
1509}
1510
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001511TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001512 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001513 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001514 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1515 .Times(AnyNumber());
1516 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001517 int64_t future =
1518 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001519 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1520 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001521 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1522 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1523 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1524 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1525 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1526 .WillOnce(Return(true));
1527 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1528 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001529 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001530 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001531 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001532 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1533 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001534 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001535 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001536 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001537 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001538 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001539 metrics::CheckResult::kNoUpdateAvailable,
1540 metrics::CheckReaction::kUnset,
1541 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001542 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001543 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001544 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001545 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001546}
1547
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001548TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001549 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001550 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001551 // may fail if it runs for longer than 5 seconds. It shouldn't run
1552 // that long though.
1553 int64_t midnight =
1554 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1555 int64_t midnight_slack =
1556 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001557 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001558 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001559 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1560 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001561 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1562 AllOf(Ge(midnight), Le(midnight_slack))))
1563 .WillOnce(Return(true));
1564 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1565 AllOf(Ge(midnight), Le(midnight_slack))))
1566 .WillOnce(Return(true));
1567 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001568 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001569 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1570 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001571 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001572 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001573 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001574 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001575 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001576 metrics::CheckResult::kNoUpdateAvailable,
1577 metrics::CheckReaction::kUnset,
1578 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001579 nullptr,
1580 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001581}
1582
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001583TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001584 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001585 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001586 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1587 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001588 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1589 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1590 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001591 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001592 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1593 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001594 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001595 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001596 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001597 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001598 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001599 metrics::CheckResult::kNoUpdateAvailable,
1600 metrics::CheckReaction::kUnset,
1601 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001602 nullptr,
1603 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001604}
1605
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001606TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001607 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001608 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001609 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1610 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001611 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1612 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1613 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001614 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001615 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1616 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001617 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001618 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001619 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001620 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001621 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001622 metrics::CheckResult::kNoUpdateAvailable,
1623 metrics::CheckReaction::kUnset,
1624 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001625 nullptr,
1626 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001627}
1628
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001629TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001630 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001631 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001632 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001633 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001634 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001635 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001636 metrics::CheckResult::kParsingError,
1637 metrics::CheckReaction::kUnset,
1638 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001639 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001640 &post_data));
1641 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001642 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001643 EXPECT_EQ(post_str.find("machineid="), string::npos);
1644 EXPECT_EQ(post_str.find("userid="), string::npos);
1645}
1646
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001647TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001648 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001649 const int http_error_code =
1650 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001651 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001652 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001653 "",
1654 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001655 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001656 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001657 metrics::CheckResult::kDownloadError,
1658 metrics::CheckReaction::kUnset,
1659 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001660 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001661 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001662 EXPECT_FALSE(response.update_exists);
1663}
1664
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001665TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001666 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001667 const int http_error_code =
1668 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001669 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001670 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001671 "",
1672 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001673 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001674 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001675 metrics::CheckResult::kDownloadError,
1676 metrics::CheckReaction::kUnset,
1677 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001678 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001679 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001680 EXPECT_FALSE(response.update_exists);
1681}
1682
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001683TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001684 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001685 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001686 params.set_wall_clock_based_wait_enabled(true);
1687 params.set_waiting_period(TimeDelta().FromDays(1));
1688 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001689
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001690 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001691 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001692 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001693 -1,
1694 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001695 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001696 metrics::CheckResult::kUpdateAvailable,
1697 metrics::CheckReaction::kDeferring,
1698 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001699 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001700 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001701
Ben Chan9abb7632014-08-07 00:10:53 -07001702 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001703 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001704 ASSERT_GT(timestamp, 0);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001705 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001706
1707 // Verify if we are interactive check we don't defer.
1708 params.set_interactive(true);
1709 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001710 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001711 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -07001712 -1,
1713 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001714 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001715 metrics::CheckResult::kUpdateAvailable,
1716 metrics::CheckReaction::kUpdating,
1717 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001718 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001719 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001720 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001721}
1722
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001723TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001724 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001725 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001726 params.set_wall_clock_based_wait_enabled(true);
1727 params.set_waiting_period(TimeDelta().FromDays(1));
1728 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001729
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001730 // Set the timestamp to a very old value such that it exceeds the
1731 // waiting period set above.
1732 Time t1;
1733 Time::FromString("1/1/2012", &t1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001734 ASSERT_TRUE(fake_prefs_.SetInt64(
1735 kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001736 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001737 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001738 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001739 -1,
1740 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001741 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001742 metrics::CheckResult::kUpdateAvailable,
1743 metrics::CheckReaction::kUpdating,
1744 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001745 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001746 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001747
1748 EXPECT_TRUE(response.update_exists);
1749
1750 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07001751 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001752 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001753 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1754}
1755
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001756TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001757 // Create a uniquely named test directory.
1758 string test_dir;
1759 ASSERT_TRUE(utils::MakeTempDirectory(
1760 "omaha_request_action-test-XXXXXX", &test_dir));
1761
1762 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1763 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001764 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001765 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001766 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001767 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001768 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001769 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001770 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1771 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1772 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1773 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001774 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001775 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
1776 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001777 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001778 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001779 params.Init("1.2.3.4", "", 0);
1780 EXPECT_EQ("canary-channel", params.current_channel());
1781 EXPECT_EQ("stable-channel", params.target_channel());
1782 EXPECT_TRUE(params.to_more_stable_channel());
1783 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001784 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001785 "invalid xml>",
1786 -1,
1787 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001788 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001789 metrics::CheckResult::kParsingError,
1790 metrics::CheckReaction::kUnset,
1791 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001792 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001793 &post_data));
1794 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001795 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001796 EXPECT_NE(string::npos, post_str.find(
1797 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1798 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1799 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001800
Alex Deymo10875d92014-11-10 21:52:57 -08001801 ASSERT_TRUE(test_utils::RecursiveUnlinkDir(test_dir));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001802}
1803
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001804TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001805 // Create a uniquely named test directory.
1806 string test_dir;
1807 ASSERT_TRUE(utils::MakeTempDirectory(
1808 "omaha_request_action-test-XXXXXX", &test_dir));
1809
1810 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1811 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001812 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001813 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001814 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001815 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001816 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001817 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001818 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1819 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1820 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1821 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001822 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001823 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001824 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001825 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001826 params.Init("5.6.7.8", "", 0);
1827 EXPECT_EQ("stable-channel", params.current_channel());
1828 EXPECT_EQ("canary-channel", params.target_channel());
1829 EXPECT_FALSE(params.to_more_stable_channel());
1830 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001831 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001832 "invalid xml>",
1833 -1,
1834 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001835 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001836 metrics::CheckResult::kParsingError,
1837 metrics::CheckReaction::kUnset,
1838 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001839 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001840 &post_data));
1841 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001842 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001843 EXPECT_NE(string::npos, post_str.find(
1844 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1845 "version=\"5.6.7.8\" "
1846 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001847 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001848}
1849
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001850// Checks that the initial ping with a=-1 r=-1 is not send when the device
1851// was powerwashed.
1852TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
1853 fake_prefs_.SetString(kPrefsPreviousVersion, "");
1854
1855 // Flag that the device was powerwashed in the past.
1856 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
1857
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001858 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001859 ASSERT_TRUE(
1860 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001861 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001862 -1,
1863 false, // ping_only
1864 ErrorCode::kSuccess,
1865 metrics::CheckResult::kNoUpdateAvailable,
1866 metrics::CheckReaction::kUnset,
1867 metrics::DownloadErrorCode::kUnset,
1868 nullptr,
1869 &post_data));
1870 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001871 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001872 EXPECT_EQ(string::npos, post_str.find("<ping"));
1873}
1874
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001875void OmahaRequestActionTest::P2PTest(
1876 bool initial_allow_p2p_for_downloading,
1877 bool initial_allow_p2p_for_sharing,
1878 bool omaha_disable_p2p_for_downloading,
1879 bool omaha_disable_p2p_for_sharing,
1880 bool payload_state_allow_p2p_attempt,
1881 bool expect_p2p_client_lookup,
1882 const string& p2p_client_result_url,
1883 bool expected_allow_p2p_for_downloading,
1884 bool expected_allow_p2p_for_sharing,
1885 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001886 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001887 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07001888 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
1889 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
1890 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07001891
1892 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001893 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07001894 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1895 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07001896 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
1897 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
1898 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
1899 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
1900 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
1901 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
1902 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
1903 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
1904 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
1905 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
1906
David Zeuthen8f191b22013-08-06 12:27:50 -07001907 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001908 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001909 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1910
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001911 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
1912 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07001913 .Times(expect_p2p_client_lookup ? 1 : 0);
1914
Alex Deymo8e18f932015-03-27 16:16:59 -07001915 fake_update_response_.disable_p2p_for_downloading =
1916 omaha_disable_p2p_for_downloading;
1917 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07001918 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001919 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001920 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07001921 -1,
1922 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001923 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001924 metrics::CheckResult::kUpdateAvailable,
1925 metrics::CheckReaction::kUpdating,
1926 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07001927 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001928 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07001929 EXPECT_TRUE(response.update_exists);
1930
Gilad Arnold74b5f552014-10-07 08:17:16 -07001931 EXPECT_EQ(omaha_disable_p2p_for_downloading,
1932 response.disable_p2p_for_downloading);
1933 EXPECT_EQ(omaha_disable_p2p_for_sharing,
1934 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07001935
Gilad Arnold74b5f552014-10-07 08:17:16 -07001936 EXPECT_EQ(expected_allow_p2p_for_downloading,
1937 actual_allow_p2p_for_downloading);
1938 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
1939 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07001940}
1941
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001942TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001943 P2PTest(true, // initial_allow_p2p_for_downloading
1944 true, // initial_allow_p2p_for_sharing
1945 false, // omaha_disable_p2p_for_downloading
1946 false, // omaha_disable_p2p_for_sharing
1947 true, // payload_state_allow_p2p_attempt
1948 true, // expect_p2p_client_lookup
1949 "http://1.3.5.7/p2p", // p2p_client_result_url
1950 true, // expected_allow_p2p_for_downloading
1951 true, // expected_allow_p2p_for_sharing
1952 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001953}
1954
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001955TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001956 P2PTest(true, // initial_allow_p2p_for_downloading
1957 true, // initial_allow_p2p_for_sharing
1958 false, // omaha_disable_p2p_for_downloading
1959 false, // omaha_disable_p2p_for_sharing
1960 true, // payload_state_allow_p2p_attempt
1961 true, // expect_p2p_client_lookup
1962 "", // p2p_client_result_url
1963 false, // expected_allow_p2p_for_downloading
1964 true, // expected_allow_p2p_for_sharing
1965 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001966}
1967
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001968TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001969 P2PTest(false, // initial_allow_p2p_for_downloading
1970 true, // initial_allow_p2p_for_sharing
1971 false, // omaha_disable_p2p_for_downloading
1972 false, // omaha_disable_p2p_for_sharing
1973 true, // payload_state_allow_p2p_attempt
1974 false, // expect_p2p_client_lookup
1975 "unset", // p2p_client_result_url
1976 false, // expected_allow_p2p_for_downloading
1977 true, // expected_allow_p2p_for_sharing
1978 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001979}
1980
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001981TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001982 P2PTest(true, // initial_allow_p2p_for_downloading
1983 true, // initial_allow_p2p_for_sharing
1984 true, // omaha_disable_p2p_for_downloading
1985 false, // omaha_disable_p2p_for_sharing
1986 true, // payload_state_allow_p2p_attempt
1987 false, // expect_p2p_client_lookup
1988 "unset", // p2p_client_result_url
1989 false, // expected_allow_p2p_for_downloading
1990 true, // expected_allow_p2p_for_sharing
1991 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001992}
1993
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001994TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001995 P2PTest(true, // initial_allow_p2p_for_downloading
1996 true, // initial_allow_p2p_for_sharing
1997 false, // omaha_disable_p2p_for_downloading
1998 true, // omaha_disable_p2p_for_sharing
1999 true, // payload_state_allow_p2p_attempt
2000 true, // expect_p2p_client_lookup
2001 "http://1.3.5.7/p2p", // p2p_client_result_url
2002 true, // expected_allow_p2p_for_downloading
2003 false, // expected_allow_p2p_for_sharing
2004 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002005}
2006
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002007TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002008 P2PTest(true, // initial_allow_p2p_for_downloading
2009 true, // initial_allow_p2p_for_sharing
2010 true, // omaha_disable_p2p_for_downloading
2011 true, // omaha_disable_p2p_for_sharing
2012 true, // payload_state_allow_p2p_attempt
2013 false, // expect_p2p_client_lookup
2014 "unset", // p2p_client_result_url
2015 false, // expected_allow_p2p_for_downloading
2016 false, // expected_allow_p2p_for_sharing
2017 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002018}
2019
Alex Deymof329b932014-10-30 01:37:48 -07002020bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2021 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002022 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002023 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002024 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002025 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002026 -1,
2027 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002028 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002029 metrics::CheckResult::kUpdateAvailable,
2030 metrics::CheckReaction::kUpdating,
2031 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002032 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002033 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002034}
2035
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002036TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002037 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002038
2039 // Check that we parse elapsed_days in the Omaha Response correctly.
2040 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002041 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2042 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002043 EXPECT_TRUE(response.update_exists);
2044 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002045 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002046 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002047 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002048 EXPECT_EQ(prefs_days, 42);
2049
2050 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002051 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002052 EXPECT_TRUE(response.update_exists);
2053 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002054 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002055 EXPECT_EQ(prefs_days, 42);
2056
2057 // Note that elapsed_days is not necessarily divisible by 7 so check
2058 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002059 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2060 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002061 EXPECT_TRUE(response.update_exists);
2062 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002063 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002064 EXPECT_EQ(prefs_days, 21);
2065
2066 // Check that we correctly handle elapsed_days not being included in
2067 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002068 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002069 EXPECT_TRUE(response.update_exists);
2070 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002071}
2072
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002073// If there is no prefs and OOBE is not complete, we should not
2074// report anything to Omaha.
2075TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
2076 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2077 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2078}
David Zeuthen639aa362014-02-03 16:23:44 -08002079
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002080// If OOBE is complete and happened on a valid date (e.g. after Jan
2081// 1 2007 0:00 PST), that date should be used and written to
2082// prefs. However, first try with an invalid date and check we do
2083// nothing.
2084TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2085 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2086 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2087 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2088 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2089}
David Zeuthen639aa362014-02-03 16:23:44 -08002090
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002091// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2092// should yield an InstallDate of 14.
2093TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2094 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2095 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2096 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2097 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002098
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002099 int64_t prefs_days;
2100 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2101 EXPECT_EQ(prefs_days, 14);
2102}
David Zeuthen639aa362014-02-03 16:23:44 -08002103
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002104// Now that we have a valid date in prefs, check that we keep using
2105// that even if OOBE date reports something else. The date Jan 30,
2106// 2007 0:00 PST should yield an InstallDate of 28... but since
2107// there's a prefs file, we should still get 14.
2108TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2109 // Set a valid date in the prefs first.
2110 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002111
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002112 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2113 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2114 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002115
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002116 int64_t prefs_days;
2117 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2118 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002119
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002120 // If we delete the prefs file, we should get 28 days.
2121 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2122 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2123 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2124 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002125}
2126
Darin Petkov6a5b3222010-07-13 14:55:28 -07002127} // namespace chromeos_update_engine