]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-03-keychain-list.c
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-03-keychain-list.c
1 /*
2 * Copyright (c) 2016 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 xLicense.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #import <Security/Security.h>
25 #import <Security/SecCertificatePriv.h>
26
27 #include "keychain_regressions.h"
28 #include "kc-helpers.h"
29
30 static void dumpSearchList(char * label, CFArrayRef searchList) {
31 printf("%s:\n", label);
32
33 for(int i = 0; i < CFArrayGetCount(searchList); i++) {
34 char pathName[300];
35 UInt32 len = sizeof(pathName);
36
37 SecKeychainGetPath((SecKeychainRef) CFArrayGetValueAtIndex(searchList, i), &len, pathName);
38 printf(" %s\n", pathName);
39 }
40 printf("\n");
41 }
42
43 static CFComparisonResult compare(const void* first, const void* second, void* context) {
44 SecKeychainRef k1 = (SecKeychainRef) first;
45 SecKeychainRef k2 = (SecKeychainRef) second;
46
47 char path1[200];
48 char path2[200];
49 UInt32 l1 = 200, l2 = 200;
50
51 SecKeychainGetPath(k1, &l1, path1);
52 SecKeychainGetPath(k2, &l2, path2);
53
54 return strcmp(path1, path2);
55 }
56
57 // Checks that these lists are equal modulo order
58 static bool keychainListsEqual(CFArrayRef list1, CFArrayRef list2) {
59
60 CFIndex size1 = CFArrayGetCount(list1);
61 CFIndex size2 = CFArrayGetCount(list2);
62
63 if(size1 != size2) {
64 return false;
65 }
66
67 CFMutableArrayRef m1 = CFArrayCreateMutableCopy(NULL, 0, list1);
68 CFMutableArrayRef m2 = CFArrayCreateMutableCopy(NULL, 0, list2);
69
70 CFArraySortValues(m1, CFRangeMake(0, size1), &compare, NULL);
71 CFArraySortValues(m2, CFRangeMake(0, size2), &compare, NULL);
72
73 bool result = CFEqual(m1, m2);
74
75 CFRelease(m1);
76 CFRelease(m2);
77
78 return result;
79 }
80
81 static void tests()
82 {
83 SecKeychainRef kc = getPopulatedTestKeychain();
84
85 CFArrayRef searchList = NULL;
86 ok_status(SecKeychainCopySearchList(&searchList), "%s: SecKeychainCopySearchList", testName);
87 dumpSearchList("initial", searchList);
88
89 CFMutableArrayRef mutableSearchList = CFArrayCreateMutableCopy(NULL, CFArrayGetCount(searchList) + 1, searchList);
90 CFArrayAppendValue(mutableSearchList, kc);
91 ok_status(SecKeychainSetSearchList(mutableSearchList), "%s: SecKeychainSetSearchList", testName);
92 dumpSearchList("to set", mutableSearchList);
93
94 CFArrayRef midSearchList = NULL;
95 ok_status(SecKeychainCopySearchList(&midSearchList), "%s: SecKeychainCopySearchList (mid)", testName);
96 dumpSearchList("after set", midSearchList);
97
98 ok(keychainListsEqual(mutableSearchList, midSearchList), "%s: retrieved search list equal to set search list", testName);
99
100 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", testName);
101 CFReleaseNull(kc);
102
103 CFArrayRef finalSearchList = NULL;
104 ok_status(SecKeychainCopySearchList(&finalSearchList), "%s: SecKeychainCopySearchList (final)", testName);
105 dumpSearchList("final", finalSearchList);
106
107 ok(keychainListsEqual(finalSearchList, searchList), "%s: final search list equal to initial search list", testName);
108
109 CFRelease(searchList);
110 CFRelease(mutableSearchList);
111 CFRelease(midSearchList);
112 CFRelease(finalSearchList);
113 }
114
115 int kc_03_keychain_list(int argc, char *const *argv)
116 {
117 plan_tests(9);
118 initializeKeychainTests(__FUNCTION__);
119
120 tests();
121
122 deleteTestFiles();
123 return 0;
124 }