ota_activator_impl_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/services/cellular_setup/ota_activator_impl.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "ash/services/cellular_setup/public/cpp/fake_activation_delegate.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/metrics/histogram_tester.h"
  10. #include "base/test/task_environment.h"
  11. #include "base/test/test_simple_task_runner.h"
  12. #include "chromeos/ash/components/network/fake_network_activation_handler.h"
  13. #include "chromeos/ash/components/network/fake_network_connection_handler.h"
  14. #include "chromeos/ash/components/network/network_connection_handler.h"
  15. #include "chromeos/ash/components/network/network_state_handler.h"
  16. #include "chromeos/ash/components/network/network_state_test_helper.h"
  17. #include "mojo/public/cpp/bindings/remote.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "third_party/cros_system_api/dbus/service_constants.h"
  20. #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
  21. namespace ash::cellular_setup {
  22. namespace {
  23. const char kTestCellularDevicePath[] = "/device/wwan0";
  24. const char kTestCellularDeviceName[] = "testDeviceName";
  25. const char kTestCellularDeviceCarrier[] = "testDeviceCarrier";
  26. const char kTestCellularDeviceMeid[] = "testDeviceMeid";
  27. const char kTestCellularDeviceImei[] = "testDeviceImei";
  28. const char kTestCellularDeviceMdn[] = "testDeviceMdn";
  29. const char kTestCellularServicePath[] = "/service/cellular0";
  30. const char kTestCellularServiceIccid[] = "1234567890";
  31. const char kTestCellularServiceGuid[] = "testServiceGuid";
  32. const char kTestCellularServiceName[] = "testServiceName";
  33. const char kTestCellularServicePaymentUrl[] = "testServicePaymentUrl.com";
  34. const char kTestCellularServicePaymentPostData[] = "testServicePostData";
  35. const char kPaymentPortalMethodPost[] = "POST";
  36. } // namespace
  37. class CellularSetupOtaActivatorImplTest : public testing::Test {
  38. public:
  39. CellularSetupOtaActivatorImplTest(const CellularSetupOtaActivatorImplTest&) =
  40. delete;
  41. CellularSetupOtaActivatorImplTest& operator=(
  42. const CellularSetupOtaActivatorImplTest&) = delete;
  43. protected:
  44. CellularSetupOtaActivatorImplTest()
  45. : test_helper_(false /* use_default_devices_and_services */) {}
  46. ~CellularSetupOtaActivatorImplTest() override = default;
  47. // testing::Test:
  48. void SetUp() override {
  49. fake_activation_delegate_ = std::make_unique<FakeActivationDelegate>();
  50. fake_network_connection_handler_ =
  51. std::make_unique<FakeNetworkConnectionHandler>();
  52. fake_network_activation_handler_ =
  53. std::make_unique<FakeNetworkActivationHandler>();
  54. }
  55. void BuildOtaActivator() {
  56. auto test_task_runner = base::MakeRefCounted<base::TestSimpleTaskRunner>();
  57. ota_activator_ = OtaActivatorImpl::Factory::Create(
  58. fake_activation_delegate_->GenerateRemote(),
  59. base::BindOnce(&CellularSetupOtaActivatorImplTest::OnFinished,
  60. base::Unretained(this)),
  61. test_helper_.network_state_handler(),
  62. fake_network_connection_handler_.get(),
  63. fake_network_activation_handler_.get(), test_task_runner);
  64. test_task_runner->RunUntilIdle();
  65. carrier_portal_handler_remote_.Bind(ota_activator_->GenerateRemote());
  66. }
  67. void AddCellularDevice(bool has_valid_sim, bool has_physical_slots = true) {
  68. ShillDeviceClient::TestInterface* device_test = test_helper_.device_test();
  69. device_test->AddDevice(kTestCellularDevicePath, shill::kTypeCellular,
  70. kTestCellularDeviceName);
  71. if (has_valid_sim) {
  72. device_test->SetDeviceProperty(
  73. kTestCellularDevicePath, shill::kSIMPresentProperty,
  74. base::Value(true), false /* notify_changed */);
  75. device_test->SetDeviceProperty(
  76. kTestCellularDevicePath, shill::kSIMSlotInfoProperty,
  77. CreateCellularSIMSlotInfo(kTestCellularServiceIccid),
  78. false /* notify_changed */);
  79. base::DictionaryValue home_provider;
  80. home_provider.SetString(shill::kOperatorNameKey,
  81. kTestCellularDeviceCarrier);
  82. device_test->SetDeviceProperty(kTestCellularDevicePath,
  83. shill::kHomeProviderProperty,
  84. home_provider, false /* notify_changed */);
  85. device_test->SetDeviceProperty(
  86. kTestCellularDevicePath, shill::kMeidProperty,
  87. base::Value(kTestCellularDeviceMeid), false /* notify_changed */);
  88. device_test->SetDeviceProperty(
  89. kTestCellularDevicePath, shill::kImeiProperty,
  90. base::Value(kTestCellularDeviceImei), false /* notify_changed */);
  91. device_test->SetDeviceProperty(
  92. kTestCellularDevicePath, shill::kMdnProperty,
  93. base::Value(kTestCellularDeviceMdn), false /* notify_changed */);
  94. } else {
  95. std::string eid = has_physical_slots ? std::string() : "test_eid";
  96. device_test->SetDeviceProperty(
  97. kTestCellularDevicePath, shill::kSIMSlotInfoProperty,
  98. CreateCellularSIMSlotInfo(kTestCellularServiceIccid, eid),
  99. false /* notify_changed */);
  100. }
  101. base::RunLoop().RunUntilIdle();
  102. }
  103. void RemoveCellularDevice() {
  104. test_helper_.device_test()->RemoveDevice(kTestCellularDevicePath);
  105. base::RunLoop().RunUntilIdle();
  106. }
  107. void AddCellularNetwork(bool has_valid_payment_info,
  108. bool is_connected,
  109. bool is_already_activated) {
  110. ShillServiceClient::TestInterface* service_test =
  111. test_helper_.service_test();
  112. service_test->AddService(
  113. kTestCellularServicePath, kTestCellularServiceGuid,
  114. kTestCellularServiceName, shill::kTypeCellular,
  115. is_connected ? shill::kStateOnline : shill::kStateIdle, true);
  116. service_test->SetServiceProperty(kTestCellularServicePath,
  117. shill::kIccidProperty,
  118. base::Value(kTestCellularServiceIccid));
  119. service_test->SetServiceProperty(
  120. kTestCellularServicePath, shill::kActivationStateProperty,
  121. is_already_activated
  122. ? base::Value(shill::kActivationStateActivated)
  123. : base::Value(shill::kActivationStateNotActivated));
  124. if (has_valid_payment_info) {
  125. base::DictionaryValue payment_portal;
  126. payment_portal.SetString(shill::kPaymentPortalURL,
  127. kTestCellularServicePaymentUrl);
  128. payment_portal.SetString(shill::kPaymentPortalMethod,
  129. kPaymentPortalMethodPost);
  130. payment_portal.SetString(shill::kPaymentPortalPostData,
  131. kTestCellularServicePaymentPostData);
  132. service_test->SetServiceProperty(kTestCellularServicePath,
  133. shill::kPaymentPortalProperty,
  134. payment_portal);
  135. }
  136. base::RunLoop().RunUntilIdle();
  137. }
  138. void RemoveCellularNetwork() {
  139. test_helper_.service_test()->RemoveService(kTestCellularServicePath);
  140. base::RunLoop().RunUntilIdle();
  141. }
  142. void FlushForTesting() {
  143. static_cast<OtaActivatorImpl*>(ota_activator_.get())->FlushForTesting();
  144. }
  145. void VerifyCellularMetadataReceivedByDelegate() {
  146. const std::vector<mojom::CellularMetadataPtr>& cellular_metadata_list =
  147. fake_activation_delegate_->cellular_metadata_list();
  148. ASSERT_EQ(1u, cellular_metadata_list.size());
  149. EXPECT_EQ(GURL(kTestCellularServicePaymentUrl),
  150. cellular_metadata_list[0]->payment_url);
  151. EXPECT_EQ(kTestCellularDeviceCarrier, cellular_metadata_list[0]->carrier);
  152. EXPECT_EQ(kTestCellularDeviceMeid, cellular_metadata_list[0]->meid);
  153. EXPECT_EQ(kTestCellularDeviceImei, cellular_metadata_list[0]->imei);
  154. EXPECT_EQ(kTestCellularDeviceMdn, cellular_metadata_list[0]->mdn);
  155. }
  156. void UpdateCarrierPortalState(
  157. mojom::CarrierPortalStatus carrier_portal_status) {
  158. carrier_portal_handler_remote_->OnCarrierPortalStatusChange(
  159. carrier_portal_status);
  160. carrier_portal_handler_remote_.FlushForTesting();
  161. }
  162. void ConnectCellularNetwork() {
  163. const std::vector<FakeNetworkConnectionHandler::ConnectionParams>&
  164. connect_calls = fake_network_connection_handler_->connect_calls();
  165. ASSERT_FALSE(connect_calls.empty());
  166. // A connection should have been requested by |ota_activator_|.
  167. EXPECT_EQ(kTestCellularServicePath, connect_calls.back().service_path());
  168. // Simulate the connection succeeding.
  169. test_helper_.service_test()->SetServiceProperty(
  170. kTestCellularServicePath, shill::kStateProperty,
  171. base::Value(shill::kStateOnline));
  172. base::RunLoop().RunUntilIdle();
  173. }
  174. void FailCellularNetworkConnect() {
  175. std::vector<FakeNetworkConnectionHandler::ConnectionParams>& connect_calls =
  176. fake_network_connection_handler_->connect_calls();
  177. ASSERT_FALSE(connect_calls.empty());
  178. // A connection should have been requested by |ota_activator_|.
  179. EXPECT_EQ(kTestCellularServicePath, connect_calls.back().service_path());
  180. EXPECT_EQ(ConnectCallbackMode::ON_COMPLETED,
  181. connect_calls.back().connect_callback_mode());
  182. connect_calls.back().InvokeErrorCallback("fake_error");
  183. base::RunLoop().RunUntilIdle();
  184. }
  185. size_t ConnectCallCount() {
  186. return fake_network_connection_handler_->connect_calls().size();
  187. }
  188. void InvokePendingActivationCallback(bool success) {
  189. std::vector<FakeNetworkActivationHandler::ActivationParams>&
  190. complete_activation_calls =
  191. fake_network_activation_handler_->complete_activation_calls();
  192. ASSERT_EQ(1u, complete_activation_calls.size());
  193. EXPECT_EQ(kTestCellularServicePath,
  194. complete_activation_calls[0].service_path());
  195. if (success) {
  196. complete_activation_calls[0].InvokeSuccessCallback();
  197. } else {
  198. complete_activation_calls[0].InvokeErrorCallback("error");
  199. }
  200. }
  201. void VerifyActivationFinished(mojom::ActivationResult activation_result) {
  202. const std::vector<mojom::ActivationResult>& activation_results =
  203. fake_activation_delegate_->activation_results();
  204. ASSERT_EQ(1u, activation_results.size());
  205. EXPECT_EQ(activation_result, activation_results[0]);
  206. histogram_tester_.ExpectBucketCount(
  207. "Network.Cellular.PSim.OtaActivationResult", activation_result,
  208. /*expected_count=*/1);
  209. EXPECT_TRUE(is_finished_);
  210. }
  211. void DisconnectDelegate() {
  212. fake_activation_delegate_->DisconnectReceivers();
  213. }
  214. void FastForwardByConnectRetryDelay() {
  215. task_environment_.FastForwardBy(OtaActivatorImpl::kConnectRetryDelay);
  216. }
  217. bool is_finished() { return is_finished_; }
  218. private:
  219. void OnFinished() {
  220. EXPECT_FALSE(is_finished_);
  221. is_finished_ = true;
  222. }
  223. base::Value CreateCellularSIMSlotInfo(
  224. const std::string& iccid,
  225. const std::string& eid = std::string()) {
  226. base::Value::List sim_slot_infos;
  227. base::Value::Dict slot_info_item;
  228. slot_info_item.Set(shill::kSIMSlotInfoEID, eid);
  229. slot_info_item.Set(shill::kSIMSlotInfoICCID, iccid);
  230. slot_info_item.Set(shill::kSIMSlotInfoPrimary, false);
  231. sim_slot_infos.Append(std::move(slot_info_item));
  232. return base::Value(std::move(sim_slot_infos));
  233. }
  234. base::test::TaskEnvironment task_environment_{
  235. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  236. NetworkStateTestHelper test_helper_;
  237. base::HistogramTester histogram_tester_;
  238. std::unique_ptr<FakeActivationDelegate> fake_activation_delegate_;
  239. std::unique_ptr<FakeNetworkConnectionHandler>
  240. fake_network_connection_handler_;
  241. std::unique_ptr<FakeNetworkActivationHandler>
  242. fake_network_activation_handler_;
  243. std::unique_ptr<OtaActivator> ota_activator_;
  244. mojo::Remote<mojom::CarrierPortalHandler> carrier_portal_handler_remote_;
  245. bool is_finished_ = false;
  246. };
  247. TEST_F(CellularSetupOtaActivatorImplTest, Success) {
  248. AddCellularDevice(true /* has_valid_sim */);
  249. AddCellularNetwork(true /* has_valid_payment_info */, true /* is_connected */,
  250. false /* is_already_activated */);
  251. BuildOtaActivator();
  252. FlushForTesting();
  253. VerifyCellularMetadataReceivedByDelegate();
  254. UpdateCarrierPortalState(
  255. mojom::CarrierPortalStatus::kPortalLoadedWithoutPaidUser);
  256. UpdateCarrierPortalState(
  257. mojom::CarrierPortalStatus::kPortalLoadedAndUserCompletedPayment);
  258. InvokePendingActivationCallback(true /* success */);
  259. FlushForTesting();
  260. VerifyActivationFinished(
  261. mojom::ActivationResult::kSuccessfullyStartedActivation);
  262. }
  263. TEST_F(CellularSetupOtaActivatorImplTest, SuccessWithPaymentError) {
  264. AddCellularDevice(true /* has_valid_sim */);
  265. AddCellularNetwork(true /* has_valid_payment_info */, true /* is_connected */,
  266. false /* is_already_activated */);
  267. BuildOtaActivator();
  268. FlushForTesting();
  269. VerifyCellularMetadataReceivedByDelegate();
  270. UpdateCarrierPortalState(
  271. mojom::CarrierPortalStatus::kPortalLoadedWithoutPaidUser);
  272. UpdateCarrierPortalState(
  273. mojom::CarrierPortalStatus::kPortalLoadedButErrorOccurredDuringPayment);
  274. InvokePendingActivationCallback(true /* success */);
  275. FlushForTesting();
  276. VerifyActivationFinished(
  277. mojom::ActivationResult::kSuccessfullyStartedActivation);
  278. }
  279. TEST_F(CellularSetupOtaActivatorImplTest, HasNoPhysicalSlots) {
  280. AddCellularDevice(false /* has_valid_sim */, false /* has_physical_slots */);
  281. BuildOtaActivator();
  282. FlushForTesting();
  283. VerifyActivationFinished(mojom::ActivationResult::kFailedToActivate);
  284. }
  285. TEST_F(CellularSetupOtaActivatorImplTest, ConnectRetry) {
  286. AddCellularDevice(true /* has_valid_sim */);
  287. AddCellularNetwork(true /* has_valid_payment_info */,
  288. false /* is_connected */,
  289. false /* is_already_activated */);
  290. BuildOtaActivator();
  291. EXPECT_EQ(1u, ConnectCallCount());
  292. FailCellularNetworkConnect();
  293. // Ensure the a new connect call is not issued immediately.
  294. EXPECT_EQ(1u, ConnectCallCount());
  295. FastForwardByConnectRetryDelay();
  296. EXPECT_EQ(2u, ConnectCallCount());
  297. ConnectCellularNetwork();
  298. FlushForTesting();
  299. VerifyCellularMetadataReceivedByDelegate();
  300. UpdateCarrierPortalState(
  301. mojom::CarrierPortalStatus::kPortalLoadedWithoutPaidUser);
  302. UpdateCarrierPortalState(
  303. mojom::CarrierPortalStatus::kPortalLoadedAndUserCompletedPayment);
  304. InvokePendingActivationCallback(true /* success */);
  305. FlushForTesting();
  306. VerifyActivationFinished(
  307. mojom::ActivationResult::kSuccessfullyStartedActivation);
  308. }
  309. TEST_F(CellularSetupOtaActivatorImplTest,
  310. SimAndPaymentInfoNotInitiallyPresent_AndNetworkNotConnected) {
  311. AddCellularDevice(false /* has_valid_sim */);
  312. AddCellularNetwork(false /* has_valid_payment_info */,
  313. false /* is_connected */,
  314. false /* is_already_activated */);
  315. BuildOtaActivator();
  316. RemoveCellularDevice();
  317. RemoveCellularNetwork();
  318. AddCellularDevice(true /* has_valid_sim */);
  319. AddCellularNetwork(true /* has_valid_payment_info */,
  320. false /* is_connected */,
  321. false /* is_already_activated */);
  322. ConnectCellularNetwork();
  323. FlushForTesting();
  324. VerifyCellularMetadataReceivedByDelegate();
  325. UpdateCarrierPortalState(
  326. mojom::CarrierPortalStatus::kPortalLoadedWithoutPaidUser);
  327. UpdateCarrierPortalState(
  328. mojom::CarrierPortalStatus::kPortalLoadedAndUserCompletedPayment);
  329. InvokePendingActivationCallback(true /* success */);
  330. FlushForTesting();
  331. VerifyActivationFinished(
  332. mojom::ActivationResult::kSuccessfullyStartedActivation);
  333. }
  334. TEST_F(CellularSetupOtaActivatorImplTest, AlreadyActivated) {
  335. AddCellularDevice(true /* has_valid_sim */);
  336. AddCellularNetwork(true /* has_valid_payment_info */, true /* is_connected */,
  337. true /* is_already_activated */);
  338. BuildOtaActivator();
  339. FlushForTesting();
  340. VerifyActivationFinished(mojom::ActivationResult::kAlreadyActivated);
  341. }
  342. TEST_F(CellularSetupOtaActivatorImplTest, DelegateBecomesDisconnected) {
  343. AddCellularDevice(true /* has_valid_sim */);
  344. AddCellularNetwork(true /* has_valid_payment_info */, true /* is_connected */,
  345. false /* is_already_activated */);
  346. BuildOtaActivator();
  347. DisconnectDelegate();
  348. FlushForTesting();
  349. // Note: Cannot check the ActivationResult received by the delegate because
  350. // the delegate was disconnected and did not receive the result.
  351. EXPECT_TRUE(is_finished());
  352. }
  353. } // namespace ash::cellular_setup