blob: 6c0718af7af03a848cf2b97f29f8009a740bd1f2 [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"
31#include "update_engine/common/utils.h"
32#include "update_engine/daemon_state_android.h"
33#include "update_engine/mock_metrics_reporter.h"
34
35using base::Time;
36using base::TimeDelta;
37using testing::_;
38using update_engine::UpdateStatus;
39
40namespace chromeos_update_engine {
41class UpdateAttempterAndroidTest : public ::testing::Test {
42 protected:
43 UpdateAttempterAndroidTest() = default;
44
45 void SetUp() override {
46 clock_ = new FakeClock();
47 metrics_reporter_ = new testing::NiceMock<MockMetricsReporter>();
48 update_attempter_android_.metrics_reporter_.reset(metrics_reporter_);
49 update_attempter_android_.clock_.reset(clock_);
50 update_attempter_android_.processor_.reset(
51 new testing::NiceMock<MockActionProcessor>());
52 }
53
54 void SetUpdateStatus(update_engine::UpdateStatus status) {
55 update_attempter_android_.status_ = status;
56 }
57
58 UpdateAttempterAndroid update_attempter_android_{
59 &daemon_state_, &prefs_, &boot_control_, &hardware_};
60
61 DaemonStateAndroid daemon_state_;
62 FakePrefs prefs_;
63 FakeBootControl boot_control_;
64 FakeHardware hardware_;
65
66 FakeClock* clock_;
67 testing::NiceMock<MockMetricsReporter>* metrics_reporter_;
68};
69
70TEST_F(UpdateAttempterAndroidTest, UpdatePrefsSameBuildVersionOnInit) {
71 std::string build_version =
72 android::base::GetProperty("ro.build.version.incremental", "");
73 prefs_.SetString(kPrefsPreviousVersion, build_version);
74 prefs_.SetString(kPrefsBootId, "oldboot");
75 prefs_.SetInt64(kPrefsNumReboots, 1);
76
77 EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(_)).Times(0);
78 update_attempter_android_.Init();
79
80 // Check that the boot_id and reboot_count are updated.
81 std::string boot_id;
82 utils::GetBootId(&boot_id);
83 EXPECT_TRUE(prefs_.Exists(kPrefsBootId));
84 std::string prefs_boot_id;
85 EXPECT_TRUE(prefs_.GetString(kPrefsBootId, &prefs_boot_id));
86 EXPECT_EQ(boot_id, prefs_boot_id);
87
88 EXPECT_TRUE(prefs_.Exists(kPrefsNumReboots));
89 int64_t reboot_count;
90 EXPECT_TRUE(prefs_.GetInt64(kPrefsNumReboots, &reboot_count));
91 EXPECT_EQ(2, reboot_count);
92}
93
94TEST_F(UpdateAttempterAndroidTest, UpdatePrefsBuildVersionChangeOnInit) {
95 prefs_.SetString(kPrefsPreviousVersion, "00001"); // Set the fake version
96 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 1);
97 prefs_.SetInt64(kPrefsSystemUpdatedMarker, 23456);
98
99 EXPECT_CALL(*metrics_reporter_,
100 ReportAbnormallyTerminatedUpdateAttemptMetrics())
101 .Times(1);
102
103 Time now = Time::FromInternalValue(34456);
104 clock_->SetMonotonicTime(now);
105 TimeDelta duration = now - Time::FromInternalValue(23456);
106 EXPECT_CALL(*metrics_reporter_, ReportTimeToReboot(duration.InMinutes()))
107 .Times(1);
108
109 update_attempter_android_.Init();
110 // Check that we reset the metric prefs.
111 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
112 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
113 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
114 EXPECT_FALSE(prefs_.Exists(kPrefsSystemUpdatedMarker));
115}
116
117TEST_F(UpdateAttempterAndroidTest, ReportMetricsOnUpdateTerminated) {
118 prefs_.SetInt64(kPrefsNumReboots, 3);
119 prefs_.SetInt64(kPrefsPayloadAttemptNumber, 2);
120 prefs_.SetString(kPrefsPreviousVersion, "56789");
121 prefs_.SetInt64(kPrefsUpdateTimestampStart, 12345);
122
123 Time now = Time::FromInternalValue(22345);
124 clock_->SetMonotonicTime(now);
125 TimeDelta duration = now - Time::FromInternalValue(12345);
126 EXPECT_CALL(
127 *metrics_reporter_,
128 ReportUpdateAttemptMetrics(_,
129 2,
130 _,
131 _,
132 duration,
133 _,
134 metrics::AttemptResult::kUpdateSucceeded,
135 ErrorCode::kSuccess))
136 .Times(1);
137 EXPECT_CALL(*metrics_reporter_,
138 ReportSuccessfulUpdateMetrics(2, 0, _, _, _, _, duration, 3, _))
139 .Times(1);
140
141 SetUpdateStatus(UpdateStatus::UPDATE_AVAILABLE);
142 update_attempter_android_.ProcessingDone(nullptr, ErrorCode::kSuccess);
143
144 EXPECT_FALSE(prefs_.Exists(kPrefsNumReboots));
145 EXPECT_FALSE(prefs_.Exists(kPrefsPayloadAttemptNumber));
146 EXPECT_FALSE(prefs_.Exists(kPrefsUpdateTimestampStart));
147 EXPECT_TRUE(prefs_.Exists(kPrefsSystemUpdatedMarker));
148}
149
150} // namespace chromeos_update_engine