LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_kyc_proof.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 70.4 % 71 50
Test Date: 2026-03-07 14:54:45 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2021-2026 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_proof.c
      22              :  * @brief Implement the testing CMDs for the /kyc-proof/ operation.
      23              :  * @author Christian Grothoff
      24              :  */
      25              : #include "taler/platform.h"
      26              : #include "taler/taler_json_lib.h"
      27              : #include <gnunet/gnunet_curl_lib.h>
      28              : 
      29              : /**
      30              :  * State for a "track transaction" CMD.
      31              :  */
      32              : struct KycProofGetState;
      33              : 
      34              : #define TALER_EXCHANGE_GET_KYC_PROOF_RESULT_CLOSURE \
      35              :         struct KycProofGetState
      36              : #include "taler/taler-exchange/get-kyc-proof-PROVIDER_NAME.h"
      37              : #include "taler/taler_testing_lib.h"
      38              : 
      39              : /**
      40              :  * State for a "track transaction" CMD.
      41              :  */
      42              : struct KycProofGetState
      43              : {
      44              : 
      45              :   /**
      46              :    * Command to get a reserve private key from.
      47              :    */
      48              :   const char *payment_target_reference;
      49              : 
      50              :   /**
      51              :    * Code to pass.
      52              :    */
      53              :   const char *code;
      54              : 
      55              :   /**
      56              :    * Logic section name to pass to `/kyc-proof/` handler.
      57              :    */
      58              :   const char *logic;
      59              : 
      60              :   /**
      61              :    * Expected HTTP response code.
      62              :    */
      63              :   unsigned int expected_response_code;
      64              : 
      65              :   /**
      66              :    * Set to the KYC REDIRECT *if* the exchange replied with
      67              :    * success (#MHD_HTTP_OK).
      68              :    */
      69              :   char *redirect_url;
      70              : 
      71              :   /**
      72              :    * Additional URL arguments (e.g. "&code=..."), kept alive
      73              :    * for the lifetime of the request.
      74              :    */
      75              :   char *uargs;
      76              : 
      77              :   /**
      78              :    * Handle to the "track transaction" pending operation.
      79              :    */
      80              :   struct TALER_EXCHANGE_GetKycProofHandle *kph;
      81              : 
      82              :   /**
      83              :    * Interpreter state.
      84              :    */
      85              :   struct TALER_TESTING_Interpreter *is;
      86              : };
      87              : 
      88              : 
      89              : /**
      90              :  * Handle response to the command.
      91              :  *
      92              :  * @param kcg our state
      93              :  * @param kpr KYC proof response details
      94              :  */
      95              : static void
      96           11 : proof_kyc_cb (
      97              :   struct KycProofGetState *kcg,
      98              :   const struct TALER_EXCHANGE_GetKycProofResponse *kpr)
      99              : {
     100           11 :   struct TALER_TESTING_Interpreter *is = kcg->is;
     101              : 
     102           11 :   kcg->kph = NULL;
     103           11 :   if (kcg->expected_response_code != kpr->hr.http_status)
     104              :   {
     105            0 :     TALER_TESTING_unexpected_status (is,
     106              :                                      kpr->hr.http_status,
     107              :                                      kcg->expected_response_code);
     108            0 :     return;
     109              :   }
     110           11 :   switch (kpr->hr.http_status)
     111              :   {
     112            9 :   case MHD_HTTP_SEE_OTHER:
     113            9 :     kcg->redirect_url = GNUNET_strdup (kpr->details.found.redirect_url);
     114            9 :     break;
     115            1 :   case MHD_HTTP_FORBIDDEN:
     116            1 :     break;
     117            1 :   case MHD_HTTP_BAD_GATEWAY:
     118            1 :     break;
     119            0 :   default:
     120            0 :     GNUNET_break (0);
     121            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     122              :                 "Unexpected response code %u to /kyc-proof\n",
     123              :                 kpr->hr.http_status);
     124            0 :     break;
     125              :   }
     126           11 :   TALER_TESTING_interpreter_next (kcg->is);
     127              : }
     128              : 
     129              : 
     130              : /**
     131              :  * Run the command.
     132              :  *
     133              :  * @param cls closure.
     134              :  * @param cmd the command to execute.
     135              :  * @param is the interpreter state.
     136              :  */
     137              : static void
     138           11 : proof_kyc_run (void *cls,
     139              :                const struct TALER_TESTING_Command *cmd,
     140              :                struct TALER_TESTING_Interpreter *is)
     141              : {
     142           11 :   struct KycProofGetState *kps = cls;
     143              :   const struct TALER_TESTING_Command *res_cmd;
     144              :   const struct TALER_NormalizedPaytoHashP *h_payto;
     145              :   const char *exchange_url;
     146              : 
     147              :   (void) cmd;
     148           11 :   kps->is = is;
     149           11 :   exchange_url = TALER_TESTING_get_exchange_url (is);
     150           11 :   if (NULL == exchange_url)
     151              :   {
     152            0 :     GNUNET_break (0);
     153            0 :     return;
     154              :   }
     155           11 :   res_cmd = TALER_TESTING_interpreter_lookup_command (
     156              :     kps->is,
     157              :     kps->payment_target_reference);
     158           11 :   if (NULL == res_cmd)
     159              :   {
     160            0 :     GNUNET_break (0);
     161            0 :     TALER_TESTING_interpreter_fail (kps->is);
     162            0 :     return;
     163              :   }
     164           11 :   if (GNUNET_OK !=
     165           11 :       TALER_TESTING_get_trait_h_normalized_payto (res_cmd,
     166              :                                                   &h_payto))
     167              :   {
     168            0 :     GNUNET_break (0);
     169            0 :     TALER_TESTING_interpreter_fail (kps->is);
     170            0 :     return;
     171              :   }
     172           11 :   if (NULL != kps->code)
     173           11 :     GNUNET_asprintf (&kps->uargs,
     174              :                      "&code=%s",
     175              :                      kps->code);
     176           11 :   kps->kph = TALER_EXCHANGE_get_kyc_proof_create (
     177              :     TALER_TESTING_interpreter_get_context (is),
     178              :     exchange_url,
     179              :     h_payto,
     180              :     kps->logic);
     181           11 :   GNUNET_assert (NULL != kps->kph);
     182           11 :   if (NULL != kps->uargs)
     183           11 :     TALER_EXCHANGE_get_kyc_proof_set_options (
     184              :       kps->kph,
     185              :       TALER_EXCHANGE_get_kyc_proof_option_args (kps->uargs));
     186              :   {
     187              :     enum TALER_ErrorCode ec;
     188              : 
     189           11 :     ec = TALER_EXCHANGE_get_kyc_proof_start (kps->kph,
     190              :                                              &proof_kyc_cb,
     191              :                                              kps);
     192           11 :     if (TALER_EC_NONE != ec)
     193              :     {
     194            0 :       GNUNET_break (0);
     195            0 :       kps->kph = NULL;
     196            0 :       TALER_TESTING_interpreter_fail (kps->is);
     197            0 :       return;
     198              :     }
     199              :   }
     200              : }
     201              : 
     202              : 
     203              : /**
     204              :  * Cleanup the state from a "kyc proof" CMD, and possibly
     205              :  * cancel a operation thereof.
     206              :  *
     207              :  * @param cls closure.
     208              :  * @param cmd the command which is being cleaned up.
     209              :  */
     210              : static void
     211           11 : proof_kyc_cleanup (void *cls,
     212              :                    const struct TALER_TESTING_Command *cmd)
     213              : {
     214           11 :   struct KycProofGetState *kps = cls;
     215              : 
     216           11 :   if (NULL != kps->kph)
     217              :   {
     218            0 :     TALER_TESTING_command_incomplete (kps->is,
     219              :                                       cmd->label);
     220            0 :     TALER_EXCHANGE_get_kyc_proof_cancel (kps->kph);
     221            0 :     kps->kph = NULL;
     222              :   }
     223           11 :   GNUNET_free (kps->redirect_url);
     224           11 :   GNUNET_free (kps->uargs);
     225           11 :   GNUNET_free (kps);
     226           11 : }
     227              : 
     228              : 
     229              : /**
     230              :  * Offer internal data from a "proof KYC" CMD.
     231              :  *
     232              :  * @param cls closure.
     233              :  * @param[out] ret result (could be anything).
     234              :  * @param trait name of the trait.
     235              :  * @param index index number of the object to offer.
     236              :  * @return #GNUNET_OK on success.
     237              :  */
     238              : static enum GNUNET_GenericReturnValue
     239           25 : proof_kyc_traits (void *cls,
     240              :                   const void **ret,
     241              :                   const char *trait,
     242              :                   unsigned int index)
     243              : {
     244           25 :   struct KycProofGetState *kps = cls;
     245              :   struct TALER_TESTING_Trait traits[] = {
     246           25 :     TALER_TESTING_make_trait_web_url (kps->redirect_url),
     247           25 :     TALER_TESTING_trait_end ()
     248              :   };
     249              : 
     250           25 :   return TALER_TESTING_get_trait (traits,
     251              :                                   ret,
     252              :                                   trait,
     253              :                                   index);
     254              : }
     255              : 
     256              : 
     257              : struct TALER_TESTING_Command
     258           11 : TALER_TESTING_cmd_proof_kyc_oauth2 (
     259              :   const char *label,
     260              :   const char *payment_target_reference,
     261              :   const char *logic_section,
     262              :   const char *code,
     263              :   unsigned int expected_response_code)
     264              : {
     265              :   struct KycProofGetState *kps;
     266              : 
     267           11 :   kps = GNUNET_new (struct KycProofGetState);
     268           11 :   kps->code = code;
     269           11 :   kps->logic = logic_section;
     270           11 :   kps->payment_target_reference = payment_target_reference;
     271           11 :   kps->expected_response_code = expected_response_code;
     272              :   {
     273           11 :     struct TALER_TESTING_Command cmd = {
     274              :       .cls = kps,
     275              :       .label = label,
     276              :       .run = &proof_kyc_run,
     277              :       .cleanup = &proof_kyc_cleanup,
     278              :       .traits = &proof_kyc_traits
     279              :     };
     280              : 
     281           11 :     return cmd;
     282              :   }
     283              : }
     284              : 
     285              : 
     286              : /* end of testing_api_cmd_kyc_proof.c */
        

Generated by: LCOV version 2.0-1