Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | #include <base/logging.h> |
| 6 | #include <chromeos/dbus/service_constants.h> |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 7 | #include <gmock/gmock.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "update_engine/connection_manager.h" |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 12 | #include "update_engine/fake_system_state.h" |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 13 | #include "update_engine/mock_dbus_wrapper.h" |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 14 | #include "update_engine/test_utils.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 15 | |
| 16 | using std::set; |
| 17 | using std::string; |
| 18 | using testing::_; |
| 19 | using testing::AnyNumber; |
| 20 | using testing::Return; |
| 21 | using testing::SetArgumentPointee; |
| 22 | using testing::StrEq; |
| 23 | |
| 24 | namespace chromeos_update_engine { |
| 25 | |
| 26 | class ConnectionManagerTest : public ::testing::Test { |
| 27 | public: |
| 28 | ConnectionManagerTest() |
| 29 | : kMockFlimFlamManagerProxy_(NULL), |
| 30 | kMockFlimFlamServiceProxy_(NULL), |
| 31 | kServicePath_(NULL), |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 32 | cmut_(&fake_system_state_) { |
| 33 | fake_system_state_.set_connection_manager(&cmut_); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | protected: |
| 37 | void SetupMocks(const char* service_path); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 38 | void SetManagerReply(const char* reply_value, const GType& reply_type); |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 39 | |
| 40 | // Sets the |service_type| Type and the |physical_technology| |
| 41 | // PhysicalTechnology properties in the mocked service. If a NULL |
| 42 | // |physical_technology| is passed, the property is not set (not present). |
| 43 | void SetServiceReply(const char* service_type, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 44 | const char* physical_technology, |
| 45 | const char* service_tethering); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 46 | void TestWithServiceType( |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 47 | const char* service_type, |
| 48 | const char* physical_technology, |
| 49 | NetworkConnectionType expected_type); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 50 | void TestWithServiceTethering( |
| 51 | const char* service_tethering, |
| 52 | NetworkTethering expected_tethering); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 53 | |
| 54 | static const char* kGetPropertiesMethod; |
| 55 | DBusGProxy* kMockFlimFlamManagerProxy_; |
| 56 | DBusGProxy* kMockFlimFlamServiceProxy_; |
| 57 | DBusGConnection* kMockSystemBus_; |
| 58 | const char* kServicePath_; |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 59 | testing::StrictMock<MockDBusWrapper> dbus_iface_; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 60 | ConnectionManager cmut_; // ConnectionManager under test. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 61 | FakeSystemState fake_system_state_; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // static |
| 65 | const char* ConnectionManagerTest::kGetPropertiesMethod = "GetProperties"; |
| 66 | |
| 67 | void ConnectionManagerTest::SetupMocks(const char* service_path) { |
| 68 | int number = 1; |
| 69 | kMockSystemBus_ = reinterpret_cast<DBusGConnection*>(number++); |
| 70 | kMockFlimFlamManagerProxy_ = reinterpret_cast<DBusGProxy*>(number++); |
| 71 | kMockFlimFlamServiceProxy_ = reinterpret_cast<DBusGProxy*>(number++); |
| 72 | ASSERT_NE(kMockSystemBus_, reinterpret_cast<DBusGConnection*>(NULL)); |
| 73 | |
| 74 | kServicePath_ = service_path; |
| 75 | |
| 76 | ON_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _)) |
| 77 | .WillByDefault(Return(kMockSystemBus_)); |
| 78 | EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _)) |
| 79 | .Times(AnyNumber()); |
| 80 | } |
| 81 | |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 82 | void ConnectionManagerTest::SetManagerReply(const char *reply_value, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 83 | const GType& reply_type) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 84 | ASSERT_TRUE(dbus_g_type_is_collection(reply_type)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 85 | |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 86 | // Create the GPtrArray array holding the |reply_value| pointer. The |
| 87 | // |reply_value| string is duplicated because it should be mutable on the |
| 88 | // interface and is because dbus-glib collections will g_free() each element |
| 89 | // of the GPtrArray automatically when the |array_as_value| GValue is unset. |
| 90 | // The g_strdup() is not being leaked. |
| 91 | GPtrArray* array = g_ptr_array_new(); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(array != NULL); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 93 | g_ptr_array_add(array, g_strdup(reply_value)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 94 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 95 | GValue* array_as_value = g_new0(GValue, 1); |
| 96 | EXPECT_EQ(array_as_value, g_value_init(array_as_value, reply_type)); |
| 97 | g_value_take_boxed(array_as_value, array); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 98 | |
| 99 | // Initialize return value for D-Bus call to Manager object, which is a |
| 100 | // hash table of static strings (char*) in GValue* containing a single array. |
| 101 | GHashTable* manager_hash_table = g_hash_table_new_full( |
| 102 | g_str_hash, g_str_equal, |
| 103 | nullptr, // no key_destroy_func because keys are static. |
| 104 | GValueFree); // value_destroy_func |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 105 | g_hash_table_insert(manager_hash_table, |
| 106 | const_cast<char*>("Services"), |
| 107 | array_as_value); |
| 108 | |
| 109 | // Plumb return value into mock object. |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 110 | EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamManagerProxy_, |
| 111 | StrEq(kGetPropertiesMethod), |
| 112 | _, _)) |
| 113 | .WillOnce(DoAll(SetArgumentPointee<3>(manager_hash_table), Return(TRUE))); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 114 | |
| 115 | // Set other expectations. |
| 116 | EXPECT_CALL(dbus_iface_, |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 117 | ProxyNewForName(kMockSystemBus_, |
| 118 | StrEq(shill::kFlimflamServiceName), |
| 119 | StrEq(shill::kFlimflamServicePath), |
| 120 | StrEq(shill::kFlimflamManagerInterface))) |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 121 | .WillOnce(Return(kMockFlimFlamManagerProxy_)); |
| 122 | EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamManagerProxy_)); |
| 123 | EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _)) |
| 124 | .RetiresOnSaturation(); |
| 125 | } |
| 126 | |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 127 | void ConnectionManagerTest::SetServiceReply(const char* service_type, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 128 | const char* physical_technology, |
| 129 | const char* service_tethering) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 130 | // Initialize return value for D-Bus call to Service object, which is a |
| 131 | // hash table of static strings (char*) in GValue*. |
| 132 | GHashTable* service_hash_table = g_hash_table_new_full( |
| 133 | g_str_hash, g_str_equal, |
| 134 | nullptr, // no key_destroy_func because keys are static. |
| 135 | GValueFree); // value_destroy_func |
| 136 | GValue* service_type_value = GValueNewString(service_type); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 137 | g_hash_table_insert(service_hash_table, |
| 138 | const_cast<char*>("Type"), |
| 139 | service_type_value); |
| 140 | |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 141 | if (physical_technology != NULL) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 142 | GValue* physical_technology_value = GValueNewString(physical_technology); |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 143 | g_hash_table_insert(service_hash_table, |
| 144 | const_cast<char*>("PhysicalTechnology"), |
| 145 | physical_technology_value); |
| 146 | } |
| 147 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 148 | if (service_tethering != NULL) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 149 | GValue* service_tethering_value = GValueNewString(service_tethering); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 150 | g_hash_table_insert(service_hash_table, |
| 151 | const_cast<char*>("Tethering"), |
| 152 | service_tethering_value); |
| 153 | } |
| 154 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 155 | // Plumb return value into mock object. |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 156 | EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamServiceProxy_, |
| 157 | StrEq(kGetPropertiesMethod), |
| 158 | _, _)) |
| 159 | .WillOnce(DoAll(SetArgumentPointee<3>(service_hash_table), Return(TRUE))); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 160 | |
| 161 | // Set other expectations. |
| 162 | EXPECT_CALL(dbus_iface_, |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 163 | ProxyNewForName(kMockSystemBus_, |
| 164 | StrEq(shill::kFlimflamServiceName), |
| 165 | StrEq(kServicePath_), |
| 166 | StrEq(shill::kFlimflamServiceInterface))) |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 167 | .WillOnce(Return(kMockFlimFlamServiceProxy_)); |
| 168 | EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamServiceProxy_)); |
| 169 | EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _)) |
| 170 | .RetiresOnSaturation(); |
| 171 | } |
| 172 | |
| 173 | void ConnectionManagerTest::TestWithServiceType( |
| 174 | const char* service_type, |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 175 | const char* physical_technology, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 176 | NetworkConnectionType expected_type) { |
| 177 | |
| 178 | SetupMocks("/service/guest-network"); |
| 179 | SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 180 | SetServiceReply(service_type, physical_technology, |
| 181 | shill::kTetheringNotDetectedState); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 182 | |
| 183 | NetworkConnectionType type; |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 184 | NetworkTethering tethering; |
| 185 | EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 186 | EXPECT_EQ(expected_type, type); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 187 | testing::Mock::VerifyAndClearExpectations(&dbus_iface_); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 190 | void ConnectionManagerTest::TestWithServiceTethering( |
| 191 | const char* service_tethering, |
| 192 | NetworkTethering expected_tethering) { |
| 193 | |
| 194 | SetupMocks("/service/guest-network"); |
| 195 | SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY); |
| 196 | SetServiceReply(shill::kTypeWifi, NULL, service_tethering); |
| 197 | |
| 198 | NetworkConnectionType type; |
| 199 | NetworkTethering tethering; |
| 200 | EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering)); |
| 201 | EXPECT_EQ(expected_tethering, tethering); |
| 202 | } |
| 203 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 204 | TEST_F(ConnectionManagerTest, SimpleTest) { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 205 | TestWithServiceType(shill::kTypeEthernet, NULL, kNetEthernet); |
| 206 | TestWithServiceType(shill::kTypeWifi, NULL, kNetWifi); |
| 207 | TestWithServiceType(shill::kTypeWimax, NULL, kNetWimax); |
| 208 | TestWithServiceType(shill::kTypeBluetooth, NULL, kNetBluetooth); |
| 209 | TestWithServiceType(shill::kTypeCellular, NULL, kNetCellular); |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 213 | TestWithServiceType(shill::kTypeVPN, NULL, kNetUnknown); |
| 214 | TestWithServiceType(shill::kTypeVPN, shill::kTypeVPN, kNetUnknown); |
| 215 | TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, kNetWifi); |
| 216 | TestWithServiceType(shill::kTypeVPN, shill::kTypeWimax, kNetWimax); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 219 | TEST_F(ConnectionManagerTest, TetheringTest) { |
| 220 | TestWithServiceTethering(shill::kTetheringConfirmedState, |
| 221 | NetworkTethering::kConfirmed); |
| 222 | TestWithServiceTethering(shill::kTetheringNotDetectedState, |
| 223 | NetworkTethering::kNotDetected); |
| 224 | TestWithServiceTethering(shill::kTetheringSuspectedState, |
| 225 | NetworkTethering::kSuspected); |
| 226 | TestWithServiceTethering("I'm not a valid property value =)", |
| 227 | NetworkTethering::kUnknown); |
| 228 | } |
| 229 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 230 | TEST_F(ConnectionManagerTest, UnknownTest) { |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 231 | TestWithServiceType("foo", NULL, kNetUnknown); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 235 | // Updates over Ethernet are allowed even if there's no policy. |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 236 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 237 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 241 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 245 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax, |
| 246 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 250 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth, |
| 251 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) { |
| 255 | policy::MockDevicePolicy allow_3g_policy; |
| 256 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 257 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 258 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 259 | // This test tests cellular (3G) being the only connection type being allowed. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 260 | set<string> allowed_set; |
| 261 | allowed_set.insert(cmut_.StringForConnectionType(kNetCellular)); |
| 262 | |
| 263 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 264 | .Times(1) |
| 265 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
| 266 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 267 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 268 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) { |
| 272 | policy::MockDevicePolicy allow_3g_policy; |
| 273 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 274 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 275 | |
| 276 | // This test tests multiple connection types being allowed, with |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 277 | // 3G one among them. Only Cellular is currently enforced by the policy |
| 278 | // setting, the others are ignored (see Bluetooth for example). |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 279 | set<string> allowed_set; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 280 | allowed_set.insert(cmut_.StringForConnectionType(kNetCellular)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 281 | allowed_set.insert(cmut_.StringForConnectionType(kNetBluetooth)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 282 | |
| 283 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 284 | .Times(3) |
| 285 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 286 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 287 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 288 | NetworkTethering::kUnknown)); |
| 289 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 290 | NetworkTethering::kNotDetected)); |
| 291 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 292 | NetworkTethering::kUnknown)); |
| 293 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown)); |
| 294 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax, NetworkTethering::kUnknown)); |
| 295 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth, |
| 296 | NetworkTethering::kUnknown)); |
| 297 | |
| 298 | // Tethered networks are treated in the same way as Cellular networks and |
| 299 | // thus allowed. |
| 300 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 301 | NetworkTethering::kConfirmed)); |
| 302 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, |
| 303 | NetworkTethering::kConfirmed)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 306 | TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 307 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 308 | NetworkTethering::kUnknown)); |
| 309 | } |
| 310 | |
| 311 | TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 312 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetWifi, |
| 313 | NetworkTethering::kConfirmed)); |
| 314 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 315 | NetworkTethering::kConfirmed)); |
| 316 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, |
| 317 | NetworkTethering::kSuspected)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) { |
| 321 | policy::MockDevicePolicy block_3g_policy; |
| 322 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 323 | fake_system_state_.set_device_policy(&block_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 324 | |
| 325 | // Test that updates for 3G are blocked while updates are allowed |
| 326 | // over several other types. |
| 327 | set<string> allowed_set; |
| 328 | allowed_set.insert(cmut_.StringForConnectionType(kNetEthernet)); |
| 329 | allowed_set.insert(cmut_.StringForConnectionType(kNetWifi)); |
| 330 | allowed_set.insert(cmut_.StringForConnectionType(kNetWimax)); |
| 331 | |
| 332 | EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 333 | .Times(1) |
| 334 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
| 335 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 336 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 337 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) { |
| 341 | policy::MockDevicePolicy allow_3g_policy; |
| 342 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 343 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 344 | |
| 345 | set<string> allowed_set; |
| 346 | allowed_set.insert(cmut_.StringForConnectionType(kNetCellular)); |
| 347 | |
| 348 | // Return false for GetAllowedConnectionTypesForUpdate and see |
| 349 | // that updates are still blocked for 3G despite the value being in |
| 350 | // the string set above. |
| 351 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 352 | .Times(1) |
| 353 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(false))); |
| 354 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 355 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 356 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 359 | TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) { |
| 360 | policy::MockDevicePolicy no_policy; |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 361 | testing::NiceMock<PrefsMock>* prefs = fake_system_state_.mock_prefs(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 362 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 363 | fake_system_state_.set_device_policy(&no_policy); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 364 | |
| 365 | // No setting enforced by the device policy, user prefs should be used. |
| 366 | EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 367 | .Times(3) |
| 368 | .WillRepeatedly(Return(false)); |
| 369 | |
| 370 | // No user pref: block. |
| 371 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 372 | .Times(1) |
| 373 | .WillOnce(Return(false)); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 374 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 375 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 376 | |
| 377 | // Allow per user pref. |
| 378 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 379 | .Times(1) |
| 380 | .WillOnce(Return(true)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 381 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 382 | .Times(1) |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 383 | .WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true))); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 384 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 385 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 386 | |
| 387 | // Block per user pref. |
| 388 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 389 | .Times(1) |
| 390 | .WillOnce(Return(true)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 391 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 392 | .Times(1) |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 393 | .WillOnce(DoAll(SetArgumentPointee<1>(false), Return(true))); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 394 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 395 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 398 | TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 399 | EXPECT_STREQ(shill::kTypeEthernet, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 400 | cmut_.StringForConnectionType(kNetEthernet)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 401 | EXPECT_STREQ(shill::kTypeWifi, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 402 | cmut_.StringForConnectionType(kNetWifi)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 403 | EXPECT_STREQ(shill::kTypeWimax, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 404 | cmut_.StringForConnectionType(kNetWimax)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 405 | EXPECT_STREQ(shill::kTypeBluetooth, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 406 | cmut_.StringForConnectionType(kNetBluetooth)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 407 | EXPECT_STREQ(shill::kTypeCellular, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 408 | cmut_.StringForConnectionType(kNetCellular)); |
| 409 | EXPECT_STREQ("Unknown", |
| 410 | cmut_.StringForConnectionType(kNetUnknown)); |
| 411 | EXPECT_STREQ("Unknown", |
| 412 | cmut_.StringForConnectionType( |
| 413 | static_cast<NetworkConnectionType>(999999))); |
| 414 | } |
| 415 | |
| 416 | TEST_F(ConnectionManagerTest, MalformedServiceList) { |
| 417 | SetupMocks("/service/guest-network"); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame^] | 418 | SetManagerReply(kServicePath_, DBUS_TYPE_G_STRING_ARRAY); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 419 | |
| 420 | NetworkConnectionType type; |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 421 | NetworkTethering tethering; |
| 422 | EXPECT_FALSE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | } // namespace chromeos_update_engine |