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 UNFOLDED " This is a string with\tlots of \tplaces to fold"
18 #define FOLD_POINTS 11
20 #define BEFORE "Date: Tue, 22 Feb 2011 00:15:59 +1100\n" \
21 "From: Mister From <from@example.com>\n" \
22 "To: Mizz To <to@example.org>\n" \
25 #define AFTER "Message-ID: <20110221131559.GA28327@example>\n" \
29 static struct bytestring fold_and_assemble(int foldat, int crlf, int truncated)
34 buf = tal_arr(NULL, char, strlen(BEFORE) + strlen(AFTER) + 3*strlen(UNFOLDED) + 2);
38 memcpy(buf, BEFORE, strlen(BEFORE));
40 p = buf + strlen(BEFORE);
42 for (i = 0; i < strlen(UNFOLDED); i++) {
43 if (rfc822_iswsp(UNFOLDED[i])) {
45 if ((foldat == -1) || (foldat == n)) {
59 memcpy(p, AFTER, strlen(AFTER));
63 return bytestring(buf, p - buf);
66 static void check_folded_header(const char *buf, size_t len)
68 struct rfc822_msg *msg;
69 struct rfc822_header *hdr;
70 struct bytestring hunfold;
72 msg = rfc822_start(NULL, buf, len);
73 allocation_failure_check();
75 hdr = rfc822_first_header(msg);
76 allocation_failure_check();
77 hdr = rfc822_next_header(msg, hdr);
78 allocation_failure_check();
79 hdr = rfc822_next_header(msg, hdr);
80 allocation_failure_check();
82 /* This is the one we care about */
83 hdr = rfc822_next_header(msg, hdr);
84 allocation_failure_check();
86 ok(hdr && (rfc822_header_errors(msg, hdr) == 0), "Folded header valid");
87 allocation_failure_check();
89 hunfold = rfc822_header_unfolded_value(msg, hdr);
90 allocation_failure_check();
92 ok(hunfold.len == strlen(UNFOLDED), "Unfolded length %zd, should be %zd",
93 hunfold.len, strlen(UNFOLDED));
94 ok1(memcmp(hunfold.ptr, UNFOLDED, hunfold.len) == 0);
97 allocation_failure_check();
100 int main(int argc, char *argv[])
102 struct bytestring msgbuf;
103 int crlf, truncated, i;
105 failtest_setup(argc, argv);
107 plan_tests(3 * 2 * 2 * (FOLD_POINTS + 2));
109 foreach_int(crlf, 0, 1) {
110 foreach_int(truncated, 0, 1) {
111 for (i = -1; i <= FOLD_POINTS; i++) {
112 msgbuf = fold_and_assemble(i, crlf, truncated);
113 check_folded_header(msgbuf.ptr, msgbuf.len);
114 tal_free(msgbuf.ptr);
119 /* This exits depending on whether all tests passed */
120 failtest_exit(exit_status());