1 #include <Security/SecKeychain.h>
10 static void tests(void)
12 char *home
= getenv("HOME");
13 char kcname1
[256], kcname2
[256];
14 SecKeychainStatus status1
, status2
;
16 if (!home
|| strlen(home
) > 200)
17 plan_skip_all("home too big");
19 sprintf(kcname1
, "%s/kc1/kc1", home
);
20 SecKeychainRef kc1
= NULL
, kc2
= NULL
;
21 ok_status(SecKeychainCreate(kcname1
, 4, "test", FALSE
, NULL
, &kc1
),
22 "SecKeychainCreate kc1");
24 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
25 is(status1
, kSecUnlockStateStatus
|kSecReadPermStatus
|kSecWritePermStatus
,
26 "status unlocked readable writable");
27 ok_status(SecKeychainLock(kc1
), "SecKeychainLock kc1");
28 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
30 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
31 "incorrect status (always writable?)");
33 is(status1
, kSecReadPermStatus
|kSecWritePermStatus
,
34 "status (locked) readable writable");
37 /* Make keychain non writable. */
39 sprintf(kcdir1
, "%s/kc1", home
);
40 ok_unix(chmod(kcdir1
, 0555), "chmod kcdir1 0555");
42 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
43 is(status1
, kSecReadPermStatus
, "status (locked) readable");
44 ok_status(SecKeychainUnlock(kc1
, 4, "test", TRUE
), "SecKeychainLock kc1");
45 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
47 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
48 "incorrect status (always writable?)");
50 is(status1
, kSecUnlockStateStatus
|kSecReadPermStatus
,
51 "status unlocked readable");
54 /* Reopen the keychain. */
56 ok_status(SecKeychainOpen(kcname1
, &kc1
), "SecKeychainOpen kc1");
58 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
60 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
61 "incorrect status (always writable?)");
63 is(status1
, kSecUnlockStateStatus
|kSecReadPermStatus
,
64 "status unlocked readable");
67 sprintf(kcname2
, "%s/kc2/kc2", home
);
68 ok_status(SecKeychainOpen(kcname2
, &kc2
), "SecKeychainOpen kc2");
69 is_status(SecKeychainGetStatus(kc2
, &status2
), errSecNoSuchKeychain
,
71 ok_status(SecKeychainCreate(kcname2
, 4, "test", FALSE
, NULL
, &kc2
),
72 "SecKeychainCreate kc2");
73 ok_unix(chmod(kcname2
, 0444), "chmod kc2 0444");
74 ok_status(SecKeychainGetStatus(kc2
, &status2
), "get kc2 status");
75 is(status2
, kSecUnlockStateStatus
|kSecReadPermStatus
|kSecWritePermStatus
,
76 "status unlocked readable writable");
78 /* Reopen the keychain. */
80 ok_status(SecKeychainOpen(kcname2
, &kc2
), "SecKeychainOpen kc2");
82 ok_status(SecKeychainGetStatus(kc2
, &status2
), "get kc2 status");
83 is(status2
, kSecUnlockStateStatus
|kSecReadPermStatus
|kSecWritePermStatus
,
84 "status unlocked readable writable");
86 /* Restore dir to writable so cleanup code will work ok. */
87 ok_unix(chmod(kcdir1
, 0755), "chmod kcdir1 0755");
91 bool testWithFreshlyCreatedKeychain
= true;
92 SecKeychainRef keychain
;
93 ok_status(SecKeychainCreate("test", 4, "test", FALSE
, NULL
, &keychain
),
95 ok_status(SecKeychainLock(keychain
), "SecKeychainLock");
98 SecKeychainStatus keychainStatus
= 0;
99 is_status(SecKeychainUnlock(keychain
, 0, NULL
, true), -25293, "SecKeychainUnlock with NULL password (incorrect)");
100 ok_status(SecKeychainGetStatus(keychain
, &keychainStatus
), "SecKeychainGetStatus");
101 is( (keychainStatus
& kSecUnlockStateStatus
), 0, "Check it's not unlocked");
104 ok_status(SecKeychainUnlock(keychain
, strlen("test"), "test", true), "SecKeychainUnlock with correct password");
105 ok_status(SecKeychainGetStatus(keychain
, &keychainStatus
), "SecKeychainGetStatus");
106 is( (keychainStatus
& kSecUnlockStateStatus
), kSecUnlockStateStatus
, "Check it's unlocked");
108 ok_status(SecKeychainLock(keychain
), "SecKeychainLock");
111 if (testWithFreshlyCreatedKeychain
)
113 testWithFreshlyCreatedKeychain
= false;
114 ok_status(SecKeychainOpen("test", &keychain
), "SecKeychainOpen");
117 testWithFreshlyCreatedKeychain
= true;
120 while(!testWithFreshlyCreatedKeychain
);
127 int main(int argc
, char *const *argv
)
130 if (!tests_begin(argc
, argv
))
131 BAIL_OUT("tests_begin failed");