]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-10-find-internet.c
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / secitem / si-10-find-internet.c
1 /*
2 * Copyright (c) 2006-2010,2012-2013 Apple Inc. All Rights Reserved.
3 */
4
5 #include <CoreFoundation/CoreFoundation.h>
6 #include <Security/SecItem.h>
7 #include <Security/SecBase.h>
8 #include <utilities/array_size.h>
9 #include <utilities/SecCFWrappers.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12
13 #include "Security_regressions.h"
14
15 // TODO: Test whether limit's works.
16
17 /* Test basic add delete update copy matching stuff. */
18 static void tests(void)
19 {
20 int v_eighty = 80;
21 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
22 const char *v_data = "test";
23 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
24 const void *keys[] = {
25 kSecClass,
26 kSecAttrServer,
27 kSecAttrAccount,
28 kSecAttrPort,
29 kSecAttrProtocol,
30 kSecAttrAuthenticationType,
31 kSecValueData
32 };
33 const void *values[] = {
34 kSecClassInternetPassword,
35 CFSTR("members.spamcop.net"),
36 CFSTR("smith"),
37 eighty,
38 CFSTR("http"),
39 CFSTR("dflt"),
40 pwdata
41 };
42 CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
43 array_size(keys), NULL, NULL);
44 ok_status(SecItemAdd(item, NULL), "add internet password");
45 is_status(SecItemAdd(item, NULL), errSecDuplicateItem,
46 "add internet password again");
47
48 CFTypeRef results = NULL;
49 /* Create a dict with all attrs except the data. */
50 CFDictionaryRef query = CFDictionaryCreate(NULL, keys, values,
51 (array_size(keys)) - 1, NULL, NULL);
52 ok_status(SecItemCopyMatching(query, &results), "find internet password");
53 CFReleaseNull(results);
54
55 CFMutableDictionaryRef query2 = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, query);
56 CFDictionarySetValue(query2, kSecReturnAttributes, kCFBooleanTrue);
57 CFDictionarySetValue(query2, kSecMatchLimit , kSecMatchLimitOne);
58 ok_status(SecItemCopyMatching(query2, &results), "find internet password, return attributes");
59 CFReleaseNull(query2);
60 query2 = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, results);
61 CFDictionarySetValue(query2, kSecClass, kSecClassInternetPassword);
62 CFDictionarySetValue(query2, kSecReturnData, kCFBooleanTrue);
63 ok_status(SecItemCopyMatching(query2, &results), "find internet password using returned attributes");
64 CFReleaseSafe(query2);
65 ok(isData(results) && CFEqual(results, pwdata), "retrieved data correctly");
66 CFReleaseNull(results);
67
68 /* Modify the server attr of the item. */
69 const void *ch_keys[] = {
70 kSecAttrServer,
71 kSecClass,
72 };
73 const void *ch_values[] = {
74 CFSTR("www.spamcop.net"),
75 kSecClassInternetPassword,
76 };
77 CFDictionaryRef changes = CFDictionaryCreate(NULL, ch_keys, ch_values,
78 1, NULL, NULL);
79 ok_status(SecItemUpdate(query, changes), "update internet password");
80
81 is_status(SecItemUpdate(query, changes), errSecItemNotFound,
82 "update non-exisiting internet password again");
83
84 /* Delete the original item (which should fail since we modified it). */
85 is_status(SecItemDelete(query), errSecItemNotFound,
86 "delete non existing internet password");
87 ok_status(SecItemAdd(item, NULL), "add unmodified password");
88 ok_status(SecItemDelete(query), "delete internet password");
89
90 ok_status(SecItemAdd(item, NULL), "add unmodified password");
91 is_status(SecItemUpdate(query, changes), errSecDuplicateItem,
92 "update internet password causing dupe");
93 ok_status(SecItemDelete(query), "delete internet password");
94
95 CFDictionaryRef changed_item = CFDictionaryCreate(NULL, ch_keys, ch_values,
96 array_size(ch_keys), NULL, NULL);
97 ok_status(SecItemDelete(changed_item), "delete changed internet password");
98
99 if (changed_item) {
100 CFRelease(changed_item);
101 changed_item = NULL;
102 }
103
104 if (changes) {
105 CFRelease(changes);
106 changes = NULL;
107 }
108
109 if (item) {
110 CFRelease(item);
111 item = NULL;
112 }
113
114 if (query) {
115 CFRelease(query);
116 query = NULL;
117 }
118
119 if (eighty) {
120 CFRelease(eighty);
121 eighty = NULL;
122 }
123 if (pwdata) {
124 CFRelease(pwdata);
125 pwdata = NULL;
126 }
127 }
128
129 int si_10_find_internet(int argc, char *const *argv)
130 {
131 plan_tests(15);
132
133 tests();
134
135 return 0;
136 }