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