2 * Copyright (c) 2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <utilities/SecCFRelease.h>
28 #include <utilities/SecCFWrappers.h>
29 #include <utilities/SecIOFormat.h>
31 #include "utilities_regressions.h"
32 #include "utilities/debugging.h"
33 #include "utilities/debugging_test.h"
36 #define kTestCount (39)
40 ok(IsScopeActive(SECLOG_LEVEL_ERR
, NULL
), "Errors are active by default");
42 ok(!IsScopeActive(SECLOG_LEVEL_INFO
, CFSTR("testscope")), "scope is off");
44 ApplyScopeListForIDC("-first", kScopeIDXPC
);
46 ok(IsScopeActive(SECLOG_LEVEL_INFO
, CFSTR("testscope")), "scope is on");
47 ok(!IsScopeActive(SECLOG_LEVEL_INFO
, CFSTR("first")), "scope is off");
49 ApplyScopeListForIDC("first", kScopeIDXPC
);
51 ok(IsScopeActive(SECLOG_LEVEL_INFO
, CFSTR("first")), "scope is on");
52 ok(!IsScopeActive(SECLOG_LEVEL_INFO
, CFSTR("testscope")), "scope is off");
54 ApplyScopeListForIDC("testscope, bar, baz,frog", kScopeIDXPC
);
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");
63 ApplyScopeListForID(CFSTR("-bonzo, boy"), kScopeIDDefaults
);
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");
72 ApplyScopeListForID(CFSTR(""), kScopeIDDefaults
);
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");
81 int value
= SECLOG_LEVEL_NOTICE
;
82 CFNumberRef noticeNumber
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &value
);
84 value
= SECLOG_LEVEL_INFO
;
85 CFNumberRef infoNumber
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &value
);
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"),
94 ApplyScopeDictionaryForID(settings_dictionary
, kScopeIDXPC
);
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");
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");
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");
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");
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");
116 CFReleaseSafe(noticeNumber
);
117 CFReleaseSafe(infoNumber
);
118 CFReleaseSafe(settings_dictionary
);
120 ApplyScopeListForIDC("", kScopeIDXPC
);
121 ApplyScopeListForIDC("", kScopeIDDefaults
);
126 #define kTestLogCount (6 + 5)
128 #define kTestLogCount (6 + 1)
134 int value
= SECLOG_LEVEL_NOTICE
;
135 CFNumberRef noticeNumber
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &value
);
137 value
= SECLOG_LEVEL_INFO
;
138 CFNumberRef infoNumber
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &value
);
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"),
148 ApplyScopeDictionaryForID(settings_dictionary
, kScopeIDXPC
);
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;
157 __block
bool called
= false;
159 security_log_handler verify
= ^(int level_sent
, CFStringRef scope_sent
, const char *functionC
,
160 const char *fileC
, int line_sent
, CFStringRef message_sent
) {
164 scope
= CFRetainSafe(scope_sent
);
165 file
= CFStringCreateWithCString(kCFAllocatorDefault
, fileC
, kCFStringEncodingUTF8
);
166 function
= CFStringCreateWithCString(kCFAllocatorDefault
, functionC
, kCFStringEncodingUTF8
);
168 message
= CFRetainSafe(message_sent
);
171 add_security_log_handler(verify
);
173 called
= false; CFReleaseNull(scope
); CFReleaseNull(message
); CFReleaseNull(file
); CFReleaseNull(function
); level
= -1; line
= 0;
175 secinfo("bar", "Get this!");
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");
184 is(called
, false, "Handler not called");
188 CFReleaseNull(scope
);
189 CFReleaseNull(message
);
191 CFReleaseNull(function
);
193 secnotice("bunz", "Get this, too!");
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");
201 CFReleaseNull(scope
);
202 CFReleaseNull(message
);
204 CFReleaseNull(function
);
206 remove_security_log_handler(verify
);
208 CFReleaseSafe(settings_dictionary
);
209 CFReleaseSafe(infoNumber
);
210 CFReleaseSafe(noticeNumber
);
212 CFPropertyListRef result
= CopyCurrentScopePlist();
214 ok(result
, "exported");
216 CFReleaseSafe(result
);
222 su_07_debugging(int argc
, char *const *argv
) {
224 plan_tests(kTestCount
+ kTestLogCount
);
229 ok(1, "Using os_log");