LCOV - code coverage report
Current view: top level - testing - testing_cmd_policy_create.c (source / functions) Hit Total Coverage
Test: GNU Taler anastasis coverage report Lines: 42 51 82.4 %
Date: 2021-06-16 06:33:01 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of Anastasis
       3             :   Copyright (C) 2020 Taler Systems SA
       4             : 
       5             :   Anastasis is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   Anastasis 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 General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file lib/testing_cmd_policy_create.c
      18             :  * @brief command to execute the anastasis secret share service
      19             :  * @author Christian Grothoff
      20             :  * @author Dennis Neufeld
      21             :  * @author Dominik Meister
      22             :  */
      23             : 
      24             : #include "platform.h"
      25             : #include "anastasis_testing_lib.h"
      26             : #include <taler/taler_util.h>
      27             : #include <taler/taler_testing_lib.h>
      28             : 
      29             : 
      30             : /**
      31             :  * State for a "policy create" CMD.
      32             :  */
      33             : struct PolicyCreateState
      34             : {
      35             :   /**
      36             :    * The interpreter state.
      37             :    */
      38             :   struct TALER_TESTING_Interpreter *is;
      39             : 
      40             :   /**
      41             :    * Label of this command.
      42             :    */
      43             :   const char *label;
      44             : 
      45             :   /**
      46             :    * References to upload commands of previous truth uploads.
      47             :    */
      48             :   const char **cmd_label_array;
      49             : 
      50             :   /**
      51             :    * Length of array of command labels (cmd_label_array).
      52             :    */
      53             :   unsigned int cmd_label_array_length;
      54             : 
      55             :   /**
      56             :    * Policy object
      57             :    */
      58             :   struct ANASTASIS_Policy *policy;
      59             : };
      60             : 
      61             : 
      62             : /**
      63             :  * Run a "policy create" CMD.
      64             :  *
      65             :  * @param cls closure.
      66             :  * @param cmd command currently being run.
      67             :  * @param is interpreter state.
      68             :  */
      69             : static void
      70           3 : policy_create_run (void *cls,
      71             :                    const struct TALER_TESTING_Command *cmd,
      72             :                    struct TALER_TESTING_Interpreter *is)
      73           3 : {
      74           3 :   struct PolicyCreateState *pcs = cls;
      75           3 :   const struct ANASTASIS_Truth *truths[pcs->cmd_label_array_length];
      76             : 
      77           3 :   GNUNET_assert (pcs->cmd_label_array_length > 0);
      78           3 :   GNUNET_assert (NULL != pcs->cmd_label_array);
      79           3 :   pcs->is = is;
      80           3 :   if (NULL != pcs->cmd_label_array)
      81             :   {
      82           9 :     for (unsigned int i = 0; i < pcs->cmd_label_array_length; i++)
      83             :     {
      84             :       const struct TALER_TESTING_Command *ref;
      85             :       const struct ANASTASIS_Truth *truth;
      86             : 
      87           6 :       ref = TALER_TESTING_interpreter_lookup_command (is,
      88           6 :                                                       pcs->cmd_label_array[i]);
      89           6 :       if (NULL == ref)
      90             :       {
      91           0 :         GNUNET_break (0);
      92           0 :         TALER_TESTING_interpreter_fail (pcs->is);
      93           0 :         return;
      94             :       }
      95           6 :       if (GNUNET_OK !=
      96           6 :           ANASTASIS_TESTING_get_trait_truth (ref,
      97             :                                              0,
      98             :                                              &truth))
      99             :       {
     100           0 :         GNUNET_break (0);
     101           0 :         TALER_TESTING_interpreter_fail (pcs->is);
     102           0 :         return;
     103             :       }
     104           6 :       GNUNET_assert (NULL != truth);
     105           6 :       truths[i] = truth;
     106             :     }
     107             :   }
     108             : 
     109           3 :   pcs->policy = ANASTASIS_policy_create (truths,
     110             :                                          pcs->cmd_label_array_length);
     111             : 
     112           3 :   if (NULL == pcs->policy)
     113             :   {
     114           0 :     GNUNET_break (0);
     115           0 :     TALER_TESTING_interpreter_fail (pcs->is);
     116           0 :     return;
     117             :   }
     118           3 :   TALER_TESTING_interpreter_next (pcs->is);
     119             : }
     120             : 
     121             : 
     122             : /**
     123             :  * Free the state of a "policy create" CMD, and possibly
     124             :  * cancel it if it did not complete.
     125             :  *
     126             :  * @param cls closure.
     127             :  * @param cmd command being freed.
     128             :  */
     129             : static void
     130           3 : policy_create_cleanup (void *cls,
     131             :                        const struct TALER_TESTING_Command *cmd)
     132             : {
     133           3 :   struct PolicyCreateState *pcs = cls;
     134             : 
     135           3 :   GNUNET_free (pcs->cmd_label_array);
     136           3 :   if (NULL != pcs->policy)
     137             :   {
     138           3 :     ANASTASIS_policy_destroy (pcs->policy);
     139           3 :     pcs->policy = NULL;
     140             :   }
     141           3 :   GNUNET_free (pcs);
     142           3 : }
     143             : 
     144             : 
     145             : /**
     146             :  * Offer internal data to other commands.
     147             :  *
     148             :  * @param cls closure
     149             :  * @param ret[out] result (could be anything)
     150             :  * @param trait name of the trait
     151             :  * @param index index number of the object to extract.
     152             :  * @return #GNUNET_OK on success
     153             :  */
     154             : static int
     155           6 : policy_create_traits (void *cls,
     156             :                       const void **ret,
     157             :                       const char *trait,
     158             :                       unsigned int index)
     159             : {
     160           6 :   struct PolicyCreateState *pcs = cls;
     161             :   struct TALER_TESTING_Trait traits[] = {
     162           6 :     ANASTASIS_TESTING_make_trait_policy (0,
     163           6 :                                          pcs->policy),
     164           6 :     TALER_TESTING_trait_end ()
     165             :   };
     166             : 
     167           6 :   return TALER_TESTING_get_trait (traits,
     168             :                                   ret,
     169             :                                   trait,
     170             :                                   index);
     171             : }
     172             : 
     173             : 
     174             : struct TALER_TESTING_Command
     175           3 : ANASTASIS_TESTING_cmd_policy_create (const char *label,
     176             :                                      ...)
     177             : {
     178             :   struct PolicyCreateState *pcs;
     179             :   va_list ap;
     180             :   const char *truth_upload_cmd;
     181             : 
     182           3 :   pcs = GNUNET_new (struct PolicyCreateState);
     183           3 :   pcs->label = label;
     184             : 
     185           3 :   va_start (ap,
     186             :             label);
     187           9 :   while (NULL != (truth_upload_cmd = va_arg (ap, const char *)))
     188             :   {
     189           6 :     GNUNET_array_append (pcs->cmd_label_array,
     190             :                          pcs->cmd_label_array_length,
     191             :                          truth_upload_cmd);
     192             :   }
     193           3 :   va_end (ap);
     194             :   {
     195           3 :     struct TALER_TESTING_Command cmd = {
     196             :       .cls = pcs,
     197             :       .label = label,
     198             :       .run = &policy_create_run,
     199             :       .cleanup = &policy_create_cleanup,
     200             :       .traits = &policy_create_traits
     201             :     };
     202             : 
     203           3 :     return cmd;
     204             :   }
     205             : }
     206             : 
     207             : 
     208             : /* end of testing_cmd_policy_create.c */

Generated by: LCOV version 1.14