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 Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 5 | #include "update_engine/connection_manager.h" |
| 6 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 7 | #include <set> |
| 8 | #include <string> |
| 9 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 10 | #include <base/logging.h> |
| 11 | #include <chromeos/dbus/service_constants.h> |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 12 | #include <gmock/gmock.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 14 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 15 | #include "update_engine/fake_system_state.h" |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 16 | #include "update_engine/mock_dbus_wrapper.h" |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 17 | #include "update_engine/test_utils.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 18 | |
| 19 | using std::set; |
| 20 | using std::string; |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 21 | using testing::A; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 22 | using testing::AnyNumber; |
| 23 | using testing::Return; |
| 24 | using testing::SetArgumentPointee; |
| 25 | using testing::StrEq; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 26 | using testing::_; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 27 | |
| 28 | namespace chromeos_update_engine { |
| 29 | |
| 30 | class ConnectionManagerTest : public ::testing::Test { |
| 31 | public: |
| 32 | ConnectionManagerTest() |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 33 | : kMockFlimFlamManagerProxy_(nullptr), |
| 34 | kMockFlimFlamServiceProxy_(nullptr), |
| 35 | kServicePath_(nullptr), |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 36 | cmut_(&fake_system_state_) { |
| 37 | fake_system_state_.set_connection_manager(&cmut_); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | protected: |
| 41 | void SetupMocks(const char* service_path); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 42 | void SetManagerReply(const char* reply_value, const GType& reply_type); |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 43 | |
| 44 | // Sets the |service_type| Type and the |physical_technology| |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 45 | // PhysicalTechnology properties in the mocked service. If a null |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 46 | // |physical_technology| is passed, the property is not set (not present). |
| 47 | void SetServiceReply(const char* service_type, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 48 | const char* physical_technology, |
| 49 | const char* service_tethering); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 50 | void TestWithServiceType( |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 51 | const char* service_type, |
| 52 | const char* physical_technology, |
| 53 | NetworkConnectionType expected_type); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 54 | void TestWithServiceTethering( |
| 55 | const char* service_tethering, |
| 56 | NetworkTethering expected_tethering); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 57 | |
| 58 | static const char* kGetPropertiesMethod; |
| 59 | DBusGProxy* kMockFlimFlamManagerProxy_; |
| 60 | DBusGProxy* kMockFlimFlamServiceProxy_; |
| 61 | DBusGConnection* kMockSystemBus_; |
| 62 | const char* kServicePath_; |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 63 | testing::StrictMock<MockDBusWrapper> dbus_iface_; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 64 | ConnectionManager cmut_; // ConnectionManager under test. |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 65 | FakeSystemState fake_system_state_; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // static |
| 69 | const char* ConnectionManagerTest::kGetPropertiesMethod = "GetProperties"; |
| 70 | |
| 71 | void 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 76 | ASSERT_NE(kMockSystemBus_, static_cast<DBusGConnection*>(nullptr)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 77 | |
| 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 Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 86 | void ConnectionManagerTest::SetManagerReply(const char *reply_value, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 87 | const GType& reply_type) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 88 | ASSERT_TRUE(dbus_g_type_is_collection(reply_type)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 89 | |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 90 | // 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 96 | ASSERT_NE(nullptr, array); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 97 | g_ptr_array_add(array, g_strdup(reply_value)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 98 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 99 | 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 Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 102 | |
| 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 Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 108 | test_utils::GValueFree); // value_destroy_func |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 109 | 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 Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 114 | EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamManagerProxy_, |
| 115 | StrEq(kGetPropertiesMethod), |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 116 | _, A<GHashTable**>())) |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 117 | .WillOnce(DoAll(SetArgumentPointee<3>(manager_hash_table), Return(TRUE))); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 118 | |
| 119 | // Set other expectations. |
| 120 | EXPECT_CALL(dbus_iface_, |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 121 | ProxyNewForName(kMockSystemBus_, |
| 122 | StrEq(shill::kFlimflamServiceName), |
| 123 | StrEq(shill::kFlimflamServicePath), |
| 124 | StrEq(shill::kFlimflamManagerInterface))) |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 125 | .WillOnce(Return(kMockFlimFlamManagerProxy_)); |
| 126 | EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamManagerProxy_)); |
| 127 | EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _)) |
| 128 | .RetiresOnSaturation(); |
| 129 | } |
| 130 | |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 131 | void ConnectionManagerTest::SetServiceReply(const char* service_type, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 132 | const char* physical_technology, |
| 133 | const char* service_tethering) { |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 134 | // 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 Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 139 | test_utils::GValueFree); // value_destroy_func |
| 140 | GValue* service_type_value = test_utils::GValueNewString(service_type); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 141 | g_hash_table_insert(service_hash_table, |
| 142 | const_cast<char*>("Type"), |
| 143 | service_type_value); |
| 144 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 145 | if (physical_technology) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 146 | GValue* physical_technology_value = |
| 147 | test_utils::GValueNewString(physical_technology); |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 148 | g_hash_table_insert(service_hash_table, |
| 149 | const_cast<char*>("PhysicalTechnology"), |
| 150 | physical_technology_value); |
| 151 | } |
| 152 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 153 | if (service_tethering) { |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 154 | GValue* service_tethering_value = |
| 155 | test_utils::GValueNewString(service_tethering); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 156 | g_hash_table_insert(service_hash_table, |
| 157 | const_cast<char*>("Tethering"), |
| 158 | service_tethering_value); |
| 159 | } |
| 160 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 161 | // Plumb return value into mock object. |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 162 | EXPECT_CALL(dbus_iface_, ProxyCall_0_1(kMockFlimFlamServiceProxy_, |
| 163 | StrEq(kGetPropertiesMethod), |
David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 164 | _, A<GHashTable**>())) |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 165 | .WillOnce(DoAll(SetArgumentPointee<3>(service_hash_table), Return(TRUE))); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 166 | |
| 167 | // Set other expectations. |
| 168 | EXPECT_CALL(dbus_iface_, |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 169 | ProxyNewForName(kMockSystemBus_, |
| 170 | StrEq(shill::kFlimflamServiceName), |
| 171 | StrEq(kServicePath_), |
| 172 | StrEq(shill::kFlimflamServiceInterface))) |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 173 | .WillOnce(Return(kMockFlimFlamServiceProxy_)); |
| 174 | EXPECT_CALL(dbus_iface_, ProxyUnref(kMockFlimFlamServiceProxy_)); |
| 175 | EXPECT_CALL(dbus_iface_, BusGet(DBUS_BUS_SYSTEM, _)) |
| 176 | .RetiresOnSaturation(); |
| 177 | } |
| 178 | |
| 179 | void ConnectionManagerTest::TestWithServiceType( |
| 180 | const char* service_type, |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 181 | const char* physical_technology, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 182 | NetworkConnectionType expected_type) { |
| 183 | |
| 184 | SetupMocks("/service/guest-network"); |
| 185 | SetManagerReply(kServicePath_, DBUS_TYPE_G_OBJECT_PATH_ARRAY); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 186 | SetServiceReply(service_type, physical_technology, |
| 187 | shill::kTetheringNotDetectedState); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 188 | |
| 189 | NetworkConnectionType type; |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 190 | NetworkTethering tethering; |
| 191 | EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 192 | EXPECT_EQ(expected_type, type); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 193 | testing::Mock::VerifyAndClearExpectations(&dbus_iface_); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 196 | void 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 202 | SetServiceReply(shill::kTypeWifi, nullptr, service_tethering); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 203 | |
| 204 | NetworkConnectionType type; |
| 205 | NetworkTethering tethering; |
| 206 | EXPECT_TRUE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering)); |
| 207 | EXPECT_EQ(expected_tethering, tethering); |
| 208 | } |
| 209 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 210 | TEST_F(ConnectionManagerTest, SimpleTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 211 | 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 Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 224 | 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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 234 | TEST_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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 245 | TEST_F(ConnectionManagerTest, UnknownTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 246 | TestWithServiceType("foo", nullptr, NetworkConnectionType::kUnknown); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 250 | // Updates over Ethernet are allowed even if there's no policy. |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 251 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 252 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 256 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, |
| 257 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 261 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWimax, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 262 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 266 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kBluetooth, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 267 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) { |
| 271 | policy::MockDevicePolicy allow_3g_policy; |
| 272 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 273 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 274 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 275 | // This test tests cellular (3G) being the only connection type being allowed. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 276 | set<string> allowed_set; |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 277 | allowed_set.insert( |
| 278 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 279 | |
| 280 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 281 | .Times(1) |
| 282 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
| 283 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 284 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 285 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) { |
| 289 | policy::MockDevicePolicy allow_3g_policy; |
| 290 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 291 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 292 | |
| 293 | // This test tests multiple connection types being allowed, with |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 294 | // 3G one among them. Only Cellular is currently enforced by the policy |
| 295 | // setting, the others are ignored (see Bluetooth for example). |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 296 | set<string> allowed_set; |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 297 | allowed_set.insert( |
| 298 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); |
| 299 | allowed_set.insert( |
| 300 | cmut_.StringForConnectionType(NetworkConnectionType::kBluetooth)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 301 | |
| 302 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 303 | .Times(3) |
| 304 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 305 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 306 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 307 | NetworkTethering::kUnknown)); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 308 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 309 | NetworkTethering::kNotDetected)); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 310 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 311 | NetworkTethering::kUnknown)); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 312 | 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 Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 317 | NetworkTethering::kUnknown)); |
| 318 | |
| 319 | // Tethered networks are treated in the same way as Cellular networks and |
| 320 | // thus allowed. |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 321 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 322 | NetworkTethering::kConfirmed)); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 323 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 324 | NetworkTethering::kConfirmed)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 327 | TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 328 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 329 | NetworkTethering::kUnknown)); |
| 330 | } |
| 331 | |
| 332 | TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) { |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 333 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 334 | NetworkTethering::kConfirmed)); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 335 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 336 | NetworkTethering::kConfirmed)); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 337 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 338 | NetworkTethering::kSuspected)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) { |
| 342 | policy::MockDevicePolicy block_3g_policy; |
| 343 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 344 | fake_system_state_.set_device_policy(&block_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 345 | |
| 346 | // Test that updates for 3G are blocked while updates are allowed |
| 347 | // over several other types. |
| 348 | set<string> allowed_set; |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 349 | 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 Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 355 | |
| 356 | EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 357 | .Times(1) |
| 358 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
| 359 | |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 360 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 361 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) { |
| 365 | policy::MockDevicePolicy allow_3g_policy; |
| 366 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 367 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 368 | |
| 369 | set<string> allowed_set; |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 370 | allowed_set.insert( |
| 371 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 372 | |
| 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 Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 380 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 381 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 384 | TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) { |
| 385 | policy::MockDevicePolicy no_policy; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 386 | testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 387 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 388 | fake_system_state_.set_device_policy(&no_policy); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 389 | |
| 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 Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 399 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 400 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 401 | |
| 402 | // Allow per user pref. |
| 403 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 404 | .Times(1) |
| 405 | .WillOnce(Return(true)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 406 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 407 | .Times(1) |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 408 | .WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true))); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 409 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 410 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 411 | |
| 412 | // Block per user pref. |
| 413 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 414 | .Times(1) |
| 415 | .WillOnce(Return(true)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 416 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 417 | .Times(1) |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 418 | .WillOnce(DoAll(SetArgumentPointee<1>(false), Return(true))); |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 419 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 420 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 423 | TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 424 | EXPECT_STREQ(shill::kTypeEthernet, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 425 | cmut_.StringForConnectionType(NetworkConnectionType::kEthernet)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 426 | EXPECT_STREQ(shill::kTypeWifi, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 427 | cmut_.StringForConnectionType(NetworkConnectionType::kWifi)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 428 | EXPECT_STREQ(shill::kTypeWimax, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 429 | cmut_.StringForConnectionType(NetworkConnectionType::kWimax)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 430 | EXPECT_STREQ(shill::kTypeBluetooth, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 431 | cmut_.StringForConnectionType( |
| 432 | NetworkConnectionType::kBluetooth)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 433 | EXPECT_STREQ(shill::kTypeCellular, |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 434 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 435 | EXPECT_STREQ("Unknown", |
Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 436 | cmut_.StringForConnectionType(NetworkConnectionType::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 437 | EXPECT_STREQ("Unknown", |
| 438 | cmut_.StringForConnectionType( |
| 439 | static_cast<NetworkConnectionType>(999999))); |
| 440 | } |
| 441 | |
| 442 | TEST_F(ConnectionManagerTest, MalformedServiceList) { |
| 443 | SetupMocks("/service/guest-network"); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 444 | SetManagerReply(kServicePath_, DBUS_TYPE_G_STRING_ARRAY); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 445 | |
| 446 | NetworkConnectionType type; |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 447 | NetworkTethering tethering; |
| 448 | EXPECT_FALSE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | } // namespace chromeos_update_engine |