anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_config.c (4522B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2019, 2021 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   Anastasis; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file anastasis/src/testing/testing_api_cmd_config.c
     18  * @brief command to obtain the configuration of an anastasis backend service.
     19  * @author Dennis Neufeld
     20  * @author Dominik Meister
     21  */
     22 #include "platform.h"
     23 #include "anastasis_testing_lib.h"
     24 #include <taler/taler_util.h>
     25 #include <taler/taler_testing_lib.h>
     26 
     27 
     28 /**
     29  * State for a "config" CMD.
     30  */
     31 struct ConfigState
     32 {
     33   /**
     34    * The interpreter state.
     35    */
     36   struct TALER_TESTING_Interpreter *is;
     37 
     38   /**
     39    * URL of the anastasis backend.
     40    */
     41   const char *anastasis_url;
     42 
     43   /**
     44    * Expected status code.
     45    */
     46   unsigned int http_status;
     47 
     48   /**
     49    * The /config GET operation handle.
     50    */
     51   struct ANASTASIS_ConfigOperation *so;
     52 
     53   /**
     54    * The salt value from server.
     55    */
     56   struct ANASTASIS_CRYPTO_ProviderSaltP provider_salt;
     57 };
     58 
     59 
     60 /**
     61  * Function called with the results of a #ANASTASIS_get_config().
     62  *
     63  * @param cls closure
     64  * @param config config from the server
     65  */
     66 static void
     67 config_cb (void *cls,
     68            const struct ANASTASIS_Config *config)
     69 {
     70   struct ConfigState *ss = cls;
     71 
     72   ss->so = NULL;
     73   if (config->http_status != ss->http_status)
     74   {
     75     TALER_TESTING_unexpected_status (ss->is,
     76                                      config->http_status,
     77                                      ss->http_status);
     78     return;
     79   }
     80   if (GNUNET_OK == config->http_status)
     81   {
     82     ss->provider_salt = config->details.ok.provider_salt;
     83   }
     84   TALER_TESTING_interpreter_next (ss->is);
     85 }
     86 
     87 
     88 /**
     89  * Run a "config" CMD.
     90  *
     91  * @param cls closure.
     92  * @param cmd command currently being run.
     93  * @param is interpreter state.
     94  */
     95 static void
     96 config_run (void *cls,
     97             const struct TALER_TESTING_Command *cmd,
     98             struct TALER_TESTING_Interpreter *is)
     99 {
    100   struct ConfigState *ss = cls;
    101 
    102   ss->is = is;
    103   ss->so = ANASTASIS_get_config (
    104     TALER_TESTING_interpreter_get_context (is),
    105     ss->anastasis_url,
    106     &config_cb,
    107     ss);
    108   if (NULL == ss->so)
    109   {
    110     GNUNET_break (0);
    111     TALER_TESTING_interpreter_fail (ss->is);
    112     return;
    113   }
    114 }
    115 
    116 
    117 /**
    118  * Free the state of a "config" CMD, and possibly
    119  * cancel it if it did not complete.
    120  *
    121  * @param cls closure.
    122  * @param cmd command being freed.
    123  */
    124 static void
    125 config_cleanup (void *cls,
    126                 const struct TALER_TESTING_Command *cmd)
    127 {
    128   struct ConfigState *ss = cls;
    129 
    130   if (NULL != ss->so)
    131   {
    132     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    133                 "Command '%s' did not complete (config)\n",
    134                 cmd->label);
    135     ANASTASIS_config_cancel (ss->so);
    136     ss->so = NULL;
    137   }
    138   GNUNET_free (ss);
    139 }
    140 
    141 
    142 /**
    143  * Offer internal data to other commands.
    144  *
    145  * @param cls closure
    146  * @param[out] ret result (could be anything)
    147  * @param trait name of the trait
    148  * @param index index number of the object to extract.
    149  * @return #GNUNET_OK on success
    150  */
    151 static int
    152 config_traits (void *cls,
    153                const void **ret,
    154                const char *trait,
    155                unsigned int index)
    156 {
    157   struct ConfigState *ss = cls;
    158   struct TALER_TESTING_Trait traits[] = {
    159     ANASTASIS_TESTING_make_trait_provider_salt (&ss->provider_salt),
    160     TALER_TESTING_trait_end ()
    161   };
    162 
    163   return TALER_TESTING_get_trait (traits,
    164                                   ret,
    165                                   trait,
    166                                   index);
    167 }
    168 
    169 
    170 struct TALER_TESTING_Command
    171 ANASTASIS_TESTING_cmd_config (const char *label,
    172                               const char *anastasis_url,
    173                               unsigned int http_status)
    174 {
    175   struct ConfigState *ss;
    176 
    177   ss = GNUNET_new (struct ConfigState);
    178   ss->http_status = http_status;
    179   ss->anastasis_url = anastasis_url;
    180   {
    181     struct TALER_TESTING_Command cmd = {
    182       .cls = ss,
    183       .label = label,
    184       .run = &config_run,
    185       .cleanup = &config_cleanup,
    186       .traits = &config_traits
    187     };
    188 
    189     return cmd;
    190   }
    191 }