]> git.saurik.com Git - apple/security.git/blob - OSX/regressions/test/testmore.h
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / regressions / test / testmore.h
1 /*
2 * Copyright (c) 2005-2007,2012-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 * testmore.h
24 */
25
26 #ifndef _TESTMORE_H_
27 #define _TESTMORE_H_ 1
28
29 #include <errno.h>
30 #include <inttypes.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <CoreFoundation/CFString.h>
34
35 __BEGIN_DECLS
36
37 // TODO Move all the testlist[] stuff and one_test_s stuff to testenv.h
38
39 /* Macros to be used in testlist.h style headers. */
40 #define ONE_TEST(x) int x(int argc, char *const *argv);
41 #define DISABLED_ONE_TEST(x) ONE_TEST(x)
42 #define OFF_ONE_TEST(x) ONE_TEST(x)
43
44 typedef int (*one_test_entry)(int argc, char *const *argv);
45
46 #define ONE_TEST_ENTRY(x) int x(int argc, char *const *argv)
47
48 struct one_test_s {
49 char *name; /* test name. */
50 one_test_entry entry; /* entry point. */
51 int off; /* off by default. */
52 int sub_tests; /* number of subtests. */
53 int failed_tests; /* number of failed tests. */
54 int todo_pass_tests; /* number of todo tests that unexpected passed. */
55 int todo_tests; /* number of todo tests that failed as expected. */
56 int actual_tests; /* number of tests attempted. */
57 int planned_tests; /* number of planned tests. */
58 const char *plan_file; /* full path to file that called plan_tests() */
59 int plan_line; /* line number in plan_file at which plan_tests was called. */
60 unsigned long duration; /* test duration in msecs. */
61 /* add more later: timing, etc... */
62 };
63
64 extern struct one_test_s testlist[];
65
66 /* this test harnes rely on shadowing for TODO, SKIP and SETUP blocks */
67 #pragma GCC diagnostic ignored "-Wshadow"
68
69 #pragma clang diagnostic push
70 #pragma clang diagnostic ignored "-Wunused-function"
71 // Call this function to stop the analyzer on a test failure. No-op.
72 static void test_failed_noreturn() __attribute((analyzer_noreturn)) {
73 // No-op.
74 }
75 #define TEST_CHECK(result) ((result) ? 1 : ({ test_failed_noreturn(); 0; }))
76 #pragma clang diagnostic pop
77
78
79 #define test_create_description(TESTNAME, ...) \
80 CFStringCreateWithFormat(NULL, NULL, CFSTR(TESTNAME), ## __VA_ARGS__)
81
82 #define ok(THIS, ...) \
83 ({ \
84 test_ok(TEST_CHECK(!!(THIS)), test_create_description(__VA_ARGS__), test_directive, \
85 test_reason, __FILE__, __LINE__, NULL); \
86 })
87 #define is(THIS, THAT, ...) \
88 ({ \
89 __typeof__(THIS) _this = (THIS); \
90 __typeof__(THAT) _that = (THAT); \
91 test_ok(TEST_CHECK(_this == _that), test_create_description(__VA_ARGS__), \
92 test_directive, test_reason, __FILE__, __LINE__, \
93 "# got: '%d'\n" \
94 "# expected: '%d'\n", \
95 _this, _that); \
96 })
97 #define isnt(THIS, THAT, ...) \
98 cmp_ok((THIS), !=, (THAT), __VA_ARGS__)
99 #define diag(MSG, ARGS...) \
100 test_diag(test_directive, test_reason, __FILE__, __LINE__, MSG, ## ARGS)
101 #define cmp_ok(THIS, OP, THAT, ...) \
102 ({ \
103 __typeof__(THIS) _this = (THIS); \
104 __typeof__(THAT) _that = (THAT); \
105 test_ok(TEST_CHECK(_this OP _that), test_create_description(__VA_ARGS__), \
106 test_directive, test_reason, __FILE__, __LINE__, \
107 "# '%d'\n" \
108 "# " #OP "\n" \
109 "# '%d'\n", \
110 _this, _that); \
111 })
112 #define eq_string(THIS, THAT, ...) \
113 ({ \
114 const char *_this = (THIS); \
115 const char *_that = (THAT); \
116 test_ok(TEST_CHECK(!strcmp(_this, _that)), test_create_description(__VA_ARGS__), \
117 test_directive, test_reason, __FILE__, __LINE__, \
118 "# '%s'\n" \
119 "# eq\n" \
120 "# '%s'\n", \
121 _this, _that); \
122 })
123 #define eq_stringn(THIS, THISLEN, THAT, THATLEN, ...) \
124 ({ \
125 __typeof__(THISLEN) _thislen = (THISLEN); \
126 __typeof__(THATLEN) _thatlen = (THATLEN); \
127 const char *_this = (THIS); \
128 const char *_that = (THAT); \
129 test_ok(TEST_CHECK(_thislen == _thatlen) && TEST_CHECK(!strncmp(_this, _that, _thislen)), \
130 test_create_description(__VA_ARGS__), test_directive, test_reason, \
131 __FILE__, __LINE__, \
132 "# '%.*s'\n" \
133 "# eq\n" \
134 "# '%.*s'\n", \
135 (int)_thislen, _this, (int)_thatlen, _that); \
136 })
137 #define eq_cf(THIS, THAT, ...) \
138 ({ \
139 CFTypeRef _this = (THIS); \
140 CFTypeRef _that = (THAT); \
141 test_ok(TEST_CHECK(CFEqualSafe(_this, _that)), test_create_description(__VA_ARGS__), test_directive, test_reason, \
142 __FILE__, __LINE__, \
143 "# '%@'\n" \
144 "# eq\n" \
145 "# '%@'\n", \
146 _this, _that); \
147 })
148
149 #define like(THIS, REGEXP, ...) like_not_yet_implemented()
150 #define unlike(THIS, REGEXP, ...) unlike_not_yet_implemented()
151 #define is_deeply(STRUCT1, STRUCT2, ...) is_deeply_not_yet_implemented()
152 #define TODO switch(0) default
153 #define SKIP switch(0) default
154 #define SETUP switch(0) default
155 #define todo(REASON) const char *test_directive __attribute__((unused)) = "TODO", \
156 *test_reason __attribute__((unused)) = (REASON)
157 #define skip(WHY, HOW_MANY, UNLESS) if (!(UNLESS)) \
158 { test_skip((WHY), (HOW_MANY), 0); break; }
159 #define setup(REASON) const char *test_directive = "SETUP", \
160 *test_reason = (REASON)
161 #define pass(...) ok(1, __VA_ARGS__)
162 #define fail(...) ok(0, __VA_ARGS__)
163 #define BAIL_OUT(WHY) test_bail_out(WHY, __FILE__, __LINE__)
164 #define plan_skip_all(REASON) test_plan_skip_all(REASON)
165 #define plan_tests(COUNT) test_plan_tests(COUNT, __FILE__, __LINE__)
166
167 #define ok_status(THIS, ...) \
168 ({ \
169 OSStatus _this = THIS; \
170 test_ok(!_this, test_create_description(__VA_ARGS__), \
171 test_directive, test_reason, __FILE__, __LINE__, \
172 "# status: %s(%" PRId32 ")\n", \
173 sec_errstr(_this), _this); \
174 })
175 #define is_status(THIS, THAT, ...) \
176 ({ \
177 OSStatus _this = (THIS); \
178 OSStatus _that = (THAT); \
179 test_ok(TEST_CHECK(_this == _that), test_create_description(__VA_ARGS__), \
180 test_directive, test_reason, __FILE__, __LINE__, \
181 "# got: %s(%" PRId32 ")\n" \
182 "# expected: %s(%" PRId32 ")\n", \
183 sec_errstr(_this), _this, sec_errstr(_that), _that); \
184 })
185 #define ok_unix(THIS, ...) \
186 ({ \
187 int _this = (THIS) < 0 ? errno : 0; \
188 test_ok(!_this, test_create_description(__VA_ARGS__), \
189 test_directive, test_reason, __FILE__, __LINE__, \
190 "# got: %s(%d)\n", \
191 strerror(_this), _this); \
192 })
193 #define is_unix(THIS, THAT, ...) \
194 ({ \
195 int _result = (THIS); \
196 int _this = _result < 0 ? errno : 0; \
197 int _that = (THAT); \
198 _that && _result < 0 \
199 ? test_ok(_this == _that, test_create_description(__VA_ARGS__), \
200 test_directive, test_reason, __FILE__, __LINE__, \
201 "# got: %s(%d)\n" \
202 "# expected: %s(%d)\n", \
203 strerror(_this), _this, strerror(_that), _that) \
204 : test_ok(_this == _that, test_create_description(__VA_ARGS__), \
205 test_directive, test_reason, __FILE__, __LINE__, \
206 "# got: %d\n" \
207 "# expected errno: %s(%d)\n", \
208 _result, strerror(_that), _that); \
209 })
210
211
212 extern const char *test_directive;
213 extern const char *test_reason;
214
215 void test_bail_out(const char *reason, const char *file, unsigned line);
216 int test_diag(const char *directive, const char *reason,
217 const char *file, unsigned line, const char *fmt, ...);
218 int test_ok(int passed, __attribute((cf_consumed)) CFStringRef description, const char *directive,
219 const char *reason, const char *file, unsigned line, const char *fmt, ...);
220 void test_plan_skip_all(const char *reason);
221 void test_plan_tests(int count, const char *file, unsigned line);
222 int test_plan_ok(void);
223 void test_plan_final(int *failed, int *todo_pass, int *todo, int *actual, int *planned, const char **file, int *line);
224
225 void test_skip(const char *reason, int how_many, int unless);
226
227 const char *sec_errstr(int err);
228
229 __END_DECLS
230
231 #endif /* !_TESTMORE_H_ */