LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_purse_delete.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 58.0 % 50 29
Test Date: 2026-04-14 15:39:31 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020-2026 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify it
       6              :   under the terms of the GNU General Public License as published by
       7              :   the Free Software Foundation; either version 3, or (at your
       8              :   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 GNU
      13              :   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              :  * @file testing/testing_api_cmd_purse_delete.c
      21              :  * @brief command for testing /management/purse/disable.
      22              :  * @author Christian Grothoff
      23              :  */
      24              : #include "taler/taler_json_lib.h"
      25              : #include <gnunet/gnunet_curl_lib.h>
      26              : #include "taler/taler_testing_lib.h"
      27              : 
      28              : 
      29              : /**
      30              :  * State for a "purse_delete" CMD.
      31              :  */
      32              : struct PurseDeleteState
      33              : {
      34              : 
      35              :   /**
      36              :    * Purse delete handle while operation is running.
      37              :    */
      38              :   struct TALER_EXCHANGE_DeletePursesHandle *dph;
      39              : 
      40              :   /**
      41              :    * Our interpreter.
      42              :    */
      43              :   struct TALER_TESTING_Interpreter *is;
      44              : 
      45              :   /**
      46              :    * Expected HTTP response code.
      47              :    */
      48              :   unsigned int expected_response_code;
      49              : 
      50              :   /**
      51              :    * Command that created the purse we now want to
      52              :    * delete.
      53              :    */
      54              :   const char *purse_cmd;
      55              : };
      56              : 
      57              : 
      58              : /**
      59              :  * Callback to analyze the DELETE /purses/$PID response, just used to check if
      60              :  * the response code is acceptable.
      61              :  *
      62              :  * @param cls closure.
      63              :  * @param pdr HTTP response details
      64              :  */
      65              : static void
      66            2 : purse_delete_cb (void *cls,
      67              :                  const struct TALER_EXCHANGE_DeletePursesResponse *pdr)
      68              : {
      69            2 :   struct PurseDeleteState *pds = cls;
      70              : 
      71            2 :   pds->dph = NULL;
      72            2 :   if (pds->expected_response_code != pdr->hr.http_status)
      73              :   {
      74            0 :     TALER_TESTING_unexpected_status (pds->is,
      75              :                                      pdr->hr.http_status,
      76              :                                      pds->expected_response_code);
      77            0 :     return;
      78              :   }
      79            2 :   TALER_TESTING_interpreter_next (pds->is);
      80              : }
      81              : 
      82              : 
      83              : /**
      84              :  * Run the command.
      85              :  *
      86              :  * @param cls closure.
      87              :  * @param cmd the command to execute.
      88              :  * @param is the interpreter state.
      89              :  */
      90              : static void
      91            2 : purse_delete_run (void *cls,
      92              :                   const struct TALER_TESTING_Command *cmd,
      93              :                   struct TALER_TESTING_Interpreter *is)
      94              : {
      95            2 :   struct PurseDeleteState *pds = cls;
      96              :   const struct TALER_PurseContractPrivateKeyP *purse_priv;
      97              :   const struct TALER_TESTING_Command *ref;
      98              :   const char *exchange_url;
      99              : 
     100              :   (void) cmd;
     101            2 :   exchange_url = TALER_TESTING_get_exchange_url (is);
     102            2 :   if (NULL == exchange_url)
     103              :   {
     104            0 :     GNUNET_break (0);
     105            0 :     return;
     106              :   }
     107            2 :   ref = TALER_TESTING_interpreter_lookup_command (is,
     108              :                                                   pds->purse_cmd);
     109            2 :   if (NULL == ref)
     110              :   {
     111            0 :     GNUNET_break (0);
     112            0 :     TALER_TESTING_interpreter_fail (is);
     113            0 :     return;
     114              :   }
     115            2 :   if (GNUNET_OK !=
     116            2 :       TALER_TESTING_get_trait_purse_priv (ref,
     117              :                                           &purse_priv))
     118              :   {
     119            0 :     GNUNET_break (0);
     120            0 :     TALER_TESTING_interpreter_fail (is);
     121            0 :     return;
     122              :   }
     123            2 :   pds->is = is;
     124            2 :   pds->dph = TALER_EXCHANGE_delete_purses_create (
     125              :     TALER_TESTING_interpreter_get_context (is),
     126              :     exchange_url,
     127              :     purse_priv);
     128            2 :   if (NULL == pds->dph)
     129              :   {
     130            0 :     GNUNET_break (0);
     131            0 :     TALER_TESTING_interpreter_fail (is);
     132            0 :     return;
     133              :   }
     134              :   {
     135              :     enum TALER_ErrorCode ec;
     136              : 
     137            2 :     ec = TALER_EXCHANGE_delete_purses_start (
     138              :       pds->dph,
     139              :       &purse_delete_cb,
     140              :       pds);
     141            2 :     if (TALER_EC_NONE != ec)
     142              :     {
     143            0 :       GNUNET_break (0);
     144            0 :       TALER_EXCHANGE_delete_purses_cancel (pds->dph);
     145            0 :       pds->dph = NULL;
     146            0 :       TALER_TESTING_interpreter_fail (is);
     147            0 :       return;
     148              :     }
     149              :   }
     150              : }
     151              : 
     152              : 
     153              : /**
     154              :  * Free the state of a "purse_delete" CMD, and possibly cancel a
     155              :  * pending operation thereof.
     156              :  *
     157              :  * @param cls closure, must be a `struct PurseDeleteState`.
     158              :  * @param cmd the command which is being cleaned up.
     159              :  */
     160              : static void
     161            2 : purse_delete_cleanup (void *cls,
     162              :                       const struct TALER_TESTING_Command *cmd)
     163              : {
     164            2 :   struct PurseDeleteState *pds = cls;
     165              : 
     166            2 :   if (NULL != pds->dph)
     167              :   {
     168            0 :     TALER_TESTING_command_incomplete (pds->is,
     169              :                                       cmd->label);
     170            0 :     TALER_EXCHANGE_delete_purses_cancel (pds->dph);
     171            0 :     pds->dph = NULL;
     172              :   }
     173            2 :   GNUNET_free (pds);
     174            2 : }
     175              : 
     176              : 
     177              : struct TALER_TESTING_Command
     178            2 : TALER_TESTING_cmd_purse_delete (const char *label,
     179              :                                 unsigned int expected_http_status,
     180              :                                 const char *purse_cmd)
     181              : {
     182              :   struct PurseDeleteState *ds;
     183              : 
     184            2 :   ds = GNUNET_new (struct PurseDeleteState);
     185            2 :   ds->expected_response_code = expected_http_status;
     186            2 :   ds->purse_cmd = purse_cmd;
     187              :   {
     188            2 :     struct TALER_TESTING_Command cmd = {
     189              :       .cls = ds,
     190              :       .label = label,
     191              :       .run = &purse_delete_run,
     192              :       .cleanup = &purse_delete_cleanup
     193              :     };
     194              : 
     195            2 :     return cmd;
     196              :   }
     197              : }
     198              : 
     199              : 
     200              : /* end of testing_api_cmd_purse_delete.c */
        

Generated by: LCOV version 2.0-1