testing_api_cmd_purse_delete.c (4863B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020-2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it 6 under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3, or (at your 8 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 GNU 13 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 * @file testing/testing_api_cmd_purse_delete.c 21 * @brief command for testing /management/purse/disable. 22 * @author Christian Grothoff 23 */ 24 #include "taler/taler_json_lib.h" 25 #include <gnunet/gnunet_curl_lib.h> 26 #include "taler/taler_testing_lib.h" 27 28 29 /** 30 * State for a "purse_delete" CMD. 31 */ 32 struct PurseDeleteState 33 { 34 35 /** 36 * Purse delete handle while operation is running. 37 */ 38 struct TALER_EXCHANGE_DeletePursesHandle *dph; 39 40 /** 41 * Our interpreter. 42 */ 43 struct TALER_TESTING_Interpreter *is; 44 45 /** 46 * Expected HTTP response code. 47 */ 48 unsigned int expected_response_code; 49 50 /** 51 * Command that created the purse we now want to 52 * delete. 53 */ 54 const char *purse_cmd; 55 }; 56 57 58 /** 59 * Callback to analyze the DELETE /purses/$PID response, just used to check if 60 * the response code is acceptable. 61 * 62 * @param cls closure. 63 * @param pdr HTTP response details 64 */ 65 static void 66 purse_delete_cb (void *cls, 67 const struct TALER_EXCHANGE_DeletePursesResponse *pdr) 68 { 69 struct PurseDeleteState *pds = cls; 70 71 pds->dph = NULL; 72 if (pds->expected_response_code != pdr->hr.http_status) 73 { 74 TALER_TESTING_unexpected_status (pds->is, 75 pdr->hr.http_status, 76 pds->expected_response_code); 77 return; 78 } 79 TALER_TESTING_interpreter_next (pds->is); 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 purse_delete_run (void *cls, 92 const struct TALER_TESTING_Command *cmd, 93 struct TALER_TESTING_Interpreter *is) 94 { 95 struct PurseDeleteState *pds = cls; 96 const struct TALER_PurseContractPrivateKeyP *purse_priv; 97 const struct TALER_TESTING_Command *ref; 98 const char *exchange_url; 99 100 (void) cmd; 101 exchange_url = TALER_TESTING_get_exchange_url (is); 102 if (NULL == exchange_url) 103 { 104 GNUNET_break (0); 105 return; 106 } 107 ref = TALER_TESTING_interpreter_lookup_command (is, 108 pds->purse_cmd); 109 if (NULL == ref) 110 { 111 GNUNET_break (0); 112 TALER_TESTING_interpreter_fail (is); 113 return; 114 } 115 if (GNUNET_OK != 116 TALER_TESTING_get_trait_purse_priv (ref, 117 &purse_priv)) 118 { 119 GNUNET_break (0); 120 TALER_TESTING_interpreter_fail (is); 121 return; 122 } 123 pds->is = is; 124 pds->dph = TALER_EXCHANGE_delete_purses_create ( 125 TALER_TESTING_interpreter_get_context (is), 126 exchange_url, 127 purse_priv); 128 if (NULL == pds->dph) 129 { 130 GNUNET_break (0); 131 TALER_TESTING_interpreter_fail (is); 132 return; 133 } 134 { 135 enum TALER_ErrorCode ec; 136 137 ec = TALER_EXCHANGE_delete_purses_start ( 138 pds->dph, 139 &purse_delete_cb, 140 pds); 141 if (TALER_EC_NONE != ec) 142 { 143 GNUNET_break (0); 144 TALER_EXCHANGE_delete_purses_cancel (pds->dph); 145 pds->dph = NULL; 146 TALER_TESTING_interpreter_fail (is); 147 return; 148 } 149 } 150 } 151 152 153 /** 154 * Free the state of a "purse_delete" CMD, and possibly cancel a 155 * pending operation thereof. 156 * 157 * @param cls closure, must be a `struct PurseDeleteState`. 158 * @param cmd the command which is being cleaned up. 159 */ 160 static void 161 purse_delete_cleanup (void *cls, 162 const struct TALER_TESTING_Command *cmd) 163 { 164 struct PurseDeleteState *pds = cls; 165 166 if (NULL != pds->dph) 167 { 168 TALER_TESTING_command_incomplete (pds->is, 169 cmd->label); 170 TALER_EXCHANGE_delete_purses_cancel (pds->dph); 171 pds->dph = NULL; 172 } 173 GNUNET_free (pds); 174 } 175 176 177 struct TALER_TESTING_Command 178 TALER_TESTING_cmd_purse_delete (const char *label, 179 unsigned int expected_http_status, 180 const char *purse_cmd) 181 { 182 struct PurseDeleteState *ds; 183 184 ds = GNUNET_new (struct PurseDeleteState); 185 ds->expected_response_code = expected_http_status; 186 ds->purse_cmd = purse_cmd; 187 { 188 struct TALER_TESTING_Command cmd = { 189 .cls = ds, 190 .label = label, 191 .run = &purse_delete_run, 192 .cleanup = &purse_delete_cleanup 193 }; 194 195 return cmd; 196 } 197 } 198 199 200 /* end of testing_api_cmd_purse_delete.c */