blob: 48f619513a8bb794e070d58adc861882a97006cb [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
Sen Jiang255e22b2016-05-20 16:15:29 -070037using chromeos_update_engine::connection_utils::StringForConnectionType;
Alex Deymo30534502015-07-20 15:06:33 -070038using org::chromium::flimflam::ManagerProxyMock;
39using org::chromium::flimflam::ServiceProxyMock;
Jay Srinivasan43488792012-06-19 00:25:31 -070040using std::set;
41using std::string;
Jay Srinivasan43488792012-06-19 00:25:31 -070042using testing::Return;
Alex Deymo30534502015-07-20 15:06:33 -070043using testing::SetArgPointee;
Alex Deymof329b932014-10-30 01:37:48 -070044using testing::_;
Jay Srinivasan43488792012-06-19 00:25:31 -070045
46namespace chromeos_update_engine {
47
48class ConnectionManagerTest : public ::testing::Test {
49 public:
Alex Deymo30534502015-07-20 15:06:33 -070050 void SetUp() override {
51 loop_.SetAsCurrent();
Gilad Arnold5bb4c902014-04-10 12:32:13 -070052 fake_system_state_.set_connection_manager(&cmut_);
Jay Srinivasan43488792012-06-19 00:25:31 -070053 }
54
Alex Deymo30534502015-07-20 15:06:33 -070055 void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
Alex Deymo1c4e6382013-07-15 12:09:51 -070056
Alex Deymo30534502015-07-20 15:06:33 -070057 protected:
58 // Sets the default_service object path in the response from the
59 // ManagerProxyMock instance.
60 void SetManagerReply(const char* default_service, bool reply_succeeds);
61
62 // Sets the |service_type|, |physical_technology| and |service_tethering|
63 // properties in the mocked service |service_path|. If any of the three
64 // const char* is a nullptr, the corresponding property will not be included
65 // in the response.
66 void SetServiceReply(const string& service_path,
67 const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -070068 const char* physical_technology,
69 const char* service_tethering);
Alex Deymo30534502015-07-20 15:06:33 -070070
Jay Srinivasan43488792012-06-19 00:25:31 -070071 void TestWithServiceType(
Alex Deymo1c4e6382013-07-15 12:09:51 -070072 const char* service_type,
73 const char* physical_technology,
Sen Jiang255e22b2016-05-20 16:15:29 -070074 ConnectionType expected_type);
Alex Deymo6ae91202014-03-10 19:21:25 -070075 void TestWithServiceTethering(
76 const char* service_tethering,
Sen Jiang255e22b2016-05-20 16:15:29 -070077 ConnectionTethering expected_tethering);
Jay Srinivasan43488792012-06-19 00:25:31 -070078
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070079 brillo::FakeMessageLoop loop_{nullptr};
Gilad Arnold5bb4c902014-04-10 12:32:13 -070080 FakeSystemState fake_system_state_;
Alex Deymo30534502015-07-20 15:06:33 -070081 FakeShillProxy fake_shill_proxy_;
82
83 // ConnectionManager under test.
84 ConnectionManager cmut_{&fake_shill_proxy_, &fake_system_state_};
Jay Srinivasan43488792012-06-19 00:25:31 -070085};
86
Alex Deymo30534502015-07-20 15:06:33 -070087void ConnectionManagerTest::SetManagerReply(const char* default_service,
88 bool reply_succeeds) {
89 ManagerProxyMock* manager_proxy_mock = fake_shill_proxy_.GetManagerProxy();
90 if (!reply_succeeds) {
91 EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
92 .WillOnce(Return(false));
93 return;
94 }
Jay Srinivasan43488792012-06-19 00:25:31 -070095
Alex Deymo30534502015-07-20 15:06:33 -070096 // Create a dictionary of properties and optionally include the default
97 // service.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070098 brillo::VariantDictionary reply_dict;
Alex Deymo30534502015-07-20 15:06:33 -070099 reply_dict["SomeOtherProperty"] = 0xC0FFEE;
Jay Srinivasan43488792012-06-19 00:25:31 -0700100
Alex Deymo30534502015-07-20 15:06:33 -0700101 if (default_service) {
102 reply_dict[shill::kDefaultServiceProperty] =
103 dbus::ObjectPath(default_service);
104 }
105 EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
106 .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700107}
108
Alex Deymo30534502015-07-20 15:06:33 -0700109void ConnectionManagerTest::SetServiceReply(const string& service_path,
110 const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -0700111 const char* physical_technology,
112 const char* service_tethering) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700113 brillo::VariantDictionary reply_dict;
Alex Deymo30534502015-07-20 15:06:33 -0700114 reply_dict["SomeOtherProperty"] = 0xC0FFEE;
115
116 if (service_type)
117 reply_dict[shill::kTypeProperty] = string(service_type);
Jay Srinivasan43488792012-06-19 00:25:31 -0700118
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700119 if (physical_technology) {
Alex Deymo30534502015-07-20 15:06:33 -0700120 reply_dict[shill::kPhysicalTechnologyProperty] =
121 string(physical_technology);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700122 }
123
Alex Deymo30534502015-07-20 15:06:33 -0700124 if (service_tethering)
125 reply_dict[shill::kTetheringProperty] = string(service_tethering);
126
127 std::unique_ptr<ServiceProxyMock> service_proxy_mock(new ServiceProxyMock());
Alex Deymo6ae91202014-03-10 19:21:25 -0700128
Jay Srinivasan43488792012-06-19 00:25:31 -0700129 // Plumb return value into mock object.
Alex Deymo30534502015-07-20 15:06:33 -0700130 EXPECT_CALL(*service_proxy_mock.get(), GetProperties(_, _, _))
131 .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700132
Alex Deymo758dd532015-09-09 15:21:22 -0700133 fake_shill_proxy_.SetServiceForPath(dbus::ObjectPath(service_path),
Alex Deymo30534502015-07-20 15:06:33 -0700134 std::move(service_proxy_mock));
Jay Srinivasan43488792012-06-19 00:25:31 -0700135}
136
137void ConnectionManagerTest::TestWithServiceType(
138 const char* service_type,
Alex Deymo1c4e6382013-07-15 12:09:51 -0700139 const char* physical_technology,
Sen Jiang255e22b2016-05-20 16:15:29 -0700140 ConnectionType expected_type) {
Alex Deymo758dd532015-09-09 15:21:22 -0700141 SetManagerReply("/service/guest/network", true);
142 SetServiceReply("/service/guest/network",
Alex Deymo30534502015-07-20 15:06:33 -0700143 service_type,
144 physical_technology,
Alex Deymo6ae91202014-03-10 19:21:25 -0700145 shill::kTetheringNotDetectedState);
Jay Srinivasan43488792012-06-19 00:25:31 -0700146
Sen Jiang255e22b2016-05-20 16:15:29 -0700147 ConnectionType type;
148 ConnectionTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700149 EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700150 EXPECT_EQ(expected_type, type);
Alex Deymo30534502015-07-20 15:06:33 -0700151 testing::Mock::VerifyAndClearExpectations(
152 fake_shill_proxy_.GetManagerProxy());
Jay Srinivasan43488792012-06-19 00:25:31 -0700153}
154
Alex Deymo6ae91202014-03-10 19:21:25 -0700155void ConnectionManagerTest::TestWithServiceTethering(
156 const char* service_tethering,
Sen Jiang255e22b2016-05-20 16:15:29 -0700157 ConnectionTethering expected_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700158 SetManagerReply("/service/guest/network", true);
Alex Deymo30534502015-07-20 15:06:33 -0700159 SetServiceReply(
Alex Deymo758dd532015-09-09 15:21:22 -0700160 "/service/guest/network", shill::kTypeWifi, nullptr, service_tethering);
Alex Deymo6ae91202014-03-10 19:21:25 -0700161
Sen Jiang255e22b2016-05-20 16:15:29 -0700162 ConnectionType type;
163 ConnectionTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700164 EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
Alex Deymo6ae91202014-03-10 19:21:25 -0700165 EXPECT_EQ(expected_tethering, tethering);
Alex Deymo30534502015-07-20 15:06:33 -0700166 testing::Mock::VerifyAndClearExpectations(
167 fake_shill_proxy_.GetManagerProxy());
Alex Deymo6ae91202014-03-10 19:21:25 -0700168}
169
Jay Srinivasan43488792012-06-19 00:25:31 -0700170TEST_F(ConnectionManagerTest, SimpleTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700171 TestWithServiceType(shill::kTypeEthernet, nullptr, ConnectionType::kEthernet);
172 TestWithServiceType(shill::kTypeWifi, nullptr, ConnectionType::kWifi);
173 TestWithServiceType(shill::kTypeWimax, nullptr, ConnectionType::kWimax);
174 TestWithServiceType(
175 shill::kTypeBluetooth, nullptr, ConnectionType::kBluetooth);
176 TestWithServiceType(shill::kTypeCellular, nullptr, ConnectionType::kCellular);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700177}
178
179TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700180 TestWithServiceType(shill::kTypeVPN, nullptr, ConnectionType::kUnknown);
181 TestWithServiceType(
182 shill::kTypeVPN, shill::kTypeVPN, ConnectionType::kUnknown);
183 TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, ConnectionType::kWifi);
184 TestWithServiceType(
185 shill::kTypeVPN, shill::kTypeWimax, ConnectionType::kWimax);
Jay Srinivasan43488792012-06-19 00:25:31 -0700186}
187
Alex Deymo6ae91202014-03-10 19:21:25 -0700188TEST_F(ConnectionManagerTest, TetheringTest) {
189 TestWithServiceTethering(shill::kTetheringConfirmedState,
Sen Jiang255e22b2016-05-20 16:15:29 -0700190 ConnectionTethering::kConfirmed);
Alex Deymo6ae91202014-03-10 19:21:25 -0700191 TestWithServiceTethering(shill::kTetheringNotDetectedState,
Sen Jiang255e22b2016-05-20 16:15:29 -0700192 ConnectionTethering::kNotDetected);
Alex Deymo6ae91202014-03-10 19:21:25 -0700193 TestWithServiceTethering(shill::kTetheringSuspectedState,
Sen Jiang255e22b2016-05-20 16:15:29 -0700194 ConnectionTethering::kSuspected);
Alex Deymo6ae91202014-03-10 19:21:25 -0700195 TestWithServiceTethering("I'm not a valid property value =)",
Sen Jiang255e22b2016-05-20 16:15:29 -0700196 ConnectionTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700197}
198
Jay Srinivasan43488792012-06-19 00:25:31 -0700199TEST_F(ConnectionManagerTest, UnknownTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700200 TestWithServiceType("foo", nullptr, ConnectionType::kUnknown);
Jay Srinivasan43488792012-06-19 00:25:31 -0700201}
202
203TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700204 // Updates over Ethernet are allowed even if there's no policy.
Sen Jiang255e22b2016-05-20 16:15:29 -0700205 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
206 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700207}
208
209TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700210 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
211 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700212}
213
214TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700215 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWimax,
216 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700217}
218
219TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700220 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kBluetooth,
221 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700222}
223
224TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
225 policy::MockDevicePolicy allow_3g_policy;
226
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700227 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700228
Alex Deymof4867c42013-06-28 14:41:39 -0700229 // This test tests cellular (3G) being the only connection type being allowed.
Jay Srinivasan43488792012-06-19 00:25:31 -0700230 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700231 allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700232
233 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
234 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700235 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700236
Sen Jiang255e22b2016-05-20 16:15:29 -0700237 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
238 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700239}
240
241TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
242 policy::MockDevicePolicy allow_3g_policy;
243
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700244 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700245
246 // This test tests multiple connection types being allowed, with
Alex Deymof4867c42013-06-28 14:41:39 -0700247 // 3G one among them. Only Cellular is currently enforced by the policy
248 // setting, the others are ignored (see Bluetooth for example).
Jay Srinivasan43488792012-06-19 00:25:31 -0700249 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700250 allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
251 allowed_set.insert(StringForConnectionType(ConnectionType::kBluetooth));
Jay Srinivasan43488792012-06-19 00:25:31 -0700252
253 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
Alex Deymo6ae91202014-03-10 19:21:25 -0700254 .Times(3)
Alex Deymo30534502015-07-20 15:06:33 -0700255 .WillRepeatedly(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700256
Sen Jiang255e22b2016-05-20 16:15:29 -0700257 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
258 ConnectionTethering::kUnknown));
259 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
260 ConnectionTethering::kNotDetected));
261 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
262 ConnectionTethering::kUnknown));
263 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
264 ConnectionTethering::kUnknown));
265 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWimax,
266 ConnectionTethering::kUnknown));
267 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kBluetooth,
268 ConnectionTethering::kUnknown));
Alex Deymo6ae91202014-03-10 19:21:25 -0700269
270 // Tethered networks are treated in the same way as Cellular networks and
271 // thus allowed.
Sen Jiang255e22b2016-05-20 16:15:29 -0700272 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
273 ConnectionTethering::kConfirmed));
274 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
275 ConnectionTethering::kConfirmed));
Jay Srinivasan43488792012-06-19 00:25:31 -0700276}
277
Alex Deymof4867c42013-06-28 14:41:39 -0700278TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700279 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
280 ConnectionTethering::kUnknown));
Alex Deymo6ae91202014-03-10 19:21:25 -0700281}
282
283TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700284 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
285 ConnectionTethering::kConfirmed));
286 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
287 ConnectionTethering::kConfirmed));
288 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
289 ConnectionTethering::kSuspected));
Jay Srinivasan43488792012-06-19 00:25:31 -0700290}
291
292TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
293 policy::MockDevicePolicy block_3g_policy;
294
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700295 fake_system_state_.set_device_policy(&block_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700296
297 // Test that updates for 3G are blocked while updates are allowed
298 // over several other types.
299 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700300 allowed_set.insert(StringForConnectionType(ConnectionType::kEthernet));
301 allowed_set.insert(StringForConnectionType(ConnectionType::kWifi));
302 allowed_set.insert(StringForConnectionType(ConnectionType::kWimax));
Jay Srinivasan43488792012-06-19 00:25:31 -0700303
304 EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
305 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700306 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700307
Sen Jiang255e22b2016-05-20 16:15:29 -0700308 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
309 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700310}
311
312TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) {
313 policy::MockDevicePolicy allow_3g_policy;
314
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700315 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700316
317 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700318 allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700319
320 // Return false for GetAllowedConnectionTypesForUpdate and see
321 // that updates are still blocked for 3G despite the value being in
322 // the string set above.
323 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
324 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700325 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(false)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700326
Sen Jiang255e22b2016-05-20 16:15:29 -0700327 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
328 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700329}
330
Alex Deymof4867c42013-06-28 14:41:39 -0700331TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) {
332 policy::MockDevicePolicy no_policy;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800333 testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs();
Alex Deymof4867c42013-06-28 14:41:39 -0700334
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700335 fake_system_state_.set_device_policy(&no_policy);
Alex Deymof4867c42013-06-28 14:41:39 -0700336
337 // No setting enforced by the device policy, user prefs should be used.
338 EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_))
339 .Times(3)
340 .WillRepeatedly(Return(false));
341
342 // No user pref: block.
343 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
344 .Times(1)
345 .WillOnce(Return(false));
Sen Jiang255e22b2016-05-20 16:15:29 -0700346 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
347 ConnectionTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700348
349 // Allow per user pref.
350 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
351 .Times(1)
352 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700353 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700354 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700355 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700356 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
357 ConnectionTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700358
359 // Block per user pref.
360 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
361 .Times(1)
362 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700363 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700364 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700365 .WillOnce(DoAll(SetArgPointee<1>(false), Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700366 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
367 ConnectionTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700368}
369
Jay Srinivasan43488792012-06-19 00:25:31 -0700370TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
Ben Chanc6007e42013-09-19 23:49:22 -0700371 EXPECT_STREQ(shill::kTypeEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -0700372 StringForConnectionType(ConnectionType::kEthernet));
Ben Chanc6007e42013-09-19 23:49:22 -0700373 EXPECT_STREQ(shill::kTypeWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -0700374 StringForConnectionType(ConnectionType::kWifi));
Ben Chanc6007e42013-09-19 23:49:22 -0700375 EXPECT_STREQ(shill::kTypeWimax,
Sen Jiang255e22b2016-05-20 16:15:29 -0700376 StringForConnectionType(ConnectionType::kWimax));
Ben Chanc6007e42013-09-19 23:49:22 -0700377 EXPECT_STREQ(shill::kTypeBluetooth,
Sen Jiang255e22b2016-05-20 16:15:29 -0700378 StringForConnectionType(ConnectionType::kBluetooth));
Ben Chanc6007e42013-09-19 23:49:22 -0700379 EXPECT_STREQ(shill::kTypeCellular,
Sen Jiang255e22b2016-05-20 16:15:29 -0700380 StringForConnectionType(ConnectionType::kCellular));
381 EXPECT_STREQ("Unknown", StringForConnectionType(ConnectionType::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700382 EXPECT_STREQ("Unknown",
Sen Jiang255e22b2016-05-20 16:15:29 -0700383 StringForConnectionType(static_cast<ConnectionType>(999999)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700384}
385
386TEST_F(ConnectionManagerTest, MalformedServiceList) {
Alex Deymo758dd532015-09-09 15:21:22 -0700387 SetManagerReply("/service/guest/network", false);
Jay Srinivasan43488792012-06-19 00:25:31 -0700388
Sen Jiang255e22b2016-05-20 16:15:29 -0700389 ConnectionType type;
390 ConnectionTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700391 EXPECT_FALSE(cmut_.GetConnectionProperties(&type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700392}
393
394} // namespace chromeos_update_engine