1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 #ifndef TASK_H_
23 #define TASK_H_
24
25 #include "uv.h"
26
27 #include <stdio.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <inttypes.h>
32
33 #if defined(_MSC_VER) && _MSC_VER < 1600
34 # include "uv/stdint-msvc2008.h"
35 #else
36 # include <stdint.h>
37 #endif
38
39 #if !defined(_WIN32)
40 # include <sys/time.h>
41 # include <sys/resource.h> /* setrlimit() */
42 #endif
43
44 #ifdef __clang__
45 # pragma clang diagnostic ignored "-Wvariadic-macros"
46 # pragma clang diagnostic ignored "-Wc99-extensions"
47 #endif
48
49 #ifdef __GNUC__
50 # pragma GCC diagnostic ignored "-Wvariadic-macros"
51 #endif
52
53 #define TEST_PORT 9123
54 #define TEST_PORT_2 9124
55 #define TEST_PORT_3 9125
56
57 #ifdef _WIN32
58 # define TEST_PIPENAME "\\\\?\\pipe\\uv-test"
59 # define TEST_PIPENAME_2 "\\\\?\\pipe\\uv-test2"
60 # define TEST_PIPENAME_3 "\\\\?\\pipe\\uv-test3"
61 #else
62 # define TEST_PIPENAME "/tmp/uv-test-sock"
63 # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
64 # define TEST_PIPENAME_3 "/tmp/uv-test-sock3"
65 #endif
66
67 #ifdef _WIN32
68 # include <io.h>
69 # ifndef S_IRUSR
70 # define S_IRUSR _S_IREAD
71 # endif
72 # ifndef S_IWUSR
73 # define S_IWUSR _S_IWRITE
74 # endif
75 #endif
76
77 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
78
79 #define container_of(ptr, type, member) \
80 ((type *) ((char *) (ptr) - offsetof(type, member)))
81
82 typedef enum {
83 TCP = 0,
84 UDP,
85 PIPE
86 } stream_type;
87
88 /* Die with fatal error. */
89 #define FATAL(msg) \
90 do { \
91 fprintf(stderr, \
92 "Fatal error in %s on line %d: %s\n", \
93 __FILE__, \
94 __LINE__, \
95 msg); \
96 fflush(stderr); \
97 abort(); \
98 } while (0)
99
100 /* Have our own assert, so we are sure it does not get optimized away in
101 * a release build.
102 */
103 #define ASSERT(expr) \
104 do { \
105 if (!(expr)) { \
106 fprintf(stderr, \
107 "Assertion failed in %s on line %d: %s\n", \
108 __FILE__, \
109 __LINE__, \
110 #expr); \
111 abort(); \
112 } \
113 } while (0)
114
115 #define ASSERT_BASE(a, operator, b, type, conv) \
116 do { \
117 volatile type eval_a = (type) (a); \
118 volatile type eval_b = (type) (b); \
119 if (!(eval_a operator eval_b)) { \
120 fprintf(stderr, \
121 "Assertion failed in %s on line %d: `%s %s %s` " \
122 "(%"conv" %s %"conv")\n", \
123 __FILE__, \
124 __LINE__, \
125 #a, \
126 #operator, \
127 #b, \
128 eval_a, \
129 #operator, \
130 eval_b); \
131 abort(); \
132 } \
133 } while (0)
134
135 #define ASSERT_BASE_STR(expr, a, operator, b, type, conv) \
136 do { \
137 if (!(expr)) { \
138 fprintf(stderr, \
139 "Assertion failed in %s on line %d: `%s %s %s` " \
140 "(%"conv" %s %"conv")\n", \
141 __FILE__, \
142 __LINE__, \
143 #a, \
144 #operator, \
145 #b, \
146 (type)a, \
147 #operator, \
148 (type)b); \
149 abort(); \
150 } \
151 } while (0)
152
153 #define ASSERT_BASE_LEN(expr, a, operator, b, conv, len) \
154 do { \
155 if (!(expr)) { \
156 fprintf(stderr, \
157 "Assertion failed in %s on line %d: `%s %s %s` " \
158 "(%.*"#conv" %s %.*"#conv")\n", \
159 __FILE__, \
160 __LINE__, \
161 #a, \
162 #operator, \
163 #b, \
164 (int)len, \
165 a, \
166 #operator, \
167 (int)len, \
168 b); \
169 abort(); \
170 } \
171 } while (0)
172
173 #define ASSERT_BASE_HEX(expr, a, operator, b, size) \
174 do { \
175 if (!(expr)) { \
176 int i; \
177 unsigned char* a_ = (unsigned char*)a; \
178 unsigned char* b_ = (unsigned char*)b; \
179 fprintf(stderr, \
180 "Assertion failed in %s on line %d: `%s %s %s` (", \
181 __FILE__, \
182 __LINE__, \
183 #a, \
184 #operator, \
185 #b); \
186 for (i = 0; i < size; ++i) { \
187 if (i > 0) fprintf(stderr, ":"); \
188 fprintf(stderr, "%02X", a_[i]); \
189 } \
190 fprintf(stderr, " %s ", #operator); \
191 for (i = 0; i < size; ++i) { \
192 if (i > 0) fprintf(stderr, ":"); \
193 fprintf(stderr, "%02X", b_[i]); \
194 } \
195 fprintf(stderr, ")\n"); \
196 abort(); \
197 } \
198 } while (0)
199
200 #define ASSERT_EQ(a, b) ASSERT_BASE(a, ==, b, int64_t, PRId64)
201 #define ASSERT_GE(a, b) ASSERT_BASE(a, >=, b, int64_t, PRId64)
202 #define ASSERT_GT(a, b) ASSERT_BASE(a, >, b, int64_t, PRId64)
203 #define ASSERT_LE(a, b) ASSERT_BASE(a, <=, b, int64_t, PRId64)
204 #define ASSERT_LT(a, b) ASSERT_BASE(a, <, b, int64_t, PRId64)
205 #define ASSERT_NE(a, b) ASSERT_BASE(a, !=, b, int64_t, PRId64)
206
207 #define ASSERT_UINT64_EQ(a, b) ASSERT_BASE(a, ==, b, uint64_t, PRIu64)
208 #define ASSERT_UINT64_GE(a, b) ASSERT_BASE(a, >=, b, uint64_t, PRIu64)
209 #define ASSERT_UINT64_GT(a, b) ASSERT_BASE(a, >, b, uint64_t, PRIu64)
210 #define ASSERT_UINT64_LE(a, b) ASSERT_BASE(a, <=, b, uint64_t, PRIu64)
211 #define ASSERT_UINT64_LT(a, b) ASSERT_BASE(a, <, b, uint64_t, PRIu64)
212 #define ASSERT_UINT64_NE(a, b) ASSERT_BASE(a, !=, b, uint64_t, PRIu64)
213
214 #define ASSERT_DOUBLE_EQ(a, b) ASSERT_BASE(a, ==, b, double, "f")
215 #define ASSERT_DOUBLE_GE(a, b) ASSERT_BASE(a, >=, b, double, "f")
216 #define ASSERT_DOUBLE_GT(a, b) ASSERT_BASE(a, >, b, double, "f")
217 #define ASSERT_DOUBLE_LE(a, b) ASSERT_BASE(a, <=, b, double, "f")
218 #define ASSERT_DOUBLE_LT(a, b) ASSERT_BASE(a, <, b, double, "f")
219 #define ASSERT_DOUBLE_NE(a, b) ASSERT_BASE(a, !=, b, double, "f")
220
221 #define ASSERT_STR_EQ(a, b) \
222 ASSERT_BASE_STR(strcmp(a, b) == 0, a, == , b, char*, "s")
223
224 #define ASSERT_STR_NE(a, b) \
225 ASSERT_BASE_STR(strcmp(a, b) != 0, a, !=, b, char*, "s")
226
227 #define ASSERT_MEM_EQ(a, b, size) \
228 ASSERT_BASE_LEN(memcmp(a, b, size) == 0, a, ==, b, s, size)
229
230 #define ASSERT_MEM_NE(a, b, size) \
231 ASSERT_BASE_LEN(memcmp(a, b, size) != 0, a, !=, b, s, size)
232
233 #define ASSERT_MEM_HEX_EQ(a, b, size) \
234 ASSERT_BASE_HEX(memcmp(a, b, size) == 0, a, ==, b, size)
235
236 #define ASSERT_MEM_HEX_NE(a, b, size) \
237 ASSERT_BASE_HEX(memcmp(a, b, size) != 0, a, !=, b, size)
238
239 #define ASSERT_NULL(a) \
240 ASSERT_BASE(a, ==, NULL, void*, "p")
241
242 #define ASSERT_NOT_NULL(a) \
243 ASSERT_BASE(a, !=, NULL, void*, "p")
244
245 #define ASSERT_PTR_EQ(a, b) \
246 ASSERT_BASE(a, ==, b, void*, "p")
247
248 #define ASSERT_PTR_NE(a, b) \
249 ASSERT_BASE(a, !=, b, void*, "p")
250
251 /* This macro cleans up the main loop. This is used to avoid valgrind
252 * warnings about memory being "leaked" by the main event loop.
253 */
254 #define MAKE_VALGRIND_HAPPY() \
255 do { \
256 close_loop(uv_default_loop()); \
257 ASSERT(0 == uv_loop_close(uv_default_loop())); \
258 uv_library_shutdown(); \
259 } while (0)
260
261 /* Just sugar for wrapping the main() for a task or helper. */
262 #define TEST_IMPL(name) \
263 int run_test_##name(void); \
264 int run_test_##name(void)
265
266 #define BENCHMARK_IMPL(name) \
267 int run_benchmark_##name(void); \
268 int run_benchmark_##name(void)
269
270 #define HELPER_IMPL(name) \
271 int run_helper_##name(void); \
272 int run_helper_##name(void)
273
274 /* Format big numbers nicely. WARNING: leaks memory. */
275 const char* fmt(double d);
276
277 /* Reserved test exit codes. */
278 enum test_status {
279 TEST_OK = 0,
280 TEST_SKIP = 7
281 };
282
283 #define RETURN_OK() \
284 do { \
285 return TEST_OK; \
286 } while (0)
287
288 #define RETURN_SKIP(explanation) \
289 do { \
290 fprintf(stderr, "%s\n", explanation); \
291 fflush(stderr); \
292 return TEST_SKIP; \
293 } while (0)
294
295 #if !defined(_WIN32)
296
297 # define TEST_FILE_LIMIT(num) \
298 do { \
299 struct rlimit lim; \
300 lim.rlim_cur = (num); \
301 lim.rlim_max = lim.rlim_cur; \
302 if (setrlimit(RLIMIT_NOFILE, &lim)) \
303 RETURN_SKIP("File descriptor limit too low."); \
304 } while (0)
305
306 #else /* defined(_WIN32) */
307
308 # define TEST_FILE_LIMIT(num) do {} while (0)
309
310 #endif
311
312 #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
313 extern int snprintf(char*, size_t, const char*, ...);
314 #endif
315
316 #if defined(__clang__) || \
317 defined(__GNUC__) || \
318 defined(__INTEL_COMPILER)
319 # define UNUSED __attribute__((unused))
320 #else
321 # define UNUSED
322 #endif
323
324 #if defined(_WIN32)
325 #define notify_parent_process() ((void) 0)
326 #else
327 extern void notify_parent_process(void);
328 #endif
329
330 /* Fully close a loop */
close_walk_cb(uv_handle_t * handle,void * arg)331 static void close_walk_cb(uv_handle_t* handle, void* arg) {
332 if (!uv_is_closing(handle))
333 uv_close(handle, NULL);
334 }
335
close_loop(uv_loop_t * loop)336 UNUSED static void close_loop(uv_loop_t* loop) {
337 uv_walk(loop, close_walk_cb, NULL);
338 uv_run(loop, UV_RUN_DEFAULT);
339 }
340
can_ipv6(void)341 UNUSED static int can_ipv6(void) {
342 uv_interface_address_t* addr;
343 int supported;
344 int count;
345 int i;
346
347 if (uv_interface_addresses(&addr, &count))
348 return 0; /* Assume no IPv6 support on failure. */
349
350 supported = 0;
351 for (i = 0; supported == 0 && i < count; i += 1)
352 supported = (AF_INET6 == addr[i].address.address6.sin6_family);
353
354 uv_free_interface_addresses(addr, count);
355 return supported;
356 }
357
358 #if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__)
359 # define NO_FS_EVENTS "Filesystem watching not supported on this platform."
360 #endif
361
362 #if defined(__MSYS__)
363 # define NO_SEND_HANDLE_ON_PIPE \
364 "MSYS2 runtime does not support sending handles on pipes."
365 #elif defined(__CYGWIN__)
366 # define NO_SEND_HANDLE_ON_PIPE \
367 "Cygwin runtime does not support sending handles on pipes."
368 #endif
369
370 #if defined(__MSYS__)
371 # define NO_SELF_CONNECT \
372 "MSYS2 runtime hangs on listen+connect in same process."
373 #elif defined(__CYGWIN__)
374 # define NO_SELF_CONNECT \
375 "Cygwin runtime hangs on listen+connect in same process."
376 #endif
377
378 #endif /* TASK_H_ */
379