summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/testsuite/testsuite-message.c
blob: 68e20ebe678d96de715c486c263ed5d7a4aec0df (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"
#include "istream.h"
#include "smtp-params.h"
#include "message-address.h"
#include "mail-storage.h"
#include "master-service.h"
#include "mail-raw.h"

#include "sieve-common.h"
#include "sieve-address.h"
#include "sieve-error.h"
#include "sieve-message.h"
#include "sieve-interpreter.h"

#include "sieve-tool.h"

#include "testsuite-common.h"
#include "testsuite-message.h"

/*
 * Testsuite message environment
 */

struct testsuite_message {
	struct testsuite_message *next;

	struct mail_raw *mail_raw;
};

struct sieve_message_data testsuite_msgdata;
static struct smtp_params_rcpt testsuite_rcpt_params;

static struct testsuite_message *testsuite_msg;

static const char *_default_message_data =
"From: sender@example.com\n"
"To: recipient@example.org\n"
"Subject: Frop!\n"
"\n"
"Friep!\n";

static struct smtp_address *testsuite_env_mail_from = NULL;
static struct smtp_address *testsuite_env_rcpt_to = NULL;
static struct smtp_address *testsuite_env_orig_rcpt_to = NULL;
static char *testsuite_env_auth = NULL;

static pool_t testsuite_msg_pool;
static char *testsuite_msg_id = NULL;

static const struct smtp_address *
testsuite_message_get_address(struct mail *mail, const char *header)
{
	struct message_address *addr;
	struct smtp_address *smtp_addr;
	const char *str;

	if (mail_get_first_header(mail, header, &str) <= 0)
		return NULL;
	addr = message_address_parse(pool_datastack_create(),
				     (const unsigned char *)str,
				     strlen(str), 1, 0);
	if (addr == NULL || addr->mailbox == NULL || *addr->mailbox == '\0')
		return NULL;
	if (smtp_address_create_from_msg_temp(addr, &smtp_addr) < 0)
		return NULL;
	return smtp_addr;
}

static void testsuite_message_set_data(struct mail *mail)
{
	const struct smtp_address *recipient = NULL, *sender = NULL;
	const char *msg_id;

	static const struct smtp_address default_recipient = {
		.localpart = "recipient",
		.domain = "example.com",
	};
	static const struct smtp_address default_sender = {
		.localpart = "sender",
		.domain = "example.com",
	};

	i_free(testsuite_env_mail_from);
	i_free(testsuite_env_rcpt_to);
	i_free(testsuite_env_orig_rcpt_to);
	i_free(testsuite_env_auth);
	i_free(testsuite_msg_id);

	/*
	 * Collect necessary message data
	 */

	/* Get recipient address */
	recipient = testsuite_message_get_address(mail, "Envelope-To");
	if (recipient == NULL)
		recipient = testsuite_message_get_address(mail, "To");
	if (recipient == NULL)
		recipient = &default_recipient;

	/* Get sender address */
	sender = testsuite_message_get_address(mail, "Return-path");
	if (sender == NULL)
		sender = testsuite_message_get_address(mail, "Sender");
	if (sender == NULL)
		sender = testsuite_message_get_address(mail, "From");
	if (sender == NULL)
		sender = &default_sender;

	testsuite_env_mail_from = smtp_address_clone(default_pool, sender);
	testsuite_env_rcpt_to = smtp_address_clone(default_pool, recipient);
	testsuite_env_orig_rcpt_to = smtp_address_clone(default_pool, recipient);

	(void)mail_get_message_id(mail, &msg_id);
	testsuite_msg_id = i_strdup(msg_id);

	i_zero(&testsuite_msgdata);
	testsuite_msgdata.mail = mail;
	testsuite_msgdata.auth_user = sieve_tool_get_username(sieve_tool);
	testsuite_msgdata.envelope.mail_from = testsuite_env_mail_from;
	testsuite_msgdata.envelope.rcpt_to = testsuite_env_rcpt_to;
	testsuite_msgdata.id = testsuite_msg_id;

	i_zero(&testsuite_rcpt_params);
	testsuite_rcpt_params.orcpt.addr = testsuite_env_orig_rcpt_to;

	testsuite_msgdata.envelope.rcpt_params = &testsuite_rcpt_params;
}

static struct testsuite_message *testsuite_message_new(void)
{
	struct testsuite_message *msg;

	msg = i_new(struct testsuite_message, 1);
	msg->next = testsuite_msg;
	testsuite_msg = msg;

	return msg;
}

static void testsuite_message_new_string(string_t *mail_str)
{
	struct mail_user *mail_raw_user =
		sieve_tool_get_mail_raw_user(sieve_tool);
	struct testsuite_message *msg;

	msg = testsuite_message_new();
	msg->mail_raw = mail_raw_open_data(mail_raw_user, mail_str);

	testsuite_message_set_data(msg->mail_raw->mail);
}

static void testsuite_message_new_file(const char *mail_path)
{
	struct mail_user *mail_raw_user =
		sieve_tool_get_mail_raw_user(sieve_tool);
	struct testsuite_message *msg;

	msg = testsuite_message_new();
	msg->mail_raw = mail_raw_open_file(mail_raw_user, mail_path);

	testsuite_message_set_data(msg->mail_raw->mail);
}

static void testsuite_message_free(bool all)
{
	struct testsuite_message *msg;

	if (testsuite_msg == NULL)
		return;

	msg = (all ? testsuite_msg : testsuite_msg->next);
	while (msg != NULL) {
		struct testsuite_message *msg_next = msg->next;

		mail_raw_close(&msg->mail_raw);
		i_free(msg);

		msg = msg_next;
	}
	if (all)
		testsuite_msg = NULL;
	else
		testsuite_msg->next = NULL;
}

void testsuite_message_flush(void)
{
	testsuite_message_free(FALSE);
}

void testsuite_message_init(void)
{
	testsuite_msg_pool = pool_alloconly_create("testsuite_message", 6096);

	string_t *default_message = str_new(testsuite_msg_pool, 1024);
	str_append(default_message, _default_message_data);

	testsuite_message_new_string(default_message);
}

void testsuite_message_set_string(const struct sieve_runtime_env *renv,
				  string_t *message)
{
	sieve_message_context_reset(renv->msgctx);

	testsuite_message_new_string(message);
}

void testsuite_message_set_file(const struct sieve_runtime_env *renv,
				const char *file_path)
{
	sieve_message_context_reset(renv->msgctx);

	testsuite_message_new_file(file_path);
}

void testsuite_message_set_mail(const struct sieve_runtime_env *renv,
				struct mail *mail)
{
	sieve_message_context_reset(renv->msgctx);

	testsuite_message_set_data(mail);
}

void testsuite_message_deinit(void)
{
	testsuite_message_free(TRUE);

	i_free(testsuite_env_mail_from);
	i_free(testsuite_env_rcpt_to);
	i_free(testsuite_env_orig_rcpt_to);
	i_free(testsuite_env_auth);
	pool_unref(&testsuite_msg_pool);
	i_free(testsuite_msg_id);
}

void testsuite_envelope_set_sender_address(const struct sieve_runtime_env *renv,
					   const struct smtp_address *address)
{
	sieve_message_context_reset(renv->msgctx);

	i_free(testsuite_env_mail_from);

	testsuite_env_mail_from = smtp_address_clone(default_pool, address);
	testsuite_msgdata.envelope.mail_from = testsuite_env_mail_from;
}

void testsuite_envelope_set_sender(const struct sieve_runtime_env *renv,
				   const char *value)
{
	struct smtp_address *address = NULL;
	const char *error;

	if (smtp_address_parse_path(pool_datastack_create(), value,
				    (SMTP_ADDRESS_PARSE_FLAG_ALLOW_EMPTY |
				     SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL),
				    &address, &error) < 0) {
		e_error(testsuite_sieve_instance->event,
			"testsuite: envelope sender address "
			"`%s' is invalid: %s", value, error);
	}
	testsuite_envelope_set_sender_address(renv, address);
}

void testsuite_envelope_set_recipient_address(
	const struct sieve_runtime_env *renv,
	const struct smtp_address *address)
{
	sieve_message_context_reset(renv->msgctx);

	i_free(testsuite_env_rcpt_to);
	i_free(testsuite_env_orig_rcpt_to);

	testsuite_env_rcpt_to = smtp_address_clone(default_pool, address);
	testsuite_env_orig_rcpt_to = smtp_address_clone(default_pool, address);
	testsuite_msgdata.envelope.rcpt_to = testsuite_env_rcpt_to;
	testsuite_rcpt_params.orcpt.addr = testsuite_env_orig_rcpt_to;
}

void testsuite_envelope_set_recipient(const struct sieve_runtime_env *renv,
				      const char *value)
{
	struct smtp_address *address = NULL;
	const char *error;

	if (smtp_address_parse_path(pool_datastack_create(), value,
				    (SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART |
				     SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL),
				    &address, &error) < 0) {
		e_error(testsuite_sieve_instance->event,
			"testsuite: envelope recipient address "
			"`%s' is invalid: %s", value, error);
	}
	testsuite_envelope_set_recipient_address(renv, address);
}

void testsuite_envelope_set_orig_recipient_address(
	const struct sieve_runtime_env *renv,
	const struct smtp_address *address)
{
	sieve_message_context_reset(renv->msgctx);

	i_free(testsuite_env_orig_rcpt_to);

	testsuite_env_orig_rcpt_to = smtp_address_clone(default_pool, address);
	testsuite_rcpt_params.orcpt.addr = testsuite_env_orig_rcpt_to;
}

void testsuite_envelope_set_orig_recipient(const struct sieve_runtime_env *renv,
					   const char *value)
{
	struct smtp_address *address = NULL;
	const char *error;

	if (smtp_address_parse_path(pool_datastack_create(), value,
				    (SMTP_ADDRESS_PARSE_FLAG_ALLOW_LOCALPART |
				     SMTP_ADDRESS_PARSE_FLAG_BRACKETS_OPTIONAL),
				    &address, &error) < 0) {
		e_error(testsuite_sieve_instance->event,
			"testsuite: envelope recipient address "
			"`%s' is invalid: %s", value, error);
	}
	testsuite_envelope_set_orig_recipient_address(renv, address);
}

void testsuite_envelope_set_auth_user(const struct sieve_runtime_env *renv,
				      const char *value)
{
	sieve_message_context_reset(renv->msgctx);

	i_free(testsuite_env_auth);

	testsuite_env_auth = i_strdup(value);
	testsuite_msgdata.auth_user = testsuite_env_auth;
}