]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_keychain/regressions/kc-helpers.h
Security-58286.260.20.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-helpers.h
index b84d344ac43e757171bda8e8163f9aff94d5a4de..fe6fde0b62faf0d5564625d325c41883f488c3c7 100644 (file)
 
 #include "kc-keychain-file-helpers.h"
 
+extern char keychainFile[1000];
+extern char keychainDbFile[1000];
+extern char keychainTempFile[1000];
+extern char keychainName[1000];
+extern char testName[1000];
+
 /* redefine this since the headers are mixed up */
 static inline bool CFEqualSafe(CFTypeRef left, CFTypeRef right)
 {
@@ -42,233 +48,40 @@ static inline bool CFEqualSafe(CFTypeRef left, CFTypeRef right)
         return CFEqual(left, right);
 }
 
-static char keychainFile[1000];
-static char keychainDbFile[1000];
-static char keychainTempFile[1000];
-static char keychainName[1000];
-static char testName[1000];
-static uint32_t promptAttempts;
-
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wunused-variable"
-#pragma clang diagnostic ignored "-Wunused-function"
-
-static void startTest(const char* thisTestName) {
-    strlcpy(testName, thisTestName, sizeof(testName));
-}
+void startTest(const char* thisTestName);
 
-static void initializeKeychainTests(const char* thisTestName) {
-    const char *home_dir = getenv("HOME");
-    sprintf(keychainName, "test-%s.asdf", thisTestName);
-    sprintf(keychainFile, "%s/Library/Keychains/%s", home_dir, keychainName);
-    sprintf(keychainDbFile, "%s/Library/Keychains/%s-db", home_dir, keychainName);
-    sprintf(keychainTempFile, "%s/Library/Keychains/test_temp", home_dir);
-
-    deleteKeychainFiles(keychainFile);
-
-    startTest(thisTestName);
-
-    SecKeychainGetUserPromptAttempts(&promptAttempts);
-    SecKeychainSetUserInteractionAllowed(FALSE);
-}
+void initializeKeychainTests(const char* thisTestName);
 
 // Use this at the bottom of every test to make sure everything is gone
-static void deleteTestFiles() {
-    deleteKeychainFiles(keychainFile);
-}
-
-static SecKeychainRef CF_RETURNS_RETAINED getPopulatedTestKeychain() {
-    deleteKeychainFiles(keychainFile);
-
-    writeFile(keychainFile, test_keychain, sizeof(test_keychain));
-
-    SecKeychainRef kc = NULL;
-    ok_status(SecKeychainOpen(keychainFile, &kc), "%s: getPopulatedTestKeychain: SecKeychainOpen", testName);
-    ok_status(SecKeychainUnlock(kc, (UInt32) strlen(test_keychain_password), test_keychain_password, true), "%s: getPopulatedTestKeychain: SecKeychainUnlock", testName);
-    return kc;
-}
-#define getPopulatedTestKeychainTests 2
-
-static SecKeychainRef CF_RETURNS_RETAINED getEmptyTestKeychain() {
-    deleteKeychainFiles(keychainFile);
-
-    SecKeychainRef kc = NULL;
-    ok_status(SecKeychainCreate(keychainFile, (UInt32) strlen(test_keychain_password), test_keychain_password, false, NULL, &kc), "%s: getPopulatedTestKeychain: SecKeychainCreate", testName);
-    return kc;
-}
-#define getEmptyTestKeychainTests 1
-
-
-static void addToSearchList(SecKeychainRef keychain) {
-    CFArrayRef searchList = NULL;
-    SecKeychainCopySearchList(&searchList);
-    CFMutableArrayRef mutableSearchList = CFArrayCreateMutableCopy(NULL, CFArrayGetCount(searchList) + 1, searchList);
-    CFArrayAppendValue(mutableSearchList, keychain);
-    SecKeychainSetSearchList(mutableSearchList);
-    CFRelease(searchList);
-    CFRelease(mutableSearchList);
-}
+void deleteTestFiles(void);
 
+void addToSearchList(SecKeychainRef keychain);
 
 /* Checks to be sure there are N elements in this search, and returns the first
  * if it exists. */
-static SecKeychainItemRef checkNCopyFirst(char* testName, const CFDictionaryRef CF_CONSUMED query, uint32_t n) {
-    CFArrayRef results = NULL;
-    if(n > 0) {
-        ok_status(SecItemCopyMatching(query, (CFTypeRef*) &results), "%s: SecItemCopyMatching", testName);
-    } else {
-        is(SecItemCopyMatching(query, (CFTypeRef*) &results), errSecItemNotFound, "%s: SecItemCopyMatching (for no items)", testName);
-    }
-
-    SecKeychainItemRef item = NULL;
-    if(results) {
-        is(CFArrayGetCount(results), n, "%s: Wrong number of results", testName);
-        if(n >= 1) {
-            ok(item = (SecKeychainItemRef) CFArrayGetValueAtIndex(results, 0), "%s: Couldn't get item", testName);
-        } else {
-            pass("make test numbers match");
-        }
-    } else if((!results) && n == 0) {
-        pass("%s: no results found (and none expected)", testName);
-        pass("make test numbers match");
-    } else {
-        fail("%s: no results found (and %d expected)", testName, n);
-        fflush(stdout); CFShow(query); fflush(stdout);
-        pass("make test numbers match");
-    }
-
-    CFRetainSafe(item);
-    CFReleaseNull(results);
-
-    CFRelease(query);
-    return item;
-}
-
-static void checkN(char* testName, const CFDictionaryRef CF_CONSUMED query, uint32_t n) {
-    SecKeychainItemRef item = checkNCopyFirst(testName, query, n);
-    CFReleaseSafe(item);
-}
+SecKeychainItemRef checkNCopyFirst(char* testName, const CFDictionaryRef CF_CONSUMED query, uint32_t n);
 
+void checkN(char* testName, const CFDictionaryRef CF_CONSUMED query, uint32_t n);
 #define checkNTests 3
 
-
-static void readPasswordContentsWithResult(SecKeychainItemRef item, OSStatus expectedResult, CFStringRef expectedContents) {
-    if(!item) {
-        fail("no item passed to readPasswordContentsWithResult");
-        fail("Match test numbers");
-        fail("Match test numbers");
-        return;
-    }
-
-    CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-
-    CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitOne);
-    CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
-
-    CFMutableArrayRef itemList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
-    CFArrayAppendValue((CFMutableArrayRef)itemList, item);
-    CFDictionarySetValue(query, kSecUseItemList, itemList);
-
-    CFTypeRef results = NULL;
-    if(expectedContents) {
-        is(SecItemCopyMatching(query, (CFTypeRef*) &results), expectedResult, "%s: readPasswordContents: SecItemCopyMatching", testName);
-        CFReleaseNull(query);
-
-        if(results) {
-            ok(CFGetTypeID(results) == CFDataGetTypeID(), "%s: result is not a data", testName);
-
-            CFDataRef data = (CFDataRef) results;
-            CFStringRef str = CFStringCreateWithBytes(NULL, CFDataGetBytePtr(data), CFDataGetLength(data), kCFStringEncodingUTF8, false);
-            eq_cf(str, expectedContents, "%s: contents do not match", testName);
-            CFReleaseNull(str);
-            CFReleaseNull(results);
-        } else {
-            fail("Didn't get any results");
-            fail("Match test numbers");
-        }
-    } else {
-        is(SecItemCopyMatching(query, (CFTypeRef*) &results), expectedResult, "%s: readPasswordContents: expecting error %d", testName, (int) expectedResult);
-        pass("Match test numbers");
-        pass("Match test numbers");
-    }
-}
+void readPasswordContentsWithResult(SecKeychainItemRef item, OSStatus expectedResult, CFStringRef expectedContents);
 #define readPasswordContentsWithResultTests 3
 
-static void readPasswordContents(SecKeychainItemRef item, CFStringRef expectedContents) {
-    return readPasswordContentsWithResult(item, errSecSuccess, expectedContents);
-}
+void readPasswordContents(SecKeychainItemRef item, CFStringRef expectedContents);
 #define readPasswordContentsTests readPasswordContentsWithResultTests
 
-static void changePasswordContents(SecKeychainItemRef item, CFStringRef newPassword) {
-    if(!item) {
-        fail("no item passed to changePasswordContents");
-        return;
-    }
-
-    CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-
-    CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitOne);
-
-    CFMutableArrayRef itemList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
-    CFArrayAppendValue((CFMutableArrayRef)itemList, item);
-    CFDictionarySetValue(query, kSecUseItemList, itemList);
-
-    CFMutableDictionaryRef attrs = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-    CFDataRef data = CFDataCreate(NULL, (const UInt8*) CFStringGetCStringPtr(newPassword, kCFStringEncodingUTF8), CFStringGetLength(newPassword));
-    CFDictionarySetValue(attrs, kSecValueData, data);
-    CFReleaseNull(data);
-
-    ok_status(SecItemUpdate(query, attrs), "%s: SecItemUpdate", testName);
-}
+void changePasswordContents(SecKeychainItemRef item, CFStringRef newPassword);
 #define changePasswordContentsTests 1
 
-static void deleteItem(SecKeychainItemRef item) {
-    CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-
-    CFMutableArrayRef itemList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
-    CFArrayAppendValue((CFMutableArrayRef)itemList, item);
-    CFDictionarySetValue(query, kSecUseItemList, itemList);
-
-    ok_status(SecItemDelete(query), "%s: SecItemDelete single item", testName);
-    CFReleaseNull(query);
-}
+void deleteItem(SecKeychainItemRef item);
 #define deleteItemTests 1
 
-static void deleteItems(CFArrayRef items) {
-    if(!items) {
-        fail("no items passed to deleteItems");
-        return;
-    }
-
-    CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-    CFDictionarySetValue(query, kSecUseItemList, items);
-
-    size_t count = (size_t) CFArrayGetCount(items);
-    if(count > 0) {
-        ok_status(SecItemDelete(query), "%s: SecItemDelete %ld items", testName, count);
-    } else {
-        is(SecItemDelete(query), errSecItemNotFound, "%s: SecItemDelete no items", testName);
-    }
-    CFReleaseNull(query);
-}
+void deleteItems(CFArrayRef items);
 #define deleteItemsTests 1
 
 /* Checks in with securityd to see how many prompts were generated since the last call to this function, and tests against the number expected.
  Returns the number generated since the last call. */
-static uint32_t checkPrompts(uint32_t expectedSinceLastCall, char* explanation) {
-    uint32_t currentPrompts = UINT_MAX;
-    uint32_t newPrompts = UINT_MAX;
-    ok_status(SecKeychainGetUserPromptAttempts(&currentPrompts), "%s: SecKeychainGetUserPromptAttempts", testName);
-
-    newPrompts = currentPrompts - promptAttempts;
-
-    is(newPrompts, expectedSinceLastCall, "%s: wrong number of prompts: %s", testName, explanation);
-    promptAttempts = currentPrompts;
-
-    return newPrompts;
-}
+uint32_t checkPrompts(uint32_t expectedSinceLastCall, char* explanation);
 #define checkPromptsTests 2
 
-#pragma clang diagnostic pop
-
 #endif /* kc_helpers_h */