blob: 612929b61b2ef851ad3cfc9b32bfe259c217ce45 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Srinivasan43488792012-06-19 00:25:31 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/connection_manager.h"
18
Alex Vakulenkod2779df2014-06-16 13:19:00 -070019#include <set>
20#include <string>
21
Jay Srinivasan43488792012-06-19 00:25:31 -070022#include <base/logging.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070023#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 Deymo5665d0c2014-05-28 17:45:43 -070027#include <gmock/gmock.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070028#include <gtest/gtest.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070029#include <shill/dbus-constants.h>
30#include <shill/dbus-proxies.h>
31#include <shill/dbus-proxy-mocks.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070032
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/test_utils.h"
Alex Deymo30534502015-07-20 15:06:33 -070034#include "update_engine/fake_shill_proxy.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070035#include "update_engine/fake_system_state.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070036
Alex Deymo30534502015-07-20 15:06:33 -070037using org::chromium::flimflam::ManagerProxyMock;
38using org::chromium::flimflam::ServiceProxyMock;
Jay Srinivasan43488792012-06-19 00:25:31 -070039using std::set;
40using std::string;
Jay Srinivasan43488792012-06-19 00:25:31 -070041using testing::Return;
Alex Deymo30534502015-07-20 15:06:33 -070042using testing::SetArgPointee;
Alex Deymof329b932014-10-30 01:37:48 -070043using testing::_;
Jay Srinivasan43488792012-06-19 00:25:31 -070044
45namespace chromeos_update_engine {
46
47class ConnectionManagerTest : public ::testing::Test {
48 public:
Alex Deymo30534502015-07-20 15:06:33 -070049 void SetUp() override {
50 loop_.SetAsCurrent();
Gilad Arnold5bb4c902014-04-10 12:32:13 -070051 fake_system_state_.set_connection_manager(&cmut_);
Jay Srinivasan43488792012-06-19 00:25:31 -070052 }
53
Alex Deymo30534502015-07-20 15:06:33 -070054 void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
Alex Deymo1c4e6382013-07-15 12:09:51 -070055
Alex Deymo30534502015-07-20 15:06:33 -070056 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 Deymo6ae91202014-03-10 19:21:25 -070067 const char* physical_technology,
68 const char* service_tethering);
Alex Deymo30534502015-07-20 15:06:33 -070069
Jay Srinivasan43488792012-06-19 00:25:31 -070070 void TestWithServiceType(
Alex Deymo1c4e6382013-07-15 12:09:51 -070071 const char* service_type,
72 const char* physical_technology,
73 NetworkConnectionType expected_type);
Alex Deymo6ae91202014-03-10 19:21:25 -070074 void TestWithServiceTethering(
75 const char* service_tethering,
76 NetworkTethering expected_tethering);
Jay Srinivasan43488792012-06-19 00:25:31 -070077
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070078 brillo::FakeMessageLoop loop_{nullptr};
Gilad Arnold5bb4c902014-04-10 12:32:13 -070079 FakeSystemState fake_system_state_;
Alex Deymo30534502015-07-20 15:06:33 -070080 FakeShillProxy fake_shill_proxy_;
81
82 // ConnectionManager under test.
83 ConnectionManager cmut_{&fake_shill_proxy_, &fake_system_state_};
Jay Srinivasan43488792012-06-19 00:25:31 -070084};
85
Alex Deymo30534502015-07-20 15:06:33 -070086void 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 Srinivasan43488792012-06-19 00:25:31 -070094
Alex Deymo30534502015-07-20 15:06:33 -070095 // Create a dictionary of properties and optionally include the default
96 // service.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070097 brillo::VariantDictionary reply_dict;
Alex Deymo30534502015-07-20 15:06:33 -070098 reply_dict["SomeOtherProperty"] = 0xC0FFEE;
Jay Srinivasan43488792012-06-19 00:25:31 -070099
Alex Deymo30534502015-07-20 15:06:33 -0700100 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 Srinivasan43488792012-06-19 00:25:31 -0700106}
107
Alex Deymo30534502015-07-20 15:06:33 -0700108void ConnectionManagerTest::SetServiceReply(const string& service_path,
109 const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -0700110 const char* physical_technology,
111 const char* service_tethering) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700112 brillo::VariantDictionary reply_dict;
Alex Deymo30534502015-07-20 15:06:33 -0700113 reply_dict["SomeOtherProperty"] = 0xC0FFEE;
114
115 if (service_type)
116 reply_dict[shill::kTypeProperty] = string(service_type);
Jay Srinivasan43488792012-06-19 00:25:31 -0700117
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700118 if (physical_technology) {
Alex Deymo30534502015-07-20 15:06:33 -0700119 reply_dict[shill::kPhysicalTechnologyProperty] =
120 string(physical_technology);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700121 }
122
Alex Deymo30534502015-07-20 15:06:33 -0700123 if (service_tethering)
124 reply_dict[shill::kTetheringProperty] = string(service_tethering);
125
126 std::unique_ptr<ServiceProxyMock> service_proxy_mock(new ServiceProxyMock());
Alex Deymo6ae91202014-03-10 19:21:25 -0700127
Jay Srinivasan43488792012-06-19 00:25:31 -0700128 // Plumb return value into mock object.
Alex Deymo30534502015-07-20 15:06:33 -0700129 EXPECT_CALL(*service_proxy_mock.get(), GetProperties(_, _, _))
130 .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700131
Alex Deymo758dd532015-09-09 15:21:22 -0700132 fake_shill_proxy_.SetServiceForPath(dbus::ObjectPath(service_path),
Alex Deymo30534502015-07-20 15:06:33 -0700133 std::move(service_proxy_mock));
Jay Srinivasan43488792012-06-19 00:25:31 -0700134}
135
136void ConnectionManagerTest::TestWithServiceType(
137 const char* service_type,
Alex Deymo1c4e6382013-07-15 12:09:51 -0700138 const char* physical_technology,
Jay Srinivasan43488792012-06-19 00:25:31 -0700139 NetworkConnectionType expected_type) {
Alex Deymo758dd532015-09-09 15:21:22 -0700140 SetManagerReply("/service/guest/network", true);
141 SetServiceReply("/service/guest/network",
Alex Deymo30534502015-07-20 15:06:33 -0700142 service_type,
143 physical_technology,
Alex Deymo6ae91202014-03-10 19:21:25 -0700144 shill::kTetheringNotDetectedState);
Jay Srinivasan43488792012-06-19 00:25:31 -0700145
146 NetworkConnectionType type;
Alex Deymo6ae91202014-03-10 19:21:25 -0700147 NetworkTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700148 EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700149 EXPECT_EQ(expected_type, type);
Alex Deymo30534502015-07-20 15:06:33 -0700150 testing::Mock::VerifyAndClearExpectations(
151 fake_shill_proxy_.GetManagerProxy());
Jay Srinivasan43488792012-06-19 00:25:31 -0700152}
153
Alex Deymo6ae91202014-03-10 19:21:25 -0700154void ConnectionManagerTest::TestWithServiceTethering(
155 const char* service_tethering,
156 NetworkTethering expected_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700157 SetManagerReply("/service/guest/network", true);
Alex Deymo30534502015-07-20 15:06:33 -0700158 SetServiceReply(
Alex Deymo758dd532015-09-09 15:21:22 -0700159 "/service/guest/network", shill::kTypeWifi, nullptr, service_tethering);
Alex Deymo6ae91202014-03-10 19:21:25 -0700160
161 NetworkConnectionType type;
162 NetworkTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700163 EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
Alex Deymo6ae91202014-03-10 19:21:25 -0700164 EXPECT_EQ(expected_tethering, tethering);
Alex Deymo30534502015-07-20 15:06:33 -0700165 testing::Mock::VerifyAndClearExpectations(
166 fake_shill_proxy_.GetManagerProxy());
Alex Deymo6ae91202014-03-10 19:21:25 -0700167}
168
Jay Srinivasan43488792012-06-19 00:25:31 -0700169TEST_F(ConnectionManagerTest, SimpleTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700170 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 Deymo1c4e6382013-07-15 12:09:51 -0700180}
181
182TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700183 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 Srinivasan43488792012-06-19 00:25:31 -0700191}
192
Alex Deymo6ae91202014-03-10 19:21:25 -0700193TEST_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 Srinivasan43488792012-06-19 00:25:31 -0700204TEST_F(ConnectionManagerTest, UnknownTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700205 TestWithServiceType("foo", nullptr, NetworkConnectionType::kUnknown);
Jay Srinivasan43488792012-06-19 00:25:31 -0700206}
207
208TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700209 // Updates over Ethernet are allowed even if there's no policy.
Alex Deymo75eac7e2015-07-29 13:39:14 -0700210 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700211 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700212}
213
214TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700215 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
216 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700217}
218
219TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700220 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWimax,
Alex Deymo6ae91202014-03-10 19:21:25 -0700221 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700222}
223
224TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700225 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kBluetooth,
Alex Deymo6ae91202014-03-10 19:21:25 -0700226 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700227}
228
229TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
230 policy::MockDevicePolicy allow_3g_policy;
231
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700232 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700233
Alex Deymof4867c42013-06-28 14:41:39 -0700234 // This test tests cellular (3G) being the only connection type being allowed.
Jay Srinivasan43488792012-06-19 00:25:31 -0700235 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700236 allowed_set.insert(
237 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700238
239 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
240 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700241 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700242
Alex Deymo75eac7e2015-07-29 13:39:14 -0700243 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700244 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700245}
246
247TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
248 policy::MockDevicePolicy allow_3g_policy;
249
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700250 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700251
252 // This test tests multiple connection types being allowed, with
Alex Deymof4867c42013-06-28 14:41:39 -0700253 // 3G one among them. Only Cellular is currently enforced by the policy
254 // setting, the others are ignored (see Bluetooth for example).
Jay Srinivasan43488792012-06-19 00:25:31 -0700255 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700256 allowed_set.insert(
257 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
258 allowed_set.insert(
259 cmut_.StringForConnectionType(NetworkConnectionType::kBluetooth));
Jay Srinivasan43488792012-06-19 00:25:31 -0700260
261 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
Alex Deymo6ae91202014-03-10 19:21:25 -0700262 .Times(3)
Alex Deymo30534502015-07-20 15:06:33 -0700263 .WillRepeatedly(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700264
Alex Deymo75eac7e2015-07-29 13:39:14 -0700265 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700266 NetworkTethering::kUnknown));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700267 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700268 NetworkTethering::kNotDetected));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700269 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700270 NetworkTethering::kUnknown));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700271 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 Deymo6ae91202014-03-10 19:21:25 -0700276 NetworkTethering::kUnknown));
277
278 // Tethered networks are treated in the same way as Cellular networks and
279 // thus allowed.
Alex Deymo75eac7e2015-07-29 13:39:14 -0700280 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700281 NetworkTethering::kConfirmed));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700282 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
Alex Deymo6ae91202014-03-10 19:21:25 -0700283 NetworkTethering::kConfirmed));
Jay Srinivasan43488792012-06-19 00:25:31 -0700284}
285
Alex Deymof4867c42013-06-28 14:41:39 -0700286TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700287 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700288 NetworkTethering::kUnknown));
289}
290
291TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) {
Alex Deymo75eac7e2015-07-29 13:39:14 -0700292 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
Alex Deymo6ae91202014-03-10 19:21:25 -0700293 NetworkTethering::kConfirmed));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700294 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kEthernet,
Alex Deymo6ae91202014-03-10 19:21:25 -0700295 NetworkTethering::kConfirmed));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700296 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kWifi,
Alex Deymo6ae91202014-03-10 19:21:25 -0700297 NetworkTethering::kSuspected));
Jay Srinivasan43488792012-06-19 00:25:31 -0700298}
299
300TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
301 policy::MockDevicePolicy block_3g_policy;
302
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700303 fake_system_state_.set_device_policy(&block_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700304
305 // Test that updates for 3G are blocked while updates are allowed
306 // over several other types.
307 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700308 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 Srinivasan43488792012-06-19 00:25:31 -0700314
315 EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
316 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700317 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700318
Alex Deymo75eac7e2015-07-29 13:39:14 -0700319 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700320 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700321}
322
323TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) {
324 policy::MockDevicePolicy allow_3g_policy;
325
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700326 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700327
328 set<string> allowed_set;
Alex Deymo75eac7e2015-07-29 13:39:14 -0700329 allowed_set.insert(
330 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700331
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 Deymo30534502015-07-20 15:06:33 -0700337 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(false)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700338
Alex Deymo75eac7e2015-07-29 13:39:14 -0700339 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700340 NetworkTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700341}
342
Alex Deymof4867c42013-06-28 14:41:39 -0700343TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) {
344 policy::MockDevicePolicy no_policy;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800345 testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs();
Alex Deymof4867c42013-06-28 14:41:39 -0700346
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700347 fake_system_state_.set_device_policy(&no_policy);
Alex Deymof4867c42013-06-28 14:41:39 -0700348
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 Deymo75eac7e2015-07-29 13:39:14 -0700358 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700359 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700360
361 // Allow per user pref.
362 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
363 .Times(1)
364 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700365 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700366 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700367 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700368 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700369 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700370
371 // Block per user pref.
372 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
373 .Times(1)
374 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700375 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700376 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700377 .WillOnce(DoAll(SetArgPointee<1>(false), Return(true)));
Alex Deymo75eac7e2015-07-29 13:39:14 -0700378 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(NetworkConnectionType::kCellular,
Alex Deymo6ae91202014-03-10 19:21:25 -0700379 NetworkTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700380}
381
Jay Srinivasan43488792012-06-19 00:25:31 -0700382TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
Ben Chanc6007e42013-09-19 23:49:22 -0700383 EXPECT_STREQ(shill::kTypeEthernet,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700384 cmut_.StringForConnectionType(NetworkConnectionType::kEthernet));
Ben Chanc6007e42013-09-19 23:49:22 -0700385 EXPECT_STREQ(shill::kTypeWifi,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700386 cmut_.StringForConnectionType(NetworkConnectionType::kWifi));
Ben Chanc6007e42013-09-19 23:49:22 -0700387 EXPECT_STREQ(shill::kTypeWimax,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700388 cmut_.StringForConnectionType(NetworkConnectionType::kWimax));
Ben Chanc6007e42013-09-19 23:49:22 -0700389 EXPECT_STREQ(shill::kTypeBluetooth,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700390 cmut_.StringForConnectionType(
391 NetworkConnectionType::kBluetooth));
Ben Chanc6007e42013-09-19 23:49:22 -0700392 EXPECT_STREQ(shill::kTypeCellular,
Alex Deymo75eac7e2015-07-29 13:39:14 -0700393 cmut_.StringForConnectionType(NetworkConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700394 EXPECT_STREQ("Unknown",
Alex Deymo75eac7e2015-07-29 13:39:14 -0700395 cmut_.StringForConnectionType(NetworkConnectionType::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700396 EXPECT_STREQ("Unknown",
397 cmut_.StringForConnectionType(
398 static_cast<NetworkConnectionType>(999999)));
399}
400
401TEST_F(ConnectionManagerTest, MalformedServiceList) {
Alex Deymo758dd532015-09-09 15:21:22 -0700402 SetManagerReply("/service/guest/network", false);
Jay Srinivasan43488792012-06-19 00:25:31 -0700403
404 NetworkConnectionType type;
Alex Deymo6ae91202014-03-10 19:21:25 -0700405 NetworkTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700406 EXPECT_FALSE(cmut_.GetConnectionProperties(&type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700407}
408
409} // namespace chromeos_update_engine