blob: e4ab7290025ad625abb4f50299dd295ee4cbb7e4 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 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"
Mike Frysinger8155d082012-04-06 15:23:18 -040011#include <base/stringprintf.h>
Darin Petkov1cbd78f2010-07-29 12:38:34 -070012#include "base/time.h"
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070013#include "gtest/gtest.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070014#include "update_engine/action_pipe.h"
15#include "update_engine/mock_http_fetcher.h"
16#include "update_engine/omaha_hash_calculator.h"
17#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070018#include "update_engine/omaha_request_params.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070019#include "update_engine/prefs_mock.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070020#include "update_engine/test_utils.h"
21
Darin Petkov1cbd78f2010-07-29 12:38:34 -070022using base::Time;
23using base::TimeDelta;
Darin Petkov6a5b3222010-07-13 14:55:28 -070024using std::string;
25using std::vector;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070026using testing::_;
27using testing::AllOf;
28using testing::Ge;
29using testing::Le;
Darin Petkov9c096d62010-11-17 14:49:04 -080030using testing::NiceMock;
Darin Petkov1cbd78f2010-07-29 12:38:34 -070031using testing::Return;
32using testing::SetArgumentPointee;
Darin Petkov6a5b3222010-07-13 14:55:28 -070033
34namespace chromeos_update_engine {
35
36class OmahaRequestActionTest : public ::testing::Test { };
37
38namespace {
Darin Petkov1cbd78f2010-07-29 12:38:34 -070039const OmahaRequestParams kDefaultTestParams(
Darin Petkov1cbd78f2010-07-29 12:38:34 -070040 OmahaRequestParams::kOsPlatform,
41 OmahaRequestParams::kOsVersion,
42 "service_pack",
43 "x86-generic",
44 OmahaRequestParams::kAppId,
45 "0.1.0.0",
46 "en-US",
47 "unittest",
Darin Petkovfbb40092010-07-29 17:05:50 -070048 "OEM MODEL 09235 7471",
Darin Petkov1cbd78f2010-07-29 12:38:34 -070049 false, // delta okay
Jay Srinivasan0a708742012-03-20 11:26:12 -070050 "http://url",
51 false, // update_disabled
52 ""); // target_version_prefix
Darin Petkov1cbd78f2010-07-29 12:38:34 -070053
Darin Petkov6a5b3222010-07-13 14:55:28 -070054string GetNoUpdateResponse(const string& app_id) {
55 return string(
56 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
57 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
58 "appid=\"") + app_id + "\" status=\"ok\"><ping "
59 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
60}
61
62string GetUpdateResponse(const string& app_id,
63 const string& display_version,
64 const string& more_info_url,
65 const string& prompt,
66 const string& codebase,
67 const string& hash,
68 const string& needsadmin,
Darin Petkov6c118642010-10-21 12:06:30 -070069 const string& size,
70 const string& deadline) {
Jay Srinivasan0a708742012-03-20 11:26:12 -070071 return string(
72 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
73 "xmlns=\"http://www.google.com/update2/response\" "
74 "protocol=\"2.0\"><app "
75 "appid=\"") + app_id + "\" status=\"ok\"><ping "
Darin Petkov6a5b3222010-07-13 14:55:28 -070076 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
Jay Srinivasan0a708742012-03-20 11:26:12 -070077 "ChromeOSVersion=\"" + display_version + "\" "
Darin Petkov6a5b3222010-07-13 14:55:28 -070078 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070079 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -070080 "codebase=\"" + codebase + "\" hash=\"not-applicable\" "
81 "sha256=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
Darin Petkov6c118642010-10-21 12:06:30 -070082 "size=\"" + size + "\" deadline=\"" + deadline +
83 "\" status=\"ok\"/></app></gupdate>";
Darin Petkov6a5b3222010-07-13 14:55:28 -070084}
85
86class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
87 public:
88 OmahaRequestActionTestProcessorDelegate()
89 : loop_(NULL),
Darin Petkovc1a8b422010-07-19 11:34:49 -070090 expected_code_(kActionCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070091 virtual ~OmahaRequestActionTestProcessorDelegate() {
92 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070093 virtual void ProcessingDone(const ActionProcessor* processor,
94 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070095 ASSERT_TRUE(loop_);
96 g_main_loop_quit(loop_);
97 }
98
99 virtual void ActionCompleted(ActionProcessor* processor,
100 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700101 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700102 // make sure actions always succeed
103 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -0700104 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105 else
Darin Petkovc1a8b422010-07-19 11:34:49 -0700106 EXPECT_EQ(kActionCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700107 }
108 GMainLoop *loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700109 ActionExitCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700110};
111
112gboolean StartProcessorInRunLoop(gpointer data) {
113 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
114 processor->StartProcessing();
115 return FALSE;
116}
Darin Petkov6a5b3222010-07-13 14:55:28 -0700117} // namespace {}
118
119class OutputObjectCollectorAction;
120
121template<>
122class ActionTraits<OutputObjectCollectorAction> {
123 public:
124 // Does not take an object for input
125 typedef OmahaResponse InputObjectType;
126 // On success, puts the output path on output
127 typedef NoneType OutputObjectType;
128};
129
130class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
131 public:
132 OutputObjectCollectorAction() : has_input_object_(false) {}
133 void PerformAction() {
134 // copy input object
135 has_input_object_ = HasInputObject();
136 if (has_input_object_)
137 omaha_response_ = GetInputObject();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700138 processor_->ActionComplete(this, kActionCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700139 }
140 // Should never be called
141 void TerminateProcessing() {
142 CHECK(false);
143 }
144 // Debugging/logging
145 static std::string StaticType() {
146 return "OutputObjectCollectorAction";
147 }
148 std::string Type() const { return StaticType(); }
149 bool has_input_object_;
150 OmahaResponse omaha_response_;
151};
152
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700153// Returns true iff an output response was obtained from the
Darin Petkovedc522e2010-11-05 09:35:17 -0700154// OmahaRequestAction. |prefs| may be NULL, in which case a local PrefsMock is
Darin Petkov265f2902011-05-09 15:17:40 -0700155// used. out_response may be NULL. If |fail_http_response_code| is non-negative,
156// the transfer will fail with that code. |ping_only| is passed through to the
157// OmahaRequestAction constructor. out_post_data may be null; if non-null, the
158// post-data received by the mock HttpFetcher is returned.
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700159bool TestUpdateCheck(PrefsInterface* prefs,
160 const OmahaRequestParams& params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700161 const string& http_response,
Darin Petkovedc522e2010-11-05 09:35:17 -0700162 int fail_http_response_code,
Darin Petkov265f2902011-05-09 15:17:40 -0700163 bool ping_only,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700164 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700165 OmahaResponse* out_response,
166 vector<char>* out_post_data) {
167 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
168 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800169 http_response.size(),
170 NULL);
Darin Petkovedc522e2010-11-05 09:35:17 -0700171 if (fail_http_response_code >= 0) {
172 fetcher->FailTransfer(fail_http_response_code);
173 }
Darin Petkov9c096d62010-11-17 14:49:04 -0800174 NiceMock<PrefsMock> local_prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700175 OmahaRequestAction action(prefs ? prefs : &local_prefs,
176 params,
177 NULL,
Thieu Le116fda32011-04-19 11:01:54 -0700178 fetcher,
Darin Petkov265f2902011-05-09 15:17:40 -0700179 ping_only);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700180 OmahaRequestActionTestProcessorDelegate delegate;
181 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700182 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700183
Darin Petkov6a5b3222010-07-13 14:55:28 -0700184 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700185 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700186 processor.EnqueueAction(&action);
187
188 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700189 BondActions(&action, &collector_action);
190 processor.EnqueueAction(&collector_action);
191
192 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
193 g_main_loop_run(loop);
194 g_main_loop_unref(loop);
195 if (collector_action.has_input_object_ && out_response)
196 *out_response = collector_action.omaha_response_;
197 if (out_post_data)
198 *out_post_data = fetcher->post_data();
199 return collector_action.has_input_object_;
200}
201
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700202// Tests Event requests -- they should always succeed. |out_post_data|
203// may be null; if non-null, the post-data received by the mock
204// HttpFetcher is returned.
205void TestEvent(const OmahaRequestParams& params,
206 OmahaEvent* event,
207 const string& http_response,
208 vector<char>* out_post_data) {
209 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
210 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800211 http_response.size(),
212 NULL);
Darin Petkov9c096d62010-11-17 14:49:04 -0800213 NiceMock<PrefsMock> prefs;
Thieu Le116fda32011-04-19 11:01:54 -0700214 OmahaRequestAction action(&prefs, params, event, fetcher, false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700215 OmahaRequestActionTestProcessorDelegate delegate;
216 delegate.loop_ = loop;
217 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700218 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700219 processor.EnqueueAction(&action);
220
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700221 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
222 g_main_loop_run(loop);
223 g_main_loop_unref(loop);
224 if (out_post_data)
225 *out_post_data = fetcher->post_data();
226}
227
Darin Petkov6a5b3222010-07-13 14:55:28 -0700228TEST(OmahaRequestActionTest, NoUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700229 OmahaResponse response;
230 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700231 TestUpdateCheck(NULL, // prefs
232 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700233 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700234 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700235 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700236 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700237 &response,
238 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700239 EXPECT_FALSE(response.update_exists);
240}
241
242TEST(OmahaRequestActionTest, ValidUpdateTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700243 OmahaResponse response;
244 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700245 TestUpdateCheck(NULL, // prefs
246 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700247 GetUpdateResponse(OmahaRequestParams::kAppId,
248 "1.2.3.4", // version
249 "http://more/info",
250 "true", // prompt
251 "http://code/base", // dl url
252 "HASH1234=", // checksum
253 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700254 "123", // size
255 "20101020"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700256 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700257 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700258 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700259 &response,
260 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700261 EXPECT_TRUE(response.update_exists);
262 EXPECT_EQ("1.2.3.4", response.display_version);
263 EXPECT_EQ("http://code/base", response.codebase);
264 EXPECT_EQ("http://more/info", response.more_info_url);
265 EXPECT_EQ("HASH1234=", response.hash);
266 EXPECT_EQ(123, response.size);
267 EXPECT_FALSE(response.needs_admin);
268 EXPECT_TRUE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700269 EXPECT_EQ("20101020", response.deadline);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700270}
271
Jay Srinivasan0a708742012-03-20 11:26:12 -0700272TEST(OmahaRequestActionTest, ValidUpdateBlockedByPolicyTest) {
273 OmahaResponse response;
274 OmahaRequestParams params = kDefaultTestParams;
275 params.update_disabled = true;
276 ASSERT_FALSE(
277 TestUpdateCheck(NULL, // prefs
278 params,
279 GetUpdateResponse(OmahaRequestParams::kAppId,
280 "1.2.3.4", // version
281 "http://more/info",
282 "true", // prompt
283 "http://code/base", // dl url
284 "HASH1234=", // checksum
285 "false", // needs admin
286 "123", // size
287 "20101020"), // deadline
288 -1,
289 false, // ping_only
290 kActionCodeOmahaUpdateIgnoredPerPolicy,
291 &response,
292 NULL));
293 EXPECT_FALSE(response.update_exists);
294}
295
296
297TEST(OmahaRequestActionTest, NoUpdatesSentWhenBlockedByPolicyTest) {
298 OmahaResponse response;
299 OmahaRequestParams params = kDefaultTestParams;
300 params.update_disabled = true;
301 ASSERT_TRUE(
302 TestUpdateCheck(NULL, // prefs
303 params,
304 GetNoUpdateResponse(OmahaRequestParams::kAppId),
305 -1,
306 false, // ping_only
307 kActionCodeSuccess,
308 &response,
309 NULL));
310 EXPECT_FALSE(response.update_exists);
311}
312
313
Darin Petkov6a5b3222010-07-13 14:55:28 -0700314TEST(OmahaRequestActionTest, NoOutputPipeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700315 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
316
317 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
318
Darin Petkov9c096d62010-11-17 14:49:04 -0800319 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700320 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700321 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800322 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700323 NULL),
324 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700325 OmahaRequestActionTestProcessorDelegate delegate;
326 delegate.loop_ = loop;
327 ActionProcessor processor;
328 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700329 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700330
331 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
332 g_main_loop_run(loop);
333 g_main_loop_unref(loop);
334 EXPECT_FALSE(processor.IsRunning());
335}
336
337TEST(OmahaRequestActionTest, InvalidXmlTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700338 OmahaResponse response;
339 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700340 TestUpdateCheck(NULL, // prefs
341 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700342 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700343 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700344 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700345 kActionCodeOmahaRequestXMLParseError,
346 &response,
347 NULL));
348 EXPECT_FALSE(response.update_exists);
349}
350
351TEST(OmahaRequestActionTest, EmptyResponseTest) {
352 OmahaResponse response;
353 ASSERT_FALSE(
354 TestUpdateCheck(NULL, // prefs
355 kDefaultTestParams,
356 "",
357 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700358 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700359 kActionCodeOmahaRequestEmptyResponseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700360 &response,
361 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700362 EXPECT_FALSE(response.update_exists);
363}
364
365TEST(OmahaRequestActionTest, MissingStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700366 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700367 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700368 NULL, // prefs
369 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700370 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
371 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
372 "appid=\"foo\" status=\"ok\"><ping "
373 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700374 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700375 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700376 kActionCodeOmahaRequestNoUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700377 &response,
378 NULL));
379 EXPECT_FALSE(response.update_exists);
380}
381
382TEST(OmahaRequestActionTest, InvalidStatusTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700383 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700384 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700385 NULL, // prefs
386 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700387 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
388 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
389 "appid=\"foo\" status=\"ok\"><ping "
390 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700391 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700392 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700393 kActionCodeOmahaRequestBadUpdateCheckStatus,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700394 &response,
395 NULL));
396 EXPECT_FALSE(response.update_exists);
397}
398
399TEST(OmahaRequestActionTest, MissingNodesetTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700400 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700401 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700402 NULL, // prefs
403 kDefaultTestParams,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700404 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
405 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
406 "appid=\"foo\" status=\"ok\"><ping "
407 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700408 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700409 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700410 kActionCodeOmahaRequestNoUpdateCheckNode,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700411 &response,
412 NULL));
413 EXPECT_FALSE(response.update_exists);
414}
415
416TEST(OmahaRequestActionTest, MissingFieldTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700417 OmahaResponse response;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700418 ASSERT_TRUE(TestUpdateCheck(NULL, // prefs
419 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700420 string("<?xml version=\"1.0\" "
421 "encoding=\"UTF-8\"?><gupdate "
422 "xmlns=\"http://www.google.com/"
423 "update2/response\" "
424 "protocol=\"2.0\"><app appid=\"") +
425 OmahaRequestParams::kAppId
426 + "\" status=\"ok\"><ping "
427 "status=\"ok\"/><updatecheck "
428 "DisplayVersion=\"1.2.3.4\" "
Jay Srinivasan0a708742012-03-20 11:26:12 -0700429 "ChromeOSVersion=\"1.2.3.4\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700430 "Prompt=\"false\" "
Darin Petkov7ed561b2011-10-04 02:59:03 -0700431 "IsDelta=\"true\" "
Darin Petkovd22cb292010-09-29 10:02:29 -0700432 "codebase=\"http://code/base\" hash=\"foo\" "
433 "sha256=\"HASH1234=\" needsadmin=\"true\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700434 "size=\"123\" "
435 "status=\"ok\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700436 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700437 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700438 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700439 &response,
440 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700441 EXPECT_TRUE(response.update_exists);
442 EXPECT_EQ("1.2.3.4", response.display_version);
443 EXPECT_EQ("http://code/base", response.codebase);
444 EXPECT_EQ("", response.more_info_url);
445 EXPECT_EQ("HASH1234=", response.hash);
446 EXPECT_EQ(123, response.size);
447 EXPECT_TRUE(response.needs_admin);
448 EXPECT_FALSE(response.prompt);
Darin Petkov6c118642010-10-21 12:06:30 -0700449 EXPECT_TRUE(response.deadline.empty());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700450}
451
452namespace {
453class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
454 public:
455 void ProcessingStopped(const ActionProcessor* processor) {
456 ASSERT_TRUE(loop_);
457 g_main_loop_quit(loop_);
458 }
459 GMainLoop *loop_;
460};
461
462gboolean TerminateTransferTestStarter(gpointer data) {
463 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
464 processor->StartProcessing();
465 CHECK(processor->IsRunning());
466 processor->StopProcessing();
467 return FALSE;
468}
469} // namespace {}
470
471TEST(OmahaRequestActionTest, TerminateTransferTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700472 string http_response("doesn't matter");
473 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
474
Darin Petkov9c096d62010-11-17 14:49:04 -0800475 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700476 OmahaRequestAction action(&prefs, kDefaultTestParams, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700477 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800478 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700479 NULL),
480 false);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700481 TerminateEarlyTestProcessorDelegate delegate;
482 delegate.loop_ = loop;
483 ActionProcessor processor;
484 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700485 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700486
487 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
488 g_main_loop_run(loop);
489 g_main_loop_unref(loop);
490}
491
492TEST(OmahaRequestActionTest, XmlEncodeTest) {
493 EXPECT_EQ("ab", XmlEncode("ab"));
494 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
495 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
496 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
497 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
498
499 vector<char> post_data;
500
501 // Make sure XML Encode is being called on the params
Darin Petkov84c763c2010-07-29 16:27:58 -0700502 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700503 OmahaRequestParams::kOsVersion,
504 "testtheservice_pack>",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700505 "x86 generic<id",
Darin Petkov6a5b3222010-07-13 14:55:28 -0700506 OmahaRequestParams::kAppId,
507 "0.1.0.0",
508 "en-US",
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700509 "unittest_track&lt;",
Darin Petkovfbb40092010-07-29 17:05:50 -0700510 "<OEM MODEL>",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700511 false, // delta okay
Jay Srinivasan0a708742012-03-20 11:26:12 -0700512 "http://url",
513 false, // update_disabled
514 ""); // target_version_prefix
Darin Petkov6a5b3222010-07-13 14:55:28 -0700515 OmahaResponse response;
516 ASSERT_FALSE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700517 TestUpdateCheck(NULL, // prefs
518 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700519 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700520 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700521 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700522 kActionCodeOmahaRequestXMLParseError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700523 &response,
524 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700525 // convert post_data to string
526 string post_str(&post_data[0], post_data.size());
Darin Petkov6a5b3222010-07-13 14:55:28 -0700527 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
528 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700529 EXPECT_NE(post_str.find("x86 generic&lt;id"), string::npos);
530 EXPECT_EQ(post_str.find("x86 generic<id"), string::npos);
531 EXPECT_NE(post_str.find("unittest_track&amp;lt;"), string::npos);
532 EXPECT_EQ(post_str.find("unittest_track&lt;"), string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700533 EXPECT_NE(post_str.find("&lt;OEM MODEL&gt;"), string::npos);
534 EXPECT_EQ(post_str.find("<OEM MODEL>"), string::npos);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700535}
536
537TEST(OmahaRequestActionTest, XmlDecodeTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700538 OmahaResponse response;
539 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700540 TestUpdateCheck(NULL, // prefs
541 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700542 GetUpdateResponse(OmahaRequestParams::kAppId,
543 "1.2.3.4", // version
544 "testthe&lt;url", // more info
545 "true", // prompt
546 "testthe&amp;codebase", // dl url
547 "HASH1234=", // checksum
548 "false", // needs admin
Darin Petkov6c118642010-10-21 12:06:30 -0700549 "123", // size
550 "&lt;20110101"), // deadline
Darin Petkovedc522e2010-11-05 09:35:17 -0700551 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700552 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700553 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700554 &response,
555 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700556
557 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700558 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6c118642010-10-21 12:06:30 -0700559 EXPECT_EQ(response.deadline, "<20110101");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700560}
561
562TEST(OmahaRequestActionTest, ParseIntTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700563 OmahaResponse response;
564 ASSERT_TRUE(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700565 TestUpdateCheck(NULL, // prefs
566 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700567 GetUpdateResponse(OmahaRequestParams::kAppId,
568 "1.2.3.4", // version
569 "theurl", // more info
570 "true", // prompt
571 "thecodebase", // dl url
572 "HASH1234=", // checksum
573 "false", // needs admin
574 // overflows int32:
Darin Petkov6c118642010-10-21 12:06:30 -0700575 "123123123123123", // size
576 "deadline"),
Darin Petkovedc522e2010-11-05 09:35:17 -0700577 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700578 false, // ping_only
Darin Petkovc1a8b422010-07-19 11:34:49 -0700579 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700580 &response,
581 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700582
583 EXPECT_EQ(response.size, 123123123123123ll);
584}
585
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700586TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
587 vector<char> post_data;
Darin Petkov95508da2011-01-05 12:42:29 -0800588 NiceMock<PrefsMock> prefs;
589 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
590 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
591 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
592 ASSERT_FALSE(TestUpdateCheck(&prefs,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700593 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700594 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700595 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700596 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700597 kActionCodeOmahaRequestXMLParseError,
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700598 NULL, // response
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700599 &post_data));
600 // convert post_data to string
601 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700602 EXPECT_NE(post_str.find(
Jay Srinivasan0a708742012-03-20 11:26:12 -0700603 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
604 " <o:updatecheck"
Jay Srinivasan0a708742012-03-20 11:26:12 -0700605 " targetversionprefix=\"\""
606 "></o:updatecheck>\n"),
607 string::npos);
Darin Petkovfbb40092010-07-29 17:05:50 -0700608 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
609 string::npos);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700610 EXPECT_EQ(post_str.find("o:event"), string::npos);
611}
612
Jay Srinivasan0a708742012-03-20 11:26:12 -0700613
Jay Srinivasan56d5aa42012-03-26 14:27:59 -0700614TEST(OmahaRequestActionTest, FormatTargetVersionPrefixTest) {
Darin Petkov95508da2011-01-05 12:42:29 -0800615 vector<char> post_data;
616 NiceMock<PrefsMock> prefs;
617 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _))
Jay Srinivasan0a708742012-03-20 11:26:12 -0700618 .WillOnce(DoAll(SetArgumentPointee<1>(string("")), Return(true)));
619 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0);
620 OmahaRequestParams params = kDefaultTestParams;
621 params.update_disabled = true;
Darin Petkov95508da2011-01-05 12:42:29 -0800622 ASSERT_FALSE(TestUpdateCheck(&prefs,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700623 params,
Darin Petkov95508da2011-01-05 12:42:29 -0800624 "invalid xml>",
625 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700626 false, // ping_only
Darin Petkov95508da2011-01-05 12:42:29 -0800627 kActionCodeOmahaRequestXMLParseError,
628 NULL, // response
629 &post_data));
630 // convert post_data to string
631 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700632 EXPECT_NE(post_str.find(
Jay Srinivasan0a708742012-03-20 11:26:12 -0700633 " <o:ping active=\"1\" a=\"-1\" r=\"-1\"></o:ping>\n"
634 " <o:updatecheck"
Jay Srinivasan0a708742012-03-20 11:26:12 -0700635 " targetversionprefix=\"\""
636 "></o:updatecheck>\n"),
637 string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -0800638 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""),
639 string::npos);
Jay Srinivasan0a708742012-03-20 11:26:12 -0700640 EXPECT_EQ(post_str.find("o:event"), string::npos);
Darin Petkov95508da2011-01-05 12:42:29 -0800641}
642
Darin Petkove17f86b2010-07-20 09:12:01 -0700643TEST(OmahaRequestActionTest, FormatSuccessEventOutputTest) {
644 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700645 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700646 new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted),
647 "invalid xml>",
648 &post_data);
649 // convert post_data to string
650 string post_str(&post_data[0], post_data.size());
651 string expected_event = StringPrintf(
652 " <o:event eventtype=\"%d\" eventresult=\"%d\"></o:event>\n",
653 OmahaEvent::kTypeUpdateDownloadStarted,
654 OmahaEvent::kResultSuccess);
655 EXPECT_NE(post_str.find(expected_event), string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700656 EXPECT_EQ(post_str.find("o:ping"), string::npos);
Darin Petkove17f86b2010-07-20 09:12:01 -0700657 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
658}
659
660TEST(OmahaRequestActionTest, FormatErrorEventOutputTest) {
661 vector<char> post_data;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700662 TestEvent(kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700663 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
664 OmahaEvent::kResultError,
665 kActionCodeError),
666 "invalid xml>",
667 &post_data);
668 // convert post_data to string
669 string post_str(&post_data[0], post_data.size());
670 string expected_event = StringPrintf(
671 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
672 "errorcode=\"%d\"></o:event>\n",
673 OmahaEvent::kTypeDownloadComplete,
674 OmahaEvent::kResultError,
Darin Petkov44d98d92011-03-21 16:08:11 -0700675 kActionCodeError);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700676 EXPECT_NE(post_str.find(expected_event), string::npos);
677 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
678}
679
680TEST(OmahaRequestActionTest, IsEventTest) {
681 string http_response("doesn't matter");
Darin Petkov9c096d62010-11-17 14:49:04 -0800682 NiceMock<PrefsMock> prefs;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700683 OmahaRequestAction update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700684 &prefs,
685 kDefaultTestParams,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700686 NULL,
687 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800688 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700689 NULL),
690 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700691 EXPECT_FALSE(update_check_action.IsEvent());
692
693 OmahaRequestAction event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700694 &prefs,
695 kDefaultTestParams,
Darin Petkove17f86b2010-07-20 09:12:01 -0700696 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700697 new MockHttpFetcher(http_response.data(),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800698 http_response.size(),
Thieu Le116fda32011-04-19 11:01:54 -0700699 NULL),
700 false);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700701 EXPECT_TRUE(event_action.IsEvent());
702}
703
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700704TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
705 for (int i = 0; i < 2; i++) {
706 bool delta_okay = i == 1;
707 const char* delta_okay_str = delta_okay ? "true" : "false";
708 vector<char> post_data;
Darin Petkov84c763c2010-07-29 16:27:58 -0700709 OmahaRequestParams params(OmahaRequestParams::kOsPlatform,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700710 OmahaRequestParams::kOsVersion,
711 "service_pack",
712 "x86-generic",
713 OmahaRequestParams::kAppId,
714 "0.1.0.0",
715 "en-US",
716 "unittest_track",
Darin Petkovfbb40092010-07-29 17:05:50 -0700717 "OEM MODEL REV 1234",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700718 delta_okay,
Jay Srinivasan0a708742012-03-20 11:26:12 -0700719 "http://url",
720 false, // update_disabled
721 ""); // target_version_prefix
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700722 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
723 params,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700724 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700725 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700726 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700727 kActionCodeOmahaRequestXMLParseError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700728 NULL,
729 &post_data));
730 // convert post_data to string
731 string post_str(&post_data[0], post_data.size());
732 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
733 string::npos)
734 << "i = " << i;
735 }
736}
737
Darin Petkove17f86b2010-07-20 09:12:01 -0700738TEST(OmahaRequestActionTest, OmahaEventTest) {
739 OmahaEvent default_event;
740 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type);
741 EXPECT_EQ(OmahaEvent::kResultError, default_event.result);
742 EXPECT_EQ(kActionCodeError, default_event.error_code);
743
744 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted);
745 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type);
746 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result);
747 EXPECT_EQ(kActionCodeSuccess, success_event.error_code);
748
749 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished,
750 OmahaEvent::kResultError,
751 kActionCodeError);
752 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type);
753 EXPECT_EQ(OmahaEvent::kResultError, error_event.result);
754 EXPECT_EQ(kActionCodeError, error_event.error_code);
755}
756
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700757TEST(OmahaRequestActionTest, PingTest) {
Darin Petkov265f2902011-05-09 15:17:40 -0700758 for (int ping_only = 0; ping_only < 2; ping_only++) {
759 NiceMock<PrefsMock> prefs;
760 // Add a few hours to the day difference to test no rounding, etc.
761 int64_t five_days_ago =
762 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue();
763 int64_t six_days_ago =
764 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue();
765 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
766 .WillOnce(DoAll(SetArgumentPointee<1>(six_days_ago), Return(true)));
767 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
768 .WillOnce(DoAll(SetArgumentPointee<1>(five_days_ago), Return(true)));
769 vector<char> post_data;
770 ASSERT_TRUE(
771 TestUpdateCheck(&prefs,
772 kDefaultTestParams,
773 GetNoUpdateResponse(OmahaRequestParams::kAppId),
774 -1,
775 ping_only,
776 kActionCodeSuccess,
777 NULL,
778 &post_data));
779 string post_str(&post_data[0], post_data.size());
780 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"6\" r=\"5\"></o:ping>"),
781 string::npos);
782 if (ping_only) {
783 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
784 EXPECT_EQ(post_str.find("previousversion"), string::npos);
785 } else {
786 EXPECT_NE(post_str.find("o:updatecheck"), string::npos);
787 EXPECT_NE(post_str.find("previousversion"), string::npos);
788 }
789 }
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700790}
791
792TEST(OmahaRequestActionTest, ActivePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800793 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700794 int64_t three_days_ago =
795 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue();
796 int64_t now = Time::Now().ToInternalValue();
797 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
798 .WillOnce(DoAll(SetArgumentPointee<1>(three_days_ago), Return(true)));
799 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
800 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
801 vector<char> post_data;
802 ASSERT_TRUE(
803 TestUpdateCheck(&prefs,
804 kDefaultTestParams,
805 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700806 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700807 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700808 kActionCodeSuccess,
809 NULL,
810 &post_data));
811 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700812 EXPECT_NE(post_str.find("<o:ping active=\"1\" a=\"3\"></o:ping>"),
813 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700814}
815
816TEST(OmahaRequestActionTest, RollCallPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800817 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700818 int64_t four_days_ago =
819 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue();
820 int64_t now = Time::Now().ToInternalValue();
821 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
822 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
823 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
824 .WillOnce(DoAll(SetArgumentPointee<1>(four_days_ago), Return(true)));
825 vector<char> post_data;
826 ASSERT_TRUE(
827 TestUpdateCheck(&prefs,
828 kDefaultTestParams,
829 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700830 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700831 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700832 kActionCodeSuccess,
833 NULL,
834 &post_data));
835 string post_str(&post_data[0], post_data.size());
Thieu Le116fda32011-04-19 11:01:54 -0700836 EXPECT_NE(post_str.find("<o:ping active=\"1\" r=\"4\"></o:ping>\n"),
837 string::npos);
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700838}
839
840TEST(OmahaRequestActionTest, NoPingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800841 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700842 int64_t one_hour_ago =
843 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue();
844 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
845 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
846 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
847 .WillOnce(DoAll(SetArgumentPointee<1>(one_hour_ago), Return(true)));
848 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
849 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
850 vector<char> post_data;
851 ASSERT_TRUE(
852 TestUpdateCheck(&prefs,
853 kDefaultTestParams,
854 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovedc522e2010-11-05 09:35:17 -0700855 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700856 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700857 kActionCodeSuccess,
858 NULL,
859 &post_data));
860 string post_str(&post_data[0], post_data.size());
861 EXPECT_EQ(post_str.find("o:ping"), string::npos);
862}
863
Thieu Leb44e9e82011-06-06 14:34:04 -0700864TEST(OmahaRequestActionTest, IgnoreEmptyPingTest) {
865 // This test ensures that we ignore empty ping only requests.
866 NiceMock<PrefsMock> prefs;
867 int64_t now = Time::Now().ToInternalValue();
868 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
869 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
870 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
871 .WillOnce(DoAll(SetArgumentPointee<1>(now), Return(true)));
872 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
873 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
874 vector<char> post_data;
875 EXPECT_TRUE(
876 TestUpdateCheck(&prefs,
877 kDefaultTestParams,
878 GetNoUpdateResponse(OmahaRequestParams::kAppId),
879 -1,
880 true, // ping_only
881 kActionCodeSuccess,
882 NULL,
883 &post_data));
884 EXPECT_EQ(post_data.size(), 0);
885}
886
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700887TEST(OmahaRequestActionTest, BackInTimePingTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800888 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700889 int64_t future =
890 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue();
891 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _))
892 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
893 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _))
894 .WillOnce(DoAll(SetArgumentPointee<1>(future), Return(true)));
895 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _))
896 .WillOnce(Return(true));
897 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _))
898 .WillOnce(Return(true));
899 vector<char> post_data;
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 elapsed_seconds=\"100\"/>"
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 &post_data));
913 string post_str(&post_data[0], post_data.size());
914 EXPECT_EQ(post_str.find("o:ping"), string::npos);
915}
916
917TEST(OmahaRequestActionTest, LastPingDayUpdateTest) {
918 // This test checks that the action updates the last ping day to now
Darin Petkov84c763c2010-07-29 16:27:58 -0700919 // minus 200 seconds with a slack of 5 seconds. Therefore, the test
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700920 // may fail if it runs for longer than 5 seconds. It shouldn't run
921 // that long though.
922 int64_t midnight =
923 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue();
924 int64_t midnight_slack =
925 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue();
Darin Petkov9c096d62010-11-17 14:49:04 -0800926 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700927 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay,
928 AllOf(Ge(midnight), Le(midnight_slack))))
929 .WillOnce(Return(true));
930 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay,
931 AllOf(Ge(midnight), Le(midnight_slack))))
932 .WillOnce(Return(true));
933 ASSERT_TRUE(
934 TestUpdateCheck(&prefs,
935 kDefaultTestParams,
936 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
937 "xmlns=\"http://www.google.com/update2/response\" "
938 "protocol=\"2.0\"><daystart elapsed_seconds=\"200\"/>"
939 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
940 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700941 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700942 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700943 kActionCodeSuccess,
944 NULL,
945 NULL));
946}
947
948TEST(OmahaRequestActionTest, NoElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800949 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700950 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
951 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
952 ASSERT_TRUE(
953 TestUpdateCheck(&prefs,
954 kDefaultTestParams,
955 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
956 "xmlns=\"http://www.google.com/update2/response\" "
957 "protocol=\"2.0\"><daystart blah=\"200\"/>"
958 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
959 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700960 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700961 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700962 kActionCodeSuccess,
963 NULL,
964 NULL));
965}
966
967TEST(OmahaRequestActionTest, BadElapsedSecondsTest) {
Darin Petkov9c096d62010-11-17 14:49:04 -0800968 NiceMock<PrefsMock> prefs;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700969 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0);
970 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0);
971 ASSERT_TRUE(
972 TestUpdateCheck(&prefs,
973 kDefaultTestParams,
974 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
975 "xmlns=\"http://www.google.com/update2/response\" "
976 "protocol=\"2.0\"><daystart elapsed_seconds=\"x\"/>"
977 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>"
978 "<updatecheck status=\"noupdate\"/></app></gupdate>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700979 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700980 false, // ping_only
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700981 kActionCodeSuccess,
982 NULL,
983 NULL));
984}
985
Darin Petkov84c763c2010-07-29 16:27:58 -0700986TEST(OmahaRequestActionTest, NoUniqueIDTest) {
987 vector<char> post_data;
988 ASSERT_FALSE(TestUpdateCheck(NULL, // prefs
989 kDefaultTestParams,
990 "invalid xml>",
Darin Petkovedc522e2010-11-05 09:35:17 -0700991 -1,
Darin Petkov265f2902011-05-09 15:17:40 -0700992 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -0700993 kActionCodeOmahaRequestXMLParseError,
Darin Petkov84c763c2010-07-29 16:27:58 -0700994 NULL, // response
995 &post_data));
996 // convert post_data to string
997 string post_str(&post_data[0], post_data.size());
998 EXPECT_EQ(post_str.find("machineid="), string::npos);
999 EXPECT_EQ(post_str.find("userid="), string::npos);
1000}
1001
Darin Petkovedc522e2010-11-05 09:35:17 -07001002TEST(OmahaRequestActionTest, NetworkFailureTest) {
1003 OmahaResponse response;
1004 ASSERT_FALSE(
1005 TestUpdateCheck(NULL, // prefs
1006 kDefaultTestParams,
1007 "",
1008 501,
Darin Petkov265f2902011-05-09 15:17:40 -07001009 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -07001010 static_cast<ActionExitCode>(
1011 kActionCodeOmahaRequestHTTPResponseBase + 501),
1012 &response,
1013 NULL));
1014 EXPECT_FALSE(response.update_exists);
1015}
1016
1017TEST(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) {
1018 OmahaResponse response;
1019 ASSERT_FALSE(
1020 TestUpdateCheck(NULL, // prefs
1021 kDefaultTestParams,
1022 "",
1023 1500,
Darin Petkov265f2902011-05-09 15:17:40 -07001024 false, // ping_only
Darin Petkovedc522e2010-11-05 09:35:17 -07001025 static_cast<ActionExitCode>(
1026 kActionCodeOmahaRequestHTTPResponseBase + 999),
1027 &response,
1028 NULL));
1029 EXPECT_FALSE(response.update_exists);
1030}
1031
Darin Petkov6a5b3222010-07-13 14:55:28 -07001032} // namespace chromeos_update_engine