real_updater_provider_unittest.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //
  2. // Copyright (C) 2014 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. #include "update_engine/update_manager/real_updater_provider.h"
  17. #include <memory>
  18. #include <string>
  19. #include <base/time/time.h>
  20. #include <gtest/gtest.h>
  21. #include <update_engine/dbus-constants.h>
  22. #include "update_engine/common/fake_clock.h"
  23. #include "update_engine/common/fake_prefs.h"
  24. #include "update_engine/fake_system_state.h"
  25. #include "update_engine/mock_update_attempter.h"
  26. #include "update_engine/omaha_request_params.h"
  27. #include "update_engine/update_manager/umtest_utils.h"
  28. using base::Time;
  29. using base::TimeDelta;
  30. using chromeos_update_engine::FakeClock;
  31. using chromeos_update_engine::FakePrefs;
  32. using chromeos_update_engine::FakeSystemState;
  33. using chromeos_update_engine::OmahaRequestParams;
  34. using std::string;
  35. using std::unique_ptr;
  36. using testing::_;
  37. using testing::DoAll;
  38. using testing::Return;
  39. using testing::SetArgPointee;
  40. using update_engine::UpdateAttemptFlags;
  41. namespace {
  42. // Generates a fixed timestamp for use in faking the current time.
  43. Time FixedTime() {
  44. Time::Exploded now_exp;
  45. now_exp.year = 2014;
  46. now_exp.month = 3;
  47. now_exp.day_of_week = 2;
  48. now_exp.day_of_month = 18;
  49. now_exp.hour = 8;
  50. now_exp.minute = 5;
  51. now_exp.second = 33;
  52. now_exp.millisecond = 675;
  53. Time time;
  54. ignore_result(Time::FromLocalExploded(now_exp, &time));
  55. return time;
  56. }
  57. // Rounds down a timestamp to the nearest second. This is useful when faking
  58. // times that are converted to time_t (no sub-second resolution).
  59. Time RoundedToSecond(Time time) {
  60. Time::Exploded exp;
  61. time.LocalExplode(&exp);
  62. exp.millisecond = 0;
  63. Time rounded_time;
  64. ignore_result(Time::FromLocalExploded(exp, &rounded_time));
  65. return rounded_time;
  66. }
  67. ACTION_P(ActionSetUpdateEngineStatusLastCheckedTime, time) {
  68. arg0->last_checked_time = time;
  69. };
  70. ACTION_P(ActionSetUpdateEngineStatusProgress, progress) {
  71. arg0->progress = progress;
  72. };
  73. ACTION_P(ActionSetUpdateEngineStatusStatus, status) {
  74. arg0->status = status;
  75. }
  76. ACTION_P(ActionSetUpdateEngineStatusNewVersion, new_version) {
  77. arg0->new_version = new_version;
  78. }
  79. ACTION_P(ActionSetUpdateEngineStatusNewSizeBytes, new_size_bytes) {
  80. arg0->new_size_bytes = new_size_bytes;
  81. }
  82. } // namespace
  83. namespace chromeos_update_manager {
  84. class UmRealUpdaterProviderTest : public ::testing::Test {
  85. protected:
  86. void SetUp() override {
  87. fake_clock_ = fake_sys_state_.fake_clock();
  88. fake_sys_state_.set_prefs(&fake_prefs_);
  89. provider_.reset(new RealUpdaterProvider(&fake_sys_state_));
  90. ASSERT_NE(nullptr, provider_.get());
  91. // Check that provider initializes correctly.
  92. ASSERT_TRUE(provider_->Init());
  93. }
  94. // Sets up mock expectations for testing the update completed time reporting.
  95. // |valid| determines whether the returned time is valid. Returns the expected
  96. // update completed time value.
  97. Time SetupUpdateCompletedTime(bool valid) {
  98. const TimeDelta kDurationSinceUpdate = TimeDelta::FromMinutes(7);
  99. const Time kUpdateBootTime = Time() + kDurationSinceUpdate * 2;
  100. const Time kCurrBootTime = (valid ? kUpdateBootTime + kDurationSinceUpdate
  101. : kUpdateBootTime - kDurationSinceUpdate);
  102. const Time kCurrWallclockTime = FixedTime();
  103. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
  104. GetBootTimeAtUpdate(_))
  105. .WillOnce(DoAll(SetArgPointee<0>(kUpdateBootTime), Return(true)));
  106. fake_clock_->SetBootTime(kCurrBootTime);
  107. fake_clock_->SetWallclockTime(kCurrWallclockTime);
  108. return kCurrWallclockTime - kDurationSinceUpdate;
  109. }
  110. FakeSystemState fake_sys_state_;
  111. FakeClock* fake_clock_; // Short for fake_sys_state_.fake_clock()
  112. FakePrefs fake_prefs_;
  113. unique_ptr<RealUpdaterProvider> provider_;
  114. };
  115. TEST_F(UmRealUpdaterProviderTest, UpdaterStartedTimeIsWallclockTime) {
  116. fake_clock_->SetWallclockTime(Time::FromDoubleT(123.456));
  117. fake_clock_->SetMonotonicTime(Time::FromDoubleT(456.123));
  118. // Run SetUp again to re-setup the provider under test to use these values.
  119. SetUp();
  120. UmTestUtils::ExpectVariableHasValue(Time::FromDoubleT(123.456),
  121. provider_->var_updater_started_time());
  122. }
  123. TEST_F(UmRealUpdaterProviderTest, GetLastCheckedTimeOkay) {
  124. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  125. .WillOnce(DoAll(
  126. ActionSetUpdateEngineStatusLastCheckedTime(FixedTime().ToTimeT()),
  127. Return(true)));
  128. UmTestUtils::ExpectVariableHasValue(RoundedToSecond(FixedTime()),
  129. provider_->var_last_checked_time());
  130. }
  131. TEST_F(UmRealUpdaterProviderTest, GetLastCheckedTimeFailNoValue) {
  132. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  133. .WillOnce(Return(false));
  134. UmTestUtils::ExpectVariableNotSet(provider_->var_last_checked_time());
  135. }
  136. TEST_F(UmRealUpdaterProviderTest, GetProgressOkayMin) {
  137. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  138. .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(0.0), Return(true)));
  139. UmTestUtils::ExpectVariableHasValue(0.0, provider_->var_progress());
  140. }
  141. TEST_F(UmRealUpdaterProviderTest, GetProgressOkayMid) {
  142. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  143. .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(0.3), Return(true)));
  144. UmTestUtils::ExpectVariableHasValue(0.3, provider_->var_progress());
  145. }
  146. TEST_F(UmRealUpdaterProviderTest, GetProgressOkayMax) {
  147. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  148. .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(1.0), Return(true)));
  149. UmTestUtils::ExpectVariableHasValue(1.0, provider_->var_progress());
  150. }
  151. TEST_F(UmRealUpdaterProviderTest, GetProgressFailNoValue) {
  152. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  153. .WillOnce(Return(false));
  154. UmTestUtils::ExpectVariableNotSet(provider_->var_progress());
  155. }
  156. TEST_F(UmRealUpdaterProviderTest, GetProgressFailTooSmall) {
  157. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  158. .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(-2.0), Return(true)));
  159. UmTestUtils::ExpectVariableNotSet(provider_->var_progress());
  160. }
  161. TEST_F(UmRealUpdaterProviderTest, GetProgressFailTooBig) {
  162. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  163. .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(2.0), Return(true)));
  164. UmTestUtils::ExpectVariableNotSet(provider_->var_progress());
  165. }
  166. TEST_F(UmRealUpdaterProviderTest, GetStageOkayIdle) {
  167. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  168. .WillOnce(DoAll(
  169. ActionSetUpdateEngineStatusStatus(update_engine::UpdateStatus::IDLE),
  170. Return(true)));
  171. UmTestUtils::ExpectVariableHasValue(Stage::kIdle, provider_->var_stage());
  172. }
  173. TEST_F(UmRealUpdaterProviderTest, GetStageOkayCheckingForUpdate) {
  174. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  175. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  176. update_engine::UpdateStatus::CHECKING_FOR_UPDATE),
  177. Return(true)));
  178. UmTestUtils::ExpectVariableHasValue(Stage::kCheckingForUpdate,
  179. provider_->var_stage());
  180. }
  181. TEST_F(UmRealUpdaterProviderTest, GetStageOkayUpdateAvailable) {
  182. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  183. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  184. update_engine::UpdateStatus::UPDATE_AVAILABLE),
  185. Return(true)));
  186. UmTestUtils::ExpectVariableHasValue(Stage::kUpdateAvailable,
  187. provider_->var_stage());
  188. }
  189. TEST_F(UmRealUpdaterProviderTest, GetStageOkayDownloading) {
  190. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  191. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  192. update_engine::UpdateStatus::DOWNLOADING),
  193. Return(true)));
  194. UmTestUtils::ExpectVariableHasValue(Stage::kDownloading,
  195. provider_->var_stage());
  196. }
  197. TEST_F(UmRealUpdaterProviderTest, GetStageOkayVerifying) {
  198. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  199. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  200. update_engine::UpdateStatus::VERIFYING),
  201. Return(true)));
  202. UmTestUtils::ExpectVariableHasValue(Stage::kVerifying,
  203. provider_->var_stage());
  204. }
  205. TEST_F(UmRealUpdaterProviderTest, GetStageOkayFinalizing) {
  206. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  207. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  208. update_engine::UpdateStatus::FINALIZING),
  209. Return(true)));
  210. UmTestUtils::ExpectVariableHasValue(Stage::kFinalizing,
  211. provider_->var_stage());
  212. }
  213. TEST_F(UmRealUpdaterProviderTest, GetStageOkayUpdatedNeedReboot) {
  214. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  215. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  216. update_engine::UpdateStatus::UPDATED_NEED_REBOOT),
  217. Return(true)));
  218. UmTestUtils::ExpectVariableHasValue(Stage::kUpdatedNeedReboot,
  219. provider_->var_stage());
  220. }
  221. TEST_F(UmRealUpdaterProviderTest, GetStageOkayReportingErrorEvent) {
  222. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  223. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  224. update_engine::UpdateStatus::REPORTING_ERROR_EVENT),
  225. Return(true)));
  226. UmTestUtils::ExpectVariableHasValue(Stage::kReportingErrorEvent,
  227. provider_->var_stage());
  228. }
  229. TEST_F(UmRealUpdaterProviderTest, GetStageOkayAttemptingRollback) {
  230. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  231. .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
  232. update_engine::UpdateStatus::ATTEMPTING_ROLLBACK),
  233. Return(true)));
  234. UmTestUtils::ExpectVariableHasValue(Stage::kAttemptingRollback,
  235. provider_->var_stage());
  236. }
  237. TEST_F(UmRealUpdaterProviderTest, GetStageFailNoValue) {
  238. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  239. .WillOnce(Return(false));
  240. UmTestUtils::ExpectVariableNotSet(provider_->var_stage());
  241. }
  242. TEST_F(UmRealUpdaterProviderTest, GetNewVersionOkay) {
  243. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  244. .WillOnce(
  245. DoAll(ActionSetUpdateEngineStatusNewVersion("1.2.0"), Return(true)));
  246. UmTestUtils::ExpectVariableHasValue(string("1.2.0"),
  247. provider_->var_new_version());
  248. }
  249. TEST_F(UmRealUpdaterProviderTest, GetNewVersionFailNoValue) {
  250. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  251. .WillOnce(Return(false));
  252. UmTestUtils::ExpectVariableNotSet(provider_->var_new_version());
  253. }
  254. TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeOkayZero) {
  255. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  256. .WillOnce(DoAll(
  257. ActionSetUpdateEngineStatusNewSizeBytes(static_cast<uint64_t>(0)),
  258. Return(true)));
  259. UmTestUtils::ExpectVariableHasValue(static_cast<uint64_t>(0),
  260. provider_->var_payload_size());
  261. }
  262. TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeOkayArbitrary) {
  263. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  264. .WillOnce(DoAll(ActionSetUpdateEngineStatusNewSizeBytes(
  265. static_cast<uint64_t>(567890)),
  266. Return(true)));
  267. UmTestUtils::ExpectVariableHasValue(static_cast<uint64_t>(567890),
  268. provider_->var_payload_size());
  269. }
  270. TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeOkayTwoGigabytes) {
  271. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  272. .WillOnce(DoAll(ActionSetUpdateEngineStatusNewSizeBytes(
  273. static_cast<uint64_t>(1) << 31),
  274. Return(true)));
  275. UmTestUtils::ExpectVariableHasValue(static_cast<uint64_t>(1) << 31,
  276. provider_->var_payload_size());
  277. }
  278. TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeFailNoValue) {
  279. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
  280. .WillOnce(Return(false));
  281. UmTestUtils::ExpectVariableNotSet(provider_->var_payload_size());
  282. }
  283. TEST_F(UmRealUpdaterProviderTest, GetCurrChannelOkay) {
  284. const string kChannelName("foo-channel");
  285. OmahaRequestParams request_params(&fake_sys_state_);
  286. request_params.Init("", "", false);
  287. request_params.set_current_channel(kChannelName);
  288. fake_sys_state_.set_request_params(&request_params);
  289. UmTestUtils::ExpectVariableHasValue(kChannelName,
  290. provider_->var_curr_channel());
  291. }
  292. TEST_F(UmRealUpdaterProviderTest, GetCurrChannelFailEmpty) {
  293. OmahaRequestParams request_params(&fake_sys_state_);
  294. request_params.Init("", "", false);
  295. request_params.set_current_channel("");
  296. fake_sys_state_.set_request_params(&request_params);
  297. UmTestUtils::ExpectVariableNotSet(provider_->var_curr_channel());
  298. }
  299. TEST_F(UmRealUpdaterProviderTest, GetNewChannelOkay) {
  300. const string kChannelName("foo-channel");
  301. OmahaRequestParams request_params(&fake_sys_state_);
  302. request_params.Init("", "", false);
  303. request_params.set_target_channel(kChannelName);
  304. fake_sys_state_.set_request_params(&request_params);
  305. UmTestUtils::ExpectVariableHasValue(kChannelName,
  306. provider_->var_new_channel());
  307. }
  308. TEST_F(UmRealUpdaterProviderTest, GetNewChannelFailEmpty) {
  309. OmahaRequestParams request_params(&fake_sys_state_);
  310. request_params.Init("", "", false);
  311. request_params.set_target_channel("");
  312. fake_sys_state_.set_request_params(&request_params);
  313. UmTestUtils::ExpectVariableNotSet(provider_->var_new_channel());
  314. }
  315. TEST_F(UmRealUpdaterProviderTest, GetP2PEnabledOkayPrefDoesntExist) {
  316. UmTestUtils::ExpectVariableHasValue(false, provider_->var_p2p_enabled());
  317. }
  318. TEST_F(UmRealUpdaterProviderTest, GetP2PEnabledOkayPrefReadsFalse) {
  319. fake_prefs_.SetBoolean(chromeos_update_engine::kPrefsP2PEnabled, false);
  320. UmTestUtils::ExpectVariableHasValue(false, provider_->var_p2p_enabled());
  321. }
  322. TEST_F(UmRealUpdaterProviderTest, GetP2PEnabledReadWhenInitialized) {
  323. fake_prefs_.SetBoolean(chromeos_update_engine::kPrefsP2PEnabled, true);
  324. SetUp();
  325. UmTestUtils::ExpectVariableHasValue(true, provider_->var_p2p_enabled());
  326. }
  327. TEST_F(UmRealUpdaterProviderTest, GetP2PEnabledUpdated) {
  328. fake_prefs_.SetBoolean(chromeos_update_engine::kPrefsP2PEnabled, false);
  329. UmTestUtils::ExpectVariableHasValue(false, provider_->var_p2p_enabled());
  330. fake_prefs_.SetBoolean(chromeos_update_engine::kPrefsP2PEnabled, true);
  331. UmTestUtils::ExpectVariableHasValue(true, provider_->var_p2p_enabled());
  332. fake_prefs_.Delete(chromeos_update_engine::kPrefsP2PEnabled);
  333. UmTestUtils::ExpectVariableHasValue(false, provider_->var_p2p_enabled());
  334. }
  335. TEST_F(UmRealUpdaterProviderTest, GetCellularEnabledOkayPrefDoesntExist) {
  336. UmTestUtils::ExpectVariableHasValue(false, provider_->var_cellular_enabled());
  337. }
  338. TEST_F(UmRealUpdaterProviderTest, GetCellularEnabledOkayPrefReadsTrue) {
  339. fake_prefs_.SetBoolean(
  340. chromeos_update_engine::kPrefsUpdateOverCellularPermission, true);
  341. UmTestUtils::ExpectVariableHasValue(true, provider_->var_cellular_enabled());
  342. }
  343. TEST_F(UmRealUpdaterProviderTest, GetUpdateCompletedTimeOkay) {
  344. Time expected = SetupUpdateCompletedTime(true);
  345. UmTestUtils::ExpectVariableHasValue(expected,
  346. provider_->var_update_completed_time());
  347. }
  348. TEST_F(UmRealUpdaterProviderTest, GetUpdateCompletedTimeFailNoValue) {
  349. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetBootTimeAtUpdate(_))
  350. .WillOnce(Return(false));
  351. UmTestUtils::ExpectVariableNotSet(provider_->var_update_completed_time());
  352. }
  353. TEST_F(UmRealUpdaterProviderTest, GetUpdateCompletedTimeFailInvalidValue) {
  354. SetupUpdateCompletedTime(false);
  355. UmTestUtils::ExpectVariableNotSet(provider_->var_update_completed_time());
  356. }
  357. TEST_F(UmRealUpdaterProviderTest, GetConsecutiveFailedUpdateChecks) {
  358. const unsigned int kNumFailedChecks = 3;
  359. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
  360. consecutive_failed_update_checks())
  361. .WillRepeatedly(Return(kNumFailedChecks));
  362. UmTestUtils::ExpectVariableHasValue(
  363. kNumFailedChecks, provider_->var_consecutive_failed_update_checks());
  364. }
  365. TEST_F(UmRealUpdaterProviderTest, GetServerDictatedPollInterval) {
  366. const unsigned int kPollInterval = 2 * 60 * 60; // Two hours.
  367. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
  368. server_dictated_poll_interval())
  369. .WillRepeatedly(Return(kPollInterval));
  370. UmTestUtils::ExpectVariableHasValue(
  371. kPollInterval, provider_->var_server_dictated_poll_interval());
  372. }
  373. TEST_F(UmRealUpdaterProviderTest, GetUpdateRestrictions) {
  374. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
  375. GetCurrentUpdateAttemptFlags())
  376. .WillRepeatedly(Return(UpdateAttemptFlags::kFlagRestrictDownload |
  377. UpdateAttemptFlags::kFlagNonInteractive));
  378. UmTestUtils::ExpectVariableHasValue(UpdateRestrictions::kRestrictDownloading,
  379. provider_->var_update_restrictions());
  380. }
  381. TEST_F(UmRealUpdaterProviderTest, GetUpdateRestrictionsNone) {
  382. EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
  383. GetCurrentUpdateAttemptFlags())
  384. .WillRepeatedly(Return(UpdateAttemptFlags::kNone));
  385. UmTestUtils::ExpectVariableHasValue(UpdateRestrictions::kNone,
  386. provider_->var_update_restrictions());
  387. }
  388. } // namespace chromeos_update_manager