1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/basictypes.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/password_manager/native_backend_gnome_x.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "components/autofill/core/common/password_form.h"
17 #include "components/password_manager/core/browser/psl_matching_helper.h"
18 #include "components/password_manager/core/common/password_manager_pref_names.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using autofill::PasswordForm
;
23 using base::UTF8ToUTF16
;
24 using base::UTF16ToUTF8
;
25 using content::BrowserThread
;
26 using password_manager::PasswordStoreChange
;
27 using password_manager::PasswordStoreChangeList
;
31 // What follows is a very simple implementation of the subset of the GNOME
32 // Keyring API that we actually use. It gets substituted for the real one by
33 // MockGnomeKeyringLoader, which hooks into the facility normally used to load
34 // the GNOME Keyring library at runtime to avoid a static dependency on it.
36 struct MockKeyringItem
{
38 MockKeyringItem(const char* keyring
,
39 const std::string
& display_name
,
40 const std::string
& password
)
41 : keyring(keyring
? keyring
: "login"),
42 display_name(display_name
),
45 struct ItemAttribute
{
46 ItemAttribute() : type(UINT32
), value_uint32(0) {}
47 explicit ItemAttribute(uint32_t value
)
48 : type(UINT32
), value_uint32(value
) {}
49 explicit ItemAttribute(const std::string
& value
)
50 : type(STRING
), value_string(value
) {}
52 bool Equals(const ItemAttribute
& x
) const {
53 if (type
!= x
.type
) return false;
54 return (type
== STRING
) ? value_string
== x
.value_string
55 : value_uint32
== x
.value_uint32
;
58 enum Type
{ UINT32
, STRING
} type
;
59 uint32_t value_uint32
;
60 std::string value_string
;
63 typedef std::map
<std::string
, ItemAttribute
> attribute_map
;
64 typedef std::vector
<std::pair
<std::string
, ItemAttribute
> > attribute_query
;
66 bool Matches(const attribute_query
& query
) const {
67 // The real GNOME Keyring doesn't match empty queries.
68 if (query
.empty()) return false;
69 for (size_t i
= 0; i
< query
.size(); ++i
) {
70 attribute_map::const_iterator match
= attributes
.find(query
[i
].first
);
71 if (match
== attributes
.end()) return false;
72 if (!match
->second
.Equals(query
[i
].second
)) return false;
78 std::string display_name
;
81 attribute_map attributes
;
84 // The list of all keyring items we have stored.
85 std::vector
<MockKeyringItem
> mock_keyring_items
;
86 bool mock_keyring_reject_local_ids
= false;
88 bool IsStringAttribute(const GnomeKeyringPasswordSchema
* schema
,
89 const std::string
& name
) {
90 for (size_t i
= 0; schema
->attributes
[i
].name
; ++i
)
91 if (name
== schema
->attributes
[i
].name
)
92 return schema
->attributes
[i
].type
== GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
;
93 NOTREACHED() << "Requested type of nonexistent attribute";
97 gboolean
mock_gnome_keyring_is_available() {
101 gpointer
mock_gnome_keyring_store_password(
102 const GnomeKeyringPasswordSchema
* schema
,
103 const gchar
* keyring
,
104 const gchar
* display_name
,
105 const gchar
* password
,
106 GnomeKeyringOperationDoneCallback callback
,
108 GDestroyNotify destroy_data
,
110 mock_keyring_items
.push_back(
111 MockKeyringItem(keyring
, display_name
, password
));
112 MockKeyringItem
* item
= &mock_keyring_items
.back();
113 const std::string keyring_desc
=
114 keyring
? base::StringPrintf("keyring %s", keyring
)
115 : std::string("default keyring");
116 VLOG(1) << "Adding item with origin " << display_name
117 << " to " << keyring_desc
;
119 va_start(ap
, destroy_data
);
121 while ((name
= va_arg(ap
, gchar
*))) {
122 if (IsStringAttribute(schema
, name
)) {
123 item
->attributes
[name
] =
124 MockKeyringItem::ItemAttribute(va_arg(ap
, gchar
*));
125 VLOG(1) << "Adding item attribute " << name
126 << ", value '" << item
->attributes
[name
].value_string
<< "'";
128 item
->attributes
[name
] =
129 MockKeyringItem::ItemAttribute(va_arg(ap
, uint32_t));
130 VLOG(1) << "Adding item attribute " << name
131 << ", value " << item
->attributes
[name
].value_uint32
;
135 // As a hack to ease testing migration, make it possible to reject the new
136 // format for the app string. This way we can add them easily to migrate.
137 if (mock_keyring_reject_local_ids
) {
138 MockKeyringItem::attribute_map::iterator it
=
139 item
->attributes
.find("application");
140 if (it
!= item
->attributes
.end() &&
141 it
->second
.type
== MockKeyringItem::ItemAttribute::STRING
&&
142 base::StringPiece(it
->second
.value_string
).starts_with("chrome-")) {
143 mock_keyring_items
.pop_back();
144 // GnomeKeyringResult, data
145 callback(GNOME_KEYRING_RESULT_IO_ERROR
, data
);
149 // GnomeKeyringResult, data
150 callback(GNOME_KEYRING_RESULT_OK
, data
);
154 gpointer
mock_gnome_keyring_delete_password(
155 const GnomeKeyringPasswordSchema
* schema
,
156 GnomeKeyringOperationDoneCallback callback
,
158 GDestroyNotify destroy_data
,
160 MockKeyringItem::attribute_query query
;
162 va_start(ap
, destroy_data
);
164 while ((name
= va_arg(ap
, gchar
*))) {
165 if (IsStringAttribute(schema
, name
)) {
166 query
.push_back(make_pair(std::string(name
),
167 MockKeyringItem::ItemAttribute(va_arg(ap
, gchar
*))));
168 VLOG(1) << "Querying with item attribute " << name
169 << ", value '" << query
.back().second
.value_string
<< "'";
171 query
.push_back(make_pair(std::string(name
),
172 MockKeyringItem::ItemAttribute(va_arg(ap
, uint32_t))));
173 VLOG(1) << "Querying with item attribute " << name
174 << ", value " << query
.back().second
.value_uint32
;
178 bool deleted
= false;
179 for (size_t i
= mock_keyring_items
.size(); i
> 0; --i
) {
180 const MockKeyringItem
* item
= &mock_keyring_items
[i
- 1];
181 if (item
->Matches(query
)) {
182 VLOG(1) << "Deleting item with origin " << item
->display_name
;
183 mock_keyring_items
.erase(mock_keyring_items
.begin() + (i
- 1));
187 // GnomeKeyringResult, data
188 callback(deleted
? GNOME_KEYRING_RESULT_OK
189 : GNOME_KEYRING_RESULT_NO_MATCH
, data
);
193 gpointer
mock_gnome_keyring_find_items(
194 GnomeKeyringItemType type
,
195 GnomeKeyringAttributeList
* attributes
,
196 GnomeKeyringOperationGetListCallback callback
,
198 GDestroyNotify destroy_data
) {
199 MockKeyringItem::attribute_query query
;
200 for (size_t i
= 0; i
< attributes
->len
; ++i
) {
201 GnomeKeyringAttribute attribute
=
202 g_array_index(attributes
, GnomeKeyringAttribute
, i
);
203 if (attribute
.type
== GNOME_KEYRING_ATTRIBUTE_TYPE_STRING
) {
205 make_pair(std::string(attribute
.name
),
206 MockKeyringItem::ItemAttribute(attribute
.value
.string
)));
207 VLOG(1) << "Querying with item attribute " << attribute
.name
208 << ", value '" << query
.back().second
.value_string
<< "'";
211 make_pair(std::string(attribute
.name
),
212 MockKeyringItem::ItemAttribute(attribute
.value
.integer
)));
213 VLOG(1) << "Querying with item attribute " << attribute
.name
<< ", value "
214 << query
.back().second
.value_uint32
;
217 // Find matches and add them to a list of results.
218 GList
* results
= nullptr;
219 for (size_t i
= 0; i
< mock_keyring_items
.size(); ++i
) {
220 const MockKeyringItem
* item
= &mock_keyring_items
[i
];
221 if (item
->Matches(query
)) {
222 GnomeKeyringFound
* found
= new GnomeKeyringFound
;
223 found
->keyring
= strdup(item
->keyring
.c_str());
225 found
->attributes
= gnome_keyring_attribute_list_new();
226 for (MockKeyringItem::attribute_map::const_iterator it
=
227 item
->attributes
.begin();
228 it
!= item
->attributes
.end();
230 if (it
->second
.type
== MockKeyringItem::ItemAttribute::STRING
) {
231 gnome_keyring_attribute_list_append_string(
232 found
->attributes
, it
->first
.c_str(),
233 it
->second
.value_string
.c_str());
235 gnome_keyring_attribute_list_append_uint32(
236 found
->attributes
, it
->first
.c_str(),
237 it
->second
.value_uint32
);
240 found
->secret
= strdup(item
->password
.c_str());
241 results
= g_list_prepend(results
, found
);
244 // GnomeKeyringResult, GList*, data
245 callback(results
? GNOME_KEYRING_RESULT_OK
246 : GNOME_KEYRING_RESULT_NO_MATCH
, results
, data
);
247 // Now free the list of results.
248 GList
* element
= g_list_first(results
);
250 GnomeKeyringFound
* found
= static_cast<GnomeKeyringFound
*>(element
->data
);
251 free(found
->keyring
);
252 gnome_keyring_attribute_list_free(found
->attributes
);
255 element
= g_list_next(element
);
257 g_list_free(results
);
261 const gchar
* mock_gnome_keyring_result_to_message(GnomeKeyringResult res
) {
262 return "mock keyring simulating failure";
265 // Inherit to get access to protected fields.
266 class MockGnomeKeyringLoader
: public GnomeKeyringLoader
{
268 static bool LoadMockGnomeKeyring() {
269 if (!LoadGnomeKeyring())
271 #define GNOME_KEYRING_ASSIGN_POINTER(name) \
272 gnome_keyring_##name = &mock_gnome_keyring_##name;
273 GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(GNOME_KEYRING_ASSIGN_POINTER
)
274 #undef GNOME_KEYRING_ASSIGN_POINTER
275 keyring_loaded
= true;
276 // Reset the state of the mock library.
277 mock_keyring_items
.clear();
278 mock_keyring_reject_local_ids
= false;
283 void CheckPasswordChanges(const PasswordStoreChangeList
& expected_list
,
284 const PasswordStoreChangeList
& actual_list
) {
285 ASSERT_EQ(expected_list
.size(), actual_list
.size());
286 for (size_t i
= 0; i
< expected_list
.size(); ++i
) {
287 EXPECT_EQ(expected_list
[i
].type(), actual_list
[i
].type());
288 const PasswordForm
& expected
= expected_list
[i
].form();
289 const PasswordForm
& actual
= actual_list
[i
].form();
291 EXPECT_EQ(expected
.origin
, actual
.origin
);
292 EXPECT_EQ(expected
.password_value
, actual
.password_value
);
293 EXPECT_EQ(expected
.action
, actual
.action
);
294 EXPECT_EQ(expected
.username_element
, actual
.username_element
);
295 EXPECT_EQ(expected
.username_value
, actual
.username_value
);
296 EXPECT_EQ(expected
.password_element
, actual
.password_element
);
297 EXPECT_EQ(expected
.submit_element
, actual
.submit_element
);
298 EXPECT_EQ(expected
.signon_realm
, actual
.signon_realm
);
299 EXPECT_EQ(expected
.ssl_valid
, actual
.ssl_valid
);
300 EXPECT_EQ(expected
.preferred
, actual
.preferred
);
301 EXPECT_EQ(expected
.date_created
, actual
.date_created
);
302 EXPECT_EQ(expected
.blacklisted_by_user
, actual
.blacklisted_by_user
);
303 EXPECT_EQ(expected
.type
, actual
.type
);
304 EXPECT_EQ(expected
.times_used
, actual
.times_used
);
305 EXPECT_EQ(expected
.scheme
, actual
.scheme
);
306 EXPECT_EQ(expected
.date_synced
, actual
.date_synced
);
307 EXPECT_EQ(expected
.display_name
, actual
.display_name
);
308 EXPECT_EQ(expected
.avatar_url
, actual
.avatar_url
);
309 EXPECT_EQ(expected
.federation_url
, actual
.federation_url
);
310 EXPECT_EQ(expected
.skip_zero_click
, actual
.skip_zero_click
);
311 EXPECT_EQ(expected
.generation_upload_status
,
312 actual
.generation_upload_status
);
316 void CheckPasswordChangesWithResult(const PasswordStoreChangeList
* expected
,
317 const PasswordStoreChangeList
* actual
,
320 CheckPasswordChanges(*expected
, *actual
);
323 } // anonymous namespace
325 class NativeBackendGnomeTest
: public testing::Test
{
327 enum UpdateType
{ // Used in CheckPSLUpdate().
328 UPDATE_BY_UPDATELOGIN
,
331 enum RemoveBetweenMethod
{ // Used in CheckRemoveLoginsBetween().
336 NativeBackendGnomeTest()
337 : ui_thread_(BrowserThread::UI
, &message_loop_
),
338 db_thread_(BrowserThread::DB
) {
341 void SetUp() override
{
342 ASSERT_TRUE(db_thread_
.Start());
344 ASSERT_TRUE(MockGnomeKeyringLoader::LoadMockGnomeKeyring());
346 form_google_
.origin
= GURL("http://www.google.com/");
347 form_google_
.action
= GURL("http://www.google.com/login");
348 form_google_
.username_element
= UTF8ToUTF16("user");
349 form_google_
.username_value
= UTF8ToUTF16("joeschmoe");
350 form_google_
.password_element
= UTF8ToUTF16("pass");
351 form_google_
.password_value
= UTF8ToUTF16("seekrit");
352 form_google_
.submit_element
= UTF8ToUTF16("submit");
353 form_google_
.signon_realm
= "http://www.google.com/";
354 form_google_
.type
= PasswordForm::TYPE_GENERATED
;
355 form_google_
.date_created
= base::Time::Now();
356 form_google_
.date_synced
= base::Time::Now();
357 form_google_
.display_name
= UTF8ToUTF16("Joe Schmoe");
358 form_google_
.avatar_url
= GURL("http://www.google.com/avatar");
359 form_google_
.federation_url
= GURL("http://www.google.com/federation_url");
360 form_google_
.skip_zero_click
= true;
361 form_google_
.generation_upload_status
= PasswordForm::POSITIVE_SIGNAL_SENT
;
362 form_google_
.form_data
.name
= UTF8ToUTF16("form_name");
363 form_google_
.form_data
.user_submitted
= true;
365 form_facebook_
.origin
= GURL("http://www.facebook.com/");
366 form_facebook_
.action
= GURL("http://www.facebook.com/login");
367 form_facebook_
.username_element
= UTF8ToUTF16("user");
368 form_facebook_
.username_value
= UTF8ToUTF16("a");
369 form_facebook_
.password_element
= UTF8ToUTF16("password");
370 form_facebook_
.password_value
= UTF8ToUTF16("b");
371 form_facebook_
.submit_element
= UTF8ToUTF16("submit");
372 form_facebook_
.signon_realm
= "http://www.facebook.com/";
373 form_facebook_
.date_created
= base::Time::Now();
374 form_facebook_
.date_synced
= base::Time::Now();
375 form_facebook_
.display_name
= UTF8ToUTF16("Joe Schmoe");
376 form_facebook_
.avatar_url
= GURL("http://www.facebook.com/avatar");
377 form_facebook_
.federation_url
= GURL("http://www.facebook.com/federation");
378 form_facebook_
.skip_zero_click
= true;
379 form_facebook_
.generation_upload_status
= PasswordForm::NO_SIGNAL_SENT
;
381 form_isc_
.origin
= GURL("http://www.isc.org/");
382 form_isc_
.action
= GURL("http://www.isc.org/auth");
383 form_isc_
.username_element
= UTF8ToUTF16("id");
384 form_isc_
.username_value
= UTF8ToUTF16("janedoe");
385 form_isc_
.password_element
= UTF8ToUTF16("passwd");
386 form_isc_
.password_value
= UTF8ToUTF16("ihazabukkit");
387 form_isc_
.submit_element
= UTF8ToUTF16("login");
388 form_isc_
.signon_realm
= "http://www.isc.org/";
389 form_isc_
.date_created
= base::Time::Now();
390 form_isc_
.date_synced
= base::Time::Now();
392 other_auth_
.origin
= GURL("http://www.example.com/");
393 other_auth_
.username_value
= UTF8ToUTF16("username");
394 other_auth_
.password_value
= UTF8ToUTF16("pass");
395 other_auth_
.signon_realm
= "http://www.example.com/Realm";
396 other_auth_
.date_created
= base::Time::Now();
397 other_auth_
.date_synced
= base::Time::Now();
400 void TearDown() override
{
401 base::MessageLoop::current()->PostTask(FROM_HERE
,
402 base::MessageLoop::QuitClosure());
403 base::MessageLoop::current()->Run();
407 void RunBothThreads() {
408 // First we post a message to the DB thread that will run after all other
409 // messages that have been posted to the DB thread (we don't expect more
410 // to be posted), which posts a message to the UI thread to quit the loop.
411 // That way we can run both loops and be sure that the UI thread loop will
412 // quit so we can get on with the rest of the test.
413 BrowserThread::PostTask(BrowserThread::DB
, FROM_HERE
,
414 base::Bind(&PostQuitTask
, &message_loop_
));
415 base::MessageLoop::current()->Run();
418 static void PostQuitTask(base::MessageLoop
* loop
) {
419 loop
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
422 void CheckUint32Attribute(const MockKeyringItem
* item
,
423 const std::string
& attribute
,
425 MockKeyringItem::attribute_map::const_iterator it
=
426 item
->attributes
.find(attribute
);
427 EXPECT_NE(item
->attributes
.end(), it
);
428 if (it
!= item
->attributes
.end()) {
429 EXPECT_EQ(MockKeyringItem::ItemAttribute::UINT32
, it
->second
.type
);
430 EXPECT_EQ(value
, it
->second
.value_uint32
);
434 void CheckStringAttribute(const MockKeyringItem
* item
,
435 const std::string
& attribute
,
436 const std::string
& value
) {
437 MockKeyringItem::attribute_map::const_iterator it
=
438 item
->attributes
.find(attribute
);
439 EXPECT_NE(item
->attributes
.end(), it
);
440 if (it
!= item
->attributes
.end()) {
441 EXPECT_EQ(MockKeyringItem::ItemAttribute::STRING
, it
->second
.type
);
442 EXPECT_EQ(value
, it
->second
.value_string
);
446 void CheckMockKeyringItem(const MockKeyringItem
* item
,
447 const PasswordForm
& form
,
448 const std::string
& app_string
) {
449 // We always add items to the login keyring.
450 EXPECT_EQ("login", item
->keyring
);
451 EXPECT_EQ(form
.origin
.spec(), item
->display_name
);
452 EXPECT_EQ(UTF16ToUTF8(form
.password_value
), item
->password
);
453 EXPECT_EQ(22u, item
->attributes
.size());
454 CheckStringAttribute(item
, "origin_url", form
.origin
.spec());
455 CheckStringAttribute(item
, "action_url", form
.action
.spec());
456 CheckStringAttribute(item
, "username_element",
457 UTF16ToUTF8(form
.username_element
));
458 CheckStringAttribute(item
, "username_value",
459 UTF16ToUTF8(form
.username_value
));
460 CheckStringAttribute(item
, "password_element",
461 UTF16ToUTF8(form
.password_element
));
462 CheckStringAttribute(item
, "submit_element",
463 UTF16ToUTF8(form
.submit_element
));
464 CheckStringAttribute(item
, "signon_realm", form
.signon_realm
);
465 CheckUint32Attribute(item
, "ssl_valid", form
.ssl_valid
);
466 CheckUint32Attribute(item
, "preferred", form
.preferred
);
467 // We don't check the date created. It varies.
468 CheckUint32Attribute(item
, "blacklisted_by_user", form
.blacklisted_by_user
);
469 CheckUint32Attribute(item
, "type", form
.type
);
470 CheckUint32Attribute(item
, "times_used", form
.times_used
);
471 CheckUint32Attribute(item
, "scheme", form
.scheme
);
472 CheckStringAttribute(item
, "date_synced", base::Int64ToString(
473 form
.date_synced
.ToInternalValue()));
474 CheckStringAttribute(item
, "display_name", UTF16ToUTF8(form
.display_name
));
475 CheckStringAttribute(item
, "avatar_url", form
.avatar_url
.spec());
476 CheckStringAttribute(item
, "federation_url", form
.federation_url
.spec());
477 CheckUint32Attribute(item
, "skip_zero_click", form
.skip_zero_click
);
478 CheckUint32Attribute(item
, "generation_upload_status",
479 form
.generation_upload_status
);
480 CheckStringAttribute(item
, "application", app_string
);
481 autofill::FormData actual
;
482 DeserializeFormDataFromBase64String(
483 item
->attributes
.at("form_data").value_string
, &actual
);
484 EXPECT_TRUE(form
.form_data
.SameFormAs(actual
));
487 // Saves |credentials| and then gets logins matching |url| and |scheme|.
488 // Returns true when something is found, and in such case copies the result to
489 // |result| when |result| is not NULL. (Note that there can be max. 1 result,
490 // derived from |credentials|.)
491 bool CheckCredentialAvailability(const PasswordForm
& credentials
,
493 const PasswordForm::Scheme
& scheme
,
494 PasswordForm
* result
) {
495 NativeBackendGnome
backend(321);
498 BrowserThread::PostTask(
501 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
502 base::Unretained(&backend
),
505 PasswordForm target_form
;
506 target_form
.origin
= url
;
507 target_form
.signon_realm
= url
.spec();
508 if (scheme
!= PasswordForm::SCHEME_HTML
) {
509 // For non-HTML forms, the realm used for authentication
510 // (http://tools.ietf.org/html/rfc1945#section-10.2) is appended to the
511 // signon_realm. Just use a default value for now.
512 target_form
.signon_realm
.append("Realm");
513 target_form
.scheme
= scheme
;
515 ScopedVector
<autofill::PasswordForm
> form_list
;
516 BrowserThread::PostTask(
519 base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins
),
520 base::Unretained(&backend
),
526 EXPECT_EQ(1u, mock_keyring_items
.size());
527 if (mock_keyring_items
.size() > 0)
528 CheckMockKeyringItem(&mock_keyring_items
[0], credentials
, "chrome-321");
529 mock_keyring_items
.clear();
531 if (form_list
.empty())
533 EXPECT_EQ(1u, form_list
.size());
535 *result
= *form_list
[0];
539 // Test that updating does not use PSL matching: Add a www.facebook.com
540 // password, then use PSL matching to get a copy of it for m.facebook.com, and
541 // add that copy as well. Now update the www.facebook.com password -- the
542 // m.facebook.com password should not get updated. Depending on the argument,
543 // the credential update is done via UpdateLogin or AddLogin.
544 void CheckPSLUpdate(UpdateType update_type
) {
545 NativeBackendGnome
backend(321);
548 // Add |form_facebook_| to saved logins.
549 BrowserThread::PostTask(
552 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
553 base::Unretained(&backend
),
556 // Get the PSL-matched copy of the saved login for m.facebook.
557 const GURL
kMobileURL("http://m.facebook.com/");
558 PasswordForm m_facebook_lookup
;
559 m_facebook_lookup
.origin
= kMobileURL
;
560 m_facebook_lookup
.signon_realm
= kMobileURL
.spec();
561 ScopedVector
<autofill::PasswordForm
> form_list
;
562 BrowserThread::PostTask(
565 base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins
),
566 base::Unretained(&backend
),
570 EXPECT_EQ(1u, mock_keyring_items
.size());
571 EXPECT_EQ(1u, form_list
.size());
572 PasswordForm m_facebook
= *form_list
[0];
574 EXPECT_EQ(kMobileURL
, m_facebook
.origin
);
575 EXPECT_EQ(kMobileURL
.spec(), m_facebook
.signon_realm
);
577 // Add the PSL-matched copy to saved logins.
578 BrowserThread::PostTask(
581 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
582 base::Unretained(&backend
),
585 EXPECT_EQ(2u, mock_keyring_items
.size());
587 // Update www.facebook.com login.
588 PasswordForm
new_facebook(form_facebook_
);
589 const base::string16
kOldPassword(form_facebook_
.password_value
);
590 const base::string16
kNewPassword(UTF8ToUTF16("new_b"));
591 EXPECT_NE(kOldPassword
, kNewPassword
);
592 new_facebook
.password_value
= kNewPassword
;
593 switch (update_type
) {
594 case UPDATE_BY_UPDATELOGIN
:
595 BrowserThread::PostTask(
598 base::Bind(base::IgnoreResult(&NativeBackendGnome::UpdateLogin
),
599 base::Unretained(&backend
),
601 base::Owned(new PasswordStoreChangeList
)));
603 case UPDATE_BY_ADDLOGIN
:
604 BrowserThread::PostTask(
607 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
608 base::Unretained(&backend
),
614 EXPECT_EQ(2u, mock_keyring_items
.size());
616 // Check that m.facebook.com login was not modified by the update.
617 BrowserThread::PostTask(
620 base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins
),
621 base::Unretained(&backend
),
625 // There should be two results -- the exact one, and the PSL-matched one.
626 EXPECT_EQ(2u, form_list
.size());
627 size_t index_non_psl
= 0;
628 if (!form_list
[index_non_psl
]->original_signon_realm
.empty())
630 EXPECT_EQ(kMobileURL
, form_list
[index_non_psl
]->origin
);
631 EXPECT_EQ(kMobileURL
.spec(), form_list
[index_non_psl
]->signon_realm
);
632 EXPECT_EQ(kOldPassword
, form_list
[index_non_psl
]->password_value
);
635 // Check that www.facebook.com login was modified by the update.
636 BrowserThread::PostTask(
639 base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins
),
640 base::Unretained(&backend
),
644 // There should be two results -- the exact one, and the PSL-matched one.
645 EXPECT_EQ(2u, form_list
.size());
647 if (!form_list
[index_non_psl
]->original_signon_realm
.empty())
649 EXPECT_EQ(form_facebook_
.origin
, form_list
[index_non_psl
]->origin
);
650 EXPECT_EQ(form_facebook_
.signon_realm
,
651 form_list
[index_non_psl
]->signon_realm
);
652 EXPECT_EQ(kNewPassword
, form_list
[index_non_psl
]->password_value
);
655 void CheckMatchingWithScheme(const PasswordForm::Scheme
& scheme
) {
656 other_auth_
.scheme
= scheme
;
658 // Don't match a non-HTML form with an HTML form.
659 EXPECT_FALSE(CheckCredentialAvailability(
660 other_auth_
, GURL("http://www.example.com"),
661 PasswordForm::SCHEME_HTML
, nullptr));
662 // Don't match an HTML form with non-HTML auth form.
663 EXPECT_FALSE(CheckCredentialAvailability(
664 form_google_
, GURL("http://www.google.com/"), scheme
, nullptr));
665 // Don't match two different non-HTML auth forms with different origin.
666 EXPECT_FALSE(CheckCredentialAvailability(
667 other_auth_
, GURL("http://first.example.com"), scheme
, nullptr));
668 // Do match non-HTML forms from the same origin.
669 EXPECT_TRUE(CheckCredentialAvailability(
670 other_auth_
, GURL("http://www.example.com/"), scheme
, nullptr));
673 void CheckRemoveLoginsBetween(RemoveBetweenMethod date_to_test
) {
674 NativeBackendGnome
backend(42);
677 base::Time now
= base::Time::Now();
678 base::Time next_day
= now
+ base::TimeDelta::FromDays(1);
679 form_google_
.date_synced
= base::Time();
680 form_isc_
.date_synced
= base::Time();
681 form_google_
.date_created
= now
;
682 form_isc_
.date_created
= now
;
683 if (date_to_test
== CREATED
) {
684 form_google_
.date_created
= now
;
685 form_isc_
.date_created
= next_day
;
687 form_google_
.date_synced
= now
;
688 form_isc_
.date_synced
= next_day
;
691 BrowserThread::PostTask(
694 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
695 base::Unretained(&backend
),
697 BrowserThread::PostTask(
700 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
701 base::Unretained(&backend
),
704 PasswordStoreChangeList expected_changes
;
705 expected_changes
.push_back(
706 PasswordStoreChange(PasswordStoreChange::REMOVE
, form_google_
));
707 PasswordStoreChangeList changes
;
708 bool (NativeBackendGnome::*method
)(
709 base::Time
, base::Time
, password_manager::PasswordStoreChangeList
*) =
710 date_to_test
== CREATED
711 ? &NativeBackendGnome::RemoveLoginsCreatedBetween
712 : &NativeBackendGnome::RemoveLoginsSyncedBetween
;
713 BrowserThread::PostTaskAndReplyWithResult(
717 base::Unretained(&backend
),
722 &CheckPasswordChangesWithResult
, &expected_changes
, &changes
));
725 EXPECT_EQ(1u, mock_keyring_items
.size());
726 if (mock_keyring_items
.size() > 0)
727 CheckMockKeyringItem(&mock_keyring_items
[0], form_isc_
, "chrome-42");
730 expected_changes
.clear();
731 expected_changes
.push_back(
732 PasswordStoreChange(PasswordStoreChange::REMOVE
, form_isc_
));
733 BrowserThread::PostTaskAndReplyWithResult(
737 base::Unretained(&backend
),
742 &CheckPasswordChangesWithResult
, &expected_changes
, &changes
));
745 EXPECT_EQ(0u, mock_keyring_items
.size());
748 base::MessageLoopForUI message_loop_
;
749 content::TestBrowserThread ui_thread_
;
750 content::TestBrowserThread db_thread_
;
752 // Provide some test forms to avoid having to set them up in each test.
753 PasswordForm form_google_
;
754 PasswordForm form_facebook_
;
755 PasswordForm form_isc_
;
756 PasswordForm other_auth_
;
759 TEST_F(NativeBackendGnomeTest
, BasicAddLogin
) {
760 NativeBackendGnome
backend(42);
763 BrowserThread::PostTask(
764 BrowserThread::DB
, FROM_HERE
,
765 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
766 base::Unretained(&backend
), form_google_
));
770 EXPECT_EQ(1u, mock_keyring_items
.size());
771 if (mock_keyring_items
.size() > 0)
772 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
775 TEST_F(NativeBackendGnomeTest
, BasicListLogins
) {
776 NativeBackendGnome
backend(42);
779 BrowserThread::PostTask(
780 BrowserThread::DB
, FROM_HERE
,
781 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
782 base::Unretained(&backend
), form_google_
));
784 ScopedVector
<autofill::PasswordForm
> form_list
;
785 BrowserThread::PostTask(
786 BrowserThread::DB
, FROM_HERE
,
788 base::IgnoreResult(&NativeBackendGnome::GetAutofillableLogins
),
789 base::Unretained(&backend
), &form_list
));
793 // Quick check that we got something back.
794 EXPECT_EQ(1u, form_list
.size());
796 EXPECT_EQ(1u, mock_keyring_items
.size());
797 if (mock_keyring_items
.size() > 0)
798 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
801 // Save a password for www.facebook.com and see it suggested for m.facebook.com.
802 TEST_F(NativeBackendGnomeTest
, PSLMatchingPositive
) {
804 const GURL
kMobileURL("http://m.facebook.com/");
805 EXPECT_TRUE(CheckCredentialAvailability(
806 form_facebook_
, kMobileURL
, PasswordForm::SCHEME_HTML
, &result
));
807 EXPECT_EQ(kMobileURL
, result
.origin
);
808 EXPECT_EQ(kMobileURL
.spec(), result
.signon_realm
);
811 // Save a password for www.facebook.com and see it not suggested for
813 TEST_F(NativeBackendGnomeTest
, PSLMatchingNegativeDomainMismatch
) {
814 EXPECT_FALSE(CheckCredentialAvailability(
815 form_facebook_
, GURL("http://m-facebook.com/"),
816 PasswordForm::SCHEME_HTML
, nullptr));
819 // Test PSL matching is off for domains excluded from it.
820 TEST_F(NativeBackendGnomeTest
, PSLMatchingDisabledDomains
) {
821 EXPECT_FALSE(CheckCredentialAvailability(
822 form_google_
, GURL("http://one.google.com/"),
823 PasswordForm::SCHEME_HTML
, nullptr));
826 // Make sure PSL matches aren't available for non-HTML forms.
827 TEST_F(NativeBackendGnomeTest
, PSLMatchingDisabledForNonHTMLForms
) {
828 CheckMatchingWithScheme(PasswordForm::SCHEME_BASIC
);
829 CheckMatchingWithScheme(PasswordForm::SCHEME_DIGEST
);
830 CheckMatchingWithScheme(PasswordForm::SCHEME_OTHER
);
834 TEST_F(NativeBackendGnomeTest
, PSLUpdatingStrictUpdateLogin
) {
835 CheckPSLUpdate(UPDATE_BY_UPDATELOGIN
);
838 TEST_F(NativeBackendGnomeTest
, PSLUpdatingStrictAddLogin
) {
839 // TODO(vabr): if AddLogin becomes no longer valid for existing logins, then
840 // just delete this test.
841 CheckPSLUpdate(UPDATE_BY_ADDLOGIN
);
844 TEST_F(NativeBackendGnomeTest
, BasicUpdateLogin
) {
845 NativeBackendGnome
backend(42);
848 // First add google login.
849 BrowserThread::PostTask(
850 BrowserThread::DB
, FROM_HERE
,
851 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
852 base::Unretained(&backend
), form_google_
));
856 PasswordForm
new_form_google(form_google_
);
857 new_form_google
.times_used
= 1;
858 new_form_google
.action
= GURL("http://www.google.com/different/login");
860 EXPECT_EQ(1u, mock_keyring_items
.size());
861 if (mock_keyring_items
.size() > 0)
862 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
865 PasswordStoreChangeList changes
;
866 BrowserThread::PostTask(
867 BrowserThread::DB
, FROM_HERE
,
868 base::Bind(base::IgnoreResult(&NativeBackendGnome::UpdateLogin
),
869 base::Unretained(&backend
),
871 base::Unretained(&changes
)));
875 ASSERT_EQ(1u, changes
.size());
876 EXPECT_EQ(PasswordStoreChange::UPDATE
, changes
.front().type());
877 EXPECT_EQ(new_form_google
, changes
.front().form());
878 EXPECT_EQ(1u, mock_keyring_items
.size());
879 if (mock_keyring_items
.size() > 0)
880 CheckMockKeyringItem(&mock_keyring_items
[0], new_form_google
, "chrome-42");
883 TEST_F(NativeBackendGnomeTest
, BasicRemoveLogin
) {
884 NativeBackendGnome
backend(42);
887 BrowserThread::PostTask(
888 BrowserThread::DB
, FROM_HERE
,
889 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
890 base::Unretained(&backend
), form_google_
));
894 EXPECT_EQ(1u, mock_keyring_items
.size());
895 if (mock_keyring_items
.size() > 0)
896 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
898 BrowserThread::PostTask(
899 BrowserThread::DB
, FROM_HERE
,
900 base::Bind(base::IgnoreResult(&NativeBackendGnome::RemoveLogin
),
901 base::Unretained(&backend
), form_google_
));
905 EXPECT_EQ(0u, mock_keyring_items
.size());
908 // Verify fix for http://crbug.com/408783.
909 TEST_F(NativeBackendGnomeTest
, RemoveLoginActionMismatch
) {
910 NativeBackendGnome
backend(42);
913 BrowserThread::PostTask(
914 BrowserThread::DB
, FROM_HERE
,
915 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
916 base::Unretained(&backend
), form_google_
));
920 EXPECT_EQ(1u, mock_keyring_items
.size());
921 if (mock_keyring_items
.size() > 0)
922 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
924 // Action url match not required for removal.
925 form_google_
.action
= GURL("https://some.other.url.com/path");
927 BrowserThread::PostTask(
928 BrowserThread::DB
, FROM_HERE
,
929 base::Bind(base::IgnoreResult(&NativeBackendGnome::RemoveLogin
),
930 base::Unretained(&backend
), form_google_
));
934 EXPECT_EQ(0u, mock_keyring_items
.size());
937 TEST_F(NativeBackendGnomeTest
, RemoveNonexistentLogin
) {
938 NativeBackendGnome
backend(42);
941 // First add an unrelated login.
942 BrowserThread::PostTask(
943 BrowserThread::DB
, FROM_HERE
,
944 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
945 base::Unretained(&backend
), form_google_
));
949 EXPECT_EQ(1u, mock_keyring_items
.size());
950 if (mock_keyring_items
.size() > 0)
951 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
953 // Attempt to remove a login that doesn't exist.
954 BrowserThread::PostTask(
955 BrowserThread::DB
, FROM_HERE
,
956 base::Bind(base::IgnoreResult(&NativeBackendGnome::RemoveLogin
),
957 base::Unretained(&backend
), form_isc_
));
959 // Make sure we can still get the first form back.
960 ScopedVector
<autofill::PasswordForm
> form_list
;
961 BrowserThread::PostTask(
962 BrowserThread::DB
, FROM_HERE
,
964 base::IgnoreResult(&NativeBackendGnome::GetAutofillableLogins
),
965 base::Unretained(&backend
), &form_list
));
969 // Quick check that we got something back.
970 EXPECT_EQ(1u, form_list
.size());
972 EXPECT_EQ(1u, mock_keyring_items
.size());
973 if (mock_keyring_items
.size() > 0)
974 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
977 TEST_F(NativeBackendGnomeTest
, UpdateNonexistentLogin
) {
978 NativeBackendGnome
backend(42);
981 // First add an unrelated login.
982 BrowserThread::PostTask(
983 BrowserThread::DB
, FROM_HERE
,
984 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
985 base::Unretained(&backend
), form_google_
));
989 EXPECT_EQ(1u, mock_keyring_items
.size());
990 if (mock_keyring_items
.size() > 0)
991 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
993 // Attempt to update a login that doesn't exist.
994 PasswordStoreChangeList changes
;
995 BrowserThread::PostTask(
996 BrowserThread::DB
, FROM_HERE
,
997 base::Bind(base::IgnoreResult(&NativeBackendGnome::UpdateLogin
),
998 base::Unretained(&backend
),
1000 base::Unretained(&changes
)));
1004 EXPECT_EQ(PasswordStoreChangeList(), changes
);
1005 EXPECT_EQ(1u, mock_keyring_items
.size());
1006 if (mock_keyring_items
.size() > 0)
1007 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
1010 TEST_F(NativeBackendGnomeTest
, AddDuplicateLogin
) {
1011 NativeBackendGnome
backend(42);
1014 PasswordStoreChangeList changes
;
1015 changes
.push_back(PasswordStoreChange(PasswordStoreChange::ADD
,
1017 BrowserThread::PostTaskAndReplyWithResult(
1018 BrowserThread::DB
, FROM_HERE
,
1019 base::Bind(&NativeBackendGnome::AddLogin
,
1020 base::Unretained(&backend
), form_google_
),
1021 base::Bind(&CheckPasswordChanges
, changes
));
1024 changes
.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE
,
1026 form_google_
.times_used
++;
1027 changes
.push_back(PasswordStoreChange(PasswordStoreChange::ADD
,
1030 BrowserThread::PostTaskAndReplyWithResult(
1031 BrowserThread::DB
, FROM_HERE
,
1032 base::Bind(&NativeBackendGnome::AddLogin
,
1033 base::Unretained(&backend
), form_google_
),
1034 base::Bind(&CheckPasswordChanges
, changes
));
1038 EXPECT_EQ(1u, mock_keyring_items
.size());
1039 if (mock_keyring_items
.size() > 0)
1040 CheckMockKeyringItem(&mock_keyring_items
[0], form_google_
, "chrome-42");
1043 TEST_F(NativeBackendGnomeTest
, AndroidCredentials
) {
1044 NativeBackendGnome
backend(42);
1047 PasswordForm observed_android_form
;
1048 observed_android_form
.scheme
= PasswordForm::SCHEME_HTML
;
1049 observed_android_form
.signon_realm
=
1050 "android://7x7IDboo8u9YKraUsbmVkuf1-@net.rateflix.app/";
1051 PasswordForm saved_android_form
= observed_android_form
;
1052 saved_android_form
.username_value
= base::UTF8ToUTF16("randomusername");
1053 saved_android_form
.password_value
= base::UTF8ToUTF16("password");
1054 saved_android_form
.date_created
= base::Time::Now();
1056 BrowserThread::PostTask(
1057 BrowserThread::DB
, FROM_HERE
,
1058 base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin
),
1059 base::Unretained(&backend
), saved_android_form
));
1061 ScopedVector
<autofill::PasswordForm
> form_list
;
1062 BrowserThread::PostTask(
1063 BrowserThread::DB
, FROM_HERE
,
1064 base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins
),
1065 base::Unretained(&backend
), observed_android_form
,
1070 EXPECT_EQ(1u, form_list
.size());
1071 EXPECT_EQ(saved_android_form
, *form_list
[0]);
1074 TEST_F(NativeBackendGnomeTest
, RemoveLoginsCreatedBetween
) {
1075 CheckRemoveLoginsBetween(CREATED
);
1078 TEST_F(NativeBackendGnomeTest
, RemoveLoginsSyncedBetween
) {
1079 CheckRemoveLoginsBetween(SYNCED
);
1082 // TODO(mdm): add more basic tests here at some point.