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