blob: dedb972ef71ebcc78432e4f0f1384bb636205ab8 [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
Darin Petkov6a5b3222010-07-13 14:55:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07007
Darin Petkov6a5b3222010-07-13 14:55:28 -07008#include <glib.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07009
10#include "base/string_util.h"
11#include "gtest/gtest.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070012#include "update_engine/action_pipe.h"
13#include "update_engine/mock_http_fetcher.h"
14#include "update_engine/omaha_hash_calculator.h"
15#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070016#include "update_engine/omaha_request_params.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070017#include "update_engine/test_utils.h"
18
19using std::string;
20using std::vector;
21
22namespace chromeos_update_engine {
23
24class OmahaRequestActionTest : public ::testing::Test { };
25
26namespace {
27string GetNoUpdateResponse(const string& app_id) {
28 return string(
29 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
30 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
31 "appid=\"") + app_id + "\" status=\"ok\"><ping "
32 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
33}
34
35string GetUpdateResponse(const string& app_id,
36 const string& display_version,
37 const string& more_info_url,
38 const string& prompt,
39 const string& codebase,
40 const string& hash,
41 const string& needsadmin,
42 const string& size) {
43 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
44 "xmlns=\"http://www.google.com/update2/response\" "
45 "protocol=\"2.0\"><app "
46 "appid=\"") + app_id + "\" status=\"ok\"><ping "
47 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
48 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070049 "IsDelta=\"true\" "
Darin Petkov6a5b3222010-07-13 14:55:28 -070050 "codebase=\"" + codebase + "\" "
51 "hash=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
52 "size=\"" + size + "\" status=\"ok\"/></app></gupdate>";
53}
54
55class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
56 public:
57 OmahaRequestActionTestProcessorDelegate()
58 : loop_(NULL),
59 expected_success_(true) {}
60 virtual ~OmahaRequestActionTestProcessorDelegate() {
61 }
62 virtual void ProcessingDone(const ActionProcessor* processor, bool success) {
63 ASSERT_TRUE(loop_);
64 g_main_loop_quit(loop_);
65 }
66
67 virtual void ActionCompleted(ActionProcessor* processor,
68 AbstractAction* action,
69 bool success) {
70 // make sure actions always succeed
71 if (action->Type() == OmahaRequestAction::StaticType())
72 EXPECT_EQ(expected_success_, success);
73 else
74 EXPECT_TRUE(success);
75 }
76 GMainLoop *loop_;
77 bool expected_success_;
78};
79
80gboolean StartProcessorInRunLoop(gpointer data) {
81 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
82 processor->StartProcessing();
83 return FALSE;
84}
85
86} // namespace {}
87
88class OutputObjectCollectorAction;
89
90template<>
91class ActionTraits<OutputObjectCollectorAction> {
92 public:
93 // Does not take an object for input
94 typedef OmahaResponse InputObjectType;
95 // On success, puts the output path on output
96 typedef NoneType OutputObjectType;
97};
98
99class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
100 public:
101 OutputObjectCollectorAction() : has_input_object_(false) {}
102 void PerformAction() {
103 // copy input object
104 has_input_object_ = HasInputObject();
105 if (has_input_object_)
106 omaha_response_ = GetInputObject();
107 processor_->ActionComplete(this, true);
108 }
109 // Should never be called
110 void TerminateProcessing() {
111 CHECK(false);
112 }
113 // Debugging/logging
114 static std::string StaticType() {
115 return "OutputObjectCollectorAction";
116 }
117 std::string Type() const { return StaticType(); }
118 bool has_input_object_;
119 OmahaResponse omaha_response_;
120};
121
122// returns true iff an output response was obtained from the
123// OmahaRequestAction. out_response may be NULL.
124// out_post_data may be null; if non-null, the post-data received by the
125// mock HttpFetcher is returned.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700126bool TestUpdateCheck(const OmahaRequestParams& params,
127 const string& http_response,
128 bool expected_success,
129 OmahaResponse* out_response,
130 vector<char>* out_post_data) {
131 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
132 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Darin Petkov6a5b3222010-07-13 14:55:28 -0700133 http_response.size());
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700134 OmahaRequestAction action(params, NULL, fetcher);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700135 OmahaRequestActionTestProcessorDelegate delegate;
136 delegate.loop_ = loop;
137 delegate.expected_success_ = expected_success;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700138
Darin Petkov6a5b3222010-07-13 14:55:28 -0700139 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700140 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700141 processor.EnqueueAction(&action);
142
143 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700144 BondActions(&action, &collector_action);
145 processor.EnqueueAction(&collector_action);
146
147 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
148 g_main_loop_run(loop);
149 g_main_loop_unref(loop);
150 if (collector_action.has_input_object_ && out_response)
151 *out_response = collector_action.omaha_response_;
152 if (out_post_data)
153 *out_post_data = fetcher->post_data();
154 return collector_action.has_input_object_;
155}
156
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700157// Tests Event requests -- they should always succeed. |out_post_data|
158// may be null; if non-null, the post-data received by the mock
159// HttpFetcher is returned.
160void TestEvent(const OmahaRequestParams& params,
161 OmahaEvent* event,
162 const string& http_response,
163 vector<char>* out_post_data) {
164 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
165 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
166 http_response.size());
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700167 OmahaRequestAction action(params, event, fetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700168 OmahaRequestActionTestProcessorDelegate delegate;
169 delegate.loop_ = loop;
170 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700171 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700172 processor.EnqueueAction(&action);
173
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700174 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
175 g_main_loop_run(loop);
176 g_main_loop_unref(loop);
177 if (out_post_data)
178 *out_post_data = fetcher->post_data();
179}
180
Darin Petkov6a5b3222010-07-13 14:55:28 -0700181TEST(OmahaRequestActionTest, NoUpdateTest) {
182 OmahaRequestParams params("", // machine_id
183 "", // user_id
184 OmahaRequestParams::kOsPlatform,
185 OmahaRequestParams::kOsVersion,
186 "", // os_sp
187 "x86-generic",
188 OmahaRequestParams::kAppId,
189 "0.1.0.0",
190 "en-US",
191 "unittest",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700192 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700193 ""); // url
194 OmahaResponse response;
195 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700196 TestUpdateCheck(params,
197 GetNoUpdateResponse(OmahaRequestParams::kAppId),
198 true,
199 &response,
200 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700201 EXPECT_FALSE(response.update_exists);
202}
203
204TEST(OmahaRequestActionTest, ValidUpdateTest) {
205 OmahaRequestParams params("machine_id",
206 "user_id",
207 OmahaRequestParams::kOsPlatform,
208 OmahaRequestParams::kOsVersion,
209 "service_pack",
210 "arm-generic",
211 OmahaRequestParams::kAppId,
212 "0.1.0.0",
213 "en-US",
214 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700215 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700216 ""); // url
217 OmahaResponse response;
218 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700219 TestUpdateCheck(params,
220 GetUpdateResponse(OmahaRequestParams::kAppId,
221 "1.2.3.4", // version
222 "http://more/info",
223 "true", // prompt
224 "http://code/base", // dl url
225 "HASH1234=", // checksum
226 "false", // needs admin
227 "123"), // size
228 true,
229 &response,
230 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700231 EXPECT_TRUE(response.update_exists);
232 EXPECT_EQ("1.2.3.4", response.display_version);
233 EXPECT_EQ("http://code/base", response.codebase);
234 EXPECT_EQ("http://more/info", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700235 EXPECT_TRUE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700236 EXPECT_EQ("HASH1234=", response.hash);
237 EXPECT_EQ(123, response.size);
238 EXPECT_FALSE(response.needs_admin);
239 EXPECT_TRUE(response.prompt);
240}
241
242TEST(OmahaRequestActionTest, NoOutputPipeTest) {
243 OmahaRequestParams params("", // machine_id
244 "", // usr_id
245 OmahaRequestParams::kOsPlatform,
246 OmahaRequestParams::kOsVersion,
247 "", // os_sp
248 "", // os_board
249 OmahaRequestParams::kAppId,
250 "0.1.0.0",
251 "en-US",
252 "unittest",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700253 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700254 ""); // url
255 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
256
257 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
258
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700259 OmahaRequestAction action(params, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700260 new MockHttpFetcher(http_response.data(),
Darin Petkov6a5b3222010-07-13 14:55:28 -0700261 http_response.size()));
262 OmahaRequestActionTestProcessorDelegate delegate;
263 delegate.loop_ = loop;
264 ActionProcessor processor;
265 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700266 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700267
268 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
269 g_main_loop_run(loop);
270 g_main_loop_unref(loop);
271 EXPECT_FALSE(processor.IsRunning());
272}
273
274TEST(OmahaRequestActionTest, InvalidXmlTest) {
275 OmahaRequestParams params("machine_id",
276 "user_id",
277 OmahaRequestParams::kOsPlatform,
278 OmahaRequestParams::kOsVersion,
279 "service_pack",
280 "x86-generic",
281 OmahaRequestParams::kAppId,
282 "0.1.0.0",
283 "en-US",
284 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700285 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700286 "http://url");
287 OmahaResponse response;
288 ASSERT_FALSE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700289 TestUpdateCheck(params,
290 "invalid xml>",
291 false,
292 &response,
293 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700294 EXPECT_FALSE(response.update_exists);
295}
296
297TEST(OmahaRequestActionTest, MissingStatusTest) {
298 OmahaRequestParams params("machine_id",
299 "user_id",
300 OmahaRequestParams::kOsPlatform,
301 OmahaRequestParams::kOsVersion,
302 "service_pack",
303 "x86-generic",
304 OmahaRequestParams::kAppId,
305 "0.1.0.0",
306 "en-US",
307 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700308 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700309 "http://url");
310 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700311 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov6a5b3222010-07-13 14:55:28 -0700312 params,
313 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
314 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
315 "appid=\"foo\" status=\"ok\"><ping "
316 "status=\"ok\"/><updatecheck/></app></gupdate>",
317 false,
318 &response,
319 NULL));
320 EXPECT_FALSE(response.update_exists);
321}
322
323TEST(OmahaRequestActionTest, InvalidStatusTest) {
324 OmahaRequestParams params("machine_id",
325 "user_id",
326 OmahaRequestParams::kOsPlatform,
327 OmahaRequestParams::kOsVersion,
328 "service_pack",
329 "x86-generic",
330 OmahaRequestParams::kAppId,
331 "0.1.0.0",
332 "en-US",
333 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700334 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700335 "http://url");
336 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700337 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov6a5b3222010-07-13 14:55:28 -0700338 params,
339 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
340 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
341 "appid=\"foo\" status=\"ok\"><ping "
342 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
343 false,
344 &response,
345 NULL));
346 EXPECT_FALSE(response.update_exists);
347}
348
349TEST(OmahaRequestActionTest, MissingNodesetTest) {
350 OmahaRequestParams params("machine_id",
351 "user_id",
352 OmahaRequestParams::kOsPlatform,
353 OmahaRequestParams::kOsVersion,
354 "service_pack",
355 "x86-generic",
356 OmahaRequestParams::kAppId,
357 "0.1.0.0",
358 "en-US",
359 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700360 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700361 "http://url");
362 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700363 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov6a5b3222010-07-13 14:55:28 -0700364 params,
365 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
366 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
367 "appid=\"foo\" status=\"ok\"><ping "
368 "status=\"ok\"/></app></gupdate>",
369 false,
370 &response,
371 NULL));
372 EXPECT_FALSE(response.update_exists);
373}
374
375TEST(OmahaRequestActionTest, MissingFieldTest) {
376 OmahaRequestParams params("machine_id",
377 "user_id",
378 OmahaRequestParams::kOsPlatform,
379 OmahaRequestParams::kOsVersion,
380 "service_pack",
381 "x86-generic",
382 OmahaRequestParams::kAppId,
383 "0.1.0.0",
384 "en-US",
385 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700386 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700387 "http://url");
388 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700389 ASSERT_TRUE(TestUpdateCheck(params,
390 string("<?xml version=\"1.0\" "
391 "encoding=\"UTF-8\"?><gupdate "
392 "xmlns=\"http://www.google.com/"
393 "update2/response\" "
394 "protocol=\"2.0\"><app appid=\"") +
395 OmahaRequestParams::kAppId
396 + "\" status=\"ok\"><ping "
397 "status=\"ok\"/><updatecheck "
398 "DisplayVersion=\"1.2.3.4\" "
399 "Prompt=\"false\" "
400 "codebase=\"http://code/base\" "
401 "hash=\"HASH1234=\" needsadmin=\"true\" "
402 "size=\"123\" "
403 "status=\"ok\"/></app></gupdate>",
404 true,
405 &response,
406 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700407 EXPECT_TRUE(response.update_exists);
408 EXPECT_EQ("1.2.3.4", response.display_version);
409 EXPECT_EQ("http://code/base", response.codebase);
410 EXPECT_EQ("", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700411 EXPECT_FALSE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700412 EXPECT_EQ("HASH1234=", response.hash);
413 EXPECT_EQ(123, response.size);
414 EXPECT_TRUE(response.needs_admin);
415 EXPECT_FALSE(response.prompt);
416}
417
418namespace {
419class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
420 public:
421 void ProcessingStopped(const ActionProcessor* processor) {
422 ASSERT_TRUE(loop_);
423 g_main_loop_quit(loop_);
424 }
425 GMainLoop *loop_;
426};
427
428gboolean TerminateTransferTestStarter(gpointer data) {
429 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
430 processor->StartProcessing();
431 CHECK(processor->IsRunning());
432 processor->StopProcessing();
433 return FALSE;
434}
435} // namespace {}
436
437TEST(OmahaRequestActionTest, TerminateTransferTest) {
438 OmahaRequestParams params("", // machine_id
439 "", // usr_id
440 OmahaRequestParams::kOsPlatform,
441 OmahaRequestParams::kOsVersion,
442 "", // os_sp
443 "", // os_board
444 OmahaRequestParams::kAppId,
445 "0.1.0.0",
446 "en-US",
447 "unittest",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700448 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700449 "http://url");
450 string http_response("doesn't matter");
451 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
452
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700453 OmahaRequestAction action(params, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700454 new MockHttpFetcher(http_response.data(),
Darin Petkov6a5b3222010-07-13 14:55:28 -0700455 http_response.size()));
456 TerminateEarlyTestProcessorDelegate delegate;
457 delegate.loop_ = loop;
458 ActionProcessor processor;
459 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700460 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700461
462 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
463 g_main_loop_run(loop);
464 g_main_loop_unref(loop);
465}
466
467TEST(OmahaRequestActionTest, XmlEncodeTest) {
468 EXPECT_EQ("ab", XmlEncode("ab"));
469 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
470 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
471 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
472 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
473
474 vector<char> post_data;
475
476 // Make sure XML Encode is being called on the params
477 OmahaRequestParams params("testthemachine<id",
478 "testtheuser_id&lt;",
479 OmahaRequestParams::kOsPlatform,
480 OmahaRequestParams::kOsVersion,
481 "testtheservice_pack>",
482 "x86 generic",
483 OmahaRequestParams::kAppId,
484 "0.1.0.0",
485 "en-US",
486 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700487 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700488 "http://url");
489 OmahaResponse response;
490 ASSERT_FALSE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700491 TestUpdateCheck(params,
492 "invalid xml>",
493 false,
494 &response,
495 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700496 // convert post_data to string
497 string post_str(&post_data[0], post_data.size());
498 EXPECT_NE(post_str.find("testthemachine&lt;id"), string::npos);
499 EXPECT_EQ(post_str.find("testthemachine<id"), string::npos);
500 EXPECT_NE(post_str.find("testtheuser_id&amp;lt;"), string::npos);
501 EXPECT_EQ(post_str.find("testtheuser_id&lt;"), string::npos);
502 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
503 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
504 EXPECT_NE(post_str.find("x86 generic"), string::npos);
505}
506
507TEST(OmahaRequestActionTest, XmlDecodeTest) {
508 OmahaRequestParams params("machine_id",
509 "user_id",
510 OmahaRequestParams::kOsPlatform,
511 OmahaRequestParams::kOsVersion,
512 "service_pack",
513 "x86-generic",
514 OmahaRequestParams::kAppId,
515 "0.1.0.0",
516 "en-US",
517 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700518 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700519 "http://url");
520 OmahaResponse response;
521 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700522 TestUpdateCheck(params,
523 GetUpdateResponse(OmahaRequestParams::kAppId,
524 "1.2.3.4", // version
525 "testthe&lt;url", // more info
526 "true", // prompt
527 "testthe&amp;codebase", // dl url
528 "HASH1234=", // checksum
529 "false", // needs admin
530 "123"), // size
531 true,
532 &response,
533 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700534
535 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700536 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700537}
538
539TEST(OmahaRequestActionTest, ParseIntTest) {
540 OmahaRequestParams params("machine_id",
541 "user_id",
542 OmahaRequestParams::kOsPlatform,
543 OmahaRequestParams::kOsVersion,
544 "service_pack",
545 "the_board",
546 OmahaRequestParams::kAppId,
547 "0.1.0.0",
548 "en-US",
549 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700550 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700551 "http://url");
552 OmahaResponse response;
553 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700554 TestUpdateCheck(params,
555 GetUpdateResponse(OmahaRequestParams::kAppId,
556 "1.2.3.4", // version
557 "theurl", // more info
558 "true", // prompt
559 "thecodebase", // dl url
560 "HASH1234=", // checksum
561 "false", // needs admin
562 // overflows int32:
563 "123123123123123"), // size
564 true,
565 &response,
566 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700567
568 EXPECT_EQ(response.size, 123123123123123ll);
569}
570
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700571TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
572 vector<char> post_data;
573 OmahaRequestParams params("machine_id",
574 "user_id",
575 OmahaRequestParams::kOsPlatform,
576 OmahaRequestParams::kOsVersion,
577 "service_pack",
578 "x86-generic",
579 OmahaRequestParams::kAppId,
580 "0.1.0.0",
581 "en-US",
582 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700583 false, // delta okay
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700584 "http://url");
585 OmahaResponse response;
586 ASSERT_FALSE(TestUpdateCheck(params,
587 "invalid xml>",
588 false,
589 &response,
590 &post_data));
591 // convert post_data to string
592 string post_str(&post_data[0], post_data.size());
593 EXPECT_NE(post_str.find(" <o:ping active=\"0\"></o:ping>\n"
594 " <o:updatecheck></o:updatecheck>\n"),
595 string::npos);
596 EXPECT_EQ(post_str.find("o:event"), string::npos);
597}
598
599TEST(OmahaRequestActionTest, FormatEventOutputTest) {
600 vector<char> post_data;
601 OmahaRequestParams params("machine_id",
602 "user_id",
603 OmahaRequestParams::kOsPlatform,
604 OmahaRequestParams::kOsVersion,
605 "service_pack",
606 "x86-generic",
607 OmahaRequestParams::kAppId,
608 "0.1.0.0",
609 "en-US",
610 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700611 false, // delta okay
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700612 "http://url");
613 TestEvent(params,
614 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
615 OmahaEvent::kResultError,
616 5),
617 "invalid xml>",
618 &post_data);
619 // convert post_data to string
620 string post_str(&post_data[0], post_data.size());
621 string expected_event = StringPrintf(
622 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
623 "errorcode=\"%d\"></o:event>\n",
624 OmahaEvent::kTypeDownloadComplete,
625 OmahaEvent::kResultError,
626 5);
627 EXPECT_NE(post_str.find(expected_event), string::npos);
628 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
629}
630
631TEST(OmahaRequestActionTest, IsEventTest) {
632 string http_response("doesn't matter");
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700633 OmahaRequestParams params("machine_id",
634 "user_id",
635 OmahaRequestParams::kOsPlatform,
636 OmahaRequestParams::kOsVersion,
637 "service_pack",
638 "x86-generic",
639 OmahaRequestParams::kAppId,
640 "0.1.0.0",
641 "en-US",
642 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700643 false, // delta okay
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700644 "http://url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700645
646 OmahaRequestAction update_check_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700647 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700648 NULL,
649 new MockHttpFetcher(http_response.data(),
650 http_response.size()));
651 EXPECT_FALSE(update_check_action.IsEvent());
652
653 OmahaRequestAction event_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700654 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700655 new OmahaEvent(OmahaEvent::kTypeInstallComplete,
656 OmahaEvent::kResultError,
657 0),
658 new MockHttpFetcher(http_response.data(),
659 http_response.size()));
660 EXPECT_TRUE(event_action.IsEvent());
661}
662
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700663TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
664 for (int i = 0; i < 2; i++) {
665 bool delta_okay = i == 1;
666 const char* delta_okay_str = delta_okay ? "true" : "false";
667 vector<char> post_data;
668 OmahaRequestParams params("machine_id",
669 "user_id",
670 OmahaRequestParams::kOsPlatform,
671 OmahaRequestParams::kOsVersion,
672 "service_pack",
673 "x86-generic",
674 OmahaRequestParams::kAppId,
675 "0.1.0.0",
676 "en-US",
677 "unittest_track",
678 delta_okay,
679 "http://url");
680 ASSERT_FALSE(TestUpdateCheck(params,
681 "invalid xml>",
682 false,
683 NULL,
684 &post_data));
685 // convert post_data to string
686 string post_str(&post_data[0], post_data.size());
687 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
688 string::npos)
689 << "i = " << i;
690 }
691}
692
Darin Petkov6a5b3222010-07-13 14:55:28 -0700693} // namespace chromeos_update_engine