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 (6863B)


      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_GetReservesAttestHandle *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 reserve_get_attestable_cb (
     82   void *cls,
     83   const struct TALER_EXCHANGE_GetReservesAttestResponse *rs)
     84 {
     85   struct GetAttestableState *ss = cls;
     86   struct TALER_TESTING_Interpreter *is = ss->is;
     87 
     88   ss->rgah = NULL;
     89   if (ss->expected_response_code != rs->hr.http_status)
     90   {
     91     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     json_dumpf (rs->hr.reply,
     97                 stderr,
     98                 JSON_INDENT (2));
     99     TALER_TESTING_interpreter_fail (ss->is);
    100     return;
    101   }
    102   if (MHD_HTTP_OK != rs->hr.http_status)
    103   {
    104     TALER_TESTING_interpreter_next (is);
    105     return;
    106   }
    107   // FIXME: check returned list matches expectations!
    108   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 get_attestable_run (void *cls,
    121                     const struct TALER_TESTING_Command *cmd,
    122                     struct TALER_TESTING_Interpreter *is)
    123 {
    124   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   ss->is = is;
    131   exchange_url = TALER_TESTING_get_exchange_url (is);
    132   if (NULL == exchange_url)
    133   {
    134     GNUNET_break (0);
    135     return;
    136   }
    137   ref_reserve
    138     = TALER_TESTING_interpreter_lookup_command (is,
    139                                                 ss->reserve_reference);
    140 
    141   if (NULL == ref_reserve)
    142   {
    143     GNUNET_break (0);
    144     TALER_TESTING_interpreter_fail (is);
    145     return;
    146   }
    147   if (GNUNET_OK ==
    148       TALER_TESTING_get_trait_reserve_priv (ref_reserve,
    149                                             &reserve_priv))
    150   {
    151     GNUNET_CRYPTO_eddsa_key_get_public (&reserve_priv->eddsa_priv,
    152                                         &ss->reserve_pub.eddsa_pub);
    153   }
    154   else
    155   {
    156     if (GNUNET_OK !=
    157         TALER_TESTING_get_trait_reserve_pub (ref_reserve,
    158                                              &reserve_pub))
    159     {
    160       GNUNET_break (0);
    161       TALER_LOG_ERROR (
    162         "Failed to find reserve_priv for get_attestable query\n");
    163       TALER_TESTING_interpreter_fail (is);
    164       return;
    165     }
    166     ss->reserve_pub = *reserve_pub;
    167   }
    168   ss->rgah = TALER_EXCHANGE_get_reserves_attest_create (
    169     TALER_TESTING_interpreter_get_context (is),
    170     exchange_url,
    171     &ss->reserve_pub);
    172   if (NULL == ss->rgah)
    173   {
    174     GNUNET_break (0);
    175     TALER_TESTING_interpreter_fail (is);
    176     return;
    177   }
    178   if (TALER_EC_NONE !=
    179       TALER_EXCHANGE_get_reserves_attest_start (ss->rgah,
    180                                                 &reserve_get_attestable_cb,
    181                                                 ss))
    182   {
    183     GNUNET_break (0);
    184     TALER_EXCHANGE_get_reserves_attest_cancel (ss->rgah);
    185     ss->rgah = NULL;
    186     TALER_TESTING_interpreter_fail (is);
    187     return;
    188   }
    189 }
    190 
    191 
    192 /**
    193  * Cleanup the state from a "reserve get_attestable" CMD, and possibly
    194  * cancel a pending operation thereof.
    195  *
    196  * @param cls closure.
    197  * @param cmd the command which is being cleaned up.
    198  */
    199 static void
    200 get_attestable_cleanup (void *cls,
    201                         const struct TALER_TESTING_Command *cmd)
    202 {
    203   struct GetAttestableState *ss = cls;
    204 
    205   if (NULL != ss->rgah)
    206   {
    207     TALER_TESTING_command_incomplete (ss->is,
    208                                       cmd->label);
    209     TALER_EXCHANGE_get_reserves_attest_cancel (ss->rgah);
    210     ss->rgah = NULL;
    211   }
    212   GNUNET_free (ss->expected_attestables);
    213   GNUNET_free (ss);
    214 }
    215 
    216 
    217 struct TALER_TESTING_Command
    218 TALER_TESTING_cmd_reserve_get_attestable (const char *label,
    219                                           const char *reserve_reference,
    220                                           unsigned int expected_response_code,
    221                                           ...)
    222 {
    223   struct GetAttestableState *ss;
    224   va_list ap;
    225   unsigned int num_expected;
    226   const char *ea;
    227 
    228   num_expected = 0;
    229   va_start (ap, expected_response_code);
    230   while (NULL != va_arg (ap, const char *))
    231     num_expected++;
    232   va_end (ap);
    233 
    234   GNUNET_assert (NULL != reserve_reference);
    235   ss = GNUNET_new (struct GetAttestableState);
    236   ss->reserve_reference = reserve_reference;
    237   ss->expected_response_code = expected_response_code;
    238   ss->expected_attestables_length = num_expected;
    239   ss->expected_attestables = GNUNET_new_array (num_expected,
    240                                                const char *);
    241   num_expected = 0;
    242   va_start (ap, expected_response_code);
    243   while (NULL != (ea = va_arg (ap, const char *)))
    244     ss->expected_attestables[num_expected++] = ea;
    245   va_end (ap);
    246 
    247   {
    248     struct TALER_TESTING_Command cmd = {
    249       .cls = ss,
    250       .label = label,
    251       .run = &get_attestable_run,
    252       .cleanup = &get_attestable_cleanup
    253     };
    254 
    255     return cmd;
    256   }
    257 }