connection_manager_unittest.cc 15 KB

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