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