testing_cmd_truth_upload.c (9264B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file testing/testing_cmd_truth_upload.c 18 * @brief command to execute the anastasis secret share service 19 * @author Christian Grothoff 20 * @author Dennis Neufeld 21 * @author Dominik Meister 22 */ 23 24 #include "platform.h" 25 #include "anastasis_testing_lib.h" 26 #include <taler/taler_util.h> 27 #include <taler/taler_testing_lib.h> 28 29 30 /** 31 * State for a "truth upload" CMD. 32 */ 33 struct TruthUploadState 34 { 35 /** 36 * The interpreter state. 37 */ 38 struct TALER_TESTING_Interpreter *is; 39 40 /** 41 * URL of the anastasis backend. 42 */ 43 const char *anastasis_url; 44 45 /** 46 * Label of this command. 47 */ 48 const char *label; 49 50 /** 51 * The ID data to generate user identifier 52 */ 53 json_t *id_data; 54 55 /** 56 * The escrow method 57 */ 58 const char *method; 59 60 /** 61 * Instructions to be returned to client/user 62 * (e.g. "Look at your smartphone. SMS was sent to you") 63 */ 64 const char *instructions; 65 66 /** 67 * Mime type of truth_data (eg. jpeg, string etc.) 68 */ 69 const char *mime_type; 70 71 /** 72 * The truth_data (e.g. hash of answer to a secure question) 73 */ 74 void *truth_data; 75 76 /** 77 * Requested order ID for this upload (if unpaid). 78 */ 79 struct ANASTASIS_PaymentSecretP payment_secret_response; 80 81 /** 82 * Size of truth_data 83 */ 84 size_t truth_data_size; 85 86 /** 87 * Expected status code. 88 */ 89 unsigned int http_status; 90 91 /** 92 * The /truth POST operation handle. 93 */ 94 struct ANASTASIS_TruthUpload *tuo; 95 96 /** 97 * closure for the payment callback 98 */ 99 void *tpc_cls; 100 101 /** 102 * Reference to salt download. 103 */ 104 const char *salt_reference; 105 106 /** 107 * Options for how we are supposed to do the upload. 108 */ 109 enum ANASTASIS_TESTING_TruthStoreOption tsopt; 110 111 /** 112 * Truth object 113 */ 114 struct ANASTASIS_Truth *truth; 115 }; 116 117 118 /** 119 * Upload information 120 * caller MUST free 't' using ANASTASIS_truth_free() 121 * 122 * @param cls closure for callback 123 * @param t Truth object (contains provider url and truth public key) 124 * @param ud upload details, useful to continue in case of errors, NULL on success 125 */ 126 static void 127 truth_upload_cb (void *cls, 128 struct ANASTASIS_Truth *t, 129 const struct ANASTASIS_UploadDetails *ud) 130 { 131 struct TruthUploadState *tus = cls; 132 133 tus->tuo = NULL; 134 if (NULL == ud) 135 { 136 GNUNET_break (0); 137 TALER_TESTING_interpreter_fail (tus->is); 138 return; 139 } 140 if (ud->http_status != tus->http_status) 141 { 142 GNUNET_break (0); 143 TALER_TESTING_interpreter_fail (tus->is); 144 return; 145 } 146 if (MHD_HTTP_PAYMENT_REQUIRED == ud->http_status) 147 { 148 if (ANASTASIS_US_PAYMENT_REQUIRED != ud->us) 149 { 150 GNUNET_break (0); 151 TALER_TESTING_interpreter_fail (tus->is); 152 return; 153 } 154 tus->payment_secret_response = ud->details.payment.ps; 155 TALER_TESTING_interpreter_next (tus->is); 156 return; 157 } 158 if ( (ANASTASIS_US_SUCCESS == ud->us) && 159 (NULL == t) ) 160 { 161 GNUNET_break (0); 162 TALER_TESTING_interpreter_next (tus->is); 163 return; 164 } 165 tus->truth = t; 166 TALER_TESTING_interpreter_next (tus->is); 167 } 168 169 170 /** 171 * Run a "truth upload" CMD. 172 * 173 * @param cls closure. 174 * @param cmd command currently being run. 175 * @param is interpreter state. 176 */ 177 static void 178 truth_upload_run (void *cls, 179 const struct TALER_TESTING_Command *cmd, 180 struct TALER_TESTING_Interpreter *is) 181 { 182 struct TruthUploadState *tus = cls; 183 const struct TALER_TESTING_Command *ref; 184 const struct ANASTASIS_CRYPTO_ProviderSaltP *provider_salt; 185 struct ANASTASIS_CRYPTO_UserIdentifierP user_id; 186 187 tus->is = is; 188 GNUNET_assert (NULL != tus->salt_reference); 189 ref = TALER_TESTING_interpreter_lookup_command ( 190 is, 191 tus->salt_reference); 192 if (NULL == ref) 193 { 194 GNUNET_break (0); 195 TALER_TESTING_interpreter_fail (tus->is); 196 return; 197 } 198 if (GNUNET_OK != 199 ANASTASIS_TESTING_get_trait_provider_salt (ref, 200 &provider_salt)) 201 { 202 GNUNET_break (0); 203 TALER_TESTING_interpreter_fail (tus->is); 204 return; 205 } 206 ANASTASIS_CRYPTO_user_identifier_derive (tus->id_data, 207 provider_salt, 208 &user_id); 209 tus->tuo = ANASTASIS_truth_upload ( 210 TALER_TESTING_interpreter_get_context (is), 211 &user_id, 212 tus->anastasis_url, 213 tus->method, 214 tus->instructions, 215 tus->mime_type, 216 provider_salt, 217 tus->truth_data, 218 tus->truth_data_size, 219 false, /* force payment */ 220 GNUNET_TIME_UNIT_ZERO, 221 &truth_upload_cb, 222 tus); 223 if (NULL == tus->tuo) 224 { 225 GNUNET_break (0); 226 TALER_TESTING_interpreter_fail (tus->is); 227 } 228 } 229 230 231 /** 232 * Free the state of a "truth upload" CMD, and possibly 233 * cancel it if it did not complete. 234 * 235 * @param cls closure. 236 * @param cmd command being freed. 237 */ 238 static void 239 truth_upload_cleanup (void *cls, 240 const struct TALER_TESTING_Command *cmd) 241 { 242 struct TruthUploadState *tus = cls; 243 244 if (NULL != tus->tuo) 245 { 246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 247 "Command '%s' did not complete\n", 248 cmd->label); 249 ANASTASIS_truth_upload_cancel (tus->tuo); 250 tus->tuo = NULL; 251 } 252 if (NULL != tus->id_data) 253 { 254 json_decref (tus->id_data); 255 tus->id_data = NULL; 256 } 257 if (NULL != tus->truth) 258 { 259 ANASTASIS_truth_free (tus->truth); 260 tus->truth = NULL; 261 } 262 GNUNET_free (tus->truth_data); 263 GNUNET_free (tus); 264 } 265 266 267 /** 268 * Offer internal data to other commands. 269 * 270 * @param cls closure 271 * @param[out] ret result (could be anything) 272 * @param trait name of the trait 273 * @param index index number of the object to extract. 274 * @return #GNUNET_OK on success 275 */ 276 static int 277 truth_upload_traits (void *cls, 278 const void **ret, 279 const char *trait, 280 unsigned int index) 281 { 282 struct TruthUploadState *tus = cls; 283 struct TALER_TESTING_Trait traits[] = { 284 ANASTASIS_TESTING_make_trait_truth ( 285 (const struct ANASTASIS_Truth **) &tus->truth), 286 ANASTASIS_TESTING_make_trait_payment_secret (&tus->payment_secret_response), 287 TALER_TESTING_trait_end () 288 }; 289 290 return TALER_TESTING_get_trait (traits, 291 ret, 292 trait, 293 index); 294 } 295 296 297 json_t * 298 ANASTASIS_TESTING_make_id_data_example (const char *id_data) 299 { 300 return GNUNET_JSON_PACK ( 301 GNUNET_JSON_pack_string ("id_data", 302 id_data)); 303 } 304 305 306 struct TALER_TESTING_Command 307 ANASTASIS_TESTING_cmd_truth_upload ( 308 const char *label, 309 const char *anastasis_url, 310 const json_t *id_data, 311 const char *method, 312 const char *instructions, 313 const char *mime_type, 314 const void *truth_data, 315 size_t truth_data_size, 316 unsigned int http_status, 317 enum ANASTASIS_TESTING_TruthStoreOption tso, 318 const char *salt_ref) 319 { 320 struct TruthUploadState *tus; 321 322 tus = GNUNET_new (struct TruthUploadState); 323 tus->label = label; 324 tus->http_status = http_status; 325 tus->tsopt = tso; 326 tus->anastasis_url = anastasis_url; 327 tus->salt_reference = salt_ref; 328 tus->id_data = json_incref ((json_t *) id_data); 329 tus->method = method; 330 tus->instructions = instructions; 331 tus->mime_type = mime_type; 332 tus->truth_data_size = truth_data_size; 333 tus->truth_data = GNUNET_memdup (truth_data, 334 truth_data_size); 335 { 336 struct TALER_TESTING_Command cmd = { 337 .cls = tus, 338 .label = label, 339 .run = &truth_upload_run, 340 .cleanup = &truth_upload_cleanup, 341 .traits = &truth_upload_traits 342 }; 343 344 return cmd; 345 } 346 } 347 348 349 struct TALER_TESTING_Command 350 ANASTASIS_TESTING_cmd_truth_upload_question ( 351 const char *label, 352 const char *anastasis_url, 353 const json_t *id_data, 354 const char *instructions, 355 const char *mime_type, 356 const void *answer, 357 unsigned int http_status, 358 enum ANASTASIS_TESTING_TruthStoreOption tso, 359 const char *salt_ref) 360 { 361 return ANASTASIS_TESTING_cmd_truth_upload (label, 362 anastasis_url, 363 id_data, 364 "question", 365 instructions, 366 mime_type, 367 answer, 368 strlen (answer), 369 http_status, 370 tso, 371 salt_ref); 372 } 373 374 375 /* end of testing_cmd_truth_upload.c */