]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-13-item-system.m
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / secitem / si-13-item-system.m
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 #define __KEYCHAINCORE__ 1
25
26 #include <Foundation/Foundation.h>
27 #include <CoreFoundation/CoreFoundation.h>
28 #include <Security/Security.h>
29 #include <Security/SecItemPriv.h>
30 #include <utilities/array_size.h>
31 #include <utilities/SecCFRelease.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35 #include "Security_regressions.h"
36
37
38 /* Test add api in all it's variants. */
39 static void tests(void)
40 {
41 NSDictionary *item, *query;
42 CFTypeRef result = NULL;
43 NSDictionary *whoami = NULL;
44
45 whoami = CFBridgingRelease(_SecSecuritydCopyWhoAmI(NULL));
46
47 NSLog(@"whoami: %@", whoami);
48
49 /*
50 * first clean some
51 */
52
53 query = @{
54 (id)kSecClass : (id)kSecClassGenericPassword,
55 (id)kSecAttrLabel : @"keychain label",
56 };
57 SecItemDelete((CFDictionaryRef)query);
58
59 query = @{
60 (id)kSecClass : (id)kSecClassGenericPassword,
61 (id)kSecAttrLabel : @"keychain label",
62 (id)kSecUseSystemKeychain : @YES,
63 };
64 SecItemDelete((CFDictionaryRef)query);
65
66 /*
67 * Add entry
68 */
69
70 item = @{
71 (id)kSecClass : (id)kSecClassGenericPassword,
72 (id)kSecAttrLabel : @"keychain label",
73 (id)kSecUseSystemKeychain : @YES,
74 };
75
76 ok_status(SecItemAdd((CFDictionaryRef)item, NULL), "SecItemAdd");
77
78 /*
79 * Check for multi user mode and its expected behavior (since its different)
80 */
81
82 bool multiUser = (whoami[@"musr"]) ? true : false;
83
84 /*
85 * Check we can't find it in our keychain
86 */
87
88 query = @{
89 (id)kSecClass : (id)kSecClassGenericPassword,
90 (id)kSecAttrLabel : @"keychain label",
91 };
92
93 is(SecItemCopyMatching((CFTypeRef)query, &result), multiUser ? errSecItemNotFound : noErr, "SecItemCopyMatching");
94 CFReleaseNull(result);
95 if (multiUser) {
96 is(SecItemDelete((CFTypeRef)query), errSecItemNotFound, "SecItemDelete");
97 } else {
98 ok(true, "dummy");
99 }
100
101 /*
102 * Check we can find it in system keychain
103 */
104
105 query = @{
106 (id)kSecClass : (id)kSecClassGenericPassword,
107 (id)kSecAttrLabel : @"keychain label",
108 (id)kSecUseSystemKeychain : @YES,
109 };
110
111 ok_status(SecItemCopyMatching((CFTypeRef)query, &result), "SecItemCopyMatching(system)");
112
113 ok_status(SecItemDelete((CFTypeRef)query), "SecItemDelete(system)");
114 }
115
116 int si_13_item_system(int argc, char *const *argv)
117 {
118 plan_tests(5);
119
120 @autoreleasepool {
121 tests();
122 }
123
124 return 0;
125 }