diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-01-21 19:57:09 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-01-21 19:57:09 +0100 |
commit | 652bb0f9c4d9527c409db6a99a7e550c57328adf (patch) | |
tree | e77e6e2e18dd9a8ee786d76477eafa8f9e7a4c01 /src/exchange-lib | |
parent | 8013c4b66a1926a9f49673a125f9822bf40dbd94 (diff) |
finish simplifying main by adding more helpers and macros
Diffstat (limited to 'src/exchange-lib')
-rw-r--r-- | src/exchange-lib/Makefile.am | 1 | ||||
-rw-r--r-- | src/exchange-lib/test_exchange_api_new.c | 149 | ||||
-rw-r--r-- | src/exchange-lib/testing_api_cmd_fakebank_transfer.c | 13 | ||||
-rw-r--r-- | src/exchange-lib/testing_api_helpers.c | 85 | ||||
-rw-r--r-- | src/exchange-lib/testing_api_loop.c | 54 |
5 files changed, 201 insertions, 101 deletions
diff --git a/src/exchange-lib/Makefile.am b/src/exchange-lib/Makefile.am index 2f02a6e8b..64a5f9649 100644 --- a/src/exchange-lib/Makefile.am +++ b/src/exchange-lib/Makefile.am @@ -55,6 +55,7 @@ libtalertesting_la_SOURCES = \ libtalertesting_la_LIBADD = \ $(top_builddir)/src/json/libtalerjson.la \ $(top_builddir)/src/util/libtalerutil.la \ + $(top_builddir)/src/bank-lib/libtalerfakebank.la \ -lgnunetcurl \ -lgnunetjson \ -lgnunetutil \ diff --git a/src/exchange-lib/test_exchange_api_new.c b/src/exchange-lib/test_exchange_api_new.c index 49934fcd3..077115a22 100644 --- a/src/exchange-lib/test_exchange_api_new.c +++ b/src/exchange-lib/test_exchange_api_new.c @@ -46,49 +46,49 @@ */ #define EXCHANGE_ACCOUNT_NO 2 - /** - * Handle to access the exchange. + * Account number of some user. */ -// static struct TALER_EXCHANGE_Handle *exchange; +#define USER_ACCOUNT_NO 42 /** - * Handle to the exchange process. + * */ -static struct GNUNET_OS_Process *exchanged; - +#define USER_LOGIN_NAME "user42" /** - * Handle to our fakebank. + * */ -static struct TALER_FAKEBANK_Handle *fakebank; - +#define USER_LOGIN_PASS "pass42" +/** + * + */ +#define FAKEBANK_URL "http://localhost:8082/" +/** + * + */ +#define CONFIG_FILE "test_exchange_api.conf" /** - * Function run when the test terminates (good or bad). - * Cleans up our state. * - * @param cls NULL */ -static void -do_shutdown (void *cls) -{ - if (NULL != fakebank) - { - TALER_FAKEBANK_stop (fakebank); - fakebank = NULL; - } -} +#define CMD_EXEC_WIREWATCH(label) \ + TALER_TESTING_cmd_exec_wirewatch (label, CONFIG_FILE) -#define CONFIG_FILE "test_exchange_api.conf" +/** + * + */ +#define CMD_TRANSFER_TO_EXCHANGE(label,amount) \ + TALER_TESTING_cmd_fakebank_transfer (label, amount, \ + FAKEBANK_URL, USER_ACCOUNT_NO, EXCHANGE_ACCOUNT_NO, \ + USER_LOGIN_NAME, USER_LOGIN_PASS) -#define RUN_WIREWATCH(label) TALER_TESTING_cmd_exec_wirewatch (label, CONFIG_FILE) /** - * Main function that will tell the interpreter what to do. + * Main function that will tell the interpreter what commands to run. * * @param cls closure */ @@ -97,97 +97,50 @@ run (void *cls, struct TALER_TESTING_Interpreter *is) { struct TALER_TESTING_Command commands[] = { - TALER_TESTING_cmd_fakebank_transfer ("create-reserve-1", - "EUR:5.01", - 42, - EXCHANGE_ACCOUNT_NO, - "user42", - "pass42"), - RUN_WIREWATCH ("exec-wirewatch-1"), + CMD_TRANSFER_TO_EXCHANGE ("create-reserve-1", + "EUR:5.01"), + CMD_EXEC_WIREWATCH ("exec-wirewatch-1"), TALER_TESTING_cmd_end () }; - fakebank = TALER_FAKEBANK_start (8082); - TALER_TESTING_run (is, - commands); - GNUNET_SCHEDULER_add_shutdown (&do_shutdown, - NULL); + TALER_TESTING_run_with_fakebank (is, + commands, + FAKEBANK_URL); } - int main (int argc, char * const *argv) { - struct GNUNET_OS_Process *proc; - enum GNUNET_OS_ProcessStatusType type; - unsigned long code; - unsigned int iter; - int result; - - /* These might get in the way... */ + /* These environment variables get in the way... */ unsetenv ("XDG_DATA_HOME"); unsetenv ("XDG_CONFIG_HOME"); GNUNET_log_setup ("test-exchange-api-new", "INFO", NULL); - TALER_TESTING_cleanup_files (CONFIG_FILE); - result = TALER_TESTING_prepare_exchange (CONFIG_FILE); - if (GNUNET_SYSERR == result) - return 1; - if (GNUNET_NO == result) - return 77; - - /* For fakebank */ if (GNUNET_OK != - GNUNET_NETWORK_test_port_free (IPPROTO_TCP, - 8082)) + TALER_TESTING_url_port_free (FAKEBANK_URL)) + return 77; + TALER_TESTING_cleanup_files (CONFIG_FILE); + switch (TALER_TESTING_prepare_exchange (CONFIG_FILE)) { - fprintf (stderr, - "Required port %u not available, skipping.\n", - 8082); + case GNUNET_SYSERR: + GNUNET_break (0); + return 1; + case GNUNET_NO: return 77; + case GNUNET_OK: + if (GNUNET_OK != + TALER_TESTING_setup_with_exchange (&run, + NULL, + CONFIG_FILE)) + return 1; + default: + GNUNET_break (0); + return 1; } - - exchanged = GNUNET_OS_start_process (GNUNET_NO, - GNUNET_OS_INHERIT_STD_ALL, - NULL, NULL, NULL, - "taler-exchange-httpd", - "taler-exchange-httpd", - "-c", CONFIG_FILE, - "-i", - NULL); - /* give child time to start and bind against the socket */ - fprintf (stderr, - "Waiting for `taler-exchange-httpd' to be ready"); - iter = 0; - do - { - if (10 == iter) - { - fprintf (stderr, - "Failed to launch `taler-exchange-httpd' (or `wget')\n"); - GNUNET_OS_process_kill (exchanged, - SIGTERM); - GNUNET_OS_process_wait (exchanged); - GNUNET_OS_process_destroy (exchanged); - return 77; - } - fprintf (stderr, "."); - sleep (1); - iter++; - } - while (0 != system ("wget -q -t 1 -T 1 http://127.0.0.1:8081/keys -o /dev/null -O /dev/null")); - fprintf (stderr, "\n"); - - result = TALER_TESTING_setup (&run, - NULL); - GNUNET_break (0 == - GNUNET_OS_process_kill (exchanged, - SIGTERM)); - GNUNET_break (GNUNET_OK == - GNUNET_OS_process_wait (exchanged)); - GNUNET_OS_process_destroy (exchanged); - return (GNUNET_OK == result) ? 0 : 1; + return 0; } + +/* end of test_exchange_api_new.c */ diff --git a/src/exchange-lib/testing_api_cmd_fakebank_transfer.c b/src/exchange-lib/testing_api_cmd_fakebank_transfer.c index eb8c6802c..6b1a99dc3 100644 --- a/src/exchange-lib/testing_api_cmd_fakebank_transfer.c +++ b/src/exchange-lib/testing_api_cmd_fakebank_transfer.c @@ -55,6 +55,11 @@ struct FakebankTransferState const char *subject; /** + * URL to use for the bank. + */ + const char *bank_url; + + /** * Sender (debit) account number. */ uint64_t debit_account_no; @@ -200,7 +205,7 @@ fakebank_transfer_run (void *cls, fts->is = is; fts->aih = TALER_BANK_admin_add_incoming (TALER_TESTING_interpreter_get_context (is), - "http://localhost:8082/", /* bank URL: FIXME */ + fts->bank_url, &auth, "https://exchange.com/", /* exchange URL: FIXME */ subject, @@ -286,6 +291,7 @@ fakebank_transfer_traits (void *cls, struct TALER_TESTING_Command TALER_TESTING_cmd_fakebank_transfer (const char *label, const char *amount, + const char *bank_url, uint64_t debit_account_no, uint64_t credit_account_no, const char *auth_username, @@ -295,6 +301,7 @@ TALER_TESTING_cmd_fakebank_transfer (const char *label, struct FakebankTransferState *fts; fts = GNUNET_new (struct FakebankTransferState); + fts->bank_url = bank_url; fts->credit_account_no = credit_account_no; fts->debit_account_no = debit_account_no; fts->auth_username = auth_username; @@ -325,6 +332,7 @@ TALER_TESTING_cmd_fakebank_transfer (const char *label, struct TALER_TESTING_Command TALER_TESTING_cmd_fakebank_transfer_with_subject (const char *label, const char *amount, + const char *bank_url, uint64_t debit_account_no, uint64_t credit_account_no, const char *auth_username, @@ -335,6 +343,7 @@ TALER_TESTING_cmd_fakebank_transfer_with_subject (const char *label, struct FakebankTransferState *fts; fts = GNUNET_new (struct FakebankTransferState); + fts->bank_url = bank_url; fts->credit_account_no = credit_account_no; fts->debit_account_no = debit_account_no; fts->auth_username = auth_username; @@ -366,6 +375,7 @@ TALER_TESTING_cmd_fakebank_transfer_with_subject (const char *label, struct TALER_TESTING_Command TALER_TESTING_cmd_fakebank_transfer_with_ref (const char *label, const char *amount, + const char *bank_url, uint64_t debit_account_no, uint64_t credit_account_no, const char *auth_username, @@ -376,6 +386,7 @@ TALER_TESTING_cmd_fakebank_transfer_with_ref (const char *label, struct FakebankTransferState *fts; fts = GNUNET_new (struct FakebankTransferState); + fts->bank_url = bank_url; fts->credit_account_no = credit_account_no; fts->debit_account_no = debit_account_no; fts->auth_username = auth_username; diff --git a/src/exchange-lib/testing_api_helpers.c b/src/exchange-lib/testing_api_helpers.c index 36ef5c942..fd3d2ddf0 100644 --- a/src/exchange-lib/testing_api_helpers.c +++ b/src/exchange-lib/testing_api_helpers.c @@ -239,4 +239,89 @@ TALER_TESTING_find_pk (const struct TALER_EXCHANGE_Keys *keys, return NULL; } + +/** + * Initialize scheduler loop and curl context for the testcase + * including starting and stopping the exchange using the given + * configuration file. + */ +int +TALER_TESTING_setup_with_exchange (TALER_TESTING_Main main_cb, + void *main_cb_cls, + const char *config_file) +{ + int result; + unsigned int iter; + struct GNUNET_OS_Process *exchanged; + + exchanged = GNUNET_OS_start_process (GNUNET_NO, + GNUNET_OS_INHERIT_STD_ALL, + NULL, NULL, NULL, + "taler-exchange-httpd", + "taler-exchange-httpd", + "-c", config_file, + "-i", + NULL); + /* give child time to start and bind against the socket */ + fprintf (stderr, + "Waiting for `taler-exchange-httpd' to be ready"); + iter = 0; + do + { + if (10 == iter) + { + fprintf (stderr, + "Failed to launch `taler-exchange-httpd' (or `wget')\n"); + GNUNET_OS_process_kill (exchanged, + SIGTERM); + GNUNET_OS_process_wait (exchanged); + GNUNET_OS_process_destroy (exchanged); + return 77; + } + fprintf (stderr, "."); + sleep (1); + iter++; + } + while (0 != system ("wget -q -t 1 -T 1 http://127.0.0.1:8081/keys -o /dev/null -O /dev/null")); + fprintf (stderr, "\n"); + + result = TALER_TESTING_setup (main_cb, + main_cb_cls); + GNUNET_break (0 == + GNUNET_OS_process_kill (exchanged, + SIGTERM)); + GNUNET_break (GNUNET_OK == + GNUNET_OS_process_wait (exchanged)); + GNUNET_OS_process_destroy (exchanged); + return result; +} + + +/** + * Test port in URL string for availability. + */ +int +TALER_TESTING_url_port_free (const char *url) +{ + const char *port; + long pnum; + + port = strrchr (url, + (unsigned char) ':'); + if (NULL == port) + pnum = 80; + else + pnum = strtol (port + 1, NULL, 10); + if (GNUNET_OK != + GNUNET_NETWORK_test_port_free (IPPROTO_TCP, + pnum)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Port %u not available.\n", + (unsigned int) pnum); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + /* end of testing_api_helpers.c */ diff --git a/src/exchange-lib/testing_api_loop.c b/src/exchange-lib/testing_api_loop.c index 47175fe47..8c2364984 100644 --- a/src/exchange-lib/testing_api_loop.c +++ b/src/exchange-lib/testing_api_loop.c @@ -26,6 +26,8 @@ #include "exchange_api_handle.h" #include "taler_signatures.h" #include "taler_testing_lib.h" +#include "taler_fakebank_lib.h" + /** * Global state of the interpreter, used by a command @@ -61,6 +63,12 @@ struct TALER_TESTING_Interpreter struct GNUNET_CURL_RescheduleContext *rc; /** + * Handle to our fakebank, if #TALER_TESTING_run_with_fakebank() was used. + * Otherwise NULL. + */ + struct TALER_FAKEBANK_Handle *fakebank; + + /** * Task run on timeout. */ struct GNUNET_SCHEDULER_Task *timeout_task; @@ -123,9 +131,46 @@ TALER_TESTING_interpreter_lookup_command (struct TALER_TESTING_Interpreter *is, * Obtain main execution context for the main loop. */ struct GNUNET_CURL_Context * -TALER_TESTING_interpreter_get_context (struct TALER_TESTING_Interpreter *i) +TALER_TESTING_interpreter_get_context (struct TALER_TESTING_Interpreter *is) +{ + return is->ctx; +} + + +struct TALER_FAKEBANK_Handle * +TALER_TESTING_interpreter_get_fakebank (struct TALER_TESTING_Interpreter *is) +{ + return is->fakebank; +} + + +void +TALER_TESTING_run_with_fakebank (struct TALER_TESTING_Interpreter *is, + struct TALER_TESTING_Command *commands, + const char *bank_url) { - return i->ctx; + const char *port; + long pnum; + + port = strrchr (bank_url, + (unsigned char) ':'); + if (NULL == port) + pnum = 80; + else + pnum = strtol (port + 1, NULL, 10); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Staring Fakebank on port %u (%s)\n", + (unsigned int) pnum, + bank_url); + is->fakebank = TALER_FAKEBANK_start ((uint16_t) pnum); + if (NULL == is->fakebank) + { + GNUNET_break (0); + is->result = GNUNET_SYSERR; + return; + } + TALER_TESTING_run (is, + commands); } @@ -258,6 +303,11 @@ do_shutdown (void *cls) GNUNET_SCHEDULER_cancel (is->child_death_task); is->child_death_task = NULL; } + if (NULL != is->fakebank) + { + TALER_FAKEBANK_stop (is->fakebank); + is->fakebank = NULL; + } GNUNET_free_non_null (is->commands); } |