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>
8 #define CCAN_RFC822_DEBUG
10 #include <ccan/rfc822/rfc822.h>
12 #include <ccan/rfc822/rfc822.c>
17 #define CHECK_HEADERS(_e, _msg, _h, _n, _crlf) \
20 for (_i = 0; _i < (_e)->nhdrs; _i++) { \
21 (_h) = rfc822_next_header((_msg), (_h)); \
22 ok((_h), "header %d exists %s", _i, (_n)); \
25 check_header((_msg), (_h), (_e)->hdrs[_i].name, \
27 (_e)->hdrs[_i].errors, crlf); \
31 static void test_bodyhdr(const struct aexample *e, const char *buf, size_t len,
32 const char *exname, int crlf)
34 struct rfc822_msg *msg;
35 struct rfc822_header *h = NULL;
36 struct bytestring body;
38 msg = rfc822_start(NULL, buf, len);
39 allocation_failure_check();
41 ok(msg, "opened %s", exname);
43 body = rfc822_body(msg);
44 allocation_failure_check();
45 ok(bytestring_eq(body, bytestring_from_string(e->body)),
46 "body content %s", exname);
48 CHECK_HEADERS(e, msg, h, exname, crlf);
49 h = rfc822_next_header(msg, h);
50 allocation_failure_check();
51 ok(!h, "Too many headers for %s", exname);
54 allocation_failure_check();
57 static void test_hdrbody(const struct aexample *e, const char *buf, size_t len,
58 const char *exname, int crlf)
60 struct rfc822_msg *msg;
61 struct rfc822_header *h = NULL;
62 struct bytestring body;
64 msg = rfc822_start(NULL, buf, len);
65 allocation_failure_check();
66 ok(msg, "opened %s", exname);
68 CHECK_HEADERS(e, msg, h, exname, crlf);
69 h = rfc822_next_header(msg, h);
70 allocation_failure_check();
71 ok(!h, "Too many headers for %s", exname);
73 body = rfc822_body(msg);
74 allocation_failure_check();
75 ok(bytestring_eq(body, bytestring_from_string(e->body)),
76 "body content %s", exname);
79 allocation_failure_check();
82 static void test_hdrhdr(const struct aexample *e, const char *buf, size_t len,
83 const char *exname, int crlf)
85 struct rfc822_msg *msg;
86 struct rfc822_header *h;
88 msg = rfc822_start(NULL, buf, len);
89 allocation_failure_check();
90 ok(msg, "opened %s", exname);
93 CHECK_HEADERS(e, msg, h, exname, crlf);
95 h = rfc822_next_header(msg, h);
96 allocation_failure_check();
97 ok(!h, "Too many headers for %s", exname);
99 /* And again, this time it should be cached */
101 CHECK_HEADERS(e, msg, h, exname, crlf);
103 h = rfc822_next_header(msg, h);
104 allocation_failure_check();
105 ok(!h, "Too many headers for %s", exname);
108 allocation_failure_check();
111 int main(int argc, char *argv[])
115 /* This is how many tests you plan to run */
116 plan_tests(20*num_aexamples()
117 + (36 + CHECK_HEADER_NUMTESTS)*num_aexample_hdrs());
119 failtest_setup(argc, argv);
121 for_each_aexample(e) {
124 foreach_int(crlf, 0, 1) {
129 sprintf(exname, "%s[%s]", e->name, NLT(crlf));
131 buf = assemble_msg(e, &len, crlf);
132 ok((buf), "assembled %s", exname);
136 test_bodyhdr(e, buf, len, exname, crlf);
137 test_hdrbody(e, buf, len, exname, crlf);
138 test_hdrhdr(e, buf, len, exname, crlf);
144 /* This exits depending on whether all tests passed */
145 failtest_exit(exit_status());