]>
git.saurik.com Git - apple/security.git/blob - Security/regressions/test/testmore.c
2 * Copyright (c) 2005-2007,2012-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@
31 #include <sys/types.h>
34 #include <AvailabilityMacros.h>
39 static int test_num
= 0;
40 static int test_fails
= 0;
41 static int test_cases
= 0;
42 static const char *test_plan_file
;
43 static int test_plan_line
=0;
45 const char *test_directive
= NULL
;
46 const char *test_reason
= NULL
;
48 static void fprint_string(FILE *file
, CFStringRef string
) {
50 CFRange range
= { .location
= 0 };
51 range
.length
= CFStringGetLength(string
);
52 while (range
.length
> 0) {
53 CFIndex bytesUsed
= 0;
54 CFIndex converted
= CFStringGetBytes(string
, range
, kCFStringEncodingUTF8
, 0, false, buf
, sizeof(buf
), &bytesUsed
);
55 fwrite(buf
, 1, bytesUsed
, file
);
56 range
.length
-= converted
;
57 range
.location
+= converted
;
61 static void cffprint(FILE *file
, CFStringRef fmt
, ...) CF_FORMAT_FUNCTION(2,0);
63 static void cffprint_v(FILE *file
, CFStringRef fmt
, va_list args
);
64 static void cffprint_c_v(FILE *file
, const char *fmt
, va_list args
);
66 static void cffprint_v(FILE *file
, CFStringRef fmt
, va_list args
) {
67 CFStringRef line
= CFStringCreateWithFormatAndArguments(NULL
, NULL
, fmt
, args
);
68 fprint_string(file
, line
);
72 static void cffprint_c_v(FILE *file
, const char *fmt
, va_list args
) {
73 CFStringRef cffmt
= CFStringCreateWithCString(kCFAllocatorDefault
, fmt
, kCFStringEncodingUTF8
);
74 cffprint_v(file
, cffmt
, args
);
78 static void cffprint(FILE *file
, CFStringRef fmt
, ...) {
81 cffprint_v(file
, fmt
, args
);
85 void test_skip(const char *reason
, int how_many
, int unless
)
91 for (done
= 0; done
< how_many
; ++done
)
92 test_ok(1, NULL
, "skip", reason
, __FILE__
, __LINE__
, NULL
);
95 void test_bail_out(const char *reason
, const char *file
, unsigned line
)
97 fprintf(stdout
, "[FAIL] BAIL OUT! (%s at line %u) %s\n", file
, line
, reason
);
102 void test_plan_skip_all(const char *reason
)
104 if (test_num
< test_cases
)
106 test_skip(reason
, test_cases
- test_num
, 0);
110 static int test_plan_exit(void)
119 fprintf(stdout
, "[BEGIN] plan_tests\nNo tests run!\n[WARN] plan_tests\n");
124 fprintf(stdout
, "[BEGIN] plan_tests\nLooks like your test died before it could output anything.\n[FAIL] plan_tests\n");
128 else if (test_num
< test_cases
)
130 fprintf(stdout
, "[BEGIN] plan_tests\nLooks like you planned %d tests but only ran %d.\n[WARN] plan_tests\n",
131 test_cases
, test_num
);
133 else if (test_num
> test_cases
)
135 fprintf(stdout
, "[BEGIN] plan_tests\nLooks like you planned %d tests but ran %d.\n[WARN] plan_tests\n",
136 test_cases
, test_num
);
140 fprintf(stdout
, "Failed %d tests of %d.\n", test_fails
, test_num
);
145 /* reset the test plan */
153 void test_plan_tests(int count
, const char *file
, unsigned line
)
156 if (atexit(test_plan_exit
) < 0)
158 fprintf(stdout
, "failed to setup atexit handler: %s\n",
168 "[FAIL] You tried to plan twice!\n");
177 fprintf(stdout
, "[BEGIN] plan_tests\nYou said to run 0 tests! "
178 "You've got to run something.\n[WARN] plan_tests\n");
190 test_diag(const char *directive
, const char *reason
,
191 const char *file
, unsigned line
, const char *fmt
, ...)
193 int is_todo
= directive
&& !strcmp(directive
, "TODO");
210 vfprintf(stdout
, fmt
, args
);
221 test_ok(int passed
, __attribute((cf_consumed
)) CFStringRef description
, const char *directive
,
222 const char *reason
, const char *file
, unsigned line
,
223 const char *fmt
, ...)
225 int is_todo
= !passed
&& directive
&& !strcmp(directive
, "TODO");
226 int is_setup
= directive
&& !is_todo
&& !strcmp(directive
, "SETUP");
233 fprintf(stdout
, "[BEGIN] SETUP\n");
237 cffprint_c_v(stdout
, fmt
, args
);
240 cffprint(stdout
, CFSTR("[WARN] SETUP%s%@%s%s\n"),
241 description
? " - " : "",
242 description
? description
: CFSTR(""),
244 reason
? reason
: "");
252 atexit((void(*)(void))test_plan_exit
);
253 fprintf(stdout
, "[FAIL] You tried to run a test without a plan! "
254 "Gotta have a plan. at %s line %u\n", file
, line
);
260 if (!passed
&& !is_todo
)
263 /* We only print this when a test fail, unless verbose is enabled */
264 if ((!passed
&& !is_todo
) || test_verbose
> 0) {
266 if (test_strict_bats
) {
267 cffprint(stdout
, CFSTR("[BEGIN] %d%s%@\n"),
269 description
? " - " : "",
270 description
? description
: CFSTR(""));
272 if (is_todo
&& passed
) {
273 fprintf(stdout
, "%s:%d: warning: Unexpectedly passed (TODO) test\n", file
, line
);
274 } else if (is_todo
&& !passed
) {
275 /* Enable this to output TODO as warning */
276 fprintf(stdout
, "%s:%d: ok: Failed (TODO) test\n", file
, line
);
277 } else if (!passed
) {
278 fprintf(stdout
, "%s:%d: error: Failed test\n", file
, line
);
283 cffprint_c_v(stdout
, fmt
, args
);
286 cffprint(stdout
, CFSTR("[%s] %d%s%@%s%s%s%s\n"), passed
? (is_todo
? "WARN" : "PASS") : (is_todo
? "PASS" : "FAIL"),
288 description
? " - " : "",
289 description
? description
: CFSTR(""),
290 directive
? " # " : "",
291 directive
? directive
: "",
293 reason
? reason
: "");
299 CFRelease(description
);
309 static int bufnum
= 0;
310 static char buf
[2][20];
311 bufnum
= bufnum
? 0 : 1;
312 sprintf(buf
[bufnum
], "0x%X", err
);
315 if (err
>= errSecErrnoBase
&& err
<= errSecErrnoLimit
)
316 return strerror(err
- 100000);
318 #ifdef MAC_OS_X_VERSION_10_4
319 /* AvailabilityMacros.h would only define this if we are on a
320 Tiger or later machine. */
321 extern const char *cssmErrorString(long);
322 return cssmErrorString(err
);
323 #else /* !defined(MAC_OS_X_VERSION_10_4) */
324 extern const char *_ZN8Security15cssmErrorStringEl(long);
325 return _ZN8Security15cssmErrorStringEl(err
);
326 #endif /* MAC_OS_X_VERSION_10_4 */
330 /* run one test, described by test, return info in test struct */
331 int run_one_test(struct one_test_s
*test
, int argc
, char * const *argv
)
333 struct timeval start
, stop
;
335 if(test
->entry
==NULL
) {
336 fprintf(stdout
, "%s:%d: error, no entry\n", __FILE__
, __LINE__
);
340 gettimeofday(&start
, NULL
);
341 test
->entry(argc
, argv
);
342 gettimeofday(&stop
, NULL
);
345 /* this may overflow... */
346 test
->duration
=(stop
.tv_sec
-start
.tv_sec
)*1000+(stop
.tv_usec
/1000)-(start
.tv_usec
/1000);
347 test
->failed_tests
=test_fails
;
349 return test_plan_exit();