]> git.ozlabs.org Git - ccan/blob - ccan/rfc822/test/testdata.h
rfc822: switch to ccan/tal.
[ccan] / ccan / rfc822 / test / testdata.h
1 #ifndef RFC822_TESTDATA_H
2 #define RFC822_TESTDATA_H
3
4 #include <ccan/tal/str/str.h>
5 #include <ccan/array_size/array_size.h>
6 #include <ccan/foreach/foreach.h>
7
8 struct testhdr {
9         const char *name, *val;
10         int index, last;
11         enum rfc822_header_errors errors;
12 };
13
14 struct aexample {
15         const char *name;
16         struct testhdr *hdrs;
17         size_t nhdrs;
18         const char *body;
19 };
20
21 #define AEXAMPLE(s)                             \
22         struct aexample s = {           \
23                 .name = #s,                     \
24                 .hdrs = s##_hdrs,               \
25                 .nhdrs = ARRAY_SIZE(s##_hdrs),  \
26                 .body = s##_body,               \
27         };
28
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"},
38 };
39 const char test_msg_1_body[] = "Test message\n";
40 AEXAMPLE(test_msg_1);
41
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);
45
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);
49
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);
53
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);
57
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);
61
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>"},
69 };
70 #define bad_hdrs_body test_msg_1_body
71 AEXAMPLE(bad_hdrs)
72
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},
79 };
80 #define repeated_hdrs_1_body test_msg_1_body
81 AEXAMPLE(repeated_hdrs_1);
82
83 struct testhdr prefix_hdr_hdrs[] = {
84         {"X-Prefix", "Prefix", 0},
85         {"X-Prefix-and-Suffix", "Suffix", 0},
86 };
87 #define prefix_hdr_body test_msg_1_body
88 AEXAMPLE(prefix_hdr);
89
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, \
95                     &bad_hdrs,            \
96                     &repeated_hdrs_1,     \
97                     &prefix_hdr)
98
99 #define for_each_aexample_buf(_e, _buf, _len)   \
100         for_each_aexample((_e))                 \
101         if (((_buf) = assemble_msg((_e), &(_len))) != NULL)
102
103 static inline int num_aexamples(void)
104 {
105         const struct aexample *e;
106         int n = 0;
107
108         for_each_aexample(e)
109                 n++;
110
111         return n;
112 }
113
114 static inline int num_aexample_hdrs(void)
115 {
116         const struct aexample *e;
117         int n = 0;
118
119         for_each_aexample(e)
120                 n += e->nhdrs;
121
122         return n;
123 }
124
125 static inline const char *assemble_msg(const struct aexample *e,
126                                        size_t *len, int crlf)
127 {
128         const char *nl = crlf ? "\r\n" : "\n";
129         int nln = crlf ? 2 : 1;
130         char *msg;
131         size_t n = 0;
132         int i;
133
134         msg = tal_strdup(NULL, "");
135         if (!msg)
136                 return NULL;
137
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)) {
141                         tal_free(msg);
142                         return NULL;
143                 }
144                 n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
145         }
146         if (!tal_append_fmt(&msg, "%s%s", nl, e->body)) {
147                 tal_free(msg);
148                 return NULL;
149         }
150         n += strlen(e->body) + nln;
151         *len = n;
152         return msg;
153 }
154
155 #define NLT(crlf)       ((crlf) ? "CRLF" : "LF")
156
157 #endif /* RFC822_TESTDATA_H */