LCOV - code coverage report
Current view: top level - testing - testing_api_helpers_auditor.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 35 42 83.3 %
Date: 2021-08-30 06:43:37 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) 2018 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_helpers_auditor.c
      21             :  * @brief helper functions
      22             :  * @author Christian Grothoff
      23             :  */
      24             : #include "platform.h"
      25             : #include "taler_json_lib.h"
      26             : #include <gnunet/gnunet_curl_lib.h>
      27             : #include "taler_testing_lib.h"
      28             : #include "taler_auditor_service.h"
      29             : 
      30             : 
      31             : /**
      32             :  * Closure for #cleanup_auditor.
      33             :  */
      34             : struct CleanupContext
      35             : {
      36             :   /**
      37             :    * Where we find the state to clean up.
      38             :    */
      39             :   struct TALER_TESTING_Interpreter *is;
      40             : 
      41             :   /**
      42             :    * Next cleanup routine to call, NULL for none.
      43             :    */
      44             :   GNUNET_SCHEDULER_TaskCallback fcb;
      45             : 
      46             :   /**
      47             :    * Closure for @e fcb
      48             :    */
      49             :   void *fcb_cls;
      50             : };
      51             : 
      52             : 
      53             : /**
      54             :  * Function to clean up the auditor connection.
      55             :  *
      56             :  * @param cls a `struct CleanupContext`
      57             :  */
      58             : static void
      59           1 : cleanup_auditor (void *cls)
      60             : {
      61           1 :   struct CleanupContext *cc = cls;
      62           1 :   struct TALER_TESTING_Interpreter *is = cc->is;
      63             : 
      64           1 :   TALER_AUDITOR_disconnect (is->auditor);
      65           1 :   is->auditor = NULL;
      66           1 :   if (NULL != cc->fcb)
      67           0 :     cc->fcb (cc->fcb_cls);
      68           1 :   GNUNET_free (cc);
      69           1 : }
      70             : 
      71             : 
      72             : /**
      73             :  * Closure for #auditor_main_wrapper()
      74             :  */
      75             : struct MainWrapperContext
      76             : {
      77             :   /**
      78             :    * Main function to launch.
      79             :    */
      80             :   TALER_TESTING_Main main_cb;
      81             : 
      82             :   /**
      83             :    * Closure for @e main_cb.
      84             :    */
      85             :   void *main_cb_cls;
      86             : 
      87             :   /**
      88             :    * Configuration we use.
      89             :    */
      90             :   const struct GNUNET_CONFIGURATION_Handle *cfg;
      91             : 
      92             :   /**
      93             :    * Name of the configuration file.
      94             :    */
      95             :   const char *config_filename;
      96             : 
      97             : };
      98             : 
      99             : 
     100             : /**
     101             :  * Function called with information about the auditor.
     102             :  *
     103             :  * @param cls closure
     104             :  * @param hr http response details
     105             :  * @param vi basic information about the auditor
     106             :  * @param compat protocol compatibility information
     107             :  */
     108             : static void
     109           1 : auditor_version_cb (void *cls,
     110             :                     const struct TALER_AUDITOR_HttpResponse *hr,
     111             :                     const struct TALER_AUDITOR_VersionInformation *vi,
     112             :                     enum TALER_AUDITOR_VersionCompatibility compat)
     113             : {
     114           1 :   struct TALER_TESTING_Interpreter *is = cls;
     115             : 
     116             :   (void) hr;
     117           1 :   if (TALER_AUDITOR_VC_MATCH != compat)
     118             :   {
     119           0 :     TALER_TESTING_interpreter_fail (is);
     120           0 :     return;
     121             :   }
     122           1 :   is->auditor_working = GNUNET_YES;
     123             : }
     124             : 
     125             : 
     126             : /**
     127             :  * Setup the @a is 'auditor' member before running the main test loop.
     128             :  *
     129             :  * @param cls must be a `struct MainWrapperContext *`
     130             :  * @param[in,out] is interpreter state to setup
     131             :  */
     132             : static void
     133           1 : auditor_main_wrapper (void *cls,
     134             :                       struct TALER_TESTING_Interpreter *is)
     135             : {
     136           1 :   struct MainWrapperContext *mwc = cls;
     137             :   struct CleanupContext *cc;
     138             :   char *auditor_base_url;
     139             : 
     140           1 :   if (GNUNET_OK !=
     141           1 :       GNUNET_CONFIGURATION_get_value_string (mwc->cfg,
     142             :                                              "auditor",
     143             :                                              "BASE_URL",
     144             :                                              &auditor_base_url))
     145             :   {
     146           0 :     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     147             :                                "auditor",
     148             :                                "BASE_URL");
     149           0 :     return;
     150             :   }
     151             : 
     152           1 :   is->auditor = TALER_AUDITOR_connect (is->ctx,
     153             :                                        auditor_base_url,
     154             :                                        &auditor_version_cb,
     155             :                                        is);
     156           1 :   GNUNET_free (auditor_base_url);
     157             : 
     158           1 :   if (NULL == is->auditor)
     159             :   {
     160           0 :     GNUNET_break (0);
     161           0 :     return;
     162             :   }
     163             : 
     164           1 :   cc = GNUNET_new (struct CleanupContext);
     165           1 :   cc->is = is;
     166           1 :   cc->fcb = is->final_cleanup_cb;
     167           1 :   cc->fcb_cls = is->final_cleanup_cb_cls;
     168           1 :   is->final_cleanup_cb = cleanup_auditor;
     169           1 :   is->final_cleanup_cb_cls = cc;
     170           1 :   mwc->main_cb (mwc->main_cb_cls,
     171             :                 is);
     172             : }
     173             : 
     174             : 
     175             : /**
     176             :  * Install signal handlers plus schedules the main wrapper
     177             :  * around the "run" method.
     178             :  *
     179             :  * @param cls our `struct MainWrapperContext`
     180             :  * @param cfg configuration we use
     181             :  * @return #GNUNET_OK if all is okay, != #GNUNET_OK otherwise.
     182             :  *         non-GNUNET_OK codes are #GNUNET_SYSERR most of the
     183             :  *         times.
     184             :  */
     185             : static int
     186           1 : setup_with_cfg (void *cls,
     187             :                 const struct GNUNET_CONFIGURATION_Handle *cfg)
     188             : {
     189           1 :   struct MainWrapperContext *mwc = cls;
     190           1 :   struct TALER_TESTING_SetupContext setup_ctx = {
     191           1 :     .config_filename = mwc->config_filename,
     192             :     .main_cb = &auditor_main_wrapper,
     193             :     .main_cb_cls = mwc
     194             :   };
     195             : 
     196           1 :   mwc->cfg = cfg;
     197           1 :   return TALER_TESTING_setup_with_auditor_and_exchange_cfg (&setup_ctx,
     198             :                                                             cfg);
     199             : }
     200             : 
     201             : 
     202             : /**
     203             :  * Install signal handlers plus schedules the main wrapper
     204             :  * around the "run" method.
     205             :  *
     206             :  * @param main_cb the "run" method which contains all the
     207             :  *        commands.
     208             :  * @param main_cb_cls a closure for "run", typically NULL.
     209             :  * @param config_filename configuration filename.
     210             :  * @return #GNUNET_OK if all is okay, != #GNUNET_OK otherwise.
     211             :  *         non-GNUNET_OK codes are #GNUNET_SYSERR most of the
     212             :  *         times.
     213             :  */
     214             : int
     215           1 : TALER_TESTING_auditor_setup (TALER_TESTING_Main main_cb,
     216             :                              void *main_cb_cls,
     217             :                              const char *config_filename)
     218             : {
     219           1 :   struct MainWrapperContext mwc = {
     220             :     .main_cb = main_cb,
     221             :     .main_cb_cls = main_cb_cls,
     222             :     .config_filename = config_filename
     223             :   };
     224             : 
     225           1 :   return GNUNET_CONFIGURATION_parse_and_run (config_filename,
     226             :                                              &setup_with_cfg,
     227             :                                              &mwc);
     228             : }
     229             : 
     230             : 
     231             : /* end of testing_auditor_api_helpers.c */

Generated by: LCOV version 1.14