LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_serialize_keys.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 53 57 93.0 %
Date: 2021-08-30 06:43:37 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (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_cmd_serialize_keys.c
      21             :  * @brief Lets tests use the keys serialization API.
      22             :  * @author Marcello Stanisci
      23             :  */
      24             : #include "platform.h"
      25             : #include <jansson.h>
      26             : #include "taler_testing_lib.h"
      27             : 
      28             : 
      29             : /**
      30             :  * Internal state for a serialize-keys CMD.
      31             :  */
      32             : struct SerializeKeysState
      33             : {
      34             :   /**
      35             :    * Serialized keys.
      36             :    */
      37             :   json_t *keys;
      38             : 
      39             :   /**
      40             :    * Exchange URL.  Needed because the exchange gets disconnected
      41             :    * from, after keys serialization.  This value is then needed by
      42             :    * subsequent commands that have to reconnect to the exchange.
      43             :    */
      44             :   char *exchange_url;
      45             : };
      46             : 
      47             : 
      48             : /**
      49             :  * Internal state for a connect-with-state CMD.
      50             :  */
      51             : struct ConnectWithStateState
      52             : {
      53             : 
      54             :   /**
      55             :    * Reference to a CMD that offers a serialized key-state
      56             :    * that will be used in the reconnection.
      57             :    */
      58             :   const char *state_reference;
      59             : 
      60             :   /**
      61             :    * If set to GNUNET_YES, then the /keys callback has already
      62             :    * been passed the control to the next CMD.  This is necessary
      63             :    * because it is not uncommon that the /keys callback gets
      64             :    * invoked multiple times, and without this flag, we would keep
      65             :    * going "next" CMD upon every invocation (causing impredictable
      66             :    * behaviour as for the instruction pointer.)
      67             :    */
      68             :   unsigned int consumed;
      69             : 
      70             :   /**
      71             :    * Interpreter state.
      72             :    */
      73             :   struct TALER_TESTING_Interpreter *is;
      74             : };
      75             : 
      76             : 
      77             : /**
      78             :  * Run the command.
      79             :  *
      80             :  * @param cls closure.
      81             :  * @param cmd the command to execute.
      82             :  * @param is the interpreter state.
      83             :  */
      84             : static void
      85           1 : serialize_keys_run (void *cls,
      86             :                     const struct TALER_TESTING_Command *cmd,
      87             :                     struct TALER_TESTING_Interpreter *is)
      88             : {
      89           1 :   struct SerializeKeysState *sks = cls;
      90             : 
      91           1 :   sks->keys = TALER_EXCHANGE_serialize_data (is->exchange);
      92           1 :   if (NULL == sks->keys)
      93           0 :     TALER_TESTING_interpreter_fail (is);
      94             : 
      95           1 :   sks->exchange_url = GNUNET_strdup
      96             :                         (TALER_EXCHANGE_get_base_url (is->exchange));
      97           1 :   TALER_EXCHANGE_disconnect (is->exchange);
      98           1 :   is->exchange = NULL;
      99           1 :   is->working = GNUNET_NO;
     100           1 :   TALER_TESTING_interpreter_next (is);
     101           1 : }
     102             : 
     103             : 
     104             : /**
     105             :  * Cleanup the state of a "serialize keys" CMD.
     106             :  *
     107             :  * @param cls closure.
     108             :  * @param cmd the command which is being cleaned up.
     109             :  */
     110             : static void
     111           1 : serialize_keys_cleanup (void *cls,
     112             :                         const struct TALER_TESTING_Command *cmd)
     113             : {
     114           1 :   struct SerializeKeysState *sks = cls;
     115             : 
     116           1 :   if (NULL != sks->keys)
     117             :   {
     118           1 :     json_decref (sks->keys);
     119             :   }
     120           1 :   GNUNET_free (sks->exchange_url);
     121           1 :   GNUNET_free (sks);
     122           1 : }
     123             : 
     124             : 
     125             : /**
     126             :  * Offer serialized keys as trait.
     127             :  *
     128             :  * @param cls closure.
     129             :  * @param[out] ret result.
     130             :  * @param trait name of the trait.
     131             :  * @param index index number of the object to offer.
     132             :  * @return #GNUNET_OK on success.
     133             :  */
     134             : static int
     135           2 : serialize_keys_traits (void *cls,
     136             :                        const void **ret,
     137             :                        const char *trait,
     138             :                        unsigned int index)
     139             : {
     140           2 :   struct SerializeKeysState *sks = cls;
     141             :   struct TALER_TESTING_Trait traits[] = {
     142           2 :     TALER_TESTING_make_trait_exchange_keys (0, sks->keys),
     143           2 :     TALER_TESTING_make_trait_url (TALER_TESTING_UT_EXCHANGE_BASE_URL,
     144           2 :                                   sks->exchange_url),
     145           2 :     TALER_TESTING_trait_end ()
     146             :   };
     147             : 
     148           2 :   return TALER_TESTING_get_trait (traits,
     149             :                                   ret,
     150             :                                   trait,
     151             :                                   index);
     152             : }
     153             : 
     154             : 
     155             : /**
     156             :  * Run the command.
     157             :  *
     158             :  * @param cls closure.
     159             :  * @param cmd the command to execute.
     160             :  * @param is the interpreter state.
     161             :  */
     162             : static void
     163           2 : connect_with_state_run (void *cls,
     164             :                         const struct TALER_TESTING_Command *cmd,
     165             :                         struct TALER_TESTING_Interpreter *is)
     166             : {
     167           2 :   struct ConnectWithStateState *cwss = cls;
     168             :   const struct TALER_TESTING_Command *state_cmd;
     169             :   const json_t *serialized_keys;
     170             :   const char *exchange_url;
     171             : 
     172             :   /* This command usually gets rescheduled after serialized
     173             :    * reconnection.  */
     174           2 :   if (GNUNET_YES == cwss->consumed)
     175             :   {
     176           1 :     TALER_TESTING_interpreter_next (is);
     177           1 :     return;
     178             :   }
     179             : 
     180           1 :   cwss->is = is;
     181           1 :   state_cmd = TALER_TESTING_interpreter_lookup_command
     182             :                 (is, cwss->state_reference);
     183             : 
     184             :   /* Command providing serialized keys not found.  */
     185           1 :   if (NULL == state_cmd)
     186             :   {
     187           0 :     GNUNET_break (0);
     188           0 :     TALER_TESTING_interpreter_fail (is);
     189           0 :     return;
     190             :   }
     191             : 
     192           1 :   GNUNET_assert (GNUNET_OK ==
     193             :                  TALER_TESTING_get_trait_exchange_keys (state_cmd,
     194             :                                                         0,
     195             :                                                         &serialized_keys));
     196             :   {
     197             :     char *dump;
     198             : 
     199           1 :     dump = json_dumps (serialized_keys,
     200             :                        JSON_INDENT (1));
     201           1 :     TALER_LOG_DEBUG ("Serialized key-state: %s\n",
     202             :                      dump);
     203           1 :     free (dump);
     204             :   }
     205             : 
     206           1 :   GNUNET_assert (GNUNET_OK ==
     207             :                  TALER_TESTING_get_trait_url (state_cmd,
     208             :                                               TALER_TESTING_UT_EXCHANGE_BASE_URL,
     209             :                                               &exchange_url));
     210           1 :   is->exchange = TALER_EXCHANGE_connect (is->ctx,
     211             :                                          exchange_url,
     212             :                                          &TALER_TESTING_cert_cb,
     213             :                                          cwss,
     214             :                                          TALER_EXCHANGE_OPTION_DATA,
     215             :                                          serialized_keys,
     216             :                                          TALER_EXCHANGE_OPTION_END);
     217           1 :   cwss->consumed = GNUNET_YES;
     218             : }
     219             : 
     220             : 
     221             : /**
     222             :  * Cleanup the state of a "connect with state" CMD.  Just
     223             :  * a placeholder to avoid jumping on an invalid address.
     224             :  *
     225             :  * @param cls closure.
     226             :  * @param cmd the command which is being cleaned up.
     227             :  */
     228             : static void
     229           1 : connect_with_state_cleanup (void *cls,
     230             :                             const struct TALER_TESTING_Command *cmd)
     231             : {
     232           1 :   struct ConnectWithStateState *cwss = cls;
     233             : 
     234           1 :   GNUNET_free (cwss);
     235           1 : }
     236             : 
     237             : 
     238             : /**
     239             :  * Make a serialize-keys CMD.  It will ask for
     240             :  * keys serialization __and__ disconnect from the
     241             :  * exchange.
     242             :  *
     243             :  * @param label CMD label
     244             :  * @return the CMD.
     245             :  */
     246             : struct TALER_TESTING_Command
     247           1 : TALER_TESTING_cmd_serialize_keys (const char *label)
     248             : {
     249             :   struct SerializeKeysState *sks;
     250             : 
     251           1 :   sks = GNUNET_new (struct SerializeKeysState);
     252             :   {
     253           1 :     struct TALER_TESTING_Command cmd = {
     254             :       .cls = sks,
     255             :       .label = label,
     256             :       .run = serialize_keys_run,
     257             :       .cleanup = serialize_keys_cleanup,
     258             :       .traits = serialize_keys_traits
     259             :     };
     260             : 
     261           1 :     return cmd;
     262             :   }
     263             : }
     264             : 
     265             : 
     266             : /**
     267             :  * Make a connect-with-state CMD.  This command
     268             :  * will use a serialized key state to reconnect
     269             :  * to the exchange.
     270             :  *
     271             :  * @param label command label
     272             :  * @param state_reference label of a CMD offering
     273             :  *        a serialized key state.
     274             :  * @return the CMD.
     275             :  */
     276             : struct TALER_TESTING_Command
     277           1 : TALER_TESTING_cmd_connect_with_state (const char *label,
     278             :                                       const char *state_reference)
     279             : {
     280             :   struct ConnectWithStateState *cwss;
     281             : 
     282           1 :   cwss = GNUNET_new (struct ConnectWithStateState);
     283           1 :   cwss->state_reference = state_reference;
     284           1 :   cwss->consumed = GNUNET_NO;
     285             :   {
     286           1 :     struct TALER_TESTING_Command cmd = {
     287             :       .cls = cwss,
     288             :       .label = label,
     289             :       .run = connect_with_state_run,
     290             :       .cleanup = connect_with_state_cleanup
     291             :     };
     292             : 
     293           1 :     return cmd;
     294             :   }
     295             : }

Generated by: LCOV version 1.14