]> git.saurik.com Git - apple/security.git/blob - OSX/regressions/test/testmore.h
Security-59754.80.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 (*_Nonnull one_test_entry)(int argc, char *_Nonnull const *_Nonnull argv);
45
46 #define ONE_TEST_ENTRY(x) int x(int argc, char *const *argv)
47
48 struct one_test_s {
49 const char * _Nonnull 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 *_Nonnull 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 test_IsTrue(THIS, ...) ok(THIS, __VA_ARGS__)
168 #define test_IsEqual(THIS, THAT, ...) is(THIS, THAT, __VA_ARGS__)
169 #define test_IsNotEqual(THIS, THAT, ...) isnt(THIS, THAT, __VA_ARGS__)
170 #define test_Pass(...) pass(__VA_ARGS__)
171 #define test_Fail(...) fail(__VA_ARGS__)
172
173 #define ok_status(THIS, ...) \
174 ({ \
175 OSStatus _this = THIS; \
176 test_ok(!_this, test_create_description(__VA_ARGS__), \
177 test_directive, test_reason, __FILE__, __LINE__, \
178 "# status: %s(%" PRId32 ")\n", \
179 sec_errstr(_this), _this); \
180 })
181 #define is_status(THIS, THAT, ...) \
182 ({ \
183 OSStatus _this = (THIS); \
184 OSStatus _that = (THAT); \
185 test_ok(TEST_CHECK(_this == _that), test_create_description(__VA_ARGS__), \
186 test_directive, test_reason, __FILE__, __LINE__, \
187 "# got: %s(%" PRId32 ")\n" \
188 "# expected: %s(%" PRId32 ")\n", \
189 sec_errstr(_this), _this, sec_errstr(_that), _that); \
190 })
191 #define ok_unix(THIS, ...) \
192 ({ \
193 int _this = (THIS) < 0 ? errno : 0; \
194 test_ok(!_this, test_create_description(__VA_ARGS__), \
195 test_directive, test_reason, __FILE__, __LINE__, \
196 "# got: %s(%d)\n", \
197 strerror(_this), _this); \
198 })
199 #define is_unix(THIS, THAT, ...) \
200 ({ \
201 int _result = (THIS); \
202 int _this = _result < 0 ? errno : 0; \
203 int _that = (THAT); \
204 _that && _result < 0 \
205 ? test_ok(_this == _that, test_create_description(__VA_ARGS__), \
206 test_directive, test_reason, __FILE__, __LINE__, \
207 "# got: %s(%d)\n" \
208 "# expected: %s(%d)\n", \
209 strerror(_this), _this, strerror(_that), _that) \
210 : test_ok(_this == _that, test_create_description(__VA_ARGS__), \
211 test_directive, test_reason, __FILE__, __LINE__, \
212 "# got: %d\n" \
213 "# expected errno: %s(%d)\n", \
214 _result, strerror(_that), _that); \
215 })
216
217
218 extern const char * _Nonnull test_directive;
219 extern const char * _Nonnull test_reason;
220
221 void test_bail_out(const char * _Nonnull reason, const char * _Nonnull file, unsigned line);
222 int test_diag(const char *_Nonnull directive, const char *_Nonnull reason,
223 const char *_Nonnull file, unsigned line, const char *_Nonnull fmt, ...) __attribute__((format(printf, 5, 6)));
224 int test_ok(int passed, __attribute((cf_consumed)) CFStringRef _Nullable description, const char *_Nonnull directive,
225 const char *_Nonnull reason, const char *_Nonnull file, unsigned line, const char *_Nullable fmt, ...);
226 void test_plan_skip_all(const char *_Nonnull reason);
227 void test_plan_tests(int count, const char *_Nonnull file, unsigned line);
228 int test_plan_ok(void);
229 void test_plan_final(int *_Nonnull failed, int *_Nonnull todo_pass, int *_Nonnull todo, int *_Nonnull actual, int *_Nonnull planned, const char *_Nonnull *_Nonnull file, int *_Nonnull line);
230
231 void test_skip(const char *_Nonnull reason, int how_many, int unless);
232
233 const char *_Nonnull sec_errstr(int err);
234
235 __END_DECLS
236
237 #endif /* !_TESTMORE_H_ */