]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-item-helpers.h
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-item-helpers.h
1 /*
2 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include "kc-helpers.h"
25 #include "utilities/SecCFRelease.h"
26
27 #ifndef kc_item_helpers_h
28 #define kc_item_helpers_h
29
30 #if TARGET_OS_MAC
31
32 #pragma clang diagnostic push
33 #pragma clang diagnostic ignored "-Wunused-variable"
34 #pragma clang diagnostic ignored "-Wunused-function"
35
36 static CFMutableDictionaryRef makeBaseDictionary(CFStringRef itemclass) {
37 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
38 CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue);
39 CFDictionarySetValue(query, kSecClass, itemclass);
40
41 return query;
42 }
43
44 static CFMutableDictionaryRef convertToQuery(CFMutableDictionaryRef query, SecKeychainRef kc) {
45 CFMutableArrayRef searchList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
46 CFArrayAppendValue((CFMutableArrayRef)searchList, kc);
47 CFDictionarySetValue(query, kSecMatchSearchList, searchList);
48
49 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitAll);
50
51 return query;
52 }
53
54 static CFMutableDictionaryRef addLabel(CFMutableDictionaryRef query, CFStringRef label) {
55 CFDictionarySetValue(query, kSecAttrLabel, label);
56 return query;
57 }
58
59 static CFMutableDictionaryRef makeBaseItemDictionary(CFStringRef itemclass, CFStringRef service) {
60 CFMutableDictionaryRef query = makeBaseDictionary(itemclass);
61
62 if(CFEqual(itemclass, kSecClassInternetPassword)) {
63 CFDictionarySetValue(query, kSecAttrServer, service == NULL ? CFSTR("test_service") : service);
64 CFDictionarySetValue(query, kSecAttrAuthenticationType, CFSTR("dflt")); // Default, I guess?
65 } else {
66 // Generic passwords have services
67 CFDictionarySetValue(query, kSecAttrService, service == NULL ? CFSTR("test_service") : service);
68 }
69 return query;
70 }
71
72 static CFMutableDictionaryRef makeQueryItemDictionaryWithService(SecKeychainRef kc, CFStringRef itemclass, CFStringRef service) {
73 return convertToQuery(makeBaseItemDictionary(itemclass, service), kc);
74 }
75 static CFMutableDictionaryRef makeQueryItemDictionary(SecKeychainRef kc, CFStringRef itemclass) {
76 return makeQueryItemDictionaryWithService(kc, itemclass, NULL);
77 }
78
79 static CFMutableDictionaryRef makeBaseQueryDictionary(SecKeychainRef kc, CFStringRef itemclass) {
80 return convertToQuery(makeBaseDictionary(itemclass), kc);
81 }
82
83 static CFMutableDictionaryRef makeQueryCustomItemDictionaryWithService(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label, CFStringRef service) {
84 CFMutableDictionaryRef query = makeQueryItemDictionaryWithService(kc, itemclass, service);
85 CFDictionarySetValue(query, kSecAttrLabel, label);
86 return query;
87 }
88 static CFMutableDictionaryRef makeQueryCustomItemDictionary(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
89 return makeQueryCustomItemDictionaryWithService(kc, itemclass, label, NULL);
90 }
91
92 static CFMutableDictionaryRef makeAddCustomItemDictionaryWithService(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label, CFStringRef account, CFStringRef service) {
93 CFMutableDictionaryRef query = makeBaseItemDictionary(itemclass, service);
94
95 CFDictionaryAddValue(query, kSecUseKeychain, kc);
96 CFDictionarySetValue(query, kSecAttrAccount, account);
97 CFDictionarySetValue(query, kSecAttrComment, CFSTR("a comment"));
98 CFDictionarySetValue(query, kSecAttrLabel, label);
99 CFDictionarySetValue(query, kSecValueData, CFDataCreate(NULL, (void*)"data", 4));
100 return query;
101 }
102 static CFMutableDictionaryRef makeAddCustomItemDictionary(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label, CFStringRef account) {
103 return makeAddCustomItemDictionaryWithService(kc, itemclass, label, account, NULL);
104 }
105
106 static CFMutableDictionaryRef makeAddItemDictionary(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
107 return makeAddCustomItemDictionary(kc, itemclass, label, CFSTR("test_account"));
108 }
109
110 static SecKeychainItemRef makeCustomItem(const char* name, SecKeychainRef kc, CFDictionaryRef addDictionary) {
111 CFTypeRef result = NULL;
112 ok_status(SecItemAdd(addDictionary, &result), "%s: SecItemAdd", name);
113 ok(result != NULL, "%s: SecItemAdd returned a result", name);
114
115 SecKeychainItemRef item = (SecKeychainItemRef) result;
116 ok(item != NULL, "%s: Couldn't convert into SecKeychainItemRef", name);
117
118 return item;
119 }
120 #define makeCustomItemTests 3
121
122 static SecKeychainItemRef makeItem(const char* name, SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
123 CFMutableDictionaryRef query = makeAddItemDictionary(kc, itemclass, label);
124
125 SecKeychainItemRef item = makeCustomItem(name, kc, query);
126
127 CFReleaseNull(query);
128 return item;
129 }
130 #define makeItemTests makeCustomItemTests
131
132 static void makeCustomDuplicateItem(const char* name, SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
133 CFMutableDictionaryRef query = makeAddItemDictionary(kc, itemclass, label);
134
135 CFTypeRef result = NULL;
136 is(SecItemAdd(query, &result), errSecDuplicateItem, "%s: SecItemAdd (duplicate)", name);
137
138 CFReleaseNull(query);
139 }
140 #define makeCustomDuplicateItemTests 1
141
142 static void makeDuplicateItem(const char* name, SecKeychainRef kc, CFStringRef itemclass) {
143 return makeCustomDuplicateItem(name, kc, itemclass, CFSTR("test_label"));
144 }
145 #define makeDuplicateItemTests makeCustomDuplicateItemTests
146
147
148 #pragma clang pop
149 #else
150
151 #endif /* TARGET_OS_MAC */
152
153 #endif /* kc_item_helpers_h */