blob: 417a72b18ac41caa7e1f7ef60cca4676bd7dfa96 [file] [log] [blame]
Alex Deymo38429cf2015-11-11 18:27:22 -08001//
2// Copyright (C) 2015 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/metrics_utils.h"
18
19#include <gtest/gtest.h>
20
Alex Deymoa2591792015-11-17 00:39:40 -030021#include "update_engine/common/fake_clock.h"
22#include "update_engine/common/fake_prefs.h"
23#include "update_engine/fake_system_state.h"
24
Alex Deymo38429cf2015-11-11 18:27:22 -080025namespace chromeos_update_engine {
26namespace metrics_utils {
27
28class MetricsUtilsTest : public ::testing::Test {};
29
30TEST(MetricsUtilsTest, GetConnectionType) {
31 // Check that expected combinations map to the right value.
32 EXPECT_EQ(metrics::ConnectionType::kUnknown,
Sen Jiang255e22b2016-05-20 16:15:29 -070033 GetConnectionType(ConnectionType::kUnknown,
34 ConnectionTethering::kUnknown));
Colin Howesc9e98d62018-09-18 10:35:20 -070035 EXPECT_EQ(metrics::ConnectionType::kDisconnected,
36 GetConnectionType(ConnectionType::kDisconnected,
37 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080038 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070039 GetConnectionType(ConnectionType::kEthernet,
40 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080041 EXPECT_EQ(metrics::ConnectionType::kWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070042 GetConnectionType(ConnectionType::kWifi,
43 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080044 EXPECT_EQ(metrics::ConnectionType::kWimax,
Sen Jiang255e22b2016-05-20 16:15:29 -070045 GetConnectionType(ConnectionType::kWimax,
46 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080047 EXPECT_EQ(metrics::ConnectionType::kBluetooth,
Sen Jiang255e22b2016-05-20 16:15:29 -070048 GetConnectionType(ConnectionType::kBluetooth,
49 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080050 EXPECT_EQ(metrics::ConnectionType::kCellular,
Sen Jiang255e22b2016-05-20 16:15:29 -070051 GetConnectionType(ConnectionType::kCellular,
52 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080053 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070054 GetConnectionType(ConnectionType::kEthernet,
55 ConnectionTethering::kConfirmed));
Alex Deymo38429cf2015-11-11 18:27:22 -080056 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070057 GetConnectionType(ConnectionType::kWifi,
58 ConnectionTethering::kConfirmed));
Alex Deymo38429cf2015-11-11 18:27:22 -080059
60 // Ensure that we don't report tethered ethernet unless it's confirmed.
61 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070062 GetConnectionType(ConnectionType::kEthernet,
63 ConnectionTethering::kNotDetected));
Alex Deymo38429cf2015-11-11 18:27:22 -080064 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070065 GetConnectionType(ConnectionType::kEthernet,
66 ConnectionTethering::kSuspected));
Alex Deymo38429cf2015-11-11 18:27:22 -080067 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070068 GetConnectionType(ConnectionType::kEthernet,
69 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080070
71 // Ditto for tethered wifi.
72 EXPECT_EQ(metrics::ConnectionType::kWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070073 GetConnectionType(ConnectionType::kWifi,
74 ConnectionTethering::kNotDetected));
Alex Deymo38429cf2015-11-11 18:27:22 -080075 EXPECT_EQ(metrics::ConnectionType::kWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070076 GetConnectionType(ConnectionType::kWifi,
77 ConnectionTethering::kSuspected));
Alex Deymo38429cf2015-11-11 18:27:22 -080078 EXPECT_EQ(metrics::ConnectionType::kWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070079 GetConnectionType(ConnectionType::kWifi,
80 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080081}
82
Alex Deymoa2591792015-11-17 00:39:40 -030083TEST(MetricsUtilsTest, WallclockDurationHelper) {
84 FakeSystemState fake_system_state;
85 FakeClock fake_clock;
86 base::TimeDelta duration;
87 const std::string state_variable_key = "test-prefs";
88 FakePrefs fake_prefs;
89
90 fake_system_state.set_clock(&fake_clock);
91 fake_system_state.set_prefs(&fake_prefs);
92
93 // Initialize wallclock to 1 sec.
94 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
95
96 // First time called so no previous measurement available.
97 EXPECT_FALSE(metrics_utils::WallclockDurationHelper(&fake_system_state,
98 state_variable_key,
99 &duration));
100
101 // Next time, we should get zero since the clock didn't advance.
102 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
103 state_variable_key,
104 &duration));
105 EXPECT_EQ(duration.InSeconds(), 0);
106
107 // We can also call it as many times as we want with it being
108 // considered a failure.
109 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
110 state_variable_key,
111 &duration));
112 EXPECT_EQ(duration.InSeconds(), 0);
113 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
114 state_variable_key,
115 &duration));
116 EXPECT_EQ(duration.InSeconds(), 0);
117
118 // Advance the clock one second, then we should get 1 sec on the
119 // next call and 0 sec on the subsequent call.
120 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
121 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
122 state_variable_key,
123 &duration));
124 EXPECT_EQ(duration.InSeconds(), 1);
125 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
126 state_variable_key,
127 &duration));
128 EXPECT_EQ(duration.InSeconds(), 0);
129
130 // Advance clock two seconds and we should get 2 sec and then 0 sec.
131 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
132 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
133 state_variable_key,
134 &duration));
135 EXPECT_EQ(duration.InSeconds(), 2);
136 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
137 state_variable_key,
138 &duration));
139 EXPECT_EQ(duration.InSeconds(), 0);
140
141 // There's a possibility that the wallclock can go backwards (NTP
142 // adjustments, for example) so check that we properly handle this
143 // case.
144 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
145 EXPECT_FALSE(metrics_utils::WallclockDurationHelper(&fake_system_state,
146 state_variable_key,
147 &duration));
148 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
149 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(&fake_system_state,
150 state_variable_key,
151 &duration));
152 EXPECT_EQ(duration.InSeconds(), 1);
153}
154
155TEST(MetricsUtilsTest, MonotonicDurationHelper) {
156 int64_t storage = 0;
157 FakeSystemState fake_system_state;
158 FakeClock fake_clock;
159 base::TimeDelta duration;
160
161 fake_system_state.set_clock(&fake_clock);
162
163 // Initialize monotonic clock to 1 sec.
164 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
165
166 // First time called so no previous measurement available.
167 EXPECT_FALSE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
168 &storage,
169 &duration));
170
171 // Next time, we should get zero since the clock didn't advance.
172 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
173 &storage,
174 &duration));
175 EXPECT_EQ(duration.InSeconds(), 0);
176
177 // We can also call it as many times as we want with it being
178 // considered a failure.
179 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
180 &storage,
181 &duration));
182 EXPECT_EQ(duration.InSeconds(), 0);
183 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
184 &storage,
185 &duration));
186 EXPECT_EQ(duration.InSeconds(), 0);
187
188 // Advance the clock one second, then we should get 1 sec on the
189 // next call and 0 sec on the subsequent call.
190 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
191 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
192 &storage,
193 &duration));
194 EXPECT_EQ(duration.InSeconds(), 1);
195 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
196 &storage,
197 &duration));
198 EXPECT_EQ(duration.InSeconds(), 0);
199
200 // Advance clock two seconds and we should get 2 sec and then 0 sec.
201 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
202 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
203 &storage,
204 &duration));
205 EXPECT_EQ(duration.InSeconds(), 2);
206 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(&fake_system_state,
207 &storage,
208 &duration));
209 EXPECT_EQ(duration.InSeconds(), 0);
210}
211
Alex Deymo38429cf2015-11-11 18:27:22 -0800212} // namespace metrics_utils
213} // namespace chromeos_update_engine