managed_sim_lock_notifier_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2022 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/system/network/managed_sim_lock_notifier.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/system/system_notification_controller.h"
  7. #include "ash/test/ash_test_base.h"
  8. #include "base/bind.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/metrics/histogram_tester.h"
  11. #include "base/test/scoped_feature_list.h"
  12. #include "chromeos/ash/components/network/cellular_metrics_logger.h"
  13. #include "chromeos/ash/components/network/network_handler.h"
  14. #include "chromeos/ash/components/network/network_handler_test_helper.h"
  15. #include "chromeos/ash/components/network/network_state_handler.h"
  16. #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
  17. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
  18. #include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
  19. #include "ui/message_center/message_center.h"
  20. #include "ui/message_center/public/cpp/notification.h"
  21. namespace ash {
  22. namespace {
  23. const char kTestCellularServicePath[] = "cellular_service_path";
  24. const char kTestCellularDevicePath[] = "cellular_path";
  25. const char kTestCellularDevicePathName[] = "stub_cellular_device";
  26. const char kTestIccid[] = "1234567890123456789";
  27. const char kTestCellularGuid[] = "cellular_guid";
  28. const char kTestCellularName[] = "cellular_name";
  29. const char kTestEid[] = "123456789012345678901234567890123";
  30. } // namespace
  31. class ManagedSimLockNotifierTest : public NoSessionAshTestBase {
  32. protected:
  33. ManagedSimLockNotifierTest() {
  34. scoped_feature_list_.InitAndEnableFeature(features::kSimLockPolicy);
  35. }
  36. ManagedSimLockNotifierTest(const ManagedSimLockNotifierTest&) = delete;
  37. ManagedSimLockNotifierTest& operator=(const ManagedSimLockNotifierTest&) =
  38. delete;
  39. ~ManagedSimLockNotifierTest() override = default;
  40. void SetUp() override {
  41. network_handler_test_helper_ = std::make_unique<NetworkHandlerTestHelper>();
  42. network_config_helper_ = std::make_unique<
  43. chromeos::network_config::CrosNetworkConfigTestHelper>();
  44. AshTestBase::SetUp();
  45. base::RunLoop().RunUntilIdle();
  46. // User must be logged in for notification to be visible.
  47. LogIn();
  48. }
  49. void TearDown() override {
  50. AshTestBase::TearDown();
  51. network_config_helper_.reset();
  52. network_handler_test_helper_.reset();
  53. }
  54. void LogIn() { SimulateUserLogin("user1@test.com"); }
  55. void LogOut() { ClearLogin(); }
  56. ManagedNetworkConfigurationHandler* managed_network_configuration_handler() {
  57. return NetworkHandler::Get()->managed_network_configuration_handler();
  58. }
  59. void SetCellularEnabled(bool enabled) {
  60. NetworkHandler::Get()->network_state_handler()->SetTechnologyEnabled(
  61. NetworkTypePattern::Cellular(), enabled,
  62. network_handler::ErrorCallback());
  63. base::RunLoop().RunUntilIdle();
  64. }
  65. void SetCellularSimLockEnabled(
  66. bool enable,
  67. const absl::optional<std::string>& lock_type = absl::nullopt) {
  68. // Simulate a locked SIM.
  69. base::Value::Dict sim_lock_status;
  70. sim_lock_status.Set(shill::kSIMLockEnabledProperty, enable);
  71. if (lock_type.has_value())
  72. sim_lock_status.Set(shill::kSIMLockTypeProperty, *lock_type);
  73. network_config_helper_->network_state_helper()
  74. .device_test()
  75. ->SetDeviceProperty(
  76. kTestCellularDevicePath, shill::kSIMLockStatusProperty,
  77. base::Value(std::move(sim_lock_status)), /*notify_changed=*/true);
  78. // Set the cellular service to be the active profile.
  79. base::Value::List sim_slot_infos;
  80. base::Value::Dict slot_info_item;
  81. slot_info_item.Set(shill::kSIMSlotInfoICCID, kTestIccid);
  82. slot_info_item.Set(shill::kSIMSlotInfoPrimary, true);
  83. sim_slot_infos.Append(std::move(slot_info_item));
  84. network_config_helper_->network_state_helper()
  85. .device_test()
  86. ->SetDeviceProperty(
  87. kTestCellularDevicePath, shill::kSIMSlotInfoProperty,
  88. base::Value(std::move(sim_slot_infos)), /*notify_changed=*/true);
  89. base::RunLoop().RunUntilIdle();
  90. }
  91. void SetAllowCellularSimLock(bool allow_cellular_sim_lock) {
  92. base::DictionaryValue global_config;
  93. global_config.SetBoolKey(
  94. ::onc::global_network_config::kAllowCellularSimLock,
  95. allow_cellular_sim_lock);
  96. managed_network_configuration_handler()->SetPolicy(
  97. ::onc::ONC_SOURCE_DEVICE_POLICY, /*userhash=*/std::string(),
  98. base::ListValue(), global_config);
  99. base::RunLoop().RunUntilIdle();
  100. }
  101. void AddCellularDevice() {
  102. network_config_helper_->network_state_helper().AddDevice(
  103. kTestCellularDevicePath, shill::kTypeCellular,
  104. kTestCellularDevicePathName);
  105. }
  106. void AddCellularService(
  107. const std::string& service_path = kTestCellularServicePath,
  108. const std::string& iccid = kTestIccid) {
  109. // Add idle, non-connectable network.
  110. network_config_helper_->network_state_helper().service_test()->AddService(
  111. service_path, kTestCellularGuid, kTestCellularName,
  112. shill::kTypeCellular, shill::kStateIdle, /*visible=*/true);
  113. network_config_helper_->network_state_helper()
  114. .service_test()
  115. ->SetServiceProperty(service_path, shill::kEidProperty,
  116. base::Value(kTestEid));
  117. network_config_helper_->network_state_helper()
  118. .service_test()
  119. ->SetServiceProperty(service_path, shill::kIccidProperty,
  120. base::Value(iccid));
  121. base::RunLoop().RunUntilIdle();
  122. }
  123. void ClickOnNotification() {
  124. message_center::MessageCenter::Get()->ClickOnNotification(
  125. ManagedSimLockNotifier::kManagedSimLockNotificationId);
  126. }
  127. void RemoveNotification(bool by_user) {
  128. message_center::MessageCenter::Get()->RemoveNotification(
  129. ManagedSimLockNotifier::kManagedSimLockNotificationId, by_user);
  130. }
  131. // Returns the managed SIM lock notification if it is shown, and null if it is
  132. // not shown.
  133. message_center::Notification* GetManagedSimLockNotification() {
  134. return message_center::MessageCenter::Get()->FindVisibleNotificationById(
  135. ManagedSimLockNotifier::kManagedSimLockNotificationId);
  136. }
  137. base::test::ScopedFeatureList scoped_feature_list_;
  138. std::unique_ptr<chromeos::network_config::CrosNetworkConfigTestHelper>
  139. network_config_helper_;
  140. std::unique_ptr<NetworkHandlerTestHelper> network_handler_test_helper_;
  141. base::HistogramTester histogram_tester_;
  142. };
  143. TEST_F(ManagedSimLockNotifierTest, PolicyChanged) {
  144. AddCellularDevice();
  145. AddCellularService();
  146. EXPECT_FALSE(GetManagedSimLockNotification());
  147. SetCellularSimLockEnabled(true);
  148. EXPECT_FALSE(GetManagedSimLockNotification());
  149. SetAllowCellularSimLock(false);
  150. EXPECT_TRUE(GetManagedSimLockNotification());
  151. SetAllowCellularSimLock(true);
  152. EXPECT_FALSE(GetManagedSimLockNotification());
  153. }
  154. TEST_F(ManagedSimLockNotifierTest, NewActiveSession) {
  155. AddCellularDevice();
  156. AddCellularService();
  157. SetCellularSimLockEnabled(true);
  158. SetAllowCellularSimLock(false);
  159. // Notification should be shown; proceed to remove it.
  160. EXPECT_TRUE(GetManagedSimLockNotification());
  161. RemoveNotification(/*by_user=*/false);
  162. EXPECT_FALSE(GetManagedSimLockNotification());
  163. LogOut();
  164. LogIn();
  165. base::RunLoop().RunUntilIdle();
  166. // Notification should surface in new active session since the SIM is PIN
  167. // locked and policy is true.
  168. EXPECT_TRUE(GetManagedSimLockNotification());
  169. RemoveNotification(/*by_user=*/false);
  170. SetAllowCellularSimLock(true);
  171. LogOut();
  172. LogIn();
  173. base::RunLoop().RunUntilIdle();
  174. // Notification should not surface in new active session since the policy is
  175. // false.
  176. EXPECT_FALSE(GetManagedSimLockNotification());
  177. SetCellularSimLockEnabled(false);
  178. SetAllowCellularSimLock(false);
  179. LogOut();
  180. LogIn();
  181. base::RunLoop().RunUntilIdle();
  182. // Notification should not surface in new active session since the SIM is not
  183. // PIN locked.
  184. EXPECT_FALSE(GetManagedSimLockNotification());
  185. SetAllowCellularSimLock(true);
  186. LogOut();
  187. LogIn();
  188. base::RunLoop().RunUntilIdle();
  189. // Notification should not surface in new active session since the SIM is not
  190. // PIN locked.
  191. EXPECT_FALSE(GetManagedSimLockNotification());
  192. }
  193. TEST_F(ManagedSimLockNotifierTest, HideNotificationOnLockDisabled) {
  194. AddCellularDevice();
  195. AddCellularService();
  196. SetCellularSimLockEnabled(true);
  197. SetAllowCellularSimLock(false);
  198. EXPECT_TRUE(GetManagedSimLockNotification());
  199. // Notification will disappear once user disables SIM Lock setting.
  200. SetCellularSimLockEnabled(false);
  201. EXPECT_FALSE(GetManagedSimLockNotification());
  202. }
  203. TEST_F(ManagedSimLockNotifierTest, PrimarySimIccidChanged) {
  204. AddCellularDevice();
  205. AddCellularService();
  206. SetCellularSimLockEnabled(true);
  207. SetAllowCellularSimLock(false);
  208. EXPECT_TRUE(GetManagedSimLockNotification());
  209. RemoveNotification(/*by_user=*/false);
  210. EXPECT_FALSE(GetManagedSimLockNotification());
  211. // Simulate primary ICCID changed. Notification should be shown after.
  212. base::Value::List sim_slot_infos;
  213. base::Value::Dict slot_info_item;
  214. slot_info_item.Set(shill::kSIMSlotInfoICCID, kTestIccid);
  215. slot_info_item.Set(shill::kSIMSlotInfoPrimary, false);
  216. sim_slot_infos.Append(std::move(slot_info_item));
  217. base::Value::Dict slot_info_item_2;
  218. slot_info_item_2.Set(shill::kSIMSlotInfoICCID, "kTestIccid2");
  219. slot_info_item_2.Set(shill::kSIMSlotInfoPrimary, true);
  220. sim_slot_infos.Append(std::move(slot_info_item_2));
  221. network_config_helper_->network_state_helper()
  222. .device_test()
  223. ->SetDeviceProperty(kTestCellularDevicePath, shill::kSIMSlotInfoProperty,
  224. base::Value(std::move(sim_slot_infos)),
  225. /*notify_changed=*/true);
  226. base::RunLoop().RunUntilIdle();
  227. EXPECT_TRUE(GetManagedSimLockNotification());
  228. }
  229. TEST_F(ManagedSimLockNotifierTest, NotificationOnCellularOnOrOff) {
  230. base::HistogramTester histograms;
  231. AddCellularDevice();
  232. AddCellularService();
  233. SetCellularSimLockEnabled(true);
  234. SetAllowCellularSimLock(false);
  235. EXPECT_TRUE(GetManagedSimLockNotification());
  236. histograms.ExpectBucketCount(
  237. CellularMetricsLogger::kSimLockNotificationEventHistogram,
  238. CellularMetricsLogger::SimLockNotificationEvent::kShown, 1);
  239. // Notification will disappear if user turns off Cellular.
  240. SetCellularEnabled(false);
  241. EXPECT_FALSE(GetManagedSimLockNotification());
  242. // Notification will appear if user turns on Cellular.
  243. SetCellularEnabled(true);
  244. EXPECT_TRUE(GetManagedSimLockNotification());
  245. }
  246. TEST_F(ManagedSimLockNotifierTest, NotificationClicked) {
  247. base::HistogramTester histograms;
  248. AddCellularDevice();
  249. AddCellularService();
  250. SetCellularSimLockEnabled(true);
  251. SetAllowCellularSimLock(false);
  252. ClickOnNotification();
  253. histograms.ExpectBucketCount(
  254. CellularMetricsLogger::kSimLockNotificationEventHistogram,
  255. CellularMetricsLogger::SimLockNotificationEvent::kShown, 1);
  256. histograms.ExpectBucketCount(
  257. CellularMetricsLogger::kSimLockNotificationEventHistogram,
  258. CellularMetricsLogger::SimLockNotificationEvent::kClicked, 1);
  259. // Notification will be dismissed by the system, in which case we shouldn't
  260. // be emitting the dismissed by user metric.
  261. histograms.ExpectBucketCount(
  262. CellularMetricsLogger::kSimLockNotificationEventHistogram,
  263. CellularMetricsLogger::SimLockNotificationEvent::kDismissed, 0);
  264. }
  265. TEST_F(ManagedSimLockNotifierTest, NotificationDismissedByUser) {
  266. base::HistogramTester histograms;
  267. AddCellularDevice();
  268. AddCellularService();
  269. SetCellularSimLockEnabled(true);
  270. SetAllowCellularSimLock(false);
  271. RemoveNotification(/*by_user=*/true);
  272. histograms.ExpectBucketCount(
  273. CellularMetricsLogger::kSimLockNotificationEventHistogram,
  274. CellularMetricsLogger::SimLockNotificationEvent::kShown, 1);
  275. histograms.ExpectBucketCount(
  276. CellularMetricsLogger::kSimLockNotificationEventHistogram,
  277. CellularMetricsLogger::SimLockNotificationEvent::kClicked, 0);
  278. histograms.ExpectBucketCount(
  279. CellularMetricsLogger::kSimLockNotificationEventHistogram,
  280. CellularMetricsLogger::SimLockNotificationEvent::kDismissed, 1);
  281. }
  282. TEST_F(ManagedSimLockNotifierTest, SIMLockTypeMetrics) {
  283. base::HistogramTester histograms;
  284. AddCellularDevice();
  285. AddCellularService();
  286. SetCellularSimLockEnabled(true, shill::kSIMLockPin);
  287. SetAllowCellularSimLock(false);
  288. EXPECT_TRUE(GetManagedSimLockNotification());
  289. histograms.ExpectBucketCount(
  290. CellularMetricsLogger::kSimLockNotificationLockType,
  291. CellularMetricsLogger::SimPinLockType::kPinLocked, 1);
  292. histograms.ExpectBucketCount(
  293. CellularMetricsLogger::kSimLockNotificationLockType,
  294. CellularMetricsLogger::SimPinLockType::kPukLocked, 0);
  295. SetCellularSimLockEnabled(false);
  296. SetAllowCellularSimLock(true);
  297. EXPECT_FALSE(GetManagedSimLockNotification());
  298. SetCellularSimLockEnabled(true, shill::kSIMLockPuk);
  299. SetAllowCellularSimLock(false);
  300. EXPECT_TRUE(GetManagedSimLockNotification());
  301. histograms.ExpectBucketCount(
  302. CellularMetricsLogger::kSimLockNotificationLockType,
  303. CellularMetricsLogger::SimPinLockType::kPinLocked, 1);
  304. histograms.ExpectBucketCount(
  305. CellularMetricsLogger::kSimLockNotificationLockType,
  306. CellularMetricsLogger::SimPinLockType::kPukLocked, 1);
  307. }
  308. } // namespace ash