blob: 721b73520574bac00e2828f9773000b3c27390bd [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
17#include "update_engine/update_attempter_android.h"
18
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
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 Xud4777a12017-10-24 14:54:18 -070032#include "update_engine/common/test_utils.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070033#include "update_engine/common/utils.h"
34#include "update_engine/daemon_state_android.h"
35#include "update_engine/mock_metrics_reporter.h"
36
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_,
141 ReportUpdateAttemptMetrics(_,
142 2,
143 _,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700144 duration,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700145 duration_uptime,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700146 _,
147 metrics::AttemptResult::kUpdateSucceeded,
148 ErrorCode::kSuccess))
149 .Times(1);
150 EXPECT_CALL(*metrics_reporter_,
Sen Jiang8712e962018-05-08 12:12:28 -0700151 ReportSuccessfulUpdateMetrics(
Tianjie Xu21030c12019-08-14 13:00:23 -0700152 2, 0, _, 50, _, _, duration, duration_uptime, 3, _))
Tianjie Xu90aaa102017-10-10 17:39:03 -0700153 .Times(1);
154
Tianjie Xu21030c12019-08-14 13:00:23 -0700155 // Adds a payload of 50 bytes to the InstallPlan.
156 InstallPlan::Payload payload;
157 payload.size = 50;
158 AddPayload(std::move(payload));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700159 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 Xud4777a12017-10-24 14:54:18 -0700168TEST_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 Xu21030c12019-08-14 13:00:23 -0700192 50,
Tianjie Xud4777a12017-10-24 14:54:18 -0700193 test_utils::DownloadSourceMatcher(total_bytes),
Tianjie Xu21030c12019-08-14 13:00:23 -0700194 80,
Tianjie Xud4777a12017-10-24 14:54:18 -0700195 _,
196 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700197 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700198 _))
199 .Times(1);
200
Tianjie Xu21030c12019-08-14 13:00:23 -0700201 // Adds a payload of 50 bytes to the InstallPlan.
202 InstallPlan::Payload payload;
203 payload.size = 50;
204 AddPayload(std::move(payload));
205
Sen Jiang771f6482018-04-04 17:59:10 -0700206 // The first update fails after receiving 50 bytes in total.
Tianjie Xud4777a12017-10-24 14:54:18 -0700207 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 Xu21030c12019-08-14 13:00:23 -0700217 // overhead of (90 - 50) / 50 = 80%.
Tianjie Xud4777a12017-10-24 14:54:18 -0700218 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 Xu90aaa102017-10-10 17:39:03 -0700228} // namespace chromeos_update_engine