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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
|
// 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 <stdarg.h>
#include "base/basictypes.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/password_manager/native_backend_gnome_x.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/psl_matching_helper.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using autofill::PasswordForm;
using base::UTF8ToUTF16;
using base::UTF16ToUTF8;
using content::BrowserThread;
using password_manager::PasswordStoreChange;
using password_manager::PasswordStoreChangeList;
namespace {
// What follows is a very simple implementation of the subset of the GNOME
// Keyring API that we actually use. It gets substituted for the real one by
// MockGnomeKeyringLoader, which hooks into the facility normally used to load
// the GNOME Keyring library at runtime to avoid a static dependency on it.
struct MockKeyringItem {
MockKeyringItem() {}
MockKeyringItem(const char* keyring,
const std::string& display_name,
const std::string& password)
: keyring(keyring ? keyring : "login"),
display_name(display_name),
password(password) {}
struct ItemAttribute {
ItemAttribute() : type(UINT32), value_uint32(0) {}
explicit ItemAttribute(uint32_t value)
: type(UINT32), value_uint32(value) {}
explicit ItemAttribute(const std::string& value)
: type(STRING), value_string(value) {}
bool Equals(const ItemAttribute& x) const {
if (type != x.type) return false;
return (type == STRING) ? value_string == x.value_string
: value_uint32 == x.value_uint32;
}
enum Type { UINT32, STRING } type;
uint32_t value_uint32;
std::string value_string;
};
typedef std::map<std::string, ItemAttribute> attribute_map;
typedef std::vector<std::pair<std::string, ItemAttribute> > attribute_query;
bool Matches(const attribute_query& query) const {
// The real GNOME Keyring doesn't match empty queries.
if (query.empty()) return false;
for (size_t i = 0; i < query.size(); ++i) {
attribute_map::const_iterator match = attributes.find(query[i].first);
if (match == attributes.end()) return false;
if (!match->second.Equals(query[i].second)) return false;
}
return true;
}
std::string keyring;
std::string display_name;
std::string password;
attribute_map attributes;
};
// The list of all keyring items we have stored.
std::vector<MockKeyringItem> mock_keyring_items;
bool mock_keyring_reject_local_ids = false;
bool IsStringAttribute(const GnomeKeyringPasswordSchema* schema,
const std::string& name) {
for (size_t i = 0; schema->attributes[i].name; ++i)
if (name == schema->attributes[i].name)
return schema->attributes[i].type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
NOTREACHED() << "Requested type of nonexistent attribute";
return false;
}
gboolean mock_gnome_keyring_is_available() {
return true;
}
gpointer mock_gnome_keyring_store_password(
const GnomeKeyringPasswordSchema* schema,
const gchar* keyring,
const gchar* display_name,
const gchar* password,
GnomeKeyringOperationDoneCallback callback,
gpointer data,
GDestroyNotify destroy_data,
...) {
mock_keyring_items.push_back(
MockKeyringItem(keyring, display_name, password));
MockKeyringItem* item = &mock_keyring_items.back();
const std::string keyring_desc =
keyring ? base::StringPrintf("keyring %s", keyring)
: std::string("default keyring");
VLOG(1) << "Adding item with origin " << display_name
<< " to " << keyring_desc;
va_list ap;
va_start(ap, destroy_data);
char* name;
while ((name = va_arg(ap, gchar*))) {
if (IsStringAttribute(schema, name)) {
item->attributes[name] =
MockKeyringItem::ItemAttribute(va_arg(ap, gchar*));
VLOG(1) << "Adding item attribute " << name
<< ", value '" << item->attributes[name].value_string << "'";
} else {
item->attributes[name] =
MockKeyringItem::ItemAttribute(va_arg(ap, uint32_t));
VLOG(1) << "Adding item attribute " << name
<< ", value " << item->attributes[name].value_uint32;
}
}
va_end(ap);
// As a hack to ease testing migration, make it possible to reject the new
// format for the app string. This way we can add them easily to migrate.
if (mock_keyring_reject_local_ids) {
MockKeyringItem::attribute_map::iterator it =
item->attributes.find("application");
if (it != item->attributes.end() &&
it->second.type == MockKeyringItem::ItemAttribute::STRING &&
base::StringPiece(it->second.value_string).starts_with("chrome-")) {
mock_keyring_items.pop_back();
// GnomeKeyringResult, data
callback(GNOME_KEYRING_RESULT_IO_ERROR, data);
return NULL;
}
}
// GnomeKeyringResult, data
callback(GNOME_KEYRING_RESULT_OK, data);
return NULL;
}
gpointer mock_gnome_keyring_delete_password(
const GnomeKeyringPasswordSchema* schema,
GnomeKeyringOperationDoneCallback callback,
gpointer data,
GDestroyNotify destroy_data,
...) {
MockKeyringItem::attribute_query query;
va_list ap;
va_start(ap, destroy_data);
char* name;
while ((name = va_arg(ap, gchar*))) {
if (IsStringAttribute(schema, name)) {
query.push_back(make_pair(std::string(name),
MockKeyringItem::ItemAttribute(va_arg(ap, gchar*))));
VLOG(1) << "Querying with item attribute " << name
<< ", value '" << query.back().second.value_string << "'";
} else {
query.push_back(make_pair(std::string(name),
MockKeyringItem::ItemAttribute(va_arg(ap, uint32_t))));
VLOG(1) << "Querying with item attribute " << name
<< ", value " << query.back().second.value_uint32;
}
}
va_end(ap);
bool deleted = false;
for (size_t i = mock_keyring_items.size(); i > 0; --i) {
const MockKeyringItem* item = &mock_keyring_items[i - 1];
if (item->Matches(query)) {
VLOG(1) << "Deleting item with origin " << item->display_name;
mock_keyring_items.erase(mock_keyring_items.begin() + (i - 1));
deleted = true;
}
}
// GnomeKeyringResult, data
callback(deleted ? GNOME_KEYRING_RESULT_OK
: GNOME_KEYRING_RESULT_NO_MATCH, data);
return NULL;
}
gpointer mock_gnome_keyring_find_items(
GnomeKeyringItemType type,
GnomeKeyringAttributeList* attributes,
GnomeKeyringOperationGetListCallback callback,
gpointer data,
GDestroyNotify destroy_data) {
MockKeyringItem::attribute_query query;
for (size_t i = 0; i < attributes->len; ++i) {
GnomeKeyringAttribute attribute =
g_array_index(attributes, GnomeKeyringAttribute, i);
if (attribute.type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING) {
query.push_back(
make_pair(std::string(attribute.name),
MockKeyringItem::ItemAttribute(attribute.value.string)));
VLOG(1) << "Querying with item attribute " << attribute.name
<< ", value '" << query.back().second.value_string << "'";
} else {
query.push_back(
make_pair(std::string(attribute.name),
MockKeyringItem::ItemAttribute(attribute.value.integer)));
VLOG(1) << "Querying with item attribute " << attribute.name << ", value "
<< query.back().second.value_uint32;
}
}
// Find matches and add them to a list of results.
GList* results = NULL;
for (size_t i = 0; i < mock_keyring_items.size(); ++i) {
const MockKeyringItem* item = &mock_keyring_items[i];
if (item->Matches(query)) {
GnomeKeyringFound* found = new GnomeKeyringFound;
found->keyring = strdup(item->keyring.c_str());
found->item_id = i;
found->attributes = gnome_keyring_attribute_list_new();
for (MockKeyringItem::attribute_map::const_iterator it =
item->attributes.begin();
it != item->attributes.end();
++it) {
if (it->second.type == MockKeyringItem::ItemAttribute::STRING) {
gnome_keyring_attribute_list_append_string(
found->attributes, it->first.c_str(),
it->second.value_string.c_str());
} else {
gnome_keyring_attribute_list_append_uint32(
found->attributes, it->first.c_str(),
it->second.value_uint32);
}
}
found->secret = strdup(item->password.c_str());
results = g_list_prepend(results, found);
}
}
// GnomeKeyringResult, GList*, data
callback(results ? GNOME_KEYRING_RESULT_OK
: GNOME_KEYRING_RESULT_NO_MATCH, results, data);
// Now free the list of results.
GList* element = g_list_first(results);
while (element) {
GnomeKeyringFound* found = static_cast<GnomeKeyringFound*>(element->data);
free(found->keyring);
gnome_keyring_attribute_list_free(found->attributes);
free(found->secret);
delete found;
element = g_list_next(element);
}
g_list_free(results);
return NULL;
}
const gchar* mock_gnome_keyring_result_to_message(GnomeKeyringResult res) {
return "mock keyring simulating failure";
}
// Inherit to get access to protected fields.
class MockGnomeKeyringLoader : public GnomeKeyringLoader {
public:
static bool LoadMockGnomeKeyring() {
if (!LoadGnomeKeyring())
return false;
#define GNOME_KEYRING_ASSIGN_POINTER(name) \
gnome_keyring_##name = &mock_gnome_keyring_##name;
GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(GNOME_KEYRING_ASSIGN_POINTER)
#undef GNOME_KEYRING_ASSIGN_POINTER
keyring_loaded = true;
// Reset the state of the mock library.
mock_keyring_items.clear();
mock_keyring_reject_local_ids = false;
return true;
}
};
void CheckPasswordChanges(const PasswordStoreChangeList& expected_list,
const PasswordStoreChangeList& actual_list) {
ASSERT_EQ(expected_list.size(), actual_list.size());
for (size_t i = 0; i < expected_list.size(); ++i) {
EXPECT_EQ(expected_list[i].type(), actual_list[i].type());
const PasswordForm& expected = expected_list[i].form();
const PasswordForm& actual = actual_list[i].form();
EXPECT_EQ(expected.origin, actual.origin);
EXPECT_EQ(expected.password_value, actual.password_value);
EXPECT_EQ(expected.action, actual.action);
EXPECT_EQ(expected.username_element, actual.username_element);
EXPECT_EQ(expected.username_value, actual.username_value);
EXPECT_EQ(expected.password_element, actual.password_element);
EXPECT_EQ(expected.submit_element, actual.submit_element);
EXPECT_EQ(expected.signon_realm, actual.signon_realm);
EXPECT_EQ(expected.ssl_valid, actual.ssl_valid);
EXPECT_EQ(expected.preferred, actual.preferred);
// We don't check the date created. It varies due to bug in the
// serialization. Integer seconds are saved instead of microseconds.
EXPECT_EQ(expected.blacklisted_by_user, actual.blacklisted_by_user);
EXPECT_EQ(expected.type, actual.type);
EXPECT_EQ(expected.times_used, actual.times_used);
EXPECT_EQ(expected.scheme, actual.scheme);
EXPECT_EQ(expected.date_synced, actual.date_synced);
EXPECT_EQ(expected.display_name, actual.display_name);
EXPECT_EQ(expected.avatar_url, actual.avatar_url);
EXPECT_EQ(expected.federation_url, actual.federation_url);
EXPECT_EQ(expected.is_zero_click, actual.is_zero_click);
}
}
void CheckPasswordChangesWithResult(const PasswordStoreChangeList* expected,
const PasswordStoreChangeList* actual,
bool result) {
EXPECT_TRUE(result);
CheckPasswordChanges(*expected, *actual);
}
} // anonymous namespace
class NativeBackendGnomeTest : public testing::Test {
protected:
enum UpdateType { // Used in CheckPSLUpdate().
UPDATE_BY_UPDATELOGIN,
UPDATE_BY_ADDLOGIN,
};
enum RemoveBetweenMethod { // Used in CheckRemoveLoginsBetween().
CREATED,
SYNCED,
};
NativeBackendGnomeTest()
: ui_thread_(BrowserThread::UI, &message_loop_),
db_thread_(BrowserThread::DB) {
}
void SetUp() override {
ASSERT_TRUE(db_thread_.Start());
ASSERT_TRUE(MockGnomeKeyringLoader::LoadMockGnomeKeyring());
form_google_.origin = GURL("http://www.google.com/");
form_google_.action = GURL("http://www.google.com/login");
form_google_.username_element = UTF8ToUTF16("user");
form_google_.username_value = UTF8ToUTF16("joeschmoe");
form_google_.password_element = UTF8ToUTF16("pass");
form_google_.password_value = UTF8ToUTF16("seekrit");
form_google_.submit_element = UTF8ToUTF16("submit");
form_google_.signon_realm = "http://www.google.com/";
form_google_.type = PasswordForm::TYPE_GENERATED;
form_google_.date_created = base::Time::Now();
form_google_.date_synced = base::Time::Now();
form_google_.display_name = UTF8ToUTF16("Joe Schmoe");
form_google_.avatar_url = GURL("http://www.google.com/avatar");
form_google_.federation_url = GURL("http://www.google.com/federation_url");
form_google_.is_zero_click = true;
form_facebook_.origin = GURL("http://www.facebook.com/");
form_facebook_.action = GURL("http://www.facebook.com/login");
form_facebook_.username_element = UTF8ToUTF16("user");
form_facebook_.username_value = UTF8ToUTF16("a");
form_facebook_.password_element = UTF8ToUTF16("password");
form_facebook_.password_value = UTF8ToUTF16("b");
form_facebook_.submit_element = UTF8ToUTF16("submit");
form_facebook_.signon_realm = "http://www.facebook.com/";
form_facebook_.date_created = base::Time::Now();
form_facebook_.date_synced = base::Time::Now();
form_facebook_.display_name = UTF8ToUTF16("Joe Schmoe");
form_facebook_.avatar_url = GURL("http://www.facebook.com/avatar");
form_facebook_.federation_url = GURL("http://www.facebook.com/federation");
form_facebook_.is_zero_click = true;
form_isc_.origin = GURL("http://www.isc.org/");
form_isc_.action = GURL("http://www.isc.org/auth");
form_isc_.username_element = UTF8ToUTF16("id");
form_isc_.username_value = UTF8ToUTF16("janedoe");
form_isc_.password_element = UTF8ToUTF16("passwd");
form_isc_.password_value = UTF8ToUTF16("ihazabukkit");
form_isc_.submit_element = UTF8ToUTF16("login");
form_isc_.signon_realm = "http://www.isc.org/";
form_isc_.date_created = base::Time::Now();
form_isc_.date_synced = base::Time::Now();
other_auth_.origin = GURL("http://www.example.com/");
other_auth_.username_value = UTF8ToUTF16("username");
other_auth_.password_value = UTF8ToUTF16("pass");
other_auth_.signon_realm = "http://www.example.com/Realm";
other_auth_.date_created = base::Time::Now();
other_auth_.date_synced = base::Time::Now();
}
void TearDown() override {
base::MessageLoop::current()->PostTask(FROM_HERE,
base::MessageLoop::QuitClosure());
base::MessageLoop::current()->Run();
db_thread_.Stop();
}
void RunBothThreads() {
// First we post a message to the DB thread that will run after all other
// messages that have been posted to the DB thread (we don't expect more
// to be posted), which posts a message to the UI thread to quit the loop.
// That way we can run both loops and be sure that the UI thread loop will
// quit so we can get on with the rest of the test.
BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
base::Bind(&PostQuitTask, &message_loop_));
base::MessageLoop::current()->Run();
}
static void PostQuitTask(base::MessageLoop* loop) {
loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
}
void CheckUint32Attribute(const MockKeyringItem* item,
const std::string& attribute,
uint32_t value) {
MockKeyringItem::attribute_map::const_iterator it =
item->attributes.find(attribute);
EXPECT_NE(item->attributes.end(), it);
if (it != item->attributes.end()) {
EXPECT_EQ(MockKeyringItem::ItemAttribute::UINT32, it->second.type);
EXPECT_EQ(value, it->second.value_uint32);
}
}
void CheckStringAttribute(const MockKeyringItem* item,
const std::string& attribute,
const std::string& value) {
MockKeyringItem::attribute_map::const_iterator it =
item->attributes.find(attribute);
EXPECT_NE(item->attributes.end(), it);
if (it != item->attributes.end()) {
EXPECT_EQ(MockKeyringItem::ItemAttribute::STRING, it->second.type);
EXPECT_EQ(value, it->second.value_string);
}
}
void CheckMockKeyringItem(const MockKeyringItem* item,
const PasswordForm& form,
const std::string& app_string) {
// We always add items to the login keyring.
EXPECT_EQ("login", item->keyring);
EXPECT_EQ(form.origin.spec(), item->display_name);
EXPECT_EQ(UTF16ToUTF8(form.password_value), item->password);
EXPECT_EQ(20u, item->attributes.size());
CheckStringAttribute(item, "origin_url", form.origin.spec());
CheckStringAttribute(item, "action_url", form.action.spec());
CheckStringAttribute(item, "username_element",
UTF16ToUTF8(form.username_element));
CheckStringAttribute(item, "username_value",
UTF16ToUTF8(form.username_value));
CheckStringAttribute(item, "password_element",
UTF16ToUTF8(form.password_element));
CheckStringAttribute(item, "submit_element",
UTF16ToUTF8(form.submit_element));
CheckStringAttribute(item, "signon_realm", form.signon_realm);
CheckUint32Attribute(item, "ssl_valid", form.ssl_valid);
CheckUint32Attribute(item, "preferred", form.preferred);
// We don't check the date created. It varies.
CheckUint32Attribute(item, "blacklisted_by_user", form.blacklisted_by_user);
CheckUint32Attribute(item, "type", form.type);
CheckUint32Attribute(item, "times_used", form.times_used);
CheckUint32Attribute(item, "scheme", form.scheme);
CheckStringAttribute(item, "date_synced", base::Int64ToString(
form.date_synced.ToInternalValue()));
CheckStringAttribute(item, "display_name", UTF16ToUTF8(form.display_name));
CheckStringAttribute(item, "avatar_url", form.avatar_url.spec());
CheckStringAttribute(item, "federation_url", form.federation_url.spec());
CheckUint32Attribute(item, "is_zero_click", form.is_zero_click);
CheckStringAttribute(item, "application", app_string);
}
// Saves |credentials| and then gets logins matching |url| and |scheme|.
// Returns true when something is found, and in such case copies the result to
// |result| when |result| is not NULL. (Note that there can be max. 1 result,
// derived from |credentials|.)
bool CheckCredentialAvailability(const PasswordForm& credentials,
const GURL& url,
const PasswordForm::Scheme& scheme,
PasswordForm* result) {
NativeBackendGnome backend(321);
backend.Init();
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend),
credentials));
PasswordForm target_form;
target_form.origin = url;
target_form.signon_realm = url.spec();
if (scheme != PasswordForm::SCHEME_HTML) {
// For non-HTML forms, the realm used for authentication
// (http://tools.ietf.org/html/rfc1945#section-10.2) is appended to the
// signon_realm. Just use a default value for now.
target_form.signon_realm.append("Realm");
target_form.scheme = scheme;
}
std::vector<PasswordForm*> form_list;
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins),
base::Unretained(&backend),
target_form,
&form_list));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], credentials, "chrome-321");
mock_keyring_items.clear();
if (form_list.empty())
return false;
EXPECT_EQ(1u, form_list.size());
if (result)
*result = *form_list[0];
STLDeleteElements(&form_list);
return true;
}
// Test that updating does not use PSL matching: Add a www.facebook.com
// password, then use PSL matching to get a copy of it for m.facebook.com, and
// add that copy as well. Now update the www.facebook.com password -- the
// m.facebook.com password should not get updated. Depending on the argument,
// the credential update is done via UpdateLogin or AddLogin.
void CheckPSLUpdate(UpdateType update_type) {
NativeBackendGnome backend(321);
backend.Init();
// Add |form_facebook_| to saved logins.
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend),
form_facebook_));
// Get the PSL-matched copy of the saved login for m.facebook.
const GURL kMobileURL("http://m.facebook.com/");
PasswordForm m_facebook_lookup;
m_facebook_lookup.origin = kMobileURL;
m_facebook_lookup.signon_realm = kMobileURL.spec();
std::vector<PasswordForm*> form_list;
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins),
base::Unretained(&backend),
m_facebook_lookup,
&form_list));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
EXPECT_EQ(1u, form_list.size());
PasswordForm m_facebook = *form_list[0];
STLDeleteElements(&form_list);
EXPECT_EQ(kMobileURL, m_facebook.origin);
EXPECT_EQ(kMobileURL.spec(), m_facebook.signon_realm);
// Add the PSL-matched copy to saved logins.
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend),
m_facebook));
RunBothThreads();
EXPECT_EQ(2u, mock_keyring_items.size());
// Update www.facebook.com login.
PasswordForm new_facebook(form_facebook_);
const base::string16 kOldPassword(form_facebook_.password_value);
const base::string16 kNewPassword(UTF8ToUTF16("new_b"));
EXPECT_NE(kOldPassword, kNewPassword);
new_facebook.password_value = kNewPassword;
switch (update_type) {
case UPDATE_BY_UPDATELOGIN:
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::UpdateLogin),
base::Unretained(&backend),
new_facebook,
base::Owned(new PasswordStoreChangeList)));
break;
case UPDATE_BY_ADDLOGIN:
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend),
new_facebook));
break;
}
RunBothThreads();
EXPECT_EQ(2u, mock_keyring_items.size());
// Check that m.facebook.com login was not modified by the update.
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins),
base::Unretained(&backend),
m_facebook_lookup,
&form_list));
RunBothThreads();
// There should be two results -- the exact one, and the PSL-matched one.
EXPECT_EQ(2u, form_list.size());
size_t index_non_psl = 0;
if (!form_list[index_non_psl]->original_signon_realm.empty())
index_non_psl = 1;
EXPECT_EQ(kMobileURL, form_list[index_non_psl]->origin);
EXPECT_EQ(kMobileURL.spec(), form_list[index_non_psl]->signon_realm);
EXPECT_EQ(kOldPassword, form_list[index_non_psl]->password_value);
STLDeleteElements(&form_list);
// Check that www.facebook.com login was modified by the update.
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins),
base::Unretained(&backend),
form_facebook_,
&form_list));
RunBothThreads();
// There should be two results -- the exact one, and the PSL-matched one.
EXPECT_EQ(2u, form_list.size());
index_non_psl = 0;
if (!form_list[index_non_psl]->original_signon_realm.empty())
index_non_psl = 1;
EXPECT_EQ(form_facebook_.origin, form_list[index_non_psl]->origin);
EXPECT_EQ(form_facebook_.signon_realm,
form_list[index_non_psl]->signon_realm);
EXPECT_EQ(kNewPassword, form_list[index_non_psl]->password_value);
STLDeleteElements(&form_list);
}
void CheckMatchingWithScheme(const PasswordForm::Scheme& scheme) {
other_auth_.scheme = scheme;
// Don't match a non-HTML form with an HTML form.
EXPECT_FALSE(CheckCredentialAvailability(
other_auth_, GURL("http://www.example.com"),
PasswordForm::SCHEME_HTML, NULL));
// Don't match an HTML form with non-HTML auth form.
EXPECT_FALSE(CheckCredentialAvailability(
form_google_, GURL("http://www.google.com/"), scheme, NULL));
// Don't match two different non-HTML auth forms with different origin.
EXPECT_FALSE(CheckCredentialAvailability(
other_auth_, GURL("http://first.example.com"), scheme, NULL));
// Do match non-HTML forms from the same origin.
EXPECT_TRUE(CheckCredentialAvailability(
other_auth_, GURL("http://www.example.com/"), scheme, NULL));
}
void CheckRemoveLoginsBetween(RemoveBetweenMethod date_to_test) {
NativeBackendGnome backend(42);
backend.Init();
form_google_.date_synced = base::Time();
form_isc_.date_synced = base::Time();
form_google_.date_created = base::Time();
form_isc_.date_created = base::Time();
base::Time now = base::Time::Now();
base::Time next_day = now + base::TimeDelta::FromDays(1);
if (date_to_test == CREATED) {
// crbug/374132. Remove the next line once it's fixed.
next_day = base::Time::FromTimeT(next_day.ToTimeT());
form_google_.date_created = now;
form_isc_.date_created = next_day;
} else {
form_google_.date_synced = now;
form_isc_.date_synced = next_day;
}
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend),
form_google_));
BrowserThread::PostTask(
BrowserThread::DB,
FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend),
form_isc_));
PasswordStoreChangeList expected_changes;
expected_changes.push_back(
PasswordStoreChange(PasswordStoreChange::REMOVE, form_google_));
PasswordStoreChangeList changes;
bool (NativeBackendGnome::*method)(
base::Time, base::Time, password_manager::PasswordStoreChangeList*) =
date_to_test == CREATED
? &NativeBackendGnome::RemoveLoginsCreatedBetween
: &NativeBackendGnome::RemoveLoginsSyncedBetween;
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::DB,
FROM_HERE,
base::Bind(method,
base::Unretained(&backend),
base::Time(),
next_day,
&changes),
base::Bind(
&CheckPasswordChangesWithResult, &expected_changes, &changes));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_isc_, "chrome-42");
// Remove form_isc_.
expected_changes.clear();
expected_changes.push_back(
PasswordStoreChange(PasswordStoreChange::REMOVE, form_isc_));
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::DB,
FROM_HERE,
base::Bind(method,
base::Unretained(&backend),
next_day,
base::Time(),
&changes),
base::Bind(
&CheckPasswordChangesWithResult, &expected_changes, &changes));
RunBothThreads();
EXPECT_EQ(0u, mock_keyring_items.size());
}
base::MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread db_thread_;
// Provide some test forms to avoid having to set them up in each test.
PasswordForm form_google_;
PasswordForm form_facebook_;
PasswordForm form_isc_;
PasswordForm other_auth_;
};
TEST_F(NativeBackendGnomeTest, BasicAddLogin) {
NativeBackendGnome backend(42);
backend.Init();
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
}
TEST_F(NativeBackendGnomeTest, BasicListLogins) {
NativeBackendGnome backend(42);
backend.Init();
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult( &NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
std::vector<PasswordForm*> form_list;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(
base::IgnoreResult(&NativeBackendGnome::GetAutofillableLogins),
base::Unretained(&backend), &form_list));
RunBothThreads();
// Quick check that we got something back.
EXPECT_EQ(1u, form_list.size());
STLDeleteElements(&form_list);
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
}
// Save a password for www.facebook.com and see it suggested for m.facebook.com.
TEST_F(NativeBackendGnomeTest, PSLMatchingPositive) {
PasswordForm result;
const GURL kMobileURL("http://m.facebook.com/");
EXPECT_TRUE(CheckCredentialAvailability(
form_facebook_, kMobileURL, PasswordForm::SCHEME_HTML, &result));
EXPECT_EQ(kMobileURL, result.origin);
EXPECT_EQ(kMobileURL.spec(), result.signon_realm);
}
// Save a password for www.facebook.com and see it not suggested for
// m-facebook.com.
TEST_F(NativeBackendGnomeTest, PSLMatchingNegativeDomainMismatch) {
EXPECT_FALSE(CheckCredentialAvailability(
form_facebook_, GURL("http://m-facebook.com/"),
PasswordForm::SCHEME_HTML, NULL));
}
// Test PSL matching is off for domains excluded from it.
TEST_F(NativeBackendGnomeTest, PSLMatchingDisabledDomains) {
EXPECT_FALSE(CheckCredentialAvailability(
form_google_, GURL("http://one.google.com/"),
PasswordForm::SCHEME_HTML, NULL));
}
// Make sure PSL matches aren't available for non-HTML forms.
TEST_F(NativeBackendGnomeTest, PSLMatchingDisabledForNonHTMLForms) {
CheckMatchingWithScheme(PasswordForm::SCHEME_BASIC);
CheckMatchingWithScheme(PasswordForm::SCHEME_DIGEST);
CheckMatchingWithScheme(PasswordForm::SCHEME_OTHER);
}
TEST_F(NativeBackendGnomeTest, PSLUpdatingStrictUpdateLogin) {
CheckPSLUpdate(UPDATE_BY_UPDATELOGIN);
}
TEST_F(NativeBackendGnomeTest, PSLUpdatingStrictAddLogin) {
// TODO(vabr): if AddLogin becomes no longer valid for existing logins, then
// just delete this test.
CheckPSLUpdate(UPDATE_BY_ADDLOGIN);
}
TEST_F(NativeBackendGnomeTest, BasicUpdateLogin) {
NativeBackendGnome backend(42);
backend.Init();
// First add google login.
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
PasswordForm new_form_google(form_google_);
new_form_google.times_used = 1;
new_form_google.action = GURL("http://www.google.com/different/login");
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
// Update login
PasswordStoreChangeList changes;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::UpdateLogin),
base::Unretained(&backend),
new_form_google,
base::Unretained(&changes)));
RunBothThreads();
ASSERT_EQ(1u, changes.size());
EXPECT_EQ(PasswordStoreChange::UPDATE, changes.front().type());
EXPECT_EQ(new_form_google, changes.front().form());
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], new_form_google, "chrome-42");
}
TEST_F(NativeBackendGnomeTest, BasicRemoveLogin) {
NativeBackendGnome backend(42);
backend.Init();
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::RemoveLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
EXPECT_EQ(0u, mock_keyring_items.size());
}
// Verify fix for http://crbug.com/408783.
TEST_F(NativeBackendGnomeTest, RemoveLoginActionMismatch) {
NativeBackendGnome backend(42);
backend.Init();
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
// Action url match not required for removal.
form_google_.action = GURL("https://some.other.url.com/path");
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::RemoveLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
EXPECT_EQ(0u, mock_keyring_items.size());
}
TEST_F(NativeBackendGnomeTest, RemoveNonexistentLogin) {
NativeBackendGnome backend(42);
backend.Init();
// First add an unrelated login.
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
// Attempt to remove a login that doesn't exist.
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::RemoveLogin),
base::Unretained(&backend), form_isc_));
// Make sure we can still get the first form back.
std::vector<PasswordForm*> form_list;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(
base::IgnoreResult(&NativeBackendGnome::GetAutofillableLogins),
base::Unretained(&backend), &form_list));
RunBothThreads();
// Quick check that we got something back.
EXPECT_EQ(1u, form_list.size());
STLDeleteElements(&form_list);
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
}
TEST_F(NativeBackendGnomeTest, UpdateNonexistentLogin) {
NativeBackendGnome backend(42);
backend.Init();
// First add an unrelated login.
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
// Attempt to update a login that doesn't exist.
PasswordStoreChangeList changes;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::UpdateLogin),
base::Unretained(&backend),
form_isc_,
base::Unretained(&changes)));
RunBothThreads();
EXPECT_EQ(PasswordStoreChangeList(), changes);
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
}
TEST_F(NativeBackendGnomeTest, AddDuplicateLogin) {
NativeBackendGnome backend(42);
backend.Init();
PasswordStoreChangeList changes;
changes.push_back(PasswordStoreChange(PasswordStoreChange::ADD,
form_google_));
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::DB, FROM_HERE,
base::Bind(&NativeBackendGnome::AddLogin,
base::Unretained(&backend), form_google_),
base::Bind(&CheckPasswordChanges, changes));
changes.clear();
changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE,
form_google_));
form_google_.times_used++;
changes.push_back(PasswordStoreChange(PasswordStoreChange::ADD,
form_google_));
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::DB, FROM_HERE,
base::Bind(&NativeBackendGnome::AddLogin,
base::Unretained(&backend), form_google_),
base::Bind(&CheckPasswordChanges, changes));
RunBothThreads();
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
}
TEST_F(NativeBackendGnomeTest, ListLoginsAppends) {
NativeBackendGnome backend(42);
backend.Init();
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), form_google_));
// Send the same request twice with the same list both times.
std::vector<PasswordForm*> form_list;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(
base::IgnoreResult(&NativeBackendGnome::GetAutofillableLogins),
base::Unretained(&backend), &form_list));
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(
base::IgnoreResult(&NativeBackendGnome::GetAutofillableLogins),
base::Unretained(&backend), &form_list));
RunBothThreads();
// Quick check that we got two results back.
EXPECT_EQ(2u, form_list.size());
STLDeleteElements(&form_list);
EXPECT_EQ(1u, mock_keyring_items.size());
if (mock_keyring_items.size() > 0)
CheckMockKeyringItem(&mock_keyring_items[0], form_google_, "chrome-42");
}
TEST_F(NativeBackendGnomeTest, AndroidCredentials) {
NativeBackendGnome backend(42);
backend.Init();
PasswordForm observed_android_form;
observed_android_form.scheme = PasswordForm::SCHEME_HTML;
observed_android_form.signon_realm =
"android://7x7IDboo8u9YKraUsbmVkuf1-@net.rateflix.app/";
PasswordForm saved_android_form = observed_android_form;
saved_android_form.username_value = base::UTF8ToUTF16("randomusername");
saved_android_form.password_value = base::UTF8ToUTF16("password");
saved_android_form.date_created = base::Time::FromTimeT(1);
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::AddLogin),
base::Unretained(&backend), saved_android_form));
std::vector<PasswordForm*> form_list;
BrowserThread::PostTask(
BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&NativeBackendGnome::GetLogins),
base::Unretained(&backend), observed_android_form,
&form_list));
RunBothThreads();
EXPECT_EQ(1u, form_list.size());
EXPECT_EQ(saved_android_form, *form_list[0]);
}
TEST_F(NativeBackendGnomeTest, RemoveLoginsCreatedBetween) {
CheckRemoveLoginsBetween(CREATED);
}
TEST_F(NativeBackendGnomeTest, RemoveLoginsSyncedBetween) {
CheckRemoveLoginsBetween(SYNCED);
}
// TODO(mdm): add more basic tests here at some point.
|