blob: 6b53a21897aaee0b010fee879e1a350cbc23f4cc [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");
123 prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
124
Tianjie Xu52c678c2017-10-18 15:52:27 -0700125 Time boot_time = Time::FromInternalValue(22345);
126 Time up_time = Time::FromInternalValue(21345);
127 clock_->SetBootTime(boot_time);
128 clock_->SetMonotonicTime(up_time);
129 TimeDelta duration = boot_time - Time::FromInternalValue(12345);
130 TimeDelta duration_uptime = up_time - Time::FromInternalValue(12345);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700131 EXPECT_CALL(
132 *metrics_reporter_,
133 ReportUpdateAttemptMetrics(_,
134 2,
135 _,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700136 duration,
Tianjie Xu52c678c2017-10-18 15:52:27 -0700137 duration_uptime,
Tianjie Xu90aaa102017-10-10 17:39:03 -0700138 _,
139 metrics::AttemptResult::kUpdateSucceeded,
140 ErrorCode::kSuccess))
141 .Times(1);
142 EXPECT_CALL(*metrics_reporter_,
Sen Jiang8712e962018-05-08 12:12:28 -0700143 ReportSuccessfulUpdateMetrics(
144 2, 0, _, _, _, _, duration, duration_uptime, 3, _))
Tianjie Xu90aaa102017-10-10 17:39:03 -0700145 .Times(1);
146
147 SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
148 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
149
150 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
151 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
152 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
153 EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
154}
155
Tianjie Xud4777a12017-10-24 14:54:18 -0700156TEST_F(UpdateAttempterAndroidTest, ReportMetricsForBytesDownloaded) {
157 // Check both prefs are updated correctly.
158 update_attempter_android_.BytesReceived(20, 50, 200);
159 EXPECT_EQ(
160 20,
161 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
162 EXPECT_EQ(
163 20,
164 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
165
166 EXPECT_CALL(*metrics_reporter_,
167 ReportUpdateAttemptDownloadMetrics(50, _, _, _, _))
168 .Times(1);
169 EXPECT_CALL(*metrics_reporter_,
170 ReportUpdateAttemptDownloadMetrics(40, _, _, _, _))
171 .Times(1);
172
173 int64_t total_bytes[kNumDownloadSources] = {};
174 total_bytes[kDownloadSourceHttpsServer] = 90;
175 EXPECT_CALL(*metrics_reporter_,
176 ReportSuccessfulUpdateMetrics(
177 _,
178 _,
179 _,
180 _,
181 test_utils::DownloadSourceMatcher(total_bytes),
182 125,
183 _,
184 _,
Sen Jiang8712e962018-05-08 12:12:28 -0700185 _,
Tianjie Xud4777a12017-10-24 14:54:18 -0700186 _))
187 .Times(1);
188
Sen Jiang771f6482018-04-04 17:59:10 -0700189 // The first update fails after receiving 50 bytes in total.
Tianjie Xud4777a12017-10-24 14:54:18 -0700190 update_attempter_android_.BytesReceived(30, 50, 200);
191 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kError);
192 EXPECT_EQ(
193 0,
194 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
195 EXPECT_EQ(
196 50,
197 metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
198
199 // The second update succeeds after receiving 40 bytes, which leads to a
200 // overhead of 50 / 40 = 125%.
201 update_attempter_android_.BytesReceived(40, 40, 50);
202 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
203 // Both prefs should be cleared.
204 EXPECT_EQ(
205 0,
206 metrics_utils::GetPersistedValue(kPrefsCurrentBytesDownloaded, &prefs_));
207 EXPECT_EQ(
208 0, metrics_utils::GetPersistedValue(kPrefsTotalBytesDownloaded, &prefs_));
209}
210
Tianjie Xu90aaa102017-10-10 17:39:03 -0700211} // namespace chromeos_update_engine