test_http_common.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010 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 transport/test_http_common.c
  18. * @brief base test case for common http functionality
  19. */
  20. #include "platform.h"
  21. #include "gnunet_transport_service.h"
  22. #include "transport-testing.h"
  23. #include "plugin_transport_http_common.h"
  24. static void
  25. clean (struct SplittedHTTPAddress *addr)
  26. {
  27. if (NULL == addr)
  28. return;
  29. GNUNET_free_non_null (addr->host);
  30. GNUNET_free_non_null (addr->path);
  31. GNUNET_free_non_null (addr->protocol);
  32. GNUNET_free (addr);
  33. }
  34. static int
  35. check (struct SplittedHTTPAddress *addr,
  36. const char *protocol,
  37. const char *host,
  38. int port,
  39. const char *path)
  40. {
  41. if (NULL == addr)
  42. return GNUNET_NO;
  43. if (((NULL == addr->protocol) && (NULL != protocol)) ||
  44. ((NULL != addr->protocol) && (NULL == protocol)))
  45. {
  46. GNUNET_break (0);
  47. return GNUNET_NO;
  48. }
  49. else if ((NULL != addr->protocol) && (NULL != protocol))
  50. {
  51. if (0 != strcmp (addr->protocol, protocol))
  52. {
  53. GNUNET_break (0);
  54. return GNUNET_NO;
  55. }
  56. }
  57. if (((NULL == addr->host) && (NULL != host)) ||
  58. ((NULL != addr->host) && (NULL == host)))
  59. {
  60. GNUNET_break (0);
  61. return GNUNET_NO;
  62. }
  63. else if ((NULL != addr->host) && (NULL != host))
  64. {
  65. if (0 != strcmp (addr->host, host))
  66. {
  67. GNUNET_break (0);
  68. return GNUNET_NO;
  69. }
  70. }
  71. if (((NULL == addr->path) && (NULL != path)) ||
  72. ((NULL != addr->path) && (NULL == path)))
  73. {
  74. GNUNET_break (0);
  75. return GNUNET_NO;
  76. }
  77. else if ((NULL != addr->path) && (NULL != path))
  78. {
  79. if (0 != strcmp (addr->path, path))
  80. {
  81. GNUNET_break (0);
  82. return GNUNET_NO;
  83. }
  84. }
  85. if ((addr->port != port))
  86. {
  87. GNUNET_break (0);
  88. return GNUNET_NO;
  89. }
  90. return GNUNET_OK;
  91. }
  92. static int
  93. check_pass (const char *src,
  94. const char *protocol,
  95. const char *host,
  96. int port,
  97. const char *path)
  98. {
  99. struct SplittedHTTPAddress *spa;
  100. spa = http_split_address (src);
  101. if (NULL == spa)
  102. {
  103. GNUNET_break (0);
  104. return GNUNET_SYSERR;
  105. }
  106. if (GNUNET_OK != check (spa, protocol, host, port, path))
  107. {
  108. clean (spa);
  109. GNUNET_break (0);
  110. return GNUNET_SYSERR;
  111. }
  112. clean (spa);
  113. return GNUNET_OK;
  114. }
  115. static int
  116. check_fail (const char *src)
  117. {
  118. struct SplittedHTTPAddress *spa;
  119. spa = http_split_address (src);
  120. if (NULL != spa)
  121. {
  122. GNUNET_break (0);
  123. clean (spa);
  124. return GNUNET_SYSERR;
  125. }
  126. return GNUNET_OK;
  127. }
  128. static void
  129. test_pass_hostname ()
  130. {
  131. check_pass ("http://test.local", "http", "test.local", HTTP_DEFAULT_PORT, "");
  132. check_pass ("http://test.local/", "http", "test.local", HTTP_DEFAULT_PORT,
  133. "/");
  134. check_pass ("http://test.local/path", "http", "test.local", HTTP_DEFAULT_PORT,
  135. "/path");
  136. check_pass ("http://test.local/path/", "http", "test.local",
  137. HTTP_DEFAULT_PORT, "/path/");
  138. check_pass ("http://test.local/path/more", "http", "test.local",
  139. HTTP_DEFAULT_PORT, "/path/more");
  140. check_pass ("http://test.local:81", "http", "test.local", 81, "");
  141. check_pass ("http://test.local:81/", "http", "test.local", 81, "/");
  142. check_pass ("http://test.local:81/path", "http", "test.local", 81, "/path");
  143. check_pass ("http://test.local:81/path/", "http", "test.local", 81, "/path/");
  144. check_pass ("http://test.local:81/path/more", "http", "test.local", 81,
  145. "/path/more");
  146. }
  147. static void
  148. test_pass_ipv4 ()
  149. {
  150. check_pass ("http://127.0.0.1", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "");
  151. check_pass ("http://127.0.0.1/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/");
  152. check_pass ("http://127.0.0.1/path", "http", "127.0.0.1", HTTP_DEFAULT_PORT,
  153. "/path");
  154. check_pass ("http://127.0.0.1/path/", "http", "127.0.0.1", HTTP_DEFAULT_PORT,
  155. "/path/");
  156. check_pass ("http://127.0.0.1:81", "http", "127.0.0.1", 81, "");
  157. check_pass ("http://127.0.0.1:81/", "http", "127.0.0.1", 81, "/");
  158. check_pass ("http://127.0.0.1:81/path", "http", "127.0.0.1", 81, "/path");
  159. check_pass ("http://127.0.0.1:81/path/", "http", "127.0.0.1", 81, "/path/");
  160. check_pass ("http://127.0.0.1:81/path/more", "http", "127.0.0.1", 81,
  161. "/path/more");
  162. }
  163. static void
  164. test_fail_ipv6 ()
  165. {
  166. check_pass ("http://[::1]", "http", "[::1]", HTTP_DEFAULT_PORT, "");
  167. check_pass ("http://[::1]/", "http", "[::1]", HTTP_DEFAULT_PORT, "/");
  168. check_pass ("http://[::1]/path", "http", "[::1]", HTTP_DEFAULT_PORT, "/path");
  169. check_pass ("http://[::1]/path/", "http", "[::1]", HTTP_DEFAULT_PORT,
  170. "/path/");
  171. check_pass ("http://[::1]:81", "http", "[::1]", 81, "");
  172. check_pass ("http://[::1]:81/", "http", "[::1]", 81, "/");
  173. check_pass ("http://[::1]:81/path", "http", "[::1]", 81, "/path");
  174. check_pass ("http://[::1]:81/path/", "http", "[::1]", 81, "/path/");
  175. check_pass ("http://[::1]:81/path/more", "http", "[::1]", 81, "/path/more");
  176. }
  177. static void
  178. test_fail ()
  179. {
  180. if (GNUNET_SYSERR == check_fail (""))
  181. GNUNET_break (0);
  182. if (GNUNET_SYSERR == check_fail ("http"))
  183. GNUNET_break (0);
  184. if (GNUNET_SYSERR == check_fail ("://"))
  185. GNUNET_break (0);
  186. if (GNUNET_SYSERR == check_fail ("http://"))
  187. GNUNET_break (0);
  188. if (GNUNET_SYSERR == check_fail ("//localhost"))
  189. GNUNET_break (0);
  190. if (GNUNET_SYSERR == check_fail ("//:80"))
  191. GNUNET_break (0);
  192. if (GNUNET_SYSERR == check_fail ("//:80/"))
  193. GNUNET_break (0);
  194. if (GNUNET_SYSERR == check_fail ("//:80:"))
  195. GNUNET_break (0);
  196. if (GNUNET_SYSERR == check_fail ("http://localhost:a/"))
  197. GNUNET_break (0);
  198. if (GNUNET_SYSERR == check_fail ("http://127.0.0.1:a/"))
  199. GNUNET_break (0);
  200. }
  201. int
  202. main (int argc, char *argv[])
  203. {
  204. int ret = 0;
  205. struct SplittedHTTPAddress *spa;
  206. GNUNET_log_setup ("test", "DEBUG", NULL);
  207. spa = http_split_address ("");
  208. if (NULL != spa)
  209. {
  210. clean (spa);
  211. spa = NULL;
  212. GNUNET_break (0);
  213. }
  214. spa = http_split_address ("http://");
  215. if (NULL != spa)
  216. {
  217. clean (spa);
  218. GNUNET_break (0);
  219. }
  220. spa = http_split_address ("://");
  221. if (NULL != spa)
  222. {
  223. clean (spa);
  224. GNUNET_break (0);
  225. }
  226. test_pass_hostname ();
  227. test_pass_ipv4 ();
  228. test_fail_ipv6 ();
  229. test_fail ();
  230. return ret;
  231. }
  232. /* end of test_http_common.c */