| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- /*
- * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
- *
- * @APPLE_APACHE_LICENSE_HEADER_START@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @APPLE_APACHE_LICENSE_HEADER_END@
- */
- #if defined(__linux__) || defined(__FreeBSD__)
- // for asprintf
- #define _GNU_SOURCE 1
- #endif
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
- #include <unistd.h>
- #endif
- #include <errno.h>
- #include <string.h>
- #ifdef __APPLE__
- #include <crt_externs.h>
- #include <mach/mach_error.h>
- #include <spawn.h>
- #include <sys/wait.h>
- #endif
- #if defined(_WIN32)
- #include <generic_win_port.h>
- #endif
- #include <inttypes.h>
- #include "bsdtests.h"
- static int _test_exit_code;
- #define _test_print(_file, _line, _desc, \
- _expr, _fmt1, _val1, _fmt2, _val2) do { \
- const char* _exprstr; \
- char _linestr[BUFSIZ]; \
- _linestr[0] = 0; \
- if (!(_expr)) { \
- _exprstr = "FAIL"; \
- _test_exit_code = 0xff; \
- if (_file && _file[0] != '\0') { \
- snprintf(_linestr, sizeof(_linestr), \
- " (%s:%ld)", _file, _line); \
- } \
- } else { \
- _exprstr = "PASS"; \
- } \
- if (_fmt2 == 0) { \
- fprintf(stdout, "\n" \
- "[BEGIN] %s\n" \
- "\tValue: " _fmt1 "\n" \
- "[%s] %s%s\n", \
- _desc, \
- _val1, \
- _exprstr, \
- _desc, \
- _linestr); \
- } else { \
- fprintf(stdout, "\n" \
- "[BEGIN] %s\n" \
- "\tActual: " _fmt1 "\n" \
- "\tExpected: " _fmt2 "\n" \
- "[%s] %s%s\n", \
- _desc, \
- _val1, \
- _val2, \
- _exprstr, \
- _desc, \
- _linestr); \
- } \
- if (!_expr && _file && _file[0] != '\0') { \
- fprintf(stdout, "\t%s:%ld\n", _file, _line); \
- } \
- fflush(stdout); \
- } while (0);
- #define GENERATE_DESC \
- char desc[BUFSIZ]; \
- va_list args; \
- \
- va_start(args, format); \
- vsnprintf(desc, sizeof(desc), format, args); \
- va_end(args);
- void
- _test_ptr_null(const char* file, long line, const char* desc, const void* ptr)
- {
- _test_print(file, line, desc,
- (ptr == NULL), "%p", ptr, "%p", (void*)0);
- }
- void
- test_ptr_null_format(void *ptr, const char *format, ...)
- {
- GENERATE_DESC
- _test_ptr_null(NULL, 0, desc, ptr);
- }
- void
- _test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr)
- {
- _test_print(file, line, desc,
- (ptr != NULL), "%p", ptr, "%p", ptr ?: (void*)~0);
- }
- void
- test_ptr_notnull_format(const void *ptr, const char *format, ...)
- {
- GENERATE_DESC
- _test_ptr_notnull(NULL, 0, desc, ptr);
- }
- void
- _test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected)
- {
- _test_print(file, line, desc,
- (actual == expected), "%p", actual, "%p", expected);
- }
- void
- test_ptr_format(const void* actual, const void* expected, const char* format, ...)
- {
- GENERATE_DESC
- _test_ptr(NULL, 0, desc, actual, expected);
- }
- void _test_ptr_not(const char* file, long line, const char* desc, const void* actual, const void* expected)
- {
- _test_print(file, line, desc,
- (actual != expected), "%p", actual, "!%p", expected);
- }
- void test_ptr_not_format(const void *actual, const void* expected, const char *format, ...)
- {
- GENERATE_DESC
- _test_ptr_not(NULL, 0, desc, actual, expected);
- }
- void
- _test_uint32(const char* file, long line, const char* desc, uint32_t actual, uint32_t expected)
- {
- _test_print(file, line, desc,
- (actual == expected), "%u", actual, "%u", expected);
- }
- void
- test_uint32_format(uint32_t actual, uint32_t expected, const char *format, ...)
- {
- GENERATE_DESC
- _test_uint32(NULL, 0, desc, actual, expected);
- }
- void
- _test_int32(const char* file, long line, const char* desc, int32_t actual, int32_t expected)
- {
- _test_print(file, line, desc,
- (actual == expected), "%d", actual, "%d", expected);
- }
- void
- test_int32_format(int32_t actual, int32_t expected, const char* format, ...)
- {
- GENERATE_DESC
- _test_int32(NULL, 0, desc, actual, expected);
- }
- void
- _test_long(const char* file, long line, const char* desc, long actual, long expected)
- {
- _test_print(file, line, desc,
- (actual == expected), "%ld", actual, "%ld", expected);
- }
- void
- test_long_format(long actual, long expected, const char* format, ...)
- {
- GENERATE_DESC
- _test_long(NULL, 0, desc, actual, expected);
- }
- void
- _test_sizet(const char* file, long line, const char* desc, size_t actual, size_t expected)
- {
- _test_print(file, line, desc,
- (actual == expected), "%zd", actual, "%zd", expected);
- }
- void
- test_sizet_format(size_t actual, size_t expected, const char* format, ...)
- {
- GENERATE_DESC
- _test_sizet(NULL, 0, desc, actual, expected);
- }
- void
- _test_uint64(const char* file, long line, const char* desc, uint64_t actual, uint64_t expected)
- {
- _test_print(file, line, desc,
- (actual == expected), "%" PRIu64, actual, "%" PRIu64, expected);
- }
- void
- test_uint64_format(uint64_t actual, uint64_t expected, const char* format, ...)
- {
- GENERATE_DESC
- _test_uint64(NULL, 0, desc, actual, expected);
- }
- void
- _test_int64(const char* file, long line, const char* desc, int64_t actual, int64_t expected)
- {
- _test_print(file, line, desc,
- (actual == expected), "%" PRId64, actual, "%" PRId64, expected);
- }
- void
- test_int64_format(int64_t actual, int64_t expected, const char* format, ...)
- {
- GENERATE_DESC
- _test_int64(NULL, 0, desc, actual, expected);
- }
- void
- _test_long_less_than(const char* file, long line, const char* desc, long actual, long expected_max)
- {
- _test_print(file, line, desc, (actual < expected_max), "%ld", actual, "<%ld", expected_max);
- }
- void
- test_long_less_than_format(long actual, long expected_max, const char* format, ...)
- {
- GENERATE_DESC
- _test_long_less_than(NULL, 0, desc, actual, expected_max);
- }
- void
- _test_long_less_than_or_equal(const char* file, long line, const char* desc, long actual, long expected_max)
- {
- _test_print(file, line, desc, (actual <= expected_max), "%ld", actual, "<=%ld", expected_max);
- }
- void
- test_long_less_than_or_equal_format(long actual, long expected_max, const char* format, ...)
- {
- GENERATE_DESC
- _test_long_less_than_or_equal(NULL, 0, desc, actual, expected_max);
- }
- void
- _test_long_greater_than_or_equal(const char* file, long line, const char* desc, long actual, long expected_min)
- {
- _test_print(file, line, desc, (actual >= expected_min), "%ld", actual, ">=%ld", expected_min);
- }
- void
- test_long_greater_than_or_equal_format(long actual, long expected_max, const char* format, ...)
- {
- GENERATE_DESC
- _test_long_greater_than_or_equal(NULL, 0, desc, actual, expected_max);
- }
- void
- _test_sizet_less_than(const char* file, long line, const char* desc, size_t actual, size_t expected_max)
- {
- _test_print(file, line, desc, (actual < expected_max), "%zd", actual, "<%zd", expected_max);
- }
- void
- test_sizet_less_than_format(size_t actual, size_t expected_max, const char* format, ...)
- {
- GENERATE_DESC
- _test_sizet_less_than(NULL, 0, desc, actual, expected_max);
- }
- void
- _test_sizet_less_than_or_equal(const char* file, long line, const char* desc, size_t actual, size_t expected_max)
- {
- _test_print(file, line, desc, (actual <= expected_max), "%zd", actual, "<=%zd", expected_max);
- }
- void
- test_sizet_less_than_or_equal_format(size_t actual, size_t expected_max, const char* format, ...)
- {
- GENERATE_DESC
- _test_sizet_less_than_or_equal(NULL, 0, desc, actual, expected_max);
- }
- void
- _test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected)
- {
- _test_print(file, line, desc, (val < max_expected), "%f", val, "<%f", max_expected);
- }
- void
- test_double_less_than_format(double val, double max_expected, const char* format, ...)
- {
- GENERATE_DESC
- _test_double_less_than(NULL, 0, desc, val, max_expected);
- }
- void
- _test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected)
- {
- _test_print(file, line, desc, (val <= max_expected), "%f", val, "<=%f", max_expected);
- }
- void
- test_double_less_than_or_equal_format(double val, double max_expected, const char *format, ...)
- {
- GENERATE_DESC
- _test_double_less_than_or_equal(NULL, 0, desc, val, max_expected);
- }
- void
- _test_double_equal(const char* file, long line, const char* desc, double val, double expected)
- {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wfloat-equal"
- _test_print(file, line, desc, (val == expected), "%f", val, "%f", expected);
- #pragma clang diagnostic pop
- }
- void
- test_double_equal_format(double val, double expected, const char *format, ...)
- {
- GENERATE_DESC
- _test_double_equal(NULL, 0, desc, val, expected);
- }
- void
- _test_errno(const char* file, long line, const char* desc, int actual, int expected)
- {
- char* actual_str;
- char* expected_str;
- (void)asprintf(&actual_str, "%d\t%s", actual, actual ? strerror(actual) : "");
- (void)asprintf(&expected_str, "%d\t%s", expected, expected ? strerror(expected) : "");
- _test_print(file, line, desc,
- (actual == expected), "%s", actual_str, "%s", expected_str);
- free(actual_str);
- free(expected_str);
- }
- void
- test_errno_format(int actual, int expected, const char *format, ...)
- {
- GENERATE_DESC
- _test_errno(NULL, 0, desc, actual, expected);
- }
- #ifdef __APPLE__
- void
- _test_mach_error(const char* file, long line, const char* desc,
- mach_error_t actual, mach_error_t expected)
- {
- char* actual_str;
- char* expected_str;
- (void)asprintf(&actual_str, "%d %s", actual, actual ? mach_error_string(actual) : "");
- (void)asprintf(&expected_str, "%d %s", expected, expected ? mach_error_string(expected) : "");
- _test_print(file, line, desc,
- (actual == expected), "%s", actual_str, "%s", expected_str);
- free(actual_str);
- free(expected_str);
- }
- void
- test_mach_error_format(mach_error_t actual, mach_error_t expected, const char *format, ...)
- {
- GENERATE_DESC
- _test_mach_error(NULL, 0, desc, actual, expected);
- }
- #endif
- void
- _test_skip(const char* file, long line, const char* desc)
- {
- if (file != NULL && file[0] != '\0') {
- fprintf(stdout, "[SKIP] %s (%s:%ld)\n", desc, file, line);
- } else {
- fprintf(stdout, "[SKIP] %s\n", desc);
- }
- fflush(stdout);
- }
- void
- test_skip_format(const char *format, ...)
- {
- GENERATE_DESC
- fprintf(stdout, "[SKIP] %s\n", desc);
- }
- #if USE_COREFOUNDATION
- void
- test_cferror(const char *desc, CFErrorRef actual, CFIndex expectedCode)
- {
- if (actual != NULL) {
- CFStringRef errDesc = CFErrorCopyDescription(actual);
- CFIndex code = CFErrorGetCode(actual);
- char* actual_str;
- char* expected_str;
- if (code != expectedCode) {
- char buffer[BUFSIZ];
- CFStringGetCString(errDesc, buffer, sizeof(buffer), kCFStringEncodingUTF8);
- (void)asprintf(&actual_str, "%ld\t%s", code, buffer);
- } else {
- (void)asprintf(&actual_str, "%ld", code);
- }
- (void)asprintf(&expected_str, "%ld", expectedCode);
- _test_print("", (long) 0, desc,
- (code == expectedCode), "%s", actual_str, "%s", expected_str);
- free(actual_str);
- free(expected_str);
- CFRelease(errDesc);
- } else {
- _test_print("", (long) 0, desc, (0 == expectedCode), "%ld", (long) 0, "%ld", (long) expectedCode);
- }
- }
- void
- test_cferror_format(CFErrorRef actual, CFIndex expectedCode, const char *format, ...)
- {
- GENERATE_DESC
- test_cferror(desc, actual, expectedCode);
- }
- #endif
- void
- test_start(const char* desc)
- {
- if (desc) {
- fprintf(stdout, "\n==================================================\n");
- fprintf(stdout, "[TEST] %s\n", desc);
- fprintf(stdout, "[PID] %d\n", getpid());
- fprintf(stdout, "==================================================\n\n");
- fflush(stdout);
- }
- _test_exit_code = EXIT_SUCCESS;
- usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
- }
- #if defined(__APPLE__) && defined(__MACH__)
- static char** get_environment(void)
- {
- return (* _NSGetEnviron());
- }
- void
- test_leaks_pid(const char *name, pid_t pid)
- {
- int res;
- char pidstr[10];
- if (getenv("NOLEAKS")) {
- return;
- }
- if (!name) {
- name = "Leaks";
- }
- /* leaks doesn't work against debug variant malloc */
- const char *dyld_image_suffix = getenv("DYLD_IMAGE_SUFFIX");
- if (dyld_image_suffix && strstr(dyld_image_suffix, "_debug")) {
- return;
- }
- char *inserted_libs = getenv("DYLD_INSERT_LIBRARIES");
- if (inserted_libs && strstr(inserted_libs, "/usr/lib/libgmalloc.dylib")) {
- return;
- }
- unsetenv("DYLD_IMAGE_SUFFIX");
- unsetenv("DYLD_INSERT_LIBRARIES");
- unsetenv("DYLD_LIBRARY_PATH");
- unsetenv("MallocStackLogging");
- unsetenv("MallocStackLoggingNoCompact");
- snprintf(pidstr, sizeof(pidstr), "%d", pid);
- char* args[] = { "./leaks-wrapper", pidstr, NULL };
- res = posix_spawnp(&pid, args[0], NULL, NULL, args, get_environment());
- if (res == 0 && pid > 0) {
- int status;
- waitpid(pid, &status, 0);
- test_long(name, status, 0);
- } else {
- perror(args[0]);
- }
- }
- void
- test_leaks(const char *name)
- {
- test_leaks_pid(name, getpid());
- }
- #endif
- void
- test_stop_after_delay(void *delay)
- {
- if (delay != NULL) {
- sleep((unsigned int)(intptr_t)delay);
- }
- #if defined(__APPLE__) && defined(__MACH__)
- test_leaks(NULL);
- #endif
- fflush(stdout);
- _exit(_test_exit_code);
- }
- void
- test_stop(void)
- {
- test_stop_after_delay((void *)(intptr_t)0);
- }
|