testing_api_cmd_policy_lookup.c (6137B)
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_api_cmd_policy_lookup.c 18 * @brief command to execute the anastasis backend service. 19 * @author Dennis Neufeld 20 * @author Dominik Meister 21 */ 22 23 #include "platform.h" 24 #include "anastasis_testing_lib.h" 25 #include <taler/taler_util.h> 26 #include <taler/taler_testing_lib.h> 27 28 29 /** 30 * State for a "policy lookup" CMD. 31 */ 32 struct PolicyLookupState 33 { 34 /** 35 * The interpreter state. 36 */ 37 struct TALER_TESTING_Interpreter *is; 38 39 /** 40 * Eddsa Publickey. 41 */ 42 struct ANASTASIS_CRYPTO_AccountPublicKeyP anastasis_pub; 43 44 /** 45 * Hash of the upload (all zeros if there was no upload). 46 */ 47 const struct GNUNET_HashCode *upload_hash; 48 49 /** 50 * URL of the anastasis backend. 51 */ 52 const char *anastasis_url; 53 54 /** 55 * Expected status code. 56 */ 57 unsigned int http_status; 58 59 /** 60 * Reference to upload command we expect to lookup. 61 */ 62 const char *upload_reference; 63 64 /** 65 * The /policy GET operation handle. 66 */ 67 struct ANASTASIS_PolicyLookupOperation *plo; 68 }; 69 70 71 /** 72 * Function called with the results of a #ANASTASIS_policy_lookup(). 73 * 74 * @param cls closure 75 * @param dd details about the lookup operation 76 */ 77 static void 78 policy_lookup_cb (void *cls, 79 const struct ANASTASIS_DownloadDetails *dd) 80 { 81 struct PolicyLookupState *pls = cls; 82 83 pls->plo = NULL; 84 if (dd->http_status != pls->http_status) 85 { 86 TALER_TESTING_unexpected_status (pls->is, 87 dd->http_status, 88 pls->http_status); 89 return; 90 } 91 if (NULL != pls->upload_reference) 92 { 93 if ( (MHD_HTTP_OK == dd->http_status) && 94 (0 != GNUNET_memcmp (&dd->details.ok.curr_policy_hash, 95 pls->upload_hash)) ) 96 { 97 GNUNET_break (0); 98 TALER_TESTING_interpreter_fail (pls->is); 99 return; 100 } 101 } 102 TALER_TESTING_interpreter_next (pls->is); 103 } 104 105 106 /** 107 * Run a "policy lookup" CMD. 108 * 109 * @param cls closure. 110 * @param cmd command currently being run. 111 * @param is interpreter state. 112 */ 113 static void 114 policy_lookup_run (void *cls, 115 const struct TALER_TESTING_Command *cmd, 116 struct TALER_TESTING_Interpreter *is) 117 { 118 struct PolicyLookupState *pls = cls; 119 120 pls->is = is; 121 if (NULL != pls->upload_reference) 122 { 123 const struct TALER_TESTING_Command *upload_cmd; 124 const struct ANASTASIS_CRYPTO_AccountPublicKeyP *anastasis_pub; 125 126 upload_cmd = TALER_TESTING_interpreter_lookup_command 127 (is, 128 pls->upload_reference); 129 if (NULL == upload_cmd) 130 { 131 GNUNET_break (0); 132 TALER_TESTING_interpreter_fail (pls->is); 133 return; 134 } 135 if (GNUNET_OK != 136 ANASTASIS_TESTING_get_trait_hash (upload_cmd, 137 &pls->upload_hash)) 138 { 139 GNUNET_break (0); 140 TALER_TESTING_interpreter_fail (pls->is); 141 return; 142 } 143 if (GNUNET_OK != 144 ANASTASIS_TESTING_get_trait_account_pub (upload_cmd, 145 &anastasis_pub)) 146 { 147 GNUNET_break (0); 148 TALER_TESTING_interpreter_fail (pls->is); 149 return; 150 } 151 pls->anastasis_pub = *anastasis_pub; 152 } 153 pls->plo = ANASTASIS_policy_lookup ( 154 TALER_TESTING_interpreter_get_context (is), 155 pls->anastasis_url, 156 &pls->anastasis_pub, 157 &policy_lookup_cb, 158 pls); 159 if (NULL == pls->plo) 160 { 161 GNUNET_break (0); 162 TALER_TESTING_interpreter_fail (pls->is); 163 return; 164 } 165 } 166 167 168 /** 169 * Free the state of a "policy lookup" CMD, and possibly 170 * cancel it if it did not complete. 171 * 172 * @param cls closure. 173 * @param cmd command being freed. 174 */ 175 static void 176 policy_lookup_cleanup (void *cls, 177 const struct TALER_TESTING_Command *cmd) 178 { 179 struct PolicyLookupState *pls = cls; 180 181 if (NULL != pls->plo) 182 { 183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 184 "Command '%s' did not complete (policy lookup)\n", 185 cmd->label); 186 ANASTASIS_policy_lookup_cancel (pls->plo); 187 pls->plo = NULL; 188 } 189 GNUNET_free (pls); 190 } 191 192 193 struct TALER_TESTING_Command 194 ANASTASIS_TESTING_cmd_policy_lookup (const char *label, 195 const char *anastasis_url, 196 unsigned int http_status, 197 const char *upload_ref) 198 { 199 struct PolicyLookupState *pls; 200 201 GNUNET_assert (NULL != upload_ref); 202 pls = GNUNET_new (struct PolicyLookupState); 203 pls->http_status = http_status; 204 pls->anastasis_url = anastasis_url; 205 pls->upload_reference = upload_ref; 206 { 207 struct TALER_TESTING_Command cmd = { 208 .cls = pls, 209 .label = label, 210 .run = &policy_lookup_run, 211 .cleanup = &policy_lookup_cleanup 212 }; 213 214 return cmd; 215 } 216 } 217 218 219 struct TALER_TESTING_Command 220 ANASTASIS_TESTING_cmd_policy_nx (const char *label, 221 const char *anastasis_url) 222 { 223 struct PolicyLookupState *pls; 224 struct GNUNET_CRYPTO_EddsaPrivateKey priv; 225 226 pls = GNUNET_new (struct PolicyLookupState); 227 pls->http_status = MHD_HTTP_NOT_FOUND; 228 pls->anastasis_url = anastasis_url; 229 GNUNET_CRYPTO_eddsa_key_create (&priv); 230 GNUNET_CRYPTO_eddsa_key_get_public (&priv, 231 &pls->anastasis_pub.pub); 232 { 233 struct TALER_TESTING_Command cmd = { 234 .cls = pls, 235 .label = label, 236 .run = &policy_lookup_run, 237 .cleanup = &policy_lookup_cleanup 238 }; 239 240 return cmd; 241 } 242 }