1 // Copyright 2014 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.
5 #include "components/password_manager/content/common/credential_manager_messages.h"
6 #include "components/password_manager/content/renderer/credential_manager_client.h"
7 #include "content/public/test/render_view_test.h"
8 #include "ipc/ipc_test_sink.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/platform/WebCredential.h"
11 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h"
12 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
13 #include "third_party/WebKit/public/platform/WebPasswordCredential.h"
15 namespace password_manager
{
19 class CredentialManagerClientTest
: public content::RenderViewTest
{
21 CredentialManagerClientTest()
22 : callback_errored_(false), callback_succeeded_(false) {}
23 ~CredentialManagerClientTest() override
{}
25 void SetUp() override
{
26 content::RenderViewTest::SetUp();
27 credential_
.reset(new blink::WebPasswordCredential("", "", "", GURL()));
28 client_
.reset(new CredentialManagerClient(view_
));
31 void TearDown() override
{
34 content::RenderViewTest::TearDown();
37 IPC::TestSink
& sink() { return render_thread_
->sink(); }
39 blink::WebCredential
* credential() { return credential_
.get(); }
41 // The browser's response to any of the messages the client sends must contain
42 // a request ID so that the client knows which request is being serviced. This
43 // method grabs the ID from an outgoing |message_id| message, and sets the
44 // |request_id| param to its value. If no request ID can be found, the method
45 // returns false, and the |request_id| is set to -1.
47 // Clears any pending messages upon return.
48 bool ExtractRequestId(uint32 message_id
, int& request_id
) {
50 const IPC::Message
* message
= sink().GetFirstMessageMatching(message_id
);
55 case CredentialManagerHostMsg_Store::ID
: {
56 base::Tuple
<int, CredentialInfo
> param
;
57 CredentialManagerHostMsg_Store::Read(message
, ¶m
);
58 request_id
= base::get
<0>(param
);
62 case CredentialManagerHostMsg_RequireUserMediation::ID
: {
63 base::Tuple
<int> param
;
64 CredentialManagerHostMsg_RequireUserMediation::Read(message
, ¶m
);
65 request_id
= base::get
<0>(param
);
69 case CredentialManagerHostMsg_RequestCredential::ID
: {
70 base::Tuple
<int, bool, std::vector
<GURL
>> param
;
71 CredentialManagerHostMsg_RequestCredential::Read(message
, ¶m
);
72 request_id
= base::get
<0>(param
);
79 sink().ClearMessages();
80 return request_id
!= -1;
83 bool callback_errored() const { return callback_errored_
; }
84 void set_callback_errored(bool state
) { callback_errored_
= state
; }
85 bool callback_succeeded() const { return callback_succeeded_
; }
86 void set_callback_succeeded(bool state
) { callback_succeeded_
= state
; }
89 scoped_ptr
<CredentialManagerClient
> client_
;
91 // True if a message's callback's 'onSuccess'/'onError' methods were called,
92 // false otherwise. We put these on the test object rather than on the
93 // Test*Callbacks objects because ownership of those objects passes into the
94 // client, which destroys the callbacks after calling them to resolve the
95 // pending Blink-side Promise.
96 bool callback_errored_
;
97 bool callback_succeeded_
;
99 scoped_ptr
<blink::WebPasswordCredential
> credential_
;
102 class TestNotificationCallbacks
103 : public blink::WebCredentialManagerClient::NotificationCallbacks
{
105 explicit TestNotificationCallbacks(CredentialManagerClientTest
* test
)
108 virtual ~TestNotificationCallbacks() {}
110 virtual void onSuccess() { test_
->set_callback_succeeded(true); }
112 virtual void onError(blink::WebCredentialManagerError
* reason
) {
113 test_
->set_callback_errored(true);
117 CredentialManagerClientTest
* test_
;
120 class TestRequestCallbacks
121 : public blink::WebCredentialManagerClient::RequestCallbacks
{
123 explicit TestRequestCallbacks(CredentialManagerClientTest
* test
)
126 virtual ~TestRequestCallbacks() {}
128 virtual void onSuccess(blink::WebCredential
*) {
129 test_
->set_callback_succeeded(true);
132 virtual void onError(blink::WebCredentialManagerError
* reason
) {
133 test_
->set_callback_errored(true);
137 CredentialManagerClientTest
* test_
;
142 TEST_F(CredentialManagerClientTest
, SendStore
) {
145 ExtractRequestId(CredentialManagerHostMsg_Store::ID
, request_id
));
147 scoped_ptr
<TestNotificationCallbacks
> callbacks(
148 new TestNotificationCallbacks(this));
149 client_
->dispatchStore(*credential(), callbacks
.release());
151 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_Store::ID
, request_id
));
153 client_
->OnAcknowledgeStore(request_id
);
154 EXPECT_TRUE(callback_succeeded());
155 EXPECT_FALSE(callback_errored());
158 TEST_F(CredentialManagerClientTest
, SendRequestUserMediation
) {
160 EXPECT_FALSE(ExtractRequestId(
161 CredentialManagerHostMsg_RequireUserMediation::ID
, request_id
));
163 scoped_ptr
<TestNotificationCallbacks
> callbacks(
164 new TestNotificationCallbacks(this));
165 client_
->dispatchRequireUserMediation(callbacks
.release());
167 EXPECT_TRUE(ExtractRequestId(
168 CredentialManagerHostMsg_RequireUserMediation::ID
, request_id
));
170 client_
->OnAcknowledgeRequireUserMediation(request_id
);
171 EXPECT_TRUE(callback_succeeded());
172 EXPECT_FALSE(callback_errored());
175 TEST_F(CredentialManagerClientTest
, SendRequestCredential
) {
177 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID
,
180 scoped_ptr
<TestRequestCallbacks
> callbacks(new TestRequestCallbacks(this));
181 std::vector
<GURL
> federations
;
182 client_
->dispatchGet(false, federations
, callbacks
.release());
184 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID
,
188 info
.type
= CredentialType::CREDENTIAL_TYPE_PASSWORD
;
189 client_
->OnSendCredential(request_id
, info
);
190 EXPECT_TRUE(callback_succeeded());
191 EXPECT_FALSE(callback_errored());
194 TEST_F(CredentialManagerClientTest
, SendRequestCredentialEmpty
) {
196 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID
,
199 scoped_ptr
<TestRequestCallbacks
> callbacks(new TestRequestCallbacks(this));
200 std::vector
<GURL
> federations
;
201 client_
->dispatchGet(false, federations
, callbacks
.release());
203 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID
,
206 CredentialInfo info
; // Send an empty credential in response.
207 client_
->OnSendCredential(request_id
, info
);
208 EXPECT_TRUE(callback_succeeded());
209 EXPECT_FALSE(callback_errored());
212 } // namespace password_manager