4 #include <ccan/talloc/talloc.h>
5 #include <ccan/failtest/failtest_override.h>
6 #include <ccan/failtest/failtest.h>
8 #include <ccan/rfc822/rfc822.h>
12 /* failtest limitations mean we need these wrappers to test talloc
14 static void *malloc_wrapper(size_t size)
19 static void free_wrapper(void *ptr)
24 static void *realloc_wrapper(void *ptr, size_t size)
26 return realloc(ptr, size);
30 static void allocation_failure_exit(const char *s)
32 fprintf(stderr, "Allocation failure: %s", s);
37 static bool allocation_failed = false;
39 static void allocation_failure_continue(const char *s)
41 fprintf(stderr, "Allocation failure: %s", s);
42 allocation_failed = true;
45 void allocation_failure_check(void)
47 if (allocation_failed) {
48 fprintf(stderr, "Exiting due to earlier failed allocation\n");
53 void failtest_setup(int argc, char *argv[])
55 failtest_init(argc, argv);
56 rfc822_set_allocation_failure_handler(allocation_failure_continue);
57 talloc_set_allocator(malloc_wrapper, free_wrapper, realloc_wrapper);
60 void check_header(struct rfc822_msg *msg, struct rfc822_header *h,
61 const char *name, const char *val,
64 struct bytestring hname, hvalue, hfull;
65 size_t namelen = strlen(name);
66 size_t valuelen = strlen(val);
67 size_t nln = crlf ? 2 : 1;
68 size_t fulllen = namelen + valuelen + 1 + nln;
70 ok(rfc822_header_errors(msg, h) == 0, "Header valid");
71 allocation_failure_check();
73 hname = rfc822_header_raw_name(msg, h);
74 allocation_failure_check();
76 ok(hname.ptr && bytestring_eq(hname, bytestring_from_string(name)),
77 "Header name \"%.*s\"", (int)hname.len, hname.ptr);
79 hvalue = rfc822_header_raw_value(msg, h);
80 allocation_failure_check();
82 ok(hvalue.ptr && ((valuelen + nln) == hvalue.len)
83 && (memcmp(val, hvalue.ptr, valuelen) == 0)
84 && (!crlf || (hvalue.ptr[hvalue.len - 2] == '\r'))
85 && (hvalue.ptr[hvalue.len - 1] == '\n'),
88 hfull = rfc822_header_raw_content(msg, h);
89 allocation_failure_check();
91 ok(hfull.ptr && (fulllen == hfull.len)
92 && (memcmp(name, hfull.ptr, namelen) == 0)
93 && (hfull.ptr[namelen] == ':')
94 && (memcmp(val, hfull.ptr + namelen + 1, valuelen) == 0)
95 && (!crlf || (hfull.ptr[fulllen-2] == '\r'))
96 && (hfull.ptr[fulllen-1] == '\n'),