Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2018 Taler Systems SA
4 :
5 : TALER 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 : TALER 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 TALER; see the file COPYING. If not, see
17 : <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file testing/testing_api_cmd_serialize_keys.c
21 : * @brief Lets tests use the keys serialization API.
22 : * @author Marcello Stanisci
23 : */
24 : #include "platform.h"
25 : #include <jansson.h>
26 : #include "taler_testing_lib.h"
27 :
28 :
29 : /**
30 : * Internal state for a serialize-keys CMD.
31 : */
32 : struct SerializeKeysState
33 : {
34 : /**
35 : * Serialized keys.
36 : */
37 : json_t *keys;
38 :
39 : /**
40 : * Exchange URL. Needed because the exchange gets disconnected
41 : * from, after keys serialization. This value is then needed by
42 : * subsequent commands that have to reconnect to the exchange.
43 : */
44 : char *exchange_url;
45 : };
46 :
47 :
48 : /**
49 : * Internal state for a connect-with-state CMD.
50 : */
51 : struct ConnectWithStateState
52 : {
53 :
54 : /**
55 : * Reference to a CMD that offers a serialized key-state
56 : * that will be used in the reconnection.
57 : */
58 : const char *state_reference;
59 :
60 : /**
61 : * If set to GNUNET_YES, then the /keys callback has already
62 : * been passed the control to the next CMD. This is necessary
63 : * because it is not uncommon that the /keys callback gets
64 : * invoked multiple times, and without this flag, we would keep
65 : * going "next" CMD upon every invocation (causing impredictable
66 : * behaviour as for the instruction pointer.)
67 : */
68 : unsigned int consumed;
69 :
70 : /**
71 : * Interpreter state.
72 : */
73 : struct TALER_TESTING_Interpreter *is;
74 : };
75 :
76 :
77 : /**
78 : * Run the command.
79 : *
80 : * @param cls closure.
81 : * @param cmd the command to execute.
82 : * @param is the interpreter state.
83 : */
84 : static void
85 0 : serialize_keys_run (void *cls,
86 : const struct TALER_TESTING_Command *cmd,
87 : struct TALER_TESTING_Interpreter *is)
88 : {
89 0 : struct SerializeKeysState *sks = cls;
90 :
91 0 : sks->keys = TALER_EXCHANGE_serialize_data (is->exchange);
92 0 : if (NULL == sks->keys)
93 0 : TALER_TESTING_interpreter_fail (is);
94 :
95 0 : sks->exchange_url = GNUNET_strdup
96 : (TALER_EXCHANGE_get_base_url (is->exchange));
97 0 : TALER_EXCHANGE_disconnect (is->exchange);
98 0 : is->exchange = NULL;
99 0 : is->working = GNUNET_NO;
100 0 : TALER_TESTING_interpreter_next (is);
101 0 : }
102 :
103 :
104 : /**
105 : * Cleanup the state of a "serialize keys" CMD.
106 : *
107 : * @param cls closure.
108 : * @param cmd the command which is being cleaned up.
109 : */
110 : static void
111 0 : serialize_keys_cleanup (void *cls,
112 : const struct TALER_TESTING_Command *cmd)
113 : {
114 0 : struct SerializeKeysState *sks = cls;
115 :
116 0 : if (NULL != sks->keys)
117 : {
118 0 : json_decref (sks->keys);
119 : }
120 0 : GNUNET_free (sks->exchange_url);
121 0 : GNUNET_free (sks);
122 0 : }
123 :
124 :
125 : /**
126 : * Offer serialized keys as trait.
127 : *
128 : * @param cls closure.
129 : * @param[out] ret result.
130 : * @param trait name of the trait.
131 : * @param index index number of the object to offer.
132 : * @return #GNUNET_OK on success.
133 : */
134 : static enum GNUNET_GenericReturnValue
135 0 : serialize_keys_traits (void *cls,
136 : const void **ret,
137 : const char *trait,
138 : unsigned int index)
139 : {
140 0 : struct SerializeKeysState *sks = cls;
141 : struct TALER_TESTING_Trait traits[] = {
142 0 : TALER_TESTING_make_trait_exchange_keys (sks->keys),
143 0 : TALER_TESTING_make_trait_exchange_url (
144 0 : (const char **) &sks->exchange_url),
145 0 : TALER_TESTING_trait_end ()
146 : };
147 :
148 0 : return TALER_TESTING_get_trait (traits,
149 : ret,
150 : trait,
151 : index);
152 : }
153 :
154 :
155 : /**
156 : * Run the command.
157 : *
158 : * @param cls closure.
159 : * @param cmd the command to execute.
160 : * @param is the interpreter state.
161 : */
162 : static void
163 0 : connect_with_state_run (void *cls,
164 : const struct TALER_TESTING_Command *cmd,
165 : struct TALER_TESTING_Interpreter *is)
166 : {
167 0 : struct ConnectWithStateState *cwss = cls;
168 : const struct TALER_TESTING_Command *state_cmd;
169 : const json_t *serialized_keys;
170 : const char **exchange_url;
171 :
172 : /* This command usually gets rescheduled after serialized
173 : * reconnection. */
174 0 : if (GNUNET_YES == cwss->consumed)
175 : {
176 0 : TALER_TESTING_interpreter_next (is);
177 0 : return;
178 : }
179 :
180 0 : cwss->is = is;
181 0 : state_cmd = TALER_TESTING_interpreter_lookup_command (is,
182 : cwss->state_reference);
183 :
184 : /* Command providing serialized keys not found. */
185 0 : if (NULL == state_cmd)
186 : {
187 0 : GNUNET_break (0);
188 0 : TALER_TESTING_interpreter_fail (is);
189 0 : return;
190 : }
191 :
192 0 : GNUNET_assert (GNUNET_OK ==
193 : TALER_TESTING_get_trait_exchange_keys (state_cmd,
194 : &serialized_keys));
195 : {
196 : char *dump;
197 :
198 0 : dump = json_dumps (serialized_keys,
199 : JSON_INDENT (1));
200 0 : TALER_LOG_DEBUG ("Serialized key-state: %s\n",
201 : dump);
202 0 : free (dump);
203 : }
204 :
205 0 : GNUNET_assert (GNUNET_OK ==
206 : TALER_TESTING_get_trait_exchange_url (state_cmd,
207 : &exchange_url));
208 0 : is->exchange = TALER_EXCHANGE_connect (is->ctx,
209 : *exchange_url,
210 : &TALER_TESTING_cert_cb,
211 : cwss,
212 : TALER_EXCHANGE_OPTION_DATA,
213 : serialized_keys,
214 : TALER_EXCHANGE_OPTION_END);
215 0 : cwss->consumed = GNUNET_YES;
216 : }
217 :
218 :
219 : /**
220 : * Cleanup the state of a "connect with state" CMD. Just
221 : * a placeholder to avoid jumping on an invalid address.
222 : *
223 : * @param cls closure.
224 : * @param cmd the command which is being cleaned up.
225 : */
226 : static void
227 0 : connect_with_state_cleanup (void *cls,
228 : const struct TALER_TESTING_Command *cmd)
229 : {
230 0 : struct ConnectWithStateState *cwss = cls;
231 :
232 0 : GNUNET_free (cwss);
233 0 : }
234 :
235 :
236 : struct TALER_TESTING_Command
237 0 : TALER_TESTING_cmd_serialize_keys (const char *label)
238 : {
239 : struct SerializeKeysState *sks;
240 :
241 0 : sks = GNUNET_new (struct SerializeKeysState);
242 : {
243 0 : struct TALER_TESTING_Command cmd = {
244 : .cls = sks,
245 : .label = label,
246 : .run = serialize_keys_run,
247 : .cleanup = serialize_keys_cleanup,
248 : .traits = serialize_keys_traits
249 : };
250 :
251 0 : return cmd;
252 : }
253 : }
254 :
255 :
256 : struct TALER_TESTING_Command
257 0 : TALER_TESTING_cmd_connect_with_state (const char *label,
258 : const char *state_reference)
259 : {
260 : struct ConnectWithStateState *cwss;
261 :
262 0 : cwss = GNUNET_new (struct ConnectWithStateState);
263 0 : cwss->state_reference = state_reference;
264 0 : cwss->consumed = GNUNET_NO;
265 : {
266 0 : struct TALER_TESTING_Command cmd = {
267 : .cls = cwss,
268 : .label = label,
269 : .run = connect_with_state_run,
270 : .cleanup = connect_with_state_cleanup
271 : };
272 :
273 0 : return cmd;
274 : }
275 : }
|