summaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_checkserver.c
blob: 1414bd1d5b8da45f5d200fb7268a28504fd3d0bf (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
/*
  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 "check_aml_decision" 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.
   */
  char *expected_method;

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

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

  /**
   * Expected body of the pending webhook.
   */
  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;
  (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;
  }

  char **expected_url;
  if (GNUNET_OK !=
      TALER_TESTING_get_trait_urls (ref,
                                    cs->index,
                                    &expected_url))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Trait url does not work\n");
      GNUNET_break (0);
      TALER_TESTING_interpreter_fail (is);
      return;
    }
  if (NULL == *expected_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,
                   *expected_url))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "URL does not match: `%s' != `%s'\n",
                  cs->expected_url,
                  *expected_url);
      TALER_TESTING_interpreter_fail (is);
      return;
    }

  char **expected_http_method;

  if (GNUNET_OK !=
      TALER_TESTING_get_trait_http_methods (ref,
                                            cs->index,
                                            &expected_http_method))
    TALER_TESTING_interpreter_fail (is);
  if (0 != strcmp (cs->expected_method,
                   *expected_http_method))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "http_method does not match\n");
      TALER_TESTING_interpreter_fail (is);
      return;
    }


  char **expected_header;

  if (GNUNET_OK !=
      TALER_TESTING_get_trait_http_header (ref,
                                           cs->index,
                                           &expected_header))
    TALER_TESTING_interpreter_fail (is);
  if ( ( (NULL == cs->expected_header) && (NULL != *expected_header)) ||
       ( (NULL != cs->expected_header) && (NULL == expected_header)) ||
       ( (NULL != cs->expected_header) &&
         (0 != strcmp (cs->expected_header,
                       *expected_header)) ) )
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "header does not match: `%s' != `%s'\n",
                  cs->expected_header,
                  *expected_header);
        TALER_TESTING_interpreter_fail (is);
        return;
    }

  char **expected_body;

  if (GNUNET_OK !=
      TALER_TESTING_get_trait_http_body (ref,
                                         cs->index,
                                         &expected_body))
    TALER_TESTING_interpreter_fail (is);
  if ( ( (NULL == cs->expected_body) && (NULL != *expected_body)) ||
       ( (NULL != cs->expected_body) && (NULL == expected_body)) ||
       ( (NULL != cs->expected_body) &&
         (0 != strcmp (cs->expected_body,
                       *expected_body) ) ) )
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "body does not match : `%s' and `%s'\n",
                  cs->expected_body,
                  *expected_body);
      TALER_TESTING_interpreter_fail (is);
      return;
    }

  TALER_TESTING_interpreter_next (is);
}


/**
 * This function is used to check the web server
 *
 * @param label command label
 * @param ref_operation reference to command to the previous set server status operation.
 * @param index index to know which web server we check.
 * @param url of the webhook
 * @param http_method of the webhook
 * @param header of the webhook
 * @param body of the webhook
 */
struct TALER_TESTING_Command
TALER_TESTING_cmd_checkserver2 (const char *label,
                                const char *ref_operation,
                                unsigned int index,
                                char *expected_url,
                                char *expected_method,
                                char *expected_header,
                                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
    };

    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 */