kc-20-identity-find-stress.c [plain text]
#import <Security/Security.h>
#import <Security/SecCertificatePriv.h>
#include "keychain_regressions.h"
#include "kc-helpers.h"
#include "kc-identity-helpers.h"
#include <Security/Security.h>
#include <stdlib.h>
#define BLOCKS 7000
static long concurrentBlocks = 64;
static void tests() {
SecKeychainRef kc = getPopulatedTestKeychain();
SecIdentityRef identity = NULL;
SecCertificateRef certRef = NULL;
SecKeyRef keyRef = NULL;
identity = copyFirstIdentity(kc);
ok_status(SecIdentityCopyCertificate(identity, &certRef), "%s: SecIdentityCopyCertificate", testName);
ok_status(SecIdentityCopyPrivateKey(identity, &keyRef), "%s: SecIdentityCopyPrivateKey", testName);
CFReleaseNull(identity);
CFReleaseNull(keyRef);
static dispatch_once_t onceToken = 0;
static dispatch_queue_t release_queue = NULL;
dispatch_once(&onceToken, ^{
release_queue = dispatch_queue_create("com.apple.security.identity-search-queue", DISPATCH_QUEUE_CONCURRENT);
});
dispatch_group_t g = dispatch_group_create();
__block int iteration = 0;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(concurrentBlocks);
dispatch_block_t identityBlock = ^() {
SecIdentityRef blockId = NULL;
SecKeyRef blockKeyRef = NULL;
ok_status(SecIdentityCreateWithCertificate(kc, certRef, &blockId), "%s: SecIdentityCreateWithCertificate", testName);
ok_status(SecIdentityCopyPrivateKey(blockId, &blockKeyRef), "%s: SecIdentityCopyPrivateKey", testName);
CFReleaseNull(blockKeyRef);
CFReleaseNull(blockId);
dispatch_semaphore_signal(semaphore);
};
dispatch_group_async(g, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (iteration = 0; iteration < BLOCKS; iteration++) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_group_async(g, release_queue, identityBlock);
}
});
dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", testName);
CFReleaseNull(kc);
}
int kc_20_identity_find_stress(int argc, char *const *argv)
{
plan_tests(2*BLOCKS + 6);
initializeKeychainTests(__FUNCTION__);
tests();
deleteTestFiles();
return 0;
}