LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_rewind.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 0 64 0.0 %
Date: 2021-08-30 06:43:37 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-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_rewind.c
      21             :  * @brief command to rewind the instruction pointer.
      22             :  * @author Marcello Stanisci
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler_exchange_service.h"
      27             : #include "taler_testing_lib.h"
      28             : 
      29             : 
      30             : /**
      31             :  * State for a "rewind" CMD.
      32             :  */
      33             : struct RewindIpState
      34             : {
      35             :   /**
      36             :    * Instruction pointer to set into the interpreter.
      37             :    */
      38             :   const char *target_label;
      39             : 
      40             :   /**
      41             :    * How many times this set should take place.  However, this value lives at
      42             :    * the calling process, and this CMD is only in charge of checking and
      43             :    * decremeting it.
      44             :    */
      45             :   unsigned int counter;
      46             : };
      47             : 
      48             : 
      49             : /**
      50             :  * Only defined to respect the API.
      51             :  */
      52             : static void
      53           0 : rewind_ip_cleanup (void *cls,
      54             :                    const struct TALER_TESTING_Command *cmd)
      55             : {
      56             :   (void) cls;
      57             :   (void) cmd;
      58           0 : }
      59             : 
      60             : 
      61             : /**
      62             :  * Seek for the @a target command in @a batch (and rewind to it
      63             :  * if successful).
      64             :  *
      65             :  * @param is the interpreter state (for failures)
      66             :  * @param cmd batch to search for @a target
      67             :  * @param target command to search for
      68             :  * @return #GNUNET_OK on success, #GNUNET_NO if target was not found,
      69             :  *         #GNUNET_SYSERR if target is in the future and we failed
      70             :  */
      71             : static int
      72           0 : seek_batch (struct TALER_TESTING_Interpreter *is,
      73             :             const struct TALER_TESTING_Command *cmd,
      74             :             const struct TALER_TESTING_Command *target)
      75             : {
      76             :   unsigned int new_ip;
      77             : #define BATCH_INDEX 1
      78             :   struct TALER_TESTING_Command *batch;
      79             :   struct TALER_TESTING_Command *current;
      80             :   struct TALER_TESTING_Command *icmd;
      81             :   const struct TALER_TESTING_Command *match;
      82             : 
      83           0 :   current = TALER_TESTING_cmd_batch_get_current (cmd);
      84           0 :   GNUNET_assert (GNUNET_OK ==
      85             :                  TALER_TESTING_get_trait_cmd (cmd,
      86             :                                               BATCH_INDEX,
      87             :                                               &batch));
      88           0 :   match = NULL;
      89           0 :   for (new_ip = 0;
      90           0 :        NULL != (icmd = &batch[new_ip]);
      91           0 :        new_ip++)
      92             :   {
      93           0 :     if (current == target)
      94           0 :       current = NULL;
      95           0 :     if (icmd == target)
      96             :     {
      97           0 :       match = icmd;
      98           0 :       break;
      99             :     }
     100           0 :     if (TALER_TESTING_cmd_is_batch (icmd))
     101             :     {
     102           0 :       int ret = seek_batch (is,
     103             :                             icmd,
     104             :                             target);
     105           0 :       if (GNUNET_SYSERR == ret)
     106           0 :         return GNUNET_SYSERR; /* failure! */
     107           0 :       if (GNUNET_OK == ret)
     108             :       {
     109           0 :         match = icmd;
     110           0 :         break;
     111             :       }
     112             :     }
     113             :   }
     114           0 :   if (NULL == current)
     115             :   {
     116             :     /* refuse to jump forward */
     117           0 :     GNUNET_break (0);
     118           0 :     TALER_TESTING_interpreter_fail (is);
     119           0 :     return GNUNET_SYSERR;
     120             :   }
     121           0 :   if (NULL == match)
     122           0 :     return GNUNET_NO; /* not found */
     123           0 :   TALER_TESTING_cmd_batch_set_current (cmd,
     124             :                                        new_ip);
     125           0 :   return GNUNET_OK;
     126             : }
     127             : 
     128             : 
     129             : /**
     130             :  * Run the "rewind" CMD.
     131             :  *
     132             :  * @param cls closure.
     133             :  * @param cmd command being executed now.
     134             :  * @param is the interpreter state.
     135             :  */
     136             : static void
     137           0 : rewind_ip_run (void *cls,
     138             :                const struct TALER_TESTING_Command *cmd,
     139             :                struct TALER_TESTING_Interpreter *is)
     140             : {
     141           0 :   struct RewindIpState *ris = cls;
     142             :   const struct TALER_TESTING_Command *target;
     143             :   unsigned int new_ip;
     144             : 
     145             :   (void) cmd;
     146           0 :   if (0 == ris->counter)
     147             :   {
     148           0 :     TALER_TESTING_interpreter_next (is);
     149           0 :     return;
     150             :   }
     151             :   target
     152           0 :     = TALER_TESTING_interpreter_lookup_command (is,
     153             :                                                 ris->target_label);
     154           0 :   if (NULL == target)
     155             :   {
     156           0 :     GNUNET_break (0);
     157           0 :     TALER_TESTING_interpreter_fail (is);
     158           0 :     return;
     159             :   }
     160           0 :   ris->counter--;
     161           0 :   for (new_ip = 0;
     162           0 :        NULL != is->commands[new_ip].label;
     163           0 :        new_ip++)
     164             :   {
     165           0 :     const struct TALER_TESTING_Command *cmd = &is->commands[new_ip];
     166             : 
     167           0 :     if (cmd == target)
     168           0 :       break;
     169           0 :     if (TALER_TESTING_cmd_is_batch (cmd))
     170             :     {
     171           0 :       int ret = seek_batch (is,
     172             :                             cmd,
     173             :                             target);
     174           0 :       if (GNUNET_SYSERR == ret)
     175           0 :         return;   /* failure! */
     176           0 :       if (GNUNET_OK == ret)
     177           0 :         break;
     178             :     }
     179             :   }
     180           0 :   if (new_ip > is->ip)
     181             :   {
     182             :     /* refuse to jump forward */
     183           0 :     GNUNET_break (0);
     184           0 :     TALER_TESTING_interpreter_fail (is);
     185           0 :     return;
     186             :   }
     187           0 :   is->ip = new_ip - 1; /* -1 because the next function will advance by one */
     188           0 :   TALER_TESTING_interpreter_next (is);
     189             : }
     190             : 
     191             : 
     192             : /**
     193             :  * Make the instruction pointer point to @a new_ip
     194             :  * only if @a counter is greater than zero.
     195             :  *
     196             :  * @param label command label
     197             :  * @param target_label label of the new instruction pointer's destination after the jump;
     198             :  *                     must be before the current instruction
     199             :  * @param counter counts how many times the rewinding is to happen.
     200             :  */
     201             : struct TALER_TESTING_Command
     202           0 : TALER_TESTING_cmd_rewind_ip (const char *label,
     203             :                              const char *target_label,
     204             :                              unsigned int counter)
     205             : {
     206             :   struct RewindIpState *ris;
     207             : 
     208           0 :   ris = GNUNET_new (struct RewindIpState);
     209           0 :   ris->target_label = target_label;
     210           0 :   ris->counter = counter;
     211             :   {
     212           0 :     struct TALER_TESTING_Command cmd = {
     213             :       .cls = ris,
     214             :       .label = label,
     215             :       .run = &rewind_ip_run,
     216             :       .cleanup = &rewind_ip_cleanup
     217             :     };
     218             : 
     219           0 :     return cmd;
     220             :   }
     221             : }

Generated by: LCOV version 1.14