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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
/*
This file is part of TALER
(C) 2016-2020 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 exchange/test_taler_exchange_aggregator.c
* @brief Tests for taler-exchange-aggregator logic
* @author Christian Grothoff <christian@grothoff.org>
* @author Marcello Stanisci
*/
#include "platform.h"
#include "taler_util.h"
#include <gnunet/gnunet_json_lib.h>
#include "taler_json_lib.h"
#include "taler_exchangedb_lib.h"
#include <microhttpd.h>
#include "taler_fakebank_lib.h"
#include "taler_testing_lib.h"
/**
* Helper structure to keep exchange configuration values.
*/
static struct TALER_TESTING_ExchangeConfiguration ec;
/**
* Bank configuration data.
*/
static struct TALER_TESTING_BankConfiguration bc;
/**
* Contains plugin and session.
*/
static struct TALER_TESTING_DatabaseConnection dbc;
/**
* Return value from main().
*/
static int result;
/**
* Name of the configuration file to use.
*/
static char *config_filename;
#define USER42_ACCOUNT "42"
/**
* @return GNUNET_NO if database could not be prepared,
* otherwise GNUNET_OK
*/
static int
prepare_database (void *cls,
const struct GNUNET_CONFIGURATION_Handle *cfg)
{
// connect to the database.
dbc.plugin = TALER_EXCHANGEDB_plugin_load (cfg);
if (NULL == dbc.plugin)
{
GNUNET_break (0);
result = 77;
return GNUNET_NO;
}
if (GNUNET_OK !=
dbc.plugin->create_tables (dbc.plugin->cls))
{
GNUNET_break (0);
TALER_EXCHANGEDB_plugin_unload (dbc.plugin);
dbc.plugin = NULL;
result = 77;
return GNUNET_NO;
}
dbc.session = dbc.plugin->get_session (dbc.plugin->cls);
GNUNET_assert (NULL != dbc.session);
return GNUNET_OK;
}
/**
* Collects all the tests.
*/
static void
run (void *cls,
struct TALER_TESTING_Interpreter *is)
{
struct TALER_TESTING_Command all[] = {
// check no aggregation happens on a empty database
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-on-empty-db",
config_filename),
TALER_TESTING_cmd_check_bank_empty ("expect-empty-transactions-on-start"),
// check aggregation happens on the simplest case:
// one deposit into the database.
TALER_TESTING_cmd_insert_deposit ("do-deposit-1",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:1",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-on-deposit-1",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-1",
ec.exchange_url,
"EUR:0.89",
bc.exchange_payto,
bc.user42_payto),
TALER_TESTING_cmd_check_bank_empty ("expect-empty-transactions-after-1"),
// check aggregation accumulates well.
TALER_TESTING_cmd_insert_deposit ("do-deposit-2a",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:1",
"EUR:0.1"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-2b",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:1",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-2",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-2",
ec.exchange_url,
"EUR:1.79",
bc.exchange_payto,
bc.user42_payto),
TALER_TESTING_cmd_check_bank_empty ("expect-empty-transactions-after-2"),
// check that different merchants stem different aggregations.
TALER_TESTING_cmd_insert_deposit ("do-deposit-3a",
&dbc,
"bob",
"4",
GNUNET_TIME_UNIT_ZERO,
"EUR:1",
"EUR:0.1"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-3b",
&dbc,
"bob",
"5",
GNUNET_TIME_UNIT_ZERO,
"EUR:1",
"EUR:0.1"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-3c",
&dbc,
"alice",
"4",
GNUNET_TIME_UNIT_ZERO,
"EUR:1",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-3",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-3a",
ec.exchange_url,
"EUR:0.89",
bc.exchange_payto,
"payto://x-taler-bank/localhost/4"),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-3b",
ec.exchange_url,
"EUR:0.89",
bc.exchange_payto,
"payto://x-taler-bank/localhost/4"),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-3c",
ec.exchange_url,
"EUR:0.89",
bc.exchange_payto,
"payto://x-taler-bank/localhost/5"),
TALER_TESTING_cmd_check_bank_empty ("expect-empty-transactions-after-3"),
// checking that aggregator waits for the deadline.
TALER_TESTING_cmd_insert_deposit ("do-deposit-4a",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
5),
"EUR:0.2",
"EUR:0.1"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-4b",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
5),
"EUR:0.2",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-4-early",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-4-fast"),
TALER_TESTING_cmd_sleep ("wait (5s)", 5),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-4-delayed",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-4",
ec.exchange_url,
"EUR:0.19",
bc.exchange_payto,
bc.user42_payto),
// test picking all deposits at earliest deadline
TALER_TESTING_cmd_insert_deposit ("do-deposit-5a",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
10),
"EUR:0.2",
"EUR:0.1"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-5b",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
5),
"EUR:0.2",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-5-early",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-5-early"),
TALER_TESTING_cmd_sleep ("wait (5s)", 5),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-5-delayed",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-5",
ec.exchange_url,
"EUR:0.19",
bc.exchange_payto,
bc.user42_payto),
// Test NEVER running 'tiny' unless they make up minimum unit
TALER_TESTING_cmd_insert_deposit ("do-deposit-6a",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.102",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-6a-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-6a-tiny"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-6b",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.102",
"EUR:0.1"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-6c",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.102",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-6c-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-6c-tiny"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-6d",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.102",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-6d-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-6d-tiny"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-6e",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.112",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-6e",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-6",
ec.exchange_url,
"EUR:0.01",
bc.exchange_payto,
bc.user42_payto),
// Test profiteering if wire deadline is short
TALER_TESTING_cmd_insert_deposit ("do-deposit-7a",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.109",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-7a-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-7a-tiny"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-7b",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.119",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-7-profit",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-7",
ec.exchange_url,
"EUR:0.01",
bc.exchange_payto,
bc.user42_payto),
// Now check profit was actually taken
TALER_TESTING_cmd_insert_deposit ("do-deposit-7c",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.122",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-7-loss",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-7",
ec.exchange_url,
"EUR:0.01",
bc.exchange_payto,
bc.user42_payto),
// Test that aggregation would happen fully if wire deadline is long
TALER_TESTING_cmd_insert_deposit ("do-deposit-8a",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
5),
"EUR:0.109",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-8a-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-8a-tiny"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-8b",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
5),
"EUR:0.109",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-8b-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-8b-tiny"),
// now trigger aggregate with large transaction and short deadline
TALER_TESTING_cmd_insert_deposit ("do-deposit-8c",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.122",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-8",
config_filename),
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-8",
ec.exchange_url,
"EUR:0.03",
bc.exchange_payto,
bc.user42_payto),
// Test aggregation with fees and rounding profits.
TALER_TESTING_cmd_insert_deposit ("do-deposit-9a",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
5),
"EUR:0.104",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-9a-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-9a-tiny"),
TALER_TESTING_cmd_insert_deposit ("do-deposit-9b",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS,
5),
"EUR:0.105",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-9b-tiny",
config_filename),
TALER_TESTING_cmd_check_bank_empty (
"expect-empty-transactions-after-9b-tiny"),
// now trigger aggregate with large transaction and short deadline
TALER_TESTING_cmd_insert_deposit ("do-deposit-9c",
&dbc,
"bob",
USER42_ACCOUNT,
GNUNET_TIME_UNIT_ZERO,
"EUR:0.112",
"EUR:0.1"),
TALER_TESTING_cmd_exec_aggregator ("run-aggregator-deposit-9",
config_filename),
// 0.009 + 0.009 + 0.022 - 0.001 - 0.002 - 0.008 = 0.029 => 0.02
TALER_TESTING_cmd_check_bank_transfer ("expect-deposit-9",
ec.exchange_url,
"EUR:0.01",
bc.exchange_payto,
bc.user42_payto),
TALER_TESTING_cmd_end ()
};
TALER_TESTING_run_with_fakebank (is,
all,
bc.bank_url);
}
int
main (int argc,
char *const argv[])
{
const char *plugin_name;
char *testname;
if (NULL == (plugin_name = strrchr (argv[0], (int) '-')))
{
GNUNET_break (0);
return -1;
}
plugin_name++;
(void) GNUNET_asprintf (&testname,
"test-taler-exchange-aggregator-%s",
plugin_name);
(void) GNUNET_asprintf (&config_filename,
"%s.conf",
testname);
GNUNET_log_setup ("test_taler_exchange_aggregator",
"DEBUG",
NULL);
/* these might get in the way */
unsetenv ("XDG_DATA_HOME");
unsetenv ("XDG_CONFIG_HOME");
TALER_TESTING_cleanup_files (config_filename);
if (GNUNET_OK != TALER_TESTING_prepare_exchange (config_filename,
&ec))
{
TALER_LOG_WARNING ("Could not prepare the exchange.\n");
return 77;
}
if (GNUNET_OK != TALER_TESTING_prepare_fakebank (config_filename,
"account-1",
&bc))
{
TALER_LOG_WARNING ("Could not prepare the fakebank\n");
return 77;
}
if (GNUNET_OK != GNUNET_CONFIGURATION_parse_and_run (config_filename,
&prepare_database,
NULL))
{
TALER_LOG_WARNING ("Could not prepare database for tests.\n");
return result;
}
result = TALER_TESTING_setup (&run,
NULL,
config_filename,
NULL, // no exchange process handle.
GNUNET_NO); // do not try to connect to the exchange
GNUNET_free (config_filename);
GNUNET_free (testname);
dbc.plugin->drop_tables (dbc.plugin->cls);
TALER_EXCHANGEDB_plugin_unload (dbc.plugin);
return GNUNET_OK == result ? 0 : 1;
}
/* end of test_taler_exchange_aggregator.c */
|