exchange/src/lib/testing_api_cmd_bank_check.c

307 lines
8.5 KiB
C
Raw Normal View History

2018-01-23 10:28:24 +01:00
/*
This file is part of TALER
2020-01-15 15:20:48 +01:00
Copyright (C) 2018-2020 Taler Systems SA
2018-01-23 10:28:24 +01:00
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 lib/testing_api_cmd_bank_check.c
2018-01-23 10:28:24 +01:00
* @brief command to check if a particular wire transfer took
* place.
* @author Marcello Stanisci
*/
#include "platform.h"
#include "taler_json_lib.h"
#include <gnunet/gnunet_curl_lib.h>
#include "exchange_api_handle.h"
#include "taler_testing_lib.h"
#include "taler_fakebank_lib.h"
2018-05-25 20:22:56 +02:00
/**
* State for a "bank check" CMD.
*/
2018-01-23 10:28:24 +01:00
struct BankCheckState
{
/**
2018-05-25 20:22:56 +02:00
* Base URL of the exchange supposed to be
* involved in the bank transaction.
2018-01-23 10:28:24 +01:00
*/
const char *exchange_base_url;
/**
* Expected transferred amount.
*/
const char *amount;
/**
2018-05-25 20:22:56 +02:00
* Expected debit bank account.
2018-01-23 10:28:24 +01:00
*/
2020-01-15 15:20:48 +01:00
const char *debit_payto;
2019-08-25 16:18:24 +02:00
2018-01-23 10:28:24 +01:00
/**
2018-05-25 20:22:56 +02:00
* Expected credit bank account.
2018-01-23 10:28:24 +01:00
*/
2020-01-15 15:20:48 +01:00
const char *credit_payto;
2018-01-23 10:28:24 +01:00
/**
2018-05-25 20:22:56 +02:00
* Binary form of the wire transfer subject.
2018-01-23 10:28:24 +01:00
*/
struct TALER_WireTransferIdentifierRawP wtid;
/**
* Interpreter state.
*/
struct TALER_TESTING_Interpreter *is;
/**
2018-05-25 20:22:56 +02:00
* Reference to a CMD that provides all the data
* needed to issue the bank check. If NULL, that data
* must exist here in the state.
*/
const char *deposit_reference;
2018-01-23 10:28:24 +01:00
};
/**
* Run the command.
*
2018-05-25 20:22:56 +02:00
* @param cls closure.
* @param cmd the command to execute.
2018-01-23 10:28:24 +01:00
* @param is the interpreter state.
*/
2018-06-27 16:49:23 +02:00
static void
2018-01-23 10:28:24 +01:00
check_bank_transfer_run (void *cls,
const struct TALER_TESTING_Command *cmd,
struct TALER_TESTING_Interpreter *is)
{
struct BankCheckState *bcs = cls;
struct TALER_Amount amount;
2020-01-15 15:20:48 +01:00
char *debit_account;
char *credit_account;
const char *exchange_base_url;
2020-01-15 15:20:48 +01:00
const char *debit_payto;
const char *credit_payto;
2018-01-23 10:28:24 +01:00
if (NULL == bcs->deposit_reference)
2018-01-23 10:28:24 +01:00
{
TALER_LOG_INFO ("Deposit reference NOT given\n");
2020-01-15 15:20:48 +01:00
debit_payto = bcs->debit_payto;
credit_payto = bcs->credit_payto;
exchange_base_url = bcs->exchange_base_url;
if (GNUNET_OK !=
TALER_string_to_amount (bcs->amount,
&amount))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to parse amount `%s' at %u\n",
bcs->amount,
is->ip);
TALER_TESTING_interpreter_fail (is);
return;
}
}
2020-01-15 15:20:48 +01:00
else
{
const struct TALER_TESTING_Command *deposit_cmd;
const struct TALER_Amount *amount_ptr;
TALER_LOG_INFO ("`%s' uses reference (%s/%p)\n",
TALER_TESTING_interpreter_get_current_label
(is),
bcs->deposit_reference,
bcs->deposit_reference);
2020-01-17 20:42:24 +01:00
deposit_cmd
= TALER_TESTING_interpreter_lookup_command (is,
bcs->deposit_reference);
if (NULL == deposit_cmd)
TALER_TESTING_FAIL (is);
2020-01-17 20:42:24 +01:00
if ( (GNUNET_OK !=
TALER_TESTING_get_trait_amount_obj (deposit_cmd,
0,
&amount_ptr)) ||
(GNUNET_OK !=
TALER_TESTING_get_trait_payto (deposit_cmd,
TALER_TESTING_PT_DEBIT,
&debit_payto)) ||
(GNUNET_OK !=
TALER_TESTING_get_trait_payto (deposit_cmd,
TALER_TESTING_PT_CREDIT,
&credit_payto)) ||
(GNUNET_OK !=
TALER_TESTING_get_trait_url (deposit_cmd,
TALER_TESTING_UT_EXCHANGE_BASE_URL,
&exchange_base_url)) )
TALER_TESTING_FAIL (is);
amount = *amount_ptr;
2018-01-23 10:28:24 +01:00
}
2020-01-17 02:23:38 +01:00
2020-01-15 15:20:48 +01:00
debit_account = TALER_xtalerbank_account_from_payto (debit_payto);
credit_account = TALER_xtalerbank_account_from_payto (credit_payto);
2020-01-17 02:23:38 +01:00
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"converted debit_payto (%s) to debit_account (%s)\n",
debit_payto,
debit_account);
2020-01-17 04:17:39 +01:00
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"converted credit_payto (%s) to credit_account (%s)\n",
credit_payto,
credit_account);
2018-01-23 10:28:24 +01:00
if (GNUNET_OK !=
2020-01-12 18:14:16 +01:00
TALER_FAKEBANK_check_debit (is->fakebank,
&amount,
debit_account,
credit_account,
exchange_base_url,
&bcs->wtid))
2018-01-23 10:28:24 +01:00
{
GNUNET_break (0);
2020-01-15 15:20:48 +01:00
GNUNET_free (credit_account);
GNUNET_free (debit_account);
2018-01-23 10:28:24 +01:00
TALER_TESTING_interpreter_fail (is);
return;
}
2020-01-15 15:20:48 +01:00
GNUNET_free (credit_account);
GNUNET_free (debit_account);
2018-01-23 10:28:24 +01:00
TALER_TESTING_interpreter_next (is);
}
2019-10-31 12:59:50 +01:00
2018-01-23 10:28:24 +01:00
/**
2018-05-25 20:22:56 +02:00
* Free the state of a "bank check" CMD.
2018-01-23 10:28:24 +01:00
*
2018-05-25 20:22:56 +02:00
* @param cls closure.
2018-01-23 10:28:24 +01:00
* @param cmd the command which is being cleaned up.
*/
2018-06-27 16:49:23 +02:00
static void
2020-01-18 13:57:47 +01:00
check_bank_transfer_cleanup (void *cls,
const struct TALER_TESTING_Command *cmd)
2018-01-23 10:28:24 +01:00
{
struct BankCheckState *bcs = cls;
2019-08-25 16:18:24 +02:00
2018-01-23 10:28:24 +01:00
GNUNET_free (bcs);
}
2019-10-31 12:59:50 +01:00
2018-01-23 10:28:24 +01:00
/**
2018-05-25 20:22:56 +02:00
* Offer internal data from a "bank check" CMD state.
2018-01-23 10:28:24 +01:00
*
2018-05-25 20:22:56 +02:00
* @param cls closure.
2020-01-18 13:57:47 +01:00
* @param[out] ret result.
2018-05-25 20:22:56 +02:00
* @param trait name of the trait.
2018-05-29 12:43:34 +02:00
* @param index index number of the object to offer.
2018-05-25 20:22:56 +02:00
* @return #GNUNET_OK on success.
2018-01-23 10:28:24 +01:00
*/
static int
check_bank_transfer_traits (void *cls,
2018-11-17 17:19:02 +01:00
const void **ret,
2018-01-23 10:28:24 +01:00
const char *trait,
unsigned int index)
{
2019-08-25 16:18:24 +02:00
struct BankCheckState *bcs = cls;
2020-01-12 18:14:16 +01:00
struct TALER_WireTransferIdentifierRawP *wtid_ptr = &bcs->wtid;
struct TALER_TESTING_Trait traits[] = {
2020-01-17 20:42:24 +01:00
TALER_TESTING_make_trait_wtid (0,
wtid_ptr),
TALER_TESTING_make_trait_url (TALER_TESTING_UT_EXCHANGE_BASE_URL,
bcs->exchange_base_url),
2020-01-12 18:14:16 +01:00
TALER_TESTING_trait_end ()
};
2020-01-12 18:14:16 +01:00
return TALER_TESTING_get_trait (traits,
ret,
trait,
index);
2019-08-25 16:18:24 +02:00
}
2018-01-23 10:28:24 +01:00
/**
2018-05-25 20:22:56 +02:00
* Make a "bank check" CMD. It checks whether a
* particular wire transfer has been made or not.
2018-01-23 10:28:24 +01:00
*
2018-05-25 20:22:56 +02:00
* @param label the command label.
* @param exchange_base_url base url of the exchange involved in
* the wire transfer.
* @param amount the amount expected to be transferred.
2020-01-15 15:20:48 +01:00
* @param debit_payto the account that gave money.
* @param credit_payto the account that received money.
2018-01-23 10:28:24 +01:00
*
* @return the command
*/
struct TALER_TESTING_Command
2020-01-18 13:57:47 +01:00
TALER_TESTING_cmd_check_bank_transfer (const char *label,
const char *exchange_base_url,
const char *amount,
const char *debit_payto,
const char *credit_payto)
2018-01-23 10:28:24 +01:00
{
struct BankCheckState *bcs;
bcs = GNUNET_new (struct BankCheckState);
bcs->exchange_base_url = exchange_base_url;
bcs->amount = amount;
2020-01-15 15:20:48 +01:00
bcs->debit_payto = debit_payto;
bcs->credit_payto = credit_payto;
bcs->deposit_reference = NULL;
{
struct TALER_TESTING_Command cmd = {
.label = label,
.cls = bcs,
.run = &check_bank_transfer_run,
.cleanup = &check_bank_transfer_cleanup,
.traits = &check_bank_transfer_traits
};
return cmd;
}
2018-01-23 10:28:24 +01:00
}
2019-10-31 12:59:50 +01:00
/**
2018-05-25 20:22:56 +02:00
* Define a "bank check" CMD that takes the input
* data from another CMD that offers it.
*
* @param label command label.
* @param deposit_reference reference to a CMD that is
* able to provide the "check bank transfer" operation
* input data.
* @return the command.
*/
struct TALER_TESTING_Command
TALER_TESTING_cmd_check_bank_transfer_with_ref
(const char *label,
2019-08-25 16:18:24 +02:00
const char *deposit_reference)
{
struct BankCheckState *bcs;
bcs = GNUNET_new (struct BankCheckState);
bcs->deposit_reference = deposit_reference;
2020-01-12 18:14:16 +01:00
{
struct TALER_TESTING_Command cmd = {
.label = label,
.cls = bcs,
.run = &check_bank_transfer_run,
.cleanup = &check_bank_transfer_cleanup,
.traits = &check_bank_transfer_traits
};
2018-02-27 22:49:18 +01:00
2020-01-12 18:14:16 +01:00
return cmd;
}
}