LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_reserve_get_attestable.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 70.8 % 65 46
Test Date: 2025-12-26 23:00:34 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2014-2022 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              :  * @file testing/testing_api_cmd_reserve_get_attestable.c
      21              :  * @brief Implement the /reserve/$RID/get_attestable test command.
      22              :  * @author Christian Grothoff
      23              :  */
      24              : #include "taler/platform.h"
      25              : #include "taler/taler_json_lib.h"
      26              : #include <gnunet/gnunet_curl_lib.h>
      27              : #include "taler/taler_testing_lib.h"
      28              : 
      29              : 
      30              : /**
      31              :  * State for a "get_attestable" CMD.
      32              :  */
      33              : struct GetAttestableState
      34              : {
      35              :   /**
      36              :    * Label to the command which created the reserve to check,
      37              :    * needed to resort the reserve key.
      38              :    */
      39              :   const char *reserve_reference;
      40              : 
      41              :   /**
      42              :    * Handle to the "reserve get_attestable" operation.
      43              :    */
      44              :   struct TALER_EXCHANGE_ReservesGetAttestHandle *rgah;
      45              : 
      46              :   /**
      47              :    * Expected attestable attributes.
      48              :    */
      49              :   const char **expected_attestables;
      50              : 
      51              :   /**
      52              :    * Length of the @e expected_attestables array.
      53              :    */
      54              :   unsigned int expected_attestables_length;
      55              : 
      56              :   /**
      57              :    * Public key of the reserve being analyzed.
      58              :    */
      59              :   struct TALER_ReservePublicKeyP reserve_pub;
      60              : 
      61              :   /**
      62              :    * Expected HTTP response code.
      63              :    */
      64              :   unsigned int expected_response_code;
      65              : 
      66              :   /**
      67              :    * Interpreter state.
      68              :    */
      69              :   struct TALER_TESTING_Interpreter *is;
      70              : };
      71              : 
      72              : 
      73              : /**
      74              :  * Check that the reserve balance and HTTP response code are
      75              :  * both acceptable.
      76              :  *
      77              :  * @param cls closure.
      78              :  * @param rs HTTP response details
      79              :  */
      80              : static void
      81            6 : reserve_get_attestable_cb (
      82              :   void *cls,
      83              :   const struct TALER_EXCHANGE_ReserveGetAttestResult *rs)
      84              : {
      85            6 :   struct GetAttestableState *ss = cls;
      86            6 :   struct TALER_TESTING_Interpreter *is = ss->is;
      87              : 
      88            6 :   ss->rgah = NULL;
      89            6 :   if (ss->expected_response_code != rs->hr.http_status)
      90              :   {
      91            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      92              :                 "Unexpected HTTP response code: %d in %s:%u\n",
      93              :                 rs->hr.http_status,
      94              :                 __FILE__,
      95              :                 __LINE__);
      96            0 :     json_dumpf (rs->hr.reply,
      97              :                 stderr,
      98              :                 JSON_INDENT (2));
      99            0 :     TALER_TESTING_interpreter_fail (ss->is);
     100            0 :     return;
     101              :   }
     102            6 :   if (MHD_HTTP_OK != rs->hr.http_status)
     103              :   {
     104            5 :     TALER_TESTING_interpreter_next (is);
     105            5 :     return;
     106              :   }
     107              :   // FIXME: check returned list matches expectations!
     108            1 :   TALER_TESTING_interpreter_next (is);
     109              : }
     110              : 
     111              : 
     112              : /**
     113              :  * Run the command.
     114              :  *
     115              :  * @param cls closure.
     116              :  * @param cmd the command being executed.
     117              :  * @param is the interpreter state.
     118              :  */
     119              : static void
     120            6 : get_attestable_run (void *cls,
     121              :                     const struct TALER_TESTING_Command *cmd,
     122              :                     struct TALER_TESTING_Interpreter *is)
     123              : {
     124            6 :   struct GetAttestableState *ss = cls;
     125              :   const struct TALER_TESTING_Command *ref_reserve;
     126              :   const struct TALER_ReservePrivateKeyP *reserve_priv;
     127              :   const struct TALER_ReservePublicKeyP *reserve_pub;
     128              :   const char *exchange_url;
     129              : 
     130            6 :   ss->is = is;
     131            6 :   exchange_url = TALER_TESTING_get_exchange_url (is);
     132            6 :   if (NULL == exchange_url)
     133              :   {
     134            0 :     GNUNET_break (0);
     135            0 :     return;
     136              :   }
     137              :   ref_reserve
     138            6 :     = TALER_TESTING_interpreter_lookup_command (is,
     139              :                                                 ss->reserve_reference);
     140              : 
     141            6 :   if (NULL == ref_reserve)
     142              :   {
     143            0 :     GNUNET_break (0);
     144            0 :     TALER_TESTING_interpreter_fail (is);
     145            0 :     return;
     146              :   }
     147            6 :   if (GNUNET_OK ==
     148            6 :       TALER_TESTING_get_trait_reserve_priv (ref_reserve,
     149              :                                             &reserve_priv))
     150              :   {
     151            6 :     GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
     152              :                                         &ss->reserve_pub.eddsa_pub);
     153              :   }
     154              :   else
     155              :   {
     156            0 :     if (GNUNET_OK !=
     157            0 :         TALER_TESTING_get_trait_reserve_pub (ref_reserve,
     158              :                                              &reserve_pub))
     159              :     {
     160            0 :       GNUNET_break (0);
     161            0 :       TALER_LOG_ERROR (
     162              :         "Failed to find reserve_priv for get_attestable query\n");
     163            0 :       TALER_TESTING_interpreter_fail (is);
     164            0 :       return;
     165              :     }
     166            0 :     ss->reserve_pub = *reserve_pub;
     167              :   }
     168            6 :   ss->rgah = TALER_EXCHANGE_reserves_get_attestable (
     169              :     TALER_TESTING_interpreter_get_context (is),
     170              :     exchange_url,
     171            6 :     &ss->reserve_pub,
     172              :     &reserve_get_attestable_cb,
     173              :     ss);
     174              : }
     175              : 
     176              : 
     177              : /**
     178              :  * Cleanup the state from a "reserve get_attestable" CMD, and possibly
     179              :  * cancel a pending operation thereof.
     180              :  *
     181              :  * @param cls closure.
     182              :  * @param cmd the command which is being cleaned up.
     183              :  */
     184              : static void
     185            6 : get_attestable_cleanup (void *cls,
     186              :                         const struct TALER_TESTING_Command *cmd)
     187              : {
     188            6 :   struct GetAttestableState *ss = cls;
     189              : 
     190            6 :   if (NULL != ss->rgah)
     191              :   {
     192            0 :     TALER_TESTING_command_incomplete (ss->is,
     193              :                                       cmd->label);
     194            0 :     TALER_EXCHANGE_reserves_get_attestable_cancel (ss->rgah);
     195            0 :     ss->rgah = NULL;
     196              :   }
     197            6 :   GNUNET_free (ss->expected_attestables);
     198            6 :   GNUNET_free (ss);
     199            6 : }
     200              : 
     201              : 
     202              : struct TALER_TESTING_Command
     203            6 : TALER_TESTING_cmd_reserve_get_attestable (const char *label,
     204              :                                           const char *reserve_reference,
     205              :                                           unsigned int expected_response_code,
     206              :                                           ...)
     207              : {
     208              :   struct GetAttestableState *ss;
     209              :   va_list ap;
     210              :   unsigned int num_expected;
     211              :   const char *ea;
     212              : 
     213            6 :   num_expected = 0;
     214            6 :   va_start (ap, expected_response_code);
     215            9 :   while (NULL != va_arg (ap, const char *))
     216            3 :     num_expected++;
     217            6 :   va_end (ap);
     218              : 
     219            6 :   GNUNET_assert (NULL != reserve_reference);
     220            6 :   ss = GNUNET_new (struct GetAttestableState);
     221            6 :   ss->reserve_reference = reserve_reference;
     222            6 :   ss->expected_response_code = expected_response_code;
     223            6 :   ss->expected_attestables_length = num_expected;
     224            6 :   ss->expected_attestables = GNUNET_new_array (num_expected,
     225              :                                                const char *);
     226            6 :   num_expected = 0;
     227            6 :   va_start (ap, expected_response_code);
     228            9 :   while (NULL != (ea = va_arg (ap, const char *)))
     229            3 :     ss->expected_attestables[num_expected++] = ea;
     230            6 :   va_end (ap);
     231              : 
     232              :   {
     233            6 :     struct TALER_TESTING_Command cmd = {
     234              :       .cls = ss,
     235              :       .label = label,
     236              :       .run = &get_attestable_run,
     237              :       .cleanup = &get_attestable_cleanup
     238              :     };
     239              : 
     240            6 :     return cmd;
     241              :   }
     242              : }
        

Generated by: LCOV version 2.0-1