]>
git.saurik.com Git - apple/security.git/blob - regressions/test/testmore.c
2 * Copyright (c) 2005-2007 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(FILE *file
, CFStringRef fmt
, ...) {
66 CFStringRef line
= CFStringCreateWithFormatAndArguments(NULL
, NULL
, fmt
, args
);
68 fprint_string(file
, line
);
72 void test_skip(const char *reason
, int how_many
, int unless
)
78 for (done
= 0; done
< how_many
; ++done
)
79 test_ok(1, NULL
, "skip", reason
, __FILE__
, __LINE__
, NULL
);
82 void test_bail_out(const char *reason
, const char *file
, unsigned line
)
84 printf("BAIL OUT! (%s at line %u) %s\n", file
, line
, reason
);
89 void test_plan_skip_all(const char *reason
)
91 if (test_num
< test_cases
)
93 test_skip(reason
, test_cases
- test_num
, 0);
97 static int test_plan_exit(void)
106 fprintf(stderr
, "%s:%u: warning: No tests run!\n", test_plan_file
, test_plan_line
);
111 fprintf(stderr
, "%s:%u: error: Looks like your test died before it could "
112 "output anything.\n", test_plan_file
, test_plan_line
);
116 else if (test_num
< test_cases
)
118 fprintf(stderr
, "%s:%u: warning: Looks like you planned %d tests but only ran %d.\n",
119 test_plan_file
, test_plan_line
, test_cases
, test_num
);
120 status
= test_fails
+ test_cases
- test_num
;
122 else if (test_num
> test_cases
)
124 fprintf(stderr
, "%s:%u: warning: Looks like you planned %d tests but ran %d extra.\n",
125 test_plan_file
, test_plan_line
, test_cases
, test_num
- test_cases
);
130 fprintf(stderr
, "%s:%u: error: Looks like you failed %d tests of %d.\n",
131 test_plan_file
, test_plan_line
, test_fails
, test_cases
);
137 /* reset the test plan */
145 void test_plan_tests(int count
, const char *file
, unsigned line
)
148 if (atexit(test_plan_exit
) < 0)
150 fprintf(stderr
, "failed to setup atexit handler: %s\n",
160 "%s:%u: error: You tried to plan twice!\n",
170 fprintf(stderr
, "%s:%u: warning: You said to run 0 tests! You've got to run "
171 "something.\n", file
, line
);
180 fprintf(stderr
, "%s:%u: note: 1..%d\n", file
, line
, test_cases
);
186 test_diag(const char *directive
, const char *reason
,
187 const char *file
, unsigned line
, const char *fmt
, ...)
189 int is_todo
= directive
&& !strcmp(directive
, "TODO");
207 vfprintf(stderr
, fmt
, args
);
218 test_ok(int passed
, __attribute((cf_consumed
)) CFStringRef description
, const char *directive
,
219 const char *reason
, const char *file
, unsigned line
,
220 const char *fmt
, ...)
222 int is_todo
= !passed
&& directive
&& !strcmp(directive
, "TODO");
223 int is_setup
= directive
&& !is_todo
&& !strcmp(directive
, "SETUP");
230 cffprint(stderr
, CFSTR("# SETUP not ok%s%@%s%s\n"),
231 description
? " - " : "",
232 description
? description
: CFSTR(""),
234 reason
? reason
: "");
241 atexit((void(*)(void))test_plan_exit
);
242 fprintf(stderr
, "You tried to run a test without a plan! "
243 "Gotta have a plan. at %s line %u\n", file
, line
);
249 if (test_num
> test_cases
|| (!passed
&& !is_todo
))
252 /* We only print this when a test fail, unless verbose is enabled */
253 if ((!passed
) || (test_verbose
> 0)) {
254 cffprint(stderr
, CFSTR("%s:%u: note: %sok %d%s%@%s%s%s%s\n"),
255 file
, line
, passed
? "" : "not ", test_num
,
256 description
? " - " : "",
257 description
? description
: CFSTR(""),
258 directive
? " # " : "",
259 directive
? directive
: "",
261 reason
? reason
: "");
275 /* Enable this to output TODO as warning */
277 printf("%s:%d: warning: Failed (TODO) test\n", file
, line
);
286 fprintf(stderr
, "%s:%d: error: Failed test\n", file
, line
);
288 vfprintf(stderr
, fmt
, args
);
296 CFRelease(description
);
306 static int bufnum
= 0;
307 static char buf
[2][20];
308 bufnum
= bufnum
? 0 : 1;
309 sprintf(buf
[bufnum
], "0x%X", err
);
312 if (err
>= errSecErrnoBase
&& err
<= errSecErrnoLimit
)
313 return strerror(err
- 100000);
315 #ifdef MAC_OS_X_VERSION_10_4
316 /* AvailabilityMacros.h would only define this if we are on a
317 Tiger or later machine. */
318 extern const char *cssmErrorString(long);
319 return cssmErrorString(err
);
320 #else /* !defined(MAC_OS_X_VERSION_10_4) */
321 extern const char *_ZN8Security15cssmErrorStringEl(long);
322 return _ZN8Security15cssmErrorStringEl(err
);
323 #endif /* MAC_OS_X_VERSION_10_4 */
327 /* run one test, described by test, return info in test struct */
328 int run_one_test(struct one_test_s
*test
, int argc
, char * const *argv
)
330 struct timeval start
, stop
;
332 if(test
->entry
==NULL
) {
333 printf("%s:%d: error: wtf?\n", __FILE__
, __LINE__
);
337 gettimeofday(&start
, NULL
);
338 test
->entry(argc
, argv
);
339 gettimeofday(&stop
, NULL
);
342 /* this may overflow... */
343 test
->duration
=(stop
.tv_sec
-start
.tv_sec
)*1000+(stop
.tv_usec
/1000)-(start
.tv_usec
/1000);
344 test
->failed_tests
=test_fails
;
346 return test_plan_exit();