| Index: components/password_manager/content/renderer/credential_manager_client_browsertest.cc
|
| diff --git a/components/password_manager/content/renderer/credential_manager_client_browsertest.cc b/components/password_manager/content/renderer/credential_manager_client_browsertest.cc
|
| index c1cee2d714be2fc947949369ed7186435235a766..cb28b974bbfe3aca9f5c21ff84a5becd7e357191 100644
|
| --- a/components/password_manager/content/renderer/credential_manager_client_browsertest.cc
|
| +++ b/components/password_manager/content/renderer/credential_manager_client_browsertest.cc
|
| @@ -6,12 +6,10 @@
|
|
|
| #include <tuple>
|
|
|
| +#include "components/password_manager/content/common/credential_manager_messages.h"
|
| #include "components/password_manager/content/renderer/credential_manager_client.h"
|
| -#include "content/public/common/service_registry.h"
|
| -#include "content/public/renderer/render_frame.h"
|
| -#include "content/public/renderer/render_view.h"
|
| #include "content/public/test/render_view_test.h"
|
| -#include "mojo/public/cpp/bindings/binding_set.h"
|
| +#include "ipc/ipc_test_sink.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| #include "third_party/WebKit/public/platform/WebCredential.h"
|
| #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h"
|
| @@ -19,58 +17,9 @@
|
| #include "third_party/WebKit/public/platform/WebPassOwnPtr.h"
|
| #include "third_party/WebKit/public/platform/WebPasswordCredential.h"
|
|
|
| -using content::ServiceRegistry;
|
| -
|
| namespace password_manager {
|
|
|
| namespace {
|
| -
|
| -const char kTestCredentialPassword[] = "https://password.com/";
|
| -const char kTestCredentialEmpty[] = "https://empty.com/";
|
| -const char kTestCredentialReject[] = "https://reject.com/";
|
| -
|
| -class FakeCredentialManager : public mojom::CredentialManager {
|
| - public:
|
| - FakeCredentialManager() {}
|
| - ~FakeCredentialManager() override {}
|
| -
|
| - void BindRequest(mojom::CredentialManagerRequest request) {
|
| - bindings_.AddBinding(this, std::move(request));
|
| - }
|
| -
|
| - private:
|
| - // mojom::CredentialManager methods:
|
| - void Store(mojom::CredentialInfoPtr credential,
|
| - const StoreCallback& callback) override {
|
| - callback.Run();
|
| - }
|
| -
|
| - void RequireUserMediation(
|
| - const RequireUserMediationCallback& callback) override {
|
| - callback.Run();
|
| - }
|
| -
|
| - void Get(bool zero_click_only,
|
| - bool include_passwords,
|
| - mojo::Array<mojo::String> federations,
|
| - const GetCallback& callback) override {
|
| - mojo::String& url = federations[0];
|
| -
|
| - if (url == kTestCredentialPassword) {
|
| - mojom::CredentialInfoPtr info = mojom::CredentialInfo::New();
|
| - info->type = mojom::CredentialType::PASSWORD;
|
| - callback.Run(mojom::CredentialManagerError::SUCCESS, std::move(info));
|
| - } else if (url == kTestCredentialEmpty) {
|
| - callback.Run(mojom::CredentialManagerError::SUCCESS,
|
| - mojom::CredentialInfo::New());
|
| - } else if (url == kTestCredentialReject) {
|
| - callback.Run(mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE,
|
| - nullptr);
|
| - }
|
| - }
|
| -
|
| - mojo::BindingSet<mojom::CredentialManager> bindings_;
|
| -};
|
|
|
| class CredentialManagerClientTest : public content::RenderViewTest {
|
| public:
|
| @@ -80,14 +29,8 @@
|
|
|
| void SetUp() override {
|
| content::RenderViewTest::SetUp();
|
| + credential_.reset(new blink::WebPasswordCredential("", "", "", GURL()));
|
| client_.reset(new CredentialManagerClient(view_));
|
| -
|
| - ServiceRegistry* registry =
|
| - view_->GetMainRenderFrame()->GetServiceRegistry();
|
| - registry->AddServiceOverrideForTesting(
|
| - mojom::CredentialManager::Name_,
|
| - base::Bind(&CredentialManagerClientTest::BindCredentialManager,
|
| - base::Unretained(this)));
|
| }
|
|
|
| void TearDown() override {
|
| @@ -96,23 +39,59 @@
|
| content::RenderViewTest::TearDown();
|
| }
|
|
|
| + IPC::TestSink& sink() { return render_thread_->sink(); }
|
| +
|
| + blink::WebCredential* credential() { return credential_.get(); }
|
| +
|
| + // The browser's response to any of the messages the client sends must contain
|
| + // a request ID so that the client knows which request is being serviced. This
|
| + // method grabs the ID from an outgoing |message_id| message, and sets the
|
| + // |request_id| param to its value. If no request ID can be found, the method
|
| + // returns false, and the |request_id| is set to -1.
|
| + //
|
| + // Clears any pending messages upon return.
|
| + bool ExtractRequestId(uint32_t message_id, int& request_id) {
|
| + request_id = -1;
|
| + const IPC::Message* message = sink().GetFirstMessageMatching(message_id);
|
| + if (!message)
|
| + return false;
|
| +
|
| + switch (message_id) {
|
| + case CredentialManagerHostMsg_Store::ID: {
|
| + std::tuple<int, CredentialInfo> param;
|
| + CredentialManagerHostMsg_Store::Read(message, ¶m);
|
| + request_id = std::get<0>(param);
|
| + break;
|
| + }
|
| +
|
| + case CredentialManagerHostMsg_RequireUserMediation::ID: {
|
| + std::tuple<int> param;
|
| + CredentialManagerHostMsg_RequireUserMediation::Read(message, ¶m);
|
| + request_id = std::get<0>(param);
|
| + break;
|
| + }
|
| +
|
| + case CredentialManagerHostMsg_RequestCredential::ID: {
|
| + std::tuple<int, bool, bool, std::vector<GURL>> param;
|
| + CredentialManagerHostMsg_RequestCredential::Read(message, ¶m);
|
| + request_id = std::get<0>(param);
|
| + break;
|
| + }
|
| +
|
| + default:
|
| + break;
|
| + }
|
| + sink().ClearMessages();
|
| + return request_id != -1;
|
| + }
|
| +
|
| bool callback_errored() const { return callback_errored_; }
|
| void set_callback_errored(bool state) { callback_errored_ = state; }
|
| bool callback_succeeded() const { return callback_succeeded_; }
|
| void set_callback_succeeded(bool state) { callback_succeeded_ = state; }
|
|
|
| - void BindCredentialManager(mojo::ScopedMessagePipeHandle handle) {
|
| - fake_cm_.BindRequest(
|
| - mojo::MakeRequest<mojom::CredentialManager>(std::move(handle)));
|
| - }
|
| -
|
| - std::unique_ptr<blink::WebPasswordCredential> credential_;
|
| - blink::WebCredentialManagerError error_;
|
| -
|
| protected:
|
| std::unique_ptr<CredentialManagerClient> client_;
|
| -
|
| - FakeCredentialManager fake_cm_;
|
|
|
| // True if a message's callback's 'onSuccess'/'onError' methods were called,
|
| // false otherwise. We put these on the test object rather than on the
|
| @@ -121,6 +100,8 @@
|
| // pending Blink-side Promise.
|
| bool callback_errored_;
|
| bool callback_succeeded_;
|
| +
|
| + std::unique_ptr<blink::WebPasswordCredential> credential_;
|
| };
|
|
|
| class TestNotificationCallbacks
|
| @@ -149,101 +130,90 @@
|
|
|
| ~TestRequestCallbacks() override {}
|
|
|
| - void onSuccess(
|
| - blink::WebPassOwnPtr<blink::WebCredential> credential) override {
|
| + void onSuccess(blink::WebPassOwnPtr<blink::WebCredential>) override {
|
| test_->set_callback_succeeded(true);
|
| -
|
| - blink::WebCredential* ptr =
|
| - std::unique_ptr<blink::WebCredential>(credential).release();
|
| - test_->credential_.reset(static_cast<blink::WebPasswordCredential*>(ptr));
|
| }
|
|
|
| void onError(blink::WebCredentialManagerError reason) override {
|
| test_->set_callback_errored(true);
|
| - test_->credential_.reset();
|
| - test_->error_ = reason;
|
| }
|
|
|
| private:
|
| CredentialManagerClientTest* test_;
|
| };
|
|
|
| -void RunAllPendingTasks() {
|
| - base::RunLoop run_loop;
|
| - base::MessageLoop::current()->PostTask(
|
| - FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
|
| - run_loop.Run();
|
| -}
|
| -
|
| } // namespace
|
|
|
| TEST_F(CredentialManagerClientTest, SendStore) {
|
| - credential_.reset(new blink::WebPasswordCredential("", "", "", GURL()));
|
| + int request_id;
|
| + EXPECT_FALSE(
|
| + ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id));
|
| +
|
| std::unique_ptr<TestNotificationCallbacks> callbacks(
|
| new TestNotificationCallbacks(this));
|
| - client_->dispatchStore(*credential_, callbacks.release());
|
| -
|
| - RunAllPendingTasks();
|
| -
|
| + client_->dispatchStore(*credential(), callbacks.release());
|
| +
|
| + EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id));
|
| +
|
| + client_->OnAcknowledgeStore(request_id);
|
| EXPECT_TRUE(callback_succeeded());
|
| EXPECT_FALSE(callback_errored());
|
| }
|
|
|
| TEST_F(CredentialManagerClientTest, SendRequestUserMediation) {
|
| + int request_id;
|
| + EXPECT_FALSE(ExtractRequestId(
|
| + CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
|
| +
|
| std::unique_ptr<TestNotificationCallbacks> callbacks(
|
| new TestNotificationCallbacks(this));
|
| client_->dispatchRequireUserMediation(callbacks.release());
|
|
|
| - RunAllPendingTasks();
|
| -
|
| + EXPECT_TRUE(ExtractRequestId(
|
| + CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
|
| +
|
| + client_->OnAcknowledgeRequireUserMediation(request_id);
|
| EXPECT_TRUE(callback_succeeded());
|
| EXPECT_FALSE(callback_errored());
|
| }
|
|
|
| TEST_F(CredentialManagerClientTest, SendRequestCredential) {
|
| + int request_id;
|
| + EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
|
| + request_id));
|
| +
|
| std::unique_ptr<TestRequestCallbacks> callbacks(
|
| new TestRequestCallbacks(this));
|
| std::vector<GURL> federations;
|
| - federations.push_back(GURL(kTestCredentialPassword));
|
| client_->dispatchGet(false, true, federations, callbacks.release());
|
|
|
| - RunAllPendingTasks();
|
| -
|
| - EXPECT_TRUE(callback_succeeded());
|
| - EXPECT_FALSE(callback_errored());
|
| - EXPECT_TRUE(credential_);
|
| - EXPECT_EQ("password", credential_->type());
|
| + EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
|
| + request_id));
|
| +
|
| + CredentialInfo info;
|
| + info.type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
|
| + client_->OnSendCredential(request_id, info);
|
| + EXPECT_TRUE(callback_succeeded());
|
| + EXPECT_FALSE(callback_errored());
|
| }
|
|
|
| TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) {
|
| + int request_id;
|
| + EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
|
| + request_id));
|
| +
|
| std::unique_ptr<TestRequestCallbacks> callbacks(
|
| new TestRequestCallbacks(this));
|
| std::vector<GURL> federations;
|
| - federations.push_back(GURL(kTestCredentialEmpty));
|
| client_->dispatchGet(false, true, federations, callbacks.release());
|
|
|
| - RunAllPendingTasks();
|
| -
|
| - EXPECT_TRUE(callback_succeeded());
|
| - EXPECT_FALSE(callback_errored());
|
| - EXPECT_FALSE(credential_);
|
| -}
|
| -
|
| -TEST_F(CredentialManagerClientTest, SendRequestCredentialReject) {
|
| - std::unique_ptr<TestRequestCallbacks> callbacks(
|
| - new TestRequestCallbacks(this));
|
| - std::vector<GURL> federations;
|
| - federations.push_back(GURL(kTestCredentialReject));
|
| - client_->dispatchGet(false, true, federations, callbacks.release());
|
| -
|
| - RunAllPendingTasks();
|
| -
|
| - EXPECT_FALSE(callback_succeeded());
|
| - EXPECT_TRUE(callback_errored());
|
| - EXPECT_FALSE(credential_);
|
| - EXPECT_EQ(blink::WebCredentialManagerError::
|
| - WebCredentialManagerPasswordStoreUnavailableError,
|
| - error_);
|
| + EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
|
| + request_id));
|
| +
|
| + CredentialInfo info; // Send an empty credential in response.
|
| + client_->OnSendCredential(request_id, info);
|
| + EXPECT_TRUE(callback_succeeded());
|
| + EXPECT_FALSE(callback_errored());
|
| }
|
|
|
| } // namespace password_manager
|
|
|