-
Notifications
You must be signed in to change notification settings - Fork 19
/
connection_manager_unittest.cc
385 lines (314 loc) · 15 KB
/
connection_manager_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/connection_manager.h"
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <base/logging.h>
#include <brillo/any.h>
#include <brillo/message_loops/fake_message_loop.h>
#include <brillo/variant_dictionary.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <shill/dbus-constants.h>
#include <shill/dbus-proxies.h>
#include <shill/dbus-proxy-mocks.h>
#include "update_engine/common/test_utils.h"
#include "update_engine/fake_shill_proxy.h"
#include "update_engine/fake_system_state.h"
using chromeos_update_engine::connection_utils::StringForConnectionType;
using org::chromium::flimflam::ManagerProxyMock;
using org::chromium::flimflam::ServiceProxyMock;
using std::set;
using std::string;
using testing::_;
using testing::Return;
using testing::SetArgPointee;
namespace chromeos_update_engine {
class ConnectionManagerTest : public ::testing::Test {
public:
ConnectionManagerTest() : fake_shill_proxy_(new FakeShillProxy()) {}
void SetUp() override {
loop_.SetAsCurrent();
fake_system_state_.set_connection_manager(&cmut_);
}
void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
protected:
// Sets the default_service object path in the response from the
// ManagerProxyMock instance.
void SetManagerReply(const char* default_service, bool reply_succeeds);
// Sets the |service_type|, |physical_technology| and |service_tethering|
// properties in the mocked service |service_path|. If any of the three
// const char* is a nullptr, the corresponding property will not be included
// in the response.
void SetServiceReply(const string& service_path,
const char* service_type,
const char* physical_technology,
const char* service_tethering);
void TestWithServiceType(const char* service_type,
const char* physical_technology,
ConnectionType expected_type);
void TestWithServiceDisconnected(ConnectionType expected_type);
void TestWithServiceTethering(const char* service_tethering,
ConnectionTethering expected_tethering);
brillo::FakeMessageLoop loop_{nullptr};
FakeSystemState fake_system_state_;
FakeShillProxy* fake_shill_proxy_;
// ConnectionManager under test.
ConnectionManager cmut_{fake_shill_proxy_, &fake_system_state_};
};
void ConnectionManagerTest::SetManagerReply(const char* default_service,
bool reply_succeeds) {
ManagerProxyMock* manager_proxy_mock = fake_shill_proxy_->GetManagerProxy();
if (!reply_succeeds) {
EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
.WillOnce(Return(false));
return;
}
// Create a dictionary of properties and optionally include the default
// service.
brillo::VariantDictionary reply_dict;
reply_dict["SomeOtherProperty"] = 0xC0FFEE;
if (default_service) {
reply_dict[shill::kDefaultServiceProperty] =
dbus::ObjectPath(default_service);
}
EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
.WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
}
void ConnectionManagerTest::SetServiceReply(const string& service_path,
const char* service_type,
const char* physical_technology,
const char* service_tethering) {
brillo::VariantDictionary reply_dict;
reply_dict["SomeOtherProperty"] = 0xC0FFEE;
if (service_type)
reply_dict[shill::kTypeProperty] = string(service_type);
if (physical_technology) {
reply_dict[shill::kPhysicalTechnologyProperty] =
string(physical_technology);
}
if (service_tethering)
reply_dict[shill::kTetheringProperty] = string(service_tethering);
std::unique_ptr<ServiceProxyMock> service_proxy_mock(new ServiceProxyMock());
// Plumb return value into mock object.
EXPECT_CALL(*service_proxy_mock.get(), GetProperties(_, _, _))
.WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
fake_shill_proxy_->SetServiceForPath(dbus::ObjectPath(service_path),
std::move(service_proxy_mock));
}
void ConnectionManagerTest::TestWithServiceType(const char* service_type,
const char* physical_technology,
ConnectionType expected_type) {
SetManagerReply("/service/guest/network", true);
SetServiceReply("/service/guest/network",
service_type,
physical_technology,
shill::kTetheringNotDetectedState);
ConnectionType type;
ConnectionTethering tethering;
EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
EXPECT_EQ(expected_type, type);
testing::Mock::VerifyAndClearExpectations(
fake_shill_proxy_->GetManagerProxy());
}
void ConnectionManagerTest::TestWithServiceTethering(
const char* service_tethering, ConnectionTethering expected_tethering) {
SetManagerReply("/service/guest/network", true);
SetServiceReply(
"/service/guest/network", shill::kTypeWifi, nullptr, service_tethering);
ConnectionType type;
ConnectionTethering tethering;
EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
EXPECT_EQ(expected_tethering, tethering);
testing::Mock::VerifyAndClearExpectations(
fake_shill_proxy_->GetManagerProxy());
}
void ConnectionManagerTest::TestWithServiceDisconnected(
ConnectionType expected_type) {
SetManagerReply("/", true);
ConnectionType type;
ConnectionTethering tethering;
EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
EXPECT_EQ(expected_type, type);
testing::Mock::VerifyAndClearExpectations(
fake_shill_proxy_->GetManagerProxy());
}
TEST_F(ConnectionManagerTest, SimpleTest) {
TestWithServiceType(shill::kTypeEthernet, nullptr, ConnectionType::kEthernet);
TestWithServiceType(shill::kTypeWifi, nullptr, ConnectionType::kWifi);
TestWithServiceType(shill::kTypeWimax, nullptr, ConnectionType::kWimax);
TestWithServiceType(
shill::kTypeBluetooth, nullptr, ConnectionType::kBluetooth);
TestWithServiceType(shill::kTypeCellular, nullptr, ConnectionType::kCellular);
}
TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
TestWithServiceType(shill::kTypeVPN, nullptr, ConnectionType::kUnknown);
TestWithServiceType(
shill::kTypeVPN, shill::kTypeVPN, ConnectionType::kUnknown);
TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, ConnectionType::kWifi);
TestWithServiceType(
shill::kTypeVPN, shill::kTypeWimax, ConnectionType::kWimax);
}
TEST_F(ConnectionManagerTest, TetheringTest) {
TestWithServiceTethering(shill::kTetheringConfirmedState,
ConnectionTethering::kConfirmed);
TestWithServiceTethering(shill::kTetheringNotDetectedState,
ConnectionTethering::kNotDetected);
TestWithServiceTethering(shill::kTetheringSuspectedState,
ConnectionTethering::kSuspected);
TestWithServiceTethering("I'm not a valid property value =)",
ConnectionTethering::kUnknown);
}
TEST_F(ConnectionManagerTest, UnknownTest) {
TestWithServiceType("foo", nullptr, ConnectionType::kUnknown);
}
TEST_F(ConnectionManagerTest, DisconnectTest) {
TestWithServiceDisconnected(ConnectionType::kDisconnected);
}
TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
// Updates over Ethernet are allowed even if there's no policy.
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWimax,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kBluetooth,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
policy::MockDevicePolicy allow_3g_policy;
fake_system_state_.set_device_policy(&allow_3g_policy);
// This test tests cellular (3G) being the only connection type being allowed.
set<string> allowed_set;
allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
.Times(1)
.WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
policy::MockDevicePolicy allow_3g_policy;
fake_system_state_.set_device_policy(&allow_3g_policy);
// This test tests multiple connection types being allowed, with
// 3G one among them. Only Cellular is currently enforced by the policy
// setting, the others are ignored (see Bluetooth for example).
set<string> allowed_set;
allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
allowed_set.insert(StringForConnectionType(ConnectionType::kBluetooth));
EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
.Times(3)
.WillRepeatedly(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
ConnectionTethering::kUnknown));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
ConnectionTethering::kNotDetected));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
ConnectionTethering::kUnknown));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
ConnectionTethering::kUnknown));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWimax,
ConnectionTethering::kUnknown));
EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kBluetooth,
ConnectionTethering::kUnknown));
// Tethered networks are treated in the same way as Cellular networks and
// thus allowed.
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
ConnectionTethering::kConfirmed));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
ConnectionTethering::kConfirmed));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOverCellularByDefaultTest) {
policy::MockDevicePolicy device_policy;
// Set an empty device policy.
fake_system_state_.set_device_policy(&device_policy);
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOverTetheredNetworkByDefaultTest) {
policy::MockDevicePolicy device_policy;
// Set an empty device policy.
fake_system_state_.set_device_policy(&device_policy);
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
ConnectionTethering::kConfirmed));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
ConnectionTethering::kConfirmed));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
ConnectionTethering::kSuspected));
}
TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
policy::MockDevicePolicy block_3g_policy;
fake_system_state_.set_device_policy(&block_3g_policy);
// Test that updates for 3G are blocked while updates are allowed
// over several other types.
set<string> allowed_set;
allowed_set.insert(StringForConnectionType(ConnectionType::kEthernet));
allowed_set.insert(StringForConnectionType(ConnectionType::kWifi));
allowed_set.insert(StringForConnectionType(ConnectionType::kWimax));
EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
.Times(1)
.WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOver3GIfPolicyIsNotSet) {
policy::MockDevicePolicy device_policy;
fake_system_state_.set_device_policy(&device_policy);
// Return false for GetAllowedConnectionTypesForUpdate and see
// that updates are allowed as device policy is not set. Further
// check is left to |OmahaRequestAction|.
EXPECT_CALL(device_policy, GetAllowedConnectionTypesForUpdate(_))
.Times(1)
.WillOnce(Return(false));
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, AllowUpdatesOverCellularIfPolicyFailsToBeLoaded) {
fake_system_state_.set_device_policy(nullptr);
EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
ConnectionTethering::kUnknown));
}
TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
EXPECT_STREQ(shill::kTypeEthernet,
StringForConnectionType(ConnectionType::kEthernet));
EXPECT_STREQ(shill::kTypeWifi,
StringForConnectionType(ConnectionType::kWifi));
EXPECT_STREQ(shill::kTypeWimax,
StringForConnectionType(ConnectionType::kWimax));
EXPECT_STREQ(shill::kTypeBluetooth,
StringForConnectionType(ConnectionType::kBluetooth));
EXPECT_STREQ(shill::kTypeCellular,
StringForConnectionType(ConnectionType::kCellular));
EXPECT_STREQ("Unknown", StringForConnectionType(ConnectionType::kUnknown));
EXPECT_STREQ("Unknown",
StringForConnectionType(static_cast<ConnectionType>(999999)));
}
TEST_F(ConnectionManagerTest, MalformedServiceList) {
SetManagerReply("/service/guest/network", false);
ConnectionType type;
ConnectionTethering tethering;
EXPECT_FALSE(cmut_.GetConnectionProperties(&type, &tethering));
}
} // namespace chromeos_update_engine