webauthn_dialog_controller_impl_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2020 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/in_session_auth/webauthn_dialog_controller_impl.h"
  5. #include "ash/in_session_auth/mock_in_session_auth_dialog_client.h"
  6. #include "ash/shell.h"
  7. #include "ash/test/ash_test_base.h"
  8. #include "base/callback.h"
  9. #include "base/test/bind.h"
  10. using ::testing::_;
  11. namespace ash {
  12. namespace {
  13. using WebAuthNDialogControllerImplTest = AshTestBase;
  14. TEST_F(WebAuthNDialogControllerImplTest, PinAuthSuccess) {
  15. WebAuthNDialogController* controller =
  16. Shell::Get()->webauthn_dialog_controller();
  17. auto client = std::make_unique<MockInSessionAuthDialogClient>();
  18. std::string pin = "123456";
  19. EXPECT_CALL(*client, AuthenticateUserWithPasswordOrPin(
  20. pin, /* authenticated_by_pin = */ true, _))
  21. .WillOnce([](const std::string& pin, bool authenticated_by_pin,
  22. base::OnceCallback<void(bool success)> controller_callback) {
  23. std::move(controller_callback).Run(true);
  24. });
  25. bool result_success = false;
  26. controller->AuthenticateUserWithPasswordOrPin(
  27. pin,
  28. /*authenticated_by_password=*/true,
  29. /* View callback will be executed during controller callback. */
  30. base::BindLambdaForTesting(
  31. [&result_success](bool success, bool can_use_pin) {
  32. result_success = success;
  33. }));
  34. EXPECT_TRUE(result_success);
  35. }
  36. TEST_F(WebAuthNDialogControllerImplTest, PinAuthFail) {
  37. WebAuthNDialogController* controller =
  38. Shell::Get()->webauthn_dialog_controller();
  39. auto client = std::make_unique<MockInSessionAuthDialogClient>();
  40. std::string pin = "123456";
  41. EXPECT_CALL(*client, AuthenticateUserWithPasswordOrPin(
  42. pin, /* authenticated_by_pin = */ true, _))
  43. .WillOnce([](const std::string& pin, bool authenticated_by_pin,
  44. base::OnceCallback<void(bool success)> controller_callback) {
  45. std::move(controller_callback).Run(false);
  46. });
  47. EXPECT_CALL(*client, CheckPinAuthAvailability(_, _))
  48. .WillOnce([](const AccountId& account_id,
  49. base::OnceCallback<void(bool success)> controller_callback) {
  50. std::move(controller_callback).Run(true);
  51. });
  52. bool result_success = false;
  53. bool result_can_use_pin = false;
  54. controller->AuthenticateUserWithPasswordOrPin(
  55. pin,
  56. /*authenticated_by_password=*/true,
  57. /* View callback will be executed during controller callback. */
  58. base::BindLambdaForTesting([&result_success, &result_can_use_pin](
  59. bool success, bool can_use_pin) {
  60. result_success = success;
  61. result_can_use_pin = can_use_pin;
  62. }));
  63. EXPECT_FALSE(result_success);
  64. EXPECT_TRUE(result_can_use_pin);
  65. }
  66. TEST_F(WebAuthNDialogControllerImplTest, PinAuthFailLockout) {
  67. WebAuthNDialogController* controller =
  68. Shell::Get()->webauthn_dialog_controller();
  69. auto client = std::make_unique<MockInSessionAuthDialogClient>();
  70. std::string pin = "123456";
  71. EXPECT_CALL(*client, AuthenticateUserWithPasswordOrPin(
  72. pin, /* authenticated_by_pin = */ true, _))
  73. .WillOnce([](const std::string& pin, bool authenticated_by_pin,
  74. base::OnceCallback<void(bool success)> controller_callback) {
  75. std::move(controller_callback).Run(false);
  76. });
  77. EXPECT_CALL(*client, CheckPinAuthAvailability(_, _))
  78. .WillOnce([](const AccountId& account_id,
  79. base::OnceCallback<void(bool success)> controller_callback) {
  80. std::move(controller_callback).Run(false);
  81. });
  82. bool result_success = false;
  83. bool result_can_use_pin = false;
  84. controller->AuthenticateUserWithPasswordOrPin(
  85. pin,
  86. /*authenticated_by_password=*/true,
  87. /* View callback will be executed during controller callback. */
  88. base::BindLambdaForTesting([&result_success, &result_can_use_pin](
  89. bool success, bool can_use_pin) {
  90. result_success = success;
  91. result_can_use_pin = can_use_pin;
  92. }));
  93. EXPECT_FALSE(result_success);
  94. EXPECT_FALSE(result_can_use_pin);
  95. }
  96. TEST_F(WebAuthNDialogControllerImplTest, PasswordAuthSuccess) {
  97. WebAuthNDialogController* controller =
  98. Shell::Get()->webauthn_dialog_controller();
  99. auto client = std::make_unique<MockInSessionAuthDialogClient>();
  100. std::string password = "abcdef";
  101. EXPECT_CALL(*client, AuthenticateUserWithPasswordOrPin(
  102. password, /* authenticated_by_pin = */ false, _))
  103. .WillOnce([](const std::string& password, bool authenticated_by_pin,
  104. base::OnceCallback<void(bool success)> controller_callback) {
  105. std::move(controller_callback).Run(true);
  106. });
  107. bool result_success = false;
  108. controller->AuthenticateUserWithPasswordOrPin(
  109. password,
  110. /*authenticated_by_password=*/false,
  111. /* View callback will be executed during controller callback. */
  112. base::BindLambdaForTesting(
  113. [&result_success](bool success, bool can_use_pin) {
  114. result_success = success;
  115. }));
  116. EXPECT_TRUE(result_success);
  117. }
  118. TEST_F(WebAuthNDialogControllerImplTest, PasswordAuthFail) {
  119. WebAuthNDialogController* controller =
  120. Shell::Get()->webauthn_dialog_controller();
  121. auto client = std::make_unique<MockInSessionAuthDialogClient>();
  122. std::string password = "abcdef";
  123. EXPECT_CALL(*client, AuthenticateUserWithPasswordOrPin(
  124. password, /* authenticated_by_pin = */ false, _))
  125. .WillOnce([](const std::string& password, bool authenticated_by_pin,
  126. base::OnceCallback<void(bool success)> controller_callback) {
  127. std::move(controller_callback).Run(false);
  128. });
  129. EXPECT_CALL(*client, CheckPinAuthAvailability(_, _))
  130. .WillOnce([](const AccountId& account_id,
  131. base::OnceCallback<void(bool success)> controller_callback) {
  132. std::move(controller_callback).Run(false);
  133. });
  134. bool result_success = false;
  135. controller->AuthenticateUserWithPasswordOrPin(
  136. password,
  137. /*authenticated_by_password=*/false,
  138. /* View callback will be executed during controller callback. */
  139. base::BindLambdaForTesting(
  140. [&result_success](bool success, bool can_use_pin) {
  141. result_success = success;
  142. }));
  143. EXPECT_FALSE(result_success);
  144. }
  145. } // namespace
  146. } // namespace ash