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
|
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "ostream.h"
#include "str.h"
#include "sieve-common.h"
#include "sieve-error.h"
#include "sieve-stringlist.h"
#include "sieve-actions.h"
#include "sieve-interpreter.h"
#include "sieve-result.h"
#include "testsuite-common.h"
#include "testsuite-log.h"
#include "testsuite-message.h"
#include "testsuite-mailstore.h"
#include "testsuite-result.h"
struct sieve_execute_env testsuite_execute_env;
static pool_t testsuite_execute_pool = NULL;
static struct sieve_result *_testsuite_result = NULL;
static struct sieve_result_execution *_testsuite_rexec = NULL;
void testsuite_result_init(void)
{
struct sieve_instance *svinst = testsuite_sieve_instance;
testsuite_execute_pool = pool_alloconly_create("sieve execution", 4096);
sieve_execute_init(&testsuite_execute_env, testsuite_sieve_instance,
testsuite_execute_pool, &testsuite_msgdata,
testsuite_scriptenv, 0);
_testsuite_result = sieve_result_create(svinst, testsuite_execute_pool,
&testsuite_execute_env);
}
void testsuite_result_deinit(void)
{
sieve_result_execution_destroy(&_testsuite_rexec);
if (_testsuite_result != NULL)
sieve_result_unref(&_testsuite_result);
sieve_execute_deinit(&testsuite_execute_env);
pool_unref(&testsuite_execute_pool);
}
void testsuite_result_reset(const struct sieve_runtime_env *renv)
{
struct sieve_instance *svinst = testsuite_sieve_instance;
if (_testsuite_result != NULL) {
sieve_result_execution_destroy(&_testsuite_rexec);
sieve_result_unref(&_testsuite_result);
pool_unref(&testsuite_execute_pool);
}
testsuite_message_flush();
testsuite_mailstore_flush();
i_zero(testsuite_execute_env.exec_status);
testsuite_execute_pool = pool_alloconly_create("sieve execution", 4096);
_testsuite_result = sieve_result_create(svinst, testsuite_execute_pool,
&testsuite_execute_env);
sieve_interpreter_set_result(renv->interp, _testsuite_result);
}
struct sieve_result *testsuite_result_get(void)
{
return _testsuite_result;
}
struct sieve_result_iterate_context *testsuite_result_iterate_init(void)
{
if (_testsuite_result == NULL)
return NULL;
return sieve_result_iterate_init(_testsuite_result);
}
bool testsuite_result_execute(const struct sieve_runtime_env *renv)
{
int ret;
if (_testsuite_result == NULL) {
sieve_runtime_error(renv, NULL, "testsuite: "
"trying to execute result, "
"but no result evaluated yet");
return FALSE;
}
testsuite_log_clear_messages();
if (_testsuite_rexec == NULL) {
_testsuite_rexec = sieve_result_execution_create(
_testsuite_result, testsuite_execute_pool);
}
/* Execute the result */
ret = sieve_result_execute(_testsuite_rexec, SIEVE_EXEC_OK, TRUE,
testsuite_log_ehandler, NULL);
return (ret > 0);
}
void testsuite_result_print(const struct sieve_runtime_env *renv)
{
const struct sieve_execute_env *eenv = renv->exec_env;
struct ostream *out;
out = o_stream_create_fd(1, 0);
o_stream_set_no_error_handling(out, TRUE);
o_stream_nsend_str(out, "\n--");
sieve_result_print(_testsuite_result, eenv->scriptenv, out, NULL);
o_stream_nsend_str(out, "--\n\n");
o_stream_destroy(&out);
}
/*
* Result stringlist
*/
/* Forward declarations */
static int
testsuite_result_stringlist_next_item(struct sieve_stringlist *_strlist,
string_t **str_r);
static void
testsuite_result_stringlist_reset(struct sieve_stringlist *_strlist);
/* Stringlist object */
struct testsuite_result_stringlist {
struct sieve_stringlist strlist;
struct sieve_result_iterate_context *result_iter;
int pos, index;
};
struct sieve_stringlist *
testsuite_result_stringlist_create(const struct sieve_runtime_env *renv,
int index)
{
struct testsuite_result_stringlist *strlist;
strlist = t_new(struct testsuite_result_stringlist, 1);
strlist->strlist.runenv = renv;
strlist->strlist.exec_status = SIEVE_EXEC_OK;
strlist->strlist.next_item = testsuite_result_stringlist_next_item;
strlist->strlist.reset = testsuite_result_stringlist_reset;
strlist->result_iter = testsuite_result_iterate_init();
strlist->index = index;
strlist->pos = 0;
return &strlist->strlist;
}
static int
testsuite_result_stringlist_next_item(struct sieve_stringlist *_strlist,
string_t **str_r)
{
struct testsuite_result_stringlist *strlist =
(struct testsuite_result_stringlist *)_strlist;
const struct sieve_action *action;
const char *act_name;
bool keep;
*str_r = NULL;
if (strlist->index > 0 && strlist->pos > 0)
return 0;
do {
if ((action = sieve_result_iterate_next(strlist->result_iter,
&keep)) == NULL)
return 0;
strlist->pos++;
} while (strlist->pos < strlist->index);
if (keep)
act_name = "keep";
else {
act_name = ((action == NULL || action->def == NULL ||
action->def->name == NULL) ?
"" : action->def->name);
}
*str_r = t_str_new_const(act_name, strlen(act_name));
return 1;
}
static void testsuite_result_stringlist_reset(struct sieve_stringlist *_strlist)
{
struct testsuite_result_stringlist *strlist =
(struct testsuite_result_stringlist *)_strlist;
strlist->result_iter = testsuite_result_iterate_init();
strlist->pos = 0;
}
|