1 #include <Security/SecKeychain.h>
6 #include "keychain_regressions.h"
7 #include "kc-helpers.h"
9 static void tests(void)
11 char *home
= getenv("HOME");
12 char kcname1
[256], kcname2
[256];
13 SecKeychainStatus status1
, status2
;
15 if (!home
|| strlen(home
) > 200)
16 plan_skip_all("home too big");
18 snprintf(kcname1
, sizeof(kcname1
), "%s/kctests/kc1/kc1", home
);
19 SecKeychainRef kc1
= NULL
, kc2
= NULL
;
20 kc1
= createNewKeychainAt(kcname1
, "test");
22 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
23 is(status1
, kSecUnlockStateStatus
|kSecReadPermStatus
|kSecWritePermStatus
,
24 "status unlocked readable writable");
25 ok_status(SecKeychainLock(kc1
), "SecKeychainLock kc1");
26 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
28 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
29 "incorrect status (always writable?)");
31 is(status1
, kSecReadPermStatus
|kSecWritePermStatus
,
32 "status (locked) readable writable");
35 /* Make keychain non writable. */
37 snprintf(kcdir1
, sizeof(kcdir1
), "%s/kctests/kc1", home
);
38 ok_unix(chmod(kcdir1
, 0555), "chmod kcdir1 0555");
40 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
41 is(status1
, kSecReadPermStatus
, "status (locked) readable");
42 ok_status(SecKeychainUnlock(kc1
, 4, "test", TRUE
), "SecKeychainLock kc1");
43 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
45 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
46 "incorrect status (always writable?)");
48 is(status1
, kSecUnlockStateStatus
|kSecReadPermStatus
,
49 "status unlocked readable");
52 /* Reopen the keychain. */
54 ok_status(SecKeychainOpen(kcname1
, &kc1
), "SecKeychainOpen kc1");
56 ok_status(SecKeychainGetStatus(kc1
, &status1
), "get kc1 status");
58 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
59 "incorrect status (always writable?)");
61 is(status1
, kSecUnlockStateStatus
|kSecReadPermStatus
,
62 "status unlocked readable");
65 snprintf(kcname2
, sizeof(kcname2
), "%s/kctests/kc2/kc2", home
);
66 kc2
= createNewKeychainAt(kcname2
, "test");
67 ok_unix(chmod(kcname2
, 0444), "chmod kc2 0444");
68 ok_status(SecKeychainGetStatus(kc2
, &status2
), "get kc2 status");
69 is(status2
, kSecUnlockStateStatus
|kSecReadPermStatus
|kSecWritePermStatus
,
70 "status unlocked readable writable");
72 /* Reopen the keychain. */
74 ok_status(SecKeychainOpen(kcname2
, &kc2
), "SecKeychainOpen kc2");
76 ok_status(SecKeychainGetStatus(kc2
, &status2
), "get kc2 status");
77 is(status2
, kSecUnlockStateStatus
|kSecReadPermStatus
|kSecWritePermStatus
,
78 "status unlocked readable writable");
80 /* Restore dir to writable so cleanup code will work ok. */
81 ok_unix(chmod(kcdir1
, 0755), "chmod kcdir1 0755");
82 ok_status(SecKeychainDelete(kc1
), "%s: SecKeychainDelete", testName
);
84 ok_status(SecKeychainDelete(kc2
), "%s: SecKeychainDelete", testName
);
87 bool testWithFreshlyCreatedKeychain
= true;
88 SecKeychainRef keychain
= createNewKeychain("test", "test");
89 ok_status(SecKeychainLock(keychain
), "SecKeychainLock");
92 SecKeychainStatus keychainStatus
= 0;
93 is_status(SecKeychainUnlock(keychain
, 0, NULL
, true), -25293, "SecKeychainUnlock with NULL password (incorrect)");
94 ok_status(SecKeychainGetStatus(keychain
, &keychainStatus
), "SecKeychainGetStatus");
95 is( (keychainStatus
& kSecUnlockStateStatus
), 0, "Check it's not unlocked");
98 ok_status(SecKeychainUnlock(keychain
, strlen("test"), "test", true), "SecKeychainUnlock with correct password");
99 ok_status(SecKeychainGetStatus(keychain
, &keychainStatus
), "SecKeychainGetStatus");
100 is( (keychainStatus
& kSecUnlockStateStatus
), kSecUnlockStateStatus
, "Check it's unlocked");
102 ok_status(SecKeychainLock(keychain
), "SecKeychainLock");
104 if (testWithFreshlyCreatedKeychain
)
107 testWithFreshlyCreatedKeychain
= false;
108 ok_status(SecKeychainOpen("test", &keychain
), "SecKeychainOpen");
111 testWithFreshlyCreatedKeychain
= true;
113 ok_status(SecKeychainDelete(keychain
), "%s: SecKeychainDelete", testName
);
114 CFReleaseNull(keychain
);
118 while(!testWithFreshlyCreatedKeychain
);
124 int kc_03_status(int argc
, char *const *argv
)