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 //
16 
17 #include "update_engine/connection_manager.h"
18 
19 #include <memory>
20 #include <set>
21 #include <string>
22 #include <utility>
23 
24 #include <base/logging.h>
25 #include <brillo/any.h>
26 #include <brillo/message_loops/fake_message_loop.h>
27 #include <brillo/variant_dictionary.h>
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30 #include <shill/dbus-constants.h>
31 #include <shill/dbus-proxies.h>
32 #include <shill/dbus-proxy-mocks.h>
33 
34 #include "update_engine/common/test_utils.h"
35 #include "update_engine/fake_shill_proxy.h"
36 #include "update_engine/fake_system_state.h"
37 
38 using chromeos_update_engine::connection_utils::StringForConnectionType;
39 using org::chromium::flimflam::ManagerProxyMock;
40 using org::chromium::flimflam::ServiceProxyMock;
41 using std::set;
42 using std::string;
43 using testing::_;
44 using testing::Return;
45 using testing::SetArgPointee;
46 
47 namespace chromeos_update_engine {
48 
49 class ConnectionManagerTest : public ::testing::Test {
50  public:
ConnectionManagerTest()51   ConnectionManagerTest() : fake_shill_proxy_(new FakeShillProxy()) {}
52 
SetUp()53   void SetUp() override {
54     loop_.SetAsCurrent();
55     fake_system_state_.set_connection_manager(&cmut_);
56   }
57 
TearDown()58   void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
59 
60  protected:
61   // Sets the default_service object path in the response from the
62   // ManagerProxyMock instance.
63   void SetManagerReply(const char* default_service, bool reply_succeeds);
64 
65   // Sets the |service_type|, |physical_technology| and |service_tethering|
66   // properties in the mocked service |service_path|. If any of the three
67   // const char* is a nullptr, the corresponding property will not be included
68   // in the response.
69   void SetServiceReply(const string& service_path,
70                        const char* service_type,
71                        const char* physical_technology,
72                        const char* service_tethering);
73 
74   void TestWithServiceType(const char* service_type,
75                            const char* physical_technology,
76                            ConnectionType expected_type);
77 
78   void TestWithServiceDisconnected(ConnectionType expected_type);
79 
80   void TestWithServiceTethering(const char* service_tethering,
81                                 ConnectionTethering expected_tethering);
82 
83   brillo::FakeMessageLoop loop_{nullptr};
84   FakeSystemState fake_system_state_;
85   FakeShillProxy* fake_shill_proxy_;
86 
87   // ConnectionManager under test.
88   ConnectionManager cmut_{fake_shill_proxy_, &fake_system_state_};
89 };
90 
SetManagerReply(const char * default_service,bool reply_succeeds)91 void ConnectionManagerTest::SetManagerReply(const char* default_service,
92                                             bool reply_succeeds) {
93   ManagerProxyMock* manager_proxy_mock = fake_shill_proxy_->GetManagerProxy();
94   if (!reply_succeeds) {
95     EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
96         .WillOnce(Return(false));
97     return;
98   }
99 
100   // Create a dictionary of properties and optionally include the default
101   // service.
102   brillo::VariantDictionary reply_dict;
103   reply_dict["SomeOtherProperty"] = 0xC0FFEE;
104 
105   if (default_service) {
106     reply_dict[shill::kDefaultServiceProperty] =
107         dbus::ObjectPath(default_service);
108   }
109   EXPECT_CALL(*manager_proxy_mock, GetProperties(_, _, _))
110       .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
111 }
112 
SetServiceReply(const string & service_path,const char * service_type,const char * physical_technology,const char * service_tethering)113 void ConnectionManagerTest::SetServiceReply(const string& service_path,
114                                             const char* service_type,
115                                             const char* physical_technology,
116                                             const char* service_tethering) {
117   brillo::VariantDictionary reply_dict;
118   reply_dict["SomeOtherProperty"] = 0xC0FFEE;
119 
120   if (service_type)
121     reply_dict[shill::kTypeProperty] = string(service_type);
122 
123   if (physical_technology) {
124     reply_dict[shill::kPhysicalTechnologyProperty] =
125         string(physical_technology);
126   }
127 
128   if (service_tethering)
129     reply_dict[shill::kTetheringProperty] = string(service_tethering);
130 
131   std::unique_ptr<ServiceProxyMock> service_proxy_mock(new ServiceProxyMock());
132 
133   // Plumb return value into mock object.
134   EXPECT_CALL(*service_proxy_mock.get(), GetProperties(_, _, _))
135       .WillOnce(DoAll(SetArgPointee<0>(reply_dict), Return(true)));
136 
137   fake_shill_proxy_->SetServiceForPath(dbus::ObjectPath(service_path),
138                                        std::move(service_proxy_mock));
139 }
140 
TestWithServiceType(const char * service_type,const char * physical_technology,ConnectionType expected_type)141 void ConnectionManagerTest::TestWithServiceType(const char* service_type,
142                                                 const char* physical_technology,
143                                                 ConnectionType expected_type) {
144   SetManagerReply("/service/guest/network", true);
145   SetServiceReply("/service/guest/network",
146                   service_type,
147                   physical_technology,
148                   shill::kTetheringNotDetectedState);
149 
150   ConnectionType type;
151   ConnectionTethering tethering;
152   EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
153   EXPECT_EQ(expected_type, type);
154   testing::Mock::VerifyAndClearExpectations(
155       fake_shill_proxy_->GetManagerProxy());
156 }
157 
TestWithServiceTethering(const char * service_tethering,ConnectionTethering expected_tethering)158 void ConnectionManagerTest::TestWithServiceTethering(
159     const char* service_tethering, ConnectionTethering expected_tethering) {
160   SetManagerReply("/service/guest/network", true);
161   SetServiceReply(
162       "/service/guest/network", shill::kTypeWifi, nullptr, service_tethering);
163 
164   ConnectionType type;
165   ConnectionTethering tethering;
166   EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
167   EXPECT_EQ(expected_tethering, tethering);
168   testing::Mock::VerifyAndClearExpectations(
169       fake_shill_proxy_->GetManagerProxy());
170 }
171 
TestWithServiceDisconnected(ConnectionType expected_type)172 void ConnectionManagerTest::TestWithServiceDisconnected(
173     ConnectionType expected_type) {
174   SetManagerReply("/", true);
175 
176   ConnectionType type;
177   ConnectionTethering tethering;
178   EXPECT_TRUE(cmut_.GetConnectionProperties(&type, &tethering));
179   EXPECT_EQ(expected_type, type);
180   testing::Mock::VerifyAndClearExpectations(
181       fake_shill_proxy_->GetManagerProxy());
182 }
183 
TEST_F(ConnectionManagerTest,SimpleTest)184 TEST_F(ConnectionManagerTest, SimpleTest) {
185   TestWithServiceType(shill::kTypeEthernet, nullptr, ConnectionType::kEthernet);
186   TestWithServiceType(shill::kTypeWifi, nullptr, ConnectionType::kWifi);
187   TestWithServiceType(shill::kTypeCellular, nullptr, ConnectionType::kCellular);
188 }
189 
TEST_F(ConnectionManagerTest,PhysicalTechnologyTest)190 TEST_F(ConnectionManagerTest, PhysicalTechnologyTest) {
191   TestWithServiceType(shill::kTypeVPN, nullptr, ConnectionType::kUnknown);
192   TestWithServiceType(
193       shill::kTypeVPN, shill::kTypeVPN, ConnectionType::kUnknown);
194   TestWithServiceType(shill::kTypeVPN, shill::kTypeWifi, ConnectionType::kWifi);
195 }
196 
TEST_F(ConnectionManagerTest,TetheringTest)197 TEST_F(ConnectionManagerTest, TetheringTest) {
198   TestWithServiceTethering(shill::kTetheringConfirmedState,
199                            ConnectionTethering::kConfirmed);
200   TestWithServiceTethering(shill::kTetheringNotDetectedState,
201                            ConnectionTethering::kNotDetected);
202   TestWithServiceTethering(shill::kTetheringSuspectedState,
203                            ConnectionTethering::kSuspected);
204   TestWithServiceTethering("I'm not a valid property value =)",
205                            ConnectionTethering::kUnknown);
206 }
207 
TEST_F(ConnectionManagerTest,UnknownTest)208 TEST_F(ConnectionManagerTest, UnknownTest) {
209   TestWithServiceType("foo", nullptr, ConnectionType::kUnknown);
210 }
211 
TEST_F(ConnectionManagerTest,DisconnectTest)212 TEST_F(ConnectionManagerTest, DisconnectTest) {
213   TestWithServiceDisconnected(ConnectionType::kDisconnected);
214 }
215 
TEST_F(ConnectionManagerTest,AllowUpdatesOverEthernetTest)216 TEST_F(ConnectionManagerTest, AllowUpdatesOverEthernetTest) {
217   // Updates over Ethernet are allowed even if there's no policy.
218   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
219                                         ConnectionTethering::kUnknown));
220 }
221 
TEST_F(ConnectionManagerTest,AllowUpdatesOverWifiTest)222 TEST_F(ConnectionManagerTest, AllowUpdatesOverWifiTest) {
223   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
224                                         ConnectionTethering::kUnknown));
225 }
226 
TEST_F(ConnectionManagerTest,AllowUpdatesOnlyOver3GPerPolicyTest)227 TEST_F(ConnectionManagerTest, AllowUpdatesOnlyOver3GPerPolicyTest) {
228   policy::MockDevicePolicy allow_3g_policy;
229 
230   fake_system_state_.set_device_policy(&allow_3g_policy);
231 
232   // This test tests cellular (3G) being the only connection type being allowed.
233   set<string> allowed_set;
234   allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
235 
236   EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
237       .Times(1)
238       .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
239 
240   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
241                                         ConnectionTethering::kUnknown));
242 }
243 
TEST_F(ConnectionManagerTest,AllowUpdatesOver3GAndOtherTypesPerPolicyTest)244 TEST_F(ConnectionManagerTest, AllowUpdatesOver3GAndOtherTypesPerPolicyTest) {
245   policy::MockDevicePolicy allow_3g_policy;
246 
247   fake_system_state_.set_device_policy(&allow_3g_policy);
248 
249   // This test tests multiple connection types being allowed, with
250   // 3G one among them. Only Cellular is currently enforced by the policy
251   // setting.
252   set<string> allowed_set;
253   allowed_set.insert(StringForConnectionType(ConnectionType::kCellular));
254 
255   EXPECT_CALL(allow_3g_policy, GetAllowedConnectionTypesForUpdate(_))
256       .Times(3)
257       .WillRepeatedly(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
258 
259   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 
268   // Tethered networks are treated in the same way as Cellular networks and
269   // thus allowed.
270   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
271                                         ConnectionTethering::kConfirmed));
272   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
273                                         ConnectionTethering::kConfirmed));
274 }
275 
TEST_F(ConnectionManagerTest,AllowUpdatesOverCellularByDefaultTest)276 TEST_F(ConnectionManagerTest, AllowUpdatesOverCellularByDefaultTest) {
277   policy::MockDevicePolicy device_policy;
278   // Set an empty device policy.
279   fake_system_state_.set_device_policy(&device_policy);
280 
281   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
282                                         ConnectionTethering::kUnknown));
283 }
284 
TEST_F(ConnectionManagerTest,AllowUpdatesOverTetheredNetworkByDefaultTest)285 TEST_F(ConnectionManagerTest, AllowUpdatesOverTetheredNetworkByDefaultTest) {
286   policy::MockDevicePolicy device_policy;
287   // Set an empty device policy.
288   fake_system_state_.set_device_policy(&device_policy);
289 
290   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
291                                         ConnectionTethering::kConfirmed));
292   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kEthernet,
293                                         ConnectionTethering::kConfirmed));
294   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kWifi,
295                                         ConnectionTethering::kSuspected));
296 }
297 
TEST_F(ConnectionManagerTest,BlockUpdatesOver3GPerPolicyTest)298 TEST_F(ConnectionManagerTest, BlockUpdatesOver3GPerPolicyTest) {
299   policy::MockDevicePolicy block_3g_policy;
300 
301   fake_system_state_.set_device_policy(&block_3g_policy);
302 
303   // Test that updates for 3G are blocked while updates are allowed
304   // over several other types.
305   set<string> allowed_set;
306   allowed_set.insert(StringForConnectionType(ConnectionType::kEthernet));
307   allowed_set.insert(StringForConnectionType(ConnectionType::kWifi));
308 
309   EXPECT_CALL(block_3g_policy, GetAllowedConnectionTypesForUpdate(_))
310       .Times(1)
311       .WillOnce(DoAll(SetArgPointee<0>(allowed_set), Return(true)));
312 
313   EXPECT_FALSE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
314                                          ConnectionTethering::kUnknown));
315 }
316 
TEST_F(ConnectionManagerTest,AllowUpdatesOver3GIfPolicyIsNotSet)317 TEST_F(ConnectionManagerTest, AllowUpdatesOver3GIfPolicyIsNotSet) {
318   policy::MockDevicePolicy device_policy;
319 
320   fake_system_state_.set_device_policy(&device_policy);
321 
322   // Return false for GetAllowedConnectionTypesForUpdate and see
323   // that updates are allowed as device policy is not set. Further
324   // check is left to |OmahaRequestAction|.
325   EXPECT_CALL(device_policy, GetAllowedConnectionTypesForUpdate(_))
326       .Times(1)
327       .WillOnce(Return(false));
328 
329   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
330                                         ConnectionTethering::kUnknown));
331 }
332 
TEST_F(ConnectionManagerTest,AllowUpdatesOverCellularIfPolicyFailsToBeLoaded)333 TEST_F(ConnectionManagerTest, AllowUpdatesOverCellularIfPolicyFailsToBeLoaded) {
334   fake_system_state_.set_device_policy(nullptr);
335 
336   EXPECT_TRUE(cmut_.IsUpdateAllowedOver(ConnectionType::kCellular,
337                                         ConnectionTethering::kUnknown));
338 }
339 
TEST_F(ConnectionManagerTest,StringForConnectionTypeTest)340 TEST_F(ConnectionManagerTest, StringForConnectionTypeTest) {
341   EXPECT_STREQ(shill::kTypeEthernet,
342                StringForConnectionType(ConnectionType::kEthernet));
343   EXPECT_STREQ(shill::kTypeWifi,
344                StringForConnectionType(ConnectionType::kWifi));
345   EXPECT_STREQ(shill::kTypeCellular,
346                StringForConnectionType(ConnectionType::kCellular));
347   EXPECT_STREQ("Unknown", StringForConnectionType(ConnectionType::kUnknown));
348   EXPECT_STREQ("Unknown",
349                StringForConnectionType(static_cast<ConnectionType>(999999)));
350 }
351 
TEST_F(ConnectionManagerTest,MalformedServiceList)352 TEST_F(ConnectionManagerTest, MalformedServiceList) {
353   SetManagerReply("/service/guest/network", false);
354 
355   ConnectionType type;
356   ConnectionTethering tethering;
357   EXPECT_FALSE(cmut_.GetConnectionProperties(&type, &tethering));
358 }
359 
360 }  // namespace chromeos_update_engine
361