1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/password_manager/mock_password_store.h"
#include "chrome/browser/password_manager/password_manager.h"
#include "chrome/browser/password_manager/password_manager_delegate.h"
#include "chrome/browser/password_manager/password_store.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/common/frame_navigate_params.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::PasswordForm;
using testing::_;
using testing::DoAll;
using testing::Exactly;
using testing::Return;
using testing::WithArg;
class MockPasswordManagerDelegate : public PasswordManagerDelegate {
public:
MOCK_METHOD1(FillPasswordForm, void(const autofill::PasswordFormFillData&));
MOCK_METHOD1(AddSavePasswordInfoBarIfPermitted, void(PasswordFormManager*));
MOCK_METHOD0(GetProfile, Profile*());
MOCK_METHOD0(DidLastPageLoadEncounterSSLErrors, bool());
};
ACTION_P(InvokeConsumer, forms) {
arg0->OnGetPasswordStoreResults(forms);
}
ACTION_P(SaveToScopedPtr, scoped) {
scoped->reset(arg0);
}
class PasswordManagerTest : public ChromeRenderViewHostTestHarness {
protected:
virtual void SetUp() {
ChromeRenderViewHostTestHarness::SetUp();
store_ = static_cast<MockPasswordStore*>(
PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse(
profile(), MockPasswordStore::Build).get());
EXPECT_CALL(delegate_, GetProfile()).WillRepeatedly(Return(profile()));
PasswordManager::CreateForWebContentsAndDelegate(
web_contents(), &delegate_);
EXPECT_CALL(delegate_, DidLastPageLoadEncounterSSLErrors())
.WillRepeatedly(Return(false));
}
virtual void TearDown() {
store_ = NULL;
ChromeRenderViewHostTestHarness::TearDown();
}
PasswordForm MakeSimpleForm() {
PasswordForm form;
form.origin = GURL("http://www.google.com/a/LoginAuth");
form.action = GURL("http://www.google.com/a/Login");
form.username_element = ASCIIToUTF16("Email");
form.password_element = ASCIIToUTF16("Passwd");
form.username_value = ASCIIToUTF16("google");
form.password_value = ASCIIToUTF16("password");
// Default to true so we only need to add tests in autocomplete=off cases.
form.password_autocomplete_set = true;
form.submit_element = ASCIIToUTF16("signIn");
form.signon_realm = "http://www.google.com";
return form;
}
PasswordManager* manager() {
return PasswordManager::FromWebContents(web_contents());
}
scoped_refptr<MockPasswordStore> store_;
MockPasswordManagerDelegate delegate_; // Owned by manager_.
};
MATCHER_P(FormMatches, form, "") {
return form.signon_realm == arg.signon_realm &&
form.origin == arg.origin &&
form.action == arg.action &&
form.username_element == arg.username_element &&
form.password_element == arg.password_element &&
form.password_autocomplete_set ==
arg.password_autocomplete_set &&
form.submit_element == arg.submit_element;
}
TEST_F(PasswordManagerTest, FormSubmitEmptyStore) {
// Test that observing a newly submitted form shows the save password bar.
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillOnce(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
// And the form submit contract is to call ProvisionallySavePassword.
manager()->ProvisionallySavePassword(form);
scoped_ptr<PasswordFormManager> form_to_save;
EXPECT_CALL(delegate_, AddSavePasswordInfoBarIfPermitted(_))
.WillOnce(WithArg<0>(SaveToScopedPtr(&form_to_save)));
// Now the password manager waits for the navigation to complete.
observed.clear();
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
ASSERT_TRUE(form_to_save.get());
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(form)));
// Simulate saving the form, as if the info bar was accepted.
form_to_save->Save();
}
TEST_F(PasswordManagerTest, GeneratedPasswordFormSubmitEmptyStore) {
// This test is the same FormSubmitEmptyStore, except that it simulates the
// user generating the password through the browser.
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillOnce(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
// Simulate the user generating the password and submitting the form.
manager()->SetFormHasGeneratedPassword(form);
manager()->ProvisionallySavePassword(form);
// The user should not be presented with an infobar as they have already given
// consent by using the generated password. The form should be saved once
// navigation occurs.
EXPECT_CALL(delegate_,
AddSavePasswordInfoBarIfPermitted(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(form)));
// Now the password manager waits for the navigation to complete.
observed.clear();
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
}
TEST_F(PasswordManagerTest, FormSubmitNoGoodMatch) {
// Same as above, except with an existing form for the same signon realm,
// but different origin. Detailed cases like this are covered by
// PasswordFormManagerTest.
std::vector<PasswordForm*> result;
PasswordForm* existing_different = new PasswordForm(MakeSimpleForm());
existing_different->username_value = ASCIIToUTF16("google2");
result.push_back(existing_different);
EXPECT_CALL(delegate_, FillPasswordForm(_));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillOnce(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
manager()->ProvisionallySavePassword(form);
// We still expect an add, since we didn't have a good match.
scoped_ptr<PasswordFormManager> form_to_save;
EXPECT_CALL(delegate_, AddSavePasswordInfoBarIfPermitted(_))
.WillOnce(WithArg<0>(SaveToScopedPtr(&form_to_save)));
// Now the password manager waits for the navigation to complete.
observed.clear();
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
ASSERT_TRUE(form_to_save.get());
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(form)));
// Simulate saving the form.
form_to_save->Save();
}
TEST_F(PasswordManagerTest, FormSeenThenLeftPage) {
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillOnce(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
PasswordForm empty_form(form);
empty_form.username_value = string16();
empty_form.password_value = string16();
content::LoadCommittedDetails details;
content::FrameNavigateParams params;
params.password_form = empty_form;
manager()->DidNavigateAnyFrame(details, params);
// No expected calls.
EXPECT_CALL(delegate_, AddSavePasswordInfoBarIfPermitted(_)).Times(0);
observed.clear();
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
}
TEST_F(PasswordManagerTest, FormSubmitAfterNavigateSubframe) {
// Test that navigating a subframe does not prevent us from showing the save
// password infobar.
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillOnce(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
// Simulate navigating a sub-frame.
content::LoadCommittedDetails details;
details.is_main_frame = false;
content::FrameNavigateParams params;
manager()->DidNavigateAnyFrame(details, params);
// Simulate navigating the real page.
details.is_main_frame = true;
params.password_form = form;
manager()->DidNavigateAnyFrame(details, params);
// Now the password manager waits for the navigation to complete.
scoped_ptr<PasswordFormManager> form_to_save;
EXPECT_CALL(delegate_, AddSavePasswordInfoBarIfPermitted(_))
.WillOnce(WithArg<0>(SaveToScopedPtr(&form_to_save)));
observed.clear();
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
ASSERT_FALSE(NULL == form_to_save.get());
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(form)));
// Simulate saving the form, as if the info bar was accepted.
form_to_save->Save();
}
// This test verifies a fix for http://crbug.com/236673
TEST_F(PasswordManagerTest, FormSubmitWithFormOnPreviousPage) {
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillRepeatedly(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
PasswordForm first_form(MakeSimpleForm());
first_form.origin = GURL("http://www.nytimes.com/");
first_form.action = GURL("https://myaccount.nytimes.com/auth/login");
first_form.signon_realm = "http://www.nytimes.com/";
PasswordForm second_form(MakeSimpleForm());
second_form.origin = GURL("https://myaccount.nytimes.com/auth/login");
second_form.action = GURL("https://myaccount.nytimes.com/auth/login");
second_form.signon_realm = "https://myaccount.nytimes.com/";
// Pretend that the form is hidden on the first page.
std::vector<PasswordForm> observed;
observed.push_back(first_form);
manager()->OnPasswordFormsParsed(observed);
observed.clear();
manager()->OnPasswordFormsRendered(observed);
// Now navigate to a second page.
content::LoadCommittedDetails details;
details.is_main_frame = true;
content::FrameNavigateParams params;
manager()->DidNavigateAnyFrame(details, params);
// This page contains a form with the same markup, but on a different
// URL.
observed.push_back(second_form);
manager()->OnPasswordFormsParsed(observed);
manager()->OnPasswordFormsRendered(observed);
// Now submit this form
params.password_form = second_form;
manager()->DidNavigateAnyFrame(details, params);
// Navigation after form submit.
scoped_ptr<PasswordFormManager> form_to_save;
EXPECT_CALL(delegate_, AddSavePasswordInfoBarIfPermitted(_))
.WillOnce(WithArg<0>(SaveToScopedPtr(&form_to_save)));
observed.clear();
manager()->OnPasswordFormsParsed(observed);
manager()->OnPasswordFormsRendered(observed);
// Make sure that the saved form matches the second form, not the first.
ASSERT_TRUE(form_to_save.get());
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(second_form)));
// Simulate saving the form, as if the info bar was accepted.
form_to_save->Save();
}
TEST_F(PasswordManagerTest, FormSubmitFailedLogin) {
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillRepeatedly(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
manager()->ProvisionallySavePassword(form);
// The form reappears, and is visible in the layout:
// No expected calls to the PasswordStore...
manager()->OnPasswordFormsParsed(observed);
manager()->OnPasswordFormsRendered(observed);
}
TEST_F(PasswordManagerTest, FormSubmitInvisibleLogin) {
// Tests fix of issue 28911: if the login form reappears on the subsequent
// page, but is invisible, it shouldn't count as a failed login.
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillRepeatedly(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
manager()->ProvisionallySavePassword(form);
// Expect info bar to appear:
scoped_ptr<PasswordFormManager> form_to_save;
EXPECT_CALL(delegate_, AddSavePasswordInfoBarIfPermitted(_))
.WillOnce(WithArg<0>(SaveToScopedPtr(&form_to_save)));
// The form reappears, but is not visible in the layout:
manager()->OnPasswordFormsParsed(observed);
observed.clear();
manager()->OnPasswordFormsRendered(observed);
ASSERT_TRUE(form_to_save.get());
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(form)));
// Simulate saving the form.
form_to_save->Save();
}
TEST_F(PasswordManagerTest, InitiallyInvisibleForm) {
// Make sure an invisible login form still gets autofilled.
std::vector<PasswordForm*> result;
PasswordForm* existing = new PasswordForm(MakeSimpleForm());
result.push_back(existing);
EXPECT_CALL(delegate_, FillPasswordForm(_));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillRepeatedly(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
observed.clear();
manager()->OnPasswordFormsRendered(observed); // The initial layout.
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
}
TEST_F(PasswordManagerTest, SavingDependsOnManagerEnabledPreference) {
// Test that saving passwords depends on the password manager enabled
// preference.
TestingPrefServiceSyncable* prefService = profile()->GetTestingPrefService();
prefService->SetUserPref(prefs::kPasswordManagerEnabled,
Value::CreateBooleanValue(true));
EXPECT_TRUE(manager()->IsSavingEnabled());
prefService->SetUserPref(prefs::kPasswordManagerEnabled,
Value::CreateBooleanValue(false));
EXPECT_FALSE(manager()->IsSavingEnabled());
}
TEST_F(PasswordManagerTest, FillPasswordsOnDisabledManager) {
// Test fix for issue 158296: Passwords must be filled even if the password
// manager is disabled.
std::vector<PasswordForm*> result;
PasswordForm* existing = new PasswordForm(MakeSimpleForm());
result.push_back(existing);
TestingPrefServiceSyncable* prefService = profile()->GetTestingPrefService();
prefService->SetUserPref(prefs::kPasswordManagerEnabled,
Value::CreateBooleanValue(false));
EXPECT_CALL(delegate_, FillPasswordForm(_));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillRepeatedly(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed);
}
TEST_F(PasswordManagerTest, FormNotSavedAutocompleteOff) {
// Test password form with non-generated password will not be saved if
// autocomplete=off.
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillOnce(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
form.password_autocomplete_set = false;
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
// And the form submit contract is to call ProvisionallySavePassword.
manager()->ProvisionallySavePassword(form);
// Password form should not be saved.
EXPECT_CALL(delegate_,
AddSavePasswordInfoBarIfPermitted(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(form))).Times(Exactly(0));
// Now the password manager waits for the navigation to complete.
observed.clear();
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
}
TEST_F(PasswordManagerTest, GeneratedPasswordFormSavedAutocompleteOff) {
// Test password form with generated password will still be saved if
// autocomplete=off.
std::vector<PasswordForm*> result; // Empty password store.
EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), GetLogins(_, _))
.WillOnce(DoAll(WithArg<1>(InvokeConsumer(result)), Return(1)));
std::vector<PasswordForm> observed;
PasswordForm form(MakeSimpleForm());
form.password_autocomplete_set = false;
observed.push_back(form);
manager()->OnPasswordFormsParsed(observed); // The initial load.
manager()->OnPasswordFormsRendered(observed); // The initial layout.
// Simulate the user generating the password and submitting the form.
manager()->SetFormHasGeneratedPassword(form);
manager()->ProvisionallySavePassword(form);
// The user should not be presented with an infobar as they have already given
// consent by using the generated password. The form should be saved once
// navigation occurs.
EXPECT_CALL(delegate_,
AddSavePasswordInfoBarIfPermitted(_)).Times(Exactly(0));
EXPECT_CALL(*store_.get(), AddLogin(FormMatches(form)));
// Now the password manager waits for the navigation to complete.
observed.clear();
manager()->OnPasswordFormsParsed(observed); // The post-navigation load.
manager()->OnPasswordFormsRendered(observed); // The post-navigation layout.
}
|