update_attempter_android_unittest.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // Copyright (C) 2017 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_attempter_android.h"
  17. #include <memory>
  18. #include <string>
  19. #include <android-base/properties.h>
  20. #include <base/time/time.h>
  21. #include <gtest/gtest.h>
  22. #include "update_engine/common/fake_boot_control.h"
  23. #include "update_engine/common/fake_clock.h"
  24. #include "update_engine/common/fake_hardware.h"
  25. #include "update_engine/common/fake_prefs.h"
  26. #include "update_engine/common/mock_action_processor.h"
  27. #include "update_engine/common/test_utils.h"
  28. #include "update_engine/common/utils.h"
  29. #include "update_engine/daemon_state_android.h"
  30. #include "update_engine/mock_metrics_reporter.h"
  31. using base::Time;
  32. using base::TimeDelta;
  33. using testing::_;
  34. using update_engine::UpdateStatus;
  35. namespace chromeos_update_engine {
  36. class UpdateAttempterAndroidTest : public ::testing::Test {
  37. protected:
  38. UpdateAttempterAndroidTest() = default;
  39. void SetUp() override {
  40. clock_ = new FakeClock();
  41. metrics_reporter_ = new testing::NiceMock<MockMetricsReporter>();
  42. update_attempter_android_.metrics_reporter_.reset(metrics_reporter_);
  43. update_attempter_android_.clock_.reset(clock_);
  44. update_attempter_android_.processor_.reset(
  45. new testing::NiceMock<MockActionProcessor>());
  46. }
  47. void SetUpdateStatus(update_engine::UpdateStatus status) {
  48. update_attempter_android_.status_ = status;
  49. }
  50. UpdateAttempterAndroid update_attempter_android_{
  51. &daemon_state_, &prefs_, &boot_control_, &hardware_};
  52. DaemonStateAndroid daemon_state_;
  53. FakePrefs prefs_;
  54. FakeBootControl boot_control_;
  55. FakeHardware hardware_;
  56. FakeClock* clock_;
  57. testing::NiceMock<MockMetricsReporter>* metrics_reporter_;
  58. };
  59. TEST_F(UpdateAttempterAndroidTest, UpdatePrefsSameBuildVersionOnInit) {
  60. std::string build_version =
  61. android::base::GetProperty("ro.build.version.incremental", "");
  62. prefs_.SetString(kPrefsPreviousVersion, build_version);
  63. prefs_.SetString(kPrefsBootId, "oldboot");
  64. prefs_.SetInt64(kPrefsNumReboots, 1);
  65. EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(_)).Times(0);
  66. update_attempter_android_.Init();
  67. // Check that the boot_id and reboot_count are updated.
  68. std::string boot_id;
  69. utils::GetBootId(&boot_id);
  70. EXPECT_TRUE(prefs_.Exists(kPrefsBootId));
  71. std::string prefs_boot_id;
  72. EXPECT_TRUE(prefs_.GetString(kPrefsBootId, &prefs_boot_id));
  73. EXPECT_EQ(boot_id, prefs_boot_id);
  74. EXPECT_TRUE(prefs_.Exists(kPrefsNumReboots));
  75. int64_t reboot_count;
  76. EXPECT_TRUE(prefs_.GetInt64(kPrefsNumReboots, &reboot_count));
  77. EXPECT_EQ(2, reboot_count);
  78. }
  79. TEST_F(UpdateAttempterAndroidTest, UpdatePrefsBuildVersionChangeOnInit) {
  80. prefs_.SetString(kPrefsPreviousVersion, "00001"); // Set the fake version
  81. prefs_.SetInt64(kPrefsPayloadAttemptNumber, 1);
  82. prefs_.SetInt64(kPrefsSystemUpdatedMarker, 23456);
  83. EXPECT_CALL(*metrics_reporter_,
  84. ReportAbnormallyTerminatedUpdateAttemptMetrics())
  85. .Times(1);
  86. Time now = Time::FromInternalValue(34456);
  87. clock_->SetMonotonicTime(now);
  88. TimeDelta duration = now - Time::FromInternalValue(23456);
  89. EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(duration.InMinutes()))
  90. .Times(1);
  91. update_attempter_android_.Init();
  92. // Check that we reset the metric prefs.
  93. EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
  94. EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
  95. EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker));
  96. // PayloadAttemptNumber should persist across reboots.
  97. EXPECT_TRUE(prefs_.Exists(kPrefsPayloadAttemptNumber));
  98. }
  99. TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) {
  100. prefs_.SetInt64(kPrefsNumReboots, 3);
  101. prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2);
  102. prefs_.SetString(kPrefsPreviousVersion, "56789");
  103. prefs_.SetInt64(kPrefsUpdateBootTimestampStart, 10000);
  104. prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
  105. Time boot_time = Time::FromInternalValue(22345);
  106. Time up_time = Time::FromInternalValue(21345);
  107. clock_->SetBootTime(boot_time);
  108. clock_->SetMonotonicTime(up_time);
  109. TimeDelta duration = boot_time - Time::FromInternalValue(10000);
  110. TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345);
  111. EXPECT_CALL(
  112. *metrics_reporter_,
  113. ReportUpdateAttemptMetrics(_,
  114. 2,
  115. _,
  116. duration,
  117. duration_uptime,
  118. _,
  119. metrics::AttemptResult::kUpdateSucceeded,
  120. ErrorCode::kSuccess))
  121. .Times(1);
  122. EXPECT_CALL(*metrics_reporter_,
  123. ReportSuccessfulUpdateMetrics(
  124. 2, 0, _, _, _, _, duration, duration_uptime, 3, _))
  125. .Times(1);
  126. SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
  127. update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
  128. EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
  129. EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
  130. EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
  131. EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
  132. }
  133. TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) {
  134. // Check both prefs are updated correctly.
  135. update_attempter_android_.BytesReceived(20, 50, 200);
  136. EXPECT_EQ(
  137. 20,
  138. metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
  139. EXPECT_EQ(
  140. 20,
  141. metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
  142. EXPECT_CALL(*metrics_reporter_,
  143. ReportUpdateAttemptDownloadMetrics(50, _, _, _, _))
  144. .Times(1);
  145. EXPECT_CALL(*metrics_reporter_,
  146. ReportUpdateAttemptDownloadMetrics(40, _, _, _, _))
  147. .Times(1);
  148. int64_t total_bytes[kNumDownloadSources] = {};
  149. total_bytes[kDownloadSourceHttpsServer] = 90;
  150. EXPECT_CALL(*metrics_reporter_,
  151. ReportSuccessfulUpdateMetrics(
  152. _,
  153. _,
  154. _,
  155. _,
  156. test_utils::DownloadSourceMatcher(total_bytes),
  157. 125,
  158. _,
  159. _,
  160. _,
  161. _))
  162. .Times(1);
  163. // The first update fails after receiving 50 bytes in total.
  164. update_attempter_android_.BytesReceived(30, 50, 200);
  165. update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError);
  166. EXPECT_EQ(
  167. 0,
  168. metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
  169. EXPECT_EQ(
  170. 50,
  171. metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
  172. // The second update succeeds after receiving 40 bytes, which leads to a
  173. // overhead of 50 / 40 = 125%.
  174. update_attempter_android_.BytesReceived(40, 40, 50);
  175. update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
  176. // Both prefs should be cleared.
  177. EXPECT_EQ(
  178. 0,
  179. metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
  180. EXPECT_EQ(
  181. 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
  182. }
  183. } // namespace chromeos_update_engine