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


      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 reserve_get_attestable_cb (
     82   void *cls,
     83   const struct TALER_EXCHANGE_ReserveGetAttestResult *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_reserves_get_attestable (
    169     TALER_TESTING_interpreter_get_context (is),
    170     exchange_url,
    171     &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 get_attestable_cleanup (void *cls,
    186                         const struct TALER_TESTING_Command *cmd)
    187 {
    188   struct GetAttestableState *ss = cls;
    189 
    190   if (NULL != ss->rgah)
    191   {
    192     TALER_TESTING_command_incomplete (ss->is,
    193                                       cmd->label);
    194     TALER_EXCHANGE_reserves_get_attestable_cancel (ss->rgah);
    195     ss->rgah = NULL;
    196   }
    197   GNUNET_free (ss->expected_attestables);
    198   GNUNET_free (ss);
    199 }
    200 
    201 
    202 struct TALER_TESTING_Command
    203 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   num_expected = 0;
    214   va_start (ap, expected_response_code);
    215   while (NULL != va_arg (ap, const char *))
    216     num_expected++;
    217   va_end (ap);
    218 
    219   GNUNET_assert (NULL != reserve_reference);
    220   ss = GNUNET_new (struct GetAttestableState);
    221   ss->reserve_reference = reserve_reference;
    222   ss->expected_response_code = expected_response_code;
    223   ss->expected_attestables_length = num_expected;
    224   ss->expected_attestables = GNUNET_new_array (num_expected,
    225                                                const char *);
    226   num_expected = 0;
    227   va_start (ap, expected_response_code);
    228   while (NULL != (ea = va_arg (ap, const char *)))
    229     ss->expected_attestables[num_expected++] = ea;
    230   va_end (ap);
    231 
    232   {
    233     struct TALER_TESTING_Command cmd = {
    234       .cls = ss,
    235       .label = label,
    236       .run = &get_attestable_run,
    237       .cleanup = &get_attestable_cleanup
    238     };
    239 
    240     return cmd;
    241   }
    242 }