Line data Source code
1 : /*
2 : This file is part of SYNC
3 : Copyright (C) 2014-2019 Taler Systems SA
4 :
5 : SYNC 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 : SYNC 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 SYNC; see the file COPYING. If not, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file lib/testing_api_cmd_backup_download.c
21 : * @brief command to download data to the sync backend service.
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : #include "sync_service.h"
26 : #include "sync_testing_lib.h"
27 : #include <taler/taler_util.h>
28 : #include <taler/taler_testing_lib.h>
29 :
30 : /**
31 : * State for a "backup download" CMD.
32 : */
33 : struct BackupDownloadState
34 : {
35 :
36 : /**
37 : * Eddsa public key.
38 : */
39 : struct SYNC_AccountPublicKeyP sync_pub;
40 :
41 : /**
42 : * Hash of the upload (all zeros if there was no upload).
43 : */
44 : const struct GNUNET_HashCode *upload_hash;
45 :
46 : /**
47 : * Hash of the previous upload (all zeros if there was no previous upload).
48 : */
49 : const struct GNUNET_HashCode *prev_upload_hash;
50 :
51 : /**
52 : * The /backups POST operation handle.
53 : */
54 : struct SYNC_DownloadOperation *download;
55 :
56 : /**
57 : * URL of the sync backend.
58 : */
59 : const char *sync_url;
60 :
61 : /**
62 : * The interpreter state.
63 : */
64 : struct TALER_TESTING_Interpreter *is;
65 :
66 : /**
67 : * Reference to upload command we expect to download.
68 : */
69 : const char *upload_reference;
70 :
71 : /**
72 : * Expected status code.
73 : */
74 : unsigned int http_status;
75 :
76 : };
77 :
78 :
79 : /**
80 : * Function called with the results of a #SYNC_download().
81 : *
82 : * @param cls closure
83 : * @param http_status HTTP status of the request
84 : * @param ud details about the download operation
85 : */
86 : static void
87 0 : backup_download_cb (void *cls,
88 : unsigned int http_status,
89 : const struct SYNC_DownloadDetails *dd)
90 : {
91 0 : struct BackupDownloadState *bds = cls;
92 :
93 0 : bds->download = NULL;
94 0 : if (http_status != bds->http_status)
95 : {
96 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
97 : "Unexpected response code %u to command %s in %s:%u\n",
98 : http_status,
99 : bds->is->commands[bds->is->ip].label,
100 : __FILE__,
101 : __LINE__);
102 0 : TALER_TESTING_interpreter_fail (bds->is);
103 0 : return;
104 : }
105 0 : if (NULL != bds->upload_reference)
106 : {
107 0 : if ( (MHD_HTTP_OK == http_status) &&
108 0 : (0 != GNUNET_memcmp (&dd->curr_backup_hash,
109 : bds->upload_hash)) )
110 : {
111 0 : GNUNET_break (0);
112 0 : TALER_TESTING_interpreter_fail (bds->is);
113 0 : return;
114 : }
115 0 : if ( (MHD_HTTP_OK == http_status) &&
116 0 : (0 != GNUNET_memcmp (&dd->prev_backup_hash,
117 : bds->prev_upload_hash)) )
118 : {
119 0 : GNUNET_break (0);
120 0 : TALER_TESTING_interpreter_fail (bds->is);
121 0 : return;
122 : }
123 : }
124 0 : TALER_TESTING_interpreter_next (bds->is);
125 : }
126 :
127 :
128 : /**
129 : * Run a "backup download" CMD.
130 : *
131 : * @param cls closure.
132 : * @param cmd command currently being run.
133 : * @param is interpreter state.
134 : */
135 : static void
136 0 : backup_download_run (void *cls,
137 : const struct TALER_TESTING_Command *cmd,
138 : struct TALER_TESTING_Interpreter *is)
139 : {
140 0 : struct BackupDownloadState *bds = cls;
141 :
142 0 : bds->is = is;
143 0 : if (NULL != bds->upload_reference)
144 : {
145 : const struct TALER_TESTING_Command *upload_cmd;
146 : const struct SYNC_AccountPublicKeyP *sync_pub;
147 :
148 0 : upload_cmd = TALER_TESTING_interpreter_lookup_command
149 : (is,
150 : bds->upload_reference);
151 0 : if (NULL == upload_cmd)
152 : {
153 0 : GNUNET_break (0);
154 0 : TALER_TESTING_interpreter_fail (bds->is);
155 0 : return;
156 : }
157 0 : if (GNUNET_OK !=
158 0 : SYNC_TESTING_get_trait_hash (upload_cmd,
159 : SYNC_TESTING_TRAIT_HASH_CURRENT,
160 : &bds->upload_hash))
161 : {
162 0 : GNUNET_break (0);
163 0 : TALER_TESTING_interpreter_fail (bds->is);
164 0 : return;
165 : }
166 0 : if (GNUNET_OK !=
167 0 : SYNC_TESTING_get_trait_hash (upload_cmd,
168 : SYNC_TESTING_TRAIT_HASH_PREVIOUS,
169 : &bds->prev_upload_hash))
170 : {
171 0 : GNUNET_break (0);
172 0 : TALER_TESTING_interpreter_fail (bds->is);
173 0 : return;
174 : }
175 0 : if (GNUNET_OK !=
176 0 : SYNC_TESTING_get_trait_account_pub (upload_cmd,
177 : 0,
178 : &sync_pub))
179 : {
180 0 : GNUNET_break (0);
181 0 : TALER_TESTING_interpreter_fail (bds->is);
182 0 : return;
183 : }
184 0 : bds->sync_pub = *sync_pub;
185 : }
186 0 : bds->download = SYNC_download (is->ctx,
187 : bds->sync_url,
188 0 : &bds->sync_pub,
189 : &backup_download_cb,
190 : bds);
191 0 : if (NULL == bds->download)
192 : {
193 0 : GNUNET_break (0);
194 0 : TALER_TESTING_interpreter_fail (bds->is);
195 0 : return;
196 : }
197 : }
198 :
199 :
200 : /**
201 : * Free the state of a "backup download" CMD, and possibly
202 : * cancel it if it did not complete.
203 : *
204 : * @param cls closure.
205 : * @param cmd command being freed.
206 : */
207 : static void
208 0 : backup_download_cleanup (void *cls,
209 : const struct TALER_TESTING_Command *cmd)
210 : {
211 0 : struct BackupDownloadState *bds = cls;
212 :
213 0 : if (NULL != bds->download)
214 : {
215 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
216 : "Command '%s' did not complete (backup download)\n",
217 : cmd->label);
218 0 : SYNC_download_cancel (bds->download);
219 0 : bds->download = NULL;
220 : }
221 0 : GNUNET_free (bds);
222 0 : }
223 :
224 :
225 : /**
226 : * Make the "backup download" command.
227 : *
228 : * @param label command label
229 : * @param sync_url base URL of the sync serving
230 : * the policy store request.
231 : * @param http_status expected HTTP status.
232 : * @param upload_ref reference to upload command
233 : * @return the command
234 : */
235 : struct TALER_TESTING_Command
236 0 : SYNC_TESTING_cmd_backup_download (const char *label,
237 : const char *sync_url,
238 : unsigned int http_status,
239 : const char *upload_ref)
240 : {
241 : struct BackupDownloadState *bds;
242 :
243 0 : GNUNET_assert (NULL != upload_ref);
244 0 : bds = GNUNET_new (struct BackupDownloadState);
245 0 : bds->http_status = http_status;
246 0 : bds->sync_url = sync_url;
247 0 : bds->upload_reference = upload_ref;
248 : {
249 0 : struct TALER_TESTING_Command cmd = {
250 : .cls = bds,
251 : .label = label,
252 : .run = &backup_download_run,
253 : .cleanup = &backup_download_cleanup
254 : };
255 :
256 0 : return cmd;
257 : }
258 : }
259 :
260 :
261 : /**
262 : * Make the "backup download" command for a non-existent upload.
263 : *
264 : * @param label command label
265 : * @param sync_url base URL of the sync serving
266 : * the policy store request.
267 : * @return the command
268 : */
269 : struct TALER_TESTING_Command
270 0 : SYNC_TESTING_cmd_backup_nx (const char *label,
271 : const char *sync_url)
272 : {
273 : struct BackupDownloadState *bds;
274 : struct GNUNET_CRYPTO_EddsaPrivateKey priv;
275 :
276 0 : bds = GNUNET_new (struct BackupDownloadState);
277 0 : bds->http_status = MHD_HTTP_NOT_FOUND;
278 0 : bds->sync_url = sync_url;
279 0 : GNUNET_CRYPTO_eddsa_key_create (&priv);
280 0 : GNUNET_CRYPTO_eddsa_key_get_public (&priv,
281 : &bds->sync_pub.eddsa_pub);
282 : {
283 0 : struct TALER_TESTING_Command cmd = {
284 : .cls = bds,
285 : .label = label,
286 : .run = &backup_download_run,
287 : .cleanup = &backup_download_cleanup
288 : };
289 :
290 0 : return cmd;
291 : }
292 : }
|