]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-10-find-internet.c
Security-58286.31.2.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 CFDictionaryRemoveValue(query2, kSecAttrSHA1),
63 CFDictionarySetValue(query2, kSecClass, kSecClassInternetPassword);
64 CFDictionarySetValue(query2, kSecReturnData, kCFBooleanTrue);
65 ok_status(SecItemCopyMatching(query2, &results), "find internet password using returned attributes");
66 CFReleaseSafe(query2);
67 ok(isData(results) && CFEqual(results, pwdata), "retrieved data correctly");
68 CFReleaseNull(results);
69
70 /* Modify the server attr of the item. */
71 const void *ch_keys[] = {
72 kSecAttrServer,
73 kSecClass,
74 };
75 const void *ch_values[] = {
76 CFSTR("www.spamcop.net"),
77 kSecClassInternetPassword,
78 };
79 CFDictionaryRef changes = CFDictionaryCreate(NULL, ch_keys, ch_values,
80 1, NULL, NULL);
81 ok_status(SecItemUpdate(query, changes), "update internet password");
82
83 is_status(SecItemUpdate(query, changes), errSecItemNotFound,
84 "update non-exisiting internet password again");
85
86 /* Delete the original item (which should fail since we modified it). */
87 is_status(SecItemDelete(query), errSecItemNotFound,
88 "delete non existing internet password");
89 ok_status(SecItemAdd(item, NULL), "add unmodified password");
90 ok_status(SecItemDelete(query), "delete internet password");
91
92 ok_status(SecItemAdd(item, NULL), "add unmodified password");
93 is_status(SecItemUpdate(query, changes), errSecDuplicateItem,
94 "update internet password causing dupe");
95 ok_status(SecItemDelete(query), "delete internet password");
96
97 CFDictionaryRef changed_item = CFDictionaryCreate(NULL, ch_keys, ch_values,
98 array_size(ch_keys), NULL, NULL);
99 ok_status(SecItemDelete(changed_item), "delete changed internet password");
100
101 if (changed_item) {
102 CFRelease(changed_item);
103 changed_item = NULL;
104 }
105
106 if (changes) {
107 CFRelease(changes);
108 changes = NULL;
109 }
110
111 if (item) {
112 CFRelease(item);
113 item = NULL;
114 }
115
116 if (query) {
117 CFRelease(query);
118 query = NULL;
119 }
120
121 if (eighty) {
122 CFRelease(eighty);
123 eighty = NULL;
124 }
125 if (pwdata) {
126 CFRelease(pwdata);
127 pwdata = NULL;
128 }
129 }
130
131 int si_10_find_internet(int argc, char *const *argv)
132 {
133 plan_tests(15);
134
135 tests();
136
137 return 0;
138 }