blob: 8ca1a003ea848f8db36c754b36d4014b60d2fe00 [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;
David Pursell02c18642014-11-06 11:26:11 -080021using testing::A;
Jay Srinivasan43488792012-06-19 00:25:31 -070022using testing::AnyNumber;
23using testing::Return;
24using testing::SetArgumentPointee;
25using testing::StrEq;
Alex Deymof329b932014-10-30 01:37:48 -070026using testing::_;
Jay Srinivasan43488792012-06-19 00:25:31 -070027
28namespace chromeos_update_engine {
29
30class ConnectionManagerTest : public ::testing::Test {
31 public:
32 ConnectionManagerTest()
Alex Vakulenko88b591f2014-08-28 16:48:57 -070033 : kMockFlimFlamManagerProxy_(nullptr),
34 kMockFlimFlamServiceProxy_(nullptr),
35 kServicePath_(nullptr),
Gilad Arnold5bb4c902014-04-10 12:32:13 -070036 cmut_(&fake_system_state_) {
37 fake_system_state_.set_connection_manager(&cmut_);
Jay Srinivasan43488792012-06-19 00:25:31 -070038 }
39
40 protected:
41 void SetupMocks(const char* service_path);
Alex Deymo5665d0c2014-05-28 17:45:43 -070042 void SetManagerReply(const char* reply_value, const GType& reply_type);
Alex Deymo1c4e6382013-07-15 12:09:51 -070043
44 // Sets the |service_type| Type and the |physical_technology|
Alex Vakulenko88b591f2014-08-28 16:48:57 -070045 // PhysicalTechnology properties in the mocked service. If a null
Alex Deymo1c4e6382013-07-15 12:09:51 -070046 // |physical_technology| is passed, the property is not set (not present).
47 void SetServiceReply(const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -070048 const char* physical_technology,
49 const char* service_tethering);
Jay Srinivasan43488792012-06-19 00:25:31 -070050 void TestWithServiceType(
Alex Deymo1c4e6382013-07-15 12:09:51 -070051 const char* service_type,
52 const char* physical_technology,
53 NetworkConnectionType expected_type);
Alex Deymo6ae91202014-03-10 19:21:25 -070054 void TestWithServiceTethering(
55 const char* service_tethering,
56 NetworkTethering expected_tethering);
Jay Srinivasan43488792012-06-19 00:25:31 -070057
58 static const char* kGetPropertiesMethod;
59 DBusGProxy* kMockFlimFlamManagerProxy_;
60 DBusGProxy* kMockFlimFlamServiceProxy_;
61 DBusGConnection* kMockSystemBus_;
62 const char* kServicePath_;
Alex Deymo5665d0c2014-05-28 17:45:43 -070063 testing::StrictMock<MockDBusWrapper> dbus_iface_;
Jay Srinivasan43488792012-06-19 00:25:31 -070064 ConnectionManager cmut_; // ConnectionManager under test.
Gilad Arnold5bb4c902014-04-10 12:32:13 -070065 FakeSystemState fake_system_state_;
Jay Srinivasan43488792012-06-19 00:25:31 -070066};
67
68// static
69const char* ConnectionManagerTest::kGetPropertiesMethod = "GetProperties";
70
71void ConnectionManagerTest::SetupMocks(const char* service_path) {
72 int number = 1;
73 kMockSystemBus_ = reinterpret_cast<DBusGConnection*>(number++);
74 kMockFlimFlamManagerProxy_ = reinterpret_cast<DBusGProxy*>(number++);
75 kMockFlimFlamServiceProxy_ = reinterpret_cast<DBusGProxy*>(number++);
Alex Vakulenko88b591f2014-08-28 16:48:57 -070076 ASSERT_NE(kMockSystemBus_, static_cast<DBusGConnection*>(nullptr));
Jay Srinivasan43488792012-06-19 00:25:31 -070077
78 kServicePath_ = service_path;
79
80 ON_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
81 .WillByDefault(Return(kMockSystemBus_));
82 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
83 .Times(AnyNumber());
84}
85
Alex Deymo5665d0c2014-05-28 17:45:43 -070086void ConnectionManagerTest::SetManagerReply(const char *reply_value,
Jay Srinivasan43488792012-06-19 00:25:31 -070087 const GType& reply_type) {
Alex Deymo5665d0c2014-05-28 17:45:43 -070088 ASSERT_TRUE(dbus_g_type_is_collection(reply_type));
Jay Srinivasan43488792012-06-19 00:25:31 -070089
Alex Deymo5665d0c2014-05-28 17:45:43 -070090 // Create the GPtrArray array holding the |reply_value| pointer. The
91 // |reply_value| string is duplicated because it should be mutable on the
92 // interface and is because dbus-glib collections will g_free() each element
93 // of the GPtrArray automatically when the |array_as_value| GValue is unset.
94 // The g_strdup() is not being leaked.
95 GPtrArray* array = g_ptr_array_new();
Alex Vakulenko88b591f2014-08-28 16:48:57 -070096 ASSERT_NE(nullptr, array);
Alex Deymo5665d0c2014-05-28 17:45:43 -070097 g_ptr_array_add(array, g_strdup(reply_value));
Jay Srinivasan43488792012-06-19 00:25:31 -070098
Jay Srinivasan43488792012-06-19 00:25:31 -070099 GValue* array_as_value = g_new0(GValue, 1);
100 EXPECT_EQ(array_as_value, g_value_init(array_as_value, reply_type));
101 g_value_take_boxed(array_as_value, array);
Alex Deymo5665d0c2014-05-28 17:45:43 -0700102
103 // Initialize return value for D-Bus call to Manager object, which is a
104 // hash table of static strings (char*) in GValue* containing a single array.
105 GHashTable* manager_hash_table = g_hash_table_new_full(
106 g_str_hash, g_str_equal,
107 nullptr, // no key_destroy_func because keys are static.
Alex Deymo10875d92014-11-10 21:52:57 -0800108 test_utils::GValueFree); // value_destroy_func
Jay Srinivasan43488792012-06-19 00:25:31 -0700109 g_hash_table_insert(manager_hash_table,
110 const_cast<char*>("Services"),
111 array_as_value);
112
113 // Plumb return value into mock object.
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800114 EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamManagerProxy_,
115 StrEq(kGetPropertiesMethod),
David Pursell02c18642014-11-06 11:26:11 -0800116 _, A<GHashTable**>()))
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800117 .WillOnce(DoAll(SetArgumentPointee<3>(manager_hash_table), Return(TRUE)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700118
119 // Set other expectations.
120 EXPECT_CALL(dbus_iface_,
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800121 ProxyNewForName(kMockSystemBus_,
122 StrEq(shill::kFlimflamServiceName),
123 StrEq(shill::kFlimflamServicePath),
124 StrEq(shill::kFlimflamManagerInterface)))
Jay Srinivasan43488792012-06-19 00:25:31 -0700125 .WillOnce(Return(kMockFlimFlamManagerProxy_));
126 EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamManagerProxy_));
127 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
128 .RetiresOnSaturation();
129}
130
Alex Deymo1c4e6382013-07-15 12:09:51 -0700131void ConnectionManagerTest::SetServiceReply(const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -0700132 const char* physical_technology,
133 const char* service_tethering) {
Alex Deymo5665d0c2014-05-28 17:45:43 -0700134 // Initialize return value for D-Bus call to Service object, which is a
135 // hash table of static strings (char*) in GValue*.
136 GHashTable* service_hash_table = g_hash_table_new_full(
137 g_str_hash, g_str_equal,
138 nullptr, // no key_destroy_func because keys are static.
Alex Deymo10875d92014-11-10 21:52:57 -0800139 test_utils::GValueFree); // value_destroy_func
140 GValue* service_type_value = test_utils::GValueNewString(service_type);
Jay Srinivasan43488792012-06-19 00:25:31 -0700141 g_hash_table_insert(service_hash_table,
142 const_cast<char*>("Type"),
143 service_type_value);
144
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700145 if (physical_technology) {
Alex Deymo10875d92014-11-10 21:52:57 -0800146 GValue* physical_technology_value =
147 test_utils::GValueNewString(physical_technology);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700148 g_hash_table_insert(service_hash_table,
149 const_cast<char*>("PhysicalTechnology"),
150 physical_technology_value);
151 }
152
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700153 if (service_tethering) {
Alex Deymo10875d92014-11-10 21:52:57 -0800154 GValue* service_tethering_value =
155 test_utils::GValueNewString(service_tethering);
Alex Deymo6ae91202014-03-10 19:21:25 -0700156 g_hash_table_insert(service_hash_table,
157 const_cast<char*>("Tethering"),
158 service_tethering_value);
159 }
160
Jay Srinivasan43488792012-06-19 00:25:31 -0700161 // Plumb return value into mock object.
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800162 EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamServiceProxy_,
163 StrEq(kGetPropertiesMethod),
David Pursell02c18642014-11-06 11:26:11 -0800164 _, A<GHashTable**>()))
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800165 .WillOnce(DoAll(SetArgumentPointee<3>(service_hash_table), Return(TRUE)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700166
167 // Set other expectations.
168 EXPECT_CALL(dbus_iface_,
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800169 ProxyNewForName(kMockSystemBus_,
170 StrEq(shill::kFlimflamServiceName),
171 StrEq(kServicePath_),
172 StrEq(shill::kFlimflamServiceInterface)))
Jay Srinivasan43488792012-06-19 00:25:31 -0700173 .WillOnce(Return(kMockFlimFlamServiceProxy_));
174 EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamServiceProxy_));
175 EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _))
176 .RetiresOnSaturation();
177}
178
179void ConnectionManagerTest::TestWithServiceType(
180 const char* service_type,
Alex Deymo1c4e6382013-07-15 12:09:51 -0700181 const char* physical_technology,
Jay Srinivasan43488792012-06-19 00:25:31 -0700182 NetworkConnectionType expected_type) {
183
184 SetupMocks("/service/guest-network");
185 SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY);
Alex Deymo6ae91202014-03-10 19:21:25 -0700186 SetServiceReply(service_type, physical_technology,
187 shill::kTetheringNotDetectedState);
Jay Srinivasan43488792012-06-19 00:25:31 -0700188
189 NetworkConnectionType type;
Alex Deymo6ae91202014-03-10 19:21:25 -0700190 NetworkTethering tethering;
191 EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700192 EXPECT_EQ(expected_type, type);
Alex Deymo5665d0c2014-05-28 17:45:43 -0700193 testing::Mock::VerifyAndClearExpectations(&dbus_iface_);
Jay Srinivasan43488792012-06-19 00:25:31 -0700194}
195
Alex Deymo6ae91202014-03-10 19:21:25 -0700196void ConnectionManagerTest::TestWithServiceTethering(
197 const char* service_tethering,
198 NetworkTethering expected_tethering) {
199
200 SetupMocks("/service/guest-network");
201 SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700202 SetServiceReply(shill::kTypeWifi, nullptr, service_tethering);
Alex Deymo6ae91202014-03-10 19:21:25 -0700203
204 NetworkConnectionType type;
205 NetworkTethering tethering;
206 EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering));
207 EXPECT_EQ(expected_tethering, tethering);
208}
209
Jay Srinivasan43488792012-06-19 00:25:31 -0700210TEST_F(ConnectionManagerTest, SimpleTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700211 TestWithServiceType(shill::kTypeEthernet, nullptr, kNetEthernet);
212 TestWithServiceType(shill::kTypeWifi, nullptr, kNetWifi);
213 TestWithServiceType(shill::kTypeWimax, nullptr, kNetWimax);
214 TestWithServiceType(shill::kTypeBluetooth, nullptr, kNetBluetooth);
215 TestWithServiceType(shill::kTypeCellular, nullptr, kNetCellular);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700216}
217
218TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700219 TestWithServiceType(shill::kTypeVPN, nullptr, kNetUnknown);
Ben Chanc6007e42013-09-19 23:49:22 -0700220 TestWithServiceType(shill::kTypeVPN, shill::kTypeVPN, kNetUnknown);
221 TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, kNetWifi);
222 TestWithServiceType(shill::kTypeVPN, shill::kTypeWimax, kNetWimax);
Jay Srinivasan43488792012-06-19 00:25:31 -0700223}
224
Alex Deymo6ae91202014-03-10 19:21:25 -0700225TEST_F(ConnectionManagerTest, TetheringTest) {
226 TestWithServiceTethering(shill::kTetheringConfirmedState,
227 NetworkTethering::kConfirmed);
228 TestWithServiceTethering(shill::kTetheringNotDetectedState,
229 NetworkTethering::kNotDetected);
230 TestWithServiceTethering(shill::kTetheringSuspectedState,
231 NetworkTethering::kSuspected);
232 TestWithServiceTethering("I'm not a valid property value =)",
233 NetworkTethering::kUnknown);
234}
235
Jay Srinivasan43488792012-06-19 00:25:31 -0700236TEST_F(ConnectionManagerTest, UnknownTest) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700237 TestWithServiceType("foo", nullptr, kNetUnknown);
Jay Srinivasan43488792012-06-19 00:25:31 -0700238}
239
240TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700241 // Updates over Ethernet are allowed even if there's no policy.
Alex Deymo6ae91202014-03-10 19:21:25 -0700242 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
243 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700244}
245
246TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700247 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700248}
249
250TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700251 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax,
252 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700253}
254
255TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700256 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth,
257 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700258}
259
260TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
261 policy::MockDevicePolicy allow_3g_policy;
262
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700263 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700264
Alex Deymof4867c42013-06-28 14:41:39 -0700265 // This test tests cellular (3G) being the only connection type being allowed.
Jay Srinivasan43488792012-06-19 00:25:31 -0700266 set<string> allowed_set;
267 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
268
269 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
270 .Times(1)
271 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
272
Alex Deymo6ae91202014-03-10 19:21:25 -0700273 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular,
274 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700275}
276
277TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
278 policy::MockDevicePolicy allow_3g_policy;
279
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700280 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700281
282 // This test tests multiple connection types being allowed, with
Alex Deymof4867c42013-06-28 14:41:39 -0700283 // 3G one among them. Only Cellular is currently enforced by the policy
284 // setting, the others are ignored (see Bluetooth for example).
Jay Srinivasan43488792012-06-19 00:25:31 -0700285 set<string> allowed_set;
Jay Srinivasan43488792012-06-19 00:25:31 -0700286 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
Alex Deymof4867c42013-06-28 14:41:39 -0700287 allowed_set.insert(cmut_.StringForConnectionType(kNetBluetooth));
Jay Srinivasan43488792012-06-19 00:25:31 -0700288
289 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
Alex Deymo6ae91202014-03-10 19:21:25 -0700290 .Times(3)
291 .WillRepeatedly(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700292
Alex Deymo6ae91202014-03-10 19:21:25 -0700293 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
294 NetworkTethering::kUnknown));
295 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
296 NetworkTethering::kNotDetected));
297 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular,
298 NetworkTethering::kUnknown));
299 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown));
300 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax, NetworkTethering::kUnknown));
301 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth,
302 NetworkTethering::kUnknown));
303
304 // Tethered networks are treated in the same way as Cellular networks and
305 // thus allowed.
306 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet,
307 NetworkTethering::kConfirmed));
308 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi,
309 NetworkTethering::kConfirmed));
Jay Srinivasan43488792012-06-19 00:25:31 -0700310}
311
Alex Deymof4867c42013-06-28 14:41:39 -0700312TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700313 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
314 NetworkTethering::kUnknown));
315}
316
317TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700318 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetWifi,
319 NetworkTethering::kConfirmed));
320 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetEthernet,
321 NetworkTethering::kConfirmed));
322 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi,
323 NetworkTethering::kSuspected));
Jay Srinivasan43488792012-06-19 00:25:31 -0700324}
325
326TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
327 policy::MockDevicePolicy block_3g_policy;
328
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700329 fake_system_state_.set_device_policy(&block_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700330
331 // Test that updates for 3G are blocked while updates are allowed
332 // over several other types.
333 set<string> allowed_set;
334 allowed_set.insert(cmut_.StringForConnectionType(kNetEthernet));
335 allowed_set.insert(cmut_.StringForConnectionType(kNetWifi));
336 allowed_set.insert(cmut_.StringForConnectionType(kNetWimax));
337
338 EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
339 .Times(1)
340 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
341
Alex Deymo6ae91202014-03-10 19:21:25 -0700342 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
343 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700344}
345
346TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) {
347 policy::MockDevicePolicy allow_3g_policy;
348
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700349 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700350
351 set<string> allowed_set;
352 allowed_set.insert(cmut_.StringForConnectionType(kNetCellular));
353
354 // Return false for GetAllowedConnectionTypesForUpdate and see
355 // that updates are still blocked for 3G despite the value being in
356 // the string set above.
357 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
358 .Times(1)
359 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(false)));
360
Alex Deymo6ae91202014-03-10 19:21:25 -0700361 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
362 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700363}
364
Alex Deymof4867c42013-06-28 14:41:39 -0700365TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) {
366 policy::MockDevicePolicy no_policy;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800367 testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs();
Alex Deymof4867c42013-06-28 14:41:39 -0700368
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700369 fake_system_state_.set_device_policy(&no_policy);
Alex Deymof4867c42013-06-28 14:41:39 -0700370
371 // No setting enforced by the device policy, user prefs should be used.
372 EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_))
373 .Times(3)
374 .WillRepeatedly(Return(false));
375
376 // No user pref: block.
377 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
378 .Times(1)
379 .WillOnce(Return(false));
Alex Deymo6ae91202014-03-10 19:21:25 -0700380 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
381 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700382
383 // Allow per user pref.
384 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
385 .Times(1)
386 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700387 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700388 .Times(1)
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700389 .WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true)));
Alex Deymo6ae91202014-03-10 19:21:25 -0700390 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular,
391 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700392
393 // Block per user pref.
394 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
395 .Times(1)
396 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700397 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700398 .Times(1)
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700399 .WillOnce(DoAll(SetArgumentPointee<1>(false), Return(true)));
Alex Deymo6ae91202014-03-10 19:21:25 -0700400 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular,
401 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700402}
403
Jay Srinivasan43488792012-06-19 00:25:31 -0700404TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
Ben Chanc6007e42013-09-19 23:49:22 -0700405 EXPECT_STREQ(shill::kTypeEthernet,
Jay Srinivasan43488792012-06-19 00:25:31 -0700406 cmut_.StringForConnectionType(kNetEthernet));
Ben Chanc6007e42013-09-19 23:49:22 -0700407 EXPECT_STREQ(shill::kTypeWifi,
Jay Srinivasan43488792012-06-19 00:25:31 -0700408 cmut_.StringForConnectionType(kNetWifi));
Ben Chanc6007e42013-09-19 23:49:22 -0700409 EXPECT_STREQ(shill::kTypeWimax,
Jay Srinivasan43488792012-06-19 00:25:31 -0700410 cmut_.StringForConnectionType(kNetWimax));
Ben Chanc6007e42013-09-19 23:49:22 -0700411 EXPECT_STREQ(shill::kTypeBluetooth,
Jay Srinivasan43488792012-06-19 00:25:31 -0700412 cmut_.StringForConnectionType(kNetBluetooth));
Ben Chanc6007e42013-09-19 23:49:22 -0700413 EXPECT_STREQ(shill::kTypeCellular,
Jay Srinivasan43488792012-06-19 00:25:31 -0700414 cmut_.StringForConnectionType(kNetCellular));
415 EXPECT_STREQ("Unknown",
416 cmut_.StringForConnectionType(kNetUnknown));
417 EXPECT_STREQ("Unknown",
418 cmut_.StringForConnectionType(
419 static_cast<NetworkConnectionType>(999999)));
420}
421
422TEST_F(ConnectionManagerTest, MalformedServiceList) {
423 SetupMocks("/service/guest-network");
Alex Deymo5665d0c2014-05-28 17:45:43 -0700424 SetManagerReply(kServicePath_, DBUS_TYPE_G_STRING_ARRAY);
Jay Srinivasan43488792012-06-19 00:25:31 -0700425
426 NetworkConnectionType type;
Alex Deymo6ae91202014-03-10 19:21:25 -0700427 NetworkTethering tethering;
428 EXPECT_FALSE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700429}
430
431} // namespace chromeos_update_engine