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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 211 | TestWithServiceType(shill::kTypeEthernet, nullptr, kNetEthernet); |
| 212 | TestWithServiceType(shill::kTypeWifi, nullptr, kNetWifi); |
| 213 | TestWithServiceType(shill::kTypeWimax, nullptr, kNetWimax); |
| 214 | TestWithServiceType(shill::kTypeBluetooth, nullptr, kNetBluetooth); |
| 215 | TestWithServiceType(shill::kTypeCellular, nullptr, kNetCellular); |
Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 219 | TestWithServiceType(shill::kTypeVPN, nullptr, kNetUnknown); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 220 | TestWithServiceType(shill::kTypeVPN, shill::kTypeVPN, kNetUnknown); |
| 221 | TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, kNetWifi); |
| 222 | TestWithServiceType(shill::kTypeVPN, shill::kTypeWimax, kNetWimax); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 225 | TEST_F(ConnectionManagerTest, TetheringTest) { |
| 226 | TestWithServiceTethering(shill::kTetheringConfirmedState, |
| 227 | NetworkTethering::kConfirmed); |
| 228 | TestWithServiceTethering(shill::kTetheringNotDetectedState, |
| 229 | NetworkTethering::kNotDetected); |
| 230 | TestWithServiceTethering(shill::kTetheringSuspectedState, |
| 231 | NetworkTethering::kSuspected); |
| 232 | TestWithServiceTethering("I'm not a valid property value =)", |
| 233 | NetworkTethering::kUnknown); |
| 234 | } |
| 235 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 236 | TEST_F(ConnectionManagerTest, UnknownTest) { |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 237 | TestWithServiceType("foo", nullptr, kNetUnknown); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 241 | // Updates over Ethernet are allowed even if there's no policy. |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 242 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 243 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 247 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 251 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax, |
| 252 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 256 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth, |
| 257 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) { |
| 261 | policy::MockDevicePolicy allow_3g_policy; |
| 262 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 263 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 264 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 265 | // This test tests cellular (3G) being the only connection type being allowed. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 266 | set<string> allowed_set; |
| 267 | allowed_set.insert(cmut_.StringForConnectionType(kNetCellular)); |
| 268 | |
| 269 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 270 | .Times(1) |
| 271 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
| 272 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 273 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 274 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) { |
| 278 | policy::MockDevicePolicy allow_3g_policy; |
| 279 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 280 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 281 | |
| 282 | // This test tests multiple connection types being allowed, with |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 283 | // 3G one among them. Only Cellular is currently enforced by the policy |
| 284 | // setting, the others are ignored (see Bluetooth for example). |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 285 | set<string> allowed_set; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 286 | allowed_set.insert(cmut_.StringForConnectionType(kNetCellular)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 287 | allowed_set.insert(cmut_.StringForConnectionType(kNetBluetooth)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 288 | |
| 289 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 290 | .Times(3) |
| 291 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 292 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 293 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 294 | NetworkTethering::kUnknown)); |
| 295 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 296 | NetworkTethering::kNotDetected)); |
| 297 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 298 | NetworkTethering::kUnknown)); |
| 299 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, NetworkTethering::kUnknown)); |
| 300 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWimax, NetworkTethering::kUnknown)); |
| 301 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetBluetooth, |
| 302 | NetworkTethering::kUnknown)); |
| 303 | |
| 304 | // Tethered networks are treated in the same way as Cellular networks and |
| 305 | // thus allowed. |
| 306 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 307 | NetworkTethering::kConfirmed)); |
| 308 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, |
| 309 | NetworkTethering::kConfirmed)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 312 | TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 313 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 314 | NetworkTethering::kUnknown)); |
| 315 | } |
| 316 | |
| 317 | TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) { |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 318 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetWifi, |
| 319 | NetworkTethering::kConfirmed)); |
| 320 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetEthernet, |
| 321 | NetworkTethering::kConfirmed)); |
| 322 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetWifi, |
| 323 | NetworkTethering::kSuspected)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) { |
| 327 | policy::MockDevicePolicy block_3g_policy; |
| 328 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 329 | fake_system_state_.set_device_policy(&block_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 330 | |
| 331 | // Test that updates for 3G are blocked while updates are allowed |
| 332 | // over several other types. |
| 333 | set<string> allowed_set; |
| 334 | allowed_set.insert(cmut_.StringForConnectionType(kNetEthernet)); |
| 335 | allowed_set.insert(cmut_.StringForConnectionType(kNetWifi)); |
| 336 | allowed_set.insert(cmut_.StringForConnectionType(kNetWimax)); |
| 337 | |
| 338 | EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 339 | .Times(1) |
| 340 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(true))); |
| 341 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 342 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 343 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) { |
| 347 | policy::MockDevicePolicy allow_3g_policy; |
| 348 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 349 | fake_system_state_.set_device_policy(&allow_3g_policy); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 350 | |
| 351 | set<string> allowed_set; |
| 352 | allowed_set.insert(cmut_.StringForConnectionType(kNetCellular)); |
| 353 | |
| 354 | // Return false for GetAllowedConnectionTypesForUpdate and see |
| 355 | // that updates are still blocked for 3G despite the value being in |
| 356 | // the string set above. |
| 357 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 358 | .Times(1) |
| 359 | .WillOnce(DoAll(SetArgumentPointee<0>(allowed_set), Return(false))); |
| 360 | |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 361 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 362 | NetworkTethering::kUnknown)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 365 | TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) { |
| 366 | policy::MockDevicePolicy no_policy; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 367 | testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 368 | |
Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 369 | fake_system_state_.set_device_policy(&no_policy); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 370 | |
| 371 | // No setting enforced by the device policy, user prefs should be used. |
| 372 | EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_)) |
| 373 | .Times(3) |
| 374 | .WillRepeatedly(Return(false)); |
| 375 | |
| 376 | // No user pref: block. |
| 377 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 378 | .Times(1) |
| 379 | .WillOnce(Return(false)); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 380 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 381 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 382 | |
| 383 | // Allow per user pref. |
| 384 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 385 | .Times(1) |
| 386 | .WillOnce(Return(true)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 387 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 388 | .Times(1) |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 389 | .WillOnce(DoAll(SetArgumentPointee<1>(true), Return(true))); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 390 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 391 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 392 | |
| 393 | // Block per user pref. |
| 394 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) |
| 395 | .Times(1) |
| 396 | .WillOnce(Return(true)); |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 397 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 398 | .Times(1) |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 399 | .WillOnce(DoAll(SetArgumentPointee<1>(false), Return(true))); |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 400 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(kNetCellular, |
| 401 | NetworkTethering::kUnknown)); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 404 | TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) { |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 405 | EXPECT_STREQ(shill::kTypeEthernet, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 406 | cmut_.StringForConnectionType(kNetEthernet)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 407 | EXPECT_STREQ(shill::kTypeWifi, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 408 | cmut_.StringForConnectionType(kNetWifi)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 409 | EXPECT_STREQ(shill::kTypeWimax, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 410 | cmut_.StringForConnectionType(kNetWimax)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 411 | EXPECT_STREQ(shill::kTypeBluetooth, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 412 | cmut_.StringForConnectionType(kNetBluetooth)); |
Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 413 | EXPECT_STREQ(shill::kTypeCellular, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 414 | cmut_.StringForConnectionType(kNetCellular)); |
| 415 | EXPECT_STREQ("Unknown", |
| 416 | cmut_.StringForConnectionType(kNetUnknown)); |
| 417 | EXPECT_STREQ("Unknown", |
| 418 | cmut_.StringForConnectionType( |
| 419 | static_cast<NetworkConnectionType>(999999))); |
| 420 | } |
| 421 | |
| 422 | TEST_F(ConnectionManagerTest, MalformedServiceList) { |
| 423 | SetupMocks("/service/guest-network"); |
Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 424 | SetManagerReply(kServicePath_, DBUS_TYPE_G_STRING_ARRAY); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 425 | |
| 426 | NetworkConnectionType type; |
Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 427 | NetworkTethering tethering; |
| 428 | EXPECT_FALSE(cmut_.GetConnectionProperties(&dbus_iface_, &type, &tethering)); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | } // namespace chromeos_update_engine |