1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webauthn/sheet_models.h"
#include <string>
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/webauthn/authenticator_request_dialog_model.h"
#include "chrome/browser/webauthn/authenticator_transport.h"
#include "chrome/grit/generated_resources.h"
#include "device/fido/fido_types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
namespace {
using Mechanism = AuthenticatorRequestDialogModel::Mechanism;
using MechanismVisibility =
AuthenticatorGenericErrorSheetModel::OtherMechanismButtonVisibility;
class AuthenticatorSheetBaseTest : public testing::Test {};
class TestAuthenticatorSheetModel : public AuthenticatorSheetModelBase {
public:
TestAuthenticatorSheetModel(
AuthenticatorRequestDialogModel* dialog_model,
OtherMechanismButtonVisibility other_mechanism_button_visibility)
: AuthenticatorSheetModelBase(dialog_model,
other_mechanism_button_visibility) {
vector_illustrations_.emplace(kPasskeyUsbDarkIcon, kPasskeyUsbDarkIcon);
}
std::u16string GetStepTitle() const override { return u"Step title"; }
std::u16string GetStepDescription() const override {
return u"Step description";
}
bool IsOtherMechanismButtonVisible() const override {
return AuthenticatorSheetModelBase::IsOtherMechanismButtonVisible();
}
};
TEST_F(AuthenticatorSheetBaseTest, IsOtherMechanismButtonVisible) {
auto dialog_model = base::MakeRefCounted<AuthenticatorRequestDialogModel>(
/*render_frame_host=*/nullptr);
// No mechanisms present.
{
TestAuthenticatorSheetModel sheet_model(dialog_model.get(),
MechanismVisibility::kVisible);
dialog_model->mechanisms.clear();
EXPECT_FALSE(sheet_model.IsOtherMechanismButtonVisible());
}
// Two mechanisms present.
{
TestAuthenticatorSheetModel sheet_model(dialog_model.get(),
MechanismVisibility::kVisible);
dialog_model->mechanisms.clear();
dialog_model->mechanisms.emplace_back(
Mechanism::Credential(
{device::AuthenticatorType::kEnclave, {0}, std::nullopt}),
u"credential", u"cred", kPasskeyAoaIcon, base::DoNothing());
dialog_model->mechanisms.emplace_back(
Mechanism::Transport(AuthenticatorTransport::kUsbHumanInterfaceDevice),
u"security key", u"usb", kPasskeyAoaIcon, base::DoNothing());
EXPECT_TRUE(sheet_model.IsOtherMechanismButtonVisible());
}
// Hidden button.
{
TestAuthenticatorSheetModel sheet_model(dialog_model.get(),
MechanismVisibility::kHidden);
dialog_model->mechanisms.clear();
dialog_model->mechanisms.emplace_back(
Mechanism::Credential(
{device::AuthenticatorType::kEnclave, {0}, std::nullopt}),
u"credential", u"cred", kPasskeyAoaIcon, base::DoNothing());
EXPECT_FALSE(sheet_model.IsOtherMechanismButtonVisible());
}
}
// Regression test for crbug.com/1408492.
TEST_F(AuthenticatorSheetBaseTest,
IsOtherMechanismButtonVisible_NoDialogModel) {
auto dialog_model = base::MakeRefCounted<AuthenticatorRequestDialogModel>(
/*render_frame_host=*/nullptr);
TestAuthenticatorSheetModel sheet_model(dialog_model.get(),
MechanismVisibility::kVisible);
dialog_model.reset();
EXPECT_FALSE(sheet_model.IsOtherMechanismButtonVisible());
}
class AuthenticatorMultiSourcePickerSheetModelTest : public testing::Test {};
constexpr char16_t kPasskeyName1[] = u"yuki";
constexpr char16_t kPasskeyName2[] = u"kodai";
TEST_F(AuthenticatorMultiSourcePickerSheetModelTest, GPMPasskeysOnly) {
auto dialog_model = base::MakeRefCounted<AuthenticatorRequestDialogModel>(
/*render_frame_host=*/nullptr);
dialog_model->mechanisms.emplace_back(
Mechanism::Credential(
{device::AuthenticatorType::kEnclave, {0}, std::nullopt}),
kPasskeyName1, kPasskeyName1, kPasskeyPhoneIcon, base::DoNothing());
dialog_model->mechanisms.emplace_back(
Mechanism::Credential(
{device::AuthenticatorType::kEnclave, {1}, std::nullopt}),
kPasskeyName2, kPasskeyName2, kPasskeyPhoneIcon, base::DoNothing());
dialog_model->mechanisms.emplace_back(
Mechanism::Transport(AuthenticatorTransport::kUsbHumanInterfaceDevice),
u"security key", u"usb", kPasskeyAoaIcon, base::DoNothing());
AuthenticatorMultiSourcePickerSheetModel model(dialog_model.get());
EXPECT_THAT(model.primary_passkey_indices(), testing::ElementsAre(0, 1));
EXPECT_THAT(model.secondary_passkey_indices(), testing::ElementsAre(2));
EXPECT_EQ(model.primary_passkeys_label(),
l10n_util::GetStringUTF16(IDS_WEBAUTHN_THIS_DEVICE_LABEL));
}
TEST_F(AuthenticatorMultiSourcePickerSheetModelTest,
GPMPasskeysAndLocalPasskeys) {
auto dialog_model = base::MakeRefCounted<AuthenticatorRequestDialogModel>(
/*render_frame_host=*/nullptr);
dialog_model->mechanisms.emplace_back(
Mechanism::Credential(
{device::AuthenticatorType::kEnclave, {0}, std::nullopt}),
kPasskeyName1, kPasskeyName1, kPasskeyPhoneIcon, base::DoNothing());
dialog_model->mechanisms.emplace_back(
Mechanism::Credential(
{device::AuthenticatorType::kTouchID, {1}, std::nullopt}),
kPasskeyName2, kPasskeyName2, kPasskeyAoaIcon, base::DoNothing());
dialog_model->mechanisms.emplace_back(
Mechanism::Transport(AuthenticatorTransport::kUsbHumanInterfaceDevice),
u"security key", u"usb", kPasskeyAoaIcon, base::DoNothing());
AuthenticatorMultiSourcePickerSheetModel model(dialog_model.get());
EXPECT_THAT(model.primary_passkey_indices(), testing::ElementsAre(0, 1));
EXPECT_THAT(model.secondary_passkey_indices(), testing::ElementsAre(2));
EXPECT_EQ(model.primary_passkeys_label(),
l10n_util::GetStringUTF16(IDS_WEBAUTHN_THIS_DEVICE_LABEL));
}
TEST_F(AuthenticatorMultiSourcePickerSheetModelTest, NoDiscoveredPasskeys) {
auto dialog_model = base::MakeRefCounted<AuthenticatorRequestDialogModel>(
/*render_frame_host=*/nullptr);
dialog_model->mechanisms.emplace_back(
Mechanism::Transport(AuthenticatorTransport::kUsbHumanInterfaceDevice),
u"security key", u"usb", kPasskeyAoaIcon, base::DoNothing());
AuthenticatorMultiSourcePickerSheetModel model(dialog_model.get());
EXPECT_TRUE(model.primary_passkey_indices().empty());
EXPECT_THAT(model.secondary_passkey_indices(), testing::ElementsAre(0));
EXPECT_EQ(model.primary_passkeys_label(), u"");
}
} // namespace
|