aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_age_withdraw.c
blob: 56e65a990ad627d2ff694c7dffc4df657e7466c1 (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
/*
  This file is part of TALER
  Copyright (C) 2023 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 testing/testing_api_cmd_age_withdraw.c
 * @brief implements the age-withdraw command
 * @author Özgür Kesim
 */

#include "platform.h"
#include "taler_json_lib.h"
#include <microhttpd.h>
#include <gnunet/gnunet_curl_lib.h>
#include "taler_signatures.h"
#include "taler_extensions.h"
#include "taler_testing_lib.h"

/**
 * State for a "age withdraw" CMD:
 */

struct AgeWithdrawState
{
  /*
   * Which reserve should we withdraw from?
   */
  const char *reserve_reference;

  /**
   * Expected HTTP response code to the request.
   */
  unsigned int expected_response_code;

  /**
   * The maximum age we commit to
   */
  uint8_t max_age;

  /**
   * Number of coins to withdraw
   */
  size_t num_coins;
};


struct TALER_TESTING_Command
TALER_TESTING_cmd_age_withdraw (const char *label,
                                const char *reserve_reference,
                                uint8_t max_age,
                                unsigned int expected_response_code,
                                const char *amount,
                                ...)
{
  struct AgeWithdrawState *aws;
  unsigned int cnt;
  va_list ap;

  aws = GNUNET_new (struct AgeWithdrawState);
  aws->max_age = max_age;
  aws->reserve_reference = reserve_reference;
  aws->expected_response_code = expected_response_code;

  cnt = 1;
  va_start (ap, amount);
  while (NULL != (va_arg (ap, const char *)))
    cnt++;
  aws->num_coins = cnt;
  aws->coins = GNUNET_new_array (cnt,
                                 struct CoinState);
  va_end (ap);
  va_start (ap, amount);
  for (unsigned int i = 0; i<ws->num_coins; i++)
  {
    struct CoinState *cs = &ws->coins[i];

    if (0 < age)
    {
      struct TALER_AgeCommitmentProof *acp;
      struct TALER_AgeCommitmentHash *hac;
      struct GNUNET_HashCode seed;
      struct TALER_AgeMask mask;

      acp = GNUNET_new (struct TALER_AgeCommitmentProof);
      hac = GNUNET_new (struct TALER_AgeCommitmentHash);
      mask = TALER_extensions_get_age_restriction_mask ();
      GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
                                  &seed,
                                  sizeof(seed));

      if (GNUNET_OK !=
          TALER_age_restriction_commit (
            &mask,
            age,
            &seed,
            acp))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "Failed to generate age commitment for age %d at %s\n",
                    age,
                    label);
        GNUNET_assert (0);
      }

      TALER_age_commitment_hash (&acp->commitment,
                                 hac);
      cs->age_commitment_proof = acp;
      cs->h_age_commitment = hac;
    }

    if (GNUNET_OK !=
        TALER_string_to_amount (amount,
                                &cs->amount))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Failed to parse amount `%s' at %s\n",
                  amount,
                  label);
      GNUNET_assert (0);
    }
    /* move on to next vararg! */
    amount = va_arg (ap, const char *);
  }
  GNUNET_assert (NULL == amount);
  va_end (ap);

  {
    struct TALER_TESTING_Command cmd = {
      .cls = ws,
      .label = label,
      .run = &age_withdraw_run,
      .cleanup = &age_withdraw_cleanup,
      .traits = &age_withdraw_traits
    };

    return cmd;
  }
}


/* end of testing_api_cmd_age_withdraw.c */