summaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_policy_store.c
blob: 4b964727080bca6b3d417e3784255156d3372842 (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
/*
  This file is part of ANASTASIS
  Copyright (C) 2014-2019 Anastasis SARL

  ANASTASIS is free software; you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation; either version 3, or
  (at your option) any later version.

  ANASTASIS 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public
  License along with ANASTASIS; see the file COPYING.  If not, see
  <http://www.gnu.org/licenses/>
*/

/**
 * @file testing/testing_api_cmd_policy_store.c
 * @brief command to execute the anastasis backend service.
 * @author Christian Grothoff
 * @author Dominik Meister
 * @author Dennis Neufeld
 */

#include "platform.h"
#include "anastasis_testing_lib.h"
#include <taler/taler_util.h>
#include <taler/taler_testing_lib.h>
#include <taler/taler_merchant_service.h>


/**
 * State for a "policy store" CMD.
 */
struct PolicyStoreState
{
  /**
   * Claim token we got back, if any. Otherwise all zeros.
   */
  struct TALER_ClaimTokenP claim_token;

  /**
   * The policy data.
   */
  const void *recovery_data;

  /**
   * Number of bytes in @e recovery_data
   */
  size_t recovery_data_size;

  /**
   * Expected status code.
   */
  unsigned int http_status;

  /**
   * Eddsa Publickey.
   */
  struct ANASTASIS_CRYPTO_AccountPublicKeyP anastasis_pub;

  /**
   * Eddsa Privatekey.
   */
  struct ANASTASIS_CRYPTO_AccountPrivateKeyP anastasis_priv;

  /**
   * Hash of uploaded data, used to verify the response.
   */
  struct GNUNET_HashCode curr_hash;

  /**
   * The /policy POST operation handle.
   */
  struct ANASTASIS_PolicyStoreOperation *pso;

  /**
   * The nonce.
   */
  struct ANASTASIS_CRYPTO_NonceP nonce;

  /**
   * URL of the anastasis backend.
   */
  const char *anastasis_url;

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

  /**
   * Previous upload, or NULL for none. Used to calculate what THIS
   * upload is based on.
   */
  const char *prev_upload;

  /**
   * Payment order ID we are to provide in the request, or zero.
   */
  struct ANASTASIS_PaymentSecretP payment_secret_request;

  /**
   * The order ID, for making the payment.
   */
  char *order_id;

  /**
   * Payment order ID we are to provide in the response, or zero.
   */
  struct ANASTASIS_PaymentSecretP payment_secret_response;

  /**
   * Options for how we are supposed to do the upload.
   */
  enum ANASTASIS_TESTING_PolicyStoreOption psopt;

  /**
   * True if @e payment_secret_request is initialized.
   */
  bool payment_secret_set;
};

/**
 * Function called with the results of an #ANASTASIS_policy_store() operation.
 *
 * @param cls closure
 * @param ud details about the upload operation
 */
static void
policy_store_cb (void *cls,
                 const struct ANASTASIS_UploadDetails *ud)
{
  struct PolicyStoreState *pss = cls;

  pss->pso = NULL;
  if (ud->http_status != pss->http_status)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u to command %s in %s:%u\n",
                ud->http_status,
                pss->is->commands[pss->is->ip].label,
                __FILE__,
                __LINE__);
    TALER_TESTING_interpreter_fail (pss->is);
    return;
  }
  switch (ud->us)
  {
  case ANASTASIS_US_SUCCESS:
    if (0 != GNUNET_memcmp (&pss->curr_hash,
                            ud->details.success.curr_backup_hash))
    {
      GNUNET_break (0);
      TALER_TESTING_interpreter_fail (pss->is);
      return;
    }
    break;
  case ANASTASIS_US_PAYMENT_REQUIRED:
    pss->payment_secret_response = ud->details.payment.ps;
    {
      struct TALER_MERCHANT_PayUriData pd;

      if (GNUNET_OK !=
          TALER_MERCHANT_parse_pay_uri (ud->details.payment.payment_request,
                                        &pd))
      {
        GNUNET_break (0);
        TALER_TESTING_interpreter_fail (pss->is);
        return;
      }
      pss->order_id = GNUNET_strdup (pd.order_id);
      if (NULL != pd.claim_token)
        pss->claim_token = *pd.claim_token;
      TALER_MERCHANT_parse_pay_uri_free (&pd);
    }
    break;
  case ANASTASIS_US_HTTP_ERROR:
    break;
  case ANASTASIS_US_CLIENT_ERROR:
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (pss->is);
    return;
  case ANASTASIS_US_SERVER_ERROR:
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (pss->is);
    return;
  default:
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (pss->is);
    return;
  }
  TALER_TESTING_interpreter_next (pss->is);
}


/**
 * Run a "policy store" CMD.
 *
 * @param cls closure.
 * @param cmd command currently being run.
 * @param is interpreter state.
 */
static void
policy_store_run (void *cls,
                  const struct TALER_TESTING_Command *cmd,
                  struct TALER_TESTING_Interpreter *is)
{
  struct PolicyStoreState *pss = cls;

  pss->is = is;
  if (NULL != pss->prev_upload)
  {
    const struct TALER_TESTING_Command *ref;

    ref = TALER_TESTING_interpreter_lookup_command (is,
                                                    pss->prev_upload);
    if (NULL == ref)
    {
      GNUNET_break (0);
      TALER_TESTING_interpreter_fail (pss->is);
      return;
    }
    {
      const struct ANASTASIS_CRYPTO_AccountPrivateKeyP *priv;

      if (GNUNET_OK !=
          ANASTASIS_TESTING_get_trait_account_priv (ref,
                                                    0,
                                                    &priv))
      {
        GNUNET_break (0);
        TALER_TESTING_interpreter_fail (pss->is);
        return;
      }
      pss->anastasis_priv = *priv;
    }
    {
      const struct ANASTASIS_CRYPTO_AccountPublicKeyP *pub;

      if (GNUNET_OK !=
          ANASTASIS_TESTING_get_trait_account_pub (ref,
                                                   0,
                                                   &pub))
      {
        GNUNET_break (0);
        TALER_TESTING_interpreter_fail (pss->is);
        return;
      }
      pss->anastasis_pub = *pub;
    }
    {
      const struct ANASTASIS_PaymentSecretP *ps;

      if (GNUNET_OK !=
          ANASTASIS_TESTING_get_trait_payment_secret (ref,
                                                      0,
                                                      &ps))
      {
        GNUNET_break (0);
        TALER_TESTING_interpreter_fail (pss->is);
        return;
      }
      pss->payment_secret_request = *ps;
      pss->payment_secret_set = true;
    }
  }
  else
  {
    GNUNET_CRYPTO_eddsa_key_create (&pss->anastasis_priv.priv);
    GNUNET_CRYPTO_eddsa_key_get_public (&pss->anastasis_priv.priv,
                                        &pss->anastasis_pub.pub);
  }

  GNUNET_CRYPTO_hash (pss->recovery_data,
                      pss->recovery_data_size,
                      &pss->curr_hash);
  pss->pso = ANASTASIS_policy_store (
    is->ctx,
    pss->anastasis_url,
    &pss->anastasis_priv,
    pss->recovery_data,
    pss->recovery_data_size,
    (0 != (ANASTASIS_TESTING_PSO_REQUEST_PAYMENT & pss->psopt)),
    pss->payment_secret_set ? &pss->payment_secret_request : NULL,
    GNUNET_TIME_UNIT_ZERO,
    &policy_store_cb,
    pss);
  if (NULL == pss->pso)
  {
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (pss->is);
    return;
  }
}


/**
 * Free the state of a "policy store" CMD, and possibly
 * cancel it if it did not complete.
 *
 * @param cls closure.
 * @param cmd command being freed.
 */
static void
policy_store_cleanup (void *cls,
                      const struct TALER_TESTING_Command *cmd)
{
  struct PolicyStoreState *pss = cls;

  if (NULL != pss->pso)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Command '%s' did not complete (policy post)\n",
                cmd->label);
    ANASTASIS_policy_store_cancel (pss->pso);
    pss->pso = NULL;
  }
  GNUNET_free (pss->order_id);
  GNUNET_free (pss);
}


/**
 * Offer internal data to other commands.
 *
 * @param cls closure
 * @param[out] ret 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
policy_store_traits (void *cls,
                     const void **ret,
                     const char *trait,
                     unsigned int index)
{
  struct PolicyStoreState *pss = cls;
  struct TALER_TESTING_Trait traits[] = {
    TALER_TESTING_make_trait_claim_token (0,
                                          &pss->claim_token),
    TALER_TESTING_make_trait_order_id (0,
                                       pss->order_id),
    ANASTASIS_TESTING_make_trait_hash (0,
                                       &pss->curr_hash),
    ANASTASIS_TESTING_make_trait_account_pub (0,
                                              &pss->anastasis_pub),
    ANASTASIS_TESTING_make_trait_account_priv (0,
                                               &pss->anastasis_priv),
    ANASTASIS_TESTING_make_trait_payment_secret (0,
                                                 &pss->payment_secret_response),
    TALER_TESTING_trait_end ()
  };

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


struct TALER_TESTING_Command
ANASTASIS_TESTING_cmd_policy_store (
  const char *label,
  const char *anastasis_url,
  const char *prev_upload,
  unsigned int http_status,
  enum ANASTASIS_TESTING_PolicyStoreOption pso,
  const void *recovery_data,
  size_t recovery_data_size)
{
  struct PolicyStoreState *pss;

  pss = GNUNET_new (struct PolicyStoreState);
  pss->recovery_data = recovery_data;
  pss->recovery_data_size = recovery_data_size;
  pss->http_status = http_status;
  pss->psopt = pso;
  pss->anastasis_url = anastasis_url;
  pss->prev_upload = prev_upload;
  {
    struct TALER_TESTING_Command cmd = {
      .cls = pss,
      .label = label,
      .run = &policy_store_run,
      .cleanup = &policy_store_cleanup,
      .traits = &policy_store_traits
    };

    return cmd;
  }
}