blob: fdfb5909549a5d4e7e44e10dad0de05715bddb7f [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov6a5b3222010-07-13 14:55:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07007
Darin Petkov6a5b3222010-07-13 14:55:28 -07008#include <glib.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07009
Alex Vakulenko75039d72014-03-25 12:36:28 -070010#include <base/strings/string_util.h>
11#include <base/strings/stringprintf.h>
12#include <base/time/time.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070013#include "gtest/gtest.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070014
Darin Petkov6a5b3222010-07-13 14:55:28 -070015#include "update_engine/action_pipe.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070016#include "update_engine/constants.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070017#include "update_engine/mock_http_fetcher.h"
18#include "update_engine/omaha_hash_calculator.h"
19#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070020#include "update_engine/omaha_request_params.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070021#include "update_engine/prefs.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070022#include "update_engine/test_utils.h"
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070023#include "update_engine/utils.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070024
Darin Petkov1cbd78f2010-07-29 12:38:34 -070025using base::Time;
26using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070027using std::string;
28using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070029using testing::_;
30using testing::AllOf;
Jay Srinivasan34b5d862012-07-23 11:43:22 -070031using testing::DoAll;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070032using testing::Ge;
33using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080034using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070035using testing::Return;
36using testing::SetArgumentPointee;
David Zeuthen33bae492014-02-25 16:16:18 -080037using testing::AnyNumber;
Darin Petkov6a5b3222010-07-13 14:55:28 -070038
39namespace chromeos_update_engine {
40
Jay Srinivasanae4697c2013-03-18 17:08:08 -070041class OmahaRequestActionTest : public ::testing::Test {};
Darin Petkov6a5b3222010-07-13 14:55:28 -070042
43namespace {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070044
Jay Srinivasanae4697c2013-03-18 17:08:08 -070045MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070046OmahaRequestParams kDefaultTestParams(
Jay Srinivasanae4697c2013-03-18 17:08:08 -070047 &mock_system_state,
Darin Petkov1cbd78f2010-07-29 12:38:34 -070048 OmahaRequestParams::kOsPlatform,
49 OmahaRequestParams::kOsVersion,
50 "service_pack",
51 "x86-generic",
52 OmahaRequestParams::kAppId,
53 "0.1.0.0",
54 "en-US",
55 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070056 "OEM MODEL 09235 7471",
Chris Sosac1972482013-04-30 22:31:10 -070057 "ChromeOSFirmware.1.0",
58 "0X0A1",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070059 false, // delta okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080060 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -070061 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -070062 false, // update_disabled
63 "", // target_version_prefix
64 false, // use_p2p_for_downloading
65 false); // use_p2p_for_sharing
Darin Petkov1cbd78f2010-07-29 12:38:34 -070066
Darin Petkov6a5b3222010-07-13 14:55:28 -070067string GetNoUpdateResponse(const string& app_id) {
68 return string(
Jay Srinivasan23b92a52012-10-27 02:00:21 -070069 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
70 "<daystart elapsed_seconds=\"100\"/>"
71 "<app appid=\"") + app_id + "\" status=\"ok\"><ping "
72 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></response>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070073}
74
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070075string GetUpdateResponse2(const string& app_id,
Chris Sosa3b748432013-06-20 16:42:59 -070076 const string& version,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070077 const string& more_info_url,
78 const string& prompt,
79 const string& codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -070080 const string& filename,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070081 const string& hash,
82 const string& needsadmin,
83 const string& size,
84 const string& deadline,
David Zeuthen8f191b22013-08-06 12:27:50 -070085 const string& max_days_to_scatter,
David Zeuthen639aa362014-02-03 16:23:44 -080086 const string& elapsed_days,
David Zeuthen8f191b22013-08-06 12:27:50 -070087 bool disable_p2p_for_downloading,
88 bool disable_p2p_for_sharing) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -070089 string response =
90 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
91 "protocol=\"3.0\">"
David Zeuthen639aa362014-02-03 16:23:44 -080092 "<daystart elapsed_seconds=\"100\"" +
93 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\"")) +
94 "/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -070095 "<app appid=\"" + app_id + "\" status=\"ok\">"
96 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
97 "<urls><url codebase=\"" + codebase + "\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -070098 "<manifest version=\"" + version + "\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -070099 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
100 "size=\"" + size + "\"/></packages>"
101 "<actions><action event=\"postinstall\" "
Chris Sosa3b748432013-06-20 16:42:59 -0700102 "ChromeOSVersion=\"" + version + "\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700103 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
104 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800105 "IsDeltaPayload=\"true\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700106 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700107 "sha256=\"" + hash + "\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700108 "needsadmin=\"" + needsadmin + "\" " +
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700109 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
David Zeuthen8f191b22013-08-06 12:27:50 -0700110 (disable_p2p_for_downloading ?
111 "DisableP2PForDownloading=\"true\" " : "") +
112 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700113 "/></actions></manifest></updatecheck></app></response>";
114 LOG(INFO) << "Response = " << response;
115 return response;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700116}
117
Darin Petkov6a5b3222010-07-13 14:55:28 -0700118string GetUpdateResponse(const string& app_id,
Chris Sosa3b748432013-06-20 16:42:59 -0700119 const string& version,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700120 const string& more_info_url,
121 const string& prompt,
122 const string& codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700123 const string& filename,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700124 const string& hash,
125 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -0700126 const string& size,
127 const string& deadline) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700128 return GetUpdateResponse2(app_id,
Chris Sosa3b748432013-06-20 16:42:59 -0700129 version,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700130 more_info_url,
131 prompt,
132 codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700133 filename,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700134 hash,
135 needsadmin,
136 size,
137 deadline,
David Zeuthen8f191b22013-08-06 12:27:50 -0700138 "7",
David Zeuthen639aa362014-02-03 16:23:44 -0800139 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700140 false, // disable_p2p_for_downloading
141 false); // disable_p2p_for sharing
Darin Petkov6a5b3222010-07-13 14:55:28 -0700142}
143
144class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
145 public:
146 OmahaRequestActionTestProcessorDelegate()
147 : loop_(NULL),
David Zeuthena99981f2013-04-29 13:42:47 -0700148 expected_code_(kErrorCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700149 virtual ~OmahaRequestActionTestProcessorDelegate() {
150 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700151 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700152 ErrorCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700153 ASSERT_TRUE(loop_);
154 g_main_loop_quit(loop_);
155 }
156
157 virtual void ActionCompleted(ActionProcessor* processor,
158 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700159 ErrorCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700160 // make sure actions always succeed
161 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700162 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700163 else
David Zeuthena99981f2013-04-29 13:42:47 -0700164 EXPECT_EQ(kErrorCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700165 }
166 GMainLoop *loop_;
David Zeuthena99981f2013-04-29 13:42:47 -0700167 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700168};
169
170gboolean StartProcessorInRunLoop(gpointer data) {
171 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
172 processor->StartProcessing();
173 return FALSE;
174}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700175} // namespace {}
176
177class OutputObjectCollectorAction;
178
179template<>
180class ActionTraits<OutputObjectCollectorAction> {
181 public:
182 // Does not take an object for input
183 typedef OmahaResponse InputObjectType;
184 // On success, puts the output path on output
185 typedef NoneType OutputObjectType;
186};
187
188class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
189 public:
190 OutputObjectCollectorAction() : has_input_object_(false) {}
191 void PerformAction() {
192 // copy input object
193 has_input_object_ = HasInputObject();
194 if (has_input_object_)
195 omaha_response_ = GetInputObject();
David Zeuthena99981f2013-04-29 13:42:47 -0700196 processor_->ActionComplete(this, kErrorCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700197 }
198 // Should never be called
199 void TerminateProcessing() {
200 CHECK(false);
201 }
202 // Debugging/logging
203 static std::string StaticType() {
204 return "OutputObjectCollectorAction";
205 }
206 std::string Type() const { return StaticType(); }
207 bool has_input_object_;
208 OmahaResponse omaha_response_;
209};
210
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700211// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700212// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
David Zeuthen8f191b22013-08-06 12:27:50 -0700213// used. |payload_state| may be NULL, in which case a local mock is used.
214// |p2p_manager| may be NULL, in which case a local mock is used.
215// out_response may be NULL. If |fail_http_response_code| is non-negative,
Darin Petkov265f2902011-05-09 15:17:40 -0700216// the transfer will fail with that code. |ping_only| is passed through to the
217// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
218// post-data received by the mock HttpFetcher is returned.
David Zeuthen33bae492014-02-25 16:16:18 -0800219//
220// The |expected_check_result|, |expected_check_reaction| and
221// |expected_error_code| parameters are for checking expectations
222// about reporting UpdateEngine.Check.{Result,Reaction,DownloadError}
223// UMA statistics. Use the appropriate ::kUnset value to specify that
224// the given metric should not be reported.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700225bool TestUpdateCheck(PrefsInterface* prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -0700226 PayloadStateInterface *payload_state,
227 P2PManager *p2p_manager,
228 OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700229 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700230 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700231 bool ping_only,
David Zeuthena99981f2013-04-29 13:42:47 -0700232 ErrorCode expected_code,
David Zeuthen33bae492014-02-25 16:16:18 -0800233 metrics::CheckResult expected_check_result,
234 metrics::CheckReaction expected_check_reaction,
235 metrics::DownloadErrorCode expected_download_error_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700236 OmahaResponse* out_response,
237 vector<char>* out_post_data) {
238 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
239 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800240 http_response.size(),
241 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700242 if (fail_http_response_code >= 0) {
243 fetcher->FailTransfer(fail_http_response_code);
244 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800245 MockSystemState mock_system_state;
246 if (prefs)
247 mock_system_state.set_prefs(prefs);
David Zeuthen8f191b22013-08-06 12:27:50 -0700248 if (payload_state)
249 mock_system_state.set_payload_state(payload_state);
250 if (p2p_manager)
251 mock_system_state.set_p2p_manager(p2p_manager);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700252 mock_system_state.set_request_params(&params);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800253 OmahaRequestAction action(&mock_system_state,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700254 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700255 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700256 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700257 OmahaRequestActionTestProcessorDelegate delegate;
258 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700259 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700260
Darin Petkov6a5b3222010-07-13 14:55:28 -0700261 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700263 processor.EnqueueAction(&action);
264
265 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700266 BondActions(&action, &collector_action);
267 processor.EnqueueAction(&collector_action);
268
David Zeuthen33bae492014-02-25 16:16:18 -0800269 EXPECT_CALL(*mock_system_state.mock_metrics_lib(), SendEnumToUMA(_, _, _))
270 .Times(AnyNumber());
271 EXPECT_CALL(*mock_system_state.mock_metrics_lib(),
272 SendEnumToUMA(metrics::kMetricCheckResult,
273 static_cast<int>(expected_check_result),
274 static_cast<int>(metrics::CheckResult::kNumConstants) - 1))
275 .Times(expected_check_result == metrics::CheckResult::kUnset ? 0 : 1);
276 EXPECT_CALL(*mock_system_state.mock_metrics_lib(),
277 SendEnumToUMA(metrics::kMetricCheckReaction,
278 static_cast<int>(expected_check_reaction),
279 static_cast<int>(metrics::CheckReaction::kNumConstants) - 1))
280 .Times(expected_check_reaction == metrics::CheckReaction::kUnset ? 0 : 1);
281 EXPECT_CALL(*mock_system_state.mock_metrics_lib(),
282 SendEnumToUMA(metrics::kMetricCheckDownloadErrorCode,
283 static_cast<int>(expected_download_error_code),
284 static_cast<int>(metrics::DownloadErrorCode::kNumConstants) - 1))
285 .Times(expected_download_error_code == metrics::DownloadErrorCode::kUnset
286 ? 0 : 1);
287
Darin Petkov6a5b3222010-07-13 14:55:28 -0700288 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
289 g_main_loop_run(loop);
290 g_main_loop_unref(loop);
291 if (collector_action.has_input_object_ && out_response)
292 *out_response = collector_action.omaha_response_;
293 if (out_post_data)
294 *out_post_data = fetcher->post_data();
295 return collector_action.has_input_object_;
296}
297
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700298// Tests Event requests -- they should always succeed. |out_post_data|
299// may be null; if non-null, the post-data received by the mock
300// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700301void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700302 OmahaEvent* event,
303 const string& http_response,
304 vector<char>* out_post_data) {
305 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
306 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800307 http_response.size(),
308 NULL);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800309 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700310 mock_system_state.set_request_params(&params);
311 OmahaRequestAction action(&mock_system_state, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700312 OmahaRequestActionTestProcessorDelegate delegate;
313 delegate.loop_ = loop;
314 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700315 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700316 processor.EnqueueAction(&action);
317
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700318 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
319 g_main_loop_run(loop);
320 g_main_loop_unref(loop);
321 if (out_post_data)
322 *out_post_data = fetcher->post_data();
323}
324
Darin Petkov6a5b3222010-07-13 14:55:28 -0700325TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700326 OmahaResponse response;
327 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700328 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700329 NULL, // payload_state
330 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700331 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700332 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700333 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700334 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700335 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800336 metrics::CheckResult::kNoUpdateAvailable,
337 metrics::CheckReaction::kUnset,
338 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700339 &response,
340 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700341 EXPECT_FALSE(response.update_exists);
342}
343
344TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700345 OmahaResponse response;
346 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700347 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700348 NULL, // payload_state
349 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700350 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700351 GetUpdateResponse(OmahaRequestParams::kAppId,
352 "1.2.3.4", // version
353 "http://more/info",
354 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700355 "http://code/base/", // dl url
356 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700357 "HASH1234=", // checksum
358 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700359 "123", // size
360 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700361 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700362 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700363 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800364 metrics::CheckResult::kUpdateAvailable,
365 metrics::CheckReaction::kUpdating,
366 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700367 &response,
368 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700369 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700370 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700371 EXPECT_EQ("1.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800372 EXPECT_EQ("http://code/base/file.signed", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700373 EXPECT_EQ("http://more/info", response.more_info_url);
374 EXPECT_EQ("HASH1234=", response.hash);
375 EXPECT_EQ(123, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700376 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700377 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700378}
379
Jay Srinivasan0a708742012-03-20 11:26:12 -0700380TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) {
381 OmahaResponse response;
382 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700383 params.set_update_disabled(true);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700384 ASSERT_FALSE(
385 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700386 NULL, // payload_state
387 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -0700388 params,
389 GetUpdateResponse(OmahaRequestParams::kAppId,
390 "1.2.3.4", // version
391 "http://more/info",
392 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700393 "http://code/base/", // dl url
394 "file.signed", // file name
Jay Srinivasan0a708742012-03-20 11:26:12 -0700395 "HASH1234=", // checksum
396 "false", // needs admin
397 "123", // size
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700398 ""), // deadline
Jay Srinivasan0a708742012-03-20 11:26:12 -0700399 -1,
400 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700401 kErrorCodeOmahaUpdateIgnoredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800402 metrics::CheckResult::kUpdateAvailable,
403 metrics::CheckReaction::kIgnored,
404 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700405 &response,
406 NULL));
407 EXPECT_FALSE(response.update_exists);
408}
409
Jay Srinivasan0a708742012-03-20 11:26:12 -0700410TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) {
411 OmahaResponse response;
412 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700413 params.set_update_disabled(true);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700414 ASSERT_TRUE(
415 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700416 NULL, // payload_state
417 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -0700418 params,
419 GetNoUpdateResponse(OmahaRequestParams::kAppId),
420 -1,
421 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700422 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800423 metrics::CheckResult::kNoUpdateAvailable,
424 metrics::CheckReaction::kUnset,
425 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700426 &response,
427 NULL));
428 EXPECT_FALSE(response.update_exists);
429}
430
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700431TEST(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
432 OmahaResponse response;
433 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700434 params.set_wall_clock_based_wait_enabled(true);
435 params.set_update_check_count_wait_enabled(false);
436 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700437
438 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800439 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700440 &prefs_dir));
441 ScopedDirRemover temp_dir_remover(prefs_dir);
442
443 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700444 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700445 << "Failed to initialize preferences.";
446
447 ASSERT_FALSE(
448 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700449 NULL, // payload_state
450 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700451 params,
452 GetUpdateResponse2(OmahaRequestParams::kAppId,
453 "1.2.3.4", // version
454 "http://more/info",
455 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700456 "http://code/base/", // dl url
457 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700458 "HASH1234=", // checksum
459 "false", // needs admin
460 "123", // size
461 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700462 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800463 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700464 false, // disable_p2p_for_downloading
465 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700466 -1,
467 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700468 kErrorCodeOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800469 metrics::CheckResult::kUpdateAvailable,
470 metrics::CheckReaction::kDeferring,
471 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700472 &response,
473 NULL));
474 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700475
476 // Verify if we are interactive check we don't defer.
477 params.set_interactive(true);
478 ASSERT_TRUE(
479 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700480 NULL, // payload_state
481 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700482 params,
483 GetUpdateResponse2(OmahaRequestParams::kAppId,
484 "1.2.3.4", // version
485 "http://more/info",
486 "true", // prompt
487 "http://code/base/", // dl url
488 "file.signed", // file name
489 "HASH1234=", // checksum
490 "false", // needs admin
491 "123", // size
492 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700493 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800494 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700495 false, // disable_p2p_for_downloading
496 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700497 -1,
498 false, // ping_only
499 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800500 metrics::CheckResult::kUpdateAvailable,
501 metrics::CheckReaction::kUpdating,
502 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700503 &response,
504 NULL));
505 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700506}
507
508TEST(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
509 OmahaResponse response;
510 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700511 params.set_wall_clock_based_wait_enabled(false);
512 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700513
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700514 params.set_update_check_count_wait_enabled(true);
515 params.set_min_update_checks_needed(1);
516 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700517
518 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800519 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700520 &prefs_dir));
521 ScopedDirRemover temp_dir_remover(prefs_dir);
522
523 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700524 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700525 << "Failed to initialize preferences.";
526
527 ASSERT_TRUE(
528 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700529 NULL, // payload_state
530 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700531 params,
532 GetUpdateResponse2(OmahaRequestParams::kAppId,
533 "1.2.3.4", // version
534 "http://more/info",
535 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700536 "http://code/base/", // dl url
537 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700538 "HASH1234=", // checksum
539 "false", // needs admin
540 "123", // size
541 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700542 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800543 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700544 false, // disable_p2p_for_downloading
545 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700546 -1,
547 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700548 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800549 metrics::CheckResult::kUpdateAvailable,
550 metrics::CheckReaction::kUpdating,
551 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700552 &response,
553 NULL));
554 EXPECT_TRUE(response.update_exists);
555}
556
557TEST(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
558 OmahaResponse response;
559 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700560 params.set_wall_clock_based_wait_enabled(true);
561 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700562
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700563 params.set_update_check_count_wait_enabled(true);
564 params.set_min_update_checks_needed(1);
565 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700566
567 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800568 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700569 &prefs_dir));
570 ScopedDirRemover temp_dir_remover(prefs_dir);
571
572 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700573 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700574 << "Failed to initialize preferences.";
575
576 ASSERT_TRUE(
577 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700578 NULL, // payload_state
579 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700580 params,
581 GetUpdateResponse2(OmahaRequestParams::kAppId,
582 "1.2.3.4", // version
583 "http://more/info",
584 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700585 "http://code/base/", // dl url
586 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700587 "HASH1234=", // checksum
588 "false", // needs admin
589 "123", // size
590 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700591 "0", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800592 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700593 false, // disable_p2p_for_downloading
594 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700595 -1,
596 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700597 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800598 metrics::CheckResult::kUpdateAvailable,
599 metrics::CheckReaction::kUpdating,
600 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700601 &response,
602 NULL));
603 EXPECT_TRUE(response.update_exists);
604}
605
606
607TEST(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
608 OmahaResponse response;
609 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700610 params.set_wall_clock_based_wait_enabled(true);
611 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700612
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700613 params.set_update_check_count_wait_enabled(true);
614 params.set_min_update_checks_needed(0);
615 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700616
617 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800618 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700619 &prefs_dir));
620 ScopedDirRemover temp_dir_remover(prefs_dir);
621
622 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700623 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700624 << "Failed to initialize preferences.";
625
626 ASSERT_TRUE(TestUpdateCheck(
627 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700628 NULL, // payload_state
629 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700630 params,
631 GetUpdateResponse2(OmahaRequestParams::kAppId,
632 "1.2.3.4", // version
633 "http://more/info",
634 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700635 "http://code/base/", // dl url
636 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700637 "HASH1234=", // checksum
638 "false", // needs admin
639 "123", // size
640 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700641 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800642 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700643 false, // disable_p2p_for_downloading
644 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700645 -1,
646 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700647 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800648 metrics::CheckResult::kUpdateAvailable,
649 metrics::CheckReaction::kUpdating,
650 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700651 &response,
652 NULL));
653
654 int64 count;
655 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
656 ASSERT_TRUE(count == 0);
657 EXPECT_TRUE(response.update_exists);
658}
659
660TEST(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
661 OmahaResponse response;
662 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700663 params.set_wall_clock_based_wait_enabled(true);
664 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700665
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700666 params.set_update_check_count_wait_enabled(true);
667 params.set_min_update_checks_needed(1);
668 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700669
670 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800671 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700672 &prefs_dir));
673 ScopedDirRemover temp_dir_remover(prefs_dir);
674
675 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700676 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700677 << "Failed to initialize preferences.";
678
679 ASSERT_FALSE(TestUpdateCheck(
680 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700681 NULL, // payload_state
682 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700683 params,
684 GetUpdateResponse2(OmahaRequestParams::kAppId,
685 "1.2.3.4", // version
686 "http://more/info",
687 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700688 "http://code/base/", // dl url
689 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700690 "HASH1234=", // checksum
691 "false", // needs admin
692 "123", // size
693 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700694 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800695 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700696 false, // disable_p2p_for_downloading
697 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700698 -1,
699 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700700 kErrorCodeOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800701 metrics::CheckResult::kUpdateAvailable,
702 metrics::CheckReaction::kDeferring,
703 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700704 &response,
705 NULL));
706
707 int64 count;
708 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
709 ASSERT_TRUE(count > 0);
710 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700711
712 // Verify if we are interactive check we don't defer.
713 params.set_interactive(true);
714 ASSERT_TRUE(
715 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700716 NULL, // payload_state
717 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700718 params,
719 GetUpdateResponse2(OmahaRequestParams::kAppId,
720 "1.2.3.4", // version
721 "http://more/info",
722 "true", // prompt
723 "http://code/base/", // dl url
724 "file.signed", // file name
725 "HASH1234=", // checksum
726 "false", // needs admin
727 "123", // size
728 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700729 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800730 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700731 false, // disable_p2p_for_downloading
732 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700733 -1,
734 false, // ping_only
735 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800736 metrics::CheckResult::kUpdateAvailable,
737 metrics::CheckReaction::kUpdating,
738 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700739 &response,
740 NULL));
741 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700742}
743
744TEST(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
745 OmahaResponse response;
746 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700747 params.set_wall_clock_based_wait_enabled(true);
748 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700749
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700750 params.set_update_check_count_wait_enabled(true);
751 params.set_min_update_checks_needed(1);
752 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700753
754 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800755 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700756 &prefs_dir));
757 ScopedDirRemover temp_dir_remover(prefs_dir);
758
759 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700760 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700761 << "Failed to initialize preferences.";
762
763 ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, 5));
764
765 ASSERT_FALSE(TestUpdateCheck(
766 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700767 NULL, // payload_state
768 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700769 params,
770 GetUpdateResponse2(OmahaRequestParams::kAppId,
771 "1.2.3.4", // version
772 "http://more/info",
773 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700774 "http://code/base/", // dl url
775 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700776 "HASH1234=", // checksum
777 "false", // needs admin
778 "123", // size
779 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700780 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800781 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700782 false, // disable_p2p_for_downloading
783 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700784 -1,
785 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700786 kErrorCodeOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -0800787 metrics::CheckResult::kUpdateAvailable,
788 metrics::CheckReaction::kDeferring,
789 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700790 &response,
791 NULL));
792
793 int64 count;
794 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
795 // count remains the same, as the decrementing happens in update_attempter
796 // which this test doesn't exercise.
797 ASSERT_TRUE(count == 5);
798 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700799
800 // Verify if we are interactive check we don't defer.
801 params.set_interactive(true);
802 ASSERT_TRUE(
803 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700804 NULL, // payload_state
805 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700806 params,
807 GetUpdateResponse2(OmahaRequestParams::kAppId,
808 "1.2.3.4", // version
809 "http://more/info",
810 "true", // prompt
811 "http://code/base/", // dl url
812 "file.signed", // file name
813 "HASH1234=", // checksum
814 "false", // needs admin
815 "123", // size
816 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700817 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800818 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700819 false, // disable_p2p_for_downloading
820 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700821 -1,
822 false, // ping_only
823 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800824 metrics::CheckResult::kUpdateAvailable,
825 metrics::CheckReaction::kUpdating,
826 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -0700827 &response,
828 NULL));
829 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700830}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700831
Darin Petkov6a5b3222010-07-13 14:55:28 -0700832TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700833 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
834
835 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
836
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800837 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700838 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700839 mock_system_state.set_request_params(&params);
840 OmahaRequestAction action(&mock_system_state, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700841 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800842 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700843 NULL),
844 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700845 OmahaRequestActionTestProcessorDelegate delegate;
846 delegate.loop_ = loop;
847 ActionProcessor processor;
848 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700849 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700850
851 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
852 g_main_loop_run(loop);
853 g_main_loop_unref(loop);
854 EXPECT_FALSE(processor.IsRunning());
855}
856
857TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700858 OmahaResponse response;
859 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700860 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700861 NULL, // payload_state
862 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700863 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700864 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700865 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700866 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700867 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800868 metrics::CheckResult::kParsingError,
869 metrics::CheckReaction::kUnset,
870 metrics::DownloadErrorCode::kUnset,
Darin Petkovedc522e2010-11-05 09:35:17 -0700871 &response,
872 NULL));
873 EXPECT_FALSE(response.update_exists);
874}
875
876TEST(OmahaRequestActionTest, EmptyResponseTest) {
877 OmahaResponse response;
878 ASSERT_FALSE(
879 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700880 NULL, // payload_state
881 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -0700882 kDefaultTestParams,
883 "",
884 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700885 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700886 kErrorCodeOmahaRequestEmptyResponseError,
David Zeuthen33bae492014-02-25 16:16:18 -0800887 metrics::CheckResult::kParsingError,
888 metrics::CheckReaction::kUnset,
889 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700890 &response,
891 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700892 EXPECT_FALSE(response.update_exists);
893}
894
895TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700896 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700897 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700898 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700899 NULL, // payload_state
900 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700901 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700902 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
903 "<daystart elapsed_seconds=\"100\"/>"
904 "<app appid=\"foo\" status=\"ok\">"
905 "<ping status=\"ok\"/>"
906 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700907 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700908 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700909 kErrorCodeOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800910 metrics::CheckResult::kParsingError,
911 metrics::CheckReaction::kUnset,
912 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700913 &response,
914 NULL));
915 EXPECT_FALSE(response.update_exists);
916}
917
918TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700919 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700920 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700921 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700922 NULL, // payload_state
923 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700924 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700925 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
926 "<daystart elapsed_seconds=\"100\"/>"
927 "<app appid=\"foo\" status=\"ok\">"
928 "<ping status=\"ok\"/>"
929 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700930 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700931 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700932 kErrorCodeOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800933 metrics::CheckResult::kParsingError,
934 metrics::CheckReaction::kUnset,
935 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700936 &response,
937 NULL));
938 EXPECT_FALSE(response.update_exists);
939}
940
941TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700942 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700943 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700944 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700945 NULL, // payload_state
946 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700947 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700948 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
949 "<daystart elapsed_seconds=\"100\"/>"
950 "<app appid=\"foo\" status=\"ok\">"
951 "<ping status=\"ok\"/>"
952 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700953 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700954 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700955 kErrorCodeOmahaResponseInvalid,
David Zeuthen33bae492014-02-25 16:16:18 -0800956 metrics::CheckResult::kParsingError,
957 metrics::CheckReaction::kUnset,
958 metrics::DownloadErrorCode::kUnset,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700959 &response,
960 NULL));
961 EXPECT_FALSE(response.update_exists);
962}
963
964TEST(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700965 string input_response =
966 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
967 "<daystart elapsed_seconds=\"100\"/>"
968 "<app appid=\"xyz\" status=\"ok\">"
969 "<updatecheck status=\"ok\">"
970 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700971 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700972 "<packages><package hash=\"not-used\" name=\"f\" "
973 "size=\"587\"/></packages>"
974 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700975 "ChromeOSVersion=\"10.2.3.4\" "
976 "Prompt=\"false\" "
977 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800978 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700979 "sha256=\"lkq34j5345\" "
980 "needsadmin=\"true\" "
981 "/></actions></manifest></updatecheck></app></response>";
982 LOG(INFO) << "Input Response = " << input_response;
983
Darin Petkov6a5b3222010-07-13 14:55:28 -0700984 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700985 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700986 NULL, // payload_state
987 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700988 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700989 input_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700990 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700991 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700992 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -0800993 metrics::CheckResult::kUpdateAvailable,
994 metrics::CheckReaction::kUpdating,
995 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700996 &response,
997 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700998 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700999 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001000 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001001 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001002 EXPECT_EQ("lkq34j5345", response.hash);
1003 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001004 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -07001005 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001006}
1007
1008namespace {
1009class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
1010 public:
1011 void ProcessingStopped(const ActionProcessor* processor) {
1012 ASSERT_TRUE(loop_);
1013 g_main_loop_quit(loop_);
1014 }
1015 GMainLoop *loop_;
1016};
1017
1018gboolean TerminateTransferTestStarter(gpointer data) {
1019 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
1020 processor->StartProcessing();
1021 CHECK(processor->IsRunning());
1022 processor->StopProcessing();
1023 return FALSE;
1024}
1025} // namespace {}
1026
1027TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001028 string http_response("doesn't matter");
1029 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
1030
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001031 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001032 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001033 mock_system_state.set_request_params(&params);
1034 OmahaRequestAction action(&mock_system_state, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001035 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001036 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -07001037 NULL),
1038 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001039 TerminateEarlyTestProcessorDelegate delegate;
1040 delegate.loop_ = loop;
1041 ActionProcessor processor;
1042 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001043 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001044
1045 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
1046 g_main_loop_run(loop);
1047 g_main_loop_unref(loop);
1048}
1049
1050TEST(OmahaRequestActionTest, XmlEncodeTest) {
1051 EXPECT_EQ("ab", XmlEncode("ab"));
1052 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
1053 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
1054 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
1055 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
1056
1057 vector<char> post_data;
1058
1059 // Make sure XML Encode is being called on the params
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001060 MockSystemState mock_system_state;
1061 OmahaRequestParams params(&mock_system_state,
1062 OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -07001063 OmahaRequestParams::kOsVersion,
1064 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001065 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -07001066 OmahaRequestParams::kAppId,
1067 "0.1.0.0",
1068 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001069 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -07001070 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -07001071 "ChromeOSFirmware.1.0",
1072 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001073 false, // delta okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001074 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001075 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -07001076 false, // update_disabled
1077 "", // target_version_prefix
1078 false, // use_p2p_for_downloading
1079 false); // use_p2p_for_sharing
Darin Petkov6a5b3222010-07-13 14:55:28 -07001080 OmahaResponse response;
1081 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001082 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001083 NULL, // payload_state
1084 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001085 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001086 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001087 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001088 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001089 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001090 metrics::CheckResult::kParsingError,
1091 metrics::CheckReaction::kUnset,
1092 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001093 &response,
1094 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001095 // convert post_data to string
1096 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001097 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
1098 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001099 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
1100 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
1101 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
1102 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001103 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
1104 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001105}
1106
1107TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001108 OmahaResponse response;
1109 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001110 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001111 NULL, // payload_state
1112 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001113 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001114 GetUpdateResponse(OmahaRequestParams::kAppId,
1115 "1.2.3.4", // version
1116 "testthe&lt;url", // more info
1117 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001118 "testthe&amp;codebase/", // dl url
1119 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001120 "HASH1234=", // checksum
1121 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -07001122 "123", // size
1123 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -07001124 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001125 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001126 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001127 metrics::CheckResult::kUpdateAvailable,
1128 metrics::CheckReaction::kUpdating,
1129 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001130 &response,
1131 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001132
1133 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001134 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001135 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001136}
1137
1138TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001139 OmahaResponse response;
1140 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001141 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001142 NULL, // payload_state
1143 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001144 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001145 GetUpdateResponse(OmahaRequestParams::kAppId,
1146 "1.2.3.4", // version
1147 "theurl", // more info
1148 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001149 "thecodebase/", // dl url
1150 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001151 "HASH1234=", // checksum
1152 "false", // needs admin
1153 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -07001154 "123123123123123", // size
1155 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -07001156 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001157 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001158 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001159 metrics::CheckResult::kUpdateAvailable,
1160 metrics::CheckReaction::kUpdating,
1161 metrics::DownloadErrorCode::kUnset,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001162 &response,
1163 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001164
1165 EXPECT_EQ(response.size, 123123123123123ll);
1166}
1167
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001168TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
1169 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -08001170 NiceMock<PrefsMock> prefs;
1171 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1172 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001173 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1);
Darin Petkov95508da2011-01-05 12:42:29 -08001174 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001175 NULL, // payload_state
1176 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001177 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001178 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001179 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001180 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001181 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001182 metrics::CheckResult::kParsingError,
1183 metrics::CheckReaction::kUnset,
1184 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001185 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001186 &post_data));
1187 // convert post_data to string
1188 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -07001189 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001190 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001191 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001192 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001193 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1194 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001195 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1196 string::npos);
1197 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1198 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001199}
1200
Jay Srinivasan0a708742012-03-20 11:26:12 -07001201
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001202TEST(OmahaRequestActionTest, FormatUpdateDisabledOutputTest) {
Darin Petkov95508da2011-01-05 12:42:29 -08001203 vector<char> post_data;
1204 NiceMock<PrefsMock> prefs;
1205 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Jay Srinivasan0a708742012-03-20 11:26:12 -07001206 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001207 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1);
Jay Srinivasan0a708742012-03-20 11:26:12 -07001208 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001209 params.set_update_disabled(true);
Darin Petkov95508da2011-01-05 12:42:29 -08001210 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001211 NULL, // payload_state
1212 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -07001213 params,
Darin Petkov95508da2011-01-05 12:42:29 -08001214 "invalid xml>",
1215 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001216 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001217 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001218 metrics::CheckResult::kParsingError,
1219 metrics::CheckReaction::kUnset,
1220 metrics::DownloadErrorCode::kUnset,
Darin Petkov95508da2011-01-05 12:42:29 -08001221 NULL, // response
1222 &post_data));
1223 // convert post_data to string
1224 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -07001225 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001226 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001227 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001228 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -08001229 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1230 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001231 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1232 string::npos);
1233 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1234 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -08001235}
1236
Darin Petkove17f86b2010-07-20 09:12:01 -07001237TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
1238 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001239 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -07001240 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1241 "invalid xml>",
1242 &post_data);
1243 // convert post_data to string
1244 string post_str(&post_data[0], post_data.size());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001245 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001246 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001247 OmahaEvent::kTypeUpdateDownloadStarted,
1248 OmahaEvent::kResultSuccess);
1249 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001250 EXPECT_EQ(post_str.find("ping"), string::npos);
1251 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001252}
1253
1254TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
1255 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001256 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -07001257 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1258 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001259 kErrorCodeError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001260 "invalid xml>",
1261 &post_data);
1262 // convert post_data to string
1263 string post_str(&post_data[0], post_data.size());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001264 string expected_event = base::StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001265 " <event eventtype=\"%d\" eventresult=\"%d\" "
1266 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001267 OmahaEvent::kTypeDownloadComplete,
1268 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001269 kErrorCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001270 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001271 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001272}
1273
1274TEST(OmahaRequestActionTest, IsEventTest) {
1275 string http_response("doesn't matter");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001276 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001277 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001278 mock_system_state.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001279 OmahaRequestAction update_check_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001280 &mock_system_state,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001281 NULL,
1282 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001283 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -07001284 NULL),
1285 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001286 EXPECT_FALSE(update_check_action.IsEvent());
1287
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001288 params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001289 mock_system_state.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001290 OmahaRequestAction event_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001291 &mock_system_state,
Darin Petkove17f86b2010-07-20 09:12:01 -07001292 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001293 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001294 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -07001295 NULL),
1296 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001297 EXPECT_TRUE(event_action.IsEvent());
1298}
1299
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001300TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
1301 for (int i = 0; i < 2; i++) {
1302 bool delta_okay = i == 1;
1303 const char* delta_okay_str = delta_okay ? "true" : "false";
1304 vector<char> post_data;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001305 MockSystemState mock_system_state;
1306 OmahaRequestParams params(&mock_system_state,
1307 OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001308 OmahaRequestParams::kOsVersion,
1309 "service_pack",
1310 "x86-generic",
1311 OmahaRequestParams::kAppId,
1312 "0.1.0.0",
1313 "en-US",
1314 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001315 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001316 "ChromeOSFirmware.1.0",
1317 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001318 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001319 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001320 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -07001321 false, // update_disabled
1322 "", // target_version_prefix
1323 false, // use_p2p_for_downloading
1324 false); // use_p2p_for_sharing
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001325 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001326 NULL, // payload_state
1327 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001328 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001329 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001330 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001331 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001332 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001333 metrics::CheckResult::kParsingError,
1334 metrics::CheckReaction::kUnset,
1335 metrics::DownloadErrorCode::kUnset,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001336 NULL,
1337 &post_data));
1338 // convert post_data to string
Alex Vakulenko75039d72014-03-25 12:36:28 -07001339 string post_str(post_data.data(), post_data.size());
1340 EXPECT_NE(post_str.find(base::StringPrintf(" delta_okay=\"%s\"",
1341 delta_okay_str)),
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001342 string::npos)
1343 << "i = " << i;
1344 }
1345}
1346
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001347TEST(OmahaRequestActionTest, FormatInteractiveOutputTest) {
1348 for (int i = 0; i < 2; i++) {
1349 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001350 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001351 vector<char> post_data;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001352 MockSystemState mock_system_state;
1353 OmahaRequestParams params(&mock_system_state,
1354 OmahaRequestParams::kOsPlatform,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001355 OmahaRequestParams::kOsVersion,
1356 "service_pack",
1357 "x86-generic",
1358 OmahaRequestParams::kAppId,
1359 "0.1.0.0",
1360 "en-US",
1361 "unittest_track",
1362 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001363 "ChromeOSFirmware.1.0",
1364 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001365 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001366 interactive,
1367 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -07001368 false, // update_disabled
1369 "", // target_version_prefix
1370 false, // use_p2p_for_downloading
1371 false); // use_p2p_for_sharing
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001372 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001373 NULL, // payload_state
1374 NULL, // p2p_manager
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001375 params,
1376 "invalid xml>",
1377 -1,
1378 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001379 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001380 metrics::CheckResult::kParsingError,
1381 metrics::CheckReaction::kUnset,
1382 metrics::DownloadErrorCode::kUnset,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001383 NULL,
1384 &post_data));
1385 // convert post_data to string
1386 string post_str(&post_data[0], post_data.size());
Alex Vakulenko75039d72014-03-25 12:36:28 -07001387 EXPECT_NE(post_str.find(base::StringPrintf("installsource=\"%s\"",
1388 interactive_str)),
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001389 string::npos)
1390 << "i = " << i;
1391 }
1392}
1393
Darin Petkove17f86b2010-07-20 09:12:01 -07001394TEST(OmahaRequestActionTest, OmahaEventTest) {
1395 OmahaEvent default_event;
1396 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1397 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001398 EXPECT_EQ(kErrorCodeError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001399
1400 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1401 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1402 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001403 EXPECT_EQ(kErrorCodeSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001404
1405 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1406 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001407 kErrorCodeError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001408 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1409 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001410 EXPECT_EQ(kErrorCodeError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001411}
1412
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001413TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -07001414 for (int ping_only = 0; ping_only < 2; ping_only++) {
1415 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001416 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1417 .Times(AnyNumber());
1418 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov265f2902011-05-09 15:17:40 -07001419 // Add a few hours to the day difference to test no rounding, etc.
1420 int64_t five_days_ago =
1421 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1422 int64_t six_days_ago =
1423 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001424 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1425 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov265f2902011-05-09 15:17:40 -07001426 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1427 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1428 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1429 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
1430 vector<char> post_data;
1431 ASSERT_TRUE(
1432 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001433 NULL, // payload_state
1434 NULL, // p2p_manager
Darin Petkov265f2902011-05-09 15:17:40 -07001435 kDefaultTestParams,
1436 GetNoUpdateResponse(OmahaRequestParams::kAppId),
1437 -1,
1438 ping_only,
David Zeuthena99981f2013-04-29 13:42:47 -07001439 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001440 metrics::CheckResult::kUnset,
1441 metrics::CheckReaction::kUnset,
1442 metrics::DownloadErrorCode::kUnset,
Darin Petkov265f2902011-05-09 15:17:40 -07001443 NULL,
1444 &post_data));
1445 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001446 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
Darin Petkov265f2902011-05-09 15:17:40 -07001447 string::npos);
1448 if (ping_only) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001449 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001450 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1451 } else {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001452 EXPECT_NE(post_str.find("updatecheck"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001453 EXPECT_NE(post_str.find("previousversion"), string::npos);
1454 }
1455 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001456}
1457
1458TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001459 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001460 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1461 .Times(AnyNumber());
1462 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001463 int64_t three_days_ago =
1464 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1465 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001466 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1467 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001468 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1469 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1470 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1471 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1472 vector<char> post_data;
1473 ASSERT_TRUE(
1474 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001475 NULL, // payload_state
1476 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001477 kDefaultTestParams,
1478 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001479 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001480 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001481 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001482 metrics::CheckResult::kNoUpdateAvailable,
1483 metrics::CheckReaction::kUnset,
1484 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001485 NULL,
1486 &post_data));
1487 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001488 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001489 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001490}
1491
1492TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001493 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001494 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1495 .Times(AnyNumber());
1496 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001497 int64_t four_days_ago =
1498 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1499 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001500 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1501 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001502 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1503 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1504 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1505 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
1506 vector<char> post_data;
1507 ASSERT_TRUE(
1508 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001509 NULL, // payload_state
1510 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001511 kDefaultTestParams,
1512 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001513 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001514 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001515 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001516 metrics::CheckResult::kNoUpdateAvailable,
1517 metrics::CheckReaction::kUnset,
1518 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001519 NULL,
1520 &post_data));
1521 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001522 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001523 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001524}
1525
1526TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001527 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001528 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1529 .Times(AnyNumber());
1530 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001531 int64_t one_hour_ago =
1532 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001533 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1534 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001535 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1536 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1537 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1538 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1539 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1540 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1541 vector<char> post_data;
1542 ASSERT_TRUE(
1543 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001544 NULL, // payload_state
1545 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001546 kDefaultTestParams,
1547 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001548 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001549 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001550 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001551 metrics::CheckResult::kNoUpdateAvailable,
1552 metrics::CheckReaction::kUnset,
1553 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001554 NULL,
1555 &post_data));
1556 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001557 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001558}
1559
Thieu Leb44e9e82011-06-06 14:34:04 -07001560TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
1561 // This test ensures that we ignore empty ping only requests.
1562 NiceMock<PrefsMock> prefs;
1563 int64_t now = Time::Now().ToInternalValue();
1564 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1565 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1566 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1567 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1568 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1569 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1570 vector<char> post_data;
1571 EXPECT_TRUE(
1572 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001573 NULL, // payload_state
1574 NULL, // p2p_manager
Thieu Leb44e9e82011-06-06 14:34:04 -07001575 kDefaultTestParams,
1576 GetNoUpdateResponse(OmahaRequestParams::kAppId),
1577 -1,
1578 true, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001579 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001580 metrics::CheckResult::kUnset,
1581 metrics::CheckReaction::kUnset,
1582 metrics::DownloadErrorCode::kUnset,
Thieu Leb44e9e82011-06-06 14:34:04 -07001583 NULL,
1584 &post_data));
1585 EXPECT_EQ(post_data.size(), 0);
1586}
1587
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001588TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001589 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001590 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _))
1591 .Times(AnyNumber());
1592 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001593 int64_t future =
1594 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001595 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1596 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001597 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1598 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1599 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1600 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1601 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1602 .WillOnce(Return(true));
1603 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1604 .WillOnce(Return(true));
1605 vector<char> post_data;
1606 ASSERT_TRUE(
1607 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001608 NULL, // payload_state
1609 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001610 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001611 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1612 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001613 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001614 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001615 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001616 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001617 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001618 metrics::CheckResult::kNoUpdateAvailable,
1619 metrics::CheckReaction::kUnset,
1620 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001621 NULL,
1622 &post_data));
1623 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001624 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001625}
1626
1627TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
1628 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001629 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001630 // may fail if it runs for longer than 5 seconds. It shouldn't run
1631 // that long though.
1632 int64_t midnight =
1633 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1634 int64_t midnight_slack =
1635 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -08001636 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001637 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1638 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001639 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1640 AllOf(Ge(midnight), Le(midnight_slack))))
1641 .WillOnce(Return(true));
1642 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1643 AllOf(Ge(midnight), Le(midnight_slack))))
1644 .WillOnce(Return(true));
1645 ASSERT_TRUE(
1646 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001647 NULL, // payload_state
1648 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001649 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001650 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1651 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001652 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001653 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001654 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001655 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001656 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001657 metrics::CheckResult::kNoUpdateAvailable,
1658 metrics::CheckReaction::kUnset,
1659 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001660 NULL,
1661 NULL));
1662}
1663
1664TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001665 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001666 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1667 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001668 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1669 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1670 ASSERT_TRUE(
1671 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001672 NULL, // payload_state
1673 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001674 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001675 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1676 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001677 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001678 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001679 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001680 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001681 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001682 metrics::CheckResult::kNoUpdateAvailable,
1683 metrics::CheckReaction::kUnset,
1684 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001685 NULL,
1686 NULL));
1687}
1688
1689TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001690 NiceMock<PrefsMock> prefs;
David Zeuthen33bae492014-02-25 16:16:18 -08001691 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber());
1692 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber());
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001693 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1694 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1695 ASSERT_TRUE(
1696 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001697 NULL, // payload_state
1698 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001699 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001700 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1701 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001702 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001703 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001704 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001705 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001706 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001707 metrics::CheckResult::kNoUpdateAvailable,
1708 metrics::CheckReaction::kUnset,
1709 metrics::DownloadErrorCode::kUnset,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001710 NULL,
1711 NULL));
1712}
1713
Darin Petkov84c763c2010-07-29 16:27:58 -07001714TEST(OmahaRequestActionTest, NoUniqueIDTest) {
1715 vector<char> post_data;
1716 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001717 NULL, // payload_state
1718 NULL, // p2p_manager
Darin Petkov84c763c2010-07-29 16:27:58 -07001719 kDefaultTestParams,
1720 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001721 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001722 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001723 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001724 metrics::CheckResult::kParsingError,
1725 metrics::CheckReaction::kUnset,
1726 metrics::DownloadErrorCode::kUnset,
Darin Petkov84c763c2010-07-29 16:27:58 -07001727 NULL, // response
1728 &post_data));
1729 // convert post_data to string
1730 string post_str(&post_data[0], post_data.size());
1731 EXPECT_EQ(post_str.find("machineid="), string::npos);
1732 EXPECT_EQ(post_str.find("userid="), string::npos);
1733}
1734
Darin Petkovedc522e2010-11-05 09:35:17 -07001735TEST(OmahaRequestActionTest, NetworkFailureTest) {
1736 OmahaResponse response;
1737 ASSERT_FALSE(
1738 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001739 NULL, // payload_state
1740 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -07001741 kDefaultTestParams,
1742 "",
1743 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001744 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001745 static_cast<ErrorCode>(
1746 kErrorCodeOmahaRequestHTTPResponseBase + 501),
David Zeuthen33bae492014-02-25 16:16:18 -08001747 metrics::CheckResult::kDownloadError,
1748 metrics::CheckReaction::kUnset,
1749 static_cast<metrics::DownloadErrorCode>(501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001750 &response,
1751 NULL));
1752 EXPECT_FALSE(response.update_exists);
1753}
1754
1755TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
1756 OmahaResponse response;
1757 ASSERT_FALSE(
1758 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001759 NULL, // payload_state
1760 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -07001761 kDefaultTestParams,
1762 "",
1763 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001764 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001765 static_cast<ErrorCode>(
1766 kErrorCodeOmahaRequestHTTPResponseBase + 999),
David Zeuthen33bae492014-02-25 16:16:18 -08001767 metrics::CheckResult::kDownloadError,
1768 metrics::CheckReaction::kUnset,
1769 metrics::DownloadErrorCode::kHttpStatusOther,
Darin Petkovedc522e2010-11-05 09:35:17 -07001770 &response,
1771 NULL));
1772 EXPECT_FALSE(response.update_exists);
1773}
1774
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001775TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
1776 OmahaResponse response;
1777 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001778 params.set_wall_clock_based_wait_enabled(true);
1779 params.set_waiting_period(TimeDelta().FromDays(1));
1780 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001781
1782 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -08001783 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001784 &prefs_dir));
1785 ScopedDirRemover temp_dir_remover(prefs_dir);
1786
1787 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -07001788 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001789 << "Failed to initialize preferences.";
1790
1791 ASSERT_FALSE(TestUpdateCheck(
1792 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001793 NULL, // payload_state
1794 NULL, // p2p_manager
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001795 params,
1796 GetUpdateResponse2(OmahaRequestParams::kAppId,
1797 "1.2.3.4", // version
1798 "http://more/info",
1799 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001800 "http://code/base/", // dl url
1801 "file.signed", // file name
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001802 "HASH1234=", // checksum
1803 "false", // needs admin
1804 "123", // size
1805 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001806 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08001807 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07001808 false, // disable_p2p_for_downloading
1809 false), // disable_p2p_for sharing
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001810 -1,
1811 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001812 kErrorCodeOmahaUpdateDeferredPerPolicy,
David Zeuthen33bae492014-02-25 16:16:18 -08001813 metrics::CheckResult::kUpdateAvailable,
1814 metrics::CheckReaction::kDeferring,
1815 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001816 &response,
1817 NULL));
1818
1819 int64 timestamp = 0;
1820 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
1821 ASSERT_TRUE(timestamp > 0);
1822 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001823
1824 // Verify if we are interactive check we don't defer.
1825 params.set_interactive(true);
1826 ASSERT_TRUE(
1827 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001828 NULL, // payload_state
1829 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -07001830 params,
1831 GetUpdateResponse2(OmahaRequestParams::kAppId,
1832 "1.2.3.4", // version
1833 "http://more/info",
1834 "true", // prompt
1835 "http://code/base/", // dl url
1836 "file.signed", // file name
1837 "HASH1234=", // checksum
1838 "false", // needs admin
1839 "123", // size
1840 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001841 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08001842 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07001843 false, // disable_p2p_for_downloading
1844 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -07001845 -1,
1846 false, // ping_only
1847 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001848 metrics::CheckResult::kUpdateAvailable,
1849 metrics::CheckReaction::kUpdating,
1850 metrics::DownloadErrorCode::kUnset,
Chris Sosa968d0572013-08-23 14:46:02 -07001851 &response,
1852 NULL));
1853 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001854}
1855
1856TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
1857 OmahaResponse response;
1858 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001859 params.set_wall_clock_based_wait_enabled(true);
1860 params.set_waiting_period(TimeDelta().FromDays(1));
1861 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001862
1863 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -08001864 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001865 &prefs_dir));
1866 ScopedDirRemover temp_dir_remover(prefs_dir);
1867
1868 Prefs prefs;
Alex Vakulenko75039d72014-03-25 12:36:28 -07001869 LOG_IF(ERROR, !prefs.Init(base::FilePath(prefs_dir)))
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001870 << "Failed to initialize preferences.";
1871
1872 // Set the timestamp to a very old value such that it exceeds the
1873 // waiting period set above.
1874 Time t1;
1875 Time::FromString("1/1/2012", &t1);
1876 ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
1877 ASSERT_TRUE(TestUpdateCheck(
1878 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001879 NULL, // payload_state
1880 NULL, // p2p_manager
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001881 params,
1882 GetUpdateResponse2(OmahaRequestParams::kAppId,
1883 "1.2.3.4", // version
1884 "http://more/info",
1885 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001886 "http://code/base/", // dl url
1887 "file.signed", // file name
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001888 "HASH1234=", // checksum
1889 "false", // needs admin
1890 "123", // size
1891 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001892 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08001893 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07001894 false, // disable_p2p_for_downloading
1895 false), // disable_p2p_for sharing
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001896 -1,
1897 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001898 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08001899 metrics::CheckResult::kUpdateAvailable,
1900 metrics::CheckReaction::kUpdating,
1901 metrics::DownloadErrorCode::kUnset,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001902 &response,
1903 NULL));
1904
1905 EXPECT_TRUE(response.update_exists);
1906
1907 // Make sure the timestamp t1 is unchanged showing that it was reused.
1908 int64 timestamp = 0;
1909 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
1910 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1911}
1912
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001913TEST(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001914 // Create a uniquely named test directory.
1915 string test_dir;
1916 ASSERT_TRUE(utils::MakeTempDirectory(
1917 "omaha_request_action-test-XXXXXX", &test_dir));
1918
1919 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1920 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001921 kStatefulPartition + "/etc"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001922 vector<char> post_data;
1923 NiceMock<PrefsMock> prefs;
1924 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001925 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001926 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1927 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1928 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1929 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001930 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001931 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
1932 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1933 OmahaRequestParams params = kDefaultTestParams;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001934 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001935 params.SetLockDown(false);
1936 params.Init("1.2.3.4", "", 0);
1937 EXPECT_EQ("canary-channel", params.current_channel());
1938 EXPECT_EQ("stable-channel", params.target_channel());
1939 EXPECT_TRUE(params.to_more_stable_channel());
1940 EXPECT_TRUE(params.is_powerwash_allowed());
1941 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001942 NULL, // payload_state
1943 NULL, // p2p_manager
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001944 params,
1945 "invalid xml>",
1946 -1,
1947 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001948 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001949 metrics::CheckResult::kParsingError,
1950 metrics::CheckReaction::kUnset,
1951 metrics::DownloadErrorCode::kUnset,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001952 NULL, // response
1953 &post_data));
1954 // convert post_data to string
1955 string post_str(&post_data[0], post_data.size());
1956 EXPECT_NE(string::npos, post_str.find(
1957 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1958 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1959 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001960
1961 ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001962}
1963
1964TEST(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001965 // Create a uniquely named test directory.
1966 string test_dir;
1967 ASSERT_TRUE(utils::MakeTempDirectory(
1968 "omaha_request_action-test-XXXXXX", &test_dir));
1969
1970 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1971 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001972 kStatefulPartition + "/etc"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001973 vector<char> post_data;
1974 NiceMock<PrefsMock> prefs;
1975 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001976 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001977 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1978 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1979 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1980 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001981 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001982 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1983 OmahaRequestParams params = kDefaultTestParams;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001984 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001985 params.SetLockDown(false);
1986 params.Init("5.6.7.8", "", 0);
1987 EXPECT_EQ("stable-channel", params.current_channel());
1988 EXPECT_EQ("canary-channel", params.target_channel());
1989 EXPECT_FALSE(params.to_more_stable_channel());
1990 EXPECT_FALSE(params.is_powerwash_allowed());
1991 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001992 NULL, // payload_state
1993 NULL, // p2p_manager
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001994 params,
1995 "invalid xml>",
1996 -1,
1997 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001998 kErrorCodeOmahaRequestXMLParseError,
David Zeuthen33bae492014-02-25 16:16:18 -08001999 metrics::CheckResult::kParsingError,
2000 metrics::CheckReaction::kUnset,
2001 metrics::DownloadErrorCode::kUnset,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002002 NULL, // response
2003 &post_data));
2004 // convert post_data to string
2005 string post_str(&post_data[0], post_data.size());
2006 EXPECT_NE(string::npos, post_str.find(
2007 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
2008 "version=\"5.6.7.8\" "
2009 "track=\"canary-channel\" from_track=\"stable-channel\""));
2010 EXPECT_EQ(string::npos, post_str.find( "from_version"));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07002011
2012 ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07002013}
2014
David Zeuthen8f191b22013-08-06 12:27:50 -07002015void P2PTest(bool initial_allow_p2p_for_downloading,
2016 bool initial_allow_p2p_for_sharing,
2017 bool omaha_disable_p2p_for_downloading,
2018 bool omaha_disable_p2p_for_sharing,
2019 bool payload_state_allow_p2p_attempt,
2020 bool expect_p2p_client_lookup,
2021 const string& p2p_client_result_url,
2022 bool expected_allow_p2p_for_downloading,
2023 bool expected_allow_p2p_for_sharing,
2024 const string& expected_p2p_url) {
2025 OmahaResponse response;
2026 OmahaRequestParams request_params = kDefaultTestParams;
2027 request_params.set_use_p2p_for_downloading(initial_allow_p2p_for_downloading);
2028 request_params.set_use_p2p_for_sharing(initial_allow_p2p_for_sharing);
2029
2030 MockPayloadState mock_payload_state;
2031 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
2032 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
2033 MockP2PManager mock_p2p_manager;
2034 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
2035
David Zeuthen4cc5ed22014-01-15 12:35:03 -08002036 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
2037 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07002038 .Times(expect_p2p_client_lookup ? 1 : 0);
2039
2040 ASSERT_TRUE(
2041 TestUpdateCheck(NULL, // prefs
2042 &mock_payload_state,
2043 &mock_p2p_manager,
2044 request_params,
2045 GetUpdateResponse2(OmahaRequestParams::kAppId,
2046 "1.2.3.4", // version
2047 "http://more/info",
2048 "true", // prompt
2049 "http://code/base/", // dl url
2050 "file.signed", // file name
2051 "HASH1234=", // checksum
2052 "false", // needs admin
2053 "123", // size
2054 "", // deadline
2055 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08002056 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07002057 omaha_disable_p2p_for_downloading,
2058 omaha_disable_p2p_for_sharing),
2059 -1,
2060 false, // ping_only
2061 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002062 metrics::CheckResult::kUpdateAvailable,
2063 metrics::CheckReaction::kUpdating,
2064 metrics::DownloadErrorCode::kUnset,
David Zeuthen8f191b22013-08-06 12:27:50 -07002065 &response,
2066 NULL));
2067 EXPECT_TRUE(response.update_exists);
2068
2069 EXPECT_EQ(response.disable_p2p_for_downloading,
2070 omaha_disable_p2p_for_downloading);
2071 EXPECT_EQ(response.disable_p2p_for_sharing,
2072 omaha_disable_p2p_for_sharing);
2073
2074 EXPECT_EQ(request_params.use_p2p_for_downloading(),
2075 expected_allow_p2p_for_downloading);
2076
2077 EXPECT_EQ(request_params.use_p2p_for_sharing(),
2078 expected_allow_p2p_for_sharing);
2079
2080 EXPECT_EQ(request_params.p2p_url(), expected_p2p_url);
2081}
2082
2083TEST(OmahaRequestActionTest, P2PWithPeer) {
2084 P2PTest(true, // initial_allow_p2p_for_downloading
2085 true, // initial_allow_p2p_for_sharing
2086 false, // omaha_disable_p2p_for_downloading
2087 false, // omaha_disable_p2p_for_sharing
2088 true, // payload_state_allow_p2p_attempt
2089 true, // expect_p2p_client_lookup
2090 "http://1.3.5.7/p2p", // p2p_client_result_url
2091 true, // expected_allow_p2p_for_downloading
2092 true, // expected_allow_p2p_for_sharing
2093 "http://1.3.5.7/p2p"); // expected_p2p_url
2094}
2095
2096TEST(OmahaRequestActionTest, P2PWithoutPeer) {
2097 P2PTest(true, // initial_allow_p2p_for_downloading
2098 true, // initial_allow_p2p_for_sharing
2099 false, // omaha_disable_p2p_for_downloading
2100 false, // omaha_disable_p2p_for_sharing
2101 true, // payload_state_allow_p2p_attempt
2102 true, // expect_p2p_client_lookup
2103 "", // p2p_client_result_url
2104 false, // expected_allow_p2p_for_downloading
2105 true, // expected_allow_p2p_for_sharing
2106 ""); // expected_p2p_url
2107}
2108
2109TEST(OmahaRequestActionTest, P2PDownloadNotAllowed) {
2110 P2PTest(false, // initial_allow_p2p_for_downloading
2111 true, // initial_allow_p2p_for_sharing
2112 false, // omaha_disable_p2p_for_downloading
2113 false, // omaha_disable_p2p_for_sharing
2114 true, // payload_state_allow_p2p_attempt
2115 false, // expect_p2p_client_lookup
2116 "unset", // p2p_client_result_url
2117 false, // expected_allow_p2p_for_downloading
2118 true, // expected_allow_p2p_for_sharing
2119 ""); // expected_p2p_url
2120}
2121
2122TEST(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
2123 P2PTest(true, // initial_allow_p2p_for_downloading
2124 true, // initial_allow_p2p_for_sharing
2125 true, // omaha_disable_p2p_for_downloading
2126 false, // omaha_disable_p2p_for_sharing
2127 true, // payload_state_allow_p2p_attempt
2128 false, // expect_p2p_client_lookup
2129 "unset", // p2p_client_result_url
2130 false, // expected_allow_p2p_for_downloading
2131 true, // expected_allow_p2p_for_sharing
2132 ""); // expected_p2p_url
2133}
2134
2135TEST(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
2136 P2PTest(true, // initial_allow_p2p_for_downloading
2137 true, // initial_allow_p2p_for_sharing
2138 false, // omaha_disable_p2p_for_downloading
2139 true, // omaha_disable_p2p_for_sharing
2140 true, // payload_state_allow_p2p_attempt
2141 true, // expect_p2p_client_lookup
2142 "http://1.3.5.7/p2p", // p2p_client_result_url
2143 true, // expected_allow_p2p_for_downloading
2144 false, // expected_allow_p2p_for_sharing
2145 "http://1.3.5.7/p2p"); // expected_p2p_url
2146}
2147
2148TEST(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
2149 P2PTest(true, // initial_allow_p2p_for_downloading
2150 true, // initial_allow_p2p_for_sharing
2151 true, // omaha_disable_p2p_for_downloading
2152 true, // omaha_disable_p2p_for_sharing
2153 true, // payload_state_allow_p2p_attempt
2154 false, // expect_p2p_client_lookup
2155 "unset", // p2p_client_result_url
2156 false, // expected_allow_p2p_for_downloading
2157 false, // expected_allow_p2p_for_sharing
2158 ""); // expected_p2p_url
2159}
2160
David Zeuthen639aa362014-02-03 16:23:44 -08002161bool InstallDateParseHelper(const std::string &elapsed_days,
2162 PrefsInterface* prefs,
2163 OmahaResponse *response) {
2164 return
2165 TestUpdateCheck(prefs,
2166 NULL, // payload_state
2167 NULL, // p2p_manager
2168 kDefaultTestParams,
2169 GetUpdateResponse2(OmahaRequestParams::kAppId,
2170 "1.2.3.4", // version
2171 "http://more/info",
2172 "true", // prompt
2173 "http://code/base/", // dl url
2174 "file.signed", // file name
2175 "HASH1234=", // checksum
2176 "false", // needs admin
2177 "123", // size
2178 "", // deadline
2179 "7", // max days to scatter
2180 elapsed_days,
2181 false, // disable_p2p_for_downloading
2182 false), // disable_p2p_for sharing
2183 -1,
2184 false, // ping_only
2185 kErrorCodeSuccess,
David Zeuthen33bae492014-02-25 16:16:18 -08002186 metrics::CheckResult::kUpdateAvailable,
2187 metrics::CheckReaction::kUpdating,
2188 metrics::DownloadErrorCode::kUnset,
David Zeuthen639aa362014-02-03 16:23:44 -08002189 response,
2190 NULL);
2191}
2192
2193TEST(OmahaRequestActionTest, ParseInstallDateFromResponse) {
2194 OmahaResponse response;
2195 string temp_dir;
2196 Prefs prefs;
2197 EXPECT_TRUE(utils::MakeTempDirectory("ParseInstallDateFromResponse.XXXXXX",
2198 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07002199 prefs.Init(base::FilePath(temp_dir));
David Zeuthen639aa362014-02-03 16:23:44 -08002200
2201 // Check that we parse elapsed_days in the Omaha Response correctly.
2202 // and that the kPrefsInstallDateDays value is written to.
2203 EXPECT_FALSE(prefs.Exists(kPrefsInstallDateDays));
2204 EXPECT_TRUE(InstallDateParseHelper("42", &prefs, &response));
2205 EXPECT_TRUE(response.update_exists);
2206 EXPECT_EQ(42, response.install_date_days);
2207 EXPECT_TRUE(prefs.Exists(kPrefsInstallDateDays));
2208 int64_t prefs_days;
2209 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2210 EXPECT_EQ(prefs_days, 42);
2211
2212 // If there already is a value set, we shouldn't do anything.
2213 EXPECT_TRUE(InstallDateParseHelper("7", &prefs, &response));
2214 EXPECT_TRUE(response.update_exists);
2215 EXPECT_EQ(7, response.install_date_days);
2216 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2217 EXPECT_EQ(prefs_days, 42);
2218
2219 // Note that elapsed_days is not necessarily divisible by 7 so check
2220 // that we round down correctly when populating kPrefsInstallDateDays.
2221 EXPECT_TRUE(prefs.Delete(kPrefsInstallDateDays));
2222 EXPECT_TRUE(InstallDateParseHelper("23", &prefs, &response));
2223 EXPECT_TRUE(response.update_exists);
2224 EXPECT_EQ(23, response.install_date_days);
2225 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2226 EXPECT_EQ(prefs_days, 21);
2227
2228 // Check that we correctly handle elapsed_days not being included in
2229 // the Omaha Response.
2230 EXPECT_TRUE(InstallDateParseHelper("", &prefs, &response));
2231 EXPECT_TRUE(response.update_exists);
2232 EXPECT_EQ(-1, response.install_date_days);
2233
2234 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
2235}
2236
2237TEST(OmahaRequestActionTest, GetInstallDate) {
2238 string temp_dir;
2239 Prefs prefs;
2240 EXPECT_TRUE(utils::MakeTempDirectory("GetInstallDate.XXXXXX",
2241 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -07002242 prefs.Init(base::FilePath(temp_dir));
David Zeuthen639aa362014-02-03 16:23:44 -08002243
2244 // If there is no prefs and OOBE is not complete, we should not
2245 // report anything to Omaha.
2246 {
2247 NiceMock<MockSystemState> system_state;
2248 system_state.set_prefs(&prefs);
2249 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), -1);
2250 EXPECT_FALSE(prefs.Exists(kPrefsInstallDateDays));
2251 }
2252
2253 // If OOBE is complete and happened on a valid date (e.g. after Jan
2254 // 1 2007 0:00 PST), that date should be used and written to
2255 // prefs. However, first try with an invalid date and check we do
2256 // nothing.
2257 {
2258 NiceMock<MockSystemState> system_state;
2259 system_state.set_prefs(&prefs);
2260
2261 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2262 EXPECT_CALL(system_state,
2263 IsOOBEComplete(_))
2264 .WillRepeatedly(DoAll(SetArgumentPointee<0>(oobe_date),
2265 Return(true)));
2266 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), -1);
2267 EXPECT_FALSE(prefs.Exists(kPrefsInstallDateDays));
2268 }
2269
2270 // Then check with a valid date. The date Jan 20, 2007 0:00 PST
2271 // should yield an InstallDate of 14.
2272 {
2273 NiceMock<MockSystemState> system_state;
2274 system_state.set_prefs(&prefs);
2275
2276 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2277 EXPECT_CALL(system_state,
2278 IsOOBEComplete(_))
2279 .WillRepeatedly(DoAll(SetArgumentPointee<0>(oobe_date),
2280 Return(true)));
2281 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), 14);
2282 EXPECT_TRUE(prefs.Exists(kPrefsInstallDateDays));
2283
2284 int64_t prefs_days;
2285 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2286 EXPECT_EQ(prefs_days, 14);
2287 }
2288
2289 // Now that we have a valid date in prefs, check that we keep using
2290 // that even if OOBE date reports something else. The date Jan 30,
2291 // 2007 0:00 PST should yield an InstallDate of 28... but since
2292 // there's a prefs file, we should still get 14.
2293 {
2294 NiceMock<MockSystemState> system_state;
2295 system_state.set_prefs(&prefs);
2296
2297 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2298 EXPECT_CALL(system_state,
2299 IsOOBEComplete(_))
2300 .WillRepeatedly(DoAll(SetArgumentPointee<0>(oobe_date),
2301 Return(true)));
2302 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), 14);
2303
2304 int64_t prefs_days;
2305 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2306 EXPECT_EQ(prefs_days, 14);
2307
2308 // If we delete the prefs file, we should get 28 days.
2309 EXPECT_TRUE(prefs.Delete(kPrefsInstallDateDays));
2310 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), 28);
2311 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2312 EXPECT_EQ(prefs_days, 28);
2313 }
2314
2315 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
2316}
2317
Darin Petkov6a5b3222010-07-13 14:55:28 -07002318} // namespace chromeos_update_engine