LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_config.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 29 41 70.7 %
Date: 2021-06-16 06:33:01 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14