bsdtests.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #if defined(__linux__) || defined(__FreeBSD__)
  21. // for asprintf
  22. #define _GNU_SOURCE 1
  23. #endif
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  28. #include <unistd.h>
  29. #endif
  30. #include <errno.h>
  31. #include <string.h>
  32. #ifdef __APPLE__
  33. #include <crt_externs.h>
  34. #include <mach/mach_error.h>
  35. #include <spawn.h>
  36. #include <sys/wait.h>
  37. #endif
  38. #if defined(_WIN32)
  39. #include <generic_win_port.h>
  40. #endif
  41. #include <inttypes.h>
  42. #include "bsdtests.h"
  43. static int _test_exit_code;
  44. #define _test_print(_file, _line, _desc, \
  45. _expr, _fmt1, _val1, _fmt2, _val2) do { \
  46. const char* _exprstr; \
  47. char _linestr[BUFSIZ]; \
  48. _linestr[0] = 0; \
  49. if (!(_expr)) { \
  50. _exprstr = "FAIL"; \
  51. _test_exit_code = 0xff; \
  52. if (_file && _file[0] != '\0') { \
  53. snprintf(_linestr, sizeof(_linestr), \
  54. " (%s:%ld)", _file, _line); \
  55. } \
  56. } else { \
  57. _exprstr = "PASS"; \
  58. } \
  59. if (_fmt2 == 0) { \
  60. fprintf(stdout, "\n" \
  61. "[BEGIN] %s\n" \
  62. "\tValue: " _fmt1 "\n" \
  63. "[%s] %s%s\n", \
  64. _desc, \
  65. _val1, \
  66. _exprstr, \
  67. _desc, \
  68. _linestr); \
  69. } else { \
  70. fprintf(stdout, "\n" \
  71. "[BEGIN] %s\n" \
  72. "\tActual: " _fmt1 "\n" \
  73. "\tExpected: " _fmt2 "\n" \
  74. "[%s] %s%s\n", \
  75. _desc, \
  76. _val1, \
  77. _val2, \
  78. _exprstr, \
  79. _desc, \
  80. _linestr); \
  81. } \
  82. if (!_expr && _file && _file[0] != '\0') { \
  83. fprintf(stdout, "\t%s:%ld\n", _file, _line); \
  84. } \
  85. fflush(stdout); \
  86. } while (0);
  87. #define GENERATE_DESC \
  88. char desc[BUFSIZ]; \
  89. va_list args; \
  90. \
  91. va_start(args, format); \
  92. vsnprintf(desc, sizeof(desc), format, args); \
  93. va_end(args);
  94. void
  95. _test_ptr_null(const char* file, long line, const char* desc, const void* ptr)
  96. {
  97. _test_print(file, line, desc,
  98. (ptr == NULL), "%p", ptr, "%p", (void*)0);
  99. }
  100. void
  101. test_ptr_null_format(void *ptr, const char *format, ...)
  102. {
  103. GENERATE_DESC
  104. _test_ptr_null(NULL, 0, desc, ptr);
  105. }
  106. void
  107. _test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr)
  108. {
  109. _test_print(file, line, desc,
  110. (ptr != NULL), "%p", ptr, "%p", ptr ?: (void*)~0);
  111. }
  112. void
  113. test_ptr_notnull_format(const void *ptr, const char *format, ...)
  114. {
  115. GENERATE_DESC
  116. _test_ptr_notnull(NULL, 0, desc, ptr);
  117. }
  118. void
  119. _test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected)
  120. {
  121. _test_print(file, line, desc,
  122. (actual == expected), "%p", actual, "%p", expected);
  123. }
  124. void
  125. test_ptr_format(const void* actual, const void* expected, const char* format, ...)
  126. {
  127. GENERATE_DESC
  128. _test_ptr(NULL, 0, desc, actual, expected);
  129. }
  130. void _test_ptr_not(const char* file, long line, const char* desc, const void* actual, const void* expected)
  131. {
  132. _test_print(file, line, desc,
  133. (actual != expected), "%p", actual, "!%p", expected);
  134. }
  135. void test_ptr_not_format(const void *actual, const void* expected, const char *format, ...)
  136. {
  137. GENERATE_DESC
  138. _test_ptr_not(NULL, 0, desc, actual, expected);
  139. }
  140. void
  141. _test_uint32(const char* file, long line, const char* desc, uint32_t actual, uint32_t expected)
  142. {
  143. _test_print(file, line, desc,
  144. (actual == expected), "%u", actual, "%u", expected);
  145. }
  146. void
  147. test_uint32_format(uint32_t actual, uint32_t expected, const char *format, ...)
  148. {
  149. GENERATE_DESC
  150. _test_uint32(NULL, 0, desc, actual, expected);
  151. }
  152. void
  153. _test_int32(const char* file, long line, const char* desc, int32_t actual, int32_t expected)
  154. {
  155. _test_print(file, line, desc,
  156. (actual == expected), "%d", actual, "%d", expected);
  157. }
  158. void
  159. test_int32_format(int32_t actual, int32_t expected, const char* format, ...)
  160. {
  161. GENERATE_DESC
  162. _test_int32(NULL, 0, desc, actual, expected);
  163. }
  164. void
  165. _test_long(const char* file, long line, const char* desc, long actual, long expected)
  166. {
  167. _test_print(file, line, desc,
  168. (actual == expected), "%ld", actual, "%ld", expected);
  169. }
  170. void
  171. test_long_format(long actual, long expected, const char* format, ...)
  172. {
  173. GENERATE_DESC
  174. _test_long(NULL, 0, desc, actual, expected);
  175. }
  176. void
  177. _test_sizet(const char* file, long line, const char* desc, size_t actual, size_t expected)
  178. {
  179. _test_print(file, line, desc,
  180. (actual == expected), "%zd", actual, "%zd", expected);
  181. }
  182. void
  183. test_sizet_format(size_t actual, size_t expected, const char* format, ...)
  184. {
  185. GENERATE_DESC
  186. _test_sizet(NULL, 0, desc, actual, expected);
  187. }
  188. void
  189. _test_uint64(const char* file, long line, const char* desc, uint64_t actual, uint64_t expected)
  190. {
  191. _test_print(file, line, desc,
  192. (actual == expected), "%" PRIu64, actual, "%" PRIu64, expected);
  193. }
  194. void
  195. test_uint64_format(uint64_t actual, uint64_t expected, const char* format, ...)
  196. {
  197. GENERATE_DESC
  198. _test_uint64(NULL, 0, desc, actual, expected);
  199. }
  200. void
  201. _test_int64(const char* file, long line, const char* desc, int64_t actual, int64_t expected)
  202. {
  203. _test_print(file, line, desc,
  204. (actual == expected), "%" PRId64, actual, "%" PRId64, expected);
  205. }
  206. void
  207. test_int64_format(int64_t actual, int64_t expected, const char* format, ...)
  208. {
  209. GENERATE_DESC
  210. _test_int64(NULL, 0, desc, actual, expected);
  211. }
  212. void
  213. _test_long_less_than(const char* file, long line, const char* desc, long actual, long expected_max)
  214. {
  215. _test_print(file, line, desc, (actual < expected_max), "%ld", actual, "<%ld", expected_max);
  216. }
  217. void
  218. test_long_less_than_format(long actual, long expected_max, const char* format, ...)
  219. {
  220. GENERATE_DESC
  221. _test_long_less_than(NULL, 0, desc, actual, expected_max);
  222. }
  223. void
  224. _test_long_less_than_or_equal(const char* file, long line, const char* desc, long actual, long expected_max)
  225. {
  226. _test_print(file, line, desc, (actual <= expected_max), "%ld", actual, "<=%ld", expected_max);
  227. }
  228. void
  229. test_long_less_than_or_equal_format(long actual, long expected_max, const char* format, ...)
  230. {
  231. GENERATE_DESC
  232. _test_long_less_than_or_equal(NULL, 0, desc, actual, expected_max);
  233. }
  234. void
  235. _test_long_greater_than_or_equal(const char* file, long line, const char* desc, long actual, long expected_min)
  236. {
  237. _test_print(file, line, desc, (actual >= expected_min), "%ld", actual, ">=%ld", expected_min);
  238. }
  239. void
  240. test_long_greater_than_or_equal_format(long actual, long expected_max, const char* format, ...)
  241. {
  242. GENERATE_DESC
  243. _test_long_greater_than_or_equal(NULL, 0, desc, actual, expected_max);
  244. }
  245. void
  246. _test_sizet_less_than(const char* file, long line, const char* desc, size_t actual, size_t expected_max)
  247. {
  248. _test_print(file, line, desc, (actual < expected_max), "%zd", actual, "<%zd", expected_max);
  249. }
  250. void
  251. test_sizet_less_than_format(size_t actual, size_t expected_max, const char* format, ...)
  252. {
  253. GENERATE_DESC
  254. _test_sizet_less_than(NULL, 0, desc, actual, expected_max);
  255. }
  256. void
  257. _test_sizet_less_than_or_equal(const char* file, long line, const char* desc, size_t actual, size_t expected_max)
  258. {
  259. _test_print(file, line, desc, (actual <= expected_max), "%zd", actual, "<=%zd", expected_max);
  260. }
  261. void
  262. test_sizet_less_than_or_equal_format(size_t actual, size_t expected_max, const char* format, ...)
  263. {
  264. GENERATE_DESC
  265. _test_sizet_less_than_or_equal(NULL, 0, desc, actual, expected_max);
  266. }
  267. void
  268. _test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected)
  269. {
  270. _test_print(file, line, desc, (val < max_expected), "%f", val, "<%f", max_expected);
  271. }
  272. void
  273. test_double_less_than_format(double val, double max_expected, const char* format, ...)
  274. {
  275. GENERATE_DESC
  276. _test_double_less_than(NULL, 0, desc, val, max_expected);
  277. }
  278. void
  279. _test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected)
  280. {
  281. _test_print(file, line, desc, (val <= max_expected), "%f", val, "<=%f", max_expected);
  282. }
  283. void
  284. test_double_less_than_or_equal_format(double val, double max_expected, const char *format, ...)
  285. {
  286. GENERATE_DESC
  287. _test_double_less_than_or_equal(NULL, 0, desc, val, max_expected);
  288. }
  289. void
  290. _test_double_equal(const char* file, long line, const char* desc, double val, double expected)
  291. {
  292. #pragma clang diagnostic push
  293. #pragma clang diagnostic ignored "-Wfloat-equal"
  294. _test_print(file, line, desc, (val == expected), "%f", val, "%f", expected);
  295. #pragma clang diagnostic pop
  296. }
  297. void
  298. test_double_equal_format(double val, double expected, const char *format, ...)
  299. {
  300. GENERATE_DESC
  301. _test_double_equal(NULL, 0, desc, val, expected);
  302. }
  303. void
  304. _test_errno(const char* file, long line, const char* desc, int actual, int expected)
  305. {
  306. char* actual_str;
  307. char* expected_str;
  308. (void)asprintf(&actual_str, "%d\t%s", actual, actual ? strerror(actual) : "");
  309. (void)asprintf(&expected_str, "%d\t%s", expected, expected ? strerror(expected) : "");
  310. _test_print(file, line, desc,
  311. (actual == expected), "%s", actual_str, "%s", expected_str);
  312. free(actual_str);
  313. free(expected_str);
  314. }
  315. void
  316. test_errno_format(int actual, int expected, const char *format, ...)
  317. {
  318. GENERATE_DESC
  319. _test_errno(NULL, 0, desc, actual, expected);
  320. }
  321. #ifdef __APPLE__
  322. void
  323. _test_mach_error(const char* file, long line, const char* desc,
  324. mach_error_t actual, mach_error_t expected)
  325. {
  326. char* actual_str;
  327. char* expected_str;
  328. (void)asprintf(&actual_str, "%d %s", actual, actual ? mach_error_string(actual) : "");
  329. (void)asprintf(&expected_str, "%d %s", expected, expected ? mach_error_string(expected) : "");
  330. _test_print(file, line, desc,
  331. (actual == expected), "%s", actual_str, "%s", expected_str);
  332. free(actual_str);
  333. free(expected_str);
  334. }
  335. void
  336. test_mach_error_format(mach_error_t actual, mach_error_t expected, const char *format, ...)
  337. {
  338. GENERATE_DESC
  339. _test_mach_error(NULL, 0, desc, actual, expected);
  340. }
  341. #endif
  342. void
  343. _test_skip(const char* file, long line, const char* desc)
  344. {
  345. if (file != NULL && file[0] != '\0') {
  346. fprintf(stdout, "[SKIP] %s (%s:%ld)\n", desc, file, line);
  347. } else {
  348. fprintf(stdout, "[SKIP] %s\n", desc);
  349. }
  350. fflush(stdout);
  351. }
  352. void
  353. test_skip_format(const char *format, ...)
  354. {
  355. GENERATE_DESC
  356. fprintf(stdout, "[SKIP] %s\n", desc);
  357. }
  358. #if USE_COREFOUNDATION
  359. void
  360. test_cferror(const char *desc, CFErrorRef actual, CFIndex expectedCode)
  361. {
  362. if (actual != NULL) {
  363. CFStringRef errDesc = CFErrorCopyDescription(actual);
  364. CFIndex code = CFErrorGetCode(actual);
  365. char* actual_str;
  366. char* expected_str;
  367. if (code != expectedCode) {
  368. char buffer[BUFSIZ];
  369. CFStringGetCString(errDesc, buffer, sizeof(buffer), kCFStringEncodingUTF8);
  370. (void)asprintf(&actual_str, "%ld\t%s", code, buffer);
  371. } else {
  372. (void)asprintf(&actual_str, "%ld", code);
  373. }
  374. (void)asprintf(&expected_str, "%ld", expectedCode);
  375. _test_print("", (long) 0, desc,
  376. (code == expectedCode), "%s", actual_str, "%s", expected_str);
  377. free(actual_str);
  378. free(expected_str);
  379. CFRelease(errDesc);
  380. } else {
  381. _test_print("", (long) 0, desc, (0 == expectedCode), "%ld", (long) 0, "%ld", (long) expectedCode);
  382. }
  383. }
  384. void
  385. test_cferror_format(CFErrorRef actual, CFIndex expectedCode, const char *format, ...)
  386. {
  387. GENERATE_DESC
  388. test_cferror(desc, actual, expectedCode);
  389. }
  390. #endif
  391. void
  392. test_start(const char* desc)
  393. {
  394. if (desc) {
  395. fprintf(stdout, "\n==================================================\n");
  396. fprintf(stdout, "[TEST] %s\n", desc);
  397. fprintf(stdout, "[PID] %d\n", getpid());
  398. fprintf(stdout, "==================================================\n\n");
  399. fflush(stdout);
  400. }
  401. _test_exit_code = EXIT_SUCCESS;
  402. usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
  403. }
  404. #if defined(__APPLE__) && defined(__MACH__)
  405. static char** get_environment(void)
  406. {
  407. return (* _NSGetEnviron());
  408. }
  409. void
  410. test_leaks_pid(const char *name, pid_t pid)
  411. {
  412. int res;
  413. char pidstr[10];
  414. if (getenv("NOLEAKS")) {
  415. return;
  416. }
  417. if (!name) {
  418. name = "Leaks";
  419. }
  420. /* leaks doesn't work against debug variant malloc */
  421. const char *dyld_image_suffix = getenv("DYLD_IMAGE_SUFFIX");
  422. if (dyld_image_suffix && strstr(dyld_image_suffix, "_debug")) {
  423. return;
  424. }
  425. char *inserted_libs = getenv("DYLD_INSERT_LIBRARIES");
  426. if (inserted_libs && strstr(inserted_libs, "/usr/lib/libgmalloc.dylib")) {
  427. return;
  428. }
  429. unsetenv("DYLD_IMAGE_SUFFIX");
  430. unsetenv("DYLD_INSERT_LIBRARIES");
  431. unsetenv("DYLD_LIBRARY_PATH");
  432. unsetenv("MallocStackLogging");
  433. unsetenv("MallocStackLoggingNoCompact");
  434. snprintf(pidstr, sizeof(pidstr), "%d", pid);
  435. char* args[] = { "./leaks-wrapper", pidstr, NULL };
  436. res = posix_spawnp(&pid, args[0], NULL, NULL, args, get_environment());
  437. if (res == 0 && pid > 0) {
  438. int status;
  439. waitpid(pid, &status, 0);
  440. test_long(name, status, 0);
  441. } else {
  442. perror(args[0]);
  443. }
  444. }
  445. void
  446. test_leaks(const char *name)
  447. {
  448. test_leaks_pid(name, getpid());
  449. }
  450. #endif
  451. void
  452. test_stop_after_delay(void *delay)
  453. {
  454. if (delay != NULL) {
  455. sleep((unsigned int)(intptr_t)delay);
  456. }
  457. #if defined(__APPLE__) && defined(__MACH__)
  458. test_leaks(NULL);
  459. #endif
  460. fflush(stdout);
  461. _exit(_test_exit_code);
  462. }
  463. void
  464. test_stop(void)
  465. {
  466. test_stop_after_delay((void *)(intptr_t)0);
  467. }