summaryrefslogtreecommitdiff
path: root/ccan/rfc822/test/testdata.h
blob: ada262b5f544300653a301f566785e1fcaa6011b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef RFC822_TESTDATA_H
#define RFC822_TESTDATA_H

#include <ccan/tal/str/str.h>
#include <ccan/array_size/array_size.h>
#include <ccan/foreach/foreach.h>

struct testhdr {
	const char *name, *val;
	int index, last;
	enum rfc822_header_errors errors;
};

struct aexample {
	const char *name;
	struct testhdr *hdrs;
	size_t nhdrs;
	const char *body;
};

#define AEXAMPLE(s)				\
	struct aexample s = {		\
		.name = #s,			\
		.hdrs = s##_hdrs,		\
		.nhdrs = ARRAY_SIZE(s##_hdrs),	\
		.body = s##_body,		\
	};

struct testhdr test_msg_1_hdrs[] = {
	{"Date", "Tue, 22 Feb 2011 00:15:59 +1100"},
	{"From", "Mister From <from@example.com>"},
	{"To", "Mizz To <to@example.org>"},
	{"Subject", "Some subject"},
	{"Message-ID", "<20110221131559.GA28327@example>"},
	{"MIME-Version", "1.0"},
	{"Content-Type", "text/plain; charset=us-ascii"},
	{"Content-Disposition", "inline"},
};
const char test_msg_1_body[] = "Test message\n";
AEXAMPLE(test_msg_1);

#define test_msg_empty_body_hdrs test_msg_1_hdrs
const char test_msg_empty_body_body[] = "";
AEXAMPLE(test_msg_empty_body);

#define test_msg_nlnl_lf_hdrs test_msg_1_hdrs
const char test_msg_nlnl_lf_body[] = "Message containing \n\n inside body\n";
AEXAMPLE(test_msg_nlnl_lf);

#define test_msg_nlnl_crlf_hdrs test_msg_1_hdrs
const char test_msg_nlnl_crlf_body[] = "Message containing \r\n\r\n inside body\r\n";
AEXAMPLE(test_msg_nlnl_crlf);

#define test_msg_nlnl_mixed_hdrs test_msg_1_hdrs
const char test_msg_nlnl_mixed_body[] = "Message containing both \n\n and \r\n\r\n inside body\n\r\n";
AEXAMPLE(test_msg_nlnl_mixed);

#define test_msg_space_body_hdrs test_msg_1_hdrs
const char test_msg_space_body_body[] = " Message with LWS at start of body\n";
AEXAMPLE(test_msg_space_body);

struct testhdr bad_hdrs_hdrs[] = {
	{"From", "Mister From <from@example.com>"},
	{"To", "Mizz To <to@example.org>"},
	{"X-Bad-\bName", "This header field has bad characters in the name",
		 .errors = RFC822_HDR_BAD_NAME_CHARS},
	{"Subject", "Some subject"},
	{"Message-ID", "<20110221131559.GA28327@example>"},
};
#define bad_hdrs_body test_msg_1_body
AEXAMPLE(bad_hdrs)

struct testhdr repeated_hdrs_1_hdrs[] = {
	{"X-Repeated-Header", "#1", 0, 4},
	{"x-repeated-header", "#2", 1, 4},
	{"X-REPEATED-HEADER", "#3", 2, 4},
	{"x-rEpEaTeD-hEaDeR", "#4", 3, 4},
	{"X-Repeated-Header", "#5", 4, 4},
};
#define repeated_hdrs_1_body test_msg_1_body
AEXAMPLE(repeated_hdrs_1);

struct testhdr prefix_hdr_hdrs[] = {
	{"X-Prefix", "Prefix", 0},
	{"X-Prefix-and-Suffix", "Suffix", 0},
};
#define prefix_hdr_body test_msg_1_body
AEXAMPLE(prefix_hdr);

#define for_each_aexample(_e)				     \
	foreach_ptr((_e), &test_msg_1, &test_msg_empty_body, \
		    &test_msg_nlnl_lf, &test_msg_nlnl_crlf, \
		    &test_msg_nlnl_mixed, \
		    &test_msg_space_body, \
		    &bad_hdrs,		  \
		    &repeated_hdrs_1,	  \
		    &prefix_hdr)

#define for_each_aexample_buf(_e, _buf, _len)	\
	for_each_aexample((_e)) 		\
	if (((_buf) = assemble_msg((_e), &(_len))) != NULL)

static inline int num_aexamples(void)
{
	const struct aexample *e;
	int n = 0;

	for_each_aexample(e)
		n++;

	return n;
}

static inline int num_aexample_hdrs(void)
{
	const struct aexample *e;
	int n = 0;

	for_each_aexample(e)
		n += e->nhdrs;

	return n;
}

static inline const char *assemble_msg(const struct aexample *e,
				       size_t *len, int crlf)
{
	const char *nl = crlf ? "\r\n" : "\n";
	int nln = crlf ? 2 : 1;
	char *msg;
	size_t n = 0;
	int i;

	msg = tal_strdup(NULL, "");
	if (!msg)
		return NULL;

	for (i = 0; i < e->nhdrs; i++) {
		if (!tal_append_fmt(&msg, "%s:%s%s", e->hdrs[i].name,
				    e->hdrs[i].val, nl)) {
			tal_free(msg);
			return NULL;
		}
		n += strlen(e->hdrs[i].name) + strlen(e->hdrs[i].val) + 1 + nln;
	}
	if (!tal_append_fmt(&msg, "%s%s", nl, e->body)) {
		tal_free(msg);
		return NULL;
	}
	n += strlen(e->body) + nln;
	*len = n;
	return msg;
}

#define NLT(crlf)	((crlf) ? "CRLF" : "LF")

#endif /* RFC822_TESTDATA_H */