blob: 409e947a44d5718e62681e9571267aef7c2967a2 [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
Darin Petkov6a5b3222010-07-13 14:55:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07007
Darin Petkov6a5b3222010-07-13 14:55:28 -07008#include <glib.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07009
10#include "base/string_util.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070011#include "base/time.h"
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070012#include "gtest/gtest.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070013#include "update_engine/action_pipe.h"
14#include "update_engine/mock_http_fetcher.h"
15#include "update_engine/omaha_hash_calculator.h"
16#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070017#include "update_engine/omaha_request_params.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070018#include "update_engine/prefs_mock.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070019#include "update_engine/test_utils.h"
20
Darin Petkov1cbd78f2010-07-29 12:38:34 -070021using base::Time;
22using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070023using std::string;
24using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070025using testing::_;
26using testing::AllOf;
27using testing::Ge;
28using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080029using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070030using testing::Return;
31using testing::SetArgumentPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070032
33namespace chromeos_update_engine {
34
35class OmahaRequestActionTest : public ::testing::Test { };
36
37namespace {
Darin Petkov1cbd78f2010-07-29 12:38:34 -070038const OmahaRequestParams kDefaultTestParams(
Darin Petkov1cbd78f2010-07-29 12:38:34 -070039 OmahaRequestParams::kOsPlatform,
40 OmahaRequestParams::kOsVersion,
41 "service_pack",
42 "x86-generic",
43 OmahaRequestParams::kAppId,
44 "0.1.0.0",
45 "en-US",
46 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070047 "OEM MODEL 09235 7471",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070048 false, // delta okay
49 "http://url");
50
Darin Petkov6a5b3222010-07-13 14:55:28 -070051string GetNoUpdateResponse(const string& app_id) {
52 return string(
53 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
54 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
55 "appid=\"") + app_id + "\" status=\"ok\"><ping "
56 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
57}
58
59string GetUpdateResponse(const string& app_id,
60 const string& display_version,
61 const string& more_info_url,
62 const string& prompt,
63 const string& codebase,
64 const string& hash,
65 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -070066 const string& size,
67 const string& deadline) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070068 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
69 "xmlns=\"http://www.google.com/update2/response\" "
70 "protocol=\"2.0\"><app "
71 "appid=\"") + app_id + "\" status=\"ok\"><ping "
72 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
73 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070074 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -070075 "codebase=\"" + codebase + "\" hash=\"not-applicable\" "
76 "sha256=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
Darin Petkov6c118642010-10-21 12:06:30 -070077 "size=\"" + size + "\" deadline=\"" + deadline +
78 "\" status=\"ok\"/></app></gupdate>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070079}
80
81class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
82 public:
83 OmahaRequestActionTestProcessorDelegate()
84 : loop_(NULL),
Darin Petkovc1a8b422010-07-19 11:34:49 -070085 expected_code_(kActionCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070086 virtual ~OmahaRequestActionTestProcessorDelegate() {
87 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070088 virtual void ProcessingDone(const ActionProcessor* processor,
89 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070090 ASSERT_TRUE(loop_);
91 g_main_loop_quit(loop_);
92 }
93
94 virtual void ActionCompleted(ActionProcessor* processor,
95 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070096 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070097 // make sure actions always succeed
98 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -070099 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700100 else
Darin Petkovc1a8b422010-07-19 11:34:49 -0700101 EXPECT_EQ(kActionCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700102 }
103 GMainLoop *loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700104 ActionExitCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105};
106
107gboolean StartProcessorInRunLoop(gpointer data) {
108 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
109 processor->StartProcessing();
110 return FALSE;
111}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700112} // namespace {}
113
114class OutputObjectCollectorAction;
115
116template<>
117class ActionTraits<OutputObjectCollectorAction> {
118 public:
119 // Does not take an object for input
120 typedef OmahaResponse InputObjectType;
121 // On success, puts the output path on output
122 typedef NoneType OutputObjectType;
123};
124
125class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
126 public:
127 OutputObjectCollectorAction() : has_input_object_(false) {}
128 void PerformAction() {
129 // copy input object
130 has_input_object_ = HasInputObject();
131 if (has_input_object_)
132 omaha_response_ = GetInputObject();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700133 processor_->ActionComplete(this, kActionCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700134 }
135 // Should never be called
136 void TerminateProcessing() {
137 CHECK(false);
138 }
139 // Debugging/logging
140 static std::string StaticType() {
141 return "OutputObjectCollectorAction";
142 }
143 std::string Type() const { return StaticType(); }
144 bool has_input_object_;
145 OmahaResponse omaha_response_;
146};
147
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700148// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700149// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
150// used. out_response may be NULL. If |fail_http_response_code| is
151// non-negative, the transfer will fail with that code. out_post_data may be
152// null; if non-null, the post-data received by the mock HttpFetcher is
153// returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700154bool TestUpdateCheck(PrefsInterface* prefs,
155 const OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700156 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700157 int fail_http_response_code,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700158 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700159 OmahaResponse* out_response,
160 vector<char>* out_post_data) {
161 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
162 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800163 http_response.size(),
164 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700165 if (fail_http_response_code >= 0) {
166 fetcher->FailTransfer(fail_http_response_code);
167 }
Darin Petkov9c096d62010-11-17 14:49:04 -0800168 NiceMock<PrefsMock> local_prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700169 OmahaRequestAction action(prefs ? prefs : &local_prefs,
170 params,
171 NULL,
172 fetcher);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700173 OmahaRequestActionTestProcessorDelegate delegate;
174 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700175 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700176
Darin Petkov6a5b3222010-07-13 14:55:28 -0700177 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700178 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700179 processor.EnqueueAction(&action);
180
181 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700182 BondActions(&action, &collector_action);
183 processor.EnqueueAction(&collector_action);
184
185 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
186 g_main_loop_run(loop);
187 g_main_loop_unref(loop);
188 if (collector_action.has_input_object_ && out_response)
189 *out_response = collector_action.omaha_response_;
190 if (out_post_data)
191 *out_post_data = fetcher->post_data();
192 return collector_action.has_input_object_;
193}
194
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700195// Tests Event requests -- they should always succeed. |out_post_data|
196// may be null; if non-null, the post-data received by the mock
197// HttpFetcher is returned.
198void TestEvent(const OmahaRequestParams& params,
199 OmahaEvent* event,
200 const string& http_response,
201 vector<char>* out_post_data) {
202 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
203 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800204 http_response.size(),
205 NULL);
Darin Petkov9c096d62010-11-17 14:49:04 -0800206 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700207 OmahaRequestAction action(&prefs, params, event, fetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700208 OmahaRequestActionTestProcessorDelegate delegate;
209 delegate.loop_ = loop;
210 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700211 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700212 processor.EnqueueAction(&action);
213
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700214 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
215 g_main_loop_run(loop);
216 g_main_loop_unref(loop);
217 if (out_post_data)
218 *out_post_data = fetcher->post_data();
219}
220
Darin Petkov6a5b3222010-07-13 14:55:28 -0700221TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700222 OmahaResponse response;
223 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700224 TestUpdateCheck(NULL, // prefs
225 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700226 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700227 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700228 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700229 &response,
230 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700231 EXPECT_FALSE(response.update_exists);
232}
233
234TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700235 OmahaResponse response;
236 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700237 TestUpdateCheck(NULL, // prefs
238 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700239 GetUpdateResponse(OmahaRequestParams::kAppId,
240 "1.2.3.4", // version
241 "http://more/info",
242 "true", // prompt
243 "http://code/base", // dl url
244 "HASH1234=", // checksum
245 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700246 "123", // size
247 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700248 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700249 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700250 &response,
251 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700252 EXPECT_TRUE(response.update_exists);
253 EXPECT_EQ("1.2.3.4", response.display_version);
254 EXPECT_EQ("http://code/base", response.codebase);
255 EXPECT_EQ("http://more/info", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700256 EXPECT_TRUE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700257 EXPECT_EQ("HASH1234=", response.hash);
258 EXPECT_EQ(123, response.size);
259 EXPECT_FALSE(response.needs_admin);
260 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700261 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262}
263
264TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700265 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
266
267 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
268
Darin Petkov9c096d62010-11-17 14:49:04 -0800269 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700270 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700271 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800272 http_response.size(),
273 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700274 OmahaRequestActionTestProcessorDelegate delegate;
275 delegate.loop_ = loop;
276 ActionProcessor processor;
277 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700278 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700279
280 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
281 g_main_loop_run(loop);
282 g_main_loop_unref(loop);
283 EXPECT_FALSE(processor.IsRunning());
284}
285
Andrew de los Reyes173e63c2011-04-04 17:19:57 -0700286TEST(OmahaRequestActionTest, SkipTest) {
287 const string http_response("invalid xml>");
288
289 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
290
291 NiceMock<PrefsMock> prefs;
292 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
293 http_response.size(),
294 NULL);
295 fetcher->set_never_use(true);
296 OmahaRequestAction action(&prefs, kDefaultTestParams,
297 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
298 fetcher); // Passes fetcher ownership
299 action.set_should_skip(true);
300 OmahaRequestActionTestProcessorDelegate delegate;
301 delegate.loop_ = loop;
302 ActionProcessor processor;
303 processor.set_delegate(&delegate);
304 processor.EnqueueAction(&action);
305
306 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
307 g_main_loop_run(loop);
308 g_main_loop_unref(loop);
309 EXPECT_FALSE(processor.IsRunning());
310}
311
Darin Petkov6a5b3222010-07-13 14:55:28 -0700312TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700313 OmahaResponse response;
314 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700315 TestUpdateCheck(NULL, // prefs
316 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700317 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700318 -1,
319 kActionCodeOmahaRequestXMLParseError,
320 &response,
321 NULL));
322 EXPECT_FALSE(response.update_exists);
323}
324
325TEST(OmahaRequestActionTest, EmptyResponseTest) {
326 OmahaResponse response;
327 ASSERT_FALSE(
328 TestUpdateCheck(NULL, // prefs
329 kDefaultTestParams,
330 "",
331 -1,
332 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700333 &response,
334 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700335 EXPECT_FALSE(response.update_exists);
336}
337
338TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700339 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700340 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700341 NULL, // prefs
342 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700343 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
344 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
345 "appid=\"foo\" status=\"ok\"><ping "
346 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700347 -1,
348 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700349 &response,
350 NULL));
351 EXPECT_FALSE(response.update_exists);
352}
353
354TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700355 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700356 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700357 NULL, // prefs
358 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700359 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
360 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
361 "appid=\"foo\" status=\"ok\"><ping "
362 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700363 -1,
364 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700365 &response,
366 NULL));
367 EXPECT_FALSE(response.update_exists);
368}
369
370TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700371 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700372 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700373 NULL, // prefs
374 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700375 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
376 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
377 "appid=\"foo\" status=\"ok\"><ping "
378 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700379 -1,
380 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700381 &response,
382 NULL));
383 EXPECT_FALSE(response.update_exists);
384}
385
386TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700387 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700388 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
389 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700390 string("<?xml version=\"1.0\" "
391 "encoding=\"UTF-8\"?><gupdate "
392 "xmlns=\"http://www.google.com/"
393 "update2/response\" "
394 "protocol=\"2.0\"><app appid=\"") +
395 OmahaRequestParams::kAppId
396 + "\" status=\"ok\"><ping "
397 "status=\"ok\"/><updatecheck "
398 "DisplayVersion=\"1.2.3.4\" "
399 "Prompt=\"false\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700400 "codebase=\"http://code/base\" hash=\"foo\" "
401 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700402 "size=\"123\" "
403 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700404 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700405 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700406 &response,
407 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700408 EXPECT_TRUE(response.update_exists);
409 EXPECT_EQ("1.2.3.4", response.display_version);
410 EXPECT_EQ("http://code/base", response.codebase);
411 EXPECT_EQ("", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700412 EXPECT_FALSE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700413 EXPECT_EQ("HASH1234=", response.hash);
414 EXPECT_EQ(123, response.size);
415 EXPECT_TRUE(response.needs_admin);
416 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700417 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700418}
419
420namespace {
421class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
422 public:
423 void ProcessingStopped(const ActionProcessor* processor) {
424 ASSERT_TRUE(loop_);
425 g_main_loop_quit(loop_);
426 }
427 GMainLoop *loop_;
428};
429
430gboolean TerminateTransferTestStarter(gpointer data) {
431 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
432 processor->StartProcessing();
433 CHECK(processor->IsRunning());
434 processor->StopProcessing();
435 return FALSE;
436}
437} // namespace {}
438
439TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700440 string http_response("doesn't matter");
441 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
442
Darin Petkov9c096d62010-11-17 14:49:04 -0800443 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700444 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700445 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800446 http_response.size(),
447 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700448 TerminateEarlyTestProcessorDelegate delegate;
449 delegate.loop_ = loop;
450 ActionProcessor processor;
451 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700452 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700453
454 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
455 g_main_loop_run(loop);
456 g_main_loop_unref(loop);
457}
458
459TEST(OmahaRequestActionTest, XmlEncodeTest) {
460 EXPECT_EQ("ab", XmlEncode("ab"));
461 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
462 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
463 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
464 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
465
466 vector<char> post_data;
467
468 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700469 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700470 OmahaRequestParams::kOsVersion,
471 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700472 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700473 OmahaRequestParams::kAppId,
474 "0.1.0.0",
475 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700476 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700477 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700478 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700479 "http://url");
480 OmahaResponse response;
481 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700482 TestUpdateCheck(NULL, // prefs
483 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700484 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700485 -1,
486 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700487 &response,
488 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700489 // convert post_data to string
490 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700491 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
492 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700493 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
494 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
495 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
496 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700497 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
498 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700499}
500
501TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700502 OmahaResponse response;
503 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700504 TestUpdateCheck(NULL, // prefs
505 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700506 GetUpdateResponse(OmahaRequestParams::kAppId,
507 "1.2.3.4", // version
508 "testthe&lt;url", // more info
509 "true", // prompt
510 "testthe&amp;codebase", // dl url
511 "HASH1234=", // checksum
512 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700513 "123", // size
514 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700515 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700516 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700517 &response,
518 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700519
520 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700521 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700522 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700523}
524
525TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700526 OmahaResponse response;
527 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700528 TestUpdateCheck(NULL, // prefs
529 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700530 GetUpdateResponse(OmahaRequestParams::kAppId,
531 "1.2.3.4", // version
532 "theurl", // more info
533 "true", // prompt
534 "thecodebase", // dl url
535 "HASH1234=", // checksum
536 "false", // needs admin
537 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700538 "123123123123123", // size
539 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700540 -1,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700541 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700542 &response,
543 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700544
545 EXPECT_EQ(response.size, 123123123123123ll);
546}
547
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700548TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
549 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800550 NiceMock<PrefsMock> prefs;
551 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
552 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
553 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
554 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700555 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700556 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700557 -1,
558 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700559 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700560 &post_data));
561 // convert post_data to string
562 string post_str(&post_data[0], post_data.size());
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700563 EXPECT_NE(post_str.find(" <o:ping a=\"-1\" r=\"-1\"></o:ping>\n"
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700564 " <o:updatecheck></o:updatecheck>\n"),
565 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700566 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
567 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700568 EXPECT_EQ(post_str.find("o:event"), string::npos);
569}
570
Darin Petkov95508da2011-01-05 12:42:29 -0800571TEST(OmahaRequestActionTest, FormatUpdateCheckPrevVersionOutputTest) {
572 vector<char> post_data;
573 NiceMock<PrefsMock> prefs;
574 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800575 .WillOnce(DoAll(SetArgumentPointee<1>(string("1.2>3.4")), Return(true)));
Darin Petkov95508da2011-01-05 12:42:29 -0800576 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, ""))
577 .WillOnce(Return(true));
578 ASSERT_FALSE(TestUpdateCheck(&prefs,
579 kDefaultTestParams,
580 "invalid xml>",
581 -1,
582 kActionCodeOmahaRequestXMLParseError,
583 NULL, // response
584 &post_data));
585 // convert post_data to string
586 string post_str(&post_data[0], post_data.size());
587 EXPECT_NE(post_str.find(" <o:ping a=\"-1\" r=\"-1\"></o:ping>\n"
588 " <o:updatecheck></o:updatecheck>\n"),
589 string::npos);
590 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
591 string::npos);
592 string prev_version_event = StringPrintf(
593 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800594 "previousversion=\"1.2&gt;3.4\"></o:event>\n",
Darin Petkov95508da2011-01-05 12:42:29 -0800595 OmahaEvent::kTypeUpdateComplete,
596 OmahaEvent::kResultSuccessReboot);
597 EXPECT_NE(post_str.find(prev_version_event), string::npos);
598}
599
Darin Petkove17f86b2010-07-20 09:12:01 -0700600TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
601 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700602 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700603 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
604 "invalid xml>",
605 &post_data);
606 // convert post_data to string
607 string post_str(&post_data[0], post_data.size());
608 string expected_event = StringPrintf(
609 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
610 OmahaEvent::kTypeUpdateDownloadStarted,
611 OmahaEvent::kResultSuccess);
612 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700613 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700614 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
615}
616
617TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
618 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700619 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700620 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
621 OmahaEvent::kResultError,
622 kActionCodeError),
623 "invalid xml>",
624 &post_data);
625 // convert post_data to string
626 string post_str(&post_data[0], post_data.size());
627 string expected_event = StringPrintf(
628 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
629 "errorcode=\"%d\"></o:event>\n",
630 OmahaEvent::kTypeDownloadComplete,
631 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700632 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700633 EXPECT_NE(post_str.find(expected_event), string::npos);
634 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
635}
636
637TEST(OmahaRequestActionTest, IsEventTest) {
638 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800639 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700640 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700641 &prefs,
642 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700643 NULL,
644 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800645 http_response.size(),
646 NULL));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700647 EXPECT_FALSE(update_check_action.IsEvent());
648
649 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700650 &prefs,
651 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700652 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700653 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800654 http_response.size(),
655 NULL));
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700656 EXPECT_TRUE(event_action.IsEvent());
657}
658
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700659TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
660 for (int i = 0; i < 2; i++) {
661 bool delta_okay = i == 1;
662 const char* delta_okay_str = delta_okay ? "true" : "false";
663 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700664 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700665 OmahaRequestParams::kOsVersion,
666 "service_pack",
667 "x86-generic",
668 OmahaRequestParams::kAppId,
669 "0.1.0.0",
670 "en-US",
671 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700672 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700673 delta_okay,
674 "http://url");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700675 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
676 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700677 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700678 -1,
679 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700680 NULL,
681 &post_data));
682 // convert post_data to string
683 string post_str(&post_data[0], post_data.size());
684 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
685 string::npos)
686 << "i = " << i;
687 }
688}
689
Darin Petkove17f86b2010-07-20 09:12:01 -0700690TEST(OmahaRequestActionTest, OmahaEventTest) {
691 OmahaEvent default_event;
692 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
693 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
694 EXPECT_EQ(kActionCodeError, default_event.error_code);
695
696 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
697 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
698 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
699 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
700
701 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
702 OmahaEvent::kResultError,
703 kActionCodeError);
704 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
705 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
706 EXPECT_EQ(kActionCodeError, error_event.error_code);
707}
708
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700709TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800710 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700711 // Add a few hours to the day difference to test no rounding, etc.
712 int64_t five_days_ago =
713 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
714 int64_t six_days_ago =
715 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
716 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
717 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
718 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
719 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
720 vector<char> post_data;
721 ASSERT_TRUE(
722 TestUpdateCheck(&prefs,
723 kDefaultTestParams,
724 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700725 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700726 kActionCodeSuccess,
727 NULL,
728 &post_data));
729 string post_str(&post_data[0], post_data.size());
730 EXPECT_NE(post_str.find("<o:ping a=\"6\" r=\"5\"></o:ping>"), string::npos);
731}
732
733TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800734 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700735 int64_t three_days_ago =
736 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
737 int64_t now = Time::Now().ToInternalValue();
738 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
739 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
740 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
741 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
742 vector<char> post_data;
743 ASSERT_TRUE(
744 TestUpdateCheck(&prefs,
745 kDefaultTestParams,
746 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700747 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700748 kActionCodeSuccess,
749 NULL,
750 &post_data));
751 string post_str(&post_data[0], post_data.size());
752 EXPECT_NE(post_str.find("<o:ping a=\"3\"></o:ping>"), string::npos);
753}
754
755TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800756 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700757 int64_t four_days_ago =
758 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
759 int64_t now = Time::Now().ToInternalValue();
760 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
761 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
762 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
763 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
764 vector<char> post_data;
765 ASSERT_TRUE(
766 TestUpdateCheck(&prefs,
767 kDefaultTestParams,
768 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700769 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700770 kActionCodeSuccess,
771 NULL,
772 &post_data));
773 string post_str(&post_data[0], post_data.size());
774 EXPECT_NE(post_str.find("<o:ping r=\"4\"></o:ping>\n"), string::npos);
775}
776
777TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800778 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700779 int64_t one_hour_ago =
780 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
781 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
782 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
783 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
784 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
785 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
786 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
787 vector<char> post_data;
788 ASSERT_TRUE(
789 TestUpdateCheck(&prefs,
790 kDefaultTestParams,
791 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700792 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700793 kActionCodeSuccess,
794 NULL,
795 &post_data));
796 string post_str(&post_data[0], post_data.size());
797 EXPECT_EQ(post_str.find("o:ping"), string::npos);
798}
799
800TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800801 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700802 int64_t future =
803 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
804 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
805 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
806 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
807 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
808 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
809 .WillOnce(Return(true));
810 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
811 .WillOnce(Return(true));
812 vector<char> post_data;
813 ASSERT_TRUE(
814 TestUpdateCheck(&prefs,
815 kDefaultTestParams,
816 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
817 "xmlns=\"http://www.google.com/update2/response\" "
818 "protocol=\"2.0\"><daystart elapsed_seconds=\"100\"/>"
819 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
820 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700821 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700822 kActionCodeSuccess,
823 NULL,
824 &post_data));
825 string post_str(&post_data[0], post_data.size());
826 EXPECT_EQ(post_str.find("o:ping"), string::npos);
827}
828
829TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
830 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700831 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700832 // may fail if it runs for longer than 5 seconds. It shouldn't run
833 // that long though.
834 int64_t midnight =
835 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
836 int64_t midnight_slack =
837 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800838 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700839 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
840 AllOf(Ge(midnight), Le(midnight_slack))))
841 .WillOnce(Return(true));
842 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
843 AllOf(Ge(midnight), Le(midnight_slack))))
844 .WillOnce(Return(true));
845 ASSERT_TRUE(
846 TestUpdateCheck(&prefs,
847 kDefaultTestParams,
848 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
849 "xmlns=\"http://www.google.com/update2/response\" "
850 "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>"
851 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
852 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700853 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700854 kActionCodeSuccess,
855 NULL,
856 NULL));
857}
858
859TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800860 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700861 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
862 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
863 ASSERT_TRUE(
864 TestUpdateCheck(&prefs,
865 kDefaultTestParams,
866 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
867 "xmlns=\"http://www.google.com/update2/response\" "
868 "protocol=\"2.0\"><daystart blah=\"200\"/>"
869 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
870 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700871 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700872 kActionCodeSuccess,
873 NULL,
874 NULL));
875}
876
877TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800878 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700879 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
880 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
881 ASSERT_TRUE(
882 TestUpdateCheck(&prefs,
883 kDefaultTestParams,
884 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
885 "xmlns=\"http://www.google.com/update2/response\" "
886 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
887 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
888 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700889 -1,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700890 kActionCodeSuccess,
891 NULL,
892 NULL));
893}
894
Darin Petkov84c763c2010-07-29 16:27:58 -0700895TEST(OmahaRequestActionTest, NoUniqueIDTest) {
896 vector<char> post_data;
897 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
898 kDefaultTestParams,
899 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700900 -1,
901 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700902 NULL, // response
903 &post_data));
904 // convert post_data to string
905 string post_str(&post_data[0], post_data.size());
906 EXPECT_EQ(post_str.find("machineid="), string::npos);
907 EXPECT_EQ(post_str.find("userid="), string::npos);
908}
909
Darin Petkovedc522e2010-11-05 09:35:17 -0700910TEST(OmahaRequestActionTest, NetworkFailureTest) {
911 OmahaResponse response;
912 ASSERT_FALSE(
913 TestUpdateCheck(NULL, // prefs
914 kDefaultTestParams,
915 "",
916 501,
917 static_cast<ActionExitCode>(
918 kActionCodeOmahaRequestHTTPResponseBase + 501),
919 &response,
920 NULL));
921 EXPECT_FALSE(response.update_exists);
922}
923
924TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
925 OmahaResponse response;
926 ASSERT_FALSE(
927 TestUpdateCheck(NULL, // prefs
928 kDefaultTestParams,
929 "",
930 1500,
931 static_cast<ActionExitCode>(
932 kActionCodeOmahaRequestHTTPResponseBase + 999),
933 &response,
934 NULL));
935 EXPECT_FALSE(response.update_exists);
936}
937
Darin Petkov6a5b3222010-07-13 14:55:28 -0700938} // namespace chromeos_update_engine