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