Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020 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_api_cmd_lock_product.c
21 : * @brief command to test LOCK /product
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : #include <taler/taler_exchange_service.h>
26 : #include <taler/taler_testing_lib.h>
27 : #include "taler_merchant_service.h"
28 : #include "taler_merchant_testing_lib.h"
29 :
30 :
31 : /**
32 : * State of a "POST /products/$ID" CMD.
33 : */
34 : struct LockProductState
35 : {
36 :
37 : /**
38 : * Handle for a "GET product" request.
39 : */
40 : struct TALER_MERCHANT_ProductLockHandle *iph;
41 :
42 : /**
43 : * The interpreter state.
44 : */
45 : struct TALER_TESTING_Interpreter *is;
46 :
47 : /**
48 : * Base URL of the merchant serving the request.
49 : */
50 : const char *merchant_url;
51 :
52 : /**
53 : * ID of the product to run GET for.
54 : */
55 : const char *product_id;
56 :
57 : /**
58 : * UUID that identifies the client holding the lock
59 : */
60 : char *uuid;
61 :
62 : /**
63 : * duration how long should the lock be held
64 : */
65 : struct GNUNET_TIME_Relative duration;
66 :
67 : /**
68 : * how much product should be locked
69 : */
70 : uint32_t quantity;
71 :
72 : /**
73 : * Expected HTTP response code.
74 : */
75 : unsigned int http_status;
76 :
77 : };
78 :
79 :
80 : /**
81 : * Callback for a POST /products/$ID/lock operation.
82 : *
83 : * @param cls closure for this function
84 : * @param hr response being processed
85 : */
86 : static void
87 0 : lock_product_cb (void *cls,
88 : const struct TALER_MERCHANT_HttpResponse *hr)
89 : {
90 0 : struct LockProductState *pis = cls;
91 :
92 0 : pis->iph = NULL;
93 0 : if (pis->http_status != hr->http_status)
94 : {
95 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
96 : "Unexpected response code %u (%d) to command %s\n",
97 : hr->http_status,
98 : (int) hr->ec,
99 : TALER_TESTING_interpreter_get_current_label (pis->is));
100 0 : TALER_TESTING_interpreter_fail (pis->is);
101 0 : return;
102 : }
103 0 : switch (hr->http_status)
104 : {
105 0 : case MHD_HTTP_NO_CONTENT:
106 0 : break;
107 0 : case MHD_HTTP_FORBIDDEN:
108 0 : break;
109 0 : case MHD_HTTP_NOT_FOUND:
110 0 : break;
111 0 : case MHD_HTTP_GONE:
112 0 : break;
113 0 : default:
114 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
115 : "Unhandled HTTP status %u for lock product.\n",
116 : hr->http_status);
117 : }
118 0 : TALER_TESTING_interpreter_next (pis->is);
119 : }
120 :
121 :
122 : /**
123 : * Run the "LOCK /products/$ID" CMD.
124 : *
125 : *
126 : * @param cls closure.
127 : * @param cmd command being run now.
128 : * @param is interpreter state.
129 : */
130 : static void
131 0 : lock_product_run (void *cls,
132 : const struct TALER_TESTING_Command *cmd,
133 : struct TALER_TESTING_Interpreter *is)
134 : {
135 0 : struct LockProductState *pis = cls;
136 :
137 0 : pis->is = is;
138 0 : pis->iph = TALER_MERCHANT_product_lock (is->ctx,
139 : pis->merchant_url,
140 : pis->product_id,
141 0 : pis->uuid,
142 : pis->duration,
143 : pis->quantity,
144 : &lock_product_cb,
145 : pis);
146 0 : GNUNET_assert (NULL != pis->iph);
147 0 : }
148 :
149 :
150 : /**
151 : * Free the state of a "GET product" CMD, and possibly
152 : * cancel a pending operation thereof.
153 : *
154 : * @param cls closure.
155 : * @param cmd command being run.
156 : */
157 : static void
158 0 : lock_product_cleanup (void *cls,
159 : const struct TALER_TESTING_Command *cmd)
160 : {
161 0 : struct LockProductState *pis = cls;
162 :
163 0 : if (NULL != pis->iph)
164 : {
165 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
166 : "POST /product/$ID/lock operation did not complete\n");
167 0 : TALER_MERCHANT_product_lock_cancel (pis->iph);
168 : }
169 0 : GNUNET_free (pis->uuid);
170 0 : GNUNET_free (pis);
171 0 : }
172 :
173 :
174 : /**
175 : * Offer internal data to other commands.
176 : *
177 : * @param cls closure
178 : * @param[out] ret result (could be anything)
179 : * @param trait name of the trait
180 : * @param index index number of the object to extract.
181 : * @return #GNUNET_OK on success
182 : */
183 : static enum GNUNET_GenericReturnValue
184 0 : lock_product_traits (void *cls,
185 : const void **ret,
186 : const char *trait,
187 : unsigned int index)
188 : {
189 0 : struct LockProductState *lps = cls;
190 : struct TALER_TESTING_Trait traits[] = {
191 0 : TALER_TESTING_make_trait_lock_uuid (
192 0 : (const char **) &lps->uuid),
193 0 : TALER_TESTING_trait_end ()
194 : };
195 :
196 0 : return TALER_TESTING_get_trait (traits,
197 : ret,
198 : trait,
199 : index);
200 : }
201 :
202 :
203 : struct TALER_TESTING_Command
204 0 : TALER_TESTING_cmd_merchant_lock_product (
205 : const char *label,
206 : const char *merchant_url,
207 : const char *product_id,
208 : struct GNUNET_TIME_Relative duration,
209 : uint32_t quantity,
210 : unsigned int http_status)
211 : {
212 : struct LockProductState *pis;
213 : struct GNUNET_Uuid uuid;
214 :
215 0 : pis = GNUNET_new (struct LockProductState);
216 0 : pis->merchant_url = merchant_url;
217 0 : pis->product_id = product_id;
218 0 : pis->http_status = http_status;
219 0 : GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
220 : &uuid,
221 : sizeof (struct GNUNET_Uuid));
222 0 : pis->uuid = GNUNET_STRINGS_data_to_string_alloc (&uuid,
223 : sizeof (uuid));
224 0 : pis->duration = duration;
225 0 : pis->quantity = quantity;
226 :
227 : {
228 0 : struct TALER_TESTING_Command cmd = {
229 : .cls = pis,
230 : .label = label,
231 : .run = &lock_product_run,
232 : .cleanup = &lock_product_cleanup,
233 : .traits = &lock_product_traits
234 : };
235 :
236 0 : return cmd;
237 : }
238 : }
239 :
240 :
241 : /* end of testing_api_cmd_lock_product.c */
|