summaryrefslogtreecommitdiff
path: root/src/twister/taler-twister.c
blob: 94a9aa88b0eda53368383e93f38f8128968de772 (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
397
398
399
400
401
402
403
404
405
/*
  This file is part of Taler
  Copyright (C) 2008--2014, 2016 GNUnet e.V.
  Copyright (C) 2018 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,
  write to the Free Software Foundation, Inc., 51 Franklin
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/**
 * @file taler-twister.c
 * @brief command line utility to batch commands to the twister.
 * @author Marcello Stanisci
 */

#include "platform.h"
#include "taler_twister_service.h"

/**
 * The handle to the Twister service
 */
static struct TALER_TWISTER_Handle *tth;

/**
 * The program status; 0 for success.
 */
static int status;

/**
 * Response code to substitute to the genuine one.  0 for no
 * substitution.
 */
static unsigned int hack_response_code;

/**
 * Path to a object to be deleted.  The format is "object-like":
 * path "field0.field1.2.field3" will delete the following object:
 *
 * {"field1": {"field2": [0, 1, {"field3": "TO DELETE!"}]}}.
 */
static char *delete_path;

/**
 * If true, will randomly truncate the proxied response.
 */
static int malform_response;

/**
 * If true, will randomly truncate the proxied request.
 */
static int malform_upload;

/**
 * Path to the string-object to flip, belonging to the
 * response JSON object.
 */
static char *flip_path_dl;


/**
 * Path to the string-object to flip, belonging to the
 * request JSON object.
 */
static char *flip_path_ul;

/**
 * Path to the field to modify, from the response JSON object.
 */
static char *modify_path_dl;

/*
 * Path to the field to modify, from the request JSON object.
 */
static char *modify_path_ul;

/**
 * Name of the header to modify in the HTTP request.
 */
static char *modify_header_dl;

/**
 * New value for the object pointed by `modify_path_(ul|dl)`.
 */
static char *modify_value;

/**
 * This option is used to check whether the twister can accept
 * connections over the unix domain socket interface.  Used when
 * launching it to see if everything (?) is okay.
 */
static int check_alive;

/**
 * Number of operations we batched.
 */
static unsigned int num_ops;


/**
 * Task to shutdown and clean up all state
 *
 * @param cls NULL
 */
static void
do_shutdown (void *cls)
{
  if (NULL != tth)
  {
    TALER_TWISTER_disconnect (tth);
    tth = NULL;
  }
}


/**
 * Callback invoked after the twister batched one operation.
 *
 * @param cls NULL
 */
static void
handle_acknowledgement (void *cls)
{
  num_ops--;
  if (0 != num_ops)
    return;
  status = 0;
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Method that batches operations into the twister.
 *
 * @param cls unused
 * @param args remaining args, unused
 * @param cfgfile name of the configuration
 * @param cfg configuration handle
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  tth = TALER_TWISTER_connect (cfg);
  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
                                 NULL);
  if (NULL == tth)
  {
    GNUNET_break (0);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  if ( (0 != malform_upload) &&
       (NULL != TALER_TWISTER_malform_upload
         (tth,
          &handle_acknowledgement,
          NULL)))
    num_ops++;

  if ( (0 != malform_response) &&
       (NULL != TALER_TWISTER_malform
         (tth,
          &handle_acknowledgement,
          NULL)))
    num_ops++;

  if (0 != check_alive)
  {
    fprintf (stderr, "Could connect to twister via IPC socket.\n");
    status = 0;
  }

  if ( (0 != hack_response_code) &&
       (NULL != TALER_TWISTER_change_response_code
         (tth,
          hack_response_code,
          &handle_acknowledgement,
          NULL)) )
    num_ops++;

  if ( (NULL != flip_path_ul) &&
       (NULL != TALER_TWISTER_flip_upload
         (tth,
          flip_path_ul,
          &handle_acknowledgement,
          NULL)) )
    num_ops++;

  if ( (NULL != flip_path_dl) &&
       (NULL != TALER_TWISTER_flip_download
         (tth,
          flip_path_dl,
          &handle_acknowledgement,
          NULL)) )
    num_ops++;

  if ( (NULL != delete_path) &&
       (NULL != TALER_TWISTER_delete_path
         (tth,
          delete_path,
          &handle_acknowledgement,
          NULL)) )
    num_ops++;

  if (NULL != modify_path_ul)
  {
    if (NULL == modify_value)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "New value not given, give -V|--value also\n");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }

    if (NULL != TALER_TWISTER_modify_path_ul
        (tth,
         modify_path_ul,
         modify_value,
         &handle_acknowledgement,
         NULL))
      num_ops++;
  }

  if (NULL != modify_path_dl)
  {
    if (NULL == modify_value)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "New value not given, give -V|--value also\n");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }

    if (NULL != TALER_TWISTER_modify_path_dl
        (tth,
         modify_path_dl,
         modify_value,
         &handle_acknowledgement,
         NULL))
      num_ops++;
  }

  if (NULL != modify_header_dl)
  {
    if (NULL == modify_value)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "New value not given, give -V|--value also\n");
      GNUNET_SCHEDULER_shutdown ();
      return;
    }

    if (NULL != TALER_TWISTER_modify_header_dl
        (tth,
         modify_header_dl,
         modify_value,
         &handle_acknowledgement,
         NULL))
      num_ops++;
  }

  if (0 == num_ops)
  {
    fprintf (stderr, "No valid hacks specified!\n");
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
}


/**
 * Main function.
 *
 * @param argc number of CLI arguments given + 1.
 * @param argv array of CLI arguments given, + the binary name.
 * @return 0 on success
 */
int
main (int argc,
      char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {

    GNUNET_GETOPT_option_string
      ('X',
       "modify-ul",
       "PATH",
       gettext_noop
         ("Modify upload object pointed by PATH,"
          " require --value.\n"),
       &modify_path_ul),

    GNUNET_GETOPT_option_string
      ('m',
       "modify-dl",
       "PATH",
       gettext_noop
         ("Modify download object pointed by PATH,"
          " require --value.\n"),
       &modify_path_dl),

    GNUNET_GETOPT_option_string
      ('H',
       "modify-header-dl",
       "HEADER",
       gettext_noop
         ("Modify download HTTP header HEADER,"
          " require --value.\n"),
       &modify_header_dl),

    GNUNET_GETOPT_option_string
      ('F',
       "flip-ul",
       "PATH",
       gettext_noop
         ("Flip a char in the *string* upload object"
          " pointed by PATH.\n"),
       &flip_path_ul),

    GNUNET_GETOPT_option_string
      ('f',
       "flip-dl",
       "PATH",
       gettext_noop
         ("Flip a char in the *string* download"
          " object pointed by PATH.\n"),
       &flip_path_dl),

    GNUNET_GETOPT_option_string
      ('V',
       "value",
       "VALUE",
       gettext_noop
         ("Make VALUE the new value of the field"
          " pointed by PATH.  Note: in this version,"
          " numbers will be always parsed and _set_"
          " as strings."),
       &modify_value),

    GNUNET_GETOPT_option_string
      ('d',
       "deleteobject",
       "PATH",
       gettext_noop
         ("Delete the object pointed by PATH.\n"),
       &delete_path),

    GNUNET_GETOPT_option_flag
      ('a',
       "checkalive",
       gettext_noop
         ("Check if twister accepts IPC connections\n"),
       &check_alive),

    GNUNET_GETOPT_option_flag
      ('U',
       "malformupload",
       gettext_noop
         ("Randomly truncate proxied request"),
       &malform_upload),

    GNUNET_GETOPT_option_flag
      ('M',
       "malform",
       gettext_noop
         ("Randomly truncate proxied response"),
       &malform_response),

    GNUNET_GETOPT_option_uint
      ('r',
       "responsecode",
       "STATUS",
       gettext_noop
         ("Set the next response code to STATUS"),
       &hack_response_code),
    GNUNET_GETOPT_OPTION_END
  };

  status = 1;
  if (GNUNET_OK !=
      GNUNET_PROGRAM_run (
        argc,
        argv,
        "taler-twister",
        gettext_noop
        ("Control taler-twister service."),
        options,
        &run, NULL))
    return 2;
  return status;
}