]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/Regressions/su-05-cfwrappers.c
Security-57337.20.44.tar.gz
[apple/security.git] / OSX / utilities / Regressions / su-05-cfwrappers.c
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include <utilities/SecCFRelease.h>
26 #include <utilities/SecCFWrappers.h>
27 #include <utilities/SecIOFormat.h>
28
29 #include "utilities_regressions.h"
30
31 #define kCFWrappersTestCount 25
32
33 static CFDataRef *testCopyDataPtr(void) {
34 static CFDataRef sData = NULL;
35 if (!sData)
36 sData = CFDataCreate(kCFAllocatorDefault, NULL, 0);
37 else
38 CFRetain(sData);
39 return &sData;
40 }
41
42 static void
43 test_object(CFDataRef data) {
44 CFDataRef myData = CFRetainSafe(data);
45 ok(CFEqual(myData, data), "");
46 is(CFGetRetainCount(myData), 2, "");
47 ok(CFReleaseNull(myData) == ((CFDataRef)(0)), "");
48 is(myData, NULL, "");
49
50 is(CFGetRetainCount(data), 1);
51 CFRetainAssign(myData, data);
52 is(CFGetRetainCount(data), 2);
53 CFRetainAssign(myData, data);
54 is(CFGetRetainCount(data), 2);
55 CFRetainAssign(myData, NULL);
56 is(CFGetRetainCount(data), 1);
57 is(myData, NULL, "");
58
59 CFDataRef *pData = testCopyDataPtr();
60 is(CFGetRetainCount(*pData), 1);
61 CFDataRef objects[10] = {}, *object = objects;
62 *object = *pData;
63 CFRetainAssign(*testCopyDataPtr(), *object++);
64 is(CFGetRetainCount(*pData), 2, "CFRetainAssign evaluates it's first argument argument %" PRIdCFIndex " times", CFGetRetainCount(*pData) - 1);
65 is(object - objects, 1, "CFRetainAssign evaluates it's second argument %td times", object - objects);
66
67 is(CFGetRetainCount(data), 1);
68 CFAssignRetained(myData, data);
69 is(CFGetRetainCount(myData), 1);
70 }
71
72 static void
73 test_null(void) {
74 CFTypeRef nullObject1 = NULL;
75 CFTypeRef nullObject2 = NULL;
76
77 nullObject1 = CFRetainSafe(NULL);
78
79 is(nullObject1, NULL, "");
80 is(CFReleaseNull(nullObject1), NULL, "CFReleaseNull(nullObject1) returned");
81 is(nullObject1, NULL);
82 is(CFReleaseSafe(nullObject1), NULL, "CFReleaseSafe(nullObject1) returned");
83 is(CFReleaseSafe(NULL), NULL, "CFReleaseSafe(NULL)");
84 is(CFReleaseNull(nullObject2), NULL, "CFReleaseNull(nullObject2) returned");
85 is(nullObject2, NULL, "nullObject2 still NULL");
86
87 CFRetainAssign(nullObject2, nullObject1);
88
89 CFTypeRef *object, objects[10] = {};
90
91 object = &objects[0];
92 CFRetainSafe(*object++);
93 is(object - objects, 1, "CFRetainSafe evaluates it's argument %td times", object - objects);
94
95 object = &objects[0];
96 CFReleaseSafe(*object++);
97 is(object - objects, 1, "CFReleaseSafe evaluates it's argument %td times", object - objects);
98
99 object = &objects[0];
100 CFReleaseNull(*object++);
101 is(object - objects, 1, "CFReleaseNull evaluates it's argument %td times", object - objects);
102 }
103
104 int
105 su_05_cfwrappers(int argc, char *const *argv) {
106 plan_tests(kCFWrappersTestCount);
107
108 test_null();
109 CFDataRef data = CFDataCreate(kCFAllocatorDefault, NULL, 0);
110 test_object(data);
111 CFReleaseNull(data);
112 ok(data == NULL, "data is NULL now");
113 return 0;
114 }