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