From 4444fb80284aa86fe24f3640a0b1e4c841a98f9a Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Fri, 3 May 2019 15:10:26 +0200 Subject: add test for json_mhd.c logic --- src/Makefile.am | 2 + src/json/Makefile.am | 26 +++++++- src/json/test_json.c | 129 +++++++++++++++------------------------- src/json/test_json_mhd.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 84 deletions(-) create mode 100644 src/json/test_json_mhd.c 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 endif endif +if HAVE_MHD if HAVE_JSON JSON_DIR = json if HAVE_LIBGNURL @@ -33,6 +34,7 @@ if HAVE_LIBCURL endif endif endif +endif if BUILD_PULSE_HELPERS CONVERSATION_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 = \ $(XLIB) check_PROGRAMS = \ - test_json + test_json \ + test_json_mhd TESTS = \ $(check_PROGRAMS) @@ -36,3 +37,26 @@ test_json_LDADD = \ libgnunetjson.la \ $(top_builddir)/src/util/libgnunetutil.la \ -ljansson + + +if HAVE_LIBGNURL +LIB_GNURL=@LIBGNURL@ +CPP_GNURL=@LIBGNURL_CPPFLAGS@ +else +if HAVE_LIBCURL +LIB_GNURL=@LIBCURL@ +CPP_GNURL=@LIBCURL_CPPFLAGS@ +endif +endif + + +test_json_mhd_SOURCES = \ + test_json_mhd.c +test_json_mhd_LDADD = \ + libgnunetjson.la \ + $(top_builddir)/src/util/libgnunetutil.la \ + -ljansson \ + -lmicrohttpd \ + $(LIB_GNURL) +test_json_mhd_CPPFLAGS = \ + $(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 @@ 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 . @@ -39,32 +39,26 @@ test_abs_time () json_t *j; struct GNUNET_TIME_Absolute a1; struct GNUNET_TIME_Absolute a2; - struct GNUNET_JSON_Specification s1[] = { - GNUNET_JSON_spec_absolute_time (NULL, &a2), - GNUNET_JSON_spec_end() - }; - struct GNUNET_JSON_Specification s2[] = { - GNUNET_JSON_spec_absolute_time (NULL, &a2), - GNUNET_JSON_spec_end() - }; + struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_absolute_time (NULL, + &a2), + GNUNET_JSON_spec_end ()}; + struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_absolute_time (NULL, + &a2), + GNUNET_JSON_spec_end ()}; a1 = GNUNET_TIME_absolute_get (); GNUNET_TIME_round_abs (&a1); j = GNUNET_JSON_from_time_abs (a1); GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (j, s1, NULL, NULL)); - GNUNET_assert (a1.abs_value_us == - a2.abs_value_us); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); + GNUNET_assert (a1.abs_value_us == a2.abs_value_us); json_decref (j); a1 = GNUNET_TIME_UNIT_FOREVER_ABS; j = GNUNET_JSON_from_time_abs (a1); GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (j, s2, NULL, NULL)); - GNUNET_assert (a1.abs_value_us == - a2.abs_value_us); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); + GNUNET_assert (a1.abs_value_us == a2.abs_value_us); json_decref (j); return 0; } @@ -81,31 +75,25 @@ test_rel_time () json_t *j; struct GNUNET_TIME_Relative r1; struct GNUNET_TIME_Relative r2; - struct GNUNET_JSON_Specification s1[] = { - GNUNET_JSON_spec_relative_time (NULL, &r2), - GNUNET_JSON_spec_end() - }; - struct GNUNET_JSON_Specification s2[] = { - GNUNET_JSON_spec_relative_time (NULL, &r2), - GNUNET_JSON_spec_end() - }; + struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_relative_time (NULL, + &r2), + GNUNET_JSON_spec_end ()}; + struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_relative_time (NULL, + &r2), + GNUNET_JSON_spec_end ()}; r1 = GNUNET_TIME_UNIT_SECONDS; j = GNUNET_JSON_from_time_rel (r1); GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (j, s1, NULL, NULL)); - GNUNET_assert (r1.rel_value_us == - r2.rel_value_us); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); + GNUNET_assert (r1.rel_value_us == r2.rel_value_us); json_decref (j); r1 = GNUNET_TIME_UNIT_FOREVER_REL; j = GNUNET_JSON_from_time_rel (r1); GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (j, s2, NULL, NULL)); - GNUNET_assert (r1.rel_value_us == - r2.rel_value_us); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); + GNUNET_assert (r1.rel_value_us == r2.rel_value_us); json_decref (j); return 0; } @@ -123,24 +111,19 @@ test_raw () unsigned int i; json_t *j; - for (i=0;i<=256;i++) + for (i = 0; i <= 256; i++) { char blob2[256]; - struct GNUNET_JSON_Specification spec[] = { - GNUNET_JSON_spec_fixed (NULL, blob2, i), - GNUNET_JSON_spec_end() - }; + struct GNUNET_JSON_Specification spec[] = {GNUNET_JSON_spec_fixed (NULL, + blob2, + i), + GNUNET_JSON_spec_end ()}; memset (blob, i, i); j = GNUNET_JSON_from_data (blob, i); GNUNET_assert (NULL != j); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (j, spec, - NULL, NULL)); - GNUNET_assert (0 == - memcmp (blob, - blob2, - i)); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL)); + GNUNET_assert (0 == memcmp (blob, blob2, i)); } return 0; } @@ -156,16 +139,12 @@ test_rsa () { struct GNUNET_CRYPTO_RsaPublicKey *pub; struct GNUNET_CRYPTO_RsaPublicKey *pub2; - struct GNUNET_JSON_Specification pspec[] = { - GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), - GNUNET_JSON_spec_end() - }; + struct GNUNET_JSON_Specification pspec[] = + {GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end ()}; struct GNUNET_CRYPTO_RsaSignature *sig; struct GNUNET_CRYPTO_RsaSignature *sig2; - struct GNUNET_JSON_Specification sspec[] = { - GNUNET_JSON_spec_rsa_signature (NULL, &sig2), - GNUNET_JSON_spec_end() - }; + struct GNUNET_JSON_Specification sspec[] = + {GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end ()}; struct GNUNET_CRYPTO_RsaPrivateKey *priv; struct GNUNET_HashCode msg; json_t *jp; @@ -174,22 +153,13 @@ test_rsa () priv = GNUNET_CRYPTO_rsa_private_key_create (1024); pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); memset (&msg, 42, sizeof (msg)); - sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, - &msg); + sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg); GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub))); GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig))); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (jp, pspec, - NULL, NULL)); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (js, sspec, - NULL, NULL)); - GNUNET_break (0 == - GNUNET_CRYPTO_rsa_signature_cmp (sig, - sig2)); - GNUNET_break (0 == - GNUNET_CRYPTO_rsa_public_key_cmp (pub, - pub2)); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (jp, pspec, NULL, NULL)); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL)); + GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2)); + GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2)); GNUNET_CRYPTO_rsa_signature_free (sig); GNUNET_CRYPTO_rsa_signature_free (sig2); GNUNET_CRYPTO_rsa_private_key_free (priv); @@ -210,40 +180,33 @@ test_boolean () int b1; int b2; json_t *json; - struct GNUNET_JSON_Specification pspec[] = { - GNUNET_JSON_spec_boolean ("b1", &b1), - GNUNET_JSON_spec_boolean ("b2", &b2), - GNUNET_JSON_spec_end() - }; + struct GNUNET_JSON_Specification pspec[] = {GNUNET_JSON_spec_boolean ("b1", + &b1), + GNUNET_JSON_spec_boolean ("b2", + &b2), + GNUNET_JSON_spec_end ()}; json = json_object (); json_object_set_new (json, "b1", json_true ()); json_object_set_new (json, "b2", json_false ()); - GNUNET_assert (GNUNET_OK == - GNUNET_JSON_parse (json, pspec, - NULL, NULL)); + GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL)); GNUNET_assert (GNUNET_YES == b1); GNUNET_assert (GNUNET_NO == b2); json_object_set_new (json, "b1", json_integer (42)); - GNUNET_assert (GNUNET_OK != - GNUNET_JSON_parse (json, pspec, - NULL, NULL)); + GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL)); return 0; } int -main(int argc, - const char *const argv[]) +main (int argc, const char *const argv[]) { - GNUNET_log_setup ("test-json", - "WARNING", - NULL); + GNUNET_log_setup ("test-json", "WARNING", NULL); if (0 != test_abs_time ()) return 1; 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 @@ +/* + 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 . + + SPDX-License-Identifier: AGPL3.0-or-later +*/ + +/** + * @file json/test_json_mhd.c + * @brief Tests for JSON MHD integration functions + * @author Christian Grothoff + */ +#include "platform.h" +#include "gnunet_util_lib.h" +#include "gnunet_json_lib.h" +#include "gnunet_curl_lib.h" + +#define MAX_SIZE 1024 * 1024 + +static json_t *bigj; + +static int global_ret; + + +static int +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 (2, "OK", 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; + long post_data_size; + void *post_data; + + 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)); + } + post_data = json_dumps (bigj, JSON_INDENT (2)); + post_data_size = strlen (post_data); + + 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, 1); + 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); + if (0 != curl_easy_perform (easy)) + { + GNUNET_break (0); + MHD_stop_daemon (daemon); + GNUNET_free (url); + json_decref (bigj); + return 1; + } + MHD_stop_daemon (daemon); + GNUNET_free (url); + json_decref (bigj); + return global_ret; +} + +/* end of test_json_mhd.c */ -- cgit v1.2.3