blob: fc3026849c8d94b60428cacd211e0f48cdedf4f5 [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
Amin Hassaniec7bc112020-10-29 16:47:58 -070027#include "update_engine/aosp/daemon_state_android.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070028#include "update_engine/common/fake_boot_control.h"
29#include "update_engine/common/fake_clock.h"
30#include "update_engine/common/fake_hardware.h"
31#include "update_engine/common/fake_prefs.h"
32#include "update_engine/common/mock_action_processor.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070033#include "update_engine/common/mock_metrics_reporter.h"
Tianjie Xud4777a12017-10-24 14:54:18 -070034#include "update_engine/common/test_utils.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070035#include "update_engine/common/utils.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070036
37using base::Time;
38using base::TimeDelta;
39using testing::_;
40using update_engine::UpdateStatus;
41
42namespace chromeos_update_engine {
Tianjie Xud4777a12017-10-24 14:54:18 -070043
Tianjie Xu90aaa102017-10-10 17:39:03 -070044class 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 Xu21030c12019-08-14 13:00:23 -070061 void AddPayload(InstallPlan::Payload&& payload) {
62 update_attempter_android_.install_plan_.payloads.push_back(
63 std::move(payload));
64 }
65
Tianjie Xu90aaa102017-10-10 17:39:03 -070066 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
78TEST_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
102TEST_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 Xu90aaa102017-10-10 17:39:03 -0700120 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
121 EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker));
xunchang9cf52622019-01-25 11:04:58 -0800122 // PayloadAttemptNumber should persist across reboots.
123 EXPECT_TRUE(prefs_.Exists(kPrefsPayloadAttemptNumber));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700124}
125
126TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) {
127 prefs_.SetInt64(kPrefsNumReboots, 3);
128 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2);
129 prefs_.SetString(kPrefsPreviousVersion, "56789");
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700130 prefs_.SetInt64(kPrefsUpdateBootTimestampStart, 10000);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700131 prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
132
Tianjie Xu52c678c2017-10-18 15:52:27 -0700133 Time boot_time = Time::FromInternalValue(22345);
134 Time up_time = Time::FromInternalValue(21345);
135 clock_->SetBootTime(boot_time);
136 clock_->SetMonotonicTime(up_time);
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700137 TimeDelta duration = boot_time - Time::FromInternalValue(10000);
Tianjie Xu52c678c2017-10-18 15:52:27 -0700138 TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700139 EXPECT_CALL(
140 *metrics_reporter_,
Amin Hassani538bd592020-11-04 20:46:08 -0800141 ReportUpdateAttemptMetrics(2,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700142 _,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700143 duration,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700144 duration_uptime,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700145 _,
146 metrics::AttemptResult::kUpdateSucceeded,
147 ErrorCode::kSuccess))
148 .Times(1);
149 EXPECT_CALL(*metrics_reporter_,
Sen Jiang8712e962018-05-08 12:12:28 -0700150 ReportSuccessfulUpdateMetrics(
Tianjie Xu21030c12019-08-14 13:00:23 -0700151 2, 0, _, 50, _, _, duration, duration_uptime, 3, _))
Tianjie Xu90aaa102017-10-10 17:39:03 -0700152 .Times(1);
153
Tianjie Xu21030c12019-08-14 13:00:23 -0700154 // Adds a payload of 50 bytes to the InstallPlan.
155 InstallPlan::Payload payload;
156 payload.size = 50;
157 AddPayload(std::move(payload));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700158 SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
159 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
160
161 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
162 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
163 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
164 EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
165}
166
Tianjie Xud4777a12017-10-24 14:54:18 -0700167TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) {
168 // Check both prefs are updated correctly.
169 update_attempter_android_.BytesReceived(20, 50, 200);
170 EXPECT_EQ(
171 20,
172 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
173 EXPECT_EQ(
174 20,
175 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
176
177 EXPECT_CALL(*metrics_reporter_,
178 ReportUpdateAttemptDownloadMetrics(50, _, _, _, _))
179 .Times(1);
180 EXPECT_CALL(*metrics_reporter_,
181 ReportUpdateAttemptDownloadMetrics(40, _, _, _, _))
182 .Times(1);
183
184 int64_t total_bytes[kNumDownloadSources] = {};
185 total_bytes[kDownloadSourceHttpsServer] = 90;
186 EXPECT_CALL(*metrics_reporter_,
187 ReportSuccessfulUpdateMetrics(
188 _,
189 _,
190 _,
Tianjie Xu21030c12019-08-14 13:00:23 -0700191 50,
Tianjie Xud4777a12017-10-24 14:54:18 -0700192 test_utils::DownloadSourceMatcher(total_bytes),
Tianjie Xu21030c12019-08-14 13:00:23 -0700193 80,
Tianjie Xud4777a12017-10-24 14:54:18 -0700194 _,
195 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700196 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700197 _))
198 .Times(1);
199
Tianjie Xu21030c12019-08-14 13:00:23 -0700200 // Adds a payload of 50 bytes to the InstallPlan.
201 InstallPlan::Payload payload;
202 payload.size = 50;
203 AddPayload(std::move(payload));
204
Sen Jiang771f6482018-04-04 17:59:10 -0700205 // The first update fails after receiving 50 bytes in total.
Tianjie Xud4777a12017-10-24 14:54:18 -0700206 update_attempter_android_.BytesReceived(30, 50, 200);
207 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError);
208 EXPECT_EQ(
209 0,
210 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
211 EXPECT_EQ(
212 50,
213 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
214
215 // The second update succeeds after receiving 40 bytes, which leads to a
Tianjie Xu21030c12019-08-14 13:00:23 -0700216 // overhead of (90 - 50) / 50 = 80%.
Tianjie Xud4777a12017-10-24 14:54:18 -0700217 update_attempter_android_.BytesReceived(40, 40, 50);
218 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
219 // Both prefs should be cleared.
220 EXPECT_EQ(
221 0,
222 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
223 EXPECT_EQ(
224 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
225}
226
Tianjie Xu90aaa102017-10-10 17:39:03 -0700227} // namespace chromeos_update_engine