diff options
author | stuartmorgan@chromium.org <stuartmorgan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-24 15:54:38 +0000 |
---|---|---|
committer | stuartmorgan@chromium.org <stuartmorgan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-24 15:54:38 +0000 |
commit | 734d9be68c10a2d85336baaf3f21d21520ea9f5c (patch) | |
tree | 684b5199392bcaef2dc65f60ee7d84749f01ddff /chrome/browser | |
parent | ff579d4a9e7af6d7c8dea51b9feedcacb7fd2a38 (diff) | |
download | chromium_src-734d9be68c10a2d85336baaf3f21d21520ea9f5c.zip chromium_src-734d9be68c10a2d85336baaf3f21d21520ea9f5c.tar.gz chromium_src-734d9be68c10a2d85336baaf3f21d21520ea9f5c.tar.bz2 |
Separate the Keychain test data from the Mock itself, and make the mock data creation table-driven.
Replaces the const-ness of helper methods with casting away constness when necessary, so that the new public AddTestItem function isn't const as well.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/147056
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19124 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/keychain_mock_mac.cc | 165 | ||||
-rw-r--r-- | chrome/browser/keychain_mock_mac.h | 49 | ||||
-rw-r--r-- | chrome/browser/password_manager/password_store_mac_unittest.cc | 202 |
3 files changed, 195 insertions, 221 deletions
diff --git a/chrome/browser/keychain_mock_mac.cc b/chrome/browser/keychain_mock_mac.cc index 858fdb6..ef74b52 100644 --- a/chrome/browser/keychain_mock_mac.cc +++ b/chrome/browser/keychain_mock_mac.cc @@ -6,9 +6,9 @@ #include "base/time.h" #include "chrome/browser/keychain_mock_mac.h" -MockKeychain::MockKeychain() - : search_copy_count_(0), keychain_item_copy_count_(0), - attribute_data_copy_count_(0) { +MockKeychain::MockKeychain(unsigned int item_capacity) + : item_capacity_(item_capacity), item_count_(0), search_copy_count_(0), + keychain_item_copy_count_(0), attribute_data_copy_count_(0) { UInt32 tags[] = { kSecAccountItemAttr, kSecServerItemAttr, kSecPortItemAttr, @@ -19,9 +19,7 @@ MockKeychain::MockKeychain() kSecCreationDateItemAttr, kSecNegativeItemAttr }; - // Create the test keychain data to return from ItemCopyAttributesAndData, - // and set up everything that's consistent across all the items. - item_capacity_ = 9; + // Create the test keychain data storage. keychain_attr_list_ = static_cast<SecKeychainAttributeList*>( calloc(item_capacity_, sizeof(SecKeychainAttributeList))); keychain_data_ = static_cast<KeychainPasswordData*>( @@ -53,94 +51,6 @@ MockKeychain::MockKeychain() } } } - - // Save one slot for use by AddInternetPassword. - unsigned int available_slots = item_capacity_ - 1; - item_count_ = 0; - - // Basic HTML form. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecAccountItemAttr, "joe_user"); - SetTestDataString(item_count_, kSecServerItemAttr, "some.domain.com"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTP); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTMLForm); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "20020601171500Z"); - SetTestDataPasswordString(item_count_, "sekrit"); - ++item_count_; - - // HTML form with path. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecAccountItemAttr, "joe_user"); - SetTestDataString(item_count_, kSecServerItemAttr, "some.domain.com"); - SetTestDataString(item_count_, kSecPathItemAttr, "/insecure.html"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTP); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTMLForm); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "19991231235959Z"); - SetTestDataPasswordString(item_count_, "sekrit"); - ++item_count_; - - // Secure HTML form with path. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecAccountItemAttr, "secure_user"); - SetTestDataString(item_count_, kSecServerItemAttr, "some.domain.com"); - SetTestDataString(item_count_, kSecPathItemAttr, "/secure.html"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTPS); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTMLForm); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "20100908070605Z"); - SetTestDataPasswordString(item_count_, "password"); - ++item_count_; - - // True negative item. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecServerItemAttr, "dont.remember.com"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTP); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTMLForm); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "20000101000000Z"); - SetTestDataNegativeItem(item_count_, true); - ++item_count_; - - // De-facto negative item, type one. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecAccountItemAttr, "Password Not Stored"); - SetTestDataString(item_count_, kSecServerItemAttr, "dont.remember.com"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTP); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTMLForm); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "20000101000000Z"); - SetTestDataPasswordString(item_count_, ""); - ++item_count_; - - // De-facto negative item, type two. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecServerItemAttr, "dont.remember.com"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTPS); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTMLForm); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "20000101000000Z"); - SetTestDataPasswordString(item_count_, " "); - ++item_count_; - - // HTTP auth basic, with port and path. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecAccountItemAttr, "basic_auth_user"); - SetTestDataString(item_count_, kSecServerItemAttr, "some.domain.com"); - SetTestDataString(item_count_, kSecSecurityDomainItemAttr, "low_security"); - SetTestDataString(item_count_, kSecPathItemAttr, "/insecure.html"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTP); - SetTestDataPort(item_count_, 4567); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTTPBasic); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "19980330100000Z"); - SetTestDataPasswordString(item_count_, "basic"); - ++item_count_; - - // HTTP auth digest, secure. - CHECK(item_count_ < available_slots); - SetTestDataString(item_count_, kSecAccountItemAttr, "digest_auth_user"); - SetTestDataString(item_count_, kSecServerItemAttr, "some.domain.com"); - SetTestDataString(item_count_, kSecSecurityDomainItemAttr, "high_security"); - SetTestDataProtocol(item_count_, kSecProtocolTypeHTTPS); - SetTestDataAuthType(item_count_, kSecAuthenticationTypeHTTPDigest); - SetTestDataString(item_count_, kSecCreationDateItemAttr, "19980330100000Z"); - SetTestDataPasswordString(item_count_, "digest"); - ++item_count_; } MockKeychain::~MockKeychain() { @@ -171,7 +81,7 @@ int MockKeychain::IndexForTag(const SecKeychainAttributeList& attribute_list, } void MockKeychain::SetTestDataBytes(int item, UInt32 tag, const void* data, - size_t length) const { + size_t length) { int attribute_index = IndexForTag(keychain_attr_list_[item], tag); keychain_attr_list_[item].attr[attribute_index].length = length; if (length > 0) { @@ -186,34 +96,32 @@ void MockKeychain::SetTestDataBytes(int item, UInt32 tag, const void* data, } } -void MockKeychain::SetTestDataString(int item, UInt32 tag, - const char* value) const { +void MockKeychain::SetTestDataString(int item, UInt32 tag, const char* value) { SetTestDataBytes(item, tag, value, value ? strlen(value) : 0); } -void MockKeychain::SetTestDataPort(int item, UInt32 value) const { +void MockKeychain::SetTestDataPort(int item, UInt32 value) { int attribute_index = IndexForTag(keychain_attr_list_[item], kSecPortItemAttr); void* data = keychain_attr_list_[item].attr[attribute_index].data; *(static_cast<UInt32*>(data)) = value; } -void MockKeychain::SetTestDataProtocol(int item, SecProtocolType value) const { +void MockKeychain::SetTestDataProtocol(int item, SecProtocolType value) { int attribute_index = IndexForTag(keychain_attr_list_[item], kSecProtocolItemAttr); void* data = keychain_attr_list_[item].attr[attribute_index].data; *(static_cast<SecProtocolType*>(data)) = value; } -void MockKeychain::SetTestDataAuthType(int item, - SecAuthenticationType value) const { +void MockKeychain::SetTestDataAuthType(int item, SecAuthenticationType value) { int attribute_index = IndexForTag(keychain_attr_list_[item], kSecAuthenticationTypeItemAttr); void* data = keychain_attr_list_[item].attr[attribute_index].data; *(static_cast<SecAuthenticationType*>(data)) = value; } -void MockKeychain::SetTestDataNegativeItem(int item, Boolean value) const { +void MockKeychain::SetTestDataNegativeItem(int item, Boolean value) { int attribute_index = IndexForTag(keychain_attr_list_[item], kSecNegativeItemAttr); void* data = keychain_attr_list_[item].attr[attribute_index].data; @@ -221,7 +129,7 @@ void MockKeychain::SetTestDataNegativeItem(int item, Boolean value) const { } void MockKeychain::SetTestDataPasswordBytes(int item, const void* data, - size_t length) const { + size_t length) { keychain_data_[item].length = length; if (length > 0) { if (keychain_data_[item].data) { @@ -234,8 +142,7 @@ void MockKeychain::SetTestDataPasswordBytes(int item, const void* data, } } -void MockKeychain::SetTestDataPasswordString(int item, - const char* value) const { +void MockKeychain::SetTestDataPasswordString(int item, const char* value) { SetTestDataPasswordBytes(item, value, value ? strlen(value) : 0); } @@ -282,7 +189,8 @@ OSStatus MockKeychain::ItemModifyAttributesAndData( NOTIMPLEMENTED(); } if (data) { - SetTestDataPasswordBytes(item_index, data, length); + MockKeychain* mutable_this = const_cast<MockKeychain*>(this); + mutable_this->SetTestDataPasswordBytes(item_index, data, length); } return noErr; } @@ -346,24 +254,28 @@ OSStatus MockKeychain::AddInternetPassword( int target_item = (item_count_ == item_capacity_) ? item_capacity_ - 1 : item_count_++; - SetTestDataBytes(target_item, kSecServerItemAttr, serverName, - serverNameLength); - SetTestDataBytes(target_item, kSecSecurityDomainItemAttr, securityDomain, - securityDomainLength); - SetTestDataBytes(target_item, kSecAccountItemAttr, accountName, - accountNameLength); - SetTestDataBytes(target_item, kSecPathItemAttr, path, pathLength); - SetTestDataPort(target_item, port); - SetTestDataProtocol(target_item, protocol); - SetTestDataAuthType(target_item, authenticationType); - SetTestDataPasswordBytes(target_item, passwordData, passwordLength); + MockKeychain* mutable_this = const_cast<MockKeychain*>(this); + mutable_this->SetTestDataBytes(target_item, kSecServerItemAttr, serverName, + serverNameLength); + mutable_this->SetTestDataBytes(target_item, kSecSecurityDomainItemAttr, + securityDomain, securityDomainLength); + mutable_this->SetTestDataBytes(target_item, kSecAccountItemAttr, accountName, + accountNameLength); + mutable_this->SetTestDataBytes(target_item, kSecPathItemAttr, path, + pathLength); + mutable_this->SetTestDataPort(target_item, port); + mutable_this->SetTestDataProtocol(target_item, protocol); + mutable_this->SetTestDataAuthType(target_item, authenticationType); + mutable_this->SetTestDataPasswordBytes(target_item, passwordData, + passwordLength); base::Time::Exploded exploded_time; base::Time::Now().UTCExplode(&exploded_time); char time_string[128]; snprintf(time_string, sizeof(time_string), "%04d%02d%02d%02d%02d%02dZ", exploded_time.year, exploded_time.month, exploded_time.day_of_month, exploded_time.hour, exploded_time.minute, exploded_time.second); - SetTestDataString(target_item, kSecCreationDateItemAttr, time_string); + mutable_this->SetTestDataString(target_item, kSecCreationDateItemAttr, + time_string); if (itemRef) { *itemRef = reinterpret_cast<SecKeychainItemRef>(target_item + 1); @@ -406,3 +318,20 @@ int MockKeychain::UnfreedKeychainItemCount() const { int MockKeychain::UnfreedAttributeDataCount() const { return attribute_data_copy_count_; } + +void MockKeychain::AddTestItem(const KeychainTestData& item_data) { + unsigned int index = item_count_++; + CHECK(index < item_capacity_); + + SetTestDataAuthType(index, item_data.auth_type); + SetTestDataString(index, kSecServerItemAttr, item_data.server); + SetTestDataProtocol(index, item_data.protocol); + SetTestDataString(index, kSecPathItemAttr, item_data.path); + SetTestDataPort(index, item_data.port); + SetTestDataString(index, kSecSecurityDomainItemAttr, + item_data.security_domain); + SetTestDataString(index, kSecCreationDateItemAttr, item_data.creation_date); + SetTestDataString(index, kSecAccountItemAttr, item_data.username); + SetTestDataPasswordString(index, item_data.password); + SetTestDataNegativeItem(index, item_data.negative_item); +} diff --git a/chrome/browser/keychain_mock_mac.h b/chrome/browser/keychain_mock_mac.h index 1a98a3a..48b3bf8 100644 --- a/chrome/browser/keychain_mock_mac.h +++ b/chrome/browser/keychain_mock_mac.h @@ -14,12 +14,13 @@ // SecKeychainItemRef values are just indexes into that array (offset by 1 to // prevent problems with clients that null-check refs), cast to pointers. // -// TODO(stuartmorgan): The test data is currently hard-coded; instead it should -// be settable by tests so that there isn't a strong coupling between the mock -// implementation and the tests. +// Note that "const" is pretty much meaningless for this class; the const-ness +// of MacKeychain doesn't apply to the actual keychain data, so all of the Mock +// data is mutable; don't assume that it won't change over the life of tests. class MockKeychain : public MacKeychain { public: - MockKeychain(); + // Create a Mock Keychain capable of holding item_capacity keychain items. + explicit MockKeychain(unsigned int item_capacity); virtual ~MockKeychain(); virtual OSStatus ItemCopyAttributesAndData( SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, @@ -37,6 +38,8 @@ class MockKeychain : public MacKeychain { SecKeychainSearchRef *searchRef) const; virtual OSStatus SearchCopyNext(SecKeychainSearchRef searchRef, SecKeychainItemRef *itemRef) const; + // If there are unused slots in the Mock Keychain's capacity, the new item + // will use the first free one, otherwise it will stomp the last item. // Pass "some.domain.com" as the serverName to get errSecDuplicateItem. virtual OSStatus AddInternetPassword(SecKeychainRef keychain, UInt32 serverNameLength, @@ -59,33 +62,41 @@ class MockKeychain : public MacKeychain { int UnfreedKeychainItemCount() const; int UnfreedAttributeDataCount() const; - private: - // Note that "const" is pretty much meaningless for this class; the const-ness - // of MacKeychain doesn't apply to the actual keychain data, so all of our - // data is mutable. These helpers are all const because they are needed by - // some of the const API functions we are mocking. + struct KeychainTestData { + const SecAuthenticationType auth_type; + const char* server; + const SecProtocolType protocol; + const char* path; + const UInt32 port; + const char* security_domain; + const char* creation_date; + const char* username; + const char* password; + const bool negative_item; + }; + // Adds a keychain item with the given info to the test set. + void AddTestItem(const KeychainTestData& item_data); + private: // Sets the data and length of |tag| in the item-th test item. - void SetTestDataBytes(int item, UInt32 tag, const void* data, - size_t length) const; + void SetTestDataBytes(int item, UInt32 tag, const void* data, size_t length); // Sets the data and length of |tag| in the item-th test item based on // |value|. The null-terminator will not be included; the Keychain Services // docs don't indicate whether it is or not, so clients should not assume // that it will be. - void SetTestDataString(int item, UInt32 tag, const char* value) const; + void SetTestDataString(int item, UInt32 tag, const char* value); // Sets the data of the corresponding attribute of the item-th test item to // |value|. Assumes that the space has alread been allocated, and the length // set. - void SetTestDataPort(int item, UInt32 value) const; - void SetTestDataProtocol(int item, SecProtocolType value) const; - void SetTestDataAuthType(int item, SecAuthenticationType value) const; - void SetTestDataNegativeItem(int item, Boolean value) const; + void SetTestDataPort(int item, UInt32 value); + void SetTestDataProtocol(int item, SecProtocolType value); + void SetTestDataAuthType(int item, SecAuthenticationType value); + void SetTestDataNegativeItem(int item, Boolean value); // Sets the password data and length for the item-th test item. - void SetTestDataPasswordBytes(int item, const void* data, - size_t length) const; + void SetTestDataPasswordBytes(int item, const void* data, size_t length); // Sets the password for the item-th test item. As with SetTestDataString, // the data will not be null-terminated. - void SetTestDataPasswordString(int item, const char* value) const; + void SetTestDataPasswordString(int item, const char* value); // Returns the index of |tag| in |attribute_list|, or -1 if it's not found. static int IndexForTag(const SecKeychainAttributeList& attribute_list, diff --git a/chrome/browser/password_manager/password_store_mac_unittest.cc b/chrome/browser/password_manager/password_store_mac_unittest.cc index 3ef66c1..a785a0c 100644 --- a/chrome/browser/password_manager/password_store_mac_unittest.cc +++ b/chrome/browser/password_manager/password_store_mac_unittest.cc @@ -11,14 +11,73 @@ using webkit_glue::PasswordForm; -// Causes a test failure unless everything returned from -// ItemCopyAttributesAndData, SearchCreateFromAttributes, and SearchCopyNext -// was correctly freed. -static void ExpectCreatesAndFreesBalanced(const MockKeychain& keychain) { - EXPECT_EQ(0, keychain.UnfreedSearchCount()); - EXPECT_EQ(0, keychain.UnfreedKeychainItemCount()); - EXPECT_EQ(0, keychain.UnfreedAttributeDataCount()); -} +class PasswordStoreMacTest : public testing::Test { + public: + virtual void SetUp() { + MockKeychain::KeychainTestData test_data[] = { + // Basic HTML form. + { kSecAuthenticationTypeHTMLForm, "some.domain.com", + kSecProtocolTypeHTTP, NULL, 0, NULL, "20020601171500Z", + "joe_user", "sekrit", false }, + // HTML form with path. + { kSecAuthenticationTypeHTMLForm, "some.domain.com", + kSecProtocolTypeHTTP, "/insecure.html", 0, NULL, "19991231235959Z", + "joe_user", "sekrit", false }, + // Secure HTML form with path. + { kSecAuthenticationTypeHTMLForm, "some.domain.com", + kSecProtocolTypeHTTPS, "/secure.html", 0, NULL, "20100908070605Z", + "secure_user", "password", false }, + // True negative item. + { kSecAuthenticationTypeHTMLForm, "dont.remember.com", + kSecProtocolTypeHTTP, NULL, 0, NULL, "20000101000000Z", + "", "", true }, + // De-facto negative item, type one. + {kSecAuthenticationTypeHTMLForm, "dont.remember.com", + kSecProtocolTypeHTTP, NULL, 0, NULL, "20000101000000Z", + "Password Not Stored", "", false }, + // De-facto negative item, type two. + { kSecAuthenticationTypeHTMLForm, "dont.remember.com", + kSecProtocolTypeHTTPS, NULL, 0, NULL, "20000101000000Z", + "Password Not Stored", " ", false }, + // HTTP auth basic, with port and path. + { kSecAuthenticationTypeHTTPBasic, "some.domain.com", + kSecProtocolTypeHTTP, "/insecure.html", 4567, "low_security", + "19980330100000Z", + "basic_auth_user", "basic", false }, + // HTTP auth digest, secure. + { kSecAuthenticationTypeHTTPDigest, "some.domain.com", + kSecProtocolTypeHTTPS, NULL, 0, "high_security", "19980330100000Z", + "digest_auth_user", "digest", false }, + }; + + // Save one slot for use by AddInternetPassword. + unsigned int capacity = arraysize(test_data) + 1; + keychain_ = new MockKeychain(capacity); + + for (unsigned int i = 0; i < arraysize(test_data); ++i) { + keychain_->AddTestItem(test_data[i]); + } + } + + virtual void TearDown() { + ExpectCreatesAndFreesBalanced(); + delete keychain_; + } + + protected: + // Causes a test failure unless everything returned from keychain_'s + // ItemCopyAttributesAndData, SearchCreateFromAttributes, and SearchCopyNext + // was correctly freed. + void ExpectCreatesAndFreesBalanced() { + EXPECT_EQ(0, keychain_->UnfreedSearchCount()); + EXPECT_EQ(0, keychain_->UnfreedKeychainItemCount()); + EXPECT_EQ(0, keychain_->UnfreedAttributeDataCount()); + } + + MockKeychain* keychain_; +}; + +#pragma mark - // Struct used for creation of PasswordForms from static arrays of data. struct PasswordFormData { @@ -141,7 +200,7 @@ static void DeletePasswordForms(std::vector<PasswordForm*>* forms) { #pragma mark - -TEST(PasswordStoreMacTest, TestSignonRealmParsing) { +TEST_F(PasswordStoreMacTest, TestSignonRealmParsing) { typedef struct { const char* signon_realm; const bool expected_parsed; @@ -198,7 +257,7 @@ TEST(PasswordStoreMacTest, TestSignonRealmParsing) { EXPECT_TRUE(parsed); } -TEST(PasswordStoreMacTest, TestURLConstruction) { +TEST_F(PasswordStoreMacTest, TestURLConstruction) { std::string host("exampledomain.com"); std::string path("/path/to/page.html"); @@ -213,7 +272,7 @@ TEST(PasswordStoreMacTest, TestURLConstruction) { EXPECT_EQ(GURL("https://exampledomain.com/"), simple_secure_url); } -TEST(PasswordStoreMacTest, TestKeychainTime) { +TEST_F(PasswordStoreMacTest, TestKeychainTime) { typedef struct { const char* time_string; const bool expected_parsed; @@ -262,7 +321,7 @@ TEST(PasswordStoreMacTest, TestKeychainTime) { } } -TEST(PasswordStoreMacTest, TestAuthTypeSchemeTranslation) { +TEST_F(PasswordStoreMacTest, TestAuthTypeSchemeTranslation) { // Our defined types should round-trip correctly. SecAuthenticationType auth_types[] = { kSecAuthenticationTypeHTMLForm, kSecAuthenticationTypeHTTPBasic, @@ -283,7 +342,7 @@ TEST(PasswordStoreMacTest, TestAuthTypeSchemeTranslation) { EXPECT_EQ(kSecAuthenticationTypeDefault, round_tripped_other_auth_type); } -TEST(PasswordStoreMacTest, TestKeychainToFormTranslation) { +TEST_F(PasswordStoreMacTest, TestKeychainToFormTranslation) { typedef struct { const PasswordForm::Scheme scheme; const char* signon_realm; @@ -326,18 +385,15 @@ TEST(PasswordStoreMacTest, TestKeychainToFormTranslation) { 1998, 3, 30, 10, 0, 0 }, }; - MockKeychain mock_keychain; - for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(expected); ++i) { // Create our fake KeychainItemRef; see MockKeychain docs. SecKeychainItemRef keychain_item = reinterpret_cast<SecKeychainItemRef>(i + 1); PasswordForm form; bool parsed = internal_keychain_helpers::FillPasswordFormFromKeychainItem( - mock_keychain, keychain_item, &form); + *keychain_, keychain_item, &form); EXPECT_TRUE(parsed) << "In iteration " << i; - ExpectCreatesAndFreesBalanced(mock_keychain); EXPECT_EQ(expected[i].scheme, form.scheme) << "In iteration " << i; EXPECT_EQ(GURL(expected[i].origin), form.origin) << "In iteration " << i; @@ -374,79 +430,68 @@ TEST(PasswordStoreMacTest, TestKeychainToFormTranslation) { SecKeychainItemRef keychain_item = reinterpret_cast<SecKeychainItemRef>(99); PasswordForm form; bool parsed = internal_keychain_helpers::FillPasswordFormFromKeychainItem( - mock_keychain, keychain_item, &form); - ExpectCreatesAndFreesBalanced(mock_keychain); + *keychain_, keychain_item, &form); EXPECT_FALSE(parsed); } } -TEST(PasswordStoreMacTest, TestKeychainSearch) { - MockKeychain mock_keychain; - +TEST_F(PasswordStoreMacTest, TestKeychainSearch) { { // An HTML form we've seen. std::vector<SecKeychainItemRef> matching_items; internal_keychain_helpers::FindMatchingKeychainItems( - mock_keychain, std::string("http://some.domain.com/"), + *keychain_, std::string("http://some.domain.com/"), PasswordForm::SCHEME_HTML, &matching_items); EXPECT_EQ(static_cast<size_t>(2), matching_items.size()); - FreeKeychainItems(mock_keychain, &matching_items); - ExpectCreatesAndFreesBalanced(mock_keychain); + FreeKeychainItems(*keychain_, &matching_items); } { // An HTML form we haven't seen std::vector<SecKeychainItemRef> matching_items; internal_keychain_helpers::FindMatchingKeychainItems( - mock_keychain, std::string("http://www.unseendomain.com/"), + *keychain_, std::string("http://www.unseendomain.com/"), PasswordForm::SCHEME_HTML, &matching_items); EXPECT_EQ(static_cast<size_t>(0), matching_items.size()); - FreeKeychainItems(mock_keychain, &matching_items); - ExpectCreatesAndFreesBalanced(mock_keychain); + FreeKeychainItems(*keychain_, &matching_items); } { // Basic auth that should match. std::vector<SecKeychainItemRef> matching_items; internal_keychain_helpers::FindMatchingKeychainItems( - mock_keychain, std::string("http://some.domain.com:4567/low_security"), + *keychain_, std::string("http://some.domain.com:4567/low_security"), PasswordForm::SCHEME_BASIC, &matching_items); EXPECT_EQ(static_cast<size_t>(1), matching_items.size()); - FreeKeychainItems(mock_keychain, &matching_items); - ExpectCreatesAndFreesBalanced(mock_keychain); + FreeKeychainItems(*keychain_, &matching_items); } { // Basic auth with the wrong port. std::vector<SecKeychainItemRef> matching_items; internal_keychain_helpers::FindMatchingKeychainItems( - mock_keychain, std::string("http://some.domain.com:1111/low_security"), + *keychain_, std::string("http://some.domain.com:1111/low_security"), PasswordForm::SCHEME_BASIC, &matching_items); EXPECT_EQ(static_cast<size_t>(0), matching_items.size()); - FreeKeychainItems(mock_keychain, &matching_items); - ExpectCreatesAndFreesBalanced(mock_keychain); + FreeKeychainItems(*keychain_, &matching_items); } { // Digest auth we've saved under https, visited with http. std::vector<SecKeychainItemRef> matching_items; internal_keychain_helpers::FindMatchingKeychainItems( - mock_keychain, std::string("http://some.domain.com/high_security"), + *keychain_, std::string("http://some.domain.com/high_security"), PasswordForm::SCHEME_DIGEST, &matching_items); EXPECT_EQ(static_cast<size_t>(0), matching_items.size()); - FreeKeychainItems(mock_keychain, &matching_items); - ExpectCreatesAndFreesBalanced(mock_keychain); + FreeKeychainItems(*keychain_, &matching_items); } { // Digest auth that should match. std::vector<SecKeychainItemRef> matching_items; internal_keychain_helpers::FindMatchingKeychainItems( - mock_keychain, std::string("https://some.domain.com/high_security"), + *keychain_, std::string("https://some.domain.com/high_security"), PasswordForm::SCHEME_DIGEST, &matching_items); EXPECT_EQ(static_cast<size_t>(1), matching_items.size()); - FreeKeychainItems(mock_keychain, &matching_items); - ExpectCreatesAndFreesBalanced(mock_keychain); + FreeKeychainItems(*keychain_, &matching_items); } } -TEST(PasswordStoreMacTest, TestKeychainExactSearch) { - MockKeychain mock_keychain; - +TEST_F(PasswordStoreMacTest, TestKeychainExactSearch) { // Test a web form entry (SCHEME_HTML). { PasswordForm search_form; @@ -458,47 +503,45 @@ TEST(PasswordStoreMacTest, TestKeychainExactSearch) { search_form.password_element = std::wstring(L"password"); search_form.preferred = true; SecKeychainItemRef match; - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, search_form); EXPECT_EQ(reinterpret_cast<SecKeychainItemRef>(2), match); - mock_keychain.Free(match); + keychain_->Free(match); // Make sure that the matching isn't looser than it should be. PasswordForm wrong_username(search_form); wrong_username.username_value = std::wstring(L"wrong_user"); - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_username); EXPECT_EQ(NULL, match); PasswordForm wrong_path(search_form); wrong_path.origin = GURL("http://some.domain.com/elsewhere.html"); - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_path); EXPECT_EQ(NULL, match); PasswordForm wrong_scheme(search_form); wrong_scheme.scheme = PasswordForm::SCHEME_BASIC; - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_scheme); EXPECT_EQ(NULL, match); // With no path, we should match the pathless Keychain entry. PasswordForm no_path(search_form); no_path.origin = GURL("http://some.domain.com/"); - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, no_path); EXPECT_EQ(reinterpret_cast<SecKeychainItemRef>(1), match); - mock_keychain.Free(match); + keychain_->Free(match); // We don't store blacklist entries in the keychain, and we want to ignore // those stored by other browsers. PasswordForm blacklist(search_form); blacklist.blacklisted_by_user = true; - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, blacklist); EXPECT_EQ(NULL, match); - - ExpectCreatesAndFreesBalanced(mock_keychain); } // Test an http auth entry (SCHEME_BASIC, but SCHEME_DIGEST works is searched @@ -511,41 +554,41 @@ TEST(PasswordStoreMacTest, TestKeychainExactSearch) { search_form.username_value = std::wstring(L"basic_auth_user"); search_form.scheme = PasswordForm::SCHEME_BASIC; SecKeychainItemRef match; - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, search_form); EXPECT_EQ(reinterpret_cast<SecKeychainItemRef>(7), match); - mock_keychain.Free(match); + keychain_->Free(match); // Make sure that the matching isn't looser than it should be. PasswordForm wrong_username(search_form); wrong_username.username_value = std::wstring(L"wrong_user"); - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_username); EXPECT_EQ(NULL, match); PasswordForm wrong_path(search_form); wrong_path.origin = GURL("http://some.domain.com:4567/elsewhere.html"); - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_path); EXPECT_EQ(NULL, match); PasswordForm wrong_scheme(search_form); wrong_scheme.scheme = PasswordForm::SCHEME_DIGEST; - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_scheme); EXPECT_EQ(NULL, match); PasswordForm wrong_port(search_form); wrong_port.signon_realm = std::string("http://some.domain.com:1234/low_security"); - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_port); EXPECT_EQ(NULL, match); PasswordForm wrong_realm(search_form); wrong_realm.signon_realm = std::string("http://some.domain.com:4567/incorrect"); - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, wrong_realm); EXPECT_EQ(NULL, match); @@ -553,21 +596,18 @@ TEST(PasswordStoreMacTest, TestKeychainExactSearch) { // those stored by other browsers. PasswordForm blacklist(search_form); blacklist.blacklisted_by_user = true; - match = internal_keychain_helpers::FindMatchingKeychainItem(mock_keychain, + match = internal_keychain_helpers::FindMatchingKeychainItem(*keychain_, blacklist); EXPECT_EQ(NULL, match); - - ExpectCreatesAndFreesBalanced(mock_keychain); } } -TEST(PasswordStoreMacTest, TestKeychainModify) { - MockKeychain mock_keychain; +TEST_F(PasswordStoreMacTest, TestKeychainModify) { SecKeychainItemRef keychain_item = reinterpret_cast<SecKeychainItemRef>(1); EXPECT_TRUE(internal_keychain_helpers::SetKeychainItemPassword( - mock_keychain, keychain_item, std::string("allnewpassword"))); + *keychain_, keychain_item, std::string("allnewpassword"))); PasswordForm form; - internal_keychain_helpers::FillPasswordFormFromKeychainItem(mock_keychain, + internal_keychain_helpers::FillPasswordFormFromKeychainItem(*keychain_, keychain_item, &form); EXPECT_EQ(L"allnewpassword", form.password_value); @@ -575,17 +615,14 @@ TEST(PasswordStoreMacTest, TestKeychainModify) { // Check that invalid items fail to update SecKeychainItemRef invalid_item = reinterpret_cast<SecKeychainItemRef>(1000); EXPECT_FALSE(internal_keychain_helpers::SetKeychainItemPassword( - mock_keychain, invalid_item, std::string("allnewpassword"))); + *keychain_, invalid_item, std::string("allnewpassword"))); // Check that other errors are reported (using the magic failure value). EXPECT_FALSE(internal_keychain_helpers::SetKeychainItemPassword( - mock_keychain, keychain_item, std::string("fail_me"))); - - ExpectCreatesAndFreesBalanced(mock_keychain); + *keychain_, keychain_item, std::string("fail_me"))); } -TEST(PasswordStoreMacTest, TestKeychainAdd) { - MockKeychain mock_keychain; +TEST_F(PasswordStoreMacTest, TestKeychainAdd) { struct TestDataAndExpectation { PasswordFormData data; bool should_succeed; @@ -618,25 +655,24 @@ TEST(PasswordStoreMacTest, TestKeychainAdd) { for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { PasswordForm* in_form = CreatePasswordFormFromData(test_data[i].data); bool add_succeeded = - internal_keychain_helpers::AddKeychainEntryForForm(mock_keychain, + internal_keychain_helpers::AddKeychainEntryForForm(*keychain_, *in_form); EXPECT_EQ(test_data[i].should_succeed, add_succeeded); if (add_succeeded) { SecKeychainItemRef matching_item; matching_item = internal_keychain_helpers::FindMatchingKeychainItem( - mock_keychain, *in_form); + *keychain_, *in_form); EXPECT_TRUE(matching_item != NULL); PasswordForm out_form; internal_keychain_helpers::FillPasswordFormFromKeychainItem( - mock_keychain, matching_item, &out_form); + *keychain_, matching_item, &out_form); EXPECT_EQ(out_form.scheme, in_form->scheme); EXPECT_EQ(out_form.signon_realm, in_form->signon_realm); EXPECT_EQ(out_form.origin, in_form->origin); EXPECT_EQ(out_form.username_value, in_form->username_value); EXPECT_EQ(out_form.password_value, in_form->password_value); - mock_keychain.Free(matching_item); + keychain_->Free(matching_item); } - ExpectCreatesAndFreesBalanced(mock_keychain); delete in_form; } @@ -649,20 +685,18 @@ TEST(PasswordStoreMacTest, TestKeychainAdd) { }; PasswordForm* update_form = CreatePasswordFormFromData(data); EXPECT_TRUE(internal_keychain_helpers::AddKeychainEntryForForm( - mock_keychain, *update_form)); + *keychain_, *update_form)); SecKeychainItemRef keychain_item = reinterpret_cast<SecKeychainItemRef>(2); PasswordForm stored_form; - internal_keychain_helpers::FillPasswordFormFromKeychainItem(mock_keychain, + internal_keychain_helpers::FillPasswordFormFromKeychainItem(*keychain_, keychain_item, &stored_form); EXPECT_EQ(update_form->password_value, stored_form.password_value); delete update_form; } - - ExpectCreatesAndFreesBalanced(mock_keychain); } -TEST(PasswordStoreMacTest, TestFormMatch) { +TEST_F(PasswordStoreMacTest, TestFormMatch) { PasswordForm base_form; base_form.signon_realm = std::string("http://some.domain.com/"); base_form.origin = GURL("http://some.domain.com/page.html"); @@ -734,7 +768,7 @@ TEST(PasswordStoreMacTest, TestFormMatch) { } } -TEST(PasswordStoreMacTest, TestFormMerge) { +TEST_F(PasswordStoreMacTest, TestFormMerge) { // Set up a bunch of test data to use in varying combinations. PasswordFormData keychain_user_1 = { PasswordForm::SCHEME_HTML, "http://some.domain.com/", |