testing_api_cmd_object_upload.c (5520B)
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_object_upload.c 21 * @brief command to upload an object to the sync backend service. 22 * @author Iván Ávalos 23 */ 24 #include "platform.h" 25 #include "sync/sync_service.h" 26 #include "sync/sync_testing_lib.h" 27 #include <taler/taler_util.h> 28 #include <taler/taler_signatures.h> 29 #include <taler/taler_testing_lib.h> 30 #include <gnunet/gnunet_curl_lib.h> 31 32 33 /** 34 * State for an "object upload" CMD. 35 */ 36 struct ObjectUploadState 37 { 38 39 /** 40 * URL of the sync backend. 41 */ 42 const char *sync_url; 43 44 /** 45 * Reference to a command that provides account keys. 46 */ 47 const char *account_reference; 48 49 /** 50 * Eddsa private key. 51 */ 52 struct SYNC_AccountPrivateKeyP sync_priv; 53 54 /** 55 * Eddsa public key. 56 */ 57 struct SYNC_AccountPublicKeyP sync_pub; 58 59 /** 60 * UID of the object. 61 */ 62 struct SYNC_ObjectUID uid; 63 64 /** 65 * Object data. 66 */ 67 const void *data; 68 69 /** 70 * Number of bytes in @e data. 71 */ 72 size_t data_size; 73 74 /** 75 * Expected HTTP status code. 76 */ 77 unsigned int http_status; 78 79 /** 80 * The interpreter state. 81 */ 82 struct TALER_TESTING_Interpreter *is; 83 84 /** 85 * Handle for the library object upload operation. 86 */ 87 struct SYNC_BlockOperation *bop; 88 89 }; 90 91 92 /** 93 * Function called when we're done processing the 94 * object upload. 95 * 96 * @param cls the `struct ObjectUploadState` 97 * @param ud details about the upload operation 98 */ 99 static void 100 object_upload_finished (void *cls, 101 const struct SYNC_BlockOperationDetails *ud) 102 { 103 struct ObjectUploadState *ous = cls; 104 105 ous->bop = NULL; 106 if (ud->http_status != ous->http_status) 107 { 108 TALER_TESTING_unexpected_status (ous->is, 109 ud->http_status, 110 ous->http_status); 111 return; 112 } 113 TALER_TESTING_interpreter_next (ous->is); 114 } 115 116 117 /** 118 * Run an "object upload" CMD. 119 * 120 * @param cls closure. 121 * @param cmd command currently being run. 122 * @param is interpreter state. 123 */ 124 static void 125 object_upload_run (void *cls, 126 const struct TALER_TESTING_Command *cmd, 127 struct TALER_TESTING_Interpreter *is) 128 { 129 struct ObjectUploadState *ous = cls; 130 131 (void) cmd; 132 ous->is = is; 133 134 { 135 const struct TALER_TESTING_Command *ref; 136 const struct SYNC_AccountPrivateKeyP *priv; 137 const struct SYNC_AccountPublicKeyP *pub; 138 139 ref = TALER_TESTING_interpreter_lookup_command (is, 140 ous->account_reference); 141 if (NULL == ref) 142 { 143 GNUNET_break (0); 144 goto fail; 145 } 146 if ( (GNUNET_OK != 147 TALER_TESTING_get_trait_sync_account_priv (ref, 148 &priv)) || 149 (GNUNET_OK != 150 TALER_TESTING_get_trait_sync_account_pub (ref, 151 &pub)) ) 152 { 153 GNUNET_break (0); 154 goto fail; 155 } 156 ous->sync_priv = *priv; 157 ous->sync_pub = *pub; 158 } 159 160 ous->bop = SYNC_object_upload ( 161 TALER_TESTING_interpreter_get_context (is), 162 ous->sync_url, 163 &ous->sync_priv, 164 &ous->uid, 165 ous->data_size, 166 ous->data, 167 &object_upload_finished, 168 ous); 169 if (NULL == ous->bop) 170 goto fail; 171 172 return; 173 174 fail: 175 GNUNET_break (0); 176 TALER_TESTING_interpreter_fail (is); 177 } 178 179 180 /** 181 * Free the state of an "object upload" CMD, and possibly 182 * cancel it if it did not complete. 183 * 184 * @param cls closure. 185 * @param cmd command being freed. 186 */ 187 static void 188 object_upload_cleanup (void *cls, 189 const struct TALER_TESTING_Command *cmd) 190 { 191 struct ObjectUploadState *ous = cls; 192 193 if (NULL != ous->bop) 194 { 195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 196 "Command '%s' did not complete (object upload)\n", 197 cmd->label); 198 SYNC_block_operation_cancel (ous->bop); 199 ous->bop = NULL; 200 } 201 GNUNET_free (ous); 202 } 203 204 205 struct TALER_TESTING_Command 206 SYNC_TESTING_cmd_object_upload (const char *label, 207 const char *sync_url, 208 const char *account_reference, 209 const struct SYNC_ObjectUID *uid, 210 unsigned int http_status, 211 const void *data, 212 size_t data_size) 213 { 214 struct ObjectUploadState *ous; 215 216 ous = GNUNET_new (struct ObjectUploadState); 217 ous->sync_url = sync_url; 218 ous->account_reference = account_reference; 219 if (NULL != uid) 220 ous->uid = *uid; 221 ous->http_status = http_status; 222 ous->data = data; 223 ous->data_size = data_size; 224 { 225 struct TALER_TESTING_Command cmd = { 226 .cls = ous, 227 .label = label, 228 .run = &object_upload_run, 229 .cleanup = &object_upload_cleanup, 230 .traits = NULL 231 }; 232 233 return cmd; 234 } 235 } 236 237 238 /* end of testing_api_cmd_object_upload.c */