4 #include <ccan/failtest/failtest_override.h>
5 #include <ccan/failtest/failtest.h>
7 #include <ccan/rfc822/rfc822.h>
11 /* failtest limitations mean we need these wrappers to test talloc
13 #ifndef TAL_USE_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);
31 static void allocation_failure_exit(const char *s)
33 fprintf(stderr, "Allocation failure: %s", s);
38 static bool allocation_failed = false;
40 static void allocation_failure_continue(const char *s)
42 fprintf(stderr, "Allocation failure: %s", s);
43 allocation_failed = true;
46 void allocation_failure_check(void)
48 if (allocation_failed) {
49 fprintf(stderr, "Exiting due to earlier failed allocation\n");
55 #include <ccan/tal/talloc/talloc.h>
57 #include <ccan/tal/tal.h>
60 /* Don't abort on allocation failures! */
61 static void noabort_wrapper(const char *why)
66 void failtest_setup(int argc, char *argv[])
68 failtest_init(argc, argv);
69 rfc822_set_allocation_failure_handler(allocation_failure_continue);
71 /* FIXME: we can't inject allocation failures in talloc! */
72 tal_set_backend(NULL, NULL, NULL, noabort_wrapper);
74 tal_set_backend(malloc_wrapper, realloc_wrapper, free_wrapper,
79 void check_header(struct rfc822_msg *msg,
80 struct rfc822_header *h,
81 const char *name, const char *val,
82 enum rfc822_header_errors experr, int crlf)
84 enum rfc822_header_errors errs;
85 struct bytestring hname, hvalue, hfull;
86 size_t namelen = strlen(name);
87 size_t valuelen = strlen(val);
88 size_t nln = crlf ? 2 : 1;
89 size_t fulllen = namelen + valuelen + 1 + nln;
91 errs = rfc822_header_errors(msg, h);
92 ok(errs == experr, "Header errors 0x%x != 0x%x", errs, experr);
93 allocation_failure_check();
95 hname = rfc822_header_raw_name(msg, h);
96 allocation_failure_check();
98 ok(hname.ptr && bytestring_eq(hname, bytestring_from_string(name)),
99 "Header name \"%.*s\"", (int)hname.len, hname.ptr);
101 hvalue = rfc822_header_raw_value(msg, h);
102 allocation_failure_check();
104 ok(hvalue.ptr && ((valuelen + nln) == hvalue.len)
105 && (memcmp(val, hvalue.ptr, valuelen) == 0)
106 && (!crlf || (hvalue.ptr[hvalue.len - 2] == '\r'))
107 && (hvalue.ptr[hvalue.len - 1] == '\n'),
110 hfull = rfc822_header_raw_content(msg, h);
111 allocation_failure_check();
113 ok(hfull.ptr && (fulllen == hfull.len)
114 && (memcmp(name, hfull.ptr, namelen) == 0)
115 && (hfull.ptr[namelen] == ':')
116 && (memcmp(val, hfull.ptr + namelen + 1, valuelen) == 0)
117 && (!crlf || (hfull.ptr[fulllen-2] == '\r'))
118 && (hfull.ptr[fulllen-1] == '\n'),