blob: 776e5e7aed034fc011e063a71db6e6c83cfecb49 [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);
260 EXPECT_EQ("HASH1234=", response.hash);
261 EXPECT_EQ(123, response.size);
262 EXPECT_FALSE(response.needs_admin);
263 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700264 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700265}
266
267TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700268 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
269
270 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
271
Darin Petkov9c096d62010-11-17 14:49:04 -0800272 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700273 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700274 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800275 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700276 NULL),
277 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700278 OmahaRequestActionTestProcessorDelegate delegate;
279 delegate.loop_ = loop;
280 ActionProcessor processor;
281 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700282 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700283
284 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
285 g_main_loop_run(loop);
286 g_main_loop_unref(loop);
287 EXPECT_FALSE(processor.IsRunning());
288}
289
290TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700291 OmahaResponse response;
292 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700293 TestUpdateCheck(NULL, // prefs
294 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700295 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700296 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700297 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700298 kActionCodeOmahaRequestXMLParseError,
299 &response,
300 NULL));
301 EXPECT_FALSE(response.update_exists);
302}
303
304TEST(OmahaRequestActionTest, EmptyResponseTest) {
305 OmahaResponse response;
306 ASSERT_FALSE(
307 TestUpdateCheck(NULL, // prefs
308 kDefaultTestParams,
309 "",
310 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700311 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700312 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700313 &response,
314 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700315 EXPECT_FALSE(response.update_exists);
316}
317
318TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700319 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700320 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700321 NULL, // prefs
322 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700323 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
324 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
325 "appid=\"foo\" status=\"ok\"><ping "
326 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700327 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700328 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700329 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700330 &response,
331 NULL));
332 EXPECT_FALSE(response.update_exists);
333}
334
335TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700336 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700337 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700338 NULL, // prefs
339 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700340 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
341 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
342 "appid=\"foo\" status=\"ok\"><ping "
343 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700344 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700345 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700346 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700347 &response,
348 NULL));
349 EXPECT_FALSE(response.update_exists);
350}
351
352TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700353 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700354 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700355 NULL, // prefs
356 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700357 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
358 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
359 "appid=\"foo\" status=\"ok\"><ping "
360 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700361 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700362 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700363 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700364 &response,
365 NULL));
366 EXPECT_FALSE(response.update_exists);
367}
368
369TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700370 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700371 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
372 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700373 string("<?xml version=\"1.0\" "
374 "encoding=\"UTF-8\"?><gupdate "
375 "xmlns=\"http://www.google.com/"
376 "update2/response\" "
377 "protocol=\"2.0\"><app appid=\"") +
378 OmahaRequestParams::kAppId
379 + "\" status=\"ok\"><ping "
380 "status=\"ok\"/><updatecheck "
381 "DisplayVersion=\"1.2.3.4\" "
382 "Prompt=\"false\" "
Darin Petkov7ed561b2011-10-04 02:59:03 -0700383 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700384 "codebase=\"http://code/base\" hash=\"foo\" "
385 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700386 "size=\"123\" "
387 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700388 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700389 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700390 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700391 &response,
392 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700393 EXPECT_TRUE(response.update_exists);
394 EXPECT_EQ("1.2.3.4", response.display_version);
395 EXPECT_EQ("http://code/base", response.codebase);
396 EXPECT_EQ("", response.more_info_url);
397 EXPECT_EQ("HASH1234=", response.hash);
398 EXPECT_EQ(123, response.size);
399 EXPECT_TRUE(response.needs_admin);
400 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700401 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700402}
403
404namespace {
405class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
406 public:
407 void ProcessingStopped(const ActionProcessor* processor) {
408 ASSERT_TRUE(loop_);
409 g_main_loop_quit(loop_);
410 }
411 GMainLoop *loop_;
412};
413
414gboolean TerminateTransferTestStarter(gpointer data) {
415 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
416 processor->StartProcessing();
417 CHECK(processor->IsRunning());
418 processor->StopProcessing();
419 return FALSE;
420}
421} // namespace {}
422
423TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700424 string http_response("doesn't matter");
425 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
426
Darin Petkov9c096d62010-11-17 14:49:04 -0800427 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700428 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700429 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800430 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700431 NULL),
432 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700433 TerminateEarlyTestProcessorDelegate delegate;
434 delegate.loop_ = loop;
435 ActionProcessor processor;
436 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700437 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700438
439 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
440 g_main_loop_run(loop);
441 g_main_loop_unref(loop);
442}
443
444TEST(OmahaRequestActionTest, XmlEncodeTest) {
445 EXPECT_EQ("ab", XmlEncode("ab"));
446 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
447 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
448 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
449 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
450
451 vector<char> post_data;
452
453 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700454 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700455 OmahaRequestParams::kOsVersion,
456 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700457 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700458 OmahaRequestParams::kAppId,
459 "0.1.0.0",
460 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700461 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700462 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700463 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700464 "http://url");
465 OmahaResponse response;
466 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700467 TestUpdateCheck(NULL, // prefs
468 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700469 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700470 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700471 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700472 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700473 &response,
474 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700475 // convert post_data to string
476 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700477 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
478 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700479 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
480 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
481 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
482 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700483 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
484 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700485}
486
487TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700488 OmahaResponse response;
489 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700490 TestUpdateCheck(NULL, // prefs
491 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700492 GetUpdateResponse(OmahaRequestParams::kAppId,
493 "1.2.3.4", // version
494 "testthe&lt;url", // more info
495 "true", // prompt
496 "testthe&amp;codebase", // dl url
497 "HASH1234=", // checksum
498 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700499 "123", // size
500 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700501 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700502 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700503 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700504 &response,
505 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700506
507 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700508 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700509 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700510}
511
512TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700513 OmahaResponse response;
514 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700515 TestUpdateCheck(NULL, // prefs
516 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700517 GetUpdateResponse(OmahaRequestParams::kAppId,
518 "1.2.3.4", // version
519 "theurl", // more info
520 "true", // prompt
521 "thecodebase", // dl url
522 "HASH1234=", // checksum
523 "false", // needs admin
524 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700525 "123123123123123", // size
526 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700527 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700528 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700529 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700530 &response,
531 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700532
533 EXPECT_EQ(response.size, 123123123123123ll);
534}
535
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700536TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
537 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800538 NiceMock<PrefsMock> prefs;
539 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
540 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
541 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
542 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700543 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700544 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700545 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700546 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700547 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700548 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700549 &post_data));
550 // convert post_data to string
551 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700552 EXPECT_NE(post_str.find(
553 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
554 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700555 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700556 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
557 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700558 EXPECT_EQ(post_str.find("o:event"), string::npos);
559}
560
Darin Petkov95508da2011-01-05 12:42:29 -0800561TEST(OmahaRequestActionTest, FormatUpdateCheckPrevVersionOutputTest) {
562 vector<char> post_data;
563 NiceMock<PrefsMock> prefs;
564 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800565 .WillOnce(DoAll(SetArgumentPointee<1>(string("1.2>3.4")), Return(true)));
Darin Petkov95508da2011-01-05 12:42:29 -0800566 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, ""))
567 .WillOnce(Return(true));
568 ASSERT_FALSE(TestUpdateCheck(&prefs,
569 kDefaultTestParams,
570 "invalid xml>",
571 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700572 false, // ping_only
Darin Petkov95508da2011-01-05 12:42:29 -0800573 kActionCodeOmahaRequestXMLParseError,
574 NULL, // response
575 &post_data));
576 // convert post_data to string
577 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700578 EXPECT_NE(post_str.find(
579 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
580 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov95508da2011-01-05 12:42:29 -0800581 string::npos);
582 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
583 string::npos);
584 string prev_version_event = StringPrintf(
585 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800586 "previousversion=\"1.2&gt;3.4\"></o:event>\n",
Darin Petkov95508da2011-01-05 12:42:29 -0800587 OmahaEvent::kTypeUpdateComplete,
588 OmahaEvent::kResultSuccessReboot);
589 EXPECT_NE(post_str.find(prev_version_event), string::npos);
590}
591
Darin Petkove17f86b2010-07-20 09:12:01 -0700592TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
593 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700594 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700595 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
596 "invalid xml>",
597 &post_data);
598 // convert post_data to string
599 string post_str(&post_data[0], post_data.size());
600 string expected_event = StringPrintf(
601 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
602 OmahaEvent::kTypeUpdateDownloadStarted,
603 OmahaEvent::kResultSuccess);
604 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700605 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700606 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
607}
608
609TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
610 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700611 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700612 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
613 OmahaEvent::kResultError,
614 kActionCodeError),
615 "invalid xml>",
616 &post_data);
617 // convert post_data to string
618 string post_str(&post_data[0], post_data.size());
619 string expected_event = StringPrintf(
620 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
621 "errorcode=\"%d\"></o:event>\n",
622 OmahaEvent::kTypeDownloadComplete,
623 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700624 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700625 EXPECT_NE(post_str.find(expected_event), string::npos);
626 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
627}
628
629TEST(OmahaRequestActionTest, IsEventTest) {
630 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800631 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700632 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700633 &prefs,
634 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700635 NULL,
636 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800637 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700638 NULL),
639 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700640 EXPECT_FALSE(update_check_action.IsEvent());
641
642 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700643 &prefs,
644 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700645 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700646 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800647 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700648 NULL),
649 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700650 EXPECT_TRUE(event_action.IsEvent());
651}
652
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700653TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
654 for (int i = 0; i < 2; i++) {
655 bool delta_okay = i == 1;
656 const char* delta_okay_str = delta_okay ? "true" : "false";
657 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700658 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700659 OmahaRequestParams::kOsVersion,
660 "service_pack",
661 "x86-generic",
662 OmahaRequestParams::kAppId,
663 "0.1.0.0",
664 "en-US",
665 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700666 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700667 delta_okay,
668 "http://url");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700669 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
670 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700671 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700672 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700673 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700674 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700675 NULL,
676 &post_data));
677 // convert post_data to string
678 string post_str(&post_data[0], post_data.size());
679 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
680 string::npos)
681 << "i = " << i;
682 }
683}
684
Darin Petkove17f86b2010-07-20 09:12:01 -0700685TEST(OmahaRequestActionTest, OmahaEventTest) {
686 OmahaEvent default_event;
687 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
688 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
689 EXPECT_EQ(kActionCodeError, default_event.error_code);
690
691 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
692 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
693 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
694 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
695
696 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
697 OmahaEvent::kResultError,
698 kActionCodeError);
699 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
700 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
701 EXPECT_EQ(kActionCodeError, error_event.error_code);
702}
703
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700704TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -0700705 for (int ping_only = 0; ping_only < 2; ping_only++) {
706 NiceMock<PrefsMock> prefs;
707 // Add a few hours to the day difference to test no rounding, etc.
708 int64_t five_days_ago =
709 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
710 int64_t six_days_ago =
711 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
712 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
713 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
714 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
715 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
716 vector<char> post_data;
717 ASSERT_TRUE(
718 TestUpdateCheck(&prefs,
719 kDefaultTestParams,
720 GetNoUpdateResponse(OmahaRequestParams::kAppId),
721 -1,
722 ping_only,
723 kActionCodeSuccess,
724 NULL,
725 &post_data));
726 string post_str(&post_data[0], post_data.size());
727 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"),
728 string::npos);
729 if (ping_only) {
730 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
731 EXPECT_EQ(post_str.find("previousversion"), string::npos);
732 } else {
733 EXPECT_NE(post_str.find("o:updatecheck"), string::npos);
734 EXPECT_NE(post_str.find("previousversion"), string::npos);
735 }
736 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700737}
738
739TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800740 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700741 int64_t three_days_ago =
742 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
743 int64_t now = Time::Now().ToInternalValue();
744 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
745 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
746 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
747 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
748 vector<char> post_data;
749 ASSERT_TRUE(
750 TestUpdateCheck(&prefs,
751 kDefaultTestParams,
752 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700753 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700754 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700755 kActionCodeSuccess,
756 NULL,
757 &post_data));
758 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700759 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"3\"></o:ping>"),
760 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700761}
762
763TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800764 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700765 int64_t four_days_ago =
766 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
767 int64_t now = Time::Now().ToInternalValue();
768 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
769 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
770 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
771 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
772 vector<char> post_data;
773 ASSERT_TRUE(
774 TestUpdateCheck(&prefs,
775 kDefaultTestParams,
776 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700777 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700778 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700779 kActionCodeSuccess,
780 NULL,
781 &post_data));
782 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700783 EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"),
784 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700785}
786
787TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800788 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700789 int64_t one_hour_ago =
790 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
791 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
792 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
793 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
794 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
795 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
796 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
797 vector<char> post_data;
798 ASSERT_TRUE(
799 TestUpdateCheck(&prefs,
800 kDefaultTestParams,
801 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700802 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700803 false, // ping_only
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
Thieu Leb44e9e82011-06-06 14:34:04 -0700811TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
812 // This test ensures that we ignore empty ping only requests.
813 NiceMock<PrefsMock> prefs;
814 int64_t now = Time::Now().ToInternalValue();
815 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
816 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
817 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
818 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
819 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
820 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
821 vector<char> post_data;
822 EXPECT_TRUE(
823 TestUpdateCheck(&prefs,
824 kDefaultTestParams,
825 GetNoUpdateResponse(OmahaRequestParams::kAppId),
826 -1,
827 true, // ping_only
828 kActionCodeSuccess,
829 NULL,
830 &post_data));
831 EXPECT_EQ(post_data.size(), 0);
832}
833
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700834TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800835 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700836 int64_t future =
837 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
838 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
839 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
840 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
841 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
842 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
843 .WillOnce(Return(true));
844 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
845 .WillOnce(Return(true));
846 vector<char> post_data;
847 ASSERT_TRUE(
848 TestUpdateCheck(&prefs,
849 kDefaultTestParams,
850 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
851 "xmlns=\"http://www.google.com/update2/response\" "
852 "protocol=\"2.0\"><daystart elapsed_seconds=\"100\"/>"
853 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
854 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700855 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700856 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700857 kActionCodeSuccess,
858 NULL,
859 &post_data));
860 string post_str(&post_data[0], post_data.size());
861 EXPECT_EQ(post_str.find("o:ping"), string::npos);
862}
863
864TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
865 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700866 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700867 // may fail if it runs for longer than 5 seconds. It shouldn't run
868 // that long though.
869 int64_t midnight =
870 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
871 int64_t midnight_slack =
872 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800873 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700874 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
875 AllOf(Ge(midnight), Le(midnight_slack))))
876 .WillOnce(Return(true));
877 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
878 AllOf(Ge(midnight), Le(midnight_slack))))
879 .WillOnce(Return(true));
880 ASSERT_TRUE(
881 TestUpdateCheck(&prefs,
882 kDefaultTestParams,
883 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
884 "xmlns=\"http://www.google.com/update2/response\" "
885 "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>"
886 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
887 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700888 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700889 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700890 kActionCodeSuccess,
891 NULL,
892 NULL));
893}
894
895TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800896 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700897 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
898 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
899 ASSERT_TRUE(
900 TestUpdateCheck(&prefs,
901 kDefaultTestParams,
902 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
903 "xmlns=\"http://www.google.com/update2/response\" "
904 "protocol=\"2.0\"><daystart blah=\"200\"/>"
905 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
906 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700907 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700908 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700909 kActionCodeSuccess,
910 NULL,
911 NULL));
912}
913
914TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800915 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700916 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
917 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
918 ASSERT_TRUE(
919 TestUpdateCheck(&prefs,
920 kDefaultTestParams,
921 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
922 "xmlns=\"http://www.google.com/update2/response\" "
923 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
924 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
925 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700926 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700927 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700928 kActionCodeSuccess,
929 NULL,
930 NULL));
931}
932
Darin Petkov84c763c2010-07-29 16:27:58 -0700933TEST(OmahaRequestActionTest, NoUniqueIDTest) {
934 vector<char> post_data;
935 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
936 kDefaultTestParams,
937 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700938 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700939 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700940 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700941 NULL, // response
942 &post_data));
943 // convert post_data to string
944 string post_str(&post_data[0], post_data.size());
945 EXPECT_EQ(post_str.find("machineid="), string::npos);
946 EXPECT_EQ(post_str.find("userid="), string::npos);
947}
948
Darin Petkovedc522e2010-11-05 09:35:17 -0700949TEST(OmahaRequestActionTest, NetworkFailureTest) {
950 OmahaResponse response;
951 ASSERT_FALSE(
952 TestUpdateCheck(NULL, // prefs
953 kDefaultTestParams,
954 "",
955 501,
Darin Petkov265f2902011-05-09 15:17:40 -0700956 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700957 static_cast<ActionExitCode>(
958 kActionCodeOmahaRequestHTTPResponseBase + 501),
959 &response,
960 NULL));
961 EXPECT_FALSE(response.update_exists);
962}
963
964TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
965 OmahaResponse response;
966 ASSERT_FALSE(
967 TestUpdateCheck(NULL, // prefs
968 kDefaultTestParams,
969 "",
970 1500,
Darin Petkov265f2902011-05-09 15:17:40 -0700971 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700972 static_cast<ActionExitCode>(
973 kActionCodeOmahaRequestHTTPResponseBase + 999),
974 &response,
975 NULL));
976 EXPECT_FALSE(response.update_exists);
977}
978
Darin Petkov6a5b3222010-07-13 14:55:28 -0700979} // namespace chromeos_update_engine