blob: 2593d44ab75e3b102461752e034d18ee44b72cdf [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));
114 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
115 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
116 EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker));
117}
118
119TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) {
120 prefs_.SetInt64(kPrefsNumReboots, 3);
121 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2);
122 prefs_.SetString(kPrefsPreviousVersion, "56789");
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700123 prefs_.SetInt64(kPrefsUpdateBootTimestampStart, 10000);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700124 prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
125
Tianjie Xu52c678c2017-10-18 15:52:27 -0700126 Time boot_time = Time::FromInternalValue(22345);
127 Time up_time = Time::FromInternalValue(21345);
128 clock_->SetBootTime(boot_time);
129 clock_->SetMonotonicTime(up_time);
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700130 TimeDelta duration = boot_time - Time::FromInternalValue(10000);
Tianjie Xu52c678c2017-10-18 15:52:27 -0700131 TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700132 EXPECT_CALL(
133 *metrics_reporter_,
134 ReportUpdateAttemptMetrics(_,
135 2,
136 _,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700137 duration,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700138 duration_uptime,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700139 _,
140 metrics::AttemptResult::kUpdateSucceeded,
141 ErrorCode::kSuccess))
142 .Times(1);
143 EXPECT_CALL(*metrics_reporter_,
Sen Jiang8712e962018-05-08 12:12:28 -0700144 ReportSuccessfulUpdateMetrics(
145 2, 0, _, _, _, _, duration, duration_uptime, 3, _))
Tianjie Xu90aaa102017-10-10 17:39:03 -0700146 .Times(1);
147
148 SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
149 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
150
151 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
152 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
153 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
154 EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
155}
156
Tianjie Xud4777a12017-10-24 14:54:18 -0700157TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) {
158 // Check both prefs are updated correctly.
159 update_attempter_android_.BytesReceived(20, 50, 200);
160 EXPECT_EQ(
161 20,
162 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
163 EXPECT_EQ(
164 20,
165 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
166
167 EXPECT_CALL(*metrics_reporter_,
168 ReportUpdateAttemptDownloadMetrics(50, _, _, _, _))
169 .Times(1);
170 EXPECT_CALL(*metrics_reporter_,
171 ReportUpdateAttemptDownloadMetrics(40, _, _, _, _))
172 .Times(1);
173
174 int64_t total_bytes[kNumDownloadSources] = {};
175 total_bytes[kDownloadSourceHttpsServer] = 90;
176 EXPECT_CALL(*metrics_reporter_,
177 ReportSuccessfulUpdateMetrics(
178 _,
179 _,
180 _,
181 _,
182 test_utils::DownloadSourceMatcher(total_bytes),
183 125,
184 _,
185 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700186 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700187 _))
188 .Times(1);
189
Sen Jiang771f6482018-04-04 17:59:10 -0700190 // The first update fails after receiving 50 bytes in total.
Tianjie Xud4777a12017-10-24 14:54:18 -0700191 update_attempter_android_.BytesReceived(30, 50, 200);
192 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError);
193 EXPECT_EQ(
194 0,
195 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
196 EXPECT_EQ(
197 50,
198 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
199
200 // The second update succeeds after receiving 40 bytes, which leads to a
201 // overhead of 50 / 40 = 125%.
202 update_attempter_android_.BytesReceived(40, 40, 50);
203 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
204 // Both prefs should be cleared.
205 EXPECT_EQ(
206 0,
207 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
208 EXPECT_EQ(
209 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
210}
211
Tianjie Xu90aaa102017-10-10 17:39:03 -0700212} // namespace chromeos_update_engine