Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 2 | // 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 Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 7 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 8 | #include <glib.h> |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 9 | |
| 10 | #include "base/string_util.h" |
| 11 | #include "gtest/gtest.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 12 | #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 Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 16 | #include "update_engine/omaha_request_params.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 17 | #include "update_engine/test_utils.h" |
| 18 | |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
| 24 | class OmahaRequestActionTest : public ::testing::Test { }; |
| 25 | |
| 26 | namespace { |
| 27 | string 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 | |
| 35 | string 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 Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 49 | "IsDelta=\"true\" " |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 50 | "codebase=\"" + codebase + "\" " |
| 51 | "hash=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" " |
| 52 | "size=\"" + size + "\" status=\"ok\"/></app></gupdate>"; |
| 53 | } |
| 54 | |
| 55 | class 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 | |
| 80 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 81 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 82 | processor->StartProcessing(); |
| 83 | return FALSE; |
| 84 | } |
| 85 | |
| 86 | } // namespace {} |
| 87 | |
| 88 | class OutputObjectCollectorAction; |
| 89 | |
| 90 | template<> |
| 91 | class 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 | |
| 99 | class 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 Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 126 | bool 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 Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 133 | http_response.size()); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 134 | OmahaRequestAction action(params, NULL, fetcher); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 135 | OmahaRequestActionTestProcessorDelegate delegate; |
| 136 | delegate.loop_ = loop; |
| 137 | delegate.expected_success_ = expected_success; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 138 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 139 | ActionProcessor processor; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 140 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 141 | processor.EnqueueAction(&action); |
| 142 | |
| 143 | OutputObjectCollectorAction collector_action; |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 144 | 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 Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 157 | // 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. |
| 160 | void 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 Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 167 | OmahaRequestAction action(params, event, fetcher); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 168 | OmahaRequestActionTestProcessorDelegate delegate; |
| 169 | delegate.loop_ = loop; |
| 170 | ActionProcessor processor; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 171 | processor.set_delegate(&delegate); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 172 | processor.EnqueueAction(&action); |
| 173 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 174 | 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 Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 181 | TEST(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", |
| 192 | ""); // url |
| 193 | OmahaResponse response; |
| 194 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 195 | TestUpdateCheck(params, |
| 196 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 197 | true, |
| 198 | &response, |
| 199 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 200 | EXPECT_FALSE(response.update_exists); |
| 201 | } |
| 202 | |
| 203 | TEST(OmahaRequestActionTest, ValidUpdateTest) { |
| 204 | OmahaRequestParams params("machine_id", |
| 205 | "user_id", |
| 206 | OmahaRequestParams::kOsPlatform, |
| 207 | OmahaRequestParams::kOsVersion, |
| 208 | "service_pack", |
| 209 | "arm-generic", |
| 210 | OmahaRequestParams::kAppId, |
| 211 | "0.1.0.0", |
| 212 | "en-US", |
| 213 | "unittest_track", |
| 214 | ""); // url |
| 215 | OmahaResponse response; |
| 216 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 217 | TestUpdateCheck(params, |
| 218 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 219 | "1.2.3.4", // version |
| 220 | "http://more/info", |
| 221 | "true", // prompt |
| 222 | "http://code/base", // dl url |
| 223 | "HASH1234=", // checksum |
| 224 | "false", // needs admin |
| 225 | "123"), // size |
| 226 | true, |
| 227 | &response, |
| 228 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 229 | EXPECT_TRUE(response.update_exists); |
| 230 | EXPECT_EQ("1.2.3.4", response.display_version); |
| 231 | EXPECT_EQ("http://code/base", response.codebase); |
| 232 | EXPECT_EQ("http://more/info", response.more_info_url); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 233 | EXPECT_TRUE(response.is_delta); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 234 | EXPECT_EQ("HASH1234=", response.hash); |
| 235 | EXPECT_EQ(123, response.size); |
| 236 | EXPECT_FALSE(response.needs_admin); |
| 237 | EXPECT_TRUE(response.prompt); |
| 238 | } |
| 239 | |
| 240 | TEST(OmahaRequestActionTest, NoOutputPipeTest) { |
| 241 | OmahaRequestParams params("", // machine_id |
| 242 | "", // usr_id |
| 243 | OmahaRequestParams::kOsPlatform, |
| 244 | OmahaRequestParams::kOsVersion, |
| 245 | "", // os_sp |
| 246 | "", // os_board |
| 247 | OmahaRequestParams::kAppId, |
| 248 | "0.1.0.0", |
| 249 | "en-US", |
| 250 | "unittest", |
| 251 | ""); // url |
| 252 | const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); |
| 253 | |
| 254 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 255 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 256 | OmahaRequestAction action(params, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 257 | new MockHttpFetcher(http_response.data(), |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 258 | http_response.size())); |
| 259 | OmahaRequestActionTestProcessorDelegate delegate; |
| 260 | delegate.loop_ = loop; |
| 261 | ActionProcessor processor; |
| 262 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 263 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 264 | |
| 265 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 266 | g_main_loop_run(loop); |
| 267 | g_main_loop_unref(loop); |
| 268 | EXPECT_FALSE(processor.IsRunning()); |
| 269 | } |
| 270 | |
| 271 | TEST(OmahaRequestActionTest, InvalidXmlTest) { |
| 272 | OmahaRequestParams params("machine_id", |
| 273 | "user_id", |
| 274 | OmahaRequestParams::kOsPlatform, |
| 275 | OmahaRequestParams::kOsVersion, |
| 276 | "service_pack", |
| 277 | "x86-generic", |
| 278 | OmahaRequestParams::kAppId, |
| 279 | "0.1.0.0", |
| 280 | "en-US", |
| 281 | "unittest_track", |
| 282 | "http://url"); |
| 283 | OmahaResponse response; |
| 284 | ASSERT_FALSE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 285 | TestUpdateCheck(params, |
| 286 | "invalid xml>", |
| 287 | false, |
| 288 | &response, |
| 289 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 290 | EXPECT_FALSE(response.update_exists); |
| 291 | } |
| 292 | |
| 293 | TEST(OmahaRequestActionTest, MissingStatusTest) { |
| 294 | OmahaRequestParams params("machine_id", |
| 295 | "user_id", |
| 296 | OmahaRequestParams::kOsPlatform, |
| 297 | OmahaRequestParams::kOsVersion, |
| 298 | "service_pack", |
| 299 | "x86-generic", |
| 300 | OmahaRequestParams::kAppId, |
| 301 | "0.1.0.0", |
| 302 | "en-US", |
| 303 | "unittest_track", |
| 304 | "http://url"); |
| 305 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 306 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 307 | params, |
| 308 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 309 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 310 | "appid=\"foo\" status=\"ok\"><ping " |
| 311 | "status=\"ok\"/><updatecheck/></app></gupdate>", |
| 312 | false, |
| 313 | &response, |
| 314 | NULL)); |
| 315 | EXPECT_FALSE(response.update_exists); |
| 316 | } |
| 317 | |
| 318 | TEST(OmahaRequestActionTest, InvalidStatusTest) { |
| 319 | OmahaRequestParams params("machine_id", |
| 320 | "user_id", |
| 321 | OmahaRequestParams::kOsPlatform, |
| 322 | OmahaRequestParams::kOsVersion, |
| 323 | "service_pack", |
| 324 | "x86-generic", |
| 325 | OmahaRequestParams::kAppId, |
| 326 | "0.1.0.0", |
| 327 | "en-US", |
| 328 | "unittest_track", |
| 329 | "http://url"); |
| 330 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 331 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 332 | params, |
| 333 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 334 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 335 | "appid=\"foo\" status=\"ok\"><ping " |
| 336 | "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>", |
| 337 | false, |
| 338 | &response, |
| 339 | NULL)); |
| 340 | EXPECT_FALSE(response.update_exists); |
| 341 | } |
| 342 | |
| 343 | TEST(OmahaRequestActionTest, MissingNodesetTest) { |
| 344 | OmahaRequestParams params("machine_id", |
| 345 | "user_id", |
| 346 | OmahaRequestParams::kOsPlatform, |
| 347 | OmahaRequestParams::kOsVersion, |
| 348 | "service_pack", |
| 349 | "x86-generic", |
| 350 | OmahaRequestParams::kAppId, |
| 351 | "0.1.0.0", |
| 352 | "en-US", |
| 353 | "unittest_track", |
| 354 | "http://url"); |
| 355 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 356 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 357 | params, |
| 358 | "<?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>", |
| 362 | false, |
| 363 | &response, |
| 364 | NULL)); |
| 365 | EXPECT_FALSE(response.update_exists); |
| 366 | } |
| 367 | |
| 368 | TEST(OmahaRequestActionTest, MissingFieldTest) { |
| 369 | OmahaRequestParams params("machine_id", |
| 370 | "user_id", |
| 371 | OmahaRequestParams::kOsPlatform, |
| 372 | OmahaRequestParams::kOsVersion, |
| 373 | "service_pack", |
| 374 | "x86-generic", |
| 375 | OmahaRequestParams::kAppId, |
| 376 | "0.1.0.0", |
| 377 | "en-US", |
| 378 | "unittest_track", |
| 379 | "http://url"); |
| 380 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 381 | ASSERT_TRUE(TestUpdateCheck(params, |
| 382 | string("<?xml version=\"1.0\" " |
| 383 | "encoding=\"UTF-8\"?><gupdate " |
| 384 | "xmlns=\"http://www.google.com/" |
| 385 | "update2/response\" " |
| 386 | "protocol=\"2.0\"><app appid=\"") + |
| 387 | OmahaRequestParams::kAppId |
| 388 | + "\" status=\"ok\"><ping " |
| 389 | "status=\"ok\"/><updatecheck " |
| 390 | "DisplayVersion=\"1.2.3.4\" " |
| 391 | "Prompt=\"false\" " |
| 392 | "codebase=\"http://code/base\" " |
| 393 | "hash=\"HASH1234=\" needsadmin=\"true\" " |
| 394 | "size=\"123\" " |
| 395 | "status=\"ok\"/></app></gupdate>", |
| 396 | true, |
| 397 | &response, |
| 398 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 399 | EXPECT_TRUE(response.update_exists); |
| 400 | EXPECT_EQ("1.2.3.4", response.display_version); |
| 401 | EXPECT_EQ("http://code/base", response.codebase); |
| 402 | EXPECT_EQ("", response.more_info_url); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 403 | EXPECT_FALSE(response.is_delta); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 404 | EXPECT_EQ("HASH1234=", response.hash); |
| 405 | EXPECT_EQ(123, response.size); |
| 406 | EXPECT_TRUE(response.needs_admin); |
| 407 | EXPECT_FALSE(response.prompt); |
| 408 | } |
| 409 | |
| 410 | namespace { |
| 411 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { |
| 412 | public: |
| 413 | void ProcessingStopped(const ActionProcessor* processor) { |
| 414 | ASSERT_TRUE(loop_); |
| 415 | g_main_loop_quit(loop_); |
| 416 | } |
| 417 | GMainLoop *loop_; |
| 418 | }; |
| 419 | |
| 420 | gboolean TerminateTransferTestStarter(gpointer data) { |
| 421 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 422 | processor->StartProcessing(); |
| 423 | CHECK(processor->IsRunning()); |
| 424 | processor->StopProcessing(); |
| 425 | return FALSE; |
| 426 | } |
| 427 | } // namespace {} |
| 428 | |
| 429 | TEST(OmahaRequestActionTest, TerminateTransferTest) { |
| 430 | OmahaRequestParams params("", // machine_id |
| 431 | "", // usr_id |
| 432 | OmahaRequestParams::kOsPlatform, |
| 433 | OmahaRequestParams::kOsVersion, |
| 434 | "", // os_sp |
| 435 | "", // os_board |
| 436 | OmahaRequestParams::kAppId, |
| 437 | "0.1.0.0", |
| 438 | "en-US", |
| 439 | "unittest", |
| 440 | "http://url"); |
| 441 | string http_response("doesn't matter"); |
| 442 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 443 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 444 | OmahaRequestAction action(params, NULL, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 445 | new MockHttpFetcher(http_response.data(), |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 446 | http_response.size())); |
| 447 | TerminateEarlyTestProcessorDelegate delegate; |
| 448 | delegate.loop_ = loop; |
| 449 | ActionProcessor processor; |
| 450 | processor.set_delegate(&delegate); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 451 | processor.EnqueueAction(&action); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 452 | |
| 453 | g_timeout_add(0, &TerminateTransferTestStarter, &processor); |
| 454 | g_main_loop_run(loop); |
| 455 | g_main_loop_unref(loop); |
| 456 | } |
| 457 | |
| 458 | TEST(OmahaRequestActionTest, XmlEncodeTest) { |
| 459 | EXPECT_EQ("ab", XmlEncode("ab")); |
| 460 | EXPECT_EQ("a<b", XmlEncode("a<b")); |
| 461 | EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); |
| 462 | EXPECT_EQ("<&>", XmlEncode("<&>")); |
| 463 | EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>")); |
| 464 | |
| 465 | vector<char> post_data; |
| 466 | |
| 467 | // Make sure XML Encode is being called on the params |
| 468 | OmahaRequestParams params("testthemachine<id", |
| 469 | "testtheuser_id<", |
| 470 | OmahaRequestParams::kOsPlatform, |
| 471 | OmahaRequestParams::kOsVersion, |
| 472 | "testtheservice_pack>", |
| 473 | "x86 generic", |
| 474 | OmahaRequestParams::kAppId, |
| 475 | "0.1.0.0", |
| 476 | "en-US", |
| 477 | "unittest_track", |
| 478 | "http://url"); |
| 479 | OmahaResponse response; |
| 480 | ASSERT_FALSE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 481 | TestUpdateCheck(params, |
| 482 | "invalid xml>", |
| 483 | false, |
| 484 | &response, |
| 485 | &post_data)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 486 | // convert post_data to string |
| 487 | string post_str(&post_data[0], post_data.size()); |
| 488 | EXPECT_NE(post_str.find("testthemachine<id"), string::npos); |
| 489 | EXPECT_EQ(post_str.find("testthemachine<id"), string::npos); |
| 490 | EXPECT_NE(post_str.find("testtheuser_id&lt;"), string::npos); |
| 491 | EXPECT_EQ(post_str.find("testtheuser_id<"), string::npos); |
| 492 | EXPECT_NE(post_str.find("testtheservice_pack>"), string::npos); |
| 493 | EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos); |
| 494 | EXPECT_NE(post_str.find("x86 generic"), string::npos); |
| 495 | } |
| 496 | |
| 497 | TEST(OmahaRequestActionTest, XmlDecodeTest) { |
| 498 | OmahaRequestParams params("machine_id", |
| 499 | "user_id", |
| 500 | OmahaRequestParams::kOsPlatform, |
| 501 | OmahaRequestParams::kOsVersion, |
| 502 | "service_pack", |
| 503 | "x86-generic", |
| 504 | OmahaRequestParams::kAppId, |
| 505 | "0.1.0.0", |
| 506 | "en-US", |
| 507 | "unittest_track", |
| 508 | "http://url"); |
| 509 | OmahaResponse response; |
| 510 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 511 | TestUpdateCheck(params, |
| 512 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 513 | "1.2.3.4", // version |
| 514 | "testthe<url", // more info |
| 515 | "true", // prompt |
| 516 | "testthe&codebase", // dl url |
| 517 | "HASH1234=", // checksum |
| 518 | "false", // needs admin |
| 519 | "123"), // size |
| 520 | true, |
| 521 | &response, |
| 522 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 523 | |
| 524 | EXPECT_EQ(response.more_info_url, "testthe<url"); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 525 | EXPECT_EQ(response.codebase, "testthe&codebase"); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | TEST(OmahaRequestActionTest, ParseIntTest) { |
| 529 | OmahaRequestParams params("machine_id", |
| 530 | "user_id", |
| 531 | OmahaRequestParams::kOsPlatform, |
| 532 | OmahaRequestParams::kOsVersion, |
| 533 | "service_pack", |
| 534 | "the_board", |
| 535 | OmahaRequestParams::kAppId, |
| 536 | "0.1.0.0", |
| 537 | "en-US", |
| 538 | "unittest_track", |
| 539 | "http://url"); |
| 540 | OmahaResponse response; |
| 541 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 542 | TestUpdateCheck(params, |
| 543 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 544 | "1.2.3.4", // version |
| 545 | "theurl", // more info |
| 546 | "true", // prompt |
| 547 | "thecodebase", // dl url |
| 548 | "HASH1234=", // checksum |
| 549 | "false", // needs admin |
| 550 | // overflows int32: |
| 551 | "123123123123123"), // size |
| 552 | true, |
| 553 | &response, |
| 554 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 555 | |
| 556 | EXPECT_EQ(response.size, 123123123123123ll); |
| 557 | } |
| 558 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 559 | TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) { |
| 560 | vector<char> post_data; |
| 561 | OmahaRequestParams params("machine_id", |
| 562 | "user_id", |
| 563 | OmahaRequestParams::kOsPlatform, |
| 564 | OmahaRequestParams::kOsVersion, |
| 565 | "service_pack", |
| 566 | "x86-generic", |
| 567 | OmahaRequestParams::kAppId, |
| 568 | "0.1.0.0", |
| 569 | "en-US", |
| 570 | "unittest_track", |
| 571 | "http://url"); |
| 572 | OmahaResponse response; |
| 573 | ASSERT_FALSE(TestUpdateCheck(params, |
| 574 | "invalid xml>", |
| 575 | false, |
| 576 | &response, |
| 577 | &post_data)); |
| 578 | // convert post_data to string |
| 579 | string post_str(&post_data[0], post_data.size()); |
| 580 | EXPECT_NE(post_str.find(" <o:ping active=\"0\"></o:ping>\n" |
| 581 | " <o:updatecheck></o:updatecheck>\n"), |
| 582 | string::npos); |
| 583 | EXPECT_EQ(post_str.find("o:event"), string::npos); |
| 584 | } |
| 585 | |
| 586 | TEST(OmahaRequestActionTest, FormatEventOutputTest) { |
| 587 | vector<char> post_data; |
| 588 | OmahaRequestParams params("machine_id", |
| 589 | "user_id", |
| 590 | OmahaRequestParams::kOsPlatform, |
| 591 | OmahaRequestParams::kOsVersion, |
| 592 | "service_pack", |
| 593 | "x86-generic", |
| 594 | OmahaRequestParams::kAppId, |
| 595 | "0.1.0.0", |
| 596 | "en-US", |
| 597 | "unittest_track", |
| 598 | "http://url"); |
| 599 | TestEvent(params, |
| 600 | new OmahaEvent(OmahaEvent::kTypeDownloadComplete, |
| 601 | OmahaEvent::kResultError, |
| 602 | 5), |
| 603 | "invalid xml>", |
| 604 | &post_data); |
| 605 | // convert post_data to string |
| 606 | string post_str(&post_data[0], post_data.size()); |
| 607 | string expected_event = StringPrintf( |
| 608 | " <o:event eventtype=\"%d\" eventresult=\"%d\" " |
| 609 | "errorcode=\"%d\"></o:event>\n", |
| 610 | OmahaEvent::kTypeDownloadComplete, |
| 611 | OmahaEvent::kResultError, |
| 612 | 5); |
| 613 | EXPECT_NE(post_str.find(expected_event), string::npos); |
| 614 | EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); |
| 615 | } |
| 616 | |
| 617 | TEST(OmahaRequestActionTest, IsEventTest) { |
| 618 | string http_response("doesn't matter"); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 619 | OmahaRequestParams params("machine_id", |
| 620 | "user_id", |
| 621 | OmahaRequestParams::kOsPlatform, |
| 622 | OmahaRequestParams::kOsVersion, |
| 623 | "service_pack", |
| 624 | "x86-generic", |
| 625 | OmahaRequestParams::kAppId, |
| 626 | "0.1.0.0", |
| 627 | "en-US", |
| 628 | "unittest_track", |
| 629 | "http://url"); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 630 | |
| 631 | OmahaRequestAction update_check_action( |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 632 | params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 633 | NULL, |
| 634 | new MockHttpFetcher(http_response.data(), |
| 635 | http_response.size())); |
| 636 | EXPECT_FALSE(update_check_action.IsEvent()); |
| 637 | |
| 638 | OmahaRequestAction event_action( |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 639 | params, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 640 | new OmahaEvent(OmahaEvent::kTypeInstallComplete, |
| 641 | OmahaEvent::kResultError, |
| 642 | 0), |
| 643 | new MockHttpFetcher(http_response.data(), |
| 644 | http_response.size())); |
| 645 | EXPECT_TRUE(event_action.IsEvent()); |
| 646 | } |
| 647 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 648 | } // namespace chromeos_update_engine |