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