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" | |
10 #include "components/password_manager/content/renderer/credential_manager_client .h" | 9 #include "components/password_manager/content/renderer/credential_manager_client .h" |
10 #include "content/common/mojo/service_registry_impl.h" | |
11 #include "content/public/renderer/render_frame.h" | |
12 #include "content/public/renderer/render_view.h" | |
11 #include "content/public/test/render_view_test.h" | 13 #include "content/public/test/render_view_test.h" |
12 #include "ipc/ipc_test_sink.h" | 14 #include "mojo/public/cpp/bindings/binding_set.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/WebKit/public/platform/WebCredential.h" | 16 #include "third_party/WebKit/public/platform/WebCredential.h" |
15 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h" | 17 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h" |
16 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" | 18 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h" |
17 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" | 19 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" |
18 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" | 20 #include "third_party/WebKit/public/platform/WebPasswordCredential.h" |
19 | 21 |
22 using content::ServiceRegistry; | |
23 using content::ServiceRegistryImpl; | |
24 | |
20 namespace password_manager { | 25 namespace password_manager { |
21 | 26 |
22 namespace { | 27 namespace { |
23 | 28 |
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 | |
24 class CredentialManagerClientTest : public content::RenderViewTest { | 76 class CredentialManagerClientTest : public content::RenderViewTest { |
25 public: | 77 public: |
26 CredentialManagerClientTest() | 78 CredentialManagerClientTest() |
27 : callback_errored_(false), callback_succeeded_(false) {} | 79 : callback_errored_(false), callback_succeeded_(false) {} |
28 ~CredentialManagerClientTest() override {} | 80 ~CredentialManagerClientTest() override {} |
29 | 81 |
30 void SetUp() override { | 82 void SetUp() override { |
31 content::RenderViewTest::SetUp(); | 83 content::RenderViewTest::SetUp(); |
32 credential_.reset(new blink::WebPasswordCredential("", "", "", GURL())); | |
33 client_.reset(new CredentialManagerClient(view_)); | 84 client_.reset(new CredentialManagerClient(view_)); |
85 | |
86 ServiceRegistryImpl* registry = static_cast<ServiceRegistryImpl*>( | |
87 view_->GetMainRenderFrame()->GetServiceRegistry()); | |
88 registry->AddServiceOverrideForTesting( | |
89 mojom::CredentialManager::Name_, | |
90 base::Bind(&CredentialManagerClientTest::BindCredentialManager, | |
91 base::Unretained(this))); | |
34 } | 92 } |
35 | 93 |
36 void TearDown() override { | 94 void TearDown() override { |
37 credential_.reset(); | 95 credential_.reset(); |
38 client_.reset(); | 96 client_.reset(); |
39 content::RenderViewTest::TearDown(); | 97 content::RenderViewTest::TearDown(); |
40 } | 98 } |
41 | 99 |
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 | |
88 bool callback_errored() const { return callback_errored_; } | 100 bool callback_errored() const { return callback_errored_; } |
89 void set_callback_errored(bool state) { callback_errored_ = state; } | 101 void set_callback_errored(bool state) { callback_errored_ = state; } |
90 bool callback_succeeded() const { return callback_succeeded_; } | 102 bool callback_succeeded() const { return callback_succeeded_; } |
91 void set_callback_succeeded(bool state) { callback_succeeded_ = state; } | 103 void set_callback_succeeded(bool state) { callback_succeeded_ = state; } |
92 | 104 |
105 void BindCredentialManager(mojo::ScopedMessagePipeHandle handle) { | |
106 fake_cm_.BindRequest( | |
107 mojo::MakeRequest<mojom::CredentialManager>(std::move(handle))); | |
108 } | |
109 | |
110 scoped_ptr<blink::WebPasswordCredential> credential_; | |
111 blink::WebCredentialManagerError error_; | |
112 | |
93 protected: | 113 protected: |
94 scoped_ptr<CredentialManagerClient> client_; | 114 scoped_ptr<CredentialManagerClient> client_; |
95 | 115 |
116 FakeCredentialManager fake_cm_; | |
117 | |
96 // True if a message's callback's 'onSuccess'/'onError' methods were called, | 118 // True if a message's callback's 'onSuccess'/'onError' methods were called, |
97 // false otherwise. We put these on the test object rather than on the | 119 // false otherwise. We put these on the test object rather than on the |
98 // Test*Callbacks objects because ownership of those objects passes into the | 120 // Test*Callbacks objects because ownership of those objects passes into the |
99 // client, which destroys the callbacks after calling them to resolve the | 121 // client, which destroys the callbacks after calling them to resolve the |
100 // pending Blink-side Promise. | 122 // pending Blink-side Promise. |
101 bool callback_errored_; | 123 bool callback_errored_; |
102 bool callback_succeeded_; | 124 bool callback_succeeded_; |
103 | 125 |
104 scoped_ptr<blink::WebPasswordCredential> credential_; | |
105 }; | 126 }; |
106 | 127 |
107 class TestNotificationCallbacks | 128 class TestNotificationCallbacks |
108 : public blink::WebCredentialManagerClient::NotificationCallbacks { | 129 : public blink::WebCredentialManagerClient::NotificationCallbacks { |
109 public: | 130 public: |
110 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) | 131 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) |
111 : test_(test) {} | 132 : test_(test) {} |
112 | 133 |
113 ~TestNotificationCallbacks() override {} | 134 ~TestNotificationCallbacks() override {} |
114 | 135 |
115 void onSuccess() override { test_->set_callback_succeeded(true); } | 136 void onSuccess() override { test_->set_callback_succeeded(true); } |
116 | 137 |
117 void onError(blink::WebCredentialManagerError reason) override { | 138 void onError(blink::WebCredentialManagerError reason) override { |
118 test_->set_callback_errored(true); | 139 test_->set_callback_errored(true); |
119 } | 140 } |
120 | 141 |
121 private: | 142 private: |
122 CredentialManagerClientTest* test_; | 143 CredentialManagerClientTest* test_; |
123 }; | 144 }; |
124 | 145 |
125 class TestRequestCallbacks | 146 class TestRequestCallbacks |
126 : public blink::WebCredentialManagerClient::RequestCallbacks { | 147 : public blink::WebCredentialManagerClient::RequestCallbacks { |
127 public: | 148 public: |
128 explicit TestRequestCallbacks(CredentialManagerClientTest* test) | 149 explicit TestRequestCallbacks(CredentialManagerClientTest* test) |
129 : test_(test) {} | 150 : test_(test) {} |
130 | 151 |
131 ~TestRequestCallbacks() override {} | 152 ~TestRequestCallbacks() override {} |
132 | 153 |
133 void onSuccess(blink::WebPassOwnPtr<blink::WebCredential>) override { | 154 void onSuccess( |
155 blink::WebPassOwnPtr<blink::WebCredential> credential) override { | |
134 test_->set_callback_succeeded(true); | 156 test_->set_callback_succeeded(true); |
157 | |
158 blink::WebCredential* ptr = | |
159 scoped_ptr<blink::WebCredential>(credential).release(); | |
160 test_->credential_.reset(static_cast<blink::WebPasswordCredential*>(ptr)); | |
135 } | 161 } |
136 | 162 |
137 void onError(blink::WebCredentialManagerError reason) override { | 163 void onError(blink::WebCredentialManagerError reason) override { |
138 test_->set_callback_errored(true); | 164 test_->set_callback_errored(true); |
165 test_->credential_.reset(); | |
166 test_->error_ = reason; | |
139 } | 167 } |
140 | 168 |
141 private: | 169 private: |
142 CredentialManagerClientTest* test_; | 170 CredentialManagerClientTest* test_; |
143 }; | 171 }; |
144 | 172 |
173 void RunAllPendingTasks() { | |
174 base::RunLoop run_loop; | |
175 base::MessageLoop::current()->PostTask( | |
176 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
177 run_loop.Run(); | |
178 } | |
179 | |
145 } // namespace | 180 } // namespace |
146 | 181 |
147 TEST_F(CredentialManagerClientTest, SendStore) { | 182 TEST_F(CredentialManagerClientTest, SendStore) { |
148 int request_id; | 183 credential_.reset(new blink::WebPasswordCredential("", "", "", GURL())); |
149 EXPECT_FALSE( | |
150 ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id)); | |
151 | |
152 scoped_ptr<TestNotificationCallbacks> callbacks( | 184 scoped_ptr<TestNotificationCallbacks> callbacks( |
153 new TestNotificationCallbacks(this)); | 185 new TestNotificationCallbacks(this)); |
154 client_->dispatchStore(*credential(), callbacks.release()); | 186 client_->dispatchStore(*(credential_.get()), callbacks.release()); |
vabr (Chromium)
2016/03/14 15:40:26
nit: You should not need the .get(), scoped_ptr ha
leonhsl(Using Gerrit)
2016/03/16 07:07:28
Done.
| |
155 | 187 |
156 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id)); | 188 RunAllPendingTasks(); |
157 | 189 |
158 client_->OnAcknowledgeStore(request_id); | |
159 EXPECT_TRUE(callback_succeeded()); | 190 EXPECT_TRUE(callback_succeeded()); |
160 EXPECT_FALSE(callback_errored()); | 191 EXPECT_FALSE(callback_errored()); |
161 } | 192 } |
162 | 193 |
163 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) { | 194 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) { |
164 int request_id; | |
165 EXPECT_FALSE(ExtractRequestId( | |
166 CredentialManagerHostMsg_RequireUserMediation::ID, request_id)); | |
167 | |
168 scoped_ptr<TestNotificationCallbacks> callbacks( | 195 scoped_ptr<TestNotificationCallbacks> callbacks( |
169 new TestNotificationCallbacks(this)); | 196 new TestNotificationCallbacks(this)); |
170 client_->dispatchRequireUserMediation(callbacks.release()); | 197 client_->dispatchRequireUserMediation(callbacks.release()); |
171 | 198 |
172 EXPECT_TRUE(ExtractRequestId( | 199 RunAllPendingTasks(); |
173 CredentialManagerHostMsg_RequireUserMediation::ID, request_id)); | |
174 | 200 |
175 client_->OnAcknowledgeRequireUserMediation(request_id); | |
176 EXPECT_TRUE(callback_succeeded()); | 201 EXPECT_TRUE(callback_succeeded()); |
177 EXPECT_FALSE(callback_errored()); | 202 EXPECT_FALSE(callback_errored()); |
178 } | 203 } |
179 | 204 |
180 TEST_F(CredentialManagerClientTest, SendRequestCredential) { | 205 TEST_F(CredentialManagerClientTest, SendRequestCredential) { |
181 int request_id; | |
182 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | |
183 request_id)); | |
184 | |
185 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this)); | 206 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this)); |
186 std::vector<GURL> federations; | 207 std::vector<GURL> federations; |
208 federations.push_back(GURL(kTestCredentialPassword)); | |
187 client_->dispatchGet(false, true, federations, callbacks.release()); | 209 client_->dispatchGet(false, true, federations, callbacks.release()); |
188 | 210 |
189 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | 211 RunAllPendingTasks(); |
190 request_id)); | |
191 | 212 |
192 CredentialInfo info; | |
193 info.type = CredentialType::CREDENTIAL_TYPE_PASSWORD; | |
194 client_->OnSendCredential(request_id, info); | |
195 EXPECT_TRUE(callback_succeeded()); | 213 EXPECT_TRUE(callback_succeeded()); |
196 EXPECT_FALSE(callback_errored()); | 214 EXPECT_FALSE(callback_errored()); |
215 EXPECT_TRUE(credential_.get()); | |
vabr (Chromium)
2016/03/14 15:40:26
nit: No need for .get(), scoped_ptr is implicitly
leonhsl(Using Gerrit)
2016/03/16 07:07:28
Done and solved all codes like this. Thanks!
| |
216 EXPECT_EQ("password", credential_->type()); | |
197 } | 217 } |
198 | 218 |
199 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) { | 219 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) { |
200 int request_id; | |
201 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | |
202 request_id)); | |
203 | |
204 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this)); | 220 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this)); |
205 std::vector<GURL> federations; | 221 std::vector<GURL> federations; |
222 federations.push_back(GURL(kTestCredentialEmpty)); | |
206 client_->dispatchGet(false, true, federations, callbacks.release()); | 223 client_->dispatchGet(false, true, federations, callbacks.release()); |
207 | 224 |
208 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | 225 RunAllPendingTasks(); |
209 request_id)); | |
210 | 226 |
211 CredentialInfo info; // Send an empty credential in response. | |
212 client_->OnSendCredential(request_id, info); | |
213 EXPECT_TRUE(callback_succeeded()); | 227 EXPECT_TRUE(callback_succeeded()); |
214 EXPECT_FALSE(callback_errored()); | 228 EXPECT_FALSE(callback_errored()); |
229 EXPECT_FALSE(credential_.get()); | |
230 } | |
231 | |
232 TEST_F(CredentialManagerClientTest, SendRequestCredentialReject) { | |
233 scoped_ptr<TestRequestCallbacks> callbacks(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_.get()); | |
243 EXPECT_TRUE(error_ == blink::WebCredentialManagerError:: | |
vabr (Chromium)
2016/03/14 15:40:26
EXPECT_EQ(blink::WebCredentialManagerError::WebCre
leonhsl(Using Gerrit)
2016/03/16 07:07:28
Done.
| |
244 WebCredentialManagerPasswordStoreUnavailableError); | |
215 } | 245 } |
216 | 246 |
217 } // namespace password_manager | 247 } // namespace password_manager |
OLD | NEW |