blob: 3bf02e2966377da155f468bfdf328448e98250f0 [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 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
150// used. out_response may be NULL. If |fail_http_response_code| is
151// non-negative, the transfer will fail with that code. out_post_data may be
152// null; if non-null, the post-data received by the mock HttpFetcher is
153// 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 Petkovc1a8b422010-07-19 11:34:49 -0700158 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700159 OmahaResponse* out_response,
160 vector<char>* out_post_data) {
161 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
162 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800163 http_response.size(),
164 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700165 if (fail_http_response_code >= 0) {
166 fetcher->FailTransfer(fail_http_response_code);
167 }
Darin Petkov9c096d62010-11-17 14:49:04 -0800168 NiceMock<PrefsMock> local_prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700169 OmahaRequestAction action(prefs ? prefs : &local_prefs,
170 params,
171 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700172 fetcher,
173 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700174 OmahaRequestActionTestProcessorDelegate delegate;
175 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700176 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700177
Darin Petkov6a5b3222010-07-13 14:55:28 -0700178 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700179 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700180 processor.EnqueueAction(&action);
181
182 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700183 BondActions(&action, &collector_action);
184 processor.EnqueueAction(&collector_action);
185
186 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
187 g_main_loop_run(loop);
188 g_main_loop_unref(loop);
189 if (collector_action.has_input_object_ && out_response)
190 *out_response = collector_action.omaha_response_;
191 if (out_post_data)
192 *out_post_data = fetcher->post_data();
193 return collector_action.has_input_object_;
194}
195
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700196// Tests Event requests -- they should always succeed. |out_post_data|
197// may be null; if non-null, the post-data received by the mock
198// HttpFetcher is returned.
199void TestEvent(const OmahaRequestParams& params,
200 OmahaEvent* event,
201 const string& http_response,
202 vector<char>* out_post_data) {
203 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
204 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800205 http_response.size(),
206 NULL);
Darin Petkov9c096d62010-11-17 14:49:04 -0800207 NiceMock<PrefsMock> prefs;
Thieu Le116fda32011-04-19 11:01:54 -0700208 OmahaRequestAction action(&prefs, params, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700209 OmahaRequestActionTestProcessorDelegate delegate;
210 delegate.loop_ = loop;
211 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700212 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700213 processor.EnqueueAction(&action);
214
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700215 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
216 g_main_loop_run(loop);
217 g_main_loop_unref(loop);
218 if (out_post_data)
219 *out_post_data = fetcher->post_data();
220}
221
Darin Petkov6a5b3222010-07-13 14:55:28 -0700222TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700223 OmahaResponse response;
224 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700225 TestUpdateCheck(NULL, // prefs
226 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700227 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700228 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700229 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700230 &response,
231 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700232 EXPECT_FALSE(response.update_exists);
233}
234
235TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700236 OmahaResponse response;
237 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700238 TestUpdateCheck(NULL, // prefs
239 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700240 GetUpdateResponse(OmahaRequestParams::kAppId,
241 "1.2.3.4", // version
242 "http://more/info",
243 "true", // prompt
244 "http://code/base", // dl url
245 "HASH1234=", // checksum
246 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700247 "123", // size
248 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700249 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700250 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700251 &response,
252 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700253 EXPECT_TRUE(response.update_exists);
254 EXPECT_EQ("1.2.3.4", response.display_version);
255 EXPECT_EQ("http://code/base", response.codebase);
256 EXPECT_EQ("http://more/info", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700257 EXPECT_TRUE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700258 EXPECT_EQ("HASH1234=", response.hash);
259 EXPECT_EQ(123, response.size);
260 EXPECT_FALSE(response.needs_admin);
261 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700262 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700263}
264
265TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700266 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
267
268 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
269
Darin Petkov9c096d62010-11-17 14:49:04 -0800270 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700271 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700272 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800273 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700274 NULL),
275 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700276 OmahaRequestActionTestProcessorDelegate delegate;
277 delegate.loop_ = loop;
278 ActionProcessor processor;
279 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700280 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700281
282 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
283 g_main_loop_run(loop);
284 g_main_loop_unref(loop);
285 EXPECT_FALSE(processor.IsRunning());
286}
287
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700288TEST(OmahaRequestActionTest, SkipTest) {
289 const string http_response("invalid xml>");
290
291 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
292
293 NiceMock<PrefsMock> prefs;
294 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
295 http_response.size(),
296 NULL);
297 fetcher->set_never_use(true);
298 OmahaRequestAction action(&prefs, kDefaultTestParams,
299 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Thieu Le116fda32011-04-19 11:01:54 -0700300 fetcher, // Passes fetcher ownership
301 false);
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700302 action.set_should_skip(true);
303 OmahaRequestActionTestProcessorDelegate delegate;
304 delegate.loop_ = loop;
305 ActionProcessor processor;
306 processor.set_delegate(&delegate);
307 processor.EnqueueAction(&action);
308
309 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
310 g_main_loop_run(loop);
311 g_main_loop_unref(loop);
312 EXPECT_FALSE(processor.IsRunning());
313}
314
Darin Petkov6a5b3222010-07-13 14:55:28 -0700315TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700316 OmahaResponse response;
317 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700318 TestUpdateCheck(NULL, // prefs
319 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700320 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700321 -1,
322 kActionCodeOmahaRequestXMLParseError,
323 &response,
324 NULL));
325 EXPECT_FALSE(response.update_exists);
326}
327
328TEST(OmahaRequestActionTest, EmptyResponseTest) {
329 OmahaResponse response;
330 ASSERT_FALSE(
331 TestUpdateCheck(NULL, // prefs
332 kDefaultTestParams,
333 "",
334 -1,
335 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700336 &response,
337 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700338 EXPECT_FALSE(response.update_exists);
339}
340
341TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700342 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700343 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700344 NULL, // prefs
345 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700346 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
347 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
348 "appid=\"foo\" status=\"ok\"><ping "
349 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700350 -1,
351 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700352 &response,
353 NULL));
354 EXPECT_FALSE(response.update_exists);
355}
356
357TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700358 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700359 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700360 NULL, // prefs
361 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700362 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
363 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
364 "appid=\"foo\" status=\"ok\"><ping "
365 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700366 -1,
367 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700368 &response,
369 NULL));
370 EXPECT_FALSE(response.update_exists);
371}
372
373TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700374 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700375 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700376 NULL, // prefs
377 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700378 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
379 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
380 "appid=\"foo\" status=\"ok\"><ping "
381 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700382 -1,
383 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700384 &response,
385 NULL));
386 EXPECT_FALSE(response.update_exists);
387}
388
389TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700390 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700391 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
392 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700393 string("<?xml version=\"1.0\" "
394 "encoding=\"UTF-8\"?><gupdate "
395 "xmlns=\"http://www.google.com/"
396 "update2/response\" "
397 "protocol=\"2.0\"><app appid=\"") +
398 OmahaRequestParams::kAppId
399 + "\" status=\"ok\"><ping "
400 "status=\"ok\"/><updatecheck "
401 "DisplayVersion=\"1.2.3.4\" "
402 "Prompt=\"false\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700403 "codebase=\"http://code/base\" hash=\"foo\" "
404 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700405 "size=\"123\" "
406 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700407 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700408 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700409 &response,
410 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700411 EXPECT_TRUE(response.update_exists);
412 EXPECT_EQ("1.2.3.4", response.display_version);
413 EXPECT_EQ("http://code/base", response.codebase);
414 EXPECT_EQ("", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700415 EXPECT_FALSE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700416 EXPECT_EQ("HASH1234=", response.hash);
417 EXPECT_EQ(123, response.size);
418 EXPECT_TRUE(response.needs_admin);
419 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700420 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700421}
422
423namespace {
424class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
425 public:
426 void ProcessingStopped(const ActionProcessor* processor) {
427 ASSERT_TRUE(loop_);
428 g_main_loop_quit(loop_);
429 }
430 GMainLoop *loop_;
431};
432
433gboolean TerminateTransferTestStarter(gpointer data) {
434 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
435 processor->StartProcessing();
436 CHECK(processor->IsRunning());
437 processor->StopProcessing();
438 return FALSE;
439}
440} // namespace {}
441
442TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700443 string http_response("doesn't matter");
444 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
445
Darin Petkov9c096d62010-11-17 14:49:04 -0800446 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700447 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700448 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800449 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700450 NULL),
451 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700452 TerminateEarlyTestProcessorDelegate delegate;
453 delegate.loop_ = loop;
454 ActionProcessor processor;
455 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700456 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700457
458 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
459 g_main_loop_run(loop);
460 g_main_loop_unref(loop);
461}
462
463TEST(OmahaRequestActionTest, XmlEncodeTest) {
464 EXPECT_EQ("ab", XmlEncode("ab"));
465 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
466 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
467 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
468 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
469
470 vector<char> post_data;
471
472 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700473 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700474 OmahaRequestParams::kOsVersion,
475 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700476 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700477 OmahaRequestParams::kAppId,
478 "0.1.0.0",
479 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700480 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700481 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700482 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700483 "http://url");
484 OmahaResponse response;
485 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700486 TestUpdateCheck(NULL, // prefs
487 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700488 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700489 -1,
490 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700491 &response,
492 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700493 // convert post_data to string
494 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700495 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
496 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700497 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
498 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
499 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
500 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700501 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
502 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700503}
504
505TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700506 OmahaResponse response;
507 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700508 TestUpdateCheck(NULL, // prefs
509 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700510 GetUpdateResponse(OmahaRequestParams::kAppId,
511 "1.2.3.4", // version
512 "testthe&lt;url", // more info
513 "true", // prompt
514 "testthe&amp;codebase", // dl url
515 "HASH1234=", // checksum
516 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700517 "123", // size
518 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700519 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700520 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700521 &response,
522 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700523
524 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700525 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700526 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700527}
528
529TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700530 OmahaResponse response;
531 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700532 TestUpdateCheck(NULL, // prefs
533 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700534 GetUpdateResponse(OmahaRequestParams::kAppId,
535 "1.2.3.4", // version
536 "theurl", // more info
537 "true", // prompt
538 "thecodebase", // dl url
539 "HASH1234=", // checksum
540 "false", // needs admin
541 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700542 "123123123123123", // size
543 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700544 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700545 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700546 &response,
547 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700548
549 EXPECT_EQ(response.size, 123123123123123ll);
550}
551
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700552TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
553 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800554 NiceMock<PrefsMock> prefs;
555 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
556 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
557 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
558 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700559 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700560 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700561 -1,
562 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700563 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700564 &post_data));
565 // convert post_data to string
566 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700567 EXPECT_NE(post_str.find(
568 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
569 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700570 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700571 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
572 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700573 EXPECT_EQ(post_str.find("o:event"), string::npos);
574}
575
Darin Petkov95508da2011-01-05 12:42:29 -0800576TEST(OmahaRequestActionTest, FormatUpdateCheckPrevVersionOutputTest) {
577 vector<char> post_data;
578 NiceMock<PrefsMock> prefs;
579 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800580 .WillOnce(DoAll(SetArgumentPointee<1>(string("1.2>3.4")), Return(true)));
Darin Petkov95508da2011-01-05 12:42:29 -0800581 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, ""))
582 .WillOnce(Return(true));
583 ASSERT_FALSE(TestUpdateCheck(&prefs,
584 kDefaultTestParams,
585 "invalid xml>",
586 -1,
587 kActionCodeOmahaRequestXMLParseError,
588 NULL, // response
589 &post_data));
590 // convert post_data to string
591 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700592 EXPECT_NE(post_str.find(
593 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
594 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov95508da2011-01-05 12:42:29 -0800595 string::npos);
596 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
597 string::npos);
598 string prev_version_event = StringPrintf(
599 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800600 "previousversion=\"1.2&gt;3.4\"></o:event>\n",
Darin Petkov95508da2011-01-05 12:42:29 -0800601 OmahaEvent::kTypeUpdateComplete,
602 OmahaEvent::kResultSuccessReboot);
603 EXPECT_NE(post_str.find(prev_version_event), string::npos);
604}
605
Darin Petkove17f86b2010-07-20 09:12:01 -0700606TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
607 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700608 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700609 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
610 "invalid xml>",
611 &post_data);
612 // convert post_data to string
613 string post_str(&post_data[0], post_data.size());
614 string expected_event = StringPrintf(
615 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
616 OmahaEvent::kTypeUpdateDownloadStarted,
617 OmahaEvent::kResultSuccess);
618 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700619 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700620 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
621}
622
623TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
624 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700625 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700626 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
627 OmahaEvent::kResultError,
628 kActionCodeError),
629 "invalid xml>",
630 &post_data);
631 // convert post_data to string
632 string post_str(&post_data[0], post_data.size());
633 string expected_event = StringPrintf(
634 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
635 "errorcode=\"%d\"></o:event>\n",
636 OmahaEvent::kTypeDownloadComplete,
637 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700638 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700639 EXPECT_NE(post_str.find(expected_event), string::npos);
640 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
641}
642
643TEST(OmahaRequestActionTest, IsEventTest) {
644 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800645 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700646 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700647 &prefs,
648 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700649 NULL,
650 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800651 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700652 NULL),
653 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700654 EXPECT_FALSE(update_check_action.IsEvent());
655
656 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700657 &prefs,
658 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700659 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700660 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800661 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700662 NULL),
663 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700664 EXPECT_TRUE(event_action.IsEvent());
665}
666
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700667TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
668 for (int i = 0; i < 2; i++) {
669 bool delta_okay = i == 1;
670 const char* delta_okay_str = delta_okay ? "true" : "false";
671 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700672 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700673 OmahaRequestParams::kOsVersion,
674 "service_pack",
675 "x86-generic",
676 OmahaRequestParams::kAppId,
677 "0.1.0.0",
678 "en-US",
679 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700680 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700681 delta_okay,
682 "http://url");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700683 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
684 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700685 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700686 -1,
687 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700688 NULL,
689 &post_data));
690 // convert post_data to string
691 string post_str(&post_data[0], post_data.size());
692 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
693 string::npos)
694 << "i = " << i;
695 }
696}
697
Darin Petkove17f86b2010-07-20 09:12:01 -0700698TEST(OmahaRequestActionTest, OmahaEventTest) {
699 OmahaEvent default_event;
700 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
701 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
702 EXPECT_EQ(kActionCodeError, default_event.error_code);
703
704 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
705 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
706 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
707 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
708
709 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
710 OmahaEvent::kResultError,
711 kActionCodeError);
712 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
713 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
714 EXPECT_EQ(kActionCodeError, error_event.error_code);
715}
716
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700717TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800718 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700719 // Add a few hours to the day difference to test no rounding, etc.
720 int64_t five_days_ago =
721 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
722 int64_t six_days_ago =
723 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
724 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
725 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
726 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
727 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
728 vector<char> post_data;
729 ASSERT_TRUE(
730 TestUpdateCheck(&prefs,
731 kDefaultTestParams,
732 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700733 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700734 kActionCodeSuccess,
735 NULL,
736 &post_data));
737 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700738 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"),
739 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700740}
741
742TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800743 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700744 int64_t three_days_ago =
745 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
746 int64_t now = Time::Now().ToInternalValue();
747 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
748 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
749 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
750 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
751 vector<char> post_data;
752 ASSERT_TRUE(
753 TestUpdateCheck(&prefs,
754 kDefaultTestParams,
755 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700756 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700757 kActionCodeSuccess,
758 NULL,
759 &post_data));
760 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700761 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"3\"></o:ping>"),
762 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700763}
764
765TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800766 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700767 int64_t four_days_ago =
768 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
769 int64_t now = Time::Now().ToInternalValue();
770 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
771 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
772 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
773 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
774 vector<char> post_data;
775 ASSERT_TRUE(
776 TestUpdateCheck(&prefs,
777 kDefaultTestParams,
778 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700779 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700780 kActionCodeSuccess,
781 NULL,
782 &post_data));
783 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700784 EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"),
785 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700786}
787
788TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800789 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700790 int64_t one_hour_ago =
791 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
792 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
793 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
794 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
795 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
796 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
797 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
798 vector<char> post_data;
799 ASSERT_TRUE(
800 TestUpdateCheck(&prefs,
801 kDefaultTestParams,
802 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700803 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700804 kActionCodeSuccess,
805 NULL,
806 &post_data));
807 string post_str(&post_data[0], post_data.size());
808 EXPECT_EQ(post_str.find("o:ping"), string::npos);
809}
810
811TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800812 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700813 int64_t future =
814 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
815 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
816 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
817 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
818 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
819 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
820 .WillOnce(Return(true));
821 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
822 .WillOnce(Return(true));
823 vector<char> post_data;
824 ASSERT_TRUE(
825 TestUpdateCheck(&prefs,
826 kDefaultTestParams,
827 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
828 "xmlns=\"http://www.google.com/update2/response\" "
829 "protocol=\"2.0\"><daystart elapsed_seconds=\"100\"/>"
830 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
831 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700832 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700833 kActionCodeSuccess,
834 NULL,
835 &post_data));
836 string post_str(&post_data[0], post_data.size());
837 EXPECT_EQ(post_str.find("o:ping"), string::npos);
838}
839
840TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
841 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700842 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700843 // may fail if it runs for longer than 5 seconds. It shouldn't run
844 // that long though.
845 int64_t midnight =
846 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
847 int64_t midnight_slack =
848 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800849 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700850 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
851 AllOf(Ge(midnight), Le(midnight_slack))))
852 .WillOnce(Return(true));
853 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
854 AllOf(Ge(midnight), Le(midnight_slack))))
855 .WillOnce(Return(true));
856 ASSERT_TRUE(
857 TestUpdateCheck(&prefs,
858 kDefaultTestParams,
859 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
860 "xmlns=\"http://www.google.com/update2/response\" "
861 "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>"
862 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
863 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700864 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700865 kActionCodeSuccess,
866 NULL,
867 NULL));
868}
869
870TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800871 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700872 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
873 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
874 ASSERT_TRUE(
875 TestUpdateCheck(&prefs,
876 kDefaultTestParams,
877 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
878 "xmlns=\"http://www.google.com/update2/response\" "
879 "protocol=\"2.0\"><daystart blah=\"200\"/>"
880 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
881 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700882 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700883 kActionCodeSuccess,
884 NULL,
885 NULL));
886}
887
888TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800889 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700890 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
891 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
892 ASSERT_TRUE(
893 TestUpdateCheck(&prefs,
894 kDefaultTestParams,
895 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
896 "xmlns=\"http://www.google.com/update2/response\" "
897 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
898 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
899 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700900 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700901 kActionCodeSuccess,
902 NULL,
903 NULL));
904}
905
Darin Petkov84c763c2010-07-29 16:27:58 -0700906TEST(OmahaRequestActionTest, NoUniqueIDTest) {
907 vector<char> post_data;
908 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
909 kDefaultTestParams,
910 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700911 -1,
912 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700913 NULL, // response
914 &post_data));
915 // convert post_data to string
916 string post_str(&post_data[0], post_data.size());
917 EXPECT_EQ(post_str.find("machineid="), string::npos);
918 EXPECT_EQ(post_str.find("userid="), string::npos);
919}
920
Darin Petkovedc522e2010-11-05 09:35:17 -0700921TEST(OmahaRequestActionTest, NetworkFailureTest) {
922 OmahaResponse response;
923 ASSERT_FALSE(
924 TestUpdateCheck(NULL, // prefs
925 kDefaultTestParams,
926 "",
927 501,
928 static_cast<ActionExitCode>(
929 kActionCodeOmahaRequestHTTPResponseBase + 501),
930 &response,
931 NULL));
932 EXPECT_FALSE(response.update_exists);
933}
934
935TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
936 OmahaResponse response;
937 ASSERT_FALSE(
938 TestUpdateCheck(NULL, // prefs
939 kDefaultTestParams,
940 "",
941 1500,
942 static_cast<ActionExitCode>(
943 kActionCodeOmahaRequestHTTPResponseBase + 999),
944 &response,
945 NULL));
946 EXPECT_FALSE(response.update_exists);
947}
948
Darin Petkov6a5b3222010-07-13 14:55:28 -0700949} // namespace chromeos_update_engine