blob: 3be0b7eaaf2817e91a18ddf793d9f5bc2f2f027f [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>
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 Xud4777a12017-10-24 14:54:18 -070031#include "update_engine/common/test_utils.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070032#include "update_engine/common/utils.h"
33#include "update_engine/daemon_state_android.h"
34#include "update_engine/mock_metrics_reporter.h"
35
36using base::Time;
37using base::TimeDelta;
38using testing::_;
39using update_engine::UpdateStatus;
40
41namespace chromeos_update_engine {
Tianjie Xud4777a12017-10-24 14:54:18 -070042
Tianjie Xu90aaa102017-10-10 17:39:03 -070043class 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
72TEST_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
96TEST_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));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700114 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
115 EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker));
xunchang9cf52622019-01-25 11:04:58 -0800116 // PayloadAttemptNumber should persist across reboots.
117 EXPECT_TRUE(prefs_.Exists(kPrefsPayloadAttemptNumber));
Tianjie Xu90aaa102017-10-10 17:39:03 -0700118}
119
120TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) {
121 prefs_.SetInt64(kPrefsNumReboots, 3);
122 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2);
123 prefs_.SetString(kPrefsPreviousVersion, "56789");
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700124 prefs_.SetInt64(kPrefsUpdateBootTimestampStart, 10000);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700125 prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
126
Tianjie Xu52c678c2017-10-18 15:52:27 -0700127 Time boot_time = Time::FromInternalValue(22345);
128 Time up_time = Time::FromInternalValue(21345);
129 clock_->SetBootTime(boot_time);
130 clock_->SetMonotonicTime(up_time);
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700131 TimeDelta duration = boot_time - Time::FromInternalValue(10000);
Tianjie Xu52c678c2017-10-18 15:52:27 -0700132 TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700133 EXPECT_CALL(
134 *metrics_reporter_,
135 ReportUpdateAttemptMetrics(_,
136 2,
137 _,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700138 duration,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700139 duration_uptime,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700140 _,
141 metrics::AttemptResult::kUpdateSucceeded,
142 ErrorCode::kSuccess))
143 .Times(1);
144 EXPECT_CALL(*metrics_reporter_,
Sen Jiang8712e962018-05-08 12:12:28 -0700145 ReportSuccessfulUpdateMetrics(
146 2, 0, _, _, _, _, duration, duration_uptime, 3, _))
Tianjie Xu90aaa102017-10-10 17:39:03 -0700147 .Times(1);
148
149 SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
150 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
151
152 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
153 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
154 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
155 EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
156}
157
Tianjie Xud4777a12017-10-24 14:54:18 -0700158TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) {
159 // Check both prefs are updated correctly.
160 update_attempter_android_.BytesReceived(20, 50, 200);
161 EXPECT_EQ(
162 20,
163 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
164 EXPECT_EQ(
165 20,
166 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
167
168 EXPECT_CALL(*metrics_reporter_,
169 ReportUpdateAttemptDownloadMetrics(50, _, _, _, _))
170 .Times(1);
171 EXPECT_CALL(*metrics_reporter_,
172 ReportUpdateAttemptDownloadMetrics(40, _, _, _, _))
173 .Times(1);
174
175 int64_t total_bytes[kNumDownloadSources] = {};
176 total_bytes[kDownloadSourceHttpsServer] = 90;
177 EXPECT_CALL(*metrics_reporter_,
178 ReportSuccessfulUpdateMetrics(
179 _,
180 _,
181 _,
182 _,
183 test_utils::DownloadSourceMatcher(total_bytes),
184 125,
185 _,
186 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700187 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700188 _))
189 .Times(1);
190
Sen Jiang771f6482018-04-04 17:59:10 -0700191 // The first update fails after receiving 50 bytes in total.
Tianjie Xud4777a12017-10-24 14:54:18 -0700192 update_attempter_android_.BytesReceived(30, 50, 200);
193 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError);
194 EXPECT_EQ(
195 0,
196 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
197 EXPECT_EQ(
198 50,
199 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
200
201 // The second update succeeds after receiving 40 bytes, which leads to a
202 // overhead of 50 / 40 = 125%.
203 update_attempter_android_.BytesReceived(40, 40, 50);
204 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
205 // Both prefs should be cleared.
206 EXPECT_EQ(
207 0,
208 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
209 EXPECT_EQ(
210 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
211}
212
Tianjie Xu90aaa102017-10-10 17:39:03 -0700213} // namespace chromeos_update_engine