1 // Copyright 2015 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.
5 #include "chrome/browser/signin/easy_unlock_auth_attempt.h"
7 #include "base/macros.h"
8 #include "chrome/browser/signin/easy_unlock_app_manager.h"
9 #include "components/proximity_auth/screenlock_bridge.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #if defined(OS_CHROMEOS)
13 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h"
18 // Fake user ids used in tests.
19 const char kTestUser1
[] = "user1";
20 const char kTestUser2
[] = "user2";
22 #if defined(OS_CHROMEOS)
23 const unsigned char kSecret
[] = {
24 0x7c, 0x85, 0x82, 0x7d, 0x00, 0x1f, 0x6a, 0x29, 0x2f, 0xc4, 0xb5, 0x60,
25 0x08, 0x9b, 0xb0, 0x5b
29 const unsigned char kSessionKey
[] = {
30 0xc3, 0xd9, 0x83, 0x16, 0x52, 0xde, 0x99, 0xd7, 0x4e, 0x60, 0xf9, 0xec,
31 0xa8, 0x9c, 0x0e, 0xbe
34 const unsigned char kWrappedSecret
[] = {
35 0x3a, 0xea, 0x51, 0xd9, 0x64, 0x64, 0xe1, 0xcd, 0xd8, 0xee, 0x99, 0xf5,
36 0xb1, 0xd4, 0x9f, 0xc4, 0x28, 0xd6, 0xfd, 0x69, 0x0b, 0x9e, 0x06, 0x21,
37 0xfc, 0x40, 0x1f, 0xeb, 0x75, 0x64, 0x52, 0xd8
40 #if defined(OS_CHROMEOS)
41 std::string
GetSecret() {
42 return std::string(reinterpret_cast<const char*>(kSecret
),
47 std::string
GetWrappedSecret() {
48 return std::string(reinterpret_cast<const char*>(kWrappedSecret
),
49 arraysize(kWrappedSecret
));
52 std::string
GetSessionKey() {
53 return std::string(reinterpret_cast<const char*>(kSessionKey
),
54 arraysize(kSessionKey
));
57 // Fake app manager used by the EasyUnlockAuthAttempt during tests.
58 // It tracks screenlockPrivate.onAuthAttempted events.
59 class FakeAppManager
: public EasyUnlockAppManager
{
62 : auth_attempt_count_(0u), auth_attempt_should_fail_(false) {}
63 ~FakeAppManager() override
{}
65 void EnsureReady(const base::Closure
& ready_callback
) override
{
66 ADD_FAILURE() << "Not reached";
69 void LaunchSetup() override
{ ADD_FAILURE() << "Not reached"; }
71 void LoadApp() override
{ ADD_FAILURE() << "Not reached"; }
73 void DisableAppIfLoaded() override
{ ADD_FAILURE() << "Not reached"; }
75 void ReloadApp() override
{ ADD_FAILURE() << "Not reached"; }
77 bool SendUserUpdatedEvent(const std::string
& user_id
,
79 bool data_ready
) override
{
80 ADD_FAILURE() << "Not reached";
84 bool SendAuthAttemptEvent() override
{
85 ++auth_attempt_count_
;
86 return !auth_attempt_should_fail_
;
89 size_t auth_attempt_count() const { return auth_attempt_count_
; }
91 void set_auth_attempt_should_fail(bool value
) {
92 auth_attempt_should_fail_
= value
;
96 size_t auth_attempt_count_
;
97 bool auth_attempt_should_fail_
;
99 DISALLOW_COPY_AND_ASSIGN(FakeAppManager
);
102 // Fake lock handler to be used in these tests.
103 class TestLockHandler
: public proximity_auth::ScreenlockBridge::LockHandler
{
105 // The state of unlock/signin procedure.
108 STATE_ATTEMPTING_UNLOCK
,
109 STATE_UNLOCK_CANCELED
,
111 STATE_ATTEMPTING_SIGNIN
,
112 STATE_SIGNIN_CANCELED
,
116 explicit TestLockHandler(const std::string
& user_id
)
117 : state_(STATE_NONE
), auth_type_(USER_CLICK
), user_id_(user_id
) {}
119 ~TestLockHandler() override
{}
121 void set_state(AuthState value
) { state_
= value
; }
122 AuthState
state() const { return state_
; }
124 // Changes the user associated with the lock handler.
125 // Caller should make sure that |state_| is also appropriately updated.
126 void set_user_id(const std::string
& value
) { user_id_
= value
; }
128 // Sets the secret that is expected to be sent to |AttemptEasySignin|
129 void set_expected_secret(const std::string
& value
) {
130 expected_secret_
= value
;
133 // Not using |SetAuthType| to make sure it's not called during tests.
134 void set_auth_type(AuthType value
) { auth_type_
= value
; }
136 // proximity_auth::ScreenlockBridge::LockHandler implementation:
137 void ShowBannerMessage(const base::string16
& message
) override
{
138 ADD_FAILURE() << "Should not be reached.";
141 void ShowUserPodCustomIcon(
142 const std::string
& user_email
,
143 const proximity_auth::ScreenlockBridge::UserPodCustomIconOptions
& icon
)
145 ADD_FAILURE() << "Should not be reached.";
148 void HideUserPodCustomIcon(const std::string
& user_email
) override
{
149 ADD_FAILURE() << "Should not be reached.";
152 void EnableInput() override
{
153 ASSERT_EQ(STATE_ATTEMPTING_UNLOCK
, state_
);
154 state_
= STATE_UNLOCK_CANCELED
;
157 void SetAuthType(const std::string
& user_email
,
159 const base::string16
& auth_value
) override
{
160 ADD_FAILURE() << "Should not be reached.";
163 AuthType
GetAuthType(const std::string
& user_email
) const override
{
167 ScreenType
GetScreenType() const override
{
168 // Return an arbitrary value; this is not used by the test code.
172 void Unlock(const std::string
& user_email
) override
{
173 ASSERT_EQ(user_id_
, user_email
);
174 ASSERT_EQ(STATE_ATTEMPTING_UNLOCK
, state_
);
175 state_
= STATE_UNLOCK_DONE
;
178 void AttemptEasySignin(const std::string
& user_email
,
179 const std::string
& secret
,
180 const std::string
& key_label
) override
{
181 #if defined(OS_CHROMEOS)
182 ASSERT_EQ(user_id_
, user_email
);
184 ASSERT_EQ(STATE_ATTEMPTING_SIGNIN
, state_
);
185 if (secret
.empty()) {
186 state_
= STATE_SIGNIN_CANCELED
;
188 ASSERT_EQ(expected_secret_
, secret
);
189 ASSERT_EQ(chromeos::EasyUnlockKeyManager::GetKeyLabel(0u), key_label
);
190 state_
= STATE_SIGNIN_DONE
;
192 #else // if !defined(OS_CHROMEOS)
193 ADD_FAILURE() << "Should not be reached.";
200 std::string user_id_
;
201 std::string expected_secret_
;
203 DISALLOW_COPY_AND_ASSIGN(TestLockHandler
);
206 class EasyUnlockAuthAttemptUnlockTest
: public testing::Test
{
208 EasyUnlockAuthAttemptUnlockTest() {}
209 ~EasyUnlockAuthAttemptUnlockTest() override
{}
211 void SetUp() override
{
212 app_manager_
.reset(new FakeAppManager());
213 auth_attempt_
.reset(new EasyUnlockAuthAttempt(
214 app_manager_
.get(), kTestUser1
, EasyUnlockAuthAttempt::TYPE_UNLOCK
,
215 EasyUnlockAuthAttempt::FinalizedCallback()));
218 void TearDown() override
{
219 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL
);
220 auth_attempt_
.reset();
224 void InitScreenLock() {
225 lock_handler_
.reset(new TestLockHandler(kTestUser1
));
226 lock_handler_
->set_state(TestLockHandler::STATE_ATTEMPTING_UNLOCK
);
227 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(
228 lock_handler_
.get());
231 scoped_ptr
<EasyUnlockAuthAttempt
> auth_attempt_
;
232 scoped_ptr
<FakeAppManager
> app_manager_
;
233 scoped_ptr
<TestLockHandler
> lock_handler_
;
236 DISALLOW_COPY_AND_ASSIGN(EasyUnlockAuthAttemptUnlockTest
);
239 TEST_F(EasyUnlockAuthAttemptUnlockTest
, StartWhenNotLocked
) {
240 ASSERT_FALSE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
242 EXPECT_FALSE(auth_attempt_
->Start());
243 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
246 TEST_F(EasyUnlockAuthAttemptUnlockTest
, StartWhenAuthTypeIsPassword
) {
248 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
249 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
251 lock_handler_
->set_auth_type(
252 proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
);
254 EXPECT_FALSE(auth_attempt_
->Start());
256 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
257 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
260 TEST_F(EasyUnlockAuthAttemptUnlockTest
,
261 StartWhenDispatchingAuthAttemptEventFails
) {
263 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
264 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
266 app_manager_
->set_auth_attempt_should_fail(true);
268 EXPECT_FALSE(auth_attempt_
->Start());
270 EXPECT_EQ(1u, app_manager_
->auth_attempt_count());
271 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
274 TEST_F(EasyUnlockAuthAttemptUnlockTest
, ResetBeforeFinalizeUnlock
) {
276 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
277 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
279 ASSERT_TRUE(auth_attempt_
->Start());
281 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
282 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
284 auth_attempt_
.reset();
286 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
289 TEST_F(EasyUnlockAuthAttemptUnlockTest
, FinalizeUnlockFailure
) {
291 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
292 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
294 ASSERT_TRUE(auth_attempt_
->Start());
296 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
297 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
299 auth_attempt_
->FinalizeUnlock(kTestUser1
, false);
301 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
304 TEST_F(EasyUnlockAuthAttemptUnlockTest
, FinalizeSigninCalled
) {
306 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
307 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
309 ASSERT_TRUE(auth_attempt_
->Start());
311 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
312 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
314 // Wrapped secret and key should be irrelevant in this case.
315 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(),
318 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
321 TEST_F(EasyUnlockAuthAttemptUnlockTest
, UnlockSucceeds
) {
323 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
324 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
326 ASSERT_TRUE(auth_attempt_
->Start());
328 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
329 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
331 auth_attempt_
->FinalizeUnlock(kTestUser1
, true);
333 ASSERT_EQ(TestLockHandler::STATE_UNLOCK_DONE
, lock_handler_
->state());
336 TEST_F(EasyUnlockAuthAttemptUnlockTest
, FinalizeUnlockCalledForWrongUser
) {
338 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
339 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
341 ASSERT_TRUE(auth_attempt_
->Start());
343 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
344 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
346 auth_attempt_
->FinalizeUnlock(kTestUser2
, true);
348 // If FinalizeUnlock is called for an incorrect user, it should be ignored
349 // rather than cancelling the authentication.
350 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
352 // When FinalizeUnlock is called for the correct user, it should work as
354 auth_attempt_
->FinalizeUnlock(kTestUser1
, true);
356 ASSERT_EQ(TestLockHandler::STATE_UNLOCK_DONE
, lock_handler_
->state());
359 #if defined(OS_CHROMEOS)
360 class EasyUnlockAuthAttemptSigninTest
: public testing::Test
{
362 EasyUnlockAuthAttemptSigninTest() {}
363 ~EasyUnlockAuthAttemptSigninTest() override
{}
365 void SetUp() override
{
366 app_manager_
.reset(new FakeAppManager());
367 auth_attempt_
.reset(new EasyUnlockAuthAttempt(
368 app_manager_
.get(), kTestUser1
, EasyUnlockAuthAttempt::TYPE_SIGNIN
,
369 EasyUnlockAuthAttempt::FinalizedCallback()));
372 void TearDown() override
{
373 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(NULL
);
374 auth_attempt_
.reset();
378 void InitScreenLock() {
379 lock_handler_
.reset(new TestLockHandler(kTestUser1
));
380 lock_handler_
->set_state(TestLockHandler::STATE_ATTEMPTING_SIGNIN
);
381 proximity_auth::ScreenlockBridge::Get()->SetLockHandler(
382 lock_handler_
.get());
385 scoped_ptr
<EasyUnlockAuthAttempt
> auth_attempt_
;
386 scoped_ptr
<FakeAppManager
> app_manager_
;
387 scoped_ptr
<TestLockHandler
> lock_handler_
;
390 DISALLOW_COPY_AND_ASSIGN(EasyUnlockAuthAttemptSigninTest
);
393 TEST_F(EasyUnlockAuthAttemptSigninTest
, StartWhenNotLocked
) {
394 ASSERT_FALSE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
396 EXPECT_FALSE(auth_attempt_
->Start());
397 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
400 TEST_F(EasyUnlockAuthAttemptSigninTest
, StartWhenAuthTypeIsPassword
) {
402 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
403 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
405 lock_handler_
->set_auth_type(
406 proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
);
408 EXPECT_FALSE(auth_attempt_
->Start());
410 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
411 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
414 TEST_F(EasyUnlockAuthAttemptSigninTest
,
415 StartWhenDispatchingAuthAttemptEventFails
) {
417 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
418 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
420 app_manager_
->set_auth_attempt_should_fail(true);
422 EXPECT_FALSE(auth_attempt_
->Start());
424 EXPECT_EQ(1u, app_manager_
->auth_attempt_count());
425 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
428 TEST_F(EasyUnlockAuthAttemptSigninTest
, ResetBeforeFinalizeSignin
) {
430 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
431 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
433 ASSERT_TRUE(auth_attempt_
->Start());
435 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
436 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
438 auth_attempt_
.reset();
440 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
443 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeSigninWithEmtpySecret
) {
445 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
446 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
448 ASSERT_TRUE(auth_attempt_
->Start());
450 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
451 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
453 auth_attempt_
->FinalizeSignin(kTestUser1
, "", GetSessionKey());
455 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
458 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeSigninWithEmtpyKey
) {
460 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
461 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
463 ASSERT_TRUE(auth_attempt_
->Start());
465 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
466 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
468 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(), "");
470 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
473 TEST_F(EasyUnlockAuthAttemptSigninTest
, SigninSuccess
) {
475 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
476 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
478 ASSERT_TRUE(auth_attempt_
->Start());
480 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
481 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
483 lock_handler_
->set_expected_secret(GetSecret());
484 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(),
487 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_DONE
, lock_handler_
->state());
490 TEST_F(EasyUnlockAuthAttemptSigninTest
, WrongWrappedSecret
) {
492 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
493 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
495 ASSERT_TRUE(auth_attempt_
->Start());
497 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
498 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
500 auth_attempt_
->FinalizeSignin(kTestUser1
, "wrong_secret", GetSessionKey());
502 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
505 TEST_F(EasyUnlockAuthAttemptSigninTest
, InvalidSessionKey
) {
507 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
508 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
510 ASSERT_TRUE(auth_attempt_
->Start());
512 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
513 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
515 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(), "invalid_key");
517 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
520 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeUnlockCalled
) {
522 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
523 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
525 ASSERT_TRUE(auth_attempt_
->Start());
527 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
528 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
530 auth_attempt_
->FinalizeUnlock(kTestUser1
, true);
532 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
535 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeSigninCalledForWrongUser
) {
537 ASSERT_TRUE(proximity_auth::ScreenlockBridge::Get()->IsLocked());
538 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
540 ASSERT_TRUE(auth_attempt_
->Start());
542 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
543 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
545 lock_handler_
->set_expected_secret(GetSecret());
547 auth_attempt_
->FinalizeSignin(kTestUser2
, GetWrappedSecret(),
550 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
552 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(),
555 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_DONE
, lock_handler_
->state());
557 #endif // defined(OS_CHROMEOS)