]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/Regressions/su-07-debugging.c
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / utilities / Regressions / su-07-debugging.c
1 /*
2 * Copyright (c) 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 <stdio.h>
26
27 #include <utilities/SecCFRelease.h>
28 #include <utilities/SecCFWrappers.h>
29 #include <utilities/SecIOFormat.h>
30
31 #include "utilities_regressions.h"
32 #include "utilities/debugging.h"
33 #include "utilities/debugging_test.h"
34
35 #if USINGOLDLOGGING
36 #define kTestCount (39)
37
38 static void
39 tests(void) {
40 ok(IsScopeActive(SECLOG_LEVEL_ERR, NULL), "Errors are active by default");
41
42 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("testscope")), "scope is off");
43
44 ApplyScopeListForIDC("-first", kScopeIDXPC);
45
46 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("testscope")), "scope is on");
47 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("first")), "scope is off");
48
49 ApplyScopeListForIDC("first", kScopeIDXPC);
50
51 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("first")), "scope is on");
52 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("testscope")), "scope is off");
53
54 ApplyScopeListForIDC("testscope, bar, baz,frog", kScopeIDXPC);
55
56 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("testscope")), "scope is on");
57 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("bar")), "scope is on");
58 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("baz")), "scope is on");
59 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("frog")), "scope is on");
60 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("bonzo")), "scope is off");
61 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("nothing")), "scope is off");
62
63 ApplyScopeListForID(CFSTR("-bonzo, boy"), kScopeIDDefaults);
64
65 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("testscope")), "scope is on");
66 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("bar")), "scope is on");
67 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("baz")), "scope is on");
68 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("frog")), "scope is on");
69 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("bonzo")), "scope is off");
70 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("nothing")), "scope is on");
71
72 ApplyScopeListForID(CFSTR(""), kScopeIDDefaults);
73
74 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("testscope")), "scope is on");
75 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("bar")), "scope is on");
76 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("baz")), "scope is on");
77 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("frog")), "scope is on");
78 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("bonzo")), "scope is off");
79 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("nothing")), "scope is on");
80
81 int value = SECLOG_LEVEL_NOTICE;
82 CFNumberRef noticeNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &value);
83
84 value = SECLOG_LEVEL_INFO;
85 CFNumberRef infoNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &value);
86
87 CFDictionaryRef settings_dictionary = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
88 CFSTR(ASL_STRING_DEBUG), CFSTR("-baz"),
89 CFSTR(ASL_STRING_WARNING), CFSTR("baz,bar"),
90 noticeNumber, CFSTR("bar"),
91 infoNumber, CFSTR("baz"),
92 NULL);
93
94 ApplyScopeDictionaryForID(settings_dictionary, kScopeIDXPC);
95
96 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("testscope")), "scope is off");
97 ok(!IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("bar")), "scope is off");
98 ok(IsScopeActive(SECLOG_LEVEL_INFO, CFSTR("baz")), "scope is on");
99
100 ok(!IsScopeActive(SECLOG_LEVEL_NOTICE, CFSTR("testscope")), "scope is off");
101 ok(IsScopeActive(SECLOG_LEVEL_NOTICE, CFSTR("bar")), "scope is on");
102 ok(!IsScopeActive(SECLOG_LEVEL_NOTICE, CFSTR("baz")), "scope is off");
103
104 ok(!IsScopeActive(SECLOG_LEVEL_WARNING, CFSTR("testscope")), "scope is off");
105 ok(IsScopeActive(SECLOG_LEVEL_WARNING, CFSTR("bar")), "scope is on");
106 ok(IsScopeActive(SECLOG_LEVEL_WARNING, CFSTR("baz")), "scope is on");
107
108 ok(IsScopeActive(SECLOG_LEVEL_DEBUG, CFSTR("testscope")), "scope is on");
109 ok(IsScopeActive(SECLOG_LEVEL_DEBUG, CFSTR("bar")), "scope is on");
110 ok(!IsScopeActive(SECLOG_LEVEL_DEBUG, CFSTR("baz")), "scope is off");
111
112 ok(!IsScopeActive(SECLOG_LEVEL_ALERT, CFSTR("testscope")), "scope is off");
113 ok(!IsScopeActive(SECLOG_LEVEL_ALERT, CFSTR("bar")), "scope is off");
114 ok(!IsScopeActive(SECLOG_LEVEL_ALERT, CFSTR("baz")), "scope is off");
115
116 CFReleaseSafe(noticeNumber);
117 CFReleaseSafe(infoNumber);
118 CFReleaseSafe(settings_dictionary);
119
120 ApplyScopeListForIDC("", kScopeIDXPC);
121 ApplyScopeListForIDC("", kScopeIDDefaults);
122
123 }
124
125 #if !defined(NDEBUG)
126 #define kTestLogCount (6 + 5)
127 #else
128 #define kTestLogCount (6 + 1)
129 #endif
130
131 static void
132 testLog()
133 {
134 int value = SECLOG_LEVEL_NOTICE;
135 CFNumberRef noticeNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &value);
136
137 value = SECLOG_LEVEL_INFO;
138 CFNumberRef infoNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &value);
139
140 CFDictionaryRef settings_dictionary = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
141 CFSTR(ASL_STRING_DEBUG), CFSTR("-baz"),
142 CFSTR(ASL_STRING_WARNING), CFSTR("baz,bar"),
143 noticeNumber, CFSTR("-bar"),
144 infoNumber, CFSTR("baz"),
145 NULL);
146
147
148 ApplyScopeDictionaryForID(settings_dictionary, kScopeIDXPC);
149
150 __block int level = -1;
151 __block CFStringRef scope = NULL;
152 __block CFStringRef message = NULL;
153 __block CFStringRef file = NULL;
154 __block CFStringRef function = NULL;
155 __block int line = 0;
156
157 __block bool called = false;
158
159 security_log_handler verify = ^(int level_sent, CFStringRef scope_sent, const char *functionC,
160 const char *fileC, int line_sent, CFStringRef message_sent) {
161 called = true;
162
163 level = level_sent;
164 scope = CFRetainSafe(scope_sent);
165 file = CFStringCreateWithCString(kCFAllocatorDefault, fileC, kCFStringEncodingUTF8);
166 function = CFStringCreateWithCString(kCFAllocatorDefault, functionC, kCFStringEncodingUTF8);
167 line = line_sent;
168 message = CFRetainSafe(message_sent);
169 };
170
171 add_security_log_handler(verify);
172
173 called = false; CFReleaseNull(scope); CFReleaseNull(message); CFReleaseNull(file); CFReleaseNull(function); level = -1; line = 0;
174
175 secinfo("bar", "Get this!");
176
177 #if !defined(NDEBUG)
178 is(called, true, "Handler called");
179 is(level, SECLOG_LEVEL_DEBUG, "level");
180 eq_cf(scope, CFSTR("bar"), "Scope");
181 eq_cf(message, CFSTR("Get this!"), "message");
182 eq_cf(function, CFSTR("testLog"), "function");
183 #else
184 is(called, false, "Handler not called");
185 #endif
186
187 called = false;
188 CFReleaseNull(scope);
189 CFReleaseNull(message);
190 CFReleaseNull(file);
191 CFReleaseNull(function);
192
193 secnotice("bunz", "Get this, too!");
194
195 is(called, true, "Handler called");
196 is(level, SECLOG_LEVEL_NOTICE, "level");
197 eq_cf(scope, CFSTR("bunz"), "Scope");
198 eq_cf(message, CFSTR("Get this, too!"), "message");
199 eq_cf(function, CFSTR("testLog"), "function");
200
201 CFReleaseNull(scope);
202 CFReleaseNull(message);
203 CFReleaseNull(file);
204 CFReleaseNull(function);
205
206 remove_security_log_handler(verify);
207
208 CFReleaseSafe(settings_dictionary);
209 CFReleaseSafe(infoNumber);
210 CFReleaseSafe(noticeNumber);
211
212 CFPropertyListRef result = CopyCurrentScopePlist();
213
214 ok(result, "exported");
215
216 CFReleaseSafe(result);
217 }
218 #endif
219
220
221 int
222 su_07_debugging(int argc, char *const *argv) {
223 #if USINGOLDLOGGING
224 plan_tests(kTestCount + kTestLogCount);
225 tests();
226 testLog();
227 #else
228 plan_tests(1);
229 ok(1, "Using os_log");
230 #endif
231
232 return 0;
233 }