blob: 7a0d796c4bf78bc3f6f6c618ebbaac22143f92f9 [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
49 "http://url");
50
Darin Petkov6a5b3222010-07-13 14:55:28 -070051string GetNoUpdateResponse(const string& app_id) {
52 return string(
53 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
54 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
55 "appid=\"") + app_id + "\" status=\"ok\"><ping "
56 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
57}
58
59string GetUpdateResponse(const string& app_id,
60 const string& display_version,
61 const string& more_info_url,
62 const string& prompt,
63 const string& codebase,
64 const string& hash,
65 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -070066 const string& size,
67 const string& deadline) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070068 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
69 "xmlns=\"http://www.google.com/update2/response\" "
70 "protocol=\"2.0\"><app "
71 "appid=\"") + app_id + "\" status=\"ok\"><ping "
72 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
73 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070074 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -070075 "codebase=\"" + codebase + "\" hash=\"not-applicable\" "
76 "sha256=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
Darin Petkov6c118642010-10-21 12:06:30 -070077 "size=\"" + size + "\" deadline=\"" + deadline +
78 "\" status=\"ok\"/></app></gupdate>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070079}
80
81class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
82 public:
83 OmahaRequestActionTestProcessorDelegate()
84 : loop_(NULL),
Darin Petkovc1a8b422010-07-19 11:34:49 -070085 expected_code_(kActionCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070086 virtual ~OmahaRequestActionTestProcessorDelegate() {
87 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070088 virtual void ProcessingDone(const ActionProcessor* processor,
89 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070090 ASSERT_TRUE(loop_);
91 g_main_loop_quit(loop_);
92 }
93
94 virtual void ActionCompleted(ActionProcessor* processor,
95 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070096 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070097 // make sure actions always succeed
98 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -070099 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700100 else
Darin Petkovc1a8b422010-07-19 11:34:49 -0700101 EXPECT_EQ(kActionCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700102 }
103 GMainLoop *loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700104 ActionExitCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105};
106
107gboolean StartProcessorInRunLoop(gpointer data) {
108 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
109 processor->StartProcessing();
110 return FALSE;
111}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700112} // namespace {}
113
114class OutputObjectCollectorAction;
115
116template<>
117class ActionTraits<OutputObjectCollectorAction> {
118 public:
119 // Does not take an object for input
120 typedef OmahaResponse InputObjectType;
121 // On success, puts the output path on output
122 typedef NoneType OutputObjectType;
123};
124
125class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
126 public:
127 OutputObjectCollectorAction() : has_input_object_(false) {}
128 void PerformAction() {
129 // copy input object
130 has_input_object_ = HasInputObject();
131 if (has_input_object_)
132 omaha_response_ = GetInputObject();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700133 processor_->ActionComplete(this, kActionCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700134 }
135 // Should never be called
136 void TerminateProcessing() {
137 CHECK(false);
138 }
139 // Debugging/logging
140 static std::string StaticType() {
141 return "OutputObjectCollectorAction";
142 }
143 std::string Type() const { return StaticType(); }
144 bool has_input_object_;
145 OmahaResponse omaha_response_;
146};
147
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700148// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700149// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
Darin Petkov265f2902011-05-09 15:17:40 -0700150// used. out_response may be NULL. If |fail_http_response_code| is non-negative,
151// the transfer will fail with that code. |ping_only| is passed through to the
152// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
153// post-data received by the mock HttpFetcher is returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700154bool TestUpdateCheck(PrefsInterface* prefs,
155 const OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700156 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700157 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700158 bool ping_only,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700159 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700160 OmahaResponse* out_response,
161 vector<char>* out_post_data) {
162 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
163 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800164 http_response.size(),
165 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700166 if (fail_http_response_code >= 0) {
167 fetcher->FailTransfer(fail_http_response_code);
168 }
Darin Petkov9c096d62010-11-17 14:49:04 -0800169 NiceMock<PrefsMock> local_prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700170 OmahaRequestAction action(prefs ? prefs : &local_prefs,
171 params,
172 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700173 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700174 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700175 OmahaRequestActionTestProcessorDelegate delegate;
176 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700177 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700178
Darin Petkov6a5b3222010-07-13 14:55:28 -0700179 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700180 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700181 processor.EnqueueAction(&action);
182
183 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700184 BondActions(&action, &collector_action);
185 processor.EnqueueAction(&collector_action);
186
187 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
188 g_main_loop_run(loop);
189 g_main_loop_unref(loop);
190 if (collector_action.has_input_object_ && out_response)
191 *out_response = collector_action.omaha_response_;
192 if (out_post_data)
193 *out_post_data = fetcher->post_data();
194 return collector_action.has_input_object_;
195}
196
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700197// Tests Event requests -- they should always succeed. |out_post_data|
198// may be null; if non-null, the post-data received by the mock
199// HttpFetcher is returned.
200void TestEvent(const OmahaRequestParams& params,
201 OmahaEvent* event,
202 const string& http_response,
203 vector<char>* out_post_data) {
204 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
205 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800206 http_response.size(),
207 NULL);
Darin Petkov9c096d62010-11-17 14:49:04 -0800208 NiceMock<PrefsMock> prefs;
Thieu Le116fda32011-04-19 11:01:54 -0700209 OmahaRequestAction action(&prefs, params, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700210 OmahaRequestActionTestProcessorDelegate delegate;
211 delegate.loop_ = loop;
212 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700213 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700214 processor.EnqueueAction(&action);
215
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700216 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
217 g_main_loop_run(loop);
218 g_main_loop_unref(loop);
219 if (out_post_data)
220 *out_post_data = fetcher->post_data();
221}
222
Darin Petkov6a5b3222010-07-13 14:55:28 -0700223TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700224 OmahaResponse response;
225 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700226 TestUpdateCheck(NULL, // prefs
227 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700228 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700229 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700230 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700231 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700232 &response,
233 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700234 EXPECT_FALSE(response.update_exists);
235}
236
237TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238 OmahaResponse response;
239 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700240 TestUpdateCheck(NULL, // prefs
241 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700242 GetUpdateResponse(OmahaRequestParams::kAppId,
243 "1.2.3.4", // version
244 "http://more/info",
245 "true", // prompt
246 "http://code/base", // dl url
247 "HASH1234=", // checksum
248 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700249 "123", // size
250 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700251 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700252 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700253 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700254 &response,
255 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700256 EXPECT_TRUE(response.update_exists);
257 EXPECT_EQ("1.2.3.4", response.display_version);
258 EXPECT_EQ("http://code/base", response.codebase);
259 EXPECT_EQ("http://more/info", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700260 EXPECT_TRUE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700261 EXPECT_EQ("HASH1234=", response.hash);
262 EXPECT_EQ(123, response.size);
263 EXPECT_FALSE(response.needs_admin);
264 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700265 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700266}
267
268TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700269 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
270
271 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
272
Darin Petkov9c096d62010-11-17 14:49:04 -0800273 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700274 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700275 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800276 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700277 NULL),
278 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700279 OmahaRequestActionTestProcessorDelegate delegate;
280 delegate.loop_ = loop;
281 ActionProcessor processor;
282 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700283 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700284
285 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
286 g_main_loop_run(loop);
287 g_main_loop_unref(loop);
288 EXPECT_FALSE(processor.IsRunning());
289}
290
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700291TEST(OmahaRequestActionTest, SkipTest) {
292 const string http_response("invalid xml>");
293
294 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
295
296 NiceMock<PrefsMock> prefs;
297 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
298 http_response.size(),
299 NULL);
300 fetcher->set_never_use(true);
301 OmahaRequestAction action(&prefs, kDefaultTestParams,
302 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Thieu Le116fda32011-04-19 11:01:54 -0700303 fetcher, // Passes fetcher ownership
304 false);
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700305 action.set_should_skip(true);
306 OmahaRequestActionTestProcessorDelegate delegate;
307 delegate.loop_ = loop;
308 ActionProcessor processor;
309 processor.set_delegate(&delegate);
310 processor.EnqueueAction(&action);
311
312 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
313 g_main_loop_run(loop);
314 g_main_loop_unref(loop);
315 EXPECT_FALSE(processor.IsRunning());
316}
317
Darin Petkov6a5b3222010-07-13 14:55:28 -0700318TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700319 OmahaResponse response;
320 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700321 TestUpdateCheck(NULL, // prefs
322 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700323 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700324 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700325 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700326 kActionCodeOmahaRequestXMLParseError,
327 &response,
328 NULL));
329 EXPECT_FALSE(response.update_exists);
330}
331
332TEST(OmahaRequestActionTest, EmptyResponseTest) {
333 OmahaResponse response;
334 ASSERT_FALSE(
335 TestUpdateCheck(NULL, // prefs
336 kDefaultTestParams,
337 "",
338 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700339 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700340 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700341 &response,
342 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700343 EXPECT_FALSE(response.update_exists);
344}
345
346TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700347 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700348 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700349 NULL, // prefs
350 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700351 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
352 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
353 "appid=\"foo\" status=\"ok\"><ping "
354 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700355 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700356 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700357 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700358 &response,
359 NULL));
360 EXPECT_FALSE(response.update_exists);
361}
362
363TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700364 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700365 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700366 NULL, // prefs
367 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700368 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
369 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
370 "appid=\"foo\" status=\"ok\"><ping "
371 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700372 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700373 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700374 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700375 &response,
376 NULL));
377 EXPECT_FALSE(response.update_exists);
378}
379
380TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700381 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700382 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700383 NULL, // prefs
384 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700385 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
386 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
387 "appid=\"foo\" status=\"ok\"><ping "
388 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700389 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700390 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700391 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700392 &response,
393 NULL));
394 EXPECT_FALSE(response.update_exists);
395}
396
397TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700398 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700399 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
400 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700401 string("<?xml version=\"1.0\" "
402 "encoding=\"UTF-8\"?><gupdate "
403 "xmlns=\"http://www.google.com/"
404 "update2/response\" "
405 "protocol=\"2.0\"><app appid=\"") +
406 OmahaRequestParams::kAppId
407 + "\" status=\"ok\"><ping "
408 "status=\"ok\"/><updatecheck "
409 "DisplayVersion=\"1.2.3.4\" "
410 "Prompt=\"false\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700411 "codebase=\"http://code/base\" hash=\"foo\" "
412 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700413 "size=\"123\" "
414 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700415 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700416 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700417 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700418 &response,
419 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700420 EXPECT_TRUE(response.update_exists);
421 EXPECT_EQ("1.2.3.4", response.display_version);
422 EXPECT_EQ("http://code/base", response.codebase);
423 EXPECT_EQ("", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700424 EXPECT_FALSE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700425 EXPECT_EQ("HASH1234=", response.hash);
426 EXPECT_EQ(123, response.size);
427 EXPECT_TRUE(response.needs_admin);
428 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700429 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700430}
431
432namespace {
433class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
434 public:
435 void ProcessingStopped(const ActionProcessor* processor) {
436 ASSERT_TRUE(loop_);
437 g_main_loop_quit(loop_);
438 }
439 GMainLoop *loop_;
440};
441
442gboolean TerminateTransferTestStarter(gpointer data) {
443 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
444 processor->StartProcessing();
445 CHECK(processor->IsRunning());
446 processor->StopProcessing();
447 return FALSE;
448}
449} // namespace {}
450
451TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700452 string http_response("doesn't matter");
453 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
454
Darin Petkov9c096d62010-11-17 14:49:04 -0800455 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700456 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700457 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800458 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700459 NULL),
460 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700461 TerminateEarlyTestProcessorDelegate delegate;
462 delegate.loop_ = loop;
463 ActionProcessor processor;
464 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700465 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700466
467 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
468 g_main_loop_run(loop);
469 g_main_loop_unref(loop);
470}
471
472TEST(OmahaRequestActionTest, XmlEncodeTest) {
473 EXPECT_EQ("ab", XmlEncode("ab"));
474 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
475 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
476 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
477 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
478
479 vector<char> post_data;
480
481 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700482 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700483 OmahaRequestParams::kOsVersion,
484 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700485 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700486 OmahaRequestParams::kAppId,
487 "0.1.0.0",
488 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700489 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700490 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700491 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700492 "http://url");
493 OmahaResponse response;
494 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700495 TestUpdateCheck(NULL, // prefs
496 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700497 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700498 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700499 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700500 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700501 &response,
502 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700503 // convert post_data to string
504 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700505 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
506 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700507 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
508 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
509 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
510 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700511 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
512 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700513}
514
515TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700516 OmahaResponse response;
517 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700518 TestUpdateCheck(NULL, // prefs
519 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700520 GetUpdateResponse(OmahaRequestParams::kAppId,
521 "1.2.3.4", // version
522 "testthe&lt;url", // more info
523 "true", // prompt
524 "testthe&amp;codebase", // dl url
525 "HASH1234=", // checksum
526 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700527 "123", // size
528 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700529 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700530 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700531 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700532 &response,
533 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700534
535 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700536 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700537 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700538}
539
540TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700541 OmahaResponse response;
542 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700543 TestUpdateCheck(NULL, // prefs
544 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700545 GetUpdateResponse(OmahaRequestParams::kAppId,
546 "1.2.3.4", // version
547 "theurl", // more info
548 "true", // prompt
549 "thecodebase", // dl url
550 "HASH1234=", // checksum
551 "false", // needs admin
552 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700553 "123123123123123", // size
554 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700555 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700556 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700557 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700558 &response,
559 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700560
561 EXPECT_EQ(response.size, 123123123123123ll);
562}
563
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700564TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
565 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800566 NiceMock<PrefsMock> prefs;
567 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
568 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
569 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
570 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700571 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700572 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700573 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700574 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700575 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700576 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700577 &post_data));
578 // convert post_data to string
579 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700580 EXPECT_NE(post_str.find(
581 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
582 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700583 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700584 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
585 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700586 EXPECT_EQ(post_str.find("o:event"), string::npos);
587}
588
Darin Petkov95508da2011-01-05 12:42:29 -0800589TEST(OmahaRequestActionTest, FormatUpdateCheckPrevVersionOutputTest) {
590 vector<char> post_data;
591 NiceMock<PrefsMock> prefs;
592 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800593 .WillOnce(DoAll(SetArgumentPointee<1>(string("1.2>3.4")), Return(true)));
Darin Petkov95508da2011-01-05 12:42:29 -0800594 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, ""))
595 .WillOnce(Return(true));
596 ASSERT_FALSE(TestUpdateCheck(&prefs,
597 kDefaultTestParams,
598 "invalid xml>",
599 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700600 false, // ping_only
Darin Petkov95508da2011-01-05 12:42:29 -0800601 kActionCodeOmahaRequestXMLParseError,
602 NULL, // response
603 &post_data));
604 // convert post_data to string
605 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700606 EXPECT_NE(post_str.find(
607 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
608 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov95508da2011-01-05 12:42:29 -0800609 string::npos);
610 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
611 string::npos);
612 string prev_version_event = StringPrintf(
613 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800614 "previousversion=\"1.2&gt;3.4\"></o:event>\n",
Darin Petkov95508da2011-01-05 12:42:29 -0800615 OmahaEvent::kTypeUpdateComplete,
616 OmahaEvent::kResultSuccessReboot);
617 EXPECT_NE(post_str.find(prev_version_event), string::npos);
618}
619
Darin Petkove17f86b2010-07-20 09:12:01 -0700620TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
621 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700622 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700623 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
624 "invalid xml>",
625 &post_data);
626 // convert post_data to string
627 string post_str(&post_data[0], post_data.size());
628 string expected_event = StringPrintf(
629 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
630 OmahaEvent::kTypeUpdateDownloadStarted,
631 OmahaEvent::kResultSuccess);
632 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700633 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700634 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
635}
636
637TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
638 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700639 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700640 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
641 OmahaEvent::kResultError,
642 kActionCodeError),
643 "invalid xml>",
644 &post_data);
645 // convert post_data to string
646 string post_str(&post_data[0], post_data.size());
647 string expected_event = StringPrintf(
648 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
649 "errorcode=\"%d\"></o:event>\n",
650 OmahaEvent::kTypeDownloadComplete,
651 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700652 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700653 EXPECT_NE(post_str.find(expected_event), string::npos);
654 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
655}
656
657TEST(OmahaRequestActionTest, IsEventTest) {
658 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800659 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700660 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700661 &prefs,
662 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700663 NULL,
664 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800665 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700666 NULL),
667 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700668 EXPECT_FALSE(update_check_action.IsEvent());
669
670 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700671 &prefs,
672 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700673 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700674 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800675 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700676 NULL),
677 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700678 EXPECT_TRUE(event_action.IsEvent());
679}
680
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700681TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
682 for (int i = 0; i < 2; i++) {
683 bool delta_okay = i == 1;
684 const char* delta_okay_str = delta_okay ? "true" : "false";
685 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700686 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700687 OmahaRequestParams::kOsVersion,
688 "service_pack",
689 "x86-generic",
690 OmahaRequestParams::kAppId,
691 "0.1.0.0",
692 "en-US",
693 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700694 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700695 delta_okay,
696 "http://url");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700697 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
698 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700699 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700700 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700701 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700702 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700703 NULL,
704 &post_data));
705 // convert post_data to string
706 string post_str(&post_data[0], post_data.size());
707 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
708 string::npos)
709 << "i = " << i;
710 }
711}
712
Darin Petkove17f86b2010-07-20 09:12:01 -0700713TEST(OmahaRequestActionTest, OmahaEventTest) {
714 OmahaEvent default_event;
715 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
716 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
717 EXPECT_EQ(kActionCodeError, default_event.error_code);
718
719 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
720 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
721 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
722 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
723
724 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
725 OmahaEvent::kResultError,
726 kActionCodeError);
727 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
728 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
729 EXPECT_EQ(kActionCodeError, error_event.error_code);
730}
731
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700732TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -0700733 for (int ping_only = 0; ping_only < 2; ping_only++) {
734 NiceMock<PrefsMock> prefs;
735 // Add a few hours to the day difference to test no rounding, etc.
736 int64_t five_days_ago =
737 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
738 int64_t six_days_ago =
739 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
740 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
741 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
742 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
743 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
744 vector<char> post_data;
745 ASSERT_TRUE(
746 TestUpdateCheck(&prefs,
747 kDefaultTestParams,
748 GetNoUpdateResponse(OmahaRequestParams::kAppId),
749 -1,
750 ping_only,
751 kActionCodeSuccess,
752 NULL,
753 &post_data));
754 string post_str(&post_data[0], post_data.size());
755 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"),
756 string::npos);
757 if (ping_only) {
758 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
759 EXPECT_EQ(post_str.find("previousversion"), string::npos);
760 } else {
761 EXPECT_NE(post_str.find("o:updatecheck"), string::npos);
762 EXPECT_NE(post_str.find("previousversion"), string::npos);
763 }
764 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700765}
766
767TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800768 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700769 int64_t three_days_ago =
770 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
771 int64_t now = Time::Now().ToInternalValue();
772 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
773 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
774 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
775 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
776 vector<char> post_data;
777 ASSERT_TRUE(
778 TestUpdateCheck(&prefs,
779 kDefaultTestParams,
780 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700781 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700782 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700783 kActionCodeSuccess,
784 NULL,
785 &post_data));
786 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700787 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"3\"></o:ping>"),
788 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700789}
790
791TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800792 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700793 int64_t four_days_ago =
794 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
795 int64_t now = Time::Now().ToInternalValue();
796 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
797 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
798 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
799 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
800 vector<char> post_data;
801 ASSERT_TRUE(
802 TestUpdateCheck(&prefs,
803 kDefaultTestParams,
804 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700805 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700806 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700807 kActionCodeSuccess,
808 NULL,
809 &post_data));
810 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700811 EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"),
812 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700813}
814
815TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800816 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700817 int64_t one_hour_ago =
818 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
819 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
820 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
821 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
822 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
823 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
824 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
825 vector<char> post_data;
826 ASSERT_TRUE(
827 TestUpdateCheck(&prefs,
828 kDefaultTestParams,
829 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700830 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700831 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700832 kActionCodeSuccess,
833 NULL,
834 &post_data));
835 string post_str(&post_data[0], post_data.size());
836 EXPECT_EQ(post_str.find("o:ping"), string::npos);
837}
838
Thieu Leb44e9e82011-06-06 14:34:04 -0700839TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
840 // This test ensures that we ignore empty ping only requests.
841 NiceMock<PrefsMock> prefs;
842 int64_t now = Time::Now().ToInternalValue();
843 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
844 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
845 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
846 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
847 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
848 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
849 vector<char> post_data;
850 EXPECT_TRUE(
851 TestUpdateCheck(&prefs,
852 kDefaultTestParams,
853 GetNoUpdateResponse(OmahaRequestParams::kAppId),
854 -1,
855 true, // ping_only
856 kActionCodeSuccess,
857 NULL,
858 &post_data));
859 EXPECT_EQ(post_data.size(), 0);
860}
861
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700862TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800863 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700864 int64_t future =
865 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
866 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
867 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
868 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
869 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
870 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
871 .WillOnce(Return(true));
872 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
873 .WillOnce(Return(true));
874 vector<char> post_data;
875 ASSERT_TRUE(
876 TestUpdateCheck(&prefs,
877 kDefaultTestParams,
878 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
879 "xmlns=\"http://www.google.com/update2/response\" "
880 "protocol=\"2.0\"><daystart elapsed_seconds=\"100\"/>"
881 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
882 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700883 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700884 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700885 kActionCodeSuccess,
886 NULL,
887 &post_data));
888 string post_str(&post_data[0], post_data.size());
889 EXPECT_EQ(post_str.find("o:ping"), string::npos);
890}
891
892TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
893 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700894 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700895 // may fail if it runs for longer than 5 seconds. It shouldn't run
896 // that long though.
897 int64_t midnight =
898 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
899 int64_t midnight_slack =
900 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800901 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700902 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
903 AllOf(Ge(midnight), Le(midnight_slack))))
904 .WillOnce(Return(true));
905 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
906 AllOf(Ge(midnight), Le(midnight_slack))))
907 .WillOnce(Return(true));
908 ASSERT_TRUE(
909 TestUpdateCheck(&prefs,
910 kDefaultTestParams,
911 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
912 "xmlns=\"http://www.google.com/update2/response\" "
913 "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>"
914 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
915 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700916 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700917 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700918 kActionCodeSuccess,
919 NULL,
920 NULL));
921}
922
923TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800924 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700925 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
926 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
927 ASSERT_TRUE(
928 TestUpdateCheck(&prefs,
929 kDefaultTestParams,
930 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
931 "xmlns=\"http://www.google.com/update2/response\" "
932 "protocol=\"2.0\"><daystart blah=\"200\"/>"
933 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
934 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700935 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700936 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700937 kActionCodeSuccess,
938 NULL,
939 NULL));
940}
941
942TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800943 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700944 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
945 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
946 ASSERT_TRUE(
947 TestUpdateCheck(&prefs,
948 kDefaultTestParams,
949 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
950 "xmlns=\"http://www.google.com/update2/response\" "
951 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
952 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
953 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700954 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700955 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700956 kActionCodeSuccess,
957 NULL,
958 NULL));
959}
960
Darin Petkov84c763c2010-07-29 16:27:58 -0700961TEST(OmahaRequestActionTest, NoUniqueIDTest) {
962 vector<char> post_data;
963 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
964 kDefaultTestParams,
965 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700966 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700967 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700968 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700969 NULL, // response
970 &post_data));
971 // convert post_data to string
972 string post_str(&post_data[0], post_data.size());
973 EXPECT_EQ(post_str.find("machineid="), string::npos);
974 EXPECT_EQ(post_str.find("userid="), string::npos);
975}
976
Darin Petkovedc522e2010-11-05 09:35:17 -0700977TEST(OmahaRequestActionTest, NetworkFailureTest) {
978 OmahaResponse response;
979 ASSERT_FALSE(
980 TestUpdateCheck(NULL, // prefs
981 kDefaultTestParams,
982 "",
983 501,
Darin Petkov265f2902011-05-09 15:17:40 -0700984 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700985 static_cast<ActionExitCode>(
986 kActionCodeOmahaRequestHTTPResponseBase + 501),
987 &response,
988 NULL));
989 EXPECT_FALSE(response.update_exists);
990}
991
992TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
993 OmahaResponse response;
994 ASSERT_FALSE(
995 TestUpdateCheck(NULL, // prefs
996 kDefaultTestParams,
997 "",
998 1500,
Darin Petkov265f2902011-05-09 15:17:40 -0700999 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -07001000 static_cast<ActionExitCode>(
1001 kActionCodeOmahaRequestHTTPResponseBase + 999),
1002 &response,
1003 NULL));
1004 EXPECT_FALSE(response.update_exists);
1005}
1006
Darin Petkov6a5b3222010-07-13 14:55:28 -07001007} // namespace chromeos_update_engine