/src/pigeonhole/src/lib-sieve/plugins/enotify/tst-valid-notify-method.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-stringlist.h" |
7 | | #include "sieve-code.h" |
8 | | #include "sieve-comparators.h" |
9 | | #include "sieve-match-types.h" |
10 | | #include "sieve-validator.h" |
11 | | #include "sieve-generator.h" |
12 | | #include "sieve-interpreter.h" |
13 | | #include "sieve-dump.h" |
14 | | #include "sieve-match.h" |
15 | | |
16 | | #include "ext-enotify-common.h" |
17 | | |
18 | | /* |
19 | | * Valid_notify_method test |
20 | | * |
21 | | * Syntax: |
22 | | * valid_notify_method <notification-uris: string-list> |
23 | | */ |
24 | | |
25 | | static bool tst_vnotifym_validate |
26 | | (struct sieve_validator *valdtr, struct sieve_command *tst); |
27 | | static bool tst_vnotifym_generate |
28 | | (const struct sieve_codegen_env *cgenv, struct sieve_command *ctx); |
29 | | |
30 | | const struct sieve_command_def valid_notify_method_test = { |
31 | | .identifier = "valid_notify_method", |
32 | | .type = SCT_TEST, |
33 | | .positional_args = 1, |
34 | | .subtests = 0, |
35 | | .block_allowed = FALSE, |
36 | | .block_required = FALSE, |
37 | | .validate = tst_vnotifym_validate, |
38 | | .generate = tst_vnotifym_generate |
39 | | }; |
40 | | |
41 | | /* |
42 | | * Valid_notify_method operation |
43 | | */ |
44 | | |
45 | | static bool tst_vnotifym_operation_dump |
46 | | (const struct sieve_dumptime_env *denv, sieve_size_t *address); |
47 | | static int tst_vnotifym_operation_execute |
48 | | (const struct sieve_runtime_env *renv, sieve_size_t *address); |
49 | | |
50 | | const struct sieve_operation_def valid_notify_method_operation = { |
51 | | .mnemonic = "VALID_NOTIFY_METHOD", |
52 | | .ext_def = &enotify_extension, |
53 | | .code = EXT_ENOTIFY_OPERATION_VALID_NOTIFY_METHOD, |
54 | | .dump = tst_vnotifym_operation_dump, |
55 | | .execute = tst_vnotifym_operation_execute |
56 | | }; |
57 | | |
58 | | /* |
59 | | * Test validation |
60 | | */ |
61 | | |
62 | | static bool tst_vnotifym_validate |
63 | | (struct sieve_validator *valdtr, struct sieve_command *tst) |
64 | 0 | { |
65 | 0 | struct sieve_ast_argument *arg = tst->first_positional; |
66 | |
|
67 | 0 | if ( !sieve_validate_positional_argument |
68 | 0 | (valdtr, tst, arg, "notification-uris", 1, SAAT_STRING_LIST) ) { |
69 | 0 | return FALSE; |
70 | 0 | } |
71 | | |
72 | 0 | return sieve_validator_argument_activate(valdtr, tst, arg, FALSE); |
73 | 0 | } |
74 | | |
75 | | /* |
76 | | * Test generation |
77 | | */ |
78 | | |
79 | | static bool tst_vnotifym_generate |
80 | | (const struct sieve_codegen_env *cgenv, struct sieve_command *cmd) |
81 | 0 | { |
82 | 0 | sieve_operation_emit(cgenv->sblock, cmd->ext, &valid_notify_method_operation); |
83 | | |
84 | | /* Generate arguments */ |
85 | 0 | return sieve_generate_arguments(cgenv, cmd, NULL); |
86 | 0 | } |
87 | | |
88 | | /* |
89 | | * Code dump |
90 | | */ |
91 | | |
92 | | static bool tst_vnotifym_operation_dump |
93 | | (const struct sieve_dumptime_env *denv, sieve_size_t *address) |
94 | 0 | { |
95 | 0 | sieve_code_dumpf(denv, "VALID_NOTIFY_METHOD"); |
96 | 0 | sieve_code_descend(denv); |
97 | |
|
98 | 0 | return |
99 | 0 | sieve_opr_stringlist_dump(denv, address, "notify-uris"); |
100 | 0 | } |
101 | | |
102 | | /* |
103 | | * Code execution |
104 | | */ |
105 | | |
106 | | static int tst_vnotifym_operation_execute |
107 | | (const struct sieve_runtime_env *renv, sieve_size_t *address) |
108 | 0 | { |
109 | 0 | struct sieve_stringlist *notify_uris; |
110 | 0 | string_t *uri_item; |
111 | 0 | bool all_valid = TRUE; |
112 | 0 | int ret; |
113 | | |
114 | | /* |
115 | | * Read operands |
116 | | */ |
117 | | |
118 | | /* Read notify uris */ |
119 | 0 | if ( (ret=sieve_opr_stringlist_read |
120 | 0 | (renv, address, "notify-uris", ¬ify_uris)) <= 0 ) |
121 | 0 | return ret; |
122 | | |
123 | | /* |
124 | | * Perform operation |
125 | | */ |
126 | | |
127 | 0 | sieve_runtime_trace(renv, SIEVE_TRLVL_TESTS, "valid_notify_method test"); |
128 | |
|
129 | 0 | uri_item = NULL; |
130 | 0 | while ( (ret=sieve_stringlist_next_item(notify_uris, &uri_item)) > 0 ) { |
131 | 0 | if ( !ext_enotify_runtime_method_validate(renv, uri_item) ) { |
132 | 0 | all_valid = FALSE; |
133 | 0 | break; |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | 0 | if ( ret < 0 ) { |
138 | 0 | sieve_runtime_trace_error(renv, "invalid method uri item"); |
139 | 0 | return SIEVE_EXEC_BIN_CORRUPT; |
140 | 0 | } |
141 | | |
142 | 0 | sieve_interpreter_set_test_result(renv->interp, all_valid); |
143 | 0 | return SIEVE_EXEC_OK; |
144 | 0 | } |