si-34-one-true-keychain.c [plain text]
#include <CoreFoundation/CoreFoundation.h>
#include <TargetConditionals.h>
#include <stdio.h>
#include "keychain_regressions.h"
#include <utilities/SecCFRelease.h>
#include <Security/SecBase.h>
#include <Security/SecItem.h>
#include <Security/SecItemPriv.h>
#include <utilities/SecCFRelease.h>
#include <libaks.h>
#include <AssertMacros.h>
static void tests(void)
{
int v_eighty = 80;
CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
const char *v_data = "test";
const char *v_data2 = "test";
CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
CFDataRef pwdata2 = CFDataCreate(NULL, (UInt8 *)v_data2, strlen(v_data2));
CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFTypeRef result = NULL;
CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net"));
CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
CFDictionaryAddValue(query, kSecAttrPort, eighty); CFReleaseNull(eighty);
CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
CFMutableDictionaryRef noLegacyQuery = CFDictionaryCreateMutableCopy(NULL, 0, query);
CFMutableDictionaryRef syncAnyQuery = CFDictionaryCreateMutableCopy(NULL, 0, query);
CFMutableDictionaryRef syncQuery = CFDictionaryCreateMutableCopy(NULL, 0, query);
CFDictionaryAddValue(noLegacyQuery, kSecUseDataProtectionKeychain, kCFBooleanTrue);
CFDictionaryAddValue(syncAnyQuery, kSecAttrSynchronizable, kSecAttrSynchronizableAny);
CFDictionaryAddValue(syncQuery, kSecAttrSynchronizable, kCFBooleanTrue);
SecItemDelete(query);
SecItemDelete(noLegacyQuery);
SecItemDelete(syncQuery);
SecItemDelete(syncAnyQuery);
CFDictionaryAddValue(query, kSecValueData, pwdata);
ok_status(SecItemAdd(query, NULL), "add internet password in OS X keychain");
CFDictionaryRemoveValue(query, kSecValueData);
ok_status(SecItemCopyMatching(query, &result), "find the osx item");
CFReleaseNull(result);
is_status(SecItemCopyMatching(noLegacyQuery, &result), errSecItemNotFound, "do not find the osx item with noLegacy");
CFReleaseNull(result);
ok_status(SecItemCopyMatching(syncAnyQuery, &result), "find the osx item with synchronizableAny");
CFReleaseNull(result);
is_status(SecItemCopyMatching(syncQuery, &result), errSecItemNotFound, "do not find the osx item with synchronizable");
CFReleaseNull(result);
CFMutableDictionaryRef toUpdate = CFDictionaryCreateMutable(NULL, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(toUpdate, kSecValueData, pwdata2);
CFReleaseNull(pwdata2);
ok_status(SecItemUpdate(query, toUpdate), "update the osx item");
is_status(SecItemUpdate(noLegacyQuery, toUpdate), errSecItemNotFound, "do not update the osx item with noLegacy");
ok_status(SecItemUpdate(syncAnyQuery, toUpdate), "update the osx item with synchronizableAny");
is_status(SecItemDelete(noLegacyQuery), errSecItemNotFound, "do not delete the osx item with noLegacy");
ok_status(SecItemDelete(syncAnyQuery), "delete the osx item with synchronizableAny");
CFDictionaryAddValue(noLegacyQuery, kSecValueData, pwdata);
CFReleaseNull(pwdata);
ok_status(SecItemAdd(noLegacyQuery, &result), "add internet password in iOS keychain");
CFDictionaryRemoveValue(noLegacyQuery, kSecValueData);
ok_status(SecItemCopyMatching(query, &result), "find the ios item with generic query");
CFReleaseNull(result);
ok_status(SecItemCopyMatching(noLegacyQuery, &result), "find the ios item with noLegacy");
CFReleaseNull(result);
ok_status(SecItemCopyMatching(syncAnyQuery, &result), "find the ios item with synchronizableAny");
CFReleaseNull(result);
is_status(SecItemCopyMatching(syncQuery, &result), errSecItemNotFound, "do not find the ios item with synchronizable");
CFReleaseNull(result);
ok_status(SecItemUpdate(query, toUpdate), "update the ios item without any flags");
ok_status(SecItemUpdate(noLegacyQuery, toUpdate), "update the ios item with noLegacy");
ok_status(SecItemUpdate(syncAnyQuery, toUpdate), "update the ios item with synchronizableAny");
CFDictionaryRemoveValue(noLegacyQuery, kSecValueData);
ok_status(SecItemDelete(noLegacyQuery), "delete the item with noLegacy");
CFReleaseNull(toUpdate);
CFReleaseNull(query);
CFReleaseNull(noLegacyQuery);
CFReleaseNull(syncQuery);
CFReleaseNull(syncAnyQuery);
}
int si_34_one_true_keychain(int argc, char *const *argv)
{
plan_tests(19);
tests();
return 0;
}