Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as
7 : published by the Free Software Foundation; either version 3, or
8 : (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful, but
11 : WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 :
20 : /**
21 : * @file src/testing/testing_api_cmd_checkserver.c
22 : * @brief Implement a CMD to run an Checkserver service for faking the legitimation service
23 : * @author Priscilla HUANG
24 : */
25 : #include "platform.h"
26 : #include "taler/taler_json_lib.h"
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler/taler_testing_lib.h"
29 : #include "taler/taler_mhd_lib.h"
30 : #include "taler/taler_merchant_testing_lib.h"
31 : #include "taler/taler_merchant_service.h"
32 : #include <taler/taler_exchange_service.h>
33 :
34 :
35 : /**
36 : * State for a "checkserver" CMD.
37 : */
38 : struct CheckState
39 : {
40 : /**
41 : * Handle to the "testserver" service.
42 : */
43 : struct MHD_Daemon *mhd;
44 :
45 : /**
46 : * Our interpreter.
47 : */
48 : struct TALER_TESTING_Interpreter *is;
49 :
50 : /**
51 : * Index to know which web server we check.
52 : */
53 : unsigned int index;
54 :
55 : /**
56 : * Reference to command to the previous set server status operation.
57 : */
58 : const char *ref_operation;
59 :
60 : /**
61 : * Expected method of the pending webhook.
62 : */
63 : const char *expected_method;
64 :
65 : /**
66 : * Expected url of the pending webhook.
67 : */
68 : const char *expected_url;
69 :
70 : /**
71 : * Expected header of the pending webhook.
72 : */
73 : const char *expected_header;
74 :
75 : /**
76 : * Expected body of the pending webhook.
77 : */
78 : const char *expected_body;
79 :
80 : };
81 :
82 :
83 : /**
84 : * Run the command.
85 : *
86 : * @param cls closure.
87 : * @param cmd the command to execute.
88 : * @param is the interpreter state.
89 : */
90 : static void
91 10 : checkserver_run (void *cls,
92 : const struct TALER_TESTING_Command *cmd,
93 : struct TALER_TESTING_Interpreter *is)
94 : {
95 10 : struct CheckState *cs = cls;
96 : const struct TALER_TESTING_Command *ref;
97 : const char *url;
98 : const char *http_method;
99 : const char *header;
100 : const void *body;
101 : const size_t *body_size;
102 :
103 : (void) cmd;
104 10 : cs->is = is;
105 10 : ref = TALER_TESTING_interpreter_lookup_command (is,
106 : cs->ref_operation);
107 10 : if (NULL == ref)
108 : {
109 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 : "ref NULL\n");
111 0 : GNUNET_break (0);
112 0 : TALER_TESTING_interpreter_fail (is);
113 0 : return;
114 : }
115 10 : if (GNUNET_OK !=
116 10 : TALER_TESTING_get_trait_urls (ref,
117 : cs->index,
118 : &url))
119 : {
120 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121 : "Trait url does not work\n");
122 0 : GNUNET_break (0);
123 0 : TALER_TESTING_interpreter_fail (is);
124 0 : return;
125 : }
126 10 : if (NULL == url)
127 : {
128 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 : "Trait for url is NULL!?\n");
130 0 : GNUNET_break (0);
131 0 : TALER_TESTING_interpreter_fail (is);
132 0 : return;
133 : }
134 10 : if (0 != strcmp (cs->expected_url,
135 : url))
136 : {
137 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138 : "URL does not match: `%s' != `%s'\n",
139 : cs->expected_url,
140 : url);
141 0 : TALER_TESTING_interpreter_fail (is);
142 0 : return;
143 : }
144 10 : if (GNUNET_OK !=
145 10 : TALER_TESTING_get_trait_http_methods (ref,
146 : cs->index,
147 : &http_method))
148 : {
149 0 : TALER_TESTING_interpreter_fail (is);
150 0 : return;
151 : }
152 10 : if (0 != strcmp (cs->expected_method,
153 : http_method))
154 : {
155 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156 : "http_method does not match\n");
157 0 : TALER_TESTING_interpreter_fail (is);
158 0 : return;
159 : }
160 10 : if (GNUNET_OK !=
161 10 : TALER_TESTING_get_trait_http_header (ref,
162 : cs->index,
163 : &header))
164 : {
165 0 : TALER_TESTING_interpreter_fail (is);
166 0 : return;
167 : }
168 10 : if ( ( (NULL == cs->expected_header) && (NULL != header)) ||
169 10 : ( (NULL != cs->expected_header) && (NULL == header)) ||
170 10 : ( (NULL != cs->expected_header) &&
171 4 : (0 != strcmp (cs->expected_header,
172 : header)) ) )
173 : {
174 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 : "header does not match: `%s' != `%s'\n",
176 : cs->expected_header,
177 : header);
178 0 : TALER_TESTING_interpreter_fail (is);
179 0 : return;
180 : }
181 10 : if (GNUNET_OK !=
182 10 : TALER_TESTING_get_trait_http_body (ref,
183 : cs->index,
184 : &body))
185 : {
186 0 : TALER_TESTING_interpreter_fail (is);
187 0 : return;
188 : }
189 10 : if (GNUNET_OK !=
190 10 : TALER_TESTING_get_trait_http_body_size (ref,
191 : cs->index,
192 : &body_size))
193 : {
194 0 : TALER_TESTING_interpreter_fail (is);
195 0 : return;
196 : }
197 10 : if ( ( (NULL == cs->expected_body) &&
198 0 : (NULL != body) ) ||
199 10 : ( (NULL != cs->expected_body) &&
200 10 : (NULL == body) ) ||
201 10 : ( (NULL != cs->expected_body) &&
202 10 : ( (*body_size != strlen (cs->expected_body)) ||
203 10 : (0 != memcmp (cs->expected_body,
204 : body,
205 : *body_size) ) ) ) )
206 : {
207 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 : "body does not match : `%s' and `%.*s'\n",
209 : cs->expected_body,
210 : (int) *body_size,
211 : (const char *) body);
212 0 : TALER_TESTING_interpreter_fail (is);
213 0 : return;
214 : }
215 10 : TALER_TESTING_interpreter_next (is);
216 : }
217 :
218 :
219 : /**
220 : * Free the state of a "checkserver" CMD.
221 : *
222 : * @param cls closure.
223 : * @param cmd command being run.
224 : */
225 : static void
226 10 : checkserver_cleanup (void *cls,
227 : const struct TALER_TESTING_Command *cmd)
228 : {
229 10 : struct CheckState *cs = cls;
230 :
231 10 : GNUNET_free (cs);
232 10 : }
233 :
234 :
235 : struct TALER_TESTING_Command
236 10 : TALER_TESTING_cmd_checkserver2 (const char *label,
237 : const char *ref_operation,
238 : unsigned int index,
239 : const char *expected_url,
240 : const char *expected_method,
241 : const char *expected_header,
242 : const char *expected_body)
243 : {
244 : struct CheckState *cs;
245 :
246 10 : cs = GNUNET_new (struct CheckState);
247 10 : cs->ref_operation = ref_operation;
248 10 : cs->index = index;
249 10 : cs->expected_url = expected_url;
250 10 : cs->expected_method = expected_method;
251 10 : cs->expected_header = expected_header;
252 10 : cs->expected_body = expected_body;
253 :
254 : {
255 10 : struct TALER_TESTING_Command cmd = {
256 : .cls = cs,
257 : .label = label,
258 : .run = &checkserver_run,
259 : .cleanup = &checkserver_cleanup
260 : };
261 :
262 10 : return cmd;
263 : }
264 : }
265 :
266 :
267 : struct TALER_TESTING_Command
268 2 : TALER_TESTING_cmd_checkserver (const char *label,
269 : const char *ref_operation,
270 : unsigned int index)
271 : {
272 2 : return TALER_TESTING_cmd_checkserver2 (label,
273 : ref_operation,
274 : index,
275 : "/",
276 : "POST",
277 : "EFEHYJS-Bakery",
278 : "5.0 EUR");
279 : }
280 :
281 :
282 : /* end of testing_api_cmd_checkserver.c */
|