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 check_header(struct rfc822_msg *msg,
31 struct rfc822_header *h,
32 const char *name, const char *val,
35 struct bytestring hname, hvalue, hfull;
36 size_t namelen = strlen(name);
37 size_t valuelen = strlen(val);
38 size_t nln = crlf ? 2 : 1;
39 size_t fulllen = namelen + valuelen + 1 + nln;
41 ok(rfc822_header_errors(msg, h) == 0, "Header valid");
42 allocation_failure_check();
44 hname = rfc822_header_raw_name(msg, h);
45 allocation_failure_check();
47 ok(hname.ptr && bytestring_eq(hname, bytestring_from_string(name)),
48 "Header name \"%.*s\"", hname.len, hname.ptr);
50 hvalue = rfc822_header_raw_value(msg, h);
51 allocation_failure_check();
53 ok(hvalue.ptr && ((valuelen + nln) == hvalue.len)
54 && (memcmp(val, hvalue.ptr, valuelen) == 0)
55 && (!crlf || (hvalue.ptr[hvalue.len - 2] == '\r'))
56 && (hvalue.ptr[hvalue.len - 1] == '\n'),
59 hfull = rfc822_header_raw_content(msg, h);
60 allocation_failure_check();
62 ok(hfull.ptr && (fulllen == hfull.len)
63 && (memcmp(name, hfull.ptr, namelen) == 0)
64 && (hfull.ptr[namelen] == ':')
65 && (memcmp(val, hfull.ptr + namelen + 1, valuelen) == 0)
66 && (!crlf || (hfull.ptr[fulllen-2] == '\r'))
67 && (hfull.ptr[fulllen-1] == '\n'),
71 static void test_bodyhdr(const struct aexample *e, const char *buf, size_t len,
72 const char *exname, int crlf)
74 struct rfc822_msg *msg;
75 struct rfc822_header *h = NULL;
76 struct bytestring body;
78 msg = rfc822_start(NULL, buf, len);
79 allocation_failure_check();
81 ok(msg, "opened %s", exname);
83 body = rfc822_body(msg);
84 allocation_failure_check();
85 ok(bytestring_eq(body, bytestring_from_string(e->body)),
86 "body content %s", exname);
88 CHECK_HEADERS(e, msg, h, exname, crlf);
89 h = rfc822_next_header(msg, h);
90 allocation_failure_check();
91 ok(!h, "Too many headers for %s", exname);
94 allocation_failure_check();
97 static void test_hdrbody(const struct aexample *e, const char *buf, size_t len,
98 const char *exname, int crlf)
100 struct rfc822_msg *msg;
101 struct rfc822_header *h = NULL;
102 struct bytestring body;
104 msg = rfc822_start(NULL, buf, len);
105 allocation_failure_check();
106 ok(msg, "opened %s", exname);
108 CHECK_HEADERS(e, msg, h, exname, crlf);
109 h = rfc822_next_header(msg, h);
110 allocation_failure_check();
111 ok(!h, "Too many headers for %s", exname);
113 body = rfc822_body(msg);
114 allocation_failure_check();
115 ok(bytestring_eq(body, bytestring_from_string(e->body)),
116 "body content %s", exname);
119 allocation_failure_check();
122 static void test_hdrhdr(const struct aexample *e, const char *buf, size_t len,
123 const char *exname, int crlf)
125 struct rfc822_msg *msg;
126 struct rfc822_header *h;
128 msg = rfc822_start(NULL, buf, len);
129 allocation_failure_check();
130 ok(msg, "opened %s", exname);
133 CHECK_HEADERS(e, msg, h, exname, crlf);
135 h = rfc822_next_header(msg, h);
136 allocation_failure_check();
137 ok(!h, "Too many headers for %s", exname);
139 /* And again, this time it should be cached */
141 CHECK_HEADERS(e, msg, h, exname, crlf);
143 h = rfc822_next_header(msg, h);
144 allocation_failure_check();
145 ok(!h, "Too many headers for %s", exname);
148 allocation_failure_check();
151 int main(int argc, char *argv[])
155 /* This is how many tests you plan to run */
156 plan_tests(20*num_aexamples() + 40*num_aexample_hdrs());
158 failtest_setup(argc, argv);
160 for_each_aexample(e) {
163 foreach_int(crlf, 0, 1) {
168 sprintf(exname, "%s[%s]", e->name, NLT(crlf));
170 buf = assemble_msg(e, &len, crlf);
171 ok((buf), "assembled %s", exname);
175 test_bodyhdr(e, buf, len, exname, crlf);
176 test_hdrbody(e, buf, len, exname, crlf);
177 test_hdrhdr(e, buf, len, exname, crlf);
183 /* This exits depending on whether all tests passed */
184 failtest_exit(exit_status());