remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / json / test_json_mhd.c
1 /*
2    This file is part of GNUnet
3    (C) 2019 GNUnet e.V.
4
5    GNUnet is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License,
8    or (at your option) any later version.
9
10    GNUnet is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file json/test_json_mhd.c
23  * @brief Tests for JSON MHD integration functions
24  * @author Christian Grothoff <christian@grothoff.org>
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_json_lib.h"
29 #include "gnunet_curl_lib.h"
30 #include <zlib.h>
31
32 #define MAX_SIZE 1024 * 1024
33
34 static json_t *bigj;
35
36 static int global_ret;
37
38
39 static int
40 access_handler_cb (void *cls,
41                    struct MHD_Connection *connection,
42                    const char *url,
43                    const char *method,
44                    const char *version,
45                    const char *upload_data,
46                    size_t *upload_data_size,
47                    void **con_cls)
48 {
49   int ret;
50   json_t *json;
51   struct MHD_Response *resp;
52
53   json = NULL;
54   ret = GNUNET_JSON_post_parser (MAX_SIZE,
55                                  connection,
56                                  con_cls,
57                                  upload_data,
58                                  upload_data_size,
59                                  &json);
60   switch (ret)
61   {
62   case GNUNET_JSON_PR_SUCCESS:
63     if (json_equal (bigj, json))
64     {
65       global_ret = 0;
66     }
67     else
68     {
69       GNUNET_break (0);
70       global_ret = 6;
71     }
72     json_decref (json);
73     resp = MHD_create_response_from_buffer (3, "OK\n", MHD_RESPMEM_PERSISTENT);
74     ret = MHD_queue_response (connection, MHD_HTTP_OK, resp);
75     MHD_destroy_response (resp);
76     return ret;
77
78   case GNUNET_JSON_PR_CONTINUE:
79     return MHD_YES;
80
81   case GNUNET_JSON_PR_OUT_OF_MEMORY:
82     GNUNET_break (0);
83     global_ret = 3;
84     break;
85
86   case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
87     GNUNET_break (0);
88     global_ret = 4;
89     break;
90
91   case GNUNET_JSON_PR_JSON_INVALID:
92     GNUNET_break (0);
93     global_ret = 5;
94     break;
95   }
96   GNUNET_break (0);
97   return MHD_NO;
98 }
99
100
101 int
102 main (int argc, const char *const argv[])
103 {
104   struct MHD_Daemon *daemon;
105   uint16_t port;
106   CURL *easy;
107   char *url;
108   char *str;
109   size_t slen;
110   long post_data_size;
111   void *post_data;
112   uLongf dlen;
113   struct curl_slist *json_header;
114
115   GNUNET_log_setup ("test-json-mhd", "WARNING", NULL);
116   global_ret = 2;
117   daemon = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_AUTO_INTERNAL_THREAD,
118                              0,
119                              NULL,
120                              NULL,
121                              &access_handler_cb,
122                              NULL,
123                              MHD_OPTION_END);
124   if (NULL == daemon)
125     return 77;
126   bigj = json_object ();
127   json_object_set_new (bigj, "test", json_string ("value"));
128   for (unsigned int i = 0; i < 1000; i++)
129   {
130     char tmp[5];
131
132     GNUNET_snprintf (tmp, sizeof(tmp), "%u", i);
133     json_object_set_new (bigj, tmp, json_string (tmp));
134   }
135   str = json_dumps (bigj, JSON_INDENT (2));
136   slen = strlen (str);
137
138 #ifdef compressBound
139   dlen = compressBound (slen);
140 #else
141   dlen = slen + slen / 100 + 20;
142   /* documentation says 100.1% oldSize + 12 bytes, but we
143    * should be able to overshoot by more to be safe */
144 #endif
145   post_data = GNUNET_malloc (dlen);
146   if (Z_OK !=
147       compress2 ((Bytef *) post_data, &dlen, (const Bytef *) str, slen, 9))
148   {
149     GNUNET_break (0);
150     MHD_stop_daemon (daemon);
151     json_decref (bigj);
152     GNUNET_free (post_data);
153     GNUNET_free (str);
154     return 1;
155   }
156   post_data_size = (long) dlen;
157   port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port;
158   easy = curl_easy_init ();
159   GNUNET_asprintf (&url, "http://localhost:%u/", (unsigned int) port);
160   curl_easy_setopt (easy, CURLOPT_VERBOSE, 0);
161   curl_easy_setopt (easy, CURLOPT_URL, url);
162   curl_easy_setopt (easy, CURLOPT_POST, 1);
163   curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data);
164   curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size);
165
166   json_header = curl_slist_append (NULL, "Content-Type: application/json");
167   json_header = curl_slist_append (json_header, "Content-Encoding: deflate");
168   curl_easy_setopt (easy, CURLOPT_HTTPHEADER, json_header);
169   if (0 != curl_easy_perform (easy))
170   {
171     GNUNET_break (0);
172     MHD_stop_daemon (daemon);
173     GNUNET_free (url);
174     json_decref (bigj);
175     GNUNET_free (post_data);
176     GNUNET_free (str);
177     curl_slist_free_all (json_header);
178     curl_easy_cleanup (easy);
179     return 1;
180   }
181   MHD_stop_daemon (daemon);
182   GNUNET_free (url);
183   json_decref (bigj);
184   GNUNET_free (post_data);
185   GNUNET_free (str);
186   curl_slist_free_all (json_header);
187   curl_easy_cleanup (easy);
188   return global_ret;
189 }
190
191
192 /* end of test_json_mhd.c */