testing_cmd_policy_create.c (5221B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU 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 testing/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 policy_create_run (void *cls, 71 const struct TALER_TESTING_Command *cmd, 72 struct TALER_TESTING_Interpreter *is) 73 { 74 struct PolicyCreateState *pcs = cls; 75 const struct ANASTASIS_Truth *truths[pcs->cmd_label_array_length]; 76 77 GNUNET_assert (pcs->cmd_label_array_length > 0); 78 GNUNET_assert (NULL != pcs->cmd_label_array); 79 pcs->is = is; 80 if (NULL != pcs->cmd_label_array) 81 { 82 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 ref = TALER_TESTING_interpreter_lookup_command (is, 88 pcs->cmd_label_array[i]); 89 if (NULL == ref) 90 { 91 GNUNET_break (0); 92 TALER_TESTING_interpreter_fail (pcs->is); 93 return; 94 } 95 if (GNUNET_OK != 96 ANASTASIS_TESTING_get_trait_truth (ref, 97 &truth)) 98 { 99 GNUNET_break (0); 100 TALER_TESTING_interpreter_fail (pcs->is); 101 return; 102 } 103 GNUNET_assert (NULL != *truth); 104 truths[i] = *truth; 105 } 106 } 107 108 pcs->policy = ANASTASIS_policy_create (truths, 109 pcs->cmd_label_array_length); 110 111 if (NULL == pcs->policy) 112 { 113 GNUNET_break (0); 114 TALER_TESTING_interpreter_fail (pcs->is); 115 return; 116 } 117 TALER_TESTING_interpreter_next (pcs->is); 118 } 119 120 121 /** 122 * Free the state of a "policy create" CMD, and possibly 123 * cancel it if it did not complete. 124 * 125 * @param cls closure. 126 * @param cmd command being freed. 127 */ 128 static void 129 policy_create_cleanup (void *cls, 130 const struct TALER_TESTING_Command *cmd) 131 { 132 struct PolicyCreateState *pcs = cls; 133 134 GNUNET_free (pcs->cmd_label_array); 135 if (NULL != pcs->policy) 136 { 137 ANASTASIS_policy_destroy (pcs->policy); 138 pcs->policy = NULL; 139 } 140 GNUNET_free (pcs); 141 } 142 143 144 /** 145 * Offer internal data to other commands. 146 * 147 * @param cls closure 148 * @param[out] ret result (could be anything) 149 * @param trait name of the trait 150 * @param index index number of the object to extract. 151 * @return #GNUNET_OK on success 152 */ 153 static int 154 policy_create_traits (void *cls, 155 const void **ret, 156 const char *trait, 157 unsigned int index) 158 { 159 struct PolicyCreateState *pcs = cls; 160 struct TALER_TESTING_Trait traits[] = { 161 ANASTASIS_TESTING_make_trait_policy ( 162 (const struct ANASTASIS_Policy **) &pcs->policy), 163 TALER_TESTING_trait_end () 164 }; 165 166 return TALER_TESTING_get_trait (traits, 167 ret, 168 trait, 169 index); 170 } 171 172 173 struct TALER_TESTING_Command 174 ANASTASIS_TESTING_cmd_policy_create (const char *label, 175 ...) 176 { 177 struct PolicyCreateState *pcs; 178 va_list ap; 179 const char *truth_upload_cmd; 180 181 pcs = GNUNET_new (struct PolicyCreateState); 182 pcs->label = label; 183 184 va_start (ap, 185 label); 186 while (NULL != (truth_upload_cmd = va_arg (ap, const char *))) 187 { 188 GNUNET_array_append (pcs->cmd_label_array, 189 pcs->cmd_label_array_length, 190 truth_upload_cmd); 191 } 192 va_end (ap); 193 { 194 struct TALER_TESTING_Command cmd = { 195 .cls = pcs, 196 .label = label, 197 .run = &policy_create_run, 198 .cleanup = &policy_create_cleanup, 199 .traits = &policy_create_traits 200 }; 201 202 return cmd; 203 } 204 } 205 206 207 /* end of testing_cmd_policy_create.c */