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