]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/helper.c
cast, str, take, tal/grabfile, tal/str, typesafe_cb: use argc
[ccan] / ccan / rfc822 / test / helper.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include <ccan/failtest/failtest_override.h>
5 #include <ccan/failtest/failtest.h>
6
7 #include <ccan/rfc822/rfc822.h>
8
9 #include "helper.h"
10
11 /* failtest limitations mean we need these wrappers to test talloc
12  * failure paths. */
13 #ifndef TAL_USE_TALLOC
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 #endif
29
30 #if 0
31 static void allocation_failure_exit(const char *s)
32 {
33         fprintf(stderr, "Allocation failure: %s", s);
34         exit(0);
35 }
36 #endif
37
38 static bool allocation_failed = false;
39
40 static void allocation_failure_continue(const char *s)
41 {
42         fprintf(stderr, "Allocation failure: %s", s);
43         allocation_failed = true;
44 }
45
46 void allocation_failure_check(void)
47 {
48         if (allocation_failed) {
49                 fprintf(stderr, "Exiting due to earlier failed allocation\n");
50                 exit(0);
51         }
52 }
53
54 #ifdef TAL_USE_TALLOC
55 #include <ccan/tal/talloc/talloc.h>
56 #else
57 #include <ccan/tal/tal.h>
58 #endif
59
60 /* Don't abort on allocation failures! */
61 static void noabort_wrapper(const char *why)
62 {
63         return;
64 }
65
66 void failtest_setup(int argc, char *argv[])
67 {
68         failtest_init(argc, argv);
69         rfc822_set_allocation_failure_handler(allocation_failure_continue);
70 #ifdef TAL_USE_TALLOC
71         /* FIXME: we can't inject allocation failures in talloc! */
72         tal_set_backend(NULL, NULL, NULL, noabort_wrapper);
73 #else
74         tal_set_backend(malloc_wrapper, realloc_wrapper, free_wrapper,
75                         noabort_wrapper);
76 #endif
77 }
78
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)
83 {
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;
90
91         errs = rfc822_header_errors(msg, h);
92         ok(errs == experr, "Header errors 0x%x != 0x%x", errs, experr);
93         allocation_failure_check();
94
95         hname = rfc822_header_raw_name(msg, h);
96         allocation_failure_check();
97
98         ok(hname.ptr && bytestring_eq(hname, bytestring_from_string(name)),
99            "Header name \"%.*s\"", (int)hname.len, hname.ptr);
100
101         hvalue = rfc822_header_raw_value(msg, h);
102         allocation_failure_check();
103
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'),
108            "Header value");
109
110         hfull = rfc822_header_raw_content(msg, h);
111         allocation_failure_check();
112
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'),
119            "Full header");
120 }