1//
2//  SecdKeychainUtilities.c
3//  sec
4//
5//  Created by Mitch Adler on 6/11/13.
6//
7//
8
9#include "SecdTestKeychainUtilities.h"
10
11#include <test/testmore.h>
12#include <utilities/SecFileLocations.h>
13#include <utilities/SecCFWrappers.h>
14
15#include <CoreFoundation/CoreFoundation.h>
16
17//#include <stdlib.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <sys/stat.h>
21
22void kc_dbhandle_reset(void);
23
24void secd_test_setup_temp_keychain(const char* test_prefix, dispatch_block_t do_before_reset)
25{
26    CFStringRef tmp_dir = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("/tmp/%s.%X/"), test_prefix, arc4random());
27    CFStringRef keychain_dir = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@Library/Keychains"), tmp_dir);
28
29    CFStringPerformWithCString(keychain_dir, ^(const char *keychain_dir_string) {
30        ok_unix(mkpath_np(keychain_dir_string, 0755), "Create temp dir");
31
32        printf("Created temporary directory %s\n", keychain_dir_string);
33    });
34
35
36    /* set custom keychain dir, reset db */
37    CFStringPerformWithCString(tmp_dir, ^(const char *tmp_dir_string) {
38        SetCustomHomeURL(tmp_dir_string);
39    });
40
41    if(do_before_reset)
42        do_before_reset();
43
44    kc_dbhandle_reset();
45}
46