exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_reserve_get_attestable.c (6996B)


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