2 // Copyright 2016 Apple. All rights reserved.
6 * This is to fool os services to not provide the Keychain manager
7 * interface tht doens't work since we don't have unified headers
8 * between iOS and OS X. rdar://23405418/
10 #define __KEYCHAINCORE__ 1
12 #include <Foundation/Foundation.h>
13 #include <Security/Security.h>
14 #include <Security/SecItemPriv.h>
15 #include <Security/SecBasePriv.h>
16 #include <Security/SecIdentityPriv.h>
17 #include <mach/mach_time.h>
21 #if SEC_OS_OSX_INCLUDES
22 #include <Security/SecKeychain.h>
26 fail(const char *fmt, ...) __printflike(1, 2) __attribute__((noreturn));
30 fail(const char *fmt, ...)
42 * Create item w/o data, try to make sure we end up in the OS X keychain
46 CheckItemAddDeleteMaybeLegacyKeychainNoData(void)
50 printf("[BEGIN] %s\n", __FUNCTION__);
52 NSDictionary *query = @{
53 (id)kSecClass : (id)kSecClassGenericPassword,
54 (id)kSecAttrAccount : @"item-delete-me",
55 (id)kSecAttrAccessible : (id)kSecAttrAccessibleAfterFirstUnlock,
57 status = SecItemDelete((__bridge CFDictionaryRef)query);
58 if (status != errSecSuccess && status != errSecItemNotFound)
59 fail("cleanup item: %d", (int)status);
62 * now check add notification
65 status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
66 if (status != errSecSuccess)
67 fail("add item: %d: %s", (int)status, [[query description] UTF8String]);
73 status = SecItemDelete((__bridge CFDictionaryRef)query);
74 if (status != errSecSuccess)
75 fail("cleanup2 item: %d", (int)status);
78 printf("[PASS] %s\n", __FUNCTION__);
83 CheckItemAddDeleteNoData(void)
87 printf("[BEGIN] %s\n", __FUNCTION__);
89 NSDictionary *query = @{
90 (id)kSecClass : (id)kSecClassGenericPassword,
91 (id)kSecAttrAccessGroup : @"keychain-test1",
92 (id)kSecAttrAccount : @"item-delete-me",
93 (id)kSecAttrAccessible : (id)kSecAttrAccessibleAfterFirstUnlock,
95 status = SecItemDelete((__bridge CFDictionaryRef)query);
96 if (status != errSecSuccess && status != errSecItemNotFound)
97 fail("cleanup item: %d", (int)status);
103 status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
104 if (status != errSecSuccess)
105 fail("add item: %d: %s", (int)status, [[query description] UTF8String]);
111 status = SecItemDelete((__bridge CFDictionaryRef)query);
112 if (status != errSecSuccess)
113 fail("cleanup2 item: %d", (int)status);
115 printf("[PASS] %s\n", __FUNCTION__);
119 CheckItemUpdateAccessGroupGENP(void)
123 printf("[BEGIN] %s\n", __FUNCTION__);
125 NSDictionary *clean1 = @{
126 (id)kSecClass : (id)kSecClassGenericPassword,
127 (id)kSecAttrAccessGroup : @"keychain-test1",
129 NSDictionary *clean2 = @{
130 (id)kSecClass : (id)kSecClassGenericPassword,
131 (id)kSecAttrAccessGroup : @"keychain-test2",
134 (void)SecItemDelete((__bridge CFDictionaryRef)clean1);
135 (void)SecItemDelete((__bridge CFDictionaryRef)clean2);
141 NSDictionary *add = @{
142 (id)kSecClass : (id)kSecClassGenericPassword,
143 (id)kSecAttrAccessGroup : @"keychain-test1",
144 (id)kSecAttrAccount : @"item-delete-me",
145 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
146 (id)kSecAttrAccessible : (id)kSecAttrAccessibleAfterFirstUnlock,
148 status = SecItemAdd((__bridge CFDictionaryRef)add, NULL);
149 if (status != errSecSuccess)
150 fail("add item: %d: %s", (int)status, [[add description] UTF8String]);
153 * Update access group
155 NSDictionary *query = @{
156 (id)kSecClass : (id)kSecClassGenericPassword,
157 (id)kSecAttrAccessGroup : @"keychain-test1",
158 (id)kSecAttrAccount : @"item-delete-me",
159 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
161 NSDictionary *modified = @{
162 (id)kSecAttrAccessGroup : @"keychain-test2",
165 status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)modified);
166 if (status != errSecSuccess)
167 fail("cleanup2 item: %d", (int)status);
172 NSDictionary *check1 = @{
173 (id)kSecClass : (id)kSecClassGenericPassword,
174 (id)kSecAttrAccessGroup : @"keychain-test1",
175 (id)kSecAttrAccount : @"item-delete-me",
176 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
178 status = SecItemCopyMatching((__bridge CFDictionaryRef)check1, NULL);
179 if (status != errSecItemNotFound)
180 fail("check1 item: %d", (int)status);
183 NSDictionary *check2 = @{
184 (id)kSecClass : (id)kSecClassGenericPassword,
185 (id)kSecAttrAccessGroup : @"keychain-test2",
186 (id)kSecAttrAccount : @"item-delete-me",
187 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
189 status = SecItemCopyMatching((__bridge CFDictionaryRef)check2, NULL);
190 if (status != errSecSuccess)
191 fail("check2 item: %d", (int)status);
196 (void)SecItemDelete((__bridge CFDictionaryRef)clean1);
197 (void)SecItemDelete((__bridge CFDictionaryRef)clean2);
199 printf("[PASS] %s\n", __FUNCTION__);
202 static NSString *certDataBase64 = @"\
203 MIIEQjCCAyqgAwIBAgIJAJdFadWqNIfiMA0GCSqGSIb3DQEBBQUAMHMxCzAJBgNVBAYTAkNaMQ8wDQYD\
204 VQQHEwZQcmFndWUxFTATBgNVBAoTDENvc21vcywgSW5jLjEXMBUGA1UEAxMOc3VuLmNvc21vcy5nb2Qx\
205 IzAhBgkqhkiG9w0BCQEWFHRoaW5nQHN1bi5jb3Ntb3MuZ29kMB4XDTE2MDIyNjE0NTQ0OVoXDTE4MTEy\
206 MjE0NTQ0OVowczELMAkGA1UEBhMCQ1oxDzANBgNVBAcTBlByYWd1ZTEVMBMGA1UEChMMQ29zbW9zLCBJ\
207 bmMuMRcwFQYDVQQDEw5zdW4uY29zbW9zLmdvZDEjMCEGCSqGSIb3DQEJARYUdGhpbmdAc3VuLmNvc21v\
208 cy5nb2QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC5u9gnYEDzQIVu7yC40VcXTZ01D9CJ\
209 oD/mH62tebEHEdfVPLWKeq+uAHnJ6fTIJQvksaISOxwiOosFjtI30mbe6LZ/oK22wYX+OUwKhAYjZQPy\
210 RYfuaJe/52F0zmfUSJ+KTbUZrXbV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ivwfYdyb5If3tHhP4VkdIM5dABnCWoVOWy/NvA7XtE+KF/fItuGqKRP\
240 WCGaiRHoEeqZ23SQm5VmvdF7OXNi/R5LiQ3o4QKBgAGX8qg2TTrRR33ksgGbbyi1UJrWC3/TqWWTjbEY\
241 uU51OS3jvEQ3ImdjjM3EtPW7LqHSxUhjGZjvYMk7bZefrIGgkOHx2IRRkotcn9ynKURbD+mcE249beuc\
242 6cFTJVTrXGcFvqomPWtV895A2JzECQZvt1ja88uuu/i2YoHDQdGJAoGAL2TEgiMXiunb6PzYMMKKa+mx\
243 mFnagF0Ek3UJ9ByXKoLz3HFEl7cADIkqyenXFsAER/ifMyCoZp/PDBd6ZkpqLTdH0jQ2Yo4SllLykoiZ\
244 fBWMfjRu4iw9E0MbPB3blmtzfv53BtWKy0LUOlN4juvpqryA7TgaUlZkfMT+T1TC7xU=\
248 static SecIdentityRef
249 CreateTestIdentity(void)
251 NSData *certData = [[NSData alloc] initWithBase64EncodedString:certDataBase64 options:0];
252 SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
254 fail("create certificate from data");
256 NSData *keyData = [[NSData alloc] initWithBase64EncodedString:keyDataBase64 options:0];
257 NSDictionary *keyAttrs = @{
258 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
259 (id)kSecAttrKeySizeInBits: @2048,
260 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate
262 SecKeyRef privateKey = SecKeyCreateWithData((CFDataRef)keyData, (CFDictionaryRef)keyAttrs, NULL);
263 if (privateKey == NULL)
264 fail("create private key from data");
266 // Create identity from certificate and private key.
267 SecIdentityRef identity = SecIdentityCreate(kCFAllocatorDefault, cert, privateKey);
268 CFRelease(privateKey);
275 CheckIdentityItem(NSString *accessGroup, OSStatus expectedStatus)
279 NSDictionary *check = @{
280 (id)kSecClass : (id)kSecClassIdentity,
281 (id)kSecAttrAccessGroup : accessGroup,
282 (id)kSecAttrLabel : @"item-delete-me",
283 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
285 status = SecItemCopyMatching((__bridge CFDictionaryRef)check, NULL);
286 if (status != expectedStatus)
287 fail("check %s for %d item: %d", [accessGroup UTF8String], (int)expectedStatus, (int)status);
291 CheckItemUpdateAccessGroupIdentity(void)
294 CFTypeRef ref = NULL;
296 printf("[BEGIN] %s\n", __FUNCTION__);
298 NSDictionary *clean1 = @{
299 (id)kSecClass : (id)kSecClassIdentity,
300 (id)kSecAttrAccessGroup : @"keychain-test1",
302 NSDictionary *clean2 = @{
303 (id)kSecClass : (id)kSecClassIdentity,
304 (id)kSecAttrAccessGroup : @"keychain-test2",
307 (void)SecItemDelete((__bridge CFDictionaryRef)clean1);
308 (void)SecItemDelete((__bridge CFDictionaryRef)clean2);
310 CheckIdentityItem(@"keychain-test1", errSecItemNotFound);
311 CheckIdentityItem(@"keychain-test2", errSecItemNotFound);
313 SecIdentityRef identity = CreateTestIdentity();
314 if (identity == NULL)
315 fail("create private key from data");
322 NSDictionary *add = @{
323 (id)kSecValueRef : (__bridge id)identity,
324 (id)kSecAttrAccessGroup : @"keychain-test1",
325 (id)kSecAttrLabel : @"item-delete-me",
326 (id)kSecAttrAccessible : (id)kSecAttrAccessibleAfterFirstUnlock,
327 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
328 (id)kSecReturnPersistentRef: (id)kCFBooleanTrue,
330 status = SecItemAdd((__bridge CFDictionaryRef)add, &ref);
331 if (status != errSecSuccess)
332 fail("add item: %d: %s", (int)status, [[add description] UTF8String]);
337 CheckIdentityItem(@"keychain-test1", errSecSuccess);
338 CheckIdentityItem(@"keychain-test2", errSecItemNotFound);
342 * Update access group
344 NSDictionary *query = @{
345 (id)kSecClass : (id)kSecClassIdentity,
346 (id)kSecAttrAccessGroup : @"keychain-test1",
347 (id)kSecAttrLabel : @"item-delete-me",
348 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
350 NSDictionary *modified = @{
351 (id)kSecAttrAccessGroup : @"keychain-test2",
354 status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)modified);
355 if (status != errSecSuccess)
356 fail("cleanup2 item: %d", (int)status);
362 CheckIdentityItem(@"keychain-test1", errSecItemNotFound);
363 CheckIdentityItem(@"keychain-test2", errSecSuccess);
368 CFDataRef data = NULL;
370 NSDictionary *prefQuery = @{
371 (id)kSecClass : (id)kSecClassIdentity,
372 (id)kSecAttrAccessGroup : @"keychain-test2",
373 (id)kSecAttrLabel : @"item-delete-me",
374 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
375 (id)kSecReturnPersistentRef : (id)kCFBooleanTrue,
377 status = SecItemCopyMatching((__bridge CFDictionaryRef)prefQuery, (CFTypeRef *)&data);
378 if (status != errSecSuccess)
379 fail("prefQuery item: %d", (int)status);
382 * Update access group for identity
384 NSDictionary *query2 = @{
385 (id)kSecValuePersistentRef : (__bridge id)data,
386 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
388 NSDictionary *modified2 = @{
389 (id)kSecAttrAccessGroup : @"keychain-test1",
392 status = SecItemUpdate((__bridge CFDictionaryRef)query2, (__bridge CFDictionaryRef)modified2);
393 if (status != errSecInternal)
394 fail("update identity with pref fails differntly: %d", (int)status);
397 CheckIdentityItem(@"keychain-test1", errSecSuccess);
398 CheckIdentityItem(@"keychain-test2", errSecItemNotFound);
405 (void)SecItemDelete((__bridge CFDictionaryRef)clean1);
406 (void)SecItemDelete((__bridge CFDictionaryRef)clean2);
410 CheckIdentityItem(@"keychain-test1", errSecItemNotFound);
411 CheckIdentityItem(@"keychain-test2", errSecItemNotFound);
414 printf("[PASS] %s\n", __FUNCTION__);
418 CheckFindIdentityByReference(void)
421 CFDataRef pref = NULL, pref2 = NULL;
423 printf("[BEGIN] %s\n", __FUNCTION__);
428 NSDictionary *clean1 = @{
429 (id)kSecClass : (id)kSecClassIdentity,
430 (id)kSecAttrAccessGroup : @"keychain-test1",
432 (void)SecItemDelete((__bridge CFDictionaryRef)clean1);
437 SecIdentityRef identity = CreateTestIdentity();
438 if (identity == NULL)
439 fail("create private key from data");
442 NSDictionary *add = @{
443 (id)kSecValueRef : (__bridge id)identity,
444 (id)kSecAttrAccessGroup : @"keychain-test1",
445 (id)kSecAttrLabel : @"CheckItemReference",
446 (id)kSecAttrAccessible : (id)kSecAttrAccessibleAfterFirstUnlock,
447 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
448 (id)kSecReturnPersistentRef: (id)kCFBooleanTrue,
450 status = SecItemAdd((__bridge CFDictionaryRef)add, (CFTypeRef *)&pref);
451 if (status != errSecSuccess)
452 fail("add item: %d: %s", (int)status, [[add description] UTF8String]);
454 if (pref == NULL || CFGetTypeID(pref) != CFDataGetTypeID())
455 fail("no pref returned");
461 NSDictionary *query = @{
462 (id)kSecValueRef : (__bridge id)identity,
463 (id)kSecReturnPersistentRef: (id)kCFBooleanTrue,
465 status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&pref2);
467 fail("SecItemCopyMatching: %d", (int)status);
469 if (pref2 == NULL || CFGetTypeID(pref2) != CFDataGetTypeID())
470 fail("no pref2 returned");
473 if (!CFEqual(pref, pref2))
474 fail("prefs not same");
482 NSDictionary *query2 = @{
483 (id)kSecClass : (id)kSecClassIdentity,
484 (id)kSecAttrAccessGroup : @"keychain-test1",
485 (id)kSecAttrLabel : @"CheckItemReference",
486 (id)kSecReturnPersistentRef: (id)kCFBooleanTrue,
488 status = SecItemCopyMatching((CFDictionaryRef)query2, (CFTypeRef *)&pref2);
490 fail("SecItemCopyMatching: %d", (int)status);
492 if (pref2 == NULL || CFGetTypeID(pref2) != CFDataGetTypeID())
493 fail("no pref2 returned");
496 if (!CFEqual(pref, pref2))
497 fail("prefs not same");
502 * Find by label + reference
505 NSDictionary *query3 = @{
506 (id)kSecAttrAccessGroup : @"keychain-test1",
507 (id)kSecAttrLabel : @"CheckItemReference",
508 (id)kSecValueRef : (__bridge id)identity,
509 (id)kSecReturnPersistentRef: (id)kCFBooleanTrue,
511 status = SecItemCopyMatching((CFDictionaryRef)query3, (CFTypeRef *)&pref2);
513 fail("SecItemCopyMatching: %d", (int)status);
515 if (pref2 == NULL || CFGetTypeID(pref2) != CFDataGetTypeID())
516 fail("no pref2 returned");
519 if (!CFEqual(pref, pref2))
520 fail("prefs not same");
530 printf("[PASS] %s\n", __FUNCTION__);
534 timeDiff(uint64_t start, uint64_t stop)
536 static uint64_t time_overhead_measured = 0;
537 static double timebase_factor = 0;
539 if (time_overhead_measured == 0) {
540 uint64_t t0 = mach_absolute_time();
541 time_overhead_measured = mach_absolute_time() - t0;
543 struct mach_timebase_info timebase_info = {};
544 mach_timebase_info(&timebase_info);
545 timebase_factor = ((double)timebase_info.numer)/((double)timebase_info.denom);
548 return ((stop - start - time_overhead_measured) * timebase_factor) / NSEC_PER_USEC;
552 RunCopyPerfTest(NSString *name, NSDictionary *query)
554 uint64_t start = mach_absolute_time();
556 CFTypeRef result = NULL;
558 status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
559 uint64_t stop = mach_absolute_time();
562 printf("SecItemCopyMatching failed with: %d\n", (int)status);
570 uint64_t us = timeDiff(start, stop);
572 puts([[NSString stringWithFormat:@"[RESULT_KEY] SecItemCopyMatching-%@\n[RESULT_VALUE] %lu\n",
573 name, (unsigned long)us] UTF8String]);
577 RunDigestPerfTest(NSString *name, NSString *itemClass, NSString *accessGroup, NSUInteger expectedCount)
579 uint64_t start = mach_absolute_time();
580 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
581 __block uint64_t stop;
583 _SecItemFetchDigests(itemClass, accessGroup, ^(NSArray *items, NSError *error) {
584 stop = mach_absolute_time();
586 printf("_SecItemFetchDigests failed with: %ld\n", (long)error.code);
590 dispatch_semaphore_signal(sema);
592 if (expectedCount != [items count]) {
593 printf("_SecItemFetchDigests didn't return expected items: %ld\n", (long)[items count]);
598 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
601 uint64_t us = timeDiff(start, stop);
603 puts([[NSString stringWithFormat:@"[RESULT_KEY] SecItemCopyDigest-%@\n[RESULT_VALUE] %lu\n",
604 name, (unsigned long)us] UTF8String]);
609 CheckItemPerformance(void)
613 printf("[BEGIN] %s\n", __FUNCTION__);
618 NSDictionary *clean1 = @{
619 (id)kSecClass : (id)kSecClassGenericPassword,
620 (id)kSecAttrService : @"service",
621 (id)kSecAttrAccessGroup : @"keychain-test1",
622 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
624 (void)SecItemDelete((__bridge CFDictionaryRef)clean1);
626 NSData *data = [NSData dataWithBytes:"password" length:8];
628 for (n = 0; n < 1000; n++) {
629 NSDictionary *item = @{
630 (id)kSecClass : (id)kSecClassGenericPassword,
631 (id)kSecAttrAccount : [NSString stringWithFormat:@"account-%d", n],
632 (id)kSecAttrService : @"service",
633 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
634 (id)kSecAttrAccessGroup : @"keychain-test1",
635 (id)kSecAttrNoLegacy : (id)kCFBooleanTrue,
636 (id)kSecValueData : data,
638 SecItemAdd((__bridge CFDictionaryRef)item, NULL);
642 RunCopyPerfTest(@"FindOneItemLimit", @{
643 (id)kSecClass : (id)kSecClassGenericPassword,
644 (id)kSecAttrService : @"service",
645 (id)kSecMatchLimit : (id)kSecMatchLimitOne,
647 RunCopyPerfTest(@"FindOneItemUnique", @{
648 (id)kSecClass : (id)kSecClassGenericPassword,
649 (id)kSecAttrAccount : @"account-0",
650 (id)kSecAttrService : @"service",
651 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
653 RunCopyPerfTest(@"Find1000Items", @{
654 (id)kSecClass : (id)kSecClassGenericPassword,
655 (id)kSecAttrService : @"service",
656 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
658 RunDigestPerfTest(@"Digest1000Items", (id)kSecClassGenericPassword, @"keychain-test1", 1000);
659 RunCopyPerfTest(@"GetAttrOneItemUnique", @{
660 (id)kSecClass : (id)kSecClassGenericPassword,
661 (id)kSecAttrAccount : @"account-0",
662 (id)kSecAttrService : @"service",
663 (id)kSecReturnAttributes : (id)kCFBooleanTrue,
664 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
666 RunCopyPerfTest(@"GetData1000Items", @{
667 (id)kSecClass : (id)kSecClassGenericPassword,
668 (id)kSecAttrService : @"service",
669 (id)kSecReturnData : (id)kCFBooleanTrue,
670 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
672 RunCopyPerfTest(@"GetDataOneItemUnique", @{
673 (id)kSecClass : (id)kSecClassGenericPassword,
674 (id)kSecAttrAccount : @"account-0",
675 (id)kSecAttrService : @"service",
676 (id)kSecReturnData : (id)kCFBooleanTrue,
677 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
679 RunCopyPerfTest(@"GetDataAttrOneItemUnique", @{
680 (id)kSecClass : (id)kSecClassGenericPassword,
681 (id)kSecAttrAccount : @"account-0",
682 (id)kSecAttrService : @"service",
683 (id)kSecReturnData : (id)kCFBooleanTrue,
684 (id)kSecReturnAttributes : (id)kCFBooleanTrue,
685 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
687 #if TARGET_OS_IPHONE /* macOS doesn't support fetching data for more then one item */
688 RunCopyPerfTest(@"GetData1000Items", @{
689 (id)kSecClass : (id)kSecClassGenericPassword,
690 (id)kSecAttrService : @"service",
691 (id)kSecReturnData : (id)kCFBooleanTrue,
692 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
694 RunCopyPerfTest(@"GetDataAttr1000Items", @{
695 (id)kSecClass : (id)kSecClassGenericPassword,
696 (id)kSecAttrService : @"service",
697 (id)kSecReturnData : (id)kCFBooleanTrue,
698 (id)kSecReturnAttributes : (id)kCFBooleanTrue,
699 (id)kSecMatchLimit : (id)kSecMatchLimitAll,
703 (void)SecItemDelete((__bridge CFDictionaryRef)clean1);
706 printf("[PASS] %s\n", __FUNCTION__);
710 main(int argc, const char ** argv)
712 printf("[TEST] secitemfunctionality\n");
715 char *user = getenv("USER");
716 if (user && strcmp("bats", user) == 0) {
717 (void)SecKeychainUnlock(NULL, 4, "bats", true);
720 CheckItemPerformance();
722 CheckFindIdentityByReference();
724 CheckItemAddDeleteMaybeLegacyKeychainNoData();
725 CheckItemAddDeleteNoData();
726 CheckItemUpdateAccessGroupGENP();
727 CheckItemUpdateAccessGroupIdentity();
729 printf("[SUMMARY]\n");
730 printf("test completed\n");