| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #include "config.h"
- #include <stdlib.h>
- #include <setjmp.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <assert.h>
- #include <ccan/tap/tap.h>
- /* We don't actually want it to exit... */
- static jmp_buf exited;
- #define exit(status) longjmp(exited, (status) + 1)
- #define printf saved_printf
- static int saved_printf(const char *fmt, ...);
- #define fprintf saved_fprintf
- static int saved_fprintf(FILE *ignored, const char *fmt, ...);
- #define vfprintf saved_vfprintf
- static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap);
- /* Hack to avoid a memory leak which valgrind complains about. */
- #define realloc set_realloc
- static void *set_realloc(void *ptr, size_t size);
- #define free set_free
- static void set_free(void *ptr);
- /* Include the C files directly. */
- #include <ccan/failtest/failtest.c>
- #undef realloc
- #undef free
- static char *buffer;
- static void *set_realloc(void *ptr, size_t size)
- {
- return buffer = realloc(ptr, size);
- }
- static void set_free(void *ptr)
- {
- if (ptr == buffer)
- buffer = NULL;
- free(ptr);
- }
- static char *output = NULL;
- static int saved_vprintf(const char *fmt, va_list ap)
- {
- int ret;
- int len = 0;
- va_list ap2;
- va_copy(ap2, ap);
- ret = vsnprintf(NULL, 0, fmt, ap2);
- va_end(ap2);
- if (output)
- len = strlen(output);
- output = realloc(output, len + ret + 1);
- return vsprintf(output + len, fmt, ap);
- }
- static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap)
- {
- return saved_vprintf(fmt, ap);
- }
- static int saved_printf(const char *fmt, ...)
- {
- va_list ap;
- int ret;
- va_start(ap, fmt);
- ret = saved_vprintf(fmt, ap);
- va_end(ap);
- return ret;
- }
- static int saved_fprintf(FILE *ignored, const char *fmt, ...)
- {
- va_list ap;
- int ret;
- va_start(ap, fmt);
- ret = saved_vprintf(fmt, ap);
- va_end(ap);
- return ret;
- }
- int main(void)
- {
- int status;
- plan_tests(3);
- failtest_init(0, NULL);
- status = setjmp(exited);
- if (status == 0) {
- char *p = failtest_malloc(1, "run-malloc.c", 1);
- /* If we just segv, valgrind counts that as a failure.
- * So kill ourselves creatively. */
- if (!p)
- kill(getpid(), SIGSEGV);
- fail("Expected child to crash!");
- } else {
- ok1(status == 2);
- ok1(strstr(output, "Killed by signal"));
- ok1(strstr(output, "--failpath=M\n"));
- }
- free(buffer);
- return exit_status();
- }
|