| OLD | NEW | 
|    1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |    1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #include <base/file_util.h> |    5 #include <base/file_util.h> | 
|    6 #include <gtest/gtest.h> |    6 #include <gtest/gtest.h> | 
|    7  |    7  | 
|    8 #include "update_engine/action_mock.h" |    8 #include "update_engine/action_mock.h" | 
|    9 #include "update_engine/action_processor_mock.h" |    9 #include "update_engine/action_processor_mock.h" | 
|   10 #include "update_engine/filesystem_copier_action.h" |   10 #include "update_engine/filesystem_copier_action.h" | 
|   11 #include "update_engine/postinstall_runner_action.h" |   11 #include "update_engine/postinstall_runner_action.h" | 
 |   12 #include "update_engine/prefs_mock.h" | 
|   12 #include "update_engine/set_bootable_flag_action.h" |   13 #include "update_engine/set_bootable_flag_action.h" | 
|   13 #include "update_engine/update_attempter.h" |   14 #include "update_engine/update_attempter.h" | 
|   14  |   15  | 
|   15 using std::string; |   16 using std::string; | 
 |   17 using testing::_; | 
 |   18 using testing::DoAll; | 
|   16 using testing::InSequence; |   19 using testing::InSequence; | 
|   17 using testing::Property; |   20 using testing::Property; | 
|   18 using testing::Return; |   21 using testing::Return; | 
 |   22 using testing::SetArgumentPointee; | 
|   19  |   23  | 
|   20 namespace chromeos_update_engine { |   24 namespace chromeos_update_engine { | 
|   21  |   25  | 
|   22 // Test a subclass rather than the main class directly so that we can mock out |   26 // Test a subclass rather than the main class directly so that we can mock out | 
|   23 // methods within the class. There're explicit unit tests for the mocked out |   27 // methods within the class. There're explicit unit tests for the mocked out | 
|   24 // methods. |   28 // methods. | 
|   25 class UpdateAttempterUnderTest : public UpdateAttempter { |   29 class UpdateAttempterUnderTest : public UpdateAttempter { | 
|   26  public: |   30  public: | 
|   27   UpdateAttempterUnderTest() |   31   UpdateAttempterUnderTest() | 
|   28       : UpdateAttempter(NULL, NULL) {} |   32       : UpdateAttempter(NULL, NULL) {} | 
|   29 }; |   33 }; | 
|   30  |   34  | 
|   31 class UpdateAttempterTest : public ::testing::Test { |   35 class UpdateAttempterTest : public ::testing::Test { | 
|   32  protected: |   36  protected: | 
|   33   virtual void SetUp() { |   37   virtual void SetUp() { | 
|   34     EXPECT_EQ(NULL, attempter_.dbus_service_); |   38     EXPECT_EQ(NULL, attempter_.dbus_service_); | 
|   35     EXPECT_EQ(NULL, attempter_.prefs_); |   39     EXPECT_EQ(NULL, attempter_.prefs_); | 
|   36     EXPECT_EQ(NULL, attempter_.metrics_lib_); |   40     EXPECT_EQ(NULL, attempter_.metrics_lib_); | 
|   37     EXPECT_EQ(NULL, attempter_.update_check_scheduler_); |   41     EXPECT_EQ(NULL, attempter_.update_check_scheduler_); | 
|   38     EXPECT_EQ(0, attempter_.http_response_code_); |   42     EXPECT_EQ(0, attempter_.http_response_code_); | 
|   39     EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_); |   43     EXPECT_EQ(utils::kProcessPriorityNormal, attempter_.priority_); | 
|   40     EXPECT_EQ(NULL, attempter_.manage_priority_source_); |   44     EXPECT_EQ(NULL, attempter_.manage_priority_source_); | 
|   41     EXPECT_FALSE(attempter_.download_active_); |   45     EXPECT_FALSE(attempter_.download_active_); | 
|   42     EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_); |   46     EXPECT_EQ(UPDATE_STATUS_IDLE, attempter_.status_); | 
|   43     EXPECT_EQ(0.0, attempter_.download_progress_); |   47     EXPECT_EQ(0.0, attempter_.download_progress_); | 
|   44     EXPECT_EQ(0, attempter_.last_checked_time_); |   48     EXPECT_EQ(0, attempter_.last_checked_time_); | 
|   45     EXPECT_EQ("0.0.0.0", attempter_.new_version_); |   49     EXPECT_EQ("0.0.0.0", attempter_.new_version_); | 
|   46     EXPECT_EQ(0, attempter_.new_size_); |   50     EXPECT_EQ(0, attempter_.new_size_); | 
 |   51     EXPECT_FALSE(attempter_.is_full_update_); | 
|   47     processor_ = new ActionProcessorMock(); |   52     processor_ = new ActionProcessorMock(); | 
|   48     attempter_.processor_.reset(processor_);  // Transfers ownership. |   53     attempter_.processor_.reset(processor_);  // Transfers ownership. | 
 |   54     attempter_.prefs_ = &prefs_; | 
|   49   } |   55   } | 
|   50  |   56  | 
|   51   UpdateAttempterUnderTest attempter_; |   57   UpdateAttempterUnderTest attempter_; | 
|   52   ActionProcessorMock* processor_; |   58   ActionProcessorMock* processor_; | 
 |   59   PrefsMock prefs_; | 
|   53 }; |   60 }; | 
|   54  |   61  | 
|   55 TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { |   62 TEST_F(UpdateAttempterTest, RunAsRootConstructWithUpdatedMarkerTest) { | 
|   56   extern const char* kUpdateCompletedMarker; |   63   extern const char* kUpdateCompletedMarker; | 
|   57   const FilePath kMarker(kUpdateCompletedMarker); |   64   const FilePath kMarker(kUpdateCompletedMarker); | 
|   58   EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0)); |   65   EXPECT_EQ(0, file_util::WriteFile(kMarker, "", 0)); | 
|   59   UpdateAttempterUnderTest attempter; |   66   UpdateAttempterUnderTest attempter; | 
|   60   EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status()); |   67   EXPECT_EQ(UPDATE_STATUS_UPDATED_NEED_REBOOT, attempter.status()); | 
|   61   EXPECT_TRUE(file_util::Delete(kMarker, false)); |   68   EXPECT_TRUE(file_util::Delete(kMarker, false)); | 
|   62 } |   69 } | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
|   85   SetBootableFlagAction set_bootable_flag_action; |   92   SetBootableFlagAction set_bootable_flag_action; | 
|   86   EXPECT_EQ(kActionCodeSetBootableFlagError, |   93   EXPECT_EQ(kActionCodeSetBootableFlagError, | 
|   87             GetErrorCodeForAction(&set_bootable_flag_action, |   94             GetErrorCodeForAction(&set_bootable_flag_action, | 
|   88                                   kActionCodeError)); |   95                                   kActionCodeError)); | 
|   89   ActionMock action_mock; |   96   ActionMock action_mock; | 
|   90   EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock")); |   97   EXPECT_CALL(action_mock, Type()).Times(1).WillOnce(Return("ActionMock")); | 
|   91   EXPECT_EQ(kActionCodeError, |   98   EXPECT_EQ(kActionCodeError, | 
|   92             GetErrorCodeForAction(&action_mock, kActionCodeError)); |   99             GetErrorCodeForAction(&action_mock, kActionCodeError)); | 
|   93 } |  100 } | 
|   94  |  101  | 
 |  102 TEST_F(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest) { | 
 |  103   attempter_.omaha_request_params_.delta_okay = true; | 
 |  104   EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) | 
 |  105       .WillOnce(Return(false)); | 
 |  106   attempter_.DisableDeltaUpdateIfNeeded(); | 
 |  107   EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); | 
 |  108   EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) | 
 |  109       .WillOnce(DoAll( | 
 |  110           SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures - 1), | 
 |  111           Return(true))); | 
 |  112   attempter_.DisableDeltaUpdateIfNeeded(); | 
 |  113   EXPECT_TRUE(attempter_.omaha_request_params_.delta_okay); | 
 |  114   EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) | 
 |  115       .WillOnce(DoAll( | 
 |  116           SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), | 
 |  117           Return(true))); | 
 |  118   attempter_.DisableDeltaUpdateIfNeeded(); | 
 |  119   EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); | 
 |  120   EXPECT_CALL(prefs_, GetInt64(_, _)).Times(0); | 
 |  121   attempter_.DisableDeltaUpdateIfNeeded(); | 
 |  122   EXPECT_FALSE(attempter_.omaha_request_params_.delta_okay); | 
 |  123 } | 
 |  124  | 
 |  125 TEST_F(UpdateAttempterTest, MarkDeltaUpdateFailureTest) { | 
 |  126   attempter_.is_full_update_ = false; | 
 |  127   EXPECT_CALL(prefs_, GetInt64(kPrefsDeltaUpdateFailures, _)) | 
 |  128       .WillOnce(Return(false)) | 
 |  129       .WillOnce(DoAll(SetArgumentPointee<1>(-1), Return(true))) | 
 |  130       .WillOnce(DoAll(SetArgumentPointee<1>(1), Return(true))) | 
 |  131       .WillOnce(DoAll( | 
 |  132           SetArgumentPointee<1>(UpdateAttempter::kMaxDeltaUpdateFailures), | 
 |  133           Return(true))); | 
 |  134   EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 1)).Times(2); | 
 |  135   EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, 2)).Times(1); | 
 |  136   EXPECT_CALL(prefs_, SetInt64(kPrefsDeltaUpdateFailures, | 
 |  137                                UpdateAttempter::kMaxDeltaUpdateFailures + 1)) | 
 |  138       .Times(1); | 
 |  139   for (int i = 0; i < 4; i ++) | 
 |  140     attempter_.MarkDeltaUpdateFailure(); | 
 |  141 } | 
 |  142  | 
|   95 TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { |  143 TEST_F(UpdateAttempterTest, UpdateStatusToStringTest) { | 
|   96   extern const char* UpdateStatusToString(UpdateStatus); |  144   extern const char* UpdateStatusToString(UpdateStatus); | 
|   97   EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); |  145   EXPECT_STREQ("UPDATE_STATUS_IDLE", UpdateStatusToString(UPDATE_STATUS_IDLE)); | 
|   98   EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", |  146   EXPECT_STREQ("UPDATE_STATUS_CHECKING_FOR_UPDATE", | 
|   99                UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); |  147                UpdateStatusToString(UPDATE_STATUS_CHECKING_FOR_UPDATE)); | 
|  100   EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", |  148   EXPECT_STREQ("UPDATE_STATUS_UPDATE_AVAILABLE", | 
|  101                UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); |  149                UpdateStatusToString(UPDATE_STATUS_UPDATE_AVAILABLE)); | 
|  102   EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", |  150   EXPECT_STREQ("UPDATE_STATUS_DOWNLOADING", | 
|  103                UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); |  151                UpdateStatusToString(UPDATE_STATUS_DOWNLOADING)); | 
|  104   EXPECT_STREQ("UPDATE_STATUS_VERIFYING", |  152   EXPECT_STREQ("UPDATE_STATUS_VERIFYING", | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  147   EXPECT_EQ(attempter_.response_handler_action_.get(), |  195   EXPECT_EQ(attempter_.response_handler_action_.get(), | 
|  148             attempter_.actions_[1].get()); |  196             attempter_.actions_[1].get()); | 
|  149   DownloadAction* download_action = |  197   DownloadAction* download_action = | 
|  150       dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); |  198       dynamic_cast<DownloadAction*>(attempter_.actions_[5].get()); | 
|  151   ASSERT_TRUE(download_action != NULL); |  199   ASSERT_TRUE(download_action != NULL); | 
|  152   EXPECT_EQ(&attempter_, download_action->delegate()); |  200   EXPECT_EQ(&attempter_, download_action->delegate()); | 
|  153   EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); |  201   EXPECT_EQ(UPDATE_STATUS_CHECKING_FOR_UPDATE, attempter_.status()); | 
|  154 } |  202 } | 
|  155  |  203  | 
|  156 }  // namespace chromeos_update_engine |  204 }  // namespace chromeos_update_engine | 
| OLD | NEW |