testing_api_cmd_checkserver.c (7586B)
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 checkserver_run (void *cls, 92 const struct TALER_TESTING_Command *cmd, 93 struct TALER_TESTING_Interpreter *is) 94 { 95 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 cs->is = is; 105 ref = TALER_TESTING_interpreter_lookup_command (is, 106 cs->ref_operation); 107 if (NULL == ref) 108 { 109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 110 "ref NULL\n"); 111 GNUNET_break (0); 112 TALER_TESTING_interpreter_fail (is); 113 return; 114 } 115 if (GNUNET_OK != 116 TALER_TESTING_get_trait_urls (ref, 117 cs->index, 118 &url)) 119 { 120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 121 "Trait url does not work\n"); 122 GNUNET_break (0); 123 TALER_TESTING_interpreter_fail (is); 124 return; 125 } 126 if (NULL == url) 127 { 128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 129 "Trait for url is NULL!?\n"); 130 GNUNET_break (0); 131 TALER_TESTING_interpreter_fail (is); 132 return; 133 } 134 if (0 != strcmp (cs->expected_url, 135 url)) 136 { 137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 138 "URL does not match: `%s' != `%s'\n", 139 cs->expected_url, 140 url); 141 TALER_TESTING_interpreter_fail (is); 142 return; 143 } 144 if (GNUNET_OK != 145 TALER_TESTING_get_trait_http_methods (ref, 146 cs->index, 147 &http_method)) 148 { 149 TALER_TESTING_interpreter_fail (is); 150 return; 151 } 152 if (0 != strcmp (cs->expected_method, 153 http_method)) 154 { 155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 156 "http_method does not match\n"); 157 TALER_TESTING_interpreter_fail (is); 158 return; 159 } 160 if (GNUNET_OK != 161 TALER_TESTING_get_trait_http_header (ref, 162 cs->index, 163 &header)) 164 { 165 TALER_TESTING_interpreter_fail (is); 166 return; 167 } 168 if ( ( (NULL == cs->expected_header) && (NULL != header)) || 169 ( (NULL != cs->expected_header) && (NULL == header)) || 170 ( (NULL != cs->expected_header) && 171 (0 != strcmp (cs->expected_header, 172 header)) ) ) 173 { 174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 175 "header does not match: `%s' != `%s'\n", 176 cs->expected_header, 177 header); 178 TALER_TESTING_interpreter_fail (is); 179 return; 180 } 181 if (GNUNET_OK != 182 TALER_TESTING_get_trait_http_body (ref, 183 cs->index, 184 &body)) 185 { 186 TALER_TESTING_interpreter_fail (is); 187 return; 188 } 189 if (GNUNET_OK != 190 TALER_TESTING_get_trait_http_body_size (ref, 191 cs->index, 192 &body_size)) 193 { 194 TALER_TESTING_interpreter_fail (is); 195 return; 196 } 197 if ( ( (NULL == cs->expected_body) && 198 (NULL != body) ) || 199 ( (NULL != cs->expected_body) && 200 (NULL == body) ) || 201 ( (NULL != cs->expected_body) && 202 ( (*body_size != strlen (cs->expected_body)) || 203 (0 != memcmp (cs->expected_body, 204 body, 205 *body_size) ) ) ) ) 206 { 207 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 TALER_TESTING_interpreter_fail (is); 213 return; 214 } 215 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 checkserver_cleanup (void *cls, 227 const struct TALER_TESTING_Command *cmd) 228 { 229 struct CheckState *cs = cls; 230 231 GNUNET_free (cs); 232 } 233 234 235 struct TALER_TESTING_Command 236 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 cs = GNUNET_new (struct CheckState); 247 cs->ref_operation = ref_operation; 248 cs->index = index; 249 cs->expected_url = expected_url; 250 cs->expected_method = expected_method; 251 cs->expected_header = expected_header; 252 cs->expected_body = expected_body; 253 254 { 255 struct TALER_TESTING_Command cmd = { 256 .cls = cs, 257 .label = label, 258 .run = &checkserver_run, 259 .cleanup = &checkserver_cleanup 260 }; 261 262 return cmd; 263 } 264 } 265 266 267 struct TALER_TESTING_Command 268 TALER_TESTING_cmd_checkserver (const char *label, 269 const char *ref_operation, 270 unsigned int index) 271 { 272 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 */