aboutsummaryrefslogtreecommitdiff
path: root/src/lib/testing_api_cmd_refund.c
blob: c3a50509d2c22bb21fbeffde2dd2449544785158 (plain)
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
  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_refund.c
 * @brief command to test refunds.
 * @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 "refund increase" CMD.
 */
struct RefundIncreaseState
{
  /**
   * Operation handle for a POST /refund request.
   */
  struct TALER_MERCHANT_RefundIncreaseOperation *rio;

  /**
   * Base URL of the merchant serving the request.
   */
  const char *merchant_url;

  /**
   * CURL context.
   */
  struct GNUNET_CURL_Context *ctx;

  /**
   * Order id of the contract to refund.
   */
  const char *order_id;

  /**
   * The amount to refund.
   */
  const char *refund_amount;

  /**
   * Refund fee.
   */
  const char *refund_fee;

  /**
   * Human-readable justification for the refund.
   */
  const char *reason;

  /**
   * Interpreter state.
   */
  struct TALER_TESTING_Interpreter *is;

  /**
   * Expected HTTP response code.
   */
  unsigned int http_code;
};


/**
 * State for a "refund lookup" CMD.
 */
struct RefundLookupState
{
  /**
   * Operation handle for a GET /public/refund request.
   */
  struct TALER_MERCHANT_RefundLookupOperation *rlo;

  /**
   * Base URL of the merchant serving the request.
   */
  const char *merchant_url;

  /**
   * CURL context.
   */
  struct GNUNET_CURL_Context *ctx;

  /**
   * Order id to look up.
   */
  const char *order_id;

  /**
   * Reference to a "pay" CMD, used to double-check if
   * refunded coins were actually spent:
   */
  const char *pay_reference;

  /**
   * Reference to a "refund increase" CMD that offer
   * the expected amount to be refunded; can be NULL.
   */
  const char *increase_reference;

  /**
   * Expected HTTP response code.
   */
  unsigned int http_code;

  /**
   * Interpreter state.
   */
  struct TALER_TESTING_Interpreter *is;

  /**
   * Explicit amount to be refunded, must be defined if @a
   * increase_reference is NULL.
   */
  const char *refund_amount;
};


/**
 * Free the state of a "refund increase" CMD, and
 * possibly cancel a pending "refund increase" operation.
 *
 * @param cls closure
 * @param cmd command currently being freed.
 */
static void
refund_increase_cleanup (void *cls,
                         const struct TALER_TESTING_Command *cmd)
{
  struct RefundIncreaseState *ris = cls;

  if (NULL != ris->rio)
  {
    TALER_LOG_WARNING ("Refund-increase operation"
                       " did not complete\n");
    TALER_MERCHANT_refund_increase_cancel (ris->rio);
  }
  GNUNET_free (ris);
}


/**
 * Free the state of a "refund lookup" CMD, and
 * possibly cancel a pending "refund lookup" operation.
 *
 * @param cls closure
 * @param cmd command currently being freed.
 */
static void
refund_lookup_cleanup (void *cls,
                       const struct TALER_TESTING_Command *cmd)
{
  /* FIXME: make sure no other data must be free'd */
  struct RefundLookupState *rls = cls;

  if (NULL != rls->rlo)
  {
    TALER_LOG_WARNING ("Refund-lookup operation"
                       " did not complete\n");
    TALER_MERCHANT_refund_lookup_cancel (rls->rlo);
  }
  GNUNET_free (rls);
}

/**
 * Process POST /refund (increase) response; just checking
 * if the HTTP response code is the one expected.
 *
 * @param cls closure
 * @param http_status HTTP status code
 * @param ec taler-specific error object
 * @param obj response body; is NULL on success.
 */
static void
refund_increase_cb (void *cls,
                    unsigned int http_status,
                    enum TALER_ErrorCode ec,
                    const json_t *obj)
{
  struct RefundIncreaseState *ris = cls;

  ris->rio = NULL;
  if (ris->http_code != http_status)
    TALER_TESTING_FAIL (ris->is);

  TALER_TESTING_interpreter_next (ris->is);
}


/**
 * Run the "refund increase" CMD.
 *
 * @param cls closure.
 * @param cmd command currently being run.
 * @param is the interpreter state.
 */
static void
refund_increase_run (void *cls,
                     const struct TALER_TESTING_Command *cmd,
                     struct TALER_TESTING_Interpreter *is)
{
  struct RefundIncreaseState *ris = cls;
  struct TALER_Amount refund_amount;

  ris->is = is;
  if (GNUNET_OK != TALER_string_to_amount (ris->refund_amount,
                                           &refund_amount))
    TALER_TESTING_FAIL (is);
  ris->rio = TALER_MERCHANT_refund_increase (ris->ctx,
                                             ris->merchant_url,
                                             ris->order_id,
                                             &refund_amount,
                                             ris->reason,
                                             "default",
                                             &refund_increase_cb,
                                             ris);
  GNUNET_assert (NULL != ris->rio);
}

/**
 * Callback that frees all the elements in the hashmap
 *
 * @param cls closure, NULL
 * @param key current key
 * @param value a `struct TALER_Amount`
 *
 * @return always #GNUNET_YES (continue to iterate)
 */
static int
hashmap_free (void *cls,
              const struct GNUNET_HashCode *key,
              void *value)
{
  struct TALER_Amount *refund_amount = value;

  GNUNET_free (refund_amount);
  return GNUNET_YES;
}


/**
 * Process "GET /public/refund" (lookup) response;
 * mainly checking if the refunded amount matches the
 * expectation.
 *
 * @param cls closure
 * @param http_status HTTP status code
 * @param ec taler-specific error object
 * @param obj response body; is NULL on error.
 */
static void
refund_lookup_cb (void *cls,
                  unsigned int http_status,
                  enum TALER_ErrorCode ec,
                  const json_t *obj)
{
  struct RefundLookupState *rls = cls;
  struct GNUNET_CONTAINER_MultiHashMap *map;
  size_t index;
  json_t *elem;
  const char *error_name;
  unsigned int error_line;
  struct GNUNET_HashCode h_coin_pub;
  const char *coin_reference;
  char *coin_reference_dup;
  const char *icoin_reference;
  const struct TALER_TESTING_Command *pay_cmd;
  const struct TALER_TESTING_Command *icoin_cmd;
  const struct TALER_TESTING_Command *increase_cmd;
  const char *refund_amount;
  struct TALER_Amount acc;
  struct TALER_Amount ra;
  const json_t *arr;

  rls->rlo = NULL;
  if (rls->http_code != http_status)
    TALER_TESTING_FAIL (rls->is);

  map = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
  arr = json_object_get (obj, "refund_permissions");
  if (NULL == arr)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Tolerating a refund permission not found\n");
    TALER_TESTING_interpreter_next (rls->is);
    return;
  }

  /* Put in array every refunded coin.  */
  json_array_foreach (arr, index, elem)
  {
    struct TALER_CoinSpendPublicKeyP coin_pub;
    struct TALER_Amount *irefund_amount = GNUNET_new
      (struct TALER_Amount);
    struct GNUNET_JSON_Specification spec[] = {
      GNUNET_JSON_spec_fixed_auto ("coin_pub", &coin_pub),
      TALER_JSON_spec_amount ("refund_amount", irefund_amount),
      GNUNET_JSON_spec_end ()
    };

    GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (elem,
			                           spec,
                                                   &error_name,
                                                   &error_line));
    GNUNET_CRYPTO_hash (&coin_pub,
			sizeof (struct TALER_CoinSpendPublicKeyP),
			&h_coin_pub);
    GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put
      (map,
       &h_coin_pub, // which
       irefund_amount, // how much
       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  };

  /* Compare spent coins with refunded, and if they match,
   * increase an accumulator.  */
  if ( NULL ==
     ( pay_cmd = TALER_TESTING_interpreter_lookup_command
       (rls->is, rls->pay_reference)))
    TALER_TESTING_FAIL (rls->is);

  if (GNUNET_OK != TALER_TESTING_get_trait_coin_reference
      (pay_cmd, 0, &coin_reference))
    TALER_TESTING_FAIL (rls->is);

  GNUNET_assert (GNUNET_OK == TALER_amount_get_zero ("EUR",
                                                     &acc));
  coin_reference_dup = GNUNET_strdup (coin_reference);
  for (icoin_reference = strtok (coin_reference_dup, ";");
       NULL != icoin_reference;
       icoin_reference = strtok (NULL, ";"))
  {
    struct TALER_CoinSpendPrivateKeyP *icoin_priv;
    struct TALER_CoinSpendPublicKeyP icoin_pub;
    struct GNUNET_HashCode h_icoin_pub;
    struct TALER_Amount *iamount;

    if ( NULL ==
       ( icoin_cmd = TALER_TESTING_interpreter_lookup_command
         (rls->is, icoin_reference)) )
    {
      GNUNET_break (0);
      TALER_LOG_ERROR ("Bad reference `%s'\n",
                       icoin_reference); 
      TALER_TESTING_interpreter_fail (rls->is);
      return;
    }

   if (GNUNET_OK != TALER_TESTING_get_trait_coin_priv
      (icoin_cmd, 0, &icoin_priv))
   {
     GNUNET_break (0);
     TALER_LOG_ERROR ("Command `%s' failed to give coin"
                      " priv trait\n",
                      icoin_reference); 
     TALER_TESTING_interpreter_fail (rls->is);
     return;
    }
     
    GNUNET_CRYPTO_eddsa_key_get_public (&icoin_priv->eddsa_priv,
                                        &icoin_pub.eddsa_pub);
    GNUNET_CRYPTO_hash (&icoin_pub,
                        sizeof (struct TALER_CoinSpendPublicKeyP),
                        &h_icoin_pub);

    iamount = GNUNET_CONTAINER_multihashmap_get
      (map, &h_icoin_pub);

    /* Can be NULL: not all coins are involved in refund */
    if (NULL == iamount)
      continue;

    GNUNET_assert (GNUNET_OK == TALER_amount_add (&acc,
                                                  &acc,
                                                  iamount));
  }

  GNUNET_free (coin_reference_dup);
  
  if (NULL !=
    (increase_cmd = TALER_TESTING_interpreter_lookup_command
      (rls->is, rls->increase_reference)))
  {
    if (GNUNET_OK != TALER_TESTING_get_trait_amount
        (increase_cmd, 0, &refund_amount))
      TALER_TESTING_FAIL (rls->is);

    if (GNUNET_OK != TALER_string_to_amount
        (refund_amount, &ra))
      TALER_TESTING_FAIL (rls->is);
  }
  else
  {
    GNUNET_assert (NULL != rls->refund_amount);

    if (GNUNET_OK != TALER_string_to_amount
        (rls->refund_amount, &ra))
      TALER_TESTING_FAIL (rls->is);
  }

  GNUNET_CONTAINER_multihashmap_iterate (map,
                                         &hashmap_free,
                                         NULL);
  GNUNET_CONTAINER_multihashmap_destroy (map);

  /* Check that what the backend claims to have been refunded
   * actually matches _our_ refund expectation.  */
  if (0 != TALER_amount_cmp (&acc,
                             &ra))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Incomplete refund: expected '%s', got '%s'\n",
                TALER_amount_to_string (&ra),
                TALER_amount_to_string (&acc));
    TALER_TESTING_interpreter_fail (rls->is);
    return;
  }

  TALER_TESTING_interpreter_next (rls->is);
}


/**
 * Run the "refund lookup" CMD.
 *
 * @param cls closure.
 * @param cmd command being currently run.
 * @param is interpreter state.
 */
static void
refund_lookup_run (void *cls,
                   const struct TALER_TESTING_Command *cmd,
                   struct TALER_TESTING_Interpreter *is)
{
  struct RefundLookupState *rls = cls;
  
  rls->is = is;
  rls->rlo = TALER_MERCHANT_refund_lookup (rls->ctx,
                                           rls->merchant_url,
                                           rls->order_id,
                                           "default",
                                           &refund_lookup_cb,
                                           rls);
  GNUNET_assert (NULL != rls->rlo);
}


/**
 * Offer internal data from the "refund increase" CMD
 * state to other commands.
 *
 * @param cls closure
 * @param ret[out] result (could be anything)
 * @param trait name of the trait
 * @param index index number of the object to extract.
 * @return #GNUNET_OK on success
 */
static int
refund_increase_traits (void *cls,
                        void **ret,
                        const char *trait,
                        unsigned int index)
{
  struct RefundIncreaseState *ris = cls;
  
  struct TALER_TESTING_Trait traits[] = {
    TALER_TESTING_make_trait_amount (0, ris->refund_amount),
    TALER_TESTING_trait_end ()
  };

  return TALER_TESTING_get_trait (traits,
                                  ret,
                                  trait,
                                  index);

  return GNUNET_SYSERR;
}

/**
 * Define a "refund increase" CMD.
 *
 * @param label command label.
 * @param merchant_url base URL of the backend serving the
 *        "refund increase" request.
 * @param ctx CURL context.
 * @param reason refund justification, human-readable.
 * @param order_id order id of the contract to refund.
 * @param refund_amount amount to be refund-increased.
 * @param refund_fee refund fee.
 * @param http_code expected HTTP response code.
 *
 * @return the command.
 */
struct TALER_TESTING_Command
TALER_TESTING_cmd_refund_increase
  (const char *label,
   const char *merchant_url,
   struct GNUNET_CURL_Context *ctx,
   const char *reason,
   const char *order_id,
   const char *refund_amount,
   const char *refund_fee,
   unsigned int http_code)
{
  struct RefundIncreaseState *ris;
  struct TALER_TESTING_Command cmd;

  ris = GNUNET_new (struct RefundIncreaseState);
  ris->merchant_url = merchant_url;
  ris->ctx = ctx;
  ris->order_id = order_id;
  ris->refund_amount = refund_amount;
  ris->refund_fee = refund_fee;
  ris->reason = reason;
  ris->http_code = http_code;

  cmd.cls = ris;
  cmd.label = label;
  cmd.run = &refund_increase_run;
  cmd.cleanup = &refund_increase_cleanup;
  cmd.traits = &refund_increase_traits;

  return cmd;
}

/**
 * Define a "refund lookup" CMD.
 *
 * @param label command label.
 * @param merchant_url base URL of the merchant serving the
 *        "refund lookup" request.
 * @param ctx CURL context.
 * @param increase_reference reference to a "refund increase" CMD
 *        that will offer the amount to check the looked up refund
 *        against.  Must NOT be NULL.
 * @param pay_reference reference to the "pay" CMD whose coins got
 *        refunded.  It is used to double-check if the refunded
 *        coins were actually spent in the first place.
 * @param order_id order id whose refund status is to be looked up.
 * @param http_code expected HTTP response code.
 *
 * @return the command.
 */
struct TALER_TESTING_Command
TALER_TESTING_cmd_refund_lookup
  (const char *label,
   const char *merchant_url,
   struct GNUNET_CURL_Context *ctx,
   const char *increase_reference,
   const char *pay_reference,
   const char *order_id,
   unsigned int http_code)
{
  struct RefundLookupState *rls;
  struct TALER_TESTING_Command cmd;

  rls = GNUNET_new (struct RefundLookupState);
  rls->merchant_url = merchant_url;
  rls->ctx = ctx;
  rls->order_id = order_id;
  rls->pay_reference = pay_reference;
  rls->increase_reference = increase_reference;
  rls->http_code = http_code;

  cmd.cls = rls;
  cmd.label = label;
  cmd.run = &refund_lookup_run;
  cmd.cleanup = &refund_lookup_cleanup;

  return cmd;
}


/**
 * Define a "refund lookup" CMD, equipped with a expected refund
 * amount.
 *
 * @param label command label.
 * @param merchant_url base URL of the merchant serving the
 *        "refund lookup" request.
 * @param ctx CURL context.
 * @param increase_reference reference to a "refund increase" CMD
 *        that will offer the amount to check the looked up refund
 *        against.  Can be NULL, takes precedence over @a
 *        refund_amount.
 * @param pay_reference reference to the "pay" CMD whose coins got
 *        refunded.  It is used to double-check if the refunded
 *        coins were actually spent in the first place.
 * @param order_id order id whose refund status is to be looked up.
 * @param http_code expected HTTP response code.
 * @param refund_amount expected refund amount.  Must be defined
 *        if @a increase_reference is NULL.
 *
 * @return the command.
 */
struct TALER_TESTING_Command
TALER_TESTING_cmd_refund_lookup_with_amount
  (const char *label,
   const char *merchant_url,
   struct GNUNET_CURL_Context *ctx,
   const char *increase_reference,
   const char *pay_reference,
   const char *order_id,
   unsigned int http_code,
   const char *refund_amount)
{
  struct RefundLookupState *rls;
  struct TALER_TESTING_Command cmd;

  rls = GNUNET_new (struct RefundLookupState);
  rls->merchant_url = merchant_url;
  rls->ctx = ctx;
  rls->order_id = order_id;
  rls->pay_reference = pay_reference;
  rls->increase_reference = increase_reference;
  rls->http_code = http_code;
  rls->refund_amount = refund_amount;

  cmd.cls = rls;
  cmd.label = label;
  cmd.run = &refund_lookup_run;
  cmd.cleanup = &refund_lookup_cleanup;

  return cmd;
}

/* end of testing_api_cmd_refund.c */