From 89431a41b795cfbd14d6ce08dc33272e4a9ab9e6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 6 Apr 2022 13:54:08 +0200 Subject: -complete va coin parser of purse_create_deposit CMD --- src/include/taler_testing_lib.h | 15 +++++ src/testing/Makefile.am | 1 + src/testing/testing_api_cmd_common.c | 64 ++++++++++++++++++++++ src/testing/testing_api_cmd_purse_create_deposit.c | 37 ++++++++++++- src/testing/testing_api_cmd_recoup.c | 58 +++----------------- src/testing/testing_api_cmd_recoup_refresh.c | 58 +++----------------- src/testing/testing_api_cmd_withdraw.c | 51 ++--------------- 7 files changed, 135 insertions(+), 149 deletions(-) create mode 100644 src/testing/testing_api_cmd_common.c (limited to 'src') diff --git a/src/include/taler_testing_lib.h b/src/include/taler_testing_lib.h index 6980d9d81..7c4071521 100644 --- a/src/include/taler_testing_lib.h +++ b/src/include/taler_testing_lib.h @@ -941,6 +941,21 @@ TALER_TESTING_has_in_name (const char *prog, const char *marker); +/** + * Parse reference to a coin. + * + * @param coin_reference of format $LABEL['#' $INDEX]? + * @param[out] cref where we return a copy of $LABEL + * @param[out] idx where we set $INDEX + * @return #GNUNET_SYSERR if $INDEX is present but not numeric + */ +enum GNUNET_GenericReturnValue +TALER_TESTING_parse_coin_reference ( + const char *coin_reference, + char **cref, + unsigned int *idx); + + /* ************** Specific interpreter commands ************ */ diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am index c0e675883..3f1afd692 100644 --- a/src/testing/Makefile.am +++ b/src/testing/Makefile.am @@ -54,6 +54,7 @@ libtalertesting_la_SOURCES = \ testing_api_cmd_batch.c \ testing_api_cmd_change_auth.c \ testing_api_cmd_check_keys.c \ + testing_api_cmd_common.c \ testing_api_cmd_deposit.c \ testing_api_cmd_deposits_get.c \ testing_api_cmd_exec_aggregator.c \ diff --git a/src/testing/testing_api_cmd_common.c b/src/testing/testing_api_cmd_common.c new file mode 100644 index 000000000..2d828a2b0 --- /dev/null +++ b/src/testing/testing_api_cmd_common.c @@ -0,0 +1,64 @@ +/* + This file is part of TALER + Copyright (C) 2018-2022 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 published by + the Free Software Foundation; either version 3, or (at your + option) any later version. + + TALER is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with TALER; see the file COPYING. If not, see + +*/ +/** + * @file testing/testing_api_cmd_common.c + * @brief common functions for commands + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler_testing_lib.h" + + +enum GNUNET_GenericReturnValue +TALER_TESTING_parse_coin_reference ( + const char *coin_reference, + char **cref, + unsigned int *idx) +{ + const char *index; + char dummy; + + /* We allow command references of the form "$LABEL#$INDEX" or + just "$LABEL", which implies the index is 0. Figure out + which one it is. */ + index = strchr (coin_reference, '#'); + if (NULL == index) + { + *idx = 0; + *cref = GNUNET_strdup (coin_reference); + return GNUNET_OK; + } + *cref = GNUNET_strndup (coin_reference, + index - coin_reference); + if (1 != sscanf (index + 1, + "%u%c", + idx, + &dummy)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Numeric index (not `%s') required after `#' in command reference of command in %s:%u\n", + index, + __FILE__, + __LINE__); + GNUNET_free (*cref); + *cref = NULL; + return GNUNET_SYSERR; + } + return GNUNET_OK; +} diff --git a/src/testing/testing_api_cmd_purse_create_deposit.c b/src/testing/testing_api_cmd_purse_create_deposit.c index 8aeab744f..af7ed3f5d 100644 --- a/src/testing/testing_api_cmd_purse_create_deposit.c +++ b/src/testing/testing_api_cmd_purse_create_deposit.c @@ -36,7 +36,7 @@ struct Coin /** * Reference to the respective command. */ - const char *command_ref; + char *command_ref; /** * index of the specific coin in the traits of @e command_ref. @@ -308,6 +308,8 @@ deposit_cleanup (void *cls, TALER_EXCHANGE_purse_create_with_deposit_cancel (ds->dh); ds->dh = NULL; } + for (unsigned int i = 0; inum_coin_references; i++) + GNUNET_free (ds->coin_references[i].command_ref); json_decref (ds->contract_terms); GNUNET_free (ds->coin_references); GNUNET_free (ds); @@ -376,7 +378,38 @@ TALER_TESTING_cmd_purse_create_with_deposit ( label); GNUNET_assert (0); } - // FIXME: parse varargs! + { + va_list ap; + unsigned int i; + const char *ref; + const char *val; + + va_start (ap, purse_expiration); + while (NULL != (va_arg (ap, const char *))) + ds->num_coin_references++; + va_end (ap); + GNUNET_assert (0 == (ds->num_coin_references % 2)); + ds->num_coin_references /= 2; + ds->coin_references = GNUNET_new_array (ds->num_coin_references, + struct Coin); + i = 0; + va_start (ap, purse_expiration); + while (NULL != (ref = va_arg (ap, const char *))) + { + struct Coin *c = &ds->coin_references[i++]; + + GNUNET_assert (NULL != (val = va_arg (ap, const char *))); + GNUNET_assert (GNUNET_OK == + TALER_TESTING_parse_coin_reference ( + ref, + &c->command_ref, + &c->coin_index)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount (val, + &c->deposit_with_fee)); + } + va_end (ap); + } GNUNET_assert (GNUNET_OK == TALER_string_to_amount (target_amount, &ds->target_amount)); diff --git a/src/testing/testing_api_cmd_recoup.c b/src/testing/testing_api_cmd_recoup.c index ef2716226..1a8290206 100644 --- a/src/testing/testing_api_cmd_recoup.c +++ b/src/testing/testing_api_cmd_recoup.c @@ -67,50 +67,6 @@ struct RecoupState }; -/** - * Parser reference to a coin. - * - * @param coin_reference of format $LABEL['#' $INDEX]? - * @param[out] cref where we return a copy of $LABEL - * @param[out] idx where we set $INDEX - * @return #GNUNET_SYSERR if $INDEX is present but not numeric - */ -static enum GNUNET_GenericReturnValue -parse_coin_reference (const char *coin_reference, - char **cref, - unsigned int *idx) -{ - const char *index; - - /* We allow command references of the form "$LABEL#$INDEX" or - just "$LABEL", which implies the index is 0. Figure out - which one it is. */ - index = strchr (coin_reference, '#'); - if (NULL == index) - { - *idx = 0; - *cref = GNUNET_strdup (coin_reference); - return GNUNET_OK; - } - *cref = GNUNET_strndup (coin_reference, - index - coin_reference); - if (1 != sscanf (index + 1, - "%u", - idx)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Numeric index (not `%s') required after `#' in command reference of command in %s:%u\n", - index, - __FILE__, - __LINE__); - GNUNET_free (*cref); - *cref = NULL; - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - /** * Check the result of the recoup request: checks whether * the HTTP response code is good, and that the coin that @@ -151,9 +107,10 @@ recoup_cb (void *cls, } if (GNUNET_OK != - parse_coin_reference (ps->coin_reference, - &cref, - &idx)) + TALER_TESTING_parse_coin_reference ( + ps->coin_reference, + &cref, + &idx)) { TALER_TESTING_interpreter_fail (is); return; @@ -246,9 +203,10 @@ recoup_run (void *cls, ps->is = is; if (GNUNET_OK != - parse_coin_reference (ps->coin_reference, - &cref, - &idx)) + TALER_TESTING_parse_coin_reference ( + ps->coin_reference, + &cref, + &idx)) { TALER_TESTING_interpreter_fail (is); return; diff --git a/src/testing/testing_api_cmd_recoup_refresh.c b/src/testing/testing_api_cmd_recoup_refresh.c index 1102fc757..6081a4ba1 100644 --- a/src/testing/testing_api_cmd_recoup_refresh.c +++ b/src/testing/testing_api_cmd_recoup_refresh.c @@ -67,50 +67,6 @@ struct RecoupRefreshState }; -/** - * Parser reference to a coin. - * - * @param coin_reference of format $LABEL['#' $INDEX]? - * @param[out] cref where we return a copy of $LABEL - * @param[out] idx where we set $INDEX - * @return #GNUNET_SYSERR if $INDEX is present but not numeric - */ -static enum GNUNET_GenericReturnValue -parse_coin_reference (const char *coin_reference, - char **cref, - unsigned int *idx) -{ - const char *index; - - /* We allow command references of the form "$LABEL#$INDEX" or - just "$LABEL", which implies the index is 0. Figure out - which one it is. */ - index = strchr (coin_reference, '#'); - if (NULL == index) - { - *idx = 0; - *cref = GNUNET_strdup (coin_reference); - return GNUNET_OK; - } - *cref = GNUNET_strndup (coin_reference, - index - coin_reference); - if (1 != sscanf (index + 1, - "%u", - idx)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Numeric index (not `%s') required after `#' in command reference of command in %s:%u\n", - index, - __FILE__, - __LINE__); - GNUNET_free (*cref); - *cref = NULL; - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - /** * Check the result of the recoup_refresh request: checks whether * the HTTP response code is good, and that the coin that @@ -150,9 +106,10 @@ recoup_refresh_cb (void *cls, } if (GNUNET_OK != - parse_coin_reference (rrs->coin_reference, - &cref, - &idx)) + TALER_TESTING_parse_coin_reference ( + rrs->coin_reference, + &cref, + &idx)) { TALER_TESTING_interpreter_fail (is); return; @@ -242,9 +199,10 @@ recoup_refresh_run (void *cls, rrs->is = is; if (GNUNET_OK != - parse_coin_reference (rrs->coin_reference, - &cref, - &idx)) + TALER_TESTING_parse_coin_reference ( + rrs->coin_reference, + &cref, + &idx)) { TALER_TESTING_interpreter_fail (is); return; diff --git a/src/testing/testing_api_cmd_withdraw.c b/src/testing/testing_api_cmd_withdraw.c index a38233980..80f8402cf 100644 --- a/src/testing/testing_api_cmd_withdraw.c +++ b/src/testing/testing_api_cmd_withdraw.c @@ -332,50 +332,6 @@ reserve_withdraw_cb (void *cls, } -/** - * Parser reference to a coin. - * - * @param coin_reference of format $LABEL['#' $INDEX]? - * @param[out] cref where we return a copy of $LABEL - * @param[out] idx where we set $INDEX - * @return #GNUNET_SYSERR if $INDEX is present but not numeric - */ -static enum GNUNET_GenericReturnValue -parse_coin_reference (const char *coin_reference, - char **cref, - unsigned int *idx) -{ - const char *index; - - /* We allow command references of the form "$LABEL#$INDEX" or - just "$LABEL", which implies the index is 0. Figure out - which one it is. */ - index = strchr (coin_reference, '#'); - if (NULL == index) - { - *idx = 0; - *cref = GNUNET_strdup (coin_reference); - return GNUNET_OK; - } - *cref = GNUNET_strndup (coin_reference, - index - coin_reference); - if (1 != sscanf (index + 1, - "%u", - idx)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Numeric index (not `%s') required after `#' in command reference of command in %s:%u\n", - index, - __FILE__, - __LINE__); - GNUNET_free (*cref); - *cref = NULL; - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - /** * Run the command. */ @@ -434,9 +390,10 @@ withdraw_run (void *cls, unsigned int index; GNUNET_assert (GNUNET_OK == - parse_coin_reference (ws->reuse_coin_key_ref, - &cstr, - &index)); + TALER_TESTING_parse_coin_reference ( + ws->reuse_coin_key_ref, + &cstr, + &index)); cref = TALER_TESTING_interpreter_lookup_command (is, cstr); GNUNET_assert (NULL != cref); -- cgit v1.2.3