2018-02-22 14:51:12 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
|
|
|
Copyright (C) 2018 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
|
|
|
|
<http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file bank-lib/testing_api_helpers.c
|
|
|
|
* @brief convenience functions for bank-lib tests.
|
|
|
|
* @author Marcello Stanisci
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
#include <gnunet/gnunet_util_lib.h>
|
|
|
|
#include "taler_testing_bank_lib.h"
|
2019-09-09 16:46:01 +02:00
|
|
|
#include "taler_fakebank_lib.h"
|
2018-02-22 14:51:12 +01:00
|
|
|
|
2019-09-15 10:27:39 +02:00
|
|
|
|
|
|
|
#define BANK_FAIL() \
|
|
|
|
do {GNUNET_break (0); return NULL; } while (0)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep each bank account credentials at index:
|
|
|
|
* bank account number - 1
|
|
|
|
*/
|
2018-02-22 14:51:12 +01:00
|
|
|
struct TALER_BANK_AuthenticationData AUTHS[] = {
|
|
|
|
|
|
|
|
/* Bank credentials */
|
|
|
|
{.method = TALER_BANK_AUTH_BASIC,
|
2019-12-23 11:47:16 +01:00
|
|
|
.details.basic.username = TALER_TESTING_BANK_USERNAME,
|
|
|
|
.details.basic.password = TALER_TESTING_BANK_PASSWORD},
|
2018-02-22 14:51:12 +01:00
|
|
|
|
|
|
|
/* Exchange credentials */
|
|
|
|
{.method = TALER_BANK_AUTH_BASIC,
|
2019-12-23 11:47:16 +01:00
|
|
|
.details.basic.username = TALER_TESTING_EXCHANGE_USERNAME,
|
|
|
|
.details.basic.password = TALER_TESTING_EXCHANGE_PASSWORD },
|
2018-02-22 14:51:12 +01:00
|
|
|
|
|
|
|
/* User credentials */
|
|
|
|
{.method = TALER_BANK_AUTH_BASIC,
|
2019-12-23 11:47:16 +01:00
|
|
|
.details.basic.username = TALER_TESTING_USER_USERNAME,
|
|
|
|
.details.basic.password = TALER_TESTING_USER_PASSWORD }
|
2018-02-22 14:51:12 +01:00
|
|
|
};
|
|
|
|
|
2019-09-09 16:46:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the Fakebank by guessing / extracting the portnumber
|
|
|
|
* from the base URL.
|
|
|
|
*
|
|
|
|
* @param bank_url bank's base URL.
|
|
|
|
* @return the fakebank process handle, or NULL if any
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
struct TALER_FAKEBANK_Handle *
|
|
|
|
TALER_TESTING_run_fakebank (const char *bank_url)
|
|
|
|
{
|
|
|
|
const char *port;
|
|
|
|
long pnum;
|
|
|
|
struct TALER_FAKEBANK_Handle *fakebankd;
|
|
|
|
|
|
|
|
port = strrchr (bank_url,
|
|
|
|
(unsigned char) ':');
|
|
|
|
if (NULL == port)
|
|
|
|
pnum = 80;
|
|
|
|
else
|
|
|
|
pnum = strtol (port + 1, NULL, 10);
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Starting Fakebank on port %u (%s)\n",
|
|
|
|
(unsigned int) pnum,
|
|
|
|
bank_url);
|
|
|
|
fakebankd = TALER_FAKEBANK_start ((uint16_t) pnum);
|
|
|
|
if (NULL == fakebankd)
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return fakebankd;
|
|
|
|
}
|
|
|
|
|
2019-09-09 23:20:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Look for substring in a programs' name.
|
|
|
|
*
|
|
|
|
* @param prog program's name to look into
|
|
|
|
* @param marker chunk to find in @a prog
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
TALER_TESTING_has_in_name (const char *prog_name,
|
|
|
|
const char *marker)
|
|
|
|
{
|
|
|
|
size_t name_pos;
|
|
|
|
size_t pos;
|
|
|
|
|
2019-09-15 10:27:39 +02:00
|
|
|
if (! prog_name || ! marker)
|
2019-09-09 23:20:18 +02:00
|
|
|
return GNUNET_NO;
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
name_pos = 0;
|
|
|
|
while (prog_name[pos])
|
|
|
|
{
|
|
|
|
if ('/' == prog_name[pos])
|
|
|
|
name_pos = pos + 1;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
if (name_pos == pos)
|
|
|
|
return GNUNET_YES;
|
2019-09-15 10:27:39 +02:00
|
|
|
return strstr (prog_name + name_pos, marker) != NULL;
|
2019-09-09 23:20:18 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 12:59:50 +01:00
|
|
|
|
2018-02-22 14:51:12 +01:00
|
|
|
/**
|
|
|
|
* Start the (Python) bank process. Assume the port
|
|
|
|
* is available and the database is clean. Use the "prepare
|
|
|
|
* bank" function to do such tasks.
|
|
|
|
*
|
|
|
|
* @param config_filename configuration filename.
|
2018-05-29 10:27:41 +02:00
|
|
|
* @param bank_url base URL of the bank, used by `wget' to check
|
|
|
|
* that the bank was started right.
|
2018-02-22 14:51:12 +01:00
|
|
|
*
|
|
|
|
* @return the process, or NULL if the process could not
|
|
|
|
* be started.
|
|
|
|
*/
|
|
|
|
struct GNUNET_OS_Process *
|
2018-05-04 15:46:07 +02:00
|
|
|
TALER_TESTING_run_bank (const char *config_filename,
|
|
|
|
const char *bank_url)
|
2018-02-22 14:51:12 +01:00
|
|
|
{
|
|
|
|
struct GNUNET_OS_Process *bank_proc;
|
|
|
|
unsigned int iter;
|
2018-05-04 15:46:07 +02:00
|
|
|
char *wget_cmd;
|
|
|
|
char *database;
|
|
|
|
char *serve_cfg;
|
|
|
|
char *serve_arg;
|
|
|
|
struct GNUNET_CONFIGURATION_Handle *cfg;
|
|
|
|
|
|
|
|
cfg = GNUNET_CONFIGURATION_create ();
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CONFIGURATION_load (cfg,
|
|
|
|
config_filename))
|
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
|
|
|
exit (77);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CONFIGURATION_get_value_string (cfg,
|
|
|
|
"bank",
|
|
|
|
"database",
|
|
|
|
&database))
|
|
|
|
{
|
|
|
|
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"bank",
|
|
|
|
"database");
|
|
|
|
GNUNET_break (0);
|
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
|
|
|
exit (77);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CONFIGURATION_get_value_string (cfg,
|
|
|
|
"bank",
|
|
|
|
"serve",
|
|
|
|
&serve_cfg))
|
|
|
|
{
|
|
|
|
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"bank",
|
|
|
|
"serve");
|
|
|
|
GNUNET_break (0);
|
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (database);
|
2018-05-04 15:46:07 +02:00
|
|
|
exit (77);
|
|
|
|
}
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
2018-05-04 15:46:07 +02:00
|
|
|
|
|
|
|
serve_arg = "serve-http";
|
|
|
|
if (0 != strcmp ("http", serve_cfg))
|
|
|
|
serve_arg = "serve-uwsgi";
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (serve_cfg);
|
2018-02-22 14:51:12 +01:00
|
|
|
bank_proc = GNUNET_OS_start_process
|
2019-08-25 16:18:24 +02:00
|
|
|
(GNUNET_NO,
|
|
|
|
GNUNET_OS_INHERIT_STD_ALL,
|
|
|
|
NULL, NULL, NULL,
|
2019-12-23 11:47:16 +01:00
|
|
|
"taler-bank-manage-testing",
|
|
|
|
"taler-bank-manage-testing",
|
|
|
|
config_filename,
|
|
|
|
database,
|
2019-08-25 16:18:24 +02:00
|
|
|
serve_arg, NULL);
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (database);
|
2018-02-22 14:51:12 +01:00
|
|
|
if (NULL == bank_proc)
|
2019-09-15 10:27:39 +02:00
|
|
|
{
|
2018-02-22 14:51:12 +01:00
|
|
|
BANK_FAIL ();
|
2019-09-15 10:27:39 +02:00
|
|
|
}
|
2018-02-22 14:51:12 +01:00
|
|
|
|
2018-05-04 15:46:07 +02:00
|
|
|
GNUNET_asprintf (&wget_cmd,
|
2019-09-15 13:13:29 +02:00
|
|
|
"wget -q -t 1 -T 1 %s -o /dev/null -O /dev/null",
|
2018-05-04 15:46:07 +02:00
|
|
|
bank_url);
|
|
|
|
|
2018-02-22 14:51:12 +01:00
|
|
|
/* give child time to start and bind against the socket */
|
|
|
|
fprintf (stderr,
|
|
|
|
"Waiting for `taler-bank-manage' to be ready");
|
|
|
|
iter = 0;
|
|
|
|
do
|
2019-08-25 16:18:24 +02:00
|
|
|
{
|
|
|
|
if (10 == iter)
|
2018-02-22 14:51:12 +01:00
|
|
|
{
|
2019-08-25 16:18:24 +02:00
|
|
|
fprintf (
|
|
|
|
stderr,
|
|
|
|
"Failed to launch `taler-bank-manage' (or `wget')\n");
|
|
|
|
GNUNET_OS_process_kill (bank_proc,
|
|
|
|
SIGTERM);
|
|
|
|
GNUNET_OS_process_wait (bank_proc);
|
|
|
|
GNUNET_OS_process_destroy (bank_proc);
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (wget_cmd);
|
2019-08-25 16:18:24 +02:00
|
|
|
BANK_FAIL ();
|
2018-02-22 14:51:12 +01:00
|
|
|
}
|
2019-08-25 16:18:24 +02:00
|
|
|
fprintf (stderr, ".");
|
|
|
|
sleep (1);
|
|
|
|
iter++;
|
|
|
|
}
|
2018-05-04 15:46:07 +02:00
|
|
|
while (0 != system (wget_cmd));
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (wget_cmd);
|
2018-02-22 14:51:12 +01:00
|
|
|
fprintf (stderr, "\n");
|
|
|
|
|
|
|
|
return bank_proc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-29 10:27:41 +02:00
|
|
|
|
2018-02-22 14:51:12 +01:00
|
|
|
/**
|
|
|
|
* Prepare the bank execution. Check if the port is available
|
2018-05-29 10:27:41 +02:00
|
|
|
* and reset database.
|
2018-02-22 14:51:12 +01:00
|
|
|
*
|
2018-05-29 10:34:41 +02:00
|
|
|
* @param config_filename configuration file name.
|
2018-02-22 14:51:12 +01:00
|
|
|
*
|
|
|
|
* @return the base url, or NULL upon errors. Must be freed
|
|
|
|
* by the caller.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
TALER_TESTING_prepare_bank (const char *config_filename)
|
|
|
|
{
|
|
|
|
struct GNUNET_CONFIGURATION_Handle *cfg;
|
|
|
|
unsigned long long port;
|
|
|
|
struct GNUNET_OS_Process *dbreset_proc;
|
|
|
|
enum GNUNET_OS_ProcessStatusType type;
|
|
|
|
unsigned long code;
|
|
|
|
char *base_url;
|
2018-05-29 10:27:41 +02:00
|
|
|
char *database;
|
2018-02-22 14:51:12 +01:00
|
|
|
|
|
|
|
cfg = GNUNET_CONFIGURATION_create ();
|
|
|
|
|
2019-09-15 10:27:39 +02:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CONFIGURATION_load (cfg, config_filename))
|
|
|
|
{
|
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
2018-02-22 14:51:12 +01:00
|
|
|
BANK_FAIL ();
|
2019-09-15 10:27:39 +02:00
|
|
|
}
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CONFIGURATION_get_value_string (cfg,
|
|
|
|
"bank",
|
|
|
|
"DATABASE",
|
|
|
|
&database))
|
2018-05-29 10:27:41 +02:00
|
|
|
{
|
|
|
|
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"bank",
|
|
|
|
"DATABASE");
|
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
|
|
|
BANK_FAIL ();
|
|
|
|
}
|
|
|
|
|
2019-09-15 10:27:39 +02:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CONFIGURATION_get_value_number (cfg,
|
|
|
|
"bank",
|
|
|
|
"HTTP_PORT",
|
|
|
|
&port))
|
2018-02-22 14:51:12 +01:00
|
|
|
{
|
|
|
|
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"bank",
|
|
|
|
"HTTP_PORT");
|
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (database);
|
2018-02-22 14:51:12 +01:00
|
|
|
BANK_FAIL ();
|
|
|
|
}
|
|
|
|
GNUNET_CONFIGURATION_destroy (cfg);
|
|
|
|
|
|
|
|
if (GNUNET_OK != GNUNET_NETWORK_test_port_free
|
2019-08-25 16:18:24 +02:00
|
|
|
(IPPROTO_TCP, (uint16_t) port))
|
2018-02-22 14:51:12 +01:00
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"Required port %llu not available, skipping.\n",
|
2019-08-25 16:18:24 +02:00
|
|
|
port);
|
2018-02-22 14:51:12 +01:00
|
|
|
BANK_FAIL ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DB preparation */
|
|
|
|
if (NULL ==
|
2019-08-25 16:18:24 +02:00
|
|
|
(dbreset_proc = GNUNET_OS_start_process (
|
|
|
|
GNUNET_NO,
|
|
|
|
GNUNET_OS_INHERIT_STD_ALL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
"taler-bank-manage",
|
|
|
|
"taler-bank-manage",
|
|
|
|
"-c", "bank.conf",
|
|
|
|
"--with-db", database,
|
|
|
|
"django",
|
|
|
|
"flush",
|
|
|
|
"--no-input", NULL)))
|
2018-02-22 14:51:12 +01:00
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Failed to flush the bank db.\n");
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (database);
|
2018-02-22 14:51:12 +01:00
|
|
|
BANK_FAIL ();
|
|
|
|
}
|
2019-09-15 10:27:39 +02:00
|
|
|
GNUNET_free (database);
|
2018-02-22 14:51:12 +01:00
|
|
|
|
|
|
|
if (GNUNET_SYSERR ==
|
|
|
|
GNUNET_OS_process_wait_status (dbreset_proc,
|
|
|
|
&type,
|
|
|
|
&code))
|
|
|
|
{
|
|
|
|
GNUNET_OS_process_destroy (dbreset_proc);
|
|
|
|
BANK_FAIL ();
|
|
|
|
}
|
|
|
|
if ( (type == GNUNET_OS_PROCESS_EXITED) &&
|
|
|
|
(0 != code) )
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"Failed to setup database\n");
|
|
|
|
BANK_FAIL ();
|
|
|
|
}
|
|
|
|
if ( (type != GNUNET_OS_PROCESS_EXITED) ||
|
|
|
|
(0 != code) )
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"Unexpected error running"
|
|
|
|
" `taler-bank-manage django flush..'!\n");
|
|
|
|
BANK_FAIL ();
|
|
|
|
}
|
|
|
|
|
|
|
|
GNUNET_OS_process_destroy (dbreset_proc);
|
|
|
|
|
|
|
|
GNUNET_asprintf (&base_url,
|
|
|
|
"http://localhost:%llu/",
|
|
|
|
port);
|
|
|
|
return base_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* end of testing_api_helpers.c */
|