/src/pigeonhole/src/testsuite/cmd-test-mailbox.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "str-sanitize.h" |
5 | | |
6 | | #include "sieve-common.h" |
7 | | #include "sieve-commands.h" |
8 | | #include "sieve-validator.h" |
9 | | #include "sieve-generator.h" |
10 | | #include "sieve-interpreter.h" |
11 | | #include "sieve-code.h" |
12 | | #include "sieve-binary.h" |
13 | | #include "sieve-dump.h" |
14 | | #include "sieve-actions.h" |
15 | | |
16 | | #include "testsuite-common.h" |
17 | | #include "testsuite-mailstore.h" |
18 | | |
19 | | /* |
20 | | * Commands |
21 | | */ |
22 | | |
23 | | static bool |
24 | | cmd_test_mailbox_validate(struct sieve_validator *valdtr, |
25 | | struct sieve_command *cmd); |
26 | | static bool |
27 | | cmd_test_mailbox_generate(const struct sieve_codegen_env *cgenv, |
28 | | struct sieve_command *ctx); |
29 | | |
30 | | /* Test_mailbox_create command |
31 | | * |
32 | | * Syntax: |
33 | | * test_mailbox_create <mailbox: string> |
34 | | */ |
35 | | |
36 | | const struct sieve_command_def cmd_test_mailbox_create = { |
37 | | .identifier = "test_mailbox_create", |
38 | | .type = SCT_COMMAND, |
39 | | .positional_args = 1, |
40 | | .subtests = 0, |
41 | | .block_allowed = FALSE, |
42 | | .block_required = FALSE, |
43 | | .validate = cmd_test_mailbox_validate, |
44 | | .generate = cmd_test_mailbox_generate, |
45 | | }; |
46 | | |
47 | | /* Test_mailbox_delete command |
48 | | * |
49 | | * Syntax: |
50 | | * test_mailbox_create <mailbox: string> |
51 | | */ |
52 | | |
53 | | const struct sieve_command_def cmd_test_mailbox_delete = { |
54 | | .identifier = "test_mailbox_delete", |
55 | | .type = SCT_COMMAND, |
56 | | .positional_args = 1, |
57 | | .subtests = 0, |
58 | | .block_allowed = FALSE, |
59 | | .block_required = FALSE, |
60 | | .validate = cmd_test_mailbox_validate, |
61 | | .generate = cmd_test_mailbox_generate, |
62 | | }; |
63 | | |
64 | | /* |
65 | | * Operations |
66 | | */ |
67 | | |
68 | | static bool |
69 | | cmd_test_mailbox_operation_dump(const struct sieve_dumptime_env *denv, |
70 | | sieve_size_t *address); |
71 | | static int |
72 | | cmd_test_mailbox_operation_execute(const struct sieve_runtime_env *renv, |
73 | | sieve_size_t *address); |
74 | | |
75 | | /* Test_mailbox_create operation */ |
76 | | |
77 | | const struct sieve_operation_def test_mailbox_create_operation = { |
78 | | .mnemonic = "TEST_MAILBOX_CREATE", |
79 | | .ext_def = &testsuite_extension, |
80 | | .code = TESTSUITE_OPERATION_TEST_MAILBOX_CREATE, |
81 | | .dump = cmd_test_mailbox_operation_dump, |
82 | | .execute = cmd_test_mailbox_operation_execute, |
83 | | }; |
84 | | |
85 | | /* Test_mailbox_delete operation */ |
86 | | |
87 | | const struct sieve_operation_def test_mailbox_delete_operation = { |
88 | | .mnemonic = "TEST_MAILBOX_DELETE", |
89 | | .ext_def = &testsuite_extension, |
90 | | .code = TESTSUITE_OPERATION_TEST_MAILBOX_DELETE, |
91 | | .dump = cmd_test_mailbox_operation_dump, |
92 | | .execute = cmd_test_mailbox_operation_execute, |
93 | | }; |
94 | | |
95 | | /* |
96 | | * Validation |
97 | | */ |
98 | | |
99 | | static bool |
100 | | cmd_test_mailbox_validate(struct sieve_validator *valdtr, |
101 | | struct sieve_command *cmd) |
102 | 0 | { |
103 | 0 | struct sieve_ast_argument *arg = cmd->first_positional; |
104 | |
|
105 | 0 | if (!sieve_validate_positional_argument(valdtr, cmd, arg, "mailbox", 1, |
106 | 0 | SAAT_STRING)) |
107 | 0 | return FALSE; |
108 | | |
109 | 0 | if ( !sieve_validator_argument_activate(valdtr, cmd, arg, FALSE) ) |
110 | 0 | return FALSE; |
111 | | |
112 | | /* Check name validity when folder argument is not a variable */ |
113 | 0 | if ( sieve_argument_is_string_literal(arg) ) { |
114 | 0 | const char *folder = sieve_ast_argument_strc(arg), *error; |
115 | |
|
116 | 0 | if ( !sieve_mailbox_check_name(folder, &error) ) { |
117 | 0 | sieve_command_validate_error( |
118 | 0 | valdtr, cmd, "%s command: " |
119 | 0 | "invalid mailbox '%s' specified: %s", |
120 | 0 | sieve_command_identifier(cmd), |
121 | 0 | str_sanitize(folder, 256), error); |
122 | 0 | return FALSE; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | return TRUE; |
127 | 0 | } |
128 | | |
129 | | /* |
130 | | * Code generation |
131 | | */ |
132 | | |
133 | | static bool |
134 | | cmd_test_mailbox_generate(const struct sieve_codegen_env *cgenv, |
135 | | struct sieve_command *cmd) |
136 | 0 | { |
137 | | /* Emit operation */ |
138 | 0 | if (sieve_command_is(cmd, cmd_test_mailbox_create)) { |
139 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, |
140 | 0 | &test_mailbox_create_operation); |
141 | 0 | } else if (sieve_command_is(cmd, cmd_test_mailbox_delete)) { |
142 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, |
143 | 0 | &test_mailbox_delete_operation); |
144 | 0 | } else { |
145 | 0 | i_unreached(); |
146 | 0 | } |
147 | | |
148 | | /* Generate arguments */ |
149 | 0 | if (!sieve_generate_arguments(cgenv, cmd, NULL)) |
150 | 0 | return FALSE; |
151 | | |
152 | 0 | return TRUE; |
153 | 0 | } |
154 | | |
155 | | /* |
156 | | * Code dump |
157 | | */ |
158 | | |
159 | | static bool |
160 | | cmd_test_mailbox_operation_dump(const struct sieve_dumptime_env *denv, |
161 | | sieve_size_t *address) |
162 | 0 | { |
163 | 0 | sieve_code_dumpf(denv, "%s:", sieve_operation_mnemonic(denv->oprtn)); |
164 | |
|
165 | 0 | sieve_code_descend(denv); |
166 | |
|
167 | 0 | return sieve_opr_string_dump(denv, address, "mailbox"); |
168 | 0 | } |
169 | | |
170 | | /* |
171 | | * Intepretation |
172 | | */ |
173 | | |
174 | | static const char * |
175 | | cmd_test_mailbox_get_command_name(const struct sieve_operation *oprtn) |
176 | 0 | { |
177 | 0 | if (sieve_operation_is(oprtn, test_mailbox_create_operation)) |
178 | 0 | return "test_mailbox_create"; |
179 | 0 | if (sieve_operation_is(oprtn, test_mailbox_delete_operation)) |
180 | 0 | return "test_mailbox_delete"; |
181 | | |
182 | 0 | i_unreached(); |
183 | 0 | } |
184 | | |
185 | | static int |
186 | | cmd_test_mailbox_create_execute(const struct sieve_runtime_env *renv, |
187 | | const char *mailbox) |
188 | 0 | { |
189 | 0 | if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_COMMANDS)) { |
190 | 0 | sieve_runtime_trace( |
191 | 0 | renv, 0, |
192 | 0 | "testsuite/test_mailbox_create command"); |
193 | 0 | sieve_runtime_trace_descend(renv); |
194 | 0 | sieve_runtime_trace( |
195 | 0 | renv, 0, "create mailbox '%s'", mailbox); |
196 | 0 | } |
197 | |
|
198 | 0 | testsuite_mailstore_mailbox_create(renv, mailbox); |
199 | 0 | return SIEVE_EXEC_OK; |
200 | 0 | } |
201 | | |
202 | | static int |
203 | | cmd_test_mailbox_delete_execute(const struct sieve_runtime_env *renv, |
204 | | const char *mailbox) |
205 | 0 | { |
206 | 0 | if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_COMMANDS)) { |
207 | 0 | sieve_runtime_trace( |
208 | 0 | renv, 0, |
209 | 0 | "testsuite/test_mailbox_delete command"); |
210 | 0 | sieve_runtime_trace_descend(renv); |
211 | 0 | sieve_runtime_trace( |
212 | 0 | renv, 0, "delete mailbox '%s'", mailbox); |
213 | 0 | } |
214 | | |
215 | | /* FIXME: implement */ |
216 | 0 | return testsuite_test_failf( |
217 | 0 | renv, "test_mailbox_delete: NOT IMPLEMENTED"); |
218 | 0 | } |
219 | | |
220 | | static int |
221 | | cmd_test_mailbox_operation_execute(const struct sieve_runtime_env *renv, |
222 | | sieve_size_t *address) |
223 | 0 | { |
224 | 0 | const struct sieve_operation *oprtn = renv->oprtn; |
225 | 0 | string_t *mailbox = NULL; |
226 | 0 | const char *error; |
227 | 0 | int ret; |
228 | | |
229 | | /* |
230 | | * Read operands |
231 | | */ |
232 | | |
233 | | /* Mailbox */ |
234 | |
|
235 | 0 | ret = sieve_opr_string_read(renv, address, "mailbox", &mailbox); |
236 | 0 | if (ret <= 0) |
237 | 0 | return ret; |
238 | | |
239 | 0 | if (!sieve_mailbox_check_name(str_c(mailbox), &error)) { |
240 | 0 | sieve_runtime_error( |
241 | 0 | renv, NULL, "%s command: " |
242 | 0 | "invalid mailbox '%s' specified: %s", |
243 | 0 | cmd_test_mailbox_get_command_name(oprtn), |
244 | 0 | str_c(mailbox), error); |
245 | 0 | return SIEVE_EXEC_FAILURE; |
246 | 0 | } |
247 | | |
248 | | /* |
249 | | * Perform operation |
250 | | */ |
251 | | |
252 | 0 | if (sieve_operation_is(oprtn, test_mailbox_create_operation)) |
253 | 0 | ret = cmd_test_mailbox_create_execute(renv, str_c(mailbox)); |
254 | 0 | else if (sieve_operation_is(oprtn, test_mailbox_delete_operation)) |
255 | 0 | ret = cmd_test_mailbox_delete_execute(renv, str_c(mailbox)); |
256 | 0 | else |
257 | 0 | i_unreached(); |
258 | | |
259 | 0 | return ret; |
260 | 0 | } |