blob: 496143e59123fcc6e025c4d10efe874282c1a5d4 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov6a5b3222010-07-13 14:55:28 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_action.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Darin Petkov6a5b3222010-07-13 14:55:28 -070021#include <string>
22#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070023
Alex Deymo60ca1a72015-06-18 18:19:15 -070024#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070025#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070026#include <base/files/scoped_temp_dir.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070027#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_util.h>
29#include <base/strings/stringprintf.h>
30#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/bind_lambda.h>
Alex Deymoc1c17b42015-11-23 03:53:15 -030032#include <brillo/make_unique_ptr.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070033#include <brillo/message_loops/fake_message_loop.h>
34#include <brillo/message_loops/message_loop.h>
35#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070036#include <gtest/gtest.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070037
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/action_pipe.h"
39#include "update_engine/common/constants.h"
40#include "update_engine/common/fake_prefs.h"
41#include "update_engine/common/hash_calculator.h"
42#include "update_engine/common/mock_http_fetcher.h"
43#include "update_engine/common/platform_constants.h"
44#include "update_engine/common/prefs.h"
45#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030046#include "update_engine/fake_system_state.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080047#include "update_engine/metrics.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070048#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070049#include "update_engine/mock_payload_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070050#include "update_engine/omaha_request_params.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070051
Darin Petkov1cbd78f2010-07-29 12:38:34 -070052using base::Time;
53using base::TimeDelta;
Alex Deymo10875d92014-11-10 21:52:57 -080054using chromeos_update_engine::test_utils::System;
55using chromeos_update_engine::test_utils::WriteFileString;
Darin Petkov6a5b3222010-07-13 14:55:28 -070056using std::string;
57using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070059using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070060using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070061using testing::Ge;
62using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080063using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070064using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070065using testing::ReturnPointee;
66using testing::SaveArg;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070067using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070068using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070069
Alex Deymo8e18f932015-03-27 16:16:59 -070070namespace {
71
Alex Deymo85616652015-10-15 18:48:31 -070072const char kTestAppId[] = "test-app-id";
73
Alex Deymo8e18f932015-03-27 16:16:59 -070074// This is a helper struct to allow unit tests build an update response with the
75// values they care about.
76struct FakeUpdateResponse {
77 string GetNoUpdateResponse() const {
78 string entity_str;
79 if (include_entity)
80 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070081 return
82 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
83 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070084 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070085 "<app appid=\"" + app_id + "\" " +
86 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
87 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
88 " status=\"ok\">"
89 "<ping status=\"ok\"/>"
90 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070091 }
92
93 string GetUpdateResponse() const {
94 return
95 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
96 "protocol=\"3.0\">"
97 "<daystart elapsed_seconds=\"100\"" +
98 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
99 + "/>"
100 "<app appid=\"" + app_id + "\" " +
101 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
102 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
103 " status=\"ok\">"
104 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
105 "<urls><url codebase=\"" + codebase + "\"/></urls>"
106 "<manifest version=\"" + version + "\">"
107 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
108 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
109 "<actions><action event=\"postinstall\" "
110 "ChromeOSVersion=\"" + version + "\" "
111 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
112 "IsDelta=\"true\" "
113 "IsDeltaPayload=\"true\" "
114 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
115 "sha256=\"" + hash + "\" "
116 "needsadmin=\"" + needsadmin + "\" " +
117 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
118 (disable_p2p_for_downloading ?
119 "DisableP2PForDownloading=\"true\" " : "") +
120 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
121 "/></actions></manifest></updatecheck></app></response>";
122 }
123
124 // Return the payload URL, which is split in two fields in the XML response.
125 string GetPayloadUrl() {
126 return codebase + filename;
127 }
128
Alex Deymo85616652015-10-15 18:48:31 -0700129 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700130 string version = "1.2.3.4";
131 string more_info_url = "http://more/info";
132 string prompt = "true";
133 string codebase = "http://code/base/";
134 string filename = "file.signed";
135 string hash = "HASH1234=";
136 string needsadmin = "false";
137 int64_t size = 123;
138 string deadline = "";
139 string max_days_to_scatter = "7";
140 string elapsed_days = "42";
141
142 // P2P setting defaults to allowed.
143 bool disable_p2p_for_downloading = false;
144 bool disable_p2p_for_sharing = false;
145
146 // Omaha cohorts settings.
147 bool include_cohorts = false;
148 string cohort = "";
149 string cohorthint = "";
150 string cohortname = "";
151
152 // Whether to include the CrOS <!ENTITY> in the XML response.
153 bool include_entity = false;
154};
155
156} // namespace
157
Darin Petkov6a5b3222010-07-13 14:55:28 -0700158namespace chromeos_update_engine {
159
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700160class OmahaRequestActionTest : public ::testing::Test {
161 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800162 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700163 fake_system_state_.set_request_params(&request_params_);
164 fake_system_state_.set_prefs(&fake_prefs_);
165 }
166
167 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800168 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700169 // is used. |payload_state| may be null, in which case a local mock is used.
170 // |p2p_manager| may be null, in which case a local mock is used.
171 // |connection_manager| may be null, in which case a local mock is used.
172 // out_response may be null. If |fail_http_response_code| is non-negative,
173 // the transfer will fail with that code. |ping_only| is passed through to the
174 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
175 // post-data received by the mock HttpFetcher is returned.
176 //
177 // The |expected_check_result|, |expected_check_reaction| and
178 // |expected_error_code| parameters are for checking expectations
179 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
180 // UMA statistics. Use the appropriate ::kUnset value to specify that
181 // the given metric should not be reported.
182 bool TestUpdateCheck(OmahaRequestParams* request_params,
183 const string& http_response,
184 int fail_http_response_code,
185 bool ping_only,
186 ErrorCode expected_code,
187 metrics::CheckResult expected_check_result,
188 metrics::CheckReaction expected_check_reaction,
189 metrics::DownloadErrorCode expected_download_error_code,
190 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700191 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700192
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800193 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700194 // only a ping or also an updatecheck.
195 void PingTest(bool ping_only);
196
197 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700198 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700199 OmahaResponse *response);
200
201 // P2P test helper function.
202 void P2PTest(
203 bool initial_allow_p2p_for_downloading,
204 bool initial_allow_p2p_for_sharing,
205 bool omaha_disable_p2p_for_downloading,
206 bool omaha_disable_p2p_for_sharing,
207 bool payload_state_allow_p2p_attempt,
208 bool expect_p2p_client_lookup,
209 const string& p2p_client_result_url,
210 bool expected_allow_p2p_for_downloading,
211 bool expected_allow_p2p_for_sharing,
212 const string& expected_p2p_url);
213
214 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700215 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700216
217 // By default, all tests use these objects unless they replace them in the
218 // fake_system_state_.
219 OmahaRequestParams request_params_ = OmahaRequestParams{
220 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700221 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700222 OmahaRequestParams::kOsVersion,
223 "service_pack",
224 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700225 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700226 "0.1.0.0",
227 "en-US",
228 "unittest",
229 "OEM MODEL 09235 7471",
230 "ChromeOSFirmware.1.0",
231 "0X0A1",
232 false, // delta okay
233 false, // interactive
234 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700235 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700236
237 FakePrefs fake_prefs_;
238};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700239
240namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700241class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
242 public:
243 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700244 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800245 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700246 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800247 void ProcessingDone(const ActionProcessor* processor,
248 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700249 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700250 }
251
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800252 void ActionCompleted(ActionProcessor* processor,
253 AbstractAction* action,
254 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700255 // make sure actions always succeed
256 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700257 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700258 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700259 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260 }
David Zeuthena99981f2013-04-29 13:42:47 -0700261 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700263} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700264
265class OutputObjectCollectorAction;
266
267template<>
268class ActionTraits<OutputObjectCollectorAction> {
269 public:
270 // Does not take an object for input
271 typedef OmahaResponse InputObjectType;
272 // On success, puts the output path on output
273 typedef NoneType OutputObjectType;
274};
275
276class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
277 public:
278 OutputObjectCollectorAction() : has_input_object_(false) {}
279 void PerformAction() {
280 // copy input object
281 has_input_object_ = HasInputObject();
282 if (has_input_object_)
283 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700284 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700285 }
286 // Should never be called
287 void TerminateProcessing() {
288 CHECK(false);
289 }
290 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700291 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700292 return "OutputObjectCollectorAction";
293 }
Alex Deymof329b932014-10-30 01:37:48 -0700294 string Type() const { return StaticType(); }
Alex Vakulenko0057daa2016-01-23 16:22:50 -0800295 using InputObjectType =
296 ActionTraits<OutputObjectCollectorAction>::InputObjectType;
297 using OutputObjectType =
298 ActionTraits<OutputObjectCollectorAction>::OutputObjectType;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700299 bool has_input_object_;
300 OmahaResponse omaha_response_;
301};
302
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700303bool OmahaRequestActionTest::TestUpdateCheck(
304 OmahaRequestParams* request_params,
305 const string& http_response,
306 int fail_http_response_code,
307 bool ping_only,
308 ErrorCode expected_code,
309 metrics::CheckResult expected_check_result,
310 metrics::CheckReaction expected_check_reaction,
311 metrics::DownloadErrorCode expected_download_error_code,
312 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700313 brillo::Blob* out_post_data) {
314 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700315 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700316 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800317 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700318 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700319 if (fail_http_response_code >= 0) {
320 fetcher->FailTransfer(fail_http_response_code);
321 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700322 if (request_params)
323 fake_system_state_.set_request_params(request_params);
324 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700325 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300326 brillo::make_unique_ptr(fetcher),
Darin Petkov265f2902011-05-09 15:17:40 -0700327 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700328 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700329 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700330
Darin Petkov6a5b3222010-07-13 14:55:28 -0700331 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700332 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700333 processor.EnqueueAction(&action);
334
335 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700336 BondActions(&action, &collector_action);
337 processor.EnqueueAction(&collector_action);
338
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700339 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800340 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700341 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800342 SendEnumToUMA(metrics::kMetricCheckResult,
343 static_cast<int>(expected_check_result),
344 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
345 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700346 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800347 SendEnumToUMA(metrics::kMetricCheckReaction,
348 static_cast<int>(expected_check_reaction),
349 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
350 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700351 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700352 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
353 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800354 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
355 ? 0 : 1);
356
Alex Deymo60ca1a72015-06-18 18:19:15 -0700357 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
358 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
359 loop.Run();
360 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
361 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700362 if (collector_action.has_input_object_ && out_response)
363 *out_response = collector_action.omaha_response_;
364 if (out_post_data)
365 *out_post_data = fetcher->post_data();
366 return collector_action.has_input_object_;
367}
368
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700369// Tests Event requests -- they should always succeed. |out_post_data|
370// may be null; if non-null, the post-data received by the mock
371// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700372void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700373 OmahaEvent* event,
374 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700375 brillo::Blob* out_post_data) {
376 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700377 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700378 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800379 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700380 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700381 FakeSystemState fake_system_state;
382 fake_system_state.set_request_params(&params);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300383 OmahaRequestAction action(&fake_system_state,
384 event,
385 brillo::make_unique_ptr(fetcher),
386 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700387 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700388 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700389 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700390 processor.EnqueueAction(&action);
391
Alex Deymo60ca1a72015-06-18 18:19:15 -0700392 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
393 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700394 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700395
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700396 if (out_post_data)
397 *out_post_data = fetcher->post_data();
398}
399
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700400TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400401 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700402 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400403 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700404 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700405 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400406 -1,
407 false, // ping_only
408 ErrorCode::kOmahaRequestXMLHasEntityDecl,
409 metrics::CheckResult::kParsingError,
410 metrics::CheckReaction::kUnset,
411 metrics::DownloadErrorCode::kUnset,
412 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700413 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400414 EXPECT_FALSE(response.update_exists);
415}
416
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700417TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700418 OmahaResponse response;
419 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700420 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700421 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700422 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700423 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700424 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800425 metrics::CheckResult::kNoUpdateAvailable,
426 metrics::CheckReaction::kUnset,
427 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700428 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700429 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700430 EXPECT_FALSE(response.update_exists);
431}
432
Alex Deymo8e18f932015-03-27 16:16:59 -0700433// Test that all the values in the response are parsed in a normal update
434// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700435TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700436 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700437 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700438 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700439 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700440 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700441 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700442 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700443 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800444 metrics::CheckResult::kUpdateAvailable,
445 metrics::CheckReaction::kUpdating,
446 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700447 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700448 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700449 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700450 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700451 EXPECT_EQ(fake_update_response_.version, response.version);
452 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
453 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
454 EXPECT_EQ(fake_update_response_.hash, response.hash);
455 EXPECT_EQ(fake_update_response_.size, response.size);
456 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
457 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
458 // Omaha cohort attribets are not set in the response, so they should not be
459 // persisted.
460 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
461 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
462 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700463}
464
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700465TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700466 OmahaResponse response;
467 // Set up a connection manager that doesn't allow a valid update over
468 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700469 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700470 fake_system_state_.set_connection_manager(&mock_cm);
471
Alex Deymo30534502015-07-20 15:06:33 -0700472 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
473 .WillRepeatedly(
474 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet),
475 SetArgumentPointee<1>(NetworkTethering::kUnknown),
476 Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700477 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _))
Chris Sosa77f79e82014-06-02 18:16:24 -0700478 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700479
480 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700481 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700482 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700483 -1,
484 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700485 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700486 metrics::CheckResult::kUpdateAvailable,
487 metrics::CheckReaction::kIgnored,
488 metrics::DownloadErrorCode::kUnset,
489 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700490 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700491 EXPECT_FALSE(response.update_exists);
492}
493
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700494TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700495 string rollback_version = "1234.0.0";
496 OmahaResponse response;
497
498 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700499 fake_system_state_.set_payload_state(&mock_payload_state);
500
Chris Sosa77f79e82014-06-02 18:16:24 -0700501 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
502 .WillRepeatedly(Return(rollback_version));
503
Alex Deymo8e18f932015-03-27 16:16:59 -0700504 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700505 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700506 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700507 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700508 -1,
509 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700510 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700511 metrics::CheckResult::kUpdateAvailable,
512 metrics::CheckReaction::kIgnored,
513 metrics::DownloadErrorCode::kUnset,
514 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700515 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700516 EXPECT_FALSE(response.update_exists);
517}
518
Kevin Cernekee2494e282016-03-29 18:03:53 -0700519// Verify that update checks called during OOBE will only try to download
520// an update if the response includes a non-empty deadline field.
521TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
522 OmahaResponse response;
523
524 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
525 ASSERT_FALSE(
526 TestUpdateCheck(nullptr, // request_params
527 fake_update_response_.GetUpdateResponse(),
528 -1,
529 false, // ping_only
530 ErrorCode::kNonCriticalUpdateInOOBE,
531 metrics::CheckResult::kUnset,
532 metrics::CheckReaction::kUnset,
533 metrics::DownloadErrorCode::kUnset,
534 &response,
535 nullptr));
536 EXPECT_FALSE(response.update_exists);
537
538 fake_update_response_.deadline = "20101020";
539 ASSERT_TRUE(
540 TestUpdateCheck(nullptr, // request_params
541 fake_update_response_.GetUpdateResponse(),
542 -1,
543 false, // ping_only
544 ErrorCode::kSuccess,
545 metrics::CheckResult::kUpdateAvailable,
546 metrics::CheckReaction::kUpdating,
547 metrics::DownloadErrorCode::kUnset,
548 &response,
549 nullptr));
550 EXPECT_TRUE(response.update_exists);
551}
552
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700553TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700554 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700555 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700556 params.set_wall_clock_based_wait_enabled(true);
557 params.set_update_check_count_wait_enabled(false);
558 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700559
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700560 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700561 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700562 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700563 -1,
564 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700565 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800566 metrics::CheckResult::kUpdateAvailable,
567 metrics::CheckReaction::kDeferring,
568 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700569 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700570 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700571 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700572
573 // Verify if we are interactive check we don't defer.
574 params.set_interactive(true);
575 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700576 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700577 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700578 -1,
579 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700580 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800581 metrics::CheckResult::kUpdateAvailable,
582 metrics::CheckReaction::kUpdating,
583 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700584 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700585 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700586 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700587}
588
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700589TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700590 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700591 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700592 params.set_wall_clock_based_wait_enabled(false);
593 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700594
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700595 params.set_update_check_count_wait_enabled(true);
596 params.set_min_update_checks_needed(1);
597 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700598
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700599 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700600 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700601 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700602 -1,
603 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700604 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800605 metrics::CheckResult::kUpdateAvailable,
606 metrics::CheckReaction::kUpdating,
607 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700608 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700609 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700610 EXPECT_TRUE(response.update_exists);
611}
612
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700613TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700614 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700615 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700616 params.set_wall_clock_based_wait_enabled(true);
617 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700618
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700619 params.set_update_check_count_wait_enabled(true);
620 params.set_min_update_checks_needed(1);
621 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700622
Alex Deymo8e18f932015-03-27 16:16:59 -0700623 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700624 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700625 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700626 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700627 -1,
628 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700629 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800630 metrics::CheckResult::kUpdateAvailable,
631 metrics::CheckReaction::kUpdating,
632 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700633 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700634 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700635 EXPECT_TRUE(response.update_exists);
636}
637
638
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700639TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700640 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700641 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700642 params.set_wall_clock_based_wait_enabled(true);
643 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700644
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700645 params.set_update_check_count_wait_enabled(true);
646 params.set_min_update_checks_needed(0);
647 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700648
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700649 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700650 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700651 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700652 -1,
653 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700654 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800655 metrics::CheckResult::kUpdateAvailable,
656 metrics::CheckReaction::kUpdating,
657 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700658 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700659 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700660
Ben Chan9abb7632014-08-07 00:10:53 -0700661 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700662 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700663 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700664 EXPECT_TRUE(response.update_exists);
665}
666
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700667TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700668 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700669 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700670 params.set_wall_clock_based_wait_enabled(true);
671 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700672
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700673 params.set_update_check_count_wait_enabled(true);
674 params.set_min_update_checks_needed(1);
675 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700676
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700677 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700678 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700679 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700680 -1,
681 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700682 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800683 metrics::CheckResult::kUpdateAvailable,
684 metrics::CheckReaction::kDeferring,
685 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700686 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700687 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700688
Ben Chan9abb7632014-08-07 00:10:53 -0700689 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700690 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700691 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700692 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700693
694 // Verify if we are interactive check we don't defer.
695 params.set_interactive(true);
696 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700697 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700698 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700699 -1,
700 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700701 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800702 metrics::CheckResult::kUpdateAvailable,
703 metrics::CheckReaction::kUpdating,
704 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700705 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700706 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700707 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700708}
709
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700710TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700711 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700712 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700713 params.set_wall_clock_based_wait_enabled(true);
714 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700715
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700716 params.set_update_check_count_wait_enabled(true);
717 params.set_min_update_checks_needed(1);
718 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700719
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700720 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700721
722 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700723 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700724 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700725 -1,
726 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700727 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800728 metrics::CheckResult::kUpdateAvailable,
729 metrics::CheckReaction::kDeferring,
730 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700731 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700732 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700733
Ben Chan9abb7632014-08-07 00:10:53 -0700734 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700735 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700736 // count remains the same, as the decrementing happens in update_attempter
737 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700738 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700739 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700740
741 // Verify if we are interactive check we don't defer.
742 params.set_interactive(true);
743 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700744 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700745 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700746 -1,
747 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700748 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800749 metrics::CheckResult::kUpdateAvailable,
750 metrics::CheckReaction::kUpdating,
751 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700752 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700753 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700754 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700755}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700756
Alex Deymo8e18f932015-03-27 16:16:59 -0700757TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
758 OmahaResponse response;
759 OmahaRequestParams params = request_params_;
760 fake_update_response_.include_cohorts = true;
761 fake_update_response_.cohort = "s/154454/8479665";
762 fake_update_response_.cohorthint = "please-put-me-on-beta";
763 fake_update_response_.cohortname = "stable";
764
765 ASSERT_TRUE(TestUpdateCheck(&params,
766 fake_update_response_.GetUpdateResponse(),
767 -1,
768 false, // ping_only
769 ErrorCode::kSuccess,
770 metrics::CheckResult::kUpdateAvailable,
771 metrics::CheckReaction::kUpdating,
772 metrics::DownloadErrorCode::kUnset,
773 &response,
774 nullptr));
775
776 string value;
777 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
778 EXPECT_EQ(fake_update_response_.cohort, value);
779
780 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
781 EXPECT_EQ(fake_update_response_.cohorthint, value);
782
783 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
784 EXPECT_EQ(fake_update_response_.cohortname, value);
785}
786
787TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
788 OmahaResponse response;
789 OmahaRequestParams params = request_params_;
790 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
791 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
792 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
793 fake_update_response_.include_cohorts = true;
794 fake_update_response_.cohort = "s/154454/8479665";
795 fake_update_response_.cohorthint = "please-put-me-on-beta";
796 fake_update_response_.cohortname = "";
797
798 ASSERT_TRUE(TestUpdateCheck(&params,
799 fake_update_response_.GetUpdateResponse(),
800 -1,
801 false, // ping_only
802 ErrorCode::kSuccess,
803 metrics::CheckResult::kUpdateAvailable,
804 metrics::CheckReaction::kUpdating,
805 metrics::DownloadErrorCode::kUnset,
806 &response,
807 nullptr));
808
809 string value;
810 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
811 EXPECT_EQ(fake_update_response_.cohort, value);
812
813 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
814 EXPECT_EQ(fake_update_response_.cohorthint, value);
815
816 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
817}
818
819TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
820 OmahaResponse response;
821 OmahaRequestParams params = request_params_;
822 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
823
824 ASSERT_TRUE(TestUpdateCheck(&params,
825 fake_update_response_.GetUpdateResponse(),
826 -1,
827 false, // ping_only
828 ErrorCode::kSuccess,
829 metrics::CheckResult::kUpdateAvailable,
830 metrics::CheckReaction::kUpdating,
831 metrics::DownloadErrorCode::kUnset,
832 &response,
833 nullptr));
834
835 string value;
836 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
837 EXPECT_EQ("old_value", value);
838
839 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
840 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
841}
842
Alex Deymo00d79ac2015-06-29 15:41:49 -0700843TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
844 OmahaResponse response;
845 OmahaRequestParams params = request_params_;
846 fake_update_response_.include_cohorts = true;
847 fake_update_response_.cohort = "s/154454/8479665";
848 fake_update_response_.cohorthint = "please-put-me-on-beta";
849 fake_update_response_.cohortname = "stable";
850
851 ASSERT_TRUE(TestUpdateCheck(&params,
852 fake_update_response_.GetNoUpdateResponse(),
853 -1,
854 false, // ping_only
855 ErrorCode::kSuccess,
856 metrics::CheckResult::kNoUpdateAvailable,
857 metrics::CheckReaction::kUnset,
858 metrics::DownloadErrorCode::kUnset,
859 &response,
860 nullptr));
861
862 string value;
863 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
864 EXPECT_EQ(fake_update_response_.cohort, value);
865
866 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
867 EXPECT_EQ(fake_update_response_.cohorthint, value);
868
869 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
870 EXPECT_EQ(fake_update_response_.cohortname, value);
871}
872
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700873TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700874 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700875
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700876 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700877 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700878
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700879 OmahaRequestParams params = request_params_;
880 fake_system_state_.set_request_params(&params);
881 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300882 brillo::make_unique_ptr(
883 new MockHttpFetcher(http_response.data(),
884 http_response.size(),
885 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -0700886 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700887 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700888 ActionProcessor processor;
889 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700890 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700891
Alex Deymo60ca1a72015-06-18 18:19:15 -0700892 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
893 loop.Run();
894 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700895 EXPECT_FALSE(processor.IsRunning());
896}
897
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700898TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700899 OmahaResponse response;
900 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700901 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700902 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700903 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700904 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700905 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800906 metrics::CheckResult::kParsingError,
907 metrics::CheckReaction::kUnset,
908 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700909 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700910 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -0700911 EXPECT_FALSE(response.update_exists);
912}
913
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700914TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700915 OmahaResponse response;
916 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700917 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -0700918 "",
919 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700920 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700921 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800922 metrics::CheckResult::kParsingError,
923 metrics::CheckReaction::kUnset,
924 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700925 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700926 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700927 EXPECT_FALSE(response.update_exists);
928}
929
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700930TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700931 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700932 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700933 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700934 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
935 "<daystart elapsed_seconds=\"100\"/>"
936 "<app appid=\"foo\" status=\"ok\">"
937 "<ping status=\"ok\"/>"
938 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700939 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700940 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700941 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800942 metrics::CheckResult::kParsingError,
943 metrics::CheckReaction::kUnset,
944 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700945 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700946 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700947 EXPECT_FALSE(response.update_exists);
948}
949
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700950TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700951 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700952 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700953 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700954 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
955 "<daystart elapsed_seconds=\"100\"/>"
956 "<app appid=\"foo\" status=\"ok\">"
957 "<ping status=\"ok\"/>"
958 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700959 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700960 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700961 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800962 metrics::CheckResult::kParsingError,
963 metrics::CheckReaction::kUnset,
964 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700965 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700966 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700967 EXPECT_FALSE(response.update_exists);
968}
969
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700970TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700971 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700972 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700973 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700974 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
975 "<daystart elapsed_seconds=\"100\"/>"
976 "<app appid=\"foo\" status=\"ok\">"
977 "<ping status=\"ok\"/>"
978 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700979 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700980 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700981 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800982 metrics::CheckResult::kParsingError,
983 metrics::CheckReaction::kUnset,
984 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700985 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700986 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700987 EXPECT_FALSE(response.update_exists);
988}
989
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700990TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700991 string input_response =
992 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
993 "<daystart elapsed_seconds=\"100\"/>"
994 "<app appid=\"xyz\" status=\"ok\">"
995 "<updatecheck status=\"ok\">"
996 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700997 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700998 "<packages><package hash=\"not-used\" name=\"f\" "
999 "size=\"587\"/></packages>"
1000 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001001 "ChromeOSVersion=\"10.2.3.4\" "
1002 "Prompt=\"false\" "
1003 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001004 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001005 "sha256=\"lkq34j5345\" "
1006 "needsadmin=\"true\" "
1007 "/></actions></manifest></updatecheck></app></response>";
1008 LOG(INFO) << "Input Response = " << input_response;
1009
Darin Petkov6a5b3222010-07-13 14:55:28 -07001010 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001011 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001012 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -07001013 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001014 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001015 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001016 metrics::CheckResult::kUpdateAvailable,
1017 metrics::CheckReaction::kUpdating,
1018 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001019 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001020 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001021 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001022 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001023 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001024 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001025 EXPECT_EQ("lkq34j5345", response.hash);
1026 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001027 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001028 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001029}
1030
1031namespace {
1032class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1033 public:
1034 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001035 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001036 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001037};
1038
Alex Deymo60ca1a72015-06-18 18:19:15 -07001039void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001040 processor->StartProcessing();
1041 CHECK(processor->IsRunning());
1042 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001043}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001044} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001045
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001046TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001047 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001048 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001049
Alex Deymo60ca1a72015-06-18 18:19:15 -07001050 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001051 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001052 brillo::make_unique_ptr(
1053 new MockHttpFetcher(http_response.data(),
1054 http_response.size(),
1055 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001056 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001057 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001058 ActionProcessor processor;
1059 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001060 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001061
Alex Deymo60ca1a72015-06-18 18:19:15 -07001062 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1063 loop.Run();
1064 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001065}
1066
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001067TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001068 string output;
1069 EXPECT_TRUE(XmlEncode("ab", &output));
1070 EXPECT_EQ("ab", output);
1071 EXPECT_TRUE(XmlEncode("a<b", &output));
1072 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001073 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1074 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001075 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1076 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001077 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001078 EXPECT_FALSE(XmlEncode("\xc2", &output));
1079 // Fail with invalid ASCII-7 chars.
1080 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1081}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001082
Alex Deymob0d74eb2015-03-30 17:59:17 -07001083TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1084 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1085 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1086}
1087
1088TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001089 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001090
1091 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001092 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001093 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001094 OmahaRequestParams::kOsVersion,
1095 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001096 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001097 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001098 "0.1.0.0",
1099 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001100 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001101 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001102 "ChromeOSFirmware.1.0",
1103 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001104 false, // delta okay
1105 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001106 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001107 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001108 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1109 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1110 fake_prefs_.SetString(kPrefsOmahaCohortName,
Alex Vakulenko0103c362016-01-20 07:56:15 -08001111 base::JoinString(
1112 vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001113 OmahaResponse response;
1114 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001115 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001116 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001117 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001118 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001119 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001120 metrics::CheckResult::kParsingError,
1121 metrics::CheckReaction::kUnset,
1122 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001123 &response,
1124 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001125 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001126 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001127 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1128 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1129 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1130 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1131 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1132 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1133 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1134 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1135 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1136 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1137 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1138 // Values from Prefs that are too big are removed from the XML instead of
1139 // encoded.
1140 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001141}
1142
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001143TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001144 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001145 fake_update_response_.deadline = "&lt;20110101";
1146 fake_update_response_.more_info_url = "testthe&lt;url";
1147 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001148 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001149 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001150 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001151 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001152 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001153 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001154 metrics::CheckResult::kUpdateAvailable,
1155 metrics::CheckReaction::kUpdating,
1156 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001157 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001158 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001159
1160 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001161 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001162 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001163}
1164
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001165TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001166 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001167 // overflows int32_t:
1168 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001169 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001170 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001171 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001172 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001173 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001174 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001175 metrics::CheckResult::kUpdateAvailable,
1176 metrics::CheckReaction::kUpdating,
1177 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001178 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001179 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001180
1181 EXPECT_EQ(response.size, 123123123123123ll);
1182}
1183
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001184TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001185 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001186 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001187 fake_system_state_.set_prefs(&prefs);
1188
Darin Petkov95508da2011-01-05 12:42:29 -08001189 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1190 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001191 // An existing but empty previous version means that we didn't reboot to a new
1192 // update, therefore, no need to update the previous version.
1193 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001194 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001195 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001196 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001197 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001198 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001199 metrics::CheckResult::kParsingError,
1200 metrics::CheckReaction::kUnset,
1201 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001202 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001203 &post_data));
1204 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001205 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001206 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001207 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001208 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001209 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001210 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1211 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001212 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1213 string::npos);
1214 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1215 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001216 // No <event> tag should be sent if we didn't reboot to an update.
1217 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001218}
1219
Jay Srinivasan0a708742012-03-20 11:26:12 -07001220
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001221TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001222 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001223 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001224 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1225 "invalid xml>",
1226 &post_data);
1227 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001228 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001229 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001230 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001231 OmahaEvent::kTypeUpdateDownloadStarted,
1232 OmahaEvent::kResultSuccess);
1233 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001234 EXPECT_EQ(post_str.find("ping"), string::npos);
1235 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001236}
1237
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001238TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001239 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001240 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001241 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1242 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001243 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001244 "invalid xml>",
1245 &post_data);
1246 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001247 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001248 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001249 " <event eventtype=\"%d\" eventresult=\"%d\" "
1250 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001251 OmahaEvent::kTypeDownloadComplete,
1252 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001253 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001254 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001255 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001256}
1257
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001258TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001259 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001260 // Create a copy of the OmahaRequestParams to reuse it later.
1261 OmahaRequestParams params = request_params_;
1262 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001263 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001264 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001265 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001266 brillo::make_unique_ptr(
1267 new MockHttpFetcher(http_response.data(),
1268 http_response.size(),
1269 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001270 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001271 EXPECT_FALSE(update_check_action.IsEvent());
1272
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001273 params = request_params_;
1274 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001275 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001276 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001277 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001278 brillo::make_unique_ptr(
1279 new MockHttpFetcher(http_response.data(),
1280 http_response.size(),
1281 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001282 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001283 EXPECT_TRUE(event_action.IsEvent());
1284}
1285
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001286TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001287 for (int i = 0; i < 2; i++) {
1288 bool delta_okay = i == 1;
1289 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001290 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001291 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001292 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001293 OmahaRequestParams::kOsVersion,
1294 "service_pack",
1295 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001296 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001297 "0.1.0.0",
1298 "en-US",
1299 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001300 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001301 "ChromeOSFirmware.1.0",
1302 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001303 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001304 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001305 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001306 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001307 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001308 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001309 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001310 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001311 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001312 metrics::CheckResult::kParsingError,
1313 metrics::CheckReaction::kUnset,
1314 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001315 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001316 &post_data));
1317 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001318 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001319 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1320 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001321 string::npos)
1322 << "i = " << i;
1323 }
1324}
1325
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001326TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001327 for (int i = 0; i < 2; i++) {
1328 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001329 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001330 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001331 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001332 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001333 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001334 OmahaRequestParams::kOsVersion,
1335 "service_pack",
1336 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001337 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001338 "0.1.0.0",
1339 "en-US",
1340 "unittest_track",
1341 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001342 "ChromeOSFirmware.1.0",
1343 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001344 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001345 interactive,
1346 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001347 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001348 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001349 "invalid xml>",
1350 -1,
1351 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001352 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001353 metrics::CheckResult::kParsingError,
1354 metrics::CheckReaction::kUnset,
1355 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001356 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001357 &post_data));
1358 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001359 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001360 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1361 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001362 string::npos)
1363 << "i = " << i;
1364 }
1365}
1366
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001367TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001368 OmahaEvent default_event;
1369 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1370 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001371 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001372
1373 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1374 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1375 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001376 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001377
1378 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1379 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001380 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001381 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1382 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001383 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001384}
1385
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001386void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001387 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001388 fake_system_state_.set_prefs(&prefs);
1389 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1390 .Times(AnyNumber());
1391 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1392 // Add a few hours to the day difference to test no rounding, etc.
1393 int64_t five_days_ago =
1394 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1395 int64_t six_days_ago =
1396 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1397 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1398 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1399 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1400 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1401 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1402 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001403 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001404 ASSERT_TRUE(
1405 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001406 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001407 -1,
1408 ping_only,
1409 ErrorCode::kSuccess,
1410 metrics::CheckResult::kUnset,
1411 metrics::CheckReaction::kUnset,
1412 metrics::DownloadErrorCode::kUnset,
1413 nullptr,
1414 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001415 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001416 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1417 string::npos);
1418 if (ping_only) {
1419 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1420 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1421 } else {
1422 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1423 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001424 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001425}
1426
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001427TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1428 PingTest(true /* ping_only */);
1429}
1430
1431TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1432 PingTest(false /* ping_only */);
1433}
1434
1435TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001436 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001437 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001438 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1439 .Times(AnyNumber());
1440 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001441 int64_t three_days_ago =
1442 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1443 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001444 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1445 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001446 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1447 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1448 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1449 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001450 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001451 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001452 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001453 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001454 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001455 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001456 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001457 metrics::CheckResult::kNoUpdateAvailable,
1458 metrics::CheckReaction::kUnset,
1459 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001460 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001461 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001462 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001463 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001464 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001465}
1466
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001467TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001468 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001469 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001470 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1471 .Times(AnyNumber());
1472 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001473 int64_t four_days_ago =
1474 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1475 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001476 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1477 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001478 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1479 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1480 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1481 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001482 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001483 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001484 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001485 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001486 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001487 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001488 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001489 metrics::CheckResult::kNoUpdateAvailable,
1490 metrics::CheckReaction::kUnset,
1491 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001492 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001493 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001494 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001495 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001496 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001497}
1498
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001499TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001500 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001501 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001502 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1503 .Times(AnyNumber());
1504 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001505 int64_t one_hour_ago =
1506 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001507 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1508 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001509 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1510 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1511 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1512 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001513 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1514 // send a ping.
1515 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1516 .WillOnce(Return(true));
1517 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1518 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001519 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001520 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001521 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001522 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001523 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001524 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001525 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001526 metrics::CheckResult::kNoUpdateAvailable,
1527 metrics::CheckReaction::kUnset,
1528 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001529 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001530 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001531 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001532 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001533}
1534
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001535TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001536 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001537 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001538 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001539 int64_t now = Time::Now().ToInternalValue();
1540 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1541 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1542 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1543 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1544 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1545 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001546 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001547 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001548 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001549 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001550 -1,
1551 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001552 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001553 metrics::CheckResult::kUnset,
1554 metrics::CheckReaction::kUnset,
1555 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001556 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001557 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08001558 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07001559}
1560
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001561TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001562 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001563 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001564 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1565 .Times(AnyNumber());
1566 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001567 int64_t future =
1568 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001569 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1570 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001571 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1572 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1573 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1574 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1575 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1576 .WillOnce(Return(true));
1577 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1578 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001579 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001580 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001581 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001582 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1583 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001584 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001585 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001586 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001587 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001588 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001589 metrics::CheckResult::kNoUpdateAvailable,
1590 metrics::CheckReaction::kUnset,
1591 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001592 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001593 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001594 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001595 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001596}
1597
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001598TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001599 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001600 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001601 // may fail if it runs for longer than 5 seconds. It shouldn't run
1602 // that long though.
1603 int64_t midnight =
1604 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1605 int64_t midnight_slack =
1606 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
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,
1612 AllOf(Ge(midnight), Le(midnight_slack))))
1613 .WillOnce(Return(true));
1614 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1615 AllOf(Ge(midnight), Le(midnight_slack))))
1616 .WillOnce(Return(true));
1617 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001618 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001619 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1620 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001621 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001622 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001623 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001624 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001625 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001626 metrics::CheckResult::kNoUpdateAvailable,
1627 metrics::CheckReaction::kUnset,
1628 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001629 nullptr,
1630 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001631}
1632
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001633TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001634 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001635 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001636 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1637 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001638 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1639 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1640 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001641 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001642 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1643 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001644 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001645 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001646 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001647 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001648 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001649 metrics::CheckResult::kNoUpdateAvailable,
1650 metrics::CheckReaction::kUnset,
1651 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001652 nullptr,
1653 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001654}
1655
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001656TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001657 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001658 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001659 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1660 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001661 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1662 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1663 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001664 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001665 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1666 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001667 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001668 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001669 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001670 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001671 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001672 metrics::CheckResult::kNoUpdateAvailable,
1673 metrics::CheckReaction::kUnset,
1674 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001675 nullptr,
1676 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001677}
1678
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001679TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001680 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001681 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001682 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001683 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001684 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001685 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001686 metrics::CheckResult::kParsingError,
1687 metrics::CheckReaction::kUnset,
1688 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001689 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001690 &post_data));
1691 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001692 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001693 EXPECT_EQ(post_str.find("machineid="), string::npos);
1694 EXPECT_EQ(post_str.find("userid="), string::npos);
1695}
1696
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001697TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001698 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001699 const int http_error_code =
1700 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001701 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001702 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001703 "",
1704 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001705 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001706 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001707 metrics::CheckResult::kDownloadError,
1708 metrics::CheckReaction::kUnset,
1709 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001710 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001711 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001712 EXPECT_FALSE(response.update_exists);
1713}
1714
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001715TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001716 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001717 const int http_error_code =
1718 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001719 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001720 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001721 "",
1722 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001723 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001724 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001725 metrics::CheckResult::kDownloadError,
1726 metrics::CheckReaction::kUnset,
1727 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001728 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001729 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001730 EXPECT_FALSE(response.update_exists);
1731}
1732
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001733TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001734 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001735 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001736 params.set_wall_clock_based_wait_enabled(true);
1737 params.set_waiting_period(TimeDelta().FromDays(1));
1738 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001739
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001740 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001741 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001742 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001743 -1,
1744 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001745 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001746 metrics::CheckResult::kUpdateAvailable,
1747 metrics::CheckReaction::kDeferring,
1748 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001749 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001750 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001751
Ben Chan9abb7632014-08-07 00:10:53 -07001752 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001753 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001754 ASSERT_GT(timestamp, 0);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001755 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001756
1757 // Verify if we are interactive check we don't defer.
1758 params.set_interactive(true);
1759 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001760 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001761 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -07001762 -1,
1763 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001764 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001765 metrics::CheckResult::kUpdateAvailable,
1766 metrics::CheckReaction::kUpdating,
1767 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001768 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001769 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001770 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001771}
1772
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001773TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001774 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001775 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001776 params.set_wall_clock_based_wait_enabled(true);
1777 params.set_waiting_period(TimeDelta().FromDays(1));
1778 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001779
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001780 // Set the timestamp to a very old value such that it exceeds the
1781 // waiting period set above.
1782 Time t1;
1783 Time::FromString("1/1/2012", &t1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001784 ASSERT_TRUE(fake_prefs_.SetInt64(
1785 kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001786 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001787 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001788 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001789 -1,
1790 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001791 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001792 metrics::CheckResult::kUpdateAvailable,
1793 metrics::CheckReaction::kUpdating,
1794 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001795 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001796 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001797
1798 EXPECT_TRUE(response.update_exists);
1799
1800 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07001801 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001802 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001803 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1804}
1805
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001806TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001807 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07001808 base::ScopedTempDir tempdir;
1809 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001810
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001811 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07001812 OmahaRequestParams params(&fake_system_state_);
1813 params.set_root(tempdir.path().value());
1814 params.set_app_id("{22222222-2222-2222-2222-222222222222}");
1815 params.set_app_version("1.2.3.4");
1816 params.set_current_channel("canary-channel");
1817 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
1818 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001819 EXPECT_TRUE(params.to_more_stable_channel());
1820 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001821 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001822 "invalid xml>",
1823 -1,
1824 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001825 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001826 metrics::CheckResult::kParsingError,
1827 metrics::CheckReaction::kUnset,
1828 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001829 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001830 &post_data));
1831 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001832 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001833 EXPECT_NE(string::npos, post_str.find(
1834 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1835 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1836 "track=\"stable-channel\" from_track=\"canary-channel\" "));
1837}
1838
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001839TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001840 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07001841 base::ScopedTempDir tempdir;
1842 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001843
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001844 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07001845 OmahaRequestParams params(&fake_system_state_);
1846 params.set_root(tempdir.path().value());
1847 params.set_app_id("{11111111-1111-1111-1111-111111111111}");
1848 params.set_app_version("5.6.7.8");
1849 params.set_current_channel("stable-channel");
1850 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
1851 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001852 EXPECT_FALSE(params.to_more_stable_channel());
1853 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001854 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001855 "invalid xml>",
1856 -1,
1857 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001858 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001859 metrics::CheckResult::kParsingError,
1860 metrics::CheckReaction::kUnset,
1861 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001862 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001863 &post_data));
1864 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001865 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001866 EXPECT_NE(string::npos, post_str.find(
1867 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1868 "version=\"5.6.7.8\" "
1869 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001870 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001871}
1872
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001873// Checks that the initial ping with a=-1 r=-1 is not send when the device
1874// was powerwashed.
1875TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
1876 fake_prefs_.SetString(kPrefsPreviousVersion, "");
1877
1878 // Flag that the device was powerwashed in the past.
1879 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
1880
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001881 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001882 ASSERT_TRUE(
1883 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001884 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001885 -1,
1886 false, // ping_only
1887 ErrorCode::kSuccess,
1888 metrics::CheckResult::kNoUpdateAvailable,
1889 metrics::CheckReaction::kUnset,
1890 metrics::DownloadErrorCode::kUnset,
1891 nullptr,
1892 &post_data));
1893 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001894 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001895 EXPECT_EQ(string::npos, post_str.find("<ping"));
1896}
1897
Alex Deymo9fded1e2015-11-05 12:31:19 -08001898// Checks that the event 54 is sent on a reboot to a new update.
1899TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
1900 // Flag that the device was updated in a previous boot.
1901 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
1902
1903 brillo::Blob post_data;
1904 ASSERT_TRUE(
1905 TestUpdateCheck(nullptr, // request_params
1906 fake_update_response_.GetNoUpdateResponse(),
1907 -1,
1908 false, // ping_only
1909 ErrorCode::kSuccess,
1910 metrics::CheckResult::kNoUpdateAvailable,
1911 metrics::CheckReaction::kUnset,
1912 metrics::DownloadErrorCode::kUnset,
1913 nullptr,
1914 &post_data));
1915 string post_str(post_data.begin(), post_data.end());
1916
1917 // An event 54 is included and has the right version.
1918 EXPECT_NE(string::npos,
1919 post_str.find(base::StringPrintf(
1920 "<event eventtype=\"%d\"",
1921 OmahaEvent::kTypeRebootedAfterUpdate)));
1922 EXPECT_NE(string::npos,
1923 post_str.find("previousversion=\"1.2.3.4\"></event>"));
1924
1925 // The previous version flag should have been removed.
1926 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
1927 string prev_version;
1928 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
1929 EXPECT_TRUE(prev_version.empty());
1930}
1931
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001932void OmahaRequestActionTest::P2PTest(
1933 bool initial_allow_p2p_for_downloading,
1934 bool initial_allow_p2p_for_sharing,
1935 bool omaha_disable_p2p_for_downloading,
1936 bool omaha_disable_p2p_for_sharing,
1937 bool payload_state_allow_p2p_attempt,
1938 bool expect_p2p_client_lookup,
1939 const string& p2p_client_result_url,
1940 bool expected_allow_p2p_for_downloading,
1941 bool expected_allow_p2p_for_sharing,
1942 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001943 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001944 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07001945 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
1946 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
1947 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07001948
1949 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001950 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07001951 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1952 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07001953 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
1954 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
1955 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
1956 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
1957 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
1958 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
1959 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
1960 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
1961 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
1962 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
1963
David Zeuthen8f191b22013-08-06 12:27:50 -07001964 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001965 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001966 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1967
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001968 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
1969 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07001970 .Times(expect_p2p_client_lookup ? 1 : 0);
1971
Alex Deymo8e18f932015-03-27 16:16:59 -07001972 fake_update_response_.disable_p2p_for_downloading =
1973 omaha_disable_p2p_for_downloading;
1974 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07001975 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001976 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001977 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07001978 -1,
1979 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001980 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001981 metrics::CheckResult::kUpdateAvailable,
1982 metrics::CheckReaction::kUpdating,
1983 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07001984 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001985 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07001986 EXPECT_TRUE(response.update_exists);
1987
Gilad Arnold74b5f552014-10-07 08:17:16 -07001988 EXPECT_EQ(omaha_disable_p2p_for_downloading,
1989 response.disable_p2p_for_downloading);
1990 EXPECT_EQ(omaha_disable_p2p_for_sharing,
1991 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07001992
Gilad Arnold74b5f552014-10-07 08:17:16 -07001993 EXPECT_EQ(expected_allow_p2p_for_downloading,
1994 actual_allow_p2p_for_downloading);
1995 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
1996 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07001997}
1998
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001999TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002000 P2PTest(true, // initial_allow_p2p_for_downloading
2001 true, // initial_allow_p2p_for_sharing
2002 false, // omaha_disable_p2p_for_downloading
2003 false, // omaha_disable_p2p_for_sharing
2004 true, // payload_state_allow_p2p_attempt
2005 true, // expect_p2p_client_lookup
2006 "http://1.3.5.7/p2p", // p2p_client_result_url
2007 true, // expected_allow_p2p_for_downloading
2008 true, // expected_allow_p2p_for_sharing
2009 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002010}
2011
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002012TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002013 P2PTest(true, // initial_allow_p2p_for_downloading
2014 true, // initial_allow_p2p_for_sharing
2015 false, // omaha_disable_p2p_for_downloading
2016 false, // omaha_disable_p2p_for_sharing
2017 true, // payload_state_allow_p2p_attempt
2018 true, // expect_p2p_client_lookup
2019 "", // p2p_client_result_url
2020 false, // expected_allow_p2p_for_downloading
2021 true, // expected_allow_p2p_for_sharing
2022 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002023}
2024
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002025TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002026 P2PTest(false, // initial_allow_p2p_for_downloading
2027 true, // initial_allow_p2p_for_sharing
2028 false, // omaha_disable_p2p_for_downloading
2029 false, // omaha_disable_p2p_for_sharing
2030 true, // payload_state_allow_p2p_attempt
2031 false, // expect_p2p_client_lookup
2032 "unset", // p2p_client_result_url
2033 false, // expected_allow_p2p_for_downloading
2034 true, // expected_allow_p2p_for_sharing
2035 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002036}
2037
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002038TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002039 P2PTest(true, // initial_allow_p2p_for_downloading
2040 true, // initial_allow_p2p_for_sharing
2041 true, // omaha_disable_p2p_for_downloading
2042 false, // omaha_disable_p2p_for_sharing
2043 true, // payload_state_allow_p2p_attempt
2044 false, // expect_p2p_client_lookup
2045 "unset", // p2p_client_result_url
2046 false, // expected_allow_p2p_for_downloading
2047 true, // expected_allow_p2p_for_sharing
2048 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002049}
2050
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002051TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002052 P2PTest(true, // initial_allow_p2p_for_downloading
2053 true, // initial_allow_p2p_for_sharing
2054 false, // omaha_disable_p2p_for_downloading
2055 true, // omaha_disable_p2p_for_sharing
2056 true, // payload_state_allow_p2p_attempt
2057 true, // expect_p2p_client_lookup
2058 "http://1.3.5.7/p2p", // p2p_client_result_url
2059 true, // expected_allow_p2p_for_downloading
2060 false, // expected_allow_p2p_for_sharing
2061 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002062}
2063
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002064TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002065 P2PTest(true, // initial_allow_p2p_for_downloading
2066 true, // initial_allow_p2p_for_sharing
2067 true, // omaha_disable_p2p_for_downloading
2068 true, // omaha_disable_p2p_for_sharing
2069 true, // payload_state_allow_p2p_attempt
2070 false, // expect_p2p_client_lookup
2071 "unset", // p2p_client_result_url
2072 false, // expected_allow_p2p_for_downloading
2073 false, // expected_allow_p2p_for_sharing
2074 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002075}
2076
Alex Deymof329b932014-10-30 01:37:48 -07002077bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2078 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002079 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002080 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002081 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002082 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002083 -1,
2084 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002085 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002086 metrics::CheckResult::kUpdateAvailable,
2087 metrics::CheckReaction::kUpdating,
2088 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002089 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002090 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002091}
2092
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002093TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002094 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002095
Kevin Cernekee2494e282016-03-29 18:03:53 -07002096 // Simulate a successful update check that happens during OOBE. The
2097 // deadline in the response is needed to force the update attempt to
2098 // occur; responses without a deadline seen during OOBE will normally
2099 // return ErrorCode::kNonCriticalUpdateInOOBE.
2100 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2101 fake_update_response_.deadline = "20101020";
2102
David Zeuthen639aa362014-02-03 16:23:44 -08002103 // Check that we parse elapsed_days in the Omaha Response correctly.
2104 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002105 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2106 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002107 EXPECT_TRUE(response.update_exists);
2108 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002109 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002110 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002111 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002112 EXPECT_EQ(prefs_days, 42);
2113
2114 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002115 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002116 EXPECT_TRUE(response.update_exists);
2117 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002118 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002119 EXPECT_EQ(prefs_days, 42);
2120
2121 // Note that elapsed_days is not necessarily divisible by 7 so check
2122 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002123 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2124 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002125 EXPECT_TRUE(response.update_exists);
2126 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002127 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002128 EXPECT_EQ(prefs_days, 21);
2129
2130 // Check that we correctly handle elapsed_days not being included in
2131 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002132 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002133 EXPECT_TRUE(response.update_exists);
2134 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002135}
2136
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002137// If there is no prefs and OOBE is not complete, we should not
2138// report anything to Omaha.
2139TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002140 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002141 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2142 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2143}
David Zeuthen639aa362014-02-03 16:23:44 -08002144
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002145// If OOBE is complete and happened on a valid date (e.g. after Jan
2146// 1 2007 0:00 PST), that date should be used and written to
2147// prefs. However, first try with an invalid date and check we do
2148// nothing.
2149TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2150 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2151 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2152 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2153 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2154}
David Zeuthen639aa362014-02-03 16:23:44 -08002155
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002156// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2157// should yield an InstallDate of 14.
2158TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2159 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2160 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2161 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2162 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002163
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002164 int64_t prefs_days;
2165 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2166 EXPECT_EQ(prefs_days, 14);
2167}
David Zeuthen639aa362014-02-03 16:23:44 -08002168
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002169// Now that we have a valid date in prefs, check that we keep using
2170// that even if OOBE date reports something else. The date Jan 30,
2171// 2007 0:00 PST should yield an InstallDate of 28... but since
2172// there's a prefs file, we should still get 14.
2173TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2174 // Set a valid date in the prefs first.
2175 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002176
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002177 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2178 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2179 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002180
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002181 int64_t prefs_days;
2182 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2183 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002184
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002185 // If we delete the prefs file, we should get 28 days.
2186 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2187 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2188 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2189 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002190}
2191
Darin Petkov6a5b3222010-07-13 14:55:28 -07002192} // namespace chromeos_update_engine