test_json_mhd.c 5.1 KB

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