summaryrefslogtreecommitdiff
path: root/src/json/test_json_mhd.c
blob: 642715f2577503bd3ba1658f3ba96102457347ae (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
/*
   This file is part of GNUnet
   (C) 2019 GNUnet e.V.

   GNUnet 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 of the License,
   or (at your option) any later version.

   GNUnet 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 this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file json/test_json_mhd.c
 * @brief Tests for JSON MHD integration functions
 * @author Christian Grothoff <christian@grothoff.org>
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_json_lib.h"
#include "gnunet_curl_lib.h"
#include "gnunet_mhd_compat.h"
#include <zlib.h>

#define MAX_SIZE 1024 * 1024

static json_t *bigj;

static int global_ret;


static MHD_RESULT
access_handler_cb (void *cls,
                   struct MHD_Connection *connection,
                   const char *url,
                   const char *method,
                   const char *version,
                   const char *upload_data,
                   size_t *upload_data_size,
                   void **con_cls)
{
  int ret;
  json_t *json;
  struct MHD_Response *resp;

  json = NULL;
  ret = GNUNET_JSON_post_parser (MAX_SIZE,
                                 connection,
                                 con_cls,
                                 upload_data,
                                 upload_data_size,
                                 &json);
  switch (ret)
  {
  case GNUNET_JSON_PR_SUCCESS:
    if (json_equal (bigj, json))
    {
      global_ret = 0;
    }
    else
    {
      GNUNET_break (0);
      global_ret = 6;
    }
    json_decref (json);
    resp = MHD_create_response_from_buffer (3, "OK\n", MHD_RESPMEM_PERSISTENT);
    ret = MHD_queue_response (connection, MHD_HTTP_OK, resp);
    MHD_destroy_response (resp);
    return ret;

  case GNUNET_JSON_PR_CONTINUE:
    return MHD_YES;

  case GNUNET_JSON_PR_OUT_OF_MEMORY:
    GNUNET_break (0);
    global_ret = 3;
    break;

  case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
    GNUNET_break (0);
    global_ret = 4;
    break;

  case GNUNET_JSON_PR_JSON_INVALID:
    GNUNET_break (0);
    global_ret = 5;
    break;
  }
  GNUNET_break (0);
  return MHD_NO;
}


int
main (int argc, const char *const argv[])
{
  struct MHD_Daemon *daemon;
  uint16_t port;
  CURL *easy;
  char *url;
  char *str;
  size_t slen;
  long post_data_size;
  void *post_data;
  uLongf dlen;
  struct curl_slist *json_header;

  GNUNET_log_setup ("test-json-mhd", "WARNING", NULL);
  global_ret = 2;
  daemon = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_AUTO_INTERNAL_THREAD,
                             0,
                             NULL,
                             NULL,
                             &access_handler_cb,
                             NULL,
                             MHD_OPTION_END);
  if (NULL == daemon)
    return 77;
  bigj = json_object ();
  json_object_set_new (bigj, "test", json_string ("value"));
  for (unsigned int i = 0; i < 1000; i++)
  {
    char tmp[5];

    GNUNET_snprintf (tmp, sizeof(tmp), "%u", i);
    json_object_set_new (bigj, tmp, json_string (tmp));
  }
  str = json_dumps (bigj, JSON_INDENT (2));
  slen = strlen (str);

#ifdef compressBound
  dlen = compressBound (slen);
#else
  dlen = slen + slen / 100 + 20;
  /* documentation says 100.1% oldSize + 12 bytes, but we
   * should be able to overshoot by more to be safe */
#endif
  post_data = GNUNET_malloc (dlen);
  if (Z_OK !=
      compress2 ((Bytef *) post_data, &dlen, (const Bytef *) str, slen, 9))
  {
    GNUNET_break (0);
    MHD_stop_daemon (daemon);
    json_decref (bigj);
    GNUNET_free (post_data);
    GNUNET_free (str);
    return 1;
  }
  post_data_size = (long) dlen;
  port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port;
  easy = curl_easy_init ();
  GNUNET_asprintf (&url, "http://localhost:%u/", (unsigned int) port);
  curl_easy_setopt (easy, CURLOPT_VERBOSE, 0);
  curl_easy_setopt (easy, CURLOPT_URL, url);
  curl_easy_setopt (easy, CURLOPT_POST, 1);
  curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data);
  curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size);

  json_header = curl_slist_append (NULL, "Content-Type: application/json");
  json_header = curl_slist_append (json_header, "Content-Encoding: deflate");
  curl_easy_setopt (easy, CURLOPT_HTTPHEADER, json_header);
  if (0 != curl_easy_perform (easy))
  {
    GNUNET_break (0);
    MHD_stop_daemon (daemon);
    GNUNET_free (url);
    json_decref (bigj);
    GNUNET_free (post_data);
    GNUNET_free (str);
    curl_slist_free_all (json_header);
    curl_easy_cleanup (easy);
    return 1;
  }
  MHD_stop_daemon (daemon);
  GNUNET_free (url);
  json_decref (bigj);
  GNUNET_free (post_data);
  GNUNET_free (str);
  curl_slist_free_all (json_header);
  curl_easy_cleanup (easy);
  return global_ret;
}


/* end of test_json_mhd.c */