blob: 1ddfc8679461c6fddf320642405dc31f98643c0b [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Ben Murdocheb525c52013-07-10 11:40:50 +01006#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00007#include "crypto/mock_apple_keychain.h"
8
9namespace crypto {
10
11OSStatus 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
35OSStatus MockAppleKeychain::ItemFreeContent(SecKeychainAttributeList* attrList,
36 void* data) const {
37 // No-op.
38 password_data_count_--;
39 return noErr;
40}
41
42OSStatus 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