blob: 576d24db86ba0dca37820e5dbe09e5cb861e5489 [file] [log] [blame]
Jay Srinivasan43488792012-06-19 00:25:31 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo8427b4a2014-11-05 14:00:32 -08005#include "update_engine/connection_manager.h"
6
Alex Vakulenkod2779df2014-06-16 13:19:00 -07007#include <set>
8#include <string>
9
Jay Srinivasan43488792012-06-19 00:25:31 -070010#include <base/logging.h>
11#include <chromeos/dbus/service_constants.h>
Alex Deymo5665d0c2014-05-28 17:45:43 -070012#include <gmock/gmock.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070013#include <gtest/gtest.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070014
Gilad Arnold5bb4c902014-04-10 12:32:13 -070015#include "update_engine/fake_system_state.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080016#include "update_engine/mock_dbus_wrapper.h"
Alex Deymo5665d0c2014-05-28 17:45:43 -070017#include "update_engine/test_utils.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070018
19using std::set;
20using std::string;
Jay Srinivasan43488792012-06-19 00:25:31 -070021using testing::AnyNumber;
22using testing::Return;
23using testing::SetArgumentPointee;
24using testing::StrEq;
Alex Deymof329b932014-10-30 01:37:48 -070025using testing::_;
Jay Srinivasan43488792012-06-19 00:25:31 -070026
27namespace chromeos_update_engine {
28
29class ConnectionManagerTest : public ::testing::Test {
30 public:
31 ConnectionManagerTest()
Alex Vakulenko88b591f2014-08-28 16:48:57 -070032 : kMockFlimFlamManagerProxy_(nullptr),
33 kMockFlimFlamServiceProxy_(nullptr),
34 kServicePath_(nullptr),
Gilad Arnold5bb4c902014-04-10 12:32:13 -070035 cmut_(&fake_system_state_) {
36 fake_system_state_.set_connection_manager(&cmut_);
Jay Srinivasan43488792012-06-19 00:25:31 -070037 }
38
39 protected:
40 void SetupMocks(const char* service_path);
Alex Deymo5665d0c2014-05-28 17:45:43 -070041 void SetManagerReply(const char* reply_value, const GType& reply_type);
Alex Deymo1c4e6382013-07-15 12:09:51 -070042
43 // Sets the |service_type| Type and the |physical_technology|
Alex Vakulenko88b591f2014-08-28 16:48:57 -070044 // PhysicalTechnology properties in the mocked service. If a null
Alex Deymo1c4e6382013-07-15 12:09:51 -070045 // |physical_technology| is passed, the property is not set (not present).
46 void SetServiceReply(const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -070047 const char* physical_technology,
48 const char* service_tethering);
Jay Srinivasan43488792012-06-19 00:25:31 -070049 void TestWithServiceType(
Alex Deymo1c4e6382013-07-15 12:09:51 -070050 const char* service_type,
51 const char* physical_technology,
52 NetworkConnectionType expected_type);
Alex Deymo6ae91202014-03-10 19:21:25 -070053 void TestWithServiceTethering(
54 const char* service_tethering,
55 NetworkTethering expected_tethering);
Jay Srinivasan43488792012-06-19 00:25:31 -070056
57 static const char* kGetPropertiesMethod;
58 DBusGProxy* kMockFlimFlamManagerProxy_;
59 DBusGProxy* kMockFlimFlamServiceProxy_;
60 DBusGConnection* kMockSystemBus_;
61 const char* kServicePath_;
Alex Deymo5665d0c2014-05-28 17:45:43 -070062 testing::StrictMock<MockDBusWrapper> dbus_iface_;
Jay Srinivasan43488792012-06-19 00:25:31 -070063 ConnectionManager cmut_; // ConnectionManager under test.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070064 FakeSystemState fake_system_state_;
Jay Srinivasan43488792012-06-19 00:25:31 -070065};
66
67// static
68const char* ConnectionManagerTest::kGetPropertiesMethod = "GetProperties";
69
70void ConnectionManagerTest::SetupMocks(const char* service_path) {
71 int number = 1;
72 kMockSystemBus_ = reinterpret_cast<DBusGConnection*>(number++);
73 kMockFlimFlamManagerProxy_ = reinterpret_cast<DBusGProxy*>(number++);
74 kMockFlimFlamServiceProxy_ = reinterpret_cast<DBusGProxy*>(number++);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070075 ASSERT_NE(kMockSystemBus_, static_cast<DBusGConnection*>(nullptr));
Jay Srinivasan43488792012-06-19 00:25:31 -070076
77 kServicePath_ = service_path;
78
79 ON_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
80 .WillByDefault(Return(kMockSystemBus_));
81 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
82 .Times(AnyNumber());
83}
84
Alex Deymo5665d0c2014-05-28 17:45:43 -070085void ConnectionManagerTest::SetManagerReply(const char *reply_value,
Jay Srinivasan43488792012-06-19 00:25:31 -070086 const GType& reply_type) {
Alex Deymo5665d0c2014-05-28 17:45:43 -070087 ASSERT_TRUE(dbus_g_type_is_collection(reply_type));
Jay Srinivasan43488792012-06-19 00:25:31 -070088
Alex Deymo5665d0c2014-05-28 17:45:43 -070089 // Create the GPtrArray array holding the |reply_value| pointer. The
90 // |reply_value| string is duplicated because it should be mutable on the
91 // interface and is because dbus-glib collections will g_free() each element
92 // of the GPtrArray automatically when the |array_as_value| GValue is unset.
93 // The g_strdup() is not being leaked.
94 GPtrArray* array = g_ptr_array_new();
Alex Vakulenko88b591f2014-08-28 16:48:57 -070095 ASSERT_NE(nullptr, array);
Alex Deymo5665d0c2014-05-28 17:45:43 -070096 g_ptr_array_add(array, g_strdup(reply_value));
Jay Srinivasan43488792012-06-19 00:25:31 -070097
Jay Srinivasan43488792012-06-19 00:25:31 -070098 GValue* array_as_value = g_new0(GValue, 1);
99 EXPECT_EQ(array_as_value, g_value_init(array_as_value, reply_type));
100 g_value_take_boxed(array_as_value, array);
Alex Deymo5665d0c2014-05-28 17:45:43 -0700101
102 // Initialize return value for D-Bus call to Manager object, which is a
103 // hash table of static strings (char*) in GValue* containing a single array.
104 GHashTable* manager_hash_table = g_hash_table_new_full(
105 g_str_hash, g_str_equal,
106 nullptr, // no key_destroy_func because keys are static.
Alex Deymo10875d92014-11-10 21:52:57 -0800107 test_utils::GValueFree); // value_destroy_func
Jay Srinivasan43488792012-06-19 00:25:31 -0700108 g_hash_table_insert(manager_hash_table,
109 const_cast<char*>("Services"),
110 array_as_value);
111
112 // Plumb return value into mock object.
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800113 EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamManagerProxy_,
114 StrEq(kGetPropertiesMethod),
115 _, _))
116 .WillOnce(DoAll(SetArgumentPointee<3>(manager_hash_table), Return(TRUE)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700117
118 // Set other expectations.
119 EXPECT_CALL(dbus_iface_,
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800120 ProxyNewForName(kMockSystemBus_,
121 StrEq(shill::kFlimflamServiceName),
122 StrEq(shill::kFlimflamServicePath),
123 StrEq(shill::kFlimflamManagerInterface)))
Jay Srinivasan43488792012-06-19 00:25:31 -0700124 .WillOnce(Return(kMockFlimFlamManagerProxy_));
125 EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamManagerProxy_));
126 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
127 .RetiresOnSaturation();
128}
129
Alex Deymo1c4e6382013-07-15 12:09:51 -0700130void ConnectionManagerTest::SetServiceReply(const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -0700131 const char* physical_technology,
132 const char* service_tethering) {
Alex Deymo5665d0c2014-05-28 17:45:43 -0700133 // Initialize return value for D-Bus call to Service object, which is a
134 // hash table of static strings (char*) in GValue*.
135 GHashTable* service_hash_table = g_hash_table_new_full(
136 g_str_hash, g_str_equal,
137 nullptr, // no key_destroy_func because keys are static.
Alex Deymo10875d92014-11-10 21:52:57 -0800138 test_utils::GValueFree); // value_destroy_func
139 GValue* service_type_value = test_utils::GValueNewString(service_type);
Jay Srinivasan43488792012-06-19 00:25:31 -0700140 g_hash_table_insert(service_hash_table,
141 const_cast<char*>("Type"),
142 service_type_value);
143
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700144 if (physical_technology) {
Alex Deymo10875d92014-11-10 21:52:57 -0800145 GValue* physical_technology_value =
146 test_utils::GValueNewString(physical_technology);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700147 g_hash_table_insert(service_hash_table,
148 const_cast<char*>("PhysicalTechnology"),
149 physical_technology_value);
150 }
151
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700152 if (service_tethering) {
Alex Deymo10875d92014-11-10 21:52:57 -0800153 GValue* service_tethering_value =
154 test_utils::GValueNewString(service_tethering);
Alex Deymo6ae91202014-03-10 19:21:25 -0700155 g_hash_table_insert(service_hash_table,
156 const_cast<char*>("Tethering"),
157 service_tethering_value);
158 }
159
Jay Srinivasan43488792012-06-19 00:25:31 -0700160 // Plumb return value into mock object.
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800161 EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamServiceProxy_,
162 StrEq(kGetPropertiesMethod),
163 _, _))
164 .WillOnce(DoAll(SetArgumentPointee<3>(service_hash_table), Return(TRUE)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700165
166 // Set other expectations.
167 EXPECT_CALL(dbus_iface_,
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800168 ProxyNewForName(kMockSystemBus_,
169 StrEq(shill::kFlimflamServiceName),
170 StrEq(kServicePath_),
171 StrEq(shill::kFlimflamServiceInterface)))
Jay Srinivasan43488792012-06-19 00:25:31 -0700172 .WillOnce(Return(kMockFlimFlamServiceProxy_));
173 EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamServiceProxy_));
174 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
175 .RetiresOnSaturation();
176}
177
178void ConnectionManagerTest::TestWithServiceType(
179 const char* service_type,
Alex Deymo1c4e6382013-07-15 12:09:51 -0700180 const char* physical_technology,
Jay Srinivasan43488792012-06-19 00:25:31 -0700181 NetworkConnectionType expected_type) {
182
183 SetupMocks("/service/guest-network");
184 SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY);
Alex Deymo6ae91202014-03-10 19:21:25 -0700185 SetServiceReply(service_type, physical_technology,
186 shill::kTetheringNotDetectedState);
Jay Srinivasan43488792012-06-19 00:25:31 -0700187
188 NetworkConnectionType type;
Alex Deymo6ae91202014-03-10 19:21:25 -0700189 NetworkTethering tethering;
190 EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700191 EXPECT_EQ(expected_type, type);
Alex Deymo5665d0c2014-05-28 17:45:43 -0700192 testing::Mock::VerifyAndClearExpectations(&dbus_iface_);
Jay Srinivasan43488792012-06-19 00:25:31 -0700193}
194
Alex Deymo6ae91202014-03-10 19:21:25 -0700195void ConnectionManagerTest::TestWithServiceTethering(
196 const char* service_tethering,
197 NetworkTethering expected_tethering) {
198
199 SetupMocks("/service/guest-network");
200 SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700201 SetServiceReply(shill::kTypeWifi, nullptr, service_tethering);
Alex Deymo6ae91202014-03-10 19:21:25 -0700202
203 NetworkConnectionType type;
204 NetworkTethering tethering;
205 EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering));
206 EXPECT_EQ(expected_tethering, tethering);
207}
208
Jay Srinivasan43488792012-06-19 00:25:31 -0700209TEST_F(ConnectionManagerTest, SimpleTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700210 TestWithServiceType(shill::kTypeEthernet, nullptr, kNetEthernet);
211 TestWithServiceType(shill::kTypeWifi, nullptr, kNetWifi);
212 TestWithServiceType(shill::kTypeWimax, nullptr, kNetWimax);
213 TestWithServiceType(shill::kTypeBluetooth, nullptr, kNetBluetooth);
214 TestWithServiceType(shill::kTypeCellular, nullptr, kNetCellular);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700215}
216
217TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700218 TestWithServiceType(shill::kTypeVPN, nullptr, kNetUnknown);
Ben Chanc6007e42013-09-19 23:49:22 -0700219 TestWithServiceType(shill::kTypeVPN, shill::kTypeVPN, kNetUnknown);
220 TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, kNetWifi);
221 TestWithServiceType(shill::kTypeVPN, shill::kTypeWimax, kNetWimax);
Jay Srinivasan43488792012-06-19 00:25:31 -0700222}
223
Alex Deymo6ae91202014-03-10 19:21:25 -0700224TEST_F(ConnectionManagerTest, TetheringTest) {
225 TestWithServiceTethering(shill::kTetheringConfirmedState,
226 NetworkTethering::kConfirmed);
227 TestWithServiceTethering(shill::kTetheringNotDetectedState,
228 NetworkTethering::kNotDetected);
229 TestWithServiceTethering(shill::kTetheringSuspectedState,
230 NetworkTethering::kSuspected);
231 TestWithServiceTethering("I'm not a valid property value =)",
232 NetworkTethering::kUnknown);
233}
234
Jay Srinivasan43488792012-06-19 00:25:31 -0700235TEST_F(ConnectionManagerTest, UnknownTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700236 TestWithServiceType("foo", nullptr, kNetUnknown);
Jay Srinivasan43488792012-06-19 00:25:31 -0700237}
238
239TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700240 // Updates over Ethernet are allowed even if there's no policy.
Alex Deymo6ae91202014-03-10 19:21:25 -0700241 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
242 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700243}
244
245TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700246 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700247}
248
249TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700250 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax,
251 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700252}
253
254TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700255 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth,
256 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700257}
258
259TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
260 policy::MockDevicePolicy allow_3g_policy;
261
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700262 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700263
Alex Deymof4867c42013-06-28 14:41:39 -0700264 // This test tests cellular (3G) being the only connection type being allowed.
Jay Srinivasan43488792012-06-19 00:25:31 -0700265 set<string> allowed_set;
266 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
267
268 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
269 .Times(1)
270 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
271
Alex Deymo6ae91202014-03-10 19:21:25 -0700272 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular,
273 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700274}
275
276TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
277 policy::MockDevicePolicy allow_3g_policy;
278
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700279 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700280
281 // This test tests multiple connection types being allowed, with
Alex Deymof4867c42013-06-28 14:41:39 -0700282 // 3G one among them. Only Cellular is currently enforced by the policy
283 // setting, the others are ignored (see Bluetooth for example).
Jay Srinivasan43488792012-06-19 00:25:31 -0700284 set<string> allowed_set;
Jay Srinivasan43488792012-06-19 00:25:31 -0700285 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
Alex Deymof4867c42013-06-28 14:41:39 -0700286 allowed_set.insert(cmut_.StringForConnectionType(kNetBluetooth));
Jay Srinivasan43488792012-06-19 00:25:31 -0700287
288 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
Alex Deymo6ae91202014-03-10 19:21:25 -0700289 .Times(3)
290 .WillRepeatedly(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700291
Alex Deymo6ae91202014-03-10 19:21:25 -0700292 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
293 NetworkTethering::kUnknown));
294 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
295 NetworkTethering::kNotDetected));
296 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular,
297 NetworkTethering::kUnknown));
298 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown));
299 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax, NetworkTethering::kUnknown));
300 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth,
301 NetworkTethering::kUnknown));
302
303 // Tethered networks are treated in the same way as Cellular networks and
304 // thus allowed.
305 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
306 NetworkTethering::kConfirmed));
307 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi,
308 NetworkTethering::kConfirmed));
Jay Srinivasan43488792012-06-19 00:25:31 -0700309}
310
Alex Deymof4867c42013-06-28 14:41:39 -0700311TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700312 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
313 NetworkTethering::kUnknown));
314}
315
316TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700317 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetWifi,
318 NetworkTethering::kConfirmed));
319 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetEthernet,
320 NetworkTethering::kConfirmed));
321 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi,
322 NetworkTethering::kSuspected));
Jay Srinivasan43488792012-06-19 00:25:31 -0700323}
324
325TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
326 policy::MockDevicePolicy block_3g_policy;
327
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700328 fake_system_state_.set_device_policy(&block_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700329
330 // Test that updates for 3G are blocked while updates are allowed
331 // over several other types.
332 set<string> allowed_set;
333 allowed_set.insert(cmut_.StringForConnectionType(kNetEthernet));
334 allowed_set.insert(cmut_.StringForConnectionType(kNetWifi));
335 allowed_set.insert(cmut_.StringForConnectionType(kNetWimax));
336
337 EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
338 .Times(1)
339 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
340
Alex Deymo6ae91202014-03-10 19:21:25 -0700341 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
342 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700343}
344
345TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) {
346 policy::MockDevicePolicy allow_3g_policy;
347
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700348 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700349
350 set<string> allowed_set;
351 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
352
353 // Return false for GetAllowedConnectionTypesForUpdate and see
354 // that updates are still blocked for 3G despite the value being in
355 // the string set above.
356 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
357 .Times(1)
358 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(false)));
359
Alex Deymo6ae91202014-03-10 19:21:25 -0700360 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
361 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700362}
363
Alex Deymof4867c42013-06-28 14:41:39 -0700364TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) {
365 policy::MockDevicePolicy no_policy;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800366 testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs();
Alex Deymof4867c42013-06-28 14:41:39 -0700367
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700368 fake_system_state_.set_device_policy(&no_policy);
Alex Deymof4867c42013-06-28 14:41:39 -0700369
370 // No setting enforced by the device policy, user prefs should be used.
371 EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_))
372 .Times(3)
373 .WillRepeatedly(Return(false));
374
375 // No user pref: block.
376 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
377 .Times(1)
378 .WillOnce(Return(false));
Alex Deymo6ae91202014-03-10 19:21:25 -0700379 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
380 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700381
382 // Allow per user pref.
383 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
384 .Times(1)
385 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700386 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700387 .Times(1)
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700388 .WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true)));
Alex Deymo6ae91202014-03-10 19:21:25 -0700389 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular,
390 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700391
392 // Block per user pref.
393 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
394 .Times(1)
395 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700396 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700397 .Times(1)
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700398 .WillOnce(DoAll(SetArgumentPointee<1>(false), Return(true)));
Alex Deymo6ae91202014-03-10 19:21:25 -0700399 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
400 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700401}
402
Jay Srinivasan43488792012-06-19 00:25:31 -0700403TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
Ben Chanc6007e42013-09-19 23:49:22 -0700404 EXPECT_STREQ(shill::kTypeEthernet,
Jay Srinivasan43488792012-06-19 00:25:31 -0700405 cmut_.StringForConnectionType(kNetEthernet));
Ben Chanc6007e42013-09-19 23:49:22 -0700406 EXPECT_STREQ(shill::kTypeWifi,
Jay Srinivasan43488792012-06-19 00:25:31 -0700407 cmut_.StringForConnectionType(kNetWifi));
Ben Chanc6007e42013-09-19 23:49:22 -0700408 EXPECT_STREQ(shill::kTypeWimax,
Jay Srinivasan43488792012-06-19 00:25:31 -0700409 cmut_.StringForConnectionType(kNetWimax));
Ben Chanc6007e42013-09-19 23:49:22 -0700410 EXPECT_STREQ(shill::kTypeBluetooth,
Jay Srinivasan43488792012-06-19 00:25:31 -0700411 cmut_.StringForConnectionType(kNetBluetooth));
Ben Chanc6007e42013-09-19 23:49:22 -0700412 EXPECT_STREQ(shill::kTypeCellular,
Jay Srinivasan43488792012-06-19 00:25:31 -0700413 cmut_.StringForConnectionType(kNetCellular));
414 EXPECT_STREQ("Unknown",
415 cmut_.StringForConnectionType(kNetUnknown));
416 EXPECT_STREQ("Unknown",
417 cmut_.StringForConnectionType(
418 static_cast<NetworkConnectionType>(999999)));
419}
420
421TEST_F(ConnectionManagerTest, MalformedServiceList) {
422 SetupMocks("/service/guest-network");
Alex Deymo5665d0c2014-05-28 17:45:43 -0700423 SetManagerReply(kServicePath_, DBUS_TYPE_G_STRING_ARRAY);
Jay Srinivasan43488792012-06-19 00:25:31 -0700424
425 NetworkConnectionType type;
Alex Deymo6ae91202014-03-10 19:21:25 -0700426 NetworkTethering tethering;
427 EXPECT_FALSE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700428}
429
430} // namespace chromeos_update_engine