rfc822: new module.
[ccan] / ccan / rfc822 / test / testdata.h
1 #ifndef RFC822_TESTDATA_H
2 #define RFC822_TESTDATA_H
3
4 #include <ccan/talloc/talloc.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 };
11
12 struct aexample {
13         const char *name;
14         struct testhdr *hdrs;
15         size_t nhdrs;
16         const char *body;
17 };
18
19 #define AEXAMPLE(s)                             \
20         struct aexample s = {           \
21                 .name = #s,                     \
22                 .hdrs = s##_hdrs,               \
23                 .nhdrs = ARRAY_SIZE(s##_hdrs),  \
24                 .body = s##_body,               \
25         };
26
27 struct testhdr test_msg_1_hdrs[] = {
28         {"Date", "Tue, 22 Feb 2011 00:15:59 +1100"},
29         {"From", "Mister From <from@example.com>"},
30         {"To", "Mizz To <to@example.org>"},
31         {"Subject", "Some subject"},
32         {"Message-ID", "<20110221131559.GA28327@example>"},
33         {"MIME-Version", "1.0"},
34         {"Content-Type", "text/plain; charset=us-ascii"},
35         {"Content-Disposition", "inline"},
36 };
37 const char test_msg_1_body[] = "Test message\n";
38 AEXAMPLE(test_msg_1);
39
40 #define test_msg_empty_body_hdrs test_msg_1_hdrs
41 const char test_msg_empty_body_body[] = "";
42 AEXAMPLE(test_msg_empty_body);
43
44 #define test_msg_nlnl_lf_hdrs test_msg_1_hdrs
45 const char test_msg_nlnl_lf_body[] = "Message containing \n\n inside body\n";
46 AEXAMPLE(test_msg_nlnl_lf);
47
48 #define test_msg_nlnl_crlf_hdrs test_msg_1_hdrs
49 const char test_msg_nlnl_crlf_body[] = "Message containing \r\n\r\n inside body\r\n";
50 AEXAMPLE(test_msg_nlnl_crlf);
51
52 #define test_msg_nlnl_mixed_hdrs test_msg_1_hdrs
53 const char test_msg_nlnl_mixed_body[] = "Message containing both \n\n and \r\n\r\n inside body\n\r\n";
54 AEXAMPLE(test_msg_nlnl_mixed);
55
56
57 #define for_each_aexample(_e)                                \
58         foreach_ptr((_e), &test_msg_1, &test_msg_empty_body, \
59                     &test_msg_nlnl_lf, &test_msg_nlnl_crlf, \
60                     &test_msg_nlnl_mixed)
61
62 #define for_each_aexample_buf(_e, _buf, _len)   \
63         for_each_aexample((_e))                 \
64         if (((_buf) = assemble_msg((_e), &(_len))) != NULL)
65
66 static inline int num_aexamples(void)
67 {
68         const struct aexample *e;
69         int n = 0;
70
71         for_each_aexample(e)
72                 n++;
73
74         return n;
75 }
76
77 static inline int num_aexample_hdrs(void)
78 {
79         const struct aexample *e;
80         int n = 0;
81
82         for_each_aexample(e)
83                 n += e->nhdrs;
84
85         return n;
86 }
87
88 static inline const char *assemble_msg(const struct aexample *e,
89                                        size_t *len, int crlf)
90 {
91         const char *nl = crlf ? "\r\n" : "\n";
92         int nln = crlf ? 2 : 1;
93         char *msg, *amsg;
94         size_t n = 0;
95         int i;
96
97         msg = talloc_strdup(NULL, "");
98         if (!msg)
99                 return NULL;
100
101         for (i = 0; i < e->nhdrs; i++) {
102                 amsg = talloc_asprintf_append(msg, "%s:%s%s", e->hdrs[i].name,
103                                               e->hdrs[i].val, nl);
104                 if (!amsg) {
105                         talloc_free(msg);
106                         return NULL;
107                 }
108                 msg = amsg;
109                 n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
110         }
111         amsg = talloc_asprintf_append(msg, "%s%s", nl, e->body);
112         if (!amsg) {
113                 talloc_free(msg);
114                 return NULL;
115         }
116         msg = amsg;
117         n += strlen(e->body) + nln;
118         *len = n;
119         return msg;
120 }
121
122 #define NLT(crlf)       ((crlf) ? "CRLF" : "LF")
123
124 #endif /* RFC822_TESTDATA_H */