Commit e944a276 authored by Andrey Zaytsev's avatar Andrey Zaytsev Committed by Commit Bot

Moved TestPasswordsPrivateDelegate into a separate file so it can be later...

Moved TestPasswordsPrivateDelegate into a separate file so it can be later used by safety check passwords unit tests

Bug: 1015841
Change-Id: Id1662ec772f20eb8e33a98eb35b7b94be4fac422
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2096588
Commit-Queue: Andrey Zaytsev <andzaytsev@google.com>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749659}
parent 41367aa1
......@@ -288,6 +288,8 @@ jumbo_static_library("extensions") {
"api/passwords_private/passwords_private_event_router_factory.h",
"api/passwords_private/passwords_private_utils.cc",
"api/passwords_private/passwords_private_utils.h",
"api/passwords_private/test_passwords_private_delegate.cc",
"api/passwords_private/test_passwords_private_delegate.h",
"api/permissions/permissions_api.cc",
"api/permissions/permissions_api.h",
"api/permissions/permissions_api_helpers.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/passwords_private/test_passwords_private_delegate.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/api/passwords_private/passwords_private_event_router.h"
#include "chrome/browser/extensions/api/passwords_private/passwords_private_event_router_factory.h"
#include "ui/base/l10n/time_format.h"
namespace extensions {
namespace {
using ui::TimeFormat;
constexpr size_t kNumMocks = 3;
constexpr int kNumCharactersInPassword = 10;
api::passwords_private::PasswordUiEntry CreateEntry(int id) {
api::passwords_private::PasswordUiEntry entry;
entry.urls.shown = "test" + base::NumberToString(id) + ".com";
entry.urls.origin = "http://" + entry.urls.shown + "/login";
entry.urls.link = entry.urls.origin;
entry.username = "testName" + base::NumberToString(id);
entry.num_characters_in_password = kNumCharactersInPassword;
entry.id = id;
return entry;
}
api::passwords_private::ExceptionEntry CreateException(int id) {
api::passwords_private::ExceptionEntry exception;
exception.urls.shown = "exception" + base::NumberToString(id) + ".com";
exception.urls.origin = "http://" + exception.urls.shown + "/login";
exception.urls.link = exception.urls.origin;
exception.id = id;
return exception;
}
} // namespace
TestPasswordsPrivateDelegate::TestPasswordsPrivateDelegate()
: profile_(nullptr) {
// Create mock data.
for (size_t i = 0; i < kNumMocks; i++) {
current_entries_.push_back(CreateEntry(i));
current_exceptions_.push_back(CreateException(i));
}
}
TestPasswordsPrivateDelegate::~TestPasswordsPrivateDelegate() = default;
void TestPasswordsPrivateDelegate::GetSavedPasswordsList(
UiEntriesCallback callback) {
std::move(callback).Run(current_entries_);
}
void TestPasswordsPrivateDelegate::GetPasswordExceptionsList(
ExceptionEntriesCallback callback) {
std::move(callback).Run(current_exceptions_);
}
void TestPasswordsPrivateDelegate::ChangeSavedPassword(
int id,
base::string16 username,
base::Optional<base::string16> password) {
if (static_cast<size_t>(id) >= current_entries_.size())
return;
// PasswordUiEntry does not contain a password. Thus we are only updating
// the username and the length of the password.
current_entries_[id].username = base::UTF16ToUTF8(username);
if (password)
current_entries_[id].num_characters_in_password = password->size();
SendSavedPasswordsList();
}
void TestPasswordsPrivateDelegate::RemoveSavedPassword(int id) {
if (current_entries_.empty())
return;
// Since this is just mock data, remove the first entry regardless of
// the data contained.
last_deleted_entry_ = std::move(current_entries_.front());
current_entries_.erase(current_entries_.begin());
SendSavedPasswordsList();
}
void TestPasswordsPrivateDelegate::RemovePasswordException(int id) {
// Since this is just mock data, remove the first entry regardless of
// the data contained.
last_deleted_exception_ = std::move(current_exceptions_.front());
current_exceptions_.erase(current_exceptions_.begin());
SendPasswordExceptionsList();
}
// Simplified version of undo logic, only use for testing.
void TestPasswordsPrivateDelegate::UndoRemoveSavedPasswordOrException() {
if (last_deleted_entry_) {
current_entries_.insert(current_entries_.begin(),
std::move(*last_deleted_entry_));
last_deleted_entry_ = base::nullopt;
SendSavedPasswordsList();
} else if (last_deleted_exception_) {
current_exceptions_.insert(current_exceptions_.begin(),
std::move(*last_deleted_exception_));
last_deleted_exception_ = base::nullopt;
SendPasswordExceptionsList();
}
}
void TestPasswordsPrivateDelegate::RequestPlaintextPassword(
int id,
api::passwords_private::PlaintextReason reason,
PlaintextPasswordCallback callback,
content::WebContents* web_contents) {
// Return a mocked password value.
std::move(callback).Run(plaintext_password_);
}
void TestPasswordsPrivateDelegate::ImportPasswords(
content::WebContents* web_contents) {
// The testing of password importing itself should be handled via
// |PasswordManagerPorter|.
import_passwords_triggered_ = true;
}
void TestPasswordsPrivateDelegate::ExportPasswords(
base::OnceCallback<void(const std::string&)> callback,
content::WebContents* web_contents) {
// The testing of password exporting itself should be handled via
// |PasswordManagerPorter|.
export_passwords_triggered_ = true;
std::move(callback).Run(std::string());
}
void TestPasswordsPrivateDelegate::CancelExportPasswords() {
cancel_export_passwords_triggered_ = true;
}
api::passwords_private::ExportProgressStatus
TestPasswordsPrivateDelegate::GetExportProgressStatus() {
// The testing of password exporting itself should be handled via
// |PasswordManagerPorter|.
return api::passwords_private::ExportProgressStatus::
EXPORT_PROGRESS_STATUS_IN_PROGRESS;
}
bool TestPasswordsPrivateDelegate::IsOptedInForAccountStorage() {
return is_opted_in_for_account_storage_;
}
std::vector<api::passwords_private::CompromisedCredential>
TestPasswordsPrivateDelegate::GetCompromisedCredentials() {
api::passwords_private::CompromisedCredential credential;
credential.username = "alice";
credential.formatted_origin = "example.com";
credential.detailed_origin = "https://example.com";
credential.is_android_credential = false;
credential.change_password_url =
std::make_unique<std::string>("https://example.com/change-password");
credential.compromise_type = api::passwords_private::COMPROMISE_TYPE_LEAKED;
credential.compromise_time = 1583236800000; // Mar 03 2020 12:00:00 UTC
credential.elapsed_time_since_compromise = base::UTF16ToUTF8(
TimeFormat::Simple(TimeFormat::FORMAT_ELAPSED, TimeFormat::LENGTH_LONG,
base::TimeDelta::FromDays(3)));
std::vector<api::passwords_private::CompromisedCredential> credentials;
credentials.push_back(std::move(credential));
return credentials;
}
void TestPasswordsPrivateDelegate::GetPlaintextCompromisedPassword(
api::passwords_private::CompromisedCredential credential,
api::passwords_private::PlaintextReason reason,
content::WebContents* web_contents,
PlaintextCompromisedPasswordCallback callback) {
// Return a mocked password value.
if (!plaintext_password_) {
std::move(callback).Run(base::nullopt);
return;
}
credential.password =
std::make_unique<std::string>(base::UTF16ToUTF8(*plaintext_password_));
std::move(callback).Run(std::move(credential));
}
// Fake implementation of ChangeCompromisedCredential. This succeeds if the
// delegate knows of a compromised credential with the same id.
bool TestPasswordsPrivateDelegate::ChangeCompromisedCredential(
const api::passwords_private::CompromisedCredential& credential,
base::StringPiece new_password) {
return std::any_of(compromised_credentials_.begin(),
compromised_credentials_.end(),
[&credential](const auto& compromised_credential) {
return compromised_credential.id == credential.id;
});
}
// Fake implementation of RemoveCompromisedCredential. This succeeds if the
// delegate knows of a compromised credential with the same id.
bool TestPasswordsPrivateDelegate::RemoveCompromisedCredential(
const api::passwords_private::CompromisedCredential& credential) {
return base::EraseIf(compromised_credentials_,
[&credential](const auto& compromised_credential) {
return compromised_credential.id == credential.id;
}) != 0;
}
bool TestPasswordsPrivateDelegate::StartPasswordCheck() {
start_password_check_triggered_ = true;
return start_password_check_return_success_;
}
void TestPasswordsPrivateDelegate::StopPasswordCheck() {
stop_password_check_triggered_ = true;
}
api::passwords_private::PasswordCheckStatus
TestPasswordsPrivateDelegate::GetPasswordCheckStatus() {
api::passwords_private::PasswordCheckStatus status;
status.state = api::passwords_private::PASSWORD_CHECK_STATE_RUNNING;
status.already_processed = std::make_unique<int>(5);
status.remaining_in_queue = std::make_unique<int>(10);
status.elapsed_time_since_last_check =
std::make_unique<std::string>(base::UTF16ToUTF8(TimeFormat::Simple(
TimeFormat::FORMAT_ELAPSED, TimeFormat::LENGTH_SHORT,
base::TimeDelta::FromMinutes(5))));
return status;
}
void TestPasswordsPrivateDelegate::SetProfile(Profile* profile) {
profile_ = profile;
}
void TestPasswordsPrivateDelegate::SetOptedInForAccountStorage(bool opted_in) {
is_opted_in_for_account_storage_ = opted_in;
}
void TestPasswordsPrivateDelegate::AddCompromisedCredential(int id) {
api::passwords_private::CompromisedCredential cred;
cred.id = id;
compromised_credentials_.push_back(std::move(cred));
}
void TestPasswordsPrivateDelegate::SendSavedPasswordsList() {
PasswordsPrivateEventRouter* router =
PasswordsPrivateEventRouterFactory::GetForProfile(profile_);
if (router)
router->OnSavedPasswordsListChanged(current_entries_);
}
void TestPasswordsPrivateDelegate::SendPasswordExceptionsList() {
PasswordsPrivateEventRouter* router =
PasswordsPrivateEventRouterFactory::GetForProfile(profile_);
if (router)
router->OnPasswordExceptionsListChanged(current_exceptions_);
}
} // namespace extensions
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_API_PASSWORDS_PRIVATE_TEST_PASSWORDS_PRIVATE_DELEGATE_H_
#define CHROME_BROWSER_EXTENSIONS_API_PASSWORDS_PRIVATE_TEST_PASSWORDS_PRIVATE_DELEGATE_H_
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/api/passwords_private/passwords_private_delegate.h"
#include "chrome/browser/profiles/profile.h"
namespace extensions {
// A test PasswordsPrivateDelegate implementation which uses mock data.
// TestDelegate starts out with kNumMocks mocks of each type (saved password
// and password exception) and removes one mock each time RemoveSavedPassword()
// or RemovePasswordException() is called.
class TestPasswordsPrivateDelegate : public PasswordsPrivateDelegate {
public:
TestPasswordsPrivateDelegate();
~TestPasswordsPrivateDelegate() override;
// PasswordsPrivateDelegate implementation.
void GetSavedPasswordsList(UiEntriesCallback callback) override;
void GetPasswordExceptionsList(ExceptionEntriesCallback callback) override;
void ChangeSavedPassword(int id,
base::string16 username,
base::Optional<base::string16> password) override;
void RemoveSavedPassword(int id) override;
void RemovePasswordException(int id) override;
// Simplified version of undo logic, only use for testing.
void UndoRemoveSavedPasswordOrException() override;
void RequestPlaintextPassword(int id,
api::passwords_private::PlaintextReason reason,
PlaintextPasswordCallback callback,
content::WebContents* web_contents) override;
void ImportPasswords(content::WebContents* web_contents) override;
void ExportPasswords(base::OnceCallback<void(const std::string&)> callback,
content::WebContents* web_contents) override;
void CancelExportPasswords() override;
api::passwords_private::ExportProgressStatus GetExportProgressStatus()
override;
bool IsOptedInForAccountStorage() override;
std::vector<api::passwords_private::CompromisedCredential>
GetCompromisedCredentials() override;
void GetPlaintextCompromisedPassword(
api::passwords_private::CompromisedCredential credential,
api::passwords_private::PlaintextReason reason,
content::WebContents* web_contents,
PlaintextCompromisedPasswordCallback callback) override;
// Fake implementation of ChangeCompromisedCredential. This succeeds if the
// delegate knows of a compromised credential with the same id.
bool ChangeCompromisedCredential(
const api::passwords_private::CompromisedCredential& credential,
base::StringPiece new_password) override;
// Fake implementation of RemoveCompromisedCredential. This succeeds if the
// delegate knows of a compromised credential with the same id.
bool RemoveCompromisedCredential(
const api::passwords_private::CompromisedCredential& credential) override;
bool StartPasswordCheck() override;
void StopPasswordCheck() override;
api::passwords_private::PasswordCheckStatus GetPasswordCheckStatus() override;
void SetProfile(Profile* profile);
void SetOptedInForAccountStorage(bool opted_in);
void AddCompromisedCredential(int id);
void ResetPlaintextPassword() { plaintext_password_.reset(); }
bool ImportPasswordsTriggered() const { return import_passwords_triggered_; }
bool ExportPasswordsTriggered() const { return export_passwords_triggered_; }
bool CancelExportPasswordsTriggered() const {
return cancel_export_passwords_triggered_;
}
bool StartPasswordCheckTriggered() const {
return start_password_check_triggered_;
}
bool StopPasswordCheckTriggered() const {
return stop_password_check_triggered_;
}
void SetStartPasswordCheckReturnSuccess(bool value) {
start_password_check_return_success_ = value;
}
private:
void SendSavedPasswordsList();
void SendPasswordExceptionsList();
// The current list of entries/exceptions. Cached here so that when new
// observers are added, this delegate can send the current lists without
// having to request them from |password_manager_presenter_| again.
std::vector<api::passwords_private::PasswordUiEntry> current_entries_;
std::vector<api::passwords_private::ExceptionEntry> current_exceptions_;
// Simplified version of a undo manager that only allows undoing and redoing
// the very last deletion.
base::Optional<api::passwords_private::PasswordUiEntry> last_deleted_entry_;
base::Optional<api::passwords_private::ExceptionEntry>
last_deleted_exception_;
base::Optional<base::string16> plaintext_password_ =
base::ASCIIToUTF16("plaintext");
// List of compromised credentials.
std::vector<api::passwords_private::CompromisedCredential>
compromised_credentials_;
Profile* profile_ = nullptr;
bool is_opted_in_for_account_storage_ = false;
// Flags for detecting whether import/export operations have been invoked.
bool import_passwords_triggered_ = false;
bool export_passwords_triggered_ = false;
bool cancel_export_passwords_triggered_ = false;
// Flags for detecting whether password check operations have been invoked.
bool start_password_check_triggered_ = false;
bool stop_password_check_triggered_ = false;
bool start_password_check_return_success_ = true;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_PASSWORDS_PRIVATE_TEST_PASSWORDS_PRIVATE_DELEGATE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment