testing_api_cmd_backup_download.c (7310B)
1 /* 2 This file is part of SYNC 3 Copyright (C) 2014-2019 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_backup_download.c 21 * @brief command to download data to the sync backend service. 22 * @author Christian Grothoff 23 */ 24 #include "platform.h" 25 #include "sync_service.h" 26 #include "sync_testing_lib.h" 27 #include <taler/taler_util.h> 28 #include <taler/taler_testing_lib.h> 29 30 /** 31 * State for a "backup download" CMD. 32 */ 33 struct BackupDownloadState 34 { 35 36 /** 37 * Eddsa public key. 38 */ 39 struct SYNC_AccountPublicKeyP sync_pub; 40 41 /** 42 * Hash of the upload (all zeros if there was no upload). 43 */ 44 const struct GNUNET_HashCode *upload_hash; 45 46 /** 47 * Hash of the previous upload (all zeros if there was no previous upload). 48 */ 49 const struct GNUNET_HashCode *prev_upload_hash; 50 51 /** 52 * The /backups POST operation handle. 53 */ 54 struct SYNC_DownloadOperation *download; 55 56 /** 57 * URL of the sync backend. 58 */ 59 const char *sync_url; 60 61 /** 62 * The interpreter state. 63 */ 64 struct TALER_TESTING_Interpreter *is; 65 66 /** 67 * Reference to upload command we expect to download. 68 */ 69 const char *upload_reference; 70 71 /** 72 * Expected status code. 73 */ 74 unsigned int http_status; 75 76 }; 77 78 79 /** 80 * Function called with the results of a #SYNC_download(). 81 * 82 * @param cls closure 83 * @param dd details about the download operation 84 */ 85 static void 86 backup_download_cb (void *cls, 87 const struct SYNC_DownloadDetails *dd) 88 { 89 struct BackupDownloadState *bds = cls; 90 91 bds->download = NULL; 92 if (dd->http_status != bds->http_status) 93 { 94 TALER_TESTING_unexpected_status (bds->is, 95 dd->http_status, 96 bds->http_status); 97 return; 98 } 99 if (NULL != bds->upload_reference) 100 { 101 if ( (MHD_HTTP_OK == dd->http_status) && 102 (0 != GNUNET_memcmp (&dd->details.ok.curr_backup_hash, 103 bds->upload_hash)) ) 104 { 105 GNUNET_break (0); 106 TALER_TESTING_interpreter_fail (bds->is); 107 return; 108 } 109 if ( (MHD_HTTP_OK == dd->http_status) && 110 (0 != GNUNET_memcmp (&dd->details.ok.prev_backup_hash, 111 bds->prev_upload_hash)) ) 112 { 113 GNUNET_break (0); 114 TALER_TESTING_interpreter_fail (bds->is); 115 return; 116 } 117 } 118 TALER_TESTING_interpreter_next (bds->is); 119 } 120 121 122 /** 123 * Run a "backup download" CMD. 124 * 125 * @param cls closure. 126 * @param cmd command currently being run. 127 * @param is interpreter state. 128 */ 129 static void 130 backup_download_run (void *cls, 131 const struct TALER_TESTING_Command *cmd, 132 struct TALER_TESTING_Interpreter *is) 133 { 134 struct BackupDownloadState *bds = cls; 135 136 bds->is = is; 137 if (NULL != bds->upload_reference) 138 { 139 const struct TALER_TESTING_Command *upload_cmd; 140 const struct SYNC_AccountPublicKeyP *sync_pub; 141 142 upload_cmd = TALER_TESTING_interpreter_lookup_command 143 (is, 144 bds->upload_reference); 145 if (NULL == upload_cmd) 146 { 147 GNUNET_break (0); 148 TALER_TESTING_interpreter_fail (bds->is); 149 return; 150 } 151 if (GNUNET_OK != 152 TALER_TESTING_get_trait_curr_hash (upload_cmd, 153 &bds->upload_hash)) 154 { 155 GNUNET_break (0); 156 TALER_TESTING_interpreter_fail (bds->is); 157 return; 158 } 159 if (GNUNET_OK != 160 TALER_TESTING_get_trait_prev_hash (upload_cmd, 161 &bds->prev_upload_hash)) 162 { 163 GNUNET_break (0); 164 TALER_TESTING_interpreter_fail (bds->is); 165 return; 166 } 167 if (GNUNET_OK != 168 TALER_TESTING_get_trait_sync_account_pub (upload_cmd, 169 &sync_pub)) 170 { 171 GNUNET_break (0); 172 TALER_TESTING_interpreter_fail (bds->is); 173 return; 174 } 175 bds->sync_pub = *sync_pub; 176 } 177 bds->download = SYNC_download (TALER_TESTING_interpreter_get_context (is), 178 bds->sync_url, 179 &bds->sync_pub, 180 &backup_download_cb, 181 bds); 182 if (NULL == bds->download) 183 { 184 GNUNET_break (0); 185 TALER_TESTING_interpreter_fail (bds->is); 186 return; 187 } 188 } 189 190 191 /** 192 * Free the state of a "backup download" CMD, and possibly 193 * cancel it if it did not complete. 194 * 195 * @param cls closure. 196 * @param cmd command being freed. 197 */ 198 static void 199 backup_download_cleanup (void *cls, 200 const struct TALER_TESTING_Command *cmd) 201 { 202 struct BackupDownloadState *bds = cls; 203 204 if (NULL != bds->download) 205 { 206 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 207 "Command '%s' did not complete (backup download)\n", 208 cmd->label); 209 SYNC_download_cancel (bds->download); 210 bds->download = NULL; 211 } 212 GNUNET_free (bds); 213 } 214 215 216 /** 217 * Make the "backup download" command. 218 * 219 * @param label command label 220 * @param sync_url base URL of the sync serving 221 * the policy store request. 222 * @param http_status expected HTTP status. 223 * @param upload_ref reference to upload command 224 * @return the command 225 */ 226 struct TALER_TESTING_Command 227 SYNC_TESTING_cmd_backup_download (const char *label, 228 const char *sync_url, 229 unsigned int http_status, 230 const char *upload_ref) 231 { 232 struct BackupDownloadState *bds; 233 234 GNUNET_assert (NULL != upload_ref); 235 bds = GNUNET_new (struct BackupDownloadState); 236 bds->http_status = http_status; 237 bds->sync_url = sync_url; 238 bds->upload_reference = upload_ref; 239 { 240 struct TALER_TESTING_Command cmd = { 241 .cls = bds, 242 .label = label, 243 .run = &backup_download_run, 244 .cleanup = &backup_download_cleanup 245 }; 246 247 return cmd; 248 } 249 } 250 251 252 /** 253 * Make the "backup download" command for a non-existent upload. 254 * 255 * @param label command label 256 * @param sync_url base URL of the sync serving 257 * the policy store request. 258 * @return the command 259 */ 260 struct TALER_TESTING_Command 261 SYNC_TESTING_cmd_backup_nx (const char *label, 262 const char *sync_url) 263 { 264 struct BackupDownloadState *bds; 265 struct GNUNET_CRYPTO_EddsaPrivateKey priv; 266 267 bds = GNUNET_new (struct BackupDownloadState); 268 bds->http_status = MHD_HTTP_NOT_FOUND; 269 bds->sync_url = sync_url; 270 GNUNET_CRYPTO_eddsa_key_create (&priv); 271 GNUNET_CRYPTO_eddsa_key_get_public (&priv, 272 &bds->sync_pub.eddsa_pub); 273 { 274 struct TALER_TESTING_Command cmd = { 275 .cls = bds, 276 .label = label, 277 .run = &backup_download_run, 278 .cleanup = &backup_download_cleanup 279 }; 280 281 return cmd; 282 } 283 }