From c4b37438f88051d4271af0b3db3aed6deb6ec3d8 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 20 Jun 2023 20:39:40 +0200 Subject: -modernize sync API for 0.9.3 --- src/include/sync_service.h | 125 ++++++++++++++------- src/lib/sync_api_download.c | 23 ++-- src/lib/sync_api_upload.c | 51 ++++----- src/syncdb/test_sync_db.c | 4 +- src/testing/test_sync_api.c | 2 +- src/testing/testing_api_cmd_backup_download.c | 26 ++--- src/testing/testing_api_cmd_backup_upload.c | 149 ++++++++++++-------------- 7 files changed, 201 insertions(+), 179 deletions(-) (limited to 'src') diff --git a/src/include/sync_service.h b/src/include/sync_service.h index 8b47f33..2702e70 100644 --- a/src/include/sync_service.h +++ b/src/include/sync_service.h @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2019 Taler Systems SA + Copyright (C) 2019-2023 Taler Systems SA Anastasis is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software @@ -138,19 +138,53 @@ enum SYNC_UploadStatus */ struct SYNC_UploadDetails { + + /** + * Taler error code. + */ + enum TALER_ErrorCode ec; + + /** + * HTTP status of the request. + */ + unsigned int http_status; + /** * High level status of the upload operation. */ enum SYNC_UploadStatus us; + /** + * Details depending on @e us. + */ union { /** - * Hash of the synchronized backup, returned if - * @e us is #SYNC_US_SUCCESS. + * Data returned if @e us is #SYNC_US_SUCCESS. */ - const struct GNUNET_HashCode *curr_backup_hash; + struct + { + + /** + * Hash of the synchronized backup. + */ + const struct GNUNET_HashCode *curr_backup_hash; + + } success; + + /** + * Data returned if @e us is #SYNC_US_NOT_MODIFIED. + */ + struct + { + + /** + * Hash of the synchronized backup. + */ + const struct GNUNET_HashCode *curr_backup_hash; + + } not_modified; /** * Previous backup. Returned if @e us is @@ -177,11 +211,15 @@ struct SYNC_UploadDetails } recovered_backup; - /** - * A taler://pay/-URI with a request to pay the annual fee for - * the service. Returned if @e us is #SYNC_US_PAYMENT_REQUIRED. - */ - const char *payment_request; + struct + { + /** + * A taler://pay/-URI with a request to pay the annual fee for + * the service. Returned if @e us is #SYNC_US_PAYMENT_REQUIRED. + */ + const char *payment_request; + + } payment_required; } details; @@ -192,14 +230,10 @@ struct SYNC_UploadDetails * Function called with the results of a #SYNC_upload(). * * @param cls closure - * @param ec Taler error code - * @param http_status HTTP status of the request * @param ud details about the upload operation */ typedef void (*SYNC_UploadCallback)(void *cls, - enum TALER_ErrorCode ec, - unsigned int http_status, const struct SYNC_UploadDetails *ud); @@ -291,50 +325,63 @@ SYNC_upload_cancel (struct SYNC_UploadOperation *uo); */ struct SYNC_DownloadDetails { - /** - * Signature (already verified). - */ - struct SYNC_AccountSignatureP sig; /** - * Hash of the previous version. + * HTTP status code. */ - struct GNUNET_HashCode prev_backup_hash; + unsigned int http_status; /** - * Hash over @e backup and @e backup_size. + * Details depending on @e http_status. */ - struct GNUNET_HashCode curr_backup_hash; + union + { - /** - * The backup we downloaded. - */ - const void *backup; + /** + * Details if status is #MHD_HTTP_OK. + */ + struct + { - /** - * Number of bytes in @e backup. - */ - size_t backup_size; + /** + * Signature (already verified). + */ + struct SYNC_AccountSignatureP sig; + + /** + * Hash of the previous version. + */ + struct GNUNET_HashCode prev_backup_hash; + + /** + * Hash over @e backup and @e backup_size. + */ + struct GNUNET_HashCode curr_backup_hash; + + /** + * The backup we downloaded. + */ + const void *backup; + + /** + * Number of bytes in @e backup. + */ + size_t backup_size; + } ok; + + } details; }; + /** * Function called with the results of a #SYNC_download(). * * @param cls closure - * @param sig signature of the account owner, affirming the - * integrity of the backup (already verified) - * @param prev_backup_hash hash of the previous backup (used - * to verify the signature, could be used by clients - * to verify backup chains) - * @param curr_backup_hash hash over @a backup (verified) - * @param backup_size number of bytes in @a backup - * @param backup the latest backup as downloaded from the - * server and affirmed by @a sig + * @param dd download details */ typedef void (*SYNC_DownloadCallback)(void *cls, - unsigned int http_status, const struct SYNC_DownloadDetails *dd); diff --git a/src/lib/sync_api_download.c b/src/lib/sync_api_download.c index 9a7c8f5..a5515dd 100644 --- a/src/lib/sync_api_download.c +++ b/src/lib/sync_api_download.c @@ -100,6 +100,9 @@ handle_download_finished (void *cls, size_t data_size) { struct SYNC_DownloadOperation *download = cls; + struct SYNC_DownloadDetails dd = { + .http_status = (unsigned int) response_code + }; download->job = NULL; switch (response_code) @@ -108,7 +111,6 @@ handle_download_finished (void *cls, break; case MHD_HTTP_OK: { - struct SYNC_DownloadDetails dd; struct SYNC_UploadSignaturePS usp = { .purpose.purpose = htonl (TALER_SIGNATURE_SYNC_BACKUP_UPLOAD), .purpose.size = htonl (sizeof (usp)), @@ -125,18 +127,16 @@ handle_download_finished (void *cls, &download->account_pub.eddsa_pub)) { GNUNET_break_op (0); - response_code = 0; + dd.http_status = 0; break; } /* Success, call callback with all details! */ - memset (&dd, 0, sizeof (dd)); - dd.sig = download->account_sig; - dd.prev_backup_hash = download->sync_previous; - dd.curr_backup_hash = usp.new_backup_hash; - dd.backup = data; - dd.backup_size = data_size; + dd.details.ok.sig = download->account_sig; + dd.details.ok.prev_backup_hash = download->sync_previous; + dd.details.ok.curr_backup_hash = usp.new_backup_hash; + dd.details.ok.backup = data; + dd.details.ok.backup_size = data_size; download->cb (download->cb_cls, - response_code, &dd); download->cb = NULL; SYNC_download_cancel (download); @@ -159,14 +159,13 @@ handle_download_finished (void *cls, "Unexpected response code %u\n", (unsigned int) response_code); GNUNET_break (0); - response_code = 0; + dd.http_status = 0; break; } if (NULL != download->cb) { download->cb (download->cb_cls, - response_code, - NULL); + &dd); download->cb = NULL; } SYNC_download_cancel (download); diff --git a/src/lib/sync_api_upload.c b/src/lib/sync_api_upload.c index daaa2eb..26d3708 100644 --- a/src/lib/sync_api_upload.c +++ b/src/lib/sync_api_upload.c @@ -92,44 +92,40 @@ handle_upload_finished (void *cls, size_t data_size) { struct SYNC_UploadOperation *uo = cls; - enum TALER_ErrorCode ec = TALER_EC_INVALID; - struct SYNC_UploadDetails ud; - struct SYNC_UploadDetails *udp; + struct SYNC_UploadDetails ud = { + .http_status = (unsigned int) response_code, + .ec = TALER_EC_INVALID + }; uo->job = NULL; - udp = NULL; - memset (&ud, 0, sizeof (ud)); switch (response_code) { case 0: break; case MHD_HTTP_NO_CONTENT: ud.us = SYNC_US_SUCCESS; - ud.details.curr_backup_hash = &uo->new_upload_hash; - udp = &ud; - ec = TALER_EC_NONE; + ud.details.success.curr_backup_hash = &uo->new_upload_hash; + ud.ec = TALER_EC_NONE; break; case MHD_HTTP_NOT_MODIFIED: ud.us = SYNC_US_SUCCESS; - ud.details.curr_backup_hash = &uo->new_upload_hash; - udp = &ud; - ec = TALER_EC_NONE; + ud.details.not_modified.curr_backup_hash = &uo->new_upload_hash; + ud.ec = TALER_EC_NONE; break; case MHD_HTTP_BAD_REQUEST: GNUNET_break (0); - ec = TALER_JSON_get_error_code2 (data, - data_size); + ud.ec = TALER_JSON_get_error_code2 (data, + data_size); break; case MHD_HTTP_PAYMENT_REQUIRED: ud.us = SYNC_US_PAYMENT_REQUIRED; - ud.details.payment_request = uo->pay_uri; - udp = &ud; - ec = TALER_EC_NONE; + ud.details.payment_required.payment_request = uo->pay_uri; + ud.ec = TALER_EC_NONE; break; case MHD_HTTP_FORBIDDEN: GNUNET_break (0); - ec = TALER_JSON_get_error_code2 (data, - data_size); + ud.ec = TALER_JSON_get_error_code2 (data, + data_size); break; case MHD_HTTP_CONFLICT: ud.us = SYNC_US_CONFLICTING_BACKUP; @@ -140,23 +136,22 @@ handle_upload_finished (void *cls, = data_size; ud.details.recovered_backup.existing_backup = data; - udp = &ud; - ec = TALER_EC_NONE; + ud.ec = TALER_EC_NONE; break; case MHD_HTTP_GONE: - ec = TALER_JSON_get_error_code2 (data, - data_size); + ud.ec = TALER_JSON_get_error_code2 (data, + data_size); break; case MHD_HTTP_LENGTH_REQUIRED: GNUNET_break (0); break; case MHD_HTTP_REQUEST_ENTITY_TOO_LARGE: - ec = TALER_JSON_get_error_code2 (data, - data_size); + ud.ec = TALER_JSON_get_error_code2 (data, + data_size); break; case MHD_HTTP_TOO_MANY_REQUESTS: - ec = TALER_JSON_get_error_code2 (data, - data_size); + ud.ec = TALER_JSON_get_error_code2 (data, + data_size); break; case MHD_HTTP_INTERNAL_SERVER_ERROR: GNUNET_log (GNUNET_ERROR_TYPE_WARNING, @@ -168,9 +163,7 @@ handle_upload_finished (void *cls, if (NULL != uo->cb) { uo->cb (uo->cb_cls, - ec, - response_code, - udp); + &ud); uo->cb = NULL; } SYNC_upload_cancel (uo); diff --git a/src/syncdb/test_sync_db.c b/src/syncdb/test_sync_db.c index 73c5e1f..d01941b 100644 --- a/src/syncdb/test_sync_db.c +++ b/src/syncdb/test_sync_db.c @@ -274,7 +274,9 @@ main (int argc, GNUNET_break (0); return EXIT_FAILURE; } - GNUNET_log_setup (argv[0], "DEBUG", NULL); + GNUNET_log_setup (argv[0], + "DEBUG", + NULL); (void) TALER_project_data_default (); GNUNET_OS_init (SYNC_project_data_default ()); plugin_name++; diff --git a/src/testing/test_sync_api.c b/src/testing/test_sync_api.c index 9dc4b03..70c94b8 100644 --- a/src/testing/test_sync_api.c +++ b/src/testing/test_sync_api.c @@ -1,6 +1,6 @@ /* This file is part of TALER - Copyright (C) 2014-2020 Taler Systems SA + Copyright (C) 2014-2023 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as diff --git a/src/testing/testing_api_cmd_backup_download.c b/src/testing/testing_api_cmd_backup_download.c index e6bfad4..7ed1f3b 100644 --- a/src/testing/testing_api_cmd_backup_download.c +++ b/src/testing/testing_api_cmd_backup_download.c @@ -80,40 +80,32 @@ struct BackupDownloadState * Function called with the results of a #SYNC_download(). * * @param cls closure - * @param http_status HTTP status of the request - * @param ud details about the download operation + * @param dd details about the download operation */ static void backup_download_cb (void *cls, - unsigned int http_status, const struct SYNC_DownloadDetails *dd) { struct BackupDownloadState *bds = cls; bds->download = NULL; - if (http_status != bds->http_status) + if (dd->http_status != bds->http_status) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Unexpected response code %u to command %s in %s:%u\n", - http_status, - bds->is->commands[bds->is->ip].label, - __FILE__, - __LINE__); - TALER_TESTING_interpreter_fail (bds->is); - return; + TALER_TESTING_unexpected_status (bds->is, + dd->http_status); } if (NULL != bds->upload_reference) { - if ( (MHD_HTTP_OK == http_status) && - (0 != GNUNET_memcmp (&dd->curr_backup_hash, + if ( (MHD_HTTP_OK == dd->http_status) && + (0 != GNUNET_memcmp (&dd->details.ok.curr_backup_hash, bds->upload_hash)) ) { GNUNET_break (0); TALER_TESTING_interpreter_fail (bds->is); return; } - if ( (MHD_HTTP_OK == http_status) && - (0 != GNUNET_memcmp (&dd->prev_backup_hash, + if ( (MHD_HTTP_OK == dd->http_status) && + (0 != GNUNET_memcmp (&dd->details.ok.prev_backup_hash, bds->prev_upload_hash)) ) { GNUNET_break (0); @@ -183,7 +175,7 @@ backup_download_run (void *cls, } bds->sync_pub = *sync_pub; } - bds->download = SYNC_download (is->ctx, + bds->download = SYNC_download (TALER_TESTING_interpreter_get_context (is), bds->sync_url, &bds->sync_pub, &backup_download_cb, diff --git a/src/testing/testing_api_cmd_backup_upload.c b/src/testing/testing_api_cmd_backup_upload.c index d2c3851..090e74b 100644 --- a/src/testing/testing_api_cmd_backup_upload.c +++ b/src/testing/testing_api_cmd_backup_upload.c @@ -1,6 +1,6 @@ /* This file is part of SYNC - Copyright (C) 2014-2019 Taler Systems SA + Copyright (C) 2014-2023 Taler Systems SA SYNC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -126,100 +126,89 @@ struct BackupUploadState * Function called with the results of a #SYNC_upload(). * * @param cls closure - * @param ec Taler error code - * @param http_status HTTP status of the request * @param ud details about the upload operation */ static void backup_upload_cb (void *cls, - enum TALER_ErrorCode ec, - unsigned int http_status, const struct SYNC_UploadDetails *ud) { struct BackupUploadState *bus = cls; bus->uo = NULL; - if (http_status != bus->http_status) + if (ud->http_status != bus->http_status) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Unexpected response code %u to command %s in %s:%u\n", - http_status, - bus->is->commands[bus->is->ip].label, - __FILE__, - __LINE__); - TALER_TESTING_interpreter_fail (bus->is); - return; + TALER_TESTING_unexpected_status (bus->is, + ud->http_status); } - if (NULL != ud) + switch (ud->us) { - switch (ud->us) + case SYNC_US_SUCCESS: + if (0 != GNUNET_memcmp ( + &bus->curr_hash, + ud->details.success.curr_backup_hash)) + { + GNUNET_break (0); + TALER_TESTING_interpreter_fail (bus->is); + return; + } + break; + case SYNC_US_PAYMENT_REQUIRED: { - case SYNC_US_SUCCESS: - if (0 != GNUNET_memcmp (&bus->curr_hash, - ud->details.curr_backup_hash)) + struct TALER_MERCHANT_PayUriData pd; + + if (GNUNET_OK != + TALER_MERCHANT_parse_pay_uri ( + ud->details.payment_required.payment_request, + &pd)) { GNUNET_break (0); TALER_TESTING_interpreter_fail (bus->is); return; } - break; - case SYNC_US_PAYMENT_REQUIRED: - { - struct TALER_MERCHANT_PayUriData pd; - - if (GNUNET_OK != - TALER_MERCHANT_parse_pay_uri (ud->details.payment_request, - &pd)) - { - GNUNET_break (0); - TALER_TESTING_interpreter_fail (bus->is); - return; - } - bus->payment_order_id = GNUNET_strdup (pd.order_id); - if (NULL != pd.claim_token) - bus->token = *pd.claim_token; - TALER_MERCHANT_parse_pay_uri_free (&pd); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Order ID from Sync service is `%s'\n", - bus->payment_order_id); - memset (&bus->curr_hash, - 0, - sizeof (struct GNUNET_HashCode)); - } - break; - case SYNC_US_CONFLICTING_BACKUP: + bus->payment_order_id = GNUNET_strdup (pd.order_id); + if (NULL != pd.claim_token) + bus->token = *pd.claim_token; + TALER_MERCHANT_parse_pay_uri_free (&pd); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Order ID from Sync service is `%s'\n", + bus->payment_order_id); + memset (&bus->curr_hash, + 0, + sizeof (struct GNUNET_HashCode)); + } + break; + case SYNC_US_CONFLICTING_BACKUP: + { + const struct TALER_TESTING_Command *ref; + const struct GNUNET_HashCode *h; + + ref = TALER_TESTING_interpreter_lookup_command + (bus->is, + bus->last_upload); + GNUNET_assert (NULL != ref); + GNUNET_assert (GNUNET_OK == + SYNC_TESTING_get_trait_hash (ref, + SYNC_TESTING_TRAIT_HASH_CURRENT, + &h)); + if (0 != GNUNET_memcmp (h, + &ud->details.recovered_backup. + existing_backup_hash)) { - const struct TALER_TESTING_Command *ref; - const struct GNUNET_HashCode *h; - - ref = TALER_TESTING_interpreter_lookup_command - (bus->is, - bus->last_upload); - GNUNET_assert (NULL != ref); - GNUNET_assert (GNUNET_OK == - SYNC_TESTING_get_trait_hash (ref, - SYNC_TESTING_TRAIT_HASH_CURRENT, - &h)); - if (0 != GNUNET_memcmp (h, - &ud->details.recovered_backup. - existing_backup_hash)) - { - GNUNET_break (0); - TALER_TESTING_interpreter_fail (bus->is); - return; - } + GNUNET_break (0); + TALER_TESTING_interpreter_fail (bus->is); + return; } - case SYNC_US_HTTP_ERROR: - break; - case SYNC_US_CLIENT_ERROR: - GNUNET_break (0); - TALER_TESTING_interpreter_fail (bus->is); - return; - case SYNC_US_SERVER_ERROR: - GNUNET_break (0); - TALER_TESTING_interpreter_fail (bus->is); - return; } + case SYNC_US_HTTP_ERROR: + break; + case SYNC_US_CLIENT_ERROR: + GNUNET_break (0); + TALER_TESTING_interpreter_fail (bus->is); + return; + case SYNC_US_SERVER_ERROR: + GNUNET_break (0); + TALER_TESTING_interpreter_fail (bus->is); + return; } TALER_TESTING_interpreter_next (bus->is); } @@ -294,7 +283,7 @@ backup_upload_run (void *cls, } if (0 != (SYNC_TESTING_UO_REFERENCE_ORDER_ID & bus->uopt)) { - const char **order_id; + const char *order_id; if (GNUNET_OK != TALER_TESTING_get_trait_order_id (ref, @@ -304,7 +293,7 @@ backup_upload_run (void *cls, TALER_TESTING_interpreter_fail (bus->is); return; } - bus->payment_order_req = *order_id; + bus->payment_order_req = order_id; if (NULL == bus->payment_order_req) { GNUNET_break (0); @@ -326,7 +315,7 @@ backup_upload_run (void *cls, GNUNET_CRYPTO_hash (bus->backup, bus->backup_size, &bus->curr_hash); - bus->uo = SYNC_upload (is->ctx, + bus->uo = SYNC_upload (TALER_TESTING_interpreter_get_context (is), bus->sync_url, &bus->sync_priv, ( ( (NULL != bus->prev_upload) && @@ -387,7 +376,7 @@ backup_upload_cleanup (void *cls, * @param index index number of the object to extract. * @return #GNUNET_OK on success */ -static int +static enum GNUNET_GenericReturnValue backup_upload_traits (void *cls, const void **ret, const char *trait, @@ -404,7 +393,7 @@ backup_upload_traits (void *cls, &bus->sync_pub), SYNC_TESTING_make_trait_account_priv (0, &bus->sync_priv), - TALER_TESTING_make_trait_order_id (&bus->payment_order_id), + TALER_TESTING_make_trait_order_id (bus->payment_order_id), TALER_TESTING_trait_end () }; struct TALER_TESTING_Trait ftraits[] = { @@ -413,7 +402,7 @@ backup_upload_traits (void *cls, &bus->sync_pub), SYNC_TESTING_make_trait_account_priv (0, &bus->sync_priv), - TALER_TESTING_make_trait_order_id (&bus->payment_order_id), + TALER_TESTING_make_trait_order_id (bus->payment_order_id), TALER_TESTING_trait_end () }; -- cgit v1.2.3