aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-05-03 15:10:26 +0200
committerChristian Grothoff <christian@grothoff.org>2019-05-03 15:10:46 +0200
commit4444fb80284aa86fe24f3640a0b1e4c841a98f9a (patch)
tree17ef4a5b51f2ff51b476e28f8aa6443b29f8b5ac
parentafe1bb7673a350d86228e2d916e346e27e4fe7a6 (diff)
downloadgnunet-4444fb80284aa86fe24f3640a0b1e4c841a98f9a.tar.gz
gnunet-4444fb80284aa86fe24f3640a0b1e4c841a98f9a.zip
add test for json_mhd.c logic
-rw-r--r--src/Makefile.am2
-rw-r--r--src/json/Makefile.am26
-rw-r--r--src/json/test_json.c129
-rw-r--r--src/json/test_json_mhd.c151
4 files changed, 224 insertions, 84 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index ab8731fc2..5ae328f9a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,6 +23,7 @@ if HAVE_JSON
23endif 23endif
24endif 24endif
25 25
26if HAVE_MHD
26if HAVE_JSON 27if HAVE_JSON
27 JSON_DIR = json 28 JSON_DIR = json
28if HAVE_LIBGNURL 29if HAVE_LIBGNURL
@@ -33,6 +34,7 @@ if HAVE_LIBCURL
33endif 34endif
34endif 35endif
35endif 36endif
37endif
36 38
37if BUILD_PULSE_HELPERS 39if BUILD_PULSE_HELPERS
38CONVERSATION_DIR = conversation 40CONVERSATION_DIR = conversation
diff --git a/src/json/Makefile.am b/src/json/Makefile.am
index f3fa28d69..f030c3016 100644
--- a/src/json/Makefile.am
+++ b/src/json/Makefile.am
@@ -25,7 +25,8 @@ libgnunetjson_la_LIBADD = \
25 $(XLIB) 25 $(XLIB)
26 26
27check_PROGRAMS = \ 27check_PROGRAMS = \
28 test_json 28 test_json \
29 test_json_mhd
29 30
30TESTS = \ 31TESTS = \
31 $(check_PROGRAMS) 32 $(check_PROGRAMS)
@@ -36,3 +37,26 @@ test_json_LDADD = \
36 libgnunetjson.la \ 37 libgnunetjson.la \
37 $(top_builddir)/src/util/libgnunetutil.la \ 38 $(top_builddir)/src/util/libgnunetutil.la \
38 -ljansson 39 -ljansson
40
41
42if HAVE_LIBGNURL
43LIB_GNURL=@LIBGNURL@
44CPP_GNURL=@LIBGNURL_CPPFLAGS@
45else
46if HAVE_LIBCURL
47LIB_GNURL=@LIBCURL@
48CPP_GNURL=@LIBCURL_CPPFLAGS@
49endif
50endif
51
52
53test_json_mhd_SOURCES = \
54 test_json_mhd.c
55test_json_mhd_LDADD = \
56 libgnunetjson.la \
57 $(top_builddir)/src/util/libgnunetutil.la \
58 -ljansson \
59 -lmicrohttpd \
60 $(LIB_GNURL)
61test_json_mhd_CPPFLAGS = \
62 $(CPP_GNURL) $(AM_CPPFLAGS)
diff --git a/src/json/test_json.c b/src/json/test_json.c
index dd957eb8e..40fb7f090 100644
--- a/src/json/test_json.c
+++ b/src/json/test_json.c
@@ -11,7 +11,7 @@
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
@@ -39,32 +39,26 @@ test_abs_time ()
39 json_t *j; 39 json_t *j;
40 struct GNUNET_TIME_Absolute a1; 40 struct GNUNET_TIME_Absolute a1;
41 struct GNUNET_TIME_Absolute a2; 41 struct GNUNET_TIME_Absolute a2;
42 struct GNUNET_JSON_Specification s1[] = { 42 struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_absolute_time (NULL,
43 GNUNET_JSON_spec_absolute_time (NULL, &a2), 43 &a2),
44 GNUNET_JSON_spec_end() 44 GNUNET_JSON_spec_end ()};
45 }; 45 struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_absolute_time (NULL,
46 struct GNUNET_JSON_Specification s2[] = { 46 &a2),
47 GNUNET_JSON_spec_absolute_time (NULL, &a2), 47 GNUNET_JSON_spec_end ()};
48 GNUNET_JSON_spec_end()
49 };
50 48
51 a1 = GNUNET_TIME_absolute_get (); 49 a1 = GNUNET_TIME_absolute_get ();
52 GNUNET_TIME_round_abs (&a1); 50 GNUNET_TIME_round_abs (&a1);
53 j = GNUNET_JSON_from_time_abs (a1); 51 j = GNUNET_JSON_from_time_abs (a1);
54 GNUNET_assert (NULL != j); 52 GNUNET_assert (NULL != j);
55 GNUNET_assert (GNUNET_OK == 53 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL));
56 GNUNET_JSON_parse (j, s1, NULL, NULL)); 54 GNUNET_assert (a1.abs_value_us == a2.abs_value_us);
57 GNUNET_assert (a1.abs_value_us ==
58 a2.abs_value_us);
59 json_decref (j); 55 json_decref (j);
60 56
61 a1 = GNUNET_TIME_UNIT_FOREVER_ABS; 57 a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
62 j = GNUNET_JSON_from_time_abs (a1); 58 j = GNUNET_JSON_from_time_abs (a1);
63 GNUNET_assert (NULL != j); 59 GNUNET_assert (NULL != j);
64 GNUNET_assert (GNUNET_OK == 60 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL));
65 GNUNET_JSON_parse (j, s2, NULL, NULL)); 61 GNUNET_assert (a1.abs_value_us == a2.abs_value_us);
66 GNUNET_assert (a1.abs_value_us ==
67 a2.abs_value_us);
68 json_decref (j); 62 json_decref (j);
69 return 0; 63 return 0;
70} 64}
@@ -81,31 +75,25 @@ test_rel_time ()
81 json_t *j; 75 json_t *j;
82 struct GNUNET_TIME_Relative r1; 76 struct GNUNET_TIME_Relative r1;
83 struct GNUNET_TIME_Relative r2; 77 struct GNUNET_TIME_Relative r2;
84 struct GNUNET_JSON_Specification s1[] = { 78 struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_relative_time (NULL,
85 GNUNET_JSON_spec_relative_time (NULL, &r2), 79 &r2),
86 GNUNET_JSON_spec_end() 80 GNUNET_JSON_spec_end ()};
87 }; 81 struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_relative_time (NULL,
88 struct GNUNET_JSON_Specification s2[] = { 82 &r2),
89 GNUNET_JSON_spec_relative_time (NULL, &r2), 83 GNUNET_JSON_spec_end ()};
90 GNUNET_JSON_spec_end()
91 };
92 84
93 r1 = GNUNET_TIME_UNIT_SECONDS; 85 r1 = GNUNET_TIME_UNIT_SECONDS;
94 j = GNUNET_JSON_from_time_rel (r1); 86 j = GNUNET_JSON_from_time_rel (r1);
95 GNUNET_assert (NULL != j); 87 GNUNET_assert (NULL != j);
96 GNUNET_assert (GNUNET_OK == 88 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL));
97 GNUNET_JSON_parse (j, s1, NULL, NULL)); 89 GNUNET_assert (r1.rel_value_us == r2.rel_value_us);
98 GNUNET_assert (r1.rel_value_us ==
99 r2.rel_value_us);
100 json_decref (j); 90 json_decref (j);
101 91
102 r1 = GNUNET_TIME_UNIT_FOREVER_REL; 92 r1 = GNUNET_TIME_UNIT_FOREVER_REL;
103 j = GNUNET_JSON_from_time_rel (r1); 93 j = GNUNET_JSON_from_time_rel (r1);
104 GNUNET_assert (NULL != j); 94 GNUNET_assert (NULL != j);
105 GNUNET_assert (GNUNET_OK == 95 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL));
106 GNUNET_JSON_parse (j, s2, NULL, NULL)); 96 GNUNET_assert (r1.rel_value_us == r2.rel_value_us);
107 GNUNET_assert (r1.rel_value_us ==
108 r2.rel_value_us);
109 json_decref (j); 97 json_decref (j);
110 return 0; 98 return 0;
111} 99}
@@ -123,24 +111,19 @@ test_raw ()
123 unsigned int i; 111 unsigned int i;
124 json_t *j; 112 json_t *j;
125 113
126 for (i=0;i<=256;i++) 114 for (i = 0; i <= 256; i++)
127 { 115 {
128 char blob2[256]; 116 char blob2[256];
129 struct GNUNET_JSON_Specification spec[] = { 117 struct GNUNET_JSON_Specification spec[] = {GNUNET_JSON_spec_fixed (NULL,
130 GNUNET_JSON_spec_fixed (NULL, blob2, i), 118 blob2,
131 GNUNET_JSON_spec_end() 119 i),
132 }; 120 GNUNET_JSON_spec_end ()};
133 121
134 memset (blob, i, i); 122 memset (blob, i, i);
135 j = GNUNET_JSON_from_data (blob, i); 123 j = GNUNET_JSON_from_data (blob, i);
136 GNUNET_assert (NULL != j); 124 GNUNET_assert (NULL != j);
137 GNUNET_assert (GNUNET_OK == 125 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL));
138 GNUNET_JSON_parse (j, spec, 126 GNUNET_assert (0 == memcmp (blob, blob2, i));
139 NULL, NULL));
140 GNUNET_assert (0 ==
141 memcmp (blob,
142 blob2,
143 i));
144 } 127 }
145 return 0; 128 return 0;
146} 129}
@@ -156,16 +139,12 @@ test_rsa ()
156{ 139{
157 struct GNUNET_CRYPTO_RsaPublicKey *pub; 140 struct GNUNET_CRYPTO_RsaPublicKey *pub;
158 struct GNUNET_CRYPTO_RsaPublicKey *pub2; 141 struct GNUNET_CRYPTO_RsaPublicKey *pub2;
159 struct GNUNET_JSON_Specification pspec[] = { 142 struct GNUNET_JSON_Specification pspec[] =
160 GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), 143 {GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end ()};
161 GNUNET_JSON_spec_end()
162 };
163 struct GNUNET_CRYPTO_RsaSignature *sig; 144 struct GNUNET_CRYPTO_RsaSignature *sig;
164 struct GNUNET_CRYPTO_RsaSignature *sig2; 145 struct GNUNET_CRYPTO_RsaSignature *sig2;
165 struct GNUNET_JSON_Specification sspec[] = { 146 struct GNUNET_JSON_Specification sspec[] =
166 GNUNET_JSON_spec_rsa_signature (NULL, &sig2), 147 {GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end ()};
167 GNUNET_JSON_spec_end()
168 };
169 struct GNUNET_CRYPTO_RsaPrivateKey *priv; 148 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
170 struct GNUNET_HashCode msg; 149 struct GNUNET_HashCode msg;
171 json_t *jp; 150 json_t *jp;
@@ -174,22 +153,13 @@ test_rsa ()
174 priv = GNUNET_CRYPTO_rsa_private_key_create (1024); 153 priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
175 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); 154 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
176 memset (&msg, 42, sizeof (msg)); 155 memset (&msg, 42, sizeof (msg));
177 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, 156 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg);
178 &msg);
179 GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub))); 157 GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub)));
180 GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig))); 158 GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig)));
181 GNUNET_assert (GNUNET_OK == 159 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (jp, pspec, NULL, NULL));
182 GNUNET_JSON_parse (jp, pspec, 160 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL));
183 NULL, NULL)); 161 GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2));
184 GNUNET_assert (GNUNET_OK == 162 GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2));
185 GNUNET_JSON_parse (js, sspec,
186 NULL, NULL));
187 GNUNET_break (0 ==
188 GNUNET_CRYPTO_rsa_signature_cmp (sig,
189 sig2));
190 GNUNET_break (0 ==
191 GNUNET_CRYPTO_rsa_public_key_cmp (pub,
192 pub2));
193 GNUNET_CRYPTO_rsa_signature_free (sig); 163 GNUNET_CRYPTO_rsa_signature_free (sig);
194 GNUNET_CRYPTO_rsa_signature_free (sig2); 164 GNUNET_CRYPTO_rsa_signature_free (sig2);
195 GNUNET_CRYPTO_rsa_private_key_free (priv); 165 GNUNET_CRYPTO_rsa_private_key_free (priv);
@@ -210,40 +180,33 @@ test_boolean ()
210 int b1; 180 int b1;
211 int b2; 181 int b2;
212 json_t *json; 182 json_t *json;
213 struct GNUNET_JSON_Specification pspec[] = { 183 struct GNUNET_JSON_Specification pspec[] = {GNUNET_JSON_spec_boolean ("b1",
214 GNUNET_JSON_spec_boolean ("b1", &b1), 184 &b1),
215 GNUNET_JSON_spec_boolean ("b2", &b2), 185 GNUNET_JSON_spec_boolean ("b2",
216 GNUNET_JSON_spec_end() 186 &b2),
217 }; 187 GNUNET_JSON_spec_end ()};
218 188
219 json = json_object (); 189 json = json_object ();
220 json_object_set_new (json, "b1", json_true ()); 190 json_object_set_new (json, "b1", json_true ());
221 json_object_set_new (json, "b2", json_false ()); 191 json_object_set_new (json, "b2", json_false ());
222 192
223 GNUNET_assert (GNUNET_OK == 193 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL));
224 GNUNET_JSON_parse (json, pspec,
225 NULL, NULL));
226 194
227 GNUNET_assert (GNUNET_YES == b1); 195 GNUNET_assert (GNUNET_YES == b1);
228 GNUNET_assert (GNUNET_NO == b2); 196 GNUNET_assert (GNUNET_NO == b2);
229 197
230 json_object_set_new (json, "b1", json_integer (42)); 198 json_object_set_new (json, "b1", json_integer (42));
231 199
232 GNUNET_assert (GNUNET_OK != 200 GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL));
233 GNUNET_JSON_parse (json, pspec,
234 NULL, NULL));
235 201
236 return 0; 202 return 0;
237} 203}
238 204
239 205
240int 206int
241main(int argc, 207main (int argc, const char *const argv[])
242 const char *const argv[])
243{ 208{
244 GNUNET_log_setup ("test-json", 209 GNUNET_log_setup ("test-json", "WARNING", NULL);
245 "WARNING",
246 NULL);
247 if (0 != test_abs_time ()) 210 if (0 != test_abs_time ())
248 return 1; 211 return 1;
249 if (0 != test_rel_time ()) 212 if (0 != test_rel_time ())
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
33static json_t *bigj;
34
35static int global_ret;
36
37
38static int
39access_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
96int
97main (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 */