LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_kyc_wallet_get.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 66 85 77.6 %
Date: 2025-06-05 21:03:14 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2021-2024 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU General Public License as
       7             :   published by the Free Software Foundation; either version 3, or
       8             :   (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU General Public
      16             :   License along with TALER; see the file COPYING.  If not, see
      17             :   <http://www.gnu.org/licenses/>
      18             : */
      19             : 
      20             : /**
      21             :  * @file testing/testing_api_cmd_kyc_wallet_get.c
      22             :  * @brief Implement the testing CMDs for the /kyc_wallet/ GET operations.
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler_json_lib.h"
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_testing_lib.h"
      29             : 
      30             : /**
      31             :  * State for a "/kyc-wallet" GET CMD.
      32             :  */
      33             : struct KycWalletGetState
      34             : {
      35             : 
      36             :   /**
      37             :    * Private key of the reserve (account).
      38             :    */
      39             :   union TALER_AccountPrivateKeyP account_priv;
      40             : 
      41             :   /**
      42             :    * Public key of the reserve (account).
      43             :    */
      44             :   union TALER_AccountPublicKeyP account_pub;
      45             : 
      46             :   /**
      47             :    * Payto URI of the reserve of the wallet.
      48             :    */
      49             :   struct TALER_NormalizedPayto reserve_payto_uri;
      50             : 
      51             :   /**
      52             :    * Our command.
      53             :    */
      54             :   const struct TALER_TESTING_Command *cmd;
      55             : 
      56             :   /**
      57             :    * Command to get a reserve private key from.
      58             :    */
      59             :   const char *reserve_reference;
      60             : 
      61             :   /**
      62             :    * Expected HTTP response code.
      63             :    */
      64             :   unsigned int expected_response_code;
      65             : 
      66             :   /**
      67             :    * Set to the KYC requirement payto hash *if* the exchange replied with a
      68             :    * request for KYC (#MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS).
      69             :    */
      70             :   struct TALER_NormalizedPaytoHashP h_payto;
      71             : 
      72             :   /**
      73             :    * Set to the KYC requirement row *if* the exchange replied with
      74             :    * request for KYC (#MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS).
      75             :    */
      76             :   uint64_t requirement_row;
      77             : 
      78             :   /**
      79             :    * Handle to the "track transaction" pending operation.
      80             :    */
      81             :   struct TALER_EXCHANGE_KycWalletHandle *kwh;
      82             : 
      83             :   /**
      84             :    * Balance to pass to the exchange.
      85             :    */
      86             :   struct TALER_Amount balance;
      87             : 
      88             :   /**
      89             :    * Interpreter state.
      90             :    */
      91             :   struct TALER_TESTING_Interpreter *is;
      92             : };
      93             : 
      94             : 
      95             : /**
      96             :  * Handle response to the command.
      97             :  *
      98             :  * @param cls closure.
      99             :  * @param wkr GET deposit response details
     100             :  */
     101             : static void
     102           7 : wallet_kyc_cb (void *cls,
     103             :                const struct TALER_EXCHANGE_WalletKycResponse *wkr)
     104             : {
     105           7 :   struct KycWalletGetState *kwg = cls;
     106           7 :   struct TALER_TESTING_Interpreter *is = kwg->is;
     107             : 
     108           7 :   kwg->kwh = NULL;
     109           7 :   if (kwg->expected_response_code != wkr->hr.http_status)
     110             :   {
     111           0 :     TALER_TESTING_unexpected_status (is,
     112             :                                      wkr->hr.http_status,
     113             :                                      kwg->expected_response_code);
     114           0 :     return;
     115             :   }
     116           7 :   switch (wkr->hr.http_status)
     117             :   {
     118           0 :   case MHD_HTTP_NO_CONTENT:
     119           0 :     break;
     120           0 :   case MHD_HTTP_FORBIDDEN:
     121           0 :     GNUNET_break (0);
     122           0 :     TALER_TESTING_interpreter_fail (is);
     123           0 :     return;
     124           5 :   case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
     125             :     kwg->requirement_row
     126           5 :       = wkr->details.unavailable_for_legal_reasons.requirement_row;
     127             :     kwg->h_payto
     128           5 :       = wkr->details.unavailable_for_legal_reasons.h_payto;
     129           5 :     break;
     130           2 :   default:
     131           2 :     GNUNET_break (0);
     132           2 :     break;
     133             :   }
     134           7 :   TALER_TESTING_interpreter_next (kwg->is);
     135             : }
     136             : 
     137             : 
     138             : /**
     139             :  * Run the command.
     140             :  *
     141             :  * @param cls closure.
     142             :  * @param cmd the command to execute.
     143             :  * @param is the interpreter state.
     144             :  */
     145             : static void
     146           7 : wallet_kyc_run (void *cls,
     147             :                 const struct TALER_TESTING_Command *cmd,
     148             :                 struct TALER_TESTING_Interpreter *is)
     149             : {
     150           7 :   struct KycWalletGetState *kwg = cls;
     151             :   const char *exchange_url;
     152             : 
     153           7 :   kwg->cmd = cmd;
     154           7 :   kwg->is = is;
     155           7 :   exchange_url = TALER_TESTING_get_exchange_url (is);
     156           7 :   if (NULL == exchange_url)
     157             :   {
     158           0 :     GNUNET_break (0);
     159           0 :     return;
     160             :   }
     161           7 :   if (NULL != kwg->reserve_reference)
     162             :   {
     163             :     const struct TALER_TESTING_Command *res_cmd;
     164             :     const struct TALER_ReservePrivateKeyP *reserve_priv;
     165             : 
     166             :     res_cmd
     167           4 :       = TALER_TESTING_interpreter_lookup_command (kwg->is,
     168             :                                                   kwg->reserve_reference);
     169           4 :     if (NULL == res_cmd)
     170             :     {
     171           0 :       GNUNET_break (0);
     172           0 :       TALER_TESTING_interpreter_fail (kwg->is);
     173           0 :       return;
     174             :     }
     175           4 :     if (GNUNET_OK !=
     176           4 :         TALER_TESTING_get_trait_reserve_priv (
     177             :           res_cmd,
     178             :           &reserve_priv))
     179             :     {
     180           0 :       GNUNET_break (0);
     181           0 :       TALER_TESTING_interpreter_fail (kwg->is);
     182           0 :       return;
     183             :     }
     184           4 :     kwg->account_priv.reserve_priv = *reserve_priv;
     185             :   }
     186             :   else
     187             :   {
     188           3 :     GNUNET_CRYPTO_eddsa_key_create (
     189             :       &kwg->account_priv.reserve_priv.eddsa_priv);
     190             :   }
     191           7 :   GNUNET_CRYPTO_eddsa_key_get_public (
     192           7 :     &kwg->account_priv.reserve_priv.eddsa_priv,
     193             :     &kwg->account_pub.reserve_pub.eddsa_pub);
     194             :   kwg->reserve_payto_uri
     195           7 :     = TALER_reserve_make_payto (exchange_url,
     196           7 :                                 &kwg->account_pub.reserve_pub);
     197           7 :   kwg->kwh = TALER_EXCHANGE_kyc_wallet (
     198             :     TALER_TESTING_interpreter_get_context (is),
     199             :     exchange_url,
     200           7 :     &kwg->account_priv.reserve_priv,
     201           7 :     &kwg->balance,
     202             :     &wallet_kyc_cb,
     203             :     kwg);
     204           7 :   GNUNET_assert (NULL != kwg->kwh);
     205             : }
     206             : 
     207             : 
     208             : /**
     209             :  * Cleanup the state from a "track transaction" CMD, and possibly
     210             :  * cancel a operation thereof.
     211             :  *
     212             :  * @param cls closure with our `struct KycWalletGetState`
     213             :  * @param cmd the command which is being cleaned up.
     214             :  */
     215             : static void
     216           7 : wallet_kyc_cleanup (
     217             :   void *cls,
     218             :   const struct TALER_TESTING_Command *cmd)
     219             : {
     220           7 :   struct KycWalletGetState *kwg = cls;
     221             : 
     222           7 :   if (NULL != kwg->kwh)
     223             :   {
     224           0 :     TALER_TESTING_command_incomplete (kwg->is,
     225             :                                       cmd->label);
     226           0 :     TALER_EXCHANGE_kyc_wallet_cancel (kwg->kwh);
     227           0 :     kwg->kwh = NULL;
     228             :   }
     229           7 :   GNUNET_free (kwg->reserve_payto_uri.normalized_payto);
     230           7 :   GNUNET_free (kwg);
     231           7 : }
     232             : 
     233             : 
     234             : /**
     235             :  * Offer internal data from a "wallet KYC" CMD.
     236             :  *
     237             :  * @param cls closure.
     238             :  * @param[out] ret result (could be anything).
     239             :  * @param trait name of the trait.
     240             :  * @param index index number of the object to offer.
     241             :  * @return #GNUNET_OK on success.
     242             :  */
     243             : static enum GNUNET_GenericReturnValue
     244          20 : wallet_kyc_traits (void *cls,
     245             :                    const void **ret,
     246             :                    const char *trait,
     247             :                    unsigned int index)
     248             : {
     249          20 :   struct KycWalletGetState *kwg = cls;
     250             :   struct TALER_TESTING_Trait traits[] = {
     251          20 :     TALER_TESTING_make_trait_account_priv (
     252          20 :       &kwg->account_priv),
     253          20 :     TALER_TESTING_make_trait_account_pub (
     254          20 :       &kwg->account_pub),
     255          20 :     TALER_TESTING_make_trait_reserve_priv (
     256          20 :       &kwg->account_priv.reserve_priv),
     257          20 :     TALER_TESTING_make_trait_reserve_pub (
     258          20 :       &kwg->account_pub.reserve_pub),
     259          20 :     TALER_TESTING_make_trait_legi_requirement_row (
     260          20 :       &kwg->requirement_row),
     261          20 :     TALER_TESTING_make_trait_h_normalized_payto (
     262          20 :       &kwg->h_payto),
     263          20 :     TALER_TESTING_make_trait_normalized_payto_uri (
     264          20 :       &kwg->reserve_payto_uri),
     265          20 :     TALER_TESTING_trait_end ()
     266             :   };
     267             : 
     268          20 :   return TALER_TESTING_get_trait (traits,
     269             :                                   ret,
     270             :                                   trait,
     271             :                                   index);
     272             : }
     273             : 
     274             : 
     275             : struct TALER_TESTING_Command
     276           7 : TALER_TESTING_cmd_wallet_kyc_get (
     277             :   const char *label,
     278             :   const char *reserve_reference,
     279             :   const char *threshold_balance,
     280             :   unsigned int expected_response_code)
     281             : {
     282             :   struct KycWalletGetState *kwg;
     283             : 
     284           7 :   kwg = GNUNET_new (struct KycWalletGetState);
     285           7 :   kwg->reserve_reference = reserve_reference;
     286           7 :   kwg->expected_response_code = expected_response_code;
     287           7 :   GNUNET_assert (GNUNET_OK ==
     288             :                  TALER_string_to_amount (threshold_balance,
     289             :                                          &kwg->balance));
     290             :   {
     291           7 :     struct TALER_TESTING_Command cmd = {
     292             :       .cls = kwg,
     293             :       .label = label,
     294             :       .run = &wallet_kyc_run,
     295             :       .cleanup = &wallet_kyc_cleanup,
     296             :       .traits = &wallet_kyc_traits
     297             :     };
     298             : 
     299           7 :     return cmd;
     300             :   }
     301             : }
     302             : 
     303             : 
     304             : /* end of testing_api_cmd_kyc_wallet_get.c */

Generated by: LCOV version 1.16