/src/pigeonhole/src/testsuite/tst-test-script-run.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "sieve-common.h" |
4 | | #include "sieve-script.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 | | #include "sieve.h" |
13 | | |
14 | | #include "testsuite-common.h" |
15 | | #include "testsuite-script.h" |
16 | | #include "testsuite-result.h" |
17 | | |
18 | | /* |
19 | | * Test_script_run command |
20 | | * |
21 | | * Syntax: |
22 | | * test_script_run |
23 | | */ |
24 | | |
25 | | static bool tst_test_script_run_registered |
26 | | (struct sieve_validator *validator, const struct sieve_extension *ext, |
27 | | struct sieve_command_registration *cmd_reg); |
28 | | static bool tst_test_script_run_generate |
29 | | (const struct sieve_codegen_env *cgenv, struct sieve_command *cmd); |
30 | | |
31 | | const struct sieve_command_def tst_test_script_run = { |
32 | | .identifier = "test_script_run", |
33 | | .type = SCT_TEST, |
34 | | .positional_args = 0, |
35 | | .subtests = 0, |
36 | | .block_allowed = FALSE, |
37 | | .block_required = FALSE, |
38 | | .registered = tst_test_script_run_registered, |
39 | | .generate = tst_test_script_run_generate |
40 | | }; |
41 | | |
42 | | /* |
43 | | * Operation |
44 | | */ |
45 | | |
46 | | static bool tst_test_script_run_operation_dump |
47 | | (const struct sieve_dumptime_env *denv, sieve_size_t *address); |
48 | | static int tst_test_script_run_operation_execute |
49 | | (const struct sieve_runtime_env *renv, sieve_size_t *address); |
50 | | |
51 | | const struct sieve_operation_def test_script_run_operation = { |
52 | | .mnemonic = "TEST_SCRIPT_RUN", |
53 | | .ext_def = &testsuite_extension, |
54 | | .code = TESTSUITE_OPERATION_TEST_SCRIPT_RUN, |
55 | | .dump = tst_test_script_run_operation_dump, |
56 | | .execute = tst_test_script_run_operation_execute |
57 | | }; |
58 | | |
59 | | /* |
60 | | * Tagged arguments |
61 | | */ |
62 | | |
63 | | /* Codes for optional arguments */ |
64 | | |
65 | | enum cmd_vacation_optional { |
66 | | OPT_END, |
67 | | OPT_APPEND_RESULT |
68 | | }; |
69 | | |
70 | | /* Tags */ |
71 | | |
72 | | static const struct sieve_argument_def append_result_tag = { |
73 | | .identifier = "append_result" |
74 | | }; |
75 | | |
76 | | static bool tst_test_script_run_registered |
77 | | (struct sieve_validator *validator, const struct sieve_extension *ext, |
78 | | struct sieve_command_registration *cmd_reg) |
79 | 0 | { |
80 | 0 | sieve_validator_register_tag |
81 | 0 | (validator, cmd_reg, ext, &append_result_tag, OPT_APPEND_RESULT); |
82 | |
|
83 | 0 | return TRUE; |
84 | 0 | } |
85 | | |
86 | | |
87 | | /* |
88 | | * Code generation |
89 | | */ |
90 | | |
91 | | static bool tst_test_script_run_generate |
92 | | (const struct sieve_codegen_env *cgenv, struct sieve_command *tst) |
93 | 0 | { |
94 | 0 | sieve_operation_emit(cgenv->sblock, tst->ext, &test_script_run_operation); |
95 | |
|
96 | 0 | return sieve_generate_arguments(cgenv, tst, NULL); |
97 | 0 | } |
98 | | |
99 | | /* |
100 | | * Code dump |
101 | | */ |
102 | | |
103 | | static bool tst_test_script_run_operation_dump |
104 | | (const struct sieve_dumptime_env *denv, sieve_size_t *address) |
105 | 0 | { |
106 | 0 | int opt_code = 0; |
107 | |
|
108 | 0 | sieve_code_dumpf(denv, "TEST_SCRIPT_RUN"); |
109 | 0 | sieve_code_descend(denv); |
110 | | |
111 | | /* Dump optional operands */ |
112 | 0 | for (;;) { |
113 | 0 | int opt; |
114 | |
|
115 | 0 | if ( (opt=sieve_opr_optional_dump(denv, address, &opt_code)) < 0 ) |
116 | 0 | return FALSE; |
117 | | |
118 | 0 | if ( opt == 0 ) break; |
119 | | |
120 | 0 | switch ( opt_code ) { |
121 | 0 | case OPT_APPEND_RESULT: |
122 | 0 | sieve_code_dumpf(denv, "append_result"); |
123 | 0 | break; |
124 | 0 | default: |
125 | 0 | return FALSE; |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | 0 | return TRUE; |
130 | 0 | } |
131 | | |
132 | | |
133 | | /* |
134 | | * Intepretation |
135 | | */ |
136 | | |
137 | | static int tst_test_script_run_operation_execute |
138 | | (const struct sieve_runtime_env *renv, sieve_size_t *address) |
139 | 0 | { |
140 | 0 | bool append_result = FALSE; |
141 | 0 | int opt_code = 0; |
142 | 0 | bool result = TRUE; |
143 | | |
144 | | /* |
145 | | * Read operands |
146 | | */ |
147 | | |
148 | | /* Optional operands */ |
149 | 0 | for (;;) { |
150 | 0 | int opt; |
151 | |
|
152 | 0 | if ( (opt=sieve_opr_optional_read(renv, address, &opt_code)) < 0 ) |
153 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
154 | | |
155 | 0 | if ( opt == 0 ) break; |
156 | | |
157 | 0 | switch ( opt_code ) { |
158 | 0 | case OPT_APPEND_RESULT: |
159 | 0 | append_result = TRUE; |
160 | 0 | break; |
161 | 0 | default: |
162 | 0 | sieve_runtime_trace_error(renv, |
163 | 0 | "unknown optional operand"); |
164 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | /* |
169 | | * Perform operation |
170 | | */ |
171 | | |
172 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS, |
173 | 0 | "testsuite: run compiled script [append_result=%s]", |
174 | 0 | ( append_result ? "yes" : "no" )); |
175 | | |
176 | | /* Reset result object */ |
177 | 0 | if ( !append_result ) |
178 | 0 | testsuite_result_reset(renv); |
179 | | |
180 | | /* Run script */ |
181 | 0 | result = testsuite_script_run(renv); |
182 | |
|
183 | 0 | if ( sieve_runtime_trace_active(renv, SIEVE_TRLVL_TESTS) ) { |
184 | 0 | sieve_runtime_trace_descend(renv); |
185 | 0 | sieve_runtime_trace(renv, 0, "execution of script %s", |
186 | 0 | ( result ? "succeeded" : "failed" )); |
187 | 0 | } |
188 | | |
189 | | /* Indicate test status */ |
190 | 0 | sieve_interpreter_set_test_result(renv->interp, result); |
191 | |
|
192 | 0 | return SIEVE_EXEC_OK; |
193 | 0 | } |
194 | | |
195 | | |
196 | | |
197 | | |