| 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 + "\" " | 
 | 49 |       "codebase=\"" + codebase + "\" " | 
 | 50 |       "hash=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" " | 
 | 51 |       "size=\"" + size + "\" status=\"ok\"/></app></gupdate>"; | 
 | 52 | } | 
 | 53 |  | 
 | 54 | class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate { | 
 | 55 |  public: | 
 | 56 |   OmahaRequestActionTestProcessorDelegate() | 
 | 57 |       : loop_(NULL), | 
 | 58 |         expected_success_(true) {} | 
 | 59 |   virtual ~OmahaRequestActionTestProcessorDelegate() { | 
 | 60 |   } | 
 | 61 |   virtual void ProcessingDone(const ActionProcessor* processor, bool success) { | 
 | 62 |     ASSERT_TRUE(loop_); | 
 | 63 |     g_main_loop_quit(loop_); | 
 | 64 |   } | 
 | 65 |  | 
 | 66 |   virtual void ActionCompleted(ActionProcessor* processor, | 
 | 67 |                                AbstractAction* action, | 
 | 68 |                                bool success) { | 
 | 69 |     // make sure actions always succeed | 
 | 70 |     if (action->Type() == OmahaRequestAction::StaticType()) | 
 | 71 |       EXPECT_EQ(expected_success_, success); | 
 | 72 |     else | 
 | 73 |       EXPECT_TRUE(success); | 
 | 74 |   } | 
 | 75 |   GMainLoop *loop_; | 
 | 76 |   bool expected_success_; | 
 | 77 | }; | 
 | 78 |  | 
 | 79 | gboolean StartProcessorInRunLoop(gpointer data) { | 
 | 80 |   ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 
 | 81 |   processor->StartProcessing(); | 
 | 82 |   return FALSE; | 
 | 83 | } | 
 | 84 |  | 
 | 85 | }  // namespace {} | 
 | 86 |  | 
 | 87 | class OutputObjectCollectorAction; | 
 | 88 |  | 
 | 89 | template<> | 
 | 90 | class ActionTraits<OutputObjectCollectorAction> { | 
 | 91 |  public: | 
 | 92 |   // Does not take an object for input | 
 | 93 |   typedef OmahaResponse InputObjectType; | 
 | 94 |   // On success, puts the output path on output | 
 | 95 |   typedef NoneType OutputObjectType; | 
 | 96 | }; | 
 | 97 |  | 
 | 98 | class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> { | 
 | 99 |  public: | 
 | 100 |   OutputObjectCollectorAction() : has_input_object_(false) {} | 
 | 101 |   void PerformAction() { | 
 | 102 |     // copy input object | 
 | 103 |     has_input_object_ = HasInputObject(); | 
 | 104 |     if (has_input_object_) | 
 | 105 |       omaha_response_ = GetInputObject(); | 
 | 106 |     processor_->ActionComplete(this, true); | 
 | 107 |   } | 
 | 108 |   // Should never be called | 
 | 109 |   void TerminateProcessing() { | 
 | 110 |     CHECK(false); | 
 | 111 |   } | 
 | 112 |   // Debugging/logging | 
 | 113 |   static std::string StaticType() { | 
 | 114 |     return "OutputObjectCollectorAction"; | 
 | 115 |   } | 
 | 116 |   std::string Type() const { return StaticType(); } | 
 | 117 |   bool has_input_object_; | 
 | 118 |   OmahaResponse omaha_response_; | 
 | 119 | }; | 
 | 120 |  | 
 | 121 | // returns true iff an output response was obtained from the | 
 | 122 | // OmahaRequestAction. out_response may be NULL. | 
 | 123 | // out_post_data may be null; if non-null, the post-data received by the | 
 | 124 | // mock HttpFetcher is returned. | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 125 | bool TestUpdateCheck(const OmahaRequestParams& params, | 
 | 126 |                      const string& http_response, | 
 | 127 |                      bool expected_success, | 
 | 128 |                      OmahaResponse* out_response, | 
 | 129 |                      vector<char>* out_post_data) { | 
 | 130 |   GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); | 
 | 131 |   MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 132 |                                                  http_response.size()); | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 133 |   OmahaRequestAction action(params, NULL, fetcher); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 134 |   OmahaRequestActionTestProcessorDelegate delegate; | 
 | 135 |   delegate.loop_ = loop; | 
 | 136 |   delegate.expected_success_ = expected_success; | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 137 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 138 |   ActionProcessor processor; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 139 |   processor.set_delegate(&delegate); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 140 |   processor.EnqueueAction(&action); | 
 | 141 |  | 
 | 142 |   OutputObjectCollectorAction collector_action; | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 143 |   BondActions(&action, &collector_action); | 
 | 144 |   processor.EnqueueAction(&collector_action); | 
 | 145 |  | 
 | 146 |   g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 
 | 147 |   g_main_loop_run(loop); | 
 | 148 |   g_main_loop_unref(loop); | 
 | 149 |   if (collector_action.has_input_object_ && out_response) | 
 | 150 |     *out_response = collector_action.omaha_response_; | 
 | 151 |   if (out_post_data) | 
 | 152 |     *out_post_data = fetcher->post_data(); | 
 | 153 |   return collector_action.has_input_object_; | 
 | 154 | } | 
 | 155 |  | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 156 | // Tests Event requests -- they should always succeed. |out_post_data| | 
 | 157 | // may be null; if non-null, the post-data received by the mock | 
 | 158 | // HttpFetcher is returned. | 
 | 159 | void TestEvent(const OmahaRequestParams& params, | 
 | 160 |                OmahaEvent* event, | 
 | 161 |                const string& http_response, | 
 | 162 |                vector<char>* out_post_data) { | 
 | 163 |   GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); | 
 | 164 |   MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), | 
 | 165 |                                                  http_response.size()); | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 166 |   OmahaRequestAction action(params, event, fetcher); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 167 |   OmahaRequestActionTestProcessorDelegate delegate; | 
 | 168 |   delegate.loop_ = loop; | 
 | 169 |   ActionProcessor processor; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 170 |   processor.set_delegate(&delegate); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 171 |   processor.EnqueueAction(&action); | 
 | 172 |  | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 173 |   g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 
 | 174 |   g_main_loop_run(loop); | 
 | 175 |   g_main_loop_unref(loop); | 
 | 176 |   if (out_post_data) | 
 | 177 |     *out_post_data = fetcher->post_data(); | 
 | 178 | } | 
 | 179 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 180 | TEST(OmahaRequestActionTest, NoUpdateTest) { | 
 | 181 |   OmahaRequestParams params("",  // machine_id | 
 | 182 |                             "",  // user_id | 
 | 183 |                             OmahaRequestParams::kOsPlatform, | 
 | 184 |                             OmahaRequestParams::kOsVersion, | 
 | 185 |                             "",  // os_sp | 
 | 186 |                             "x86-generic", | 
 | 187 |                             OmahaRequestParams::kAppId, | 
 | 188 |                             "0.1.0.0", | 
 | 189 |                             "en-US", | 
 | 190 |                             "unittest", | 
 | 191 |                             "");  // url | 
 | 192 |   OmahaResponse response; | 
 | 193 |   ASSERT_TRUE( | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 194 |       TestUpdateCheck(params, | 
 | 195 |                       GetNoUpdateResponse(OmahaRequestParams::kAppId), | 
 | 196 |                       true, | 
 | 197 |                       &response, | 
 | 198 |                       NULL)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 199 |   EXPECT_FALSE(response.update_exists); | 
 | 200 | } | 
 | 201 |  | 
 | 202 | TEST(OmahaRequestActionTest, ValidUpdateTest) { | 
 | 203 |   OmahaRequestParams params("machine_id", | 
 | 204 |                             "user_id", | 
 | 205 |                             OmahaRequestParams::kOsPlatform, | 
 | 206 |                             OmahaRequestParams::kOsVersion, | 
 | 207 |                             "service_pack", | 
 | 208 |                             "arm-generic", | 
 | 209 |                             OmahaRequestParams::kAppId, | 
 | 210 |                             "0.1.0.0", | 
 | 211 |                             "en-US", | 
 | 212 |                             "unittest_track", | 
 | 213 |                             "");  // url | 
 | 214 |   OmahaResponse response; | 
 | 215 |   ASSERT_TRUE( | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 216 |       TestUpdateCheck(params, | 
 | 217 |                       GetUpdateResponse(OmahaRequestParams::kAppId, | 
 | 218 |                                         "1.2.3.4",  // version | 
 | 219 |                                         "http://more/info", | 
 | 220 |                                         "true",  // prompt | 
 | 221 |                                         "http://code/base",  // dl url | 
 | 222 |                                         "HASH1234=",  // checksum | 
 | 223 |                                         "false",  // needs admin | 
 | 224 |                                         "123"),  // size | 
 | 225 |                       true, | 
 | 226 |                       &response, | 
 | 227 |                       NULL)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 228 |   EXPECT_TRUE(response.update_exists); | 
 | 229 |   EXPECT_EQ("1.2.3.4", response.display_version); | 
 | 230 |   EXPECT_EQ("http://code/base", response.codebase); | 
 | 231 |   EXPECT_EQ("http://more/info", response.more_info_url); | 
 | 232 |   EXPECT_EQ("HASH1234=", response.hash); | 
 | 233 |   EXPECT_EQ(123, response.size); | 
 | 234 |   EXPECT_FALSE(response.needs_admin); | 
 | 235 |   EXPECT_TRUE(response.prompt); | 
 | 236 | } | 
 | 237 |  | 
 | 238 | TEST(OmahaRequestActionTest, NoOutputPipeTest) { | 
 | 239 |   OmahaRequestParams params("",  // machine_id | 
 | 240 |                             "",  // usr_id | 
 | 241 |                             OmahaRequestParams::kOsPlatform, | 
 | 242 |                             OmahaRequestParams::kOsVersion, | 
 | 243 |                             "",  // os_sp | 
 | 244 |                             "",  // os_board | 
 | 245 |                             OmahaRequestParams::kAppId, | 
 | 246 |                             "0.1.0.0", | 
 | 247 |                             "en-US", | 
 | 248 |                             "unittest", | 
 | 249 |                             "");  // url | 
 | 250 |   const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); | 
 | 251 |  | 
 | 252 |   GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 
 | 253 |  | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 254 |   OmahaRequestAction action(params, NULL, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 255 |                             new MockHttpFetcher(http_response.data(), | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 256 |                                                 http_response.size())); | 
 | 257 |   OmahaRequestActionTestProcessorDelegate delegate; | 
 | 258 |   delegate.loop_ = loop; | 
 | 259 |   ActionProcessor processor; | 
 | 260 |   processor.set_delegate(&delegate); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 261 |   processor.EnqueueAction(&action); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 262 |  | 
 | 263 |   g_timeout_add(0, &StartProcessorInRunLoop, &processor); | 
 | 264 |   g_main_loop_run(loop); | 
 | 265 |   g_main_loop_unref(loop); | 
 | 266 |   EXPECT_FALSE(processor.IsRunning()); | 
 | 267 | } | 
 | 268 |  | 
 | 269 | TEST(OmahaRequestActionTest, InvalidXmlTest) { | 
 | 270 |   OmahaRequestParams params("machine_id", | 
 | 271 |                             "user_id", | 
 | 272 |                             OmahaRequestParams::kOsPlatform, | 
 | 273 |                             OmahaRequestParams::kOsVersion, | 
 | 274 |                             "service_pack", | 
 | 275 |                             "x86-generic", | 
 | 276 |                             OmahaRequestParams::kAppId, | 
 | 277 |                             "0.1.0.0", | 
 | 278 |                             "en-US", | 
 | 279 |                             "unittest_track", | 
 | 280 |                             "http://url"); | 
 | 281 |   OmahaResponse response; | 
 | 282 |   ASSERT_FALSE( | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 283 |       TestUpdateCheck(params, | 
 | 284 |                       "invalid xml>", | 
 | 285 |                       false, | 
 | 286 |                       &response, | 
 | 287 |                       NULL)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 288 |   EXPECT_FALSE(response.update_exists); | 
 | 289 | } | 
 | 290 |  | 
 | 291 | TEST(OmahaRequestActionTest, MissingStatusTest) { | 
 | 292 |   OmahaRequestParams params("machine_id", | 
 | 293 |                             "user_id", | 
 | 294 |                             OmahaRequestParams::kOsPlatform, | 
 | 295 |                             OmahaRequestParams::kOsVersion, | 
 | 296 |                             "service_pack", | 
 | 297 |                             "x86-generic", | 
 | 298 |                             OmahaRequestParams::kAppId, | 
 | 299 |                             "0.1.0.0", | 
 | 300 |                             "en-US", | 
 | 301 |                             "unittest_track", | 
 | 302 |                             "http://url"); | 
 | 303 |   OmahaResponse response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 304 |   ASSERT_FALSE(TestUpdateCheck( | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 305 |       params, | 
 | 306 |       "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " | 
 | 307 |       "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " | 
 | 308 |       "appid=\"foo\" status=\"ok\"><ping " | 
 | 309 |       "status=\"ok\"/><updatecheck/></app></gupdate>", | 
 | 310 |       false, | 
 | 311 |       &response, | 
 | 312 |       NULL)); | 
 | 313 |   EXPECT_FALSE(response.update_exists); | 
 | 314 | } | 
 | 315 |  | 
 | 316 | TEST(OmahaRequestActionTest, InvalidStatusTest) { | 
 | 317 |   OmahaRequestParams params("machine_id", | 
 | 318 |                             "user_id", | 
 | 319 |                             OmahaRequestParams::kOsPlatform, | 
 | 320 |                             OmahaRequestParams::kOsVersion, | 
 | 321 |                             "service_pack", | 
 | 322 |                             "x86-generic", | 
 | 323 |                             OmahaRequestParams::kAppId, | 
 | 324 |                             "0.1.0.0", | 
 | 325 |                             "en-US", | 
 | 326 |                             "unittest_track", | 
 | 327 |                             "http://url"); | 
 | 328 |   OmahaResponse response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 329 |   ASSERT_FALSE(TestUpdateCheck( | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 330 |       params, | 
 | 331 |       "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " | 
 | 332 |       "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " | 
 | 333 |       "appid=\"foo\" status=\"ok\"><ping " | 
 | 334 |       "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>", | 
 | 335 |       false, | 
 | 336 |       &response, | 
 | 337 |       NULL)); | 
 | 338 |   EXPECT_FALSE(response.update_exists); | 
 | 339 | } | 
 | 340 |  | 
 | 341 | TEST(OmahaRequestActionTest, MissingNodesetTest) { | 
 | 342 |   OmahaRequestParams params("machine_id", | 
 | 343 |                             "user_id", | 
 | 344 |                             OmahaRequestParams::kOsPlatform, | 
 | 345 |                             OmahaRequestParams::kOsVersion, | 
 | 346 |                             "service_pack", | 
 | 347 |                             "x86-generic", | 
 | 348 |                             OmahaRequestParams::kAppId, | 
 | 349 |                             "0.1.0.0", | 
 | 350 |                             "en-US", | 
 | 351 |                             "unittest_track", | 
 | 352 |                             "http://url"); | 
 | 353 |   OmahaResponse response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 354 |   ASSERT_FALSE(TestUpdateCheck( | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 355 |       params, | 
 | 356 |       "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " | 
 | 357 |       "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " | 
 | 358 |       "appid=\"foo\" status=\"ok\"><ping " | 
 | 359 |       "status=\"ok\"/></app></gupdate>", | 
 | 360 |       false, | 
 | 361 |       &response, | 
 | 362 |       NULL)); | 
 | 363 |   EXPECT_FALSE(response.update_exists); | 
 | 364 | } | 
 | 365 |  | 
 | 366 | TEST(OmahaRequestActionTest, MissingFieldTest) { | 
 | 367 |   OmahaRequestParams params("machine_id", | 
 | 368 |                             "user_id", | 
 | 369 |                             OmahaRequestParams::kOsPlatform, | 
 | 370 |                             OmahaRequestParams::kOsVersion, | 
 | 371 |                             "service_pack", | 
 | 372 |                             "x86-generic", | 
 | 373 |                             OmahaRequestParams::kAppId, | 
 | 374 |                             "0.1.0.0", | 
 | 375 |                             "en-US", | 
 | 376 |                             "unittest_track", | 
 | 377 |                             "http://url"); | 
 | 378 |   OmahaResponse response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 379 |   ASSERT_TRUE(TestUpdateCheck(params, | 
 | 380 |                               string("<?xml version=\"1.0\" " | 
 | 381 |                                      "encoding=\"UTF-8\"?><gupdate " | 
 | 382 |                                      "xmlns=\"http://www.google.com/" | 
 | 383 |                                      "update2/response\" " | 
 | 384 |                                      "protocol=\"2.0\"><app appid=\"") + | 
 | 385 |                               OmahaRequestParams::kAppId | 
 | 386 |                               + "\" status=\"ok\"><ping " | 
 | 387 |                               "status=\"ok\"/><updatecheck " | 
 | 388 |                               "DisplayVersion=\"1.2.3.4\" " | 
 | 389 |                               "Prompt=\"false\" " | 
 | 390 |                               "codebase=\"http://code/base\" " | 
 | 391 |                               "hash=\"HASH1234=\" needsadmin=\"true\" " | 
 | 392 |                               "size=\"123\" " | 
 | 393 |                               "status=\"ok\"/></app></gupdate>", | 
 | 394 |                               true, | 
 | 395 |                               &response, | 
 | 396 |                               NULL)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 397 |   EXPECT_TRUE(response.update_exists); | 
 | 398 |   EXPECT_EQ("1.2.3.4", response.display_version); | 
 | 399 |   EXPECT_EQ("http://code/base", response.codebase); | 
 | 400 |   EXPECT_EQ("", response.more_info_url); | 
 | 401 |   EXPECT_EQ("HASH1234=", response.hash); | 
 | 402 |   EXPECT_EQ(123, response.size); | 
 | 403 |   EXPECT_TRUE(response.needs_admin); | 
 | 404 |   EXPECT_FALSE(response.prompt); | 
 | 405 | } | 
 | 406 |  | 
 | 407 | namespace { | 
 | 408 | class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { | 
 | 409 |  public: | 
 | 410 |   void ProcessingStopped(const ActionProcessor* processor) { | 
 | 411 |     ASSERT_TRUE(loop_); | 
 | 412 |     g_main_loop_quit(loop_); | 
 | 413 |   } | 
 | 414 |   GMainLoop *loop_; | 
 | 415 | }; | 
 | 416 |  | 
 | 417 | gboolean TerminateTransferTestStarter(gpointer data) { | 
 | 418 |   ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); | 
 | 419 |   processor->StartProcessing(); | 
 | 420 |   CHECK(processor->IsRunning()); | 
 | 421 |   processor->StopProcessing(); | 
 | 422 |   return FALSE; | 
 | 423 | } | 
 | 424 | }  // namespace {} | 
 | 425 |  | 
 | 426 | TEST(OmahaRequestActionTest, TerminateTransferTest) { | 
 | 427 |   OmahaRequestParams params("",  // machine_id | 
 | 428 |                             "",  // usr_id | 
 | 429 |                             OmahaRequestParams::kOsPlatform, | 
 | 430 |                             OmahaRequestParams::kOsVersion, | 
 | 431 |                             "",  // os_sp | 
 | 432 |                             "",  // os_board | 
 | 433 |                             OmahaRequestParams::kAppId, | 
 | 434 |                             "0.1.0.0", | 
 | 435 |                             "en-US", | 
 | 436 |                             "unittest", | 
 | 437 |                             "http://url"); | 
 | 438 |   string http_response("doesn't matter"); | 
 | 439 |   GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 
 | 440 |  | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 441 |   OmahaRequestAction action(params, NULL, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 442 |                             new MockHttpFetcher(http_response.data(), | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 443 |                                                 http_response.size())); | 
 | 444 |   TerminateEarlyTestProcessorDelegate delegate; | 
 | 445 |   delegate.loop_ = loop; | 
 | 446 |   ActionProcessor processor; | 
 | 447 |   processor.set_delegate(&delegate); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 448 |   processor.EnqueueAction(&action); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 449 |  | 
 | 450 |   g_timeout_add(0, &TerminateTransferTestStarter, &processor); | 
 | 451 |   g_main_loop_run(loop); | 
 | 452 |   g_main_loop_unref(loop); | 
 | 453 | } | 
 | 454 |  | 
 | 455 | TEST(OmahaRequestActionTest, XmlEncodeTest) { | 
 | 456 |   EXPECT_EQ("ab", XmlEncode("ab")); | 
 | 457 |   EXPECT_EQ("a<b", XmlEncode("a<b")); | 
 | 458 |   EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); | 
 | 459 |   EXPECT_EQ("<&>", XmlEncode("<&>")); | 
 | 460 |   EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>")); | 
 | 461 |  | 
 | 462 |   vector<char> post_data; | 
 | 463 |  | 
 | 464 |   // Make sure XML Encode is being called on the params | 
 | 465 |   OmahaRequestParams params("testthemachine<id", | 
 | 466 |                             "testtheuser_id<", | 
 | 467 |                             OmahaRequestParams::kOsPlatform, | 
 | 468 |                             OmahaRequestParams::kOsVersion, | 
 | 469 |                             "testtheservice_pack>", | 
 | 470 |                             "x86 generic", | 
 | 471 |                             OmahaRequestParams::kAppId, | 
 | 472 |                             "0.1.0.0", | 
 | 473 |                             "en-US", | 
 | 474 |                             "unittest_track", | 
 | 475 |                             "http://url"); | 
 | 476 |   OmahaResponse response; | 
 | 477 |   ASSERT_FALSE( | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 478 |       TestUpdateCheck(params, | 
 | 479 |                       "invalid xml>", | 
 | 480 |                       false, | 
 | 481 |                       &response, | 
 | 482 |                       &post_data)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 483 |   // convert post_data to string | 
 | 484 |   string post_str(&post_data[0], post_data.size()); | 
 | 485 |   EXPECT_NE(post_str.find("testthemachine<id"), string::npos); | 
 | 486 |   EXPECT_EQ(post_str.find("testthemachine<id"), string::npos); | 
 | 487 |   EXPECT_NE(post_str.find("testtheuser_id&lt;"), string::npos); | 
 | 488 |   EXPECT_EQ(post_str.find("testtheuser_id<"), string::npos); | 
 | 489 |   EXPECT_NE(post_str.find("testtheservice_pack>"), string::npos); | 
 | 490 |   EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos); | 
 | 491 |   EXPECT_NE(post_str.find("x86 generic"), string::npos); | 
 | 492 | } | 
 | 493 |  | 
 | 494 | TEST(OmahaRequestActionTest, XmlDecodeTest) { | 
 | 495 |   OmahaRequestParams params("machine_id", | 
 | 496 |                             "user_id", | 
 | 497 |                             OmahaRequestParams::kOsPlatform, | 
 | 498 |                             OmahaRequestParams::kOsVersion, | 
 | 499 |                             "service_pack", | 
 | 500 |                             "x86-generic", | 
 | 501 |                             OmahaRequestParams::kAppId, | 
 | 502 |                             "0.1.0.0", | 
 | 503 |                             "en-US", | 
 | 504 |                             "unittest_track", | 
 | 505 |                             "http://url"); | 
 | 506 |   OmahaResponse response; | 
 | 507 |   ASSERT_TRUE( | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 508 |       TestUpdateCheck(params, | 
 | 509 |                       GetUpdateResponse(OmahaRequestParams::kAppId, | 
 | 510 |                                         "1.2.3.4",  // version | 
 | 511 |                                         "testthe<url",  // more info | 
 | 512 |                                         "true",  // prompt | 
 | 513 |                                         "testthe&codebase",  // dl url | 
 | 514 |                                         "HASH1234=", // checksum | 
 | 515 |                                         "false",  // needs admin | 
 | 516 |                                         "123"),  // size | 
 | 517 |                       true, | 
 | 518 |                       &response, | 
 | 519 |                       NULL)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 520 |  | 
 | 521 |   EXPECT_EQ(response.more_info_url, "testthe<url"); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 522 |   EXPECT_EQ(response.codebase, "testthe&codebase"); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 523 | } | 
 | 524 |  | 
 | 525 | TEST(OmahaRequestActionTest, ParseIntTest) { | 
 | 526 |   OmahaRequestParams params("machine_id", | 
 | 527 |                             "user_id", | 
 | 528 |                             OmahaRequestParams::kOsPlatform, | 
 | 529 |                             OmahaRequestParams::kOsVersion, | 
 | 530 |                             "service_pack", | 
 | 531 |                             "the_board", | 
 | 532 |                             OmahaRequestParams::kAppId, | 
 | 533 |                             "0.1.0.0", | 
 | 534 |                             "en-US", | 
 | 535 |                             "unittest_track", | 
 | 536 |                             "http://url"); | 
 | 537 |   OmahaResponse response; | 
 | 538 |   ASSERT_TRUE( | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 539 |       TestUpdateCheck(params, | 
 | 540 |                       GetUpdateResponse(OmahaRequestParams::kAppId, | 
 | 541 |                                         "1.2.3.4",  // version | 
 | 542 |                                         "theurl",  // more info | 
 | 543 |                                         "true",  // prompt | 
 | 544 |                                         "thecodebase",  // dl url | 
 | 545 |                                         "HASH1234=", // checksum | 
 | 546 |                                         "false",  // needs admin | 
 | 547 |                                         // overflows int32: | 
 | 548 |                                         "123123123123123"),  // size | 
 | 549 |                       true, | 
 | 550 |                       &response, | 
 | 551 |                       NULL)); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 552 |  | 
 | 553 |   EXPECT_EQ(response.size, 123123123123123ll); | 
 | 554 | } | 
 | 555 |  | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 556 | TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) { | 
 | 557 |   vector<char> post_data; | 
 | 558 |   OmahaRequestParams params("machine_id", | 
 | 559 |                             "user_id", | 
 | 560 |                             OmahaRequestParams::kOsPlatform, | 
 | 561 |                             OmahaRequestParams::kOsVersion, | 
 | 562 |                             "service_pack", | 
 | 563 |                             "x86-generic", | 
 | 564 |                             OmahaRequestParams::kAppId, | 
 | 565 |                             "0.1.0.0", | 
 | 566 |                             "en-US", | 
 | 567 |                             "unittest_track", | 
 | 568 |                             "http://url"); | 
 | 569 |   OmahaResponse response; | 
 | 570 |   ASSERT_FALSE(TestUpdateCheck(params, | 
 | 571 |                                "invalid xml>", | 
 | 572 |                                false, | 
 | 573 |                                &response, | 
 | 574 |                                &post_data)); | 
 | 575 |   // convert post_data to string | 
 | 576 |   string post_str(&post_data[0], post_data.size()); | 
 | 577 |   EXPECT_NE(post_str.find("        <o:ping active=\"0\"></o:ping>\n" | 
 | 578 |                           "        <o:updatecheck></o:updatecheck>\n"), | 
 | 579 |             string::npos); | 
 | 580 |   EXPECT_EQ(post_str.find("o:event"), string::npos); | 
 | 581 | } | 
 | 582 |  | 
 | 583 | TEST(OmahaRequestActionTest, FormatEventOutputTest) { | 
 | 584 |   vector<char> post_data; | 
 | 585 |   OmahaRequestParams params("machine_id", | 
 | 586 |                             "user_id", | 
 | 587 |                             OmahaRequestParams::kOsPlatform, | 
 | 588 |                             OmahaRequestParams::kOsVersion, | 
 | 589 |                             "service_pack", | 
 | 590 |                             "x86-generic", | 
 | 591 |                             OmahaRequestParams::kAppId, | 
 | 592 |                             "0.1.0.0", | 
 | 593 |                             "en-US", | 
 | 594 |                             "unittest_track", | 
 | 595 |                             "http://url"); | 
 | 596 |   TestEvent(params, | 
 | 597 |             new OmahaEvent(OmahaEvent::kTypeDownloadComplete, | 
 | 598 |                            OmahaEvent::kResultError, | 
 | 599 |                            5), | 
 | 600 |             "invalid xml>", | 
 | 601 |             &post_data); | 
 | 602 |   // convert post_data to string | 
 | 603 |   string post_str(&post_data[0], post_data.size()); | 
 | 604 |   string expected_event = StringPrintf( | 
 | 605 |       "        <o:event eventtype=\"%d\" eventresult=\"%d\" " | 
 | 606 |       "errorcode=\"%d\"></o:event>\n", | 
 | 607 |       OmahaEvent::kTypeDownloadComplete, | 
 | 608 |       OmahaEvent::kResultError, | 
 | 609 |       5); | 
 | 610 |   EXPECT_NE(post_str.find(expected_event), string::npos); | 
 | 611 |   EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); | 
 | 612 | } | 
 | 613 |  | 
 | 614 | TEST(OmahaRequestActionTest, IsEventTest) { | 
 | 615 |   string http_response("doesn't matter"); | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 616 |   OmahaRequestParams params("machine_id", | 
 | 617 |                             "user_id", | 
 | 618 |                             OmahaRequestParams::kOsPlatform, | 
 | 619 |                             OmahaRequestParams::kOsVersion, | 
 | 620 |                             "service_pack", | 
 | 621 |                             "x86-generic", | 
 | 622 |                             OmahaRequestParams::kAppId, | 
 | 623 |                             "0.1.0.0", | 
 | 624 |                             "en-US", | 
 | 625 |                             "unittest_track", | 
 | 626 |                             "http://url"); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 627 |  | 
 | 628 |   OmahaRequestAction update_check_action( | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 629 |       params, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 630 |       NULL, | 
 | 631 |       new MockHttpFetcher(http_response.data(), | 
 | 632 |                           http_response.size())); | 
 | 633 |   EXPECT_FALSE(update_check_action.IsEvent()); | 
 | 634 |  | 
 | 635 |   OmahaRequestAction event_action( | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 636 |       params, | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 637 |       new OmahaEvent(OmahaEvent::kTypeInstallComplete, | 
 | 638 |                      OmahaEvent::kResultError, | 
 | 639 |                      0), | 
 | 640 |       new MockHttpFetcher(http_response.data(), | 
 | 641 |                           http_response.size())); | 
 | 642 |   EXPECT_TRUE(event_action.IsEvent()); | 
 | 643 | } | 
 | 644 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 645 | }  // namespace chromeos_update_engine |