• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/logging.h"
6 #include "base/time/time.h"
7 #include "crypto/mock_apple_keychain.h"
8 
9 namespace crypto {
10 
FindGenericPassword(CFTypeRef keychainOrArray,UInt32 serviceNameLength,const char * serviceName,UInt32 accountNameLength,const char * accountName,UInt32 * passwordLength,void ** passwordData,SecKeychainItemRef * itemRef) const11 OSStatus MockAppleKeychain::FindGenericPassword(
12     CFTypeRef keychainOrArray,
13     UInt32 serviceNameLength,
14     const char* serviceName,
15     UInt32 accountNameLength,
16     const char* accountName,
17     UInt32* passwordLength,
18     void** passwordData,
19     SecKeychainItemRef* itemRef) const {
20   // When simulating |noErr|, return canned |passwordData| and
21   // |passwordLength|.  Otherwise, just return given code.
22   if (find_generic_result_ == noErr) {
23     static const char kPassword[] = "my_password";
24     DCHECK(passwordData);
25     // The function to free this data is mocked so the cast is fine.
26     *passwordData = const_cast<char*>(kPassword);
27     DCHECK(passwordLength);
28     *passwordLength = arraysize(kPassword);
29     password_data_count_++;
30   }
31 
32   return find_generic_result_;
33 }
34 
ItemFreeContent(SecKeychainAttributeList * attrList,void * data) const35 OSStatus MockAppleKeychain::ItemFreeContent(SecKeychainAttributeList* attrList,
36                                             void* data) const {
37   // No-op.
38   password_data_count_--;
39   return noErr;
40 }
41 
AddGenericPassword(SecKeychainRef keychain,UInt32 serviceNameLength,const char * serviceName,UInt32 accountNameLength,const char * accountName,UInt32 passwordLength,const void * passwordData,SecKeychainItemRef * itemRef) const42 OSStatus MockAppleKeychain::AddGenericPassword(
43     SecKeychainRef keychain,
44     UInt32 serviceNameLength,
45     const char* serviceName,
46     UInt32 accountNameLength,
47     const char* accountName,
48     UInt32 passwordLength,
49     const void* passwordData,
50     SecKeychainItemRef* itemRef) const {
51   called_add_generic_ = true;
52 
53   DCHECK_GT(passwordLength, 0U);
54   DCHECK(passwordData);
55   add_generic_password_ =
56       std::string(const_cast<char*>(static_cast<const char*>(passwordData)),
57                   passwordLength);
58   return noErr;
59 }
60 
61 }  // namespace crypto
62