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 10 : closer_run (void *cls,
81 : const struct TALER_TESTING_Command *cmd,
82 : struct TALER_TESTING_Interpreter *is)
83 : {
84 10 : struct CloserState *as = cls;
85 :
86 : (void) cmd;
87 10 : if (NULL != as->reserve_ref)
88 : {
89 : const struct TALER_TESTING_Command *rcmd;
90 : const struct TALER_ReservePublicKeyP *reserve_pubp;
91 :
92 9 : rcmd = TALER_TESTING_interpreter_lookup_command (is,
93 : as->reserve_ref);
94 9 : GNUNET_assert (NULL != rcmd);
95 9 : if (GNUNET_OK !=
96 9 : TALER_TESTING_get_trait_reserve_pub (rcmd,
97 : &reserve_pubp))
98 : {
99 0 : GNUNET_break (0);
100 0 : TALER_TESTING_interpreter_fail (is);
101 0 : return;
102 : }
103 9 : as->reserve_pub = *reserve_pubp;
104 : }
105 : as->closer_proc
106 10 : = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_ALL,
107 : NULL, NULL, NULL,
108 : "taler-exchange-closer",
109 : "taler-exchange-closer",
110 : "-c", as->config_filename,
111 : "-t", /* exit when done */
112 : NULL);
113 10 : if (NULL == as->closer_proc)
114 : {
115 0 : GNUNET_break (0);
116 0 : TALER_TESTING_interpreter_fail (is);
117 0 : return;
118 : }
119 10 : TALER_TESTING_wait_for_sigchld (is);
120 : }
121 :
122 :
123 : /**
124 : * Free the state of a "closer" CMD, and possibly kill its
125 : * process if it did not terminate correctly.
126 : *
127 : * @param cls closure.
128 : * @param cmd the command being freed.
129 : */
130 : static void
131 10 : closer_cleanup (void *cls,
132 : const struct TALER_TESTING_Command *cmd)
133 : {
134 10 : struct CloserState *as = cls;
135 :
136 : (void) cmd;
137 10 : if (NULL != as->closer_proc)
138 : {
139 0 : GNUNET_break (0 ==
140 : GNUNET_OS_process_kill (as->closer_proc,
141 : SIGKILL));
142 0 : GNUNET_OS_process_wait (as->closer_proc);
143 0 : GNUNET_OS_process_destroy (as->closer_proc);
144 0 : as->closer_proc = NULL;
145 : }
146 10 : GNUNET_free (as);
147 10 : }
148 :
149 :
150 : /**
151 : * Offer "closer" CMD internal data to other commands.
152 : *
153 : * @param cls closure.
154 : * @param[out] ret result.
155 : * @param trait name of the trait.
156 : * @param index index number of the object to offer.
157 : * @return #GNUNET_OK on success
158 : */
159 : static enum GNUNET_GenericReturnValue
160 20 : closer_traits (void *cls,
161 : const void **ret,
162 : const char *trait,
163 : unsigned int index)
164 : {
165 20 : struct CloserState *as = cls;
166 : struct TALER_TESTING_Trait traits[] = {
167 20 : TALER_TESTING_make_trait_process (&as->closer_proc),
168 20 : TALER_TESTING_trait_end ()
169 : };
170 : struct TALER_TESTING_Trait xtraits[] = {
171 20 : TALER_TESTING_make_trait_process (&as->closer_proc),
172 20 : TALER_TESTING_make_trait_reserve_pub (&as->reserve_pub),
173 20 : TALER_TESTING_make_trait_reserve_history (0,
174 20 : &as->reserve_history),
175 20 : TALER_TESTING_trait_end ()
176 : };
177 :
178 20 : return TALER_TESTING_get_trait ((as->expect_close)
179 : ? xtraits
180 : : traits,
181 : ret,
182 : trait,
183 : index);
184 : }
185 :
186 :
187 : struct TALER_TESTING_Command
188 10 : TALER_TESTING_cmd_exec_closer (const char *label,
189 : const char *config_filename,
190 : const char *expected_amount,
191 : const char *expected_fee,
192 : const char *expected_reserve_ref)
193 : {
194 : struct CloserState *as;
195 :
196 10 : as = GNUNET_new (struct CloserState);
197 10 : as->config_filename = config_filename;
198 10 : if (NULL != expected_reserve_ref)
199 : {
200 9 : as->expect_close = true;
201 9 : as->reserve_ref = expected_reserve_ref;
202 9 : if (GNUNET_OK !=
203 9 : TALER_string_to_amount (expected_amount,
204 : &as->reserve_history.amount))
205 : {
206 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 : "Failed to parse amount `%s' at %s\n",
208 : expected_amount,
209 : label);
210 0 : GNUNET_assert (0);
211 : }
212 9 : if (GNUNET_OK !=
213 9 : TALER_string_to_amount (expected_fee,
214 : &as->reserve_history.details.close_details.fee))
215 : {
216 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217 : "Failed to parse amount `%s' at %s\n",
218 : expected_fee,
219 : label);
220 0 : GNUNET_assert (0);
221 : }
222 : /* expected amount includes fee, while our argument
223 : gives the amount _without_ the fee. So add the fee. */
224 9 : GNUNET_assert (0 <=
225 : TALER_amount_add (
226 : &as->reserve_history.amount,
227 : &as->reserve_history.amount,
228 : &as->reserve_history.details.close_details.fee));
229 9 : as->reserve_history.type = TALER_EXCHANGE_RTT_CLOSING;
230 : }
231 : {
232 10 : struct TALER_TESTING_Command cmd = {
233 : .cls = as,
234 : .label = label,
235 : .run = &closer_run,
236 : .cleanup = &closer_cleanup,
237 : .traits = &closer_traits
238 : };
239 :
240 10 : return cmd;
241 : }
242 : }
243 :
244 :
245 : /* end of testing_api_cmd_exec_closer.c */
|