]> git.saurik.com Git - apple/security.git/blob - SecurityTests/regressions/kc/kc-12-status.c
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / regressions / kc / kc-12-status.c
1 #include <Security/SecKeychain.h>
2 #include <stdlib.h>
3 #include <sys/stat.h>
4 #include <sys/types.h>
5
6 #include "testmore.h"
7 #include "testenv.h"
8 #include "testleaks.h"
9
10 static void tests(void)
11 {
12 char *home = getenv("HOME");
13 char kcname1[256], kcname2[256];
14 SecKeychainStatus status1, status2;
15
16 if (!home || strlen(home) > 200)
17 plan_skip_all("home too big");
18
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");
23
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");
29 TODO: {
30 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
31 "incorrect status (always writable?)");
32
33 is(status1, kSecReadPermStatus|kSecWritePermStatus,
34 "status (locked) readable writable");
35 }
36
37 /* Make keychain non writable. */
38 char kcdir1[256];
39 sprintf(kcdir1, "%s/kc1", home);
40 ok_unix(chmod(kcdir1, 0555), "chmod kcdir1 0555");
41
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");
46 TODO: {
47 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
48 "incorrect status (always writable?)");
49
50 is(status1, kSecUnlockStateStatus|kSecReadPermStatus,
51 "status unlocked readable");
52 }
53
54 /* Reopen the keychain. */
55 CFRelease(kc1);
56 ok_status(SecKeychainOpen(kcname1, &kc1), "SecKeychainOpen kc1");
57
58 ok_status(SecKeychainGetStatus(kc1, &status1), "get kc1 status");
59 TODO: {
60 todo("<rdar://problem/2668794> KeychainImpl::status() returns "
61 "incorrect status (always writable?)");
62
63 is(status1, kSecUnlockStateStatus|kSecReadPermStatus,
64 "status unlocked readable");
65 }
66
67 sprintf(kcname2, "%s/kc2/kc2", home);
68 ok_status(SecKeychainOpen(kcname2, &kc2), "SecKeychainOpen kc2");
69 is_status(SecKeychainGetStatus(kc2, &status2), errSecNoSuchKeychain,
70 "get kc2 status");
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");
77
78 /* Reopen the keychain. */
79 CFRelease(kc2);
80 ok_status(SecKeychainOpen(kcname2, &kc2), "SecKeychainOpen kc2");
81
82 ok_status(SecKeychainGetStatus(kc2, &status2), "get kc2 status");
83 is(status2, kSecUnlockStateStatus|kSecReadPermStatus|kSecWritePermStatus,
84 "status unlocked readable writable");
85
86 /* Restore dir to writable so cleanup code will work ok. */
87 ok_unix(chmod(kcdir1, 0755), "chmod kcdir1 0755");
88 CFRelease(kc1);
89 CFRelease(kc2);
90
91 bool testWithFreshlyCreatedKeychain = true;
92 SecKeychainRef keychain;
93 ok_status(SecKeychainCreate("test", 4, "test", FALSE, NULL, &keychain),
94 "SecKeychainCreate");
95 ok_status(SecKeychainLock(keychain), "SecKeychainLock");
96
97 do {
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");
102
103 keychainStatus = 0;
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");
107
108 ok_status(SecKeychainLock(keychain), "SecKeychainLock");
109 CFRelease(keychain);
110
111 if (testWithFreshlyCreatedKeychain)
112 {
113 testWithFreshlyCreatedKeychain = false;
114 ok_status(SecKeychainOpen("test", &keychain), "SecKeychainOpen");
115 }
116 else
117 testWithFreshlyCreatedKeychain = true;
118
119 }
120 while(!testWithFreshlyCreatedKeychain);
121
122 tests_end(1);
123 }
124
125
126
127 int main(int argc, char *const *argv)
128 {
129 plan_tests(43);
130 if (!tests_begin(argc, argv))
131 BAIL_OUT("tests_begin failed");
132
133 tests();
134
135 ok_leaks("leaks");
136
137 return 0;
138 }