diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-17 12:14:50 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-17 12:14:50 +0000 |
commit | b9d3d0028bbef6846b017c3ce8514f012921f65f (patch) | |
tree | 1667a362918c39ab52c25c9acb63a68751329630 | |
parent | 236790f7ae982eb6dda025960964a88d4fc538f8 (diff) | |
download | chromium_src-b9d3d0028bbef6846b017c3ce8514f012921f65f.zip chromium_src-b9d3d0028bbef6846b017c3ce8514f012921f65f.tar.gz chromium_src-b9d3d0028bbef6846b017c3ce8514f012921f65f.tar.bz2 |
Move *keychain_mac* files to crypto/
This is in preparation for moving password_manager/encryptor* to
crypto/.
This also fixes an ODR violation; mock_keychain_mac.cc was being
compiled in two targets which are linked together.
BUG=118564
TEST=
Review URL: http://codereview.chromium.org/9699112
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127358 0039d316-1c4b-4281-b951-d872f2087c98
16 files changed, 270 insertions, 200 deletions
diff --git a/chrome/browser/password_manager/encryptor_mac.mm b/chrome/browser/password_manager/encryptor_mac.mm index 56b048c..00116fa 100644 --- a/chrome/browser/password_manager/encryptor_mac.mm +++ b/chrome/browser/password_manager/encryptor_mac.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -9,10 +9,12 @@ #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/password_manager/encryptor_password_mac.h" #include "crypto/encryptor.h" +#include "crypto/keychain_mac.h" #include "crypto/symmetric_key.h" -#include "chrome/browser/password_manager/encryptor_password_mac.h" -#include "chrome/browser/keychain_mac.h" + +using crypto::MacKeychain; namespace { diff --git a/chrome/browser/password_manager/encryptor_password_mac.h b/chrome/browser/password_manager/encryptor_password_mac.h index 7d58e32..002163c 100644 --- a/chrome/browser/password_manager/encryptor_password_mac.h +++ b/chrome/browser/password_manager/encryptor_password_mac.h @@ -10,11 +10,13 @@ #include "base/basictypes.h" +namespace crypto { class MacKeychain; +} // namespace crypto class EncryptorPassword { public: - explicit EncryptorPassword(const MacKeychain& keychain) + explicit EncryptorPassword(const crypto::MacKeychain& keychain) : keychain_(keychain) { } @@ -28,7 +30,7 @@ class EncryptorPassword { private: DISALLOW_COPY_AND_ASSIGN(EncryptorPassword); - const MacKeychain& keychain_; + const crypto::MacKeychain& keychain_; }; #endif // CHROME_BROWSER_PASSWORD_MANAGER_ENCRYPTOR_PASSWORD_H__ diff --git a/chrome/browser/password_manager/encryptor_password_mac.mm b/chrome/browser/password_manager/encryptor_password_mac.mm index d8b417d..9b5c23a 100644 --- a/chrome/browser/password_manager/encryptor_password_mac.mm +++ b/chrome/browser/password_manager/encryptor_password_mac.mm @@ -9,9 +9,11 @@ #include "base/base64.h" #include "base/mac/mac_logging.h" #include "base/rand_util.h" -#include "chrome/browser/keychain_mac.h" +#include "crypto/keychain_mac.h" #include "ui/base/l10n/l10n_util.h" +using crypto::MacKeychain; + namespace { // Generates a random password and adds it to the Keychain. The added password diff --git a/chrome/browser/password_manager/encryptor_password_mac_unittest.cc b/chrome/browser/password_manager/encryptor_password_mac_unittest.cc index f331c38..284b881 100644 --- a/chrome/browser/password_manager/encryptor_password_mac_unittest.cc +++ b/chrome/browser/password_manager/encryptor_password_mac_unittest.cc @@ -2,12 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/mock_keychain_mac.h" #include "chrome/browser/password_manager/encryptor_password_mac.h" +#include "crypto/mock_keychain_mac.h" #include "testing/gtest/include/gtest/gtest.h" namespace { +using crypto::MockKeychain; + // Test that if we have an existing password in the Keychain and we are // authorized by the user to read it then we get it back correctly. TEST(EncryptorPasswordTest, FindPasswordSuccess) { diff --git a/chrome/browser/password_manager/password_store_factory.cc b/chrome/browser/password_manager/password_store_factory.cc index d2f0ef7..a404453 100644 --- a/chrome/browser/password_manager/password_store_factory.cc +++ b/chrome/browser/password_manager/password_store_factory.cc @@ -18,9 +18,9 @@ #if defined(OS_WIN) #include "chrome/browser/password_manager/password_store_win.h" #elif defined(OS_MACOSX) -#include "chrome/browser/keychain_mac.h" -#include "chrome/browser/mock_keychain_mac.h" #include "chrome/browser/password_manager/password_store_mac.h" +#include "crypto/keychain_mac.h" +#include "crypto/mock_keychain_mac.h" #elif defined(OS_CHROMEOS) || defined(OS_ANDROID) // Don't do anything. We're going to use the default store. #elif defined(OS_POSIX) @@ -111,9 +111,9 @@ PasswordStoreFactory::BuildServiceInstanceFor(Profile* profile) const { profile->GetWebDataService(Profile::IMPLICIT_ACCESS)); #elif defined(OS_MACOSX) if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseMockKeychain)) { - ps = new PasswordStoreMac(new MockKeychain(), login_db); + ps = new PasswordStoreMac(new crypto::MockKeychain(), login_db); } else { - ps = new PasswordStoreMac(new MacKeychain(), login_db); + ps = new PasswordStoreMac(new crypto::MacKeychain(), login_db); } #elif defined(OS_CHROMEOS) || defined(OS_ANDROID) // For now, we use PasswordStoreDefault. We might want to make a native diff --git a/chrome/browser/password_manager/password_store_mac.cc b/chrome/browser/password_manager/password_store_mac.cc index 3452975..a25a944 100644 --- a/chrome/browser/password_manager/password_store_mac.cc +++ b/chrome/browser/password_manager/password_store_mac.cc @@ -18,12 +18,13 @@ #include "base/stl_util.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/keychain_mac.h" #include "chrome/browser/password_manager/login_database.h" #include "chrome/browser/password_manager/password_store_change.h" #include "chrome/common/chrome_notification_types.h" #include "content/public/browser/notification_service.h" +#include "crypto/keychain_mac.h" +using crypto::MacKeychain; using webkit::forms::PasswordForm; // Utility class to handle the details of constructing and running a keychain diff --git a/chrome/browser/password_manager/password_store_mac.h b/chrome/browser/password_manager/password_store_mac.h index f662bd0..ddf19ab 100644 --- a/chrome/browser/password_manager/password_store_mac.h +++ b/chrome/browser/password_manager/password_store_mac.h @@ -14,12 +14,14 @@ #include "chrome/browser/password_manager/login_database.h" #include "chrome/browser/password_manager/password_store.h" -class MacKeychain; - namespace content { class NotificationService; } +namespace crypto { +class MacKeychain; +} + // Implements PasswordStore on top of the OS X Keychain, with an internal // database for extra metadata. For an overview of the interactions with the // Keychain, as well as the rationale for some of the behaviors, see the @@ -29,7 +31,7 @@ class PasswordStoreMac : public PasswordStore { public: // Takes ownership of |keychain| and |login_db|, both of which must be // non-NULL. - PasswordStoreMac(MacKeychain* keychain, LoginDatabase* login_db); + PasswordStoreMac(crypto::MacKeychain* keychain, LoginDatabase* login_db); // Initializes |thread_| and |notification_service_|. virtual bool Init() OVERRIDE; @@ -86,7 +88,7 @@ class PasswordStoreMac : public PasswordStore { // thread. void CreateNotificationService(); - scoped_ptr<MacKeychain> keychain_; + scoped_ptr<crypto::MacKeychain> keychain_; scoped_ptr<LoginDatabase> login_metadata_db_; // Thread that the synchronous methods are run on. diff --git a/chrome/browser/password_manager/password_store_mac_internal.h b/chrome/browser/password_manager/password_store_mac_internal.h index c70af27..dc8acb7 100644 --- a/chrome/browser/password_manager/password_store_mac_internal.h +++ b/chrome/browser/password_manager/password_store_mac_internal.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -12,7 +12,9 @@ #include <vector> #include "base/time.h" -#include "chrome/browser/keychain_mac.h" +#include "crypto/keychain_mac.h" + +using crypto::MacKeychain; // Adapter that wraps a MacKeychain and provides interaction in terms of // PasswordForms instead of Keychain items. diff --git a/chrome/browser/password_manager/password_store_mac_unittest.cc b/chrome/browser/password_manager/password_store_mac_unittest.cc index 994db5d..b79e660 100644 --- a/chrome/browser/password_manager/password_store_mac_unittest.cc +++ b/chrome/browser/password_manager/password_store_mac_unittest.cc @@ -12,14 +12,15 @@ #include "base/stl_util.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/mock_keychain_mac.h" #include "chrome/browser/password_manager/password_store_consumer.h" #include "chrome/browser/password_manager/password_store_mac.h" #include "chrome/browser/password_manager/password_store_mac_internal.h" #include "chrome/common/chrome_paths.h" #include "content/test/test_browser_thread.h" +#include "crypto/mock_keychain_mac.h" using content::BrowserThread; +using crypto::MockKeychain; using webkit::forms::PasswordForm; using testing::_; using testing::DoAll; diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 71d35d5..232346e 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1319,8 +1319,6 @@ 'browser/jankometer_mac.cc', 'browser/jumplist_win.cc', 'browser/jumplist_win.h', - 'browser/keychain_mac.cc', - 'browser/keychain_mac.h', 'browser/language_usage_metrics.cc', 'browser/language_usage_metrics.h', 'browser/low_memory_observer.cc', @@ -1377,8 +1375,6 @@ 'browser/metrics/metrics_service.h', 'browser/metrics/thread_watcher.cc', 'browser/metrics/thread_watcher.h', - 'browser/mock_keychain_mac.cc', - 'browser/mock_keychain_mac.h', 'browser/native_window_notification_source.h', 'browser/net/browser_url_util.cc', 'browser/net/browser_url_util.h', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index f108585..9d7244c 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1523,8 +1523,6 @@ 'browser/metrics/metrics_response_unittest.cc', 'browser/metrics/metrics_service_unittest.cc', 'browser/metrics/thread_watcher_unittest.cc', - 'browser/mock_keychain_mac.cc', - 'browser/mock_keychain_mac.h', 'browser/net/browser_url_util_unittest.cc', 'browser/net/chrome_fraudulent_certificate_reporter_unittest.cc', 'browser/net/chrome_net_log_unittest.cc', diff --git a/crypto/crypto.gyp b/crypto/crypto.gyp index ef7b87b..405ad0b 100644 --- a/crypto/crypto.gyp +++ b/crypto/crypto.gyp @@ -157,8 +157,12 @@ 'hmac_nss.cc', 'hmac_openssl.cc', 'hmac_win.cc', + 'keychain_mac.cc', + 'keychain_mac.h', 'mac_security_services_lock.cc', 'mac_security_services_lock.h', + 'mock_keychain_mac.cc', + 'mock_keychain_mac.h', 'p224_spake.cc', 'p224_spake.h', 'nss_util.cc', diff --git a/chrome/browser/keychain_mac.cc b/crypto/keychain_mac.cc index 5640e6b..4d5715b 100644 --- a/chrome/browser/keychain_mac.cc +++ b/crypto/keychain_mac.cc @@ -1,26 +1,38 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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 "chrome/browser/keychain_mac.h" +#include "crypto/keychain_mac.h" + +namespace crypto { + +MacKeychain::MacKeychain() {} + +MacKeychain::~MacKeychain() {} OSStatus MacKeychain::ItemCopyAttributesAndData( - SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, - SecItemClass *itemClass, SecKeychainAttributeList **attrList, - UInt32 *length, void **outData) const { + SecKeychainItemRef itemRef, + SecKeychainAttributeInfo* info, + SecItemClass* itemClass, + SecKeychainAttributeList** attrList, + UInt32* length, + void** outData) const { return SecKeychainItemCopyAttributesAndData(itemRef, info, itemClass, attrList, length, outData); } OSStatus MacKeychain::ItemModifyAttributesAndData( - SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, - UInt32 length, const void *data) const { + SecKeychainItemRef itemRef, + const SecKeychainAttributeList* attrList, + UInt32 length, + const void* data) const { return SecKeychainItemModifyAttributesAndData(itemRef, attrList, length, data); } OSStatus MacKeychain::ItemFreeAttributesAndData( - SecKeychainAttributeList *attrList, void *data) const { + SecKeychainAttributeList* attrList, + void* data) const { return SecKeychainItemFreeAttributesAndData(attrList, data); } @@ -29,28 +41,35 @@ OSStatus MacKeychain::ItemDelete(SecKeychainItemRef itemRef) const { } OSStatus MacKeychain::SearchCreateFromAttributes( - CFTypeRef keychainOrArray, SecItemClass itemClass, - const SecKeychainAttributeList *attrList, - SecKeychainSearchRef *searchRef) const { + CFTypeRef keychainOrArray, + SecItemClass itemClass, + const SecKeychainAttributeList* attrList, + SecKeychainSearchRef* searchRef) const { return SecKeychainSearchCreateFromAttributes(keychainOrArray, itemClass, attrList, searchRef); } OSStatus MacKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, - SecKeychainItemRef *itemRef) const { + SecKeychainItemRef* itemRef) const { return SecKeychainSearchCopyNext(searchRef, itemRef); } OSStatus MacKeychain::AddInternetPassword( SecKeychainRef keychain, - UInt32 serverNameLength, const char *serverName, - UInt32 securityDomainLength, const char *securityDomain, - UInt32 accountNameLength, const char *accountName, - UInt32 pathLength, const char *path, - UInt16 port, SecProtocolType protocol, + UInt32 serverNameLength, + const char* serverName, + UInt32 securityDomainLength, + const char* securityDomain, + UInt32 accountNameLength, + const char* accountName, + UInt32 pathLength, + const char* path, + UInt16 port, + SecProtocolType protocol, SecAuthenticationType authenticationType, - UInt32 passwordLength, const void *passwordData, - SecKeychainItemRef *itemRef) const { + UInt32 passwordLength, + const void* passwordData, + SecKeychainItemRef* itemRef) const { return SecKeychainAddInternetPassword(keychain, serverNameLength, serverName, securityDomainLength, securityDomain, @@ -63,12 +82,12 @@ OSStatus MacKeychain::AddInternetPassword( OSStatus MacKeychain::FindGenericPassword(CFTypeRef keychainOrArray, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, - UInt32 *passwordLength, - void **passwordData, - SecKeychainItemRef *itemRef) const { + const char* accountName, + UInt32* passwordLength, + void** passwordData, + SecKeychainItemRef* itemRef) const { return SecKeychainFindGenericPassword(keychainOrArray, serviceNameLength, serviceName, @@ -79,19 +98,19 @@ OSStatus MacKeychain::FindGenericPassword(CFTypeRef keychainOrArray, itemRef); } -OSStatus MacKeychain::ItemFreeContent(SecKeychainAttributeList *attrList, - void *data) const { +OSStatus MacKeychain::ItemFreeContent(SecKeychainAttributeList* attrList, + void* data) const { return SecKeychainItemFreeContent(attrList, data); } OSStatus MacKeychain::AddGenericPassword(SecKeychainRef keychain, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, + const char* accountName, UInt32 passwordLength, - const void *passwordData, - SecKeychainItemRef *itemRef) const { + const void* passwordData, + SecKeychainItemRef* itemRef) const { return SecKeychainAddGenericPassword(keychain, serviceNameLength, serviceName, @@ -103,7 +122,8 @@ OSStatus MacKeychain::AddGenericPassword(SecKeychainRef keychain, } void MacKeychain::Free(CFTypeRef ref) const { - if (ref) { + if (ref) CFRelease(ref); - } } + +} // namespace crypto diff --git a/chrome/browser/keychain_mac.h b/crypto/keychain_mac.h index f3c98e0..39ba31c 100644 --- a/chrome/browser/keychain_mac.h +++ b/crypto/keychain_mac.h @@ -1,15 +1,17 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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. -#ifndef CHROME_BROWSER_KEYCHAIN_MAC_H_ -#define CHROME_BROWSER_KEYCHAIN_MAC_H_ +#ifndef CRYPTO_KEYCHAIN_MAC_H_ +#define CRYPTO_KEYCHAIN_MAC_H_ #pragma once #include <Security/Security.h> #include "base/basictypes.h" +namespace crypto { + // Wraps the KeychainServices API in a very thin layer, to allow it to be // mocked out for testing. @@ -20,65 +22,71 @@ // CFRelease (to aid in testing). class MacKeychain { public: - MacKeychain() {} - virtual ~MacKeychain() {} + MacKeychain(); + virtual ~MacKeychain(); virtual OSStatus ItemCopyAttributesAndData( - SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, - SecItemClass *itemClass, SecKeychainAttributeList **attrList, - UInt32 *length, void **outData) const; + SecKeychainItemRef itemRef, + SecKeychainAttributeInfo* info, + SecItemClass* itemClass, + SecKeychainAttributeList** attrList, + UInt32* length, + void** outData) const; virtual OSStatus ItemModifyAttributesAndData( - SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, - UInt32 length, const void *data) const; + SecKeychainItemRef itemRef, + const SecKeychainAttributeList* attrList, + UInt32 length, + const void* data) const; - virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList *attrList, - void *data) const; + virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList* attrList, + void* data) const; virtual OSStatus ItemDelete(SecKeychainItemRef itemRef) const; virtual OSStatus SearchCreateFromAttributes( - CFTypeRef keychainOrArray, SecItemClass itemClass, - const SecKeychainAttributeList *attrList, - SecKeychainSearchRef *searchRef) const; + CFTypeRef keychainOrArray, + SecItemClass itemClass, + const SecKeychainAttributeList* attrList, + SecKeychainSearchRef* searchRef) const; virtual OSStatus SearchCopyNext(SecKeychainSearchRef searchRef, - SecKeychainItemRef *itemRef) const; + SecKeychainItemRef* itemRef) const; virtual OSStatus AddInternetPassword(SecKeychainRef keychain, UInt32 serverNameLength, - const char *serverName, + const char* serverName, UInt32 securityDomainLength, - const char *securityDomain, + const char* securityDomain, UInt32 accountNameLength, - const char *accountName, - UInt32 pathLength, const char *path, + const char* accountName, + UInt32 pathLength, const char* path, UInt16 port, SecProtocolType protocol, SecAuthenticationType authenticationType, UInt32 passwordLength, - const void *passwordData, - SecKeychainItemRef *itemRef) const; + const void* passwordData, + SecKeychainItemRef* itemRef) const; virtual OSStatus FindGenericPassword(CFTypeRef keychainOrArray, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, - UInt32 *passwordLength, - void **passwordData, - SecKeychainItemRef *itemRef) const; + const char* accountName, + UInt32* passwordLength, + void** passwordData, + SecKeychainItemRef* itemRef) const; - virtual OSStatus ItemFreeContent(SecKeychainAttributeList *attrList, - void *data) const; + virtual OSStatus ItemFreeContent(SecKeychainAttributeList* attrList, + void* data) const; virtual OSStatus AddGenericPassword(SecKeychainRef keychain, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, + const char* accountName, UInt32 passwordLength, - const void *passwordData, - SecKeychainItemRef *itemRef) const; + const void* passwordData, + SecKeychainItemRef* itemRef) const; // Calls CFRelease on the given ref, after checking that |ref| is non-NULL. virtual void Free(CFTypeRef ref) const; @@ -87,4 +95,6 @@ class MacKeychain { DISALLOW_COPY_AND_ASSIGN(MacKeychain); }; -#endif // CHROME_BROWSER_KEYCHAIN_MAC_H_ +} // namespace crypto + +#endif // CRYPTO_KEYCHAIN_MAC_H_ diff --git a/chrome/browser/mock_keychain_mac.cc b/crypto/mock_keychain_mac.cc index 59d2b58..a3bacf6 100644 --- a/chrome/browser/mock_keychain_mac.cc +++ b/crypto/mock_keychain_mac.cc @@ -4,7 +4,9 @@ #include "base/logging.h" #include "base/time.h" -#include "chrome/browser/mock_keychain_mac.h" +#include "crypto/mock_keychain_mac.h" + +namespace crypto { MockKeychain::MockKeychain() : next_item_key_(0), @@ -62,21 +64,20 @@ MockKeychain::~MockKeychain() { for (std::map<unsigned int, SecKeychainAttributeList>::iterator it = keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { for (unsigned int i = 0; i < it->second.count; ++i) { - if (it->second.attr[i].data) { + if (it->second.attr[i].data) free(it->second.attr[i].data); - } } free(it->second.attr); - if (keychain_data_[it->first].data) { + if (keychain_data_[it->first].data) free(keychain_data_[it->first].data); - } } keychain_attr_list_.clear(); keychain_data_.clear(); } SecKeychainAttribute* MockKeychain::AttributeWithTag( - const SecKeychainAttributeList& attribute_list, UInt32 tag) { + const SecKeychainAttributeList& attribute_list, + UInt32 tag) { int attribute_index = -1; for (unsigned int i = 0; i < attribute_list.count; ++i) { if (attribute_list.attr[i].tag == tag) { @@ -91,15 +92,16 @@ SecKeychainAttribute* MockKeychain::AttributeWithTag( return &(attribute_list.attr[attribute_index]); } -void MockKeychain::SetTestDataBytes(int item, UInt32 tag, const void* data, +void MockKeychain::SetTestDataBytes(int item, + UInt32 tag, + const void* data, size_t length) { SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[item], tag); attribute->length = length; if (length > 0) { - if (attribute->data) { + if (attribute->data) free(attribute->data); - } attribute->data = malloc(length); CHECK(attribute->data); memcpy(attribute->data, data, length); @@ -152,9 +154,8 @@ void MockKeychain::SetTestDataPasswordBytes(int item, const void* data, size_t length) { keychain_data_[item].length = length; if (length > 0) { - if (keychain_data_[item].data) { + if (keychain_data_[item].data) free(keychain_data_[item].data); - } keychain_data_[item].data = malloc(length); memcpy(keychain_data_[item].data, data, length); } else { @@ -167,19 +168,20 @@ void MockKeychain::SetTestDataPasswordString(int item, const char* value) { } OSStatus MockKeychain::ItemCopyAttributesAndData( - SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, - SecItemClass *itemClass, SecKeychainAttributeList **attrList, - UInt32 *length, void **outData) const { + SecKeychainItemRef itemRef, + SecKeychainAttributeInfo* info, + SecItemClass* itemClass, + SecKeychainAttributeList** attrList, + UInt32* length, + void** outData) const { DCHECK(itemRef); unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; - if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) { + if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) return errSecInvalidItemRef; - } DCHECK(!itemClass); // itemClass not implemented in the Mock. - if (attrList) { + if (attrList) *attrList = &(keychain_attr_list_[key]); - } if (outData) { *outData = keychain_data_[key].data; DCHECK(length); @@ -191,8 +193,10 @@ OSStatus MockKeychain::ItemCopyAttributesAndData( } OSStatus MockKeychain::ItemModifyAttributesAndData( - SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, - UInt32 length, const void *data) const { + SecKeychainItemRef itemRef, + const SecKeychainAttributeList* attrList, + UInt32 length, + const void* data) const { DCHECK(itemRef); const char* fail_trigger = "fail_me"; if (length == strlen(fail_trigger) && @@ -201,9 +205,8 @@ OSStatus MockKeychain::ItemModifyAttributesAndData( } unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; - if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) { + if (keychain_attr_list_.find(key) == keychain_attr_list_.end()) return errSecInvalidItemRef; - } MockKeychain* mutable_this = const_cast<MockKeychain*>(this); if (attrList) { @@ -216,15 +219,14 @@ OSStatus MockKeychain::ItemModifyAttributesAndData( } } } - if (data) { + if (data) mutable_this->SetTestDataPasswordBytes(key, data, length); - } return noErr; } OSStatus MockKeychain::ItemFreeAttributesAndData( - SecKeychainAttributeList *attrList, - void *data) const { + SecKeychainAttributeList* attrList, + void* data) const { --attribute_data_copy_count_; return noErr; } @@ -233,14 +235,12 @@ OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { unsigned int key = reinterpret_cast<unsigned int>(itemRef) - 1; for (unsigned int i = 0; i < keychain_attr_list_[key].count; ++i) { - if (keychain_attr_list_[key].attr[i].data) { + if (keychain_attr_list_[key].attr[i].data) free(keychain_attr_list_[key].attr[i].data); - } } free(keychain_attr_list_[key].attr); - if (keychain_data_[key].data) { + if (keychain_data_[key].data) free(keychain_data_[key].data); - } keychain_attr_list_.erase(key); keychain_data_.erase(key); @@ -249,9 +249,10 @@ OSStatus MockKeychain::ItemDelete(SecKeychainItemRef itemRef) const { } OSStatus MockKeychain::SearchCreateFromAttributes( - CFTypeRef keychainOrArray, SecItemClass itemClass, - const SecKeychainAttributeList *attrList, - SecKeychainSearchRef *searchRef) const { + CFTypeRef keychainOrArray, + SecItemClass itemClass, + const SecKeychainAttributeList* attrList, + SecKeychainSearchRef* searchRef) const { // Figure out which of our mock items matches, and set up the array we'll use // to generate results out of SearchCopyNext. remaining_search_results_.clear(); @@ -268,9 +269,8 @@ OSStatus MockKeychain::SearchCreateFromAttributes( break; } } - if (mock_item_matches) { + if (mock_item_matches) remaining_search_results_.push_back(it->first); - } } DCHECK(searchRef); @@ -280,11 +280,16 @@ OSStatus MockKeychain::SearchCreateFromAttributes( } bool MockKeychain::AlreadyContainsInternetPassword( - UInt32 serverNameLength, const char *serverName, - UInt32 securityDomainLength, const char *securityDomain, - UInt32 accountNameLength, const char *accountName, - UInt32 pathLength, const char *path, - UInt16 port, SecProtocolType protocol, + UInt32 serverNameLength, + const char* serverName, + UInt32 securityDomainLength, + const char* securityDomain, + UInt32 accountNameLength, + const char* accountName, + UInt32 pathLength, + const char* path, + UInt16 port, + SecProtocolType protocol, SecAuthenticationType authenticationType) const { for (std::map<unsigned int, SecKeychainAttributeList>::const_iterator it = keychain_attr_list_.begin(); it != keychain_attr_list_.end(); ++it) { @@ -350,19 +355,24 @@ bool MockKeychain::AlreadyContainsInternetPassword( OSStatus MockKeychain::AddInternetPassword( SecKeychainRef keychain, - UInt32 serverNameLength, const char *serverName, - UInt32 securityDomainLength, const char *securityDomain, - UInt32 accountNameLength, const char *accountName, - UInt32 pathLength, const char *path, - UInt16 port, SecProtocolType protocol, + UInt32 serverNameLength, + const char* serverName, + UInt32 securityDomainLength, + const char* securityDomain, + UInt32 accountNameLength, + const char* accountName, + UInt32 pathLength, + const char* path, + UInt16 port, + SecProtocolType protocol, SecAuthenticationType authenticationType, - UInt32 passwordLength, const void *passwordData, - SecKeychainItemRef *itemRef) const { + UInt32 passwordLength, + const void* passwordData, + SecKeychainItemRef* itemRef) const { // Check for the magic duplicate item trigger. - if (strcmp(serverName, "some.domain.com") == 0) { + if (strcmp(serverName, "some.domain.com") == 0) return errSecDuplicateItem; - } // If the account already exists in the keychain, we don't add it. if (AlreadyContainsInternetPassword(serverNameLength, serverName, @@ -411,10 +421,9 @@ OSStatus MockKeychain::AddInternetPassword( } OSStatus MockKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, - SecKeychainItemRef *itemRef) const { - if (remaining_search_results_.empty()) { + SecKeychainItemRef* itemRef) const { + if (remaining_search_results_.empty()) return errSecItemNotFound; - } unsigned int key = remaining_search_results_.front(); remaining_search_results_.erase(remaining_search_results_.begin()); *itemRef = reinterpret_cast<SecKeychainItemRef>(key + 1); @@ -424,12 +433,12 @@ OSStatus MockKeychain::SearchCopyNext(SecKeychainSearchRef searchRef, OSStatus MockKeychain::FindGenericPassword(CFTypeRef keychainOrArray, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, - UInt32 *passwordLength, - void **passwordData, - SecKeychainItemRef *itemRef) const { + const char* accountName, + UInt32* passwordLength, + void** passwordData, + SecKeychainItemRef* itemRef) const { // When simulating |noErr| we return canned |passwordData| and // |passwordLenght|. Otherwise, just return given code. if (find_generic_result_ == noErr) { @@ -445,8 +454,8 @@ OSStatus MockKeychain::FindGenericPassword(CFTypeRef keychainOrArray, return find_generic_result_; } -OSStatus MockKeychain::ItemFreeContent(SecKeychainAttributeList *attrList, - void *data) const { +OSStatus MockKeychain::ItemFreeContent(SecKeychainAttributeList* attrList, + void* data) const { // No-op. password_data_count_--; return noErr; @@ -454,12 +463,12 @@ OSStatus MockKeychain::ItemFreeContent(SecKeychainAttributeList *attrList, OSStatus MockKeychain::AddGenericPassword(SecKeychainRef keychain, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, + const char* accountName, UInt32 passwordLength, - const void *passwordData, - SecKeychainItemRef *itemRef) const { + const void* passwordData, + SecKeychainItemRef* itemRef) const { called_add_generic_ = true; DCHECK(passwordLength > 0); @@ -471,9 +480,8 @@ OSStatus MockKeychain::AddGenericPassword(SecKeychainRef keychain, } void MockKeychain::Free(CFTypeRef ref) const { - if (!ref) { + if (!ref) return; - } if (reinterpret_cast<int>(ref) == kDummySearchRef) { --search_copy_count_; @@ -500,9 +508,8 @@ bool MockKeychain::CreatorCodesSetForAddedItems() const { SecKeychainAttribute* attribute = AttributeWithTag(keychain_attr_list_[*i], kSecCreatorItemAttr); OSType* data = static_cast<OSType*>(attribute->data); - if (*data == 0) { + if (*data == 0) return false; - } } return true; } @@ -523,3 +530,5 @@ void MockKeychain::AddTestItem(const KeychainTestData& item_data) { SetTestDataPasswordString(key, item_data.password); SetTestDataNegativeItem(key, item_data.negative_item); } + +} // namespace crypto diff --git a/chrome/browser/mock_keychain_mac.h b/crypto/mock_keychain_mac.h index 7d378f2..d52353e 100644 --- a/chrome/browser/mock_keychain_mac.h +++ b/crypto/mock_keychain_mac.h @@ -1,9 +1,9 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. -#ifndef CHROME_BROWSER_MOCK_KEYCHAIN_MAC_H_ -#define CHROME_BROWSER_MOCK_KEYCHAIN_MAC_H_ +#ifndef CRYPTO_MOCK_KEYCHAIN_MAC_H_ +#define CRYPTO_MOCK_KEYCHAIN_MAC_H_ #pragma once #include <map> @@ -12,11 +12,14 @@ #include <vector> #include "base/compiler_specific.h" -#include "chrome/browser/keychain_mac.h" +#include "crypto/keychain_mac.h" -// Mock Keychain wrapper for testing code that interacts with the OS Keychain. -// Implemented by storing SecKeychainAttributeList and KeychainPasswordData -// values in separate mutable containers and mapping them to integer keys. +namespace crypto { + +// Mock Keychain wrapper for testing code that interacts with the OS X +// Keychain. Implemented by storing SecKeychainAttributeList and +// KeychainPasswordData values in separate mutable containers and +// mapping them to integer keys. // // 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 @@ -25,58 +28,66 @@ class MockKeychain : public MacKeychain { public: MockKeychain(); virtual ~MockKeychain(); + + // MacKeychain implementation. virtual OSStatus ItemCopyAttributesAndData( - SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, - SecItemClass *itemClass, SecKeychainAttributeList **attrList, - UInt32 *length, void **outData) const OVERRIDE; + SecKeychainItemRef itemRef, + SecKeychainAttributeInfo* info, + SecItemClass* itemClass, + SecKeychainAttributeList** attrList, + UInt32* length, + void** outData) const OVERRIDE; // Pass "fail_me" as the data to get errSecAuthFailed. virtual OSStatus ItemModifyAttributesAndData( - SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, - UInt32 length, const void *data) const OVERRIDE; - virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList *attrList, - void *data) const OVERRIDE; + SecKeychainItemRef itemRef, + const SecKeychainAttributeList* attrList, + UInt32 length, + const void* data) const OVERRIDE; + virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList* attrList, + void* data) const OVERRIDE; virtual OSStatus ItemDelete(SecKeychainItemRef itemRef) const OVERRIDE; virtual OSStatus SearchCreateFromAttributes( - CFTypeRef keychainOrArray, SecItemClass itemClass, - const SecKeychainAttributeList *attrList, - SecKeychainSearchRef *searchRef) const OVERRIDE; + CFTypeRef keychainOrArray, + SecItemClass itemClass, + const SecKeychainAttributeList* attrList, + SecKeychainSearchRef* searchRef) const OVERRIDE; virtual OSStatus SearchCopyNext(SecKeychainSearchRef searchRef, - SecKeychainItemRef *itemRef) const OVERRIDE; + SecKeychainItemRef* itemRef) const OVERRIDE; // Pass "some.domain.com" as the serverName to get errSecDuplicateItem. virtual OSStatus AddInternetPassword( SecKeychainRef keychain, UInt32 serverNameLength, - const char *serverName, + const char* serverName, UInt32 securityDomainLength, - const char *securityDomain, + const char* securityDomain, UInt32 accountNameLength, - const char *accountName, - UInt32 pathLength, const char *path, + const char* accountName, + UInt32 pathLength, const char* path, UInt16 port, SecProtocolType protocol, SecAuthenticationType authenticationType, UInt32 passwordLength, - const void *passwordData, - SecKeychainItemRef *itemRef) const OVERRIDE; + const void* passwordData, + SecKeychainItemRef* itemRef) const OVERRIDE; virtual OSStatus FindGenericPassword( CFTypeRef keychainOrArray, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, - UInt32 *passwordLength, - void **passwordData, - SecKeychainItemRef *itemRef) const OVERRIDE; - virtual OSStatus ItemFreeContent(SecKeychainAttributeList *attrList, - void *data) const OVERRIDE; + const char* accountName, + UInt32* passwordLength, + void** passwordData, + SecKeychainItemRef* itemRef) const OVERRIDE; + virtual OSStatus ItemFreeContent(SecKeychainAttributeList* attrList, + void* data) const OVERRIDE; virtual OSStatus AddGenericPassword( SecKeychainRef keychain, UInt32 serviceNameLength, - const char *serviceName, + const char* serviceName, UInt32 accountNameLength, - const char *accountName, + const char* accountName, UInt32 passwordLength, - const void *passwordData, - SecKeychainItemRef *itemRef) const OVERRIDE; + const void* passwordData, + SecKeychainItemRef* itemRef) const OVERRIDE; virtual void Free(CFTypeRef ref) const OVERRIDE; // Return the counts of objects returned by Create/Copy functions but never @@ -127,11 +138,16 @@ class MockKeychain : public MacKeychain { // Returns true if the keychain already contains a password that matches the // attributes provided. bool AlreadyContainsInternetPassword( - UInt32 serverNameLength, const char *serverName, - UInt32 securityDomainLength, const char *securityDomain, - UInt32 accountNameLength, const char *accountName, - UInt32 pathLength, const char *path, - UInt16 port, SecProtocolType protocol, + UInt32 serverNameLength, + const char* serverName, + UInt32 securityDomainLength, + const char* securityDomain, + UInt32 accountNameLength, + const char* accountName, + UInt32 pathLength, + const char* path, + UInt16 port, + SecProtocolType protocol, SecAuthenticationType authenticationType) const; // Initializes storage for keychain data at |key|. void InitializeKeychainData(unsigned int key) const; @@ -158,7 +174,8 @@ class MockKeychain : public MacKeychain { // Returns the address of the attribute in attribute_list with tag |tag|. static SecKeychainAttribute* AttributeWithTag( - const SecKeychainAttributeList& attribute_list, UInt32 tag); + const SecKeychainAttributeList& attribute_list, + UInt32 tag); static const int kDummySearchRef = 1000; @@ -204,4 +221,6 @@ class MockKeychain : public MacKeychain { mutable std::string add_generic_password_; }; -#endif // CHROME_BROWSER_MOCK_KEYCHAIN_MAC_H_ +} // namespace crypto + +#endif // CRYPTO_MOCK_KEYCHAIN_MAC_H_ |