2 * si-31-keychain-unreadable.c
5 * Created by Michael Brouwer on 5/23/08.
6 * Copyright (c) 2008-2010 Apple Inc. All Rights Reserved.
10 #include <CoreFoundation/CoreFoundation.h>
11 #include <Security/SecBase.h>
12 #include <Security/SecItem.h>
13 #include <Security/SecInternal.h>
21 #include "Security_regressions.h"
24 static void ensureKeychainExists(void) {
25 CFDictionaryRef query
= CFDictionaryCreate(0, &kSecClass
, &kSecClassInternetPassword
, 1, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
26 CFTypeRef results
= NULL
;
27 is_status(SecItemCopyMatching(query
, &results
), errSecItemNotFound
, "expected nothing got %@", results
);
29 CFReleaseNull(results
);
33 void kc_dbhandle_reset(void);
35 /* Create an empty keychain file that can't be read or written and make sure
36 securityd can deal with it. */
37 static void tests(void)
40 plan_skip_all("No testing against server.");
42 const char *home_dir
= getenv("HOME");
43 char keychain_dir
[1000];
44 char keychain_name
[1000];
45 sprintf(keychain_dir
, "%s/Library/Keychains", home_dir
);
46 sprintf(keychain_name
, "%s/keychain-2-debug.db", keychain_dir
);
48 ensureKeychainExists();
50 ok_unix(fd
= open(keychain_name
, O_RDWR
| O_CREAT
| O_TRUNC
, 0644),
51 "create keychain file '%s'", keychain_name
);
52 ok_unix(fchmod(fd
, 0), " keychain file '%s'", keychain_name
);
53 ok_unix(close(fd
), "close keychain file '%s'", keychain_name
);
58 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
59 const char *v_data
= "test";
60 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
61 CFMutableDictionaryRef query
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
62 CFDictionaryAddValue(query
, kSecClass
, kSecClassInternetPassword
);
63 CFDictionaryAddValue(query
, kSecAttrServer
, CFSTR("members.spamcop.net"));
64 CFDictionaryAddValue(query
, kSecAttrAccount
, CFSTR("smith"));
65 CFDictionaryAddValue(query
, kSecAttrPort
, eighty
);
66 CFDictionaryAddValue(query
, kSecAttrProtocol
, kSecAttrProtocolHTTP
);
67 CFDictionaryAddValue(query
, kSecAttrAuthenticationType
, kSecAttrAuthenticationTypeDefault
);
68 CFDictionaryAddValue(query
, kSecValueData
, pwdata
);
69 ok_status(SecItemAdd(query
, NULL
), "add internet password");
70 is_status(SecItemAdd(query
, NULL
), errSecDuplicateItem
,
71 "add internet password again");
73 ok_status(SecItemCopyMatching(query
, NULL
), "Found the item we added");
75 ok_status(SecItemDelete(query
),"Deleted the item we added");
77 CFReleaseSafe(eighty
);
78 CFReleaseSafe(pwdata
);
83 int si_31_keychain_unreadable(int argc
, char *const *argv
)