Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2018 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it
6 : under the terms of the GNU General Public License as published
7 : by the Free Software Foundation; either version 3, or (at your
8 : 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,
17 : see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file testing/testing_api_cmd_exec_closer.c
21 : * @brief run the taler-exchange-closer command
22 : * @author Marcello Stanisci
23 : */
24 : #include "platform.h"
25 : #include "taler_json_lib.h"
26 : #include <gnunet/gnunet_curl_lib.h>
27 : #include "taler_signatures.h"
28 : #include "taler_testing_lib.h"
29 :
30 :
31 : /**
32 : * State for a "closer" CMD.
33 : */
34 : struct CloserState
35 : {
36 :
37 : /**
38 : * Closer process.
39 : */
40 : struct GNUNET_OS_Process *closer_proc;
41 :
42 : /**
43 : * Configuration file used by the closer.
44 : */
45 : const char *config_filename;
46 :
47 : /**
48 : * Reserve history entry that corresponds to this operation. Set if @e
49 : * expect_close is true. Will be of type
50 : * #TALER_EXCHANGE_RTT_RESERVE_CLOSED.
51 : */
52 : struct TALER_EXCHANGE_ReserveHistoryEntry reserve_history;
53 :
54 : /**
55 : * If the closer filled a reserve (@e expect_close is set), this is set to
56 : * the reserve's public key.
57 : */
58 : struct TALER_ReservePublicKeyP reserve_pub;
59 :
60 : /**
61 : * Reference to a command to get the @e reserve_pub.
62 : */
63 : const char *reserve_ref;
64 :
65 : /**
66 : * Do we expect the command to actually close a reserve?
67 : */
68 : bool expect_close;
69 : };
70 :
71 :
72 : /**
73 : * Run the command. Use the `taler-exchange-closer' program.
74 : *
75 : * @param cls closure.
76 : * @param cmd command being run.
77 : * @param is interpreter state.
78 : */
79 : static void
80 0 : closer_run (void *cls,
81 : const struct TALER_TESTING_Command *cmd,
82 : struct TALER_TESTING_Interpreter *is)
83 : {
84 0 : struct CloserState *as = cls;
85 :
86 : (void) cmd;
87 0 : if (NULL != as->reserve_ref)
88 : {
89 : const struct TALER_TESTING_Command *rcmd;
90 : const struct TALER_ReservePublicKeyP *reserve_pubp;
91 :
92 0 : rcmd = TALER_TESTING_interpreter_lookup_command (is,
93 : as->reserve_ref);
94 0 : if (GNUNET_OK !=
95 0 : TALER_TESTING_get_trait_reserve_pub (rcmd,
96 : &reserve_pubp))
97 : {
98 0 : GNUNET_break (0);
99 0 : TALER_TESTING_interpreter_fail (is);
100 0 : return;
101 : }
102 0 : as->reserve_pub = *reserve_pubp;
103 : }
104 : as->closer_proc
105 0 : = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
106 : NULL, NULL, NULL,
107 : "taler-exchange-closer",
108 : "taler-exchange-closer",
109 : "-c", as->config_filename,
110 : "-t", /* exit when done */
111 : NULL);
112 0 : if (NULL == as->closer_proc)
113 : {
114 0 : GNUNET_break (0);
115 0 : TALER_TESTING_interpreter_fail (is);
116 0 : return;
117 : }
118 0 : TALER_TESTING_wait_for_sigchld (is);
119 : }
120 :
121 :
122 : /**
123 : * Free the state of a "closer" CMD, and possibly kill its
124 : * process if it did not terminate correctly.
125 : *
126 : * @param cls closure.
127 : * @param cmd the command being freed.
128 : */
129 : static void
130 0 : closer_cleanup (void *cls,
131 : const struct TALER_TESTING_Command *cmd)
132 : {
133 0 : struct CloserState *as = cls;
134 :
135 : (void) cmd;
136 0 : if (NULL != as->closer_proc)
137 : {
138 0 : GNUNET_break (0 ==
139 : GNUNET_OS_process_kill (as->closer_proc,
140 : SIGKILL));
141 0 : GNUNET_OS_process_wait (as->closer_proc);
142 0 : GNUNET_OS_process_destroy (as->closer_proc);
143 0 : as->closer_proc = NULL;
144 : }
145 0 : GNUNET_free (as);
146 0 : }
147 :
148 :
149 : /**
150 : * Offer "closer" CMD internal data to other commands.
151 : *
152 : * @param cls closure.
153 : * @param[out] ret result.
154 : * @param trait name of the trait.
155 : * @param index index number of the object to offer.
156 : * @return #GNUNET_OK on success
157 : */
158 : static enum GNUNET_GenericReturnValue
159 0 : closer_traits (void *cls,
160 : const void **ret,
161 : const char *trait,
162 : unsigned int index)
163 : {
164 0 : struct CloserState *as = cls;
165 : struct TALER_TESTING_Trait traits[] = {
166 0 : TALER_TESTING_make_trait_process (&as->closer_proc),
167 0 : TALER_TESTING_trait_end ()
168 : };
169 : struct TALER_TESTING_Trait xtraits[] = {
170 0 : TALER_TESTING_make_trait_process (&as->closer_proc),
171 0 : TALER_TESTING_make_trait_reserve_pub (&as->reserve_pub),
172 0 : TALER_TESTING_make_trait_reserve_history (0,
173 0 : &as->reserve_history),
174 0 : TALER_TESTING_trait_end ()
175 : };
176 :
177 0 : return TALER_TESTING_get_trait ((as->expect_close)
178 : ? xtraits
179 : : traits,
180 : ret,
181 : trait,
182 : index);
183 : }
184 :
185 :
186 : struct TALER_TESTING_Command
187 0 : TALER_TESTING_cmd_exec_closer (const char *label,
188 : const char *config_filename,
189 : const char *expected_amount,
190 : const char *expected_fee,
191 : const char *expected_reserve_ref)
192 : {
193 : struct CloserState *as;
194 :
195 0 : as = GNUNET_new (struct CloserState);
196 0 : as->config_filename = config_filename;
197 0 : if (NULL != expected_reserve_ref)
198 : {
199 0 : as->expect_close = true;
200 0 : as->reserve_ref = expected_reserve_ref;
201 0 : if (GNUNET_OK !=
202 0 : TALER_string_to_amount (expected_amount,
203 : &as->reserve_history.amount))
204 : {
205 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 : "Failed to parse amount `%s' at %s\n",
207 : expected_amount,
208 : label);
209 0 : GNUNET_assert (0);
210 : }
211 0 : if (GNUNET_OK !=
212 0 : TALER_string_to_amount (expected_fee,
213 : &as->reserve_history.details.close_details.fee))
214 : {
215 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216 : "Failed to parse amount `%s' at %s\n",
217 : expected_fee,
218 : label);
219 0 : GNUNET_assert (0);
220 : }
221 : /* expected amount includes fee, while our argument
222 : gives the amount _without_ the fee. So add the fee. */
223 0 : GNUNET_assert (0 <=
224 : TALER_amount_add (&as->reserve_history.amount,
225 : &as->reserve_history.amount,
226 : &as->reserve_history.details.close_details.
227 : fee));
228 0 : as->reserve_history.type = TALER_EXCHANGE_RTT_CLOSE;
229 : }
230 : {
231 0 : struct TALER_TESTING_Command cmd = {
232 : .cls = as,
233 : .label = label,
234 : .run = &closer_run,
235 : .cleanup = &closer_cleanup,
236 : .traits = &closer_traits
237 : };
238 :
239 0 : return cmd;
240 : }
241 : }
242 :
243 :
244 : /* end of testing_api_cmd_exec_closer.c */
|