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_set_officer.c (8296B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it
      6   under the terms of the GNU General Public License as published by
      7   the Free Software Foundation; either version 3, or (at your
      8   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 GNU
     13   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_set_officer.c
     21  * @brief command for testing /management/aml-officers
     22  * @author Christian Grothoff
     23  */
     24 
     25 struct SetOfficerState;
     26 #define TALER_EXCHANGE_POST_MANAGEMENT_AML_OFFICERS_RESULT_CLOSURE \
     27         struct SetOfficerState
     28 #include "taler/exchange/post-management-aml-officers.h"
     29 
     30 #include "taler/taler_json_lib.h"
     31 #include <gnunet/gnunet_curl_lib.h>
     32 #include "taler/taler_testing_lib.h"
     33 
     34 
     35 /**
     36  * State for a "set_officer" CMD.
     37  */
     38 struct SetOfficerState
     39 {
     40 
     41   /**
     42    * Update AML officer handle while operation is running.
     43    */
     44   struct TALER_EXCHANGE_PostManagementAmlOfficersHandle *dh;
     45 
     46   /**
     47    * Our interpreter.
     48    */
     49   struct TALER_TESTING_Interpreter *is;
     50 
     51   /**
     52    * Reference to command to previous set officer
     53    * to update, or NULL.
     54    */
     55   const char *ref_cmd;
     56 
     57   /**
     58    * Name to use for the officer.
     59    */
     60   const char *name;
     61 
     62   /**
     63    * Private key of the AML officer.
     64    */
     65   struct TALER_AmlOfficerPrivateKeyP officer_priv;
     66 
     67   /**
     68    * Public key of the AML officer.
     69    */
     70   struct TALER_AmlOfficerPublicKeyP officer_pub;
     71 
     72   /**
     73    * Is the officer supposed to be enabled?
     74    */
     75   bool is_active;
     76 
     77   /**
     78    * Is access supposed to be read-only?
     79    */
     80   bool read_only;
     81 
     82 };
     83 
     84 
     85 /**
     86  * Callback to analyze the /management/XXX response, just used to check
     87  * if the response code is acceptable.
     88  *
     89  * @param ds our context
     90  * @param ar response details
     91  */
     92 static void
     93 set_officer_cb (
     94   struct SetOfficerState *ds,
     95   const struct TALER_EXCHANGE_PostManagementAmlOfficersResponse *ar)
     96 {
     97   const struct TALER_EXCHANGE_HttpResponse *hr = &ar->hr;
     98 
     99   ds->dh = NULL;
    100   if (MHD_HTTP_NO_CONTENT != hr->http_status)
    101   {
    102     TALER_TESTING_unexpected_status (ds->is,
    103                                      hr->http_status,
    104                                      MHD_HTTP_NO_CONTENT);
    105     return;
    106   }
    107   TALER_TESTING_interpreter_next (ds->is);
    108 }
    109 
    110 
    111 /**
    112  * Run the command.
    113  *
    114  * @param cls closure.
    115  * @param cmd the command to execute.
    116  * @param is the interpreter state.
    117  */
    118 static void
    119 set_officer_run (void *cls,
    120                  const struct TALER_TESTING_Command *cmd,
    121                  struct TALER_TESTING_Interpreter *is)
    122 {
    123   struct SetOfficerState *ds = cls;
    124   struct GNUNET_TIME_Timestamp now;
    125   struct TALER_MasterSignatureP master_sig;
    126   const char *exchange_url;
    127 
    128   (void) cmd;
    129   {
    130     const struct TALER_TESTING_Command *exchange_cmd;
    131 
    132     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
    133                                                           "exchange");
    134     if (NULL == exchange_cmd)
    135     {
    136       GNUNET_break (0);
    137       TALER_TESTING_interpreter_fail (is);
    138       return;
    139     }
    140     GNUNET_assert (GNUNET_OK ==
    141                    TALER_TESTING_get_trait_exchange_url (exchange_cmd,
    142                                                          &exchange_url));
    143   }
    144   now = GNUNET_TIME_timestamp_get ();
    145   ds->is = is;
    146   if (NULL == ds->ref_cmd)
    147   {
    148     GNUNET_CRYPTO_eddsa_key_create (&ds->officer_priv.eddsa_priv);
    149     GNUNET_CRYPTO_eddsa_key_get_public (&ds->officer_priv.eddsa_priv,
    150                                         &ds->officer_pub.eddsa_pub);
    151   }
    152   else
    153   {
    154     const struct TALER_TESTING_Command *ref;
    155     const struct TALER_AmlOfficerPrivateKeyP *officer_priv;
    156     const struct TALER_AmlOfficerPublicKeyP *officer_pub;
    157 
    158     ref = TALER_TESTING_interpreter_lookup_command (is,
    159                                                     ds->ref_cmd);
    160     if (NULL == ref)
    161     {
    162       GNUNET_break (0);
    163       TALER_TESTING_interpreter_fail (is);
    164       return;
    165     }
    166     GNUNET_assert (GNUNET_OK ==
    167                    TALER_TESTING_get_trait_officer_pub (ref,
    168                                                         &officer_pub));
    169     GNUNET_assert (GNUNET_OK ==
    170                    TALER_TESTING_get_trait_officer_priv (ref,
    171                                                          &officer_priv));
    172     ds->officer_pub = *officer_pub;
    173     ds->officer_priv = *officer_priv;
    174   }
    175   {
    176     const struct TALER_TESTING_Command *exchange_cmd;
    177     const struct TALER_MasterPrivateKeyP *master_priv;
    178 
    179     exchange_cmd = TALER_TESTING_interpreter_get_command (is,
    180                                                           "exchange");
    181     if (NULL == exchange_cmd)
    182     {
    183       GNUNET_break (0);
    184       TALER_TESTING_interpreter_fail (is);
    185       return;
    186     }
    187     GNUNET_assert (GNUNET_OK ==
    188                    TALER_TESTING_get_trait_master_priv (exchange_cmd,
    189                                                         &master_priv));
    190 
    191     TALER_exchange_offline_aml_officer_status_sign (&ds->officer_pub,
    192                                                     ds->name,
    193                                                     now,
    194                                                     ds->is_active,
    195                                                     ds->read_only,
    196                                                     master_priv,
    197                                                     &master_sig);
    198   }
    199   ds->dh = TALER_EXCHANGE_post_management_aml_officers_create (
    200     TALER_TESTING_interpreter_get_context (is),
    201     exchange_url,
    202     &ds->officer_pub,
    203     ds->name,
    204     now,
    205     ds->is_active,
    206     ds->read_only,
    207     &master_sig);
    208   if (NULL == ds->dh)
    209   {
    210     GNUNET_break (0);
    211     TALER_TESTING_interpreter_fail (is);
    212     return;
    213   }
    214   GNUNET_assert (TALER_EC_NONE ==
    215                  TALER_EXCHANGE_post_management_aml_officers_start (
    216                    ds->dh,
    217                    &set_officer_cb,
    218                    ds));
    219 }
    220 
    221 
    222 /**
    223  * Free the state of a "set_officer" CMD, and possibly cancel a
    224  * pending operation thereof.
    225  *
    226  * @param cls closure, must be a `struct SetOfficerState`.
    227  * @param cmd the command which is being cleaned up.
    228  */
    229 static void
    230 set_officer_cleanup (void *cls,
    231                      const struct TALER_TESTING_Command *cmd)
    232 {
    233   struct SetOfficerState *ds = cls;
    234 
    235   if (NULL != ds->dh)
    236   {
    237     TALER_TESTING_command_incomplete (ds->is,
    238                                       cmd->label);
    239     TALER_EXCHANGE_post_management_aml_officers_cancel (ds->dh);
    240     ds->dh = NULL;
    241   }
    242   GNUNET_free (ds);
    243 }
    244 
    245 
    246 /**
    247  * Offer internal data of a "set officer" CMD state to other
    248  * commands.
    249  *
    250  * @param cls closure
    251  * @param[out] ret result (could be anything)
    252  * @param trait name of the trait
    253  * @param index index number of the object to offer.
    254  * @return #GNUNET_OK on success
    255  */
    256 static enum GNUNET_GenericReturnValue
    257 set_officer_traits (void *cls,
    258                     const void **ret,
    259                     const char *trait,
    260                     unsigned int index)
    261 {
    262   struct SetOfficerState *ws = cls;
    263   struct TALER_TESTING_Trait traits[] = {
    264     TALER_TESTING_make_trait_officer_pub (&ws->officer_pub),
    265     TALER_TESTING_make_trait_officer_priv (&ws->officer_priv),
    266     TALER_TESTING_make_trait_officer_name (ws->name),
    267     TALER_TESTING_trait_end ()
    268   };
    269 
    270   return TALER_TESTING_get_trait (traits,
    271                                   ret,
    272                                   trait,
    273                                   index);
    274 }
    275 
    276 
    277 struct TALER_TESTING_Command
    278 TALER_TESTING_cmd_set_officer (
    279   const char *label,
    280   const char *ref_cmd,
    281   const char *name,
    282   bool is_active,
    283   bool read_only)
    284 {
    285   struct SetOfficerState *ds;
    286 
    287   ds = GNUNET_new (struct SetOfficerState);
    288   ds->ref_cmd = ref_cmd;
    289   ds->name = name;
    290   ds->is_active = is_active;
    291   ds->read_only = read_only;
    292   {
    293     struct TALER_TESTING_Command cmd = {
    294       .cls = ds,
    295       .label = label,
    296       .run = &set_officer_run,
    297       .cleanup = &set_officer_cleanup,
    298       .traits = &set_officer_traits
    299     };
    300 
    301     return cmd;
    302   }
    303 }
    304 
    305 
    306 /* end of testing_api_cmd_set_officer.c */