blob: 1ee215105577f024aaae60b654fd8a3aa2bfc332 [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
Darin Petkov6a5b3222010-07-13 14:55:28 -070036#include "update_engine/action_pipe.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070037#include "update_engine/constants.h"
Alex Deymoe1e3afe2014-10-30 13:02:49 -070038#include "update_engine/fake_prefs.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070039#include "update_engine/mock_connection_manager.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070040#include "update_engine/mock_http_fetcher.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070041#include "update_engine/mock_payload_state.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070042#include "update_engine/omaha_hash_calculator.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070043#include "update_engine/omaha_request_params.h"
Alex Deymoac41a822015-09-15 20:52:53 -070044#include "update_engine/platform_constants.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070045#include "update_engine/prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070046#include "update_engine/test_utils.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070047#include "update_engine/utils.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070048
Darin Petkov1cbd78f2010-07-29 12:38:34 -070049using base::Time;
50using base::TimeDelta;
Alex Deymo10875d92014-11-10 21:52:57 -080051using chromeos_update_engine::test_utils::System;
52using chromeos_update_engine::test_utils::WriteFileString;
Darin Petkov6a5b3222010-07-13 14:55:28 -070053using std::string;
54using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070055using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070056using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070057using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058using testing::Ge;
59using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080060using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070061using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070062using testing::ReturnPointee;
63using testing::SaveArg;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070064using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070065using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070066
Alex Deymo8e18f932015-03-27 16:16:59 -070067namespace {
68
Alex Deymo85616652015-10-15 18:48:31 -070069const char kTestAppId[] = "test-app-id";
70
Alex Deymo8e18f932015-03-27 16:16:59 -070071// This is a helper struct to allow unit tests build an update response with the
72// values they care about.
73struct FakeUpdateResponse {
74 string GetNoUpdateResponse() const {
75 string entity_str;
76 if (include_entity)
77 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070078 return
79 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
80 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070081 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070082 "<app appid=\"" + app_id + "\" " +
83 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
84 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
85 " status=\"ok\">"
86 "<ping status=\"ok\"/>"
87 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070088 }
89
90 string GetUpdateResponse() const {
91 return
92 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
93 "protocol=\"3.0\">"
94 "<daystart elapsed_seconds=\"100\"" +
95 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
96 + "/>"
97 "<app appid=\"" + app_id + "\" " +
98 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
99 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
100 " status=\"ok\">"
101 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
102 "<urls><url codebase=\"" + codebase + "\"/></urls>"
103 "<manifest version=\"" + version + "\">"
104 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
105 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
106 "<actions><action event=\"postinstall\" "
107 "ChromeOSVersion=\"" + version + "\" "
108 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
109 "IsDelta=\"true\" "
110 "IsDeltaPayload=\"true\" "
111 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
112 "sha256=\"" + hash + "\" "
113 "needsadmin=\"" + needsadmin + "\" " +
114 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
115 (disable_p2p_for_downloading ?
116 "DisableP2PForDownloading=\"true\" " : "") +
117 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
118 "/></actions></manifest></updatecheck></app></response>";
119 }
120
121 // Return the payload URL, which is split in two fields in the XML response.
122 string GetPayloadUrl() {
123 return codebase + filename;
124 }
125
Alex Deymo85616652015-10-15 18:48:31 -0700126 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700127 string version = "1.2.3.4";
128 string more_info_url = "http://more/info";
129 string prompt = "true";
130 string codebase = "http://code/base/";
131 string filename = "file.signed";
132 string hash = "HASH1234=";
133 string needsadmin = "false";
134 int64_t size = 123;
135 string deadline = "";
136 string max_days_to_scatter = "7";
137 string elapsed_days = "42";
138
139 // P2P setting defaults to allowed.
140 bool disable_p2p_for_downloading = false;
141 bool disable_p2p_for_sharing = false;
142
143 // Omaha cohorts settings.
144 bool include_cohorts = false;
145 string cohort = "";
146 string cohorthint = "";
147 string cohortname = "";
148
149 // Whether to include the CrOS <!ENTITY> in the XML response.
150 bool include_entity = false;
151};
152
153} // namespace
154
Darin Petkov6a5b3222010-07-13 14:55:28 -0700155namespace chromeos_update_engine {
156
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700157class OmahaRequestActionTest : public ::testing::Test {
158 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800159 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700160 fake_system_state_.set_request_params(&request_params_);
161 fake_system_state_.set_prefs(&fake_prefs_);
162 }
163
164 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800165 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700166 // is used. |payload_state| may be null, in which case a local mock is used.
167 // |p2p_manager| may be null, in which case a local mock is used.
168 // |connection_manager| may be null, in which case a local mock is used.
169 // out_response may be null. If |fail_http_response_code| is non-negative,
170 // the transfer will fail with that code. |ping_only| is passed through to the
171 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
172 // post-data received by the mock HttpFetcher is returned.
173 //
174 // The |expected_check_result|, |expected_check_reaction| and
175 // |expected_error_code| parameters are for checking expectations
176 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
177 // UMA statistics. Use the appropriate ::kUnset value to specify that
178 // the given metric should not be reported.
179 bool TestUpdateCheck(OmahaRequestParams* request_params,
180 const string& http_response,
181 int fail_http_response_code,
182 bool ping_only,
183 ErrorCode expected_code,
184 metrics::CheckResult expected_check_result,
185 metrics::CheckReaction expected_check_reaction,
186 metrics::DownloadErrorCode expected_download_error_code,
187 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700188 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700189
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800190 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700191 // only a ping or also an updatecheck.
192 void PingTest(bool ping_only);
193
194 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700195 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700196 OmahaResponse *response);
197
198 // P2P test helper function.
199 void P2PTest(
200 bool initial_allow_p2p_for_downloading,
201 bool initial_allow_p2p_for_sharing,
202 bool omaha_disable_p2p_for_downloading,
203 bool omaha_disable_p2p_for_sharing,
204 bool payload_state_allow_p2p_attempt,
205 bool expect_p2p_client_lookup,
206 const string& p2p_client_result_url,
207 bool expected_allow_p2p_for_downloading,
208 bool expected_allow_p2p_for_sharing,
209 const string& expected_p2p_url);
210
211 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700212 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700213
214 // By default, all tests use these objects unless they replace them in the
215 // fake_system_state_.
216 OmahaRequestParams request_params_ = OmahaRequestParams{
217 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700218 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700219 OmahaRequestParams::kOsVersion,
220 "service_pack",
221 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700222 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700223 "0.1.0.0",
224 "en-US",
225 "unittest",
226 "OEM MODEL 09235 7471",
227 "ChromeOSFirmware.1.0",
228 "0X0A1",
229 false, // delta okay
230 false, // interactive
231 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700232 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700233
234 FakePrefs fake_prefs_;
235};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700236
237namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
239 public:
240 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700241 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800242 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700243 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800244 void ProcessingDone(const ActionProcessor* processor,
245 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700246 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700247 }
248
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800249 void ActionCompleted(ActionProcessor* processor,
250 AbstractAction* action,
251 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700252 // make sure actions always succeed
253 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700254 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700255 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700256 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700257 }
David Zeuthena99981f2013-04-29 13:42:47 -0700258 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700259};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700260} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700261
262class OutputObjectCollectorAction;
263
264template<>
265class ActionTraits<OutputObjectCollectorAction> {
266 public:
267 // Does not take an object for input
268 typedef OmahaResponse InputObjectType;
269 // On success, puts the output path on output
270 typedef NoneType OutputObjectType;
271};
272
273class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
274 public:
275 OutputObjectCollectorAction() : has_input_object_(false) {}
276 void PerformAction() {
277 // copy input object
278 has_input_object_ = HasInputObject();
279 if (has_input_object_)
280 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700281 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700282 }
283 // Should never be called
284 void TerminateProcessing() {
285 CHECK(false);
286 }
287 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700288 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700289 return "OutputObjectCollectorAction";
290 }
Alex Deymof329b932014-10-30 01:37:48 -0700291 string Type() const { return StaticType(); }
Darin Petkov6a5b3222010-07-13 14:55:28 -0700292 bool has_input_object_;
293 OmahaResponse omaha_response_;
294};
295
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700296bool OmahaRequestActionTest::TestUpdateCheck(
297 OmahaRequestParams* request_params,
298 const string& http_response,
299 int fail_http_response_code,
300 bool ping_only,
301 ErrorCode expected_code,
302 metrics::CheckResult expected_check_result,
303 metrics::CheckReaction expected_check_reaction,
304 metrics::DownloadErrorCode expected_download_error_code,
305 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700306 brillo::Blob* out_post_data) {
307 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700308 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700309 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800310 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700311 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700312 if (fail_http_response_code >= 0) {
313 fetcher->FailTransfer(fail_http_response_code);
314 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700315 if (request_params)
316 fake_system_state_.set_request_params(request_params);
317 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700318 nullptr,
Thieu Le116fda32011-04-19 11:01:54 -0700319 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700320 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700321 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700322 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700323
Darin Petkov6a5b3222010-07-13 14:55:28 -0700324 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700325 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700326 processor.EnqueueAction(&action);
327
328 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700329 BondActions(&action, &collector_action);
330 processor.EnqueueAction(&collector_action);
331
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700332 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800333 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700334 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800335 SendEnumToUMA(metrics::kMetricCheckResult,
336 static_cast<int>(expected_check_result),
337 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
338 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700339 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800340 SendEnumToUMA(metrics::kMetricCheckReaction,
341 static_cast<int>(expected_check_reaction),
342 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
343 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700344 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700345 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
346 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800347 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
348 ? 0 : 1);
349
Alex Deymo60ca1a72015-06-18 18:19:15 -0700350 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
351 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
352 loop.Run();
353 LOG(INFO) << "loop.PendingTasks() = " << loop.PendingTasks();
354 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700355 if (collector_action.has_input_object_ && out_response)
356 *out_response = collector_action.omaha_response_;
357 if (out_post_data)
358 *out_post_data = fetcher->post_data();
359 return collector_action.has_input_object_;
360}
361
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700362// Tests Event requests -- they should always succeed. |out_post_data|
363// may be null; if non-null, the post-data received by the mock
364// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700365void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700366 OmahaEvent* event,
367 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700368 brillo::Blob* out_post_data) {
369 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700370 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700371 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800372 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700373 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700374 FakeSystemState fake_system_state;
375 fake_system_state.set_request_params(&params);
376 OmahaRequestAction action(&fake_system_state, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700377 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700378 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700379 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700380 processor.EnqueueAction(&action);
381
Alex Deymo60ca1a72015-06-18 18:19:15 -0700382 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
383 loop.Run();
384
385 // This test should schedule a callback to notify the crash reporter if
386 // the passed event is an error.
387 EXPECT_EQ(event->result == OmahaEvent::kResultError, loop.PendingTasks());
388
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700389 if (out_post_data)
390 *out_post_data = fetcher->post_data();
391}
392
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700393TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400394 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700395 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400396 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700397 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700398 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400399 -1,
400 false, // ping_only
401 ErrorCode::kOmahaRequestXMLHasEntityDecl,
402 metrics::CheckResult::kParsingError,
403 metrics::CheckReaction::kUnset,
404 metrics::DownloadErrorCode::kUnset,
405 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700406 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400407 EXPECT_FALSE(response.update_exists);
408}
409
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700410TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700411 OmahaResponse response;
412 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700413 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700414 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700415 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700416 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700417 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800418 metrics::CheckResult::kNoUpdateAvailable,
419 metrics::CheckReaction::kUnset,
420 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700421 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700422 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700423 EXPECT_FALSE(response.update_exists);
424}
425
Alex Deymo8e18f932015-03-27 16:16:59 -0700426// Test that all the values in the response are parsed in a normal update
427// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700428TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700429 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700430 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700431 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700432 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700433 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700434 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700435 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700436 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800437 metrics::CheckResult::kUpdateAvailable,
438 metrics::CheckReaction::kUpdating,
439 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700440 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700441 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700442 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700443 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700444 EXPECT_EQ(fake_update_response_.version, response.version);
445 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
446 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
447 EXPECT_EQ(fake_update_response_.hash, response.hash);
448 EXPECT_EQ(fake_update_response_.size, response.size);
449 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
450 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
451 // Omaha cohort attribets are not set in the response, so they should not be
452 // persisted.
453 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
454 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
455 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700456}
457
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700458TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700459 OmahaResponse response;
460 // Set up a connection manager that doesn't allow a valid update over
461 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700462 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700463 fake_system_state_.set_connection_manager(&mock_cm);
464
Alex Deymo30534502015-07-20 15:06:33 -0700465 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
466 .WillRepeatedly(
467 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet),
468 SetArgumentPointee<1>(NetworkTethering::kUnknown),
469 Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700470 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _))
Chris Sosa77f79e82014-06-02 18:16:24 -0700471 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700472
473 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700474 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700475 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700476 -1,
477 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700478 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700479 metrics::CheckResult::kUpdateAvailable,
480 metrics::CheckReaction::kIgnored,
481 metrics::DownloadErrorCode::kUnset,
482 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700483 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700484 EXPECT_FALSE(response.update_exists);
485}
486
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700487TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700488 string rollback_version = "1234.0.0";
489 OmahaResponse response;
490
491 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700492 fake_system_state_.set_payload_state(&mock_payload_state);
493
Chris Sosa77f79e82014-06-02 18:16:24 -0700494 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
495 .WillRepeatedly(Return(rollback_version));
496
Alex Deymo8e18f932015-03-27 16:16:59 -0700497 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700498 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700499 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700500 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700501 -1,
502 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700503 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700504 metrics::CheckResult::kUpdateAvailable,
505 metrics::CheckReaction::kIgnored,
506 metrics::DownloadErrorCode::kUnset,
507 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700508 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700509 EXPECT_FALSE(response.update_exists);
510}
511
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700512TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700513 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700514 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700515 params.set_wall_clock_based_wait_enabled(true);
516 params.set_update_check_count_wait_enabled(false);
517 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700518
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700519 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700520 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700521 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700522 -1,
523 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700524 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800525 metrics::CheckResult::kUpdateAvailable,
526 metrics::CheckReaction::kDeferring,
527 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700528 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700529 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700530 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700531
532 // Verify if we are interactive check we don't defer.
533 params.set_interactive(true);
534 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700535 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700536 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700537 -1,
538 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700539 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800540 metrics::CheckResult::kUpdateAvailable,
541 metrics::CheckReaction::kUpdating,
542 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700543 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700544 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700545 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700546}
547
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700548TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700549 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700550 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700551 params.set_wall_clock_based_wait_enabled(false);
552 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700553
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700554 params.set_update_check_count_wait_enabled(true);
555 params.set_min_update_checks_needed(1);
556 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700557
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700558 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700559 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700560 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700561 -1,
562 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700563 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800564 metrics::CheckResult::kUpdateAvailable,
565 metrics::CheckReaction::kUpdating,
566 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700567 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700568 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700569 EXPECT_TRUE(response.update_exists);
570}
571
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700572TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700573 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700574 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700575 params.set_wall_clock_based_wait_enabled(true);
576 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700577
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700578 params.set_update_check_count_wait_enabled(true);
579 params.set_min_update_checks_needed(1);
580 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700581
Alex Deymo8e18f932015-03-27 16:16:59 -0700582 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700583 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700584 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700585 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700586 -1,
587 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700588 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800589 metrics::CheckResult::kUpdateAvailable,
590 metrics::CheckReaction::kUpdating,
591 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700592 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700593 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700594 EXPECT_TRUE(response.update_exists);
595}
596
597
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700598TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700599 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700600 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700601 params.set_wall_clock_based_wait_enabled(true);
602 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700603
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700604 params.set_update_check_count_wait_enabled(true);
605 params.set_min_update_checks_needed(0);
606 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700607
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700608 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700609 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700610 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700611 -1,
612 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700613 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800614 metrics::CheckResult::kUpdateAvailable,
615 metrics::CheckReaction::kUpdating,
616 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700617 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700618 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700619
Ben Chan9abb7632014-08-07 00:10:53 -0700620 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700621 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700622 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700623 EXPECT_TRUE(response.update_exists);
624}
625
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700626TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700627 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700628 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700629 params.set_wall_clock_based_wait_enabled(true);
630 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700631
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700632 params.set_update_check_count_wait_enabled(true);
633 params.set_min_update_checks_needed(1);
634 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700635
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700636 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700637 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700638 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700639 -1,
640 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700641 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800642 metrics::CheckResult::kUpdateAvailable,
643 metrics::CheckReaction::kDeferring,
644 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700645 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700646 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700647
Ben Chan9abb7632014-08-07 00:10:53 -0700648 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700649 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700650 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700651 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700652
653 // Verify if we are interactive check we don't defer.
654 params.set_interactive(true);
655 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700656 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700657 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700658 -1,
659 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700660 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800661 metrics::CheckResult::kUpdateAvailable,
662 metrics::CheckReaction::kUpdating,
663 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700664 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700665 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700666 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700667}
668
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700669TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700670 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700671 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700672 params.set_wall_clock_based_wait_enabled(true);
673 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700674
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700675 params.set_update_check_count_wait_enabled(true);
676 params.set_min_update_checks_needed(1);
677 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700678
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700679 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700680
681 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700682 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700683 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700684 -1,
685 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700686 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800687 metrics::CheckResult::kUpdateAvailable,
688 metrics::CheckReaction::kDeferring,
689 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700690 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700691 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700692
Ben Chan9abb7632014-08-07 00:10:53 -0700693 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700694 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700695 // count remains the same, as the decrementing happens in update_attempter
696 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700697 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700698 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700699
700 // Verify if we are interactive check we don't defer.
701 params.set_interactive(true);
702 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700703 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700704 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700705 -1,
706 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700707 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800708 metrics::CheckResult::kUpdateAvailable,
709 metrics::CheckReaction::kUpdating,
710 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700711 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700712 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700713 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700714}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700715
Alex Deymo8e18f932015-03-27 16:16:59 -0700716TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
717 OmahaResponse response;
718 OmahaRequestParams params = request_params_;
719 fake_update_response_.include_cohorts = true;
720 fake_update_response_.cohort = "s/154454/8479665";
721 fake_update_response_.cohorthint = "please-put-me-on-beta";
722 fake_update_response_.cohortname = "stable";
723
724 ASSERT_TRUE(TestUpdateCheck(&params,
725 fake_update_response_.GetUpdateResponse(),
726 -1,
727 false, // ping_only
728 ErrorCode::kSuccess,
729 metrics::CheckResult::kUpdateAvailable,
730 metrics::CheckReaction::kUpdating,
731 metrics::DownloadErrorCode::kUnset,
732 &response,
733 nullptr));
734
735 string value;
736 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
737 EXPECT_EQ(fake_update_response_.cohort, value);
738
739 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
740 EXPECT_EQ(fake_update_response_.cohorthint, value);
741
742 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
743 EXPECT_EQ(fake_update_response_.cohortname, value);
744}
745
746TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
747 OmahaResponse response;
748 OmahaRequestParams params = request_params_;
749 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
750 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
751 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
752 fake_update_response_.include_cohorts = true;
753 fake_update_response_.cohort = "s/154454/8479665";
754 fake_update_response_.cohorthint = "please-put-me-on-beta";
755 fake_update_response_.cohortname = "";
756
757 ASSERT_TRUE(TestUpdateCheck(&params,
758 fake_update_response_.GetUpdateResponse(),
759 -1,
760 false, // ping_only
761 ErrorCode::kSuccess,
762 metrics::CheckResult::kUpdateAvailable,
763 metrics::CheckReaction::kUpdating,
764 metrics::DownloadErrorCode::kUnset,
765 &response,
766 nullptr));
767
768 string value;
769 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
770 EXPECT_EQ(fake_update_response_.cohort, value);
771
772 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
773 EXPECT_EQ(fake_update_response_.cohorthint, value);
774
775 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
776}
777
778TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
779 OmahaResponse response;
780 OmahaRequestParams params = request_params_;
781 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
782
783 ASSERT_TRUE(TestUpdateCheck(&params,
784 fake_update_response_.GetUpdateResponse(),
785 -1,
786 false, // ping_only
787 ErrorCode::kSuccess,
788 metrics::CheckResult::kUpdateAvailable,
789 metrics::CheckReaction::kUpdating,
790 metrics::DownloadErrorCode::kUnset,
791 &response,
792 nullptr));
793
794 string value;
795 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
796 EXPECT_EQ("old_value", value);
797
798 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
799 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
800}
801
Alex Deymo00d79ac2015-06-29 15:41:49 -0700802TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
803 OmahaResponse response;
804 OmahaRequestParams params = request_params_;
805 fake_update_response_.include_cohorts = true;
806 fake_update_response_.cohort = "s/154454/8479665";
807 fake_update_response_.cohorthint = "please-put-me-on-beta";
808 fake_update_response_.cohortname = "stable";
809
810 ASSERT_TRUE(TestUpdateCheck(&params,
811 fake_update_response_.GetNoUpdateResponse(),
812 -1,
813 false, // ping_only
814 ErrorCode::kSuccess,
815 metrics::CheckResult::kNoUpdateAvailable,
816 metrics::CheckReaction::kUnset,
817 metrics::DownloadErrorCode::kUnset,
818 &response,
819 nullptr));
820
821 string value;
822 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
823 EXPECT_EQ(fake_update_response_.cohort, value);
824
825 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
826 EXPECT_EQ(fake_update_response_.cohorthint, value);
827
828 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
829 EXPECT_EQ(fake_update_response_.cohortname, value);
830}
831
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700832TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700833 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700834
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700835 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700836 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700837
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700838 OmahaRequestParams params = request_params_;
839 fake_system_state_.set_request_params(&params);
840 OmahaRequestAction action(&fake_system_state_, nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700841 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800842 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700843 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -0700844 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700845 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700846 ActionProcessor processor;
847 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700848 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700849
Alex Deymo60ca1a72015-06-18 18:19:15 -0700850 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
851 loop.Run();
852 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700853 EXPECT_FALSE(processor.IsRunning());
854}
855
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700856TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700857 OmahaResponse response;
858 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700859 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700860 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700861 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700862 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700863 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800864 metrics::CheckResult::kParsingError,
865 metrics::CheckReaction::kUnset,
866 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700867 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700868 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -0700869 EXPECT_FALSE(response.update_exists);
870}
871
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700872TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -0700873 OmahaResponse response;
874 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700875 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -0700876 "",
877 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700878 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700879 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800880 metrics::CheckResult::kParsingError,
881 metrics::CheckReaction::kUnset,
882 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700883 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700884 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700885 EXPECT_FALSE(response.update_exists);
886}
887
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700888TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700889 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700890 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700891 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700892 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
893 "<daystart elapsed_seconds=\"100\"/>"
894 "<app appid=\"foo\" status=\"ok\">"
895 "<ping status=\"ok\"/>"
896 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700897 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700898 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700899 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800900 metrics::CheckResult::kParsingError,
901 metrics::CheckReaction::kUnset,
902 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700903 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700904 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700905 EXPECT_FALSE(response.update_exists);
906}
907
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700908TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700909 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700910 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700911 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700912 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
913 "<daystart elapsed_seconds=\"100\"/>"
914 "<app appid=\"foo\" status=\"ok\">"
915 "<ping status=\"ok\"/>"
916 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700917 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700918 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700919 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800920 metrics::CheckResult::kParsingError,
921 metrics::CheckReaction::kUnset,
922 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700923 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700924 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700925 EXPECT_FALSE(response.update_exists);
926}
927
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700928TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700929 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700930 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700931 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700932 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
933 "<daystart elapsed_seconds=\"100\"/>"
934 "<app appid=\"foo\" status=\"ok\">"
935 "<ping status=\"ok\"/>"
936 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700937 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700938 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700939 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800940 metrics::CheckResult::kParsingError,
941 metrics::CheckReaction::kUnset,
942 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700943 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700944 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700945 EXPECT_FALSE(response.update_exists);
946}
947
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700948TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700949 string input_response =
950 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
951 "<daystart elapsed_seconds=\"100\"/>"
952 "<app appid=\"xyz\" status=\"ok\">"
953 "<updatecheck status=\"ok\">"
954 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700955 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700956 "<packages><package hash=\"not-used\" name=\"f\" "
957 "size=\"587\"/></packages>"
958 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700959 "ChromeOSVersion=\"10.2.3.4\" "
960 "Prompt=\"false\" "
961 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800962 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700963 "sha256=\"lkq34j5345\" "
964 "needsadmin=\"true\" "
965 "/></actions></manifest></updatecheck></app></response>";
966 LOG(INFO) << "Input Response = " << input_response;
967
Darin Petkov6a5b3222010-07-13 14:55:28 -0700968 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700969 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700970 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700971 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700972 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700973 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800974 metrics::CheckResult::kUpdateAvailable,
975 metrics::CheckReaction::kUpdating,
976 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700977 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700978 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700979 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700980 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800981 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700982 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700983 EXPECT_EQ("lkq34j5345", response.hash);
984 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700985 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700986 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700987}
988
989namespace {
990class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
991 public:
992 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700993 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700994 }
Darin Petkov6a5b3222010-07-13 14:55:28 -0700995};
996
Alex Deymo60ca1a72015-06-18 18:19:15 -0700997void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700998 processor->StartProcessing();
999 CHECK(processor->IsRunning());
1000 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001001}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001002} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001003
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001004TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001005 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001006 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001007
Alex Deymo60ca1a72015-06-18 18:19:15 -07001008 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001009 OmahaRequestAction action(&fake_system_state_, nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001010 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001011 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001012 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001013 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001014 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001015 ActionProcessor processor;
1016 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001017 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001018
Alex Deymo60ca1a72015-06-18 18:19:15 -07001019 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1020 loop.Run();
1021 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001022}
1023
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001024TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001025 string output;
1026 EXPECT_TRUE(XmlEncode("ab", &output));
1027 EXPECT_EQ("ab", output);
1028 EXPECT_TRUE(XmlEncode("a<b", &output));
1029 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001030 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1031 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001032 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1033 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001034 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001035 EXPECT_FALSE(XmlEncode("\xc2", &output));
1036 // Fail with invalid ASCII-7 chars.
1037 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1038}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001039
Alex Deymob0d74eb2015-03-30 17:59:17 -07001040TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1041 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1042 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1043}
1044
1045TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001046 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001047
1048 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001049 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001050 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001051 OmahaRequestParams::kOsVersion,
1052 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001053 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001054 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001055 "0.1.0.0",
1056 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001057 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001058 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001059 "ChromeOSFirmware.1.0",
1060 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001061 false, // delta okay
1062 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001063 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001064 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001065 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1066 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1067 fake_prefs_.SetString(kPrefsOmahaCohortName,
1068 JoinString(vector<string>(100, "My spoon is too big."),
1069 ' '));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001070 OmahaResponse response;
1071 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001072 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001073 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001074 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001075 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001076 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001077 metrics::CheckResult::kParsingError,
1078 metrics::CheckReaction::kUnset,
1079 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001080 &response,
1081 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001082 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001083 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001084 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1085 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1086 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1087 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1088 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1089 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1090 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1091 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1092 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1093 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1094 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1095 // Values from Prefs that are too big are removed from the XML instead of
1096 // encoded.
1097 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001098}
1099
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001100TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001101 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001102 fake_update_response_.deadline = "&lt;20110101";
1103 fake_update_response_.more_info_url = "testthe&lt;url";
1104 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001105 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001106 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001107 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001108 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001109 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001110 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001111 metrics::CheckResult::kUpdateAvailable,
1112 metrics::CheckReaction::kUpdating,
1113 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001114 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001115 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001116
1117 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001118 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001119 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001120}
1121
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001122TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001123 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001124 // overflows int32_t:
1125 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001126 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001127 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001128 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001129 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001130 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001131 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001132 metrics::CheckResult::kUpdateAvailable,
1133 metrics::CheckReaction::kUpdating,
1134 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001135 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001136 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001137
1138 EXPECT_EQ(response.size, 123123123123123ll);
1139}
1140
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001141TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001142 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001143 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001144 fake_system_state_.set_prefs(&prefs);
1145
Darin Petkov95508da2011-01-05 12:42:29 -08001146 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1147 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001148 // An existing but empty previous version means that we didn't reboot to a new
1149 // update, therefore, no need to update the previous version.
1150 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001151 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001152 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001153 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001154 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001155 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001156 metrics::CheckResult::kParsingError,
1157 metrics::CheckReaction::kUnset,
1158 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001159 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001160 &post_data));
1161 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001162 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001163 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001164 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001165 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001166 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001167 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1168 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001169 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1170 string::npos);
1171 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1172 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001173 // No <event> tag should be sent if we didn't reboot to an update.
1174 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001175}
1176
Jay Srinivasan0a708742012-03-20 11:26:12 -07001177
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001178TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001179 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001180 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001181 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1182 "invalid xml>",
1183 &post_data);
1184 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001185 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001186 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001187 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001188 OmahaEvent::kTypeUpdateDownloadStarted,
1189 OmahaEvent::kResultSuccess);
1190 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001191 EXPECT_EQ(post_str.find("ping"), string::npos);
1192 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001193}
1194
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001195TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001196 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001197 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001198 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1199 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001200 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001201 "invalid xml>",
1202 &post_data);
1203 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001204 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001205 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001206 " <event eventtype=\"%d\" eventresult=\"%d\" "
1207 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001208 OmahaEvent::kTypeDownloadComplete,
1209 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001210 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001211 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001212 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001213}
1214
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001215TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001216 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001217 // Create a copy of the OmahaRequestParams to reuse it later.
1218 OmahaRequestParams params = request_params_;
1219 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001220 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001221 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001222 nullptr,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001223 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001224 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001225 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001226 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001227 EXPECT_FALSE(update_check_action.IsEvent());
1228
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001229 params = request_params_;
1230 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001231 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001232 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001233 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001234 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001235 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001236 nullptr),
Thieu Le116fda32011-04-19 11:01:54 -07001237 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001238 EXPECT_TRUE(event_action.IsEvent());
1239}
1240
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001241TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001242 for (int i = 0; i < 2; i++) {
1243 bool delta_okay = i == 1;
1244 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001245 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001246 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001247 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001248 OmahaRequestParams::kOsVersion,
1249 "service_pack",
1250 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001251 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001252 "0.1.0.0",
1253 "en-US",
1254 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001255 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001256 "ChromeOSFirmware.1.0",
1257 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001258 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001259 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001260 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001261 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001262 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001263 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001264 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001265 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001266 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001267 metrics::CheckResult::kParsingError,
1268 metrics::CheckReaction::kUnset,
1269 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001270 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001271 &post_data));
1272 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001273 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001274 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1275 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001276 string::npos)
1277 << "i = " << i;
1278 }
1279}
1280
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001281TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001282 for (int i = 0; i < 2; i++) {
1283 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001284 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001285 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001286 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001287 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001288 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001289 OmahaRequestParams::kOsVersion,
1290 "service_pack",
1291 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001292 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001293 "0.1.0.0",
1294 "en-US",
1295 "unittest_track",
1296 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001297 "ChromeOSFirmware.1.0",
1298 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001299 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001300 interactive,
1301 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001302 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001303 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001304 "invalid xml>",
1305 -1,
1306 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001307 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001308 metrics::CheckResult::kParsingError,
1309 metrics::CheckReaction::kUnset,
1310 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001311 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001312 &post_data));
1313 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001314 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001315 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1316 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001317 string::npos)
1318 << "i = " << i;
1319 }
1320}
1321
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001322TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001323 OmahaEvent default_event;
1324 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1325 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001326 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001327
1328 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1329 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1330 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001331 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001332
1333 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1334 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001335 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001336 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1337 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001338 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001339}
1340
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001341void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001342 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001343 fake_system_state_.set_prefs(&prefs);
1344 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1345 .Times(AnyNumber());
1346 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1347 // Add a few hours to the day difference to test no rounding, etc.
1348 int64_t five_days_ago =
1349 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1350 int64_t six_days_ago =
1351 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1352 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1353 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1354 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1355 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1356 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1357 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001358 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001359 ASSERT_TRUE(
1360 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001361 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001362 -1,
1363 ping_only,
1364 ErrorCode::kSuccess,
1365 metrics::CheckResult::kUnset,
1366 metrics::CheckReaction::kUnset,
1367 metrics::DownloadErrorCode::kUnset,
1368 nullptr,
1369 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001370 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001371 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1372 string::npos);
1373 if (ping_only) {
1374 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1375 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1376 } else {
1377 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1378 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001379 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001380}
1381
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001382TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1383 PingTest(true /* ping_only */);
1384}
1385
1386TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1387 PingTest(false /* ping_only */);
1388}
1389
1390TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001391 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001392 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001393 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1394 .Times(AnyNumber());
1395 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001396 int64_t three_days_ago =
1397 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1398 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001399 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1400 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001401 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1402 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1403 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1404 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001405 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001406 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001407 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001408 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001409 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001410 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001411 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001412 metrics::CheckResult::kNoUpdateAvailable,
1413 metrics::CheckReaction::kUnset,
1414 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001415 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001416 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001417 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001418 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001419 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001420}
1421
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001422TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001423 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001424 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001425 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1426 .Times(AnyNumber());
1427 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001428 int64_t four_days_ago =
1429 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1430 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001431 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1432 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001433 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1434 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1435 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1436 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001437 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001438 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001439 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001440 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001441 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001442 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001443 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001444 metrics::CheckResult::kNoUpdateAvailable,
1445 metrics::CheckReaction::kUnset,
1446 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001447 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001448 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001449 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001450 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001451 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001452}
1453
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001454TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001455 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001456 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001457 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1458 .Times(AnyNumber());
1459 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001460 int64_t one_hour_ago =
1461 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001462 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1463 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001464 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1465 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1466 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1467 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001468 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1469 // send a ping.
1470 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1471 .WillOnce(Return(true));
1472 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1473 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001474 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001475 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001476 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001477 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001478 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001479 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001480 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001481 metrics::CheckResult::kNoUpdateAvailable,
1482 metrics::CheckReaction::kUnset,
1483 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001484 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001485 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001486 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001487 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001488}
1489
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001490TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001491 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001492 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001493 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001494 int64_t now = Time::Now().ToInternalValue();
1495 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1496 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1497 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1498 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1499 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1500 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001501 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001502 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001503 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001504 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001505 -1,
1506 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001507 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001508 metrics::CheckResult::kUnset,
1509 metrics::CheckReaction::kUnset,
1510 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001511 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001512 &post_data));
1513 EXPECT_EQ(post_data.size(), 0);
1514}
1515
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001516TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001517 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001518 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001519 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1520 .Times(AnyNumber());
1521 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001522 int64_t future =
1523 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001524 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1525 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001526 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1527 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1528 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1529 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1530 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1531 .WillOnce(Return(true));
1532 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1533 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001534 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001535 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001536 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001537 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1538 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001539 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001540 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001541 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001542 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001543 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001544 metrics::CheckResult::kNoUpdateAvailable,
1545 metrics::CheckReaction::kUnset,
1546 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001547 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001548 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001549 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001550 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001551}
1552
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001553TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001554 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001555 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001556 // may fail if it runs for longer than 5 seconds. It shouldn't run
1557 // that long though.
1558 int64_t midnight =
1559 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1560 int64_t midnight_slack =
1561 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001562 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001563 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001564 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1565 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001566 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1567 AllOf(Ge(midnight), Le(midnight_slack))))
1568 .WillOnce(Return(true));
1569 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1570 AllOf(Ge(midnight), Le(midnight_slack))))
1571 .WillOnce(Return(true));
1572 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001573 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001574 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1575 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001576 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001577 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001578 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001579 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001580 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001581 metrics::CheckResult::kNoUpdateAvailable,
1582 metrics::CheckReaction::kUnset,
1583 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001584 nullptr,
1585 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001586}
1587
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001588TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001589 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001590 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001591 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1592 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001593 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1594 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1595 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001596 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001597 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1598 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001599 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001600 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001601 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001602 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001603 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001604 metrics::CheckResult::kNoUpdateAvailable,
1605 metrics::CheckReaction::kUnset,
1606 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001607 nullptr,
1608 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001609}
1610
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001611TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001612 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001613 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001614 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1615 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001616 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1617 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1618 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001619 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001620 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1621 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001622 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001623 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001624 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001625 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001626 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001627 metrics::CheckResult::kNoUpdateAvailable,
1628 metrics::CheckReaction::kUnset,
1629 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001630 nullptr,
1631 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001632}
1633
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001634TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001635 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001636 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001637 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001638 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001639 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001640 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001641 metrics::CheckResult::kParsingError,
1642 metrics::CheckReaction::kUnset,
1643 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001644 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001645 &post_data));
1646 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001647 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001648 EXPECT_EQ(post_str.find("machineid="), string::npos);
1649 EXPECT_EQ(post_str.find("userid="), string::npos);
1650}
1651
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001652TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001653 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001654 const int http_error_code =
1655 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001656 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001657 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001658 "",
1659 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001660 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001661 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001662 metrics::CheckResult::kDownloadError,
1663 metrics::CheckReaction::kUnset,
1664 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001665 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001666 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001667 EXPECT_FALSE(response.update_exists);
1668}
1669
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001670TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001671 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001672 const int http_error_code =
1673 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001674 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001675 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001676 "",
1677 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001678 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001679 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001680 metrics::CheckResult::kDownloadError,
1681 metrics::CheckReaction::kUnset,
1682 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001683 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001684 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001685 EXPECT_FALSE(response.update_exists);
1686}
1687
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001688TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001689 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001690 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001691 params.set_wall_clock_based_wait_enabled(true);
1692 params.set_waiting_period(TimeDelta().FromDays(1));
1693 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001694
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001695 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001696 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001697 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001698 -1,
1699 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001700 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001701 metrics::CheckResult::kUpdateAvailable,
1702 metrics::CheckReaction::kDeferring,
1703 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001704 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001705 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001706
Ben Chan9abb7632014-08-07 00:10:53 -07001707 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001708 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001709 ASSERT_GT(timestamp, 0);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001710 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001711
1712 // Verify if we are interactive check we don't defer.
1713 params.set_interactive(true);
1714 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001715 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001716 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -07001717 -1,
1718 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001719 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001720 metrics::CheckResult::kUpdateAvailable,
1721 metrics::CheckReaction::kUpdating,
1722 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001723 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001724 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001725 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001726}
1727
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001728TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001729 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001730 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001731 params.set_wall_clock_based_wait_enabled(true);
1732 params.set_waiting_period(TimeDelta().FromDays(1));
1733 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001734
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001735 // Set the timestamp to a very old value such that it exceeds the
1736 // waiting period set above.
1737 Time t1;
1738 Time::FromString("1/1/2012", &t1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001739 ASSERT_TRUE(fake_prefs_.SetInt64(
1740 kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001741 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001742 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001743 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001744 -1,
1745 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001746 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001747 metrics::CheckResult::kUpdateAvailable,
1748 metrics::CheckReaction::kUpdating,
1749 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001750 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001751 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001752
1753 EXPECT_TRUE(response.update_exists);
1754
1755 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07001756 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001757 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001758 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1759}
1760
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001761TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001762 // Create a uniquely named test directory.
1763 string test_dir;
1764 ASSERT_TRUE(utils::MakeTempDirectory(
1765 "omaha_request_action-test-XXXXXX", &test_dir));
1766
1767 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1768 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001769 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001770 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001771 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001772 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001773 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001774 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001775 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1776 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1777 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1778 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001779 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001780 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
1781 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001782 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001783 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001784 params.Init("1.2.3.4", "", 0);
1785 EXPECT_EQ("canary-channel", params.current_channel());
1786 EXPECT_EQ("stable-channel", params.target_channel());
1787 EXPECT_TRUE(params.to_more_stable_channel());
1788 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001789 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001790 "invalid xml>",
1791 -1,
1792 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001793 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001794 metrics::CheckResult::kParsingError,
1795 metrics::CheckReaction::kUnset,
1796 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001797 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001798 &post_data));
1799 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001800 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001801 EXPECT_NE(string::npos, post_str.find(
1802 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1803 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1804 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001805
Alex Deymo110e0302015-10-19 20:35:21 -07001806 ASSERT_TRUE(base::DeleteFile(base::FilePath(test_dir), true));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001807}
1808
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001809TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001810 // Create a uniquely named test directory.
1811 string test_dir;
1812 ASSERT_TRUE(utils::MakeTempDirectory(
1813 "omaha_request_action-test-XXXXXX", &test_dir));
1814
1815 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1816 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001817 kStatefulPartition + "/etc"));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001818 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001819 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001820 fake_system_state_.set_prefs(&prefs);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001821 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001822 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001823 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1824 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1825 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1826 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001827 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001828 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001829 OmahaRequestParams params = request_params_;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001830 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001831 params.Init("5.6.7.8", "", 0);
1832 EXPECT_EQ("stable-channel", params.current_channel());
1833 EXPECT_EQ("canary-channel", params.target_channel());
1834 EXPECT_FALSE(params.to_more_stable_channel());
1835 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001836 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001837 "invalid xml>",
1838 -1,
1839 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001840 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001841 metrics::CheckResult::kParsingError,
1842 metrics::CheckReaction::kUnset,
1843 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001844 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001845 &post_data));
1846 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001847 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001848 EXPECT_NE(string::npos, post_str.find(
1849 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1850 "version=\"5.6.7.8\" "
1851 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001852 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001853}
1854
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001855// Checks that the initial ping with a=-1 r=-1 is not send when the device
1856// was powerwashed.
1857TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
1858 fake_prefs_.SetString(kPrefsPreviousVersion, "");
1859
1860 // Flag that the device was powerwashed in the past.
1861 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
1862
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001863 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001864 ASSERT_TRUE(
1865 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001866 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001867 -1,
1868 false, // ping_only
1869 ErrorCode::kSuccess,
1870 metrics::CheckResult::kNoUpdateAvailable,
1871 metrics::CheckReaction::kUnset,
1872 metrics::DownloadErrorCode::kUnset,
1873 nullptr,
1874 &post_data));
1875 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001876 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001877 EXPECT_EQ(string::npos, post_str.find("<ping"));
1878}
1879
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001880void OmahaRequestActionTest::P2PTest(
1881 bool initial_allow_p2p_for_downloading,
1882 bool initial_allow_p2p_for_sharing,
1883 bool omaha_disable_p2p_for_downloading,
1884 bool omaha_disable_p2p_for_sharing,
1885 bool payload_state_allow_p2p_attempt,
1886 bool expect_p2p_client_lookup,
1887 const string& p2p_client_result_url,
1888 bool expected_allow_p2p_for_downloading,
1889 bool expected_allow_p2p_for_sharing,
1890 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07001891 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001892 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07001893 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
1894 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
1895 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07001896
1897 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001898 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07001899 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1900 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07001901 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
1902 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
1903 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
1904 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
1905 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
1906 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
1907 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
1908 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
1909 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
1910 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
1911
David Zeuthen8f191b22013-08-06 12:27:50 -07001912 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001913 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07001914 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1915
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001916 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
1917 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07001918 .Times(expect_p2p_client_lookup ? 1 : 0);
1919
Alex Deymo8e18f932015-03-27 16:16:59 -07001920 fake_update_response_.disable_p2p_for_downloading =
1921 omaha_disable_p2p_for_downloading;
1922 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07001923 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001924 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001925 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07001926 -1,
1927 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001928 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001929 metrics::CheckResult::kUpdateAvailable,
1930 metrics::CheckReaction::kUpdating,
1931 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07001932 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001933 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07001934 EXPECT_TRUE(response.update_exists);
1935
Gilad Arnold74b5f552014-10-07 08:17:16 -07001936 EXPECT_EQ(omaha_disable_p2p_for_downloading,
1937 response.disable_p2p_for_downloading);
1938 EXPECT_EQ(omaha_disable_p2p_for_sharing,
1939 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07001940
Gilad Arnold74b5f552014-10-07 08:17:16 -07001941 EXPECT_EQ(expected_allow_p2p_for_downloading,
1942 actual_allow_p2p_for_downloading);
1943 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
1944 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07001945}
1946
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001947TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001948 P2PTest(true, // initial_allow_p2p_for_downloading
1949 true, // initial_allow_p2p_for_sharing
1950 false, // omaha_disable_p2p_for_downloading
1951 false, // omaha_disable_p2p_for_sharing
1952 true, // payload_state_allow_p2p_attempt
1953 true, // expect_p2p_client_lookup
1954 "http://1.3.5.7/p2p", // p2p_client_result_url
1955 true, // expected_allow_p2p_for_downloading
1956 true, // expected_allow_p2p_for_sharing
1957 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001958}
1959
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001960TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001961 P2PTest(true, // initial_allow_p2p_for_downloading
1962 true, // initial_allow_p2p_for_sharing
1963 false, // omaha_disable_p2p_for_downloading
1964 false, // omaha_disable_p2p_for_sharing
1965 true, // payload_state_allow_p2p_attempt
1966 true, // expect_p2p_client_lookup
1967 "", // p2p_client_result_url
1968 false, // expected_allow_p2p_for_downloading
1969 true, // expected_allow_p2p_for_sharing
1970 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001971}
1972
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001973TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001974 P2PTest(false, // initial_allow_p2p_for_downloading
1975 true, // initial_allow_p2p_for_sharing
1976 false, // omaha_disable_p2p_for_downloading
1977 false, // omaha_disable_p2p_for_sharing
1978 true, // payload_state_allow_p2p_attempt
1979 false, // expect_p2p_client_lookup
1980 "unset", // p2p_client_result_url
1981 false, // expected_allow_p2p_for_downloading
1982 true, // expected_allow_p2p_for_sharing
1983 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001984}
1985
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001986TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001987 P2PTest(true, // initial_allow_p2p_for_downloading
1988 true, // initial_allow_p2p_for_sharing
1989 true, // omaha_disable_p2p_for_downloading
1990 false, // omaha_disable_p2p_for_sharing
1991 true, // payload_state_allow_p2p_attempt
1992 false, // expect_p2p_client_lookup
1993 "unset", // p2p_client_result_url
1994 false, // expected_allow_p2p_for_downloading
1995 true, // expected_allow_p2p_for_sharing
1996 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07001997}
1998
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001999TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002000 P2PTest(true, // initial_allow_p2p_for_downloading
2001 true, // initial_allow_p2p_for_sharing
2002 false, // omaha_disable_p2p_for_downloading
2003 true, // omaha_disable_p2p_for_sharing
2004 true, // payload_state_allow_p2p_attempt
2005 true, // expect_p2p_client_lookup
2006 "http://1.3.5.7/p2p", // p2p_client_result_url
2007 true, // expected_allow_p2p_for_downloading
2008 false, // expected_allow_p2p_for_sharing
2009 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002010}
2011
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002012TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002013 P2PTest(true, // initial_allow_p2p_for_downloading
2014 true, // initial_allow_p2p_for_sharing
2015 true, // omaha_disable_p2p_for_downloading
2016 true, // omaha_disable_p2p_for_sharing
2017 true, // payload_state_allow_p2p_attempt
2018 false, // expect_p2p_client_lookup
2019 "unset", // p2p_client_result_url
2020 false, // expected_allow_p2p_for_downloading
2021 false, // expected_allow_p2p_for_sharing
2022 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002023}
2024
Alex Deymof329b932014-10-30 01:37:48 -07002025bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2026 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002027 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002028 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002029 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002030 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002031 -1,
2032 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002033 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002034 metrics::CheckResult::kUpdateAvailable,
2035 metrics::CheckReaction::kUpdating,
2036 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002037 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002038 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002039}
2040
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002041TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002042 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002043
2044 // Check that we parse elapsed_days in the Omaha Response correctly.
2045 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002046 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2047 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002048 EXPECT_TRUE(response.update_exists);
2049 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002050 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002051 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002052 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002053 EXPECT_EQ(prefs_days, 42);
2054
2055 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002056 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002057 EXPECT_TRUE(response.update_exists);
2058 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002059 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002060 EXPECT_EQ(prefs_days, 42);
2061
2062 // Note that elapsed_days is not necessarily divisible by 7 so check
2063 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002064 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2065 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002066 EXPECT_TRUE(response.update_exists);
2067 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002068 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002069 EXPECT_EQ(prefs_days, 21);
2070
2071 // Check that we correctly handle elapsed_days not being included in
2072 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002073 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002074 EXPECT_TRUE(response.update_exists);
2075 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002076}
2077
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002078// If there is no prefs and OOBE is not complete, we should not
2079// report anything to Omaha.
2080TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
2081 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2082 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2083}
David Zeuthen639aa362014-02-03 16:23:44 -08002084
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002085// If OOBE is complete and happened on a valid date (e.g. after Jan
2086// 1 2007 0:00 PST), that date should be used and written to
2087// prefs. However, first try with an invalid date and check we do
2088// nothing.
2089TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2090 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2091 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2092 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2093 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2094}
David Zeuthen639aa362014-02-03 16:23:44 -08002095
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002096// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2097// should yield an InstallDate of 14.
2098TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2099 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2100 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2101 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2102 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002103
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002104 int64_t prefs_days;
2105 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2106 EXPECT_EQ(prefs_days, 14);
2107}
David Zeuthen639aa362014-02-03 16:23:44 -08002108
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002109// Now that we have a valid date in prefs, check that we keep using
2110// that even if OOBE date reports something else. The date Jan 30,
2111// 2007 0:00 PST should yield an InstallDate of 28... but since
2112// there's a prefs file, we should still get 14.
2113TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2114 // Set a valid date in the prefs first.
2115 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002116
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002117 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2118 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2119 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002120
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002121 int64_t prefs_days;
2122 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2123 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002124
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002125 // If we delete the prefs file, we should get 28 days.
2126 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2127 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2128 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2129 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002130}
2131
Darin Petkov6a5b3222010-07-13 14:55:28 -07002132} // namespace chromeos_update_engine