OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/password_manager/content/renderer/credential_manager_client
.h" | 5 #include "components/password_manager/content/renderer/credential_manager_client
.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <tuple> | 10 #include <tuple> |
11 | 11 |
12 #include "content/public/common/service_registry.h" | 12 #include "components/password_manager/content/common/credential_manager_messages
.h" |
13 #include "content/public/renderer/render_frame.h" | |
14 #include "content/public/renderer/render_view.h" | |
15 #include "content/public/test/render_view_test.h" | 13 #include "content/public/test/render_view_test.h" |
16 #include "mojo/public/cpp/bindings/binding_set.h" | 14 #include "ipc/ipc_test_sink.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/WebKit/public/platform/WebCredential.h" | 16 #include "third_party/WebKit/public/platform/WebCredential.h" |
19 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h" | 17 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h" |
20 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" | 18 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" |
21 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" | 19 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" |
22 | 20 |
23 using content::ServiceRegistry; | |
24 | |
25 namespace password_manager { | 21 namespace password_manager { |
26 | 22 |
27 namespace { | 23 namespace { |
28 | 24 |
29 const char kTestCredentialPassword[] = "https://password.com/"; | |
30 const char kTestCredentialEmpty[] = "https://empty.com/"; | |
31 const char kTestCredentialReject[] = "https://reject.com/"; | |
32 | |
33 class FakeCredentialManager : public mojom::CredentialManager { | |
34 public: | |
35 FakeCredentialManager() {} | |
36 ~FakeCredentialManager() override {} | |
37 | |
38 void BindRequest(mojom::CredentialManagerRequest request) { | |
39 bindings_.AddBinding(this, std::move(request)); | |
40 } | |
41 | |
42 private: | |
43 // mojom::CredentialManager methods: | |
44 void Store(mojom::CredentialInfoPtr credential, | |
45 const StoreCallback& callback) override { | |
46 callback.Run(); | |
47 } | |
48 | |
49 void RequireUserMediation( | |
50 const RequireUserMediationCallback& callback) override { | |
51 callback.Run(); | |
52 } | |
53 | |
54 void Get(bool zero_click_only, | |
55 bool include_passwords, | |
56 mojo::Array<mojo::String> federations, | |
57 const GetCallback& callback) override { | |
58 mojo::String& url = federations[0]; | |
59 | |
60 if (url == kTestCredentialPassword) { | |
61 mojom::CredentialInfoPtr info = mojom::CredentialInfo::New(); | |
62 info->type = mojom::CredentialType::PASSWORD; | |
63 callback.Run(mojom::CredentialManagerError::SUCCESS, std::move(info)); | |
64 } else if (url == kTestCredentialEmpty) { | |
65 callback.Run(mojom::CredentialManagerError::SUCCESS, | |
66 mojom::CredentialInfo::New()); | |
67 } else if (url == kTestCredentialReject) { | |
68 callback.Run(mojom::CredentialManagerError::PASSWORDSTOREUNAVAILABLE, | |
69 nullptr); | |
70 } | |
71 } | |
72 | |
73 mojo::BindingSet<mojom::CredentialManager> bindings_; | |
74 }; | |
75 | |
76 class CredentialManagerClientTest : public content::RenderViewTest { | 25 class CredentialManagerClientTest : public content::RenderViewTest { |
77 public: | 26 public: |
78 CredentialManagerClientTest() | 27 CredentialManagerClientTest() |
79 : callback_errored_(false), callback_succeeded_(false) {} | 28 : callback_errored_(false), callback_succeeded_(false) {} |
80 ~CredentialManagerClientTest() override {} | 29 ~CredentialManagerClientTest() override {} |
81 | 30 |
82 void SetUp() override { | 31 void SetUp() override { |
83 content::RenderViewTest::SetUp(); | 32 content::RenderViewTest::SetUp(); |
| 33 credential_.reset(new blink::WebPasswordCredential("", "", "", GURL())); |
84 client_.reset(new CredentialManagerClient(view_)); | 34 client_.reset(new CredentialManagerClient(view_)); |
85 | |
86 ServiceRegistry* registry = | |
87 view_->GetMainRenderFrame()->GetServiceRegistry(); | |
88 registry->AddServiceOverrideForTesting( | |
89 mojom::CredentialManager::Name_, | |
90 base::Bind(&CredentialManagerClientTest::BindCredentialManager, | |
91 base::Unretained(this))); | |
92 } | 35 } |
93 | 36 |
94 void TearDown() override { | 37 void TearDown() override { |
95 credential_.reset(); | 38 credential_.reset(); |
96 client_.reset(); | 39 client_.reset(); |
97 content::RenderViewTest::TearDown(); | 40 content::RenderViewTest::TearDown(); |
98 } | 41 } |
99 | 42 |
| 43 IPC::TestSink& sink() { return render_thread_->sink(); } |
| 44 |
| 45 blink::WebCredential* credential() { return credential_.get(); } |
| 46 |
| 47 // The browser's response to any of the messages the client sends must contain |
| 48 // a request ID so that the client knows which request is being serviced. This |
| 49 // method grabs the ID from an outgoing |message_id| message, and sets the |
| 50 // |request_id| param to its value. If no request ID can be found, the method |
| 51 // returns false, and the |request_id| is set to -1. |
| 52 // |
| 53 // Clears any pending messages upon return. |
| 54 bool ExtractRequestId(uint32_t message_id, int& request_id) { |
| 55 request_id = -1; |
| 56 const IPC::Message* message = sink().GetFirstMessageMatching(message_id); |
| 57 if (!message) |
| 58 return false; |
| 59 |
| 60 switch (message_id) { |
| 61 case CredentialManagerHostMsg_Store::ID: { |
| 62 std::tuple<int, CredentialInfo> param; |
| 63 CredentialManagerHostMsg_Store::Read(message, ¶m); |
| 64 request_id = std::get<0>(param); |
| 65 break; |
| 66 } |
| 67 |
| 68 case CredentialManagerHostMsg_RequireUserMediation::ID: { |
| 69 std::tuple<int> param; |
| 70 CredentialManagerHostMsg_RequireUserMediation::Read(message, ¶m); |
| 71 request_id = std::get<0>(param); |
| 72 break; |
| 73 } |
| 74 |
| 75 case CredentialManagerHostMsg_RequestCredential::ID: { |
| 76 std::tuple<int, bool, bool, std::vector<GURL>> param; |
| 77 CredentialManagerHostMsg_RequestCredential::Read(message, ¶m); |
| 78 request_id = std::get<0>(param); |
| 79 break; |
| 80 } |
| 81 |
| 82 default: |
| 83 break; |
| 84 } |
| 85 sink().ClearMessages(); |
| 86 return request_id != -1; |
| 87 } |
| 88 |
100 bool callback_errored() const { return callback_errored_; } | 89 bool callback_errored() const { return callback_errored_; } |
101 void set_callback_errored(bool state) { callback_errored_ = state; } | 90 void set_callback_errored(bool state) { callback_errored_ = state; } |
102 bool callback_succeeded() const { return callback_succeeded_; } | 91 bool callback_succeeded() const { return callback_succeeded_; } |
103 void set_callback_succeeded(bool state) { callback_succeeded_ = state; } | 92 void set_callback_succeeded(bool state) { callback_succeeded_ = state; } |
104 | 93 |
105 void BindCredentialManager(mojo::ScopedMessagePipeHandle handle) { | |
106 fake_cm_.BindRequest( | |
107 mojo::MakeRequest<mojom::CredentialManager>(std::move(handle))); | |
108 } | |
109 | |
110 std::unique_ptr<blink::WebPasswordCredential> credential_; | |
111 blink::WebCredentialManagerError error_; | |
112 | |
113 protected: | 94 protected: |
114 std::unique_ptr<CredentialManagerClient> client_; | 95 std::unique_ptr<CredentialManagerClient> client_; |
115 | 96 |
116 FakeCredentialManager fake_cm_; | |
117 | |
118 // True if a message's callback's 'onSuccess'/'onError' methods were called, | 97 // True if a message's callback's 'onSuccess'/'onError' methods were called, |
119 // false otherwise. We put these on the test object rather than on the | 98 // false otherwise. We put these on the test object rather than on the |
120 // Test*Callbacks objects because ownership of those objects passes into the | 99 // Test*Callbacks objects because ownership of those objects passes into the |
121 // client, which destroys the callbacks after calling them to resolve the | 100 // client, which destroys the callbacks after calling them to resolve the |
122 // pending Blink-side Promise. | 101 // pending Blink-side Promise. |
123 bool callback_errored_; | 102 bool callback_errored_; |
124 bool callback_succeeded_; | 103 bool callback_succeeded_; |
| 104 |
| 105 std::unique_ptr<blink::WebPasswordCredential> credential_; |
125 }; | 106 }; |
126 | 107 |
127 class TestNotificationCallbacks | 108 class TestNotificationCallbacks |
128 : public blink::WebCredentialManagerClient::NotificationCallbacks { | 109 : public blink::WebCredentialManagerClient::NotificationCallbacks { |
129 public: | 110 public: |
130 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) | 111 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) |
131 : test_(test) {} | 112 : test_(test) {} |
132 | 113 |
133 ~TestNotificationCallbacks() override {} | 114 ~TestNotificationCallbacks() override {} |
134 | 115 |
135 void onSuccess() override { test_->set_callback_succeeded(true); } | 116 void onSuccess() override { test_->set_callback_succeeded(true); } |
136 | 117 |
137 void onError(blink::WebCredentialManagerError reason) override { | 118 void onError(blink::WebCredentialManagerError reason) override { |
138 test_->set_callback_errored(true); | 119 test_->set_callback_errored(true); |
139 } | 120 } |
140 | 121 |
141 private: | 122 private: |
142 CredentialManagerClientTest* test_; | 123 CredentialManagerClientTest* test_; |
143 }; | 124 }; |
144 | 125 |
145 class TestRequestCallbacks | 126 class TestRequestCallbacks |
146 : public blink::WebCredentialManagerClient::RequestCallbacks { | 127 : public blink::WebCredentialManagerClient::RequestCallbacks { |
147 public: | 128 public: |
148 explicit TestRequestCallbacks(CredentialManagerClientTest* test) | 129 explicit TestRequestCallbacks(CredentialManagerClientTest* test) |
149 : test_(test) {} | 130 : test_(test) {} |
150 | 131 |
151 ~TestRequestCallbacks() override {} | 132 ~TestRequestCallbacks() override {} |
152 | 133 |
153 void onSuccess(std::unique_ptr<blink::WebCredential> credential) override { | 134 void onSuccess(std::unique_ptr<blink::WebCredential>) override { |
154 test_->set_callback_succeeded(true); | 135 test_->set_callback_succeeded(true); |
155 | |
156 blink::WebCredential* ptr = credential.release(); | |
157 test_->credential_.reset(static_cast<blink::WebPasswordCredential*>(ptr)); | |
158 } | 136 } |
159 | 137 |
160 void onError(blink::WebCredentialManagerError reason) override { | 138 void onError(blink::WebCredentialManagerError reason) override { |
161 test_->set_callback_errored(true); | 139 test_->set_callback_errored(true); |
162 test_->credential_.reset(); | |
163 test_->error_ = reason; | |
164 } | 140 } |
165 | 141 |
166 private: | 142 private: |
167 CredentialManagerClientTest* test_; | 143 CredentialManagerClientTest* test_; |
168 }; | 144 }; |
169 | 145 |
170 void RunAllPendingTasks() { | |
171 base::RunLoop run_loop; | |
172 base::MessageLoop::current()->PostTask( | |
173 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
174 run_loop.Run(); | |
175 } | |
176 | |
177 } // namespace | 146 } // namespace |
178 | 147 |
179 TEST_F(CredentialManagerClientTest, SendStore) { | 148 TEST_F(CredentialManagerClientTest, SendStore) { |
180 credential_.reset(new blink::WebPasswordCredential("", "", "", GURL())); | 149 int request_id; |
| 150 EXPECT_FALSE( |
| 151 ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id)); |
| 152 |
181 std::unique_ptr<TestNotificationCallbacks> callbacks( | 153 std::unique_ptr<TestNotificationCallbacks> callbacks( |
182 new TestNotificationCallbacks(this)); | 154 new TestNotificationCallbacks(this)); |
183 client_->dispatchStore(*credential_, callbacks.release()); | 155 client_->dispatchStore(*credential(), callbacks.release()); |
184 | 156 |
185 RunAllPendingTasks(); | 157 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id)); |
186 | 158 |
| 159 client_->OnAcknowledgeStore(request_id); |
187 EXPECT_TRUE(callback_succeeded()); | 160 EXPECT_TRUE(callback_succeeded()); |
188 EXPECT_FALSE(callback_errored()); | 161 EXPECT_FALSE(callback_errored()); |
189 } | 162 } |
190 | 163 |
191 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) { | 164 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) { |
| 165 int request_id; |
| 166 EXPECT_FALSE(ExtractRequestId( |
| 167 CredentialManagerHostMsg_RequireUserMediation::ID, request_id)); |
| 168 |
192 std::unique_ptr<TestNotificationCallbacks> callbacks( | 169 std::unique_ptr<TestNotificationCallbacks> callbacks( |
193 new TestNotificationCallbacks(this)); | 170 new TestNotificationCallbacks(this)); |
194 client_->dispatchRequireUserMediation(callbacks.release()); | 171 client_->dispatchRequireUserMediation(callbacks.release()); |
195 | 172 |
196 RunAllPendingTasks(); | 173 EXPECT_TRUE(ExtractRequestId( |
| 174 CredentialManagerHostMsg_RequireUserMediation::ID, request_id)); |
197 | 175 |
| 176 client_->OnAcknowledgeRequireUserMediation(request_id); |
198 EXPECT_TRUE(callback_succeeded()); | 177 EXPECT_TRUE(callback_succeeded()); |
199 EXPECT_FALSE(callback_errored()); | 178 EXPECT_FALSE(callback_errored()); |
200 } | 179 } |
201 | 180 |
202 TEST_F(CredentialManagerClientTest, SendRequestCredential) { | 181 TEST_F(CredentialManagerClientTest, SendRequestCredential) { |
| 182 int request_id; |
| 183 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, |
| 184 request_id)); |
| 185 |
203 std::unique_ptr<TestRequestCallbacks> callbacks( | 186 std::unique_ptr<TestRequestCallbacks> callbacks( |
204 new TestRequestCallbacks(this)); | 187 new TestRequestCallbacks(this)); |
205 std::vector<GURL> federations; | 188 std::vector<GURL> federations; |
206 federations.push_back(GURL(kTestCredentialPassword)); | |
207 client_->dispatchGet(false, true, federations, callbacks.release()); | 189 client_->dispatchGet(false, true, federations, callbacks.release()); |
208 | 190 |
209 RunAllPendingTasks(); | 191 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, |
| 192 request_id)); |
210 | 193 |
| 194 CredentialInfo info; |
| 195 info.type = CredentialType::CREDENTIAL_TYPE_PASSWORD; |
| 196 client_->OnSendCredential(request_id, info); |
211 EXPECT_TRUE(callback_succeeded()); | 197 EXPECT_TRUE(callback_succeeded()); |
212 EXPECT_FALSE(callback_errored()); | 198 EXPECT_FALSE(callback_errored()); |
213 EXPECT_TRUE(credential_); | |
214 EXPECT_EQ("password", credential_->type()); | |
215 } | 199 } |
216 | 200 |
217 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) { | 201 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) { |
| 202 int request_id; |
| 203 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, |
| 204 request_id)); |
| 205 |
218 std::unique_ptr<TestRequestCallbacks> callbacks( | 206 std::unique_ptr<TestRequestCallbacks> callbacks( |
219 new TestRequestCallbacks(this)); | 207 new TestRequestCallbacks(this)); |
220 std::vector<GURL> federations; | 208 std::vector<GURL> federations; |
221 federations.push_back(GURL(kTestCredentialEmpty)); | |
222 client_->dispatchGet(false, true, federations, callbacks.release()); | 209 client_->dispatchGet(false, true, federations, callbacks.release()); |
223 | 210 |
224 RunAllPendingTasks(); | 211 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, |
| 212 request_id)); |
225 | 213 |
| 214 CredentialInfo info; // Send an empty credential in response. |
| 215 client_->OnSendCredential(request_id, info); |
226 EXPECT_TRUE(callback_succeeded()); | 216 EXPECT_TRUE(callback_succeeded()); |
227 EXPECT_FALSE(callback_errored()); | 217 EXPECT_FALSE(callback_errored()); |
228 EXPECT_FALSE(credential_); | |
229 } | |
230 | |
231 TEST_F(CredentialManagerClientTest, SendRequestCredentialReject) { | |
232 std::unique_ptr<TestRequestCallbacks> callbacks( | |
233 new TestRequestCallbacks(this)); | |
234 std::vector<GURL> federations; | |
235 federations.push_back(GURL(kTestCredentialReject)); | |
236 client_->dispatchGet(false, true, federations, callbacks.release()); | |
237 | |
238 RunAllPendingTasks(); | |
239 | |
240 EXPECT_FALSE(callback_succeeded()); | |
241 EXPECT_TRUE(callback_errored()); | |
242 EXPECT_FALSE(credential_); | |
243 EXPECT_EQ(blink::WebCredentialManagerError:: | |
244 WebCredentialManagerPasswordStoreUnavailableError, | |
245 error_); | |
246 } | 218 } |
247 | 219 |
248 } // namespace password_manager | 220 } // namespace password_manager |
OLD | NEW |