aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_checkserver.c
blob: 5b10b1fc80225ddca4e34bf4114edb7fb79d6d8d (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
/*
  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_checkserver.c
 * @brief Implement a CMD to run an Checkserver service for faking the legitimation service
 * @author Priscilla HUANG
 */
#include "platform.h"
#include "taler/taler_json_lib.h"
#include <gnunet/gnunet_curl_lib.h>
#include "taler/taler_testing_lib.h"
#include "taler/taler_mhd_lib.h"
#include "taler_merchant_testing_lib.h"
#include "taler_merchant_service.h"
#include <taler/taler_exchange_service.h>


/**
 * State for a "checkserver" CMD.
 */
struct CheckState
{
  /**
   * Handle to the "testserver" service.
   */
  struct MHD_Daemon *mhd;

  /**
   * Our interpreter.
   */
  struct TALER_TESTING_Interpreter *is;

  /**
   * Index to know which web server we check.
   */
  unsigned int index;

  /**
   * Reference to command to the previous set server status operation.
   */
  const char *ref_operation;

  /**
   * Expected method of the pending webhook.
   */
  const char *expected_method;

  /**
   * Expected url of the pending webhook.
   */
  const char *expected_url;

  /**
   * Expected header of the pending webhook.
   */
  const char *expected_header;

  /**
   * Expected body of the pending webhook.
   */
  const char *expected_body;

};


/**
 * Run the command.
 *
 * @param cls closure.
 * @param cmd the command to execute.
 * @param is the interpreter state.
 */
static void
checkserver_run (void *cls,
                 const struct TALER_TESTING_Command *cmd,
                 struct TALER_TESTING_Interpreter *is)
{
  struct CheckState *cs = cls;
  const struct TALER_TESTING_Command *ref;
  const char *url;
  const char *http_method;
  const char *header;
  const void *body;
  const size_t *body_size;

  (void) cmd;
  cs->is = is;
  ref = TALER_TESTING_interpreter_lookup_command (is,
                                                  cs->ref_operation);
  if (NULL == ref)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "ref NULL\n");
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  if (GNUNET_OK !=
      TALER_TESTING_get_trait_urls (ref,
                                    cs->index,
                                    &url))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Trait url does not work\n");
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  if (NULL == url)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Trait for url is NULL!?\n");
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  if (0 != strcmp (cs->expected_url,
                   url))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "URL does not match: `%s' != `%s'\n",
                cs->expected_url,
                url);
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  if (GNUNET_OK !=
      TALER_TESTING_get_trait_http_methods (ref,
                                            cs->index,
                                            &http_method))
    TALER_TESTING_interpreter_fail (is);
  if (0 != strcmp (cs->expected_method,
                   http_method))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "http_method does not match\n");
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  if (GNUNET_OK !=
      TALER_TESTING_get_trait_http_header (ref,
                                           cs->index,
                                           &header))
    TALER_TESTING_interpreter_fail (is);
  if ( ( (NULL == cs->expected_header) && (NULL != header)) ||
       ( (NULL != cs->expected_header) && (NULL == header)) ||
       ( (NULL != cs->expected_header) &&
         (0 != strcmp (cs->expected_header,
                       header)) ) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "header does not match: `%s' != `%s'\n",
                cs->expected_header,
                header);
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  if (GNUNET_OK !=
      TALER_TESTING_get_trait_http_body (ref,
                                         cs->index,
                                         &body))
    TALER_TESTING_interpreter_fail (is);
  if (GNUNET_OK !=
      TALER_TESTING_get_trait_http_body_size (ref,
                                              cs->index,
                                              &body_size))
    TALER_TESTING_interpreter_fail (is);
  if ( ( (NULL == cs->expected_body) &&
         (NULL != body) ) ||
       ( (NULL != cs->expected_body) &&
         (NULL == body) ) ||
       ( (NULL != cs->expected_body) &&
         ( (*body_size != strlen (cs->expected_body)) ||
           (0 != memcmp (cs->expected_body,
                         body,
                         *body_size) ) ) ) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "body does not match : `%s' and `%.*s'\n",
                cs->expected_body,
                (int) *body_size,
                (const char *) body);
    TALER_TESTING_interpreter_fail (is);
    return;
  }
  TALER_TESTING_interpreter_next (is);
}


/**
 * Free the state of a "checkserver" CMD.
 *
 * @param cls closure.
 * @param cmd command being run.
 */
static void
checkserver_cleanup (void *cls,
                     const struct TALER_TESTING_Command *cmd)
{
  struct CheckState *cs = cls;

  GNUNET_free (cs);
}


struct TALER_TESTING_Command
TALER_TESTING_cmd_checkserver2 (const char *label,
                                const char *ref_operation,
                                unsigned int index,
                                const char *expected_url,
                                const char *expected_method,
                                const char *expected_header,
                                const char *expected_body)
{
  struct CheckState *cs;

  cs = GNUNET_new (struct CheckState);
  cs->ref_operation = ref_operation;
  cs->index = index;
  cs->expected_url = expected_url;
  cs->expected_method = expected_method;
  cs->expected_header = expected_header;
  cs->expected_body = expected_body;

  {
    struct TALER_TESTING_Command cmd = {
      .cls = cs,
      .label = label,
      .run = &checkserver_run,
      .cleanup = &checkserver_cleanup
    };

    return cmd;
  }
}


struct TALER_TESTING_Command
TALER_TESTING_cmd_checkserver (const char *label,
                               const char *ref_operation,
                               unsigned int index)
{
  return TALER_TESTING_cmd_checkserver2 (label,
                                         ref_operation,
                                         index,
                                         "/",
                                         "POST",
                                         "EFEHYJS-Bakery",
                                         "5.0 EUR");
}


/* end of testing_api_cmd_checkserver.c */