summaryrefslogtreecommitdiff
path: root/src/lib/testing_api_cmd_tip_pickup.c
blob: 21045a22edbce75add9658c15622b495e52186aa (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
/*
  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_tip.c
 * @brief command to test the tipping.
 * @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 /tip-pickup CMD.
 */
struct TipPickupState
{
  /**
   * Merchant base URL.
   */
  const char *merchant_url;

  /**
   * Exchange base URL.
   */
  const char *exchange_url;

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

  /**
   * Reference to a /tip/authorize CMD.  This will be used to
   * get the tip id to make the request with.
   */
  const char *authorize_reference;

  /**
   * If set to non NULL, it references another pickup CMD
   * that will provide all the data which is needed to issue
   * the request (like planchet secrets, denomination keys..).
   */
  const char *replay_reference;

  /**
   * Handle to a on-going /tip/pickup request.
   */
  struct TALER_MERCHANT_TipPickupOperation *tpo;

  /**
   * The interpreter state.
   */
  struct TALER_TESTING_Interpreter *is;

  /**
   * An array of string-defined amounts that indicates
   * which denominations are going to be used to receive
   * tips.
   */
  const char **amounts;

  /**
   * The object version of the above @a amounts.
   */
  struct TALER_Amount *amounts_obj;

  /**
   * How many coins are involved in the tipping operation.
   */
  unsigned int num_coins;

  /**
   * The array of denomination keys, in the same order of @a
   * amounts.
   */
  const struct TALER_EXCHANGE_DenomPublicKey **dks;

  /**
   * The array of planchet secrets, in the same order of @a
   * amounts.
   */
  struct TALER_PlanchetSecretsP *psa;

  /**
   * Temporary data structure of @e num_coins entries for the
   * withdraw operations.
   */
  struct WithdrawHandle *withdraws;

  /**
   * Set (by the interpreter) to an array of @a num_coins
   * signatures created from the (successful) tip operation.
   */
  struct TALER_DenominationSignature *sigs;

  /**
   * Expected Taler error code (NOTE: this is NOT the HTTP
   * response code).
   */
  enum TALER_ErrorCode expected_ec;
};


/**
 * Internal withdraw handle used when withdrawing tips.
 */
struct WithdrawHandle
{
  /**
   * Withdraw operation this handle represents.
   */
  struct TALER_EXCHANGE_WithdrawHandle *wsh;

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

  /**
   * Offset of this withdraw operation in the current
   * @e is command.
   */
  unsigned int off;

  /**
   * Internal state of the "pickup" CMD.
   */
  struct TipPickupState *tps;
};


/**
 * This callback handles the response of a withdraw operation
 * from the exchange, that is the final step in getting the tip.
 *
 * @param cls closure, a `struct WithdrawHandle *`
 * @param hr HTTP response details
 * @param sig signature over the coin, NULL on error
 */
static void
pickup_withdraw_cb (void *cls,
                    const struct TALER_EXCHANGE_HttpResponse *hr,
                    const struct TALER_DenominationSignature *sig)
{
  struct WithdrawHandle *wh = cls;
  struct TALER_TESTING_Interpreter *is = wh->is;

  struct TipPickupState *tps = wh->tps;

  wh->wsh = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Withdraw operation %u completed with %u (%d)\n",
              wh->off,
              hr->http_status,
              hr->ec);
  GNUNET_assert (wh->off < tps->num_coins);
  if ( (MHD_HTTP_OK != hr->http_status) ||
       (TALER_EC_NONE != hr->ec) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u (%d) to command %s when withdrawing\n",
                hr->http_status,
                hr->ec,
                TALER_TESTING_interpreter_get_current_label (is));
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  if (NULL == tps->sigs)
    tps->sigs = GNUNET_new_array
                  (tps->num_coins, struct TALER_DenominationSignature);

  GNUNET_assert (NULL == tps->sigs[wh->off].rsa_signature);
  tps->sigs[wh->off].rsa_signature
    = GNUNET_CRYPTO_rsa_signature_dup (sig->rsa_signature);

  for (unsigned int i = 0; i<tps->num_coins; i++)
    if (NULL != tps->withdraws[wh->off].wsh)
      return;
  /* still some ops ongoing */

  GNUNET_free (tps->withdraws);
  tps->withdraws = NULL;
  TALER_TESTING_interpreter_next (is);
}


/**
 * Callback for a /tip-pickup request, it mainly checks if
 * values returned from the backend are as expected, and if so
 * (and if the status was 200 OK) proceede with the withdrawal.
 *
 * @param cls closure
 * @param hr HTTP response
 * @param reserve_pub public key of the reserve that made the
 *        @a reserve_sigs, NULL on error
 * @param num_reserve_sigs length of the @a reserve_sigs array,
 *        0 on error
 * @param reserve_sigs array of signatures authorizing withdrawals,
 *        NULL on error
 */
static void
pickup_cb (void *cls,
           const struct TALER_MERCHANT_HttpResponse *hr,
           const struct TALER_ReservePublicKeyP *reserve_pub,
           unsigned int num_reserve_sigs,
           const struct TALER_ReserveSignatureP *reserve_sigs)
{
  struct TipPickupState *tps = cls;

  tps->tpo = NULL;
  if (hr->http_status != tps->http_status)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u (%d) to command %s\n",
                hr->http_status,
                (int) hr->ec,
                TALER_TESTING_interpreter_get_current_label (tps->is));
    TALER_TESTING_FAIL (tps->is);
  }

  if (hr->ec != tps->expected_ec)
    TALER_TESTING_FAIL (tps->is);

  /* Safe to go ahead: http status was expected.  */
  if ( (MHD_HTTP_OK != hr->http_status) ||
       (TALER_EC_NONE != hr->ec) )
  {
    TALER_TESTING_interpreter_next (tps->is);
    return;
  }
  if (num_reserve_sigs != tps->num_coins)
    TALER_TESTING_FAIL (tps->is);

  /* pickup successful, now withdraw! */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Obtained %u signatures for withdrawal from picking up a tip\n",
              num_reserve_sigs);

  GNUNET_assert (NULL == tps->withdraws);
  tps->withdraws = GNUNET_new_array (num_reserve_sigs,
                                     struct WithdrawHandle);

  for (unsigned int i = 0; i<num_reserve_sigs; i++)
  {
    struct WithdrawHandle *wh = &tps->withdraws[i];

    wh->off = i;
    wh->is = tps->is;
    wh->tps = tps;
    GNUNET_assert ( (NULL == wh->wsh) &&
                    ( (NULL == tps->sigs) ||
                      (NULL == tps->sigs[wh->off].rsa_signature) ) );
    wh->wsh = TALER_EXCHANGE_withdraw2 (tps->is->exchange,
                                        tps->dks[i],
                                        &reserve_sigs[i],
                                        reserve_pub,
                                        &tps->psa[i],
                                        &pickup_withdraw_cb,
                                        wh);
    if (NULL == wh->wsh)
      TALER_TESTING_FAIL (tps->is);
  }
  if (0 == num_reserve_sigs)
    TALER_TESTING_interpreter_next (tps->is);
}


/**
 * Run a /tip-pickup CMD.
 *
 * @param cls closure
 * @param cmd the current /tip-pickup CMD.
 * @param is interpreter state.
 */
static void
tip_pickup_run (void *cls,
                const struct TALER_TESTING_Command *cmd,
                struct TALER_TESTING_Interpreter *is)
{
  struct TipPickupState *tps = cls;
  unsigned int num_planchets;
  const struct TALER_TESTING_Command *replay_cmd;
  const struct TALER_TESTING_Command *authorize_cmd;
  const struct GNUNET_HashCode *tip_id;

  tps->is = is;
  tps->exchange_url = TALER_EXCHANGE_get_base_url (is->exchange);
  if (NULL == tps->replay_reference)
  {
    replay_cmd = NULL;

    /* Count planchets. */
    for (num_planchets = 0;
         NULL != tps->amounts[num_planchets];
         num_planchets++)
      ;
  }
  else
  {
    const unsigned int *np;

    if (NULL ==  /* looking for "parent" tip-pickup command */
        (replay_cmd
           = TALER_TESTING_interpreter_lookup_command (is,
                                                       tps->replay_reference)) )
      TALER_TESTING_FAIL (is);

    if (GNUNET_OK !=
        TALER_TESTING_get_trait_uint (replay_cmd,
                                      0,
                                      &np))
      TALER_TESTING_FAIL (is);
    num_planchets = *np;
  }

  if (NULL ==
      (authorize_cmd
         = TALER_TESTING_interpreter_lookup_command (is,
                                                     tps->authorize_reference)) )
    TALER_TESTING_FAIL (is);

  tps->num_coins = num_planchets;
  {
    struct TALER_PlanchetDetail planchets[num_planchets];

    tps->psa = GNUNET_new_array (num_planchets,
                                 struct TALER_PlanchetSecretsP);
    tps->dks = GNUNET_new_array (num_planchets,
                                 const struct TALER_EXCHANGE_DenomPublicKey *);
    tps->amounts_obj = GNUNET_new_array (num_planchets,
                                         struct TALER_Amount);

    for (unsigned int i = 0; i<num_planchets; i++)
    {
      if (NULL == replay_cmd)
      {
        GNUNET_assert (GNUNET_OK ==
                       TALER_string_to_amount (tps->amounts[i],
                                               &tps->amounts_obj[i]));
        tps->dks[i] = TALER_TESTING_find_pk (is->keys,
                                             &tps->amounts_obj[i]);
        if (NULL == tps->dks[i])
          TALER_TESTING_FAIL (is);
        TALER_planchet_setup_random (&tps->psa[i]);
      }
      else
      {
        struct TALER_PlanchetSecretsP *ps;

        if (GNUNET_OK !=
            TALER_TESTING_get_trait_denom_pub (replay_cmd,
                                               i,
                                               &tps->dks[i]))
          TALER_TESTING_FAIL (is);
        if (GNUNET_OK !=
            TALER_TESTING_get_trait_planchet_secrets (replay_cmd,
                                                      i,
                                                      &ps))
          TALER_TESTING_FAIL (is);
        tps->psa[i] = *ps;
      }

      if (GNUNET_OK !=
          TALER_planchet_prepare (&tps->dks[i]->key,
                                  &tps->psa[i],
                                  &planchets[i]))
        TALER_TESTING_FAIL (is);
    }
    if (GNUNET_OK !=
        TALER_TESTING_get_trait_tip_id (authorize_cmd,
                                        0,
                                        &tip_id))
      TALER_TESTING_FAIL (is);

    tps->tpo = TALER_MERCHANT_tip_pickup (is->ctx,
                                          tps->merchant_url,
                                          tip_id,
                                          num_planchets,
                                          planchets,
                                          &pickup_cb,
                                          tps);
    for (unsigned int i = 0; i<num_planchets; i++)
    {
      GNUNET_free (planchets[i].coin_ev);
      planchets[i].coin_ev = NULL;
      planchets[i].coin_ev_size = 0;
    }
    GNUNET_assert (NULL != tps->tpo);
  }
}


/**
 * Free a /tip-pickup CMD state, and possibly cancel a
 * pending /tip-pickup request.
 *
 * @param cls closure.
 * @param cmd current CMD to be freed.
 */
static void
tip_pickup_cleanup (void *cls,
                    const struct TALER_TESTING_Command *cmd)
{
  struct TipPickupState *tps = cls;

  GNUNET_free_non_null (tps->amounts_obj);
  GNUNET_free_non_null (tps->dks);
  GNUNET_free_non_null (tps->psa);
  GNUNET_free_non_null (tps->withdraws);
  if (NULL != tps->sigs)
  {
    for (unsigned int i = 0; i<tps->num_coins; i++)
      if (NULL != tps->sigs[i].rsa_signature)
        GNUNET_CRYPTO_rsa_signature_free (tps->sigs[i].rsa_signature);
    GNUNET_free (tps->sigs);
  }
  if (NULL != tps->tpo)
  {
    TALER_LOG_WARNING ("Tip-pickup operation did not complete\n");
    TALER_MERCHANT_tip_pickup_cancel (tps->tpo);
  }
  GNUNET_free (tps);
}


/**
 * Offers information from the /tip-pickup 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
tip_pickup_traits (void *cls,
                   const void **ret,
                   const char *trait,
                   unsigned int index)
{
  struct TipPickupState *tps = cls;
  #define NUM_TRAITS (tps->num_coins * 5) + 2
  struct TALER_TESTING_Trait traits[NUM_TRAITS];

  for (unsigned int i = 0; i<tps->num_coins; i++)
  {
    traits[i] = TALER_TESTING_make_trait_planchet_secrets (i,
                                                           &tps->psa[i]);
    traits[i + tps->num_coins] =
      TALER_TESTING_make_trait_coin_priv (i, &tps->psa[i].coin_priv);
    traits[i + (tps->num_coins * 2)] =
      TALER_TESTING_make_trait_denom_pub (i, tps->dks[i]);
    traits[i + (tps->num_coins * 3)] =
      TALER_TESTING_make_trait_denom_sig (i, &tps->sigs[i]);
    traits[i + (tps->num_coins * 4)] =
      TALER_TESTING_make_trait_amount_obj (i, &tps->amounts_obj[i]);
  }
  traits[NUM_TRAITS - 2]
    = TALER_TESTING_make_trait_url (TALER_TESTING_UT_EXCHANGE_BASE_URL,
                                    tps->exchange_url);
  traits[NUM_TRAITS - 1] = TALER_TESTING_trait_end ();
  return TALER_TESTING_get_trait (traits,
                                  ret,
                                  trait,
                                  index);
}


/**
 * Define a /tip-pickup CMD, equipped with the expected error
 * code.
 *
 * @param label the command label
 * @param merchant_url base URL of the backend which will serve
 *        the /tip-pickup request.
 * @param http_status expected HTTP response code.
 * @param authorize_reference reference to a /tip-autorize CMD
 *        that offers a tip id to pick up.
 * @param amounts array of string-defined amounts that specifies
 *        which denominations will be accepted for tipping.
 * @param exchange connection handle to the exchange that will
 *        eventually serve the withdraw operation.
 * @param ec expected Taler error code.
 */
struct TALER_TESTING_Command
TALER_TESTING_cmd_tip_pickup_with_ec (const char *label,
                                      const char *merchant_url,
                                      unsigned int http_status,
                                      const char *authorize_reference,
                                      const char **amounts,
                                      enum TALER_ErrorCode ec)
{
  struct TipPickupState *tps;

  tps = GNUNET_new (struct TipPickupState);
  tps->merchant_url = merchant_url;
  tps->authorize_reference = authorize_reference;
  tps->amounts = amounts;
  tps->http_status = http_status;
  tps->expected_ec = ec;
  {
    struct TALER_TESTING_Command cmd = {
      .cls = tps,
      .label = label,
      .run = &tip_pickup_run,
      .cleanup = &tip_pickup_cleanup,
      .traits = &tip_pickup_traits
    };

    return cmd;
  }
}


/**
 * Define a /tip-pickup CMD.
 *
 * @param label the command label
 * @param merchant_url base URL of the backend which will serve
 *        the /tip-pickup request.
 * @param http_status expected HTTP response code.
 * @param authorize_reference reference to a /tip-autorize CMD
 *        that offers a tip id to pick up.
 * @param amounts array of string-defined amounts that specifies
 *        which denominations will be accepted for tipping.
 */
struct TALER_TESTING_Command
TALER_TESTING_cmd_tip_pickup (const char *label,
                              const char *merchant_url,
                              unsigned int http_status,
                              const char *authorize_reference,
                              const char **amounts)
{
  struct TipPickupState *tps;

  tps = GNUNET_new (struct TipPickupState);
  tps->merchant_url = merchant_url;
  tps->authorize_reference = authorize_reference;
  tps->amounts = amounts;
  tps->http_status = http_status;
  {
    struct TALER_TESTING_Command cmd = {
      .cls = tps,
      .label = label,
      .run = &tip_pickup_run,
      .cleanup = &tip_pickup_cleanup,
      .traits = &tip_pickup_traits
    };

    return cmd;
  }
}


/* end of testing_api_cmd_tip_pickup.c */