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, \
26 (_e)->hdrs[_i].val, crlf); \
30 static void test_bodyhdr(const struct aexample *e, const char *buf, size_t len,
31 const char *exname, int crlf)
33 struct rfc822_msg *msg;
34 struct rfc822_header *h = NULL;
35 struct bytestring body;
37 msg = rfc822_start(NULL, buf, len);
38 allocation_failure_check();
40 ok(msg, "opened %s", exname);
42 body = rfc822_body(msg);
43 allocation_failure_check();
44 ok(bytestring_eq(body, bytestring_from_string(e->body)),
45 "body content %s", exname);
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);
53 allocation_failure_check();
56 static void test_hdrbody(const struct aexample *e, const char *buf, size_t len,
57 const char *exname, int crlf)
59 struct rfc822_msg *msg;
60 struct rfc822_header *h = NULL;
61 struct bytestring body;
63 msg = rfc822_start(NULL, buf, len);
64 allocation_failure_check();
65 ok(msg, "opened %s", exname);
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);
72 body = rfc822_body(msg);
73 allocation_failure_check();
74 ok(bytestring_eq(body, bytestring_from_string(e->body)),
75 "body content %s", exname);
78 allocation_failure_check();
81 static void test_hdrhdr(const struct aexample *e, const char *buf, size_t len,
82 const char *exname, int crlf)
84 struct rfc822_msg *msg;
85 struct rfc822_header *h;
87 msg = rfc822_start(NULL, buf, len);
88 allocation_failure_check();
89 ok(msg, "opened %s", exname);
92 CHECK_HEADERS(e, msg, h, exname, crlf);
94 h = rfc822_next_header(msg, h);
95 allocation_failure_check();
96 ok(!h, "Too many headers for %s", exname);
98 /* And again, this time it should be cached */
100 CHECK_HEADERS(e, msg, h, exname, crlf);
102 h = rfc822_next_header(msg, h);
103 allocation_failure_check();
104 ok(!h, "Too many headers for %s", exname);
107 allocation_failure_check();
110 int main(int argc, char *argv[])
114 /* This is how many tests you plan to run */
115 plan_tests(20*num_aexamples()
116 + (36 + CHECK_HEADER_NUMTESTS)*num_aexample_hdrs());
118 failtest_setup(argc, argv);
120 for_each_aexample(e) {
123 foreach_int(crlf, 0, 1) {
128 sprintf(exname, "%s[%s]", e->name, NLT(crlf));
130 buf = assemble_msg(e, &len, crlf);
131 ok((buf), "assembled %s", exname);
135 test_bodyhdr(e, buf, len, exname, crlf);
136 test_hdrbody(e, buf, len, exname, crlf);
137 test_hdrhdr(e, buf, len, exname, crlf);
143 /* This exits depending on whether all tests passed */
144 failtest_exit(exit_status());