aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_policy_lookup.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testing/testing_api_cmd_policy_lookup.c')
-rw-r--r--src/testing/testing_api_cmd_policy_lookup.c267
1 files changed, 267 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_policy_lookup.c b/src/testing/testing_api_cmd_policy_lookup.c
new file mode 100644
index 0000000..e97f746
--- /dev/null
+++ b/src/testing/testing_api_cmd_policy_lookup.c
@@ -0,0 +1,267 @@
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_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 */
32struct 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_lookup().
73 *
74 * @param cls closure
75 * @param http_status HTTP status of the request
76 * @param ud details about the lookup operation
77 */
78static void
79policy_lookup_cb (void *cls,
80 unsigned int http_status,
81 const struct ANASTASIS_DownloadDetails *dd)
82{
83 struct PolicyLookupState *pls = cls;
84
85 pls->plo = NULL;
86 if (http_status != pls->http_status)
87 {
88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89 "Unexpected response code %u to command %s in %s:%u\n",
90 http_status,
91 pls->is->commands[pls->is->ip].label,
92 __FILE__,
93 __LINE__);
94 TALER_TESTING_interpreter_fail (pls->is);
95 return;
96 }
97 if (NULL != pls->upload_reference)
98 {
99 if ( (MHD_HTTP_OK == http_status) &&
100 (0 != GNUNET_memcmp (&dd->curr_policy_hash,
101 pls->upload_hash)) )
102 {
103 GNUNET_break (0);
104 TALER_TESTING_interpreter_fail (pls->is);
105 return;
106 }
107 }
108 TALER_TESTING_interpreter_next (pls->is);
109}
110
111
112/**
113 * Run a "policy lookup" CMD.
114 *
115 * @param cls closure.
116 * @param cmd command currently being run.
117 * @param is interpreter state.
118 */
119static void
120policy_lookup_run (void *cls,
121 const struct TALER_TESTING_Command *cmd,
122 struct TALER_TESTING_Interpreter *is)
123{
124 struct PolicyLookupState *pls = cls;
125
126 pls->is = is;
127 if (NULL != pls->upload_reference)
128 {
129 const struct TALER_TESTING_Command *upload_cmd;
130 const struct ANASTASIS_CRYPTO_AccountPublicKeyP *anastasis_pub;
131
132 upload_cmd = TALER_TESTING_interpreter_lookup_command
133 (is,
134 pls->upload_reference);
135 if (NULL == upload_cmd)
136 {
137 GNUNET_break (0);
138 TALER_TESTING_interpreter_fail (pls->is);
139 return;
140 }
141 if (GNUNET_OK !=
142 ANASTASIS_TESTING_get_trait_hash (upload_cmd,
143 ANASTASIS_TESTING_TRAIT_HASH_CURRENT,
144 &pls->upload_hash))
145 {
146 GNUNET_break (0);
147 TALER_TESTING_interpreter_fail (pls->is);
148 return;
149 }
150 if (GNUNET_OK !=
151 ANASTASIS_TESTING_get_trait_account_pub (upload_cmd,
152 0,
153 &anastasis_pub))
154 {
155 GNUNET_break (0);
156 TALER_TESTING_interpreter_fail (pls->is);
157 return;
158 }
159 pls->anastasis_pub = *anastasis_pub;
160 }
161 pls->plo = ANASTASIS_policy_lookup (is->ctx,
162 pls->anastasis_url,
163 &pls->anastasis_pub,
164 &policy_lookup_cb,
165 pls);
166 if (NULL == pls->plo)
167 {
168 GNUNET_break (0);
169 TALER_TESTING_interpreter_fail (pls->is);
170 return;
171 }
172}
173
174
175/**
176 * Free the state of a "policy lookup" CMD, and possibly
177 * cancel it if it did not complete.
178 *
179 * @param cls closure.
180 * @param cmd command being freed.
181 */
182static void
183policy_lookup_cleanup (void *cls,
184 const struct TALER_TESTING_Command *cmd)
185{
186 struct PolicyLookupState *pls = cls;
187
188 if (NULL != pls->plo)
189 {
190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
191 "Command '%s' did not complete (policy lookup)\n",
192 cmd->label);
193 ANASTASIS_policy_lookup_cancel (pls->plo);
194 pls->plo = NULL;
195 }
196 GNUNET_free (pls);
197}
198
199
200/**
201 * Make the "policy lookup" command.
202 *
203 * @param label command label
204 * @param anastasis_url base URL of the ANASTASIS serving
205 * the policy store request.
206 * @param http_status expected HTTP status.
207 * @param upload_ref reference to upload command
208 * @return the command
209 */
210struct TALER_TESTING_Command
211ANASTASIS_TESTING_cmd_policy_lookup (const char *label,
212 const char *anastasis_url,
213 unsigned int http_status,
214 const char *upload_ref)
215{
216 struct PolicyLookupState *pls;
217
218 GNUNET_assert (NULL != upload_ref);
219 pls = GNUNET_new (struct PolicyLookupState);
220 pls->http_status = http_status;
221 pls->anastasis_url = anastasis_url;
222 pls->upload_reference = upload_ref;
223 {
224 struct TALER_TESTING_Command cmd = {
225 .cls = pls,
226 .label = label,
227 .run = &policy_lookup_run,
228 .cleanup = &policy_lookup_cleanup
229 };
230
231 return cmd;
232 }
233}
234
235
236/**
237 * Make the "policy lookup" command for a non-existent upload.
238 *
239 * @param label command label
240 * @param anastasis_url base URL of the ANASTASIS serving
241 * the policy lookup request.
242 * @return the command
243 */
244struct TALER_TESTING_Command
245ANASTASIS_TESTING_cmd_policy_nx (const char *label,
246 const char *anastasis_url)
247{
248 struct PolicyLookupState *pls;
249 struct GNUNET_CRYPTO_EddsaPrivateKey priv;
250
251 pls = GNUNET_new (struct PolicyLookupState);
252 pls->http_status = MHD_HTTP_NOT_FOUND;
253 pls->anastasis_url = anastasis_url;
254 GNUNET_CRYPTO_eddsa_key_create (&priv);
255 GNUNET_CRYPTO_eddsa_key_get_public (&priv,
256 &pls->anastasis_pub.pub);
257 {
258 struct TALER_TESTING_Command cmd = {
259 .cls = pls,
260 .label = label,
261 .run = &policy_lookup_run,
262 .cleanup = &policy_lookup_cleanup
263 };
264
265 return cmd;
266 }
267}