blob: 6f4df63500d87c60dada4281781d8bf2de75e5c5 [file] [log] [blame]
Darin Petkov265f2902011-05-09 15:17:40 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkov6a5b3222010-07-13 14:55:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07007
Darin Petkov6a5b3222010-07-13 14:55:28 -07008#include <glib.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07009
10#include "base/string_util.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070011#include "base/time.h"
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070012#include "gtest/gtest.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070013#include "update_engine/action_pipe.h"
14#include "update_engine/mock_http_fetcher.h"
15#include "update_engine/omaha_hash_calculator.h"
16#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070017#include "update_engine/omaha_request_params.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070018#include "update_engine/prefs_mock.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070019#include "update_engine/test_utils.h"
20
Darin Petkov1cbd78f2010-07-29 12:38:34 -070021using base::Time;
22using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070023using std::string;
24using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070025using testing::_;
26using testing::AllOf;
27using testing::Ge;
28using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080029using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070030using testing::Return;
31using testing::SetArgumentPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070032
33namespace chromeos_update_engine {
34
35class OmahaRequestActionTest : public ::testing::Test { };
36
37namespace {
Darin Petkov1cbd78f2010-07-29 12:38:34 -070038const OmahaRequestParams kDefaultTestParams(
Darin Petkov1cbd78f2010-07-29 12:38:34 -070039 OmahaRequestParams::kOsPlatform,
40 OmahaRequestParams::kOsVersion,
41 "service_pack",
42 "x86-generic",
43 OmahaRequestParams::kAppId,
44 "0.1.0.0",
45 "en-US",
46 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070047 "OEM MODEL 09235 7471",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070048 false, // delta okay
49 "http://url");
50
Darin Petkov6a5b3222010-07-13 14:55:28 -070051string GetNoUpdateResponse(const string& app_id) {
52 return string(
53 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
54 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
55 "appid=\"") + app_id + "\" status=\"ok\"><ping "
56 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
57}
58
59string GetUpdateResponse(const string& app_id,
60 const string& display_version,
61 const string& more_info_url,
62 const string& prompt,
63 const string& codebase,
64 const string& hash,
65 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -070066 const string& size,
67 const string& deadline) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070068 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
69 "xmlns=\"http://www.google.com/update2/response\" "
70 "protocol=\"2.0\"><app "
71 "appid=\"") + app_id + "\" status=\"ok\"><ping "
72 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
73 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070074 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -070075 "codebase=\"" + codebase + "\" hash=\"not-applicable\" "
76 "sha256=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
Darin Petkov6c118642010-10-21 12:06:30 -070077 "size=\"" + size + "\" deadline=\"" + deadline +
78 "\" status=\"ok\"/></app></gupdate>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070079}
80
81class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
82 public:
83 OmahaRequestActionTestProcessorDelegate()
84 : loop_(NULL),
Darin Petkovc1a8b422010-07-19 11:34:49 -070085 expected_code_(kActionCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070086 virtual ~OmahaRequestActionTestProcessorDelegate() {
87 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070088 virtual void ProcessingDone(const ActionProcessor* processor,
89 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070090 ASSERT_TRUE(loop_);
91 g_main_loop_quit(loop_);
92 }
93
94 virtual void ActionCompleted(ActionProcessor* processor,
95 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070096 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070097 // make sure actions always succeed
98 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -070099 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700100 else
Darin Petkovc1a8b422010-07-19 11:34:49 -0700101 EXPECT_EQ(kActionCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700102 }
103 GMainLoop *loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700104 ActionExitCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105};
106
107gboolean StartProcessorInRunLoop(gpointer data) {
108 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
109 processor->StartProcessing();
110 return FALSE;
111}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700112} // namespace {}
113
114class OutputObjectCollectorAction;
115
116template<>
117class ActionTraits<OutputObjectCollectorAction> {
118 public:
119 // Does not take an object for input
120 typedef OmahaResponse InputObjectType;
121 // On success, puts the output path on output
122 typedef NoneType OutputObjectType;
123};
124
125class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
126 public:
127 OutputObjectCollectorAction() : has_input_object_(false) {}
128 void PerformAction() {
129 // copy input object
130 has_input_object_ = HasInputObject();
131 if (has_input_object_)
132 omaha_response_ = GetInputObject();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700133 processor_->ActionComplete(this, kActionCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700134 }
135 // Should never be called
136 void TerminateProcessing() {
137 CHECK(false);
138 }
139 // Debugging/logging
140 static std::string StaticType() {
141 return "OutputObjectCollectorAction";
142 }
143 std::string Type() const { return StaticType(); }
144 bool has_input_object_;
145 OmahaResponse omaha_response_;
146};
147
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700148// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700149// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
Darin Petkov265f2902011-05-09 15:17:40 -0700150// used. out_response may be NULL. If |fail_http_response_code| is non-negative,
151// the transfer will fail with that code. |ping_only| is passed through to the
152// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
153// post-data received by the mock HttpFetcher is returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700154bool TestUpdateCheck(PrefsInterface* prefs,
155 const OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700156 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700157 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700158 bool ping_only,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700159 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700160 OmahaResponse* out_response,
161 vector<char>* out_post_data) {
162 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
163 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800164 http_response.size(),
165 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700166 if (fail_http_response_code >= 0) {
167 fetcher->FailTransfer(fail_http_response_code);
168 }
Darin Petkov9c096d62010-11-17 14:49:04 -0800169 NiceMock<PrefsMock> local_prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700170 OmahaRequestAction action(prefs ? prefs : &local_prefs,
171 params,
172 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700173 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700174 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700175 OmahaRequestActionTestProcessorDelegate delegate;
176 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700177 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700178
Darin Petkov6a5b3222010-07-13 14:55:28 -0700179 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700180 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700181 processor.EnqueueAction(&action);
182
183 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700184 BondActions(&action, &collector_action);
185 processor.EnqueueAction(&collector_action);
186
187 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
188 g_main_loop_run(loop);
189 g_main_loop_unref(loop);
190 if (collector_action.has_input_object_ && out_response)
191 *out_response = collector_action.omaha_response_;
192 if (out_post_data)
193 *out_post_data = fetcher->post_data();
194 return collector_action.has_input_object_;
195}
196
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700197// Tests Event requests -- they should always succeed. |out_post_data|
198// may be null; if non-null, the post-data received by the mock
199// HttpFetcher is returned.
200void TestEvent(const OmahaRequestParams& params,
201 OmahaEvent* event,
202 const string& http_response,
203 vector<char>* out_post_data) {
204 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
205 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800206 http_response.size(),
207 NULL);
Darin Petkov9c096d62010-11-17 14:49:04 -0800208 NiceMock<PrefsMock> prefs;
Thieu Le116fda32011-04-19 11:01:54 -0700209 OmahaRequestAction action(&prefs, params, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700210 OmahaRequestActionTestProcessorDelegate delegate;
211 delegate.loop_ = loop;
212 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700213 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700214 processor.EnqueueAction(&action);
215
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700216 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
217 g_main_loop_run(loop);
218 g_main_loop_unref(loop);
219 if (out_post_data)
220 *out_post_data = fetcher->post_data();
221}
222
Darin Petkov6a5b3222010-07-13 14:55:28 -0700223TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700224 OmahaResponse response;
225 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700226 TestUpdateCheck(NULL, // prefs
227 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700228 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700229 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700230 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700231 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700232 &response,
233 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700234 EXPECT_FALSE(response.update_exists);
235}
236
237TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700238 OmahaResponse response;
239 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700240 TestUpdateCheck(NULL, // prefs
241 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700242 GetUpdateResponse(OmahaRequestParams::kAppId,
243 "1.2.3.4", // version
244 "http://more/info",
245 "true", // prompt
246 "http://code/base", // dl url
247 "HASH1234=", // checksum
248 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700249 "123", // size
250 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700251 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700252 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700253 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700254 &response,
255 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700256 EXPECT_TRUE(response.update_exists);
257 EXPECT_EQ("1.2.3.4", response.display_version);
258 EXPECT_EQ("http://code/base", response.codebase);
259 EXPECT_EQ("http://more/info", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700260 EXPECT_TRUE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700261 EXPECT_EQ("HASH1234=", response.hash);
262 EXPECT_EQ(123, response.size);
263 EXPECT_FALSE(response.needs_admin);
264 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700265 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700266}
267
268TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700269 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
270
271 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
272
Darin Petkov9c096d62010-11-17 14:49:04 -0800273 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700274 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700275 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800276 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700277 NULL),
278 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700279 OmahaRequestActionTestProcessorDelegate delegate;
280 delegate.loop_ = loop;
281 ActionProcessor processor;
282 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700283 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700284
285 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
286 g_main_loop_run(loop);
287 g_main_loop_unref(loop);
288 EXPECT_FALSE(processor.IsRunning());
289}
290
291TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700292 OmahaResponse response;
293 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700294 TestUpdateCheck(NULL, // prefs
295 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700296 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700297 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700298 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700299 kActionCodeOmahaRequestXMLParseError,
300 &response,
301 NULL));
302 EXPECT_FALSE(response.update_exists);
303}
304
305TEST(OmahaRequestActionTest, EmptyResponseTest) {
306 OmahaResponse response;
307 ASSERT_FALSE(
308 TestUpdateCheck(NULL, // prefs
309 kDefaultTestParams,
310 "",
311 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700312 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700313 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700314 &response,
315 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700316 EXPECT_FALSE(response.update_exists);
317}
318
319TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700320 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700321 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700322 NULL, // prefs
323 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700324 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
325 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
326 "appid=\"foo\" status=\"ok\"><ping "
327 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700328 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700329 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700330 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700331 &response,
332 NULL));
333 EXPECT_FALSE(response.update_exists);
334}
335
336TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700337 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700338 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700339 NULL, // prefs
340 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700341 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
342 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
343 "appid=\"foo\" status=\"ok\"><ping "
344 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700345 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700346 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700347 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700348 &response,
349 NULL));
350 EXPECT_FALSE(response.update_exists);
351}
352
353TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700354 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700355 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700356 NULL, // prefs
357 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700358 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
359 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
360 "appid=\"foo\" status=\"ok\"><ping "
361 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700362 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700363 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700364 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700365 &response,
366 NULL));
367 EXPECT_FALSE(response.update_exists);
368}
369
370TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700371 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700372 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
373 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700374 string("<?xml version=\"1.0\" "
375 "encoding=\"UTF-8\"?><gupdate "
376 "xmlns=\"http://www.google.com/"
377 "update2/response\" "
378 "protocol=\"2.0\"><app appid=\"") +
379 OmahaRequestParams::kAppId
380 + "\" status=\"ok\"><ping "
381 "status=\"ok\"/><updatecheck "
382 "DisplayVersion=\"1.2.3.4\" "
383 "Prompt=\"false\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700384 "codebase=\"http://code/base\" hash=\"foo\" "
385 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700386 "size=\"123\" "
387 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700388 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700389 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700390 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700391 &response,
392 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700393 EXPECT_TRUE(response.update_exists);
394 EXPECT_EQ("1.2.3.4", response.display_version);
395 EXPECT_EQ("http://code/base", response.codebase);
396 EXPECT_EQ("", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700397 EXPECT_FALSE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700398 EXPECT_EQ("HASH1234=", response.hash);
399 EXPECT_EQ(123, response.size);
400 EXPECT_TRUE(response.needs_admin);
401 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700402 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700403}
404
405namespace {
406class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
407 public:
408 void ProcessingStopped(const ActionProcessor* processor) {
409 ASSERT_TRUE(loop_);
410 g_main_loop_quit(loop_);
411 }
412 GMainLoop *loop_;
413};
414
415gboolean TerminateTransferTestStarter(gpointer data) {
416 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
417 processor->StartProcessing();
418 CHECK(processor->IsRunning());
419 processor->StopProcessing();
420 return FALSE;
421}
422} // namespace {}
423
424TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700425 string http_response("doesn't matter");
426 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
427
Darin Petkov9c096d62010-11-17 14:49:04 -0800428 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700429 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700430 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800431 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700432 NULL),
433 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700434 TerminateEarlyTestProcessorDelegate delegate;
435 delegate.loop_ = loop;
436 ActionProcessor processor;
437 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700438 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700439
440 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
441 g_main_loop_run(loop);
442 g_main_loop_unref(loop);
443}
444
445TEST(OmahaRequestActionTest, XmlEncodeTest) {
446 EXPECT_EQ("ab", XmlEncode("ab"));
447 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
448 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
449 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
450 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
451
452 vector<char> post_data;
453
454 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700455 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700456 OmahaRequestParams::kOsVersion,
457 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700458 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700459 OmahaRequestParams::kAppId,
460 "0.1.0.0",
461 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700462 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700463 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700464 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700465 "http://url");
466 OmahaResponse response;
467 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700468 TestUpdateCheck(NULL, // prefs
469 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700470 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700471 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700472 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700473 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700474 &response,
475 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700476 // convert post_data to string
477 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700478 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
479 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700480 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
481 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
482 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
483 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700484 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
485 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700486}
487
488TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700489 OmahaResponse response;
490 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700491 TestUpdateCheck(NULL, // prefs
492 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700493 GetUpdateResponse(OmahaRequestParams::kAppId,
494 "1.2.3.4", // version
495 "testthe&lt;url", // more info
496 "true", // prompt
497 "testthe&amp;codebase", // dl url
498 "HASH1234=", // checksum
499 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700500 "123", // size
501 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700502 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700503 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700504 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700505 &response,
506 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700507
508 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700509 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700510 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700511}
512
513TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700514 OmahaResponse response;
515 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700516 TestUpdateCheck(NULL, // prefs
517 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700518 GetUpdateResponse(OmahaRequestParams::kAppId,
519 "1.2.3.4", // version
520 "theurl", // more info
521 "true", // prompt
522 "thecodebase", // dl url
523 "HASH1234=", // checksum
524 "false", // needs admin
525 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700526 "123123123123123", // size
527 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700528 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700529 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700530 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700531 &response,
532 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700533
534 EXPECT_EQ(response.size, 123123123123123ll);
535}
536
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700537TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
538 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800539 NiceMock<PrefsMock> prefs;
540 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
541 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
542 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
543 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700544 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700545 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700546 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700547 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700548 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700549 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700550 &post_data));
551 // convert post_data to string
552 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700553 EXPECT_NE(post_str.find(
554 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
555 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700556 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700557 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
558 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700559 EXPECT_EQ(post_str.find("o:event"), string::npos);
560}
561
Darin Petkov95508da2011-01-05 12:42:29 -0800562TEST(OmahaRequestActionTest, FormatUpdateCheckPrevVersionOutputTest) {
563 vector<char> post_data;
564 NiceMock<PrefsMock> prefs;
565 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800566 .WillOnce(DoAll(SetArgumentPointee<1>(string("1.2>3.4")), Return(true)));
Darin Petkov95508da2011-01-05 12:42:29 -0800567 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, ""))
568 .WillOnce(Return(true));
569 ASSERT_FALSE(TestUpdateCheck(&prefs,
570 kDefaultTestParams,
571 "invalid xml>",
572 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700573 false, // ping_only
Darin Petkov95508da2011-01-05 12:42:29 -0800574 kActionCodeOmahaRequestXMLParseError,
575 NULL, // response
576 &post_data));
577 // convert post_data to string
578 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700579 EXPECT_NE(post_str.find(
580 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
581 " <o:updatecheck></o:updatecheck>\n"),
Darin Petkov95508da2011-01-05 12:42:29 -0800582 string::npos);
583 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
584 string::npos);
585 string prev_version_event = StringPrintf(
586 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
Darin Petkov5c0f36e2011-01-13 14:02:36 -0800587 "previousversion=\"1.2&gt;3.4\"></o:event>\n",
Darin Petkov95508da2011-01-05 12:42:29 -0800588 OmahaEvent::kTypeUpdateComplete,
589 OmahaEvent::kResultSuccessReboot);
590 EXPECT_NE(post_str.find(prev_version_event), string::npos);
591}
592
Darin Petkove17f86b2010-07-20 09:12:01 -0700593TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
594 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700595 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700596 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
597 "invalid xml>",
598 &post_data);
599 // convert post_data to string
600 string post_str(&post_data[0], post_data.size());
601 string expected_event = StringPrintf(
602 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
603 OmahaEvent::kTypeUpdateDownloadStarted,
604 OmahaEvent::kResultSuccess);
605 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700606 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700607 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
608}
609
610TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
611 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700612 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700613 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
614 OmahaEvent::kResultError,
615 kActionCodeError),
616 "invalid xml>",
617 &post_data);
618 // convert post_data to string
619 string post_str(&post_data[0], post_data.size());
620 string expected_event = StringPrintf(
621 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
622 "errorcode=\"%d\"></o:event>\n",
623 OmahaEvent::kTypeDownloadComplete,
624 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700625 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700626 EXPECT_NE(post_str.find(expected_event), string::npos);
627 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
628}
629
630TEST(OmahaRequestActionTest, IsEventTest) {
631 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800632 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700633 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700634 &prefs,
635 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700636 NULL,
637 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800638 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700639 NULL),
640 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700641 EXPECT_FALSE(update_check_action.IsEvent());
642
643 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700644 &prefs,
645 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700646 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700647 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800648 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700649 NULL),
650 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700651 EXPECT_TRUE(event_action.IsEvent());
652}
653
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700654TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
655 for (int i = 0; i < 2; i++) {
656 bool delta_okay = i == 1;
657 const char* delta_okay_str = delta_okay ? "true" : "false";
658 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700659 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700660 OmahaRequestParams::kOsVersion,
661 "service_pack",
662 "x86-generic",
663 OmahaRequestParams::kAppId,
664 "0.1.0.0",
665 "en-US",
666 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700667 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700668 delta_okay,
669 "http://url");
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700670 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
671 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700672 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700673 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700674 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700675 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700676 NULL,
677 &post_data));
678 // convert post_data to string
679 string post_str(&post_data[0], post_data.size());
680 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
681 string::npos)
682 << "i = " << i;
683 }
684}
685
Darin Petkove17f86b2010-07-20 09:12:01 -0700686TEST(OmahaRequestActionTest, OmahaEventTest) {
687 OmahaEvent default_event;
688 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
689 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
690 EXPECT_EQ(kActionCodeError, default_event.error_code);
691
692 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
693 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
694 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
695 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
696
697 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
698 OmahaEvent::kResultError,
699 kActionCodeError);
700 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
701 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
702 EXPECT_EQ(kActionCodeError, error_event.error_code);
703}
704
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700705TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -0700706 for (int ping_only = 0; ping_only < 2; ping_only++) {
707 NiceMock<PrefsMock> prefs;
708 // Add a few hours to the day difference to test no rounding, etc.
709 int64_t five_days_ago =
710 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
711 int64_t six_days_ago =
712 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
713 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
714 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
715 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
716 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
717 vector<char> post_data;
718 ASSERT_TRUE(
719 TestUpdateCheck(&prefs,
720 kDefaultTestParams,
721 GetNoUpdateResponse(OmahaRequestParams::kAppId),
722 -1,
723 ping_only,
724 kActionCodeSuccess,
725 NULL,
726 &post_data));
727 string post_str(&post_data[0], post_data.size());
728 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"),
729 string::npos);
730 if (ping_only) {
731 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
732 EXPECT_EQ(post_str.find("previousversion"), string::npos);
733 } else {
734 EXPECT_NE(post_str.find("o:updatecheck"), string::npos);
735 EXPECT_NE(post_str.find("previousversion"), string::npos);
736 }
737 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700738}
739
740TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800741 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700742 int64_t three_days_ago =
743 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
744 int64_t now = Time::Now().ToInternalValue();
745 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
746 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
747 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
748 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
749 vector<char> post_data;
750 ASSERT_TRUE(
751 TestUpdateCheck(&prefs,
752 kDefaultTestParams,
753 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700754 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700755 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700756 kActionCodeSuccess,
757 NULL,
758 &post_data));
759 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700760 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"3\"></o:ping>"),
761 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700762}
763
764TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800765 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700766 int64_t four_days_ago =
767 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
768 int64_t now = Time::Now().ToInternalValue();
769 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
770 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
771 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
772 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
773 vector<char> post_data;
774 ASSERT_TRUE(
775 TestUpdateCheck(&prefs,
776 kDefaultTestParams,
777 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700778 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700779 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700780 kActionCodeSuccess,
781 NULL,
782 &post_data));
783 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700784 EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"),
785 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700786}
787
788TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800789 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700790 int64_t one_hour_ago =
791 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
792 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
793 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
794 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
795 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
796 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
797 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
798 vector<char> post_data;
799 ASSERT_TRUE(
800 TestUpdateCheck(&prefs,
801 kDefaultTestParams,
802 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700803 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700804 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700805 kActionCodeSuccess,
806 NULL,
807 &post_data));
808 string post_str(&post_data[0], post_data.size());
809 EXPECT_EQ(post_str.find("o:ping"), string::npos);
810}
811
Thieu Leb44e9e82011-06-06 14:34:04 -0700812TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
813 // This test ensures that we ignore empty ping only requests.
814 NiceMock<PrefsMock> prefs;
815 int64_t now = Time::Now().ToInternalValue();
816 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
817 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
818 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
819 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
820 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
821 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
822 vector<char> post_data;
823 EXPECT_TRUE(
824 TestUpdateCheck(&prefs,
825 kDefaultTestParams,
826 GetNoUpdateResponse(OmahaRequestParams::kAppId),
827 -1,
828 true, // ping_only
829 kActionCodeSuccess,
830 NULL,
831 &post_data));
832 EXPECT_EQ(post_data.size(), 0);
833}
834
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700835TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800836 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700837 int64_t future =
838 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
839 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
840 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
841 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
842 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
843 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
844 .WillOnce(Return(true));
845 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
846 .WillOnce(Return(true));
847 vector<char> post_data;
848 ASSERT_TRUE(
849 TestUpdateCheck(&prefs,
850 kDefaultTestParams,
851 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
852 "xmlns=\"http://www.google.com/update2/response\" "
853 "protocol=\"2.0\"><daystart elapsed_seconds=\"100\"/>"
854 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
855 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700856 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700857 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700858 kActionCodeSuccess,
859 NULL,
860 &post_data));
861 string post_str(&post_data[0], post_data.size());
862 EXPECT_EQ(post_str.find("o:ping"), string::npos);
863}
864
865TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
866 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700867 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700868 // may fail if it runs for longer than 5 seconds. It shouldn't run
869 // that long though.
870 int64_t midnight =
871 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
872 int64_t midnight_slack =
873 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800874 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700875 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
876 AllOf(Ge(midnight), Le(midnight_slack))))
877 .WillOnce(Return(true));
878 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
879 AllOf(Ge(midnight), Le(midnight_slack))))
880 .WillOnce(Return(true));
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=\"200\"/>"
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 Petkov265f2902011-05-09 15:17:40 -0700890 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700891 kActionCodeSuccess,
892 NULL,
893 NULL));
894}
895
896TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800897 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700898 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
899 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
900 ASSERT_TRUE(
901 TestUpdateCheck(&prefs,
902 kDefaultTestParams,
903 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
904 "xmlns=\"http://www.google.com/update2/response\" "
905 "protocol=\"2.0\"><daystart blah=\"200\"/>"
906 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
907 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700908 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700909 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700910 kActionCodeSuccess,
911 NULL,
912 NULL));
913}
914
915TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800916 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700917 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
918 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
919 ASSERT_TRUE(
920 TestUpdateCheck(&prefs,
921 kDefaultTestParams,
922 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
923 "xmlns=\"http://www.google.com/update2/response\" "
924 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
925 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
926 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700927 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700928 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700929 kActionCodeSuccess,
930 NULL,
931 NULL));
932}
933
Darin Petkov84c763c2010-07-29 16:27:58 -0700934TEST(OmahaRequestActionTest, NoUniqueIDTest) {
935 vector<char> post_data;
936 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
937 kDefaultTestParams,
938 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700939 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700940 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700941 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700942 NULL, // response
943 &post_data));
944 // convert post_data to string
945 string post_str(&post_data[0], post_data.size());
946 EXPECT_EQ(post_str.find("machineid="), string::npos);
947 EXPECT_EQ(post_str.find("userid="), string::npos);
948}
949
Darin Petkovedc522e2010-11-05 09:35:17 -0700950TEST(OmahaRequestActionTest, NetworkFailureTest) {
951 OmahaResponse response;
952 ASSERT_FALSE(
953 TestUpdateCheck(NULL, // prefs
954 kDefaultTestParams,
955 "",
956 501,
Darin Petkov265f2902011-05-09 15:17:40 -0700957 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700958 static_cast<ActionExitCode>(
959 kActionCodeOmahaRequestHTTPResponseBase + 501),
960 &response,
961 NULL));
962 EXPECT_FALSE(response.update_exists);
963}
964
965TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
966 OmahaResponse response;
967 ASSERT_FALSE(
968 TestUpdateCheck(NULL, // prefs
969 kDefaultTestParams,
970 "",
971 1500,
Darin Petkov265f2902011-05-09 15:17:40 -0700972 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700973 static_cast<ActionExitCode>(
974 kActionCodeOmahaRequestHTTPResponseBase + 999),
975 &response,
976 NULL));
977 EXPECT_FALSE(response.update_exists);
978}
979
Darin Petkov6a5b3222010-07-13 14:55:28 -0700980} // namespace chromeos_update_engine