blob: 0bb55479f8e213f50fc493811677a34a492b1383 [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:
Sen Jiangf5bebae2016-06-03 15:36:54 -070050 ConnectionManagerTest() : fake_shill_proxy_(new FakeShillProxy()) {}
51
Alex Deymo30534502015-07-20 15:06:33 -070052 void SetUp() override {
53 loop_.SetAsCurrent();
Gilad Arnold5bb4c902014-04-10 12:32:13 -070054 fake_system_state_.set_connection_manager(&cmut_);
Jay Srinivasan43488792012-06-19 00:25:31 -070055 }
56
Alex Deymo30534502015-07-20 15:06:33 -070057 void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
Alex Deymo1c4e6382013-07-15 12:09:51 -070058
Alex Deymo30534502015-07-20 15:06:33 -070059 protected:
60 // Sets the default_service object path in the response from the
61 // ManagerProxyMock instance.
62 void SetManagerReply(const char* default_service, bool reply_succeeds);
63
64 // Sets the |service_type|, |physical_technology| and |service_tethering|
65 // properties in the mocked service |service_path|. If any of the three
66 // const char* is a nullptr, the corresponding property will not be included
67 // in the response.
68 void SetServiceReply(const string& service_path,
69 const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -070070 const char* physical_technology,
71 const char* service_tethering);
Alex Deymo30534502015-07-20 15:06:33 -070072
Jay Srinivasan43488792012-06-19 00:25:31 -070073 void TestWithServiceType(
Alex Deymo1c4e6382013-07-15 12:09:51 -070074 const char* service_type,
75 const char* physical_technology,
Sen Jiang255e22b2016-05-20 16:15:29 -070076 ConnectionType expected_type);
Alex Deymo6ae91202014-03-10 19:21:25 -070077 void TestWithServiceTethering(
78 const char* service_tethering,
Sen Jiang255e22b2016-05-20 16:15:29 -070079 ConnectionTethering expected_tethering);
Jay Srinivasan43488792012-06-19 00:25:31 -070080
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070081 brillo::FakeMessageLoop loop_{nullptr};
Gilad Arnold5bb4c902014-04-10 12:32:13 -070082 FakeSystemState fake_system_state_;
Sen Jiangf5bebae2016-06-03 15:36:54 -070083 FakeShillProxy* fake_shill_proxy_;
Alex Deymo30534502015-07-20 15:06:33 -070084
85 // ConnectionManager under test.
Sen Jiangf5bebae2016-06-03 15:36:54 -070086 ConnectionManager cmut_{fake_shill_proxy_, &fake_system_state_};
Jay Srinivasan43488792012-06-19 00:25:31 -070087};
88
Alex Deymo30534502015-07-20 15:06:33 -070089void ConnectionManagerTest::SetManagerReply(const char* default_service,
90 bool reply_succeeds) {
Sen Jiangf5bebae2016-06-03 15:36:54 -070091 ManagerProxyMock* manager_proxy_mock = fake_shill_proxy_->GetManagerProxy();
Alex Deymo30534502015-07-20 15:06:33 -070092 if (!reply_succeeds) {
93 EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
94 .WillOnce(Return(false));
95 return;
96 }
Jay Srinivasan43488792012-06-19 00:25:31 -070097
Alex Deymo30534502015-07-20 15:06:33 -070098 // Create a dictionary of properties and optionally include the default
99 // service.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700100 brillo::VariantDictionary reply_dict;
Alex Deymo30534502015-07-20 15:06:33 -0700101 reply_dict["SomeOtherProperty"] = 0xC0FFEE;
Jay Srinivasan43488792012-06-19 00:25:31 -0700102
Alex Deymo30534502015-07-20 15:06:33 -0700103 if (default_service) {
104 reply_dict[shill::kDefaultServiceProperty] =
105 dbus::ObjectPath(default_service);
106 }
107 EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
108 .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700109}
110
Alex Deymo30534502015-07-20 15:06:33 -0700111void ConnectionManagerTest::SetServiceReply(const string& service_path,
112 const char* service_type,
Alex Deymo6ae91202014-03-10 19:21:25 -0700113 const char* physical_technology,
114 const char* service_tethering) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700115 brillo::VariantDictionary reply_dict;
Alex Deymo30534502015-07-20 15:06:33 -0700116 reply_dict["SomeOtherProperty"] = 0xC0FFEE;
117
118 if (service_type)
119 reply_dict[shill::kTypeProperty] = string(service_type);
Jay Srinivasan43488792012-06-19 00:25:31 -0700120
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700121 if (physical_technology) {
Alex Deymo30534502015-07-20 15:06:33 -0700122 reply_dict[shill::kPhysicalTechnologyProperty] =
123 string(physical_technology);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700124 }
125
Alex Deymo30534502015-07-20 15:06:33 -0700126 if (service_tethering)
127 reply_dict[shill::kTetheringProperty] = string(service_tethering);
128
129 std::unique_ptr<ServiceProxyMock> service_proxy_mock(new ServiceProxyMock());
Alex Deymo6ae91202014-03-10 19:21:25 -0700130
Jay Srinivasan43488792012-06-19 00:25:31 -0700131 // Plumb return value into mock object.
Alex Deymo30534502015-07-20 15:06:33 -0700132 EXPECT_CALL(*service_proxy_mock.get(), GetProperties(_, _, _))
133 .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700134
Sen Jiangf5bebae2016-06-03 15:36:54 -0700135 fake_shill_proxy_->SetServiceForPath(dbus::ObjectPath(service_path),
136 std::move(service_proxy_mock));
Jay Srinivasan43488792012-06-19 00:25:31 -0700137}
138
139void ConnectionManagerTest::TestWithServiceType(
140 const char* service_type,
Alex Deymo1c4e6382013-07-15 12:09:51 -0700141 const char* physical_technology,
Sen Jiang255e22b2016-05-20 16:15:29 -0700142 ConnectionType expected_type) {
Alex Deymo758dd532015-09-09 15:21:22 -0700143 SetManagerReply("/service/guest/network", true);
144 SetServiceReply("/service/guest/network",
Alex Deymo30534502015-07-20 15:06:33 -0700145 service_type,
146 physical_technology,
Alex Deymo6ae91202014-03-10 19:21:25 -0700147 shill::kTetheringNotDetectedState);
Jay Srinivasan43488792012-06-19 00:25:31 -0700148
Sen Jiang255e22b2016-05-20 16:15:29 -0700149 ConnectionType type;
150 ConnectionTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700151 EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700152 EXPECT_EQ(expected_type, type);
Alex Deymo30534502015-07-20 15:06:33 -0700153 testing::Mock::VerifyAndClearExpectations(
Sen Jiangf5bebae2016-06-03 15:36:54 -0700154 fake_shill_proxy_->GetManagerProxy());
Jay Srinivasan43488792012-06-19 00:25:31 -0700155}
156
Alex Deymo6ae91202014-03-10 19:21:25 -0700157void ConnectionManagerTest::TestWithServiceTethering(
158 const char* service_tethering,
Sen Jiang255e22b2016-05-20 16:15:29 -0700159 ConnectionTethering expected_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700160 SetManagerReply("/service/guest/network", true);
Alex Deymo30534502015-07-20 15:06:33 -0700161 SetServiceReply(
Alex Deymo758dd532015-09-09 15:21:22 -0700162 "/service/guest/network", shill::kTypeWifi, nullptr, service_tethering);
Alex Deymo6ae91202014-03-10 19:21:25 -0700163
Sen Jiang255e22b2016-05-20 16:15:29 -0700164 ConnectionType type;
165 ConnectionTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700166 EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
Alex Deymo6ae91202014-03-10 19:21:25 -0700167 EXPECT_EQ(expected_tethering, tethering);
Alex Deymo30534502015-07-20 15:06:33 -0700168 testing::Mock::VerifyAndClearExpectations(
Sen Jiangf5bebae2016-06-03 15:36:54 -0700169 fake_shill_proxy_->GetManagerProxy());
Alex Deymo6ae91202014-03-10 19:21:25 -0700170}
171
Jay Srinivasan43488792012-06-19 00:25:31 -0700172TEST_F(ConnectionManagerTest, SimpleTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700173 TestWithServiceType(shill::kTypeEthernet, nullptr, ConnectionType::kEthernet);
174 TestWithServiceType(shill::kTypeWifi, nullptr, ConnectionType::kWifi);
175 TestWithServiceType(shill::kTypeWimax, nullptr, ConnectionType::kWimax);
176 TestWithServiceType(
177 shill::kTypeBluetooth, nullptr, ConnectionType::kBluetooth);
178 TestWithServiceType(shill::kTypeCellular, nullptr, ConnectionType::kCellular);
Alex Deymo1c4e6382013-07-15 12:09:51 -0700179}
180
181TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700182 TestWithServiceType(shill::kTypeVPN, nullptr, ConnectionType::kUnknown);
183 TestWithServiceType(
184 shill::kTypeVPN, shill::kTypeVPN, ConnectionType::kUnknown);
185 TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, ConnectionType::kWifi);
186 TestWithServiceType(
187 shill::kTypeVPN, shill::kTypeWimax, ConnectionType::kWimax);
Jay Srinivasan43488792012-06-19 00:25:31 -0700188}
189
Alex Deymo6ae91202014-03-10 19:21:25 -0700190TEST_F(ConnectionManagerTest, TetheringTest) {
191 TestWithServiceTethering(shill::kTetheringConfirmedState,
Sen Jiang255e22b2016-05-20 16:15:29 -0700192 ConnectionTethering::kConfirmed);
Alex Deymo6ae91202014-03-10 19:21:25 -0700193 TestWithServiceTethering(shill::kTetheringNotDetectedState,
Sen Jiang255e22b2016-05-20 16:15:29 -0700194 ConnectionTethering::kNotDetected);
Alex Deymo6ae91202014-03-10 19:21:25 -0700195 TestWithServiceTethering(shill::kTetheringSuspectedState,
Sen Jiang255e22b2016-05-20 16:15:29 -0700196 ConnectionTethering::kSuspected);
Alex Deymo6ae91202014-03-10 19:21:25 -0700197 TestWithServiceTethering("I'm not a valid property value =)",
Sen Jiang255e22b2016-05-20 16:15:29 -0700198 ConnectionTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700199}
200
Jay Srinivasan43488792012-06-19 00:25:31 -0700201TEST_F(ConnectionManagerTest, UnknownTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700202 TestWithServiceType("foo", nullptr, ConnectionType::kUnknown);
Jay Srinivasan43488792012-06-19 00:25:31 -0700203}
204
205TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700206 // Updates over Ethernet are allowed even if there's no policy.
Sen Jiang255e22b2016-05-20 16:15:29 -0700207 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
208 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700209}
210
211TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700212 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
213 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700214}
215
216TEST_F(ConnectionManagerTest, AllowUpdatesOverWimaxTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700217 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWimax,
218 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700219}
220
221TEST_F(ConnectionManagerTest, BlockUpdatesOverBluetoothTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700222 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kBluetooth,
223 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700224}
225
226TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
227 policy::MockDevicePolicy allow_3g_policy;
228
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700229 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700230
Alex Deymof4867c42013-06-28 14:41:39 -0700231 // This test tests cellular (3G) being the only connection type being allowed.
Jay Srinivasan43488792012-06-19 00:25:31 -0700232 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700233 allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700234
235 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
236 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700237 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700238
Sen Jiang255e22b2016-05-20 16:15:29 -0700239 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
240 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700241}
242
243TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
244 policy::MockDevicePolicy allow_3g_policy;
245
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700246 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700247
248 // This test tests multiple connection types being allowed, with
Alex Deymof4867c42013-06-28 14:41:39 -0700249 // 3G one among them. Only Cellular is currently enforced by the policy
250 // setting, the others are ignored (see Bluetooth for example).
Jay Srinivasan43488792012-06-19 00:25:31 -0700251 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700252 allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
253 allowed_set.insert(StringForConnectionType(ConnectionType::kBluetooth));
Jay Srinivasan43488792012-06-19 00:25:31 -0700254
255 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
Alex Deymo6ae91202014-03-10 19:21:25 -0700256 .Times(3)
Alex Deymo30534502015-07-20 15:06:33 -0700257 .WillRepeatedly(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700258
Sen Jiang255e22b2016-05-20 16:15:29 -0700259 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
260 ConnectionTethering::kUnknown));
261 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
262 ConnectionTethering::kNotDetected));
263 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
264 ConnectionTethering::kUnknown));
265 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
266 ConnectionTethering::kUnknown));
267 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWimax,
268 ConnectionTethering::kUnknown));
269 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kBluetooth,
270 ConnectionTethering::kUnknown));
Alex Deymo6ae91202014-03-10 19:21:25 -0700271
272 // Tethered networks are treated in the same way as Cellular networks and
273 // thus allowed.
Sen Jiang255e22b2016-05-20 16:15:29 -0700274 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
275 ConnectionTethering::kConfirmed));
276 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
277 ConnectionTethering::kConfirmed));
Jay Srinivasan43488792012-06-19 00:25:31 -0700278}
279
Alex Deymof4867c42013-06-28 14:41:39 -0700280TEST_F(ConnectionManagerTest, BlockUpdatesOverCellularByDefaultTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700281 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
282 ConnectionTethering::kUnknown));
Alex Deymo6ae91202014-03-10 19:21:25 -0700283}
284
285TEST_F(ConnectionManagerTest, BlockUpdatesOverTetheredNetworkByDefaultTest) {
Sen Jiang255e22b2016-05-20 16:15:29 -0700286 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
287 ConnectionTethering::kConfirmed));
288 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
289 ConnectionTethering::kConfirmed));
290 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
291 ConnectionTethering::kSuspected));
Jay Srinivasan43488792012-06-19 00:25:31 -0700292}
293
294TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
295 policy::MockDevicePolicy block_3g_policy;
296
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700297 fake_system_state_.set_device_policy(&block_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700298
299 // Test that updates for 3G are blocked while updates are allowed
300 // over several other types.
301 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700302 allowed_set.insert(StringForConnectionType(ConnectionType::kEthernet));
303 allowed_set.insert(StringForConnectionType(ConnectionType::kWifi));
304 allowed_set.insert(StringForConnectionType(ConnectionType::kWimax));
Jay Srinivasan43488792012-06-19 00:25:31 -0700305
306 EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
307 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700308 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700309
Sen Jiang255e22b2016-05-20 16:15:29 -0700310 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
311 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700312}
313
314TEST_F(ConnectionManagerTest, BlockUpdatesOver3GIfErrorInPolicyFetchTest) {
315 policy::MockDevicePolicy allow_3g_policy;
316
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700317 fake_system_state_.set_device_policy(&allow_3g_policy);
Jay Srinivasan43488792012-06-19 00:25:31 -0700318
319 set<string> allowed_set;
Sen Jiang255e22b2016-05-20 16:15:29 -0700320 allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
Jay Srinivasan43488792012-06-19 00:25:31 -0700321
322 // Return false for GetAllowedConnectionTypesForUpdate and see
323 // that updates are still blocked for 3G despite the value being in
324 // the string set above.
325 EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
326 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700327 .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(false)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700328
Sen Jiang255e22b2016-05-20 16:15:29 -0700329 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
330 ConnectionTethering::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700331}
332
Alex Deymof4867c42013-06-28 14:41:39 -0700333TEST_F(ConnectionManagerTest, UseUserPrefForUpdatesOverCellularIfNoPolicyTest) {
334 policy::MockDevicePolicy no_policy;
Alex Deymo8427b4a2014-11-05 14:00:32 -0800335 testing::NiceMock<MockPrefs>* prefs = fake_system_state_.mock_prefs();
Alex Deymof4867c42013-06-28 14:41:39 -0700336
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700337 fake_system_state_.set_device_policy(&no_policy);
Alex Deymof4867c42013-06-28 14:41:39 -0700338
339 // No setting enforced by the device policy, user prefs should be used.
340 EXPECT_CALL(no_policy, GetAllowedConnectionTypesForUpdate(_))
341 .Times(3)
342 .WillRepeatedly(Return(false));
343
344 // No user pref: block.
345 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
346 .Times(1)
347 .WillOnce(Return(false));
Sen Jiang255e22b2016-05-20 16:15:29 -0700348 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
349 ConnectionTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700350
351 // Allow per user pref.
352 EXPECT_CALL(*prefs, Exists(kPrefsUpdateOverCellularPermission))
353 .Times(1)
354 .WillOnce(Return(true));
Alex Deymoefb7c4c2013-07-09 14:34:00 -0700355 EXPECT_CALL(*prefs, GetBoolean(kPrefsUpdateOverCellularPermission, _))
Alex Deymof4867c42013-06-28 14:41:39 -0700356 .Times(1)
Alex Deymo30534502015-07-20 15:06:33 -0700357 .WillOnce(DoAll(SetArgPointee<1>(true), Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700358 EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
359 ConnectionTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700360
361 // Block 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>(false), Return(true)));
Sen Jiang255e22b2016-05-20 16:15:29 -0700368 EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
369 ConnectionTethering::kUnknown));
Alex Deymof4867c42013-06-28 14:41:39 -0700370}
371
Jay Srinivasan43488792012-06-19 00:25:31 -0700372TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
Ben Chanc6007e42013-09-19 23:49:22 -0700373 EXPECT_STREQ(shill::kTypeEthernet,
Sen Jiang255e22b2016-05-20 16:15:29 -0700374 StringForConnectionType(ConnectionType::kEthernet));
Ben Chanc6007e42013-09-19 23:49:22 -0700375 EXPECT_STREQ(shill::kTypeWifi,
Sen Jiang255e22b2016-05-20 16:15:29 -0700376 StringForConnectionType(ConnectionType::kWifi));
Ben Chanc6007e42013-09-19 23:49:22 -0700377 EXPECT_STREQ(shill::kTypeWimax,
Sen Jiang255e22b2016-05-20 16:15:29 -0700378 StringForConnectionType(ConnectionType::kWimax));
Ben Chanc6007e42013-09-19 23:49:22 -0700379 EXPECT_STREQ(shill::kTypeBluetooth,
Sen Jiang255e22b2016-05-20 16:15:29 -0700380 StringForConnectionType(ConnectionType::kBluetooth));
Ben Chanc6007e42013-09-19 23:49:22 -0700381 EXPECT_STREQ(shill::kTypeCellular,
Sen Jiang255e22b2016-05-20 16:15:29 -0700382 StringForConnectionType(ConnectionType::kCellular));
383 EXPECT_STREQ("Unknown", StringForConnectionType(ConnectionType::kUnknown));
Jay Srinivasan43488792012-06-19 00:25:31 -0700384 EXPECT_STREQ("Unknown",
Sen Jiang255e22b2016-05-20 16:15:29 -0700385 StringForConnectionType(static_cast<ConnectionType>(999999)));
Jay Srinivasan43488792012-06-19 00:25:31 -0700386}
387
388TEST_F(ConnectionManagerTest, MalformedServiceList) {
Alex Deymo758dd532015-09-09 15:21:22 -0700389 SetManagerReply("/service/guest/network", false);
Jay Srinivasan43488792012-06-19 00:25:31 -0700390
Sen Jiang255e22b2016-05-20 16:15:29 -0700391 ConnectionType type;
392 ConnectionTethering tethering;
Alex Deymo30534502015-07-20 15:06:33 -0700393 EXPECT_FALSE(cmut_.GetConnectionProperties(&type, &tethering));
Jay Srinivasan43488792012-06-19 00:25:31 -0700394}
395
396} // namespace chromeos_update_engine