summaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_policy_lookup.c
blob: 6608d9001865f5f2605857811693f2229dea806c (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
/*
  This file is part of Anastasis
  Copyright (C) 2020 Taler Systems SA

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser 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 General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file testing/testing_api_cmd_policy_lookup.c
 * @brief command to execute the anastasis backend service.
 * @author Dennis Neufeld
 * @author Dominik Meister
 */

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


/**
 * State for a "policy lookup" CMD.
 */
struct PolicyLookupState
{
  /**
   * The interpreter state.
   */
  struct TALER_TESTING_Interpreter *is;

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

  /**
   * Hash of the upload (all zeros if there was no upload).
   */
  const struct GNUNET_HashCode *upload_hash;

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

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

  /**
   * Reference to upload command we expect to lookup.
   */
  const char *upload_reference;

  /**
   * The /policy GET operation handle.
   */
  struct ANASTASIS_PolicyLookupOperation *plo;
};


/**
 * Function called with the results of a #ANASTASIS_policy_lookup().
 *
 * @param cls closure
 * @param http_status HTTP status of the request
 * @param dd details about the lookup operation
 */
static void
policy_lookup_cb (void *cls,
                  unsigned int http_status,
                  const struct ANASTASIS_DownloadDetails *dd)
{
  struct PolicyLookupState *pls = cls;

  pls->plo = NULL;
  if (http_status != pls->http_status)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u to command %s in %s:%u\n",
                http_status,
                pls->is->commands[pls->is->ip].label,
                __FILE__,
                __LINE__);
    TALER_TESTING_interpreter_fail (pls->is);
    return;
  }
  if (NULL != pls->upload_reference)
  {
    if ( (MHD_HTTP_OK == http_status) &&
         (0 != GNUNET_memcmp (&dd->curr_policy_hash,
                              pls->upload_hash)) )
    {
      GNUNET_break (0);
      TALER_TESTING_interpreter_fail (pls->is);
      return;
    }
  }
  TALER_TESTING_interpreter_next (pls->is);
}


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

  pls->is = is;
  if (NULL != pls->upload_reference)
  {
    const struct TALER_TESTING_Command *upload_cmd;
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *anastasis_pub;

    upload_cmd = TALER_TESTING_interpreter_lookup_command
                   (is,
                   pls->upload_reference);
    if (NULL == upload_cmd)
    {
      GNUNET_break (0);
      TALER_TESTING_interpreter_fail (pls->is);
      return;
    }
    if (GNUNET_OK !=
        ANASTASIS_TESTING_get_trait_hash (upload_cmd,
                                          ANASTASIS_TESTING_TRAIT_HASH_CURRENT,
                                          &pls->upload_hash))
    {
      GNUNET_break (0);
      TALER_TESTING_interpreter_fail (pls->is);
      return;
    }
    if (GNUNET_OK !=
        ANASTASIS_TESTING_get_trait_account_pub (upload_cmd,
                                                 0,
                                                 &anastasis_pub))
    {
      GNUNET_break (0);
      TALER_TESTING_interpreter_fail (pls->is);
      return;
    }
    pls->anastasis_pub = *anastasis_pub;
  }
  pls->plo = ANASTASIS_policy_lookup (is->ctx,
                                      pls->anastasis_url,
                                      &pls->anastasis_pub,
                                      &policy_lookup_cb,
                                      pls);
  if (NULL == pls->plo)
  {
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (pls->is);
    return;
  }
}


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

  if (NULL != pls->plo)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Command '%s' did not complete (policy lookup)\n",
                cmd->label);
    ANASTASIS_policy_lookup_cancel (pls->plo);
    pls->plo = NULL;
  }
  GNUNET_free (pls);
}


struct TALER_TESTING_Command
ANASTASIS_TESTING_cmd_policy_lookup (const char *label,
                                     const char *anastasis_url,
                                     unsigned int http_status,
                                     const char *upload_ref)
{
  struct PolicyLookupState *pls;

  GNUNET_assert (NULL != upload_ref);
  pls = GNUNET_new (struct PolicyLookupState);
  pls->http_status = http_status;
  pls->anastasis_url = anastasis_url;
  pls->upload_reference = upload_ref;
  {
    struct TALER_TESTING_Command cmd = {
      .cls = pls,
      .label = label,
      .run = &policy_lookup_run,
      .cleanup = &policy_lookup_cleanup
    };

    return cmd;
  }
}


struct TALER_TESTING_Command
ANASTASIS_TESTING_cmd_policy_nx (const char *label,
                                 const char *anastasis_url)
{
  struct PolicyLookupState *pls;
  struct GNUNET_CRYPTO_EddsaPrivateKey priv;

  pls = GNUNET_new (struct PolicyLookupState);
  pls->http_status = MHD_HTTP_NOT_FOUND;
  pls->anastasis_url = anastasis_url;
  GNUNET_CRYPTO_eddsa_key_create (&priv);
  GNUNET_CRYPTO_eddsa_key_get_public (&priv,
                                      &pls->anastasis_pub.pub);
  {
    struct TALER_TESTING_Command cmd = {
      .cls = pls,
      .label = label,
      .run = &policy_lookup_run,
      .cleanup = &policy_lookup_cleanup
    };

    return cmd;
  }
}