blob: e92fb03ba91a51146dab1244a1e606669e979296 [file] [log] [blame]
Darin Petkov265f2902011-05-09 15:17:40 -07001// Copyright (c) 2011 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"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070011#include "base/time.h"
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070012#include "gtest/gtest.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070013#include "update_engine/action_pipe.h"
14#include "update_engine/mock_http_fetcher.h"
15#include "update_engine/omaha_hash_calculator.h"
16#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070017#include "update_engine/omaha_request_params.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070018#include "update_engine/prefs_mock.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070019#include "update_engine/test_utils.h"
20
Darin Petkov1cbd78f2010-07-29 12:38:34 -070021using base::Time;
22using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070023using std::string;
24using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070025using testing::_;
26using testing::AllOf;
27using testing::Ge;
28using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080029using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070030using testing::Return;
31using testing::SetArgumentPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070032
33namespace chromeos_update_engine {
34
35class OmahaRequestActionTest : public ::testing::Test { };
36
37namespace {
Darin Petkov1cbd78f2010-07-29 12:38:34 -070038const OmahaRequestParams kDefaultTestParams(
Darin Petkov1cbd78f2010-07-29 12:38:34 -070039 OmahaRequestParams::kOsPlatform,
40 OmahaRequestParams::kOsVersion,
41 "service_pack",
42 "x86-generic",
43 OmahaRequestParams::kAppId,
44 "0.1.0.0",
45 "en-US",
46 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070047 "OEM MODEL 09235 7471",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070048 false, // delta okay
Jay Srinivasan0a708742012-03-20 11:26:12 -070049 "http://url",
50 false, // update_disabled
51 ""); // target_version_prefix
Darin Petkov1cbd78f2010-07-29 12:38:34 -070052
Darin Petkov6a5b3222010-07-13 14:55:28 -070053string GetNoUpdateResponse(const string& app_id) {
54 return string(
55 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
56 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
57 "appid=\"") + app_id + "\" status=\"ok\"><ping "
58 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
59}
60
61string GetUpdateResponse(const string& app_id,
62 const string& display_version,
63 const string& more_info_url,
64 const string& prompt,
65 const string& codebase,
66 const string& hash,
67 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -070068 const string& size,
69 const string& deadline) {
Jay Srinivasan0a708742012-03-20 11:26:12 -070070 return string(
71 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
72 "xmlns=\"http://www.google.com/update2/response\" "
73 "protocol=\"2.0\"><app "
74 "appid=\"") + app_id + "\" status=\"ok\"><ping "
Darin Petkov6a5b3222010-07-13 14:55:28 -070075 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
Jay Srinivasan0a708742012-03-20 11:26:12 -070076 "ChromeOSVersion=\"" + display_version + "\" "
Darin Petkov6a5b3222010-07-13 14:55:28 -070077 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070078 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -070079 "codebase=\"" + codebase + "\" hash=\"not-applicable\" "
80 "sha256=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
Darin Petkov6c118642010-10-21 12:06:30 -070081 "size=\"" + size + "\" deadline=\"" + deadline +
82 "\" status=\"ok\"/></app></gupdate>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070083}
84
85class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
86 public:
87 OmahaRequestActionTestProcessorDelegate()
88 : loop_(NULL),
Darin Petkovc1a8b422010-07-19 11:34:49 -070089 expected_code_(kActionCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070090 virtual ~OmahaRequestActionTestProcessorDelegate() {
91 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070092 virtual void ProcessingDone(const ActionProcessor* processor,
93 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070094 ASSERT_TRUE(loop_);
95 g_main_loop_quit(loop_);
96 }
97
98 virtual void ActionCompleted(ActionProcessor* processor,
99 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700100 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700101 // make sure actions always succeed
102 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700103 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700104 else
Darin Petkovc1a8b422010-07-19 11:34:49 -0700105 EXPECT_EQ(kActionCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700106 }
107 GMainLoop *loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700108 ActionExitCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700109};
110
111gboolean StartProcessorInRunLoop(gpointer data) {
112 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
113 processor->StartProcessing();
114 return FALSE;
115}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700116} // namespace {}
117
118class OutputObjectCollectorAction;
119
120template<>
121class ActionTraits<OutputObjectCollectorAction> {
122 public:
123 // Does not take an object for input
124 typedef OmahaResponse InputObjectType;
125 // On success, puts the output path on output
126 typedef NoneType OutputObjectType;
127};
128
129class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
130 public:
131 OutputObjectCollectorAction() : has_input_object_(false) {}
132 void PerformAction() {
133 // copy input object
134 has_input_object_ = HasInputObject();
135 if (has_input_object_)
136 omaha_response_ = GetInputObject();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700137 processor_->ActionComplete(this, kActionCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700138 }
139 // Should never be called
140 void TerminateProcessing() {
141 CHECK(false);
142 }
143 // Debugging/logging
144 static std::string StaticType() {
145 return "OutputObjectCollectorAction";
146 }
147 std::string Type() const { return StaticType(); }
148 bool has_input_object_;
149 OmahaResponse omaha_response_;
150};
151
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700152// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700153// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
Darin Petkov265f2902011-05-09 15:17:40 -0700154// used. out_response may be NULL. If |fail_http_response_code| is non-negative,
155// the transfer will fail with that code. |ping_only| is passed through to the
156// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
157// post-data received by the mock HttpFetcher is returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700158bool TestUpdateCheck(PrefsInterface* prefs,
159 const OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700160 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700161 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700162 bool ping_only,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700163 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700164 OmahaResponse* out_response,
165 vector<char>* out_post_data) {
166 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
167 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800168 http_response.size(),
169 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700170 if (fail_http_response_code >= 0) {
171 fetcher->FailTransfer(fail_http_response_code);
172 }
Darin Petkov9c096d62010-11-17 14:49:04 -0800173 NiceMock<PrefsMock> local_prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700174 OmahaRequestAction action(prefs ? prefs : &local_prefs,
175 params,
176 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700177 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700178 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700179 OmahaRequestActionTestProcessorDelegate delegate;
180 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700181 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700182
Darin Petkov6a5b3222010-07-13 14:55:28 -0700183 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700184 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700185 processor.EnqueueAction(&action);
186
187 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700188 BondActions(&action, &collector_action);
189 processor.EnqueueAction(&collector_action);
190
191 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
192 g_main_loop_run(loop);
193 g_main_loop_unref(loop);
194 if (collector_action.has_input_object_ && out_response)
195 *out_response = collector_action.omaha_response_;
196 if (out_post_data)
197 *out_post_data = fetcher->post_data();
198 return collector_action.has_input_object_;
199}
200
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700201// Tests Event requests -- they should always succeed. |out_post_data|
202// may be null; if non-null, the post-data received by the mock
203// HttpFetcher is returned.
204void TestEvent(const OmahaRequestParams& params,
205 OmahaEvent* event,
206 const string& http_response,
207 vector<char>* out_post_data) {
208 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
209 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800210 http_response.size(),
211 NULL);
Darin Petkov9c096d62010-11-17 14:49:04 -0800212 NiceMock<PrefsMock> prefs;
Thieu Le116fda32011-04-19 11:01:54 -0700213 OmahaRequestAction action(&prefs, params, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700214 OmahaRequestActionTestProcessorDelegate delegate;
215 delegate.loop_ = loop;
216 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700217 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700218 processor.EnqueueAction(&action);
219
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700220 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
221 g_main_loop_run(loop);
222 g_main_loop_unref(loop);
223 if (out_post_data)
224 *out_post_data = fetcher->post_data();
225}
226
Darin Petkov6a5b3222010-07-13 14:55:28 -0700227TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700228 OmahaResponse response;
229 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700230 TestUpdateCheck(NULL, // prefs
231 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700232 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700233 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700234 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700235 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700236 &response,
237 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238 EXPECT_FALSE(response.update_exists);
239}
240
241TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700242 OmahaResponse response;
243 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700244 TestUpdateCheck(NULL, // prefs
245 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700246 GetUpdateResponse(OmahaRequestParams::kAppId,
247 "1.2.3.4", // version
248 "http://more/info",
249 "true", // prompt
250 "http://code/base", // dl url
251 "HASH1234=", // checksum
252 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700253 "123", // size
254 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700255 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700256 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700257 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700258 &response,
259 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260 EXPECT_TRUE(response.update_exists);
261 EXPECT_EQ("1.2.3.4", response.display_version);
262 EXPECT_EQ("http://code/base", response.codebase);
263 EXPECT_EQ("http://more/info", response.more_info_url);
264 EXPECT_EQ("HASH1234=", response.hash);
265 EXPECT_EQ(123, response.size);
266 EXPECT_FALSE(response.needs_admin);
267 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700268 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700269}
270
Jay Srinivasan0a708742012-03-20 11:26:12 -0700271TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) {
272 OmahaResponse response;
273 OmahaRequestParams params = kDefaultTestParams;
274 params.update_disabled = true;
275 ASSERT_FALSE(
276 TestUpdateCheck(NULL, // prefs
277 params,
278 GetUpdateResponse(OmahaRequestParams::kAppId,
279 "1.2.3.4", // version
280 "http://more/info",
281 "true", // prompt
282 "http://code/base", // dl url
283 "HASH1234=", // checksum
284 "false", // needs admin
285 "123", // size
286 "20101020"), // deadline
287 -1,
288 false, // ping_only
289 kActionCodeOmahaUpdateIgnoredPerPolicy,
290 &response,
291 NULL));
292 EXPECT_FALSE(response.update_exists);
293}
294
295
296TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) {
297 OmahaResponse response;
298 OmahaRequestParams params = kDefaultTestParams;
299 params.update_disabled = true;
300 ASSERT_TRUE(
301 TestUpdateCheck(NULL, // prefs
302 params,
303 GetNoUpdateResponse(OmahaRequestParams::kAppId),
304 -1,
305 false, // ping_only
306 kActionCodeSuccess,
307 &response,
308 NULL));
309 EXPECT_FALSE(response.update_exists);
310}
311
312
Darin Petkov6a5b3222010-07-13 14:55:28 -0700313TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700314 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
315
316 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
317
Darin Petkov9c096d62010-11-17 14:49:04 -0800318 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700319 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700320 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800321 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700322 NULL),
323 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700324 OmahaRequestActionTestProcessorDelegate delegate;
325 delegate.loop_ = loop;
326 ActionProcessor processor;
327 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700328 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700329
330 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
331 g_main_loop_run(loop);
332 g_main_loop_unref(loop);
333 EXPECT_FALSE(processor.IsRunning());
334}
335
336TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700337 OmahaResponse response;
338 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700339 TestUpdateCheck(NULL, // prefs
340 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700341 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700342 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700343 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700344 kActionCodeOmahaRequestXMLParseError,
345 &response,
346 NULL));
347 EXPECT_FALSE(response.update_exists);
348}
349
350TEST(OmahaRequestActionTest, EmptyResponseTest) {
351 OmahaResponse response;
352 ASSERT_FALSE(
353 TestUpdateCheck(NULL, // prefs
354 kDefaultTestParams,
355 "",
356 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700357 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700358 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700359 &response,
360 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700361 EXPECT_FALSE(response.update_exists);
362}
363
364TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700365 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700366 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700367 NULL, // prefs
368 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700369 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
370 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
371 "appid=\"foo\" status=\"ok\"><ping "
372 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700373 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700374 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700375 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700376 &response,
377 NULL));
378 EXPECT_FALSE(response.update_exists);
379}
380
381TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700382 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700383 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700384 NULL, // prefs
385 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700386 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
387 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
388 "appid=\"foo\" status=\"ok\"><ping "
389 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700390 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700391 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700392 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700393 &response,
394 NULL));
395 EXPECT_FALSE(response.update_exists);
396}
397
398TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700399 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700400 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700401 NULL, // prefs
402 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700403 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
404 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
405 "appid=\"foo\" status=\"ok\"><ping "
406 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700407 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700408 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700409 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700410 &response,
411 NULL));
412 EXPECT_FALSE(response.update_exists);
413}
414
415TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700416 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700417 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
418 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700419 string("<?xml version=\"1.0\" "
420 "encoding=\"UTF-8\"?><gupdate "
421 "xmlns=\"http://www.google.com/"
422 "update2/response\" "
423 "protocol=\"2.0\"><app appid=\"") +
424 OmahaRequestParams::kAppId
425 + "\" status=\"ok\"><ping "
426 "status=\"ok\"/><updatecheck "
427 "DisplayVersion=\"1.2.3.4\" "
Jay Srinivasan0a708742012-03-20 11:26:12 -0700428 "ChromeOSVersion=\"1.2.3.4\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700429 "Prompt=\"false\" "
Darin Petkov7ed561b2011-10-04 02:59:03 -0700430 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700431 "codebase=\"http://code/base\" hash=\"foo\" "
432 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700433 "size=\"123\" "
434 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700435 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700436 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700437 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700438 &response,
439 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700440 EXPECT_TRUE(response.update_exists);
441 EXPECT_EQ("1.2.3.4", response.display_version);
442 EXPECT_EQ("http://code/base", response.codebase);
443 EXPECT_EQ("", response.more_info_url);
444 EXPECT_EQ("HASH1234=", response.hash);
445 EXPECT_EQ(123, response.size);
446 EXPECT_TRUE(response.needs_admin);
447 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700448 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700449}
450
451namespace {
452class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
453 public:
454 void ProcessingStopped(const ActionProcessor* processor) {
455 ASSERT_TRUE(loop_);
456 g_main_loop_quit(loop_);
457 }
458 GMainLoop *loop_;
459};
460
461gboolean TerminateTransferTestStarter(gpointer data) {
462 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
463 processor->StartProcessing();
464 CHECK(processor->IsRunning());
465 processor->StopProcessing();
466 return FALSE;
467}
468} // namespace {}
469
470TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700471 string http_response("doesn't matter");
472 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
473
Darin Petkov9c096d62010-11-17 14:49:04 -0800474 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700475 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700476 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800477 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700478 NULL),
479 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700480 TerminateEarlyTestProcessorDelegate delegate;
481 delegate.loop_ = loop;
482 ActionProcessor processor;
483 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700484 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700485
486 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
487 g_main_loop_run(loop);
488 g_main_loop_unref(loop);
489}
490
491TEST(OmahaRequestActionTest, XmlEncodeTest) {
492 EXPECT_EQ("ab", XmlEncode("ab"));
493 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
494 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
495 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
496 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
497
498 vector<char> post_data;
499
500 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700501 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700502 OmahaRequestParams::kOsVersion,
503 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700504 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700505 OmahaRequestParams::kAppId,
506 "0.1.0.0",
507 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700508 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700509 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700510 false, // delta okay
Jay Srinivasan0a708742012-03-20 11:26:12 -0700511 "http://url",
512 false, // update_disabled
513 ""); // target_version_prefix
Darin Petkov6a5b3222010-07-13 14:55:28 -0700514 OmahaResponse response;
515 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700516 TestUpdateCheck(NULL, // prefs
517 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700518 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700519 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700520 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700521 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700522 &response,
523 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700524 // convert post_data to string
525 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700526 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
527 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700528 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
529 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
530 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
531 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700532 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
533 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700534}
535
536TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700537 OmahaResponse response;
538 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700539 TestUpdateCheck(NULL, // prefs
540 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700541 GetUpdateResponse(OmahaRequestParams::kAppId,
542 "1.2.3.4", // version
543 "testthe&lt;url", // more info
544 "true", // prompt
545 "testthe&amp;codebase", // dl url
546 "HASH1234=", // checksum
547 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700548 "123", // size
549 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700550 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700551 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700552 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700553 &response,
554 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700555
556 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700557 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700558 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700559}
560
561TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700562 OmahaResponse response;
563 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700564 TestUpdateCheck(NULL, // prefs
565 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700566 GetUpdateResponse(OmahaRequestParams::kAppId,
567 "1.2.3.4", // version
568 "theurl", // more info
569 "true", // prompt
570 "thecodebase", // dl url
571 "HASH1234=", // checksum
572 "false", // needs admin
573 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700574 "123123123123123", // size
575 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700576 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700577 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700578 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700579 &response,
580 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700581
582 EXPECT_EQ(response.size, 123123123123123ll);
583}
584
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700585TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
586 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800587 NiceMock<PrefsMock> prefs;
588 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
589 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
590 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
591 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700592 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700593 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700594 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700595 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700596 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700597 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700598 &post_data));
599 // convert post_data to string
600 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700601 EXPECT_NE(post_str.find(
Jay Srinivasan0a708742012-03-20 11:26:12 -0700602 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
603 " <o:updatecheck"
604 " updatedisabled=\"false\""
605 " targetversionprefix=\"\""
606 "></o:updatecheck>\n"),
607 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700608 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
609 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700610 EXPECT_EQ(post_str.find("o:event"), string::npos);
611}
612
Jay Srinivasan0a708742012-03-20 11:26:12 -0700613
614TEST(OmahaRequestActionTest, FormatUpdateDisabledTest) {
Darin Petkov95508da2011-01-05 12:42:29 -0800615 vector<char> post_data;
616 NiceMock<PrefsMock> prefs;
617 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Jay Srinivasan0a708742012-03-20 11:26:12 -0700618 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
619 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
620 OmahaRequestParams params = kDefaultTestParams;
621 params.update_disabled = true;
Darin Petkov95508da2011-01-05 12:42:29 -0800622 ASSERT_FALSE(TestUpdateCheck(&prefs,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700623 params,
Darin Petkov95508da2011-01-05 12:42:29 -0800624 "invalid xml>",
625 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700626 false, // ping_only
Darin Petkov95508da2011-01-05 12:42:29 -0800627 kActionCodeOmahaRequestXMLParseError,
628 NULL, // response
629 &post_data));
630 // convert post_data to string
631 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700632 EXPECT_NE(post_str.find(
Jay Srinivasan0a708742012-03-20 11:26:12 -0700633 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
634 " <o:updatecheck"
635 " updatedisabled=\"true\""
636 " targetversionprefix=\"\""
637 "></o:updatecheck>\n"),
638 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -0800639 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
640 string::npos);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700641 EXPECT_EQ(post_str.find("o:event"), string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -0800642}
643
Darin Petkove17f86b2010-07-20 09:12:01 -0700644TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
645 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700646 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700647 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
648 "invalid xml>",
649 &post_data);
650 // convert post_data to string
651 string post_str(&post_data[0], post_data.size());
652 string expected_event = StringPrintf(
653 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
654 OmahaEvent::kTypeUpdateDownloadStarted,
655 OmahaEvent::kResultSuccess);
656 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700657 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700658 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
659}
660
661TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
662 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700663 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700664 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
665 OmahaEvent::kResultError,
666 kActionCodeError),
667 "invalid xml>",
668 &post_data);
669 // convert post_data to string
670 string post_str(&post_data[0], post_data.size());
671 string expected_event = StringPrintf(
672 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
673 "errorcode=\"%d\"></o:event>\n",
674 OmahaEvent::kTypeDownloadComplete,
675 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700676 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700677 EXPECT_NE(post_str.find(expected_event), string::npos);
678 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
679}
680
681TEST(OmahaRequestActionTest, IsEventTest) {
682 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800683 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700684 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700685 &prefs,
686 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700687 NULL,
688 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800689 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700690 NULL),
691 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700692 EXPECT_FALSE(update_check_action.IsEvent());
693
694 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700695 &prefs,
696 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700697 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700698 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800699 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700700 NULL),
701 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700702 EXPECT_TRUE(event_action.IsEvent());
703}
704
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700705TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
706 for (int i = 0; i < 2; i++) {
707 bool delta_okay = i == 1;
708 const char* delta_okay_str = delta_okay ? "true" : "false";
709 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700710 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700711 OmahaRequestParams::kOsVersion,
712 "service_pack",
713 "x86-generic",
714 OmahaRequestParams::kAppId,
715 "0.1.0.0",
716 "en-US",
717 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700718 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700719 delta_okay,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700720 "http://url",
721 false, // update_disabled
722 ""); // target_version_prefix
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700723 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
724 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700725 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700726 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700727 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700728 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700729 NULL,
730 &post_data));
731 // convert post_data to string
732 string post_str(&post_data[0], post_data.size());
733 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
734 string::npos)
735 << "i = " << i;
736 }
737}
738
Darin Petkove17f86b2010-07-20 09:12:01 -0700739TEST(OmahaRequestActionTest, OmahaEventTest) {
740 OmahaEvent default_event;
741 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
742 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
743 EXPECT_EQ(kActionCodeError, default_event.error_code);
744
745 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
746 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
747 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
748 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
749
750 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
751 OmahaEvent::kResultError,
752 kActionCodeError);
753 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
754 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
755 EXPECT_EQ(kActionCodeError, error_event.error_code);
756}
757
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700758TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -0700759 for (int ping_only = 0; ping_only < 2; ping_only++) {
760 NiceMock<PrefsMock> prefs;
761 // Add a few hours to the day difference to test no rounding, etc.
762 int64_t five_days_ago =
763 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
764 int64_t six_days_ago =
765 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
766 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
767 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
768 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
769 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
770 vector<char> post_data;
771 ASSERT_TRUE(
772 TestUpdateCheck(&prefs,
773 kDefaultTestParams,
774 GetNoUpdateResponse(OmahaRequestParams::kAppId),
775 -1,
776 ping_only,
777 kActionCodeSuccess,
778 NULL,
779 &post_data));
780 string post_str(&post_data[0], post_data.size());
781 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"),
782 string::npos);
783 if (ping_only) {
784 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
785 EXPECT_EQ(post_str.find("previousversion"), string::npos);
786 } else {
787 EXPECT_NE(post_str.find("o:updatecheck"), string::npos);
788 EXPECT_NE(post_str.find("previousversion"), string::npos);
789 }
790 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700791}
792
793TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800794 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700795 int64_t three_days_ago =
796 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
797 int64_t now = Time::Now().ToInternalValue();
798 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
799 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
800 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
801 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
802 vector<char> post_data;
803 ASSERT_TRUE(
804 TestUpdateCheck(&prefs,
805 kDefaultTestParams,
806 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700807 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700808 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700809 kActionCodeSuccess,
810 NULL,
811 &post_data));
812 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700813 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"3\"></o:ping>"),
814 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700815}
816
817TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800818 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700819 int64_t four_days_ago =
820 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
821 int64_t now = Time::Now().ToInternalValue();
822 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
823 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
824 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
825 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
826 vector<char> post_data;
827 ASSERT_TRUE(
828 TestUpdateCheck(&prefs,
829 kDefaultTestParams,
830 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700831 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700832 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700833 kActionCodeSuccess,
834 NULL,
835 &post_data));
836 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700837 EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"),
838 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700839}
840
841TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800842 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700843 int64_t one_hour_ago =
844 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
845 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
846 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
847 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
848 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
849 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
850 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
851 vector<char> post_data;
852 ASSERT_TRUE(
853 TestUpdateCheck(&prefs,
854 kDefaultTestParams,
855 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700856 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700857 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700858 kActionCodeSuccess,
859 NULL,
860 &post_data));
861 string post_str(&post_data[0], post_data.size());
862 EXPECT_EQ(post_str.find("o:ping"), string::npos);
863}
864
Thieu Leb44e9e82011-06-06 14:34:04 -0700865TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
866 // This test ensures that we ignore empty ping only requests.
867 NiceMock<PrefsMock> prefs;
868 int64_t now = Time::Now().ToInternalValue();
869 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
870 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
871 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
872 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
873 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
874 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
875 vector<char> post_data;
876 EXPECT_TRUE(
877 TestUpdateCheck(&prefs,
878 kDefaultTestParams,
879 GetNoUpdateResponse(OmahaRequestParams::kAppId),
880 -1,
881 true, // ping_only
882 kActionCodeSuccess,
883 NULL,
884 &post_data));
885 EXPECT_EQ(post_data.size(), 0);
886}
887
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700888TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800889 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700890 int64_t future =
891 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
892 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
893 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
894 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
895 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
896 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
897 .WillOnce(Return(true));
898 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
899 .WillOnce(Return(true));
900 vector<char> post_data;
901 ASSERT_TRUE(
902 TestUpdateCheck(&prefs,
903 kDefaultTestParams,
904 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
905 "xmlns=\"http://www.google.com/update2/response\" "
906 "protocol=\"2.0\"><daystart elapsed_seconds=\"100\"/>"
907 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
908 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700909 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700910 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700911 kActionCodeSuccess,
912 NULL,
913 &post_data));
914 string post_str(&post_data[0], post_data.size());
915 EXPECT_EQ(post_str.find("o:ping"), string::npos);
916}
917
918TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
919 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700920 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700921 // may fail if it runs for longer than 5 seconds. It shouldn't run
922 // that long though.
923 int64_t midnight =
924 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
925 int64_t midnight_slack =
926 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800927 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700928 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
929 AllOf(Ge(midnight), Le(midnight_slack))))
930 .WillOnce(Return(true));
931 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
932 AllOf(Ge(midnight), Le(midnight_slack))))
933 .WillOnce(Return(true));
934 ASSERT_TRUE(
935 TestUpdateCheck(&prefs,
936 kDefaultTestParams,
937 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
938 "xmlns=\"http://www.google.com/update2/response\" "
939 "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>"
940 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
941 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700942 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700943 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700944 kActionCodeSuccess,
945 NULL,
946 NULL));
947}
948
949TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800950 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700951 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
952 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
953 ASSERT_TRUE(
954 TestUpdateCheck(&prefs,
955 kDefaultTestParams,
956 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
957 "xmlns=\"http://www.google.com/update2/response\" "
958 "protocol=\"2.0\"><daystart blah=\"200\"/>"
959 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
960 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700961 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700962 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700963 kActionCodeSuccess,
964 NULL,
965 NULL));
966}
967
968TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800969 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700970 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
971 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
972 ASSERT_TRUE(
973 TestUpdateCheck(&prefs,
974 kDefaultTestParams,
975 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
976 "xmlns=\"http://www.google.com/update2/response\" "
977 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
978 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
979 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700980 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700981 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700982 kActionCodeSuccess,
983 NULL,
984 NULL));
985}
986
Darin Petkov84c763c2010-07-29 16:27:58 -0700987TEST(OmahaRequestActionTest, NoUniqueIDTest) {
988 vector<char> post_data;
989 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
990 kDefaultTestParams,
991 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700992 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700993 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700994 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700995 NULL, // response
996 &post_data));
997 // convert post_data to string
998 string post_str(&post_data[0], post_data.size());
999 EXPECT_EQ(post_str.find("machineid="), string::npos);
1000 EXPECT_EQ(post_str.find("userid="), string::npos);
1001}
1002
Darin Petkovedc522e2010-11-05 09:35:17 -07001003TEST(OmahaRequestActionTest, NetworkFailureTest) {
1004 OmahaResponse response;
1005 ASSERT_FALSE(
1006 TestUpdateCheck(NULL, // prefs
1007 kDefaultTestParams,
1008 "",
1009 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001010 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -07001011 static_cast<ActionExitCode>(
1012 kActionCodeOmahaRequestHTTPResponseBase + 501),
1013 &response,
1014 NULL));
1015 EXPECT_FALSE(response.update_exists);
1016}
1017
1018TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
1019 OmahaResponse response;
1020 ASSERT_FALSE(
1021 TestUpdateCheck(NULL, // prefs
1022 kDefaultTestParams,
1023 "",
1024 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001025 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -07001026 static_cast<ActionExitCode>(
1027 kActionCodeOmahaRequestHTTPResponseBase + 999),
1028 &response,
1029 NULL));
1030 EXPECT_FALSE(response.update_exists);
1031}
1032
Darin Petkov6a5b3222010-07-13 14:55:28 -07001033} // namespace chromeos_update_engine