/src/pigeonhole/src/testsuite/cmd-test-binary.c
Line | Count | Source |
1 | | /* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file |
2 | | */ |
3 | | |
4 | | #include "sieve-common.h" |
5 | | #include "sieve-commands.h" |
6 | | #include "sieve-validator.h" |
7 | | #include "sieve-generator.h" |
8 | | #include "sieve-interpreter.h" |
9 | | #include "sieve-code.h" |
10 | | #include "sieve-binary.h" |
11 | | #include "sieve-dump.h" |
12 | | |
13 | | #include "testsuite-common.h" |
14 | | #include "testsuite-binary.h" |
15 | | #include "testsuite-script.h" |
16 | | |
17 | | /* |
18 | | * Commands |
19 | | */ |
20 | | |
21 | | static bool |
22 | | cmd_test_binary_validate(struct sieve_validator *valdtr, |
23 | | struct sieve_command *cmd); |
24 | | static bool |
25 | | cmd_test_binary_generate(const struct sieve_codegen_env *cgenv, |
26 | | struct sieve_command *ctx); |
27 | | |
28 | | /* Test_binary_load command |
29 | | * |
30 | | * Syntax: |
31 | | * test_binary_load <binary-name: string> |
32 | | */ |
33 | | |
34 | | const struct sieve_command_def cmd_test_binary_load = { |
35 | | .identifier = "test_binary_load", |
36 | | .type = SCT_COMMAND, |
37 | | .positional_args = 1, |
38 | | .subtests = 0, |
39 | | .block_allowed = FALSE, |
40 | | .block_required = FALSE, |
41 | | .validate = cmd_test_binary_validate, |
42 | | .generate = cmd_test_binary_generate, |
43 | | }; |
44 | | |
45 | | /* Test_binary_save command |
46 | | * |
47 | | * Syntax: |
48 | | * test_binary_save <binary-name: string> |
49 | | */ |
50 | | |
51 | | const struct sieve_command_def cmd_test_binary_save = { |
52 | | .identifier = "test_binary_save", |
53 | | .type = SCT_COMMAND, |
54 | | .positional_args = 1, |
55 | | .subtests = 0, |
56 | | .block_allowed = FALSE, |
57 | | .block_required = FALSE, |
58 | | .validate = cmd_test_binary_validate, |
59 | | .generate = cmd_test_binary_generate, |
60 | | }; |
61 | | |
62 | | /* |
63 | | * Operations |
64 | | */ |
65 | | |
66 | | static bool |
67 | | cmd_test_binary_operation_dump(const struct sieve_dumptime_env *denv, |
68 | | sieve_size_t *address); |
69 | | static int |
70 | | cmd_test_binary_operation_execute(const struct sieve_runtime_env *renv, |
71 | | sieve_size_t *address); |
72 | | |
73 | | /* test_binary_create operation */ |
74 | | |
75 | | const struct sieve_operation_def test_binary_load_operation = { |
76 | | .mnemonic = "TEST_BINARY_LOAD", |
77 | | .ext_def = &testsuite_extension, |
78 | | .code = TESTSUITE_OPERATION_TEST_BINARY_LOAD, |
79 | | .dump = cmd_test_binary_operation_dump, |
80 | | .execute = cmd_test_binary_operation_execute, |
81 | | }; |
82 | | |
83 | | /* test_binary_delete operation */ |
84 | | |
85 | | const struct sieve_operation_def test_binary_save_operation = { |
86 | | .mnemonic = "TEST_BINARY_SAVE", |
87 | | .ext_def = &testsuite_extension, |
88 | | .code = TESTSUITE_OPERATION_TEST_BINARY_SAVE, |
89 | | .dump = cmd_test_binary_operation_dump, |
90 | | .execute = cmd_test_binary_operation_execute, |
91 | | }; |
92 | | |
93 | | /* |
94 | | * Validation |
95 | | */ |
96 | | |
97 | | static bool |
98 | | cmd_test_binary_validate(struct sieve_validator *valdtr, |
99 | | struct sieve_command *cmd) |
100 | 0 | { |
101 | 0 | struct sieve_ast_argument *arg = cmd->first_positional; |
102 | |
|
103 | 0 | if (!sieve_validate_positional_argument(valdtr, cmd, arg, "binary-name", |
104 | 0 | 1, SAAT_STRING)) |
105 | 0 | return FALSE; |
106 | | |
107 | 0 | return sieve_validator_argument_activate(valdtr, cmd, arg, FALSE); |
108 | 0 | } |
109 | | |
110 | | /* |
111 | | * Code generation |
112 | | */ |
113 | | |
114 | | static bool |
115 | | cmd_test_binary_generate(const struct sieve_codegen_env *cgenv, |
116 | | struct sieve_command *cmd) |
117 | 0 | { |
118 | | /* Emit operation */ |
119 | 0 | if (sieve_command_is(cmd, cmd_test_binary_load)) { |
120 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, |
121 | 0 | &test_binary_load_operation); |
122 | 0 | } else if (sieve_command_is(cmd, cmd_test_binary_save)) { |
123 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, |
124 | 0 | &test_binary_save_operation); |
125 | 0 | } else { |
126 | 0 | i_unreached(); |
127 | 0 | } |
128 | | |
129 | | /* Generate arguments */ |
130 | 0 | if (!sieve_generate_arguments(cgenv, cmd, NULL)) |
131 | 0 | return FALSE; |
132 | | |
133 | 0 | return TRUE; |
134 | 0 | } |
135 | | |
136 | | /* |
137 | | * Code dump |
138 | | */ |
139 | | |
140 | | static bool |
141 | | cmd_test_binary_operation_dump(const struct sieve_dumptime_env *denv, |
142 | | sieve_size_t *address) |
143 | 0 | { |
144 | 0 | sieve_code_dumpf(denv, "%s:", sieve_operation_mnemonic(denv->oprtn)); |
145 | |
|
146 | 0 | sieve_code_descend(denv); |
147 | |
|
148 | 0 | return sieve_opr_string_dump(denv, address, "binary-name"); |
149 | 0 | } |
150 | | |
151 | | /* |
152 | | * Intepretation |
153 | | */ |
154 | | |
155 | | static int |
156 | | cmd_test_binary_operation_execute(const struct sieve_runtime_env *renv, |
157 | | sieve_size_t *address) |
158 | 0 | { |
159 | 0 | const struct sieve_operation *oprtn = renv->oprtn; |
160 | 0 | string_t *binary_name = NULL; |
161 | 0 | int ret; |
162 | | |
163 | | /* |
164 | | * Read operands |
165 | | */ |
166 | | |
167 | | /* Binary Name */ |
168 | |
|
169 | 0 | ret = sieve_opr_string_read(renv, address, "binary-name", &binary_name); |
170 | 0 | if (ret <= 0 ) |
171 | 0 | return ret; |
172 | | |
173 | | /* |
174 | | * Perform operation |
175 | | */ |
176 | | |
177 | 0 | if (sieve_operation_is(oprtn, test_binary_load_operation)) { |
178 | 0 | struct sieve_binary *sbin = |
179 | 0 | testsuite_binary_load(str_c(binary_name)); |
180 | |
|
181 | 0 | if (sieve_runtime_trace_active(renv, SIEVE_TRLVL_COMMANDS)) { |
182 | 0 | sieve_runtime_trace(renv, 0, "testsuite: " |
183 | 0 | "test_binary_load command"); |
184 | 0 | sieve_runtime_trace_descend(renv); |
185 | 0 | sieve_runtime_trace(renv, 0, "load binary '%s'", |
186 | 0 | str_c(binary_name)); |
187 | 0 | } |
188 | |
|
189 | 0 | if ( sbin != NULL ) { |
190 | 0 | testsuite_script_set_binary(renv, sbin); |
191 | |
|
192 | 0 | sieve_binary_unref(&sbin); |
193 | 0 | } else { |
194 | 0 | e_error(testsuite_sieve_instance->event, |
195 | 0 | "failed to load binary %s", str_c(binary_name)); |
196 | 0 | return SIEVE_EXEC_FAILURE; |
197 | 0 | } |
198 | 0 | } else if ( sieve_operation_is(oprtn, test_binary_save_operation) ) { |
199 | 0 | struct sieve_binary *sbin = testsuite_script_get_binary(renv); |
200 | |
|
201 | 0 | if ( sieve_runtime_trace_active(renv, SIEVE_TRLVL_COMMANDS) ) { |
202 | 0 | sieve_runtime_trace(renv, 0, "testsuite: " |
203 | 0 | "test_binary_save command"); |
204 | 0 | sieve_runtime_trace_descend(renv); |
205 | 0 | sieve_runtime_trace(renv, 0, "save binary '%s'", |
206 | 0 | str_c(binary_name)); |
207 | 0 | } |
208 | |
|
209 | 0 | if ( sbin != NULL ) |
210 | 0 | testsuite_binary_save(sbin, str_c(binary_name)); |
211 | 0 | else { |
212 | 0 | e_error(testsuite_sieve_instance->event, |
213 | 0 | "no compiled binary to save as %s", |
214 | 0 | str_c(binary_name)); |
215 | 0 | return SIEVE_EXEC_FAILURE; |
216 | 0 | } |
217 | 0 | } else { |
218 | 0 | i_unreached(); |
219 | 0 | } |
220 | | |
221 | 0 | return SIEVE_EXEC_OK; |
222 | 0 | } |