| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 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 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/aosp/update_attempter_android.h" | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include <memory> | 
|  | 20 | #include <string> | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 21 | #include <utility> | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <android-base/properties.h> | 
|  | 24 | #include <base/time/time.h> | 
|  | 25 | #include <gtest/gtest.h> | 
|  | 26 |  | 
| Kelvin Zhang | 8660347 | 2021-05-11 12:16:27 -0400 | [diff] [blame] | 27 | #include "common/constants.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 28 | #include "update_engine/aosp/daemon_state_android.h" | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 29 | #include "update_engine/common/fake_boot_control.h" | 
|  | 30 | #include "update_engine/common/fake_clock.h" | 
|  | 31 | #include "update_engine/common/fake_hardware.h" | 
|  | 32 | #include "update_engine/common/fake_prefs.h" | 
|  | 33 | #include "update_engine/common/mock_action_processor.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 34 | #include "update_engine/common/mock_metrics_reporter.h" | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 35 | #include "update_engine/common/test_utils.h" | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 36 | #include "update_engine/common/utils.h" | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | using base::Time; | 
|  | 39 | using base::TimeDelta; | 
|  | 40 | using testing::_; | 
|  | 41 | using update_engine::UpdateStatus; | 
|  | 42 |  | 
|  | 43 | namespace chromeos_update_engine { | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 44 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 45 | class UpdateAttempterAndroidTest : public ::testing::Test { | 
|  | 46 | protected: | 
|  | 47 | UpdateAttempterAndroidTest() = default; | 
|  | 48 |  | 
|  | 49 | void SetUp() override { | 
|  | 50 | clock_ = new FakeClock(); | 
|  | 51 | metrics_reporter_ = new testing::NiceMock<MockMetricsReporter>(); | 
|  | 52 | update_attempter_android_.metrics_reporter_.reset(metrics_reporter_); | 
|  | 53 | update_attempter_android_.clock_.reset(clock_); | 
|  | 54 | update_attempter_android_.processor_.reset( | 
|  | 55 | new testing::NiceMock<MockActionProcessor>()); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | void SetUpdateStatus(update_engine::UpdateStatus status) { | 
|  | 59 | update_attempter_android_.status_ = status; | 
|  | 60 | } | 
|  | 61 |  | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 62 | void AddPayload(InstallPlan::Payload&& payload) { | 
|  | 63 | update_attempter_android_.install_plan_.payloads.push_back( | 
|  | 64 | std::move(payload)); | 
|  | 65 | } | 
|  | 66 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 67 | DaemonStateAndroid daemon_state_; | 
|  | 68 | FakePrefs prefs_; | 
|  | 69 | FakeBootControl boot_control_; | 
|  | 70 | FakeHardware hardware_; | 
|  | 71 |  | 
| Yifan Hong | 93c497d | 2021-02-08 14:25:52 -0800 | [diff] [blame] | 72 | UpdateAttempterAndroid update_attempter_android_{ | 
| Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 73 | &daemon_state_, &prefs_, &boot_control_, &hardware_, nullptr}; | 
| Yifan Hong | 93c497d | 2021-02-08 14:25:52 -0800 | [diff] [blame] | 74 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 75 | FakeClock* clock_; | 
|  | 76 | testing::NiceMock<MockMetricsReporter>* metrics_reporter_; | 
|  | 77 | }; | 
|  | 78 |  | 
|  | 79 | TEST_F(UpdateAttempterAndroidTest, UpdatePrefsSameBuildVersionOnInit) { | 
|  | 80 | std::string build_version = | 
|  | 81 | android::base::GetProperty("ro.build.version.incremental", ""); | 
|  | 82 | prefs_.SetString(kPrefsPreviousVersion, build_version); | 
|  | 83 | prefs_.SetString(kPrefsBootId, "oldboot"); | 
|  | 84 | prefs_.SetInt64(kPrefsNumReboots, 1); | 
| Kelvin Zhang | 8660347 | 2021-05-11 12:16:27 -0400 | [diff] [blame] | 85 | prefs_.SetInt64(kPrefsPreviousSlot, 1); | 
|  | 86 | boot_control_.SetCurrentSlot(1); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(_)).Times(0); | 
|  | 89 | update_attempter_android_.Init(); | 
|  | 90 |  | 
|  | 91 | // Check that the boot_id and reboot_count are updated. | 
|  | 92 | std::string boot_id; | 
|  | 93 | utils::GetBootId(&boot_id); | 
| Kelvin Zhang | 8660347 | 2021-05-11 12:16:27 -0400 | [diff] [blame] | 94 | ASSERT_TRUE(prefs_.Exists(kPrefsBootId)); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 95 | std::string prefs_boot_id; | 
| Kelvin Zhang | 8660347 | 2021-05-11 12:16:27 -0400 | [diff] [blame] | 96 | ASSERT_TRUE(prefs_.GetString(kPrefsBootId, &prefs_boot_id)); | 
|  | 97 | ASSERT_EQ(boot_id, prefs_boot_id); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 98 |  | 
| Kelvin Zhang | 8660347 | 2021-05-11 12:16:27 -0400 | [diff] [blame] | 99 | ASSERT_TRUE(prefs_.Exists(kPrefsNumReboots)); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 100 | int64_t reboot_count; | 
| Kelvin Zhang | 8660347 | 2021-05-11 12:16:27 -0400 | [diff] [blame] | 101 | ASSERT_TRUE(prefs_.GetInt64(kPrefsNumReboots, &reboot_count)); | 
|  | 102 | ASSERT_EQ(2, reboot_count); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | TEST_F(UpdateAttempterAndroidTest, UpdatePrefsBuildVersionChangeOnInit) { | 
|  | 106 | prefs_.SetString(kPrefsPreviousVersion, "00001");  // Set the fake version | 
|  | 107 | prefs_.SetInt64(kPrefsPayloadAttemptNumber, 1); | 
|  | 108 | prefs_.SetInt64(kPrefsSystemUpdatedMarker, 23456); | 
| Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 109 | prefs_.SetInt64(kPrefsPreviousSlot, 1); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | EXPECT_CALL(*metrics_reporter_, | 
|  | 112 | ReportAbnormallyTerminatedUpdateAttemptMetrics()) | 
|  | 113 | .Times(1); | 
|  | 114 |  | 
|  | 115 | Time now = Time::FromInternalValue(34456); | 
|  | 116 | clock_->SetMonotonicTime(now); | 
|  | 117 | TimeDelta duration = now - Time::FromInternalValue(23456); | 
|  | 118 | EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(duration.InMinutes())) | 
|  | 119 | .Times(1); | 
|  | 120 |  | 
|  | 121 | update_attempter_android_.Init(); | 
|  | 122 | // Check that we reset the metric prefs. | 
|  | 123 | EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots)); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart)); | 
|  | 125 | EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker)); | 
| xunchang | 9cf5262 | 2019-01-25 11:04:58 -0800 | [diff] [blame] | 126 | // PayloadAttemptNumber should persist across reboots. | 
|  | 127 | EXPECT_TRUE(prefs_.Exists(kPrefsPayloadAttemptNumber)); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
|  | 130 | TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) { | 
|  | 131 | prefs_.SetInt64(kPrefsNumReboots, 3); | 
|  | 132 | prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2); | 
|  | 133 | prefs_.SetString(kPrefsPreviousVersion, "56789"); | 
| Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 134 | prefs_.SetInt64(kPrefsUpdateBootTimestampStart, 10000); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 135 | prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345); | 
|  | 136 |  | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 137 | Time boot_time = Time::FromInternalValue(22345); | 
|  | 138 | Time up_time = Time::FromInternalValue(21345); | 
|  | 139 | clock_->SetBootTime(boot_time); | 
|  | 140 | clock_->SetMonotonicTime(up_time); | 
| Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 141 | TimeDelta duration = boot_time - Time::FromInternalValue(10000); | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 142 | TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 143 | EXPECT_CALL( | 
|  | 144 | *metrics_reporter_, | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 145 | ReportUpdateAttemptMetrics(2, | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 146 | _, | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 147 | duration, | 
| Tianjie Xu | 52c678c | 2017-10-18 15:52:27 -0700 | [diff] [blame] | 148 | duration_uptime, | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 149 | _, | 
|  | 150 | metrics::AttemptResult::kUpdateSucceeded, | 
|  | 151 | ErrorCode::kSuccess)) | 
|  | 152 | .Times(1); | 
|  | 153 | EXPECT_CALL(*metrics_reporter_, | 
| Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 154 | ReportSuccessfulUpdateMetrics( | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 155 | 2, 0, _, 50, _, _, duration, duration_uptime, 3, _)) | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 156 | .Times(1); | 
|  | 157 |  | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 158 | // Adds a payload of 50 bytes to the InstallPlan. | 
|  | 159 | InstallPlan::Payload payload; | 
|  | 160 | payload.size = 50; | 
|  | 161 | AddPayload(std::move(payload)); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 162 | SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE); | 
|  | 163 | update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess); | 
|  | 164 |  | 
|  | 165 | EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots)); | 
|  | 166 | EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber)); | 
|  | 167 | EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart)); | 
|  | 168 | EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker)); | 
|  | 169 | } | 
|  | 170 |  | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 171 | TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) { | 
|  | 172 | // Check both prefs are updated correctly. | 
|  | 173 | update_attempter_android_.BytesReceived(20, 50, 200); | 
|  | 174 | EXPECT_EQ( | 
|  | 175 | 20, | 
|  | 176 | metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_)); | 
|  | 177 | EXPECT_EQ( | 
|  | 178 | 20, | 
|  | 179 | metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_)); | 
|  | 180 |  | 
|  | 181 | EXPECT_CALL(*metrics_reporter_, | 
|  | 182 | ReportUpdateAttemptDownloadMetrics(50, _, _, _, _)) | 
|  | 183 | .Times(1); | 
|  | 184 | EXPECT_CALL(*metrics_reporter_, | 
|  | 185 | ReportUpdateAttemptDownloadMetrics(40, _, _, _, _)) | 
|  | 186 | .Times(1); | 
|  | 187 |  | 
|  | 188 | int64_t total_bytes[kNumDownloadSources] = {}; | 
|  | 189 | total_bytes[kDownloadSourceHttpsServer] = 90; | 
|  | 190 | EXPECT_CALL(*metrics_reporter_, | 
|  | 191 | ReportSuccessfulUpdateMetrics( | 
|  | 192 | _, | 
|  | 193 | _, | 
|  | 194 | _, | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 195 | 50, | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 196 | test_utils::DownloadSourceMatcher(total_bytes), | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 197 | 80, | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 198 | _, | 
|  | 199 | _, | 
| Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 200 | _, | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 201 | _)) | 
|  | 202 | .Times(1); | 
|  | 203 |  | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 204 | // Adds a payload of 50 bytes to the InstallPlan. | 
|  | 205 | InstallPlan::Payload payload; | 
|  | 206 | payload.size = 50; | 
|  | 207 | AddPayload(std::move(payload)); | 
|  | 208 |  | 
| Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 209 | // The first update fails after receiving 50 bytes in total. | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 210 | update_attempter_android_.BytesReceived(30, 50, 200); | 
|  | 211 | update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError); | 
|  | 212 | EXPECT_EQ( | 
|  | 213 | 0, | 
|  | 214 | metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_)); | 
|  | 215 | EXPECT_EQ( | 
|  | 216 | 50, | 
|  | 217 | metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_)); | 
|  | 218 |  | 
|  | 219 | // The second update succeeds after receiving 40 bytes, which leads to a | 
| Tianjie Xu | 21030c1 | 2019-08-14 13:00:23 -0700 | [diff] [blame] | 220 | // overhead of (90 - 50) / 50 = 80%. | 
| Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 221 | update_attempter_android_.BytesReceived(40, 40, 50); | 
|  | 222 | update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess); | 
|  | 223 | // Both prefs should be cleared. | 
|  | 224 | EXPECT_EQ( | 
|  | 225 | 0, | 
|  | 226 | metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_)); | 
|  | 227 | EXPECT_EQ( | 
|  | 228 | 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_)); | 
|  | 229 | } | 
|  | 230 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 231 | }  // namespace chromeos_update_engine |