blob: 3ee6769fa7912ea805ba4e3c6adf8b5bdb095d85 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov6a5b3222010-07-13 14:55:28 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_action.h"
18
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
Darin Petkov6a5b3222010-07-13 14:55:28 -070021#include <string>
22#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070023
Alex Deymo60ca1a72015-06-18 18:19:15 -070024#include <base/bind.h>
Alex Deymo110e0302015-10-19 20:35:21 -070025#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070026#include <base/files/scoped_temp_dir.h>
Alex Deymo8e18f932015-03-27 16:16:59 -070027#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_util.h>
29#include <base/strings/stringprintf.h>
30#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/bind_lambda.h>
Alex Deymoc1c17b42015-11-23 03:53:15 -030032#include <brillo/make_unique_ptr.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070033#include <brillo/message_loops/fake_message_loop.h>
34#include <brillo/message_loops/message_loop.h>
35#include <brillo/message_loops/message_loop_utils.h>
Alex Deymoe1e3afe2014-10-30 13:02:49 -070036#include <gtest/gtest.h>
Jay Srinivasand29695d2013-04-08 15:08:05 -070037
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/action_pipe.h"
39#include "update_engine/common/constants.h"
40#include "update_engine/common/fake_prefs.h"
41#include "update_engine/common/hash_calculator.h"
42#include "update_engine/common/mock_http_fetcher.h"
43#include "update_engine/common/platform_constants.h"
44#include "update_engine/common/prefs.h"
45#include "update_engine/common/test_utils.h"
Alex Deymoc1c17b42015-11-23 03:53:15 -030046#include "update_engine/fake_system_state.h"
Alex Deymo38429cf2015-11-11 18:27:22 -080047#include "update_engine/metrics.h"
Chris Sosa77f79e82014-06-02 18:16:24 -070048#include "update_engine/mock_connection_manager.h"
Gilad Arnold74b5f552014-10-07 08:17:16 -070049#include "update_engine/mock_payload_state.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070050#include "update_engine/omaha_request_params.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070051
Darin Petkov1cbd78f2010-07-29 12:38:34 -070052using base::Time;
53using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070054using std::string;
55using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070056using testing::AllOf;
Alex Deymof329b932014-10-30 01:37:48 -070057using testing::AnyNumber;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070058using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070059using testing::Ge;
60using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080061using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070062using testing::Return;
Gilad Arnold74b5f552014-10-07 08:17:16 -070063using testing::ReturnPointee;
64using testing::SaveArg;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065using testing::SetArgumentPointee;
Alex Deymof329b932014-10-30 01:37:48 -070066using testing::_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070067
Alex Deymo8e18f932015-03-27 16:16:59 -070068namespace {
69
Alex Deymo85616652015-10-15 18:48:31 -070070const char kTestAppId[] = "test-app-id";
71
Alex Deymo8e18f932015-03-27 16:16:59 -070072// This is a helper struct to allow unit tests build an update response with the
73// values they care about.
74struct FakeUpdateResponse {
75 string GetNoUpdateResponse() const {
76 string entity_str;
77 if (include_entity)
78 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>";
Alex Deymo00d79ac2015-06-29 15:41:49 -070079 return
80 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
81 entity_str + "<response protocol=\"3.0\">"
Alex Deymo8e18f932015-03-27 16:16:59 -070082 "<daystart elapsed_seconds=\"100\"/>"
Alex Deymo00d79ac2015-06-29 15:41:49 -070083 "<app appid=\"" + app_id + "\" " +
84 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
85 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
86 " status=\"ok\">"
87 "<ping status=\"ok\"/>"
88 "<updatecheck status=\"noupdate\"/></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -070089 }
90
91 string GetUpdateResponse() const {
Sen Jiang2703ef42017-03-16 13:36:21 -070092 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
93 "protocol=\"3.0\">"
94 "<daystart elapsed_seconds=\"100\"" +
95 (elapsed_days.empty() ? ""
96 : (" elapsed_days=\"" + elapsed_days + "\"")) +
97 "/>"
98 "<app appid=\"" +
99 app_id + "\" " +
100 (include_cohorts
101 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint +
102 "\" cohortname=\"" + cohortname + "\" "
103 : "") +
104 " status=\"ok\">"
105 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
106 "<urls><url codebase=\"" +
107 codebase +
108 "\"/></urls>"
109 "<manifest version=\"" +
110 version +
111 "\">"
112 "<packages><package hash=\"not-used\" name=\"" +
113 filename + "\" size=\"" + base::Int64ToString(size) +
Sen Jiang0affc2c2017-02-10 15:55:05 -0800114 "\" hash_sha256=\"" + hash + "\"/>" +
115 (multi_package ? "<package name=\"package2\" size=\"222\" "
116 "hash_sha256=\"hash2\"/>"
117 : "") +
118 "</packages>"
Sen Jiang2703ef42017-03-16 13:36:21 -0700119 "<actions><action event=\"postinstall\" "
120 "ChromeOSVersion=\"" +
121 version + "\" MoreInfo=\"" + more_info_url + "\" Prompt=\"" +
122 prompt +
123 "\" "
124 "IsDelta=\"true\" "
125 "IsDeltaPayload=\"true\" "
126 "MaxDaysToScatter=\"" +
127 max_days_to_scatter +
128 "\" "
129 "sha256=\"not-used\" "
130 "needsadmin=\"" +
131 needsadmin + "\" " +
132 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
133 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" "
134 : "") +
135 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
136 "/></actions></manifest></updatecheck></app></response>";
Alex Deymo8e18f932015-03-27 16:16:59 -0700137 }
138
139 // Return the payload URL, which is split in two fields in the XML response.
140 string GetPayloadUrl() {
141 return codebase + filename;
142 }
143
Alex Deymo85616652015-10-15 18:48:31 -0700144 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700145 string version = "1.2.3.4";
146 string more_info_url = "http://more/info";
147 string prompt = "true";
148 string codebase = "http://code/base/";
149 string filename = "file.signed";
Sen Jiang2703ef42017-03-16 13:36:21 -0700150 string hash = "4841534831323334";
Alex Deymo8e18f932015-03-27 16:16:59 -0700151 string needsadmin = "false";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800152 uint64_t size = 123;
Alex Deymo8e18f932015-03-27 16:16:59 -0700153 string deadline = "";
154 string max_days_to_scatter = "7";
155 string elapsed_days = "42";
156
157 // P2P setting defaults to allowed.
158 bool disable_p2p_for_downloading = false;
159 bool disable_p2p_for_sharing = false;
160
161 // Omaha cohorts settings.
162 bool include_cohorts = false;
163 string cohort = "";
164 string cohorthint = "";
165 string cohortname = "";
166
167 // Whether to include the CrOS <!ENTITY> in the XML response.
168 bool include_entity = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800169
170 // Whether to include more than one package.
171 bool multi_package = false;
Alex Deymo8e18f932015-03-27 16:16:59 -0700172};
173
174} // namespace
175
Darin Petkov6a5b3222010-07-13 14:55:28 -0700176namespace chromeos_update_engine {
177
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700178class OmahaRequestActionTest : public ::testing::Test {
179 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800180 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700181 fake_system_state_.set_request_params(&request_params_);
182 fake_system_state_.set_prefs(&fake_prefs_);
183 }
184
185 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800186 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700187 // is used. |payload_state| may be null, in which case a local mock is used.
188 // |p2p_manager| may be null, in which case a local mock is used.
189 // |connection_manager| may be null, in which case a local mock is used.
190 // out_response may be null. If |fail_http_response_code| is non-negative,
191 // the transfer will fail with that code. |ping_only| is passed through to the
192 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
193 // post-data received by the mock HttpFetcher is returned.
194 //
195 // The |expected_check_result|, |expected_check_reaction| and
196 // |expected_error_code| parameters are for checking expectations
197 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
198 // UMA statistics. Use the appropriate ::kUnset value to specify that
199 // the given metric should not be reported.
200 bool TestUpdateCheck(OmahaRequestParams* request_params,
201 const string& http_response,
202 int fail_http_response_code,
203 bool ping_only,
204 ErrorCode expected_code,
205 metrics::CheckResult expected_check_result,
206 metrics::CheckReaction expected_check_reaction,
207 metrics::DownloadErrorCode expected_download_error_code,
208 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700209 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700210
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800211 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700212 // only a ping or also an updatecheck.
213 void PingTest(bool ping_only);
214
215 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700216 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700217 OmahaResponse *response);
218
219 // P2P test helper function.
220 void P2PTest(
221 bool initial_allow_p2p_for_downloading,
222 bool initial_allow_p2p_for_sharing,
223 bool omaha_disable_p2p_for_downloading,
224 bool omaha_disable_p2p_for_sharing,
225 bool payload_state_allow_p2p_attempt,
226 bool expect_p2p_client_lookup,
227 const string& p2p_client_result_url,
228 bool expected_allow_p2p_for_downloading,
229 bool expected_allow_p2p_for_sharing,
230 const string& expected_p2p_url);
231
232 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700233 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700234
235 // By default, all tests use these objects unless they replace them in the
236 // fake_system_state_.
237 OmahaRequestParams request_params_ = OmahaRequestParams{
238 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700239 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700240 OmahaRequestParams::kOsVersion,
241 "service_pack",
242 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700243 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700244 "0.1.0.0",
245 "en-US",
246 "unittest",
247 "OEM MODEL 09235 7471",
248 "ChromeOSFirmware.1.0",
249 "0X0A1",
250 false, // delta okay
251 false, // interactive
252 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700253 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700254
255 FakePrefs fake_prefs_;
256};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700257
258namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700259class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
260 public:
261 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700262 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800263 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700264 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800265 void ProcessingDone(const ActionProcessor* processor,
266 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700267 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700268 }
269
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800270 void ActionCompleted(ActionProcessor* processor,
271 AbstractAction* action,
272 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700273 // make sure actions always succeed
274 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700275 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700276 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700277 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700278 }
David Zeuthena99981f2013-04-29 13:42:47 -0700279 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700280};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700281} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700282
283class OutputObjectCollectorAction;
284
285template<>
286class ActionTraits<OutputObjectCollectorAction> {
287 public:
288 // Does not take an object for input
289 typedef OmahaResponse InputObjectType;
290 // On success, puts the output path on output
291 typedef NoneType OutputObjectType;
292};
293
294class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
295 public:
296 OutputObjectCollectorAction() : has_input_object_(false) {}
297 void PerformAction() {
298 // copy input object
299 has_input_object_ = HasInputObject();
300 if (has_input_object_)
301 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700302 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700303 }
304 // Should never be called
305 void TerminateProcessing() {
306 CHECK(false);
307 }
308 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700309 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700310 return "OutputObjectCollectorAction";
311 }
Alex Deymof329b932014-10-30 01:37:48 -0700312 string Type() const { return StaticType(); }
Alex Vakulenko0057daa2016-01-23 16:22:50 -0800313 using InputObjectType =
314 ActionTraits<OutputObjectCollectorAction>::InputObjectType;
315 using OutputObjectType =
316 ActionTraits<OutputObjectCollectorAction>::OutputObjectType;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700317 bool has_input_object_;
318 OmahaResponse omaha_response_;
319};
320
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700321bool OmahaRequestActionTest::TestUpdateCheck(
322 OmahaRequestParams* request_params,
323 const string& http_response,
324 int fail_http_response_code,
325 bool ping_only,
326 ErrorCode expected_code,
327 metrics::CheckResult expected_check_result,
328 metrics::CheckReaction expected_check_reaction,
329 metrics::DownloadErrorCode expected_download_error_code,
330 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700331 brillo::Blob* out_post_data) {
332 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700333 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700334 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800335 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700336 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700337 if (fail_http_response_code >= 0) {
338 fetcher->FailTransfer(fail_http_response_code);
339 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700340 if (request_params)
341 fake_system_state_.set_request_params(request_params);
342 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700343 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300344 brillo::make_unique_ptr(fetcher),
Darin Petkov265f2902011-05-09 15:17:40 -0700345 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700346 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700347 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700348
Darin Petkov6a5b3222010-07-13 14:55:28 -0700349 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700350 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700351 processor.EnqueueAction(&action);
352
353 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700354 BondActions(&action, &collector_action);
355 processor.EnqueueAction(&collector_action);
356
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700357 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800358 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700359 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800360 SendEnumToUMA(metrics::kMetricCheckResult,
361 static_cast<int>(expected_check_result),
362 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
363 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700364 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800365 SendEnumToUMA(metrics::kMetricCheckReaction,
366 static_cast<int>(expected_check_reaction),
367 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
368 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700369 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700370 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
371 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800372 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
373 ? 0 : 1);
374
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700375 loop.PostTask(base::Bind(
376 [](ActionProcessor* processor) { processor->StartProcessing(); },
377 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700378 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700379 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700380 if (collector_action.has_input_object_ && out_response)
381 *out_response = collector_action.omaha_response_;
382 if (out_post_data)
383 *out_post_data = fetcher->post_data();
384 return collector_action.has_input_object_;
385}
386
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700387// Tests Event requests -- they should always succeed. |out_post_data|
388// may be null; if non-null, the post-data received by the mock
389// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700390void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700391 OmahaEvent* event,
392 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700393 brillo::Blob* out_post_data) {
394 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700395 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700396 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800397 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700398 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700399 FakeSystemState fake_system_state;
400 fake_system_state.set_request_params(&params);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300401 OmahaRequestAction action(&fake_system_state,
402 event,
403 brillo::make_unique_ptr(fetcher),
404 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700405 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700406 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700407 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700408 processor.EnqueueAction(&action);
409
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700410 loop.PostTask(base::Bind(
411 [](ActionProcessor* processor) { processor->StartProcessing(); },
412 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700413 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700414 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700415
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700416 if (out_post_data)
417 *out_post_data = fetcher->post_data();
418}
419
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700420TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400421 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700422 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400423 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700424 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700425 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400426 -1,
427 false, // ping_only
428 ErrorCode::kOmahaRequestXMLHasEntityDecl,
429 metrics::CheckResult::kParsingError,
430 metrics::CheckReaction::kUnset,
431 metrics::DownloadErrorCode::kUnset,
432 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700433 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400434 EXPECT_FALSE(response.update_exists);
435}
436
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700437TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700438 OmahaResponse response;
439 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700440 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700441 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700442 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700443 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700444 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800445 metrics::CheckResult::kNoUpdateAvailable,
446 metrics::CheckReaction::kUnset,
447 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700448 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700449 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700450 EXPECT_FALSE(response.update_exists);
451}
452
Alex Deymo8e18f932015-03-27 16:16:59 -0700453// Test that all the values in the response are parsed in a normal update
454// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700455TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700456 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700457 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700458 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700459 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700460 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700461 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700462 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700463 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800464 metrics::CheckResult::kUpdateAvailable,
465 metrics::CheckReaction::kUpdating,
466 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700467 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700468 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700469 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700470 EXPECT_EQ(fake_update_response_.version, response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800471 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
472 response.packages[0].payload_urls[0]);
Alex Deymo8e18f932015-03-27 16:16:59 -0700473 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800474 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
475 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Alex Deymo8e18f932015-03-27 16:16:59 -0700476 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
477 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
478 // Omaha cohort attribets are not set in the response, so they should not be
479 // persisted.
480 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
481 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
482 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700483}
484
Sen Jiang0affc2c2017-02-10 15:55:05 -0800485TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) {
486 OmahaResponse response;
487 fake_update_response_.multi_package = true;
488 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
489 fake_update_response_.GetUpdateResponse(),
490 -1,
491 false, // ping_only
492 ErrorCode::kSuccess,
493 metrics::CheckResult::kUpdateAvailable,
494 metrics::CheckReaction::kUpdating,
495 metrics::DownloadErrorCode::kUnset,
496 &response,
497 nullptr));
498 EXPECT_TRUE(response.update_exists);
499 EXPECT_EQ(fake_update_response_.version, response.version);
500 EXPECT_EQ(fake_update_response_.GetPayloadUrl(),
501 response.packages[0].payload_urls[0]);
502 EXPECT_EQ(fake_update_response_.codebase + "package2",
503 response.packages[1].payload_urls[0]);
504 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash);
505 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
506 ASSERT_EQ(2u, response.packages.size());
507 EXPECT_EQ(222u, response.packages[1].size);
508}
509
Alex Deymo14ad88e2016-06-29 12:30:14 -0700510TEST_F(OmahaRequestActionTest, ExtraHeadersSentTest) {
511 const string http_response = "<?xml invalid response";
512 request_params_.set_interactive(true);
513
514 brillo::FakeMessageLoop loop(nullptr);
515 loop.SetAsCurrent();
516
517 MockHttpFetcher* fetcher =
518 new MockHttpFetcher(http_response.data(), http_response.size(), nullptr);
519 OmahaRequestAction action(
520 &fake_system_state_, nullptr, brillo::make_unique_ptr(fetcher), false);
521 ActionProcessor processor;
522 processor.EnqueueAction(&action);
523
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700524 loop.PostTask(base::Bind(
525 [](ActionProcessor* processor) { processor->StartProcessing(); },
526 base::Unretained(&processor)));
Alex Deymo14ad88e2016-06-29 12:30:14 -0700527 loop.Run();
528 EXPECT_FALSE(loop.PendingTasks());
529
530 // Check that the headers were set in the fetcher during the action. Note that
531 // we set this request as "interactive".
532 EXPECT_EQ("fg", fetcher->GetHeader("X-GoogleUpdate-Interactivity"));
533 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-GoogleUpdate-AppId"));
534 EXPECT_NE("", fetcher->GetHeader("X-GoogleUpdate-Updater"));
535}
536
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700537TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700538 OmahaResponse response;
539 // Set up a connection manager that doesn't allow a valid update over
540 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700541 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700542 fake_system_state_.set_connection_manager(&mock_cm);
543
Alex Deymo30534502015-07-20 15:06:33 -0700544 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
545 .WillRepeatedly(
Sen Jiang255e22b2016-05-20 16:15:29 -0700546 DoAll(SetArgumentPointee<0>(ConnectionType::kEthernet),
547 SetArgumentPointee<1>(ConnectionTethering::kUnknown),
Alex Deymo30534502015-07-20 15:06:33 -0700548 Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700549 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _))
550 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700551
552 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700553 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700554 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700555 -1,
556 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700557 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700558 metrics::CheckResult::kUpdateAvailable,
559 metrics::CheckReaction::kIgnored,
560 metrics::DownloadErrorCode::kUnset,
561 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700562 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700563 EXPECT_FALSE(response.update_exists);
564}
565
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700566TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700567 string rollback_version = "1234.0.0";
568 OmahaResponse response;
569
570 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700571 fake_system_state_.set_payload_state(&mock_payload_state);
572
Chris Sosa77f79e82014-06-02 18:16:24 -0700573 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
574 .WillRepeatedly(Return(rollback_version));
575
Alex Deymo8e18f932015-03-27 16:16:59 -0700576 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700577 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700578 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700579 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700580 -1,
581 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700582 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700583 metrics::CheckResult::kUpdateAvailable,
584 metrics::CheckReaction::kIgnored,
585 metrics::DownloadErrorCode::kUnset,
586 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700587 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700588 EXPECT_FALSE(response.update_exists);
589}
590
Kevin Cernekee2494e282016-03-29 18:03:53 -0700591// Verify that update checks called during OOBE will only try to download
592// an update if the response includes a non-empty deadline field.
593TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
594 OmahaResponse response;
595
596 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
597 ASSERT_FALSE(
598 TestUpdateCheck(nullptr, // request_params
599 fake_update_response_.GetUpdateResponse(),
600 -1,
601 false, // ping_only
602 ErrorCode::kNonCriticalUpdateInOOBE,
603 metrics::CheckResult::kUnset,
604 metrics::CheckReaction::kUnset,
605 metrics::DownloadErrorCode::kUnset,
606 &response,
607 nullptr));
608 EXPECT_FALSE(response.update_exists);
609
Alex Deymo46a9aae2016-05-04 20:20:11 -0700610 // The IsOOBEComplete() value is ignored when the OOBE flow is not enabled.
611 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
612 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
613 fake_update_response_.GetUpdateResponse(),
614 -1,
615 false, // ping_only
616 ErrorCode::kSuccess,
617 metrics::CheckResult::kUpdateAvailable,
618 metrics::CheckReaction::kUpdating,
619 metrics::DownloadErrorCode::kUnset,
620 &response,
621 nullptr));
622 EXPECT_TRUE(response.update_exists);
623 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
624
625 // The payload is applied when a deadline was set in the response.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700626 fake_update_response_.deadline = "20101020";
627 ASSERT_TRUE(
628 TestUpdateCheck(nullptr, // request_params
629 fake_update_response_.GetUpdateResponse(),
630 -1,
631 false, // ping_only
632 ErrorCode::kSuccess,
633 metrics::CheckResult::kUpdateAvailable,
634 metrics::CheckReaction::kUpdating,
635 metrics::DownloadErrorCode::kUnset,
636 &response,
637 nullptr));
638 EXPECT_TRUE(response.update_exists);
639}
640
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700641TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700642 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700643 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700644 params.set_wall_clock_based_wait_enabled(true);
645 params.set_update_check_count_wait_enabled(false);
646 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700647
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700648 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700649 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700650 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700651 -1,
652 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700653 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800654 metrics::CheckResult::kUpdateAvailable,
655 metrics::CheckReaction::kDeferring,
656 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700657 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700658 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700659 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700660
661 // Verify if we are interactive check we don't defer.
662 params.set_interactive(true);
663 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700664 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700665 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700666 -1,
667 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700668 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800669 metrics::CheckResult::kUpdateAvailable,
670 metrics::CheckReaction::kUpdating,
671 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700672 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700673 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700674 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700675}
676
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700677TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700678 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700679 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700680 params.set_wall_clock_based_wait_enabled(false);
681 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700682
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700683 params.set_update_check_count_wait_enabled(true);
684 params.set_min_update_checks_needed(1);
685 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700686
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700687 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700688 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700689 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700690 -1,
691 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700692 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800693 metrics::CheckResult::kUpdateAvailable,
694 metrics::CheckReaction::kUpdating,
695 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700696 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700697 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700698 EXPECT_TRUE(response.update_exists);
699}
700
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700701TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700702 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700703 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700704 params.set_wall_clock_based_wait_enabled(true);
705 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700706
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700707 params.set_update_check_count_wait_enabled(true);
708 params.set_min_update_checks_needed(1);
709 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700710
Alex Deymo8e18f932015-03-27 16:16:59 -0700711 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700712 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700713 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700714 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700715 -1,
716 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700717 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800718 metrics::CheckResult::kUpdateAvailable,
719 metrics::CheckReaction::kUpdating,
720 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700721 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700722 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700723 EXPECT_TRUE(response.update_exists);
724}
725
726
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700727TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700728 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700729 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700730 params.set_wall_clock_based_wait_enabled(true);
731 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700732
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700733 params.set_update_check_count_wait_enabled(true);
734 params.set_min_update_checks_needed(0);
735 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700736
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700737 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700738 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700739 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700740 -1,
741 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700742 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800743 metrics::CheckResult::kUpdateAvailable,
744 metrics::CheckReaction::kUpdating,
745 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700746 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700747 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700748
Ben Chan9abb7632014-08-07 00:10:53 -0700749 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700750 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700751 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700752 EXPECT_TRUE(response.update_exists);
753}
754
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700755TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700756 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700757 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700758 params.set_wall_clock_based_wait_enabled(true);
759 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700760
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700761 params.set_update_check_count_wait_enabled(true);
762 params.set_min_update_checks_needed(1);
763 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700764
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700765 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700766 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700767 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700768 -1,
769 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700770 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800771 metrics::CheckResult::kUpdateAvailable,
772 metrics::CheckReaction::kDeferring,
773 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700774 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700775 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700776
Ben Chan9abb7632014-08-07 00:10:53 -0700777 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700778 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700779 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700780 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700781
782 // Verify if we are interactive check we don't defer.
783 params.set_interactive(true);
784 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700785 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700786 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700787 -1,
788 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700789 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800790 metrics::CheckResult::kUpdateAvailable,
791 metrics::CheckReaction::kUpdating,
792 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700793 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700794 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700795 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700796}
797
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700798TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700799 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700800 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700801 params.set_wall_clock_based_wait_enabled(true);
802 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700803
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700804 params.set_update_check_count_wait_enabled(true);
805 params.set_min_update_checks_needed(1);
806 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700807
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700808 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700809
810 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700811 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700812 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700813 -1,
814 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700815 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800816 metrics::CheckResult::kUpdateAvailable,
817 metrics::CheckReaction::kDeferring,
818 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700819 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700820 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700821
Ben Chan9abb7632014-08-07 00:10:53 -0700822 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700823 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700824 // count remains the same, as the decrementing happens in update_attempter
825 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700826 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700827 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700828
829 // Verify if we are interactive check we don't defer.
830 params.set_interactive(true);
831 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700832 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700833 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700834 -1,
835 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700836 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800837 metrics::CheckResult::kUpdateAvailable,
838 metrics::CheckReaction::kUpdating,
839 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700840 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700841 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700842 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700843}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700844
Alex Deymo8e18f932015-03-27 16:16:59 -0700845TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
846 OmahaResponse response;
847 OmahaRequestParams params = request_params_;
848 fake_update_response_.include_cohorts = true;
849 fake_update_response_.cohort = "s/154454/8479665";
850 fake_update_response_.cohorthint = "please-put-me-on-beta";
851 fake_update_response_.cohortname = "stable";
852
853 ASSERT_TRUE(TestUpdateCheck(&params,
854 fake_update_response_.GetUpdateResponse(),
855 -1,
856 false, // ping_only
857 ErrorCode::kSuccess,
858 metrics::CheckResult::kUpdateAvailable,
859 metrics::CheckReaction::kUpdating,
860 metrics::DownloadErrorCode::kUnset,
861 &response,
862 nullptr));
863
864 string value;
865 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
866 EXPECT_EQ(fake_update_response_.cohort, value);
867
868 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
869 EXPECT_EQ(fake_update_response_.cohorthint, value);
870
871 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
872 EXPECT_EQ(fake_update_response_.cohortname, value);
873}
874
875TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
876 OmahaResponse response;
877 OmahaRequestParams params = request_params_;
878 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
879 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
880 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
881 fake_update_response_.include_cohorts = true;
882 fake_update_response_.cohort = "s/154454/8479665";
883 fake_update_response_.cohorthint = "please-put-me-on-beta";
884 fake_update_response_.cohortname = "";
885
886 ASSERT_TRUE(TestUpdateCheck(&params,
887 fake_update_response_.GetUpdateResponse(),
888 -1,
889 false, // ping_only
890 ErrorCode::kSuccess,
891 metrics::CheckResult::kUpdateAvailable,
892 metrics::CheckReaction::kUpdating,
893 metrics::DownloadErrorCode::kUnset,
894 &response,
895 nullptr));
896
897 string value;
898 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
899 EXPECT_EQ(fake_update_response_.cohort, value);
900
901 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
902 EXPECT_EQ(fake_update_response_.cohorthint, value);
903
904 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
905}
906
907TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
908 OmahaResponse response;
909 OmahaRequestParams params = request_params_;
910 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
911
912 ASSERT_TRUE(TestUpdateCheck(&params,
913 fake_update_response_.GetUpdateResponse(),
914 -1,
915 false, // ping_only
916 ErrorCode::kSuccess,
917 metrics::CheckResult::kUpdateAvailable,
918 metrics::CheckReaction::kUpdating,
919 metrics::DownloadErrorCode::kUnset,
920 &response,
921 nullptr));
922
923 string value;
924 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
925 EXPECT_EQ("old_value", value);
926
927 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
928 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
929}
930
Alex Deymo00d79ac2015-06-29 15:41:49 -0700931TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
932 OmahaResponse response;
933 OmahaRequestParams params = request_params_;
934 fake_update_response_.include_cohorts = true;
935 fake_update_response_.cohort = "s/154454/8479665";
936 fake_update_response_.cohorthint = "please-put-me-on-beta";
937 fake_update_response_.cohortname = "stable";
938
939 ASSERT_TRUE(TestUpdateCheck(&params,
940 fake_update_response_.GetNoUpdateResponse(),
941 -1,
942 false, // ping_only
943 ErrorCode::kSuccess,
944 metrics::CheckResult::kNoUpdateAvailable,
945 metrics::CheckReaction::kUnset,
946 metrics::DownloadErrorCode::kUnset,
947 &response,
948 nullptr));
949
950 string value;
951 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
952 EXPECT_EQ(fake_update_response_.cohort, value);
953
954 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
955 EXPECT_EQ(fake_update_response_.cohorthint, value);
956
957 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
958 EXPECT_EQ(fake_update_response_.cohortname, value);
959}
960
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700961TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -0700962 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700963
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700964 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700965 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700966
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700967 OmahaRequestParams params = request_params_;
968 fake_system_state_.set_request_params(&params);
969 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300970 brillo::make_unique_ptr(
971 new MockHttpFetcher(http_response.data(),
972 http_response.size(),
973 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -0700974 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700975 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700976 ActionProcessor processor;
977 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700978 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700979
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700980 loop.PostTask(base::Bind(
981 [](ActionProcessor* processor) { processor->StartProcessing(); },
982 base::Unretained(&processor)));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700983 loop.Run();
984 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700985 EXPECT_FALSE(processor.IsRunning());
986}
987
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700988TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700989 OmahaResponse response;
990 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700991 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700992 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700993 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700994 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700995 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800996 metrics::CheckResult::kParsingError,
997 metrics::CheckReaction::kUnset,
998 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700999 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001000 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001001 EXPECT_FALSE(response.update_exists);
1002}
1003
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001004TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001005 OmahaResponse response;
1006 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001007 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001008 "",
1009 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001010 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001011 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001012 metrics::CheckResult::kParsingError,
1013 metrics::CheckReaction::kUnset,
1014 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001015 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001016 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001017 EXPECT_FALSE(response.update_exists);
1018}
1019
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001020TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001021 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001022 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001023 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001024 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1025 "<daystart elapsed_seconds=\"100\"/>"
1026 "<app appid=\"foo\" status=\"ok\">"
1027 "<ping status=\"ok\"/>"
1028 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001029 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001030 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001031 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001032 metrics::CheckResult::kParsingError,
1033 metrics::CheckReaction::kUnset,
1034 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001035 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001036 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001037 EXPECT_FALSE(response.update_exists);
1038}
1039
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001040TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001041 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001042 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001043 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001044 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1045 "<daystart elapsed_seconds=\"100\"/>"
1046 "<app appid=\"foo\" status=\"ok\">"
1047 "<ping status=\"ok\"/>"
1048 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001049 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001050 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001051 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001052 metrics::CheckResult::kParsingError,
1053 metrics::CheckReaction::kUnset,
1054 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001055 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001056 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001057 EXPECT_FALSE(response.update_exists);
1058}
1059
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001060TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001061 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001062 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001063 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001064 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1065 "<daystart elapsed_seconds=\"100\"/>"
1066 "<app appid=\"foo\" status=\"ok\">"
1067 "<ping status=\"ok\"/>"
1068 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001069 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001070 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001071 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001072 metrics::CheckResult::kParsingError,
1073 metrics::CheckReaction::kUnset,
1074 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001075 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001076 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001077 EXPECT_FALSE(response.update_exists);
1078}
1079
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001080TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001081 string input_response =
1082 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1083 "<daystart elapsed_seconds=\"100\"/>"
1084 "<app appid=\"xyz\" status=\"ok\">"
1085 "<updatecheck status=\"ok\">"
1086 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001087 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001088 "<packages><package hash=\"not-used\" name=\"f\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001089 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001090 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001091 "ChromeOSVersion=\"10.2.3.4\" "
1092 "Prompt=\"false\" "
1093 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001094 "IsDeltaPayload=\"false\" "
Sen Jiang2703ef42017-03-16 13:36:21 -07001095 "sha256=\"not-used\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001096 "needsadmin=\"true\" "
1097 "/></actions></manifest></updatecheck></app></response>";
1098 LOG(INFO) << "Input Response = " << input_response;
1099
Darin Petkov6a5b3222010-07-13 14:55:28 -07001100 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001101 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001102 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -07001103 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001104 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001105 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001106 metrics::CheckResult::kUpdateAvailable,
1107 metrics::CheckReaction::kUpdating,
1108 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001109 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001110 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001111 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001112 EXPECT_EQ("10.2.3.4", response.version);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001113 EXPECT_EQ("http://missing/field/test/f",
1114 response.packages[0].payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001115 EXPECT_EQ("", response.more_info_url);
Sen Jiang0affc2c2017-02-10 15:55:05 -08001116 EXPECT_EQ("lkq34j5345", response.packages[0].hash);
1117 EXPECT_EQ(587u, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001118 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001119 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001120}
1121
1122namespace {
1123class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1124 public:
1125 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001126 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001127 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001128};
1129
Alex Deymo60ca1a72015-06-18 18:19:15 -07001130void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001131 processor->StartProcessing();
1132 CHECK(processor->IsRunning());
1133 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001134}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001135} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001136
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001137TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001138 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001139 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001140
Alex Deymo60ca1a72015-06-18 18:19:15 -07001141 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001142 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001143 brillo::make_unique_ptr(
1144 new MockHttpFetcher(http_response.data(),
1145 http_response.size(),
1146 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001147 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001148 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001149 ActionProcessor processor;
1150 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001151 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001152
Alex Deymo60ca1a72015-06-18 18:19:15 -07001153 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1154 loop.Run();
1155 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001156}
1157
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001158TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001159 string output;
1160 EXPECT_TRUE(XmlEncode("ab", &output));
1161 EXPECT_EQ("ab", output);
1162 EXPECT_TRUE(XmlEncode("a<b", &output));
1163 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001164 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1165 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001166 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1167 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001168 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001169 EXPECT_FALSE(XmlEncode("\xc2", &output));
1170 // Fail with invalid ASCII-7 chars.
1171 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1172}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001173
Alex Deymob0d74eb2015-03-30 17:59:17 -07001174TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1175 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1176 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1177}
1178
1179TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001180 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001181
1182 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001183 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001184 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001185 OmahaRequestParams::kOsVersion,
1186 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001187 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001188 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001189 "0.1.0.0",
1190 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001191 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001192 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001193 "ChromeOSFirmware.1.0",
1194 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001195 false, // delta okay
1196 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001197 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001198 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001199 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1200 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1201 fake_prefs_.SetString(kPrefsOmahaCohortName,
Alex Vakulenko0103c362016-01-20 07:56:15 -08001202 base::JoinString(
1203 vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001204 OmahaResponse response;
1205 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001206 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001207 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001208 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001209 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001210 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001211 metrics::CheckResult::kParsingError,
1212 metrics::CheckReaction::kUnset,
1213 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001214 &response,
1215 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001216 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001217 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001218 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1219 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1220 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1221 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1222 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1223 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1224 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1225 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1226 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1227 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1228 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1229 // Values from Prefs that are too big are removed from the XML instead of
1230 // encoded.
1231 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001232}
1233
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001234TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001235 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001236 fake_update_response_.deadline = "&lt;20110101";
1237 fake_update_response_.more_info_url = "testthe&lt;url";
1238 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001239 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001240 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001241 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001242 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001243 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001244 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001245 metrics::CheckResult::kUpdateAvailable,
1246 metrics::CheckReaction::kUpdating,
1247 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001248 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001249 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001250
Sen Jiang0affc2c2017-02-10 15:55:05 -08001251 EXPECT_EQ("testthe<url", response.more_info_url);
1252 EXPECT_EQ("testthe&codebase/file.signed",
1253 response.packages[0].payload_urls[0]);
1254 EXPECT_EQ("<20110101", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001255}
1256
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001257TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001258 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001259 // overflows int32_t:
Sen Jiang0affc2c2017-02-10 15:55:05 -08001260 fake_update_response_.size = 123123123123123ull;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001261 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001262 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001263 fake_update_response_.GetUpdateResponse(),
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::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001267 metrics::CheckResult::kUpdateAvailable,
1268 metrics::CheckReaction::kUpdating,
1269 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001270 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001271 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001272
Sen Jiang0affc2c2017-02-10 15:55:05 -08001273 EXPECT_EQ(fake_update_response_.size, response.packages[0].size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001274}
1275
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001276TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001277 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001278 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001279 fake_system_state_.set_prefs(&prefs);
1280
Darin Petkov95508da2011-01-05 12:42:29 -08001281 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1282 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001283 // An existing but empty previous version means that we didn't reboot to a new
1284 // update, therefore, no need to update the previous version.
1285 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001286 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001287 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001288 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001289 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001290 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001291 metrics::CheckResult::kParsingError,
1292 metrics::CheckReaction::kUnset,
1293 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001294 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001295 &post_data));
1296 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001297 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001298 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001299 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001300 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001301 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001302 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1303 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001304 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1305 string::npos);
1306 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1307 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001308 // No <event> tag should be sent if we didn't reboot to an update.
1309 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001310}
1311
Jay Srinivasan0a708742012-03-20 11:26:12 -07001312
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001313TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001314 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001315 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001316 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1317 "invalid xml>",
1318 &post_data);
1319 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001320 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001321 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001322 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001323 OmahaEvent::kTypeUpdateDownloadStarted,
1324 OmahaEvent::kResultSuccess);
1325 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001326 EXPECT_EQ(post_str.find("ping"), string::npos);
1327 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001328}
1329
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001330TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001331 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001332 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001333 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1334 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001335 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001336 "invalid xml>",
1337 &post_data);
1338 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001339 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001340 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001341 " <event eventtype=\"%d\" eventresult=\"%d\" "
1342 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001343 OmahaEvent::kTypeDownloadComplete,
1344 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001345 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001346 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001347 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001348}
1349
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001350TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001351 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001352 // Create a copy of the OmahaRequestParams to reuse it later.
1353 OmahaRequestParams params = request_params_;
1354 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001355 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001356 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001357 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001358 brillo::make_unique_ptr(
1359 new MockHttpFetcher(http_response.data(),
1360 http_response.size(),
1361 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001362 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001363 EXPECT_FALSE(update_check_action.IsEvent());
1364
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001365 params = request_params_;
1366 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001367 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001368 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001369 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001370 brillo::make_unique_ptr(
1371 new MockHttpFetcher(http_response.data(),
1372 http_response.size(),
1373 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001374 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001375 EXPECT_TRUE(event_action.IsEvent());
1376}
1377
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001378TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001379 for (int i = 0; i < 2; i++) {
1380 bool delta_okay = i == 1;
1381 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001382 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001383 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001384 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001385 OmahaRequestParams::kOsVersion,
1386 "service_pack",
1387 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001388 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001389 "0.1.0.0",
1390 "en-US",
1391 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001392 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001393 "ChromeOSFirmware.1.0",
1394 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001395 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001396 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001397 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001398 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001399 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001400 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001401 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001402 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001403 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001404 metrics::CheckResult::kParsingError,
1405 metrics::CheckReaction::kUnset,
1406 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001407 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001408 &post_data));
1409 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001410 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001411 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1412 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001413 string::npos)
1414 << "i = " << i;
1415 }
1416}
1417
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001418TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001419 for (int i = 0; i < 2; i++) {
1420 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001421 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001422 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001423 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001424 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001425 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001426 OmahaRequestParams::kOsVersion,
1427 "service_pack",
1428 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001429 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001430 "0.1.0.0",
1431 "en-US",
1432 "unittest_track",
1433 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001434 "ChromeOSFirmware.1.0",
1435 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001436 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001437 interactive,
1438 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001439 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001440 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001441 "invalid xml>",
1442 -1,
1443 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001444 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001445 metrics::CheckResult::kParsingError,
1446 metrics::CheckReaction::kUnset,
1447 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001448 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001449 &post_data));
1450 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001451 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001452 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1453 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001454 string::npos)
1455 << "i = " << i;
1456 }
1457}
1458
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001459TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001460 OmahaEvent default_event;
1461 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1462 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001463 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001464
1465 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1466 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1467 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001468 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001469
1470 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1471 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001472 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001473 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1474 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001475 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001476}
1477
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001478void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001479 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001480 fake_system_state_.set_prefs(&prefs);
1481 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1482 .Times(AnyNumber());
1483 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1484 // Add a few hours to the day difference to test no rounding, etc.
1485 int64_t five_days_ago =
1486 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1487 int64_t six_days_ago =
1488 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1489 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1490 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1491 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1492 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1493 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1494 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001495 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001496 ASSERT_TRUE(
1497 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001498 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001499 -1,
1500 ping_only,
1501 ErrorCode::kSuccess,
1502 metrics::CheckResult::kUnset,
1503 metrics::CheckReaction::kUnset,
1504 metrics::DownloadErrorCode::kUnset,
1505 nullptr,
1506 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001507 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001508 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1509 string::npos);
1510 if (ping_only) {
1511 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1512 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1513 } else {
1514 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1515 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001516 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001517}
1518
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001519TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1520 PingTest(true /* ping_only */);
1521}
1522
1523TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1524 PingTest(false /* ping_only */);
1525}
1526
1527TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001528 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001529 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001530 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1531 .Times(AnyNumber());
1532 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001533 int64_t three_days_ago =
1534 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1535 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001536 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1537 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001538 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1539 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1540 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1541 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001542 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001543 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001544 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001545 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001546 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001547 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001548 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001549 metrics::CheckResult::kNoUpdateAvailable,
1550 metrics::CheckReaction::kUnset,
1551 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001552 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001553 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001554 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001555 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001556 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001557}
1558
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001559TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001560 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001561 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001562 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1563 .Times(AnyNumber());
1564 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001565 int64_t four_days_ago =
1566 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1567 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001568 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1569 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001570 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1571 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1572 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1573 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001574 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001575 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001576 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001577 fake_update_response_.GetNoUpdateResponse(),
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,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001585 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001586 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001587 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001588 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001589}
1590
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001591TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001592 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001593 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001594 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1595 .Times(AnyNumber());
1596 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001597 int64_t one_hour_ago =
1598 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001599 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1600 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001601 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1602 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1603 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1604 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001605 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1606 // send a ping.
1607 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1608 .WillOnce(Return(true));
1609 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1610 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001611 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001612 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001613 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001614 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001615 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001616 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001617 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001618 metrics::CheckResult::kNoUpdateAvailable,
1619 metrics::CheckReaction::kUnset,
1620 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001621 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001622 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001623 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001624 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001625}
1626
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001627TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001628 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001629 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001630 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001631 int64_t now = Time::Now().ToInternalValue();
1632 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1633 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1634 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1635 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1636 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1637 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001638 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001639 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001640 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001641 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001642 -1,
1643 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001644 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001645 metrics::CheckResult::kUnset,
1646 metrics::CheckReaction::kUnset,
1647 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001648 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001649 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08001650 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07001651}
1652
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001653TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001654 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001655 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001656 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1657 .Times(AnyNumber());
1658 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001659 int64_t future =
1660 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001661 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1662 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001663 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1664 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1665 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1666 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1667 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1668 .WillOnce(Return(true));
1669 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1670 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001671 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001672 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001673 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001674 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1675 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001676 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001677 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001678 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001679 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001680 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001681 metrics::CheckResult::kNoUpdateAvailable,
1682 metrics::CheckReaction::kUnset,
1683 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001684 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001685 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001686 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001687 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001688}
1689
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001690TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001691 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001692 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001693 // may fail if it runs for longer than 5 seconds. It shouldn't run
1694 // that long though.
1695 int64_t midnight =
1696 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1697 int64_t midnight_slack =
1698 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001699 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001700 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001701 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1702 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001703 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1704 AllOf(Ge(midnight), Le(midnight_slack))))
1705 .WillOnce(Return(true));
1706 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1707 AllOf(Ge(midnight), Le(midnight_slack))))
1708 .WillOnce(Return(true));
1709 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001710 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001711 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1712 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001713 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001714 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001715 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001716 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001717 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001718 metrics::CheckResult::kNoUpdateAvailable,
1719 metrics::CheckReaction::kUnset,
1720 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001721 nullptr,
1722 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001723}
1724
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001725TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001726 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001727 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001728 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1729 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001730 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1731 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1732 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001733 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001734 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1735 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001736 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001737 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001738 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001739 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001740 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001741 metrics::CheckResult::kNoUpdateAvailable,
1742 metrics::CheckReaction::kUnset,
1743 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001744 nullptr,
1745 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001746}
1747
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001748TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001749 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001750 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001751 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1752 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001753 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1754 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1755 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001756 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001757 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1758 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001759 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001760 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001761 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001762 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001763 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001764 metrics::CheckResult::kNoUpdateAvailable,
1765 metrics::CheckReaction::kUnset,
1766 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001767 nullptr,
1768 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001769}
1770
Alex Deymob3fa53b2016-04-18 19:57:58 -07001771TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1772 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1773 // the "eol" attribute.
1774 ASSERT_TRUE(
1775 TestUpdateCheck(nullptr, // request_params
1776 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1777 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1778 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1779 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/>"
1780 "</app></response>",
1781 -1,
1782 false, // ping_only
1783 ErrorCode::kSuccess,
1784 metrics::CheckResult::kNoUpdateAvailable,
1785 metrics::CheckReaction::kUnset,
1786 metrics::DownloadErrorCode::kUnset,
1787 nullptr,
1788 nullptr));
1789 string eol_pref;
1790 EXPECT_TRUE(
1791 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1792 // Note that the eol="eol" attribute should be ignored and the _eol should be
1793 // used instead.
1794 EXPECT_EQ("security-only", eol_pref);
1795}
1796
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001797TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001798 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001799 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001800 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001801 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001802 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001803 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001804 metrics::CheckResult::kParsingError,
1805 metrics::CheckReaction::kUnset,
1806 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001807 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001808 &post_data));
1809 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001810 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001811 EXPECT_EQ(post_str.find("machineid="), string::npos);
1812 EXPECT_EQ(post_str.find("userid="), string::npos);
1813}
1814
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001815TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001816 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001817 const int http_error_code =
1818 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001819 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001820 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001821 "",
1822 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001823 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001824 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001825 metrics::CheckResult::kDownloadError,
1826 metrics::CheckReaction::kUnset,
1827 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001828 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001829 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001830 EXPECT_FALSE(response.update_exists);
1831}
1832
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001833TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001834 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001835 const int http_error_code =
1836 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001837 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001838 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001839 "",
1840 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001841 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001842 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001843 metrics::CheckResult::kDownloadError,
1844 metrics::CheckReaction::kUnset,
1845 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001846 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001847 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001848 EXPECT_FALSE(response.update_exists);
1849}
1850
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001851TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001852 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001853 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001854 params.set_wall_clock_based_wait_enabled(true);
1855 params.set_waiting_period(TimeDelta().FromDays(1));
1856 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001857
Sen Jiang7c1171e2016-06-23 11:35:40 -07001858 Time arbitrary_date;
1859 Time::FromString("6/4/1989", &arbitrary_date);
1860 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date);
1861 ASSERT_FALSE(TestUpdateCheck(&params,
1862 fake_update_response_.GetUpdateResponse(),
1863 -1,
1864 false, // ping_only
1865 ErrorCode::kOmahaUpdateDeferredPerPolicy,
1866 metrics::CheckResult::kUpdateAvailable,
1867 metrics::CheckReaction::kDeferring,
1868 metrics::DownloadErrorCode::kUnset,
1869 &response,
1870 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001871
Ben Chan9abb7632014-08-07 00:10:53 -07001872 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001873 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Sen Jiang7c1171e2016-06-23 11:35:40 -07001874 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001875 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001876
1877 // Verify if we are interactive check we don't defer.
1878 params.set_interactive(true);
Sen Jiang7c1171e2016-06-23 11:35:40 -07001879 ASSERT_TRUE(TestUpdateCheck(&params,
1880 fake_update_response_.GetUpdateResponse(),
1881 -1,
1882 false, // ping_only
1883 ErrorCode::kSuccess,
1884 metrics::CheckResult::kUpdateAvailable,
1885 metrics::CheckReaction::kUpdating,
1886 metrics::DownloadErrorCode::kUnset,
1887 &response,
1888 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001889 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001890}
1891
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001892TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001893 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001894 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001895 params.set_wall_clock_based_wait_enabled(true);
1896 params.set_waiting_period(TimeDelta().FromDays(1));
1897 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001898
Sen Jiang7c1171e2016-06-23 11:35:40 -07001899 Time t1, t2;
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001900 Time::FromString("1/1/2012", &t1);
Sen Jiang7c1171e2016-06-23 11:35:40 -07001901 Time::FromString("1/3/2012", &t2);
1902 ASSERT_TRUE(
1903 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
1904 fake_system_state_.fake_clock()->SetWallclockTime(t2);
1905 ASSERT_TRUE(TestUpdateCheck(&params,
1906 fake_update_response_.GetUpdateResponse(),
1907 -1,
1908 false, // ping_only
1909 ErrorCode::kSuccess,
1910 metrics::CheckResult::kUpdateAvailable,
1911 metrics::CheckReaction::kUpdating,
1912 metrics::DownloadErrorCode::kUnset,
1913 &response,
1914 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001915
1916 EXPECT_TRUE(response.update_exists);
1917
1918 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07001919 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001920 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001921 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1922}
1923
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001924TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001925 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07001926 base::ScopedTempDir tempdir;
1927 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001928
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001929 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07001930 OmahaRequestParams params(&fake_system_state_);
1931 params.set_root(tempdir.path().value());
1932 params.set_app_id("{22222222-2222-2222-2222-222222222222}");
1933 params.set_app_version("1.2.3.4");
1934 params.set_current_channel("canary-channel");
1935 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
1936 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001937 EXPECT_TRUE(params.to_more_stable_channel());
1938 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001939 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001940 "invalid xml>",
1941 -1,
1942 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001943 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001944 metrics::CheckResult::kParsingError,
1945 metrics::CheckReaction::kUnset,
1946 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001947 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001948 &post_data));
1949 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001950 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001951 EXPECT_NE(string::npos, post_str.find(
1952 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1953 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1954 "track=\"stable-channel\" from_track=\"canary-channel\" "));
1955}
1956
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001957TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001958 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07001959 base::ScopedTempDir tempdir;
1960 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001961
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001962 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07001963 OmahaRequestParams params(&fake_system_state_);
1964 params.set_root(tempdir.path().value());
1965 params.set_app_id("{11111111-1111-1111-1111-111111111111}");
1966 params.set_app_version("5.6.7.8");
1967 params.set_current_channel("stable-channel");
1968 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
1969 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001970 EXPECT_FALSE(params.to_more_stable_channel());
1971 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001972 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001973 "invalid xml>",
1974 -1,
1975 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001976 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001977 metrics::CheckResult::kParsingError,
1978 metrics::CheckReaction::kUnset,
1979 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001980 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001981 &post_data));
1982 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001983 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001984 EXPECT_NE(string::npos, post_str.find(
1985 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1986 "version=\"5.6.7.8\" "
1987 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001988 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001989}
1990
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001991// Checks that the initial ping with a=-1 r=-1 is not send when the device
1992// was powerwashed.
1993TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
1994 fake_prefs_.SetString(kPrefsPreviousVersion, "");
1995
1996 // Flag that the device was powerwashed in the past.
1997 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
1998
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001999 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002000 ASSERT_TRUE(
2001 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002002 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002003 -1,
2004 false, // ping_only
2005 ErrorCode::kSuccess,
2006 metrics::CheckResult::kNoUpdateAvailable,
2007 metrics::CheckReaction::kUnset,
2008 metrics::DownloadErrorCode::kUnset,
2009 nullptr,
2010 &post_data));
2011 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002012 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002013 EXPECT_EQ(string::npos, post_str.find("<ping"));
2014}
2015
Alex Deymo9fded1e2015-11-05 12:31:19 -08002016// Checks that the event 54 is sent on a reboot to a new update.
2017TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2018 // Flag that the device was updated in a previous boot.
2019 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2020
2021 brillo::Blob post_data;
2022 ASSERT_TRUE(
2023 TestUpdateCheck(nullptr, // request_params
2024 fake_update_response_.GetNoUpdateResponse(),
2025 -1,
2026 false, // ping_only
2027 ErrorCode::kSuccess,
2028 metrics::CheckResult::kNoUpdateAvailable,
2029 metrics::CheckReaction::kUnset,
2030 metrics::DownloadErrorCode::kUnset,
2031 nullptr,
2032 &post_data));
2033 string post_str(post_data.begin(), post_data.end());
2034
2035 // An event 54 is included and has the right version.
2036 EXPECT_NE(string::npos,
2037 post_str.find(base::StringPrintf(
2038 "<event eventtype=\"%d\"",
2039 OmahaEvent::kTypeRebootedAfterUpdate)));
2040 EXPECT_NE(string::npos,
2041 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2042
2043 // The previous version flag should have been removed.
2044 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2045 string prev_version;
2046 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2047 EXPECT_TRUE(prev_version.empty());
2048}
2049
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002050void OmahaRequestActionTest::P2PTest(
2051 bool initial_allow_p2p_for_downloading,
2052 bool initial_allow_p2p_for_sharing,
2053 bool omaha_disable_p2p_for_downloading,
2054 bool omaha_disable_p2p_for_sharing,
2055 bool payload_state_allow_p2p_attempt,
2056 bool expect_p2p_client_lookup,
2057 const string& p2p_client_result_url,
2058 bool expected_allow_p2p_for_downloading,
2059 bool expected_allow_p2p_for_sharing,
2060 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07002061 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002062 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07002063 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2064 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2065 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002066
2067 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002068 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002069 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2070 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002071 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2072 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2073 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2074 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2075 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2076 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2077 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2078 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2079 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2080 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2081
David Zeuthen8f191b22013-08-06 12:27:50 -07002082 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002083 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002084 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2085
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002086 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2087 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002088 .Times(expect_p2p_client_lookup ? 1 : 0);
2089
Alex Deymo8e18f932015-03-27 16:16:59 -07002090 fake_update_response_.disable_p2p_for_downloading =
2091 omaha_disable_p2p_for_downloading;
2092 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07002093 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002094 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07002095 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07002096 -1,
2097 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002098 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002099 metrics::CheckResult::kUpdateAvailable,
2100 metrics::CheckReaction::kUpdating,
2101 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07002102 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002103 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07002104 EXPECT_TRUE(response.update_exists);
2105
Gilad Arnold74b5f552014-10-07 08:17:16 -07002106 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2107 response.disable_p2p_for_downloading);
2108 EXPECT_EQ(omaha_disable_p2p_for_sharing,
2109 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002110
Gilad Arnold74b5f552014-10-07 08:17:16 -07002111 EXPECT_EQ(expected_allow_p2p_for_downloading,
2112 actual_allow_p2p_for_downloading);
2113 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2114 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002115}
2116
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002117TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002118 P2PTest(true, // initial_allow_p2p_for_downloading
2119 true, // initial_allow_p2p_for_sharing
2120 false, // omaha_disable_p2p_for_downloading
2121 false, // omaha_disable_p2p_for_sharing
2122 true, // payload_state_allow_p2p_attempt
2123 true, // expect_p2p_client_lookup
2124 "http://1.3.5.7/p2p", // p2p_client_result_url
2125 true, // expected_allow_p2p_for_downloading
2126 true, // expected_allow_p2p_for_sharing
2127 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002128}
2129
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002130TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002131 P2PTest(true, // initial_allow_p2p_for_downloading
2132 true, // initial_allow_p2p_for_sharing
2133 false, // omaha_disable_p2p_for_downloading
2134 false, // omaha_disable_p2p_for_sharing
2135 true, // payload_state_allow_p2p_attempt
2136 true, // expect_p2p_client_lookup
2137 "", // p2p_client_result_url
2138 false, // expected_allow_p2p_for_downloading
2139 true, // expected_allow_p2p_for_sharing
2140 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002141}
2142
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002143TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002144 P2PTest(false, // initial_allow_p2p_for_downloading
2145 true, // initial_allow_p2p_for_sharing
2146 false, // omaha_disable_p2p_for_downloading
2147 false, // omaha_disable_p2p_for_sharing
2148 true, // payload_state_allow_p2p_attempt
2149 false, // expect_p2p_client_lookup
2150 "unset", // p2p_client_result_url
2151 false, // expected_allow_p2p_for_downloading
2152 true, // expected_allow_p2p_for_sharing
2153 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002154}
2155
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002156TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002157 P2PTest(true, // initial_allow_p2p_for_downloading
2158 true, // initial_allow_p2p_for_sharing
2159 true, // omaha_disable_p2p_for_downloading
2160 false, // omaha_disable_p2p_for_sharing
2161 true, // payload_state_allow_p2p_attempt
2162 false, // expect_p2p_client_lookup
2163 "unset", // p2p_client_result_url
2164 false, // expected_allow_p2p_for_downloading
2165 true, // expected_allow_p2p_for_sharing
2166 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002167}
2168
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002169TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002170 P2PTest(true, // initial_allow_p2p_for_downloading
2171 true, // initial_allow_p2p_for_sharing
2172 false, // omaha_disable_p2p_for_downloading
2173 true, // omaha_disable_p2p_for_sharing
2174 true, // payload_state_allow_p2p_attempt
2175 true, // expect_p2p_client_lookup
2176 "http://1.3.5.7/p2p", // p2p_client_result_url
2177 true, // expected_allow_p2p_for_downloading
2178 false, // expected_allow_p2p_for_sharing
2179 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002180}
2181
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002182TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002183 P2PTest(true, // initial_allow_p2p_for_downloading
2184 true, // initial_allow_p2p_for_sharing
2185 true, // omaha_disable_p2p_for_downloading
2186 true, // omaha_disable_p2p_for_sharing
2187 true, // payload_state_allow_p2p_attempt
2188 false, // expect_p2p_client_lookup
2189 "unset", // p2p_client_result_url
2190 false, // expected_allow_p2p_for_downloading
2191 false, // expected_allow_p2p_for_sharing
2192 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002193}
2194
Alex Deymof329b932014-10-30 01:37:48 -07002195bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2196 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002197 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002198 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002199 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002200 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002201 -1,
2202 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002203 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002204 metrics::CheckResult::kUpdateAvailable,
2205 metrics::CheckReaction::kUpdating,
2206 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002207 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002208 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002209}
2210
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002211TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002212 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002213
Kevin Cernekee2494e282016-03-29 18:03:53 -07002214 // Simulate a successful update check that happens during OOBE. The
2215 // deadline in the response is needed to force the update attempt to
2216 // occur; responses without a deadline seen during OOBE will normally
2217 // return ErrorCode::kNonCriticalUpdateInOOBE.
2218 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2219 fake_update_response_.deadline = "20101020";
2220
David Zeuthen639aa362014-02-03 16:23:44 -08002221 // Check that we parse elapsed_days in the Omaha Response correctly.
2222 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002223 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2224 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002225 EXPECT_TRUE(response.update_exists);
2226 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002227 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002228 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002229 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002230 EXPECT_EQ(prefs_days, 42);
2231
2232 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002233 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002234 EXPECT_TRUE(response.update_exists);
2235 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002236 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002237 EXPECT_EQ(prefs_days, 42);
2238
2239 // Note that elapsed_days is not necessarily divisible by 7 so check
2240 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002241 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2242 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002243 EXPECT_TRUE(response.update_exists);
2244 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002245 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002246 EXPECT_EQ(prefs_days, 21);
2247
2248 // Check that we correctly handle elapsed_days not being included in
2249 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002250 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002251 EXPECT_TRUE(response.update_exists);
2252 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002253}
2254
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002255// If there is no prefs and OOBE is not complete, we should not
2256// report anything to Omaha.
2257TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002258 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002259 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2260 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2261}
David Zeuthen639aa362014-02-03 16:23:44 -08002262
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002263// If OOBE is complete and happened on a valid date (e.g. after Jan
2264// 1 2007 0:00 PST), that date should be used and written to
2265// prefs. However, first try with an invalid date and check we do
2266// nothing.
2267TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2268 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2269 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2270 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2271 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2272}
David Zeuthen639aa362014-02-03 16:23:44 -08002273
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002274// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2275// should yield an InstallDate of 14.
2276TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2277 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2278 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2279 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2280 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002281
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002282 int64_t prefs_days;
2283 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2284 EXPECT_EQ(prefs_days, 14);
2285}
David Zeuthen639aa362014-02-03 16:23:44 -08002286
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002287// Now that we have a valid date in prefs, check that we keep using
2288// that even if OOBE date reports something else. The date Jan 30,
2289// 2007 0:00 PST should yield an InstallDate of 28... but since
2290// there's a prefs file, we should still get 14.
2291TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2292 // Set a valid date in the prefs first.
2293 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002294
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002295 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2296 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2297 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002298
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002299 int64_t prefs_days;
2300 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2301 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002302
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002303 // If we delete the prefs file, we should get 28 days.
2304 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2305 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2306 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2307 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002308}
2309
Darin Petkov6a5b3222010-07-13 14:55:28 -07002310} // namespace chromeos_update_engine