]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/helper.c
36a2a065f2607c283be3cdd1c1169494b0121a22
[ccan] / ccan / rfc822 / test / helper.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include <ccan/talloc/talloc.h>
5 #include <ccan/failtest/failtest_override.h>
6 #include <ccan/failtest/failtest.h>
7
8 #include <ccan/rfc822/rfc822.h>
9
10 #include "helper.h"
11
12 /* failtest limitations mean we need these wrappers to test talloc
13  * failure paths. */
14 static void *malloc_wrapper(size_t size)
15 {
16         return malloc(size);
17 }
18
19 static void free_wrapper(void *ptr)
20 {
21         free(ptr);
22 }
23
24 static void *realloc_wrapper(void *ptr, size_t size)
25 {
26         return realloc(ptr, size);
27 }
28
29 #if 0
30 static void allocation_failure_exit(const char *s)
31 {
32         fprintf(stderr, "Allocation failure: %s", s);
33         exit(0);
34 }
35 #endif
36
37 static bool allocation_failed = false;
38
39 static void allocation_failure_continue(const char *s)
40 {
41         fprintf(stderr, "Allocation failure: %s", s);
42         allocation_failed = true;
43 }
44
45 void allocation_failure_check(void)
46 {
47         if (allocation_failed) {
48                 fprintf(stderr, "Exiting due to earlier failed allocation\n");
49                 exit(0);
50         }
51 }
52
53 void failtest_setup(int argc, char *argv[])
54 {
55         failtest_init(argc, argv);
56         rfc822_set_allocation_failure_handler(allocation_failure_continue);
57         talloc_set_allocator(malloc_wrapper, free_wrapper, realloc_wrapper);
58 }