]> git.saurik.com Git - apple/security.git/blob - OSX/regressions/test/testmore.h
Security-57337.40.85.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 #define test_create_description(TESTNAME, ...) \
70 CFStringCreateWithFormat(NULL, NULL, CFSTR(TESTNAME), ## __VA_ARGS__)
71
72 #define ok(THIS, ...) \
73 ({ \
74 bool is_ok = !!(THIS); \
75 test_ok(is_ok, test_create_description(__VA_ARGS__), test_directive, \
76 test_reason, __FILE__, __LINE__, NULL); \
77 })
78 #define is(THIS, THAT, ...) \
79 ({ \
80 __typeof__(THIS) _this = (THIS); \
81 __typeof__(THAT) _that = (THAT); \
82 test_ok((_this == _that), test_create_description(__VA_ARGS__), \
83 test_directive, test_reason, __FILE__, __LINE__, \
84 "# got: '%d'\n" \
85 "# expected: '%d'\n", \
86 _this, _that); \
87 })
88 #define isnt(THIS, THAT, ...) \
89 cmp_ok((THIS), !=, (THAT), __VA_ARGS__)
90 #define diag(MSG, ARGS...) \
91 test_diag(test_directive, test_reason, __FILE__, __LINE__, MSG, ## ARGS)
92 #define cmp_ok(THIS, OP, THAT, ...) \
93 ({ \
94 __typeof__(THIS) _this = (THIS); \
95 __typeof__(THAT) _that = (THAT); \
96 test_ok((_this OP _that), test_create_description(__VA_ARGS__), \
97 test_directive, test_reason, __FILE__, __LINE__, \
98 "# '%d'\n" \
99 "# " #OP "\n" \
100 "# '%d'\n", \
101 _this, _that); \
102 })
103 #define eq_string(THIS, THAT, ...) \
104 ({ \
105 const char *_this = (THIS); \
106 const char *_that = (THAT); \
107 test_ok(!strcmp(_this, _that), test_create_description(__VA_ARGS__), \
108 test_directive, test_reason, __FILE__, __LINE__, \
109 "# '%s'\n" \
110 "# eq\n" \
111 "# '%s'\n", \
112 _this, _that); \
113 })
114 #define eq_stringn(THIS, THISLEN, THAT, THATLEN, ...) \
115 ({ \
116 __typeof__(THISLEN) _thislen = (THISLEN); \
117 __typeof__(THATLEN) _thatlen = (THATLEN); \
118 const char *_this = (THIS); \
119 const char *_that = (THAT); \
120 test_ok(_thislen == _thatlen && !strncmp(_this, _that, _thislen), \
121 test_create_description(__VA_ARGS__), test_directive, test_reason, \
122 __FILE__, __LINE__, \
123 "# '%.*s'\n" \
124 "# eq\n" \
125 "# '%.*s'\n", \
126 (int)_thislen, _this, (int)_thatlen, _that); \
127 })
128 #define eq_cf(THIS, THAT, ...) \
129 ({ \
130 CFTypeRef _this = (THIS); \
131 CFTypeRef _that = (THAT); \
132 test_ok(CFEqualSafe(_this, _that), test_create_description(__VA_ARGS__), test_directive, test_reason, \
133 __FILE__, __LINE__, \
134 "# '%@'\n" \
135 "# eq\n" \
136 "# '%@'\n", \
137 _this, _that); \
138 })
139
140 #define like(THIS, REGEXP, ...) like_not_yet_implemented()
141 #define unlike(THIS, REGEXP, ...) unlike_not_yet_implemented()
142 #define is_deeply(STRUCT1, STRUCT2, ...) is_deeply_not_yet_implemented()
143 #define TODO switch(0) default
144 #define SKIP switch(0) default
145 #define SETUP switch(0) default
146 #define todo(REASON) const char *test_directive __attribute__((unused)) = "TODO", \
147 *test_reason __attribute__((unused)) = (REASON)
148 #define skip(WHY, HOW_MANY, UNLESS) if (!(UNLESS)) \
149 { test_skip((WHY), (HOW_MANY), 0); break; }
150 #define setup(REASON) const char *test_directive = "SETUP", \
151 *test_reason = (REASON)
152 #define pass(...) ok(1, __VA_ARGS__)
153 #define fail(...) ok(0, __VA_ARGS__)
154 #define BAIL_OUT(WHY) test_bail_out(WHY, __FILE__, __LINE__)
155 #define plan_skip_all(REASON) test_plan_skip_all(REASON)
156 #define plan_tests(COUNT) test_plan_tests(COUNT, __FILE__, __LINE__)
157
158 #define ok_status(THIS, ...) \
159 ({ \
160 OSStatus _this = (THIS); \
161 test_ok(!_this, test_create_description(__VA_ARGS__), \
162 test_directive, test_reason, __FILE__, __LINE__, \
163 "# status: %s(%" PRId32 ")\n", \
164 sec_errstr(_this), _this); \
165 })
166 #define is_status(THIS, THAT, ...) \
167 ({ \
168 OSStatus _this = (THIS); \
169 OSStatus _that = (THAT); \
170 test_ok(_this == _that, test_create_description(__VA_ARGS__), \
171 test_directive, test_reason, __FILE__, __LINE__, \
172 "# got: %s(%" PRId32 ")\n" \
173 "# expected: %s(%" PRId32 ")\n", \
174 sec_errstr(_this), _this, sec_errstr(_that), _that); \
175 })
176 #define ok_unix(THIS, ...) \
177 ({ \
178 int _this = (THIS) < 0 ? errno : 0; \
179 test_ok(!_this, test_create_description(__VA_ARGS__), \
180 test_directive, test_reason, __FILE__, __LINE__, \
181 "# got: %s(%d)\n", \
182 strerror(_this), _this); \
183 })
184 #define is_unix(THIS, THAT, ...) \
185 ({ \
186 int _result = (THIS); \
187 int _this = _result < 0 ? errno : 0; \
188 int _that = (THAT); \
189 _that && _result < 0 \
190 ? test_ok(_this == _that, test_create_description(__VA_ARGS__), \
191 test_directive, test_reason, __FILE__, __LINE__, \
192 "# got: %s(%d)\n" \
193 "# expected: %s(%d)\n", \
194 strerror(_this), _this, strerror(_that), _that) \
195 : test_ok(_this == _that, test_create_description(__VA_ARGS__), \
196 test_directive, test_reason, __FILE__, __LINE__, \
197 "# got: %d\n" \
198 "# expected errno: %s(%d)\n", \
199 _result, strerror(_that), _that); \
200 })
201
202
203 extern const char *test_directive;
204 extern const char *test_reason;
205
206 void test_bail_out(const char *reason, const char *file, unsigned line);
207 int test_diag(const char *directive, const char *reason,
208 const char *file, unsigned line, const char *fmt, ...);
209 int test_ok(int passed, __attribute((cf_consumed)) CFStringRef description, const char *directive,
210 const char *reason, const char *file, unsigned line, const char *fmt, ...);
211 void test_plan_skip_all(const char *reason);
212 void test_plan_tests(int count, const char *file, unsigned line);
213 int test_plan_ok(void);
214 void test_plan_final(int *failed, int *todo_pass, int *todo, int *actual, int *planned, const char **file, int *line);
215
216 void test_skip(const char *reason, int how_many, int unless);
217
218 const char *sec_errstr(int err);
219
220 __END_DECLS
221
222 #endif /* !_TESTMORE_H_ */