/src/pigeonhole/src/testsuite/cmd-test-fail.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "sieve-common.h" |
4 | | #include "sieve-commands.h" |
5 | | #include "sieve-validator.h" |
6 | | #include "sieve-generator.h" |
7 | | #include "sieve-interpreter.h" |
8 | | #include "sieve-code.h" |
9 | | #include "sieve-binary.h" |
10 | | #include "sieve-dump.h" |
11 | | |
12 | | #include "testsuite-common.h" |
13 | | |
14 | | /* |
15 | | * Test_fail command |
16 | | * |
17 | | * Syntax: |
18 | | * test_fail <reason: string> |
19 | | */ |
20 | | |
21 | | static bool |
22 | | cmd_test_fail_validate(struct sieve_validator *valdtr, |
23 | | struct sieve_command *cmd); |
24 | | static bool |
25 | | cmd_test_fail_generate(const struct sieve_codegen_env *cgenv, |
26 | | struct sieve_command *ctx); |
27 | | |
28 | | const struct sieve_command_def cmd_test_fail = { |
29 | | .identifier = "test_fail", |
30 | | .type = SCT_COMMAND, |
31 | | .positional_args = 1, |
32 | | .subtests = 0, |
33 | | .block_allowed = FALSE, |
34 | | .block_required = FALSE, |
35 | | .validate = cmd_test_fail_validate, |
36 | | .generate = cmd_test_fail_generate, |
37 | | }; |
38 | | |
39 | | /* |
40 | | * Test operation |
41 | | */ |
42 | | |
43 | | static bool |
44 | | cmd_test_fail_operation_dump(const struct sieve_dumptime_env *denv, |
45 | | sieve_size_t *address); |
46 | | static int |
47 | | cmd_test_fail_operation_execute(const struct sieve_runtime_env *renv, |
48 | | sieve_size_t *address); |
49 | | |
50 | | const struct sieve_operation_def test_fail_operation = { |
51 | | .mnemonic = "TEST_FAIL", |
52 | | .ext_def = &testsuite_extension, |
53 | | .code = TESTSUITE_OPERATION_TEST_FAIL, |
54 | | .dump = cmd_test_fail_operation_dump, |
55 | | .execute = cmd_test_fail_operation_execute, |
56 | | }; |
57 | | |
58 | | /* |
59 | | * Validation |
60 | | */ |
61 | | |
62 | | static bool |
63 | | cmd_test_fail_validate(struct sieve_validator *valdtr ATTR_UNUSED, |
64 | | struct sieve_command *cmd) |
65 | 0 | { |
66 | 0 | struct sieve_ast_argument *arg = cmd->first_positional; |
67 | |
|
68 | 0 | if (!sieve_validate_positional_argument(valdtr, cmd, arg, "reason", 1, |
69 | 0 | SAAT_STRING)) |
70 | 0 | return FALSE; |
71 | | |
72 | 0 | return sieve_validator_argument_activate(valdtr, cmd, arg, FALSE); |
73 | 0 | } |
74 | | |
75 | | /* |
76 | | * Code generation |
77 | | */ |
78 | | |
79 | | static bool |
80 | | cmd_test_fail_generate(const struct sieve_codegen_env *cgenv, |
81 | | struct sieve_command *cmd) |
82 | 0 | { |
83 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, &test_fail_operation); |
84 | | |
85 | | /* Generate arguments */ |
86 | 0 | if (!sieve_generate_arguments(cgenv, cmd, NULL)) |
87 | 0 | return FALSE; |
88 | | |
89 | 0 | return TRUE; |
90 | 0 | } |
91 | | |
92 | | /* |
93 | | * Code dump |
94 | | */ |
95 | | |
96 | | static bool |
97 | | cmd_test_fail_operation_dump(const struct sieve_dumptime_env *denv, |
98 | | sieve_size_t *address) |
99 | 0 | { |
100 | 0 | sieve_code_dumpf(denv, "TEST_FAIL:"); |
101 | 0 | sieve_code_descend(denv); |
102 | |
|
103 | 0 | if (!sieve_opr_string_dump(denv, address, "reason")) |
104 | 0 | return FALSE; |
105 | | |
106 | 0 | return TRUE; |
107 | 0 | } |
108 | | |
109 | | /* |
110 | | * Intepretation |
111 | | */ |
112 | | |
113 | | static int |
114 | | cmd_test_fail_operation_execute(const struct sieve_runtime_env *renv, |
115 | | sieve_size_t *address) |
116 | 0 | { |
117 | 0 | string_t *reason; |
118 | 0 | int ret; |
119 | |
|
120 | 0 | ret = sieve_opr_string_read(renv, address, "reason", &reason); |
121 | 0 | if (ret <= 0) |
122 | 0 | return ret; |
123 | | |
124 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_COMMANDS, "testsuite: " |
125 | 0 | "test_fail command; FAIL current test"); |
126 | |
|
127 | 0 | return testsuite_test_fail(renv, reason); |
128 | 0 | } |