blob: 6ea996fae664abfe7be74cfc9d8d94e13bb5af76 [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));
Amin Hassani7cc8bb02019-01-14 16:29:47 -080041 EXPECT_EQ(
42 metrics::ConnectionType::kWifi,
43 GetConnectionType(ConnectionType::kWifi, ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080044 EXPECT_EQ(metrics::ConnectionType::kCellular,
Sen Jiang255e22b2016-05-20 16:15:29 -070045 GetConnectionType(ConnectionType::kCellular,
46 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080047 EXPECT_EQ(metrics::ConnectionType::kTetheredEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070048 GetConnectionType(ConnectionType::kEthernet,
49 ConnectionTethering::kConfirmed));
Alex Deymo38429cf2015-11-11 18:27:22 -080050 EXPECT_EQ(metrics::ConnectionType::kTetheredWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070051 GetConnectionType(ConnectionType::kWifi,
52 ConnectionTethering::kConfirmed));
Alex Deymo38429cf2015-11-11 18:27:22 -080053
54 // Ensure that we don't report tethered ethernet unless it's confirmed.
55 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070056 GetConnectionType(ConnectionType::kEthernet,
57 ConnectionTethering::kNotDetected));
Alex Deymo38429cf2015-11-11 18:27:22 -080058 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070059 GetConnectionType(ConnectionType::kEthernet,
60 ConnectionTethering::kSuspected));
Alex Deymo38429cf2015-11-11 18:27:22 -080061 EXPECT_EQ(metrics::ConnectionType::kEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -070062 GetConnectionType(ConnectionType::kEthernet,
63 ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080064
65 // Ditto for tethered wifi.
66 EXPECT_EQ(metrics::ConnectionType::kWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070067 GetConnectionType(ConnectionType::kWifi,
68 ConnectionTethering::kNotDetected));
Alex Deymo38429cf2015-11-11 18:27:22 -080069 EXPECT_EQ(metrics::ConnectionType::kWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -070070 GetConnectionType(ConnectionType::kWifi,
71 ConnectionTethering::kSuspected));
Amin Hassani7cc8bb02019-01-14 16:29:47 -080072 EXPECT_EQ(
73 metrics::ConnectionType::kWifi,
74 GetConnectionType(ConnectionType::kWifi, ConnectionTethering::kUnknown));
Alex Deymo38429cf2015-11-11 18:27:22 -080075}
76
Alex Deymoa2591792015-11-17 00:39:40 -030077TEST(MetricsUtilsTest, WallclockDurationHelper) {
78 FakeSystemState fake_system_state;
79 FakeClock fake_clock;
80 base::TimeDelta duration;
81 const std::string state_variable_key = "test-prefs";
82 FakePrefs fake_prefs;
83
84 fake_system_state.set_clock(&fake_clock);
85 fake_system_state.set_prefs(&fake_prefs);
86
87 // Initialize wallclock to 1 sec.
88 fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000));
89
90 // First time called so no previous measurement available.
Amin Hassani7cc8bb02019-01-14 16:29:47 -080091 EXPECT_FALSE(metrics_utils::WallclockDurationHelper(
92 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -030093
94 // Next time, we should get zero since the clock didn't advance.
Amin Hassani7cc8bb02019-01-14 16:29:47 -080095 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
96 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -030097 EXPECT_EQ(duration.InSeconds(), 0);
98
99 // We can also call it as many times as we want with it being
100 // considered a failure.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800101 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
102 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300103 EXPECT_EQ(duration.InSeconds(), 0);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800104 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
105 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300106 EXPECT_EQ(duration.InSeconds(), 0);
107
108 // Advance the clock one second, then we should get 1 sec on the
109 // next call and 0 sec on the subsequent call.
110 fake_clock.SetWallclockTime(base::Time::FromInternalValue(2000000));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800111 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
112 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300113 EXPECT_EQ(duration.InSeconds(), 1);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800114 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
115 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300116 EXPECT_EQ(duration.InSeconds(), 0);
117
118 // Advance clock two seconds and we should get 2 sec and then 0 sec.
119 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800120 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
121 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300122 EXPECT_EQ(duration.InSeconds(), 2);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800123 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
124 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300125 EXPECT_EQ(duration.InSeconds(), 0);
126
127 // There's a possibility that the wallclock can go backwards (NTP
128 // adjustments, for example) so check that we properly handle this
129 // case.
130 fake_clock.SetWallclockTime(base::Time::FromInternalValue(3000000));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800131 EXPECT_FALSE(metrics_utils::WallclockDurationHelper(
132 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300133 fake_clock.SetWallclockTime(base::Time::FromInternalValue(4000000));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800134 EXPECT_TRUE(metrics_utils::WallclockDurationHelper(
135 &fake_system_state, state_variable_key, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300136 EXPECT_EQ(duration.InSeconds(), 1);
137}
138
139TEST(MetricsUtilsTest, MonotonicDurationHelper) {
140 int64_t storage = 0;
141 FakeSystemState fake_system_state;
142 FakeClock fake_clock;
143 base::TimeDelta duration;
144
145 fake_system_state.set_clock(&fake_clock);
146
147 // Initialize monotonic clock to 1 sec.
148 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000));
149
150 // First time called so no previous measurement available.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800151 EXPECT_FALSE(metrics_utils::MonotonicDurationHelper(
152 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300153
154 // Next time, we should get zero since the clock didn't advance.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800155 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
156 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300157 EXPECT_EQ(duration.InSeconds(), 0);
158
159 // We can also call it as many times as we want with it being
160 // considered a failure.
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800161 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
162 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300163 EXPECT_EQ(duration.InSeconds(), 0);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800164 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
165 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300166 EXPECT_EQ(duration.InSeconds(), 0);
167
168 // Advance the clock one second, then we should get 1 sec on the
169 // next call and 0 sec on the subsequent call.
170 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(2000000));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800171 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
172 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300173 EXPECT_EQ(duration.InSeconds(), 1);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800174 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
175 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300176 EXPECT_EQ(duration.InSeconds(), 0);
177
178 // Advance clock two seconds and we should get 2 sec and then 0 sec.
179 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(4000000));
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800180 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
181 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300182 EXPECT_EQ(duration.InSeconds(), 2);
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800183 EXPECT_TRUE(metrics_utils::MonotonicDurationHelper(
184 &fake_system_state, &storage, &duration));
Alex Deymoa2591792015-11-17 00:39:40 -0300185 EXPECT_EQ(duration.InSeconds(), 0);
186}
187
Alex Deymo38429cf2015-11-11 18:27:22 -0800188} // namespace metrics_utils
189} // namespace chromeos_update_engine