5 // Created by Fabrice Gautier on 5/29/13.
10 #include "secd_regressions.h"
12 #include <securityd/SecDbItem.h>
13 #include <securityd/SecItemServer.h>
15 #include <utilities/array_size.h>
16 #include <utilities/SecFileLocations.h>
20 #include "SecdTestKeychainUtilities.h"
24 void kc_dbhandle_reset(void);
26 int secd_01_items(int argc
, char *const *argv
)
28 plan_tests(24 + kSecdTestSetupTestCount
);
30 /* custom keychain dir */
31 secd_test_setup_temp_keychain("secd_01_items", ^{
35 keybag_handle_t keybag
;
37 char *passcode
="password";
38 int passcode_len
=(int)strlen(passcode
);
40 ok(kIOReturnSuccess
==aks_create_bag(passcode
, passcode_len
, kAppleKeyStoreDeviceBag
, &keybag
), "create keybag");
41 ok(kIOReturnSuccess
==aks_get_lock_state(keybag
, &state
), "get keybag state");
42 ok(!(state
&keybag_state_locked
), "keybag unlocked");
43 SecItemServerSetKeychainKeybag(keybag
);
46 ok(kIOReturnSuccess
==aks_lock_bag(keybag
), "lock keybag");
47 ok(kIOReturnSuccess
==aks_get_lock_state(keybag
, &state
), "get keybag state");
48 ok(state
&keybag_state_locked
, "keybag locked");
53 /* Creating a password */
55 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
56 const char *v_data
= "test";
57 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
58 const void *keys
[] = {
64 kSecAttrAuthenticationType
,
67 const void *values
[] = {
68 kSecClassInternetPassword
,
69 CFSTR("members.spamcop.net"),
76 CFDictionaryRef item
= CFDictionaryCreate(NULL
, keys
, values
,
77 array_size(keys
), NULL
, NULL
);
80 is_status(SecItemAdd(item
, NULL
), errSecInteractionNotAllowed
, "add internet password while locked");
83 ok(kIOReturnSuccess
==aks_unlock_bag(keybag
, passcode
, passcode_len
), "unlock keybag");
84 ok(kIOReturnSuccess
==aks_get_lock_state(keybag
, &state
), "get keybag state");
85 ok(!(state
&keybag_state_locked
), "keybag unlocked");
87 ok_status(SecItemAdd(item
, NULL
), "add internet password, while unlocked");
91 ok(kIOReturnSuccess
==aks_lock_bag(keybag
), "lock keybag");
92 ok(kIOReturnSuccess
==aks_get_lock_state(keybag
, &state
), "get keybag state");
93 ok(state
&keybag_state_locked
, "keybag locked");
95 is_status(SecItemAdd(item
, NULL
), errSecInteractionNotAllowed
,
96 "add internet password again, while locked");
99 ok(kIOReturnSuccess
==aks_unlock_bag(keybag
, passcode
, passcode_len
), "unlock keybag");
100 ok(kIOReturnSuccess
==aks_get_lock_state(keybag
, &state
), "get keybag state");
101 ok(!(state
&keybag_state_locked
), "keybag unlocked");
103 is_status(SecItemAdd(item
, NULL
), errSecDuplicateItem
,
104 "add internet password again, while unlocked");
106 CFTypeRef results
= NULL
;
107 /* Create a dict with all attrs except the data. */
108 CFDictionaryRef query
= CFDictionaryCreate(NULL
, keys
, values
,
109 (array_size(keys
)) - 1, NULL
, NULL
);
110 ok_status(SecItemCopyMatching(query
, &results
), "find internet password, while unlocked ");
117 ok(kIOReturnSuccess
==aks_lock_bag(keybag
), "lock keybag");
118 ok(kIOReturnSuccess
==aks_get_lock_state(keybag
, &state
), "get keybag state");
119 ok(state
&keybag_state_locked
, "keybag locked");
121 is_status(SecItemCopyMatching(query
, &results
), errSecInteractionNotAllowed
, "find internet password, while locked ");
123 /* Reset keybag and custom $HOME */
124 SecItemServerResetKeychainKeybag();
125 SetCustomHomeURL(NULL
);
133 int secd_01_items(int argc
, char *const *argv
)
137 todo("Not yet working in simulator");
143 /* not implemented in simulator (no keybag) */