blob: 26e808cc9caa4683dd1f4fef102a2ee418c59b73 [file] [log] [blame]
Darin Petkov265f2902011-05-09 15:17:40 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkov6a5b3222010-07-13 14:55:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07007
Darin Petkov6a5b3222010-07-13 14:55:28 -07008#include <glib.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07009
10#include "base/string_util.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070011#include "base/time.h"
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070012#include "gtest/gtest.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070013#include "update_engine/action_pipe.h"
14#include "update_engine/mock_http_fetcher.h"
15#include "update_engine/omaha_hash_calculator.h"
16#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070017#include "update_engine/omaha_request_params.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070018#include "update_engine/prefs_mock.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070019#include "update_engine/test_utils.h"
20
Darin Petkov1cbd78f2010-07-29 12:38:34 -070021using base::Time;
22using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070023using std::string;
24using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070025using testing::_;
26using testing::AllOf;
27using testing::Ge;
28using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080029using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070030using testing::Return;
31using testing::SetArgumentPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070032
33namespace chromeos_update_engine {
34
35class OmahaRequestActionTest : public ::testing::Test { };
36
37namespace {
Darin Petkov1cbd78f2010-07-29 12:38:34 -070038const OmahaRequestParams kDefaultTestParams(
Darin Petkov1cbd78f2010-07-29 12:38:34 -070039 OmahaRequestParams::kOsPlatform,
40 OmahaRequestParams::kOsVersion,
41 "service_pack",
42 "x86-generic",
43 OmahaRequestParams::kAppId,
44 "0.1.0.0",
45 "en-US",
46 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070047 "OEM MODEL 09235 7471",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070048 false, // delta okay
Jay Srinivasan0a708742012-03-20 11:26:12 -070049 "http://url",
50 false, // update_disabled
51 ""); // target_version_prefix
Darin Petkov1cbd78f2010-07-29 12:38:34 -070052
Darin Petkov6a5b3222010-07-13 14:55:28 -070053string GetNoUpdateResponse(const string& app_id) {
54 return string(
55 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
56 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
57 "appid=\"") + app_id + "\" status=\"ok\"><ping "
58 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
59}
60
61string GetUpdateResponse(const string& app_id,
62 const string& display_version,
63 const string& more_info_url,
64 const string& prompt,
65 const string& codebase,
66 const string& hash,
67 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -070068 const string& size,
69 const string& deadline) {
Jay Srinivasan0a708742012-03-20 11:26:12 -070070 return string(
71 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
72 "xmlns=\"http://www.google.com/update2/response\" "
73 "protocol=\"2.0\"><app "
74 "appid=\"") + app_id + "\" status=\"ok\"><ping "
Darin Petkov6a5b3222010-07-13 14:55:28 -070075 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
Jay Srinivasan0a708742012-03-20 11:26:12 -070076 "ChromeOSVersion=\"" + display_version + "\" "
Darin Petkov6a5b3222010-07-13 14:55:28 -070077 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070078 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -070079 "codebase=\"" + codebase + "\" hash=\"not-applicable\" "
80 "sha256=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
Darin Petkov6c118642010-10-21 12:06:30 -070081 "size=\"" + size + "\" deadline=\"" + deadline +
82 "\" status=\"ok\"/></app></gupdate>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070083}
84
85class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
86 public:
87 OmahaRequestActionTestProcessorDelegate()
88 : loop_(NULL),
Darin Petkovc1a8b422010-07-19 11:34:49 -070089 expected_code_(kActionCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070090 virtual ~OmahaRequestActionTestProcessorDelegate() {
91 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070092 virtual void ProcessingDone(const ActionProcessor* processor,
93 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070094 ASSERT_TRUE(loop_);
95 g_main_loop_quit(loop_);
96 }
97
98 virtual void ActionCompleted(ActionProcessor* processor,
99 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700100 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700101 // make sure actions always succeed
102 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700103 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700104 else
Darin Petkovc1a8b422010-07-19 11:34:49 -0700105 EXPECT_EQ(kActionCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700106 }
107 GMainLoop *loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700108 ActionExitCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700109};
110
111gboolean StartProcessorInRunLoop(gpointer data) {
112 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
113 processor->StartProcessing();
114 return FALSE;
115}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700116} // namespace {}
117
118class OutputObjectCollectorAction;
119
120template<>
121class ActionTraits<OutputObjectCollectorAction> {
122 public:
123 // Does not take an object for input
124 typedef OmahaResponse InputObjectType;
125 // On success, puts the output path on output
126 typedef NoneType OutputObjectType;
127};
128
129class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
130 public:
131 OutputObjectCollectorAction() : has_input_object_(false) {}
132 void PerformAction() {
133 // copy input object
134 has_input_object_ = HasInputObject();
135 if (has_input_object_)
136 omaha_response_ = GetInputObject();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700137 processor_->ActionComplete(this, kActionCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700138 }
139 // Should never be called
140 void TerminateProcessing() {
141 CHECK(false);
142 }
143 // Debugging/logging
144 static std::string StaticType() {
145 return "OutputObjectCollectorAction";
146 }
147 std::string Type() const { return StaticType(); }
148 bool has_input_object_;
149 OmahaResponse omaha_response_;
150};
151
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700152// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700153// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
Darin Petkov265f2902011-05-09 15:17:40 -0700154// used. out_response may be NULL. If |fail_http_response_code| is non-negative,
155// the transfer will fail with that code. |ping_only| is passed through to the
156// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
157// post-data received by the mock HttpFetcher is returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700158bool TestUpdateCheck(PrefsInterface* prefs,
159 const OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700160 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700161 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700162 bool ping_only,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700163 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700164 OmahaResponse* out_response,
165 vector<char>* out_post_data) {
166 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
167 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800168 http_response.size(),
169 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700170 if (fail_http_response_code >= 0) {
171 fetcher->FailTransfer(fail_http_response_code);
172 }
Darin Petkov9c096d62010-11-17 14:49:04 -0800173 NiceMock<PrefsMock> local_prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700174 OmahaRequestAction action(prefs ? prefs : &local_prefs,
175 params,
176 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700177 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700178 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700179 OmahaRequestActionTestProcessorDelegate delegate;
180 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700181 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700182
Darin Petkov6a5b3222010-07-13 14:55:28 -0700183 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700184 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700185 processor.EnqueueAction(&action);
186
187 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700188 BondActions(&action, &collector_action);
189 processor.EnqueueAction(&collector_action);
190
191 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
192 g_main_loop_run(loop);
193 g_main_loop_unref(loop);
194 if (collector_action.has_input_object_ && out_response)
195 *out_response = collector_action.omaha_response_;
196 if (out_post_data)
197 *out_post_data = fetcher->post_data();
198 return collector_action.has_input_object_;
199}
200
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700201// Tests Event requests -- they should always succeed. |out_post_data|
202// may be null; if non-null, the post-data received by the mock
203// HttpFetcher is returned.
204void TestEvent(const OmahaRequestParams& params,
205 OmahaEvent* event,
206 const string& http_response,
207 vector<char>* out_post_data) {
208 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
209 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800210 http_response.size(),
211 NULL);
Darin Petkov9c096d62010-11-17 14:49:04 -0800212 NiceMock<PrefsMock> prefs;
Thieu Le116fda32011-04-19 11:01:54 -0700213 OmahaRequestAction action(&prefs, params, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700214 OmahaRequestActionTestProcessorDelegate delegate;
215 delegate.loop_ = loop;
216 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700217 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700218 processor.EnqueueAction(&action);
219
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700220 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
221 g_main_loop_run(loop);
222 g_main_loop_unref(loop);
223 if (out_post_data)
224 *out_post_data = fetcher->post_data();
225}
226
Darin Petkov6a5b3222010-07-13 14:55:28 -0700227TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700228 OmahaResponse response;
229 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700230 TestUpdateCheck(NULL, // prefs
231 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700232 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700233 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700234 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700235 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700236 &response,
237 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238 EXPECT_FALSE(response.update_exists);
239}
240
241TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700242 OmahaResponse response;
243 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700244 TestUpdateCheck(NULL, // prefs
245 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700246 GetUpdateResponse(OmahaRequestParams::kAppId,
247 "1.2.3.4", // version
248 "http://more/info",
249 "true", // prompt
250 "http://code/base", // dl url
251 "HASH1234=", // checksum
252 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700253 "123", // size
254 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700255 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700256 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700257 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700258 &response,
259 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700260 EXPECT_TRUE(response.update_exists);
261 EXPECT_EQ("1.2.3.4", response.display_version);
262 EXPECT_EQ("http://code/base", response.codebase);
263 EXPECT_EQ("http://more/info", response.more_info_url);
264 EXPECT_EQ("HASH1234=", response.hash);
265 EXPECT_EQ(123, response.size);
266 EXPECT_FALSE(response.needs_admin);
267 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700268 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700269}
270
Jay Srinivasan0a708742012-03-20 11:26:12 -0700271TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) {
272 OmahaResponse response;
273 OmahaRequestParams params = kDefaultTestParams;
274 params.update_disabled = true;
275 ASSERT_FALSE(
276 TestUpdateCheck(NULL, // prefs
277 params,
278 GetUpdateResponse(OmahaRequestParams::kAppId,
279 "1.2.3.4", // version
280 "http://more/info",
281 "true", // prompt
282 "http://code/base", // dl url
283 "HASH1234=", // checksum
284 "false", // needs admin
285 "123", // size
286 "20101020"), // deadline
287 -1,
288 false, // ping_only
289 kActionCodeOmahaUpdateIgnoredPerPolicy,
290 &response,
291 NULL));
292 EXPECT_FALSE(response.update_exists);
293}
294
295
296TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) {
297 OmahaResponse response;
298 OmahaRequestParams params = kDefaultTestParams;
299 params.update_disabled = true;
300 ASSERT_TRUE(
301 TestUpdateCheck(NULL, // prefs
302 params,
303 GetNoUpdateResponse(OmahaRequestParams::kAppId),
304 -1,
305 false, // ping_only
306 kActionCodeSuccess,
307 &response,
308 NULL));
309 EXPECT_FALSE(response.update_exists);
310}
311
312
Darin Petkov6a5b3222010-07-13 14:55:28 -0700313TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700314 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
315
316 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
317
Darin Petkov9c096d62010-11-17 14:49:04 -0800318 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700319 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700320 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800321 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700322 NULL),
323 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700324 OmahaRequestActionTestProcessorDelegate delegate;
325 delegate.loop_ = loop;
326 ActionProcessor processor;
327 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700328 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700329
330 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
331 g_main_loop_run(loop);
332 g_main_loop_unref(loop);
333 EXPECT_FALSE(processor.IsRunning());
334}
335
336TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700337 OmahaResponse response;
338 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700339 TestUpdateCheck(NULL, // prefs
340 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700341 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700342 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700343 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700344 kActionCodeOmahaRequestXMLParseError,
345 &response,
346 NULL));
347 EXPECT_FALSE(response.update_exists);
348}
349
350TEST(OmahaRequestActionTest, EmptyResponseTest) {
351 OmahaResponse response;
352 ASSERT_FALSE(
353 TestUpdateCheck(NULL, // prefs
354 kDefaultTestParams,
355 "",
356 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700357 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700358 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700359 &response,
360 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700361 EXPECT_FALSE(response.update_exists);
362}
363
364TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700365 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700366 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700367 NULL, // prefs
368 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700369 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
370 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
371 "appid=\"foo\" status=\"ok\"><ping "
372 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700373 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700374 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700375 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700376 &response,
377 NULL));
378 EXPECT_FALSE(response.update_exists);
379}
380
381TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700382 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700383 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700384 NULL, // prefs
385 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700386 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
387 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
388 "appid=\"foo\" status=\"ok\"><ping "
389 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700390 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700391 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700392 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700393 &response,
394 NULL));
395 EXPECT_FALSE(response.update_exists);
396}
397
398TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700399 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700400 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700401 NULL, // prefs
402 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700403 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
404 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
405 "appid=\"foo\" status=\"ok\"><ping "
406 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700407 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700408 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700409 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700410 &response,
411 NULL));
412 EXPECT_FALSE(response.update_exists);
413}
414
415TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700416 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700417 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
418 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700419 string("<?xml version=\"1.0\" "
420 "encoding=\"UTF-8\"?><gupdate "
421 "xmlns=\"http://www.google.com/"
422 "update2/response\" "
423 "protocol=\"2.0\"><app appid=\"") +
424 OmahaRequestParams::kAppId
425 + "\" status=\"ok\"><ping "
426 "status=\"ok\"/><updatecheck "
427 "DisplayVersion=\"1.2.3.4\" "
Jay Srinivasan0a708742012-03-20 11:26:12 -0700428 "ChromeOSVersion=\"1.2.3.4\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700429 "Prompt=\"false\" "
Darin Petkov7ed561b2011-10-04 02:59:03 -0700430 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700431 "codebase=\"http://code/base\" hash=\"foo\" "
432 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700433 "size=\"123\" "
434 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700435 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700436 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700437 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700438 &response,
439 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700440 EXPECT_TRUE(response.update_exists);
441 EXPECT_EQ("1.2.3.4", response.display_version);
442 EXPECT_EQ("http://code/base", response.codebase);
443 EXPECT_EQ("", response.more_info_url);
444 EXPECT_EQ("HASH1234=", response.hash);
445 EXPECT_EQ(123, response.size);
446 EXPECT_TRUE(response.needs_admin);
447 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700448 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700449}
450
451namespace {
452class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
453 public:
454 void ProcessingStopped(const ActionProcessor* processor) {
455 ASSERT_TRUE(loop_);
456 g_main_loop_quit(loop_);
457 }
458 GMainLoop *loop_;
459};
460
461gboolean TerminateTransferTestStarter(gpointer data) {
462 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
463 processor->StartProcessing();
464 CHECK(processor->IsRunning());
465 processor->StopProcessing();
466 return FALSE;
467}
468} // namespace {}
469
470TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700471 string http_response("doesn't matter");
472 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
473
Darin Petkov9c096d62010-11-17 14:49:04 -0800474 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700475 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700476 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800477 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700478 NULL),
479 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700480 TerminateEarlyTestProcessorDelegate delegate;
481 delegate.loop_ = loop;
482 ActionProcessor processor;
483 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700484 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700485
486 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
487 g_main_loop_run(loop);
488 g_main_loop_unref(loop);
489}
490
491TEST(OmahaRequestActionTest, XmlEncodeTest) {
492 EXPECT_EQ("ab", XmlEncode("ab"));
493 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
494 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
495 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
496 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
497
498 vector<char> post_data;
499
500 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700501 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700502 OmahaRequestParams::kOsVersion,
503 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700504 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700505 OmahaRequestParams::kAppId,
506 "0.1.0.0",
507 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700508 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700509 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700510 false, // delta okay
Jay Srinivasan0a708742012-03-20 11:26:12 -0700511 "http://url",
512 false, // update_disabled
513 ""); // target_version_prefix
Darin Petkov6a5b3222010-07-13 14:55:28 -0700514 OmahaResponse response;
515 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700516 TestUpdateCheck(NULL, // prefs
517 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700518 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700519 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700520 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700521 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700522 &response,
523 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700524 // convert post_data to string
525 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700526 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
527 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700528 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
529 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
530 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
531 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700532 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
533 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700534}
535
536TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700537 OmahaResponse response;
538 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700539 TestUpdateCheck(NULL, // prefs
540 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700541 GetUpdateResponse(OmahaRequestParams::kAppId,
542 "1.2.3.4", // version
543 "testthe&lt;url", // more info
544 "true", // prompt
545 "testthe&amp;codebase", // dl url
546 "HASH1234=", // checksum
547 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700548 "123", // size
549 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700550 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700551 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700552 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700553 &response,
554 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700555
556 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700557 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700558 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700559}
560
561TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700562 OmahaResponse response;
563 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700564 TestUpdateCheck(NULL, // prefs
565 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700566 GetUpdateResponse(OmahaRequestParams::kAppId,
567 "1.2.3.4", // version
568 "theurl", // more info
569 "true", // prompt
570 "thecodebase", // dl url
571 "HASH1234=", // checksum
572 "false", // needs admin
573 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700574 "123123123123123", // size
575 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700576 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700577 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700578 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700579 &response,
580 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700581
582 EXPECT_EQ(response.size, 123123123123123ll);
583}
584
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700585TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
586 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800587 NiceMock<PrefsMock> prefs;
588 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
589 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
590 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
591 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700592 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700593 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700594 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700595 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700596 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700597 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700598 &post_data));
599 // convert post_data to string
600 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700601 EXPECT_NE(post_str.find(
Jay Srinivasan0a708742012-03-20 11:26:12 -0700602 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
603 " <o:updatecheck"
Jay Srinivasan0a708742012-03-20 11:26:12 -0700604 " targetversionprefix=\"\""
605 "></o:updatecheck>\n"),
606 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700607 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
608 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700609 EXPECT_EQ(post_str.find("o:event"), string::npos);
610}
611
Jay Srinivasan0a708742012-03-20 11:26:12 -0700612
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700613TEST(OmahaRequestActionTest, FormatTargetVersionPrefixTest) {
Darin Petkov95508da2011-01-05 12:42:29 -0800614 vector<char> post_data;
615 NiceMock<PrefsMock> prefs;
616 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Jay Srinivasan0a708742012-03-20 11:26:12 -0700617 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
618 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
619 OmahaRequestParams params = kDefaultTestParams;
620 params.update_disabled = true;
Darin Petkov95508da2011-01-05 12:42:29 -0800621 ASSERT_FALSE(TestUpdateCheck(&prefs,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700622 params,
Darin Petkov95508da2011-01-05 12:42:29 -0800623 "invalid xml>",
624 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700625 false, // ping_only
Darin Petkov95508da2011-01-05 12:42:29 -0800626 kActionCodeOmahaRequestXMLParseError,
627 NULL, // response
628 &post_data));
629 // convert post_data to string
630 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700631 EXPECT_NE(post_str.find(
Jay Srinivasan0a708742012-03-20 11:26:12 -0700632 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
633 " <o:updatecheck"
Jay Srinivasan0a708742012-03-20 11:26:12 -0700634 " targetversionprefix=\"\""
635 "></o:updatecheck>\n"),
636 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -0800637 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
638 string::npos);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700639 EXPECT_EQ(post_str.find("o:event"), string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -0800640}
641
Darin Petkove17f86b2010-07-20 09:12:01 -0700642TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
643 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700644 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700645 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
646 "invalid xml>",
647 &post_data);
648 // convert post_data to string
649 string post_str(&post_data[0], post_data.size());
650 string expected_event = StringPrintf(
651 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
652 OmahaEvent::kTypeUpdateDownloadStarted,
653 OmahaEvent::kResultSuccess);
654 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700655 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700656 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
657}
658
659TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
660 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700661 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700662 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
663 OmahaEvent::kResultError,
664 kActionCodeError),
665 "invalid xml>",
666 &post_data);
667 // convert post_data to string
668 string post_str(&post_data[0], post_data.size());
669 string expected_event = StringPrintf(
670 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
671 "errorcode=\"%d\"></o:event>\n",
672 OmahaEvent::kTypeDownloadComplete,
673 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700674 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700675 EXPECT_NE(post_str.find(expected_event), string::npos);
676 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
677}
678
679TEST(OmahaRequestActionTest, IsEventTest) {
680 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800681 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700682 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700683 &prefs,
684 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700685 NULL,
686 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800687 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700688 NULL),
689 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700690 EXPECT_FALSE(update_check_action.IsEvent());
691
692 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700693 &prefs,
694 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700695 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700696 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800697 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700698 NULL),
699 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700700 EXPECT_TRUE(event_action.IsEvent());
701}
702
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700703TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
704 for (int i = 0; i < 2; i++) {
705 bool delta_okay = i == 1;
706 const char* delta_okay_str = delta_okay ? "true" : "false";
707 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700708 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700709 OmahaRequestParams::kOsVersion,
710 "service_pack",
711 "x86-generic",
712 OmahaRequestParams::kAppId,
713 "0.1.0.0",
714 "en-US",
715 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700716 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700717 delta_okay,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700718 "http://url",
719 false, // update_disabled
720 ""); // target_version_prefix
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700721 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
722 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700723 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700724 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700725 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700726 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700727 NULL,
728 &post_data));
729 // convert post_data to string
730 string post_str(&post_data[0], post_data.size());
731 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
732 string::npos)
733 << "i = " << i;
734 }
735}
736
Darin Petkove17f86b2010-07-20 09:12:01 -0700737TEST(OmahaRequestActionTest, OmahaEventTest) {
738 OmahaEvent default_event;
739 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
740 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
741 EXPECT_EQ(kActionCodeError, default_event.error_code);
742
743 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
744 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
745 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
746 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
747
748 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
749 OmahaEvent::kResultError,
750 kActionCodeError);
751 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
752 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
753 EXPECT_EQ(kActionCodeError, error_event.error_code);
754}
755
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700756TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -0700757 for (int ping_only = 0; ping_only < 2; ping_only++) {
758 NiceMock<PrefsMock> prefs;
759 // Add a few hours to the day difference to test no rounding, etc.
760 int64_t five_days_ago =
761 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
762 int64_t six_days_ago =
763 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
764 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
765 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
766 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
767 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
768 vector<char> post_data;
769 ASSERT_TRUE(
770 TestUpdateCheck(&prefs,
771 kDefaultTestParams,
772 GetNoUpdateResponse(OmahaRequestParams::kAppId),
773 -1,
774 ping_only,
775 kActionCodeSuccess,
776 NULL,
777 &post_data));
778 string post_str(&post_data[0], post_data.size());
779 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"),
780 string::npos);
781 if (ping_only) {
782 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
783 EXPECT_EQ(post_str.find("previousversion"), string::npos);
784 } else {
785 EXPECT_NE(post_str.find("o:updatecheck"), string::npos);
786 EXPECT_NE(post_str.find("previousversion"), string::npos);
787 }
788 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700789}
790
791TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800792 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700793 int64_t three_days_ago =
794 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
795 int64_t now = Time::Now().ToInternalValue();
796 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
797 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
798 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
799 .WillOnce(DoAll(SetArgumentPointee<1>(now), 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\" a=\"3\"></o:ping>"),
812 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700813}
814
815TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800816 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700817 int64_t four_days_ago =
818 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
819 int64_t now = Time::Now().ToInternalValue();
820 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
821 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
822 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
823 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
824 vector<char> post_data;
825 ASSERT_TRUE(
826 TestUpdateCheck(&prefs,
827 kDefaultTestParams,
828 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700829 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700830 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700831 kActionCodeSuccess,
832 NULL,
833 &post_data));
834 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700835 EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"),
836 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700837}
838
839TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800840 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700841 int64_t one_hour_ago =
842 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
843 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
844 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
845 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
846 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
847 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
848 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
849 vector<char> post_data;
850 ASSERT_TRUE(
851 TestUpdateCheck(&prefs,
852 kDefaultTestParams,
853 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700854 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700855 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700856 kActionCodeSuccess,
857 NULL,
858 &post_data));
859 string post_str(&post_data[0], post_data.size());
860 EXPECT_EQ(post_str.find("o:ping"), string::npos);
861}
862
Thieu Leb44e9e82011-06-06 14:34:04 -0700863TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
864 // This test ensures that we ignore empty ping only requests.
865 NiceMock<PrefsMock> prefs;
866 int64_t now = Time::Now().ToInternalValue();
867 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
868 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
869 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
870 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
871 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
872 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
873 vector<char> post_data;
874 EXPECT_TRUE(
875 TestUpdateCheck(&prefs,
876 kDefaultTestParams,
877 GetNoUpdateResponse(OmahaRequestParams::kAppId),
878 -1,
879 true, // ping_only
880 kActionCodeSuccess,
881 NULL,
882 &post_data));
883 EXPECT_EQ(post_data.size(), 0);
884}
885
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700886TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800887 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700888 int64_t future =
889 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
890 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
891 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
892 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
893 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
894 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
895 .WillOnce(Return(true));
896 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
897 .WillOnce(Return(true));
898 vector<char> post_data;
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 elapsed_seconds=\"100\"/>"
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 &post_data));
912 string post_str(&post_data[0], post_data.size());
913 EXPECT_EQ(post_str.find("o:ping"), string::npos);
914}
915
916TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
917 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700918 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700919 // may fail if it runs for longer than 5 seconds. It shouldn't run
920 // that long though.
921 int64_t midnight =
922 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
923 int64_t midnight_slack =
924 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800925 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700926 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
927 AllOf(Ge(midnight), Le(midnight_slack))))
928 .WillOnce(Return(true));
929 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
930 AllOf(Ge(midnight), Le(midnight_slack))))
931 .WillOnce(Return(true));
932 ASSERT_TRUE(
933 TestUpdateCheck(&prefs,
934 kDefaultTestParams,
935 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
936 "xmlns=\"http://www.google.com/update2/response\" "
937 "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>"
938 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
939 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700940 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700941 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700942 kActionCodeSuccess,
943 NULL,
944 NULL));
945}
946
947TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800948 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700949 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
950 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
951 ASSERT_TRUE(
952 TestUpdateCheck(&prefs,
953 kDefaultTestParams,
954 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
955 "xmlns=\"http://www.google.com/update2/response\" "
956 "protocol=\"2.0\"><daystart blah=\"200\"/>"
957 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
958 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700959 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700960 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700961 kActionCodeSuccess,
962 NULL,
963 NULL));
964}
965
966TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800967 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700968 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
969 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
970 ASSERT_TRUE(
971 TestUpdateCheck(&prefs,
972 kDefaultTestParams,
973 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
974 "xmlns=\"http://www.google.com/update2/response\" "
975 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
976 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
977 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700978 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700979 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700980 kActionCodeSuccess,
981 NULL,
982 NULL));
983}
984
Darin Petkov84c763c2010-07-29 16:27:58 -0700985TEST(OmahaRequestActionTest, NoUniqueIDTest) {
986 vector<char> post_data;
987 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
988 kDefaultTestParams,
989 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700990 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700991 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700992 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700993 NULL, // response
994 &post_data));
995 // convert post_data to string
996 string post_str(&post_data[0], post_data.size());
997 EXPECT_EQ(post_str.find("machineid="), string::npos);
998 EXPECT_EQ(post_str.find("userid="), string::npos);
999}
1000
Darin Petkovedc522e2010-11-05 09:35:17 -07001001TEST(OmahaRequestActionTest, NetworkFailureTest) {
1002 OmahaResponse response;
1003 ASSERT_FALSE(
1004 TestUpdateCheck(NULL, // prefs
1005 kDefaultTestParams,
1006 "",
1007 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001008 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -07001009 static_cast<ActionExitCode>(
1010 kActionCodeOmahaRequestHTTPResponseBase + 501),
1011 &response,
1012 NULL));
1013 EXPECT_FALSE(response.update_exists);
1014}
1015
1016TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
1017 OmahaResponse response;
1018 ASSERT_FALSE(
1019 TestUpdateCheck(NULL, // prefs
1020 kDefaultTestParams,
1021 "",
1022 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001023 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -07001024 static_cast<ActionExitCode>(
1025 kActionCodeOmahaRequestHTTPResponseBase + 999),
1026 &response,
1027 NULL));
1028 EXPECT_FALSE(response.update_exists);
1029}
1030
Darin Petkov6a5b3222010-07-13 14:55:28 -07001031} // namespace chromeos_update_engine