blob: 0983ed9db31d9ce7f77254dc288469e0f5a1c959 [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
10#include "base/string_util.h"
Mike Frysinger8155d082012-04-06 15:23:18 -040011#include <base/stringprintf.h>
Darin Petkov1cbd78f2010-07-29 12:38:34 -070012#include "base/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;
Darin Petkov6a5b3222010-07-13 14:55:28 -070037
38namespace chromeos_update_engine {
39
Jay Srinivasanae4697c2013-03-18 17:08:08 -070040class OmahaRequestActionTest : public ::testing::Test {};
Darin Petkov6a5b3222010-07-13 14:55:28 -070041
42namespace {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070043
Jay Srinivasanae4697c2013-03-18 17:08:08 -070044MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070045OmahaRequestParams kDefaultTestParams(
Jay Srinivasanae4697c2013-03-18 17:08:08 -070046 &mock_system_state,
Darin Petkov1cbd78f2010-07-29 12:38:34 -070047 OmahaRequestParams::kOsPlatform,
48 OmahaRequestParams::kOsVersion,
49 "service_pack",
50 "x86-generic",
51 OmahaRequestParams::kAppId,
52 "0.1.0.0",
53 "en-US",
54 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070055 "OEM MODEL 09235 7471",
Chris Sosac1972482013-04-30 22:31:10 -070056 "ChromeOSFirmware.1.0",
57 "0X0A1",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070058 false, // delta okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -080059 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -070060 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -070061 false, // update_disabled
62 "", // target_version_prefix
63 false, // use_p2p_for_downloading
64 false); // use_p2p_for_sharing
Darin Petkov1cbd78f2010-07-29 12:38:34 -070065
Darin Petkov6a5b3222010-07-13 14:55:28 -070066string GetNoUpdateResponse(const string& app_id) {
67 return string(
Jay Srinivasan23b92a52012-10-27 02:00:21 -070068 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
69 "<daystart elapsed_seconds=\"100\"/>"
70 "<app appid=\"") + app_id + "\" status=\"ok\"><ping "
71 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></response>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070072}
73
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070074string GetUpdateResponse2(const string& app_id,
Chris Sosa3b748432013-06-20 16:42:59 -070075 const string& version,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070076 const string& more_info_url,
77 const string& prompt,
78 const string& codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -070079 const string& filename,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070080 const string& hash,
81 const string& needsadmin,
82 const string& size,
83 const string& deadline,
David Zeuthen8f191b22013-08-06 12:27:50 -070084 const string& max_days_to_scatter,
David Zeuthen639aa362014-02-03 16:23:44 -080085 const string& elapsed_days,
David Zeuthen8f191b22013-08-06 12:27:50 -070086 bool disable_p2p_for_downloading,
87 bool disable_p2p_for_sharing) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -070088 string response =
89 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
90 "protocol=\"3.0\">"
David Zeuthen639aa362014-02-03 16:23:44 -080091 "<daystart elapsed_seconds=\"100\"" +
92 (elapsed_days.empty() ? "" : (" elapsed_days=\"" + elapsed_days + "\"")) +
93 "/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -070094 "<app appid=\"" + app_id + "\" status=\"ok\">"
95 "<ping status=\"ok\"/><updatecheck status=\"ok\">"
96 "<urls><url codebase=\"" + codebase + "\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -070097 "<manifest version=\"" + version + "\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -070098 "<packages><package hash=\"not-used\" name=\"" + filename + "\" "
99 "size=\"" + size + "\"/></packages>"
100 "<actions><action event=\"postinstall\" "
Chris Sosa3b748432013-06-20 16:42:59 -0700101 "ChromeOSVersion=\"" + version + "\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700102 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
103 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800104 "IsDeltaPayload=\"true\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700105 "MaxDaysToScatter=\"" + max_days_to_scatter + "\" "
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700106 "sha256=\"" + hash + "\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700107 "needsadmin=\"" + needsadmin + "\" " +
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700108 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) +
David Zeuthen8f191b22013-08-06 12:27:50 -0700109 (disable_p2p_for_downloading ?
110 "DisableP2PForDownloading=\"true\" " : "") +
111 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") +
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700112 "/></actions></manifest></updatecheck></app></response>";
113 LOG(INFO) << "Response = " << response;
114 return response;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700115}
116
Darin Petkov6a5b3222010-07-13 14:55:28 -0700117string GetUpdateResponse(const string& app_id,
Chris Sosa3b748432013-06-20 16:42:59 -0700118 const string& version,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700119 const string& more_info_url,
120 const string& prompt,
121 const string& codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700122 const string& filename,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700123 const string& hash,
124 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -0700125 const string& size,
126 const string& deadline) {
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700127 return GetUpdateResponse2(app_id,
Chris Sosa3b748432013-06-20 16:42:59 -0700128 version,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700129 more_info_url,
130 prompt,
131 codebase,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700132 filename,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700133 hash,
134 needsadmin,
135 size,
136 deadline,
David Zeuthen8f191b22013-08-06 12:27:50 -0700137 "7",
David Zeuthen639aa362014-02-03 16:23:44 -0800138 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700139 false, // disable_p2p_for_downloading
140 false); // disable_p2p_for sharing
Darin Petkov6a5b3222010-07-13 14:55:28 -0700141}
142
143class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
144 public:
145 OmahaRequestActionTestProcessorDelegate()
146 : loop_(NULL),
David Zeuthena99981f2013-04-29 13:42:47 -0700147 expected_code_(kErrorCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700148 virtual ~OmahaRequestActionTestProcessorDelegate() {
149 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700150 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -0700151 ErrorCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700152 ASSERT_TRUE(loop_);
153 g_main_loop_quit(loop_);
154 }
155
156 virtual void ActionCompleted(ActionProcessor* processor,
157 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -0700158 ErrorCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700159 // make sure actions always succeed
160 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700161 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700162 else
David Zeuthena99981f2013-04-29 13:42:47 -0700163 EXPECT_EQ(kErrorCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700164 }
165 GMainLoop *loop_;
David Zeuthena99981f2013-04-29 13:42:47 -0700166 ErrorCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700167};
168
169gboolean StartProcessorInRunLoop(gpointer data) {
170 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
171 processor->StartProcessing();
172 return FALSE;
173}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700174} // namespace {}
175
176class OutputObjectCollectorAction;
177
178template<>
179class ActionTraits<OutputObjectCollectorAction> {
180 public:
181 // Does not take an object for input
182 typedef OmahaResponse InputObjectType;
183 // On success, puts the output path on output
184 typedef NoneType OutputObjectType;
185};
186
187class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
188 public:
189 OutputObjectCollectorAction() : has_input_object_(false) {}
190 void PerformAction() {
191 // copy input object
192 has_input_object_ = HasInputObject();
193 if (has_input_object_)
194 omaha_response_ = GetInputObject();
David Zeuthena99981f2013-04-29 13:42:47 -0700195 processor_->ActionComplete(this, kErrorCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700196 }
197 // Should never be called
198 void TerminateProcessing() {
199 CHECK(false);
200 }
201 // Debugging/logging
202 static std::string StaticType() {
203 return "OutputObjectCollectorAction";
204 }
205 std::string Type() const { return StaticType(); }
206 bool has_input_object_;
207 OmahaResponse omaha_response_;
208};
209
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700210// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700211// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
David Zeuthen8f191b22013-08-06 12:27:50 -0700212// used. |payload_state| may be NULL, in which case a local mock is used.
213// |p2p_manager| may be NULL, in which case a local mock is used.
214// out_response may be NULL. If |fail_http_response_code| is non-negative,
Darin Petkov265f2902011-05-09 15:17:40 -0700215// the transfer will fail with that code. |ping_only| is passed through to the
216// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
217// post-data received by the mock HttpFetcher is returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700218bool TestUpdateCheck(PrefsInterface* prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -0700219 PayloadStateInterface *payload_state,
220 P2PManager *p2p_manager,
221 OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700222 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700223 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700224 bool ping_only,
David Zeuthena99981f2013-04-29 13:42:47 -0700225 ErrorCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700226 OmahaResponse* out_response,
227 vector<char>* out_post_data) {
228 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
229 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800230 http_response.size(),
231 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700232 if (fail_http_response_code >= 0) {
233 fetcher->FailTransfer(fail_http_response_code);
234 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800235 MockSystemState mock_system_state;
236 if (prefs)
237 mock_system_state.set_prefs(prefs);
David Zeuthen8f191b22013-08-06 12:27:50 -0700238 if (payload_state)
239 mock_system_state.set_payload_state(payload_state);
240 if (p2p_manager)
241 mock_system_state.set_p2p_manager(p2p_manager);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700242 mock_system_state.set_request_params(&params);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800243 OmahaRequestAction action(&mock_system_state,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700244 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700245 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700246 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700247 OmahaRequestActionTestProcessorDelegate delegate;
248 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700249 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700250
Darin Petkov6a5b3222010-07-13 14:55:28 -0700251 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700252 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700253 processor.EnqueueAction(&action);
254
255 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700256 BondActions(&action, &collector_action);
257 processor.EnqueueAction(&collector_action);
258
259 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
260 g_main_loop_run(loop);
261 g_main_loop_unref(loop);
262 if (collector_action.has_input_object_ && out_response)
263 *out_response = collector_action.omaha_response_;
264 if (out_post_data)
265 *out_post_data = fetcher->post_data();
266 return collector_action.has_input_object_;
267}
268
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700269// Tests Event requests -- they should always succeed. |out_post_data|
270// may be null; if non-null, the post-data received by the mock
271// HttpFetcher is returned.
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700272void TestEvent(OmahaRequestParams params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700273 OmahaEvent* event,
274 const string& http_response,
275 vector<char>* out_post_data) {
276 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
277 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800278 http_response.size(),
279 NULL);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800280 MockSystemState mock_system_state;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700281 mock_system_state.set_request_params(&params);
282 OmahaRequestAction action(&mock_system_state, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700283 OmahaRequestActionTestProcessorDelegate delegate;
284 delegate.loop_ = loop;
285 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700286 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700287 processor.EnqueueAction(&action);
288
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700289 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
290 g_main_loop_run(loop);
291 g_main_loop_unref(loop);
292 if (out_post_data)
293 *out_post_data = fetcher->post_data();
294}
295
Darin Petkov6a5b3222010-07-13 14:55:28 -0700296TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700297 OmahaResponse response;
298 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700299 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700300 NULL, // payload_state
301 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700302 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700303 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700304 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700305 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700306 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700307 &response,
308 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700309 EXPECT_FALSE(response.update_exists);
310}
311
312TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700313 OmahaResponse response;
314 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700315 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700316 NULL, // payload_state
317 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700318 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700319 GetUpdateResponse(OmahaRequestParams::kAppId,
320 "1.2.3.4", // version
321 "http://more/info",
322 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700323 "http://code/base/", // dl url
324 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700325 "HASH1234=", // checksum
326 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700327 "123", // size
328 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700329 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700330 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700331 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700332 &response,
333 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700334 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -0700335 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700336 EXPECT_EQ("1.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800337 EXPECT_EQ("http://code/base/file.signed", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700338 EXPECT_EQ("http://more/info", response.more_info_url);
339 EXPECT_EQ("HASH1234=", response.hash);
340 EXPECT_EQ(123, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700341 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700342 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700343}
344
Jay Srinivasan0a708742012-03-20 11:26:12 -0700345TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) {
346 OmahaResponse response;
347 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700348 params.set_update_disabled(true);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700349 ASSERT_FALSE(
350 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700351 NULL, // payload_state
352 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -0700353 params,
354 GetUpdateResponse(OmahaRequestParams::kAppId,
355 "1.2.3.4", // version
356 "http://more/info",
357 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700358 "http://code/base/", // dl url
359 "file.signed", // file name
Jay Srinivasan0a708742012-03-20 11:26:12 -0700360 "HASH1234=", // checksum
361 "false", // needs admin
362 "123", // size
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700363 ""), // deadline
Jay Srinivasan0a708742012-03-20 11:26:12 -0700364 -1,
365 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700366 kErrorCodeOmahaUpdateIgnoredPerPolicy,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700367 &response,
368 NULL));
369 EXPECT_FALSE(response.update_exists);
370}
371
Jay Srinivasan0a708742012-03-20 11:26:12 -0700372TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) {
373 OmahaResponse response;
374 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700375 params.set_update_disabled(true);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700376 ASSERT_TRUE(
377 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700378 NULL, // payload_state
379 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -0700380 params,
381 GetNoUpdateResponse(OmahaRequestParams::kAppId),
382 -1,
383 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700384 kErrorCodeSuccess,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700385 &response,
386 NULL));
387 EXPECT_FALSE(response.update_exists);
388}
389
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700390TEST(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) {
391 OmahaResponse response;
392 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700393 params.set_wall_clock_based_wait_enabled(true);
394 params.set_update_check_count_wait_enabled(false);
395 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700396
397 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800398 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700399 &prefs_dir));
400 ScopedDirRemover temp_dir_remover(prefs_dir);
401
402 Prefs prefs;
403 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
404 << "Failed to initialize preferences.";
405
406 ASSERT_FALSE(
407 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700408 NULL, // payload_state
409 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700410 params,
411 GetUpdateResponse2(OmahaRequestParams::kAppId,
412 "1.2.3.4", // version
413 "http://more/info",
414 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700415 "http://code/base/", // dl url
416 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700417 "HASH1234=", // checksum
418 "false", // needs admin
419 "123", // size
420 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700421 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800422 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700423 false, // disable_p2p_for_downloading
424 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700425 -1,
426 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700427 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700428 &response,
429 NULL));
430 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700431
432 // Verify if we are interactive check we don't defer.
433 params.set_interactive(true);
434 ASSERT_TRUE(
435 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700436 NULL, // payload_state
437 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700438 params,
439 GetUpdateResponse2(OmahaRequestParams::kAppId,
440 "1.2.3.4", // version
441 "http://more/info",
442 "true", // prompt
443 "http://code/base/", // dl url
444 "file.signed", // file name
445 "HASH1234=", // checksum
446 "false", // needs admin
447 "123", // size
448 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700449 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800450 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700451 false, // disable_p2p_for_downloading
452 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700453 -1,
454 false, // ping_only
455 kErrorCodeSuccess,
456 &response,
457 NULL));
458 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700459}
460
461TEST(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) {
462 OmahaResponse response;
463 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700464 params.set_wall_clock_based_wait_enabled(false);
465 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700466
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700467 params.set_update_check_count_wait_enabled(true);
468 params.set_min_update_checks_needed(1);
469 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700470
471 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800472 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700473 &prefs_dir));
474 ScopedDirRemover temp_dir_remover(prefs_dir);
475
476 Prefs prefs;
477 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
478 << "Failed to initialize preferences.";
479
480 ASSERT_TRUE(
481 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700482 NULL, // payload_state
483 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700484 params,
485 GetUpdateResponse2(OmahaRequestParams::kAppId,
486 "1.2.3.4", // version
487 "http://more/info",
488 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700489 "http://code/base/", // dl url
490 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700491 "HASH1234=", // checksum
492 "false", // needs admin
493 "123", // size
494 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700495 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800496 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700497 false, // disable_p2p_for_downloading
498 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700499 -1,
500 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700501 kErrorCodeSuccess,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700502 &response,
503 NULL));
504 EXPECT_TRUE(response.update_exists);
505}
506
507TEST(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) {
508 OmahaResponse response;
509 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700510 params.set_wall_clock_based_wait_enabled(true);
511 params.set_waiting_period(TimeDelta::FromDays(2));
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700512
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700513 params.set_update_check_count_wait_enabled(true);
514 params.set_min_update_checks_needed(1);
515 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700516
517 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800518 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700519 &prefs_dir));
520 ScopedDirRemover temp_dir_remover(prefs_dir);
521
522 Prefs prefs;
523 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
524 << "Failed to initialize preferences.";
525
526 ASSERT_TRUE(
527 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700528 NULL, // payload_state
529 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700530 params,
531 GetUpdateResponse2(OmahaRequestParams::kAppId,
532 "1.2.3.4", // version
533 "http://more/info",
534 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700535 "http://code/base/", // dl url
536 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700537 "HASH1234=", // checksum
538 "false", // needs admin
539 "123", // size
540 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700541 "0", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800542 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700543 false, // disable_p2p_for_downloading
544 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700545 -1,
546 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700547 kErrorCodeSuccess,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700548 &response,
549 NULL));
550 EXPECT_TRUE(response.update_exists);
551}
552
553
554TEST(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) {
555 OmahaResponse response;
556 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700557 params.set_wall_clock_based_wait_enabled(true);
558 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700559
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700560 params.set_update_check_count_wait_enabled(true);
561 params.set_min_update_checks_needed(0);
562 params.set_max_update_checks_allowed(0);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700563
564 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800565 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700566 &prefs_dir));
567 ScopedDirRemover temp_dir_remover(prefs_dir);
568
569 Prefs prefs;
570 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
571 << "Failed to initialize preferences.";
572
573 ASSERT_TRUE(TestUpdateCheck(
574 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700575 NULL, // payload_state
576 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700577 params,
578 GetUpdateResponse2(OmahaRequestParams::kAppId,
579 "1.2.3.4", // version
580 "http://more/info",
581 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700582 "http://code/base/", // dl url
583 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700584 "HASH1234=", // checksum
585 "false", // needs admin
586 "123", // size
587 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700588 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800589 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700590 false, // disable_p2p_for_downloading
591 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700592 -1,
593 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700594 kErrorCodeSuccess,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700595 &response,
596 NULL));
597
598 int64 count;
599 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
600 ASSERT_TRUE(count == 0);
601 EXPECT_TRUE(response.update_exists);
602}
603
604TEST(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) {
605 OmahaResponse response;
606 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700607 params.set_wall_clock_based_wait_enabled(true);
608 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700609
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700610 params.set_update_check_count_wait_enabled(true);
611 params.set_min_update_checks_needed(1);
612 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700613
614 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800615 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700616 &prefs_dir));
617 ScopedDirRemover temp_dir_remover(prefs_dir);
618
619 Prefs prefs;
620 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
621 << "Failed to initialize preferences.";
622
623 ASSERT_FALSE(TestUpdateCheck(
624 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700625 NULL, // payload_state
626 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700627 params,
628 GetUpdateResponse2(OmahaRequestParams::kAppId,
629 "1.2.3.4", // version
630 "http://more/info",
631 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700632 "http://code/base/", // dl url
633 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700634 "HASH1234=", // checksum
635 "false", // needs admin
636 "123", // size
637 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700638 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800639 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700640 false, // disable_p2p_for_downloading
641 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700642 -1,
643 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700644 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700645 &response,
646 NULL));
647
648 int64 count;
649 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
650 ASSERT_TRUE(count > 0);
651 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700652
653 // Verify if we are interactive check we don't defer.
654 params.set_interactive(true);
655 ASSERT_TRUE(
656 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700657 NULL, // payload_state
658 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700659 params,
660 GetUpdateResponse2(OmahaRequestParams::kAppId,
661 "1.2.3.4", // version
662 "http://more/info",
663 "true", // prompt
664 "http://code/base/", // dl url
665 "file.signed", // file name
666 "HASH1234=", // checksum
667 "false", // needs admin
668 "123", // size
669 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700670 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800671 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700672 false, // disable_p2p_for_downloading
673 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700674 -1,
675 false, // ping_only
676 kErrorCodeSuccess,
677 &response,
678 NULL));
679 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700680}
681
682TEST(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) {
683 OmahaResponse response;
684 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700685 params.set_wall_clock_based_wait_enabled(true);
686 params.set_waiting_period(TimeDelta());
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700687
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700688 params.set_update_check_count_wait_enabled(true);
689 params.set_min_update_checks_needed(1);
690 params.set_max_update_checks_allowed(8);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700691
692 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800693 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700694 &prefs_dir));
695 ScopedDirRemover temp_dir_remover(prefs_dir);
696
697 Prefs prefs;
698 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
699 << "Failed to initialize preferences.";
700
701 ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateCheckCount, 5));
702
703 ASSERT_FALSE(TestUpdateCheck(
704 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700705 NULL, // payload_state
706 NULL, // p2p_manager
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700707 params,
708 GetUpdateResponse2(OmahaRequestParams::kAppId,
709 "1.2.3.4", // version
710 "http://more/info",
711 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700712 "http://code/base/", // dl url
713 "file.signed", // file name
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700714 "HASH1234=", // checksum
715 "false", // needs admin
716 "123", // size
717 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700718 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800719 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700720 false, // disable_p2p_for_downloading
721 false), // disable_p2p_for sharing
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700722 -1,
723 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700724 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700725 &response,
726 NULL));
727
728 int64 count;
729 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateCheckCount, &count));
730 // count remains the same, as the decrementing happens in update_attempter
731 // which this test doesn't exercise.
732 ASSERT_TRUE(count == 5);
733 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -0700734
735 // Verify if we are interactive check we don't defer.
736 params.set_interactive(true);
737 ASSERT_TRUE(
738 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700739 NULL, // payload_state
740 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -0700741 params,
742 GetUpdateResponse2(OmahaRequestParams::kAppId,
743 "1.2.3.4", // version
744 "http://more/info",
745 "true", // prompt
746 "http://code/base/", // dl url
747 "file.signed", // file name
748 "HASH1234=", // checksum
749 "false", // needs admin
750 "123", // size
751 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -0700752 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -0800753 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -0700754 false, // disable_p2p_for_downloading
755 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -0700756 -1,
757 false, // ping_only
758 kErrorCodeSuccess,
759 &response,
760 NULL));
761 EXPECT_TRUE(response.update_exists);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700762}
Jay Srinivasan0a708742012-03-20 11:26:12 -0700763
Darin Petkov6a5b3222010-07-13 14:55:28 -0700764TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700765 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
766
767 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
768
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800769 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700770 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700771 mock_system_state.set_request_params(&params);
772 OmahaRequestAction action(&mock_system_state, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700773 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800774 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700775 NULL),
776 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700777 OmahaRequestActionTestProcessorDelegate delegate;
778 delegate.loop_ = loop;
779 ActionProcessor processor;
780 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700781 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700782
783 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
784 g_main_loop_run(loop);
785 g_main_loop_unref(loop);
786 EXPECT_FALSE(processor.IsRunning());
787}
788
789TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700790 OmahaResponse response;
791 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700792 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700793 NULL, // payload_state
794 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700795 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700796 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700797 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700798 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700799 kErrorCodeOmahaRequestXMLParseError,
Darin Petkovedc522e2010-11-05 09:35:17 -0700800 &response,
801 NULL));
802 EXPECT_FALSE(response.update_exists);
803}
804
805TEST(OmahaRequestActionTest, EmptyResponseTest) {
806 OmahaResponse response;
807 ASSERT_FALSE(
808 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700809 NULL, // payload_state
810 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -0700811 kDefaultTestParams,
812 "",
813 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700814 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700815 kErrorCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700816 &response,
817 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700818 EXPECT_FALSE(response.update_exists);
819}
820
821TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700822 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700823 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700824 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700825 NULL, // payload_state
826 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700827 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700828 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
829 "<daystart elapsed_seconds=\"100\"/>"
830 "<app appid=\"foo\" status=\"ok\">"
831 "<ping status=\"ok\"/>"
832 "<updatecheck/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700833 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700834 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700835 kErrorCodeOmahaResponseInvalid,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700836 &response,
837 NULL));
838 EXPECT_FALSE(response.update_exists);
839}
840
841TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700842 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700843 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700844 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700845 NULL, // payload_state
846 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700847 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700848 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
849 "<daystart elapsed_seconds=\"100\"/>"
850 "<app appid=\"foo\" status=\"ok\">"
851 "<ping status=\"ok\"/>"
852 "<updatecheck status=\"InvalidStatusTest\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700853 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700854 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700855 kErrorCodeOmahaResponseInvalid,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700856 &response,
857 NULL));
858 EXPECT_FALSE(response.update_exists);
859}
860
861TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700862 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700863 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700864 NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700865 NULL, // payload_state
866 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700867 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700868 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
869 "<daystart elapsed_seconds=\"100\"/>"
870 "<app appid=\"foo\" status=\"ok\">"
871 "<ping status=\"ok\"/>"
872 "</app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700873 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700874 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -0700875 kErrorCodeOmahaResponseInvalid,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700876 &response,
877 NULL));
878 EXPECT_FALSE(response.update_exists);
879}
880
881TEST(OmahaRequestActionTest, MissingFieldTest) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700882 string input_response =
883 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">"
884 "<daystart elapsed_seconds=\"100\"/>"
885 "<app appid=\"xyz\" status=\"ok\">"
886 "<updatecheck status=\"ok\">"
887 "<urls><url codebase=\"http://missing/field/test/\"/></urls>"
Chris Sosa3b748432013-06-20 16:42:59 -0700888 "<manifest version=\"10.2.3.4\">"
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700889 "<packages><package hash=\"not-used\" name=\"f\" "
890 "size=\"587\"/></packages>"
891 "<actions><action event=\"postinstall\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700892 "ChromeOSVersion=\"10.2.3.4\" "
893 "Prompt=\"false\" "
894 "IsDelta=\"true\" "
Jay Srinivasand671e972013-01-11 17:17:19 -0800895 "IsDeltaPayload=\"false\" "
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700896 "sha256=\"lkq34j5345\" "
897 "needsadmin=\"true\" "
898 "/></actions></manifest></updatecheck></app></response>";
899 LOG(INFO) << "Input Response = " << input_response;
900
Darin Petkov6a5b3222010-07-13 14:55:28 -0700901 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700902 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700903 NULL, // payload_state
904 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700905 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700906 input_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 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700910 &response,
911 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700912 EXPECT_TRUE(response.update_exists);
Chris Sosa3b748432013-06-20 16:42:59 -0700913 EXPECT_EQ("10.2.3.4", response.version);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800914 EXPECT_EQ("http://missing/field/test/f", response.payload_urls[0]);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700915 EXPECT_EQ("", response.more_info_url);
Jay Srinivasan23b92a52012-10-27 02:00:21 -0700916 EXPECT_EQ("lkq34j5345", response.hash);
917 EXPECT_EQ(587, response.size);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700918 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700919 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700920}
921
922namespace {
923class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
924 public:
925 void ProcessingStopped(const ActionProcessor* processor) {
926 ASSERT_TRUE(loop_);
927 g_main_loop_quit(loop_);
928 }
929 GMainLoop *loop_;
930};
931
932gboolean TerminateTransferTestStarter(gpointer data) {
933 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
934 processor->StartProcessing();
935 CHECK(processor->IsRunning());
936 processor->StopProcessing();
937 return FALSE;
938}
939} // namespace {}
940
941TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700942 string http_response("doesn't matter");
943 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
944
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800945 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700946 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700947 mock_system_state.set_request_params(&params);
948 OmahaRequestAction action(&mock_system_state, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700949 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800950 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700951 NULL),
952 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700953 TerminateEarlyTestProcessorDelegate delegate;
954 delegate.loop_ = loop;
955 ActionProcessor processor;
956 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700957 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700958
959 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
960 g_main_loop_run(loop);
961 g_main_loop_unref(loop);
962}
963
964TEST(OmahaRequestActionTest, XmlEncodeTest) {
965 EXPECT_EQ("ab", XmlEncode("ab"));
966 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
967 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
968 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
969 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
970
971 vector<char> post_data;
972
973 // Make sure XML Encode is being called on the params
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700974 MockSystemState mock_system_state;
975 OmahaRequestParams params(&mock_system_state,
976 OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700977 OmahaRequestParams::kOsVersion,
978 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700979 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700980 OmahaRequestParams::kAppId,
981 "0.1.0.0",
982 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700983 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700984 "<OEM MODEL>",
Chris Sosac1972482013-04-30 22:31:10 -0700985 "ChromeOSFirmware.1.0",
986 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700987 false, // delta okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -0800988 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -0700989 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -0700990 false, // update_disabled
991 "", // target_version_prefix
992 false, // use_p2p_for_downloading
993 false); // use_p2p_for_sharing
Darin Petkov6a5b3222010-07-13 14:55:28 -0700994 OmahaResponse response;
995 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700996 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -0700997 NULL, // payload_state
998 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700999 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001000 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001001 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001002 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001003 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001004 &response,
1005 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001006 // convert post_data to string
1007 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -07001008 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
1009 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001010 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
1011 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
1012 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
1013 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001014 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
1015 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -07001016}
1017
1018TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001019 OmahaResponse response;
1020 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001021 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001022 NULL, // payload_state
1023 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001024 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001025 GetUpdateResponse(OmahaRequestParams::kAppId,
1026 "1.2.3.4", // version
1027 "testthe&lt;url", // more info
1028 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001029 "testthe&amp;codebase/", // dl url
1030 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001031 "HASH1234=", // checksum
1032 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -07001033 "123", // size
1034 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -07001035 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001036 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001037 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001038 &response,
1039 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001040
1041 EXPECT_EQ(response.more_info_url, "testthe<url");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001042 EXPECT_EQ(response.payload_urls[0], "testthe&codebase/file.signed");
Darin Petkov6c118642010-10-21 12:06:30 -07001043 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -07001044}
1045
1046TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -07001047 OmahaResponse response;
1048 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001049 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001050 NULL, // payload_state
1051 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001052 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001053 GetUpdateResponse(OmahaRequestParams::kAppId,
1054 "1.2.3.4", // version
1055 "theurl", // more info
1056 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001057 "thecodebase/", // dl url
1058 "file.signed", // file name
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001059 "HASH1234=", // checksum
1060 "false", // needs admin
1061 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -07001062 "123123123123123", // size
1063 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -07001064 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001065 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001066 kErrorCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001067 &response,
1068 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -07001069
1070 EXPECT_EQ(response.size, 123123123123123ll);
1071}
1072
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001073TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
1074 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -08001075 NiceMock<PrefsMock> prefs;
1076 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
1077 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001078 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1);
Darin Petkov95508da2011-01-05 12:42:29 -08001079 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001080 NULL, // payload_state
1081 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001082 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001083 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001084 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001085 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001086 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001087 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001088 &post_data));
1089 // convert post_data to string
1090 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -07001091 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001092 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001093 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001094 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -07001095 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1096 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001097 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1098 string::npos);
1099 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1100 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001101}
1102
Jay Srinivasan0a708742012-03-20 11:26:12 -07001103
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001104TEST(OmahaRequestActionTest, FormatUpdateDisabledOutputTest) {
Darin Petkov95508da2011-01-05 12:42:29 -08001105 vector<char> post_data;
1106 NiceMock<PrefsMock> prefs;
1107 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Jay Srinivasan0a708742012-03-20 11:26:12 -07001108 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001109 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(1);
Jay Srinivasan0a708742012-03-20 11:26:12 -07001110 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001111 params.set_update_disabled(true);
Darin Petkov95508da2011-01-05 12:42:29 -08001112 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001113 NULL, // payload_state
1114 NULL, // p2p_manager
Jay Srinivasan0a708742012-03-20 11:26:12 -07001115 params,
Darin Petkov95508da2011-01-05 12:42:29 -08001116 "invalid xml>",
1117 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001118 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001119 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov95508da2011-01-05 12:42:29 -08001120 NULL, // response
1121 &post_data));
1122 // convert post_data to string
1123 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -07001124 EXPECT_NE(post_str.find(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001125 " <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n"
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001126 " <updatecheck targetversionprefix=\"\"></updatecheck>\n"),
Jay Srinivasan0a708742012-03-20 11:26:12 -07001127 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -08001128 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
1129 string::npos);
Chris Sosac1972482013-04-30 22:31:10 -07001130 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""),
1131 string::npos);
1132 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""),
1133 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -08001134}
1135
Darin Petkove17f86b2010-07-20 09:12:01 -07001136TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
1137 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001138 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -07001139 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
1140 "invalid xml>",
1141 &post_data);
1142 // convert post_data to string
1143 string post_str(&post_data[0], post_data.size());
1144 string expected_event = StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001145 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001146 OmahaEvent::kTypeUpdateDownloadStarted,
1147 OmahaEvent::kResultSuccess);
1148 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001149 EXPECT_EQ(post_str.find("ping"), string::npos);
1150 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -07001151}
1152
1153TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
1154 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001155 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -07001156 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
1157 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001158 kErrorCodeError),
Darin Petkove17f86b2010-07-20 09:12:01 -07001159 "invalid xml>",
1160 &post_data);
1161 // convert post_data to string
1162 string post_str(&post_data[0], post_data.size());
1163 string expected_event = StringPrintf(
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001164 " <event eventtype=\"%d\" eventresult=\"%d\" "
1165 "errorcode=\"%d\"></event>\n",
Darin Petkove17f86b2010-07-20 09:12:01 -07001166 OmahaEvent::kTypeDownloadComplete,
1167 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001168 kErrorCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001169 EXPECT_NE(post_str.find(expected_event), string::npos);
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001170 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001171}
1172
1173TEST(OmahaRequestActionTest, IsEventTest) {
1174 string http_response("doesn't matter");
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001175 MockSystemState mock_system_state;
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001176 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001177 mock_system_state.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001178 OmahaRequestAction update_check_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001179 &mock_system_state,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001180 NULL,
1181 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001182 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -07001183 NULL),
1184 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001185 EXPECT_FALSE(update_check_action.IsEvent());
1186
Jay Srinivasan480ddfa2012-06-01 19:15:26 -07001187 params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001188 mock_system_state.set_request_params(&params);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001189 OmahaRequestAction event_action(
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001190 &mock_system_state,
Darin Petkove17f86b2010-07-20 09:12:01 -07001191 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001192 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -08001193 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -07001194 NULL),
1195 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07001196 EXPECT_TRUE(event_action.IsEvent());
1197}
1198
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001199TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
1200 for (int i = 0; i < 2; i++) {
1201 bool delta_okay = i == 1;
1202 const char* delta_okay_str = delta_okay ? "true" : "false";
1203 vector<char> post_data;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001204 MockSystemState mock_system_state;
1205 OmahaRequestParams params(&mock_system_state,
1206 OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001207 OmahaRequestParams::kOsVersion,
1208 "service_pack",
1209 "x86-generic",
1210 OmahaRequestParams::kAppId,
1211 "0.1.0.0",
1212 "en-US",
1213 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -07001214 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001215 "ChromeOSFirmware.1.0",
1216 "EC100",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001217 delta_okay,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001218 false, // interactive
Jay Srinivasan0a708742012-03-20 11:26:12 -07001219 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -07001220 false, // update_disabled
1221 "", // target_version_prefix
1222 false, // use_p2p_for_downloading
1223 false); // use_p2p_for_sharing
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001224 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001225 NULL, // payload_state
1226 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001227 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001228 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001229 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001230 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001231 kErrorCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -07001232 NULL,
1233 &post_data));
1234 // convert post_data to string
1235 string post_str(&post_data[0], post_data.size());
1236 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
1237 string::npos)
1238 << "i = " << i;
1239 }
1240}
1241
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001242TEST(OmahaRequestActionTest, FormatInteractiveOutputTest) {
1243 for (int i = 0; i < 2; i++) {
1244 bool interactive = i == 1;
Gilad Arnold8a659d82013-01-24 11:26:00 -08001245 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler";
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001246 vector<char> post_data;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001247 MockSystemState mock_system_state;
1248 OmahaRequestParams params(&mock_system_state,
1249 OmahaRequestParams::kOsPlatform,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001250 OmahaRequestParams::kOsVersion,
1251 "service_pack",
1252 "x86-generic",
1253 OmahaRequestParams::kAppId,
1254 "0.1.0.0",
1255 "en-US",
1256 "unittest_track",
1257 "OEM MODEL REV 1234",
Chris Sosac1972482013-04-30 22:31:10 -07001258 "ChromeOSFirmware.1.0",
1259 "EC100",
David Zeuthen8f191b22013-08-06 12:27:50 -07001260 true, // delta_okay
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001261 interactive,
1262 "http://url",
David Zeuthen8f191b22013-08-06 12:27:50 -07001263 false, // update_disabled
1264 "", // target_version_prefix
1265 false, // use_p2p_for_downloading
1266 false); // use_p2p_for_sharing
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001267 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001268 NULL, // payload_state
1269 NULL, // p2p_manager
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001270 params,
1271 "invalid xml>",
1272 -1,
1273 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001274 kErrorCodeOmahaRequestXMLParseError,
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001275 NULL,
1276 &post_data));
1277 // convert post_data to string
1278 string post_str(&post_data[0], post_data.size());
Gilad Arnold8a659d82013-01-24 11:26:00 -08001279 EXPECT_NE(post_str.find(StringPrintf("installsource=\"%s\"",
Gilad Arnoldbbdd4902013-01-10 16:06:30 -08001280 interactive_str)),
1281 string::npos)
1282 << "i = " << i;
1283 }
1284}
1285
Darin Petkove17f86b2010-07-20 09:12:01 -07001286TEST(OmahaRequestActionTest, OmahaEventTest) {
1287 OmahaEvent default_event;
1288 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
1289 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001290 EXPECT_EQ(kErrorCodeError, default_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001291
1292 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
1293 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
1294 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001295 EXPECT_EQ(kErrorCodeSuccess, success_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001296
1297 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
1298 OmahaEvent::kResultError,
David Zeuthena99981f2013-04-29 13:42:47 -07001299 kErrorCodeError);
Darin Petkove17f86b2010-07-20 09:12:01 -07001300 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
1301 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
David Zeuthena99981f2013-04-29 13:42:47 -07001302 EXPECT_EQ(kErrorCodeError, error_event.error_code);
Darin Petkove17f86b2010-07-20 09:12:01 -07001303}
1304
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001305TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -07001306 for (int ping_only = 0; ping_only < 2; ping_only++) {
1307 NiceMock<PrefsMock> prefs;
1308 // Add a few hours to the day difference to test no rounding, etc.
1309 int64_t five_days_ago =
1310 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
1311 int64_t six_days_ago =
1312 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001313 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1314 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov265f2902011-05-09 15:17:40 -07001315 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1316 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
1317 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1318 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
1319 vector<char> post_data;
1320 ASSERT_TRUE(
1321 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001322 NULL, // payload_state
1323 NULL, // p2p_manager
Darin Petkov265f2902011-05-09 15:17:40 -07001324 kDefaultTestParams,
1325 GetNoUpdateResponse(OmahaRequestParams::kAppId),
1326 -1,
1327 ping_only,
David Zeuthena99981f2013-04-29 13:42:47 -07001328 kErrorCodeSuccess,
Darin Petkov265f2902011-05-09 15:17:40 -07001329 NULL,
1330 &post_data));
1331 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001332 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"),
Darin Petkov265f2902011-05-09 15:17:40 -07001333 string::npos);
1334 if (ping_only) {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001335 EXPECT_EQ(post_str.find("updatecheck"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001336 EXPECT_EQ(post_str.find("previousversion"), string::npos);
1337 } else {
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001338 EXPECT_NE(post_str.find("updatecheck"), string::npos);
Darin Petkov265f2902011-05-09 15:17:40 -07001339 EXPECT_NE(post_str.find("previousversion"), string::npos);
1340 }
1341 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001342}
1343
1344TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001345 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001346 int64_t three_days_ago =
1347 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
1348 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001349 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1350 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001351 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1352 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
1353 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1354 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1355 vector<char> post_data;
1356 ASSERT_TRUE(
1357 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001358 NULL, // payload_state
1359 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001360 kDefaultTestParams,
1361 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001362 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001363 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001364 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001365 NULL,
1366 &post_data));
1367 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001368 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"),
Thieu Le116fda32011-04-19 11:01:54 -07001369 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001370}
1371
1372TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001373 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001374 int64_t four_days_ago =
1375 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
1376 int64_t now = Time::Now().ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001377 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1378 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001379 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1380 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1381 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1382 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
1383 vector<char> post_data;
1384 ASSERT_TRUE(
1385 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001386 NULL, // payload_state
1387 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001388 kDefaultTestParams,
1389 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001390 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001391 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001392 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001393 NULL,
1394 &post_data));
1395 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001396 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"),
Thieu Le116fda32011-04-19 11:01:54 -07001397 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001398}
1399
1400TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001401 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001402 int64_t one_hour_ago =
1403 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001404 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1405 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001406 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1407 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1408 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1409 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
1410 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1411 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1412 vector<char> post_data;
1413 ASSERT_TRUE(
1414 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001415 NULL, // payload_state
1416 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001417 kDefaultTestParams,
1418 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -07001419 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001420 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001421 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001422 NULL,
1423 &post_data));
1424 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001425 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001426}
1427
Thieu Leb44e9e82011-06-06 14:34:04 -07001428TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
1429 // This test ensures that we ignore empty ping only requests.
1430 NiceMock<PrefsMock> prefs;
1431 int64_t now = Time::Now().ToInternalValue();
1432 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1433 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1434 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1435 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
1436 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1437 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1438 vector<char> post_data;
1439 EXPECT_TRUE(
1440 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001441 NULL, // payload_state
1442 NULL, // p2p_manager
Thieu Leb44e9e82011-06-06 14:34:04 -07001443 kDefaultTestParams,
1444 GetNoUpdateResponse(OmahaRequestParams::kAppId),
1445 -1,
1446 true, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001447 kErrorCodeSuccess,
Thieu Leb44e9e82011-06-06 14:34:04 -07001448 NULL,
1449 &post_data));
1450 EXPECT_EQ(post_data.size(), 0);
1451}
1452
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001453TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001454 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001455 int64_t future =
1456 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
David Zeuthen639aa362014-02-03 16:23:44 -08001457 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _))
1458 .WillOnce(DoAll(SetArgumentPointee<1>(0), Return(true)));
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001459 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
1460 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1461 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
1462 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
1463 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
1464 .WillOnce(Return(true));
1465 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
1466 .WillOnce(Return(true));
1467 vector<char> post_data;
1468 ASSERT_TRUE(
1469 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001470 NULL, // payload_state
1471 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001472 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001473 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1474 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001475 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001476 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001477 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001478 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001479 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001480 NULL,
1481 &post_data));
1482 string post_str(&post_data[0], post_data.size());
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001483 EXPECT_EQ(post_str.find("ping"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001484}
1485
1486TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
1487 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -07001488 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001489 // may fail if it runs for longer than 5 seconds. It shouldn't run
1490 // that long though.
1491 int64_t midnight =
1492 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
1493 int64_t midnight_slack =
1494 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -08001495 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001496 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
1497 AllOf(Ge(midnight), Le(midnight_slack))))
1498 .WillOnce(Return(true));
1499 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
1500 AllOf(Ge(midnight), Le(midnight_slack))))
1501 .WillOnce(Return(true));
1502 ASSERT_TRUE(
1503 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001504 NULL, // payload_state
1505 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001506 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001507 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1508 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001509 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001510 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001511 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001512 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001513 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001514 NULL,
1515 NULL));
1516}
1517
1518TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001519 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001520 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1521 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
1522 ASSERT_TRUE(
1523 TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001524 NULL, // payload_state
1525 NULL, // p2p_manager
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001526 kDefaultTestParams,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001527 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1528 "protocol=\"3.0\"><daystart blah=\"200\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001529 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001530 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001531 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001532 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001533 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001534 NULL,
1535 NULL));
1536}
1537
1538TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -08001539 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001540 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
1541 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
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,
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001547 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response "
1548 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>"
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001549 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001550 "<updatecheck status=\"noupdate\"/></app></response>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001551 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001552 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001553 kErrorCodeSuccess,
Darin Petkov1cbd78f2010-07-29 12:38:34 -07001554 NULL,
1555 NULL));
1556}
1557
Darin Petkov84c763c2010-07-29 16:27:58 -07001558TEST(OmahaRequestActionTest, NoUniqueIDTest) {
1559 vector<char> post_data;
1560 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001561 NULL, // payload_state
1562 NULL, // p2p_manager
Darin Petkov84c763c2010-07-29 16:27:58 -07001563 kDefaultTestParams,
1564 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -07001565 -1,
Darin Petkov265f2902011-05-09 15:17:40 -07001566 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001567 kErrorCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -07001568 NULL, // response
1569 &post_data));
1570 // convert post_data to string
1571 string post_str(&post_data[0], post_data.size());
1572 EXPECT_EQ(post_str.find("machineid="), string::npos);
1573 EXPECT_EQ(post_str.find("userid="), string::npos);
1574}
1575
Darin Petkovedc522e2010-11-05 09:35:17 -07001576TEST(OmahaRequestActionTest, NetworkFailureTest) {
1577 OmahaResponse response;
1578 ASSERT_FALSE(
1579 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001580 NULL, // payload_state
1581 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -07001582 kDefaultTestParams,
1583 "",
1584 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001585 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001586 static_cast<ErrorCode>(
1587 kErrorCodeOmahaRequestHTTPResponseBase + 501),
Darin Petkovedc522e2010-11-05 09:35:17 -07001588 &response,
1589 NULL));
1590 EXPECT_FALSE(response.update_exists);
1591}
1592
1593TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
1594 OmahaResponse response;
1595 ASSERT_FALSE(
1596 TestUpdateCheck(NULL, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001597 NULL, // payload_state
1598 NULL, // p2p_manager
Darin Petkovedc522e2010-11-05 09:35:17 -07001599 kDefaultTestParams,
1600 "",
1601 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001602 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001603 static_cast<ErrorCode>(
1604 kErrorCodeOmahaRequestHTTPResponseBase + 999),
Darin Petkovedc522e2010-11-05 09:35:17 -07001605 &response,
1606 NULL));
1607 EXPECT_FALSE(response.update_exists);
1608}
1609
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001610TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) {
1611 OmahaResponse response;
1612 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001613 params.set_wall_clock_based_wait_enabled(true);
1614 params.set_waiting_period(TimeDelta().FromDays(1));
1615 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001616
1617 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -08001618 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001619 &prefs_dir));
1620 ScopedDirRemover temp_dir_remover(prefs_dir);
1621
1622 Prefs prefs;
1623 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
1624 << "Failed to initialize preferences.";
1625
1626 ASSERT_FALSE(TestUpdateCheck(
1627 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001628 NULL, // payload_state
1629 NULL, // p2p_manager
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001630 params,
1631 GetUpdateResponse2(OmahaRequestParams::kAppId,
1632 "1.2.3.4", // version
1633 "http://more/info",
1634 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001635 "http://code/base/", // dl url
1636 "file.signed", // file name
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001637 "HASH1234=", // checksum
1638 "false", // needs admin
1639 "123", // size
1640 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001641 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08001642 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07001643 false, // disable_p2p_for_downloading
1644 false), // disable_p2p_for sharing
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001645 -1,
1646 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001647 kErrorCodeOmahaUpdateDeferredPerPolicy,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001648 &response,
1649 NULL));
1650
1651 int64 timestamp = 0;
1652 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
1653 ASSERT_TRUE(timestamp > 0);
1654 EXPECT_FALSE(response.update_exists);
Chris Sosa968d0572013-08-23 14:46:02 -07001655
1656 // Verify if we are interactive check we don't defer.
1657 params.set_interactive(true);
1658 ASSERT_TRUE(
1659 TestUpdateCheck(&prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001660 NULL, // payload_state
1661 NULL, // p2p_manager
Chris Sosa968d0572013-08-23 14:46:02 -07001662 params,
1663 GetUpdateResponse2(OmahaRequestParams::kAppId,
1664 "1.2.3.4", // version
1665 "http://more/info",
1666 "true", // prompt
1667 "http://code/base/", // dl url
1668 "file.signed", // file name
1669 "HASH1234=", // checksum
1670 "false", // needs admin
1671 "123", // size
1672 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001673 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08001674 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07001675 false, // disable_p2p_for_downloading
1676 false), // disable_p2p_for sharing
Chris Sosa968d0572013-08-23 14:46:02 -07001677 -1,
1678 false, // ping_only
1679 kErrorCodeSuccess,
1680 &response,
1681 NULL));
1682 EXPECT_TRUE(response.update_exists);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001683}
1684
1685TEST(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) {
1686 OmahaResponse response;
1687 OmahaRequestParams params = kDefaultTestParams;
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001688 params.set_wall_clock_based_wait_enabled(true);
1689 params.set_waiting_period(TimeDelta().FromDays(1));
1690 params.set_update_check_count_wait_enabled(false);
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001691
1692 string prefs_dir;
Gilad Arnolda6742b32014-01-11 00:18:34 -08001693 EXPECT_TRUE(utils::MakeTempDirectory("ue_ut_prefs.XXXXXX",
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001694 &prefs_dir));
1695 ScopedDirRemover temp_dir_remover(prefs_dir);
1696
1697 Prefs prefs;
1698 LOG_IF(ERROR, !prefs.Init(FilePath(prefs_dir)))
1699 << "Failed to initialize preferences.";
1700
1701 // Set the timestamp to a very old value such that it exceeds the
1702 // waiting period set above.
1703 Time t1;
1704 Time::FromString("1/1/2012", &t1);
1705 ASSERT_TRUE(prefs.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue()));
1706 ASSERT_TRUE(TestUpdateCheck(
1707 &prefs, // prefs
David Zeuthen8f191b22013-08-06 12:27:50 -07001708 NULL, // payload_state
1709 NULL, // p2p_manager
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001710 params,
1711 GetUpdateResponse2(OmahaRequestParams::kAppId,
1712 "1.2.3.4", // version
1713 "http://more/info",
1714 "true", // prompt
Jay Srinivasan23b92a52012-10-27 02:00:21 -07001715 "http://code/base/", // dl url
1716 "file.signed", // file name
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001717 "HASH1234=", // checksum
1718 "false", // needs admin
1719 "123", // size
1720 "", // deadline
David Zeuthen8f191b22013-08-06 12:27:50 -07001721 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08001722 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07001723 false, // disable_p2p_for_downloading
1724 false), // disable_p2p_for sharing
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001725 -1,
1726 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001727 kErrorCodeSuccess,
Jay Srinivasan34b5d862012-07-23 11:43:22 -07001728 &response,
1729 NULL));
1730
1731 EXPECT_TRUE(response.update_exists);
1732
1733 // Make sure the timestamp t1 is unchanged showing that it was reused.
1734 int64 timestamp = 0;
1735 ASSERT_TRUE(prefs.GetInt64(kPrefsUpdateFirstSeenAt, &timestamp));
1736 ASSERT_TRUE(timestamp == t1.ToInternalValue());
1737}
1738
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001739TEST(OmahaRequestActionTest, TestChangingToMoreStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001740 // Create a uniquely named test directory.
1741 string test_dir;
1742 ASSERT_TRUE(utils::MakeTempDirectory(
1743 "omaha_request_action-test-XXXXXX", &test_dir));
1744
1745 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1746 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001747 kStatefulPartition + "/etc"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001748 vector<char> post_data;
1749 NiceMock<PrefsMock> prefs;
1750 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001751 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001752 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1753 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1754 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1755 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001756 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001757 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n"
1758 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1759 OmahaRequestParams params = kDefaultTestParams;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001760 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001761 params.SetLockDown(false);
1762 params.Init("1.2.3.4", "", 0);
1763 EXPECT_EQ("canary-channel", params.current_channel());
1764 EXPECT_EQ("stable-channel", params.target_channel());
1765 EXPECT_TRUE(params.to_more_stable_channel());
1766 EXPECT_TRUE(params.is_powerwash_allowed());
1767 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001768 NULL, // payload_state
1769 NULL, // p2p_manager
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001770 params,
1771 "invalid xml>",
1772 -1,
1773 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001774 kErrorCodeOmahaRequestXMLParseError,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001775 NULL, // response
1776 &post_data));
1777 // convert post_data to string
1778 string post_str(&post_data[0], post_data.size());
1779 EXPECT_NE(string::npos, post_str.find(
1780 "appid=\"{22222222-2222-2222-2222-222222222222}\" "
1781 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" "
1782 "track=\"stable-channel\" from_track=\"canary-channel\" "));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001783
1784 ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001785}
1786
1787TEST(OmahaRequestActionTest, TestChangingToLessStableChannel) {
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001788 // Create a uniquely named test directory.
1789 string test_dir;
1790 ASSERT_TRUE(utils::MakeTempDirectory(
1791 "omaha_request_action-test-XXXXXX", &test_dir));
1792
1793 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir + "/etc"));
1794 ASSERT_EQ(0, System(string("mkdir -p ") + test_dir +
Chris Sosabe45bef2013-04-09 18:25:12 -07001795 kStatefulPartition + "/etc"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001796 vector<char> post_data;
1797 NiceMock<PrefsMock> prefs;
1798 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001799 test_dir + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001800 "CHROMEOS_RELEASE_APPID={11111111-1111-1111-1111-111111111111}\n"
1801 "CHROMEOS_BOARD_APPID={22222222-2222-2222-2222-222222222222}\n"
1802 "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
1803 ASSERT_TRUE(WriteFileString(
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001804 test_dir + kStatefulPartition + "/etc/lsb-release",
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001805 "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
1806 OmahaRequestParams params = kDefaultTestParams;
Gilad Arnoldd04f8e22014-01-09 13:13:40 -08001807 params.set_root(test_dir);
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001808 params.SetLockDown(false);
1809 params.Init("5.6.7.8", "", 0);
1810 EXPECT_EQ("stable-channel", params.current_channel());
1811 EXPECT_EQ("canary-channel", params.target_channel());
1812 EXPECT_FALSE(params.to_more_stable_channel());
1813 EXPECT_FALSE(params.is_powerwash_allowed());
1814 ASSERT_FALSE(TestUpdateCheck(&prefs,
David Zeuthen8f191b22013-08-06 12:27:50 -07001815 NULL, // payload_state
1816 NULL, // p2p_manager
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001817 params,
1818 "invalid xml>",
1819 -1,
1820 false, // ping_only
David Zeuthena99981f2013-04-29 13:42:47 -07001821 kErrorCodeOmahaRequestXMLParseError,
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001822 NULL, // response
1823 &post_data));
1824 // convert post_data to string
1825 string post_str(&post_data[0], post_data.size());
1826 EXPECT_NE(string::npos, post_str.find(
1827 "appid=\"{11111111-1111-1111-1111-111111111111}\" "
1828 "version=\"5.6.7.8\" "
1829 "track=\"canary-channel\" from_track=\"stable-channel\""));
1830 EXPECT_EQ(string::npos, post_str.find( "from_version"));
Gilad Arnoldeff87cc2013-07-22 18:32:09 -07001831
1832 ASSERT_TRUE(utils::RecursiveUnlinkDir(test_dir));
Jay Srinivasanae4697c2013-03-18 17:08:08 -07001833}
1834
David Zeuthen8f191b22013-08-06 12:27:50 -07001835void P2PTest(bool initial_allow_p2p_for_downloading,
1836 bool initial_allow_p2p_for_sharing,
1837 bool omaha_disable_p2p_for_downloading,
1838 bool omaha_disable_p2p_for_sharing,
1839 bool payload_state_allow_p2p_attempt,
1840 bool expect_p2p_client_lookup,
1841 const string& p2p_client_result_url,
1842 bool expected_allow_p2p_for_downloading,
1843 bool expected_allow_p2p_for_sharing,
1844 const string& expected_p2p_url) {
1845 OmahaResponse response;
1846 OmahaRequestParams request_params = kDefaultTestParams;
1847 request_params.set_use_p2p_for_downloading(initial_allow_p2p_for_downloading);
1848 request_params.set_use_p2p_for_sharing(initial_allow_p2p_for_sharing);
1849
1850 MockPayloadState mock_payload_state;
1851 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed())
1852 .WillRepeatedly(Return(payload_state_allow_p2p_attempt));
1853 MockP2PManager mock_p2p_manager;
1854 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url);
1855
David Zeuthen4cc5ed22014-01-15 12:35:03 -08001856 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds);
1857 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _))
David Zeuthen8f191b22013-08-06 12:27:50 -07001858 .Times(expect_p2p_client_lookup ? 1 : 0);
1859
1860 ASSERT_TRUE(
1861 TestUpdateCheck(NULL, // prefs
1862 &mock_payload_state,
1863 &mock_p2p_manager,
1864 request_params,
1865 GetUpdateResponse2(OmahaRequestParams::kAppId,
1866 "1.2.3.4", // version
1867 "http://more/info",
1868 "true", // prompt
1869 "http://code/base/", // dl url
1870 "file.signed", // file name
1871 "HASH1234=", // checksum
1872 "false", // needs admin
1873 "123", // size
1874 "", // deadline
1875 "7", // max days to scatter
David Zeuthen639aa362014-02-03 16:23:44 -08001876 "42", // elapsed_days
David Zeuthen8f191b22013-08-06 12:27:50 -07001877 omaha_disable_p2p_for_downloading,
1878 omaha_disable_p2p_for_sharing),
1879 -1,
1880 false, // ping_only
1881 kErrorCodeSuccess,
1882 &response,
1883 NULL));
1884 EXPECT_TRUE(response.update_exists);
1885
1886 EXPECT_EQ(response.disable_p2p_for_downloading,
1887 omaha_disable_p2p_for_downloading);
1888 EXPECT_EQ(response.disable_p2p_for_sharing,
1889 omaha_disable_p2p_for_sharing);
1890
1891 EXPECT_EQ(request_params.use_p2p_for_downloading(),
1892 expected_allow_p2p_for_downloading);
1893
1894 EXPECT_EQ(request_params.use_p2p_for_sharing(),
1895 expected_allow_p2p_for_sharing);
1896
1897 EXPECT_EQ(request_params.p2p_url(), expected_p2p_url);
1898}
1899
1900TEST(OmahaRequestActionTest, P2PWithPeer) {
1901 P2PTest(true, // initial_allow_p2p_for_downloading
1902 true, // initial_allow_p2p_for_sharing
1903 false, // omaha_disable_p2p_for_downloading
1904 false, // omaha_disable_p2p_for_sharing
1905 true, // payload_state_allow_p2p_attempt
1906 true, // expect_p2p_client_lookup
1907 "http://1.3.5.7/p2p", // p2p_client_result_url
1908 true, // expected_allow_p2p_for_downloading
1909 true, // expected_allow_p2p_for_sharing
1910 "http://1.3.5.7/p2p"); // expected_p2p_url
1911}
1912
1913TEST(OmahaRequestActionTest, P2PWithoutPeer) {
1914 P2PTest(true, // initial_allow_p2p_for_downloading
1915 true, // initial_allow_p2p_for_sharing
1916 false, // omaha_disable_p2p_for_downloading
1917 false, // omaha_disable_p2p_for_sharing
1918 true, // payload_state_allow_p2p_attempt
1919 true, // expect_p2p_client_lookup
1920 "", // p2p_client_result_url
1921 false, // expected_allow_p2p_for_downloading
1922 true, // expected_allow_p2p_for_sharing
1923 ""); // expected_p2p_url
1924}
1925
1926TEST(OmahaRequestActionTest, P2PDownloadNotAllowed) {
1927 P2PTest(false, // initial_allow_p2p_for_downloading
1928 true, // initial_allow_p2p_for_sharing
1929 false, // omaha_disable_p2p_for_downloading
1930 false, // omaha_disable_p2p_for_sharing
1931 true, // payload_state_allow_p2p_attempt
1932 false, // expect_p2p_client_lookup
1933 "unset", // p2p_client_result_url
1934 false, // expected_allow_p2p_for_downloading
1935 true, // expected_allow_p2p_for_sharing
1936 ""); // expected_p2p_url
1937}
1938
1939TEST(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) {
1940 P2PTest(true, // initial_allow_p2p_for_downloading
1941 true, // initial_allow_p2p_for_sharing
1942 true, // omaha_disable_p2p_for_downloading
1943 false, // omaha_disable_p2p_for_sharing
1944 true, // payload_state_allow_p2p_attempt
1945 false, // expect_p2p_client_lookup
1946 "unset", // p2p_client_result_url
1947 false, // expected_allow_p2p_for_downloading
1948 true, // expected_allow_p2p_for_sharing
1949 ""); // expected_p2p_url
1950}
1951
1952TEST(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) {
1953 P2PTest(true, // initial_allow_p2p_for_downloading
1954 true, // initial_allow_p2p_for_sharing
1955 false, // omaha_disable_p2p_for_downloading
1956 true, // omaha_disable_p2p_for_sharing
1957 true, // payload_state_allow_p2p_attempt
1958 true, // expect_p2p_client_lookup
1959 "http://1.3.5.7/p2p", // p2p_client_result_url
1960 true, // expected_allow_p2p_for_downloading
1961 false, // expected_allow_p2p_for_sharing
1962 "http://1.3.5.7/p2p"); // expected_p2p_url
1963}
1964
1965TEST(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) {
1966 P2PTest(true, // initial_allow_p2p_for_downloading
1967 true, // initial_allow_p2p_for_sharing
1968 true, // omaha_disable_p2p_for_downloading
1969 true, // omaha_disable_p2p_for_sharing
1970 true, // payload_state_allow_p2p_attempt
1971 false, // expect_p2p_client_lookup
1972 "unset", // p2p_client_result_url
1973 false, // expected_allow_p2p_for_downloading
1974 false, // expected_allow_p2p_for_sharing
1975 ""); // expected_p2p_url
1976}
1977
David Zeuthen639aa362014-02-03 16:23:44 -08001978bool InstallDateParseHelper(const std::string &elapsed_days,
1979 PrefsInterface* prefs,
1980 OmahaResponse *response) {
1981 return
1982 TestUpdateCheck(prefs,
1983 NULL, // payload_state
1984 NULL, // p2p_manager
1985 kDefaultTestParams,
1986 GetUpdateResponse2(OmahaRequestParams::kAppId,
1987 "1.2.3.4", // version
1988 "http://more/info",
1989 "true", // prompt
1990 "http://code/base/", // dl url
1991 "file.signed", // file name
1992 "HASH1234=", // checksum
1993 "false", // needs admin
1994 "123", // size
1995 "", // deadline
1996 "7", // max days to scatter
1997 elapsed_days,
1998 false, // disable_p2p_for_downloading
1999 false), // disable_p2p_for sharing
2000 -1,
2001 false, // ping_only
2002 kErrorCodeSuccess,
2003 response,
2004 NULL);
2005}
2006
2007TEST(OmahaRequestActionTest, ParseInstallDateFromResponse) {
2008 OmahaResponse response;
2009 string temp_dir;
2010 Prefs prefs;
2011 EXPECT_TRUE(utils::MakeTempDirectory("ParseInstallDateFromResponse.XXXXXX",
2012 &temp_dir));
2013 prefs.Init(FilePath(temp_dir));
2014
2015 // Check that we parse elapsed_days in the Omaha Response correctly.
2016 // and that the kPrefsInstallDateDays value is written to.
2017 EXPECT_FALSE(prefs.Exists(kPrefsInstallDateDays));
2018 EXPECT_TRUE(InstallDateParseHelper("42", &prefs, &response));
2019 EXPECT_TRUE(response.update_exists);
2020 EXPECT_EQ(42, response.install_date_days);
2021 EXPECT_TRUE(prefs.Exists(kPrefsInstallDateDays));
2022 int64_t prefs_days;
2023 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2024 EXPECT_EQ(prefs_days, 42);
2025
2026 // If there already is a value set, we shouldn't do anything.
2027 EXPECT_TRUE(InstallDateParseHelper("7", &prefs, &response));
2028 EXPECT_TRUE(response.update_exists);
2029 EXPECT_EQ(7, response.install_date_days);
2030 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2031 EXPECT_EQ(prefs_days, 42);
2032
2033 // Note that elapsed_days is not necessarily divisible by 7 so check
2034 // that we round down correctly when populating kPrefsInstallDateDays.
2035 EXPECT_TRUE(prefs.Delete(kPrefsInstallDateDays));
2036 EXPECT_TRUE(InstallDateParseHelper("23", &prefs, &response));
2037 EXPECT_TRUE(response.update_exists);
2038 EXPECT_EQ(23, response.install_date_days);
2039 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2040 EXPECT_EQ(prefs_days, 21);
2041
2042 // Check that we correctly handle elapsed_days not being included in
2043 // the Omaha Response.
2044 EXPECT_TRUE(InstallDateParseHelper("", &prefs, &response));
2045 EXPECT_TRUE(response.update_exists);
2046 EXPECT_EQ(-1, response.install_date_days);
2047
2048 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
2049}
2050
2051TEST(OmahaRequestActionTest, GetInstallDate) {
2052 string temp_dir;
2053 Prefs prefs;
2054 EXPECT_TRUE(utils::MakeTempDirectory("GetInstallDate.XXXXXX",
2055 &temp_dir));
2056 prefs.Init(FilePath(temp_dir));
2057
2058 // If there is no prefs and OOBE is not complete, we should not
2059 // report anything to Omaha.
2060 {
2061 NiceMock<MockSystemState> system_state;
2062 system_state.set_prefs(&prefs);
2063 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), -1);
2064 EXPECT_FALSE(prefs.Exists(kPrefsInstallDateDays));
2065 }
2066
2067 // If OOBE is complete and happened on a valid date (e.g. after Jan
2068 // 1 2007 0:00 PST), that date should be used and written to
2069 // prefs. However, first try with an invalid date and check we do
2070 // nothing.
2071 {
2072 NiceMock<MockSystemState> system_state;
2073 system_state.set_prefs(&prefs);
2074
2075 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST.
2076 EXPECT_CALL(system_state,
2077 IsOOBEComplete(_))
2078 .WillRepeatedly(DoAll(SetArgumentPointee<0>(oobe_date),
2079 Return(true)));
2080 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), -1);
2081 EXPECT_FALSE(prefs.Exists(kPrefsInstallDateDays));
2082 }
2083
2084 // Then check with a valid date. The date Jan 20, 2007 0:00 PST
2085 // should yield an InstallDate of 14.
2086 {
2087 NiceMock<MockSystemState> system_state;
2088 system_state.set_prefs(&prefs);
2089
2090 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST.
2091 EXPECT_CALL(system_state,
2092 IsOOBEComplete(_))
2093 .WillRepeatedly(DoAll(SetArgumentPointee<0>(oobe_date),
2094 Return(true)));
2095 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), 14);
2096 EXPECT_TRUE(prefs.Exists(kPrefsInstallDateDays));
2097
2098 int64_t prefs_days;
2099 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2100 EXPECT_EQ(prefs_days, 14);
2101 }
2102
2103 // Now that we have a valid date in prefs, check that we keep using
2104 // that even if OOBE date reports something else. The date Jan 30,
2105 // 2007 0:00 PST should yield an InstallDate of 28... but since
2106 // there's a prefs file, we should still get 14.
2107 {
2108 NiceMock<MockSystemState> system_state;
2109 system_state.set_prefs(&prefs);
2110
2111 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST.
2112 EXPECT_CALL(system_state,
2113 IsOOBEComplete(_))
2114 .WillRepeatedly(DoAll(SetArgumentPointee<0>(oobe_date),
2115 Return(true)));
2116 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), 14);
2117
2118 int64_t prefs_days;
2119 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2120 EXPECT_EQ(prefs_days, 14);
2121
2122 // If we delete the prefs file, we should get 28 days.
2123 EXPECT_TRUE(prefs.Delete(kPrefsInstallDateDays));
2124 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&system_state), 28);
2125 EXPECT_TRUE(prefs.GetInt64(kPrefsInstallDateDays, &prefs_days));
2126 EXPECT_EQ(prefs_days, 28);
2127 }
2128
2129 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
2130}
2131
Darin Petkov6a5b3222010-07-13 14:55:28 -07002132} // namespace chromeos_update_engine