/src/pigeonhole/src/testsuite/tst-test-multiscript.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-stringlist.h" |
11 | | #include "sieve-code.h" |
12 | | #include "sieve-binary.h" |
13 | | #include "sieve-dump.h" |
14 | | #include "sieve.h" |
15 | | |
16 | | #include "testsuite-common.h" |
17 | | #include "testsuite-script.h" |
18 | | |
19 | | /* |
20 | | * Test_multiscript command |
21 | | * |
22 | | * Syntax: |
23 | | * test_multiscript <scripts: string-list> |
24 | | */ |
25 | | |
26 | | static bool tst_test_multiscript_validate |
27 | | (struct sieve_validator *validator, struct sieve_command *cmd); |
28 | | static bool tst_test_multiscript_generate |
29 | | (const struct sieve_codegen_env *cgenv, struct sieve_command *tst); |
30 | | |
31 | | const struct sieve_command_def tst_test_multiscript = { |
32 | | .identifier = "test_multiscript", |
33 | | .type = SCT_TEST, |
34 | | .positional_args = 1, |
35 | | .subtests = 0, |
36 | | .block_allowed = FALSE, |
37 | | .block_required = FALSE, |
38 | | .validate = tst_test_multiscript_validate, |
39 | | .generate = tst_test_multiscript_generate, |
40 | | }; |
41 | | |
42 | | /* |
43 | | * Operation |
44 | | */ |
45 | | |
46 | | static bool tst_test_multiscript_operation_dump |
47 | | (const struct sieve_dumptime_env *denv, sieve_size_t *address); |
48 | | static int tst_test_multiscript_operation_execute |
49 | | (const struct sieve_runtime_env *renv, sieve_size_t *address); |
50 | | |
51 | | const struct sieve_operation_def test_multiscript_operation = { |
52 | | .mnemonic = "TEST_MULTISCRIPT", |
53 | | .ext_def = &testsuite_extension, |
54 | | .code = TESTSUITE_OPERATION_TEST_MULTISCRIPT, |
55 | | .dump = tst_test_multiscript_operation_dump, |
56 | | .execute = tst_test_multiscript_operation_execute |
57 | | }; |
58 | | |
59 | | /* |
60 | | * Validation |
61 | | */ |
62 | | |
63 | | static bool tst_test_multiscript_validate |
64 | | (struct sieve_validator *valdtr, struct sieve_command *tst) |
65 | 0 | { |
66 | 0 | struct sieve_ast_argument *arg = tst->first_positional; |
67 | |
|
68 | 0 | if ( !sieve_validate_positional_argument |
69 | 0 | (valdtr, tst, arg, "scripts", 1, SAAT_STRING_LIST) ) { |
70 | 0 | return FALSE; |
71 | 0 | } |
72 | | |
73 | 0 | return sieve_validator_argument_activate(valdtr, tst, arg, FALSE); |
74 | 0 | } |
75 | | |
76 | | /* |
77 | | * Code generation |
78 | | */ |
79 | | |
80 | | static bool tst_test_multiscript_generate |
81 | | (const struct sieve_codegen_env *cgenv, struct sieve_command *tst) |
82 | 0 | { |
83 | 0 | sieve_operation_emit(cgenv->sblock, tst->ext, &test_multiscript_operation); |
84 | | |
85 | | /* Generate arguments */ |
86 | 0 | return sieve_generate_arguments(cgenv, tst, NULL); |
87 | 0 | } |
88 | | |
89 | | /* |
90 | | * Code dump |
91 | | */ |
92 | | |
93 | | static bool tst_test_multiscript_operation_dump |
94 | | (const struct sieve_dumptime_env *denv, sieve_size_t *address) |
95 | 0 | { |
96 | 0 | sieve_code_dumpf(denv, "TEST_MULTISCRIPT:"); |
97 | 0 | sieve_code_descend(denv); |
98 | |
|
99 | 0 | if ( !sieve_opr_stringlist_dump(denv, address, "scripts") ) |
100 | 0 | return FALSE; |
101 | | |
102 | 0 | return TRUE; |
103 | 0 | } |
104 | | |
105 | | /* |
106 | | * Intepretation |
107 | | */ |
108 | | |
109 | | static int tst_test_multiscript_operation_execute |
110 | | (const struct sieve_runtime_env *renv, sieve_size_t *address) |
111 | 0 | { |
112 | 0 | struct sieve_stringlist *scripts_list; |
113 | 0 | string_t *script_name; |
114 | 0 | ARRAY_TYPE (const_string) scriptfiles; |
115 | 0 | bool result = TRUE; |
116 | 0 | int ret; |
117 | | |
118 | | /* |
119 | | * Read operands |
120 | | */ |
121 | |
|
122 | 0 | if ( (ret=sieve_opr_stringlist_read(renv, address, "scripts", &scripts_list)) |
123 | 0 | <= 0 ) |
124 | 0 | return ret; |
125 | | |
126 | | /* |
127 | | * Perform operation |
128 | | */ |
129 | | |
130 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS, |
131 | 0 | "testsuite: test_multiscript test"); |
132 | 0 | sieve_runtime_trace_descend(renv); |
133 | |
|
134 | 0 | t_array_init(&scriptfiles, 16); |
135 | |
|
136 | 0 | script_name = NULL; |
137 | 0 | while ( result && |
138 | 0 | (ret=sieve_stringlist_next_item(scripts_list, &script_name)) > 0 ) { |
139 | 0 | const char *script = t_strdup(str_c(script_name)); |
140 | |
|
141 | 0 | array_append(&scriptfiles, &script, 1); |
142 | 0 | } |
143 | |
|
144 | 0 | result = result && (ret >= 0) && |
145 | 0 | testsuite_script_multiscript(renv, &scriptfiles); |
146 | | |
147 | | /* Set result */ |
148 | 0 | sieve_interpreter_set_test_result(renv->interp, result); |
149 | |
|
150 | 0 | return SIEVE_EXEC_OK; |
151 | 0 | } |
152 | | |
153 | | |
154 | | |
155 | | |