]> git.saurik.com Git - apple/security.git/blob - SecurityTests/regressions/test/testmore.c
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / regressions / test / testmore.c
1 /*
2 * Copyright (c) 2005 Apple Computer, 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 * testmore.c
24 */
25
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <AvailabilityMacros.h>
34 #include <Security/cssmapple.h>
35
36 #include "testmore.h"
37
38 static int test_num = 0;
39 static int test_fails = 0;
40 static int test_cases = 0;
41
42 const char *test_directive = NULL;
43 const char *test_reason = NULL;
44
45 void test_skip(const char *reason, int how_many, int unless)
46 {
47 if (unless)
48 return;
49
50 int done;
51 for (done = 0; done < how_many; ++done)
52 test_ok(1, NULL, "skip", reason, __FILE__, __LINE__, NULL);
53 }
54
55 void test_bail_out(const char *reason, const char *file, unsigned line)
56 {
57 printf("BAIL OUT! (%s at line %u) %s\n", file, line, reason);
58 fflush(stdout);
59 exit(255);
60 }
61
62 void test_plan_skip_all(const char *reason)
63 {
64 if (test_num > test_cases)
65 {
66 test_skip(reason, test_cases - test_num, 0);
67 exit(test_fails > 255 ? 255 : test_fails);
68 }
69 }
70
71 static void test_plan_exit(void)
72 {
73 int status = 0;
74 fflush(stdout);
75
76 if (!test_num)
77 {
78 if (test_cases)
79 {
80 fprintf(stderr, "# No tests run!\n");
81 status = 255;
82 }
83 else
84 {
85 fprintf(stderr, "# Looks like your test died before it could "
86 "output anything.\n");
87 status = 255;
88 }
89 }
90 else if (test_num < test_cases)
91 {
92 fprintf(stderr, "# Looks like you planned %d tests but only ran %d.\n",
93 test_cases, test_num);
94 status = test_fails + test_cases - test_num;
95 }
96 else if (test_num > test_cases)
97 {
98 fprintf(stderr, "# Looks like you planned %d tests but ran %d extra.\n",
99 test_cases, test_num - test_cases);
100 status = test_fails;
101 }
102 else if (test_fails)
103 {
104 fprintf(stderr, "# Looks like you failed %d tests of %d.\n",
105 test_fails, test_cases);
106 status = test_fails;
107 }
108
109 fflush(stderr);
110 if (status)
111 _exit(status > 255 ? 255 : status);
112 }
113
114 void test_plan_tests(int count, const char *file, unsigned line)
115 {
116 if (atexit(test_plan_exit) < 0)
117 {
118 fprintf(stderr, "failed to setup atexit handler: %s\n",
119 strerror(errno));
120 fflush(stderr);
121 exit(255);
122 }
123
124 if (test_cases)
125 {
126 fprintf(stderr,
127 "You tried to plan twice! Second plan at %s line %u\n",
128 file, line);
129 fflush(stderr);
130 exit(255);
131 }
132 else
133 {
134 if (!count)
135 {
136 fprintf(stderr, "You said to run 0 tests! You've got to run "
137 "something.\n");
138 fflush(stderr);
139 exit(255);
140 }
141
142 test_cases = count;
143 printf("1..%d\n", test_cases);
144 fflush(stdout);
145 }
146 }
147
148 int
149 test_diag(const char *directive, const char *reason,
150 const char *file, unsigned line, const char *fmt, ...)
151 {
152 int is_todo = directive && !strcmp(directive, "TODO");
153 va_list args;
154
155 va_start(args, fmt);
156
157 if (is_todo)
158 {
159 fputs("# ", stdout);
160 if (fmt)
161 vprintf(fmt, args);
162 fputs("\n", stdout);
163 fflush(stdout);
164 }
165 else
166 {
167 fflush(stdout);
168 fputs("# ", stderr);
169 if (fmt)
170 vfprintf(stderr, fmt, args);
171 fputs("\n", stderr);
172 fflush(stderr);
173 }
174
175 va_end(args);
176
177 return 1;
178 }
179
180 int
181 test_ok(int passed, const char *description, const char *directive,
182 const char *reason, const char *file, unsigned line,
183 const char *fmt, ...)
184 {
185 int is_todo = !passed && directive && !strcmp(directive, "TODO");
186 int is_setup = directive && !is_todo && !strcmp(directive, "SETUP");
187
188 if (is_setup)
189 {
190 if (!passed)
191 {
192 fflush(stdout);
193 fprintf(stderr, "# SETUP not ok%s%s%s%s\n",
194 description ? " - " : "",
195 description ? description : "",
196 reason ? " - " : "",
197 reason ? reason : "");
198 }
199 }
200 else
201 {
202 if (!test_cases)
203 {
204 atexit(test_plan_exit);
205 fprintf(stderr, "You tried to run a test without a plan! "
206 "Gotta have a plan. at %s line %u\n", file, line);
207 fflush(stderr);
208 exit(255);
209 }
210
211 ++test_num;
212 if (test_num > test_cases || (!passed && !is_todo))
213 ++test_fails;
214
215 printf("%sok %d%s%s%s%s%s%s\n", passed ? "" : "not ", test_num,
216 description ? " - " : "",
217 description ? description : "",
218 directive ? " # " : "",
219 directive ? directive : "",
220 reason ? " " : "",
221 reason ? reason : "");
222 }
223
224 if (passed)
225 fflush(stdout);
226 else
227 {
228 va_list args;
229
230 va_start(args, fmt);
231
232 if (is_todo)
233 {
234 printf("# Failed (TODO) test (%s at line %u)\n", file, line);
235 if (fmt)
236 vprintf(fmt, args);
237 fflush(stdout);
238 }
239 else
240 {
241 fflush(stdout);
242 fprintf(stderr, "# Failed test (%s at line %u)\n", file, line);
243 if (fmt)
244 vfprintf(stderr, fmt, args);
245 fflush(stderr);
246 }
247
248 va_end(args);
249 }
250
251 return passed;
252 }
253
254
255 const char *
256 sec_errstr(int err)
257 {
258 if (err >= errSecErrnoBase && err <= errSecErrnoLimit)
259 return strerror(err - 100000);
260
261 #ifdef MAC_OS_X_VERSION_10_4
262 /* AvailabilityMacros.h would only define this if we are on a
263 Tiger or later machine. */
264 extern const char *cssmErrorString(long);
265 return cssmErrorString(err);
266 #else
267 #if 0
268 extern const char *_ZN8Security15cssmErrorStringEl(long);
269 return _ZN8Security15cssmErrorStringEl(err);
270 #else
271 return "";
272 #endif
273 #endif
274 }