blob: f73df168ed0ab8da2f3d71f6eab9a51573f15791 [file] [log] [blame]
Tianjie Xu90aaa102017-10-10 17:39:03 -07001//
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 Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/aosp/update_attempter_android.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070018
19#include <memory>
20#include <string>
Tianjie Xu21030c12019-08-14 13:00:23 -070021#include <utility>
Tianjie Xu90aaa102017-10-10 17:39:03 -070022
23#include <android-base/properties.h>
24#include <base/time/time.h>
25#include <gtest/gtest.h>
26
Kelvin Zhang86603472021-05-11 12:16:27 -040027#include "common/constants.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/aosp/daemon_state_android.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070029#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 Hassaniec7bc112020-10-29 16:47:58 -070034#include "update_engine/common/mock_metrics_reporter.h"
Tianjie Xud4777a12017-10-24 14:54:18 -070035#include "update_engine/common/test_utils.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070036#include "update_engine/common/utils.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070037
38using base::Time;
39using base::TimeDelta;
40using testing::_;
41using update_engine::UpdateStatus;
42
43namespace chromeos_update_engine {
Tianjie Xud4777a12017-10-24 14:54:18 -070044
Tianjie Xu90aaa102017-10-10 17:39:03 -070045class 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 Xu21030c12019-08-14 13:00:23 -070062 void AddPayload(InstallPlan::Payload&& payload) {
63 update_attempter_android_.install_plan_.payloads.push_back(
64 std::move(payload));
65 }
66
Tianjie Xu90aaa102017-10-10 17:39:03 -070067 DaemonStateAndroid daemon_state_;
68 FakePrefs prefs_;
69 FakeBootControl boot_control_;
70 FakeHardware hardware_;
71
Yifan Hong93c497d2021-02-08 14:25:52 -080072 UpdateAttempterAndroid update_attempter_android_{
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000073 &daemon_state_, &prefs_, &boot_control_, &hardware_, nullptr};
Yifan Hong93c497d2021-02-08 14:25:52 -080074
Tianjie Xu90aaa102017-10-10 17:39:03 -070075 FakeClock* clock_;
76 testing::NiceMock<MockMetricsReporter>* metrics_reporter_;
77};
78
79TEST_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 Zhang86603472021-05-11 12:16:27 -040085 prefs_.SetInt64(kPrefsPreviousSlot, 1);
86 boot_control_.SetCurrentSlot(1);
Tianjie Xu90aaa102017-10-10 17:39:03 -070087
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 Zhang86603472021-05-11 12:16:27 -040094 ASSERT_TRUE(prefs_.Exists(kPrefsBootId));
Tianjie Xu90aaa102017-10-10 17:39:03 -070095 std::string prefs_boot_id;
Kelvin Zhang86603472021-05-11 12:16:27 -040096 ASSERT_TRUE(prefs_.GetString(kPrefsBootId, &prefs_boot_id));
97 ASSERT_EQ(boot_id, prefs_boot_id);
Tianjie Xu90aaa102017-10-10 17:39:03 -070098
Kelvin Zhang86603472021-05-11 12:16:27 -040099 ASSERT_TRUE(prefs_.Exists(kPrefsNumReboots));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700100 int64_t reboot_count;
Kelvin Zhang86603472021-05-11 12:16:27 -0400101 ASSERT_TRUE(prefs_.GetInt64(kPrefsNumReboots, &reboot_count));
102 ASSERT_EQ(2, reboot_count);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700103}
104
105TEST_F(UpdateAttempterAndroidTest, UpdatePrefsBuildVersionChangeOnInit) {
106 prefs_.SetString(kPrefsPreviousVersion, "00001"); // Set the fake version
107 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 1);
108 prefs_.SetInt64(kPrefsSystemUpdatedMarker, 23456);
109
110 EXPECT_CALL(*metrics_reporter_,
111 ReportAbnormallyTerminatedUpdateAttemptMetrics())
112 .Times(1);
113
114 Time now = Time::FromInternalValue(34456);
115 clock_->SetMonotonicTime(now);
116 TimeDelta duration = now - Time::FromInternalValue(23456);
117 EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(duration.InMinutes()))
118 .Times(1);
119
120 update_attempter_android_.Init();
121 // Check that we reset the metric prefs.
122 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700123 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
124 EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker));
xunchang9cf52622019-01-25 11:04:58 -0800125 // PayloadAttemptNumber should persist across reboots.
126 EXPECT_TRUE(prefs_.Exists(kPrefsPayloadAttemptNumber));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700127}
128
129TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) {
130 prefs_.SetInt64(kPrefsNumReboots, 3);
131 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2);
132 prefs_.SetString(kPrefsPreviousVersion, "56789");
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700133 prefs_.SetInt64(kPrefsUpdateBootTimestampStart, 10000);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700134 prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
135
Tianjie Xu52c678c2017-10-18 15:52:27 -0700136 Time boot_time = Time::FromInternalValue(22345);
137 Time up_time = Time::FromInternalValue(21345);
138 clock_->SetBootTime(boot_time);
139 clock_->SetMonotonicTime(up_time);
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700140 TimeDelta duration = boot_time - Time::FromInternalValue(10000);
Tianjie Xu52c678c2017-10-18 15:52:27 -0700141 TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700142 EXPECT_CALL(
143 *metrics_reporter_,
Amin Hassani538bd592020-11-04 20:46:08 -0800144 ReportUpdateAttemptMetrics(2,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700145 _,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700146 duration,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700147 duration_uptime,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700148 _,
149 metrics::AttemptResult::kUpdateSucceeded,
150 ErrorCode::kSuccess))
151 .Times(1);
152 EXPECT_CALL(*metrics_reporter_,
Sen Jiang8712e962018-05-08 12:12:28 -0700153 ReportSuccessfulUpdateMetrics(
Tianjie Xu21030c12019-08-14 13:00:23 -0700154 2, 0, _, 50, _, _, duration, duration_uptime, 3, _))
Tianjie Xu90aaa102017-10-10 17:39:03 -0700155 .Times(1);
156
Tianjie Xu21030c12019-08-14 13:00:23 -0700157 // Adds a payload of 50 bytes to the InstallPlan.
158 InstallPlan::Payload payload;
159 payload.size = 50;
160 AddPayload(std::move(payload));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700161 SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
162 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
163
164 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
165 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
166 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
167 EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
168}
169
Tianjie Xud4777a12017-10-24 14:54:18 -0700170TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) {
171 // Check both prefs are updated correctly.
172 update_attempter_android_.BytesReceived(20, 50, 200);
173 EXPECT_EQ(
174 20,
175 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
176 EXPECT_EQ(
177 20,
178 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
179
180 EXPECT_CALL(*metrics_reporter_,
181 ReportUpdateAttemptDownloadMetrics(50, _, _, _, _))
182 .Times(1);
183 EXPECT_CALL(*metrics_reporter_,
184 ReportUpdateAttemptDownloadMetrics(40, _, _, _, _))
185 .Times(1);
186
187 int64_t total_bytes[kNumDownloadSources] = {};
188 total_bytes[kDownloadSourceHttpsServer] = 90;
189 EXPECT_CALL(*metrics_reporter_,
190 ReportSuccessfulUpdateMetrics(
191 _,
192 _,
193 _,
Tianjie Xu21030c12019-08-14 13:00:23 -0700194 50,
Tianjie Xud4777a12017-10-24 14:54:18 -0700195 test_utils::DownloadSourceMatcher(total_bytes),
Tianjie Xu21030c12019-08-14 13:00:23 -0700196 80,
Tianjie Xud4777a12017-10-24 14:54:18 -0700197 _,
198 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700199 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700200 _))
201 .Times(1);
202
Tianjie Xu21030c12019-08-14 13:00:23 -0700203 // Adds a payload of 50 bytes to the InstallPlan.
204 InstallPlan::Payload payload;
205 payload.size = 50;
206 AddPayload(std::move(payload));
207
Sen Jiang771f6482018-04-04 17:59:10 -0700208 // The first update fails after receiving 50 bytes in total.
Tianjie Xud4777a12017-10-24 14:54:18 -0700209 update_attempter_android_.BytesReceived(30, 50, 200);
210 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError);
211 EXPECT_EQ(
212 0,
213 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
214 EXPECT_EQ(
215 50,
216 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
217
218 // The second update succeeds after receiving 40 bytes, which leads to a
Tianjie Xu21030c12019-08-14 13:00:23 -0700219 // overhead of (90 - 50) / 50 = 80%.
Tianjie Xud4777a12017-10-24 14:54:18 -0700220 update_attempter_android_.BytesReceived(40, 40, 50);
221 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
222 // Both prefs should be cleared.
223 EXPECT_EQ(
224 0,
225 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
226 EXPECT_EQ(
227 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
228}
229
Tianjie Xu90aaa102017-10-10 17:39:03 -0700230} // namespace chromeos_update_engine