956a6bd6e17392e264029556558fd9d5e6166dbf
[ccan] / ccan / rfc822 / test / run-hdr-and-body.c
1 #include <ccan/foreach/foreach.h>
2 #include <ccan/failtest/failtest_override.h>
3 #include <ccan/failtest/failtest.h>
4 #include <ccan/tap/tap.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #define CCAN_RFC822_DEBUG
9
10 #include <ccan/rfc822/rfc822.h>
11
12 #include <ccan/rfc822/rfc822.c>
13
14 #include "testdata.h"
15 #include "helper.h"
16
17 #define CHECK_HEADERS(_e, _msg, _h, _n, _crlf)  \
18         do { \
19                 int _i; \
20                 for (_i = 0; _i < (_e)->nhdrs; _i++) {  \
21                         (_h) = rfc822_next_header((_msg), (_h)); \
22                         ok((_h), "header %d exists %s", _i, (_n)); \
23                         if (!(_h)) \
24                                 break; \
25                         check_header((_msg), (_h), (_e)->hdrs[_i].name, \
26                                      (_e)->hdrs[_i].val, crlf); \
27                 } \
28         } while (0)
29
30 static void test_bodyhdr(const struct aexample *e, const char *buf, size_t len,
31                          const char *exname, int crlf)
32 {
33         struct rfc822_msg *msg;
34         struct rfc822_header *h = NULL;
35         struct bytestring body;
36
37         msg = rfc822_start(NULL, buf, len);
38         allocation_failure_check();
39
40         ok(msg, "opened %s", exname);
41
42         body = rfc822_body(msg);
43         allocation_failure_check();
44         ok(bytestring_eq(body, bytestring_from_string(e->body)),
45            "body content %s", exname);
46
47         CHECK_HEADERS(e, msg, h, exname, crlf);
48         h = rfc822_next_header(msg, h);
49         allocation_failure_check();
50         ok(!h, "Too many headers for %s", exname);
51
52         rfc822_free(msg);
53         allocation_failure_check();
54 }
55
56 static void test_hdrbody(const struct aexample *e, const char *buf, size_t len,
57                          const char *exname, int crlf)
58 {
59         struct rfc822_msg *msg;
60         struct rfc822_header *h = NULL;
61         struct bytestring body;
62
63         msg = rfc822_start(NULL, buf, len);
64         allocation_failure_check();
65         ok(msg, "opened %s", exname);
66
67         CHECK_HEADERS(e, msg, h, exname, crlf);
68         h = rfc822_next_header(msg, h);
69         allocation_failure_check();
70         ok(!h, "Too many headers for %s", exname);
71
72         body = rfc822_body(msg);
73         allocation_failure_check();
74         ok(bytestring_eq(body, bytestring_from_string(e->body)),
75            "body content %s", exname);
76
77         rfc822_free(msg);
78         allocation_failure_check();
79 }
80
81 static void test_hdrhdr(const struct aexample *e, const char *buf, size_t len,
82                         const char *exname, int crlf)
83 {
84         struct rfc822_msg *msg;
85         struct rfc822_header *h;
86
87         msg = rfc822_start(NULL, buf, len);
88         allocation_failure_check();
89         ok(msg, "opened %s", exname);
90
91         h = NULL;
92         CHECK_HEADERS(e, msg, h, exname, crlf);
93
94         h = rfc822_next_header(msg, h);
95         allocation_failure_check();
96         ok(!h, "Too many headers for %s", exname);
97
98         /* And again, this time it should be cached */
99         h = NULL;
100         CHECK_HEADERS(e, msg, h, exname, crlf);
101
102         h = rfc822_next_header(msg, h);
103         allocation_failure_check();
104         ok(!h, "Too many headers for %s", exname);
105
106         rfc822_free(msg);
107         allocation_failure_check();
108 }
109
110 int main(int argc, char *argv[])
111 {
112         struct aexample *e;
113
114         /* This is how many tests you plan to run */
115         plan_tests(20*num_aexamples()
116                    + (36 + CHECK_HEADER_NUMTESTS)*num_aexample_hdrs());
117
118         failtest_setup(argc, argv);
119
120         for_each_aexample(e) {
121                 int crlf;
122
123                 foreach_int(crlf, 0, 1) {
124                         const char *buf;
125                         size_t len;
126                         char exname[256];
127
128                         sprintf(exname, "%s[%s]", e->name, NLT(crlf));
129
130                         buf = assemble_msg(e, &len, crlf);
131                         ok((buf), "assembled %s", exname);
132                         if (!buf)
133                                 continue;
134
135                         test_bodyhdr(e, buf, len, exname, crlf);
136                         test_hdrbody(e, buf, len, exname, crlf);
137                         test_hdrhdr(e, buf, len, exname, crlf);
138
139                         talloc_free(buf);
140                 }
141         }
142
143         /* This exits depending on whether all tests passed */
144         failtest_exit(exit_status());
145 }