1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/*
This file is part of TALER
Copyright (C) 2014-2018 Taler Systems SA
TALER is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3, or
(at your option) any later version.
TALER is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with TALER; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>
*/
/**
* @file lib/testing_api_cmd_history.c
* @brief command to test the /history API.
* @author Marcello Stanisci
*/
#include "platform.h"
#include <taler/taler_exchange_service.h>
#include <taler/taler_testing_lib.h>
#include "taler_merchant_service.h"
#include "taler_merchant_testing_lib.h"
/**
* State for a "history" CMD.
*/
struct HistoryState
{
/**
* Expected status code.
*/
unsigned int http_status;
/**
* The merchant instance executing this CMD.
*/
const char *instance;
/**
* URL of the merchant backend serving the /history request.
*/
const char *merchant_url;
/**
* The interpreter state.
*/
struct TALER_TESTING_Interpreter *is;
/**
* Handle to the /history operation.
*/
struct TALER_MERCHANT_HistoryOperation *ho;
/**
* Only history entries younger than this
* value will be returned.
*/
struct GNUNET_TIME_Absolute time;
/**
* First row index we want in the results.
*/
unsigned long long start;
/**
* When this flag is GNUNET_YES, then the interpreter
* will request /history *omitting* the 'start' URL argument.
*/
int use_default_start;
/**
* How many rows we want the response to contain, at most.
*/
long long nrows;
/**
* Expected number of history entries returned by the
* backend.
*/
unsigned int nresult;
};
/**
* Parse given JSON object to absolute time.
*
* @param root the json object representing data
* @param[out] ret where to write the data
* @return #GNUNET_OK upon successful parsing;
* #GNUNET_SYSERR upon error
*/
static int
parse_abs_time (json_t *root,
struct GNUNET_TIME_Absolute *ret)
{
const char *val;
unsigned long long int tval;
val = json_string_value (root);
if (NULL == val)
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
if ( (0 == strcasecmp (val,
"/forever/")) ||
(0 == strcasecmp (val,
"/end of time/")) ||
(0 == strcasecmp (val,
"/never/")) )
{
*ret = GNUNET_TIME_UNIT_FOREVER_ABS;
return GNUNET_OK;
}
if (1 != sscanf (val,
"/Date(%llu)/",
&tval))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
/* Time is in seconds in JSON, but in microseconds in
* GNUNET_TIME_Absolute */
ret->abs_value_us = tval * 1000LL * 1000LL;
if ( (ret->abs_value_us) / 1000LL / 1000LL != tval)
{
/* Integer overflow */
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
return GNUNET_OK;
}
/**
* Callback for a /history request; checks that (1) HTTP status
* is expected, the number of rows returned is expected, and that
* the rows are sorted from the youngest to the oldest record.
*
* @param cls closure
* @param http_status HTTP status returned by the merchant
* backend
* @param ec taler-specific error code
* @param json actual body containing the history
*/
static void
history_cb (void *cls,
unsigned int http_status,
enum TALER_ErrorCode ec,
const json_t *json)
{
struct HistoryState *hs = cls;
unsigned int nresult;
struct GNUNET_TIME_Absolute last_timestamp;
struct GNUNET_TIME_Absolute entry_timestamp;
hs->ho = NULL;
if (hs->http_status != http_status)
TALER_TESTING_FAIL (hs->is);
if (0 == hs->http_status)
{
/* 0 was caused intentionally by the tests,
* move on without further checking. */
TALER_TESTING_interpreter_next (hs->is);
return;
}
nresult = json_array_size (json);
if (hs->nresult != nresult)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Unexpected number of history entries."
" Got %d, expected %d\n",
nresult,
hs->nresult);
TALER_TESTING_FAIL (hs->is);
}
last_timestamp = GNUNET_TIME_absolute_get ();
last_timestamp = GNUNET_TIME_absolute_add (last_timestamp,
GNUNET_TIME_UNIT_DAYS);
{
json_t *entry;
size_t index;
json_array_foreach (json, index, entry)
{
json_t *timestamp;
timestamp = json_object_get (entry, "timestamp");
if (GNUNET_OK != parse_abs_time (timestamp, &entry_timestamp))
TALER_TESTING_FAIL (hs->is);
if (last_timestamp.abs_value_us < entry_timestamp.abs_value_us)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"History entries are NOT"
" sorted from younger to older\n");
TALER_TESTING_interpreter_fail (hs->is);
return;
}
last_timestamp = entry_timestamp;
}
}
TALER_TESTING_interpreter_next (hs->is);
}
/**
* Free the state for a "history" CMD, and possibly cancel
* any pending operation thereof.
*
* @param cls closure
* @param cmd command being freed now.
*/
static void
history_cleanup (void *cls,
const struct TALER_TESTING_Command *cmd)
{
struct HistoryState *hs = cls;
if (NULL != hs->ho)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"/history operation did not complete\n");
TALER_MERCHANT_history_cancel (hs->ho);
}
GNUNET_free (hs);
}
/**
* Run a "history" CMD.
*
* @param cls closure.
* @param cmd current command.
* @param is interpreter state.
*/
static void
history_run (void *cls,
const struct TALER_TESTING_Command *cmd,
struct TALER_TESTING_Interpreter *is)
{
struct HistoryState *hs = cls;
hs->is = is;
if (0 == hs->time.abs_value_us)
{
hs->time = GNUNET_TIME_absolute_add
(GNUNET_TIME_absolute_get (),
GNUNET_TIME_UNIT_HOURS);
GNUNET_TIME_round_abs (&hs->time);
}
switch (hs->use_default_start)
{
case GNUNET_YES:
hs->ho = TALER_MERCHANT_history_default_start
(is->ctx,
hs->merchant_url,
"default",
hs->nrows,
hs->time,
&history_cb,
hs);
break;
case GNUNET_NO:
hs->ho = TALER_MERCHANT_history (is->ctx,
hs->merchant_url,
"default",
hs->start,
hs->nrows,
hs->time,
&history_cb,
hs);
break;
default:
TALER_LOG_ERROR ("Bad value for 'use_default_start'\n");
TALER_TESTING_FAIL (is);
}
if (NULL == hs->ho)
TALER_TESTING_FAIL (is);
}
/**
* Make a "history" command.
*
* @param label command label.
* @param merchant_url base URL of the merchant serving the
* request.
* @param ctx CURL context.
* @param http_status expected HTTP response code
* @param time limit towards the past for the history
* records we want returned.
* @param nresult how many results are expected
* @param start first row id we want in the result.
* @param use_default_start if GNUNET_YES, then it will
* use the API call that requests /history omitting
* the 'start' argument. This makes easier to test
* the server default behaviour.
* @param nrows how many row we want to receive, at most.
*/
static struct TALER_TESTING_Command
cmd_history2 (const char *label,
const char *merchant_url,
unsigned int http_status,
struct GNUNET_TIME_Absolute time,
unsigned int nresult,
unsigned long long start,
int use_default_start,
long long nrows)
{
struct HistoryState *hs;
hs = GNUNET_new (struct HistoryState);
hs->http_status = http_status;
hs->time = time;
hs->nresult = nresult;
hs->start = start;
hs->nrows = nrows;
hs->merchant_url = merchant_url;
hs->use_default_start = use_default_start;
struct TALER_TESTING_Command cmd = {
.cls = hs,
.label = label,
.run = &history_run,
.cleanup = &history_cleanup
};
return cmd;
}
/**
* Make a "history" command.
*
* @param label command label.
* @param merchant_url base URL of the merchant serving the
* request.
* @param ctx CURL context.
* @param http_status expected HTTP response code
* @param time limit towards the past for the history
* records we want returned.
* @param nresult how many results are expected
* @param nrows how many row we want to receive, at most.
*/
struct TALER_TESTING_Command
TALER_TESTING_cmd_history_default_start
(const char *label,
const char *merchant_url,
unsigned int http_status,
struct GNUNET_TIME_Absolute time,
unsigned int nresult,
long long nrows)
{
return cmd_history2 (label,
merchant_url,
http_status,
time,
nresult,
-1, /* ignored */
GNUNET_YES,
nrows);
}
/**
* Make a "history" command.
*
* @param label command label.
* @param merchant_url base URL of the merchant serving the
* request.
* @param ctx CURL context.
* @param http_status expected HTTP response code
* @param time limit towards the past for the history
* records we want returned.
* @param nresult how many results are expected
* @param start first row id we want in the result.
* @param nrows how many row we want to receive, at most.
*/
struct TALER_TESTING_Command
TALER_TESTING_cmd_history (const char *label,
const char *merchant_url,
unsigned int http_status,
struct GNUNET_TIME_Absolute time,
unsigned int nresult,
unsigned long long start,
long long nrows)
{
return cmd_history2 (label,
merchant_url,
http_status,
time,
nresult,
start,
GNUNET_NO,
nrows);
}
/* end of testing_api_cmd_history.c */
|