testing_api_cmd_instance_auth.c (5556B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2021 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 * @file testing_api_cmd_instance_auth.c 21 * @brief command to test /private/auth POSTing 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 #include <taler/taler_exchange_service.h> 26 #include <taler/taler_testing_lib.h> 27 #include "taler_merchant_service.h" 28 #include "taler_merchant_testing_lib.h" 29 30 31 /** 32 * State of a "POST /instances/$ID/private/auth" CMD. 33 */ 34 struct AuthInstanceState 35 { 36 37 /** 38 * Handle for a "POST auth" request. 39 */ 40 struct TALER_MERCHANT_InstanceAuthPostHandle *iaph; 41 42 /** 43 * The interpreter state. 44 */ 45 struct TALER_TESTING_Interpreter *is; 46 47 /** 48 * Base URL of the merchant serving the request. 49 */ 50 const char *merchant_url; 51 52 /** 53 * ID of the instance to run GET for. 54 */ 55 const char *instance_id; 56 57 /** 58 * Desired token. Can be NULL 59 */ 60 const char *auth_token; 61 62 /** 63 * Expected HTTP response code. 64 */ 65 unsigned int http_status; 66 67 }; 68 69 70 /** 71 * Callback for a POST /instances/$ID/private/auth operation. 72 * 73 * @param cls closure for this function 74 * @param hr response being processed 75 */ 76 static void 77 auth_instance_cb (void *cls, 78 const struct TALER_MERCHANT_HttpResponse *hr) 79 { 80 struct AuthInstanceState *ais = cls; 81 82 ais->iaph = NULL; 83 if (ais->http_status != hr->http_status) 84 { 85 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 86 "Unexpected response code %u (%d) to command %s\n", 87 hr->http_status, 88 (int) hr->ec, 89 TALER_TESTING_interpreter_get_current_label (ais->is)); 90 TALER_TESTING_interpreter_fail (ais->is); 91 return; 92 } 93 switch (hr->http_status) 94 { 95 case MHD_HTTP_NO_CONTENT: 96 break; 97 case MHD_HTTP_BAD_REQUEST: 98 /* likely invalid auth_token value, we do not check client-side */ 99 break; 100 case MHD_HTTP_FORBIDDEN: 101 break; 102 default: 103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 104 "Unhandled HTTP status %u (%d) returned from /private/auth operation.\n", 105 hr->http_status, 106 hr->ec); 107 } 108 TALER_TESTING_interpreter_next (ais->is); 109 } 110 111 112 /** 113 * Run the "AUTH /instances/$ID" CMD. 114 * 115 * 116 * @param cls closure. 117 * @param cmd command being run now. 118 * @param is interpreter state. 119 */ 120 static void 121 auth_instance_run (void *cls, 122 const struct TALER_TESTING_Command *cmd, 123 struct TALER_TESTING_Interpreter *is) 124 { 125 struct AuthInstanceState *ais = cls; 126 127 ais->is = is; 128 ais->iaph = TALER_MERCHANT_instance_auth_post ( 129 TALER_TESTING_interpreter_get_context (is), 130 ais->merchant_url, 131 ais->instance_id, 132 ais->auth_token, 133 &auth_instance_cb, 134 ais); 135 GNUNET_assert (NULL != ais->iaph); 136 } 137 138 139 /** 140 * Free the state of a "POST instance auth" CMD, and possibly 141 * cancel a pending operation thereof. 142 * 143 * @param cls closure. 144 * @param cmd command being run. 145 */ 146 static void 147 auth_instance_cleanup (void *cls, 148 const struct TALER_TESTING_Command *cmd) 149 { 150 struct AuthInstanceState *ais = cls; 151 152 if (NULL != ais->iaph) 153 { 154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 155 "POST /instance/$ID/auth operation did not complete\n"); 156 TALER_MERCHANT_instance_auth_post_cancel (ais->iaph); 157 } 158 GNUNET_free (ais); 159 } 160 161 162 /** 163 * Offer internal data to other commands. 164 * 165 * @param cls closure 166 * @param[out] ret result (could be anything) 167 * @param trait name of the trait 168 * @param index index number of the object to extract. 169 * @return #GNUNET_OK on success 170 */ 171 static enum GNUNET_GenericReturnValue 172 auth_instance_traits (void *cls, 173 const void **ret, 174 const char *trait, 175 unsigned int index) 176 { 177 struct AuthInstanceState *ais = cls; 178 struct TALER_TESTING_Trait traits[] = { 179 TALER_TESTING_make_trait_auth_token (ais->auth_token), 180 TALER_TESTING_trait_end () 181 }; 182 183 return TALER_TESTING_get_trait (traits, 184 ret, 185 trait, 186 index); 187 } 188 189 190 struct TALER_TESTING_Command 191 TALER_TESTING_cmd_merchant_post_instance_auth (const char *label, 192 const char *merchant_url, 193 const char *instance_id, 194 const char *auth_token, 195 unsigned int http_status) 196 { 197 struct AuthInstanceState *ais; 198 199 ais = GNUNET_new (struct AuthInstanceState); 200 ais->merchant_url = merchant_url; 201 ais->instance_id = instance_id; 202 ais->auth_token = auth_token; 203 ais->http_status = http_status; 204 205 { 206 struct TALER_TESTING_Command cmd = { 207 .cls = ais, 208 .label = label, 209 .run = &auth_instance_run, 210 .cleanup = &auth_instance_cleanup, 211 .traits = &auth_instance_traits 212 }; 213 214 return cmd; 215 } 216 } 217 218 219 /* end of testing_api_cmd_auth_instance.c */