2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <Security/Security.h>
25 #import <Security/SecCertificatePriv.h>
27 #include "keychain_regressions.h"
28 #include "kc-helpers.h"
30 static void dumpSearchList(char * label
, CFArrayRef searchList
) {
31 printf("%s:\n", label
);
33 for(int i
= 0; i
< CFArrayGetCount(searchList
); i
++) {
35 UInt32 len
= sizeof(pathName
);
37 SecKeychainGetPath((SecKeychainRef
) CFArrayGetValueAtIndex(searchList
, i
), &len
, pathName
);
38 printf(" %s\n", pathName
);
43 static CFComparisonResult
compare(const void* first
, const void* second
, void* context
) {
44 SecKeychainRef k1
= (SecKeychainRef
) first
;
45 SecKeychainRef k2
= (SecKeychainRef
) second
;
49 UInt32 l1
= 200, l2
= 200;
51 SecKeychainGetPath(k1
, &l1
, path1
);
52 SecKeychainGetPath(k2
, &l2
, path2
);
54 return strcmp(path1
, path2
);
57 // Checks that these lists are equal modulo order
58 static bool keychainListsEqual(CFArrayRef list1
, CFArrayRef list2
) {
60 CFIndex size1
= CFArrayGetCount(list1
);
61 CFIndex size2
= CFArrayGetCount(list2
);
67 CFMutableArrayRef m1
= CFArrayCreateMutableCopy(NULL
, 0, list1
);
68 CFMutableArrayRef m2
= CFArrayCreateMutableCopy(NULL
, 0, list2
);
70 CFArraySortValues(m1
, CFRangeMake(0, size1
), &compare
, NULL
);
71 CFArraySortValues(m2
, CFRangeMake(0, size2
), &compare
, NULL
);
73 bool result
= CFEqual(m1
, m2
);
83 SecKeychainRef kc
= getPopulatedTestKeychain();
85 CFArrayRef searchList
= NULL
;
86 ok_status(SecKeychainCopySearchList(&searchList
), "%s: SecKeychainCopySearchList", testName
);
87 dumpSearchList("initial", searchList
);
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
);
94 CFArrayRef midSearchList
= NULL
;
95 ok_status(SecKeychainCopySearchList(&midSearchList
), "%s: SecKeychainCopySearchList (mid)", testName
);
96 dumpSearchList("after set", midSearchList
);
98 ok(keychainListsEqual(mutableSearchList
, midSearchList
), "%s: retrieved search list equal to set search list", testName
);
100 ok_status(SecKeychainDelete(kc
), "%s: SecKeychainDelete", testName
);
103 CFArrayRef finalSearchList
= NULL
;
104 ok_status(SecKeychainCopySearchList(&finalSearchList
), "%s: SecKeychainCopySearchList (final)", testName
);
105 dumpSearchList("final", finalSearchList
);
107 ok(keychainListsEqual(finalSearchList
, searchList
), "%s: final search list equal to initial search list", testName
);
109 CFRelease(searchList
);
110 CFRelease(mutableSearchList
);
111 CFRelease(midSearchList
);
112 CFRelease(finalSearchList
);
115 int kc_03_keychain_list(int argc
, char *const *argv
)
118 initializeKeychainTests(__FUNCTION__
);