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

Generated by: LCOV version 2.0-1