1 #ifndef RFC822_TESTDATA_H
2 #define RFC822_TESTDATA_H
4 #include <ccan/tal/str/str.h>
5 #include <ccan/array_size/array_size.h>
6 #include <ccan/foreach/foreach.h>
9 const char *name, *val;
11 enum rfc822_header_errors errors;
22 struct aexample s = { \
25 .nhdrs = ARRAY_SIZE(s##_hdrs), \
29 struct testhdr test_msg_1_hdrs[] = {
30 {"Date", "Tue, 22 Feb 2011 00:15:59 +1100"},
31 {"From", "Mister From <from@example.com>"},
32 {"To", "Mizz To <to@example.org>"},
33 {"Subject", "Some subject"},
34 {"Message-ID", "<20110221131559.GA28327@example>"},
35 {"MIME-Version", "1.0"},
36 {"Content-Type", "text/plain; charset=us-ascii"},
37 {"Content-Disposition", "inline"},
39 const char test_msg_1_body[] = "Test message\n";
42 #define test_msg_empty_body_hdrs test_msg_1_hdrs
43 const char test_msg_empty_body_body[] = "";
44 AEXAMPLE(test_msg_empty_body);
46 #define test_msg_nlnl_lf_hdrs test_msg_1_hdrs
47 const char test_msg_nlnl_lf_body[] = "Message containing \n\n inside body\n";
48 AEXAMPLE(test_msg_nlnl_lf);
50 #define test_msg_nlnl_crlf_hdrs test_msg_1_hdrs
51 const char test_msg_nlnl_crlf_body[] = "Message containing \r\n\r\n inside body\r\n";
52 AEXAMPLE(test_msg_nlnl_crlf);
54 #define test_msg_nlnl_mixed_hdrs test_msg_1_hdrs
55 const char test_msg_nlnl_mixed_body[] = "Message containing both \n\n and \r\n\r\n inside body\n\r\n";
56 AEXAMPLE(test_msg_nlnl_mixed);
58 #define test_msg_space_body_hdrs test_msg_1_hdrs
59 const char test_msg_space_body_body[] = " Message with LWS at start of body\n";
60 AEXAMPLE(test_msg_space_body);
62 struct testhdr bad_hdrs_hdrs[] = {
63 {"From", "Mister From <from@example.com>"},
64 {"To", "Mizz To <to@example.org>"},
65 {"X-Bad-\bName", "This header field has bad characters in the name",
66 .errors = RFC822_HDR_BAD_NAME_CHARS},
67 {"Subject", "Some subject"},
68 {"Message-ID", "<20110221131559.GA28327@example>"},
70 #define bad_hdrs_body test_msg_1_body
73 struct testhdr repeated_hdrs_1_hdrs[] = {
74 {"X-Repeated-Header", "#1", 0, 4},
75 {"x-repeated-header", "#2", 1, 4},
76 {"X-REPEATED-HEADER", "#3", 2, 4},
77 {"x-rEpEaTeD-hEaDeR", "#4", 3, 4},
78 {"X-Repeated-Header", "#5", 4, 4},
80 #define repeated_hdrs_1_body test_msg_1_body
81 AEXAMPLE(repeated_hdrs_1);
83 struct testhdr prefix_hdr_hdrs[] = {
84 {"X-Prefix", "Prefix", 0},
85 {"X-Prefix-and-Suffix", "Suffix", 0},
87 #define prefix_hdr_body test_msg_1_body
90 #define for_each_aexample(_e) \
91 foreach_ptr((_e), &test_msg_1, &test_msg_empty_body, \
92 &test_msg_nlnl_lf, &test_msg_nlnl_crlf, \
93 &test_msg_nlnl_mixed, \
94 &test_msg_space_body, \
99 #define for_each_aexample_buf(_e, _buf, _len) \
100 for_each_aexample((_e)) \
101 if (((_buf) = assemble_msg((_e), &(_len))) != NULL)
103 static inline int num_aexamples(void)
105 const struct aexample *e;
114 static inline int num_aexample_hdrs(void)
116 const struct aexample *e;
125 static inline const char *assemble_msg(const struct aexample *e,
126 size_t *len, int crlf)
128 const char *nl = crlf ? "\r\n" : "\n";
129 int nln = crlf ? 2 : 1;
134 msg = tal_strdup(NULL, "");
138 for (i = 0; i < e->nhdrs; i++) {
139 if (!tal_append_fmt(&msg, "%s:%s%s", e->hdrs[i].name,
140 e->hdrs[i].val, nl)) {
144 n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
146 if (!tal_append_fmt(&msg, "%s%s", nl, e->body)) {
150 n += strlen(e->body) + nln;
155 #define NLT(crlf) ((crlf) ? "CRLF" : "LF")
157 #endif /* RFC822_TESTDATA_H */