blob: f4291e92be621eecb501f9ec18ee943e9f2ff492 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov6a5b3222010-07-13 14:55:28 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_action.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Darin Petkov6a5b3222010-07-13 14:55:28 -070021#include <string>
22#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070023
Alex Deymo60ca1a72015-06-18 18:19:15 -070024#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070025#include <base/files/file_util.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070026#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070027#include <base/strings/string_util.h>
28#include <base/strings/stringprintf.h>
29#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/bind_lambda.h>
31#include <brillo/message_loops/fake_message_loop.h>
32#include <brillo/message_loops/message_loop.h>
33#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070034#include <gtest/gtest.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070035
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/action_pipe.h"
37#include "update_engine/common/constants.h"
38#include "update_engine/common/fake_prefs.h"
39#include "update_engine/common/hash_calculator.h"
40#include "update_engine/common/mock_http_fetcher.h"
41#include "update_engine/common/platform_constants.h"
42#include "update_engine/common/prefs.h"
43#include "update_engine/common/test_utils.h"
44#include "update_engine/common/utils.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080045#include "update_engine/metrics.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070046#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070047#include "update_engine/mock_payload_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070048#include "update_engine/omaha_request_params.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070049
Darin Petkov1cbd78f2010-07-29 12:38:34 -070050using base::Time;
51using base::TimeDelta;
Alex Deymo10875d92014-11-10 21:52:57 -080052using chromeos_update_engine::test_utils::System;
53using chromeos_update_engine::test_utils::WriteFileString;
Darin Petkov6a5b3222010-07-13 14:55:28 -070054using std::string;
55using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070056using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070057using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070058using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070059using testing::Ge;
60using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080061using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070062using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070063using testing::ReturnPointee;
64using testing::SaveArg;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070066using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070067
Alex Deymo8e18f932015-03-27 16:16:59 -070068namespace {
69
Alex Deymo85616652015-10-15 18:48:31 -070070const char kTestAppId[] = "test-app-id";
71
Alex Deymo8e18f932015-03-27 16:16:59 -070072// This is a helper struct to allow unit tests build an update response with the
73// values they care about.
74struct FakeUpdateResponse {
75 string GetNoUpdateResponse() const {
76 string entity_str;
77 if (include_entity)
78 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070079 return
80 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
81 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070082 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070083 "<app appid=\"" + app_id + "\" " +
84 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
85 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
86 " status=\"ok\">"
87 "<ping status=\"ok\"/>"
88 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070089 }
90
91 string GetUpdateResponse() const {
92 return
93 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
94 "protocol=\"3.0\">"
95 "<daystart elapsed_seconds=\"100\"" +
96 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
97 + "/>"
98 "<app appid=\"" + app_id + "\" " +
99 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
100 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
101 " status=\"ok\">"
102 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
103 "<urls><url codebase=\"" + codebase + "\"/></urls>"
104 "<manifest version=\"" + version + "\">"
105 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
106 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
107 "<actions><action event=\"postinstall\" "
108 "ChromeOSVersion=\"" + version + "\" "
109 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
110 "IsDelta=\"true\" "
111 "IsDeltaPayload=\"true\" "
112 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
113 "sha256=\"" + hash + "\" "
114 "needsadmin=\"" + needsadmin + "\" " +
115 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
116 (disable_p2p_for_downloading ?
117 "DisableP2PForDownloading=\"true\" " : "") +
118 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
119 "/></actions></manifest></updatecheck></app></response>";
120 }
121
122 // Return the payload URL, which is split in two fields in the XML response.
123 string GetPayloadUrl() {
124 return codebase + filename;
125 }
126
Alex Deymo85616652015-10-15 18:48:31 -0700127 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700128 string version = "1.2.3.4";
129 string more_info_url = "http://more/info";
130 string prompt = "true";
131 string codebase = "http://code/base/";
132 string filename = "file.signed";
133 string hash = "HASH1234=";
134 string needsadmin = "false";
135 int64_t size = 123;
136 string deadline = "";
137 string max_days_to_scatter = "7";
138 string elapsed_days = "42";
139
140 // P2P setting defaults to allowed.
141 bool disable_p2p_for_downloading = false;
142 bool disable_p2p_for_sharing = false;
143
144 // Omaha cohorts settings.
145 bool include_cohorts = false;
146 string cohort = "";
147 string cohorthint = "";
148 string cohortname = "";
149
150 // Whether to include the CrOS <!ENTITY> in the XML response.
151 bool include_entity = false;
152};
153
154} // namespace
155
Darin Petkov6a5b3222010-07-13 14:55:28 -0700156namespace chromeos_update_engine {
157
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700158class OmahaRequestActionTest : public ::testing::Test {
159 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800160 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700161 fake_system_state_.set_request_params(&request_params_);
162 fake_system_state_.set_prefs(&fake_prefs_);
163 }
164
165 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800166 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700167 // is used. |payload_state| may be null, in which case a local mock is used.
168 // |p2p_manager| may be null, in which case a local mock is used.
169 // |connection_manager| may be null, in which case a local mock is used.
170 // out_response may be null. If |fail_http_response_code| is non-negative,
171 // the transfer will fail with that code. |ping_only| is passed through to the
172 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
173 // post-data received by the mock HttpFetcher is returned.
174 //
175 // The |expected_check_result|, |expected_check_reaction| and
176 // |expected_error_code| parameters are for checking expectations
177 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
178 // UMA statistics. Use the appropriate ::kUnset value to specify that
179 // the given metric should not be reported.
180 bool TestUpdateCheck(OmahaRequestParams* request_params,
181 const string& http_response,
182 int fail_http_response_code,
183 bool ping_only,
184 ErrorCode expected_code,
185 metrics::CheckResult expected_check_result,
186 metrics::CheckReaction expected_check_reaction,
187 metrics::DownloadErrorCode expected_download_error_code,
188 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700189 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700190
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800191 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700192 // only a ping or also an updatecheck.
193 void PingTest(bool ping_only);
194
195 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700196 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700197 OmahaResponse *response);
198
199 // P2P test helper function.
200 void P2PTest(
201 bool initial_allow_p2p_for_downloading,
202 bool initial_allow_p2p_for_sharing,
203 bool omaha_disable_p2p_for_downloading,
204 bool omaha_disable_p2p_for_sharing,
205 bool payload_state_allow_p2p_attempt,
206 bool expect_p2p_client_lookup,
207 const string& p2p_client_result_url,
208 bool expected_allow_p2p_for_downloading,
209 bool expected_allow_p2p_for_sharing,
210 const string& expected_p2p_url);
211
212 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700213 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700214
215 // By default, all tests use these objects unless they replace them in the
216 // fake_system_state_.
217 OmahaRequestParams request_params_ = OmahaRequestParams{
218 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700219 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700220 OmahaRequestParams::kOsVersion,
221 "service_pack",
222 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700223 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700224 "0.1.0.0",
225 "en-US",
226 "unittest",
227 "OEM MODEL 09235 7471",
228 "ChromeOSFirmware.1.0",
229 "0X0A1",
230 false, // delta okay
231 false, // interactive
232 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700233 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700234
235 FakePrefs fake_prefs_;
236};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700237
238namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700239class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
240 public:
241 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700242 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800243 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700244 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800245 void ProcessingDone(const ActionProcessor* processor,
246 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700247 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700248 }
249
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800250 void ActionCompleted(ActionProcessor* processor,
251 AbstractAction* action,
252 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700253 // make sure actions always succeed
254 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700255 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700256 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700257 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700258 }
David Zeuthena99981f2013-04-29 13:42:47 -0700259 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700261} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262
263class OutputObjectCollectorAction;
264
265template<>
266class ActionTraits<OutputObjectCollectorAction> {
267 public:
268 // Does not take an object for input
269 typedef OmahaResponse InputObjectType;
270 // On success, puts the output path on output
271 typedef NoneType OutputObjectType;
272};
273
274class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
275 public:
276 OutputObjectCollectorAction() : has_input_object_(false) {}
277 void PerformAction() {
278 // copy input object
279 has_input_object_ = HasInputObject();
280 if (has_input_object_)
281 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700282 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700283 }
284 // Should never be called
285 void TerminateProcessing() {
286 CHECK(false);
287 }
288 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700289 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700290 return "OutputObjectCollectorAction";
291 }
Alex Deymof329b932014-10-30 01:37:48 -0700292 string Type() const { return StaticType(); }
Darin Petkov6a5b3222010-07-13 14:55:28 -0700293 bool has_input_object_;
294 OmahaResponse omaha_response_;
295};
296
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700297bool OmahaRequestActionTest::TestUpdateCheck(
298 OmahaRequestParams* request_params,
299 const string& http_response,
300 int fail_http_response_code,
301 bool ping_only,
302 ErrorCode expected_code,
303 metrics::CheckResult expected_check_result,
304 metrics::CheckReaction expected_check_reaction,
305 metrics::DownloadErrorCode expected_download_error_code,
306 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700307 brillo::Blob* out_post_data) {
308 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700309 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700310 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800311 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700312 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700313 if (fail_http_response_code >= 0) {
314 fetcher->FailTransfer(fail_http_response_code);
315 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700316 if (request_params)
317 fake_system_state_.set_request_params(request_params);
318 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700319 nullptr,
Thieu Le116fda32011-04-19 11:01:54 -0700320 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700321 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700322 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700323 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700324
Darin Petkov6a5b3222010-07-13 14:55:28 -0700325 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700326 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700327 processor.EnqueueAction(&action);
328
329 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700330 BondActions(&action, &collector_action);
331 processor.EnqueueAction(&collector_action);
332
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700333 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800334 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700335 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800336 SendEnumToUMA(metrics::kMetricCheckResult,
337 static_cast<int>(expected_check_result),
338 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
339 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700340 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800341 SendEnumToUMA(metrics::kMetricCheckReaction,
342 static_cast<int>(expected_check_reaction),
343 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
344 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700345 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700346 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
347 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800348 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
349 ? 0 : 1);
350
Alex Deymo60ca1a72015-06-18 18:19:15 -0700351 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
352 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
353 loop.Run();
354 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
355 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700356 if (collector_action.has_input_object_ && out_response)
357 *out_response = collector_action.omaha_response_;
358 if (out_post_data)
359 *out_post_data = fetcher->post_data();
360 return collector_action.has_input_object_;
361}
362
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700363// Tests Event requests -- they should always succeed. |out_post_data|
364// may be null; if non-null, the post-data received by the mock
365// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700366void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700367 OmahaEvent* event,
368 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700369 brillo::Blob* out_post_data) {
370 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700371 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700372 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800373 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700374 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700375 FakeSystemState fake_system_state;
376 fake_system_state.set_request_params(&params);
377 OmahaRequestAction action(&fake_system_state, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700378 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700379 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700380 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700381 processor.EnqueueAction(&action);
382
Alex Deymo60ca1a72015-06-18 18:19:15 -0700383 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
384 loop.Run();
385
386 // This test should schedule a callback to notify the crash reporter if
387 // the passed event is an error.
388 EXPECT_EQ(event->result == OmahaEvent::kResultError, loop.PendingTasks());
389
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700390 if (out_post_data)
391 *out_post_data = fetcher->post_data();
392}
393
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700394TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400395 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700396 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400397 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700398 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700399 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400400 -1,
401 false, // ping_only
402 ErrorCode::kOmahaRequestXMLHasEntityDecl,
403 metrics::CheckResult::kParsingError,
404 metrics::CheckReaction::kUnset,
405 metrics::DownloadErrorCode::kUnset,
406 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700407 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400408 EXPECT_FALSE(response.update_exists);
409}
410
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700411TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700412 OmahaResponse response;
413 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700414 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700415 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700416 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700417 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700418 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800419 metrics::CheckResult::kNoUpdateAvailable,
420 metrics::CheckReaction::kUnset,
421 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700422 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700423 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700424 EXPECT_FALSE(response.update_exists);
425}
426
Alex Deymo8e18f932015-03-27 16:16:59 -0700427// Test that all the values in the response are parsed in a normal update
428// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700429TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700430 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700431 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700432 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700433 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700434 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700435 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700436 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700437 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800438 metrics::CheckResult::kUpdateAvailable,
439 metrics::CheckReaction::kUpdating,
440 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700441 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700442 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700443 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700444 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700445 EXPECT_EQ(fake_update_response_.version, response.version);
446 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
447 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
448 EXPECT_EQ(fake_update_response_.hash, response.hash);
449 EXPECT_EQ(fake_update_response_.size, response.size);
450 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
451 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
452 // Omaha cohort attribets are not set in the response, so they should not be
453 // persisted.
454 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
455 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
456 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700457}
458
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700459TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700460 OmahaResponse response;
461 // Set up a connection manager that doesn't allow a valid update over
462 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700463 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700464 fake_system_state_.set_connection_manager(&mock_cm);
465
Alex Deymo30534502015-07-20 15:06:33 -0700466 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
467 .WillRepeatedly(
468 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet),
469 SetArgumentPointee<1>(NetworkTethering::kUnknown),
470 Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700471 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _))
Chris Sosa77f79e82014-06-02 18:16:24 -0700472 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700473
474 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700475 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700476 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700477 -1,
478 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700479 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700480 metrics::CheckResult::kUpdateAvailable,
481 metrics::CheckReaction::kIgnored,
482 metrics::DownloadErrorCode::kUnset,
483 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700484 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700485 EXPECT_FALSE(response.update_exists);
486}
487
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700488TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700489 string rollback_version = "1234.0.0";
490 OmahaResponse response;
491
492 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700493 fake_system_state_.set_payload_state(&mock_payload_state);
494
Chris Sosa77f79e82014-06-02 18:16:24 -0700495 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
496 .WillRepeatedly(Return(rollback_version));
497
Alex Deymo8e18f932015-03-27 16:16:59 -0700498 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700499 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700500 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700501 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700502 -1,
503 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700504 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700505 metrics::CheckResult::kUpdateAvailable,
506 metrics::CheckReaction::kIgnored,
507 metrics::DownloadErrorCode::kUnset,
508 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700509 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700510 EXPECT_FALSE(response.update_exists);
511}
512
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700513TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700514 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700515 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700516 params.set_wall_clock_based_wait_enabled(true);
517 params.set_update_check_count_wait_enabled(false);
518 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700519
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700520 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700521 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700522 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700523 -1,
524 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700525 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800526 metrics::CheckResult::kUpdateAvailable,
527 metrics::CheckReaction::kDeferring,
528 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700529 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700530 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700531 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700532
533 // Verify if we are interactive check we don't defer.
534 params.set_interactive(true);
535 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700536 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700537 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700538 -1,
539 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700540 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800541 metrics::CheckResult::kUpdateAvailable,
542 metrics::CheckReaction::kUpdating,
543 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700544 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700545 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700546 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700547}
548
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700549TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700550 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700551 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700552 params.set_wall_clock_based_wait_enabled(false);
553 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700554
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700555 params.set_update_check_count_wait_enabled(true);
556 params.set_min_update_checks_needed(1);
557 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700558
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700559 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700560 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700561 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700562 -1,
563 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700564 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800565 metrics::CheckResult::kUpdateAvailable,
566 metrics::CheckReaction::kUpdating,
567 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700568 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700569 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700570 EXPECT_TRUE(response.update_exists);
571}
572
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700573TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700574 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700575 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700576 params.set_wall_clock_based_wait_enabled(true);
577 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700578
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700579 params.set_update_check_count_wait_enabled(true);
580 params.set_min_update_checks_needed(1);
581 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700582
Alex Deymo8e18f932015-03-27 16:16:59 -0700583 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700584 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700585 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700586 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700587 -1,
588 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700589 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800590 metrics::CheckResult::kUpdateAvailable,
591 metrics::CheckReaction::kUpdating,
592 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700593 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700594 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700595 EXPECT_TRUE(response.update_exists);
596}
597
598
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700599TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700600 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700601 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700602 params.set_wall_clock_based_wait_enabled(true);
603 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700604
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700605 params.set_update_check_count_wait_enabled(true);
606 params.set_min_update_checks_needed(0);
607 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700608
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700609 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700610 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700611 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700612 -1,
613 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700614 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800615 metrics::CheckResult::kUpdateAvailable,
616 metrics::CheckReaction::kUpdating,
617 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700618 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700619 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700620
Ben Chan9abb7632014-08-07 00:10:53 -0700621 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700622 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700623 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700624 EXPECT_TRUE(response.update_exists);
625}
626
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700627TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700628 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700629 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700630 params.set_wall_clock_based_wait_enabled(true);
631 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700632
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700633 params.set_update_check_count_wait_enabled(true);
634 params.set_min_update_checks_needed(1);
635 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700636
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700637 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700638 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700639 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700640 -1,
641 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700642 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800643 metrics::CheckResult::kUpdateAvailable,
644 metrics::CheckReaction::kDeferring,
645 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700646 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700647 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700648
Ben Chan9abb7632014-08-07 00:10:53 -0700649 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700650 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700651 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700652 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700653
654 // Verify if we are interactive check we don't defer.
655 params.set_interactive(true);
656 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700657 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700658 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700659 -1,
660 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700661 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800662 metrics::CheckResult::kUpdateAvailable,
663 metrics::CheckReaction::kUpdating,
664 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700665 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700666 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700667 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700668}
669
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700670TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700671 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700672 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700673 params.set_wall_clock_based_wait_enabled(true);
674 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700675
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700676 params.set_update_check_count_wait_enabled(true);
677 params.set_min_update_checks_needed(1);
678 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700679
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700680 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700681
682 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700683 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700684 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700685 -1,
686 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700687 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800688 metrics::CheckResult::kUpdateAvailable,
689 metrics::CheckReaction::kDeferring,
690 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700691 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700692 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700693
Ben Chan9abb7632014-08-07 00:10:53 -0700694 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700695 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700696 // count remains the same, as the decrementing happens in update_attempter
697 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700698 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700699 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700700
701 // Verify if we are interactive check we don't defer.
702 params.set_interactive(true);
703 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700704 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700705 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700706 -1,
707 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700708 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800709 metrics::CheckResult::kUpdateAvailable,
710 metrics::CheckReaction::kUpdating,
711 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700712 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700713 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700714 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700715}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700716
Alex Deymo8e18f932015-03-27 16:16:59 -0700717TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
718 OmahaResponse response;
719 OmahaRequestParams params = request_params_;
720 fake_update_response_.include_cohorts = true;
721 fake_update_response_.cohort = "s/154454/8479665";
722 fake_update_response_.cohorthint = "please-put-me-on-beta";
723 fake_update_response_.cohortname = "stable";
724
725 ASSERT_TRUE(TestUpdateCheck(&params,
726 fake_update_response_.GetUpdateResponse(),
727 -1,
728 false, // ping_only
729 ErrorCode::kSuccess,
730 metrics::CheckResult::kUpdateAvailable,
731 metrics::CheckReaction::kUpdating,
732 metrics::DownloadErrorCode::kUnset,
733 &response,
734 nullptr));
735
736 string value;
737 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
738 EXPECT_EQ(fake_update_response_.cohort, value);
739
740 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
741 EXPECT_EQ(fake_update_response_.cohorthint, value);
742
743 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
744 EXPECT_EQ(fake_update_response_.cohortname, value);
745}
746
747TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
748 OmahaResponse response;
749 OmahaRequestParams params = request_params_;
750 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
751 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
752 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
753 fake_update_response_.include_cohorts = true;
754 fake_update_response_.cohort = "s/154454/8479665";
755 fake_update_response_.cohorthint = "please-put-me-on-beta";
756 fake_update_response_.cohortname = "";
757
758 ASSERT_TRUE(TestUpdateCheck(&params,
759 fake_update_response_.GetUpdateResponse(),
760 -1,
761 false, // ping_only
762 ErrorCode::kSuccess,
763 metrics::CheckResult::kUpdateAvailable,
764 metrics::CheckReaction::kUpdating,
765 metrics::DownloadErrorCode::kUnset,
766 &response,
767 nullptr));
768
769 string value;
770 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
771 EXPECT_EQ(fake_update_response_.cohort, value);
772
773 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
774 EXPECT_EQ(fake_update_response_.cohorthint, value);
775
776 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
777}
778
779TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
780 OmahaResponse response;
781 OmahaRequestParams params = request_params_;
782 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
783
784 ASSERT_TRUE(TestUpdateCheck(&params,
785 fake_update_response_.GetUpdateResponse(),
786 -1,
787 false, // ping_only
788 ErrorCode::kSuccess,
789 metrics::CheckResult::kUpdateAvailable,
790 metrics::CheckReaction::kUpdating,
791 metrics::DownloadErrorCode::kUnset,
792 &response,
793 nullptr));
794
795 string value;
796 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
797 EXPECT_EQ("old_value", value);
798
799 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
800 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
801}
802
Alex Deymo00d79ac2015-06-29 15:41:49 -0700803TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
804 OmahaResponse response;
805 OmahaRequestParams params = request_params_;
806 fake_update_response_.include_cohorts = true;
807 fake_update_response_.cohort = "s/154454/8479665";
808 fake_update_response_.cohorthint = "please-put-me-on-beta";
809 fake_update_response_.cohortname = "stable";
810
811 ASSERT_TRUE(TestUpdateCheck(&params,
812 fake_update_response_.GetNoUpdateResponse(),
813 -1,
814 false, // ping_only
815 ErrorCode::kSuccess,
816 metrics::CheckResult::kNoUpdateAvailable,
817 metrics::CheckReaction::kUnset,
818 metrics::DownloadErrorCode::kUnset,
819 &response,
820 nullptr));
821
822 string value;
823 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
824 EXPECT_EQ(fake_update_response_.cohort, value);
825
826 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
827 EXPECT_EQ(fake_update_response_.cohorthint, value);
828
829 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
830 EXPECT_EQ(fake_update_response_.cohortname, value);
831}
832
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700833TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700834 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700835
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700836 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700837 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700838
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700839 OmahaRequestParams params = request_params_;
840 fake_system_state_.set_request_params(&params);
841 OmahaRequestAction action(&fake_system_state_, nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700842 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800843 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700844 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -0700845 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700846 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700847 ActionProcessor processor;
848 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700849 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700850
Alex Deymo60ca1a72015-06-18 18:19:15 -0700851 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
852 loop.Run();
853 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700854 EXPECT_FALSE(processor.IsRunning());
855}
856
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700857TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700858 OmahaResponse response;
859 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700860 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700861 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700862 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700863 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700864 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800865 metrics::CheckResult::kParsingError,
866 metrics::CheckReaction::kUnset,
867 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700868 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700869 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -0700870 EXPECT_FALSE(response.update_exists);
871}
872
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700873TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700874 OmahaResponse response;
875 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700876 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -0700877 "",
878 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700879 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700880 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800881 metrics::CheckResult::kParsingError,
882 metrics::CheckReaction::kUnset,
883 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700884 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700885 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700886 EXPECT_FALSE(response.update_exists);
887}
888
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700889TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700890 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700891 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700892 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700893 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
894 "<daystart elapsed_seconds=\"100\"/>"
895 "<app appid=\"foo\" status=\"ok\">"
896 "<ping status=\"ok\"/>"
897 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700898 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700899 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700900 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800901 metrics::CheckResult::kParsingError,
902 metrics::CheckReaction::kUnset,
903 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700904 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700905 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700906 EXPECT_FALSE(response.update_exists);
907}
908
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700909TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700910 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700911 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700912 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700913 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
914 "<daystart elapsed_seconds=\"100\"/>"
915 "<app appid=\"foo\" status=\"ok\">"
916 "<ping status=\"ok\"/>"
917 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700918 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700919 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700920 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800921 metrics::CheckResult::kParsingError,
922 metrics::CheckReaction::kUnset,
923 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700924 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700925 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700926 EXPECT_FALSE(response.update_exists);
927}
928
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700929TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700930 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700931 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700932 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700933 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
934 "<daystart elapsed_seconds=\"100\"/>"
935 "<app appid=\"foo\" status=\"ok\">"
936 "<ping status=\"ok\"/>"
937 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700938 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700939 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700940 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800941 metrics::CheckResult::kParsingError,
942 metrics::CheckReaction::kUnset,
943 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700944 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700945 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700946 EXPECT_FALSE(response.update_exists);
947}
948
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700949TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700950 string input_response =
951 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
952 "<daystart elapsed_seconds=\"100\"/>"
953 "<app appid=\"xyz\" status=\"ok\">"
954 "<updatecheck status=\"ok\">"
955 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700956 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700957 "<packages><package hash=\"not-used\" name=\"f\" "
958 "size=\"587\"/></packages>"
959 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700960 "ChromeOSVersion=\"10.2.3.4\" "
961 "Prompt=\"false\" "
962 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800963 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700964 "sha256=\"lkq34j5345\" "
965 "needsadmin=\"true\" "
966 "/></actions></manifest></updatecheck></app></response>";
967 LOG(INFO) << "Input Response = " << input_response;
968
Darin Petkov6a5b3222010-07-13 14:55:28 -0700969 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700970 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700971 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700972 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700973 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700974 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800975 metrics::CheckResult::kUpdateAvailable,
976 metrics::CheckReaction::kUpdating,
977 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700978 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700979 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700980 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700981 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800982 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700983 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700984 EXPECT_EQ("lkq34j5345", response.hash);
985 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700986 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700987 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700988}
989
990namespace {
991class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
992 public:
993 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700994 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700995 }
Darin Petkov6a5b3222010-07-13 14:55:28 -0700996};
997
Alex Deymo60ca1a72015-06-18 18:19:15 -0700998void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700999 processor->StartProcessing();
1000 CHECK(processor->IsRunning());
1001 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001002}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001003} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001004
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001005TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001006 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001007 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001008
Alex Deymo60ca1a72015-06-18 18:19:15 -07001009 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001010 OmahaRequestAction action(&fake_system_state_, nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001011 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001012 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001013 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001014 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001015 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001016 ActionProcessor processor;
1017 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001018 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001019
Alex Deymo60ca1a72015-06-18 18:19:15 -07001020 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1021 loop.Run();
1022 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001023}
1024
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001025TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001026 string output;
1027 EXPECT_TRUE(XmlEncode("ab", &output));
1028 EXPECT_EQ("ab", output);
1029 EXPECT_TRUE(XmlEncode("a<b", &output));
1030 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001031 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1032 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001033 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1034 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001035 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001036 EXPECT_FALSE(XmlEncode("\xc2", &output));
1037 // Fail with invalid ASCII-7 chars.
1038 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1039}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001040
Alex Deymob0d74eb2015-03-30 17:59:17 -07001041TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1042 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1043 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1044}
1045
1046TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001047 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001048
1049 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001050 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001051 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001052 OmahaRequestParams::kOsVersion,
1053 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001054 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001055 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001056 "0.1.0.0",
1057 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001058 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001059 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001060 "ChromeOSFirmware.1.0",
1061 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001062 false, // delta okay
1063 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001064 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001065 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001066 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1067 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1068 fake_prefs_.SetString(kPrefsOmahaCohortName,
1069 JoinString(vector<string>(100, "My spoon is too big."),
1070 ' '));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001071 OmahaResponse response;
1072 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001073 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001074 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001075 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001076 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001077 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001078 metrics::CheckResult::kParsingError,
1079 metrics::CheckReaction::kUnset,
1080 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001081 &response,
1082 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001083 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001084 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001085 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1086 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1087 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1088 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1089 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1090 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1091 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1092 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1093 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1094 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1095 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1096 // Values from Prefs that are too big are removed from the XML instead of
1097 // encoded.
1098 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001099}
1100
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001101TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001102 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001103 fake_update_response_.deadline = "&lt;20110101";
1104 fake_update_response_.more_info_url = "testthe&lt;url";
1105 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001106 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001107 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001108 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001109 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001110 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001111 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001112 metrics::CheckResult::kUpdateAvailable,
1113 metrics::CheckReaction::kUpdating,
1114 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001115 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001116 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001117
1118 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001119 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001120 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001121}
1122
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001123TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001124 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001125 // overflows int32_t:
1126 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001127 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001128 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001129 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001130 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001131 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001132 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001133 metrics::CheckResult::kUpdateAvailable,
1134 metrics::CheckReaction::kUpdating,
1135 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001136 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001137 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001138
1139 EXPECT_EQ(response.size, 123123123123123ll);
1140}
1141
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001142TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001143 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001144 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001145 fake_system_state_.set_prefs(&prefs);
1146
Darin Petkov95508da2011-01-05 12:42:29 -08001147 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1148 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001149 // An existing but empty previous version means that we didn't reboot to a new
1150 // update, therefore, no need to update the previous version.
1151 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001152 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001153 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001154 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001155 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001156 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001157 metrics::CheckResult::kParsingError,
1158 metrics::CheckReaction::kUnset,
1159 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001160 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001161 &post_data));
1162 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001163 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001164 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001165 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001166 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001167 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001168 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1169 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001170 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1171 string::npos);
1172 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1173 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001174 // No <event> tag should be sent if we didn't reboot to an update.
1175 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001176}
1177
Jay Srinivasan0a708742012-03-20 11:26:12 -07001178
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001179TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001180 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001181 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001182 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1183 "invalid xml>",
1184 &post_data);
1185 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001186 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001187 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001188 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001189 OmahaEvent::kTypeUpdateDownloadStarted,
1190 OmahaEvent::kResultSuccess);
1191 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001192 EXPECT_EQ(post_str.find("ping"), string::npos);
1193 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001194}
1195
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001196TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001197 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001198 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001199 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1200 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001201 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001202 "invalid xml>",
1203 &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());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001206 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001207 " <event eventtype=\"%d\" eventresult=\"%d\" "
1208 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001209 OmahaEvent::kTypeDownloadComplete,
1210 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001211 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001212 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001213 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001214}
1215
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001216TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001217 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001218 // Create a copy of the OmahaRequestParams to reuse it later.
1219 OmahaRequestParams params = request_params_;
1220 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001221 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001222 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001223 nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001224 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001225 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001226 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001227 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001228 EXPECT_FALSE(update_check_action.IsEvent());
1229
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001230 params = request_params_;
1231 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001232 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001233 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001234 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001235 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001236 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001237 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001238 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001239 EXPECT_TRUE(event_action.IsEvent());
1240}
1241
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001242TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001243 for (int i = 0; i < 2; i++) {
1244 bool delta_okay = i == 1;
1245 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001246 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001247 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001248 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001249 OmahaRequestParams::kOsVersion,
1250 "service_pack",
1251 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001252 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001253 "0.1.0.0",
1254 "en-US",
1255 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001256 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001257 "ChromeOSFirmware.1.0",
1258 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001259 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001260 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001261 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001262 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001263 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001264 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001265 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001266 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001267 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001268 metrics::CheckResult::kParsingError,
1269 metrics::CheckReaction::kUnset,
1270 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001271 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001272 &post_data));
1273 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001274 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001275 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1276 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001277 string::npos)
1278 << "i = " << i;
1279 }
1280}
1281
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001282TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001283 for (int i = 0; i < 2; i++) {
1284 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001285 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001286 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001287 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001288 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001289 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001290 OmahaRequestParams::kOsVersion,
1291 "service_pack",
1292 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001293 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001294 "0.1.0.0",
1295 "en-US",
1296 "unittest_track",
1297 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001298 "ChromeOSFirmware.1.0",
1299 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001300 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001301 interactive,
1302 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001303 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001304 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001305 "invalid xml>",
1306 -1,
1307 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001308 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001309 metrics::CheckResult::kParsingError,
1310 metrics::CheckReaction::kUnset,
1311 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001312 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001313 &post_data));
1314 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001315 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001316 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1317 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001318 string::npos)
1319 << "i = " << i;
1320 }
1321}
1322
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001323TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001324 OmahaEvent default_event;
1325 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1326 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001327 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001328
1329 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1330 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1331 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001332 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001333
1334 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1335 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001336 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001337 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1338 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001339 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001340}
1341
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001342void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001343 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001344 fake_system_state_.set_prefs(&prefs);
1345 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1346 .Times(AnyNumber());
1347 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1348 // Add a few hours to the day difference to test no rounding, etc.
1349 int64_t five_days_ago =
1350 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1351 int64_t six_days_ago =
1352 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1353 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1354 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1355 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1356 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1357 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1358 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001359 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001360 ASSERT_TRUE(
1361 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001362 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001363 -1,
1364 ping_only,
1365 ErrorCode::kSuccess,
1366 metrics::CheckResult::kUnset,
1367 metrics::CheckReaction::kUnset,
1368 metrics::DownloadErrorCode::kUnset,
1369 nullptr,
1370 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001371 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001372 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1373 string::npos);
1374 if (ping_only) {
1375 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1376 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1377 } else {
1378 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1379 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001380 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001381}
1382
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001383TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1384 PingTest(true /* ping_only */);
1385}
1386
1387TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1388 PingTest(false /* ping_only */);
1389}
1390
1391TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001392 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001393 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001394 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1395 .Times(AnyNumber());
1396 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001397 int64_t three_days_ago =
1398 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1399 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001400 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1401 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001402 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1403 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1404 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1405 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001406 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001407 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001408 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001409 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001410 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001411 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001412 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001413 metrics::CheckResult::kNoUpdateAvailable,
1414 metrics::CheckReaction::kUnset,
1415 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001416 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001417 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001418 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001419 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001420 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001421}
1422
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001423TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001424 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001425 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001426 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1427 .Times(AnyNumber());
1428 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001429 int64_t four_days_ago =
1430 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1431 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001432 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1433 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001434 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1435 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1436 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1437 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001438 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001439 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001440 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001441 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001442 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001443 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001444 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001445 metrics::CheckResult::kNoUpdateAvailable,
1446 metrics::CheckReaction::kUnset,
1447 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001448 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001449 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001450 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001451 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001452 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001453}
1454
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001455TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001456 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001457 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001458 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1459 .Times(AnyNumber());
1460 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001461 int64_t one_hour_ago =
1462 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001463 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1464 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001465 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1466 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1467 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1468 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001469 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1470 // send a ping.
1471 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1472 .WillOnce(Return(true));
1473 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1474 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001475 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001476 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001477 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001478 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001479 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001480 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001481 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001482 metrics::CheckResult::kNoUpdateAvailable,
1483 metrics::CheckReaction::kUnset,
1484 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001485 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001486 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001487 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001488 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001489}
1490
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001491TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001492 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001493 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001494 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001495 int64_t now = Time::Now().ToInternalValue();
1496 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1497 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1498 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1499 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1500 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1501 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001502 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001503 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001504 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001505 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001506 -1,
1507 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001508 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001509 metrics::CheckResult::kUnset,
1510 metrics::CheckReaction::kUnset,
1511 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001512 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001513 &post_data));
1514 EXPECT_EQ(post_data.size(), 0);
1515}
1516
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001517TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001518 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001519 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001520 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1521 .Times(AnyNumber());
1522 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001523 int64_t future =
1524 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001525 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1526 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001527 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1528 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1529 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1530 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1531 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1532 .WillOnce(Return(true));
1533 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1534 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001535 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001536 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001537 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001538 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1539 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001540 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001541 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001542 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001543 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001544 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001545 metrics::CheckResult::kNoUpdateAvailable,
1546 metrics::CheckReaction::kUnset,
1547 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001548 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001549 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001550 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001551 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001552}
1553
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001554TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001555 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001556 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001557 // may fail if it runs for longer than 5 seconds. It shouldn't run
1558 // that long though.
1559 int64_t midnight =
1560 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1561 int64_t midnight_slack =
1562 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001563 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001564 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001565 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1566 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001567 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1568 AllOf(Ge(midnight), Le(midnight_slack))))
1569 .WillOnce(Return(true));
1570 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1571 AllOf(Ge(midnight), Le(midnight_slack))))
1572 .WillOnce(Return(true));
1573 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001574 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001575 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1576 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001577 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001578 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001579 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001580 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001581 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001582 metrics::CheckResult::kNoUpdateAvailable,
1583 metrics::CheckReaction::kUnset,
1584 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001585 nullptr,
1586 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001587}
1588
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001589TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001590 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001591 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001592 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1593 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001594 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1595 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1596 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001597 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001598 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1599 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001600 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001601 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001602 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001603 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001604 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001605 metrics::CheckResult::kNoUpdateAvailable,
1606 metrics::CheckReaction::kUnset,
1607 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001608 nullptr,
1609 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001610}
1611
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001612TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001613 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001614 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001615 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1616 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001617 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1618 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1619 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001620 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001621 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1622 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001623 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001624 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001625 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001626 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001627 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001628 metrics::CheckResult::kNoUpdateAvailable,
1629 metrics::CheckReaction::kUnset,
1630 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001631 nullptr,
1632 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001633}
1634
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001635TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001636 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001637 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001638 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001639 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001640 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001641 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001642 metrics::CheckResult::kParsingError,
1643 metrics::CheckReaction::kUnset,
1644 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001645 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001646 &post_data));
1647 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001648 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001649 EXPECT_EQ(post_str.find("machineid="), string::npos);
1650 EXPECT_EQ(post_str.find("userid="), string::npos);
1651}
1652
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001653TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001654 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001655 const int http_error_code =
1656 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001657 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001658 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001659 "",
1660 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001661 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001662 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001663 metrics::CheckResult::kDownloadError,
1664 metrics::CheckReaction::kUnset,
1665 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001666 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001667 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001668 EXPECT_FALSE(response.update_exists);
1669}
1670
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001671TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001672 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001673 const int http_error_code =
1674 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001675 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001676 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001677 "",
1678 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001679 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001680 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001681 metrics::CheckResult::kDownloadError,
1682 metrics::CheckReaction::kUnset,
1683 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001684 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001685 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001686 EXPECT_FALSE(response.update_exists);
1687}
1688
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001689TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001690 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001691 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001692 params.set_wall_clock_based_wait_enabled(true);
1693 params.set_waiting_period(TimeDelta().FromDays(1));
1694 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001695
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001696 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001697 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001698 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001699 -1,
1700 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001701 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001702 metrics::CheckResult::kUpdateAvailable,
1703 metrics::CheckReaction::kDeferring,
1704 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001705 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001706 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001707
Ben Chan9abb7632014-08-07 00:10:53 -07001708 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001709 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001710 ASSERT_GT(timestamp, 0);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001711 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001712
1713 // Verify if we are interactive check we don't defer.
1714 params.set_interactive(true);
1715 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001716 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001717 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -07001718 -1,
1719 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001720 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001721 metrics::CheckResult::kUpdateAvailable,
1722 metrics::CheckReaction::kUpdating,
1723 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001724 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001725 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001726 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001727}
1728
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001729TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001730 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001731 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001732 params.set_wall_clock_based_wait_enabled(true);
1733 params.set_waiting_period(TimeDelta().FromDays(1));
1734 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001735
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001736 // Set the timestamp to a very old value such that it exceeds the
1737 // waiting period set above.
1738 Time t1;
1739 Time::FromString("1/1/2012", &t1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001740 ASSERT_TRUE(fake_prefs_.SetInt64(
1741 kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001742 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001743 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001744 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001745 -1,
1746 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001747 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001748 metrics::CheckResult::kUpdateAvailable,
1749 metrics::CheckReaction::kUpdating,
1750 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001751 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001752 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001753
1754 EXPECT_TRUE(response.update_exists);
1755
1756 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07001757 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001758 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001759 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1760}
1761
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001762TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001763 // Create a uniquely named test directory.
1764 string test_dir;
1765 ASSERT_TRUE(utils::MakeTempDirectory(
1766 "omaha_request_action-test-XXXXXX", &test_dir));
1767
1768 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1769 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001770 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001771 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001772 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001773 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001774 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001775 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001776 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1777 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1778 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1779 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001780 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001781 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
1782 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001783 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001784 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001785 params.Init("1.2.3.4", "", 0);
1786 EXPECT_EQ("canary-channel", params.current_channel());
1787 EXPECT_EQ("stable-channel", params.target_channel());
1788 EXPECT_TRUE(params.to_more_stable_channel());
1789 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001790 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001791 "invalid xml>",
1792 -1,
1793 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001794 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001795 metrics::CheckResult::kParsingError,
1796 metrics::CheckReaction::kUnset,
1797 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001798 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001799 &post_data));
1800 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001801 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001802 EXPECT_NE(string::npos, post_str.find(
1803 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1804 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1805 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001806
Alex Deymo110e0302015-10-19 20:35:21 -07001807 ASSERT_TRUE(base::DeleteFile(base::FilePath(test_dir), true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001808}
1809
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001810TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001811 // Create a uniquely named test directory.
1812 string test_dir;
1813 ASSERT_TRUE(utils::MakeTempDirectory(
1814 "omaha_request_action-test-XXXXXX", &test_dir));
1815
1816 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1817 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001818 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001819 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001820 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001821 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001822 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001823 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001824 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1825 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1826 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1827 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001828 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001829 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001830 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001831 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001832 params.Init("5.6.7.8", "", 0);
1833 EXPECT_EQ("stable-channel", params.current_channel());
1834 EXPECT_EQ("canary-channel", params.target_channel());
1835 EXPECT_FALSE(params.to_more_stable_channel());
1836 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001837 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001838 "invalid xml>",
1839 -1,
1840 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001841 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001842 metrics::CheckResult::kParsingError,
1843 metrics::CheckReaction::kUnset,
1844 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001845 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001846 &post_data));
1847 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001848 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001849 EXPECT_NE(string::npos, post_str.find(
1850 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1851 "version=\"5.6.7.8\" "
1852 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001853 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001854}
1855
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001856// Checks that the initial ping with a=-1 r=-1 is not send when the device
1857// was powerwashed.
1858TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
1859 fake_prefs_.SetString(kPrefsPreviousVersion, "");
1860
1861 // Flag that the device was powerwashed in the past.
1862 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
1863
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001864 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001865 ASSERT_TRUE(
1866 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001867 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001868 -1,
1869 false, // ping_only
1870 ErrorCode::kSuccess,
1871 metrics::CheckResult::kNoUpdateAvailable,
1872 metrics::CheckReaction::kUnset,
1873 metrics::DownloadErrorCode::kUnset,
1874 nullptr,
1875 &post_data));
1876 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001877 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001878 EXPECT_EQ(string::npos, post_str.find("<ping"));
1879}
1880
Alex Deymo9fded1e2015-11-05 12:31:19 -08001881// Checks that the event 54 is sent on a reboot to a new update.
1882TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
1883 // Flag that the device was updated in a previous boot.
1884 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
1885
1886 brillo::Blob post_data;
1887 ASSERT_TRUE(
1888 TestUpdateCheck(nullptr, // request_params
1889 fake_update_response_.GetNoUpdateResponse(),
1890 -1,
1891 false, // ping_only
1892 ErrorCode::kSuccess,
1893 metrics::CheckResult::kNoUpdateAvailable,
1894 metrics::CheckReaction::kUnset,
1895 metrics::DownloadErrorCode::kUnset,
1896 nullptr,
1897 &post_data));
1898 string post_str(post_data.begin(), post_data.end());
1899
1900 // An event 54 is included and has the right version.
1901 EXPECT_NE(string::npos,
1902 post_str.find(base::StringPrintf(
1903 "<event eventtype=\"%d\"",
1904 OmahaEvent::kTypeRebootedAfterUpdate)));
1905 EXPECT_NE(string::npos,
1906 post_str.find("previousversion=\"1.2.3.4\"></event>"));
1907
1908 // The previous version flag should have been removed.
1909 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
1910 string prev_version;
1911 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
1912 EXPECT_TRUE(prev_version.empty());
1913}
1914
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001915void OmahaRequestActionTest::P2PTest(
1916 bool initial_allow_p2p_for_downloading,
1917 bool initial_allow_p2p_for_sharing,
1918 bool omaha_disable_p2p_for_downloading,
1919 bool omaha_disable_p2p_for_sharing,
1920 bool payload_state_allow_p2p_attempt,
1921 bool expect_p2p_client_lookup,
1922 const string& p2p_client_result_url,
1923 bool expected_allow_p2p_for_downloading,
1924 bool expected_allow_p2p_for_sharing,
1925 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001926 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001927 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07001928 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
1929 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
1930 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07001931
1932 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001933 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07001934 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1935 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07001936 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
1937 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
1938 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
1939 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
1940 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
1941 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
1942 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
1943 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
1944 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
1945 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
1946
David Zeuthen8f191b22013-08-06 12:27:50 -07001947 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001948 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001949 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1950
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001951 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
1952 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07001953 .Times(expect_p2p_client_lookup ? 1 : 0);
1954
Alex Deymo8e18f932015-03-27 16:16:59 -07001955 fake_update_response_.disable_p2p_for_downloading =
1956 omaha_disable_p2p_for_downloading;
1957 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07001958 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001959 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001960 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07001961 -1,
1962 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001963 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001964 metrics::CheckResult::kUpdateAvailable,
1965 metrics::CheckReaction::kUpdating,
1966 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07001967 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001968 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07001969 EXPECT_TRUE(response.update_exists);
1970
Gilad Arnold74b5f552014-10-07 08:17:16 -07001971 EXPECT_EQ(omaha_disable_p2p_for_downloading,
1972 response.disable_p2p_for_downloading);
1973 EXPECT_EQ(omaha_disable_p2p_for_sharing,
1974 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07001975
Gilad Arnold74b5f552014-10-07 08:17:16 -07001976 EXPECT_EQ(expected_allow_p2p_for_downloading,
1977 actual_allow_p2p_for_downloading);
1978 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
1979 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07001980}
1981
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001982TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001983 P2PTest(true, // initial_allow_p2p_for_downloading
1984 true, // initial_allow_p2p_for_sharing
1985 false, // omaha_disable_p2p_for_downloading
1986 false, // omaha_disable_p2p_for_sharing
1987 true, // payload_state_allow_p2p_attempt
1988 true, // expect_p2p_client_lookup
1989 "http://1.3.5.7/p2p", // p2p_client_result_url
1990 true, // expected_allow_p2p_for_downloading
1991 true, // expected_allow_p2p_for_sharing
1992 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001993}
1994
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001995TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001996 P2PTest(true, // initial_allow_p2p_for_downloading
1997 true, // initial_allow_p2p_for_sharing
1998 false, // omaha_disable_p2p_for_downloading
1999 false, // omaha_disable_p2p_for_sharing
2000 true, // payload_state_allow_p2p_attempt
2001 true, // expect_p2p_client_lookup
2002 "", // p2p_client_result_url
2003 false, // expected_allow_p2p_for_downloading
2004 true, // expected_allow_p2p_for_sharing
2005 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002006}
2007
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002008TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002009 P2PTest(false, // initial_allow_p2p_for_downloading
2010 true, // initial_allow_p2p_for_sharing
2011 false, // omaha_disable_p2p_for_downloading
2012 false, // omaha_disable_p2p_for_sharing
2013 true, // payload_state_allow_p2p_attempt
2014 false, // expect_p2p_client_lookup
2015 "unset", // p2p_client_result_url
2016 false, // expected_allow_p2p_for_downloading
2017 true, // expected_allow_p2p_for_sharing
2018 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002019}
2020
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002021TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002022 P2PTest(true, // initial_allow_p2p_for_downloading
2023 true, // initial_allow_p2p_for_sharing
2024 true, // omaha_disable_p2p_for_downloading
2025 false, // omaha_disable_p2p_for_sharing
2026 true, // payload_state_allow_p2p_attempt
2027 false, // expect_p2p_client_lookup
2028 "unset", // p2p_client_result_url
2029 false, // expected_allow_p2p_for_downloading
2030 true, // expected_allow_p2p_for_sharing
2031 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002032}
2033
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002034TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002035 P2PTest(true, // initial_allow_p2p_for_downloading
2036 true, // initial_allow_p2p_for_sharing
2037 false, // omaha_disable_p2p_for_downloading
2038 true, // omaha_disable_p2p_for_sharing
2039 true, // payload_state_allow_p2p_attempt
2040 true, // expect_p2p_client_lookup
2041 "http://1.3.5.7/p2p", // p2p_client_result_url
2042 true, // expected_allow_p2p_for_downloading
2043 false, // expected_allow_p2p_for_sharing
2044 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002045}
2046
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002047TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002048 P2PTest(true, // initial_allow_p2p_for_downloading
2049 true, // initial_allow_p2p_for_sharing
2050 true, // omaha_disable_p2p_for_downloading
2051 true, // omaha_disable_p2p_for_sharing
2052 true, // payload_state_allow_p2p_attempt
2053 false, // expect_p2p_client_lookup
2054 "unset", // p2p_client_result_url
2055 false, // expected_allow_p2p_for_downloading
2056 false, // expected_allow_p2p_for_sharing
2057 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002058}
2059
Alex Deymof329b932014-10-30 01:37:48 -07002060bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2061 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002062 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002063 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002064 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002065 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002066 -1,
2067 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002068 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002069 metrics::CheckResult::kUpdateAvailable,
2070 metrics::CheckReaction::kUpdating,
2071 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002072 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002073 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002074}
2075
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002076TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002077 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002078
2079 // Check that we parse elapsed_days in the Omaha Response correctly.
2080 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002081 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2082 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002083 EXPECT_TRUE(response.update_exists);
2084 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002085 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002086 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002087 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002088 EXPECT_EQ(prefs_days, 42);
2089
2090 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002091 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002092 EXPECT_TRUE(response.update_exists);
2093 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002094 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002095 EXPECT_EQ(prefs_days, 42);
2096
2097 // Note that elapsed_days is not necessarily divisible by 7 so check
2098 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002099 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2100 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002101 EXPECT_TRUE(response.update_exists);
2102 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002103 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002104 EXPECT_EQ(prefs_days, 21);
2105
2106 // Check that we correctly handle elapsed_days not being included in
2107 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002108 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002109 EXPECT_TRUE(response.update_exists);
2110 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002111}
2112
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002113// If there is no prefs and OOBE is not complete, we should not
2114// report anything to Omaha.
2115TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
2116 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2117 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2118}
David Zeuthen639aa362014-02-03 16:23:44 -08002119
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002120// If OOBE is complete and happened on a valid date (e.g. after Jan
2121// 1 2007 0:00 PST), that date should be used and written to
2122// prefs. However, first try with an invalid date and check we do
2123// nothing.
2124TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2125 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2126 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2127 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2128 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2129}
David Zeuthen639aa362014-02-03 16:23:44 -08002130
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002131// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2132// should yield an InstallDate of 14.
2133TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2134 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2135 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2136 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2137 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002138
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002139 int64_t prefs_days;
2140 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2141 EXPECT_EQ(prefs_days, 14);
2142}
David Zeuthen639aa362014-02-03 16:23:44 -08002143
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002144// Now that we have a valid date in prefs, check that we keep using
2145// that even if OOBE date reports something else. The date Jan 30,
2146// 2007 0:00 PST should yield an InstallDate of 28... but since
2147// there's a prefs file, we should still get 14.
2148TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2149 // Set a valid date in the prefs first.
2150 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002151
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002152 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2153 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2154 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002155
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002156 int64_t prefs_days;
2157 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2158 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002159
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002160 // If we delete the prefs file, we should get 28 days.
2161 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2162 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2163 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2164 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002165}
2166
Darin Petkov6a5b3222010-07-13 14:55:28 -07002167} // namespace chromeos_update_engine