testing_api_cmd_block_common.c (6346B)
1 /* 2 This file is part of SYNC 3 Copyright (C) 2014-2026 Taler Systems SA 4 5 SYNC 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 SYNC 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 SYNC; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 /** 20 * @file testing/testing_api_cmd_block_common.c 21 * @brief shared logic for block upload commands. 22 * @author Iván Ávalos 23 */ 24 #include <sync/sync_util.h> 25 #include "testing_api_cmd_block_common.h" 26 27 28 /** 29 * Function called when we're done processing the 30 * block upload. 31 * 32 * @param cls the `struct BlockUploadState` 33 * @param ud details about the upload operation 34 */ 35 static void 36 block_upload_finished (void *cls, 37 const struct SYNC_BlockOperationDetails *ud) 38 { 39 struct BlockUploadState *bus = cls; 40 41 bus->bop = NULL; 42 if (ud->http_status != bus->http_status) 43 { 44 TALER_TESTING_unexpected_status (bus->is, 45 ud->http_status, 46 bus->http_status); 47 return; 48 } 49 if (MHD_HTTP_PAYMENT_REQUIRED == ud->http_status) 50 { 51 struct TALER_MERCHANT_PayUriData pd; 52 53 if (GNUNET_OK != 54 TALER_MERCHANT_parse_pay_uri (ud->pay_uri, 55 &pd)) 56 { 57 GNUNET_break (0); 58 TALER_TESTING_interpreter_fail (bus->is); 59 return; 60 } 61 bus->payment_order_id = GNUNET_strdup (pd.order_id); 62 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 63 "Order ID from Sync service is `%s'\n", 64 bus->payment_order_id); 65 if (NULL != pd.claim_token) 66 bus->claim_token = *pd.claim_token; 67 TALER_MERCHANT_parse_pay_uri_free (&pd); 68 } 69 TALER_TESTING_interpreter_next (bus->is); 70 } 71 72 73 enum GNUNET_GenericReturnValue 74 SYNC_TESTING_inherit_account_keys_ ( 75 struct BlockUploadState *bus, 76 const struct TALER_TESTING_Command *ref) 77 { 78 const struct SYNC_AccountPrivateKeyP *priv; 79 const struct SYNC_AccountPublicKeyP *pub; 80 81 if (GNUNET_OK != 82 TALER_TESTING_get_trait_sync_account_priv (ref, 83 &priv)) 84 { 85 GNUNET_break (0); 86 return GNUNET_SYSERR; 87 } 88 if (GNUNET_OK != 89 TALER_TESTING_get_trait_sync_account_pub (ref, 90 &pub)) 91 { 92 GNUNET_break (0); 93 return GNUNET_SYSERR; 94 } 95 bus->sync_priv = *priv; 96 bus->sync_pub = *pub; 97 return GNUNET_OK; 98 } 99 100 101 void 102 SYNC_TESTING_block_upload_run_ ( 103 void *cls, 104 const struct TALER_TESTING_Command *cmd, 105 struct TALER_TESTING_Interpreter *is) 106 { 107 struct BlockUploadState *bus = cls; 108 109 (void) cmd; 110 bus->is = is; 111 112 if (NULL != bus->upload_reference) 113 { 114 const struct TALER_TESTING_Command *ref; 115 116 ref = TALER_TESTING_interpreter_lookup_command (is, 117 bus->upload_reference); 118 if (NULL == ref) 119 goto fail; 120 if (GNUNET_OK != 121 bus->process_ref (bus, ref)) 122 goto fail; 123 } 124 else 125 { 126 GNUNET_CRYPTO_eddsa_key_create (&bus->sync_priv.eddsa_priv); 127 GNUNET_CRYPTO_eddsa_key_get_public (&bus->sync_priv.eddsa_priv, 128 &bus->sync_pub.eddsa_pub); 129 } 130 131 /* Generate random nonce if still all zeros */ 132 if (GNUNET_is_zero (&bus->nonce)) 133 SYNC_block_nonce_generate (&bus->nonce); 134 135 /* Compute hash of block data for traits */ 136 GNUNET_CRYPTO_hash (bus->block_data, 137 bus->block_data_size, 138 &bus->new_data_hash); 139 140 /* P1: hash over the object references (covered by the signature) */ 141 if (GNUNET_OK != 142 SYNC_object_refs_hash (bus->refs_len, 143 bus->object_uids, 144 bus->object_incs, 145 &bus->refs_hash)) 146 { 147 GNUNET_break (0); 148 goto fail; 149 } 150 151 bus->bop = bus->start_upload ( 152 TALER_TESTING_interpreter_get_context (is), 153 bus->sync_url, 154 &bus->sync_priv, 155 &bus->nonce, 156 GNUNET_is_zero (&bus->prev_nonce) ? NULL : &bus->prev_nonce, 157 GNUNET_is_zero (&bus->next_nonce) ? NULL : &bus->next_nonce, 158 GNUNET_is_zero (&bus->old_data_hash) ? NULL : &bus->old_data_hash, 159 bus->block_data_size, 160 bus->block_data, 161 bus->refs_len, 162 bus->object_uids, 163 bus->object_incs, 164 bus->have_relink_target ? &bus->prev_entry : NULL, 165 &block_upload_finished, 166 bus); 167 if (NULL == bus->bop) 168 goto fail; 169 170 return; 171 172 fail: 173 GNUNET_break (0); 174 TALER_TESTING_interpreter_fail (is); 175 } 176 177 178 void 179 SYNC_TESTING_block_upload_cleanup_ ( 180 void *cls, 181 const struct TALER_TESTING_Command *cmd) 182 { 183 struct BlockUploadState *bus = cls; 184 185 if (NULL != bus->bop) 186 { 187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 188 "Command '%s' did not complete (block upload)\n", 189 cmd->label); 190 SYNC_block_operation_cancel (bus->bop); 191 bus->bop = NULL; 192 } 193 GNUNET_free (bus->payment_order_id); 194 GNUNET_free (bus); 195 } 196 197 198 enum GNUNET_GenericReturnValue 199 SYNC_TESTING_block_upload_traits_ ( 200 void *cls, 201 const void **ret, 202 const char *trait, 203 unsigned int index) 204 { 205 struct BlockUploadState *bus = cls; 206 struct TALER_TESTING_Trait traits[] = { 207 TALER_TESTING_make_trait_sync_account_pub (&bus->sync_pub), 208 TALER_TESTING_make_trait_sync_account_priv (&bus->sync_priv), 209 TALER_TESTING_make_trait_curr_hash (&bus->new_data_hash), 210 TALER_TESTING_make_trait_prev_hash (&bus->old_data_hash), 211 TALER_TESTING_make_trait_block_nonce (&bus->nonce), 212 TALER_TESTING_make_trait_prev_nonce (&bus->prev_nonce), 213 TALER_TESTING_make_trait_next_nonce (&bus->next_nonce), 214 TALER_TESTING_make_trait_block_refs_hash (&bus->refs_hash), 215 TALER_TESTING_make_trait_order_id (bus->payment_order_id), 216 TALER_TESTING_make_trait_claim_token (&bus->claim_token), 217 TALER_TESTING_trait_end () 218 }; 219 220 return TALER_TESTING_get_trait (traits, 221 ret, 222 trait, 223 index); 224 }