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 "chrome/browser/signin/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 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 // 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 ScreenlockBridge::UserPodCustomIconOptions
& icon
) override
{
144 ADD_FAILURE() << "Should not be reached.";
147 void HideUserPodCustomIcon(const std::string
& user_email
) override
{
148 ADD_FAILURE() << "Should not be reached.";
151 void EnableInput() override
{
152 ASSERT_EQ(STATE_ATTEMPTING_UNLOCK
, state_
);
153 state_
= STATE_UNLOCK_CANCELED
;
156 void SetAuthType(const std::string
& user_email
,
158 const base::string16
& auth_value
) override
{
159 ADD_FAILURE() << "Should not be reached.";
162 AuthType
GetAuthType(const std::string
& user_email
) const override
{
166 ScreenType
GetScreenType() const override
{
167 // Return an arbitrary value; this is not used by the test code.
171 void Unlock(const std::string
& user_email
) override
{
172 ASSERT_EQ(user_id_
, user_email
);
173 ASSERT_EQ(STATE_ATTEMPTING_UNLOCK
, state_
);
174 state_
= STATE_UNLOCK_DONE
;
177 void AttemptEasySignin(const std::string
& user_email
,
178 const std::string
& secret
,
179 const std::string
& key_label
) override
{
180 #if defined(OS_CHROMEOS)
181 ASSERT_EQ(user_id_
, user_email
);
183 ASSERT_EQ(STATE_ATTEMPTING_SIGNIN
, state_
);
184 if (secret
.empty()) {
185 state_
= STATE_SIGNIN_CANCELED
;
187 ASSERT_EQ(expected_secret_
, secret
);
188 ASSERT_EQ(chromeos::EasyUnlockKeyManager::GetKeyLabel(0u), key_label
);
189 state_
= STATE_SIGNIN_DONE
;
191 #else // if !defined(OS_CHROMEOS)
192 ADD_FAILURE() << "Should not be reached.";
199 std::string user_id_
;
200 std::string expected_secret_
;
202 DISALLOW_COPY_AND_ASSIGN(TestLockHandler
);
205 class EasyUnlockAuthAttemptUnlockTest
: public testing::Test
{
207 EasyUnlockAuthAttemptUnlockTest() {}
208 ~EasyUnlockAuthAttemptUnlockTest() override
{}
210 void SetUp() override
{
211 app_manager_
.reset(new FakeAppManager());
212 auth_attempt_
.reset(new EasyUnlockAuthAttempt(
213 app_manager_
.get(), kTestUser1
, EasyUnlockAuthAttempt::TYPE_UNLOCK
));
216 void TearDown() override
{
217 ScreenlockBridge::Get()->SetLockHandler(NULL
);
218 auth_attempt_
.reset();
222 void InitScreenLock() {
223 lock_handler_
.reset(new TestLockHandler(kTestUser1
));
224 lock_handler_
->set_state(TestLockHandler::STATE_ATTEMPTING_UNLOCK
);
225 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
228 scoped_ptr
<EasyUnlockAuthAttempt
> auth_attempt_
;
229 scoped_ptr
<FakeAppManager
> app_manager_
;
230 scoped_ptr
<TestLockHandler
> lock_handler_
;
233 DISALLOW_COPY_AND_ASSIGN(EasyUnlockAuthAttemptUnlockTest
);
236 TEST_F(EasyUnlockAuthAttemptUnlockTest
, StartWhenNotLocked
) {
237 ASSERT_FALSE(ScreenlockBridge::Get()->IsLocked());
239 EXPECT_FALSE(auth_attempt_
->Start());
240 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
243 TEST_F(EasyUnlockAuthAttemptUnlockTest
, StartWhenAuthTypeIsPassword
) {
245 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
246 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
248 lock_handler_
->set_auth_type(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
);
250 EXPECT_FALSE(auth_attempt_
->Start());
252 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
253 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
256 TEST_F(EasyUnlockAuthAttemptUnlockTest
,
257 StartWhenDispatchingAuthAttemptEventFails
) {
259 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
260 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
262 app_manager_
->set_auth_attempt_should_fail(true);
264 EXPECT_FALSE(auth_attempt_
->Start());
266 EXPECT_EQ(1u, app_manager_
->auth_attempt_count());
267 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
270 TEST_F(EasyUnlockAuthAttemptUnlockTest
, ResetBeforeFinalizeUnlock
) {
272 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
273 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
275 ASSERT_TRUE(auth_attempt_
->Start());
277 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
278 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
280 auth_attempt_
.reset();
282 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
285 TEST_F(EasyUnlockAuthAttemptUnlockTest
, FinalizeUnlockFailure
) {
287 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
288 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
290 ASSERT_TRUE(auth_attempt_
->Start());
292 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
293 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
295 auth_attempt_
->FinalizeUnlock(kTestUser1
, false);
297 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
300 TEST_F(EasyUnlockAuthAttemptUnlockTest
, FinalizeSigninCalled
) {
302 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
303 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
305 ASSERT_TRUE(auth_attempt_
->Start());
307 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
308 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
310 // Wrapped secret and key should be irrelevant in this case.
311 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(),
314 EXPECT_EQ(TestLockHandler::STATE_UNLOCK_CANCELED
, lock_handler_
->state());
317 TEST_F(EasyUnlockAuthAttemptUnlockTest
, UnlockSucceeds
) {
319 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
320 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
322 ASSERT_TRUE(auth_attempt_
->Start());
324 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
325 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
327 auth_attempt_
->FinalizeUnlock(kTestUser1
, true);
329 ASSERT_EQ(TestLockHandler::STATE_UNLOCK_DONE
, lock_handler_
->state());
332 TEST_F(EasyUnlockAuthAttemptUnlockTest
, FinalizeUnlockCalledForWrongUser
) {
334 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
335 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
337 ASSERT_TRUE(auth_attempt_
->Start());
339 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
340 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
342 auth_attempt_
->FinalizeUnlock(kTestUser2
, true);
344 // If FinalizeUnlock is called for an incorrect user, it should be ignored
345 // rather than cancelling the authentication.
346 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_UNLOCK
, lock_handler_
->state());
348 // When FinalizeUnlock is called for the correct user, it should work as
350 auth_attempt_
->FinalizeUnlock(kTestUser1
, true);
352 ASSERT_EQ(TestLockHandler::STATE_UNLOCK_DONE
, lock_handler_
->state());
355 #if defined(OS_CHROMEOS)
356 class EasyUnlockAuthAttemptSigninTest
: public testing::Test
{
358 EasyUnlockAuthAttemptSigninTest() {}
359 ~EasyUnlockAuthAttemptSigninTest() override
{}
361 void SetUp() override
{
362 app_manager_
.reset(new FakeAppManager());
363 auth_attempt_
.reset(new EasyUnlockAuthAttempt(
364 app_manager_
.get(), kTestUser1
, EasyUnlockAuthAttempt::TYPE_SIGNIN
));
367 void TearDown() override
{
368 ScreenlockBridge::Get()->SetLockHandler(NULL
);
369 auth_attempt_
.reset();
373 void InitScreenLock() {
374 lock_handler_
.reset(new TestLockHandler(kTestUser1
));
375 lock_handler_
->set_state(TestLockHandler::STATE_ATTEMPTING_SIGNIN
);
376 ScreenlockBridge::Get()->SetLockHandler(lock_handler_
.get());
379 scoped_ptr
<EasyUnlockAuthAttempt
> auth_attempt_
;
380 scoped_ptr
<FakeAppManager
> app_manager_
;
381 scoped_ptr
<TestLockHandler
> lock_handler_
;
384 DISALLOW_COPY_AND_ASSIGN(EasyUnlockAuthAttemptSigninTest
);
387 TEST_F(EasyUnlockAuthAttemptSigninTest
, StartWhenNotLocked
) {
388 ASSERT_FALSE(ScreenlockBridge::Get()->IsLocked());
390 EXPECT_FALSE(auth_attempt_
->Start());
391 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
394 TEST_F(EasyUnlockAuthAttemptSigninTest
, StartWhenAuthTypeIsPassword
) {
396 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
397 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
399 lock_handler_
->set_auth_type(ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
);
401 EXPECT_FALSE(auth_attempt_
->Start());
403 EXPECT_EQ(0u, app_manager_
->auth_attempt_count());
404 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
407 TEST_F(EasyUnlockAuthAttemptSigninTest
,
408 StartWhenDispatchingAuthAttemptEventFails
) {
410 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
411 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
413 app_manager_
->set_auth_attempt_should_fail(true);
415 EXPECT_FALSE(auth_attempt_
->Start());
417 EXPECT_EQ(1u, app_manager_
->auth_attempt_count());
418 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
421 TEST_F(EasyUnlockAuthAttemptSigninTest
, ResetBeforeFinalizeSignin
) {
423 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
424 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
426 ASSERT_TRUE(auth_attempt_
->Start());
428 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
429 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
431 auth_attempt_
.reset();
433 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
436 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeSigninWithEmtpySecret
) {
438 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
439 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
441 ASSERT_TRUE(auth_attempt_
->Start());
443 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
444 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
446 auth_attempt_
->FinalizeSignin(kTestUser1
, "", GetSessionKey());
448 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
451 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeSigninWithEmtpyKey
) {
453 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
454 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
456 ASSERT_TRUE(auth_attempt_
->Start());
458 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
459 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
461 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(), "");
463 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
466 TEST_F(EasyUnlockAuthAttemptSigninTest
, SigninSuccess
) {
468 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
469 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
471 ASSERT_TRUE(auth_attempt_
->Start());
473 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
474 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
476 lock_handler_
->set_expected_secret(GetSecret());
477 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(),
480 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_DONE
, lock_handler_
->state());
483 TEST_F(EasyUnlockAuthAttemptSigninTest
, WrongWrappedSecret
) {
485 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
486 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
488 ASSERT_TRUE(auth_attempt_
->Start());
490 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
491 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
493 auth_attempt_
->FinalizeSignin(kTestUser1
, "wrong_secret", GetSessionKey());
495 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
498 TEST_F(EasyUnlockAuthAttemptSigninTest
, InvalidSessionKey
) {
500 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
501 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
503 ASSERT_TRUE(auth_attempt_
->Start());
505 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
506 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
508 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(), "invalid_key");
510 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
513 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeUnlockCalled
) {
515 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
516 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
518 ASSERT_TRUE(auth_attempt_
->Start());
520 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
521 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
523 auth_attempt_
->FinalizeUnlock(kTestUser1
, true);
525 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_CANCELED
, lock_handler_
->state());
528 TEST_F(EasyUnlockAuthAttemptSigninTest
, FinalizeSigninCalledForWrongUser
) {
530 ASSERT_TRUE(ScreenlockBridge::Get()->IsLocked());
531 ASSERT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
533 ASSERT_TRUE(auth_attempt_
->Start());
535 ASSERT_EQ(1u, app_manager_
->auth_attempt_count());
536 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
538 lock_handler_
->set_expected_secret(GetSecret());
540 auth_attempt_
->FinalizeSignin(kTestUser2
, GetWrappedSecret(),
543 EXPECT_EQ(TestLockHandler::STATE_ATTEMPTING_SIGNIN
, lock_handler_
->state());
545 auth_attempt_
->FinalizeSignin(kTestUser1
, GetWrappedSecret(),
548 EXPECT_EQ(TestLockHandler::STATE_SIGNIN_DONE
, lock_handler_
->state());
550 #endif // defined(OS_CHROMEOS)