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