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

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, 9 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 scoped_ptr<blink::WebPasswordCredential> credential_;
110 blink::WebCredentialManagerError error_;
111
93 protected: 112 protected:
94 scoped_ptr<CredentialManagerClient> client_; 113 scoped_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 124
104 scoped_ptr<blink::WebPasswordCredential> credential_;
105 }; 125 };
106 126
107 class TestNotificationCallbacks 127 class TestNotificationCallbacks
108 : public blink::WebCredentialManagerClient::NotificationCallbacks { 128 : public blink::WebCredentialManagerClient::NotificationCallbacks {
109 public: 129 public:
110 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) 130 explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
111 : test_(test) {} 131 : test_(test) {}
112 132
113 ~TestNotificationCallbacks() override {} 133 ~TestNotificationCallbacks() override {}
114 134
115 void onSuccess() override { test_->set_callback_succeeded(true); } 135 void onSuccess() override { test_->set_callback_succeeded(true); }
116 136
117 void onError(blink::WebCredentialManagerError reason) override { 137 void onError(blink::WebCredentialManagerError reason) override {
118 test_->set_callback_errored(true); 138 test_->set_callback_errored(true);
119 } 139 }
120 140
121 private: 141 private:
122 CredentialManagerClientTest* test_; 142 CredentialManagerClientTest* test_;
123 }; 143 };
124 144
125 class TestRequestCallbacks 145 class TestRequestCallbacks
126 : public blink::WebCredentialManagerClient::RequestCallbacks { 146 : public blink::WebCredentialManagerClient::RequestCallbacks {
127 public: 147 public:
128 explicit TestRequestCallbacks(CredentialManagerClientTest* test) 148 explicit TestRequestCallbacks(CredentialManagerClientTest* test)
129 : test_(test) {} 149 : test_(test) {}
130 150
131 ~TestRequestCallbacks() override {} 151 ~TestRequestCallbacks() override {}
132 152
133 void onSuccess(blink::WebPassOwnPtr<blink::WebCredential>) override { 153 void onSuccess(
154 blink::WebPassOwnPtr<blink::WebCredential> credential) override {
134 test_->set_callback_succeeded(true); 155 test_->set_callback_succeeded(true);
156
157 blink::WebCredential* ptr =
158 scoped_ptr<blink::WebCredential>(credential).release();
159 test_->credential_.reset(static_cast<blink::WebPasswordCredential*>(ptr));
135 } 160 }
136 161
137 void onError(blink::WebCredentialManagerError reason) override { 162 void onError(blink::WebCredentialManagerError reason) override {
138 test_->set_callback_errored(true); 163 test_->set_callback_errored(true);
164 test_->credential_.reset();
165 test_->error_ = reason;
139 } 166 }
140 167
141 private: 168 private:
142 CredentialManagerClientTest* test_; 169 CredentialManagerClientTest* test_;
143 }; 170 };
144 171
172 void RunAllPendingTasks() {
173 base::RunLoop run_loop;
174 base::MessageLoop::current()->PostTask(
175 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
176 run_loop.Run();
177 }
178
145 } // namespace 179 } // namespace
146 180
147 TEST_F(CredentialManagerClientTest, SendStore) { 181 TEST_F(CredentialManagerClientTest, SendStore) {
148 int request_id; 182 credential_.reset(new blink::WebPasswordCredential("", "", "", GURL()));
149 EXPECT_FALSE(
150 ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id));
151
152 scoped_ptr<TestNotificationCallbacks> callbacks( 183 scoped_ptr<TestNotificationCallbacks> callbacks(
153 new TestNotificationCallbacks(this)); 184 new TestNotificationCallbacks(this));
154 client_->dispatchStore(*credential(), callbacks.release()); 185 client_->dispatchStore(*credential_, callbacks.release());
155 186
156 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id)); 187 RunAllPendingTasks();
157 188
158 client_->OnAcknowledgeStore(request_id);
159 EXPECT_TRUE(callback_succeeded()); 189 EXPECT_TRUE(callback_succeeded());
160 EXPECT_FALSE(callback_errored()); 190 EXPECT_FALSE(callback_errored());
161 } 191 }
162 192
163 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) { 193 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) {
164 int request_id;
165 EXPECT_FALSE(ExtractRequestId(
166 CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
167
168 scoped_ptr<TestNotificationCallbacks> callbacks( 194 scoped_ptr<TestNotificationCallbacks> callbacks(
169 new TestNotificationCallbacks(this)); 195 new TestNotificationCallbacks(this));
170 client_->dispatchRequireUserMediation(callbacks.release()); 196 client_->dispatchRequireUserMediation(callbacks.release());
171 197
172 EXPECT_TRUE(ExtractRequestId( 198 RunAllPendingTasks();
173 CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
174 199
175 client_->OnAcknowledgeRequireUserMediation(request_id);
176 EXPECT_TRUE(callback_succeeded()); 200 EXPECT_TRUE(callback_succeeded());
177 EXPECT_FALSE(callback_errored()); 201 EXPECT_FALSE(callback_errored());
178 } 202 }
179 203
180 TEST_F(CredentialManagerClientTest, SendRequestCredential) { 204 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)); 205 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
186 std::vector<GURL> federations; 206 std::vector<GURL> federations;
207 federations.push_back(GURL(kTestCredentialPassword));
187 client_->dispatchGet(false, true, federations, callbacks.release()); 208 client_->dispatchGet(false, true, federations, callbacks.release());
188 209
189 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, 210 RunAllPendingTasks();
190 request_id));
191 211
192 CredentialInfo info;
193 info.type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
194 client_->OnSendCredential(request_id, info);
195 EXPECT_TRUE(callback_succeeded()); 212 EXPECT_TRUE(callback_succeeded());
196 EXPECT_FALSE(callback_errored()); 213 EXPECT_FALSE(callback_errored());
214 EXPECT_TRUE(credential_);
215 EXPECT_EQ("password", credential_->type());
197 } 216 }
198 217
199 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) { 218 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)); 219 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
205 std::vector<GURL> federations; 220 std::vector<GURL> federations;
221 federations.push_back(GURL(kTestCredentialEmpty));
206 client_->dispatchGet(false, true, federations, callbacks.release()); 222 client_->dispatchGet(false, true, federations, callbacks.release());
207 223
208 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, 224 RunAllPendingTasks();
209 request_id));
210 225
211 CredentialInfo info; // Send an empty credential in response.
212 client_->OnSendCredential(request_id, info);
213 EXPECT_TRUE(callback_succeeded()); 226 EXPECT_TRUE(callback_succeeded());
214 EXPECT_FALSE(callback_errored()); 227 EXPECT_FALSE(callback_errored());
228 EXPECT_FALSE(credential_);
229 }
230
231 TEST_F(CredentialManagerClientTest, SendRequestCredentialReject) {
232 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
233 std::vector<GURL> federations;
234 federations.push_back(GURL(kTestCredentialReject));
235 client_->dispatchGet(false, true, federations, callbacks.release());
236
237 RunAllPendingTasks();
238
239 EXPECT_FALSE(callback_succeeded());
240 EXPECT_TRUE(callback_errored());
241 EXPECT_FALSE(credential_);
242 EXPECT_EQ(blink::WebCredentialManagerError::
243 WebCredentialManagerPasswordStoreUnavailableError,
244 error_);
215 } 245 }
216 246
217 } // namespace password_manager 247 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698