testing_api_cmd_get_product_image.c (5806B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025 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_get_product_image.c 21 * @brief command to test GET /products/$HASH/image 22 * @author Bohdan Potuzhnyi 23 */ 24 #include "taler/platform.h" 25 #include <taler/taler_exchange_service.h> 26 #include <taler/taler_testing_lib.h> 27 #include "taler/taler_merchant_service.h" 28 #include "taler/taler_merchant_testing_lib.h" 29 #include <taler/taler-merchant/get-products-IMAGE_HASH-image.h> 30 31 32 /** 33 * State of a "GET product image" CMD. 34 */ 35 struct GetProductImageState 36 { 37 /** 38 * Handle for a "GET product image" request. 39 */ 40 struct TALER_MERCHANT_GetProductsImageHandle *pigh; 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 * Reference for a POST or PATCH /products CMD (optional). 54 */ 55 const char *product_reference; 56 57 /** 58 * Expected HTTP response code. 59 */ 60 unsigned int http_status; 61 62 /** 63 * Expected image as a data URL. 64 */ 65 char *expected_image; 66 67 /** 68 * Hash over the expected image, lowercase hex encoding. 69 */ 70 const char *image_hash; 71 }; 72 73 74 /** 75 * Callback for a /products/$HASH/image operation. 76 * 77 * @param cls closure for this function 78 * @param pir response details 79 */ 80 static void 81 get_product_image_cb (void *cls, 82 const struct TALER_MERCHANT_GetProductsImageResponse *pir) 83 { 84 struct GetProductImageState *gis = cls; 85 86 gis->pigh = NULL; 87 if (gis->http_status != pir->hr.http_status) 88 { 89 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 90 "Unexpected response code %u (%d) to command %s\n", 91 pir->hr.http_status, 92 (int) pir->hr.ec, 93 TALER_TESTING_interpreter_get_current_label (gis->is)); 94 TALER_TESTING_interpreter_fail (gis->is); 95 return; 96 } 97 switch (pir->hr.http_status) 98 { 99 case MHD_HTTP_OK: 100 if (NULL != gis->expected_image) 101 { 102 if (0 != strcmp (pir->details.ok.image, 103 gis->expected_image)) 104 { 105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 106 "Product image did not match expected value\n"); 107 TALER_TESTING_interpreter_fail (gis->is); 108 return; 109 } 110 } 111 break; 112 default: 113 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 114 "Unhandled HTTP status.\n"); 115 } 116 TALER_TESTING_interpreter_next (gis->is); 117 } 118 119 120 /** 121 * Run the "GET product image" CMD. 122 * 123 * @param cls closure. 124 * @param cmd command being run now. 125 * @param is interpreter state. 126 */ 127 static void 128 get_product_image_run (void *cls, 129 const struct TALER_TESTING_Command *cmd, 130 struct TALER_TESTING_Interpreter *is) 131 { 132 struct GetProductImageState *gis = cls; 133 const struct TALER_TESTING_Command *product_cmd; 134 135 gis->is = is; 136 gis->expected_image = NULL; 137 if (NULL != gis->product_reference) 138 { 139 const char *product_image; 140 141 product_cmd = TALER_TESTING_interpreter_lookup_command ( 142 is, 143 gis->product_reference); 144 if (NULL == product_cmd) 145 { 146 GNUNET_break (0); 147 TALER_TESTING_interpreter_fail (is); 148 return; 149 } 150 if (GNUNET_OK != 151 TALER_TESTING_get_trait_product_image (product_cmd, 152 &product_image)) 153 { 154 TALER_TESTING_interpreter_fail (is); 155 return; 156 } 157 gis->expected_image = GNUNET_strdup (product_image); 158 } 159 gis->pigh 160 = TALER_MERCHANT_get_products_image_create ( 161 TALER_TESTING_interpreter_get_context (is), 162 gis->merchant_url, 163 gis->image_hash); 164 { 165 enum TALER_ErrorCode ec; 166 167 ec = TALER_MERCHANT_get_products_image_start ( 168 gis->pigh, 169 &get_product_image_cb, 170 gis); 171 GNUNET_assert (TALER_EC_NONE == ec); 172 } 173 } 174 175 176 /** 177 * Free the state of a "GET product image" CMD, and possibly 178 * cancel a pending operation thereof. 179 * 180 * @param cls closure. 181 * @param cmd command being run. 182 */ 183 static void 184 get_product_image_cleanup (void *cls, 185 const struct TALER_TESTING_Command *cmd) 186 { 187 struct GetProductImageState *gis = cls; 188 189 if (NULL != gis->pigh) 190 { 191 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 192 "GET /products/$HASH/image operation did not complete\n"); 193 TALER_MERCHANT_get_products_image_cancel (gis->pigh); 194 } 195 GNUNET_free (gis->expected_image); 196 GNUNET_free (gis); 197 } 198 199 200 struct TALER_TESTING_Command 201 TALER_TESTING_cmd_get_product_image (const char *label, 202 const char *merchant_url, 203 const char *product_reference, 204 const char *image_hash, 205 unsigned int http_status) 206 { 207 struct GetProductImageState *gis; 208 209 gis = GNUNET_new (struct GetProductImageState); 210 gis->merchant_url = merchant_url; 211 gis->product_reference = product_reference; 212 gis->http_status = http_status; 213 gis->image_hash = image_hash; 214 { 215 struct TALER_TESTING_Command cmd = { 216 .cls = gis, 217 .label = label, 218 .run = &get_product_image_run, 219 .cleanup = &get_product_image_cleanup 220 }; 221 222 return cmd; 223 } 224 }