From 42bc31744b9810509aef344c54bfee2f4e2a7ccb Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 4 Feb 2020 21:59:43 +0100 Subject: implement /config in fakebank and taler_bank_lib.h (#6066) --- src/testing/test_bank_api.c | 36 +++++++++--- src/testing/test_taler_exchange_aggregator.c | 78 ++++++++++++------------ src/testing/testing_api_helpers_bank.c | 7 ++- src/testing/testing_api_helpers_exchange.c | 2 +- src/testing/testing_api_loop.c | 88 +++++++++++++--------------- 5 files changed, 117 insertions(+), 94 deletions(-) (limited to 'src/testing') diff --git a/src/testing/test_bank_api.c b/src/testing/test_bank_api.c index bdafdc5fd..75e22b59e 100644 --- a/src/testing/test_bank_api.c +++ b/src/testing/test_bank_api.c @@ -52,6 +52,7 @@ static struct GNUNET_OS_Process *bankd; */ static int with_fakebank; + /** * Main function that will tell the interpreter what commands to * run. @@ -116,12 +117,32 @@ run (void *cls, } +/** + * Runs #TALER_TESTING_setup() using the configuration. + * + * @param cls unused + * @param cfg configuration to use + * @return status code + */ +static int +setup_with_cfg (void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + (void) cls; + return TALER_TESTING_setup (&run, + NULL, + cfg, + NULL, + GNUNET_NO); +} + + int main (int argc, char *const *argv) { - int rv; const char *cfgfilename; + int rv; /* These environment variables get in the way... */ unsetenv ("XDG_DATA_HOME"); @@ -165,12 +186,13 @@ main (int argc, return 77; } } - - rv = (GNUNET_OK == TALER_TESTING_setup (&run, - NULL, - cfgfilename, - NULL, - GNUNET_NO)) ? 0 : 1; + if (GNUNET_OK != + GNUNET_CONFIGURATION_parse_and_run (cfgfilename, + &setup_with_cfg, + NULL)) + rv = 1; + else + rv = 0; if (GNUNET_NO == with_fakebank) { diff --git a/src/testing/test_taler_exchange_aggregator.c b/src/testing/test_taler_exchange_aggregator.c index c709e5fe9..2e8a35ce2 100644 --- a/src/testing/test_taler_exchange_aggregator.c +++ b/src/testing/test_taler_exchange_aggregator.c @@ -57,36 +57,6 @@ static char *config_filename; #define USER42_ACCOUNT "42" -/** - * @return GNUNET_NO if database could not be prepared, - * otherwise GNUNET_OK - */ -static int -prepare_database (void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg) -{ - dbc.plugin = TALER_EXCHANGEDB_plugin_load (cfg); - if (NULL == dbc.plugin) - { - GNUNET_break (0); - result = 77; - return GNUNET_NO; - } - if (GNUNET_OK != - dbc.plugin->create_tables (dbc.plugin->cls)) - { - GNUNET_break (0); - TALER_EXCHANGEDB_plugin_unload (dbc.plugin); - dbc.plugin = NULL; - result = 77; - return GNUNET_NO; - } - dbc.session = dbc.plugin->get_session (dbc.plugin->cls); - GNUNET_assert (NULL != dbc.session); - - return GNUNET_OK; -} - /** * Collects all the tests. @@ -454,6 +424,48 @@ run (void *cls, } +/** + * Prepare database an launch the test. + * + * @param cls unused + * @param cfg our configuration + * @return #GNUNET_NO if database could not be prepared, + * otherwise #GNUNET_OK + */ +static int +prepare_database (void *cls, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + dbc.plugin = TALER_EXCHANGEDB_plugin_load (cfg); + if (NULL == dbc.plugin) + { + GNUNET_break (0); + result = 77; + return GNUNET_NO; + } + if (GNUNET_OK != + dbc.plugin->create_tables (dbc.plugin->cls)) + { + GNUNET_break (0); + TALER_EXCHANGEDB_plugin_unload (dbc.plugin); + dbc.plugin = NULL; + result = 77; + return GNUNET_NO; + } + dbc.session = dbc.plugin->get_session (dbc.plugin->cls); + GNUNET_assert (NULL != dbc.session); + + result = TALER_TESTING_setup (&run, + NULL, + cfg, + NULL, // no exchange process handle. + GNUNET_NO); // do not try to connect to the exchange + + + return GNUNET_OK; +} + + int main (int argc, char *const argv[]) @@ -507,12 +519,6 @@ main (int argc, return result; } - result = TALER_TESTING_setup (&run, - NULL, - config_filename, - NULL, // no exchange process handle. - GNUNET_NO); // do not try to connect to the exchange - GNUNET_free (config_filename); GNUNET_free (testname); dbc.plugin->drop_tables (dbc.plugin->cls); diff --git a/src/testing/testing_api_helpers_bank.c b/src/testing/testing_api_helpers_bank.c index 7b8b203a3..35ef4792f 100644 --- a/src/testing/testing_api_helpers_bank.c +++ b/src/testing/testing_api_helpers_bank.c @@ -36,11 +36,13 @@ * from the base URL. * * @param bank_url bank's base URL. + * @param currency currency the bank uses * @return the fakebank process handle, or NULL if any * error occurs. */ struct TALER_FAKEBANK_Handle * -TALER_TESTING_run_fakebank (const char *bank_url) +TALER_TESTING_run_fakebank (const char *bank_url, + const char *currency) { const char *port; long pnum; @@ -56,7 +58,8 @@ TALER_TESTING_run_fakebank (const char *bank_url) "Starting Fakebank on port %u (%s)\n", (unsigned int) pnum, bank_url); - fakebankd = TALER_FAKEBANK_start ((uint16_t) pnum); + fakebankd = TALER_FAKEBANK_start ((uint16_t) pnum, + currency); if (NULL == fakebankd) { GNUNET_break (0); diff --git a/src/testing/testing_api_helpers_exchange.c b/src/testing/testing_api_helpers_exchange.c index 29c96db19..911bc6f0e 100644 --- a/src/testing/testing_api_helpers_exchange.c +++ b/src/testing/testing_api_helpers_exchange.c @@ -788,7 +788,7 @@ TALER_TESTING_setup_with_exchange_cfg (void *cls, /* NOTE: this call blocks. */ result = TALER_TESTING_setup (setup_ctx->main_cb, setup_ctx->main_cb_cls, - setup_ctx->config_filename, + cfg, exchanged, GNUNET_YES); GNUNET_break (0 == diff --git a/src/testing/testing_api_loop.c b/src/testing/testing_api_loop.c index 61b95c2e1..e9ccdb81c 100644 --- a/src/testing/testing_api_loop.c +++ b/src/testing/testing_api_loop.c @@ -124,7 +124,33 @@ TALER_TESTING_run_with_fakebank (struct TALER_TESTING_Interpreter *is, struct TALER_TESTING_Command *commands, const char *bank_url) { - is->fakebank = TALER_TESTING_run_fakebank (bank_url); + char *currency; + + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_string (is->cfg, + "taler", + "CURRENCY", + ¤cy)) + { + GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, + "taler", + "CURRENCY"); + is->result = GNUNET_SYSERR; + return; + } + if (strlen (currency) >= TALER_CURRENCY_LEN) + { + GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, + "taler", + "CURRENCY", + "Value is too long"); + GNUNET_free (currency); + is->result = GNUNET_SYSERR; + return; + } + is->fakebank = TALER_TESTING_run_fakebank (bank_url, + currency); + GNUNET_free (currency); if (NULL == is->fakebank) { GNUNET_break (0); @@ -529,14 +555,6 @@ struct MainContext */ struct TALER_TESTING_Interpreter *is; - /** - * Configuration filename. The wrapper uses it to fetch - * the exchange port number; We could have passed the port - * number here, but having the config filename seems more - * generic. - */ - const char *config_filename; - /** * URL of the exchange. */ @@ -683,18 +701,16 @@ do_abort (void *cls) * and responsible to run the "run" method. * * @param cls a `struct MainContext *` - * @param cfg configuration to use */ -static int -main_exchange_connect_with_cfg (void *cls, - const struct GNUNET_CONFIGURATION_Handle *cfg) +static void +main_wrapper_exchange_connect (void *cls) { struct MainContext *main_ctx = cls; struct TALER_TESTING_Interpreter *is = main_ctx->is; char *exchange_url; if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_string (cfg, + GNUNET_CONFIGURATION_get_value_string (is->cfg, "exchange", "BASE_URL", &exchange_url)) @@ -702,40 +718,18 @@ main_exchange_connect_with_cfg (void *cls, GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "exchange", "BASE_URL"); - return GNUNET_SYSERR; + return; } main_ctx->exchange_url = exchange_url; - is->cfg = cfg; is->timeout_task = GNUNET_SCHEDULER_add_shutdown (&do_abort, main_ctx); GNUNET_break - (NULL != (is->exchange = TALER_EXCHANGE_connect - (is->ctx, - exchange_url, - &TALER_TESTING_cert_cb, - main_ctx, - TALER_EXCHANGE_OPTION_END))); - is->cfg = NULL; - return GNUNET_OK; -} - - -/** - * Initialize scheduler loop and curl context for the testcase, - * and responsible to run the "run" method. - * - * @param cls a `struct MainContext *` - */ -static void -main_wrapper_exchange_connect (void *cls) -{ - struct MainContext *main_ctx = cls; - - GNUNET_break (GNUNET_OK == - GNUNET_CONFIGURATION_parse_and_run (main_ctx->config_filename, - & - main_exchange_connect_with_cfg, - main_ctx)); + (NULL != (is->exchange = + TALER_EXCHANGE_connect (is->ctx, + exchange_url, + &TALER_TESTING_cert_cb, + main_ctx, + TALER_EXCHANGE_OPTION_END))); } @@ -746,7 +740,7 @@ main_wrapper_exchange_connect (void *cls) * @param main_cb the "run" method which contains all the * commands. * @param main_cb_cls a closure for "run", typically NULL. - * @param config_filename configuration filename. + * @param cfg configuration to use * @param exchanged exchange process handle: will be put in the * state as some commands - e.g. revoke - need to send * signal to it, for example to let it know to reload the @@ -761,7 +755,7 @@ main_wrapper_exchange_connect (void *cls) int TALER_TESTING_setup (TALER_TESTING_Main main_cb, void *main_cb_cls, - const char *config_filename, + const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_OS_Process *exchanged, int exchange_connect) { @@ -771,9 +765,6 @@ TALER_TESTING_setup (TALER_TESTING_Main main_cb, .main_cb_cls = main_cb_cls, /* needed to init the curl ctx */ .is = &is, - /* needed to read values like exchange port - * number to construct the exchange url.*/ - .config_filename = config_filename }; struct GNUNET_SIGNAL_Context *shc_chld; @@ -781,6 +772,7 @@ TALER_TESTING_setup (TALER_TESTING_Main main_cb, 0, sizeof (is)); is.exchanged = exchanged; + is.cfg = cfg; sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO); GNUNET_assert (NULL != sigpipe); -- cgit v1.2.3