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