]> git.saurik.com Git - apple/security.git/blob - tests/secdmockaks/mockaksxcbase.m
Security-59754.80.3.tar.gz
[apple/security.git] / tests / secdmockaks / mockaksxcbase.m
1
2 //
3 // mocksecdbase.m
4 // Security
5 //
6
7 #import "SecKeybagSupport.h"
8 #import "SecDbKeychainItem.h"
9 #import "SecdTestKeychainUtilities.h"
10 #import "CKKS.h"
11 #import "SecItemPriv.h"
12 #import "SecItemServer.h"
13 #import "spi.h"
14 #include <ipc/server_security_helpers.h>
15 #import <utilities/SecCFWrappers.h>
16 #import <utilities/SecFileLocations.h>
17 #import <SecurityFoundation/SFEncryptionOperation.h>
18 #import <XCTest/XCTest.h>
19 #import <OCMock/OCMock.h>
20 #import <sqlite3.h>
21 #import "mockaks.h"
22
23 #import "mockaksxcbase.h"
24
25 NSString* homeDirUUID;
26
27 @interface mockaksxcbase ()
28 @property NSArray<NSString *>* originalAccessGroups;
29 @property NSMutableArray<NSString *>* currentAccessGroups;
30 @end
31
32
33 @implementation mockaksxcbase
34
35 + (void)setUp
36 {
37 [super setUp];
38
39 securityd_init_local_spi();
40 securityd_init(NULL);
41
42 SecCKKSDisable();
43 /*
44 * Disable all of SOS syncing since that triggers retains of database
45 * and these tests muck around with the database over and over again, so
46 * that leads to the vnode delete kevent trap triggering for sqlite
47 * over and over again.
48 */
49 #if OCTAGON
50 SecCKKSTestSetDisableSOS(true);
51 #endif
52
53 // Give this test run a UUID within which each test has a directory
54 homeDirUUID = [[NSUUID UUID] UUIDString];
55 }
56
57 - (void)addAccessGroup:(NSString *)accessGroup
58 {
59 [self.currentAccessGroups addObject:accessGroup];
60 SecAccessGroupsSetCurrent((__bridge CFArrayRef)self.currentAccessGroups);
61 }
62
63 - (NSString*)createKeychainDirectoryWithSubPath:(NSString*)suffix
64 {
65 NSError* error;
66 NSString* dir = suffix ? [NSString stringWithFormat:@"%@/%@/", self.testHomeDirectory, suffix] : self.testHomeDirectory;
67 [[NSFileManager defaultManager] createDirectoryAtPath:dir
68 withIntermediateDirectories:YES
69 attributes:nil
70 error:&error];
71 XCTAssertNil(error, "Unable to create directory: %@", error);
72 return dir;
73 }
74
75 - (void)createKeychainDirectory
76 {
77 [self createKeychainDirectoryWithSubPath:nil];
78 }
79
80 - (void)setUp {
81 [super setUp];
82 self.originalAccessGroups = (__bridge NSArray *)SecAccessGroupsGetCurrent();
83 self.currentAccessGroups = [self.originalAccessGroups mutableCopy];
84
85 NSString* testName = [self.name componentsSeparatedByString:@" "][1];
86 testName = [testName stringByReplacingOccurrencesOfString:@"]" withString:@""];
87 secnotice("ckkstest", "Beginning test %@", testName);
88
89 self.testHomeDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@/", homeDirUUID, testName]];
90 [self createKeychainDirectory];
91
92 SetCustomHomeURLString((__bridge CFStringRef) self.testHomeDirectory);
93 SecKeychainDbReset(NULL);
94
95 // Actually load the database.
96 kc_with_dbt(true, NULL, ^bool (SecDbConnectionRef dbt) { return false; });
97 }
98
99 - (void)tearDown
100 {
101 SecAccessGroupsSetCurrent((__bridge CFArrayRef)self.originalAccessGroups);
102 [super tearDown];
103 }
104
105 + (void)tearDown
106 {
107 SetCustomHomeURLString(NULL);
108 SecKeychainDbReset(NULL);
109 }
110
111
112
113 @end