Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: components/password_manager/content/renderer/credential_manager_client_browsertest.cc

Issue 1762603002: Switch components/password_manager code from IPC messages to Mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase only Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/public/common/service_registry.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
20 namespace password_manager { 24 namespace password_manager {
21 25
22 namespace { 26 namespace {
23 27
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
24 class CredentialManagerClientTest : public content::RenderViewTest { 75 class CredentialManagerClientTest : public content::RenderViewTest {
25 public: 76 public:
26 CredentialManagerClientTest() 77 CredentialManagerClientTest()
27 : callback_errored_(false), callback_succeeded_(false) {} 78 : callback_errored_(false), callback_succeeded_(false) {}
28 ~CredentialManagerClientTest() override {} 79 ~CredentialManagerClientTest() override {}
29 80
30 void SetUp() override { 81 void SetUp() override {
31 content::RenderViewTest::SetUp(); 82 content::RenderViewTest::SetUp();
32 credential_.reset(new blink::WebPasswordCredential("", "", "", GURL()));
33 client_.reset(new CredentialManagerClient(view_)); 83 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)));
34 } 91 }
35 92
36 void TearDown() override { 93 void TearDown() override {
37 credential_.reset(); 94 credential_.reset();
38 client_.reset(); 95 client_.reset();
39 content::RenderViewTest::TearDown(); 96 content::RenderViewTest::TearDown();
40 } 97 }
41 98
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, &param);
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, &param);
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, &param);
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_; } 99 bool callback_errored() const { return callback_errored_; }
89 void set_callback_errored(bool state) { callback_errored_ = state; } 100 void set_callback_errored(bool state) { callback_errored_ = state; }
90 bool callback_succeeded() const { return callback_succeeded_; } 101 bool callback_succeeded() const { return callback_succeeded_; }
91 void set_callback_succeeded(bool state) { callback_succeeded_ = state; } 102 void set_callback_succeeded(bool state) { callback_succeeded_ = state; }
92 103
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
93 protected: 112 protected:
94 std::unique_ptr<CredentialManagerClient> client_; 113 std::unique_ptr<CredentialManagerClient> client_;
95 114
115 FakeCredentialManager fake_cm_;
116
96 // True if a message's callback's 'onSuccess'/'onError' methods were called, 117 // 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 118 // 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 119 // Test*Callbacks objects because ownership of those objects passes into the
99 // client, which destroys the callbacks after calling them to resolve the 120 // client, which destroys the callbacks after calling them to resolve the
100 // pending Blink-side Promise. 121 // pending Blink-side Promise.
101 bool callback_errored_; 122 bool callback_errored_;
102 bool callback_succeeded_; 123 bool callback_succeeded_;
103
104 std::unique_ptr<blink::WebPasswordCredential> credential_;
105 }; 124 };
106 125
107 class TestNotificationCallbacks 126 class TestNotificationCallbacks
108 : public blink::WebCredentialManagerClient::NotificationCallbacks { 127 : public blink::WebCredentialManagerClient::NotificationCallbacks {
109 public: 128 public:
110 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) 129 explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
111 : test_(test) {} 130 : test_(test) {}
112 131
113 ~TestNotificationCallbacks() override {} 132 ~TestNotificationCallbacks() override {}
114 133
115 void onSuccess() override { test_->set_callback_succeeded(true); } 134 void onSuccess() override { test_->set_callback_succeeded(true); }
116 135
117 void onError(blink::WebCredentialManagerError reason) override { 136 void onError(blink::WebCredentialManagerError reason) override {
118 test_->set_callback_errored(true); 137 test_->set_callback_errored(true);
119 } 138 }
120 139
121 private: 140 private:
122 CredentialManagerClientTest* test_; 141 CredentialManagerClientTest* test_;
123 }; 142 };
124 143
125 class TestRequestCallbacks 144 class TestRequestCallbacks
126 : public blink::WebCredentialManagerClient::RequestCallbacks { 145 : public blink::WebCredentialManagerClient::RequestCallbacks {
127 public: 146 public:
128 explicit TestRequestCallbacks(CredentialManagerClientTest* test) 147 explicit TestRequestCallbacks(CredentialManagerClientTest* test)
129 : test_(test) {} 148 : test_(test) {}
130 149
131 ~TestRequestCallbacks() override {} 150 ~TestRequestCallbacks() override {}
132 151
133 void onSuccess(blink::WebPassOwnPtr<blink::WebCredential>) override { 152 void onSuccess(
153 blink::WebPassOwnPtr<blink::WebCredential> credential) override {
134 test_->set_callback_succeeded(true); 154 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));
135 } 159 }
136 160
137 void onError(blink::WebCredentialManagerError reason) override { 161 void onError(blink::WebCredentialManagerError reason) override {
138 test_->set_callback_errored(true); 162 test_->set_callback_errored(true);
163 test_->credential_.reset();
164 test_->error_ = reason;
139 } 165 }
140 166
141 private: 167 private:
142 CredentialManagerClientTest* test_; 168 CredentialManagerClientTest* test_;
143 }; 169 };
144 170
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
145 } // namespace 178 } // namespace
146 179
147 TEST_F(CredentialManagerClientTest, SendStore) { 180 TEST_F(CredentialManagerClientTest, SendStore) {
148 int request_id; 181 credential_.reset(new blink::WebPasswordCredential("", "", "", GURL()));
149 EXPECT_FALSE(
150 ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id));
151
152 std::unique_ptr<TestNotificationCallbacks> callbacks( 182 std::unique_ptr<TestNotificationCallbacks> callbacks(
153 new TestNotificationCallbacks(this)); 183 new TestNotificationCallbacks(this));
154 client_->dispatchStore(*credential(), callbacks.release()); 184 client_->dispatchStore(*credential_, callbacks.release());
155 185
156 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id)); 186 RunAllPendingTasks();
157 187
158 client_->OnAcknowledgeStore(request_id);
159 EXPECT_TRUE(callback_succeeded()); 188 EXPECT_TRUE(callback_succeeded());
160 EXPECT_FALSE(callback_errored()); 189 EXPECT_FALSE(callback_errored());
161 } 190 }
162 191
163 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) { 192 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) {
164 int request_id;
165 EXPECT_FALSE(ExtractRequestId(
166 CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
167
168 std::unique_ptr<TestNotificationCallbacks> callbacks( 193 std::unique_ptr<TestNotificationCallbacks> callbacks(
169 new TestNotificationCallbacks(this)); 194 new TestNotificationCallbacks(this));
170 client_->dispatchRequireUserMediation(callbacks.release()); 195 client_->dispatchRequireUserMediation(callbacks.release());
171 196
172 EXPECT_TRUE(ExtractRequestId( 197 RunAllPendingTasks();
173 CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
174 198
175 client_->OnAcknowledgeRequireUserMediation(request_id);
176 EXPECT_TRUE(callback_succeeded()); 199 EXPECT_TRUE(callback_succeeded());
177 EXPECT_FALSE(callback_errored()); 200 EXPECT_FALSE(callback_errored());
178 } 201 }
179 202
180 TEST_F(CredentialManagerClientTest, SendRequestCredential) { 203 TEST_F(CredentialManagerClientTest, SendRequestCredential) {
181 int request_id;
182 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
183 request_id));
184
185 std::unique_ptr<TestRequestCallbacks> callbacks( 204 std::unique_ptr<TestRequestCallbacks> callbacks(
186 new TestRequestCallbacks(this)); 205 new TestRequestCallbacks(this));
187 std::vector<GURL> federations; 206 std::vector<GURL> federations;
207 federations.push_back(GURL(kTestCredentialPassword));
188 client_->dispatchGet(false, true, federations, callbacks.release()); 208 client_->dispatchGet(false, true, federations, callbacks.release());
189 209
190 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, 210 RunAllPendingTasks();
191 request_id));
192 211
193 CredentialInfo info;
194 info.type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
195 client_->OnSendCredential(request_id, info);
196 EXPECT_TRUE(callback_succeeded()); 212 EXPECT_TRUE(callback_succeeded());
197 EXPECT_FALSE(callback_errored()); 213 EXPECT_FALSE(callback_errored());
214 EXPECT_TRUE(credential_);
215 EXPECT_EQ("password", credential_->type());
198 } 216 }
199 217
200 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) { 218 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) {
201 int request_id;
202 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
203 request_id));
204
205 std::unique_ptr<TestRequestCallbacks> callbacks( 219 std::unique_ptr<TestRequestCallbacks> callbacks(
206 new TestRequestCallbacks(this)); 220 new TestRequestCallbacks(this));
207 std::vector<GURL> federations; 221 std::vector<GURL> federations;
222 federations.push_back(GURL(kTestCredentialEmpty));
208 client_->dispatchGet(false, true, federations, callbacks.release()); 223 client_->dispatchGet(false, true, federations, callbacks.release());
209 224
210 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, 225 RunAllPendingTasks();
211 request_id));
212 226
213 CredentialInfo info; // Send an empty credential in response.
214 client_->OnSendCredential(request_id, info);
215 EXPECT_TRUE(callback_succeeded()); 227 EXPECT_TRUE(callback_succeeded());
216 EXPECT_FALSE(callback_errored()); 228 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_);
217 } 247 }
218 248
219 } // namespace password_manager 249 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698