diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-05-03 15:10:26 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-05-03 15:10:46 +0200 |
commit | 4444fb80284aa86fe24f3640a0b1e4c841a98f9a (patch) | |
tree | 17ef4a5b51f2ff51b476e28f8aa6443b29f8b5ac /src/json/test_json_mhd.c | |
parent | afe1bb7673a350d86228e2d916e346e27e4fe7a6 (diff) | |
download | gnunet-4444fb80284aa86fe24f3640a0b1e4c841a98f9a.tar.gz gnunet-4444fb80284aa86fe24f3640a0b1e4c841a98f9a.zip |
add test for json_mhd.c logic
Diffstat (limited to 'src/json/test_json_mhd.c')
-rw-r--r-- | src/json/test_json_mhd.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/json/test_json_mhd.c b/src/json/test_json_mhd.c new file mode 100644 index 000000000..665fd140e --- /dev/null +++ b/src/json/test_json_mhd.c | |||
@@ -0,0 +1,151 @@ | |||
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 | |||
31 | #define MAX_SIZE 1024 * 1024 | ||
32 | |||
33 | static json_t *bigj; | ||
34 | |||
35 | static int global_ret; | ||
36 | |||
37 | |||
38 | static int | ||
39 | access_handler_cb (void *cls, | ||
40 | struct MHD_Connection *connection, | ||
41 | const char *url, | ||
42 | const char *method, | ||
43 | const char *version, | ||
44 | const char *upload_data, | ||
45 | size_t *upload_data_size, | ||
46 | void **con_cls) | ||
47 | { | ||
48 | int ret; | ||
49 | json_t *json; | ||
50 | struct MHD_Response *resp; | ||
51 | |||
52 | json = NULL; | ||
53 | ret = GNUNET_JSON_post_parser (MAX_SIZE, | ||
54 | connection, | ||
55 | con_cls, | ||
56 | upload_data, | ||
57 | upload_data_size, | ||
58 | &json); | ||
59 | switch (ret) | ||
60 | { | ||
61 | case GNUNET_JSON_PR_SUCCESS: | ||
62 | if (json_equal (bigj, json)) | ||
63 | { | ||
64 | global_ret = 0; | ||
65 | } | ||
66 | else | ||
67 | { | ||
68 | GNUNET_break (0); | ||
69 | global_ret = 6; | ||
70 | } | ||
71 | json_decref (json); | ||
72 | resp = MHD_create_response_from_buffer (2, "OK", MHD_RESPMEM_PERSISTENT); | ||
73 | ret = MHD_queue_response (connection, MHD_HTTP_OK, resp); | ||
74 | MHD_destroy_response (resp); | ||
75 | return ret; | ||
76 | case GNUNET_JSON_PR_CONTINUE: | ||
77 | return MHD_YES; | ||
78 | case GNUNET_JSON_PR_OUT_OF_MEMORY: | ||
79 | GNUNET_break (0); | ||
80 | global_ret = 3; | ||
81 | break; | ||
82 | case GNUNET_JSON_PR_REQUEST_TOO_LARGE: | ||
83 | GNUNET_break (0); | ||
84 | global_ret = 4; | ||
85 | break; | ||
86 | case GNUNET_JSON_PR_JSON_INVALID: | ||
87 | GNUNET_break (0); | ||
88 | global_ret = 5; | ||
89 | break; | ||
90 | } | ||
91 | GNUNET_break (0); | ||
92 | return MHD_NO; | ||
93 | } | ||
94 | |||
95 | |||
96 | int | ||
97 | main (int argc, const char *const argv[]) | ||
98 | { | ||
99 | struct MHD_Daemon *daemon; | ||
100 | uint16_t port; | ||
101 | CURL *easy; | ||
102 | char *url; | ||
103 | long post_data_size; | ||
104 | void *post_data; | ||
105 | |||
106 | GNUNET_log_setup ("test-json-mhd", "WARNING", NULL); | ||
107 | global_ret = 2; | ||
108 | daemon = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_AUTO_INTERNAL_THREAD, | ||
109 | 0, | ||
110 | NULL, | ||
111 | NULL, | ||
112 | &access_handler_cb, | ||
113 | NULL, | ||
114 | MHD_OPTION_END); | ||
115 | if (NULL == daemon) | ||
116 | return 77; | ||
117 | bigj = json_object (); | ||
118 | json_object_set_new (bigj, "test", json_string ("value")); | ||
119 | for (unsigned int i = 0; i < 1000; i++) | ||
120 | { | ||
121 | char tmp[5]; | ||
122 | |||
123 | GNUNET_snprintf (tmp, sizeof (tmp), "%u", i); | ||
124 | json_object_set_new (bigj, tmp, json_string (tmp)); | ||
125 | } | ||
126 | post_data = json_dumps (bigj, JSON_INDENT (2)); | ||
127 | post_data_size = strlen (post_data); | ||
128 | |||
129 | port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port; | ||
130 | easy = curl_easy_init (); | ||
131 | GNUNET_asprintf (&url, "http://localhost:%u/", (unsigned int) port); | ||
132 | curl_easy_setopt (easy, CURLOPT_VERBOSE, 1); | ||
133 | curl_easy_setopt (easy, CURLOPT_URL, url); | ||
134 | curl_easy_setopt (easy, CURLOPT_POST, 1); | ||
135 | curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data); | ||
136 | curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size); | ||
137 | if (0 != curl_easy_perform (easy)) | ||
138 | { | ||
139 | GNUNET_break (0); | ||
140 | MHD_stop_daemon (daemon); | ||
141 | GNUNET_free (url); | ||
142 | json_decref (bigj); | ||
143 | return 1; | ||
144 | } | ||
145 | MHD_stop_daemon (daemon); | ||
146 | GNUNET_free (url); | ||
147 | json_decref (bigj); | ||
148 | return global_ret; | ||
149 | } | ||
150 | |||
151 | /* end of test_json_mhd.c */ | ||