| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 17 | #include "update_engine/connection_manager.h" | 
|  | 18 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 19 | #include <set> | 
|  | 20 | #include <string> | 
|  | 21 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 22 | #include <base/logging.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 23 | #include <brillo/any.h> | 
|  | 24 | #include <brillo/make_unique_ptr.h> | 
|  | 25 | #include <brillo/message_loops/fake_message_loop.h> | 
|  | 26 | #include <brillo/variant_dictionary.h> | 
| Alex Deymo | 5665d0c | 2014-05-28 17:45:43 -0700 | [diff] [blame] | 27 | #include <gmock/gmock.h> | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 28 | #include <gtest/gtest.h> | 
| Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 29 | #include <shill/dbus-constants.h> | 
|  | 30 | #include <shill/dbus-proxies.h> | 
|  | 31 | #include <shill/dbus-proxy-mocks.h> | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 32 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/test_utils.h" | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 34 | #include "update_engine/fake_shill_proxy.h" | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 35 | #include "update_engine/fake_system_state.h" | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 36 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 37 | using org::chromium::flimflam::ManagerProxyMock; | 
|  | 38 | using org::chromium::flimflam::ServiceProxyMock; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 39 | using std::set; | 
|  | 40 | using std::string; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 41 | using testing::Return; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 42 | using testing::SetArgPointee; | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 43 | using testing::_; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | namespace chromeos_update_engine { | 
|  | 46 |  | 
|  | 47 | class ConnectionManagerTest : public ::testing::Test { | 
|  | 48 | public: | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 49 | void SetUp() override { | 
|  | 50 | loop_.SetAsCurrent(); | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 51 | fake_system_state_.set_connection_manager(&cmut_); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 54 | void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); } | 
| Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 55 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 56 | protected: | 
|  | 57 | // Sets the default_service object path in the response from the | 
|  | 58 | // ManagerProxyMock instance. | 
|  | 59 | void SetManagerReply(const char* default_service, bool reply_succeeds); | 
|  | 60 |  | 
|  | 61 | // Sets the |service_type|, |physical_technology| and |service_tethering| | 
|  | 62 | // properties in the mocked service |service_path|. If any of the three | 
|  | 63 | // const char* is a nullptr, the corresponding property will not be included | 
|  | 64 | // in the response. | 
|  | 65 | void SetServiceReply(const string& service_path, | 
|  | 66 | const char* service_type, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 67 | const char* physical_technology, | 
|  | 68 | const char* service_tethering); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 69 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 70 | void TestWithServiceType( | 
| Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 71 | const char* service_type, | 
|  | 72 | const char* physical_technology, | 
|  | 73 | NetworkConnectionType expected_type); | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 74 | void TestWithServiceTethering( | 
|  | 75 | const char* service_tethering, | 
|  | 76 | NetworkTethering expected_tethering); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 77 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 78 | brillo::FakeMessageLoop loop_{nullptr}; | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 79 | FakeSystemState fake_system_state_; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 80 | FakeShillProxy fake_shill_proxy_; | 
|  | 81 |  | 
|  | 82 | // ConnectionManager under test. | 
|  | 83 | ConnectionManager cmut_{&fake_shill_proxy_, &fake_system_state_}; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 84 | }; | 
|  | 85 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 86 | void ConnectionManagerTest::SetManagerReply(const char* default_service, | 
|  | 87 | bool reply_succeeds) { | 
|  | 88 | ManagerProxyMock* manager_proxy_mock = fake_shill_proxy_.GetManagerProxy(); | 
|  | 89 | if (!reply_succeeds) { | 
|  | 90 | EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _)) | 
|  | 91 | .WillOnce(Return(false)); | 
|  | 92 | return; | 
|  | 93 | } | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 94 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 95 | // Create a dictionary of properties and optionally include the default | 
|  | 96 | // service. | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 97 | brillo::VariantDictionary reply_dict; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 98 | reply_dict["SomeOtherProperty"] = 0xC0FFEE; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 99 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 100 | if (default_service) { | 
|  | 101 | reply_dict[shill::kDefaultServiceProperty] = | 
|  | 102 | dbus::ObjectPath(default_service); | 
|  | 103 | } | 
|  | 104 | EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _)) | 
|  | 105 | .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true))); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 108 | void ConnectionManagerTest::SetServiceReply(const string& service_path, | 
|  | 109 | const char* service_type, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 110 | const char* physical_technology, | 
|  | 111 | const char* service_tethering) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 112 | brillo::VariantDictionary reply_dict; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 113 | reply_dict["SomeOtherProperty"] = 0xC0FFEE; | 
|  | 114 |  | 
|  | 115 | if (service_type) | 
|  | 116 | reply_dict[shill::kTypeProperty] = string(service_type); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 117 |  | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 118 | if (physical_technology) { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 119 | reply_dict[shill::kPhysicalTechnologyProperty] = | 
|  | 120 | string(physical_technology); | 
| Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 123 | if (service_tethering) | 
|  | 124 | reply_dict[shill::kTetheringProperty] = string(service_tethering); | 
|  | 125 |  | 
|  | 126 | std::unique_ptr<ServiceProxyMock> service_proxy_mock(new ServiceProxyMock()); | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 127 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 128 | // Plumb return value into mock object. | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 129 | EXPECT_CALL(*service_proxy_mock.get(), GetProperties(_, _, _)) | 
|  | 130 | .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true))); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 131 |  | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 132 | fake_shill_proxy_.SetServiceForPath(dbus::ObjectPath(service_path), | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 133 | std::move(service_proxy_mock)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
|  | 136 | void ConnectionManagerTest::TestWithServiceType( | 
|  | 137 | const char* service_type, | 
| Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 138 | const char* physical_technology, | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 139 | NetworkConnectionType expected_type) { | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 140 | SetManagerReply("/service/guest/network", true); | 
|  | 141 | SetServiceReply("/service/guest/network", | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 142 | service_type, | 
|  | 143 | physical_technology, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 144 | shill::kTetheringNotDetectedState); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 145 |  | 
|  | 146 | NetworkConnectionType type; | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 147 | NetworkTethering tethering; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 148 | EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 149 | EXPECT_EQ(expected_type, type); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 150 | testing::Mock::VerifyAndClearExpectations( | 
|  | 151 | fake_shill_proxy_.GetManagerProxy()); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 154 | void ConnectionManagerTest::TestWithServiceTethering( | 
|  | 155 | const char* service_tethering, | 
|  | 156 | NetworkTethering expected_tethering) { | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 157 | SetManagerReply("/service/guest/network", true); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 158 | SetServiceReply( | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 159 | "/service/guest/network", shill::kTypeWifi, nullptr, service_tethering); | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 160 |  | 
|  | 161 | NetworkConnectionType type; | 
|  | 162 | NetworkTethering tethering; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering)); | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 164 | EXPECT_EQ(expected_tethering, tethering); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 165 | testing::Mock::VerifyAndClearExpectations( | 
|  | 166 | fake_shill_proxy_.GetManagerProxy()); | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 169 | TEST_F(ConnectionManagerTest, SimpleTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 170 | TestWithServiceType(shill::kTypeEthernet, nullptr, | 
|  | 171 | NetworkConnectionType::kEthernet); | 
|  | 172 | TestWithServiceType(shill::kTypeWifi, nullptr, | 
|  | 173 | NetworkConnectionType::kWifi); | 
|  | 174 | TestWithServiceType(shill::kTypeWimax, nullptr, | 
|  | 175 | NetworkConnectionType::kWimax); | 
|  | 176 | TestWithServiceType(shill::kTypeBluetooth, nullptr, | 
|  | 177 | NetworkConnectionType::kBluetooth); | 
|  | 178 | TestWithServiceType(shill::kTypeCellular, nullptr, | 
|  | 179 | NetworkConnectionType::kCellular); | 
| Alex Deymo | 1c4e638 | 2013-07-15 12:09:51 -0700 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
|  | 182 | TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 183 | TestWithServiceType(shill::kTypeVPN, nullptr, | 
|  | 184 | NetworkConnectionType::kUnknown); | 
|  | 185 | TestWithServiceType(shill::kTypeVPN, shill::kTypeVPN, | 
|  | 186 | NetworkConnectionType::kUnknown); | 
|  | 187 | TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, | 
|  | 188 | NetworkConnectionType::kWifi); | 
|  | 189 | TestWithServiceType(shill::kTypeVPN, shill::kTypeWimax, | 
|  | 190 | NetworkConnectionType::kWimax); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 193 | TEST_F(ConnectionManagerTest, TetheringTest) { | 
|  | 194 | TestWithServiceTethering(shill::kTetheringConfirmedState, | 
|  | 195 | NetworkTethering::kConfirmed); | 
|  | 196 | TestWithServiceTethering(shill::kTetheringNotDetectedState, | 
|  | 197 | NetworkTethering::kNotDetected); | 
|  | 198 | TestWithServiceTethering(shill::kTetheringSuspectedState, | 
|  | 199 | NetworkTethering::kSuspected); | 
|  | 200 | TestWithServiceTethering("I'm not a valid property value =)", | 
|  | 201 | NetworkTethering::kUnknown); | 
|  | 202 | } | 
|  | 203 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 204 | TEST_F(ConnectionManagerTest, UnknownTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 205 | TestWithServiceType("foo", nullptr, NetworkConnectionType::kUnknown); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
|  | 208 | TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) { | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 209 | // Updates over Ethernet are allowed even if there's no policy. | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 210 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 211 | NetworkTethering::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
|  | 214 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 215 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, | 
|  | 216 | NetworkTethering::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
|  | 219 | TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 220 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWimax, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 221 | NetworkTethering::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
|  | 224 | TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 225 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kBluetooth, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 226 | NetworkTethering::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
|  | 229 | TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) { | 
|  | 230 | policy::MockDevicePolicy allow_3g_policy; | 
|  | 231 |  | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 232 | fake_system_state_.set_device_policy(&allow_3g_policy); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 233 |  | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 234 | // This test tests cellular (3G) being the only connection type being allowed. | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 235 | set<string> allowed_set; | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 236 | allowed_set.insert( | 
|  | 237 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 238 |  | 
|  | 239 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) | 
|  | 240 | .Times(1) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 241 | .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true))); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 242 |  | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 243 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 244 | NetworkTethering::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
|  | 247 | TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) { | 
|  | 248 | policy::MockDevicePolicy allow_3g_policy; | 
|  | 249 |  | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 250 | fake_system_state_.set_device_policy(&allow_3g_policy); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 251 |  | 
|  | 252 | // This test tests multiple connection types being allowed, with | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 253 | // 3G one among them. Only Cellular is currently enforced by the policy | 
|  | 254 | // setting, the others are ignored (see Bluetooth for example). | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 255 | set<string> allowed_set; | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 256 | allowed_set.insert( | 
|  | 257 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); | 
|  | 258 | allowed_set.insert( | 
|  | 259 | cmut_.StringForConnectionType(NetworkConnectionType::kBluetooth)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 260 |  | 
|  | 261 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 262 | .Times(3) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 263 | .WillRepeatedly(DoAll(SetArgPointee<0>(allowed_set), Return(true))); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 264 |  | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 265 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 266 | NetworkTethering::kUnknown)); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 267 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 268 | NetworkTethering::kNotDetected)); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 269 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 270 | NetworkTethering::kUnknown)); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 271 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, | 
|  | 272 | NetworkTethering::kUnknown)); | 
|  | 273 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWimax, | 
|  | 274 | NetworkTethering::kUnknown)); | 
|  | 275 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kBluetooth, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 276 | NetworkTethering::kUnknown)); | 
|  | 277 |  | 
|  | 278 | // Tethered networks are treated in the same way as Cellular networks and | 
|  | 279 | // thus allowed. | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 280 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 281 | NetworkTethering::kConfirmed)); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 282 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 283 | NetworkTethering::kConfirmed)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 284 | } | 
|  | 285 |  | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 286 | TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 287 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 288 | NetworkTethering::kUnknown)); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) { | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 292 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 293 | NetworkTethering::kConfirmed)); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 294 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 295 | NetworkTethering::kConfirmed)); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 296 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 297 | NetworkTethering::kSuspected)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
|  | 300 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) { | 
|  | 301 | policy::MockDevicePolicy block_3g_policy; | 
|  | 302 |  | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 303 | fake_system_state_.set_device_policy(&block_3g_policy); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 304 |  | 
|  | 305 | // Test that updates for 3G are blocked while updates are allowed | 
|  | 306 | // over several other types. | 
|  | 307 | set<string> allowed_set; | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 308 | allowed_set.insert( | 
|  | 309 | cmut_.StringForConnectionType(NetworkConnectionType::kEthernet)); | 
|  | 310 | allowed_set.insert( | 
|  | 311 | cmut_.StringForConnectionType(NetworkConnectionType::kWifi)); | 
|  | 312 | allowed_set.insert( | 
|  | 313 | cmut_.StringForConnectionType(NetworkConnectionType::kWimax)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 314 |  | 
|  | 315 | EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_)) | 
|  | 316 | .Times(1) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 317 | .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true))); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 318 |  | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 319 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 320 | NetworkTethering::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 321 | } | 
|  | 322 |  | 
|  | 323 | TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) { | 
|  | 324 | policy::MockDevicePolicy allow_3g_policy; | 
|  | 325 |  | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 326 | fake_system_state_.set_device_policy(&allow_3g_policy); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 327 |  | 
|  | 328 | set<string> allowed_set; | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 329 | allowed_set.insert( | 
|  | 330 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 331 |  | 
|  | 332 | // Return false for GetAllowedConnectionTypesForUpdate and see | 
|  | 333 | // that updates are still blocked for 3G despite the value being in | 
|  | 334 | // the string set above. | 
|  | 335 | EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_)) | 
|  | 336 | .Times(1) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 337 | .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(false))); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 338 |  | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 339 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 340 | NetworkTethering::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 343 | TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) { | 
|  | 344 | policy::MockDevicePolicy no_policy; | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 345 | testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs(); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 346 |  | 
| Gilad Arnold | 5bb4c90 | 2014-04-10 12:32:13 -0700 | [diff] [blame] | 347 | fake_system_state_.set_device_policy(&no_policy); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 348 |  | 
|  | 349 | // No setting enforced by the device policy, user prefs should be used. | 
|  | 350 | EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_)) | 
|  | 351 | .Times(3) | 
|  | 352 | .WillRepeatedly(Return(false)); | 
|  | 353 |  | 
|  | 354 | // No user pref: block. | 
|  | 355 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) | 
|  | 356 | .Times(1) | 
|  | 357 | .WillOnce(Return(false)); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 358 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 359 | NetworkTethering::kUnknown)); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 360 |  | 
|  | 361 | // Allow per user pref. | 
|  | 362 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) | 
|  | 363 | .Times(1) | 
|  | 364 | .WillOnce(Return(true)); | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 365 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 366 | .Times(1) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 367 | .WillOnce(DoAll(SetArgPointee<1>(true), Return(true))); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 368 | EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 369 | NetworkTethering::kUnknown)); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 370 |  | 
|  | 371 | // Block per user pref. | 
|  | 372 | EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission)) | 
|  | 373 | .Times(1) | 
|  | 374 | .WillOnce(Return(true)); | 
| Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 375 | EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _)) | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 376 | .Times(1) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 377 | .WillOnce(DoAll(SetArgPointee<1>(false), Return(true))); | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 378 | EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular, | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 379 | NetworkTethering::kUnknown)); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 382 | TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) { | 
| Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 383 | EXPECT_STREQ(shill::kTypeEthernet, | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 384 | cmut_.StringForConnectionType(NetworkConnectionType::kEthernet)); | 
| Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 385 | EXPECT_STREQ(shill::kTypeWifi, | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 386 | cmut_.StringForConnectionType(NetworkConnectionType::kWifi)); | 
| Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 387 | EXPECT_STREQ(shill::kTypeWimax, | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 388 | cmut_.StringForConnectionType(NetworkConnectionType::kWimax)); | 
| Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 389 | EXPECT_STREQ(shill::kTypeBluetooth, | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 390 | cmut_.StringForConnectionType( | 
|  | 391 | NetworkConnectionType::kBluetooth)); | 
| Ben Chan | c6007e4 | 2013-09-19 23:49:22 -0700 | [diff] [blame] | 392 | EXPECT_STREQ(shill::kTypeCellular, | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 393 | cmut_.StringForConnectionType(NetworkConnectionType::kCellular)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 394 | EXPECT_STREQ("Unknown", | 
| Alex Deymo | 75eac7e | 2015-07-29 13:39:14 -0700 | [diff] [blame] | 395 | cmut_.StringForConnectionType(NetworkConnectionType::kUnknown)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 396 | EXPECT_STREQ("Unknown", | 
|  | 397 | cmut_.StringForConnectionType( | 
|  | 398 | static_cast<NetworkConnectionType>(999999))); | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | TEST_F(ConnectionManagerTest, MalformedServiceList) { | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 402 | SetManagerReply("/service/guest/network", false); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 403 |  | 
|  | 404 | NetworkConnectionType type; | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 405 | NetworkTethering tethering; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 406 | EXPECT_FALSE(cmut_.GetConnectionProperties(&type, &tethering)); | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 407 | } | 
|  | 408 |  | 
|  | 409 | }  // namespace chromeos_update_engine |