blob: 6c24f8609890817a54e00f578184011ab74a86c3 [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 {
92 return
93 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
94 "protocol=\"3.0\">"
95 "<daystart elapsed_seconds=\"100\"" +
96 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\""))
97 + "/>"
98 "<app appid=\"" + app_id + "\" " +
99 (include_cohorts ? "cohort=\"" + cohort + "\" cohorthint=\"" +
100 cohorthint + "\" cohortname=\"" + cohortname + "\" " : "") +
101 " status=\"ok\">"
102 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
103 "<urls><url codebase=\"" + codebase + "\"/></urls>"
104 "<manifest version=\"" + version + "\">"
105 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
106 "size=\"" + base::Int64ToString(size) + "\"/></packages>"
107 "<actions><action event=\"postinstall\" "
108 "ChromeOSVersion=\"" + version + "\" "
109 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
110 "IsDelta=\"true\" "
111 "IsDeltaPayload=\"true\" "
112 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
113 "sha256=\"" + hash + "\" "
114 "needsadmin=\"" + needsadmin + "\" " +
115 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
116 (disable_p2p_for_downloading ?
117 "DisableP2PForDownloading=\"true\" " : "") +
118 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
119 "/></actions></manifest></updatecheck></app></response>";
120 }
121
122 // Return the payload URL, which is split in two fields in the XML response.
123 string GetPayloadUrl() {
124 return codebase + filename;
125 }
126
Alex Deymo85616652015-10-15 18:48:31 -0700127 string app_id = kTestAppId;
Alex Deymo8e18f932015-03-27 16:16:59 -0700128 string version = "1.2.3.4";
129 string more_info_url = "http://more/info";
130 string prompt = "true";
131 string codebase = "http://code/base/";
132 string filename = "file.signed";
133 string hash = "HASH1234=";
134 string needsadmin = "false";
135 int64_t size = 123;
136 string deadline = "";
137 string max_days_to_scatter = "7";
138 string elapsed_days = "42";
139
140 // P2P setting defaults to allowed.
141 bool disable_p2p_for_downloading = false;
142 bool disable_p2p_for_sharing = false;
143
144 // Omaha cohorts settings.
145 bool include_cohorts = false;
146 string cohort = "";
147 string cohorthint = "";
148 string cohortname = "";
149
150 // Whether to include the CrOS <!ENTITY> in the XML response.
151 bool include_entity = false;
152};
153
154} // namespace
155
Darin Petkov6a5b3222010-07-13 14:55:28 -0700156namespace chromeos_update_engine {
157
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700158class OmahaRequestActionTest : public ::testing::Test {
159 protected:
Alex Deymo610277e2014-11-11 21:18:11 -0800160 void SetUp() override {
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700161 fake_system_state_.set_request_params(&request_params_);
162 fake_system_state_.set_prefs(&fake_prefs_);
163 }
164
165 // Returns true iff an output response was obtained from the
Alex Deymo8427b4a2014-11-05 14:00:32 -0800166 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700167 // is used. |payload_state| may be null, in which case a local mock is used.
168 // |p2p_manager| may be null, in which case a local mock is used.
169 // |connection_manager| may be null, in which case a local mock is used.
170 // out_response may be null. If |fail_http_response_code| is non-negative,
171 // the transfer will fail with that code. |ping_only| is passed through to the
172 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the
173 // post-data received by the mock HttpFetcher is returned.
174 //
175 // The |expected_check_result|, |expected_check_reaction| and
176 // |expected_error_code| parameters are for checking expectations
177 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
178 // UMA statistics. Use the appropriate ::kUnset value to specify that
179 // the given metric should not be reported.
180 bool TestUpdateCheck(OmahaRequestParams* request_params,
181 const string& http_response,
182 int fail_http_response_code,
183 bool ping_only,
184 ErrorCode expected_code,
185 metrics::CheckResult expected_check_result,
186 metrics::CheckReaction expected_check_reaction,
187 metrics::DownloadErrorCode expected_download_error_code,
188 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700189 brillo::Blob* out_post_data);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700190
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800191 // Runs and checks a ping test. |ping_only| indicates whether it should send
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700192 // only a ping or also an updatecheck.
193 void PingTest(bool ping_only);
194
195 // InstallDate test helper function.
Alex Deymof329b932014-10-30 01:37:48 -0700196 bool InstallDateParseHelper(const string &elapsed_days,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700197 OmahaResponse *response);
198
199 // P2P test helper function.
200 void P2PTest(
201 bool initial_allow_p2p_for_downloading,
202 bool initial_allow_p2p_for_sharing,
203 bool omaha_disable_p2p_for_downloading,
204 bool omaha_disable_p2p_for_sharing,
205 bool payload_state_allow_p2p_attempt,
206 bool expect_p2p_client_lookup,
207 const string& p2p_client_result_url,
208 bool expected_allow_p2p_for_downloading,
209 bool expected_allow_p2p_for_sharing,
210 const string& expected_p2p_url);
211
212 FakeSystemState fake_system_state_;
Alex Deymo8e18f932015-03-27 16:16:59 -0700213 FakeUpdateResponse fake_update_response_;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700214
215 // By default, all tests use these objects unless they replace them in the
216 // fake_system_state_.
217 OmahaRequestParams request_params_ = OmahaRequestParams{
218 &fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -0700219 constants::kOmahaPlatformName,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700220 OmahaRequestParams::kOsVersion,
221 "service_pack",
222 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -0700223 kTestAppId,
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700224 "0.1.0.0",
225 "en-US",
226 "unittest",
227 "OEM MODEL 09235 7471",
228 "ChromeOSFirmware.1.0",
229 "0X0A1",
230 false, // delta okay
231 false, // interactive
232 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -0700233 ""}; // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700234
235 FakePrefs fake_prefs_;
236};
Darin Petkov6a5b3222010-07-13 14:55:28 -0700237
238namespace {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700239class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
240 public:
241 OmahaRequestActionTestProcessorDelegate()
Alex Deymo60ca1a72015-06-18 18:19:15 -0700242 : expected_code_(ErrorCode::kSuccess) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800243 ~OmahaRequestActionTestProcessorDelegate() override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700244 }
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800245 void ProcessingDone(const ActionProcessor* processor,
246 ErrorCode code) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700247 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700248 }
249
Yunlian Jiang35866ed2015-01-29 13:09:20 -0800250 void ActionCompleted(ActionProcessor* processor,
251 AbstractAction* action,
252 ErrorCode code) override {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700253 // make sure actions always succeed
254 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700255 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700256 else
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700257 EXPECT_EQ(ErrorCode::kSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700258 }
David Zeuthena99981f2013-04-29 13:42:47 -0700259 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700261} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262
263class OutputObjectCollectorAction;
264
265template<>
266class ActionTraits<OutputObjectCollectorAction> {
267 public:
268 // Does not take an object for input
269 typedef OmahaResponse InputObjectType;
270 // On success, puts the output path on output
271 typedef NoneType OutputObjectType;
272};
273
274class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
275 public:
276 OutputObjectCollectorAction() : has_input_object_(false) {}
277 void PerformAction() {
278 // copy input object
279 has_input_object_ = HasInputObject();
280 if (has_input_object_)
281 omaha_response_ = GetInputObject();
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700282 processor_->ActionComplete(this, ErrorCode::kSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700283 }
284 // Should never be called
285 void TerminateProcessing() {
286 CHECK(false);
287 }
288 // Debugging/logging
Alex Deymof329b932014-10-30 01:37:48 -0700289 static string StaticType() {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700290 return "OutputObjectCollectorAction";
291 }
Alex Deymof329b932014-10-30 01:37:48 -0700292 string Type() const { return StaticType(); }
Alex Vakulenko0057daa2016-01-23 16:22:50 -0800293 using InputObjectType =
294 ActionTraits<OutputObjectCollectorAction>::InputObjectType;
295 using OutputObjectType =
296 ActionTraits<OutputObjectCollectorAction>::OutputObjectType;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700297 bool has_input_object_;
298 OmahaResponse omaha_response_;
299};
300
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700301bool OmahaRequestActionTest::TestUpdateCheck(
302 OmahaRequestParams* request_params,
303 const string& http_response,
304 int fail_http_response_code,
305 bool ping_only,
306 ErrorCode expected_code,
307 metrics::CheckResult expected_check_result,
308 metrics::CheckReaction expected_check_reaction,
309 metrics::DownloadErrorCode expected_download_error_code,
310 OmahaResponse* out_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700311 brillo::Blob* out_post_data) {
312 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700313 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700314 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800315 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700316 nullptr);
Darin Petkovedc522e2010-11-05 09:35:17 -0700317 if (fail_http_response_code >= 0) {
318 fetcher->FailTransfer(fail_http_response_code);
319 }
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700320 if (request_params)
321 fake_system_state_.set_request_params(request_params);
322 OmahaRequestAction action(&fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700323 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -0300324 brillo::make_unique_ptr(fetcher),
Darin Petkov265f2902011-05-09 15:17:40 -0700325 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700326 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700327 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700328
Darin Petkov6a5b3222010-07-13 14:55:28 -0700329 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700330 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700331 processor.EnqueueAction(&action);
332
333 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700334 BondActions(&action, &collector_action);
335 processor.EnqueueAction(&collector_action);
336
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700337 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(), SendEnumToUMA(_, _, _))
David Zeuthen33bae492014-02-25 16:16:18 -0800338 .Times(AnyNumber());
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700339 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800340 SendEnumToUMA(metrics::kMetricCheckResult,
341 static_cast<int>(expected_check_result),
342 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
343 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700344 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthen33bae492014-02-25 16:16:18 -0800345 SendEnumToUMA(metrics::kMetricCheckReaction,
346 static_cast<int>(expected_check_reaction),
347 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
348 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700349 EXPECT_CALL(*fake_system_state_.mock_metrics_lib(),
David Zeuthenc0dd0212014-04-04 14:49:49 -0700350 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
351 static_cast<int>(expected_download_error_code)))
David Zeuthen33bae492014-02-25 16:16:18 -0800352 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
353 ? 0 : 1);
354
Alex Deymo60ca1a72015-06-18 18:19:15 -0700355 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700356 loop.Run();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700357 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700358 if (collector_action.has_input_object_ && out_response)
359 *out_response = collector_action.omaha_response_;
360 if (out_post_data)
361 *out_post_data = fetcher->post_data();
362 return collector_action.has_input_object_;
363}
364
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700365// Tests Event requests -- they should always succeed. |out_post_data|
366// may be null; if non-null, the post-data received by the mock
367// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700368void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700369 OmahaEvent* event,
370 const string& http_response,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700371 brillo::Blob* out_post_data) {
372 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700373 loop.SetAsCurrent();
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700374 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800375 http_response.size(),
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700376 nullptr);
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700377 FakeSystemState fake_system_state;
378 fake_system_state.set_request_params(&params);
Alex Deymoc1c17b42015-11-23 03:53:15 -0300379 OmahaRequestAction action(&fake_system_state,
380 event,
381 brillo::make_unique_ptr(fetcher),
382 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700383 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700384 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700385 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700386 processor.EnqueueAction(&action);
387
Alex Deymo60ca1a72015-06-18 18:19:15 -0700388 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
389 loop.Run();
Alex Deymo72a25672016-03-23 15:44:39 -0700390 EXPECT_FALSE(loop.PendingTasks());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700391
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700392 if (out_post_data)
393 *out_post_data = fetcher->post_data();
394}
395
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700396TEST_F(OmahaRequestActionTest, RejectEntities) {
David Zeuthenf3e28012014-08-26 18:23:52 -0400397 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700398 fake_update_response_.include_entity = true;
David Zeuthenf3e28012014-08-26 18:23:52 -0400399 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700400 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700401 fake_update_response_.GetNoUpdateResponse(),
David Zeuthenf3e28012014-08-26 18:23:52 -0400402 -1,
403 false, // ping_only
404 ErrorCode::kOmahaRequestXMLHasEntityDecl,
405 metrics::CheckResult::kParsingError,
406 metrics::CheckReaction::kUnset,
407 metrics::DownloadErrorCode::kUnset,
408 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700409 nullptr));
David Zeuthenf3e28012014-08-26 18:23:52 -0400410 EXPECT_FALSE(response.update_exists);
411}
412
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700413TEST_F(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700414 OmahaResponse response;
415 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700416 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700417 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700418 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700419 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700420 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800421 metrics::CheckResult::kNoUpdateAvailable,
422 metrics::CheckReaction::kUnset,
423 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700424 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700425 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700426 EXPECT_FALSE(response.update_exists);
427}
428
Alex Deymo8e18f932015-03-27 16:16:59 -0700429// Test that all the values in the response are parsed in a normal update
430// response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700431TEST_F(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700432 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -0700433 fake_update_response_.deadline = "20101020";
Darin Petkov6a5b3222010-07-13 14:55:28 -0700434 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700435 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700436 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -0700437 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700438 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700439 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800440 metrics::CheckResult::kUpdateAvailable,
441 metrics::CheckReaction::kUpdating,
442 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700443 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700444 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700445 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700446 EXPECT_TRUE(response.update_exists);
Alex Deymo8e18f932015-03-27 16:16:59 -0700447 EXPECT_EQ(fake_update_response_.version, response.version);
448 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), response.payload_urls[0]);
449 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url);
450 EXPECT_EQ(fake_update_response_.hash, response.hash);
451 EXPECT_EQ(fake_update_response_.size, response.size);
452 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt);
453 EXPECT_EQ(fake_update_response_.deadline, response.deadline);
454 // Omaha cohort attribets are not set in the response, so they should not be
455 // persisted.
456 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort));
457 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint));
458 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700459}
460
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700461TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700462 OmahaResponse response;
463 // Set up a connection manager that doesn't allow a valid update over
464 // the current ethernet connection.
Alex Deymof6ee0162015-07-31 12:35:22 -0700465 MockConnectionManager mock_cm;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700466 fake_system_state_.set_connection_manager(&mock_cm);
467
Alex Deymo30534502015-07-20 15:06:33 -0700468 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
469 .WillRepeatedly(
470 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kEthernet),
471 SetArgumentPointee<1>(NetworkTethering::kUnknown),
472 Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700473 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kEthernet, _))
Chris Sosa77f79e82014-06-02 18:16:24 -0700474 .WillRepeatedly(Return(false));
Chris Sosa77f79e82014-06-02 18:16:24 -0700475
476 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700477 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700478 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700479 -1,
480 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700481 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700482 metrics::CheckResult::kUpdateAvailable,
483 metrics::CheckReaction::kIgnored,
484 metrics::DownloadErrorCode::kUnset,
485 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700486 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700487 EXPECT_FALSE(response.update_exists);
488}
489
Weidong Guo70063d92017-04-17 10:08:38 -0700490TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) {
491 // This test tests that update over cellular is allowed as device policy
492 // says yes.
493 OmahaResponse response;
494 MockConnectionManager mock_cm;
495
496 fake_system_state_.set_connection_manager(&mock_cm);
497
498 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
499 .WillRepeatedly(
500 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kCellular),
501 SetArgumentPointee<1>(NetworkTethering::kUnknown),
502 Return(true)));
503 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
504 .WillRepeatedly(Return(true));
505 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kCellular, _))
506 .WillRepeatedly(Return(true));
507
508 ASSERT_TRUE(
509 TestUpdateCheck(nullptr, // request_params
510 fake_update_response_.GetUpdateResponse(),
511 -1,
512 false, // ping_only
513 ErrorCode::kSuccess,
514 metrics::CheckResult::kUpdateAvailable,
515 metrics::CheckReaction::kUpdating,
516 metrics::DownloadErrorCode::kUnset,
517 &response,
518 nullptr));
519 EXPECT_TRUE(response.update_exists);
520}
521
522TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) {
523 // This test tests that update over cellular is blocked as device policy
524 // says no.
525 OmahaResponse response;
526 MockConnectionManager mock_cm;
527
528 fake_system_state_.set_connection_manager(&mock_cm);
529
530 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
531 .WillRepeatedly(
532 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kCellular),
533 SetArgumentPointee<1>(NetworkTethering::kUnknown),
534 Return(true)));
535 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
536 .WillRepeatedly(Return(true));
537 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kCellular, _))
538 .WillRepeatedly(Return(false));
539
540 ASSERT_FALSE(
541 TestUpdateCheck(nullptr, // request_params
542 fake_update_response_.GetUpdateResponse(),
543 -1,
544 false, // ping_only
545 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
546 metrics::CheckResult::kUpdateAvailable,
547 metrics::CheckReaction::kIgnored,
548 metrics::DownloadErrorCode::kUnset,
549 &response,
550 nullptr));
551 EXPECT_FALSE(response.update_exists);
552}
553
554TEST_F(OmahaRequestActionTest,
555 ValidUpdateOverCellularAllowedByUserPermissionTrue) {
556 // This test tests that, when device policy is not set, update over cellular
557 // is allowed as permission for update over cellular is set to true.
558 OmahaResponse response;
559 MockConnectionManager mock_cm;
560
561 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true);
562 fake_system_state_.set_connection_manager(&mock_cm);
563
564 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
565 .WillRepeatedly(
566 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kCellular),
567 SetArgumentPointee<1>(NetworkTethering::kUnknown),
568 Return(true)));
569 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
570 .WillRepeatedly(Return(false));
571 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kCellular, _))
572 .WillRepeatedly(Return(true));
573
574 ASSERT_TRUE(
575 TestUpdateCheck(nullptr, // request_params
576 fake_update_response_.GetUpdateResponse(),
577 -1,
578 false, // ping_only
579 ErrorCode::kSuccess,
580 metrics::CheckResult::kUpdateAvailable,
581 metrics::CheckReaction::kUpdating,
582 metrics::DownloadErrorCode::kUnset,
583 &response,
584 nullptr));
585 EXPECT_TRUE(response.update_exists);
586}
587
588TEST_F(OmahaRequestActionTest,
589 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) {
590 // This test tests that, when device policy is not set and permission for
591 // update over cellular is set to false or does not exist, update over
592 // cellular is blocked as update target does not match the omaha response.
593 OmahaResponse response;
594 MockConnectionManager mock_cm;
595 // A version different from the version in omaha response.
596 string diff_version = "99.99.99";
597 // A size different from the size in omaha response.
598 int64_t diff_size = 999;
599
600 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version);
601 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size);
602 // This test tests cellular (3G) being the only connection type being allowed.
603 fake_system_state_.set_connection_manager(&mock_cm);
604
605 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
606 .WillRepeatedly(
607 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kCellular),
608 SetArgumentPointee<1>(NetworkTethering::kUnknown),
609 Return(true)));
610 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
611 .WillRepeatedly(Return(false));
612 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kCellular, _))
613 .WillRepeatedly(Return(true));
614
615 ASSERT_FALSE(
616 TestUpdateCheck(nullptr, // request_params
617 fake_update_response_.GetUpdateResponse(),
618 -1,
619 false, // ping_only
620 ErrorCode::kOmahaUpdateIgnoredOverCellular,
621 metrics::CheckResult::kUpdateAvailable,
622 metrics::CheckReaction::kIgnored,
623 metrics::DownloadErrorCode::kUnset,
624 &response,
625 nullptr));
626 EXPECT_FALSE(response.update_exists);
627}
628
629TEST_F(OmahaRequestActionTest,
630 ValidUpdateOverCellularAllowedByUpdateTargetMatch) {
631 // This test tests that, when device policy is not set and permission for
632 // update over cellular is set to false or does not exist, update over
633 // cellular is allowed as update target matches the omaha response.
634 OmahaResponse response;
635 MockConnectionManager mock_cm;
636 // A version same as the version in omaha response.
637 string new_version = fake_update_response_.version;
638 // A size same as the size in omaha response.
639 int64_t new_size = fake_update_response_.size;
640
641 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version);
642 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size);
643 fake_system_state_.set_connection_manager(&mock_cm);
644
645 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _))
646 .WillRepeatedly(
647 DoAll(SetArgumentPointee<0>(NetworkConnectionType::kCellular),
648 SetArgumentPointee<1>(NetworkTethering::kUnknown),
649 Return(true)));
650 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet())
651 .WillRepeatedly(Return(false));
652 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(NetworkConnectionType::kCellular, _))
653 .WillRepeatedly(Return(true));
654
655 ASSERT_TRUE(
656 TestUpdateCheck(nullptr, // request_params
657 fake_update_response_.GetUpdateResponse(),
658 -1,
659 false, // ping_only
660 ErrorCode::kSuccess,
661 metrics::CheckResult::kUpdateAvailable,
662 metrics::CheckReaction::kUpdating,
663 metrics::DownloadErrorCode::kUnset,
664 &response,
665 nullptr));
666 EXPECT_TRUE(response.update_exists);
667}
668
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700669TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) {
Chris Sosa77f79e82014-06-02 18:16:24 -0700670 string rollback_version = "1234.0.0";
671 OmahaResponse response;
672
673 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700674 fake_system_state_.set_payload_state(&mock_payload_state);
675
Chris Sosa77f79e82014-06-02 18:16:24 -0700676 EXPECT_CALL(mock_payload_state, GetRollbackVersion())
677 .WillRepeatedly(Return(rollback_version));
678
Alex Deymo8e18f932015-03-27 16:16:59 -0700679 fake_update_response_.version = rollback_version;
Chris Sosa77f79e82014-06-02 18:16:24 -0700680 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700681 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -0700682 fake_update_response_.GetUpdateResponse(),
Chris Sosa77f79e82014-06-02 18:16:24 -0700683 -1,
684 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700685 ErrorCode::kOmahaUpdateIgnoredPerPolicy,
Chris Sosa77f79e82014-06-02 18:16:24 -0700686 metrics::CheckResult::kUpdateAvailable,
687 metrics::CheckReaction::kIgnored,
688 metrics::DownloadErrorCode::kUnset,
689 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700690 nullptr));
Chris Sosa77f79e82014-06-02 18:16:24 -0700691 EXPECT_FALSE(response.update_exists);
692}
693
Kevin Cernekee2494e282016-03-29 18:03:53 -0700694// Verify that update checks called during OOBE will only try to download
695// an update if the response includes a non-empty deadline field.
696TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) {
697 OmahaResponse response;
698
699 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
700 ASSERT_FALSE(
701 TestUpdateCheck(nullptr, // request_params
702 fake_update_response_.GetUpdateResponse(),
703 -1,
704 false, // ping_only
705 ErrorCode::kNonCriticalUpdateInOOBE,
706 metrics::CheckResult::kUnset,
707 metrics::CheckReaction::kUnset,
708 metrics::DownloadErrorCode::kUnset,
709 &response,
710 nullptr));
711 EXPECT_FALSE(response.update_exists);
712
Alex Deymo46a9aae2016-05-04 20:20:11 -0700713 // The IsOOBEComplete() value is ignored when the OOBE flow is not enabled.
714 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false);
715 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
716 fake_update_response_.GetUpdateResponse(),
717 -1,
718 false, // ping_only
719 ErrorCode::kSuccess,
720 metrics::CheckResult::kUpdateAvailable,
721 metrics::CheckReaction::kUpdating,
722 metrics::DownloadErrorCode::kUnset,
723 &response,
724 nullptr));
725 EXPECT_TRUE(response.update_exists);
726 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(true);
727
728 // The payload is applied when a deadline was set in the response.
Kevin Cernekee2494e282016-03-29 18:03:53 -0700729 fake_update_response_.deadline = "20101020";
730 ASSERT_TRUE(
731 TestUpdateCheck(nullptr, // request_params
732 fake_update_response_.GetUpdateResponse(),
733 -1,
734 false, // ping_only
735 ErrorCode::kSuccess,
736 metrics::CheckResult::kUpdateAvailable,
737 metrics::CheckReaction::kUpdating,
738 metrics::DownloadErrorCode::kUnset,
739 &response,
740 nullptr));
741 EXPECT_TRUE(response.update_exists);
742}
743
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700744TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700745 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700746 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700747 params.set_wall_clock_based_wait_enabled(true);
748 params.set_update_check_count_wait_enabled(false);
749 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700750
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700751 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700752 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700753 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700754 -1,
755 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700756 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800757 metrics::CheckResult::kUpdateAvailable,
758 metrics::CheckReaction::kDeferring,
759 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700760 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700761 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700762 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700763
764 // Verify if we are interactive check we don't defer.
765 params.set_interactive(true);
766 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700767 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700768 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700769 -1,
770 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700771 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800772 metrics::CheckResult::kUpdateAvailable,
773 metrics::CheckReaction::kUpdating,
774 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700775 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700776 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700777 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700778}
779
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700780TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700781 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700782 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700783 params.set_wall_clock_based_wait_enabled(false);
784 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700785
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700786 params.set_update_check_count_wait_enabled(true);
787 params.set_min_update_checks_needed(1);
788 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700789
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700790 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700791 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700792 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700793 -1,
794 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700795 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800796 metrics::CheckResult::kUpdateAvailable,
797 metrics::CheckReaction::kUpdating,
798 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700799 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700800 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700801 EXPECT_TRUE(response.update_exists);
802}
803
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700804TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700805 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700806 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700807 params.set_wall_clock_based_wait_enabled(true);
808 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700809
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700810 params.set_update_check_count_wait_enabled(true);
811 params.set_min_update_checks_needed(1);
812 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700813
Alex Deymo8e18f932015-03-27 16:16:59 -0700814 fake_update_response_.max_days_to_scatter = "0";
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700815 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700816 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700817 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700818 -1,
819 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700820 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800821 metrics::CheckResult::kUpdateAvailable,
822 metrics::CheckReaction::kUpdating,
823 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700824 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700825 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700826 EXPECT_TRUE(response.update_exists);
827}
828
829
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700830TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700831 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700832 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700833 params.set_wall_clock_based_wait_enabled(true);
834 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700835
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700836 params.set_update_check_count_wait_enabled(true);
837 params.set_min_update_checks_needed(0);
838 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700839
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700840 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700841 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700842 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700843 -1,
844 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700845 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800846 metrics::CheckResult::kUpdateAvailable,
847 metrics::CheckReaction::kUpdating,
848 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700849 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700850 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700851
Ben Chan9abb7632014-08-07 00:10:53 -0700852 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700853 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700854 ASSERT_EQ(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700855 EXPECT_TRUE(response.update_exists);
856}
857
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700858TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700859 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700860 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700861 params.set_wall_clock_based_wait_enabled(true);
862 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700863
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700864 params.set_update_check_count_wait_enabled(true);
865 params.set_min_update_checks_needed(1);
866 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700867
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700868 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700869 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700870 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700871 -1,
872 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700873 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800874 metrics::CheckResult::kUpdateAvailable,
875 metrics::CheckReaction::kDeferring,
876 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700877 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700878 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700879
Ben Chan9abb7632014-08-07 00:10:53 -0700880 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700881 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700882 ASSERT_GT(count, 0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700883 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700884
885 // Verify if we are interactive check we don't defer.
886 params.set_interactive(true);
887 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700888 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700889 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700890 -1,
891 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700892 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800893 metrics::CheckResult::kUpdateAvailable,
894 metrics::CheckReaction::kUpdating,
895 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700896 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700897 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700898 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700899}
900
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700901TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700902 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700903 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700904 params.set_wall_clock_based_wait_enabled(true);
905 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700906
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700907 params.set_update_check_count_wait_enabled(true);
908 params.set_min_update_checks_needed(1);
909 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700910
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700911 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700912
913 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700914 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700915 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700916 -1,
917 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700918 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800919 metrics::CheckResult::kUpdateAvailable,
920 metrics::CheckReaction::kDeferring,
921 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700922 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700923 nullptr));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700924
Ben Chan9abb7632014-08-07 00:10:53 -0700925 int64_t count;
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700926 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700927 // count remains the same, as the decrementing happens in update_attempter
928 // which this test doesn't exercise.
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700929 ASSERT_EQ(count, 5);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700930 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700931
932 // Verify if we are interactive check we don't defer.
933 params.set_interactive(true);
934 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -0700935 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -0700936 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -0700937 -1,
938 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700939 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800940 metrics::CheckResult::kUpdateAvailable,
941 metrics::CheckReaction::kUpdating,
942 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700943 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700944 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -0700945 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700946}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700947
Alex Deymo8e18f932015-03-27 16:16:59 -0700948TEST_F(OmahaRequestActionTest, CohortsArePersisted) {
949 OmahaResponse response;
950 OmahaRequestParams params = request_params_;
951 fake_update_response_.include_cohorts = true;
952 fake_update_response_.cohort = "s/154454/8479665";
953 fake_update_response_.cohorthint = "please-put-me-on-beta";
954 fake_update_response_.cohortname = "stable";
955
956 ASSERT_TRUE(TestUpdateCheck(&params,
957 fake_update_response_.GetUpdateResponse(),
958 -1,
959 false, // ping_only
960 ErrorCode::kSuccess,
961 metrics::CheckResult::kUpdateAvailable,
962 metrics::CheckReaction::kUpdating,
963 metrics::DownloadErrorCode::kUnset,
964 &response,
965 nullptr));
966
967 string value;
968 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
969 EXPECT_EQ(fake_update_response_.cohort, value);
970
971 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
972 EXPECT_EQ(fake_update_response_.cohorthint, value);
973
974 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
975 EXPECT_EQ(fake_update_response_.cohortname, value);
976}
977
978TEST_F(OmahaRequestActionTest, CohortsAreUpdated) {
979 OmahaResponse response;
980 OmahaRequestParams params = request_params_;
981 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
982 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint"));
983 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name"));
984 fake_update_response_.include_cohorts = true;
985 fake_update_response_.cohort = "s/154454/8479665";
986 fake_update_response_.cohorthint = "please-put-me-on-beta";
987 fake_update_response_.cohortname = "";
988
989 ASSERT_TRUE(TestUpdateCheck(&params,
990 fake_update_response_.GetUpdateResponse(),
991 -1,
992 false, // ping_only
993 ErrorCode::kSuccess,
994 metrics::CheckResult::kUpdateAvailable,
995 metrics::CheckReaction::kUpdating,
996 metrics::DownloadErrorCode::kUnset,
997 &response,
998 nullptr));
999
1000 string value;
1001 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1002 EXPECT_EQ(fake_update_response_.cohort, value);
1003
1004 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1005 EXPECT_EQ(fake_update_response_.cohorthint, value);
1006
1007 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1008}
1009
1010TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) {
1011 OmahaResponse response;
1012 OmahaRequestParams params = request_params_;
1013 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value"));
1014
1015 ASSERT_TRUE(TestUpdateCheck(&params,
1016 fake_update_response_.GetUpdateResponse(),
1017 -1,
1018 false, // ping_only
1019 ErrorCode::kSuccess,
1020 metrics::CheckResult::kUpdateAvailable,
1021 metrics::CheckReaction::kUpdating,
1022 metrics::DownloadErrorCode::kUnset,
1023 &response,
1024 nullptr));
1025
1026 string value;
1027 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1028 EXPECT_EQ("old_value", value);
1029
1030 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1031 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1032}
1033
Alex Deymo00d79ac2015-06-29 15:41:49 -07001034TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) {
1035 OmahaResponse response;
1036 OmahaRequestParams params = request_params_;
1037 fake_update_response_.include_cohorts = true;
1038 fake_update_response_.cohort = "s/154454/8479665";
1039 fake_update_response_.cohorthint = "please-put-me-on-beta";
1040 fake_update_response_.cohortname = "stable";
1041
1042 ASSERT_TRUE(TestUpdateCheck(&params,
1043 fake_update_response_.GetNoUpdateResponse(),
1044 -1,
1045 false, // ping_only
1046 ErrorCode::kSuccess,
1047 metrics::CheckResult::kNoUpdateAvailable,
1048 metrics::CheckReaction::kUnset,
1049 metrics::DownloadErrorCode::kUnset,
1050 &response,
1051 nullptr));
1052
1053 string value;
1054 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value));
1055 EXPECT_EQ(fake_update_response_.cohort, value);
1056
1057 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value));
1058 EXPECT_EQ(fake_update_response_.cohorthint, value);
1059
1060 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value));
1061 EXPECT_EQ(fake_update_response_.cohortname, value);
1062}
1063
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001064TEST_F(OmahaRequestActionTest, NoOutputPipeTest) {
Alex Deymo8e18f932015-03-27 16:16:59 -07001065 const string http_response(fake_update_response_.GetNoUpdateResponse());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001066
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001067 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001068 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001069
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001070 OmahaRequestParams params = request_params_;
1071 fake_system_state_.set_request_params(&params);
1072 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001073 brillo::make_unique_ptr(
1074 new MockHttpFetcher(http_response.data(),
1075 http_response.size(),
1076 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001077 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001078 OmahaRequestActionTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001079 ActionProcessor processor;
1080 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001081 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001082
Alex Deymo60ca1a72015-06-18 18:19:15 -07001083 loop.PostTask(base::Bind([&processor] { processor.StartProcessing(); }));
1084 loop.Run();
1085 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001086 EXPECT_FALSE(processor.IsRunning());
1087}
1088
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001089TEST_F(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001090 OmahaResponse response;
1091 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001092 TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001093 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001094 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001095 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001096 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001097 metrics::CheckResult::kParsingError,
1098 metrics::CheckReaction::kUnset,
1099 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -07001100 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001101 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001102 EXPECT_FALSE(response.update_exists);
1103}
1104
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001105TEST_F(OmahaRequestActionTest, EmptyResponseTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001106 OmahaResponse response;
1107 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001108 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001109 "",
1110 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001111 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001112 ErrorCode::kOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001113 metrics::CheckResult::kParsingError,
1114 metrics::CheckReaction::kUnset,
1115 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001116 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001117 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001118 EXPECT_FALSE(response.update_exists);
1119}
1120
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001121TEST_F(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001122 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001123 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001124 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001125 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1126 "<daystart elapsed_seconds=\"100\"/>"
1127 "<app appid=\"foo\" status=\"ok\">"
1128 "<ping status=\"ok\"/>"
1129 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001130 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001131 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001132 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001133 metrics::CheckResult::kParsingError,
1134 metrics::CheckReaction::kUnset,
1135 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001136 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001137 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001138 EXPECT_FALSE(response.update_exists);
1139}
1140
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001141TEST_F(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001142 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001143 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001144 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001145 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1146 "<daystart elapsed_seconds=\"100\"/>"
1147 "<app appid=\"foo\" status=\"ok\">"
1148 "<ping status=\"ok\"/>"
1149 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001150 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001151 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001152 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001153 metrics::CheckResult::kParsingError,
1154 metrics::CheckReaction::kUnset,
1155 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001156 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001157 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001158 EXPECT_FALSE(response.update_exists);
1159}
1160
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001161TEST_F(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001162 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001163 ASSERT_FALSE(TestUpdateCheck(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001164 nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001165 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1166 "<daystart elapsed_seconds=\"100\"/>"
1167 "<app appid=\"foo\" status=\"ok\">"
1168 "<ping status=\"ok\"/>"
1169 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001170 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001171 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001172 ErrorCode::kOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -08001173 metrics::CheckResult::kParsingError,
1174 metrics::CheckReaction::kUnset,
1175 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001176 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001177 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001178 EXPECT_FALSE(response.update_exists);
1179}
1180
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001181TEST_F(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001182 string input_response =
1183 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
1184 "<daystart elapsed_seconds=\"100\"/>"
1185 "<app appid=\"xyz\" status=\"ok\">"
1186 "<updatecheck status=\"ok\">"
1187 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -07001188 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001189 "<packages><package hash=\"not-used\" name=\"f\" "
1190 "size=\"587\"/></packages>"
1191 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001192 "ChromeOSVersion=\"10.2.3.4\" "
1193 "Prompt=\"false\" "
1194 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -08001195 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001196 "sha256=\"lkq34j5345\" "
1197 "needsadmin=\"true\" "
1198 "/></actions></manifest></updatecheck></app></response>";
1199 LOG(INFO) << "Input Response = " << input_response;
1200
Darin Petkov6a5b3222010-07-13 14:55:28 -07001201 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001202 ASSERT_TRUE(TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001203 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -07001204 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001205 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001206 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001207 metrics::CheckResult::kUpdateAvailable,
1208 metrics::CheckReaction::kUpdating,
1209 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001210 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001211 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001212 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -07001213 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001214 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001215 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001216 EXPECT_EQ("lkq34j5345", response.hash);
1217 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001218 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001219 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001220}
1221
1222namespace {
1223class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1224 public:
1225 void ProcessingStopped(const ActionProcessor* processor) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001226 brillo::MessageLoop::current()->BreakLoop();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001227 }
Darin Petkov6a5b3222010-07-13 14:55:28 -07001228};
1229
Alex Deymo60ca1a72015-06-18 18:19:15 -07001230void TerminateTransferTestStarter(ActionProcessor* processor) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001231 processor->StartProcessing();
1232 CHECK(processor->IsRunning());
1233 processor->StopProcessing();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001234}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001235} // namespace
Darin Petkov6a5b3222010-07-13 14:55:28 -07001236
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001237TEST_F(OmahaRequestActionTest, TerminateTransferTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001238 brillo::FakeMessageLoop loop(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001239 loop.SetAsCurrent();
Darin Petkov6a5b3222010-07-13 14:55:28 -07001240
Alex Deymo60ca1a72015-06-18 18:19:15 -07001241 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001242 OmahaRequestAction action(&fake_system_state_, nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001243 brillo::make_unique_ptr(
1244 new MockHttpFetcher(http_response.data(),
1245 http_response.size(),
1246 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001247 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001248 TerminateEarlyTestProcessorDelegate delegate;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001249 ActionProcessor processor;
1250 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001251 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001252
Alex Deymo60ca1a72015-06-18 18:19:15 -07001253 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor));
1254 loop.Run();
1255 EXPECT_FALSE(loop.PendingTasks());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001256}
1257
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001258TEST_F(OmahaRequestActionTest, XmlEncodeTest) {
Alex Deymob0d74eb2015-03-30 17:59:17 -07001259 string output;
1260 EXPECT_TRUE(XmlEncode("ab", &output));
1261 EXPECT_EQ("ab", output);
1262 EXPECT_TRUE(XmlEncode("a<b", &output));
1263 EXPECT_EQ("a&lt;b", output);
Alex Deymocc457852015-06-18 18:35:50 -07001264 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output));
1265 EXPECT_EQ("&lt;&amp;&gt;&quot;&apos;\\", output);
Alex Deymob0d74eb2015-03-30 17:59:17 -07001266 EXPECT_TRUE(XmlEncode("&lt;&amp;&gt;", &output));
1267 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", output);
Alex Deymocc457852015-06-18 18:35:50 -07001268 // Check that unterminated UTF-8 strings are handled properly.
Alex Deymob0d74eb2015-03-30 17:59:17 -07001269 EXPECT_FALSE(XmlEncode("\xc2", &output));
1270 // Fail with invalid ASCII-7 chars.
1271 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
1272}
Darin Petkov6a5b3222010-07-13 14:55:28 -07001273
Alex Deymob0d74eb2015-03-30 17:59:17 -07001274TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) {
1275 EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
1276 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
1277}
1278
1279TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001280 brillo::Blob post_data;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001281
1282 // Make sure XML Encode is being called on the params
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001283 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001284 constants::kOmahaPlatformName,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001285 OmahaRequestParams::kOsVersion,
1286 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001287 "x86 generic<id",
Alex Deymo85616652015-10-15 18:48:31 -07001288 kTestAppId,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001289 "0.1.0.0",
1290 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001291 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001292 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001293 "ChromeOSFirmware.1.0",
1294 "EC100",
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001295 false, // delta okay
1296 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001297 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001298 ""); // target_version_prefix
Alex Deymo8e18f932015-03-27 16:16:59 -07001299 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring");
1300 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\");
1301 fake_prefs_.SetString(kPrefsOmahaCohortName,
Alex Vakulenko0103c362016-01-20 07:56:15 -08001302 base::JoinString(
1303 vector<string>(100, "My spoon is too big."), " "));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001304 OmahaResponse response;
1305 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001306 TestUpdateCheck(&params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001307 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001308 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001309 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001310 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001311 metrics::CheckResult::kParsingError,
1312 metrics::CheckReaction::kUnset,
1313 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001314 &response,
1315 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001316 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001317 string post_str(post_data.begin(), post_data.end());
Alex Deymo8e18f932015-03-27 16:16:59 -07001318 EXPECT_NE(string::npos, post_str.find("testtheservice_pack&gt;"));
1319 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>"));
1320 EXPECT_NE(string::npos, post_str.find("x86 generic&lt;id"));
1321 EXPECT_EQ(string::npos, post_str.find("x86 generic<id"));
1322 EXPECT_NE(string::npos, post_str.find("unittest_track&amp;lt;"));
1323 EXPECT_EQ(string::npos, post_str.find("unittest_track&lt;"));
1324 EXPECT_NE(string::npos, post_str.find("&lt;OEM MODEL&gt;"));
1325 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>"));
1326 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\""));
1327 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\""));
1328 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&amp;string\\\""));
1329 // Values from Prefs that are too big are removed from the XML instead of
1330 // encoded.
1331 EXPECT_EQ(string::npos, post_str.find("cohortname="));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001332}
1333
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001334TEST_F(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001335 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001336 fake_update_response_.deadline = "&lt;20110101";
1337 fake_update_response_.more_info_url = "testthe&lt;url";
1338 fake_update_response_.codebase = "testthe&amp;codebase/";
Darin Petkov6a5b3222010-07-13 14:55:28 -07001339 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001340 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001341 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001342 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001343 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001344 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001345 metrics::CheckResult::kUpdateAvailable,
1346 metrics::CheckReaction::kUpdating,
1347 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001348 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001349 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001350
1351 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001352 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001353 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001354}
1355
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001356TEST_F(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001357 OmahaResponse response;
Alex Deymo8e18f932015-03-27 16:16:59 -07001358 // overflows int32_t:
1359 fake_update_response_.size = 123123123123123ll;
Darin Petkov6a5b3222010-07-13 14:55:28 -07001360 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001361 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001362 fake_update_response_.GetUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001363 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001364 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001365 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001366 metrics::CheckResult::kUpdateAvailable,
1367 metrics::CheckReaction::kUpdating,
1368 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001369 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001370 nullptr));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001371
1372 EXPECT_EQ(response.size, 123123123123123ll);
1373}
1374
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001375TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001376 brillo::Blob post_data;
Alex Deymo8427b4a2014-11-05 14:00:32 -08001377 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001378 fake_system_state_.set_prefs(&prefs);
1379
Darin Petkov95508da2011-01-05 12:42:29 -08001380 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1381 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Alex Deymoefb9d832015-11-02 18:39:02 -08001382 // An existing but empty previous version means that we didn't reboot to a new
1383 // update, therefore, no need to update the previous version.
1384 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001385 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001386 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001387 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001388 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001389 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001390 metrics::CheckResult::kParsingError,
1391 metrics::CheckReaction::kUnset,
1392 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001393 nullptr, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001394 &post_data));
1395 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001396 string post_str(post_data.begin(), post_data.end());
Thieu Le116fda32011-04-19 11:01:54 -07001397 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001398 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001399 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001400 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001401 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1402 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001403 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1404 string::npos);
1405 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1406 string::npos);
Alex Deymoefb9d832015-11-02 18:39:02 -08001407 // No <event> tag should be sent if we didn't reboot to an update.
1408 EXPECT_EQ(post_str.find("<event"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001409}
1410
Jay Srinivasan0a708742012-03-20 11:26:12 -07001411
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001412TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001413 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001414 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001415 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1416 "invalid xml>",
1417 &post_data);
1418 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001419 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001420 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001421 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001422 OmahaEvent::kTypeUpdateDownloadStarted,
1423 OmahaEvent::kResultSuccess);
1424 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001425 EXPECT_EQ(post_str.find("ping"), string::npos);
1426 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001427}
1428
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001429TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001430 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001431 TestEvent(request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001432 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1433 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001434 ErrorCode::kError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001435 "invalid xml>",
1436 &post_data);
1437 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001438 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001439 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001440 " <event eventtype=\"%d\" eventresult=\"%d\" "
1441 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001442 OmahaEvent::kTypeDownloadComplete,
1443 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001444 static_cast<int>(ErrorCode::kError));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001445 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001446 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001447}
1448
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001449TEST_F(OmahaRequestActionTest, IsEventTest) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001450 string http_response("doesn't matter");
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001451 // Create a copy of the OmahaRequestParams to reuse it later.
1452 OmahaRequestParams params = request_params_;
1453 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001454 OmahaRequestAction update_check_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001455 &fake_system_state_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001456 nullptr,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001457 brillo::make_unique_ptr(
1458 new MockHttpFetcher(http_response.data(),
1459 http_response.size(),
1460 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001461 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001462 EXPECT_FALSE(update_check_action.IsEvent());
1463
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001464 params = request_params_;
1465 fake_system_state_.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001466 OmahaRequestAction event_action(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001467 &fake_system_state_,
Darin Petkove17f86b2010-07-20 09:12:01 -07001468 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001469 brillo::make_unique_ptr(
1470 new MockHttpFetcher(http_response.data(),
1471 http_response.size(),
1472 nullptr)),
Thieu Le116fda32011-04-19 11:01:54 -07001473 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001474 EXPECT_TRUE(event_action.IsEvent());
1475}
1476
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001477TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001478 for (int i = 0; i < 2; i++) {
1479 bool delta_okay = i == 1;
1480 const char* delta_okay_str = delta_okay ? "true" : "false";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001481 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001482 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001483 constants::kOmahaPlatformName,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001484 OmahaRequestParams::kOsVersion,
1485 "service_pack",
1486 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001487 kTestAppId,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001488 "0.1.0.0",
1489 "en-US",
1490 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001491 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001492 "ChromeOSFirmware.1.0",
1493 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001494 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001495 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001496 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001497 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001498 ASSERT_FALSE(TestUpdateCheck(&params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001499 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001500 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001501 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001502 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001503 metrics::CheckResult::kParsingError,
1504 metrics::CheckReaction::kUnset,
1505 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001506 nullptr,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001507 &post_data));
1508 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001509 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001510 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1511 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001512 string::npos)
1513 << "i = " << i;
1514 }
1515}
1516
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001517TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) {
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001518 for (int i = 0; i < 2; i++) {
1519 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001520 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001521 brillo::Blob post_data;
Gilad Arnold5bb4c902014-04-10 12:32:13 -07001522 FakeSystemState fake_system_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001523 OmahaRequestParams params(&fake_system_state_,
Alex Deymoac41a822015-09-15 20:52:53 -07001524 constants::kOmahaPlatformName,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001525 OmahaRequestParams::kOsVersion,
1526 "service_pack",
1527 "x86-generic",
Alex Deymo85616652015-10-15 18:48:31 -07001528 kTestAppId,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001529 "0.1.0.0",
1530 "en-US",
1531 "unittest_track",
1532 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001533 "ChromeOSFirmware.1.0",
1534 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001535 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001536 interactive,
1537 "http://url",
Gilad Arnold74b5f552014-10-07 08:17:16 -07001538 ""); // target_version_prefix
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001539 ASSERT_FALSE(TestUpdateCheck(&params,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001540 "invalid xml>",
1541 -1,
1542 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001543 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001544 metrics::CheckResult::kParsingError,
1545 metrics::CheckReaction::kUnset,
1546 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001547 nullptr,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001548 &post_data));
1549 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001550 string post_str(post_data.begin(), post_data.end());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001551 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1552 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001553 string::npos)
1554 << "i = " << i;
1555 }
1556}
1557
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001558TEST_F(OmahaRequestActionTest, OmahaEventTest) {
Darin Petkove17f86b2010-07-20 09:12:01 -07001559 OmahaEvent default_event;
1560 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1561 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001562 EXPECT_EQ(ErrorCode::kError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001563
1564 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1565 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1566 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001567 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001568
1569 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1570 OmahaEvent::kResultError,
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001571 ErrorCode::kError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001572 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1573 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001574 EXPECT_EQ(ErrorCode::kError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001575}
1576
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001577void OmahaRequestActionTest::PingTest(bool ping_only) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001578 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001579 fake_system_state_.set_prefs(&prefs);
1580 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1581 .Times(AnyNumber());
1582 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
1583 // Add a few hours to the day difference to test no rounding, etc.
1584 int64_t five_days_ago =
1585 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1586 int64_t six_days_ago =
1587 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
1588 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1589 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
1590 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1591 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1592 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1593 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001594 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001595 ASSERT_TRUE(
1596 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001597 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001598 -1,
1599 ping_only,
1600 ErrorCode::kSuccess,
1601 metrics::CheckResult::kUnset,
1602 metrics::CheckReaction::kUnset,
1603 metrics::DownloadErrorCode::kUnset,
1604 nullptr,
1605 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001606 string post_str(post_data.begin(), post_data.end());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001607 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
1608 string::npos);
1609 if (ping_only) {
1610 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
1611 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1612 } else {
1613 EXPECT_NE(post_str.find("updatecheck"), string::npos);
1614 EXPECT_NE(post_str.find("previousversion"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001615 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001616}
1617
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001618TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) {
1619 PingTest(true /* ping_only */);
1620}
1621
1622TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) {
1623 PingTest(false /* ping_only */);
1624}
1625
1626TEST_F(OmahaRequestActionTest, ActivePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001627 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001628 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001629 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1630 .Times(AnyNumber());
1631 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001632 int64_t three_days_ago =
1633 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1634 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001635 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1636 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001637 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1638 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1639 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1640 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001641 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001642 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001643 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001644 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001645 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001646 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001647 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001648 metrics::CheckResult::kNoUpdateAvailable,
1649 metrics::CheckReaction::kUnset,
1650 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001651 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001652 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001653 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001654 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001655 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001656}
1657
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001658TEST_F(OmahaRequestActionTest, RollCallPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001659 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001660 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001661 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1662 .Times(AnyNumber());
1663 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001664 int64_t four_days_ago =
1665 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1666 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001667 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1668 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001669 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1670 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1671 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1672 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001673 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001674 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001675 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001676 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001677 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001678 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001679 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001680 metrics::CheckResult::kNoUpdateAvailable,
1681 metrics::CheckReaction::kUnset,
1682 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001683 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001684 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001685 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001686 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001687 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001688}
1689
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001690TEST_F(OmahaRequestActionTest, NoPingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001691 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001692 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001693 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1694 .Times(AnyNumber());
1695 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001696 int64_t one_hour_ago =
1697 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001698 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1699 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001700 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1701 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1702 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1703 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
Alex Deymoebbe7ef2014-10-30 13:02:49 -07001704 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't
1705 // send a ping.
1706 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1707 .WillOnce(Return(true));
1708 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1709 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001710 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001711 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001712 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001713 fake_update_response_.GetNoUpdateResponse(),
Darin Petkovedc522e2010-11-05 09:35:17 -07001714 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001715 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001716 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001717 metrics::CheckResult::kNoUpdateAvailable,
1718 metrics::CheckReaction::kUnset,
1719 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001720 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001721 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001722 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001723 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001724}
1725
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001726TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) {
Thieu Leb44e9e82011-06-06 14:34:04 -07001727 // This test ensures that we ignore empty ping only requests.
Alex Deymo8427b4a2014-11-05 14:00:32 -08001728 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001729 fake_system_state_.set_prefs(&prefs);
Thieu Leb44e9e82011-06-06 14:34:04 -07001730 int64_t now = Time::Now().ToInternalValue();
1731 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1732 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1733 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1734 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1735 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1736 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001737 brillo::Blob post_data;
Thieu Leb44e9e82011-06-06 14:34:04 -07001738 EXPECT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001739 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07001740 fake_update_response_.GetNoUpdateResponse(),
Thieu Leb44e9e82011-06-06 14:34:04 -07001741 -1,
1742 true, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001743 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001744 metrics::CheckResult::kUnset,
1745 metrics::CheckReaction::kUnset,
1746 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001747 nullptr,
Thieu Leb44e9e82011-06-06 14:34:04 -07001748 &post_data));
Alex Deymo80f70ff2016-02-10 16:08:11 -08001749 EXPECT_EQ(0U, post_data.size());
Thieu Leb44e9e82011-06-06 14:34:04 -07001750}
1751
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001752TEST_F(OmahaRequestActionTest, BackInTimePingTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001753 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001754 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001755 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1756 .Times(AnyNumber());
1757 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001758 int64_t future =
1759 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001760 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1761 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001762 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1763 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1764 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1765 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1766 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1767 .WillOnce(Return(true));
1768 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1769 .WillOnce(Return(true));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001770 brillo::Blob post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001771 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001772 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001773 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1774 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001775 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001776 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001777 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001778 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001779 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001780 metrics::CheckResult::kNoUpdateAvailable,
1781 metrics::CheckReaction::kUnset,
1782 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001783 nullptr,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001784 &post_data));
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001785 string post_str(post_data.begin(), post_data.end());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001786 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001787}
1788
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001789TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) {
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001790 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001791 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001792 // may fail if it runs for longer than 5 seconds. It shouldn't run
1793 // that long though.
1794 int64_t midnight =
1795 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1796 int64_t midnight_slack =
1797 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Alex Deymo8427b4a2014-11-05 14:00:32 -08001798 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001799 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001800 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1801 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001802 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1803 AllOf(Ge(midnight), Le(midnight_slack))))
1804 .WillOnce(Return(true));
1805 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1806 AllOf(Ge(midnight), Le(midnight_slack))))
1807 .WillOnce(Return(true));
1808 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001809 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001810 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1811 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001812 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001813 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001814 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001815 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001816 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001817 metrics::CheckResult::kNoUpdateAvailable,
1818 metrics::CheckReaction::kUnset,
1819 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001820 nullptr,
1821 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001822}
1823
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001824TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001825 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001826 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001827 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1828 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001829 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1830 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1831 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001832 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001833 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1834 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001835 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001836 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001837 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001838 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001839 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001840 metrics::CheckResult::kNoUpdateAvailable,
1841 metrics::CheckReaction::kUnset,
1842 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001843 nullptr,
1844 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001845}
1846
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001847TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) {
Alex Deymo8427b4a2014-11-05 14:00:32 -08001848 NiceMock<MockPrefs> prefs;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001849 fake_system_state_.set_prefs(&prefs);
David Zeuthen33bae492014-02-25 16:16:18 -08001850 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1851 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001852 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1853 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1854 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001855 TestUpdateCheck(nullptr, // request_params
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001856 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1857 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001858 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001859 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001860 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001861 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001862 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001863 metrics::CheckResult::kNoUpdateAvailable,
1864 metrics::CheckReaction::kUnset,
1865 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001866 nullptr,
1867 nullptr));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001868}
1869
Alex Deymob3fa53b2016-04-18 19:57:58 -07001870TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) {
1871 // Test that the "eol" flags is only parsed from the "_eol" attribute and not
1872 // the "eol" attribute.
1873 ASSERT_TRUE(
1874 TestUpdateCheck(nullptr, // request_params
1875 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1876 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">"
1877 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" "
1878 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/>"
1879 "</app></response>",
1880 -1,
1881 false, // ping_only
1882 ErrorCode::kSuccess,
1883 metrics::CheckResult::kNoUpdateAvailable,
1884 metrics::CheckReaction::kUnset,
1885 metrics::DownloadErrorCode::kUnset,
1886 nullptr,
1887 nullptr));
1888 string eol_pref;
1889 EXPECT_TRUE(
1890 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref));
1891 // Note that the eol="eol" attribute should be ignored and the _eol should be
1892 // used instead.
1893 EXPECT_EQ("security-only", eol_pref);
1894}
1895
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001896TEST_F(OmahaRequestActionTest, NoUniqueIDTest) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001897 brillo::Blob post_data;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001898 ASSERT_FALSE(TestUpdateCheck(nullptr, // request_params
Darin Petkov84c763c2010-07-29 16:27:58 -07001899 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001900 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001901 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001902 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001903 metrics::CheckResult::kParsingError,
1904 metrics::CheckReaction::kUnset,
1905 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001906 nullptr, // response
Darin Petkov84c763c2010-07-29 16:27:58 -07001907 &post_data));
1908 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001909 string post_str(post_data.begin(), post_data.end());
Darin Petkov84c763c2010-07-29 16:27:58 -07001910 EXPECT_EQ(post_str.find("machineid="), string::npos);
1911 EXPECT_EQ(post_str.find("userid="), string::npos);
1912}
1913
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001914TEST_F(OmahaRequestActionTest, NetworkFailureTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001915 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001916 const int http_error_code =
1917 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501;
Darin Petkovedc522e2010-11-05 09:35:17 -07001918 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001919 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001920 "",
1921 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001922 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001923 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001924 metrics::CheckResult::kDownloadError,
1925 metrics::CheckReaction::kUnset,
1926 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001927 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001928 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001929 EXPECT_FALSE(response.update_exists);
1930}
1931
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001932TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
Darin Petkovedc522e2010-11-05 09:35:17 -07001933 OmahaResponse response;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001934 const int http_error_code =
1935 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999;
Darin Petkovedc522e2010-11-05 09:35:17 -07001936 ASSERT_FALSE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001937 TestUpdateCheck(nullptr, // request_params
Darin Petkovedc522e2010-11-05 09:35:17 -07001938 "",
1939 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001940 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001941 static_cast<ErrorCode>(http_error_code),
David Zeuthen33bae492014-02-25 16:16:18 -08001942 metrics::CheckResult::kDownloadError,
1943 metrics::CheckReaction::kUnset,
1944 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001945 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001946 nullptr));
Darin Petkovedc522e2010-11-05 09:35:17 -07001947 EXPECT_FALSE(response.update_exists);
1948}
1949
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001950TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001951 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001952 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001953 params.set_wall_clock_based_wait_enabled(true);
1954 params.set_waiting_period(TimeDelta().FromDays(1));
1955 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001956
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001957 ASSERT_FALSE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001958 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001959 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001960 -1,
1961 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001962 ErrorCode::kOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001963 metrics::CheckResult::kUpdateAvailable,
1964 metrics::CheckReaction::kDeferring,
1965 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001966 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001967 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001968
Ben Chan9abb7632014-08-07 00:10:53 -07001969 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001970 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001971 ASSERT_GT(timestamp, 0);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001972 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001973
1974 // Verify if we are interactive check we don't defer.
1975 params.set_interactive(true);
1976 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001977 TestUpdateCheck(&params,
Alex Deymo8e18f932015-03-27 16:16:59 -07001978 fake_update_response_.GetUpdateResponse(),
Chris Sosa968d0572013-08-23 14:46:02 -07001979 -1,
1980 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001981 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001982 metrics::CheckResult::kUpdateAvailable,
1983 metrics::CheckReaction::kUpdating,
1984 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001985 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001986 nullptr));
Chris Sosa968d0572013-08-23 14:46:02 -07001987 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001988}
1989
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001990TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001991 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07001992 OmahaRequestParams params = request_params_;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001993 params.set_wall_clock_based_wait_enabled(true);
1994 params.set_waiting_period(TimeDelta().FromDays(1));
1995 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001996
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001997 // Set the timestamp to a very old value such that it exceeds the
1998 // waiting period set above.
1999 Time t1;
2000 Time::FromString("1/1/2012", &t1);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002001 ASSERT_TRUE(fake_prefs_.SetInt64(
2002 kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002003 ASSERT_TRUE(TestUpdateCheck(
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002004 &params,
Alex Deymo8e18f932015-03-27 16:16:59 -07002005 fake_update_response_.GetUpdateResponse(),
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002006 -1,
2007 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002008 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002009 metrics::CheckResult::kUpdateAvailable,
2010 metrics::CheckReaction::kUpdating,
2011 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002012 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002013 nullptr));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002014
2015 EXPECT_TRUE(response.update_exists);
2016
2017 // Make sure the timestamp t1 is unchanged showing that it was reused.
Ben Chan9abb7632014-08-07 00:10:53 -07002018 int64_t timestamp = 0;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002019 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
Jay Srinivasan34b5d862012-07-23 11:43:22 -07002020 ASSERT_TRUE(timestamp == t1.ToInternalValue());
2021}
2022
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002023TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002024 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002025 base::ScopedTempDir tempdir;
2026 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002027
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002028 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07002029 OmahaRequestParams params(&fake_system_state_);
2030 params.set_root(tempdir.path().value());
2031 params.set_app_id("{22222222-2222-2222-2222-222222222222}");
2032 params.set_app_version("1.2.3.4");
2033 params.set_current_channel("canary-channel");
2034 EXPECT_TRUE(params.SetTargetChannel("stable-channel", true, nullptr));
2035 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002036 EXPECT_TRUE(params.to_more_stable_channel());
2037 EXPECT_TRUE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002038 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002039 "invalid xml>",
2040 -1,
2041 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002042 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002043 metrics::CheckResult::kParsingError,
2044 metrics::CheckReaction::kUnset,
2045 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002046 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002047 &post_data));
2048 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002049 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002050 EXPECT_NE(string::npos, post_str.find(
2051 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
2052 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
2053 "track=\"stable-channel\" from_track=\"canary-channel\" "));
2054}
2055
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002056TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002057 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -07002058 base::ScopedTempDir tempdir;
2059 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002060
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002061 brillo::Blob post_data;
Sen Jiang297e5832016-03-17 14:45:51 -07002062 OmahaRequestParams params(&fake_system_state_);
2063 params.set_root(tempdir.path().value());
2064 params.set_app_id("{11111111-1111-1111-1111-111111111111}");
2065 params.set_app_version("5.6.7.8");
2066 params.set_current_channel("stable-channel");
2067 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
2068 params.UpdateDownloadChannel();
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002069 EXPECT_FALSE(params.to_more_stable_channel());
2070 EXPECT_FALSE(params.is_powerwash_allowed());
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002071 ASSERT_FALSE(TestUpdateCheck(&params,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002072 "invalid xml>",
2073 -1,
2074 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002075 ErrorCode::kOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08002076 metrics::CheckResult::kParsingError,
2077 metrics::CheckReaction::kUnset,
2078 metrics::DownloadErrorCode::kUnset,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002079 nullptr, // response
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002080 &post_data));
2081 // convert post_data to string
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002082 string post_str(post_data.begin(), post_data.end());
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002083 EXPECT_NE(string::npos, post_str.find(
2084 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
2085 "version=\"5.6.7.8\" "
2086 "track=\"canary-channel\" from_track=\"stable-channel\""));
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002087 EXPECT_EQ(string::npos, post_str.find("from_version"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002088}
2089
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002090// Checks that the initial ping with a=-1 r=-1 is not send when the device
2091// was powerwashed.
2092TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) {
2093 fake_prefs_.SetString(kPrefsPreviousVersion, "");
2094
2095 // Flag that the device was powerwashed in the past.
2096 fake_system_state_.fake_hardware()->SetPowerwashCount(1);
2097
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07002098 brillo::Blob post_data;
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002099 ASSERT_TRUE(
2100 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002101 fake_update_response_.GetNoUpdateResponse(),
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002102 -1,
2103 false, // ping_only
2104 ErrorCode::kSuccess,
2105 metrics::CheckResult::kNoUpdateAvailable,
2106 metrics::CheckReaction::kUnset,
2107 metrics::DownloadErrorCode::kUnset,
2108 nullptr,
2109 &post_data));
2110 // We shouldn't send a ping in this case since powerwash > 0.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08002111 string post_str(post_data.begin(), post_data.end());
Alex Deymoebbe7ef2014-10-30 13:02:49 -07002112 EXPECT_EQ(string::npos, post_str.find("<ping"));
2113}
2114
Alex Deymo9fded1e2015-11-05 12:31:19 -08002115// Checks that the event 54 is sent on a reboot to a new update.
2116TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) {
2117 // Flag that the device was updated in a previous boot.
2118 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4");
2119
2120 brillo::Blob post_data;
2121 ASSERT_TRUE(
2122 TestUpdateCheck(nullptr, // request_params
2123 fake_update_response_.GetNoUpdateResponse(),
2124 -1,
2125 false, // ping_only
2126 ErrorCode::kSuccess,
2127 metrics::CheckResult::kNoUpdateAvailable,
2128 metrics::CheckReaction::kUnset,
2129 metrics::DownloadErrorCode::kUnset,
2130 nullptr,
2131 &post_data));
2132 string post_str(post_data.begin(), post_data.end());
2133
2134 // An event 54 is included and has the right version.
2135 EXPECT_NE(string::npos,
2136 post_str.find(base::StringPrintf(
2137 "<event eventtype=\"%d\"",
2138 OmahaEvent::kTypeRebootedAfterUpdate)));
2139 EXPECT_NE(string::npos,
2140 post_str.find("previousversion=\"1.2.3.4\"></event>"));
2141
2142 // The previous version flag should have been removed.
2143 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion));
2144 string prev_version;
2145 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version));
2146 EXPECT_TRUE(prev_version.empty());
2147}
2148
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002149void OmahaRequestActionTest::P2PTest(
2150 bool initial_allow_p2p_for_downloading,
2151 bool initial_allow_p2p_for_sharing,
2152 bool omaha_disable_p2p_for_downloading,
2153 bool omaha_disable_p2p_for_sharing,
2154 bool payload_state_allow_p2p_attempt,
2155 bool expect_p2p_client_lookup,
2156 const string& p2p_client_result_url,
2157 bool expected_allow_p2p_for_downloading,
2158 bool expected_allow_p2p_for_sharing,
2159 const string& expected_p2p_url) {
David Zeuthen8f191b22013-08-06 12:27:50 -07002160 OmahaResponse response;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002161 OmahaRequestParams request_params = request_params_;
Gilad Arnold74b5f552014-10-07 08:17:16 -07002162 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading;
2163 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing;
2164 string actual_p2p_url;
David Zeuthen8f191b22013-08-06 12:27:50 -07002165
2166 MockPayloadState mock_payload_state;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002167 fake_system_state_.set_payload_state(&mock_payload_state);
David Zeuthen8f191b22013-08-06 12:27:50 -07002168 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2169 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
Gilad Arnold74b5f552014-10-07 08:17:16 -07002170 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading())
2171 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading));
2172 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing())
2173 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing));
2174 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_))
2175 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading));
2176 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_))
2177 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing));
2178 EXPECT_CALL(mock_payload_state, SetP2PUrl(_))
2179 .WillRepeatedly(SaveArg<0>(&actual_p2p_url));
2180
David Zeuthen8f191b22013-08-06 12:27:50 -07002181 MockP2PManager mock_p2p_manager;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002182 fake_system_state_.set_p2p_manager(&mock_p2p_manager);
David Zeuthen8f191b22013-08-06 12:27:50 -07002183 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2184
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002185 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2186 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002187 .Times(expect_p2p_client_lookup ? 1 : 0);
2188
Alex Deymo8e18f932015-03-27 16:16:59 -07002189 fake_update_response_.disable_p2p_for_downloading =
2190 omaha_disable_p2p_for_downloading;
2191 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing;
David Zeuthen8f191b22013-08-06 12:27:50 -07002192 ASSERT_TRUE(
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002193 TestUpdateCheck(&request_params,
Alex Deymo8e18f932015-03-27 16:16:59 -07002194 fake_update_response_.GetUpdateResponse(),
David Zeuthen8f191b22013-08-06 12:27:50 -07002195 -1,
2196 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002197 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002198 metrics::CheckResult::kUpdateAvailable,
2199 metrics::CheckReaction::kUpdating,
2200 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07002201 &response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002202 nullptr));
David Zeuthen8f191b22013-08-06 12:27:50 -07002203 EXPECT_TRUE(response.update_exists);
2204
Gilad Arnold74b5f552014-10-07 08:17:16 -07002205 EXPECT_EQ(omaha_disable_p2p_for_downloading,
2206 response.disable_p2p_for_downloading);
2207 EXPECT_EQ(omaha_disable_p2p_for_sharing,
2208 response.disable_p2p_for_sharing);
David Zeuthen8f191b22013-08-06 12:27:50 -07002209
Gilad Arnold74b5f552014-10-07 08:17:16 -07002210 EXPECT_EQ(expected_allow_p2p_for_downloading,
2211 actual_allow_p2p_for_downloading);
2212 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing);
2213 EXPECT_EQ(expected_p2p_url, actual_p2p_url);
David Zeuthen8f191b22013-08-06 12:27:50 -07002214}
2215
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002216TEST_F(OmahaRequestActionTest, P2PWithPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002217 P2PTest(true, // initial_allow_p2p_for_downloading
2218 true, // initial_allow_p2p_for_sharing
2219 false, // omaha_disable_p2p_for_downloading
2220 false, // omaha_disable_p2p_for_sharing
2221 true, // payload_state_allow_p2p_attempt
2222 true, // expect_p2p_client_lookup
2223 "http://1.3.5.7/p2p", // p2p_client_result_url
2224 true, // expected_allow_p2p_for_downloading
2225 true, // expected_allow_p2p_for_sharing
2226 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002227}
2228
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002229TEST_F(OmahaRequestActionTest, P2PWithoutPeer) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002230 P2PTest(true, // initial_allow_p2p_for_downloading
2231 true, // initial_allow_p2p_for_sharing
2232 false, // omaha_disable_p2p_for_downloading
2233 false, // omaha_disable_p2p_for_sharing
2234 true, // payload_state_allow_p2p_attempt
2235 true, // expect_p2p_client_lookup
2236 "", // p2p_client_result_url
2237 false, // expected_allow_p2p_for_downloading
2238 true, // expected_allow_p2p_for_sharing
2239 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002240}
2241
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002242TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002243 P2PTest(false, // initial_allow_p2p_for_downloading
2244 true, // initial_allow_p2p_for_sharing
2245 false, // omaha_disable_p2p_for_downloading
2246 false, // omaha_disable_p2p_for_sharing
2247 true, // payload_state_allow_p2p_attempt
2248 false, // expect_p2p_client_lookup
2249 "unset", // p2p_client_result_url
2250 false, // expected_allow_p2p_for_downloading
2251 true, // expected_allow_p2p_for_sharing
2252 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002253}
2254
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002255TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002256 P2PTest(true, // initial_allow_p2p_for_downloading
2257 true, // initial_allow_p2p_for_sharing
2258 true, // omaha_disable_p2p_for_downloading
2259 false, // omaha_disable_p2p_for_sharing
2260 true, // payload_state_allow_p2p_attempt
2261 false, // expect_p2p_client_lookup
2262 "unset", // p2p_client_result_url
2263 false, // expected_allow_p2p_for_downloading
2264 true, // expected_allow_p2p_for_sharing
2265 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002266}
2267
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002268TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002269 P2PTest(true, // initial_allow_p2p_for_downloading
2270 true, // initial_allow_p2p_for_sharing
2271 false, // omaha_disable_p2p_for_downloading
2272 true, // omaha_disable_p2p_for_sharing
2273 true, // payload_state_allow_p2p_attempt
2274 true, // expect_p2p_client_lookup
2275 "http://1.3.5.7/p2p", // p2p_client_result_url
2276 true, // expected_allow_p2p_for_downloading
2277 false, // expected_allow_p2p_for_sharing
2278 "http://1.3.5.7/p2p"); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002279}
2280
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002281TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
Alex Vakulenkod2779df2014-06-16 13:19:00 -07002282 P2PTest(true, // initial_allow_p2p_for_downloading
2283 true, // initial_allow_p2p_for_sharing
2284 true, // omaha_disable_p2p_for_downloading
2285 true, // omaha_disable_p2p_for_sharing
2286 true, // payload_state_allow_p2p_attempt
2287 false, // expect_p2p_client_lookup
2288 "unset", // p2p_client_result_url
2289 false, // expected_allow_p2p_for_downloading
2290 false, // expected_allow_p2p_for_sharing
2291 ""); // expected_p2p_url
David Zeuthen8f191b22013-08-06 12:27:50 -07002292}
2293
Alex Deymof329b932014-10-30 01:37:48 -07002294bool OmahaRequestActionTest::InstallDateParseHelper(const string &elapsed_days,
2295 OmahaResponse *response) {
Alex Deymo8e18f932015-03-27 16:16:59 -07002296 fake_update_response_.elapsed_days = elapsed_days;
David Zeuthen639aa362014-02-03 16:23:44 -08002297 return
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002298 TestUpdateCheck(nullptr, // request_params
Alex Deymo8e18f932015-03-27 16:16:59 -07002299 fake_update_response_.GetUpdateResponse(),
David Zeuthen639aa362014-02-03 16:23:44 -08002300 -1,
2301 false, // ping_only
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07002302 ErrorCode::kSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002303 metrics::CheckResult::kUpdateAvailable,
2304 metrics::CheckReaction::kUpdating,
2305 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002306 response,
Alex Vakulenko88b591f2014-08-28 16:48:57 -07002307 nullptr);
David Zeuthen639aa362014-02-03 16:23:44 -08002308}
2309
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002310TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) {
David Zeuthen639aa362014-02-03 16:23:44 -08002311 OmahaResponse response;
David Zeuthen639aa362014-02-03 16:23:44 -08002312
Kevin Cernekee2494e282016-03-29 18:03:53 -07002313 // Simulate a successful update check that happens during OOBE. The
2314 // deadline in the response is needed to force the update attempt to
2315 // occur; responses without a deadline seen during OOBE will normally
2316 // return ErrorCode::kNonCriticalUpdateInOOBE.
2317 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
2318 fake_update_response_.deadline = "20101020";
2319
David Zeuthen639aa362014-02-03 16:23:44 -08002320 // Check that we parse elapsed_days in the Omaha Response correctly.
2321 // and that the kPrefsInstallDateDays value is written to.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002322 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2323 EXPECT_TRUE(InstallDateParseHelper("42", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002324 EXPECT_TRUE(response.update_exists);
2325 EXPECT_EQ(42, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002326 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002327 int64_t prefs_days;
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002328 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002329 EXPECT_EQ(prefs_days, 42);
2330
2331 // If there already is a value set, we shouldn't do anything.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002332 EXPECT_TRUE(InstallDateParseHelper("7", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002333 EXPECT_TRUE(response.update_exists);
2334 EXPECT_EQ(7, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002335 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002336 EXPECT_EQ(prefs_days, 42);
2337
2338 // Note that elapsed_days is not necessarily divisible by 7 so check
2339 // that we round down correctly when populating kPrefsInstallDateDays.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002340 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2341 EXPECT_TRUE(InstallDateParseHelper("23", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002342 EXPECT_TRUE(response.update_exists);
2343 EXPECT_EQ(23, response.install_date_days);
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002344 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
David Zeuthen639aa362014-02-03 16:23:44 -08002345 EXPECT_EQ(prefs_days, 21);
2346
2347 // Check that we correctly handle elapsed_days not being included in
2348 // the Omaha Response.
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002349 EXPECT_TRUE(InstallDateParseHelper("", &response));
David Zeuthen639aa362014-02-03 16:23:44 -08002350 EXPECT_TRUE(response.update_exists);
2351 EXPECT_EQ(-1, response.install_date_days);
David Zeuthen639aa362014-02-03 16:23:44 -08002352}
2353
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002354// If there is no prefs and OOBE is not complete, we should not
2355// report anything to Omaha.
2356TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) {
Kevin Cernekee2494e282016-03-29 18:03:53 -07002357 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002358 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2359 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2360}
David Zeuthen639aa362014-02-03 16:23:44 -08002361
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002362// If OOBE is complete and happened on a valid date (e.g. after Jan
2363// 1 2007 0:00 PST), that date should be used and written to
2364// prefs. However, first try with an invalid date and check we do
2365// nothing.
2366TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) {
2367 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2368 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2369 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1);
2370 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays));
2371}
David Zeuthen639aa362014-02-03 16:23:44 -08002372
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002373// Then check with a valid date. The date Jan 20, 2007 0:00 PST
2374// should yield an InstallDate of 14.
2375TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) {
2376 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2377 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2378 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
2379 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays));
David Zeuthen639aa362014-02-03 16:23:44 -08002380
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002381 int64_t prefs_days;
2382 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2383 EXPECT_EQ(prefs_days, 14);
2384}
David Zeuthen639aa362014-02-03 16:23:44 -08002385
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002386// Now that we have a valid date in prefs, check that we keep using
2387// that even if OOBE date reports something else. The date Jan 30,
2388// 2007 0:00 PST should yield an InstallDate of 28... but since
2389// there's a prefs file, we should still get 14.
2390TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) {
2391 // Set a valid date in the prefs first.
2392 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14));
David Zeuthen639aa362014-02-03 16:23:44 -08002393
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002394 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2395 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date);
2396 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002397
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002398 int64_t prefs_days;
2399 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2400 EXPECT_EQ(prefs_days, 14);
David Zeuthen639aa362014-02-03 16:23:44 -08002401
Alex Deymoe1e3afe2014-10-30 13:02:49 -07002402 // If we delete the prefs file, we should get 28 days.
2403 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays));
2404 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28);
2405 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days));
2406 EXPECT_EQ(prefs_days, 28);
David Zeuthen639aa362014-02-03 16:23:44 -08002407}
2408
Darin Petkov6a5b3222010-07-13 14:55:28 -07002409} // namespace chromeos_update_engine