proximity_auth_system_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // Copyright 2014 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/components/proximity_auth/proximity_auth_system.h"
  5. #include <memory>
  6. #include "ash/components/multidevice/logging/logging.h"
  7. #include "ash/components/multidevice/remote_device_ref.h"
  8. #include "ash/components/multidevice/remote_device_test_util.h"
  9. #include "ash/components/multidevice/software_feature_state.h"
  10. #include "ash/components/proximity_auth/fake_lock_handler.h"
  11. #include "ash/components/proximity_auth/fake_remote_device_life_cycle.h"
  12. #include "ash/components/proximity_auth/mock_proximity_auth_client.h"
  13. #include "ash/components/proximity_auth/proximity_auth_profile_pref_manager.h"
  14. #include "ash/components/proximity_auth/unlock_manager.h"
  15. #include "ash/constants/ash_features.h"
  16. #include "ash/services/multidevice_setup/public/cpp/fake_multidevice_setup_client.h"
  17. #include "ash/services/secure_channel/public/cpp/client/fake_secure_channel_client.h"
  18. #include "base/command_line.h"
  19. #include "base/test/scoped_feature_list.h"
  20. #include "base/test/test_simple_task_runner.h"
  21. #include "base/threading/thread_task_runner_handle.h"
  22. #include "testing/gmock/include/gmock/gmock.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. using testing::_;
  25. using testing::AnyNumber;
  26. using testing::AtLeast;
  27. using testing::InSequence;
  28. using testing::NiceMock;
  29. using testing::NotNull;
  30. using testing::Return;
  31. using testing::SaveArg;
  32. namespace proximity_auth {
  33. namespace {
  34. const char kUser1[] = "user1";
  35. const char kUser2[] = "user2";
  36. void CompareRemoteDeviceRefLists(
  37. const ash::multidevice::RemoteDeviceRefList& list1,
  38. const ash::multidevice::RemoteDeviceRefList& list2) {
  39. ASSERT_EQ(list1.size(), list2.size());
  40. for (size_t i = 0; i < list1.size(); ++i) {
  41. ash::multidevice::RemoteDeviceRef device1 = list1[i];
  42. ash::multidevice::RemoteDeviceRef device2 = list2[i];
  43. EXPECT_EQ(device1.public_key(), device2.public_key());
  44. }
  45. }
  46. // Creates a RemoteDeviceRef object for |user_id| with |name|.
  47. ash::multidevice::RemoteDeviceRef CreateRemoteDevice(
  48. const std::string& user_email,
  49. const std::string& name) {
  50. return ash::multidevice::RemoteDeviceRefBuilder()
  51. .SetUserEmail(user_email)
  52. .SetName(name)
  53. .Build();
  54. }
  55. // Mock implementation of UnlockManager.
  56. class MockUnlockManager : public UnlockManager {
  57. public:
  58. MockUnlockManager() {}
  59. MockUnlockManager(const MockUnlockManager&) = delete;
  60. MockUnlockManager& operator=(const MockUnlockManager&) = delete;
  61. ~MockUnlockManager() override {}
  62. MOCK_METHOD0(IsUnlockAllowed, bool());
  63. MOCK_METHOD1(SetRemoteDeviceLifeCycle, void(RemoteDeviceLifeCycle*));
  64. MOCK_METHOD1(OnAuthAttempted, void(mojom::AuthType));
  65. MOCK_METHOD0(CancelConnectionAttempt, void());
  66. MOCK_METHOD0(GetLastRemoteStatusUnlockForLogging, std::string());
  67. };
  68. // Mock implementation of ProximityAuthProfilePrefManager.
  69. class MockProximityAuthPrefManager : public ProximityAuthProfilePrefManager {
  70. public:
  71. MockProximityAuthPrefManager(
  72. ash::multidevice_setup::FakeMultiDeviceSetupClient*
  73. fake_multidevice_setup_client)
  74. : ProximityAuthProfilePrefManager(nullptr,
  75. fake_multidevice_setup_client) {}
  76. MockProximityAuthPrefManager(const MockProximityAuthPrefManager&) = delete;
  77. MockProximityAuthPrefManager& operator=(const MockProximityAuthPrefManager&) =
  78. delete;
  79. ~MockProximityAuthPrefManager() override {}
  80. MOCK_CONST_METHOD0(GetLastPasswordEntryTimestampMs, int64_t());
  81. };
  82. // Harness for ProximityAuthSystem to make it testable.
  83. class TestableProximityAuthSystem : public ProximityAuthSystem {
  84. public:
  85. TestableProximityAuthSystem(
  86. ash::secure_channel::SecureChannelClient* secure_channel_client,
  87. std::unique_ptr<UnlockManager> unlock_manager,
  88. ProximityAuthPrefManager* pref_manager)
  89. : ProximityAuthSystem(secure_channel_client, std::move(unlock_manager)),
  90. life_cycle_(nullptr) {}
  91. TestableProximityAuthSystem(const TestableProximityAuthSystem&) = delete;
  92. TestableProximityAuthSystem& operator=(const TestableProximityAuthSystem&) =
  93. delete;
  94. ~TestableProximityAuthSystem() override {}
  95. FakeRemoteDeviceLifeCycle* life_cycle() { return life_cycle_; }
  96. private:
  97. std::unique_ptr<RemoteDeviceLifeCycle> CreateRemoteDeviceLifeCycle(
  98. ash::multidevice::RemoteDeviceRef remote_device,
  99. absl::optional<ash::multidevice::RemoteDeviceRef> local_device) override {
  100. std::unique_ptr<FakeRemoteDeviceLifeCycle> life_cycle(
  101. new FakeRemoteDeviceLifeCycle(remote_device, local_device));
  102. life_cycle_ = life_cycle.get();
  103. return life_cycle;
  104. }
  105. FakeRemoteDeviceLifeCycle* life_cycle_;
  106. };
  107. } // namespace
  108. class ProximityAuthSystemTest : public testing::Test {
  109. public:
  110. ProximityAuthSystemTest(const ProximityAuthSystemTest&) = delete;
  111. ProximityAuthSystemTest& operator=(const ProximityAuthSystemTest&) = delete;
  112. protected:
  113. ProximityAuthSystemTest()
  114. : user1_local_device_(CreateRemoteDevice(kUser1, "user1_local_device")),
  115. user2_local_device_(CreateRemoteDevice(kUser2, "user2_local_device")),
  116. task_runner_(new base::TestSimpleTaskRunner()),
  117. thread_task_runner_handle_(task_runner_) {}
  118. void TearDown() override {
  119. UnlockScreen();
  120. pref_manager_.reset();
  121. }
  122. void SetUp() override {
  123. fake_multidevice_setup_client_ =
  124. std::make_unique<ash::multidevice_setup::FakeMultiDeviceSetupClient>();
  125. pref_manager_ = std::make_unique<NiceMock<MockProximityAuthPrefManager>>(
  126. fake_multidevice_setup_client_.get());
  127. user1_remote_devices_.push_back(
  128. CreateRemoteDevice(kUser1, "user1_device1"));
  129. user1_remote_devices_.push_back(
  130. CreateRemoteDevice(kUser1, "user1_device2"));
  131. user2_remote_devices_.push_back(
  132. CreateRemoteDevice(kUser2, "user2_device1"));
  133. user2_remote_devices_.push_back(
  134. CreateRemoteDevice(kUser2, "user2_device2"));
  135. user2_remote_devices_.push_back(
  136. CreateRemoteDevice(kUser2, "user2_device3"));
  137. std::unique_ptr<MockUnlockManager> unlock_manager(
  138. new NiceMock<MockUnlockManager>());
  139. unlock_manager_ = unlock_manager.get();
  140. fake_secure_channel_client_ =
  141. std::make_unique<ash::secure_channel::FakeSecureChannelClient>();
  142. proximity_auth_system_ = std::make_unique<TestableProximityAuthSystem>(
  143. fake_secure_channel_client_.get(), std::move(unlock_manager),
  144. pref_manager_.get());
  145. proximity_auth_system_->SetRemoteDevicesForUser(
  146. AccountId::FromUserEmail(kUser1), user1_remote_devices_,
  147. user1_local_device_);
  148. proximity_auth_system_->Start();
  149. LockScreen();
  150. }
  151. void LockScreen() {
  152. ScreenlockBridge::Get()->SetFocusedUser(AccountId());
  153. ScreenlockBridge::Get()->SetLockHandler(&lock_handler_);
  154. }
  155. void FocusUser(const std::string& user_email) {
  156. ScreenlockBridge::Get()->SetFocusedUser(
  157. AccountId::FromUserEmail(user_email));
  158. }
  159. void UnlockScreen() { ScreenlockBridge::Get()->SetLockHandler(nullptr); }
  160. void SimulateSuspend() {
  161. proximity_auth_system_->OnSuspend();
  162. proximity_auth_system_->OnSuspendDone();
  163. task_runner_->RunUntilIdle();
  164. }
  165. void SimulateScreenOff() {
  166. proximity_auth_system_->OnScreenOff();
  167. proximity_auth_system_->OnScreenOffDone();
  168. task_runner_->RunUntilIdle();
  169. }
  170. FakeRemoteDeviceLifeCycle* life_cycle() {
  171. return proximity_auth_system_->life_cycle();
  172. }
  173. FakeLockHandler lock_handler_;
  174. NiceMock<MockProximityAuthClient> proximity_auth_client_;
  175. std::unique_ptr<ash::secure_channel::FakeSecureChannelClient>
  176. fake_secure_channel_client_;
  177. std::unique_ptr<TestableProximityAuthSystem> proximity_auth_system_;
  178. MockUnlockManager* unlock_manager_;
  179. std::unique_ptr<MockProximityAuthPrefManager> pref_manager_;
  180. std::unique_ptr<ash::multidevice_setup::FakeMultiDeviceSetupClient>
  181. fake_multidevice_setup_client_;
  182. ash::multidevice::RemoteDeviceRef user1_local_device_;
  183. ash::multidevice::RemoteDeviceRef user2_local_device_;
  184. ash::multidevice::RemoteDeviceRefList user1_remote_devices_;
  185. ash::multidevice::RemoteDeviceRefList user2_remote_devices_;
  186. scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
  187. base::ThreadTaskRunnerHandle thread_task_runner_handle_;
  188. private:
  189. ash::multidevice::ScopedDisableLoggingForTesting disable_logging_;
  190. };
  191. TEST_F(ProximityAuthSystemTest, SetRemoteDevicesForUser_NotStarted) {
  192. AccountId account1 = AccountId::FromUserEmail(kUser1);
  193. AccountId account2 = AccountId::FromUserEmail(kUser2);
  194. proximity_auth_system_->SetRemoteDevicesForUser(
  195. account1, user1_remote_devices_, user1_local_device_);
  196. proximity_auth_system_->SetRemoteDevicesForUser(
  197. account2, user2_remote_devices_, user1_local_device_);
  198. CompareRemoteDeviceRefLists(
  199. user1_remote_devices_,
  200. proximity_auth_system_->GetRemoteDevicesForUser(account1));
  201. CompareRemoteDeviceRefLists(
  202. user2_remote_devices_,
  203. proximity_auth_system_->GetRemoteDevicesForUser(account2));
  204. CompareRemoteDeviceRefLists(
  205. ash::multidevice::RemoteDeviceRefList(),
  206. proximity_auth_system_->GetRemoteDevicesForUser(
  207. AccountId::FromUserEmail("non_existent_user@google.com")));
  208. }
  209. TEST_F(ProximityAuthSystemTest, SetRemoteDevicesForUser_Started) {
  210. AccountId account1 = AccountId::FromUserEmail(kUser1);
  211. AccountId account2 = AccountId::FromUserEmail(kUser2);
  212. proximity_auth_system_->SetRemoteDevicesForUser(
  213. account1, user1_remote_devices_, user1_local_device_);
  214. proximity_auth_system_->Start();
  215. proximity_auth_system_->SetRemoteDevicesForUser(
  216. account2, user2_remote_devices_, user2_local_device_);
  217. CompareRemoteDeviceRefLists(
  218. user1_remote_devices_,
  219. proximity_auth_system_->GetRemoteDevicesForUser(account1));
  220. CompareRemoteDeviceRefLists(
  221. user2_remote_devices_,
  222. proximity_auth_system_->GetRemoteDevicesForUser(account2));
  223. }
  224. TEST_F(ProximityAuthSystemTest, FocusRegisteredUser) {
  225. EXPECT_FALSE(life_cycle());
  226. EXPECT_EQ(std::string(),
  227. ScreenlockBridge::Get()->focused_account_id().GetUserEmail());
  228. RemoteDeviceLifeCycle* unlock_manager_life_cycle = nullptr;
  229. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_))
  230. .WillOnce(SaveArg<0>(&unlock_manager_life_cycle));
  231. FocusUser(kUser1);
  232. EXPECT_EQ(life_cycle(), unlock_manager_life_cycle);
  233. EXPECT_TRUE(life_cycle());
  234. EXPECT_FALSE(life_cycle()->started());
  235. EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_email());
  236. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  237. .Times(AtLeast(1));
  238. }
  239. TEST_F(ProximityAuthSystemTest, FocusUnregisteredUser) {
  240. EXPECT_FALSE(life_cycle());
  241. EXPECT_EQ(std::string(),
  242. ScreenlockBridge::Get()->focused_account_id().GetUserEmail());
  243. EXPECT_FALSE(life_cycle());
  244. FocusUser(kUser2);
  245. EXPECT_FALSE(life_cycle());
  246. }
  247. TEST_F(ProximityAuthSystemTest, ToggleFocus_RegisteredUsers) {
  248. proximity_auth_system_->SetRemoteDevicesForUser(
  249. AccountId::FromUserEmail(kUser1), user1_remote_devices_,
  250. user1_local_device_);
  251. proximity_auth_system_->SetRemoteDevicesForUser(
  252. AccountId::FromUserEmail(kUser2), user2_remote_devices_,
  253. user2_local_device_);
  254. RemoteDeviceLifeCycle* life_cycle1 = nullptr;
  255. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_))
  256. .WillOnce(SaveArg<0>(&life_cycle1));
  257. FocusUser(kUser1);
  258. EXPECT_EQ(kUser1, life_cycle1->GetRemoteDevice().user_email());
  259. EXPECT_EQ(user1_local_device_, life_cycle()->local_device());
  260. RemoteDeviceLifeCycle* life_cycle2 = nullptr;
  261. {
  262. InSequence sequence;
  263. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  264. .Times(AtLeast(1));
  265. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_))
  266. .WillOnce(SaveArg<0>(&life_cycle2));
  267. }
  268. FocusUser(kUser2);
  269. EXPECT_EQ(kUser2, life_cycle2->GetRemoteDevice().user_email());
  270. EXPECT_EQ(user2_local_device_, life_cycle()->local_device());
  271. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  272. .Times(AtLeast(1));
  273. }
  274. TEST_F(ProximityAuthSystemTest, ToggleFocus_UnregisteredUsers) {
  275. FocusUser(kUser2);
  276. EXPECT_FALSE(life_cycle());
  277. FocusUser("unregistered-user");
  278. EXPECT_FALSE(life_cycle());
  279. FocusUser(kUser2);
  280. EXPECT_FALSE(life_cycle());
  281. }
  282. TEST_F(ProximityAuthSystemTest, ToggleFocus_RegisteredAndUnregisteredUsers) {
  283. // Focus User 1, who is registered. This should create a new life cycle.
  284. RemoteDeviceLifeCycle* life_cycle = nullptr;
  285. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_))
  286. .WillOnce(SaveArg<0>(&life_cycle));
  287. FocusUser(kUser1);
  288. EXPECT_EQ(kUser1, life_cycle->GetRemoteDevice().user_email());
  289. // User 2 has not been registered yet, so focusing them should not create a
  290. // new life cycle.
  291. life_cycle = nullptr;
  292. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr));
  293. FocusUser(kUser2);
  294. EXPECT_FALSE(life_cycle);
  295. // Focusing back to User 1 should recreate a new life cycle.
  296. life_cycle = nullptr;
  297. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_))
  298. .WillOnce(SaveArg<0>(&life_cycle));
  299. FocusUser(kUser1);
  300. EXPECT_EQ(kUser1, life_cycle->GetRemoteDevice().user_email());
  301. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  302. .Times(AtLeast(1));
  303. }
  304. TEST_F(ProximityAuthSystemTest, ToggleFocus_SameUserRefocused) {
  305. RemoteDeviceLifeCycle* life_cycle = nullptr;
  306. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(_))
  307. .WillOnce(SaveArg<0>(&life_cycle));
  308. FocusUser(kUser1);
  309. EXPECT_EQ(kUser1, life_cycle->GetRemoteDevice().user_email());
  310. // Focusing the user again should be idempotent. The screenlock UI may call
  311. // focus on the same user multiple times.
  312. // SetRemoteDeviceLifeCycle() is only expected to be called once.
  313. FocusUser(kUser1);
  314. // The RemoteDeviceLifeCycle should be nulled upon destruction.
  315. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  316. .Times(AtLeast(1));
  317. }
  318. TEST_F(ProximityAuthSystemTest, RestartSystem_UnregisteredUserFocused) {
  319. FocusUser(kUser2);
  320. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  321. .Times(AnyNumber());
  322. proximity_auth_system_->Stop();
  323. proximity_auth_system_->Start();
  324. EXPECT_FALSE(life_cycle());
  325. }
  326. TEST_F(ProximityAuthSystemTest, StopSystem_RegisteredUserFocused) {
  327. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(NotNull()));
  328. FocusUser(kUser1);
  329. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  330. .Times(AtLeast(1));
  331. proximity_auth_system_->Stop();
  332. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(NotNull()));
  333. proximity_auth_system_->Start();
  334. EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_email());
  335. }
  336. TEST_F(ProximityAuthSystemTest, OnAuthAttempted) {
  337. FocusUser(kUser1);
  338. EXPECT_CALL(*unlock_manager_, OnAuthAttempted(_));
  339. proximity_auth_system_->OnAuthAttempted();
  340. }
  341. TEST_F(ProximityAuthSystemTest, Suspend_ScreenUnlocked) {
  342. UnlockScreen();
  343. EXPECT_FALSE(life_cycle());
  344. SimulateSuspend();
  345. EXPECT_FALSE(life_cycle());
  346. }
  347. TEST_F(ProximityAuthSystemTest, Suspend_UnregisteredUserFocused) {
  348. SimulateSuspend();
  349. EXPECT_FALSE(life_cycle());
  350. }
  351. TEST_F(ProximityAuthSystemTest, Suspend_RegisteredUserFocused) {
  352. FocusUser(kUser1);
  353. {
  354. InSequence sequence;
  355. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  356. .Times(AtLeast(1));
  357. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(NotNull()));
  358. SimulateSuspend();
  359. }
  360. EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_email());
  361. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  362. .Times(AtLeast(1));
  363. }
  364. TEST_F(ProximityAuthSystemTest, ScreenOff_ScreenUnlocked) {
  365. base::test::ScopedFeatureList feature_list(
  366. ash::features::kSmartLockBluetoothScreenOffFix);
  367. UnlockScreen();
  368. EXPECT_FALSE(life_cycle());
  369. SimulateScreenOff();
  370. EXPECT_FALSE(life_cycle());
  371. }
  372. TEST_F(ProximityAuthSystemTest, ScreenOff_UnregisteredUserFocused) {
  373. base::test::ScopedFeatureList feature_list(
  374. ash::features::kSmartLockBluetoothScreenOffFix);
  375. SimulateScreenOff();
  376. EXPECT_FALSE(life_cycle());
  377. }
  378. TEST_F(ProximityAuthSystemTest, ScreenOff_RegisteredUserFocused) {
  379. base::test::ScopedFeatureList feature_list(
  380. ash::features::kSmartLockBluetoothScreenOffFix);
  381. FocusUser(kUser1);
  382. {
  383. InSequence sequence;
  384. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  385. .Times(AtLeast(1));
  386. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(NotNull()));
  387. SimulateScreenOff();
  388. }
  389. EXPECT_EQ(kUser1, life_cycle()->GetRemoteDevice().user_email());
  390. EXPECT_CALL(*unlock_manager_, SetRemoteDeviceLifeCycle(nullptr))
  391. .Times(AtLeast(1));
  392. }
  393. } // namespace proximity_auth