blob: 458c224068f9caaad7a5a1483a533243f93d101e [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
Colin Cross238d9042021-12-21 13:09:30 -080023#include <fcntl.h>
Kelvin Zhang3fe49642021-10-04 15:35:02 -070024#include <sys/sendfile.h>
25#include <unistd.h>
26
Tianjie Xu90aaa102017-10-10 17:39:03 -070027#include <android-base/properties.h>
28#include <base/time/time.h>
Kelvin Zhang3fe49642021-10-04 15:35:02 -070029#include <brillo/data_encoding.h>
30#include <brillo/message_loops/fake_message_loop.h>
Tianjie Xu90aaa102017-10-10 17:39:03 -070031#include <gtest/gtest.h>
Kelvin Zhang3fe49642021-10-04 15:35:02 -070032#include <liblp/builder.h>
33#include <fs_mgr.h>
34#include <liblp/liblp.h>
Tianjie Xu90aaa102017-10-10 17:39:03 -070035
Kelvin Zhang3fe49642021-10-04 15:35:02 -070036#include "update_engine/aosp/boot_control_android.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070037#include "update_engine/aosp/daemon_state_android.h"
Kelvin Zhang3fe49642021-10-04 15:35:02 -070038#include "update_engine/common/constants.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070039#include "update_engine/common/fake_boot_control.h"
40#include "update_engine/common/fake_clock.h"
41#include "update_engine/common/fake_hardware.h"
42#include "update_engine/common/fake_prefs.h"
Colin Cross26b82b12021-12-22 10:09:19 -080043#include "update_engine/common/hash_calculator.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070044#include "update_engine/common/mock_action_processor.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070045#include "update_engine/common/mock_metrics_reporter.h"
Colin Cross26b82b12021-12-22 10:09:19 -080046#include "update_engine/common/prefs.h"
Tianjie Xud4777a12017-10-24 14:54:18 -070047#include "update_engine/common/test_utils.h"
Colin Cross26b82b12021-12-22 10:09:19 -080048#include "update_engine/common/testing_constants.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070049#include "update_engine/common/utils.h"
Kelvin Zhang3fe49642021-10-04 15:35:02 -070050#include "update_engine/payload_consumer/install_plan.h"
Colin Cross26b82b12021-12-22 10:09:19 -080051#include "update_engine/payload_consumer/payload_constants.h"
Kelvin Zhang3fe49642021-10-04 15:35:02 -070052#include "update_engine/payload_generator/delta_diff_generator.h"
53#include "update_engine/payload_generator/extent_ranges.h"
54#include "update_engine/payload_generator/payload_file.h"
55#include "update_engine/payload_generator/payload_signer.h"
56#include "update_engine/update_metadata.pb.h"
57#include "update_engine/update_status_utils.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070058
59using base::Time;
60using base::TimeDelta;
61using testing::_;
62using update_engine::UpdateStatus;
63
64namespace chromeos_update_engine {
Tianjie Xud4777a12017-10-24 14:54:18 -070065
Kelvin Zhang55624032021-12-20 12:13:24 -080066// Compare the value of builtin array for download source parameter.
67MATCHER_P(DownloadSourceMatcher, source_array, "") {
68 return std::equal(source_array, source_array + kNumDownloadSources, arg);
69}
70
Tianjie Xu90aaa102017-10-10 17:39:03 -070071class UpdateAttempterAndroidTest : public ::testing::Test {
72 protected:
73 UpdateAttempterAndroidTest() = default;
74
75 void SetUp() override {
76 clock_ = new FakeClock();
77 metrics_reporter_ = new testing::NiceMock<MockMetricsReporter>();
78 update_attempter_android_.metrics_reporter_.reset(metrics_reporter_);
79 update_attempter_android_.clock_.reset(clock_);
80 update_attempter_android_.processor_.reset(
81 new testing::NiceMock<MockActionProcessor>());
82 }
83
84 void SetUpdateStatus(update_engine::UpdateStatus status) {
85 update_attempter_android_.status_ = status;
86 }
87
Tianjie Xu21030c12019-08-14 13:00:23 -070088 void AddPayload(InstallPlan::Payload&& payload) {
89 update_attempter_android_.install_plan_.payloads.push_back(
90 std::move(payload));
91 }
92
Tianjie Xu90aaa102017-10-10 17:39:03 -070093 DaemonStateAndroid daemon_state_;
94 FakePrefs prefs_;
95 FakeBootControl boot_control_;
96 FakeHardware hardware_;
97
Yifan Hong93c497d2021-02-08 14:25:52 -080098 UpdateAttempterAndroid update_attempter_android_{
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000099 &daemon_state_, &prefs_, &boot_control_, &hardware_, nullptr};
Yifan Hong93c497d2021-02-08 14:25:52 -0800100
Tianjie Xu90aaa102017-10-10 17:39:03 -0700101 FakeClock* clock_;
102 testing::NiceMock<MockMetricsReporter>* metrics_reporter_;
103};
104
Kelvin Zhang55624032021-12-20 12:13:24 -0800105namespace {
106
Tianjie Xu90aaa102017-10-10 17:39:03 -0700107TEST_F(UpdateAttempterAndroidTest, UpdatePrefsSameBuildVersionOnInit) {
108 std::string build_version =
109 android::base::GetProperty("ro.build.version.incremental", "");
110 prefs_.SetString(kPrefsPreviousVersion, build_version);
111 prefs_.SetString(kPrefsBootId, "oldboot");
112 prefs_.SetInt64(kPrefsNumReboots, 1);
Kelvin Zhang86603472021-05-11 12:16:27 -0400113 prefs_.SetInt64(kPrefsPreviousSlot, 1);
114 boot_control_.SetCurrentSlot(1);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700115
116 EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(_)).Times(0);
117 update_attempter_android_.Init();
118
119 // Check that the boot_id and reboot_count are updated.
120 std::string boot_id;
121 utils::GetBootId(&boot_id);
Kelvin Zhang86603472021-05-11 12:16:27 -0400122 ASSERT_TRUE(prefs_.Exists(kPrefsBootId));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700123 std::string prefs_boot_id;
Kelvin Zhang86603472021-05-11 12:16:27 -0400124 ASSERT_TRUE(prefs_.GetString(kPrefsBootId, &prefs_boot_id));
125 ASSERT_EQ(boot_id, prefs_boot_id);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700126
Kelvin Zhang86603472021-05-11 12:16:27 -0400127 ASSERT_TRUE(prefs_.Exists(kPrefsNumReboots));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700128 int64_t reboot_count;
Kelvin Zhang86603472021-05-11 12:16:27 -0400129 ASSERT_TRUE(prefs_.GetInt64(kPrefsNumReboots, &reboot_count));
130 ASSERT_EQ(2, reboot_count);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700131}
132
133TEST_F(UpdateAttempterAndroidTest, UpdatePrefsBuildVersionChangeOnInit) {
134 prefs_.SetString(kPrefsPreviousVersion, "00001"); // Set the fake version
135 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 1);
136 prefs_.SetInt64(kPrefsSystemUpdatedMarker, 23456);
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400137 prefs_.SetInt64(kPrefsPreviousSlot, 1);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700138
139 EXPECT_CALL(*metrics_reporter_,
140 ReportAbnormallyTerminatedUpdateAttemptMetrics())
141 .Times(1);
142
143 Time now = Time::FromInternalValue(34456);
144 clock_->SetMonotonicTime(now);
145 TimeDelta duration = now - Time::FromInternalValue(23456);
146 EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(duration.InMinutes()))
147 .Times(1);
148
149 update_attempter_android_.Init();
150 // Check that we reset the metric prefs.
151 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700152 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
153 EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker));
xunchang9cf52622019-01-25 11:04:58 -0800154 // PayloadAttemptNumber should persist across reboots.
155 EXPECT_TRUE(prefs_.Exists(kPrefsPayloadAttemptNumber));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700156}
157
158TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) {
159 prefs_.SetInt64(kPrefsNumReboots, 3);
160 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2);
161 prefs_.SetString(kPrefsPreviousVersion, "56789");
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700162 prefs_.SetInt64(kPrefsUpdateBootTimestampStart, 10000);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700163 prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
164
Tianjie Xu52c678c2017-10-18 15:52:27 -0700165 Time boot_time = Time::FromInternalValue(22345);
166 Time up_time = Time::FromInternalValue(21345);
167 clock_->SetBootTime(boot_time);
168 clock_->SetMonotonicTime(up_time);
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700169 TimeDelta duration = boot_time - Time::FromInternalValue(10000);
Tianjie Xu52c678c2017-10-18 15:52:27 -0700170 TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700171 EXPECT_CALL(
172 *metrics_reporter_,
Amin Hassani538bd592020-11-04 20:46:08 -0800173 ReportUpdateAttemptMetrics(2,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700174 _,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700175 duration,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700176 duration_uptime,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700177 _,
178 metrics::AttemptResult::kUpdateSucceeded,
179 ErrorCode::kSuccess))
180 .Times(1);
181 EXPECT_CALL(*metrics_reporter_,
Sen Jiang8712e962018-05-08 12:12:28 -0700182 ReportSuccessfulUpdateMetrics(
Tianjie Xu21030c12019-08-14 13:00:23 -0700183 2, 0, _, 50, _, _, duration, duration_uptime, 3, _))
Tianjie Xu90aaa102017-10-10 17:39:03 -0700184 .Times(1);
185
Tianjie Xu21030c12019-08-14 13:00:23 -0700186 // Adds a payload of 50 bytes to the InstallPlan.
187 InstallPlan::Payload payload;
188 payload.size = 50;
189 AddPayload(std::move(payload));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700190 SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
191 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
192
193 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
194 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
195 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
196 EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
197}
198
Tianjie Xud4777a12017-10-24 14:54:18 -0700199TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) {
200 // Check both prefs are updated correctly.
201 update_attempter_android_.BytesReceived(20, 50, 200);
202 EXPECT_EQ(
203 20,
204 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
205 EXPECT_EQ(
206 20,
207 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
208
209 EXPECT_CALL(*metrics_reporter_,
210 ReportUpdateAttemptDownloadMetrics(50, _, _, _, _))
211 .Times(1);
212 EXPECT_CALL(*metrics_reporter_,
213 ReportUpdateAttemptDownloadMetrics(40, _, _, _, _))
214 .Times(1);
215
216 int64_t total_bytes[kNumDownloadSources] = {};
217 total_bytes[kDownloadSourceHttpsServer] = 90;
Kelvin Zhang55624032021-12-20 12:13:24 -0800218 EXPECT_CALL(
219 *metrics_reporter_,
220 ReportSuccessfulUpdateMetrics(
221 _, _, _, 50, DownloadSourceMatcher(total_bytes), 80, _, _, _, _))
Tianjie Xud4777a12017-10-24 14:54:18 -0700222 .Times(1);
223
Tianjie Xu21030c12019-08-14 13:00:23 -0700224 // Adds a payload of 50 bytes to the InstallPlan.
225 InstallPlan::Payload payload;
226 payload.size = 50;
227 AddPayload(std::move(payload));
228
Sen Jiang771f6482018-04-04 17:59:10 -0700229 // The first update fails after receiving 50 bytes in total.
Tianjie Xud4777a12017-10-24 14:54:18 -0700230 update_attempter_android_.BytesReceived(30, 50, 200);
231 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError);
232 EXPECT_EQ(
233 0,
234 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
235 EXPECT_EQ(
236 50,
237 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
238
239 // The second update succeeds after receiving 40 bytes, which leads to a
Tianjie Xu21030c12019-08-14 13:00:23 -0700240 // overhead of (90 - 50) / 50 = 80%.
Tianjie Xud4777a12017-10-24 14:54:18 -0700241 update_attempter_android_.BytesReceived(40, 40, 50);
242 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
243 // Both prefs should be cleared.
244 EXPECT_EQ(
245 0,
246 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
247 EXPECT_EQ(
248 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
249}
250
Kelvin Zhang55624032021-12-20 12:13:24 -0800251} // namespace
252
Tianjie Xu90aaa102017-10-10 17:39:03 -0700253} // namespace chromeos_update_engine