finish simplifying main by adding more helpers and macros

This commit is contained in:
Christian Grothoff 2018-01-21 19:57:09 +01:00
parent 8013c4b66a
commit 652bb0f9c4
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
6 changed files with 240 additions and 109 deletions

View File

@ -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 \

View File

@ -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;
/**
* Handle to our fakebank.
*/
static struct TALER_FAKEBANK_Handle *fakebank;
/**
* 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 USER_LOGIN_NAME "user42"
/**
*
*/
#define USER_LOGIN_PASS "pass42"
/**
*
*/
#define FAKEBANK_URL "http://localhost:8082/"
/**
*
*/
#define CONFIG_FILE "test_exchange_api.conf"
#define RUN_WIREWATCH(label) TALER_TESTING_cmd_exec_wirewatch (label, CONFIG_FILE)
/**
*
*/
#define CMD_EXEC_WIREWATCH(label) \
TALER_TESTING_cmd_exec_wirewatch (label, CONFIG_FILE)
/**
*
*/
#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)
/**
* 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))
{
fprintf (stderr,
"Required port %u not available, skipping.\n",
8082);
TALER_TESTING_url_port_free (FAKEBANK_URL))
return 77;
TALER_TESTING_cleanup_files (CONFIG_FILE);
switch (TALER_TESTING_prepare_exchange (CONFIG_FILE))
{
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 */

View File

@ -54,6 +54,11 @@ struct FakebankTransferState
*/
const char *subject;
/**
* URL to use for the bank.
*/
const char *bank_url;
/**
* Sender (debit) account number.
*/
@ -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;

View File

@ -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 */

View File

@ -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
@ -60,6 +62,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.
*/
@ -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 i->ctx;
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)
{
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);
}

View File

@ -64,6 +64,13 @@ void
TALER_TESTING_cleanup_files (const char *config_name);
/**
* Test port in URL string for availability.
*/
int
TALER_TESTING_url_port_free (const char *url);
/* ******************* Generic interpreter logic ****************** */
@ -165,6 +172,9 @@ struct GNUNET_CURL_Context *
TALER_TESTING_interpreter_get_context (struct TALER_TESTING_Interpreter *is);
struct TALER_FAKEBANK_Handle *
TALER_TESTING_interpreter_get_fakebank (struct TALER_TESTING_Interpreter *is);
/**
* Current command is done, run the next one.
*/
@ -199,6 +209,13 @@ TALER_TESTING_run (struct TALER_TESTING_Interpreter *is,
struct TALER_TESTING_Command *commands);
void
TALER_TESTING_run_with_fakebank (struct TALER_TESTING_Interpreter *is,
struct TALER_TESTING_Command *commands,
const char *bank_url);
typedef void
(*TALER_TESTING_Main)(void *cls,
struct TALER_TESTING_Interpreter *is);
@ -212,6 +229,17 @@ TALER_TESTING_setup (TALER_TESTING_Main main_cb,
void *main_cb_cls);
/**
* 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);
/* ****************** Specific interpreter commands **************** */
@ -224,6 +252,7 @@ TALER_TESTING_setup (TALER_TESTING_Main main_cb,
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,
@ -237,6 +266,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,
@ -251,6 +281,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,