test_http_common.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. check_pass("http://test.local/path", "http", "test.local", HTTP_DEFAULT_PORT, "/path");
  134. check_pass("http://test.local/path/", "http", "test.local", HTTP_DEFAULT_PORT, "/path/");
  135. check_pass("http://test.local/path/more", "http", "test.local", HTTP_DEFAULT_PORT, "/path/more");
  136. check_pass("http://test.local:81", "http", "test.local", 81, "");
  137. check_pass("http://test.local:81/", "http", "test.local", 81, "/");
  138. check_pass("http://test.local:81/path", "http", "test.local", 81, "/path");
  139. check_pass("http://test.local:81/path/", "http", "test.local", 81, "/path/");
  140. check_pass("http://test.local:81/path/more", "http", "test.local", 81, "/path/more");
  141. }
  142. static void
  143. test_pass_ipv4 ()
  144. {
  145. check_pass("http://127.0.0.1", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "");
  146. check_pass("http://127.0.0.1/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/");
  147. check_pass("http://127.0.0.1/path", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path");
  148. check_pass("http://127.0.0.1/path/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path/");
  149. check_pass("http://127.0.0.1:81", "http", "127.0.0.1", 81, "");
  150. check_pass("http://127.0.0.1:81/", "http", "127.0.0.1", 81, "/");
  151. check_pass("http://127.0.0.1:81/path", "http", "127.0.0.1", 81, "/path");
  152. check_pass("http://127.0.0.1:81/path/", "http", "127.0.0.1", 81, "/path/");
  153. check_pass("http://127.0.0.1:81/path/more", "http", "127.0.0.1", 81, "/path/more");
  154. }
  155. static void
  156. test_fail_ipv6 ()
  157. {
  158. check_pass("http://[::1]", "http", "[::1]", HTTP_DEFAULT_PORT, "");
  159. check_pass("http://[::1]/", "http", "[::1]", HTTP_DEFAULT_PORT, "/");
  160. check_pass("http://[::1]/path", "http", "[::1]", HTTP_DEFAULT_PORT, "/path");
  161. check_pass("http://[::1]/path/", "http", "[::1]", HTTP_DEFAULT_PORT, "/path/");
  162. check_pass("http://[::1]:81", "http", "[::1]", 81, "");
  163. check_pass("http://[::1]:81/", "http", "[::1]", 81, "/");
  164. check_pass("http://[::1]:81/path", "http", "[::1]", 81, "/path");
  165. check_pass("http://[::1]:81/path/", "http", "[::1]", 81, "/path/");
  166. check_pass("http://[::1]:81/path/more", "http", "[::1]", 81, "/path/more");
  167. }
  168. static void
  169. test_fail ()
  170. {
  171. if (GNUNET_SYSERR == check_fail (""))
  172. GNUNET_break (0);
  173. if (GNUNET_SYSERR == check_fail ("http"))
  174. GNUNET_break (0);
  175. if (GNUNET_SYSERR == check_fail ("://"))
  176. GNUNET_break (0);
  177. if (GNUNET_SYSERR == check_fail ("http://"))
  178. GNUNET_break (0);
  179. if (GNUNET_SYSERR == check_fail ("//localhost"))
  180. GNUNET_break (0);
  181. if (GNUNET_SYSERR == check_fail ("//:80"))
  182. GNUNET_break (0);
  183. if (GNUNET_SYSERR == check_fail ("//:80/"))
  184. GNUNET_break (0);
  185. if (GNUNET_SYSERR == check_fail ("//:80:"))
  186. GNUNET_break (0);
  187. if (GNUNET_SYSERR == check_fail ("http://localhost:a/"))
  188. GNUNET_break (0);
  189. if (GNUNET_SYSERR == check_fail ("http://127.0.0.1:a/"))
  190. GNUNET_break (0);
  191. }
  192. int
  193. main (int argc, char *argv[])
  194. {
  195. int ret = 0;
  196. struct SplittedHTTPAddress * spa;
  197. GNUNET_log_setup ("test", "DEBUG", NULL);
  198. spa = http_split_address ("");
  199. if (NULL != spa)
  200. {
  201. clean (spa);
  202. spa = NULL;
  203. GNUNET_break (0);
  204. }
  205. spa = http_split_address ("http://");
  206. if (NULL != spa)
  207. {
  208. clean (spa);
  209. GNUNET_break (0);
  210. }
  211. spa = http_split_address ("://");
  212. if (NULL != spa)
  213. {
  214. clean (spa);
  215. GNUNET_break (0);
  216. }
  217. test_pass_hostname ();
  218. test_pass_ipv4 ();
  219. test_fail_ipv6 ();
  220. test_fail ();
  221. return ret;
  222. }
  223. /* end of test_http_common.c */