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


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