]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/helper.c
ccanlint: fix coverage display for gcov 4.7
[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 }
59
60 void check_header(struct rfc822_msg *msg, struct rfc822_header *h,
61                   const char *name, const char *val,
62                   int crlf)
63 {
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;
69
70         ok(rfc822_header_errors(msg, h) == 0, "Header valid");
71         allocation_failure_check();
72
73         hname = rfc822_header_raw_name(msg, h);
74         allocation_failure_check();
75
76         ok(hname.ptr && bytestring_eq(hname, bytestring_from_string(name)),
77            "Header name \"%.*s\"", (int)hname.len, hname.ptr);
78
79         hvalue = rfc822_header_raw_value(msg, h);
80         allocation_failure_check();
81
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'),
86            "Header value");
87
88         hfull = rfc822_header_raw_content(msg, h);
89         allocation_failure_check();
90
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'),
97            "Full header");
98 }