LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_check_keys.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 47 58 81.0 %
Date: 2021-08-30 06:43:37 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2018, 2020 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_check_keys.c
      21             :  * @brief Implementation of "check keys" test command.
      22             :  * @author Marcello Stanisci
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler_json_lib.h"
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_testing_lib.h"
      29             : 
      30             : 
      31             : /**
      32             :  * State for a "check keys" CMD.
      33             :  */
      34             : struct CheckKeysState
      35             : {
      36             :   /**
      37             :    * This number will instruct the CMD interpreter to
      38             :    * make sure that /keys was downloaded `generation` times
      39             :    * _before_ running the very CMD logic.
      40             :    */
      41             :   unsigned int generation;
      42             : 
      43             :   /**
      44             :    * If this value is true, then the "cherry
      45             :    * picking" facility is turned off; whole /keys is
      46             :    * downloaded.
      47             :    */
      48             :   bool pull_all_keys;
      49             : 
      50             :   /**
      51             :    * Label of a command to use to derive the "last_denom_issue" date to use.
      52             :    */
      53             :   const char *last_denom_date_ref;
      54             : 
      55             :   /**
      56             :    * Last denomination date we received when doing this request.
      57             :    */
      58             :   struct GNUNET_TIME_Absolute my_denom_date;
      59             : };
      60             : 
      61             : 
      62             : /**
      63             :  * Run the "check keys" command.
      64             :  *
      65             :  * @param cls closure.
      66             :  * @param cmd the command currently being executed.
      67             :  * @param is the interpreter state.
      68             :  */
      69             : static void
      70          25 : check_keys_run (void *cls,
      71             :                 const struct TALER_TESTING_Command *cmd,
      72             :                 struct TALER_TESTING_Interpreter *is)
      73             : {
      74          25 :   struct CheckKeysState *cks = cls;
      75             : 
      76          25 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      77             :               "cmd `%s' (ip: %u), key generation: %d\n",
      78             :               cmd->label,
      79             :               is->ip,
      80             :               is->key_generation);
      81          25 :   if (is->key_generation < cks->generation)
      82             :   {
      83             :     struct GNUNET_TIME_Absolute rdate;
      84             : 
      85          11 :     is->working = GNUNET_NO;
      86          11 :     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      87             :                 "Triggering GET /keys, cmd `%s'\n",
      88             :                 cmd->label);
      89          11 :     if (NULL != cks->last_denom_date_ref)
      90             :     {
      91           1 :       if (0 == strcmp ("zero",
      92             :                        cks->last_denom_date_ref))
      93             :       {
      94           0 :         TALER_LOG_DEBUG ("Forcing last_denom_date URL argument set to zero\n");
      95           0 :         TALER_EXCHANGE_set_last_denom (is->exchange,
      96             :                                        GNUNET_TIME_UNIT_ZERO_ABS);
      97             :       }
      98             :       else
      99             :       {
     100             :         const struct GNUNET_TIME_Absolute *last_denom_date;
     101             :         const struct TALER_TESTING_Command *ref;
     102             : 
     103           1 :         ref = TALER_TESTING_interpreter_lookup_command (is,
     104             :                                                         cks->last_denom_date_ref);
     105           1 :         if (NULL == ref)
     106             :         {
     107           0 :           GNUNET_break (0);
     108           0 :           TALER_TESTING_interpreter_fail (is);
     109           0 :           return;
     110             :         }
     111           1 :         if (GNUNET_OK !=
     112           1 :             TALER_TESTING_get_trait_absolute_time (ref,
     113             :                                                    0,
     114             :                                                    &last_denom_date))
     115             :         {
     116           0 :           GNUNET_break (0);
     117           0 :           TALER_TESTING_interpreter_fail (is);
     118           0 :           return;
     119             :         }
     120             : 
     121           1 :         TALER_LOG_DEBUG ("Forcing last_denom_date URL argument\n");
     122           1 :         TALER_EXCHANGE_set_last_denom (is->exchange,
     123             :                                        *last_denom_date);
     124             :       }
     125             :     }
     126             : 
     127          11 :     rdate = TALER_EXCHANGE_check_keys_current (
     128             :       is->exchange,
     129          11 :       cks->pull_all_keys
     130             :       ? TALER_EXCHANGE_CKF_FORCE_ALL_NOW
     131             :       : TALER_EXCHANGE_CKF_FORCE_DOWNLOAD);
     132             :     /* Redownload /keys.  */
     133          11 :     GNUNET_break (0 ==
     134             :                   rdate.abs_value_us);
     135          11 :     return;
     136             :   }
     137             :   {
     138             :     const struct TALER_EXCHANGE_Keys *keys;
     139             : 
     140          14 :     keys = TALER_EXCHANGE_get_keys (is->exchange);
     141          14 :     if (NULL == keys)
     142             :     {
     143           0 :       GNUNET_break (0);
     144           0 :       TALER_TESTING_interpreter_fail (is);
     145           0 :       return;
     146             :     }
     147          14 :     cks->my_denom_date = keys->last_denom_issue_date;
     148             :   }
     149          14 :   TALER_TESTING_interpreter_next (is);
     150             : }
     151             : 
     152             : 
     153             : /**
     154             :  * Cleanup the state.
     155             :  *
     156             :  * @param cls closure.
     157             :  * @param cmd the command which is being cleaned up.
     158             :  */
     159             : static void
     160          14 : check_keys_cleanup (void *cls,
     161             :                     const struct TALER_TESTING_Command *cmd)
     162             : {
     163          14 :   struct CheckKeysState *cks = cls;
     164             : 
     165             :   (void) cmd;
     166          14 :   GNUNET_free (cks);
     167          14 : }
     168             : 
     169             : 
     170             : /**
     171             :  * Offer internal data to a "check_keys" CMD state to other
     172             :  * commands.
     173             :  *
     174             :  * @param cls closure
     175             :  * @param[out] ret result (could be anything)
     176             :  * @param trait name of the trait
     177             :  * @param index index number of the object to offer.
     178             :  * @return #GNUNET_OK on success
     179             :  */
     180             : static int
     181           9 : check_keys_traits (void *cls,
     182             :                    const void **ret,
     183             :                    const char *trait,
     184             :                    unsigned int index)
     185             : {
     186           9 :   struct CheckKeysState *cks = cls;
     187             :   struct TALER_TESTING_Trait traits[] = {
     188             :     /* history entry MUST be first due to response code logic below! */
     189           9 :     TALER_TESTING_make_trait_absolute_time (0,
     190           9 :                                             &cks->my_denom_date),
     191           9 :     TALER_TESTING_trait_end ()
     192             :   };
     193             : 
     194           9 :   return TALER_TESTING_get_trait (traits,
     195             :                                   ret,
     196             :                                   trait,
     197             :                                   index);
     198             : }
     199             : 
     200             : 
     201             : struct TALER_TESTING_Command
     202          14 : TALER_TESTING_cmd_check_keys (const char *label,
     203             :                               unsigned int generation)
     204             : {
     205             :   struct CheckKeysState *cks;
     206             : 
     207          14 :   cks = GNUNET_new (struct CheckKeysState);
     208          14 :   cks->generation = generation;
     209             :   {
     210          14 :     struct TALER_TESTING_Command cmd = {
     211             :       .cls = cks,
     212             :       .label = label,
     213             :       .run = &check_keys_run,
     214             :       .cleanup = &check_keys_cleanup,
     215             :       .traits = &check_keys_traits
     216             :     };
     217             : 
     218          14 :     return cmd;
     219             :   }
     220             : }
     221             : 
     222             : 
     223             : struct TALER_TESTING_Command
     224           9 : TALER_TESTING_cmd_check_keys_pull_all_keys (const char *label,
     225             :                                             unsigned int generation)
     226             : {
     227             :   struct TALER_TESTING_Command cmd
     228           9 :     = TALER_TESTING_cmd_check_keys (label,
     229             :                                     generation);
     230           9 :   struct CheckKeysState *cks = cmd.cls;
     231             : 
     232           9 :   cks->pull_all_keys = true;
     233           9 :   return cmd;
     234             : }
     235             : 
     236             : 
     237             : struct TALER_TESTING_Command
     238           2 : TALER_TESTING_cmd_check_keys_with_last_denom (
     239             :   const char *label,
     240             :   unsigned int generation,
     241             :   const char *last_denom_date_ref)
     242             : {
     243             :   struct TALER_TESTING_Command cmd
     244           2 :     = TALER_TESTING_cmd_check_keys (label,
     245             :                                     generation);
     246           2 :   struct CheckKeysState *cks = cmd.cls;
     247           2 :   cks->last_denom_date_ref = last_denom_date_ref;
     248           2 :   return cmd;
     249             : }
     250             : 
     251             : 
     252             : /* end of testing_api_cmd_check_keys.c */

Generated by: LCOV version 1.14