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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "str.h"
#include "string.h"
#include "ostream.h"
#include "hash.h"
#include "mail-storage.h"
#include "env-util.h"
#include "unlink-directory.h"
#include "mail-raw.h"
#include "sieve-common.h"
#include "sieve-code.h"
#include "sieve-message.h"
#include "sieve-commands.h"
#include "sieve-extensions.h"
#include "sieve-binary.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-result.h"
#include "sieve-dump.h"
#include "testsuite-common.h"
#include "testsuite-settings.h"
#include "testsuite-objects.h"
#include "testsuite-log.h"
#include "testsuite-script.h"
#include "testsuite-binary.h"
#include "testsuite-result.h"
#include "testsuite-smtp.h"
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
/*
* Global data
*/
struct sieve_instance *testsuite_sieve_instance = NULL;
char *testsuite_test_path = NULL;
/* Test context */
static string_t *test_name;
static sieve_size_t test_block_end;
static unsigned int test_index;
static unsigned int test_failures;
/* Extension */
const struct sieve_extension *testsuite_ext;
/*
* Validator context
*/
bool testsuite_validator_context_initialize(struct sieve_validator *valdtr)
{
pool_t pool = sieve_validator_pool(valdtr);
struct testsuite_validator_context *ctx =
p_new(pool, struct testsuite_validator_context, 1);
/* Setup object registry */
ctx->object_registrations =
sieve_validator_object_registry_create(valdtr);
testsuite_register_core_objects(ctx);
sieve_validator_extension_set_context(valdtr, testsuite_ext, ctx);
return TRUE;
}
struct testsuite_validator_context *
testsuite_validator_context_get(struct sieve_validator *valdtr)
{
return (struct testsuite_validator_context *)
sieve_validator_extension_get_context(valdtr, testsuite_ext);
}
/*
* Generator context
*/
bool testsuite_generator_context_initialize(
struct sieve_generator *gentr, const struct sieve_extension *this_ext)
{
pool_t pool = sieve_generator_pool(gentr);
struct sieve_binary_block *sblock = sieve_generator_get_block(gentr);
struct testsuite_generator_context *ctx =
p_new(pool, struct testsuite_generator_context, 1);
/* Setup exit jumplist */
ctx->exit_jumps = sieve_jumplist_create(pool, sblock);
sieve_generator_extension_set_context(gentr, this_ext, ctx);
return TRUE;
}
/*
* Interpreter context
*/
static void
testsuite_interpreter_free(const struct sieve_extension *ext ATTR_UNUSED,
struct sieve_interpreter *interp ATTR_UNUSED,
void *context)
{
struct testsuite_interpreter_context *ctx =
(struct testsuite_interpreter_context *)context;
sieve_binary_unref(&ctx->compiled_script);
}
const struct sieve_interpreter_extension testsuite_interpreter_ext = {
.ext_def = &testsuite_extension,
.free = testsuite_interpreter_free,
};
bool testsuite_interpreter_context_initialize(
struct sieve_interpreter *interp, const struct sieve_extension *this_ext)
{
pool_t pool = sieve_interpreter_pool(interp);
struct testsuite_interpreter_context *ctx =
p_new(pool, struct testsuite_interpreter_context, 1);
sieve_interpreter_extension_register(interp, this_ext,
&testsuite_interpreter_ext, ctx);
return TRUE;
}
struct testsuite_interpreter_context *
testsuite_interpreter_context_get(struct sieve_interpreter *interp,
const struct sieve_extension *this_ext)
{
struct testsuite_interpreter_context *ctx =
sieve_interpreter_extension_get_context(interp, this_ext);
return ctx;
}
/*
* Test context
*/
static void testsuite_test_context_init(void)
{
test_name = str_new(default_pool, 128);
test_block_end = 0;
test_index = 0;
test_failures = 0;
}
int testsuite_test_start(const struct sieve_runtime_env *renv,
string_t *name, sieve_size_t block_end)
{
if (test_block_end != 0) {
sieve_runtime_trace_error(renv, "already inside test block");
return SIEVE_EXEC_BIN_CORRUPT;
}
str_truncate(test_name, 0);
str_append_str(test_name, name);
test_block_end = block_end;
test_index++;
return SIEVE_EXEC_OK;
}
int testsuite_test_fail(const struct sieve_runtime_env *renv,
string_t *reason)
{
return testsuite_test_fail_cstr(renv, str_c(reason));
}
int testsuite_test_failf(const struct sieve_runtime_env *renv,
const char *fmt, ...)
{
va_list args;
int ret;
va_start(args, fmt);
ret = testsuite_test_fail_cstr(renv, t_strdup_vprintf(fmt, args));
va_end(args);
return ret;
}
int testsuite_test_fail_cstr(const struct sieve_runtime_env *renv,
const char *reason)
{
sieve_size_t end = test_block_end;
if (str_len(test_name) == 0) {
if (reason == NULL || *reason == '\0')
printf("%2d: Test FAILED\n", test_index);
else
printf("%2d: Test FAILED: %s\n", test_index, reason);
} else {
if (reason == NULL || *reason == '\0') {
printf("%2d: Test '%s' FAILED\n",
test_index, str_c(test_name));
} else {
printf("%2d: Test '%s' FAILED: %s\n",
test_index, str_c(test_name), reason);
}
}
str_truncate(test_name, 0);
test_block_end = 0;
test_failures++;
return sieve_interpreter_program_jump_to(renv->interp, end, FALSE);
}
void testsuite_testcase_fail(const char *reason)
{
if (reason == NULL || *reason == '\0')
printf("XX: Test CASE FAILED\n");
else
printf("XX: Test CASE FAILED: %s\n", reason);
test_failures++;
}
int testsuite_test_succeed(const struct sieve_runtime_env *renv,
sieve_size_t *address, string_t *reason)
{
sieve_size_t end = test_block_end;
int ret;
if (str_len(test_name) == 0) {
if (reason == NULL || str_len(reason) == 0)
printf("%2d: Test SUCCEEDED\n", test_index);
else {
printf("%2d: Test SUCCEEDED: %s\n",
test_index, str_c(reason));
}
} else {
if (reason == NULL || str_len(reason) == 0) {
printf("%2d: Test '%s' SUCCEEDED\n",
test_index, str_c(test_name));
} else {
printf("%2d: Test '%s' SUCCEEDED: %s\n", test_index,
str_c(test_name), str_c(reason));
}
}
str_truncate(test_name, 0);
test_block_end = 0;
if (*address > end) {
sieve_runtime_trace_error(
renv, "invalid test block end offset");
return SIEVE_EXEC_BIN_CORRUPT;
} else if (*address < end) {
ret = sieve_interpreter_program_jump_to(
renv->interp, end, FALSE);
if (ret <= 0)
return ret;
}
return SIEVE_EXEC_OK;
}
static void testsuite_test_context_deinit(void)
{
str_free(&test_name);
}
bool testsuite_testcase_result(bool expect_failure)
{
if (expect_failure) {
if (test_failures < test_index) {
printf("\nFAIL: Only %d of %d tests failed "
"(all expected to fail).\n\n",
test_failures, test_index);
return FALSE;
}
printf("\nPASS: %d tests failed (expected to fail).\n\n",
(test_index == 0 ? 1 : test_index));
return TRUE;
}
if (test_failures > 0) {
printf("\nFAIL: %d of %d tests failed.\n\n",
test_failures, test_index);
return FALSE;
}
printf("\nPASS: %d tests succeeded.\n\n", test_index);
return TRUE;
}
/*
* Testsuite temporary directory
*/
static char *testsuite_tmp_dir;
static void testsuite_tmp_dir_init(void)
{
testsuite_tmp_dir = i_strdup_printf("/tmp/dsieve-testsuite.%s.%s",
dec2str(time(NULL)),
dec2str(getpid()));
if (mkdir(testsuite_tmp_dir, 0700) < 0) {
i_fatal("failed to create temporary directory '%s': %m.",
testsuite_tmp_dir);
}
}
static void testsuite_tmp_dir_deinit(void)
{
const char *error;
if (unlink_directory(testsuite_tmp_dir,
UNLINK_DIRECTORY_FLAG_RMDIR, &error) < 0)
i_warning("failed to remove temporary directory '%s': %s.",
testsuite_tmp_dir, error);
i_free(testsuite_tmp_dir);
}
const char *testsuite_tmp_dir_get(void)
{
return testsuite_tmp_dir;
}
/*
* Main testsuite init/deinit
*/
void testsuite_init(struct sieve_instance *svinst, const char *test_path,
bool log_stdout)
{
testsuite_sieve_instance = svinst;
testsuite_test_context_init();
testsuite_log_init(log_stdout);
testsuite_tmp_dir_init();
testsuite_script_init();
testsuite_binary_init();
testsuite_smtp_init();
testsuite_ext =
sieve_extension_register(svinst, &testsuite_extension, TRUE);
testsuite_test_path = i_strdup(test_path);
}
void testsuite_deinit(void)
{
i_free(testsuite_test_path);
testsuite_smtp_deinit();
testsuite_binary_deinit();
testsuite_script_deinit();
testsuite_tmp_dir_deinit();
testsuite_log_deinit();
testsuite_test_context_deinit();
}
|