#include "secd_regressions.h"
#include <securityd/SecDbItem.h>
#include <securityd/SecItemServer.h>
#include <utilities/array_size.h>
#include <utilities/SecFileLocations.h>
#include <unistd.h>
#include "SecdTestKeychainUtilities.h"
#if USE_KEYSTORE
#include <libaks.h>
void kc_dbhandle_reset(void);
int secd_01_items(int argc, char *const *argv)
{
plan_tests(24 + kSecdTestSetupTestCount);
secd_test_setup_temp_keychain("secd_01_items", ^{
});
keybag_handle_t keybag;
keybag_state_t state;
char *passcode="password";
int passcode_len=(int)strlen(passcode);
ok(kIOReturnSuccess==aks_create_bag(passcode, passcode_len, kAppleKeyStoreDeviceBag, &keybag), "create keybag");
ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
ok(!(state&keybag_state_locked), "keybag unlocked");
SecItemServerSetKeychainKeybag(keybag);
ok(kIOReturnSuccess==aks_lock_bag(keybag), "lock keybag");
ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
ok(state&keybag_state_locked, "keybag locked");
kc_dbhandle_reset();
int v_eighty = 80;
CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
const char *v_data = "test";
CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
const void *keys[] = {
kSecClass,
kSecAttrServer,
kSecAttrAccount,
kSecAttrPort,
kSecAttrProtocol,
kSecAttrAuthenticationType,
kSecValueData
};
const void *values[] = {
kSecClassInternetPassword,
CFSTR("members.spamcop.net"),
CFSTR("smith"),
eighty,
CFSTR("http"),
CFSTR("dflt"),
pwdata
};
CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
array_size(keys), NULL, NULL);
is_status(SecItemAdd(item, NULL), errSecInteractionNotAllowed, "add internet password while locked");
ok(kIOReturnSuccess==aks_unlock_bag(keybag, passcode, passcode_len), "unlock keybag");
ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
ok(!(state&keybag_state_locked), "keybag unlocked");
ok_status(SecItemAdd(item, NULL), "add internet password, while unlocked");
ok(kIOReturnSuccess==aks_lock_bag(keybag), "lock keybag");
ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
ok(state&keybag_state_locked, "keybag locked");
is_status(SecItemAdd(item, NULL), errSecInteractionNotAllowed,
"add internet password again, while locked");
ok(kIOReturnSuccess==aks_unlock_bag(keybag, passcode, passcode_len), "unlock keybag");
ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
ok(!(state&keybag_state_locked), "keybag unlocked");
is_status(SecItemAdd(item, NULL), errSecDuplicateItem,
"add internet password again, while unlocked");
CFTypeRef results = NULL;
CFDictionaryRef query = CFDictionaryCreate(NULL, keys, values,
(array_size(keys)) - 1, NULL, NULL);
ok_status(SecItemCopyMatching(query, &results), "find internet password, while unlocked ");
if (results) {
CFRelease(results);
results = NULL;
}
ok(kIOReturnSuccess==aks_lock_bag(keybag), "lock keybag");
ok(kIOReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
ok(state&keybag_state_locked, "keybag locked");
is_status(SecItemCopyMatching(query, &results), errSecInteractionNotAllowed, "find internet password, while locked ");
SecItemServerResetKeychainKeybag();
SetCustomHomeURL(NULL);
kc_dbhandle_reset();
return 0;
}
#else
int secd_01_items(int argc, char *const *argv)
{
plan_tests(1);
todo("Not yet working in simulator");
TODO: {
ok(false);
}
return 0;
}
#endif