blob: 0c96272a64aa26a8c05625e3f7e77ddeb981c43b [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 Deymo75eac7e2015-07-29 13:39:14 -0700211 TestWithServiceType(shill::kTypeEthernet, nullptr,
212 NetworkConnectionType::kEthernet);
213 TestWithServiceType(shill::kTypeWifi, nullptr,
214 NetworkConnectionType::kWifi);
215 TestWithServiceType(shill::kTypeWimax, nullptr,
216 NetworkConnectionType::kWimax);
217 TestWithServiceType(shill::kTypeBluetooth, nullptr,
218 NetworkConnectionType::kBluetooth);
219 TestWithServiceType(shill::kTypeCellular, nullptr,
220 NetworkConnectionType::kCellular);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700221}
222
223TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700224 TestWithServiceType(shill::kTypeVPN, nullptr,
225 NetworkConnectionType::kUnknown);
226 TestWithServiceType(shill::kTypeVPN, shill::kTypeVPN,
227 NetworkConnectionType::kUnknown);
228 TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi,
229 NetworkConnectionType::kWifi);
230 TestWithServiceType(shill::kTypeVPN, shill::kTypeWimax,
231 NetworkConnectionType::kWimax);
Jay Srinivasan43488792012-06-19 00:25:31 -0700232}
233
Alex Deymo6ae91202014-03-10 19:21:25 -0700234TEST_F(ConnectionManagerTest, TetheringTest) {
235 TestWithServiceTethering(shill::kTetheringConfirmedState,
236 NetworkTethering::kConfirmed);
237 TestWithServiceTethering(shill::kTetheringNotDetectedState,
238 NetworkTethering::kNotDetected);
239 TestWithServiceTethering(shill::kTetheringSuspectedState,
240 NetworkTethering::kSuspected);
241 TestWithServiceTethering("I'm not a valid property value =)",
242 NetworkTethering::kUnknown);
243}
244
Jay Srinivasan43488792012-06-19 00:25:31 -0700245TEST_F(ConnectionManagerTest, UnknownTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700246 TestWithServiceType("foo", nullptr, NetworkConnectionType::kUnknown);
Jay Srinivasan43488792012-06-19 00:25:31 -0700247}
248
249TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700250 // Updates over Ethernet are allowed even if there's no policy.
Alex Deymo75eac7e2015-07-29 13:39:14 -0700251 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700252 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700253}
254
255TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700256 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
257 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700258}
259
260TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700261 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWimax,
Alex Deymo6ae91202014-03-10 19:21:25 -0700262 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700263}
264
265TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700266 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kBluetooth,
Alex Deymo6ae91202014-03-10 19:21:25 -0700267 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700268}
269
270TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
271 policy::MockDevicePolicy allow_3g_policy;
272
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700273 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700274
Alex Deymof4867c42013-06-28 14:41:39 -0700275 // This test tests cellular (3G) being the only connection type being allowed.
Jay Srinivasan43488792012-06-19 00:25:31 -0700276 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700277 allowed_set.insert(
278 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700279
280 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
281 .Times(1)
282 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
283
Alex Deymo75eac7e2015-07-29 13:39:14 -0700284 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700285 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700286}
287
288TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
289 policy::MockDevicePolicy allow_3g_policy;
290
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700291 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700292
293 // This test tests multiple connection types being allowed, with
Alex Deymof4867c42013-06-28 14:41:39 -0700294 // 3G one among them. Only Cellular is currently enforced by the policy
295 // setting, the others are ignored (see Bluetooth for example).
Jay Srinivasan43488792012-06-19 00:25:31 -0700296 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700297 allowed_set.insert(
298 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
299 allowed_set.insert(
300 cmut_.StringForConnectionType(NetworkConnectionType::kBluetooth));
Jay Srinivasan43488792012-06-19 00:25:31 -0700301
302 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
Alex Deymo6ae91202014-03-10 19:21:25 -0700303 .Times(3)
304 .WillRepeatedly(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700305
Alex Deymo75eac7e2015-07-29 13:39:14 -0700306 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700307 NetworkTethering::kUnknown));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700308 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700309 NetworkTethering::kNotDetected));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700310 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700311 NetworkTethering::kUnknown));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700312 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
313 NetworkTethering::kUnknown));
314 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWimax,
315 NetworkTethering::kUnknown));
316 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kBluetooth,
Alex Deymo6ae91202014-03-10 19:21:25 -0700317 NetworkTethering::kUnknown));
318
319 // Tethered networks are treated in the same way as Cellular networks and
320 // thus allowed.
Alex Deymo75eac7e2015-07-29 13:39:14 -0700321 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700322 NetworkTethering::kConfirmed));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700323 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
Alex Deymo6ae91202014-03-10 19:21:25 -0700324 NetworkTethering::kConfirmed));
Jay Srinivasan43488792012-06-19 00:25:31 -0700325}
326
Alex Deymof4867c42013-06-28 14:41:39 -0700327TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700328 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700329 NetworkTethering::kUnknown));
330}
331
332TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700333 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
Alex Deymo6ae91202014-03-10 19:21:25 -0700334 NetworkTethering::kConfirmed));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700335 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700336 NetworkTethering::kConfirmed));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700337 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
Alex Deymo6ae91202014-03-10 19:21:25 -0700338 NetworkTethering::kSuspected));
Jay Srinivasan43488792012-06-19 00:25:31 -0700339}
340
341TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
342 policy::MockDevicePolicy block_3g_policy;
343
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700344 fake_system_state_.set_device_policy(&block_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700345
346 // Test that updates for 3G are blocked while updates are allowed
347 // over several other types.
348 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700349 allowed_set.insert(
350 cmut_.StringForConnectionType(NetworkConnectionType::kEthernet));
351 allowed_set.insert(
352 cmut_.StringForConnectionType(NetworkConnectionType::kWifi));
353 allowed_set.insert(
354 cmut_.StringForConnectionType(NetworkConnectionType::kWimax));
Jay Srinivasan43488792012-06-19 00:25:31 -0700355
356 EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
357 .Times(1)
358 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true)));
359
Alex Deymo75eac7e2015-07-29 13:39:14 -0700360 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700361 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700362}
363
364TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) {
365 policy::MockDevicePolicy allow_3g_policy;
366
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700367 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700368
369 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700370 allowed_set.insert(
371 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700372
373 // Return false for GetAllowedConnectionTypesForUpdate and see
374 // that updates are still blocked for 3G despite the value being in
375 // the string set above.
376 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
377 .Times(1)
378 .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(false)));
379
Alex Deymo75eac7e2015-07-29 13:39:14 -0700380 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700381 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700382}
383
Alex Deymof4867c42013-06-28 14:41:39 -0700384TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) {
385 policy::MockDevicePolicy no_policy;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800386 testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs();
Alex Deymof4867c42013-06-28 14:41:39 -0700387
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700388 fake_system_state_.set_device_policy(&no_policy);
Alex Deymof4867c42013-06-28 14:41:39 -0700389
390 // No setting enforced by the device policy, user prefs should be used.
391 EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_))
392 .Times(3)
393 .WillRepeatedly(Return(false));
394
395 // No user pref: block.
396 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
397 .Times(1)
398 .WillOnce(Return(false));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700399 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700400 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700401
402 // Allow per user pref.
403 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
404 .Times(1)
405 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700406 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700407 .Times(1)
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700408 .WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700409 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700410 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700411
412 // Block per user pref.
413 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
414 .Times(1)
415 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700416 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700417 .Times(1)
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700418 .WillOnce(DoAll(SetArgumentPointee<1>(false), Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700419 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700420 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700421}
422
Jay Srinivasan43488792012-06-19 00:25:31 -0700423TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
Ben Chanc6007e42013-09-19 23:49:22 -0700424 EXPECT_STREQ(shill::kTypeEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700425 cmut_.StringForConnectionType(NetworkConnectionType::kEthernet));
Ben Chanc6007e42013-09-19 23:49:22 -0700426 EXPECT_STREQ(shill::kTypeWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700427 cmut_.StringForConnectionType(NetworkConnectionType::kWifi));
Ben Chanc6007e42013-09-19 23:49:22 -0700428 EXPECT_STREQ(shill::kTypeWimax,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700429 cmut_.StringForConnectionType(NetworkConnectionType::kWimax));
Ben Chanc6007e42013-09-19 23:49:22 -0700430 EXPECT_STREQ(shill::kTypeBluetooth,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700431 cmut_.StringForConnectionType(
432 NetworkConnectionType::kBluetooth));
Ben Chanc6007e42013-09-19 23:49:22 -0700433 EXPECT_STREQ(shill::kTypeCellular,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700434 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700435 EXPECT_STREQ("Unknown",
Alex Deymo75eac7e2015-07-29 13:39:14 -0700436 cmut_.StringForConnectionType(NetworkConnectionType::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700437 EXPECT_STREQ("Unknown",
438 cmut_.StringForConnectionType(
439 static_cast<NetworkConnectionType>(999999)));
440}
441
442TEST_F(ConnectionManagerTest, MalformedServiceList) {
443 SetupMocks("/service/guest-network");
Alex Deymo5665d0c2014-05-28 17:45:43 -0700444 SetManagerReply(kServicePath_, DBUS_TYPE_G_STRING_ARRAY);
Jay Srinivasan43488792012-06-19 00:25:31 -0700445
446 NetworkConnectionType type;
Alex Deymo6ae91202014-03-10 19:21:25 -0700447 NetworkTethering tethering;
448 EXPECT_FALSE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700449}
450
451} // namespace chromeos_update_engine