1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "update_engine/omaha_request_action.h" 18 19 #include <stdint.h> 20 21 #include <memory> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 26 #include <base/bind.h> 27 #include <base/files/file_util.h> 28 #include <base/files/scoped_temp_dir.h> 29 #include <base/memory/ptr_util.h> 30 #include <base/strings/string_number_conversions.h> 31 #include <base/strings/string_util.h> 32 #include <base/strings/stringprintf.h> 33 #include <base/time/time.h> 34 #include <brillo/message_loops/fake_message_loop.h> 35 #include <brillo/message_loops/message_loop.h> 36 #include <brillo/message_loops/message_loop_utils.h> 37 #include <gtest/gtest.h> 38 #include <policy/libpolicy.h> 39 #include <policy/mock_libpolicy.h> 40 41 #include "update_engine/common/action_pipe.h" 42 #include "update_engine/common/constants.h" 43 #include "update_engine/common/fake_prefs.h" 44 #include "update_engine/common/hash_calculator.h" 45 #include "update_engine/common/mock_http_fetcher.h" 46 #include "update_engine/common/platform_constants.h" 47 #include "update_engine/common/prefs.h" 48 #include "update_engine/common/test_utils.h" 49 #include "update_engine/fake_system_state.h" 50 #include "update_engine/metrics_reporter_interface.h" 51 #include "update_engine/mock_connection_manager.h" 52 #include "update_engine/mock_payload_state.h" 53 #include "update_engine/omaha_request_params.h" 54 #include "update_engine/update_manager/rollback_prefs.h" 55 56 using base::Time; 57 using base::TimeDelta; 58 using chromeos_update_manager::kRollforwardInfinity; 59 using std::string; 60 using std::vector; 61 using testing::_; 62 using testing::AllOf; 63 using testing::AnyNumber; 64 using testing::DoAll; 65 using testing::Ge; 66 using testing::Le; 67 using testing::NiceMock; 68 using testing::Return; 69 using testing::ReturnPointee; 70 using testing::ReturnRef; 71 using testing::SaveArg; 72 using testing::SetArgPointee; 73 74 namespace { 75 76 static_assert(kRollforwardInfinity == 0xfffffffe, 77 "Don't change the value of kRollforward infinity unless its " 78 "size has been changed in firmware."); 79 80 const char kCurrentVersion[] = "0.1.0.0"; 81 const char kTestAppId[] = "test-app-id"; 82 const char kTestAppId2[] = "test-app2-id"; 83 const char kTestAppIdSkipUpdatecheck[] = "test-app-id-skip-updatecheck"; 84 85 // This is a helper struct to allow unit tests build an update response with the 86 // values they care about. 87 struct FakeUpdateResponse { 88 string GetRollbackVersionAttributes() const { 89 return (rollback ? " _rollback=\"true\"" : "") + 90 (!rollback_firmware_version.empty() 91 ? " _firmware_version=\"" + rollback_firmware_version + "\"" 92 : "") + 93 (!rollback_kernel_version.empty() 94 ? " _kernel_version=\"" + rollback_kernel_version + "\"" 95 : ""); 96 } 97 98 string GetNoUpdateResponse() const { 99 string entity_str; 100 if (include_entity) 101 entity_str = "<!DOCTYPE response [<!ENTITY CrOS \"ChromeOS\">]>"; 102 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + entity_str + 103 "<response protocol=\"3.0\">" 104 "<daystart elapsed_seconds=\"100\"/>" 105 "<app appid=\"" + 106 app_id + "\" " + 107 (include_cohorts 108 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint + 109 "\" cohortname=\"" + cohortname + "\" " 110 : "") + 111 " status=\"ok\">" 112 "<ping status=\"ok\"/>" 113 "<updatecheck status=\"noupdate\"/></app>" + 114 (multi_app_no_update 115 ? "<app appid=\"" + app_id2 + 116 "\"><updatecheck status=\"noupdate\"/></app>" 117 : "") + 118 "</response>"; 119 } 120 121 string GetUpdateResponse() const { 122 return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " 123 "protocol=\"3.0\">" 124 "<daystart elapsed_seconds=\"100\"" + 125 (elapsed_days.empty() ? "" 126 : (" elapsed_days=\"" + elapsed_days + "\"")) + 127 "/>" 128 "<app appid=\"" + 129 app_id + "\" " + 130 (include_cohorts 131 ? "cohort=\"" + cohort + "\" cohorthint=\"" + cohorthint + 132 "\" cohortname=\"" + cohortname + "\" " 133 : "") + 134 " status=\"ok\">" 135 "<ping status=\"ok\"/><updatecheck status=\"ok\"" + 136 GetRollbackVersionAttributes() + ">" + "<urls><url codebase=\"" + 137 codebase + 138 "\"/></urls>" 139 "<manifest version=\"" + 140 version + 141 "\">" 142 "<packages><package hash=\"not-used\" name=\"" + 143 filename + "\" size=\"" + base::Int64ToString(size) + 144 "\" hash_sha256=\"" + hash + "\"/>" + 145 (multi_package ? "<package name=\"package2\" size=\"222\" " 146 "hash_sha256=\"hash2\"/>" 147 : "") + 148 "</packages>" 149 "<actions><action event=\"postinstall\" MetadataSize=\"11" + 150 (multi_package ? ":22" : "") + "\" MoreInfo=\"" + more_info_url + 151 "\" Prompt=\"" + prompt + 152 "\" " 153 "IsDeltaPayload=\"true" + 154 (multi_package ? ":false" : "") + 155 "\" " 156 "MaxDaysToScatter=\"" + 157 max_days_to_scatter + 158 "\" " 159 "sha256=\"not-used\" " + 160 (deadline.empty() ? "" : ("deadline=\"" + deadline + "\" ")) + 161 (disable_p2p_for_downloading ? "DisableP2PForDownloading=\"true\" " 162 : "") + 163 (disable_p2p_for_sharing ? "DisableP2PForSharing=\"true\" " : "") + 164 (powerwash ? "Powerwash=\"true\" " : "") + 165 "/></actions></manifest></updatecheck></app>" + 166 (multi_app 167 ? "<app appid=\"" + app_id2 + "\"" + 168 (include_cohorts ? " cohort=\"cohort2\"" : "") + 169 "><updatecheck status=\"ok\"><urls><url codebase=\"" + 170 codebase2 + "\"/></urls><manifest version=\"" + version2 + 171 "\"><packages>" 172 "<package name=\"package3\" size=\"333\" " 173 "hash_sha256=\"hash3\"/></packages>" 174 "<actions><action event=\"postinstall\" " + 175 (multi_app_self_update 176 ? "noupdate=\"true\" IsDeltaPayload=\"true\" " 177 : "IsDeltaPayload=\"false\" ") + 178 "MetadataSize=\"33\"/></actions>" 179 "</manifest></updatecheck></app>" 180 : "") + 181 (multi_app_no_update 182 ? "<app><updatecheck status=\"noupdate\"/></app>" 183 : "") + 184 (multi_app_skip_updatecheck 185 ? "<app appid=\"" + app_id_skip_updatecheck + "\"></app>" 186 : "") + 187 "</response>"; 188 } 189 190 // Return the payload URL, which is split in two fields in the XML response. 191 string GetPayloadUrl() { return codebase + filename; } 192 193 string app_id = kTestAppId; 194 string app_id2 = kTestAppId2; 195 string app_id_skip_updatecheck = kTestAppIdSkipUpdatecheck; 196 string current_version = kCurrentVersion; 197 string version = "1.2.3.4"; 198 string version2 = "2.3.4.5"; 199 string more_info_url = "http://more/info"; 200 string prompt = "true"; 201 string codebase = "http://code/base/"; 202 string codebase2 = "http://code/base/2/"; 203 string filename = "file.signed"; 204 string hash = "4841534831323334"; 205 uint64_t size = 123; 206 string deadline = ""; 207 string max_days_to_scatter = "7"; 208 string elapsed_days = "42"; 209 210 // P2P setting defaults to allowed. 211 bool disable_p2p_for_downloading = false; 212 bool disable_p2p_for_sharing = false; 213 214 bool powerwash = false; 215 216 // Omaha cohorts settings. 217 bool include_cohorts = false; 218 string cohort = ""; 219 string cohorthint = ""; 220 string cohortname = ""; 221 222 // Whether to include the CrOS <!ENTITY> in the XML response. 223 bool include_entity = false; 224 225 // Whether to include more than one app. 226 bool multi_app = false; 227 // Whether to include an app with noupdate="true". 228 bool multi_app_self_update = false; 229 // Whether to include an additional app with status="noupdate". 230 bool multi_app_no_update = false; 231 // Whether to include an additional app with no updatecheck tag. 232 bool multi_app_skip_updatecheck = false; 233 // Whether to include more than one package in an app. 234 bool multi_package = false; 235 236 // Whether the payload is a rollback. 237 bool rollback = false; 238 // The verified boot firmware key version for the rollback image. 239 string rollback_firmware_version = ""; 240 // The verified boot kernel key version for the rollback image. 241 string rollback_kernel_version = ""; 242 }; 243 244 } // namespace 245 246 namespace chromeos_update_engine { 247 248 class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate { 249 public: 250 OmahaRequestActionTestProcessorDelegate() 251 : expected_code_(ErrorCode::kSuccess), 252 interactive_(false), 253 test_http_fetcher_headers_(false) {} 254 ~OmahaRequestActionTestProcessorDelegate() override = default; 255 256 void ProcessingDone(const ActionProcessor* processor, 257 ErrorCode code) override { 258 brillo::MessageLoop::current()->BreakLoop(); 259 } 260 261 void ActionCompleted(ActionProcessor* processor, 262 AbstractAction* action, 263 ErrorCode code) override { 264 // Make sure actions always succeed. 265 if (action->Type() == OmahaRequestAction::StaticType()) { 266 EXPECT_EQ(expected_code_, code); 267 // Check that the headers were set in the fetcher during the action. Note 268 // that we set this request as "interactive". 269 auto fetcher = static_cast<const MockHttpFetcher*>( 270 static_cast<OmahaRequestAction*>(action)->http_fetcher_.get()); 271 272 if (test_http_fetcher_headers_) { 273 EXPECT_EQ(interactive_ ? "fg" : "bg", 274 fetcher->GetHeader("X-Goog-Update-Interactivity")); 275 EXPECT_EQ(kTestAppId, fetcher->GetHeader("X-Goog-Update-AppId")); 276 EXPECT_NE("", fetcher->GetHeader("X-Goog-Update-Updater")); 277 } 278 post_data_ = fetcher->post_data(); 279 } else if (action->Type() == 280 ObjectCollectorAction<OmahaResponse>::StaticType()) { 281 EXPECT_EQ(ErrorCode::kSuccess, code); 282 auto collector_action = 283 static_cast<ObjectCollectorAction<OmahaResponse>*>(action); 284 omaha_response_.reset(new OmahaResponse(collector_action->object())); 285 EXPECT_TRUE(omaha_response_); 286 } else { 287 EXPECT_EQ(ErrorCode::kSuccess, code); 288 } 289 } 290 ErrorCode expected_code_; 291 brillo::Blob post_data_; 292 bool interactive_; 293 bool test_http_fetcher_headers_; 294 std::unique_ptr<OmahaResponse> omaha_response_; 295 }; 296 297 class OmahaRequestActionTest : public ::testing::Test { 298 protected: 299 void SetUp() override { 300 request_params_.set_os_sp("service_pack"); 301 request_params_.set_os_board("x86-generic"); 302 request_params_.set_app_id(kTestAppId); 303 request_params_.set_app_version(kCurrentVersion); 304 request_params_.set_app_lang("en-US"); 305 request_params_.set_current_channel("unittest"); 306 request_params_.set_target_channel("unittest"); 307 request_params_.set_hwid("OEM MODEL 09235 7471"); 308 request_params_.set_fw_version("ChromeOSFirmware.1.0"); 309 request_params_.set_ec_version("0X0A1"); 310 request_params_.set_delta_okay(true); 311 request_params_.set_interactive(false); 312 request_params_.set_update_url("http://url"); 313 request_params_.set_target_version_prefix(""); 314 request_params_.set_rollback_allowed(false); 315 request_params_.set_is_powerwash_allowed(false); 316 request_params_.set_is_install(false); 317 request_params_.set_dlc_module_ids({}); 318 319 fake_system_state_.set_request_params(&request_params_); 320 fake_system_state_.set_prefs(&fake_prefs_); 321 } 322 323 // Returns true iff an output response was obtained from the 324 // OmahaRequestAction. |prefs| may be null, in which case a local MockPrefs 325 // is used. |payload_state| may be null, in which case a local mock is used. 326 // |p2p_manager| may be null, in which case a local mock is used. 327 // |connection_manager| may be null, in which case a local mock is used. 328 // out_response may be null. If |fail_http_response_code| is non-negative, 329 // the transfer will fail with that code. |ping_only| is passed through to the 330 // OmahaRequestAction constructor. out_post_data may be null; if non-null, the 331 // post-data received by the mock HttpFetcher is returned. 332 // 333 // The |expected_check_result|, |expected_check_reaction| and 334 // |expected_error_code| parameters are for checking expectations 335 // about reporting UpdateEngine.Check.{Result,Reaction,DownloadError} 336 // UMA statistics. Use the appropriate ::kUnset value to specify that 337 // the given metric should not be reported. 338 bool TestUpdateCheck(const string& http_response, 339 int fail_http_response_code, 340 bool ping_only, 341 bool is_consumer_device, 342 int rollback_allowed_milestones, 343 bool is_policy_loaded, 344 ErrorCode expected_code, 345 metrics::CheckResult expected_check_result, 346 metrics::CheckReaction expected_check_reaction, 347 metrics::DownloadErrorCode expected_download_error_code, 348 OmahaResponse* out_response, 349 brillo::Blob* out_post_data); 350 351 // Overload of TestUpdateCheck that does not supply |is_consumer_device| or 352 // |rollback_allowed_milestones| which are only required for rollback tests. 353 bool TestUpdateCheck(const string& http_response, 354 int fail_http_response_code, 355 bool ping_only, 356 ErrorCode expected_code, 357 metrics::CheckResult expected_check_result, 358 metrics::CheckReaction expected_check_reaction, 359 metrics::DownloadErrorCode expected_download_error_code, 360 OmahaResponse* out_response, 361 brillo::Blob* out_post_data); 362 363 void TestRollbackCheck(bool is_consumer_device, 364 int rollback_allowed_milestones, 365 bool is_policy_loaded, 366 OmahaResponse* out_response); 367 368 void TestEvent(OmahaEvent* event, 369 const string& http_response, 370 brillo::Blob* out_post_data); 371 372 // Runs and checks a ping test. |ping_only| indicates whether it should send 373 // only a ping or also an updatecheck. 374 void PingTest(bool ping_only); 375 376 // InstallDate test helper function. 377 bool InstallDateParseHelper(const string& elapsed_days, 378 OmahaResponse* response); 379 380 // P2P test helper function. 381 void P2PTest(bool initial_allow_p2p_for_downloading, 382 bool initial_allow_p2p_for_sharing, 383 bool omaha_disable_p2p_for_downloading, 384 bool omaha_disable_p2p_for_sharing, 385 bool payload_state_allow_p2p_attempt, 386 bool expect_p2p_client_lookup, 387 const string& p2p_client_result_url, 388 bool expected_allow_p2p_for_downloading, 389 bool expected_allow_p2p_for_sharing, 390 const string& expected_p2p_url); 391 392 FakeSystemState fake_system_state_; 393 FakeUpdateResponse fake_update_response_; 394 // Used by all tests. 395 OmahaRequestParams request_params_{&fake_system_state_}; 396 397 FakePrefs fake_prefs_; 398 399 OmahaRequestActionTestProcessorDelegate delegate_; 400 401 bool test_http_fetcher_headers_{false}; 402 }; 403 404 bool OmahaRequestActionTest::TestUpdateCheck( 405 const string& http_response, 406 int fail_http_response_code, 407 bool ping_only, 408 bool is_consumer_device, 409 int rollback_allowed_milestones, 410 bool is_policy_loaded, 411 ErrorCode expected_code, 412 metrics::CheckResult expected_check_result, 413 metrics::CheckReaction expected_check_reaction, 414 metrics::DownloadErrorCode expected_download_error_code, 415 OmahaResponse* out_response, 416 brillo::Blob* out_post_data) { 417 brillo::FakeMessageLoop loop(nullptr); 418 loop.SetAsCurrent(); 419 auto fetcher = std::make_unique<MockHttpFetcher>( 420 http_response.data(), http_response.size(), nullptr); 421 if (fail_http_response_code >= 0) { 422 fetcher->FailTransfer(fail_http_response_code); 423 } 424 // This ensures the tests didn't forget to update fake_system_state_ if they 425 // are not using the default request_params_. 426 EXPECT_EQ(&request_params_, fake_system_state_.request_params()); 427 428 auto omaha_request_action = std::make_unique<OmahaRequestAction>( 429 &fake_system_state_, nullptr, std::move(fetcher), ping_only); 430 431 auto mock_policy_provider = 432 std::make_unique<NiceMock<policy::MockPolicyProvider>>(); 433 EXPECT_CALL(*mock_policy_provider, IsConsumerDevice()) 434 .WillRepeatedly(Return(is_consumer_device)); 435 436 EXPECT_CALL(*mock_policy_provider, device_policy_is_loaded()) 437 .WillRepeatedly(Return(is_policy_loaded)); 438 439 const policy::MockDevicePolicy device_policy; 440 const bool get_allowed_milestone_succeeds = rollback_allowed_milestones >= 0; 441 EXPECT_CALL(device_policy, GetRollbackAllowedMilestones(_)) 442 .WillRepeatedly(DoAll(SetArgPointee<0>(rollback_allowed_milestones), 443 Return(get_allowed_milestone_succeeds))); 444 445 EXPECT_CALL(*mock_policy_provider, GetDevicePolicy()) 446 .WillRepeatedly(ReturnRef(device_policy)); 447 omaha_request_action->policy_provider_ = std::move(mock_policy_provider); 448 449 delegate_.expected_code_ = expected_code; 450 delegate_.interactive_ = request_params_.interactive(); 451 delegate_.test_http_fetcher_headers_ = test_http_fetcher_headers_; 452 ActionProcessor processor; 453 processor.set_delegate(&delegate_); 454 455 auto collector_action = 456 std::make_unique<ObjectCollectorAction<OmahaResponse>>(); 457 BondActions(omaha_request_action.get(), collector_action.get()); 458 processor.EnqueueAction(std::move(omaha_request_action)); 459 processor.EnqueueAction(std::move(collector_action)); 460 461 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(), 462 ReportUpdateCheckMetrics(_, _, _, _)) 463 .Times(AnyNumber()); 464 465 EXPECT_CALL(*fake_system_state_.mock_metrics_reporter(), 466 ReportUpdateCheckMetrics(_, 467 expected_check_result, 468 expected_check_reaction, 469 expected_download_error_code)) 470 .Times(ping_only ? 0 : 1); 471 472 loop.PostTask(base::Bind( 473 [](ActionProcessor* processor) { processor->StartProcessing(); }, 474 base::Unretained(&processor))); 475 loop.Run(); 476 EXPECT_FALSE(loop.PendingTasks()); 477 if (delegate_.omaha_response_ && out_response) 478 *out_response = *delegate_.omaha_response_; 479 if (out_post_data) 480 *out_post_data = delegate_.post_data_; 481 return delegate_.omaha_response_ != nullptr; 482 } 483 484 bool OmahaRequestActionTest::TestUpdateCheck( 485 const string& http_response, 486 int fail_http_response_code, 487 bool ping_only, 488 ErrorCode expected_code, 489 metrics::CheckResult expected_check_result, 490 metrics::CheckReaction expected_check_reaction, 491 metrics::DownloadErrorCode expected_download_error_code, 492 OmahaResponse* out_response, 493 brillo::Blob* out_post_data) { 494 return TestUpdateCheck(http_response, 495 fail_http_response_code, 496 ping_only, 497 true, // is_consumer_device 498 0, // rollback_allowed_milestones 499 false, // is_policy_loaded 500 expected_code, 501 expected_check_result, 502 expected_check_reaction, 503 expected_download_error_code, 504 out_response, 505 out_post_data); 506 } 507 508 void OmahaRequestActionTest::TestRollbackCheck(bool is_consumer_device, 509 int rollback_allowed_milestones, 510 bool is_policy_loaded, 511 OmahaResponse* out_response) { 512 fake_update_response_.deadline = "20101020"; 513 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 514 -1, 515 false, // ping_only 516 is_consumer_device, 517 rollback_allowed_milestones, 518 is_policy_loaded, 519 ErrorCode::kSuccess, 520 metrics::CheckResult::kUpdateAvailable, 521 metrics::CheckReaction::kUpdating, 522 metrics::DownloadErrorCode::kUnset, 523 out_response, 524 nullptr)); 525 ASSERT_TRUE(out_response->update_exists); 526 } 527 528 // Tests Event requests -- they should always succeed. |out_post_data| may be 529 // null; if non-null, the post-data received by the mock HttpFetcher is 530 // returned. 531 void OmahaRequestActionTest::TestEvent(OmahaEvent* event, 532 const string& http_response, 533 brillo::Blob* out_post_data) { 534 brillo::FakeMessageLoop loop(nullptr); 535 loop.SetAsCurrent(); 536 537 auto action = std::make_unique<OmahaRequestAction>( 538 &fake_system_state_, 539 event, 540 std::make_unique<MockHttpFetcher>( 541 http_response.data(), http_response.size(), nullptr), 542 false); 543 ActionProcessor processor; 544 processor.set_delegate(&delegate_); 545 processor.EnqueueAction(std::move(action)); 546 547 loop.PostTask(base::Bind( 548 [](ActionProcessor* processor) { processor->StartProcessing(); }, 549 base::Unretained(&processor))); 550 loop.Run(); 551 EXPECT_FALSE(loop.PendingTasks()); 552 553 if (out_post_data) 554 *out_post_data = delegate_.post_data_; 555 } 556 557 TEST_F(OmahaRequestActionTest, RejectEntities) { 558 OmahaResponse response; 559 fake_update_response_.include_entity = true; 560 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 561 -1, 562 false, // ping_only 563 ErrorCode::kOmahaRequestXMLHasEntityDecl, 564 metrics::CheckResult::kParsingError, 565 metrics::CheckReaction::kUnset, 566 metrics::DownloadErrorCode::kUnset, 567 &response, 568 nullptr)); 569 EXPECT_FALSE(response.update_exists); 570 } 571 572 TEST_F(OmahaRequestActionTest, NoUpdateTest) { 573 OmahaResponse response; 574 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 575 -1, 576 false, // ping_only 577 ErrorCode::kSuccess, 578 metrics::CheckResult::kNoUpdateAvailable, 579 metrics::CheckReaction::kUnset, 580 metrics::DownloadErrorCode::kUnset, 581 &response, 582 nullptr)); 583 EXPECT_FALSE(response.update_exists); 584 } 585 586 TEST_F(OmahaRequestActionTest, MultiAppNoUpdateTest) { 587 OmahaResponse response; 588 fake_update_response_.multi_app_no_update = true; 589 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 590 -1, 591 false, // ping_only 592 ErrorCode::kSuccess, 593 metrics::CheckResult::kNoUpdateAvailable, 594 metrics::CheckReaction::kUnset, 595 metrics::DownloadErrorCode::kUnset, 596 &response, 597 nullptr)); 598 EXPECT_FALSE(response.update_exists); 599 } 600 601 TEST_F(OmahaRequestActionTest, MultiAppNoPartialUpdateTest) { 602 OmahaResponse response; 603 fake_update_response_.multi_app_no_update = true; 604 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 605 -1, 606 false, // ping_only 607 ErrorCode::kSuccess, 608 metrics::CheckResult::kNoUpdateAvailable, 609 metrics::CheckReaction::kUnset, 610 metrics::DownloadErrorCode::kUnset, 611 &response, 612 nullptr)); 613 EXPECT_FALSE(response.update_exists); 614 } 615 616 TEST_F(OmahaRequestActionTest, NoSelfUpdateTest) { 617 OmahaResponse response; 618 ASSERT_TRUE(TestUpdateCheck( 619 "<response><app><updatecheck status=\"ok\"><manifest><actions><action " 620 "event=\"postinstall\" noupdate=\"true\"/></actions>" 621 "</manifest></updatecheck></app></response>", 622 -1, 623 false, // ping_only 624 ErrorCode::kSuccess, 625 metrics::CheckResult::kNoUpdateAvailable, 626 metrics::CheckReaction::kUnset, 627 metrics::DownloadErrorCode::kUnset, 628 &response, 629 nullptr)); 630 EXPECT_FALSE(response.update_exists); 631 } 632 633 // Test that all the values in the response are parsed in a normal update 634 // response. 635 TEST_F(OmahaRequestActionTest, ValidUpdateTest) { 636 OmahaResponse response; 637 fake_update_response_.deadline = "20101020"; 638 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 639 -1, 640 false, // ping_only 641 ErrorCode::kSuccess, 642 metrics::CheckResult::kUpdateAvailable, 643 metrics::CheckReaction::kUpdating, 644 metrics::DownloadErrorCode::kUnset, 645 &response, 646 nullptr)); 647 EXPECT_TRUE(response.update_exists); 648 EXPECT_EQ(fake_update_response_.version, response.version); 649 EXPECT_EQ("", response.system_version); 650 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), 651 response.packages[0].payload_urls[0]); 652 EXPECT_EQ(fake_update_response_.more_info_url, response.more_info_url); 653 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash); 654 EXPECT_EQ(fake_update_response_.size, response.packages[0].size); 655 EXPECT_EQ(true, response.packages[0].is_delta); 656 EXPECT_EQ(fake_update_response_.prompt == "true", response.prompt); 657 EXPECT_EQ(fake_update_response_.deadline, response.deadline); 658 EXPECT_FALSE(response.powerwash_required); 659 // Omaha cohort attributes are not set in the response, so they should not be 660 // persisted. 661 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohort)); 662 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortHint)); 663 EXPECT_FALSE(fake_prefs_.Exists(kPrefsOmahaCohortName)); 664 } 665 666 TEST_F(OmahaRequestActionTest, MultiPackageUpdateTest) { 667 OmahaResponse response; 668 fake_update_response_.multi_package = true; 669 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 670 -1, 671 false, // ping_only 672 ErrorCode::kSuccess, 673 metrics::CheckResult::kUpdateAvailable, 674 metrics::CheckReaction::kUpdating, 675 metrics::DownloadErrorCode::kUnset, 676 &response, 677 nullptr)); 678 EXPECT_TRUE(response.update_exists); 679 EXPECT_EQ(fake_update_response_.version, response.version); 680 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), 681 response.packages[0].payload_urls[0]); 682 EXPECT_EQ(fake_update_response_.codebase + "package2", 683 response.packages[1].payload_urls[0]); 684 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash); 685 EXPECT_EQ(fake_update_response_.size, response.packages[0].size); 686 EXPECT_EQ(true, response.packages[0].is_delta); 687 EXPECT_EQ(11u, response.packages[0].metadata_size); 688 ASSERT_EQ(2u, response.packages.size()); 689 EXPECT_EQ(string("hash2"), response.packages[1].hash); 690 EXPECT_EQ(222u, response.packages[1].size); 691 EXPECT_EQ(22u, response.packages[1].metadata_size); 692 EXPECT_EQ(false, response.packages[1].is_delta); 693 } 694 695 TEST_F(OmahaRequestActionTest, MultiAppUpdateTest) { 696 OmahaResponse response; 697 fake_update_response_.multi_app = true; 698 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 699 -1, 700 false, // ping_only 701 ErrorCode::kSuccess, 702 metrics::CheckResult::kUpdateAvailable, 703 metrics::CheckReaction::kUpdating, 704 metrics::DownloadErrorCode::kUnset, 705 &response, 706 nullptr)); 707 EXPECT_TRUE(response.update_exists); 708 EXPECT_EQ(fake_update_response_.version, response.version); 709 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), 710 response.packages[0].payload_urls[0]); 711 EXPECT_EQ(fake_update_response_.codebase2 + "package3", 712 response.packages[1].payload_urls[0]); 713 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash); 714 EXPECT_EQ(fake_update_response_.size, response.packages[0].size); 715 EXPECT_EQ(11u, response.packages[0].metadata_size); 716 EXPECT_EQ(true, response.packages[0].is_delta); 717 ASSERT_EQ(2u, response.packages.size()); 718 EXPECT_EQ(string("hash3"), response.packages[1].hash); 719 EXPECT_EQ(333u, response.packages[1].size); 720 EXPECT_EQ(33u, response.packages[1].metadata_size); 721 EXPECT_EQ(false, response.packages[1].is_delta); 722 } 723 724 TEST_F(OmahaRequestActionTest, MultiAppAndSystemUpdateTest) { 725 OmahaResponse response; 726 fake_update_response_.multi_app = true; 727 // trigger the lining up of the app and system versions 728 request_params_.set_system_app_id(fake_update_response_.app_id2); 729 730 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 731 -1, 732 false, // ping_only 733 ErrorCode::kSuccess, 734 metrics::CheckResult::kUpdateAvailable, 735 metrics::CheckReaction::kUpdating, 736 metrics::DownloadErrorCode::kUnset, 737 &response, 738 nullptr)); 739 EXPECT_TRUE(response.update_exists); 740 EXPECT_EQ(fake_update_response_.version, response.version); 741 EXPECT_EQ(fake_update_response_.version2, response.system_version); 742 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), 743 response.packages[0].payload_urls[0]); 744 EXPECT_EQ(fake_update_response_.codebase2 + "package3", 745 response.packages[1].payload_urls[0]); 746 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash); 747 EXPECT_EQ(fake_update_response_.size, response.packages[0].size); 748 EXPECT_EQ(11u, response.packages[0].metadata_size); 749 EXPECT_EQ(true, response.packages[0].is_delta); 750 ASSERT_EQ(2u, response.packages.size()); 751 EXPECT_EQ(string("hash3"), response.packages[1].hash); 752 EXPECT_EQ(333u, response.packages[1].size); 753 EXPECT_EQ(33u, response.packages[1].metadata_size); 754 EXPECT_EQ(false, response.packages[1].is_delta); 755 } 756 757 TEST_F(OmahaRequestActionTest, MultiAppPartialUpdateTest) { 758 OmahaResponse response; 759 fake_update_response_.multi_app = true; 760 fake_update_response_.multi_app_self_update = true; 761 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 762 -1, 763 false, // ping_only 764 ErrorCode::kSuccess, 765 metrics::CheckResult::kUpdateAvailable, 766 metrics::CheckReaction::kUpdating, 767 metrics::DownloadErrorCode::kUnset, 768 &response, 769 nullptr)); 770 EXPECT_TRUE(response.update_exists); 771 EXPECT_EQ(fake_update_response_.version, response.version); 772 EXPECT_EQ("", response.system_version); 773 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), 774 response.packages[0].payload_urls[0]); 775 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash); 776 EXPECT_EQ(fake_update_response_.size, response.packages[0].size); 777 EXPECT_EQ(11u, response.packages[0].metadata_size); 778 ASSERT_EQ(2u, response.packages.size()); 779 EXPECT_EQ(string("hash3"), response.packages[1].hash); 780 EXPECT_EQ(333u, response.packages[1].size); 781 EXPECT_EQ(33u, response.packages[1].metadata_size); 782 EXPECT_EQ(true, response.packages[1].is_delta); 783 } 784 785 TEST_F(OmahaRequestActionTest, MultiAppMultiPackageUpdateTest) { 786 OmahaResponse response; 787 fake_update_response_.multi_app = true; 788 fake_update_response_.multi_package = true; 789 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 790 -1, 791 false, // ping_only 792 ErrorCode::kSuccess, 793 metrics::CheckResult::kUpdateAvailable, 794 metrics::CheckReaction::kUpdating, 795 metrics::DownloadErrorCode::kUnset, 796 &response, 797 nullptr)); 798 EXPECT_TRUE(response.update_exists); 799 EXPECT_EQ(fake_update_response_.version, response.version); 800 EXPECT_EQ("", response.system_version); 801 EXPECT_EQ(fake_update_response_.GetPayloadUrl(), 802 response.packages[0].payload_urls[0]); 803 EXPECT_EQ(fake_update_response_.codebase + "package2", 804 response.packages[1].payload_urls[0]); 805 EXPECT_EQ(fake_update_response_.codebase2 + "package3", 806 response.packages[2].payload_urls[0]); 807 EXPECT_EQ(fake_update_response_.hash, response.packages[0].hash); 808 EXPECT_EQ(fake_update_response_.size, response.packages[0].size); 809 EXPECT_EQ(11u, response.packages[0].metadata_size); 810 EXPECT_EQ(true, response.packages[0].is_delta); 811 ASSERT_EQ(3u, response.packages.size()); 812 EXPECT_EQ(string("hash2"), response.packages[1].hash); 813 EXPECT_EQ(222u, response.packages[1].size); 814 EXPECT_EQ(22u, response.packages[1].metadata_size); 815 EXPECT_EQ(false, response.packages[1].is_delta); 816 EXPECT_EQ(string("hash3"), response.packages[2].hash); 817 EXPECT_EQ(333u, response.packages[2].size); 818 EXPECT_EQ(33u, response.packages[2].metadata_size); 819 EXPECT_EQ(false, response.packages[2].is_delta); 820 } 821 822 TEST_F(OmahaRequestActionTest, PowerwashTest) { 823 OmahaResponse response; 824 fake_update_response_.powerwash = true; 825 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 826 -1, 827 false, // ping_only 828 ErrorCode::kSuccess, 829 metrics::CheckResult::kUpdateAvailable, 830 metrics::CheckReaction::kUpdating, 831 metrics::DownloadErrorCode::kUnset, 832 &response, 833 nullptr)); 834 EXPECT_TRUE(response.update_exists); 835 EXPECT_TRUE(response.powerwash_required); 836 } 837 838 TEST_F(OmahaRequestActionTest, ExtraHeadersSentInteractiveTest) { 839 OmahaResponse response; 840 request_params_.set_interactive(true); 841 test_http_fetcher_headers_ = true; 842 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 843 -1, 844 false, // ping_only 845 ErrorCode::kOmahaRequestXMLParseError, 846 metrics::CheckResult::kParsingError, 847 metrics::CheckReaction::kUnset, 848 metrics::DownloadErrorCode::kUnset, 849 &response, 850 nullptr)); 851 EXPECT_FALSE(response.update_exists); 852 } 853 854 TEST_F(OmahaRequestActionTest, ExtraHeadersSentNoInteractiveTest) { 855 OmahaResponse response; 856 request_params_.set_interactive(false); 857 test_http_fetcher_headers_ = true; 858 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 859 -1, 860 false, // ping_only 861 ErrorCode::kOmahaRequestXMLParseError, 862 metrics::CheckResult::kParsingError, 863 metrics::CheckReaction::kUnset, 864 metrics::DownloadErrorCode::kUnset, 865 &response, 866 nullptr)); 867 EXPECT_FALSE(response.update_exists); 868 } 869 870 TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByConnection) { 871 OmahaResponse response; 872 // Set up a connection manager that doesn't allow a valid update over 873 // the current ethernet connection. 874 MockConnectionManager mock_cm; 875 fake_system_state_.set_connection_manager(&mock_cm); 876 877 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) 878 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kEthernet), 879 SetArgPointee<1>(ConnectionTethering::kUnknown), 880 Return(true))); 881 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kEthernet, _)) 882 .WillRepeatedly(Return(false)); 883 884 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 885 -1, 886 false, // ping_only 887 ErrorCode::kOmahaUpdateIgnoredPerPolicy, 888 metrics::CheckResult::kUpdateAvailable, 889 metrics::CheckReaction::kIgnored, 890 metrics::DownloadErrorCode::kUnset, 891 &response, 892 nullptr)); 893 EXPECT_FALSE(response.update_exists); 894 } 895 896 TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularAllowedByDevicePolicy) { 897 // This test tests that update over cellular is allowed as device policy 898 // says yes. 899 OmahaResponse response; 900 MockConnectionManager mock_cm; 901 902 fake_system_state_.set_connection_manager(&mock_cm); 903 904 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) 905 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular), 906 SetArgPointee<1>(ConnectionTethering::kUnknown), 907 Return(true))); 908 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet()) 909 .WillRepeatedly(Return(true)); 910 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _)) 911 .WillRepeatedly(Return(true)); 912 913 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 914 -1, 915 false, // ping_only 916 ErrorCode::kSuccess, 917 metrics::CheckResult::kUpdateAvailable, 918 metrics::CheckReaction::kUpdating, 919 metrics::DownloadErrorCode::kUnset, 920 &response, 921 nullptr)); 922 EXPECT_TRUE(response.update_exists); 923 } 924 925 TEST_F(OmahaRequestActionTest, ValidUpdateOverCellularBlockedByDevicePolicy) { 926 // This test tests that update over cellular is blocked as device policy 927 // says no. 928 OmahaResponse response; 929 MockConnectionManager mock_cm; 930 931 fake_system_state_.set_connection_manager(&mock_cm); 932 933 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) 934 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular), 935 SetArgPointee<1>(ConnectionTethering::kUnknown), 936 Return(true))); 937 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet()) 938 .WillRepeatedly(Return(true)); 939 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _)) 940 .WillRepeatedly(Return(false)); 941 942 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 943 -1, 944 false, // ping_only 945 ErrorCode::kOmahaUpdateIgnoredPerPolicy, 946 metrics::CheckResult::kUpdateAvailable, 947 metrics::CheckReaction::kIgnored, 948 metrics::DownloadErrorCode::kUnset, 949 &response, 950 nullptr)); 951 EXPECT_FALSE(response.update_exists); 952 } 953 954 TEST_F(OmahaRequestActionTest, 955 ValidUpdateOverCellularAllowedByUserPermissionTrue) { 956 // This test tests that, when device policy is not set, update over cellular 957 // is allowed as permission for update over cellular is set to true. 958 OmahaResponse response; 959 MockConnectionManager mock_cm; 960 961 fake_prefs_.SetBoolean(kPrefsUpdateOverCellularPermission, true); 962 fake_system_state_.set_connection_manager(&mock_cm); 963 964 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) 965 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular), 966 SetArgPointee<1>(ConnectionTethering::kUnknown), 967 Return(true))); 968 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet()) 969 .WillRepeatedly(Return(false)); 970 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _)) 971 .WillRepeatedly(Return(true)); 972 973 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 974 -1, 975 false, // ping_only 976 ErrorCode::kSuccess, 977 metrics::CheckResult::kUpdateAvailable, 978 metrics::CheckReaction::kUpdating, 979 metrics::DownloadErrorCode::kUnset, 980 &response, 981 nullptr)); 982 EXPECT_TRUE(response.update_exists); 983 } 984 985 TEST_F(OmahaRequestActionTest, 986 ValidUpdateOverCellularBlockedByUpdateTargetNotMatch) { 987 // This test tests that, when device policy is not set and permission for 988 // update over cellular is set to false or does not exist, update over 989 // cellular is blocked as update target does not match the omaha response. 990 OmahaResponse response; 991 MockConnectionManager mock_cm; 992 // A version different from the version in omaha response. 993 string diff_version = "99.99.99"; 994 // A size different from the size in omaha response. 995 int64_t diff_size = 999; 996 997 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, diff_version); 998 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, diff_size); 999 // This test tests cellular (3G) being the only connection type being allowed. 1000 fake_system_state_.set_connection_manager(&mock_cm); 1001 1002 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) 1003 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular), 1004 SetArgPointee<1>(ConnectionTethering::kUnknown), 1005 Return(true))); 1006 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet()) 1007 .WillRepeatedly(Return(false)); 1008 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _)) 1009 .WillRepeatedly(Return(true)); 1010 1011 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1012 -1, 1013 false, // ping_only 1014 ErrorCode::kOmahaUpdateIgnoredOverCellular, 1015 metrics::CheckResult::kUpdateAvailable, 1016 metrics::CheckReaction::kIgnored, 1017 metrics::DownloadErrorCode::kUnset, 1018 &response, 1019 nullptr)); 1020 EXPECT_FALSE(response.update_exists); 1021 } 1022 1023 TEST_F(OmahaRequestActionTest, 1024 ValidUpdateOverCellularAllowedByUpdateTargetMatch) { 1025 // This test tests that, when device policy is not set and permission for 1026 // update over cellular is set to false or does not exist, update over 1027 // cellular is allowed as update target matches the omaha response. 1028 OmahaResponse response; 1029 MockConnectionManager mock_cm; 1030 // A version same as the version in omaha response. 1031 string new_version = fake_update_response_.version; 1032 // A size same as the size in omaha response. 1033 int64_t new_size = fake_update_response_.size; 1034 1035 fake_prefs_.SetString(kPrefsUpdateOverCellularTargetVersion, new_version); 1036 fake_prefs_.SetInt64(kPrefsUpdateOverCellularTargetSize, new_size); 1037 fake_system_state_.set_connection_manager(&mock_cm); 1038 1039 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) 1040 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular), 1041 SetArgPointee<1>(ConnectionTethering::kUnknown), 1042 Return(true))); 1043 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet()) 1044 .WillRepeatedly(Return(false)); 1045 EXPECT_CALL(mock_cm, IsUpdateAllowedOver(ConnectionType::kCellular, _)) 1046 .WillRepeatedly(Return(true)); 1047 1048 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1049 -1, 1050 false, // ping_only 1051 ErrorCode::kSuccess, 1052 metrics::CheckResult::kUpdateAvailable, 1053 metrics::CheckReaction::kUpdating, 1054 metrics::DownloadErrorCode::kUnset, 1055 &response, 1056 nullptr)); 1057 EXPECT_TRUE(response.update_exists); 1058 } 1059 1060 TEST_F(OmahaRequestActionTest, ValidUpdateBlockedByRollback) { 1061 string rollback_version = "1234.0.0"; 1062 OmahaResponse response; 1063 1064 MockPayloadState mock_payload_state; 1065 fake_system_state_.set_payload_state(&mock_payload_state); 1066 1067 EXPECT_CALL(mock_payload_state, GetRollbackVersion()) 1068 .WillRepeatedly(Return(rollback_version)); 1069 1070 fake_update_response_.version = rollback_version; 1071 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1072 -1, 1073 false, // ping_only 1074 ErrorCode::kOmahaUpdateIgnoredPerPolicy, 1075 metrics::CheckResult::kUpdateAvailable, 1076 metrics::CheckReaction::kIgnored, 1077 metrics::DownloadErrorCode::kUnset, 1078 &response, 1079 nullptr)); 1080 EXPECT_FALSE(response.update_exists); 1081 } 1082 1083 // Verify that update checks called during OOBE will not try to download an 1084 // update if the response doesn't include the deadline field. 1085 TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBE) { 1086 OmahaResponse response; 1087 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete(); 1088 1089 // TODO(senj): set better default value for metrics::checkresult in 1090 // OmahaRequestAction::ActionCompleted. 1091 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1092 -1, 1093 false, // ping_only 1094 ErrorCode::kNonCriticalUpdateInOOBE, 1095 metrics::CheckResult::kParsingError, 1096 metrics::CheckReaction::kUnset, 1097 metrics::DownloadErrorCode::kUnset, 1098 &response, 1099 nullptr)); 1100 EXPECT_FALSE(response.update_exists); 1101 } 1102 1103 // Verify that the IsOOBEComplete() value is ignored when the OOBE flow is not 1104 // enabled. 1105 TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDisabled) { 1106 OmahaResponse response; 1107 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete(); 1108 fake_system_state_.fake_hardware()->SetIsOOBEEnabled(false); 1109 1110 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1111 -1, 1112 false, // ping_only 1113 ErrorCode::kSuccess, 1114 metrics::CheckResult::kUpdateAvailable, 1115 metrics::CheckReaction::kUpdating, 1116 metrics::DownloadErrorCode::kUnset, 1117 &response, 1118 nullptr)); 1119 EXPECT_TRUE(response.update_exists); 1120 } 1121 1122 // Verify that update checks called during OOBE will still try to download an 1123 // update if the response includes the deadline field. 1124 TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBEDeadlineSet) { 1125 OmahaResponse response; 1126 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete(); 1127 fake_update_response_.deadline = "20101020"; 1128 1129 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1130 -1, 1131 false, // ping_only 1132 ErrorCode::kSuccess, 1133 metrics::CheckResult::kUpdateAvailable, 1134 metrics::CheckReaction::kUpdating, 1135 metrics::DownloadErrorCode::kUnset, 1136 &response, 1137 nullptr)); 1138 EXPECT_TRUE(response.update_exists); 1139 } 1140 1141 // Verify that update checks called during OOBE will not try to download an 1142 // update if a rollback happened, even when the response includes the deadline 1143 // field. 1144 TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesBeforeOOBERollback) { 1145 OmahaResponse response; 1146 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete(); 1147 fake_update_response_.deadline = "20101020"; 1148 EXPECT_CALL(*(fake_system_state_.mock_payload_state()), GetRollbackHappened()) 1149 .WillOnce(Return(true)); 1150 1151 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1152 -1, 1153 false, // ping_only 1154 ErrorCode::kNonCriticalUpdateInOOBE, 1155 metrics::CheckResult::kParsingError, 1156 metrics::CheckReaction::kUnset, 1157 metrics::DownloadErrorCode::kUnset, 1158 &response, 1159 nullptr)); 1160 EXPECT_FALSE(response.update_exists); 1161 } 1162 1163 // Verify that non-critical updates are skipped by reporting the 1164 // kNonCriticalUpdateInOOBE error code when attempted over cellular network - 1165 // i.e. when the update would need user permission. Note that reporting 1166 // kOmahaUpdateIgnoredOverCellular error in this case might cause undesired UX 1167 // in OOBE (warning the user about an update that will be skipped). 1168 TEST_F(OmahaRequestActionTest, SkipNonCriticalUpdatesInOOBEOverCellular) { 1169 OmahaResponse response; 1170 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete(); 1171 1172 MockConnectionManager mock_cm; 1173 fake_system_state_.set_connection_manager(&mock_cm); 1174 1175 EXPECT_CALL(mock_cm, GetConnectionProperties(_, _)) 1176 .WillRepeatedly(DoAll(SetArgPointee<0>(ConnectionType::kCellular), 1177 SetArgPointee<1>(ConnectionTethering::kUnknown), 1178 Return(true))); 1179 EXPECT_CALL(mock_cm, IsAllowedConnectionTypesForUpdateSet()) 1180 .WillRepeatedly(Return(false)); 1181 1182 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1183 -1, 1184 false, // ping_only 1185 ErrorCode::kNonCriticalUpdateInOOBE, 1186 metrics::CheckResult::kParsingError, 1187 metrics::CheckReaction::kUnset, 1188 metrics::DownloadErrorCode::kUnset, 1189 &response, 1190 nullptr)); 1191 EXPECT_FALSE(response.update_exists); 1192 } 1193 1194 TEST_F(OmahaRequestActionTest, WallClockBasedWaitAloneCausesScattering) { 1195 OmahaResponse response; 1196 request_params_.set_wall_clock_based_wait_enabled(true); 1197 request_params_.set_update_check_count_wait_enabled(false); 1198 request_params_.set_waiting_period(TimeDelta::FromDays(2)); 1199 1200 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now()); 1201 1202 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1203 -1, 1204 false, // ping_only 1205 ErrorCode::kOmahaUpdateDeferredPerPolicy, 1206 metrics::CheckResult::kUpdateAvailable, 1207 metrics::CheckReaction::kDeferring, 1208 metrics::DownloadErrorCode::kUnset, 1209 &response, 1210 nullptr)); 1211 EXPECT_FALSE(response.update_exists); 1212 1213 // Verify if we are interactive check we don't defer. 1214 request_params_.set_interactive(true); 1215 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1216 -1, 1217 false, // ping_only 1218 ErrorCode::kSuccess, 1219 metrics::CheckResult::kUpdateAvailable, 1220 metrics::CheckReaction::kUpdating, 1221 metrics::DownloadErrorCode::kUnset, 1222 &response, 1223 nullptr)); 1224 EXPECT_TRUE(response.update_exists); 1225 } 1226 1227 TEST_F(OmahaRequestActionTest, NoWallClockBasedWaitCausesNoScattering) { 1228 OmahaResponse response; 1229 request_params_.set_wall_clock_based_wait_enabled(false); 1230 request_params_.set_waiting_period(TimeDelta::FromDays(2)); 1231 request_params_.set_update_check_count_wait_enabled(true); 1232 request_params_.set_min_update_checks_needed(1); 1233 request_params_.set_max_update_checks_allowed(8); 1234 1235 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1236 -1, 1237 false, // ping_only 1238 ErrorCode::kSuccess, 1239 metrics::CheckResult::kUpdateAvailable, 1240 metrics::CheckReaction::kUpdating, 1241 metrics::DownloadErrorCode::kUnset, 1242 &response, 1243 nullptr)); 1244 EXPECT_TRUE(response.update_exists); 1245 } 1246 1247 TEST_F(OmahaRequestActionTest, ZeroMaxDaysToScatterCausesNoScattering) { 1248 OmahaResponse response; 1249 request_params_.set_wall_clock_based_wait_enabled(true); 1250 request_params_.set_waiting_period(TimeDelta::FromDays(2)); 1251 request_params_.set_update_check_count_wait_enabled(true); 1252 request_params_.set_min_update_checks_needed(1); 1253 request_params_.set_max_update_checks_allowed(8); 1254 1255 fake_update_response_.max_days_to_scatter = "0"; 1256 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1257 -1, 1258 false, // ping_only 1259 ErrorCode::kSuccess, 1260 metrics::CheckResult::kUpdateAvailable, 1261 metrics::CheckReaction::kUpdating, 1262 metrics::DownloadErrorCode::kUnset, 1263 &response, 1264 nullptr)); 1265 EXPECT_TRUE(response.update_exists); 1266 } 1267 1268 TEST_F(OmahaRequestActionTest, ZeroUpdateCheckCountCausesNoScattering) { 1269 OmahaResponse response; 1270 request_params_.set_wall_clock_based_wait_enabled(true); 1271 request_params_.set_waiting_period(TimeDelta()); 1272 request_params_.set_update_check_count_wait_enabled(true); 1273 request_params_.set_min_update_checks_needed(0); 1274 request_params_.set_max_update_checks_allowed(0); 1275 1276 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now()); 1277 1278 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1279 -1, 1280 false, // ping_only 1281 ErrorCode::kSuccess, 1282 metrics::CheckResult::kUpdateAvailable, 1283 metrics::CheckReaction::kUpdating, 1284 metrics::DownloadErrorCode::kUnset, 1285 &response, 1286 nullptr)); 1287 1288 int64_t count; 1289 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count)); 1290 ASSERT_EQ(count, 0); 1291 EXPECT_TRUE(response.update_exists); 1292 } 1293 1294 TEST_F(OmahaRequestActionTest, NonZeroUpdateCheckCountCausesScattering) { 1295 OmahaResponse response; 1296 request_params_.set_wall_clock_based_wait_enabled(true); 1297 request_params_.set_waiting_period(TimeDelta()); 1298 request_params_.set_update_check_count_wait_enabled(true); 1299 request_params_.set_min_update_checks_needed(1); 1300 request_params_.set_max_update_checks_allowed(8); 1301 1302 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now()); 1303 1304 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1305 -1, 1306 false, // ping_only 1307 ErrorCode::kOmahaUpdateDeferredPerPolicy, 1308 metrics::CheckResult::kUpdateAvailable, 1309 metrics::CheckReaction::kDeferring, 1310 metrics::DownloadErrorCode::kUnset, 1311 &response, 1312 nullptr)); 1313 1314 int64_t count; 1315 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count)); 1316 ASSERT_GT(count, 0); 1317 EXPECT_FALSE(response.update_exists); 1318 1319 // Verify if we are interactive check we don't defer. 1320 request_params_.set_interactive(true); 1321 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1322 -1, 1323 false, // ping_only 1324 ErrorCode::kSuccess, 1325 metrics::CheckResult::kUpdateAvailable, 1326 metrics::CheckReaction::kUpdating, 1327 metrics::DownloadErrorCode::kUnset, 1328 &response, 1329 nullptr)); 1330 EXPECT_TRUE(response.update_exists); 1331 } 1332 1333 TEST_F(OmahaRequestActionTest, ExistingUpdateCheckCountCausesScattering) { 1334 OmahaResponse response; 1335 request_params_.set_wall_clock_based_wait_enabled(true); 1336 request_params_.set_waiting_period(TimeDelta()); 1337 request_params_.set_update_check_count_wait_enabled(true); 1338 request_params_.set_min_update_checks_needed(1); 1339 request_params_.set_max_update_checks_allowed(8); 1340 1341 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now()); 1342 1343 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsUpdateCheckCount, 5)); 1344 1345 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1346 -1, 1347 false, // ping_only 1348 ErrorCode::kOmahaUpdateDeferredPerPolicy, 1349 metrics::CheckResult::kUpdateAvailable, 1350 metrics::CheckReaction::kDeferring, 1351 metrics::DownloadErrorCode::kUnset, 1352 &response, 1353 nullptr)); 1354 1355 int64_t count; 1356 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateCheckCount, &count)); 1357 // count remains the same, as the decrementing happens in update_attempter 1358 // which this test doesn't exercise. 1359 ASSERT_EQ(count, 5); 1360 EXPECT_FALSE(response.update_exists); 1361 1362 // Verify if we are interactive check we don't defer. 1363 request_params_.set_interactive(true); 1364 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1365 -1, 1366 false, // ping_only 1367 ErrorCode::kSuccess, 1368 metrics::CheckResult::kUpdateAvailable, 1369 metrics::CheckReaction::kUpdating, 1370 metrics::DownloadErrorCode::kUnset, 1371 &response, 1372 nullptr)); 1373 EXPECT_TRUE(response.update_exists); 1374 } 1375 1376 TEST_F(OmahaRequestActionTest, StagingTurnedOnCausesScattering) { 1377 // If staging is on, the value for max days to scatter should be ignored, and 1378 // staging's scatter value should be used. 1379 OmahaResponse response; 1380 request_params_.set_wall_clock_based_wait_enabled(true); 1381 request_params_.set_waiting_period(TimeDelta::FromDays(6)); 1382 request_params_.set_update_check_count_wait_enabled(false); 1383 1384 fake_system_state_.fake_clock()->SetWallclockTime(Time::Now()); 1385 1386 ASSERT_TRUE(fake_prefs_.SetInt64(kPrefsWallClockStagingWaitPeriod, 6)); 1387 // This should not prevent scattering due to staging. 1388 fake_update_response_.max_days_to_scatter = "0"; 1389 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1390 -1, 1391 false, // ping_only 1392 ErrorCode::kOmahaUpdateDeferredPerPolicy, 1393 metrics::CheckResult::kUpdateAvailable, 1394 metrics::CheckReaction::kDeferring, 1395 metrics::DownloadErrorCode::kUnset, 1396 &response, 1397 nullptr)); 1398 EXPECT_FALSE(response.update_exists); 1399 1400 // Interactive updates should not be affected. 1401 request_params_.set_interactive(true); 1402 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1403 -1, 1404 false, // ping_only 1405 ErrorCode::kSuccess, 1406 metrics::CheckResult::kUpdateAvailable, 1407 metrics::CheckReaction::kUpdating, 1408 metrics::DownloadErrorCode::kUnset, 1409 &response, 1410 nullptr)); 1411 EXPECT_TRUE(response.update_exists); 1412 } 1413 1414 TEST_F(OmahaRequestActionTest, CohortsArePersisted) { 1415 OmahaResponse response; 1416 fake_update_response_.include_cohorts = true; 1417 fake_update_response_.cohort = "s/154454/8479665"; 1418 fake_update_response_.cohorthint = "please-put-me-on-beta"; 1419 fake_update_response_.cohortname = "stable"; 1420 1421 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1422 -1, 1423 false, // ping_only 1424 ErrorCode::kSuccess, 1425 metrics::CheckResult::kUpdateAvailable, 1426 metrics::CheckReaction::kUpdating, 1427 metrics::DownloadErrorCode::kUnset, 1428 &response, 1429 nullptr)); 1430 1431 string value; 1432 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); 1433 EXPECT_EQ(fake_update_response_.cohort, value); 1434 1435 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); 1436 EXPECT_EQ(fake_update_response_.cohorthint, value); 1437 1438 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); 1439 EXPECT_EQ(fake_update_response_.cohortname, value); 1440 } 1441 1442 TEST_F(OmahaRequestActionTest, CohortsAreUpdated) { 1443 OmahaResponse response; 1444 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value")); 1445 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortHint, "old_hint")); 1446 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohortName, "old_name")); 1447 fake_update_response_.include_cohorts = true; 1448 fake_update_response_.cohort = "s/154454/8479665"; 1449 fake_update_response_.cohorthint = "please-put-me-on-beta"; 1450 fake_update_response_.cohortname = ""; 1451 1452 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1453 -1, 1454 false, // ping_only 1455 ErrorCode::kSuccess, 1456 metrics::CheckResult::kUpdateAvailable, 1457 metrics::CheckReaction::kUpdating, 1458 metrics::DownloadErrorCode::kUnset, 1459 &response, 1460 nullptr)); 1461 1462 string value; 1463 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); 1464 EXPECT_EQ(fake_update_response_.cohort, value); 1465 1466 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); 1467 EXPECT_EQ(fake_update_response_.cohorthint, value); 1468 1469 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); 1470 } 1471 1472 TEST_F(OmahaRequestActionTest, CohortsAreNotModifiedWhenMissing) { 1473 OmahaResponse response; 1474 EXPECT_TRUE(fake_prefs_.SetString(kPrefsOmahaCohort, "old_value")); 1475 1476 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1477 -1, 1478 false, // ping_only 1479 ErrorCode::kSuccess, 1480 metrics::CheckResult::kUpdateAvailable, 1481 metrics::CheckReaction::kUpdating, 1482 metrics::DownloadErrorCode::kUnset, 1483 &response, 1484 nullptr)); 1485 1486 string value; 1487 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); 1488 EXPECT_EQ("old_value", value); 1489 1490 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); 1491 EXPECT_FALSE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); 1492 } 1493 1494 TEST_F(OmahaRequestActionTest, CohortsArePersistedWhenNoUpdate) { 1495 OmahaResponse response; 1496 fake_update_response_.include_cohorts = true; 1497 fake_update_response_.cohort = "s/154454/8479665"; 1498 fake_update_response_.cohorthint = "please-put-me-on-beta"; 1499 fake_update_response_.cohortname = "stable"; 1500 1501 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 1502 -1, 1503 false, // ping_only 1504 ErrorCode::kSuccess, 1505 metrics::CheckResult::kNoUpdateAvailable, 1506 metrics::CheckReaction::kUnset, 1507 metrics::DownloadErrorCode::kUnset, 1508 &response, 1509 nullptr)); 1510 1511 string value; 1512 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); 1513 EXPECT_EQ(fake_update_response_.cohort, value); 1514 1515 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); 1516 EXPECT_EQ(fake_update_response_.cohorthint, value); 1517 1518 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); 1519 EXPECT_EQ(fake_update_response_.cohortname, value); 1520 } 1521 1522 TEST_F(OmahaRequestActionTest, MultiAppCohortTest) { 1523 OmahaResponse response; 1524 fake_update_response_.multi_app = true; 1525 fake_update_response_.include_cohorts = true; 1526 fake_update_response_.cohort = "s/154454/8479665"; 1527 fake_update_response_.cohorthint = "please-put-me-on-beta"; 1528 fake_update_response_.cohortname = "stable"; 1529 1530 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1531 -1, 1532 false, // ping_only 1533 ErrorCode::kSuccess, 1534 metrics::CheckResult::kUpdateAvailable, 1535 metrics::CheckReaction::kUpdating, 1536 metrics::DownloadErrorCode::kUnset, 1537 &response, 1538 nullptr)); 1539 1540 string value; 1541 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohort, &value)); 1542 EXPECT_EQ(fake_update_response_.cohort, value); 1543 1544 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortHint, &value)); 1545 EXPECT_EQ(fake_update_response_.cohorthint, value); 1546 1547 EXPECT_TRUE(fake_prefs_.GetString(kPrefsOmahaCohortName, &value)); 1548 EXPECT_EQ(fake_update_response_.cohortname, value); 1549 } 1550 1551 TEST_F(OmahaRequestActionTest, NoOutputPipeTest) { 1552 const string http_response(fake_update_response_.GetNoUpdateResponse()); 1553 1554 brillo::FakeMessageLoop loop(nullptr); 1555 loop.SetAsCurrent(); 1556 1557 auto action = std::make_unique<OmahaRequestAction>( 1558 &fake_system_state_, 1559 nullptr, 1560 std::make_unique<MockHttpFetcher>( 1561 http_response.data(), http_response.size(), nullptr), 1562 false); 1563 ActionProcessor processor; 1564 processor.set_delegate(&delegate_); 1565 processor.EnqueueAction(std::move(action)); 1566 1567 loop.PostTask(base::Bind( 1568 [](ActionProcessor* processor) { processor->StartProcessing(); }, 1569 base::Unretained(&processor))); 1570 loop.Run(); 1571 EXPECT_FALSE(loop.PendingTasks()); 1572 EXPECT_FALSE(processor.IsRunning()); 1573 } 1574 1575 TEST_F(OmahaRequestActionTest, InvalidXmlTest) { 1576 OmahaResponse response; 1577 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 1578 -1, 1579 false, // ping_only 1580 ErrorCode::kOmahaRequestXMLParseError, 1581 metrics::CheckResult::kParsingError, 1582 metrics::CheckReaction::kUnset, 1583 metrics::DownloadErrorCode::kUnset, 1584 &response, 1585 nullptr)); 1586 EXPECT_FALSE(response.update_exists); 1587 } 1588 1589 TEST_F(OmahaRequestActionTest, EmptyResponseTest) { 1590 OmahaResponse response; 1591 ASSERT_FALSE(TestUpdateCheck("", 1592 -1, 1593 false, // ping_only 1594 ErrorCode::kOmahaRequestEmptyResponseError, 1595 metrics::CheckResult::kParsingError, 1596 metrics::CheckReaction::kUnset, 1597 metrics::DownloadErrorCode::kUnset, 1598 &response, 1599 nullptr)); 1600 EXPECT_FALSE(response.update_exists); 1601 } 1602 1603 TEST_F(OmahaRequestActionTest, MissingStatusTest) { 1604 OmahaResponse response; 1605 ASSERT_FALSE(TestUpdateCheck( 1606 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" 1607 "<daystart elapsed_seconds=\"100\"/>" 1608 "<app appid=\"foo\" status=\"ok\">" 1609 "<ping status=\"ok\"/>" 1610 "<updatecheck/></app></response>", 1611 -1, 1612 false, // ping_only 1613 ErrorCode::kOmahaResponseInvalid, 1614 metrics::CheckResult::kParsingError, 1615 metrics::CheckReaction::kUnset, 1616 metrics::DownloadErrorCode::kUnset, 1617 &response, 1618 nullptr)); 1619 EXPECT_FALSE(response.update_exists); 1620 } 1621 1622 TEST_F(OmahaRequestActionTest, InvalidStatusTest) { 1623 OmahaResponse response; 1624 ASSERT_FALSE(TestUpdateCheck( 1625 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" 1626 "<daystart elapsed_seconds=\"100\"/>" 1627 "<app appid=\"foo\" status=\"ok\">" 1628 "<ping status=\"ok\"/>" 1629 "<updatecheck status=\"InvalidStatusTest\"/></app></response>", 1630 -1, 1631 false, // ping_only 1632 ErrorCode::kOmahaResponseInvalid, 1633 metrics::CheckResult::kParsingError, 1634 metrics::CheckReaction::kUnset, 1635 metrics::DownloadErrorCode::kUnset, 1636 &response, 1637 nullptr)); 1638 EXPECT_FALSE(response.update_exists); 1639 } 1640 1641 TEST_F(OmahaRequestActionTest, MissingNodesetTest) { 1642 OmahaResponse response; 1643 ASSERT_FALSE(TestUpdateCheck( 1644 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" 1645 "<daystart elapsed_seconds=\"100\"/>" 1646 "<app appid=\"foo\" status=\"ok\">" 1647 "<ping status=\"ok\"/>" 1648 "</app></response>", 1649 -1, 1650 false, // ping_only 1651 ErrorCode::kOmahaResponseInvalid, 1652 metrics::CheckResult::kParsingError, 1653 metrics::CheckReaction::kUnset, 1654 metrics::DownloadErrorCode::kUnset, 1655 &response, 1656 nullptr)); 1657 EXPECT_FALSE(response.update_exists); 1658 } 1659 1660 TEST_F(OmahaRequestActionTest, MissingFieldTest) { 1661 string input_response = 1662 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response protocol=\"3.0\">" 1663 "<daystart elapsed_seconds=\"100\"/>" 1664 // the appid needs to match that in the request params 1665 "<app appid=\"" + 1666 fake_update_response_.app_id + 1667 "\" status=\"ok\">" 1668 "<updatecheck status=\"ok\">" 1669 "<urls><url codebase=\"http://missing/field/test/\"/></urls>" 1670 "<manifest version=\"10.2.3.4\">" 1671 "<packages><package hash=\"not-used\" name=\"f\" " 1672 "size=\"587\" hash_sha256=\"lkq34j5345\"/></packages>" 1673 "<actions><action event=\"postinstall\" " 1674 "Prompt=\"false\" " 1675 "IsDeltaPayload=\"false\" " 1676 "sha256=\"not-used\" " 1677 "/></actions></manifest></updatecheck></app></response>"; 1678 LOG(INFO) << "Input Response = " << input_response; 1679 1680 OmahaResponse response; 1681 ASSERT_TRUE(TestUpdateCheck(input_response, 1682 -1, 1683 false, // ping_only 1684 ErrorCode::kSuccess, 1685 metrics::CheckResult::kUpdateAvailable, 1686 metrics::CheckReaction::kUpdating, 1687 metrics::DownloadErrorCode::kUnset, 1688 &response, 1689 nullptr)); 1690 EXPECT_TRUE(response.update_exists); 1691 EXPECT_EQ("10.2.3.4", response.version); 1692 EXPECT_EQ("http://missing/field/test/f", 1693 response.packages[0].payload_urls[0]); 1694 EXPECT_EQ("", response.more_info_url); 1695 EXPECT_EQ("lkq34j5345", response.packages[0].hash); 1696 EXPECT_EQ(587u, response.packages[0].size); 1697 EXPECT_FALSE(response.prompt); 1698 EXPECT_TRUE(response.deadline.empty()); 1699 } 1700 1701 namespace { 1702 class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate { 1703 public: 1704 void ProcessingStopped(const ActionProcessor* processor) { 1705 brillo::MessageLoop::current()->BreakLoop(); 1706 } 1707 }; 1708 1709 void TerminateTransferTestStarter(ActionProcessor* processor) { 1710 processor->StartProcessing(); 1711 CHECK(processor->IsRunning()); 1712 processor->StopProcessing(); 1713 } 1714 } // namespace 1715 1716 TEST_F(OmahaRequestActionTest, TerminateTransferTest) { 1717 brillo::FakeMessageLoop loop(nullptr); 1718 loop.SetAsCurrent(); 1719 1720 string http_response("doesn't matter"); 1721 auto action = std::make_unique<OmahaRequestAction>( 1722 &fake_system_state_, 1723 nullptr, 1724 std::make_unique<MockHttpFetcher>( 1725 http_response.data(), http_response.size(), nullptr), 1726 false); 1727 TerminateEarlyTestProcessorDelegate delegate; 1728 ActionProcessor processor; 1729 processor.set_delegate(&delegate); 1730 processor.EnqueueAction(std::move(action)); 1731 1732 loop.PostTask(base::Bind(&TerminateTransferTestStarter, &processor)); 1733 loop.Run(); 1734 EXPECT_FALSE(loop.PendingTasks()); 1735 } 1736 1737 TEST_F(OmahaRequestActionTest, XmlEncodeTest) { 1738 string output; 1739 EXPECT_TRUE(XmlEncode("ab", &output)); 1740 EXPECT_EQ("ab", output); 1741 EXPECT_TRUE(XmlEncode("a<b", &output)); 1742 EXPECT_EQ("a<b", output); 1743 EXPECT_TRUE(XmlEncode("<&>\"\'\\", &output)); 1744 EXPECT_EQ("<&>"'\\", output); 1745 EXPECT_TRUE(XmlEncode("<&>", &output)); 1746 EXPECT_EQ("&lt;&amp;&gt;", output); 1747 // Check that unterminated UTF-8 strings are handled properly. 1748 EXPECT_FALSE(XmlEncode("\xc2", &output)); 1749 // Fail with invalid ASCII-7 chars. 1750 EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output)); 1751 } 1752 1753 TEST_F(OmahaRequestActionTest, XmlEncodeWithDefaultTest) { 1754 EXPECT_EQ("<&>", XmlEncodeWithDefault("<&>", "something else")); 1755 EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>")); 1756 } 1757 1758 TEST_F(OmahaRequestActionTest, XmlEncodeIsUsedForParams) { 1759 brillo::Blob post_data; 1760 1761 // Make sure XML Encode is being called on the params 1762 request_params_.set_os_sp("testtheservice_pack>"); 1763 request_params_.set_os_board("x86 generic<id"); 1764 request_params_.set_current_channel("unittest_track<"); 1765 request_params_.set_target_channel("unittest_track<"); 1766 request_params_.set_hwid("<OEM MODEL>"); 1767 fake_prefs_.SetString(kPrefsOmahaCohort, "evil\nstring"); 1768 fake_prefs_.SetString(kPrefsOmahaCohortHint, "evil&string\\"); 1769 fake_prefs_.SetString( 1770 kPrefsOmahaCohortName, 1771 base::JoinString(vector<string>(100, "My spoon is too big."), " ")); 1772 OmahaResponse response; 1773 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 1774 -1, 1775 false, // ping_only 1776 ErrorCode::kOmahaRequestXMLParseError, 1777 metrics::CheckResult::kParsingError, 1778 metrics::CheckReaction::kUnset, 1779 metrics::DownloadErrorCode::kUnset, 1780 &response, 1781 &post_data)); 1782 // convert post_data to string 1783 string post_str(post_data.begin(), post_data.end()); 1784 EXPECT_NE(string::npos, post_str.find("testtheservice_pack>")); 1785 EXPECT_EQ(string::npos, post_str.find("testtheservice_pack>")); 1786 EXPECT_NE(string::npos, post_str.find("x86 generic<id")); 1787 EXPECT_EQ(string::npos, post_str.find("x86 generic<id")); 1788 EXPECT_NE(string::npos, post_str.find("unittest_track&lt;")); 1789 EXPECT_EQ(string::npos, post_str.find("unittest_track<")); 1790 EXPECT_NE(string::npos, post_str.find("<OEM MODEL>")); 1791 EXPECT_EQ(string::npos, post_str.find("<OEM MODEL>")); 1792 EXPECT_NE(string::npos, post_str.find("cohort=\"evil\nstring\"")); 1793 EXPECT_EQ(string::npos, post_str.find("cohorthint=\"evil&string\\\"")); 1794 EXPECT_NE(string::npos, post_str.find("cohorthint=\"evil&string\\\"")); 1795 // Values from Prefs that are too big are removed from the XML instead of 1796 // encoded. 1797 EXPECT_EQ(string::npos, post_str.find("cohortname=")); 1798 } 1799 1800 TEST_F(OmahaRequestActionTest, XmlDecodeTest) { 1801 OmahaResponse response; 1802 fake_update_response_.deadline = "<20110101"; 1803 fake_update_response_.more_info_url = "testthe<url"; 1804 fake_update_response_.codebase = "testthe&codebase/"; 1805 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1806 -1, 1807 false, // ping_only 1808 ErrorCode::kSuccess, 1809 metrics::CheckResult::kUpdateAvailable, 1810 metrics::CheckReaction::kUpdating, 1811 metrics::DownloadErrorCode::kUnset, 1812 &response, 1813 nullptr)); 1814 1815 EXPECT_EQ("testthe<url", response.more_info_url); 1816 EXPECT_EQ("testthe&codebase/file.signed", 1817 response.packages[0].payload_urls[0]); 1818 EXPECT_EQ("<20110101", response.deadline); 1819 } 1820 1821 TEST_F(OmahaRequestActionTest, ParseIntTest) { 1822 OmahaResponse response; 1823 // overflows int32_t: 1824 fake_update_response_.size = 123123123123123ull; 1825 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 1826 -1, 1827 false, // ping_only 1828 ErrorCode::kSuccess, 1829 metrics::CheckResult::kUpdateAvailable, 1830 metrics::CheckReaction::kUpdating, 1831 metrics::DownloadErrorCode::kUnset, 1832 &response, 1833 nullptr)); 1834 1835 EXPECT_EQ(fake_update_response_.size, response.packages[0].size); 1836 } 1837 1838 TEST_F(OmahaRequestActionTest, FormatUpdateCheckOutputTest) { 1839 brillo::Blob post_data; 1840 NiceMock<MockPrefs> prefs; 1841 fake_system_state_.set_prefs(&prefs); 1842 1843 EXPECT_CALL(prefs, GetString(kPrefsPreviousVersion, _)) 1844 .WillOnce(DoAll(SetArgPointee<1>(string("")), Return(true))); 1845 // An existing but empty previous version means that we didn't reboot to a new 1846 // update, therefore, no need to update the previous version. 1847 EXPECT_CALL(prefs, SetString(kPrefsPreviousVersion, _)).Times(0); 1848 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 1849 -1, 1850 false, // ping_only 1851 ErrorCode::kOmahaRequestXMLParseError, 1852 metrics::CheckResult::kParsingError, 1853 metrics::CheckReaction::kUnset, 1854 metrics::DownloadErrorCode::kUnset, 1855 nullptr, // response 1856 &post_data)); 1857 // convert post_data to string 1858 string post_str(post_data.begin(), post_data.end()); 1859 EXPECT_NE( 1860 post_str.find(" <ping active=\"1\" a=\"-1\" r=\"-1\"></ping>\n" 1861 " <updatecheck></updatecheck>\n"), 1862 string::npos); 1863 EXPECT_NE(post_str.find("hardware_class=\"OEM MODEL 09235 7471\""), 1864 string::npos); 1865 EXPECT_NE(post_str.find("fw_version=\"ChromeOSFirmware.1.0\""), string::npos); 1866 EXPECT_NE(post_str.find("ec_version=\"0X0A1\""), string::npos); 1867 // No <event> tag should be sent if we didn't reboot to an update. 1868 EXPECT_EQ(post_str.find("<event"), string::npos); 1869 } 1870 1871 TEST_F(OmahaRequestActionTest, FormatSuccessEventOutputTest) { 1872 brillo::Blob post_data; 1873 TestEvent(new OmahaEvent(OmahaEvent::kTypeUpdateDownloadStarted), 1874 "invalid xml>", 1875 &post_data); 1876 // convert post_data to string 1877 string post_str(post_data.begin(), post_data.end()); 1878 string expected_event = base::StringPrintf( 1879 " <event eventtype=\"%d\" eventresult=\"%d\"></event>\n", 1880 OmahaEvent::kTypeUpdateDownloadStarted, 1881 OmahaEvent::kResultSuccess); 1882 EXPECT_NE(post_str.find(expected_event), string::npos); 1883 EXPECT_EQ(post_str.find("ping"), string::npos); 1884 EXPECT_EQ(post_str.find("updatecheck"), string::npos); 1885 } 1886 1887 TEST_F(OmahaRequestActionTest, FormatErrorEventOutputTest) { 1888 brillo::Blob post_data; 1889 TestEvent(new OmahaEvent(OmahaEvent::kTypeDownloadComplete, 1890 OmahaEvent::kResultError, 1891 ErrorCode::kError), 1892 "invalid xml>", 1893 &post_data); 1894 // convert post_data to string 1895 string post_str(post_data.begin(), post_data.end()); 1896 string expected_event = base::StringPrintf( 1897 " <event eventtype=\"%d\" eventresult=\"%d\" " 1898 "errorcode=\"%d\"></event>\n", 1899 OmahaEvent::kTypeDownloadComplete, 1900 OmahaEvent::kResultError, 1901 static_cast<int>(ErrorCode::kError)); 1902 EXPECT_NE(post_str.find(expected_event), string::npos); 1903 EXPECT_EQ(post_str.find("updatecheck"), string::npos); 1904 } 1905 1906 TEST_F(OmahaRequestActionTest, IsEventTest) { 1907 string http_response("doesn't matter"); 1908 OmahaRequestAction update_check_action( 1909 &fake_system_state_, 1910 nullptr, 1911 std::make_unique<MockHttpFetcher>( 1912 http_response.data(), http_response.size(), nullptr), 1913 false); 1914 EXPECT_FALSE(update_check_action.IsEvent()); 1915 1916 OmahaRequestAction event_action( 1917 &fake_system_state_, 1918 new OmahaEvent(OmahaEvent::kTypeUpdateComplete), 1919 std::make_unique<MockHttpFetcher>( 1920 http_response.data(), http_response.size(), nullptr), 1921 false); 1922 EXPECT_TRUE(event_action.IsEvent()); 1923 } 1924 1925 TEST_F(OmahaRequestActionTest, FormatDeltaOkayOutputTest) { 1926 for (int i = 0; i < 2; i++) { 1927 bool delta_okay = i == 1; 1928 const char* delta_okay_str = delta_okay ? "true" : "false"; 1929 brillo::Blob post_data; 1930 1931 request_params_.set_delta_okay(delta_okay); 1932 1933 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 1934 -1, 1935 false, // ping_only 1936 ErrorCode::kOmahaRequestXMLParseError, 1937 metrics::CheckResult::kParsingError, 1938 metrics::CheckReaction::kUnset, 1939 metrics::DownloadErrorCode::kUnset, 1940 nullptr, 1941 &post_data)); 1942 // convert post_data to string 1943 string post_str(post_data.begin(), post_data.end()); 1944 EXPECT_NE( 1945 post_str.find(base::StringPrintf(" delta_okay=\"%s\"", delta_okay_str)), 1946 string::npos) 1947 << "i = " << i; 1948 } 1949 } 1950 1951 TEST_F(OmahaRequestActionTest, FormatInteractiveOutputTest) { 1952 for (int i = 0; i < 2; i++) { 1953 bool interactive = i == 1; 1954 const char* interactive_str = interactive ? "ondemandupdate" : "scheduler"; 1955 brillo::Blob post_data; 1956 FakeSystemState fake_system_state; 1957 1958 request_params_.set_interactive(interactive); 1959 1960 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 1961 -1, 1962 false, // ping_only 1963 ErrorCode::kOmahaRequestXMLParseError, 1964 metrics::CheckResult::kParsingError, 1965 metrics::CheckReaction::kUnset, 1966 metrics::DownloadErrorCode::kUnset, 1967 nullptr, 1968 &post_data)); 1969 // convert post_data to string 1970 string post_str(post_data.begin(), post_data.end()); 1971 EXPECT_NE(post_str.find( 1972 base::StringPrintf("installsource=\"%s\"", interactive_str)), 1973 string::npos) 1974 << "i = " << i; 1975 } 1976 } 1977 1978 TEST_F(OmahaRequestActionTest, FormatTargetVersionPrefixOutputTest) { 1979 for (int i = 0; i < 2; i++) { 1980 bool target_version_set = i == 1; 1981 const char* target_version_prefix = target_version_set ? "10032." : ""; 1982 brillo::Blob post_data; 1983 FakeSystemState fake_system_state; 1984 1985 request_params_.set_target_version_prefix(target_version_prefix); 1986 1987 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 1988 -1, 1989 false, // ping_only 1990 ErrorCode::kOmahaRequestXMLParseError, 1991 metrics::CheckResult::kParsingError, 1992 metrics::CheckReaction::kUnset, 1993 metrics::DownloadErrorCode::kUnset, 1994 nullptr, 1995 &post_data)); 1996 // convert post_data to string 1997 string post_str(post_data.begin(), post_data.end()); 1998 if (target_version_set) { 1999 EXPECT_NE(post_str.find("<updatecheck targetversionprefix=\"10032.\">"), 2000 string::npos) 2001 << "i = " << i; 2002 } else { 2003 EXPECT_EQ(post_str.find("targetversionprefix"), string::npos) 2004 << "i = " << i; 2005 } 2006 } 2007 } 2008 2009 TEST_F(OmahaRequestActionTest, FormatRollbackAllowedOutputTest) { 2010 for (int i = 0; i < 4; i++) { 2011 bool rollback_allowed = i / 2 == 0; 2012 bool target_version_set = i % 2 == 0; 2013 brillo::Blob post_data; 2014 FakeSystemState fake_system_state; 2015 2016 request_params_.set_target_version_prefix(target_version_set ? "10032." 2017 : ""); 2018 request_params_.set_rollback_allowed(rollback_allowed); 2019 2020 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 2021 -1, 2022 false, // ping_only 2023 ErrorCode::kOmahaRequestXMLParseError, 2024 metrics::CheckResult::kParsingError, 2025 metrics::CheckReaction::kUnset, 2026 metrics::DownloadErrorCode::kUnset, 2027 nullptr, 2028 &post_data)); 2029 // convert post_data to string 2030 string post_str(post_data.begin(), post_data.end()); 2031 if (rollback_allowed && target_version_set) { 2032 EXPECT_NE(post_str.find("rollback_allowed=\"true\""), string::npos) 2033 << "i = " << i; 2034 } else { 2035 EXPECT_EQ(post_str.find("rollback_allowed"), string::npos) << "i = " << i; 2036 } 2037 } 2038 } 2039 2040 TEST_F(OmahaRequestActionTest, OmahaEventTest) { 2041 OmahaEvent default_event; 2042 EXPECT_EQ(OmahaEvent::kTypeUnknown, default_event.type); 2043 EXPECT_EQ(OmahaEvent::kResultError, default_event.result); 2044 EXPECT_EQ(ErrorCode::kError, default_event.error_code); 2045 2046 OmahaEvent success_event(OmahaEvent::kTypeUpdateDownloadStarted); 2047 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadStarted, success_event.type); 2048 EXPECT_EQ(OmahaEvent::kResultSuccess, success_event.result); 2049 EXPECT_EQ(ErrorCode::kSuccess, success_event.error_code); 2050 2051 OmahaEvent error_event(OmahaEvent::kTypeUpdateDownloadFinished, 2052 OmahaEvent::kResultError, 2053 ErrorCode::kError); 2054 EXPECT_EQ(OmahaEvent::kTypeUpdateDownloadFinished, error_event.type); 2055 EXPECT_EQ(OmahaEvent::kResultError, error_event.result); 2056 EXPECT_EQ(ErrorCode::kError, error_event.error_code); 2057 } 2058 2059 void OmahaRequestActionTest::PingTest(bool ping_only) { 2060 NiceMock<MockPrefs> prefs; 2061 fake_system_state_.set_prefs(&prefs); 2062 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) 2063 .Times(AnyNumber()); 2064 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2065 // Add a few hours to the day difference to test no rounding, etc. 2066 int64_t five_days_ago = 2067 (Time::Now() - TimeDelta::FromHours(5 * 24 + 13)).ToInternalValue(); 2068 int64_t six_days_ago = 2069 (Time::Now() - TimeDelta::FromHours(6 * 24 + 11)).ToInternalValue(); 2070 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) 2071 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true))); 2072 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) 2073 .WillOnce(DoAll(SetArgPointee<1>(six_days_ago), Return(true))); 2074 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) 2075 .WillOnce(DoAll(SetArgPointee<1>(five_days_ago), Return(true))); 2076 brillo::Blob post_data; 2077 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2078 -1, 2079 ping_only, 2080 ErrorCode::kSuccess, 2081 metrics::CheckResult::kNoUpdateAvailable, 2082 metrics::CheckReaction::kUnset, 2083 metrics::DownloadErrorCode::kUnset, 2084 nullptr, 2085 &post_data)); 2086 string post_str(post_data.begin(), post_data.end()); 2087 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"6\" r=\"5\"></ping>"), 2088 string::npos); 2089 if (ping_only) { 2090 EXPECT_EQ(post_str.find("updatecheck"), string::npos); 2091 EXPECT_EQ(post_str.find("previousversion"), string::npos); 2092 } else { 2093 EXPECT_NE(post_str.find("updatecheck"), string::npos); 2094 EXPECT_NE(post_str.find("previousversion"), string::npos); 2095 } 2096 } 2097 2098 TEST_F(OmahaRequestActionTest, PingTestSendOnlyAPing) { 2099 PingTest(true /* ping_only */); 2100 } 2101 2102 TEST_F(OmahaRequestActionTest, PingTestSendAlsoAnUpdateCheck) { 2103 PingTest(false /* ping_only */); 2104 } 2105 2106 TEST_F(OmahaRequestActionTest, ActivePingTest) { 2107 NiceMock<MockPrefs> prefs; 2108 fake_system_state_.set_prefs(&prefs); 2109 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) 2110 .Times(AnyNumber()); 2111 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2112 int64_t three_days_ago = 2113 (Time::Now() - TimeDelta::FromHours(3 * 24 + 12)).ToInternalValue(); 2114 int64_t now = Time::Now().ToInternalValue(); 2115 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) 2116 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true))); 2117 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) 2118 .WillOnce(DoAll(SetArgPointee<1>(three_days_ago), Return(true))); 2119 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) 2120 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true))); 2121 brillo::Blob post_data; 2122 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2123 -1, 2124 false, // ping_only 2125 ErrorCode::kSuccess, 2126 metrics::CheckResult::kNoUpdateAvailable, 2127 metrics::CheckReaction::kUnset, 2128 metrics::DownloadErrorCode::kUnset, 2129 nullptr, 2130 &post_data)); 2131 string post_str(post_data.begin(), post_data.end()); 2132 EXPECT_NE(post_str.find("<ping active=\"1\" a=\"3\"></ping>"), string::npos); 2133 } 2134 2135 TEST_F(OmahaRequestActionTest, RollCallPingTest) { 2136 NiceMock<MockPrefs> prefs; 2137 fake_system_state_.set_prefs(&prefs); 2138 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) 2139 .Times(AnyNumber()); 2140 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2141 int64_t four_days_ago = 2142 (Time::Now() - TimeDelta::FromHours(4 * 24)).ToInternalValue(); 2143 int64_t now = Time::Now().ToInternalValue(); 2144 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) 2145 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true))); 2146 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) 2147 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true))); 2148 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) 2149 .WillOnce(DoAll(SetArgPointee<1>(four_days_ago), Return(true))); 2150 brillo::Blob post_data; 2151 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2152 -1, 2153 false, // ping_only 2154 ErrorCode::kSuccess, 2155 metrics::CheckResult::kNoUpdateAvailable, 2156 metrics::CheckReaction::kUnset, 2157 metrics::DownloadErrorCode::kUnset, 2158 nullptr, 2159 &post_data)); 2160 string post_str(post_data.begin(), post_data.end()); 2161 EXPECT_NE(post_str.find("<ping active=\"1\" r=\"4\"></ping>\n"), 2162 string::npos); 2163 } 2164 2165 TEST_F(OmahaRequestActionTest, NoPingTest) { 2166 NiceMock<MockPrefs> prefs; 2167 fake_system_state_.set_prefs(&prefs); 2168 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) 2169 .Times(AnyNumber()); 2170 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2171 int64_t one_hour_ago = 2172 (Time::Now() - TimeDelta::FromHours(1)).ToInternalValue(); 2173 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) 2174 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true))); 2175 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) 2176 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true))); 2177 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) 2178 .WillOnce(DoAll(SetArgPointee<1>(one_hour_ago), Return(true))); 2179 // LastActivePingDay and PrefsLastRollCallPingDay are set even if we didn't 2180 // send a ping. 2181 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)) 2182 .WillOnce(Return(true)); 2183 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)) 2184 .WillOnce(Return(true)); 2185 brillo::Blob post_data; 2186 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2187 -1, 2188 false, // ping_only 2189 ErrorCode::kSuccess, 2190 metrics::CheckResult::kNoUpdateAvailable, 2191 metrics::CheckReaction::kUnset, 2192 metrics::DownloadErrorCode::kUnset, 2193 nullptr, 2194 &post_data)); 2195 string post_str(post_data.begin(), post_data.end()); 2196 EXPECT_EQ(post_str.find("ping"), string::npos); 2197 } 2198 2199 TEST_F(OmahaRequestActionTest, IgnoreEmptyPingTest) { 2200 // This test ensures that we ignore empty ping only requests. 2201 NiceMock<MockPrefs> prefs; 2202 fake_system_state_.set_prefs(&prefs); 2203 int64_t now = Time::Now().ToInternalValue(); 2204 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) 2205 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true))); 2206 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) 2207 .WillOnce(DoAll(SetArgPointee<1>(now), Return(true))); 2208 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); 2209 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); 2210 brillo::Blob post_data; 2211 EXPECT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2212 -1, 2213 true, // ping_only 2214 ErrorCode::kSuccess, 2215 metrics::CheckResult::kUnset, 2216 metrics::CheckReaction::kUnset, 2217 metrics::DownloadErrorCode::kUnset, 2218 nullptr, 2219 &post_data)); 2220 EXPECT_EQ(0U, post_data.size()); 2221 } 2222 2223 TEST_F(OmahaRequestActionTest, BackInTimePingTest) { 2224 NiceMock<MockPrefs> prefs; 2225 fake_system_state_.set_prefs(&prefs); 2226 EXPECT_CALL(prefs, GetInt64(kPrefsMetricsCheckLastReportingTime, _)) 2227 .Times(AnyNumber()); 2228 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2229 int64_t future = 2230 (Time::Now() + TimeDelta::FromHours(3 * 24 + 4)).ToInternalValue(); 2231 EXPECT_CALL(prefs, GetInt64(kPrefsInstallDateDays, _)) 2232 .WillOnce(DoAll(SetArgPointee<1>(0), Return(true))); 2233 EXPECT_CALL(prefs, GetInt64(kPrefsLastActivePingDay, _)) 2234 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true))); 2235 EXPECT_CALL(prefs, GetInt64(kPrefsLastRollCallPingDay, _)) 2236 .WillOnce(DoAll(SetArgPointee<1>(future), Return(true))); 2237 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)) 2238 .WillOnce(Return(true)); 2239 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)) 2240 .WillOnce(Return(true)); 2241 brillo::Blob post_data; 2242 ASSERT_TRUE( 2243 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " 2244 "protocol=\"3.0\"><daystart elapsed_seconds=\"100\"/>" 2245 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" 2246 "<updatecheck status=\"noupdate\"/></app></response>", 2247 -1, 2248 false, // ping_only 2249 ErrorCode::kSuccess, 2250 metrics::CheckResult::kNoUpdateAvailable, 2251 metrics::CheckReaction::kUnset, 2252 metrics::DownloadErrorCode::kUnset, 2253 nullptr, 2254 &post_data)); 2255 string post_str(post_data.begin(), post_data.end()); 2256 EXPECT_EQ(post_str.find("ping"), string::npos); 2257 } 2258 2259 TEST_F(OmahaRequestActionTest, LastPingDayUpdateTest) { 2260 // This test checks that the action updates the last ping day to now 2261 // minus 200 seconds with a slack of 5 seconds. Therefore, the test 2262 // may fail if it runs for longer than 5 seconds. It shouldn't run 2263 // that long though. 2264 int64_t midnight = 2265 (Time::Now() - TimeDelta::FromSeconds(200)).ToInternalValue(); 2266 int64_t midnight_slack = 2267 (Time::Now() - TimeDelta::FromSeconds(195)).ToInternalValue(); 2268 NiceMock<MockPrefs> prefs; 2269 fake_system_state_.set_prefs(&prefs); 2270 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber()); 2271 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2272 EXPECT_CALL(prefs, 2273 SetInt64(kPrefsLastActivePingDay, 2274 AllOf(Ge(midnight), Le(midnight_slack)))) 2275 .WillOnce(Return(true)); 2276 EXPECT_CALL(prefs, 2277 SetInt64(kPrefsLastRollCallPingDay, 2278 AllOf(Ge(midnight), Le(midnight_slack)))) 2279 .WillOnce(Return(true)); 2280 ASSERT_TRUE( 2281 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " 2282 "protocol=\"3.0\"><daystart elapsed_seconds=\"200\"/>" 2283 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" 2284 "<updatecheck status=\"noupdate\"/></app></response>", 2285 -1, 2286 false, // ping_only 2287 ErrorCode::kSuccess, 2288 metrics::CheckResult::kNoUpdateAvailable, 2289 metrics::CheckReaction::kUnset, 2290 metrics::DownloadErrorCode::kUnset, 2291 nullptr, 2292 nullptr)); 2293 } 2294 2295 TEST_F(OmahaRequestActionTest, NoElapsedSecondsTest) { 2296 NiceMock<MockPrefs> prefs; 2297 fake_system_state_.set_prefs(&prefs); 2298 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber()); 2299 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2300 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); 2301 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); 2302 ASSERT_TRUE( 2303 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " 2304 "protocol=\"3.0\"><daystart blah=\"200\"/>" 2305 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" 2306 "<updatecheck status=\"noupdate\"/></app></response>", 2307 -1, 2308 false, // ping_only 2309 ErrorCode::kSuccess, 2310 metrics::CheckResult::kNoUpdateAvailable, 2311 metrics::CheckReaction::kUnset, 2312 metrics::DownloadErrorCode::kUnset, 2313 nullptr, 2314 nullptr)); 2315 } 2316 2317 TEST_F(OmahaRequestActionTest, BadElapsedSecondsTest) { 2318 NiceMock<MockPrefs> prefs; 2319 fake_system_state_.set_prefs(&prefs); 2320 EXPECT_CALL(prefs, GetInt64(_, _)).Times(AnyNumber()); 2321 EXPECT_CALL(prefs, SetInt64(_, _)).Times(AnyNumber()); 2322 EXPECT_CALL(prefs, SetInt64(kPrefsLastActivePingDay, _)).Times(0); 2323 EXPECT_CALL(prefs, SetInt64(kPrefsLastRollCallPingDay, _)).Times(0); 2324 ASSERT_TRUE( 2325 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " 2326 "protocol=\"3.0\"><daystart elapsed_seconds=\"x\"/>" 2327 "<app appid=\"foo\" status=\"ok\"><ping status=\"ok\"/>" 2328 "<updatecheck status=\"noupdate\"/></app></response>", 2329 -1, 2330 false, // ping_only 2331 ErrorCode::kSuccess, 2332 metrics::CheckResult::kNoUpdateAvailable, 2333 metrics::CheckReaction::kUnset, 2334 metrics::DownloadErrorCode::kUnset, 2335 nullptr, 2336 nullptr)); 2337 } 2338 2339 TEST_F(OmahaRequestActionTest, ParseUpdateCheckAttributesTest) { 2340 // Test that the "eol" flags is only parsed from the "_eol" attribute and not 2341 // the "eol" attribute. 2342 ASSERT_TRUE( 2343 TestUpdateCheck("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response " 2344 "protocol=\"3.0\"><app appid=\"foo\" status=\"ok\">" 2345 "<ping status=\"ok\"/><updatecheck status=\"noupdate\" " 2346 "_eol=\"security-only\" eol=\"eol\" _foo=\"bar\"/>" 2347 "</app></response>", 2348 -1, 2349 false, // ping_only 2350 ErrorCode::kSuccess, 2351 metrics::CheckResult::kNoUpdateAvailable, 2352 metrics::CheckReaction::kUnset, 2353 metrics::DownloadErrorCode::kUnset, 2354 nullptr, 2355 nullptr)); 2356 string eol_pref; 2357 EXPECT_TRUE( 2358 fake_system_state_.prefs()->GetString(kPrefsOmahaEolStatus, &eol_pref)); 2359 // Note that the eol="eol" attribute should be ignored and the _eol should be 2360 // used instead. 2361 EXPECT_EQ("security-only", eol_pref); 2362 } 2363 2364 TEST_F(OmahaRequestActionTest, NoUniqueIDTest) { 2365 brillo::Blob post_data; 2366 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 2367 -1, 2368 false, // ping_only 2369 ErrorCode::kOmahaRequestXMLParseError, 2370 metrics::CheckResult::kParsingError, 2371 metrics::CheckReaction::kUnset, 2372 metrics::DownloadErrorCode::kUnset, 2373 nullptr, // response 2374 &post_data)); 2375 // convert post_data to string 2376 string post_str(post_data.begin(), post_data.end()); 2377 EXPECT_EQ(post_str.find("machineid="), string::npos); 2378 EXPECT_EQ(post_str.find("userid="), string::npos); 2379 } 2380 2381 TEST_F(OmahaRequestActionTest, NetworkFailureTest) { 2382 OmahaResponse response; 2383 const int http_error_code = 2384 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 501; 2385 ASSERT_FALSE(TestUpdateCheck("", 2386 501, 2387 false, // ping_only 2388 static_cast<ErrorCode>(http_error_code), 2389 metrics::CheckResult::kDownloadError, 2390 metrics::CheckReaction::kUnset, 2391 static_cast<metrics::DownloadErrorCode>(501), 2392 &response, 2393 nullptr)); 2394 EXPECT_FALSE(response.update_exists); 2395 } 2396 2397 TEST_F(OmahaRequestActionTest, NetworkFailureBadHTTPCodeTest) { 2398 OmahaResponse response; 2399 const int http_error_code = 2400 static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + 999; 2401 ASSERT_FALSE(TestUpdateCheck("", 2402 1500, 2403 false, // ping_only 2404 static_cast<ErrorCode>(http_error_code), 2405 metrics::CheckResult::kDownloadError, 2406 metrics::CheckReaction::kUnset, 2407 metrics::DownloadErrorCode::kHttpStatusOther, 2408 &response, 2409 nullptr)); 2410 EXPECT_FALSE(response.update_exists); 2411 } 2412 2413 TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsPersistedFirstTime) { 2414 OmahaResponse response; 2415 request_params_.set_wall_clock_based_wait_enabled(true); 2416 request_params_.set_waiting_period(TimeDelta().FromDays(1)); 2417 request_params_.set_update_check_count_wait_enabled(false); 2418 2419 Time arbitrary_date; 2420 ASSERT_TRUE(Time::FromString("6/4/1989", &arbitrary_date)); 2421 fake_system_state_.fake_clock()->SetWallclockTime(arbitrary_date); 2422 ASSERT_FALSE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 2423 -1, 2424 false, // ping_only 2425 ErrorCode::kOmahaUpdateDeferredPerPolicy, 2426 metrics::CheckResult::kUpdateAvailable, 2427 metrics::CheckReaction::kDeferring, 2428 metrics::DownloadErrorCode::kUnset, 2429 &response, 2430 nullptr)); 2431 2432 int64_t timestamp = 0; 2433 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); 2434 EXPECT_EQ(arbitrary_date.ToInternalValue(), timestamp); 2435 EXPECT_FALSE(response.update_exists); 2436 2437 // Verify if we are interactive check we don't defer. 2438 request_params_.set_interactive(true); 2439 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 2440 -1, 2441 false, // ping_only 2442 ErrorCode::kSuccess, 2443 metrics::CheckResult::kUpdateAvailable, 2444 metrics::CheckReaction::kUpdating, 2445 metrics::DownloadErrorCode::kUnset, 2446 &response, 2447 nullptr)); 2448 EXPECT_TRUE(response.update_exists); 2449 } 2450 2451 TEST_F(OmahaRequestActionTest, TestUpdateFirstSeenAtGetsUsedIfAlreadyPresent) { 2452 OmahaResponse response; 2453 request_params_.set_wall_clock_based_wait_enabled(true); 2454 request_params_.set_waiting_period(TimeDelta().FromDays(1)); 2455 request_params_.set_update_check_count_wait_enabled(false); 2456 2457 Time t1, t2; 2458 ASSERT_TRUE(Time::FromString("1/1/2012", &t1)); 2459 ASSERT_TRUE(Time::FromString("1/3/2012", &t2)); 2460 ASSERT_TRUE( 2461 fake_prefs_.SetInt64(kPrefsUpdateFirstSeenAt, t1.ToInternalValue())); 2462 fake_system_state_.fake_clock()->SetWallclockTime(t2); 2463 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 2464 -1, 2465 false, // ping_only 2466 ErrorCode::kSuccess, 2467 metrics::CheckResult::kUpdateAvailable, 2468 metrics::CheckReaction::kUpdating, 2469 metrics::DownloadErrorCode::kUnset, 2470 &response, 2471 nullptr)); 2472 2473 EXPECT_TRUE(response.update_exists); 2474 2475 // Make sure the timestamp t1 is unchanged showing that it was reused. 2476 int64_t timestamp = 0; 2477 ASSERT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, ×tamp)); 2478 ASSERT_TRUE(timestamp == t1.ToInternalValue()); 2479 } 2480 2481 TEST_F(OmahaRequestActionTest, TestChangingToMoreStableChannel) { 2482 // Create a uniquely named test directory. 2483 base::ScopedTempDir tempdir; 2484 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 2485 2486 brillo::Blob post_data; 2487 request_params_.set_root(tempdir.GetPath().value()); 2488 request_params_.set_app_id("{22222222-2222-2222-2222-222222222222}"); 2489 request_params_.set_app_version("1.2.3.4"); 2490 request_params_.set_product_components("o.bundle=1"); 2491 request_params_.set_current_channel("canary-channel"); 2492 EXPECT_TRUE( 2493 request_params_.SetTargetChannel("stable-channel", true, nullptr)); 2494 request_params_.UpdateDownloadChannel(); 2495 EXPECT_TRUE(request_params_.ShouldPowerwash()); 2496 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 2497 -1, 2498 false, // ping_only 2499 ErrorCode::kOmahaRequestXMLParseError, 2500 metrics::CheckResult::kParsingError, 2501 metrics::CheckReaction::kUnset, 2502 metrics::DownloadErrorCode::kUnset, 2503 nullptr, // response 2504 &post_data)); 2505 // convert post_data to string 2506 string post_str(post_data.begin(), post_data.end()); 2507 EXPECT_NE( 2508 string::npos, 2509 post_str.find("appid=\"{22222222-2222-2222-2222-222222222222}\" " 2510 "version=\"0.0.0.0\" from_version=\"1.2.3.4\" " 2511 "track=\"stable-channel\" from_track=\"canary-channel\" ")); 2512 EXPECT_EQ(string::npos, post_str.find("o.bundle")); 2513 } 2514 2515 TEST_F(OmahaRequestActionTest, TestChangingToLessStableChannel) { 2516 // Create a uniquely named test directory. 2517 base::ScopedTempDir tempdir; 2518 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 2519 2520 brillo::Blob post_data; 2521 request_params_.set_root(tempdir.GetPath().value()); 2522 request_params_.set_app_id("{11111111-1111-1111-1111-111111111111}"); 2523 request_params_.set_app_version("5.6.7.8"); 2524 request_params_.set_product_components("o.bundle=1"); 2525 request_params_.set_current_channel("stable-channel"); 2526 EXPECT_TRUE( 2527 request_params_.SetTargetChannel("canary-channel", false, nullptr)); 2528 request_params_.UpdateDownloadChannel(); 2529 EXPECT_FALSE(request_params_.ShouldPowerwash()); 2530 ASSERT_FALSE(TestUpdateCheck("invalid xml>", 2531 -1, 2532 false, // ping_only 2533 ErrorCode::kOmahaRequestXMLParseError, 2534 metrics::CheckResult::kParsingError, 2535 metrics::CheckReaction::kUnset, 2536 metrics::DownloadErrorCode::kUnset, 2537 nullptr, // response 2538 &post_data)); 2539 // Convert post_data to string. 2540 string post_str(post_data.begin(), post_data.end()); 2541 EXPECT_NE( 2542 string::npos, 2543 post_str.find("appid=\"{11111111-1111-1111-1111-111111111111}\" " 2544 "version=\"5.6.7.8\" " 2545 "track=\"canary-channel\" from_track=\"stable-channel\"")); 2546 EXPECT_EQ(string::npos, post_str.find("from_version")); 2547 EXPECT_NE(string::npos, post_str.find("o.bundle.version=\"1\"")); 2548 } 2549 2550 // Checks that the initial ping with a=-1 r=-1 is not send when the device 2551 // was powerwashed. 2552 TEST_F(OmahaRequestActionTest, PingWhenPowerwashed) { 2553 fake_prefs_.SetString(kPrefsPreviousVersion, ""); 2554 2555 // Flag that the device was powerwashed in the past. 2556 fake_system_state_.fake_hardware()->SetPowerwashCount(1); 2557 2558 brillo::Blob post_data; 2559 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2560 -1, 2561 false, // ping_only 2562 ErrorCode::kSuccess, 2563 metrics::CheckResult::kNoUpdateAvailable, 2564 metrics::CheckReaction::kUnset, 2565 metrics::DownloadErrorCode::kUnset, 2566 nullptr, 2567 &post_data)); 2568 // We shouldn't send a ping in this case since powerwash > 0. 2569 string post_str(post_data.begin(), post_data.end()); 2570 EXPECT_EQ(string::npos, post_str.find("<ping")); 2571 } 2572 2573 // Checks that the initial ping with a=-1 r=-1 is not send when the device 2574 // first_active_omaha_ping_sent is set. 2575 TEST_F(OmahaRequestActionTest, PingWhenFirstActiveOmahaPingIsSent) { 2576 fake_prefs_.SetString(kPrefsPreviousVersion, ""); 2577 2578 // Flag that the device was not powerwashed in the past. 2579 fake_system_state_.fake_hardware()->SetPowerwashCount(0); 2580 2581 // Flag that the device has sent first active ping in the past. 2582 fake_system_state_.fake_hardware()->SetFirstActiveOmahaPingSent(); 2583 2584 brillo::Blob post_data; 2585 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2586 -1, 2587 false, // ping_only 2588 ErrorCode::kSuccess, 2589 metrics::CheckResult::kNoUpdateAvailable, 2590 metrics::CheckReaction::kUnset, 2591 metrics::DownloadErrorCode::kUnset, 2592 nullptr, 2593 &post_data)); 2594 // We shouldn't send a ping in this case since 2595 // first_active_omaha_ping_sent=true 2596 string post_str(post_data.begin(), post_data.end()); 2597 EXPECT_EQ(string::npos, post_str.find("<ping")); 2598 } 2599 2600 // Checks that the event 54 is sent on a reboot to a new update. 2601 TEST_F(OmahaRequestActionTest, RebootAfterUpdateEvent) { 2602 // Flag that the device was updated in a previous boot. 2603 fake_prefs_.SetString(kPrefsPreviousVersion, "1.2.3.4"); 2604 2605 brillo::Blob post_data; 2606 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 2607 -1, 2608 false, // ping_only 2609 ErrorCode::kSuccess, 2610 metrics::CheckResult::kNoUpdateAvailable, 2611 metrics::CheckReaction::kUnset, 2612 metrics::DownloadErrorCode::kUnset, 2613 nullptr, 2614 &post_data)); 2615 string post_str(post_data.begin(), post_data.end()); 2616 2617 // An event 54 is included and has the right version. 2618 EXPECT_NE( 2619 string::npos, 2620 post_str.find(base::StringPrintf("<event eventtype=\"%d\"", 2621 OmahaEvent::kTypeRebootedAfterUpdate))); 2622 EXPECT_NE(string::npos, 2623 post_str.find("previousversion=\"1.2.3.4\"></event>")); 2624 2625 // The previous version flag should have been removed. 2626 EXPECT_TRUE(fake_prefs_.Exists(kPrefsPreviousVersion)); 2627 string prev_version; 2628 EXPECT_TRUE(fake_prefs_.GetString(kPrefsPreviousVersion, &prev_version)); 2629 EXPECT_TRUE(prev_version.empty()); 2630 } 2631 2632 void OmahaRequestActionTest::P2PTest(bool initial_allow_p2p_for_downloading, 2633 bool initial_allow_p2p_for_sharing, 2634 bool omaha_disable_p2p_for_downloading, 2635 bool omaha_disable_p2p_for_sharing, 2636 bool payload_state_allow_p2p_attempt, 2637 bool expect_p2p_client_lookup, 2638 const string& p2p_client_result_url, 2639 bool expected_allow_p2p_for_downloading, 2640 bool expected_allow_p2p_for_sharing, 2641 const string& expected_p2p_url) { 2642 OmahaResponse response; 2643 bool actual_allow_p2p_for_downloading = initial_allow_p2p_for_downloading; 2644 bool actual_allow_p2p_for_sharing = initial_allow_p2p_for_sharing; 2645 string actual_p2p_url; 2646 2647 MockPayloadState mock_payload_state; 2648 fake_system_state_.set_payload_state(&mock_payload_state); 2649 EXPECT_CALL(mock_payload_state, P2PAttemptAllowed()) 2650 .WillRepeatedly(Return(payload_state_allow_p2p_attempt)); 2651 EXPECT_CALL(mock_payload_state, GetUsingP2PForDownloading()) 2652 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_downloading)); 2653 EXPECT_CALL(mock_payload_state, GetUsingP2PForSharing()) 2654 .WillRepeatedly(ReturnPointee(&actual_allow_p2p_for_sharing)); 2655 EXPECT_CALL(mock_payload_state, SetUsingP2PForDownloading(_)) 2656 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_downloading)); 2657 EXPECT_CALL(mock_payload_state, SetUsingP2PForSharing(_)) 2658 .WillRepeatedly(SaveArg<0>(&actual_allow_p2p_for_sharing)); 2659 EXPECT_CALL(mock_payload_state, SetP2PUrl(_)) 2660 .WillRepeatedly(SaveArg<0>(&actual_p2p_url)); 2661 2662 MockP2PManager mock_p2p_manager; 2663 fake_system_state_.set_p2p_manager(&mock_p2p_manager); 2664 mock_p2p_manager.fake().SetLookupUrlForFileResult(p2p_client_result_url); 2665 2666 TimeDelta timeout = TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds); 2667 EXPECT_CALL(mock_p2p_manager, LookupUrlForFile(_, _, timeout, _)) 2668 .Times(expect_p2p_client_lookup ? 1 : 0); 2669 2670 fake_update_response_.disable_p2p_for_downloading = 2671 omaha_disable_p2p_for_downloading; 2672 fake_update_response_.disable_p2p_for_sharing = omaha_disable_p2p_for_sharing; 2673 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 2674 -1, 2675 false, // ping_only 2676 ErrorCode::kSuccess, 2677 metrics::CheckResult::kUpdateAvailable, 2678 metrics::CheckReaction::kUpdating, 2679 metrics::DownloadErrorCode::kUnset, 2680 &response, 2681 nullptr)); 2682 EXPECT_TRUE(response.update_exists); 2683 2684 EXPECT_EQ(omaha_disable_p2p_for_downloading, 2685 response.disable_p2p_for_downloading); 2686 EXPECT_EQ(omaha_disable_p2p_for_sharing, response.disable_p2p_for_sharing); 2687 2688 EXPECT_EQ(expected_allow_p2p_for_downloading, 2689 actual_allow_p2p_for_downloading); 2690 EXPECT_EQ(expected_allow_p2p_for_sharing, actual_allow_p2p_for_sharing); 2691 EXPECT_EQ(expected_p2p_url, actual_p2p_url); 2692 } 2693 2694 TEST_F(OmahaRequestActionTest, P2PWithPeer) { 2695 P2PTest(true, // initial_allow_p2p_for_downloading 2696 true, // initial_allow_p2p_for_sharing 2697 false, // omaha_disable_p2p_for_downloading 2698 false, // omaha_disable_p2p_for_sharing 2699 true, // payload_state_allow_p2p_attempt 2700 true, // expect_p2p_client_lookup 2701 "http://1.3.5.7/p2p", // p2p_client_result_url 2702 true, // expected_allow_p2p_for_downloading 2703 true, // expected_allow_p2p_for_sharing 2704 "http://1.3.5.7/p2p"); // expected_p2p_url 2705 } 2706 2707 TEST_F(OmahaRequestActionTest, P2PWithoutPeer) { 2708 P2PTest(true, // initial_allow_p2p_for_downloading 2709 true, // initial_allow_p2p_for_sharing 2710 false, // omaha_disable_p2p_for_downloading 2711 false, // omaha_disable_p2p_for_sharing 2712 true, // payload_state_allow_p2p_attempt 2713 true, // expect_p2p_client_lookup 2714 "", // p2p_client_result_url 2715 false, // expected_allow_p2p_for_downloading 2716 true, // expected_allow_p2p_for_sharing 2717 ""); // expected_p2p_url 2718 } 2719 2720 TEST_F(OmahaRequestActionTest, P2PDownloadNotAllowed) { 2721 P2PTest(false, // initial_allow_p2p_for_downloading 2722 true, // initial_allow_p2p_for_sharing 2723 false, // omaha_disable_p2p_for_downloading 2724 false, // omaha_disable_p2p_for_sharing 2725 true, // payload_state_allow_p2p_attempt 2726 false, // expect_p2p_client_lookup 2727 "unset", // p2p_client_result_url 2728 false, // expected_allow_p2p_for_downloading 2729 true, // expected_allow_p2p_for_sharing 2730 ""); // expected_p2p_url 2731 } 2732 2733 TEST_F(OmahaRequestActionTest, P2PWithPeerDownloadDisabledByOmaha) { 2734 P2PTest(true, // initial_allow_p2p_for_downloading 2735 true, // initial_allow_p2p_for_sharing 2736 true, // omaha_disable_p2p_for_downloading 2737 false, // omaha_disable_p2p_for_sharing 2738 true, // payload_state_allow_p2p_attempt 2739 false, // expect_p2p_client_lookup 2740 "unset", // p2p_client_result_url 2741 false, // expected_allow_p2p_for_downloading 2742 true, // expected_allow_p2p_for_sharing 2743 ""); // expected_p2p_url 2744 } 2745 2746 TEST_F(OmahaRequestActionTest, P2PWithPeerSharingDisabledByOmaha) { 2747 P2PTest(true, // initial_allow_p2p_for_downloading 2748 true, // initial_allow_p2p_for_sharing 2749 false, // omaha_disable_p2p_for_downloading 2750 true, // omaha_disable_p2p_for_sharing 2751 true, // payload_state_allow_p2p_attempt 2752 true, // expect_p2p_client_lookup 2753 "http://1.3.5.7/p2p", // p2p_client_result_url 2754 true, // expected_allow_p2p_for_downloading 2755 false, // expected_allow_p2p_for_sharing 2756 "http://1.3.5.7/p2p"); // expected_p2p_url 2757 } 2758 2759 TEST_F(OmahaRequestActionTest, P2PWithPeerBothDisabledByOmaha) { 2760 P2PTest(true, // initial_allow_p2p_for_downloading 2761 true, // initial_allow_p2p_for_sharing 2762 true, // omaha_disable_p2p_for_downloading 2763 true, // omaha_disable_p2p_for_sharing 2764 true, // payload_state_allow_p2p_attempt 2765 false, // expect_p2p_client_lookup 2766 "unset", // p2p_client_result_url 2767 false, // expected_allow_p2p_for_downloading 2768 false, // expected_allow_p2p_for_sharing 2769 ""); // expected_p2p_url 2770 } 2771 2772 bool OmahaRequestActionTest::InstallDateParseHelper(const string& elapsed_days, 2773 OmahaResponse* response) { 2774 fake_update_response_.elapsed_days = elapsed_days; 2775 return TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 2776 -1, 2777 false, // ping_only 2778 ErrorCode::kSuccess, 2779 metrics::CheckResult::kUpdateAvailable, 2780 metrics::CheckReaction::kUpdating, 2781 metrics::DownloadErrorCode::kUnset, 2782 response, 2783 nullptr); 2784 } 2785 2786 TEST_F(OmahaRequestActionTest, ParseInstallDateFromResponse) { 2787 OmahaResponse response; 2788 2789 // Simulate a successful update check that happens during OOBE. The 2790 // deadline in the response is needed to force the update attempt to 2791 // occur; responses without a deadline seen during OOBE will normally 2792 // return ErrorCode::kNonCriticalUpdateInOOBE. 2793 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete(); 2794 fake_update_response_.deadline = "20101020"; 2795 2796 // Check that we parse elapsed_days in the Omaha Response correctly. 2797 // and that the kPrefsInstallDateDays value is written to. 2798 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays)); 2799 EXPECT_TRUE(InstallDateParseHelper("42", &response)); 2800 EXPECT_TRUE(response.update_exists); 2801 EXPECT_EQ(42, response.install_date_days); 2802 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays)); 2803 int64_t prefs_days; 2804 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); 2805 EXPECT_EQ(prefs_days, 42); 2806 2807 // If there already is a value set, we shouldn't do anything. 2808 EXPECT_TRUE(InstallDateParseHelper("7", &response)); 2809 EXPECT_TRUE(response.update_exists); 2810 EXPECT_EQ(7, response.install_date_days); 2811 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); 2812 EXPECT_EQ(prefs_days, 42); 2813 2814 // Note that elapsed_days is not necessarily divisible by 7 so check 2815 // that we round down correctly when populating kPrefsInstallDateDays. 2816 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays)); 2817 EXPECT_TRUE(InstallDateParseHelper("23", &response)); 2818 EXPECT_TRUE(response.update_exists); 2819 EXPECT_EQ(23, response.install_date_days); 2820 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); 2821 EXPECT_EQ(prefs_days, 21); 2822 2823 // Check that we correctly handle elapsed_days not being included in 2824 // the Omaha Response. 2825 EXPECT_TRUE(InstallDateParseHelper("", &response)); 2826 EXPECT_TRUE(response.update_exists); 2827 EXPECT_EQ(-1, response.install_date_days); 2828 } 2829 2830 // If there is no prefs and OOBE is not complete, we should not 2831 // report anything to Omaha. 2832 TEST_F(OmahaRequestActionTest, GetInstallDateWhenNoPrefsNorOOBE) { 2833 fake_system_state_.fake_hardware()->UnsetIsOOBEComplete(); 2834 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1); 2835 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays)); 2836 } 2837 2838 // If OOBE is complete and happened on a valid date (e.g. after Jan 2839 // 1 2007 0:00 PST), that date should be used and written to 2840 // prefs. However, first try with an invalid date and check we do 2841 // nothing. 2842 TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithInvalidDate) { 2843 Time oobe_date = Time::FromTimeT(42); // Dec 31, 1969 16:00:42 PST. 2844 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date); 2845 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), -1); 2846 EXPECT_FALSE(fake_prefs_.Exists(kPrefsInstallDateDays)); 2847 } 2848 2849 // Then check with a valid date. The date Jan 20, 2007 0:00 PST 2850 // should yield an InstallDate of 14. 2851 TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedWithValidDate) { 2852 Time oobe_date = Time::FromTimeT(1169280000); // Jan 20, 2007 0:00 PST. 2853 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date); 2854 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14); 2855 EXPECT_TRUE(fake_prefs_.Exists(kPrefsInstallDateDays)); 2856 2857 int64_t prefs_days; 2858 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); 2859 EXPECT_EQ(prefs_days, 14); 2860 } 2861 2862 // Now that we have a valid date in prefs, check that we keep using 2863 // that even if OOBE date reports something else. The date Jan 30, 2864 // 2007 0:00 PST should yield an InstallDate of 28... but since 2865 // there's a prefs file, we should still get 14. 2866 TEST_F(OmahaRequestActionTest, GetInstallDateWhenOOBECompletedDateChanges) { 2867 // Set a valid date in the prefs first. 2868 EXPECT_TRUE(fake_prefs_.SetInt64(kPrefsInstallDateDays, 14)); 2869 2870 Time oobe_date = Time::FromTimeT(1170144000); // Jan 30, 2007 0:00 PST. 2871 fake_system_state_.fake_hardware()->SetIsOOBEComplete(oobe_date); 2872 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 14); 2873 2874 int64_t prefs_days; 2875 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); 2876 EXPECT_EQ(prefs_days, 14); 2877 2878 // If we delete the prefs file, we should get 28 days. 2879 EXPECT_TRUE(fake_prefs_.Delete(kPrefsInstallDateDays)); 2880 EXPECT_EQ(OmahaRequestAction::GetInstallDate(&fake_system_state_), 28); 2881 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsInstallDateDays, &prefs_days)); 2882 EXPECT_EQ(prefs_days, 28); 2883 } 2884 2885 // Verifies that a device with no device policy, and is not a consumer 2886 // device sets the max kernel key version to the current version. 2887 // ie. the same behavior as if rollback is enabled. 2888 TEST_F(OmahaRequestActionTest, NoPolicyEnterpriseDevicesSetMaxRollback) { 2889 FakeHardware* fake_hw = fake_system_state_.fake_hardware(); 2890 2891 // Setup and verify some initial default values for the kernel TPM 2892 // values that control verified boot and rollback. 2893 const int min_kernel_version = 4; 2894 fake_hw->SetMinKernelKeyVersion(min_kernel_version); 2895 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity); 2896 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 2897 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward()); 2898 2899 EXPECT_CALL( 2900 *fake_system_state_.mock_metrics_reporter(), 2901 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true)) 2902 .Times(1); 2903 2904 OmahaResponse response; 2905 TestRollbackCheck(false /* is_consumer_device */, 2906 3 /* rollback_allowed_milestones */, 2907 false /* is_policy_loaded */, 2908 &response); 2909 2910 // Verify kernel_max_rollforward was set to the current minimum 2911 // kernel key version. This has the effect of freezing roll 2912 // forwards indefinitely. This will hold the rollback window 2913 // open until a future change will be able to move this forward 2914 // relative the configured window. 2915 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 2916 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward()); 2917 } 2918 2919 // Verifies that a conmsumer device with no device policy sets the 2920 // max kernel key version to the current version. ie. the same 2921 // behavior as if rollback is enabled. 2922 TEST_F(OmahaRequestActionTest, NoPolicyConsumerDevicesSetMaxRollback) { 2923 FakeHardware* fake_hw = fake_system_state_.fake_hardware(); 2924 2925 // Setup and verify some initial default values for the kernel TPM 2926 // values that control verified boot and rollback. 2927 const int min_kernel_version = 3; 2928 fake_hw->SetMinKernelKeyVersion(min_kernel_version); 2929 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity); 2930 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 2931 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward()); 2932 2933 EXPECT_CALL( 2934 *fake_system_state_.mock_metrics_reporter(), 2935 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true)) 2936 .Times(1); 2937 2938 OmahaResponse response; 2939 TestRollbackCheck(true /* is_consumer_device */, 2940 3 /* rollback_allowed_milestones */, 2941 false /* is_policy_loaded */, 2942 &response); 2943 2944 // Verify that with rollback disabled that kernel_max_rollforward 2945 // was set to logical infinity. This is the expected behavior for 2946 // consumer devices and matches the existing behavior prior to the 2947 // rollback features. 2948 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 2949 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward()); 2950 } 2951 2952 // Verifies that a device with rollback enabled sets kernel_max_rollforward 2953 // in the TPM to prevent roll forward. 2954 TEST_F(OmahaRequestActionTest, RollbackEnabledDevicesSetMaxRollback) { 2955 FakeHardware* fake_hw = fake_system_state_.fake_hardware(); 2956 2957 // Setup and verify some initial default values for the kernel TPM 2958 // values that control verified boot and rollback. 2959 const int allowed_milestones = 4; 2960 const int min_kernel_version = 3; 2961 fake_hw->SetMinKernelKeyVersion(min_kernel_version); 2962 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity); 2963 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 2964 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward()); 2965 2966 EXPECT_CALL( 2967 *fake_system_state_.mock_metrics_reporter(), 2968 ReportKeyVersionMetrics(min_kernel_version, min_kernel_version, true)) 2969 .Times(1); 2970 2971 OmahaResponse response; 2972 TestRollbackCheck(false /* is_consumer_device */, 2973 allowed_milestones, 2974 true /* is_policy_loaded */, 2975 &response); 2976 2977 // Verify that with rollback enabled that kernel_max_rollforward 2978 // was set to the current minimum kernel key version. This has 2979 // the effect of freezing roll forwards indefinitely. This will 2980 // hold the rollback window open until a future change will 2981 // be able to move this forward relative the configured window. 2982 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 2983 EXPECT_EQ(min_kernel_version, fake_hw->GetMaxKernelKeyRollforward()); 2984 } 2985 2986 // Verifies that a device with rollback disabled sets kernel_max_rollforward 2987 // in the TPM to logical infinity, to allow roll forward. 2988 TEST_F(OmahaRequestActionTest, RollbackDisabledDevicesSetMaxRollback) { 2989 FakeHardware* fake_hw = fake_system_state_.fake_hardware(); 2990 2991 // Setup and verify some initial default values for the kernel TPM 2992 // values that control verified boot and rollback. 2993 const int allowed_milestones = 0; 2994 const int min_kernel_version = 3; 2995 fake_hw->SetMinKernelKeyVersion(min_kernel_version); 2996 fake_hw->SetMaxKernelKeyRollforward(kRollforwardInfinity); 2997 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 2998 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward()); 2999 3000 EXPECT_CALL( 3001 *fake_system_state_.mock_metrics_reporter(), 3002 ReportKeyVersionMetrics(min_kernel_version, kRollforwardInfinity, true)) 3003 .Times(1); 3004 3005 OmahaResponse response; 3006 TestRollbackCheck(false /* is_consumer_device */, 3007 allowed_milestones, 3008 true /* is_policy_loaded */, 3009 &response); 3010 3011 // Verify that with rollback disabled that kernel_max_rollforward 3012 // was set to logical infinity. 3013 EXPECT_EQ(min_kernel_version, fake_hw->GetMinKernelKeyVersion()); 3014 EXPECT_EQ(kRollforwardInfinity, fake_hw->GetMaxKernelKeyRollforward()); 3015 } 3016 3017 TEST_F(OmahaRequestActionTest, RollbackResponseParsedNoEntries) { 3018 OmahaResponse response; 3019 fake_update_response_.rollback = true; 3020 TestRollbackCheck(false /* is_consumer_device */, 3021 4 /* rollback_allowed_milestones */, 3022 true /* is_policy_loaded */, 3023 &response); 3024 EXPECT_TRUE(response.is_rollback); 3025 } 3026 3027 TEST_F(OmahaRequestActionTest, RollbackResponseValidVersionsParsed) { 3028 OmahaResponse response; 3029 fake_update_response_.rollback_firmware_version = "1.2"; 3030 fake_update_response_.rollback_kernel_version = "3.4"; 3031 fake_update_response_.rollback = true; 3032 TestRollbackCheck(false /* is_consumer_device */, 3033 4 /* rollback_allowed_milestones */, 3034 true /* is_policy_loaded */, 3035 &response); 3036 EXPECT_TRUE(response.is_rollback); 3037 EXPECT_EQ(1, response.rollback_key_version.firmware_key); 3038 EXPECT_EQ(2, response.rollback_key_version.firmware); 3039 EXPECT_EQ(3, response.rollback_key_version.kernel_key); 3040 EXPECT_EQ(4, response.rollback_key_version.kernel); 3041 } 3042 3043 TEST_F(OmahaRequestActionTest, 3044 TestUpdateFirstSeenAtPrefPersistedIfUpdateExists) { 3045 FakeClock fake_clock; 3046 Time now = Time::Now(); 3047 fake_clock.SetWallclockTime(now); 3048 fake_system_state_.set_clock(&fake_clock); 3049 3050 OmahaResponse response; 3051 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 3052 -1, 3053 false, // ping_only 3054 ErrorCode::kSuccess, 3055 metrics::CheckResult::kUpdateAvailable, 3056 metrics::CheckReaction::kUpdating, 3057 metrics::DownloadErrorCode::kUnset, 3058 &response, 3059 nullptr)); 3060 EXPECT_TRUE(response.update_exists); 3061 EXPECT_TRUE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt)); 3062 3063 int64_t stored_first_seen_at_time; 3064 EXPECT_TRUE(fake_prefs_.GetInt64(kPrefsUpdateFirstSeenAt, 3065 &stored_first_seen_at_time)); 3066 EXPECT_EQ(now.ToInternalValue(), stored_first_seen_at_time); 3067 } 3068 3069 TEST_F(OmahaRequestActionTest, 3070 TestUpdateFirstSeenAtPrefNotPersistedIfUpdateFails) { 3071 FakeClock fake_clock; 3072 Time now = Time::Now(); 3073 fake_clock.SetWallclockTime(now); 3074 fake_system_state_.set_clock(&fake_clock); 3075 3076 OmahaResponse response; 3077 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetNoUpdateResponse(), 3078 -1, 3079 false, // ping_only 3080 ErrorCode::kSuccess, 3081 metrics::CheckResult::kNoUpdateAvailable, 3082 metrics::CheckReaction::kUnset, 3083 metrics::DownloadErrorCode::kUnset, 3084 &response, 3085 nullptr)); 3086 EXPECT_FALSE(response.update_exists); 3087 EXPECT_FALSE(fake_prefs_.Exists(kPrefsUpdateFirstSeenAt)); 3088 } 3089 3090 TEST_F(OmahaRequestActionTest, InstallTest) { 3091 OmahaResponse response; 3092 request_params_.set_is_install(true); 3093 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"}); 3094 brillo::Blob post_data; 3095 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 3096 -1, 3097 false, // ping_only 3098 true, // is_consumer_device 3099 0, // rollback_allowed_milestones 3100 false, // is_policy_loaded 3101 ErrorCode::kSuccess, 3102 metrics::CheckResult::kUpdateAvailable, 3103 metrics::CheckReaction::kUpdating, 3104 metrics::DownloadErrorCode::kUnset, 3105 &response, 3106 &post_data)); 3107 // Convert post_data to string. 3108 string post_str(post_data.begin(), post_data.end()); 3109 for (const auto& dlc_module_id : request_params_.dlc_module_ids()) { 3110 EXPECT_NE(string::npos, 3111 post_str.find("appid=\"" + fake_update_response_.app_id + "_" + 3112 dlc_module_id + "\"")); 3113 } 3114 EXPECT_NE(string::npos, 3115 post_str.find("appid=\"" + fake_update_response_.app_id + "\"")); 3116 3117 // Count number of updatecheck tag in response. 3118 int updatecheck_count = 0; 3119 size_t pos = 0; 3120 while ((pos = post_str.find("<updatecheck", pos)) != string::npos) { 3121 updatecheck_count++; 3122 pos++; 3123 } 3124 EXPECT_EQ(request_params_.dlc_module_ids().size(), updatecheck_count); 3125 } 3126 3127 TEST_F(OmahaRequestActionTest, InstallMissingPlatformVersionTest) { 3128 fake_update_response_.multi_app_skip_updatecheck = true; 3129 fake_update_response_.multi_app_no_update = false; 3130 request_params_.set_is_install(true); 3131 request_params_.set_dlc_module_ids({"dlc_no_0", "dlc_no_1"}); 3132 request_params_.set_app_id(fake_update_response_.app_id_skip_updatecheck); 3133 OmahaResponse response; 3134 ASSERT_TRUE(TestUpdateCheck(fake_update_response_.GetUpdateResponse(), 3135 -1, 3136 false, // ping_only 3137 ErrorCode::kSuccess, 3138 metrics::CheckResult::kUpdateAvailable, 3139 metrics::CheckReaction::kUpdating, 3140 metrics::DownloadErrorCode::kUnset, 3141 &response, 3142 nullptr)); 3143 EXPECT_TRUE(response.update_exists); 3144 EXPECT_EQ(fake_update_response_.current_version, response.version); 3145 } 3146 3147 } // namespace chromeos_update_engine 3148