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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/in_session_auth/in_session_auth_dialog_contents_view.h"
#include <memory>
#include <optional>
#include <utility>
#include "ash/public/cpp/in_session_auth_token_provider.h"
#include "ash/public/cpp/test/mock_in_session_auth_token_provider.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/test_widget_builder.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "chromeos/ash/components/auth_panel/impl/auth_panel.h"
#include "chromeos/ash/components/auth_panel/impl/views/password_auth_view.h"
#include "chromeos/ash/components/cryptohome/auth_factor.h"
#include "chromeos/ash/components/cryptohome/common_types.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/cryptohome/error_types.h"
#include "chromeos/ash/components/dbus/cryptohome/UserDataAuth.pb.h"
#include "chromeos/ash/components/dbus/userdataauth/userdataauth_client.h"
#include "chromeos/ash/components/login/auth/auth_performer.h"
#include "chromeos/ash/components/login/auth/mock_auth_performer.h"
#include "chromeos/ash/components/login/auth/public/auth_callbacks.h"
#include "chromeos/ash/components/login/auth/public/auth_session_intent.h"
#include "chromeos/ash/components/login/auth/public/authentication_error.h"
#include "chromeos/ash/components/login/auth/public/cryptohome_key_constants.h"
#include "chromeos/ash/components/login/auth/public/session_auth_factors.h"
#include "chromeos/ash/components/osauth/public/auth_engine_api.h"
#include "chromeos/ash/components/osauth/public/auth_factor_status_consumer.h"
#include "chromeos/ash/components/osauth/public/common_types.h"
#include "chromeos/ash/components/osauth/test_support/mock_auth_hub.h"
#include "chromeos/ash/components/osauth/test_support/mock_auth_hub_connector.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
#include "ui/display/screen.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/widget/widget_delegate.h"
namespace ash {
namespace {
using ::cryptohome::KeyLabel;
using ::testing::_;
const char kExpectedPassword[] = "expectedpassword";
} // namespace
class InSessionAuthDialogContentsViewTest : public AshTestBase {
public:
InSessionAuthDialogContentsViewTest() = default;
void SetUp() override {
AshTestBase::SetUp();
UserDataAuthClient::InitializeFake();
auth_hub_ = std::make_unique<MockAuthHub>();
}
void TearDown() override {
// Clean up all UI objects before tearing down Ash Shell in base test.
auth_panel_test_api_.reset();
password_auth_view_test_api_.reset();
contents_view_test_api_.reset();
contents_view_ = nullptr;
dialog_.reset();
AshTestBase::TearDown();
}
protected:
std::unique_ptr<views::Widget> CreateAuthDialogWidget(
std::unique_ptr<views::View> contents_view) {
std::unique_ptr<views::Widget> widget = CreateFramelessTestWidget();
widget->SetContentsView(std::move(contents_view));
return widget;
}
void CreateAndShowDialog() {
auto contents_view = std::make_unique<InSessionAuthDialogContentsView>(
prompt_,
base::BindOnce(
&InSessionAuthDialogContentsViewTest::OnEndAuthentication,
base::Unretained(this)),
base::BindRepeating(&InSessionAuthDialogContentsViewTest::
OnAuthPanelPreferredSizeChanged,
base::Unretained(this)),
auth_hub_connector_.get(), auth_hub_.get());
// The order of instantiation is important. Resetting `dialog_`
// destroys the owned ContentsView, causing our `contents_view_`
// pointer to dangle.
contents_view_ = contents_view.get();
contents_view_test_api_ =
std::make_unique<InSessionAuthDialogContentsView::TestApi>(
contents_view_);
auth_panel_test_api_ =
std::make_unique<AuthPanel::TestApi>(contents_view_->GetAuthPanel());
password_auth_view_test_api_.reset();
dialog_ = CreateAuthDialogWidget(std::move(contents_view));
CenterDialogOnDisplay();
dialog_->Show();
}
void OnEndAuthentication() { end_authentication_notifications_++; }
void CenterDialogOnDisplay() {
auto bounds = display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
bounds.ClampToCenteredSize(dialog_->GetContentsView()->GetPreferredSize());
dialog_->SetBounds(bounds);
}
void OnAuthPanelPreferredSizeChanged() {
size_changed_notifications_count_++;
CenterDialogOnDisplay();
}
void InitializeUiWithOnlyPasswordFactor() {
contents_view_->GetAuthPanel()->InitializeUi(
AuthFactorsSet{AshAuthFactor::kGaiaPassword},
auth_hub_connector_.get());
password_auth_view_test_api_ = std::make_unique<PasswordAuthView::TestApi>(
auth_panel_test_api_->GetPasswordAuthView());
}
void RunPreMouseClickAssertions(views::Button* view) {
EXPECT_TRUE(view->IsMouseHovered());
EXPECT_TRUE(view->GetEnabled());
EXPECT_TRUE(views::InkDrop::Get(view->ink_drop_view())->GetHighlighted());
}
void TypePasswordAndAssertEnteredCorrectly(const std::string& password) {
views::View* password_textfield =
password_auth_view_test_api_->GetPasswordTextfield();
LeftClickOn(password_textfield);
for (char c : password) {
EXPECT_TRUE(absl::ascii_isalpha(static_cast<unsigned char>(c)));
GetEventGenerator()->PressAndReleaseKey(
static_cast<ui::KeyboardCode>(ui::KeyboardCode::VKEY_A + (c - 'a')),
ui::EF_NONE);
}
ASSERT_EQ(password_auth_view_test_api_->GetPasswordTextfield()->GetText(),
base::UTF8ToUTF16(std::string{kExpectedPassword}));
}
void PressCloseButton() {
auto* generator = GetEventGenerator();
views::Button* close_dialog_button =
contents_view_test_api_->GetCloseButton();
generator->MoveMouseTo(
close_dialog_button->GetBoundsInScreen().CenterPoint());
RunPreMouseClickAssertions(close_dialog_button);
generator->ClickLeftButton();
}
void PressAndReleaseEscapeButton() {
auto* generator = GetEventGenerator();
generator->PressAndReleaseKey(ui::KeyboardCode::VKEY_ESCAPE);
}
void PressAndReleaseEnterButton() {
auto* generator = GetEventGenerator();
generator->PressAndReleaseKey(ui::KeyboardCode::VKEY_RETURN);
}
void SubmitPassword() {
views::View* submit_password_button =
password_auth_view_test_api_->GetSubmitPasswordButton();
LeftClickOn(submit_password_button);
}
void NotifyAuthPanelPasswordFactorReady() {
contents_view_->GetAuthPanel()->OnFactorStatusesChanged(FactorsStatusMap{
{AshAuthFactor::kGaiaPassword, AuthFactorState::kFactorReady}});
}
void NotifyAuthPanelFactorListChanged() {
// Calling `OnFactorListChanged` will destroy the `PasswordAuthView` to
// which the test api currently holds a pointer. To avoid dangling,
// invalidate it and recreate it after rebuilding the UI.
password_auth_view_test_api_ = nullptr;
contents_view_->GetAuthPanel()->OnFactorListChanged(FactorsStatusMap{
{AshAuthFactor::kGaiaPassword, AuthFactorState::kCheckingForPresence},
{AshAuthFactor::kCryptohomePin,
AuthFactorState::kCheckingForPresence}});
password_auth_view_test_api_ = std::make_unique<PasswordAuthView::TestApi>(
auth_panel_test_api_->GetPasswordAuthView());
}
std::unique_ptr<MockAuthHubConnector> auth_hub_connector_;
std::unique_ptr<MockAuthHub> auth_hub_;
std::unique_ptr<views::Widget> dialog_;
raw_ptr<InSessionAuthDialogContentsView> contents_view_;
std::unique_ptr<InSessionAuthDialogContentsView::TestApi>
contents_view_test_api_;
std::unique_ptr<AuthPanel::TestApi> auth_panel_test_api_;
std::unique_ptr<PasswordAuthView::TestApi> password_auth_view_test_api_;
int size_changed_notifications_count_ = 0;
int end_authentication_notifications_ = 0;
private:
std::optional<std::string> prompt_;
};
// Tests that the authentication dialog correctly notifies
// AuthHub to canel the current attempt.
TEST_F(InSessionAuthDialogContentsViewTest, CancelDialog) {
CreateAndShowDialog();
EXPECT_CALL(*auth_hub_, CancelCurrentAttempt);
PressCloseButton();
}
// Tests that the authentication dialog correctly notifies
// AuthHub to canel the current attempt twice.
TEST_F(InSessionAuthDialogContentsViewTest, CancelDialogTwice) {
CreateAndShowDialog();
EXPECT_CALL(*auth_hub_, CancelCurrentAttempt).Times(1);
PressCloseButton();
testing::Mock::VerifyAndClearExpectations(auth_hub_.get());
// And once again.
CreateAndShowDialog();
EXPECT_CALL(*auth_hub_, CancelCurrentAttempt).Times(1);
PressCloseButton();
}
// Tests that the authentication dialog correctly notifies
// AuthHub to cancel the current attempt when a password is typed into
// the input field.
TEST_F(InSessionAuthDialogContentsViewTest, TypePasswordAndCloseDialog) {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
NotifyAuthPanelPasswordFactorReady();
TypePasswordAndAssertEnteredCorrectly(kExpectedPassword);
EXPECT_CALL(*auth_hub_, CancelCurrentAttempt);
ASSERT_EQ(password_auth_view_test_api_->GetPasswordTextfield()->GetText(),
base::UTF8ToUTF16(std::string{kExpectedPassword}));
PressCloseButton();
}
// Tests that `AuthPanel` correctly notifies its parent View that
// the preferred size has changed to new factor becoming available, or
// existing factors becoming unavailable.
TEST_F(InSessionAuthDialogContentsViewTest,
AuthPanelNotifiesToRecenterDialogWhenPrefferredSizeChangesOnInit) {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
ASSERT_EQ(size_changed_notifications_count_, 1);
}
// Tests that `AuthPanel` correctly notifies its parent View that
// the preferred size has changed to new factor becoming available, or
// existing factors becoming unavailable.
TEST_F(
InSessionAuthDialogContentsViewTest,
AuthPanelNotifiesToRecenterDialogWhenPrefferredSizeChangesOnFactorsChange) {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
NotifyAuthPanelFactorListChanged();
// Called once the UI is initialized with the the initial set of factor, and
// also when the UI is destroyed and recreated as part of
// `OnFactorListChanged`.
ASSERT_EQ(size_changed_notifications_count_, 2);
}
// Tests that `AuthPanel` correctly calls the submit password callback,
// which in production, calls `AuthEngineApi::AuthenticateWithPassword`.
TEST_F(InSessionAuthDialogContentsViewTest, TypePasswordAndSubmit) {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
NotifyAuthPanelPasswordFactorReady();
TypePasswordAndAssertEnteredCorrectly(kExpectedPassword);
ASSERT_EQ(password_auth_view_test_api_->GetPasswordTextfield()->GetText(),
base::UTF8ToUTF16(std::string{kExpectedPassword}));
base::test::TestFuture<AuthHubConnector*, AshAuthFactor, const std::string&>
future;
auth_panel_test_api_->SetSubmitPasswordCallback(
future.GetRepeatingCallback());
SubmitPassword();
auto [connector, factor, password] = future.Take();
EXPECT_EQ(password, std::string{kExpectedPassword});
EXPECT_EQ(connector, auth_hub_connector_.get());
}
// Tests that `AuthPanel` correctly calls the submit password callback,
// which in production, calls `AuthEngineApi::AuthenticateWithPassword`.
// In this test we show and close the dialog prior to reopening it again and
// submitting a password.
TEST_F(InSessionAuthDialogContentsViewTest, ShowCloseThenSubmitPassword) {
auto show_and_type_password = [this]() {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
NotifyAuthPanelPasswordFactorReady();
TypePasswordAndAssertEnteredCorrectly(kExpectedPassword);
};
show_and_type_password();
PressCloseButton();
show_and_type_password();
base::test::TestFuture<AuthHubConnector*, AshAuthFactor, const std::string&>
future;
auth_panel_test_api_->SetSubmitPasswordCallback(
future.GetRepeatingCallback());
SubmitPassword();
auto [connector, factor, password] = future.Take();
EXPECT_EQ(password, std::string{kExpectedPassword});
EXPECT_EQ(connector, auth_hub_connector_.get());
}
// Tests that `AuthPanel` correctly notifies parent controller that
// authentication has ended.
TEST_F(InSessionAuthDialogContentsViewTest,
AuthPanelCorrectlyNotifiesWhenAuthOver) {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
NotifyAuthPanelPasswordFactorReady();
contents_view_->GetAuthPanel()->OnEndAuthentication();
ASSERT_EQ(end_authentication_notifications_, 1);
}
TEST_F(InSessionAuthDialogContentsViewTest, AuthPanelClosesOnEscapePressed) {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
NotifyAuthPanelPasswordFactorReady();
EXPECT_CALL(*auth_hub_, CancelCurrentAttempt);
PressAndReleaseEscapeButton();
}
TEST_F(InSessionAuthDialogContentsViewTest,
AuthPanelSubmitsPasswordOnEnterPressed) {
CreateAndShowDialog();
InitializeUiWithOnlyPasswordFactor();
NotifyAuthPanelPasswordFactorReady();
TypePasswordAndAssertEnteredCorrectly(kExpectedPassword);
ASSERT_EQ(password_auth_view_test_api_->GetPasswordTextfield()->GetText(),
base::UTF8ToUTF16(std::string{kExpectedPassword}));
base::test::TestFuture<AuthHubConnector*, AshAuthFactor, const std::string&>
future;
auth_panel_test_api_->SetSubmitPasswordCallback(
future.GetRepeatingCallback());
PressAndReleaseEnterButton();
auto [connector, factor, password] = future.Take();
EXPECT_EQ(password, std::string{kExpectedPassword});
EXPECT_EQ(connector, auth_hub_connector_.get());
}
} // namespace ash
|