]> git.saurik.com Git - apple/security.git/blob - SecurityTests/regressions/kc/kc-18-item-find-internet.c
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / regressions / kc / kc-18-item-find-internet.c
1 #include <Security/SecKeychain.h>
2 #include <Security/SecKeychainItem.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "testmore.h"
7 #include "testenv.h"
8 #include "testleaks.h"
9
10 void tests(int dont_skip)
11 {
12 SecKeychainRef keychain;
13 ok_status(SecKeychainCreate("test", 4, "test", FALSE, NULL, &keychain),
14 "create keychain");
15 SecKeychainItemRef item = NULL;
16 ok_status(SecKeychainAddInternetPassword(keychain,
17 19, "members.spamcop.net",
18 0, NULL,
19 5, "smith",
20 0, NULL,
21 80, kSecProtocolTypeHTTP,
22 kSecAuthenticationTypeDefault,
23 4, "test", &item), "add internet password");
24 ok(item, "is item non NULL");
25 SecKeychainItemRef oldItem = item;
26 is_status(SecKeychainAddInternetPassword(keychain,
27 19, "members.spamcop.net",
28 0, NULL,
29 5, "smith",
30 0, NULL,
31 80, kSecProtocolTypeHTTP,
32 kSecAuthenticationTypeDefault,
33 4, "test", &item), errSecDuplicateItem, "add internet password again");
34 is((intptr_t)item, (intptr_t)oldItem, "item is unchanged");
35
36 CFRelease(item);
37 item = NULL;
38 ok_status(SecKeychainFindInternetPassword(NULL,
39 19, "members.spamcop.net",
40 0, NULL,
41 0, NULL,
42 0, NULL,
43 80,
44 kSecProtocolTypeHTTP,
45 kSecAuthenticationTypeDefault,
46 NULL, NULL,
47 &item), "find internet password");
48
49 SecItemClass itemClass = 0;
50 SecKeychainAttribute attrs[] =
51 {
52 { kSecAccountItemAttr },
53 { kSecSecurityDomainItemAttr },
54 { kSecServerItemAttr }
55 };
56 SecKeychainAttributeList attrList =
57 {
58 sizeof(attrs) / sizeof(*attrs),
59 attrs
60 };
61 ok_status(SecKeychainItemCopyContent(item, &itemClass, &attrList,
62 NULL, NULL), "SecKeychainItemCopyContent");
63 is(itemClass, kSecInternetPasswordItemClass, "is internet password?");
64 is(attrs[0].length, 5, "account len");
65 ok(!strncmp(attrs[0].data, "smith", 5), "account eq smith");
66 is(attrs[1].length, 0, "security domain len");
67 is(attrs[2].length, 19, "server len");
68 ok(!strncmp(attrs[2].data, "members.spamcop.net", 19),
69 "servername eq members.spamcop.net");
70 ok_status(SecKeychainItemFreeContent(&attrList, NULL),
71 "SecKeychainItemCopyContent");
72
73 SecKeychainAttribute attrs2[] =
74 {
75 { kSecAccountItemAttr },
76 { kSecServiceItemAttr },
77 { kSecSecurityDomainItemAttr },
78 { kSecServerItemAttr }
79 };
80 SecKeychainAttributeList attrList2 =
81 {
82 (sizeof(attrs2) / sizeof(*attrs2)),
83 attrs2
84 };
85 SKIP: {
86 skip("<rdar://problem/3298182> 6L60 Malloc/free misuse in "
87 "SecKeychainItemCopyContent()", 1, dont_skip);
88 is_status(SecKeychainItemCopyContent(item, &itemClass, &attrList2,
89 NULL, NULL), errSecNoSuchAttr, "SecKeychainItemCopyContent fails");
90 }
91
92 is(CFGetRetainCount(item), 1, "item retaincount is 1");
93 is(CFGetRetainCount(keychain), 2, "keychain retaincount is 2");
94 CFRelease(item);
95 is(CFGetRetainCount(keychain), 1, "keychain retaincount is 1");
96 ok_status(SecKeychainDelete(keychain), "delete keychain");
97 CFRelease(keychain);
98 }
99
100 int main(int argc, char *const *argv)
101 {
102 int dont_skip = argc > 1 && !strcmp(argv[1], "-s");
103
104 plan_tests(21);
105
106 if (!tests_begin(argc, argv))
107 BAIL_OUT("tests_begin failed");
108
109 tests(dont_skip);
110 ok(tests_end(1), "cleanup");
111 ok_leaks("no leaks");
112
113 return 0;
114 }