public.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* Copyright (c) 2015 the Civetweb developers
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include "public.h"
  24. #include <civetweb.h>
  25. #if defined(_WIN32)
  26. #include <Windows.h>
  27. #define mg_Sleep(x) (Sleep(x * 1000))
  28. #else
  29. #include <unistd.h>
  30. #define mg_Sleep(x) (sleep(x))
  31. #endif
  32. /* This unit test file uses the excellent Check unit testing library.
  33. * The API documentation is available here:
  34. * http://check.sourceforge.net/doc/check_html/index.html
  35. */
  36. START_TEST(test_mg_version)
  37. {
  38. const char *ver = mg_version();
  39. unsigned major = 0, minor = 0;
  40. int ret;
  41. ck_assert(ver != NULL);
  42. ck_assert_str_eq(ver, CIVETWEB_VERSION);
  43. /* check structure of version string */
  44. ret = sscanf(ver, "%u.%u", &major, &minor);
  45. ck_assert_int_eq(ret, 2);
  46. ck_assert_uint_ge(major, 1);
  47. }
  48. END_TEST
  49. START_TEST(test_mg_get_valid_options)
  50. {
  51. int i;
  52. const struct mg_option *default_options = mg_get_valid_options();
  53. ck_assert(default_options != NULL);
  54. for (i = 0; default_options[i].name != NULL; i++) {
  55. ck_assert(default_options[i].name != NULL);
  56. ck_assert(strlen(default_options[i].name) > 0);
  57. ck_assert(((int)default_options[i].type) > 0);
  58. }
  59. ck_assert(i > 0);
  60. }
  61. END_TEST
  62. START_TEST(test_mg_get_builtin_mime_type)
  63. {
  64. ck_assert_str_eq(mg_get_builtin_mime_type("x.txt"), "text/plain");
  65. ck_assert_str_eq(mg_get_builtin_mime_type("x.html"), "text/html");
  66. ck_assert_str_eq(mg_get_builtin_mime_type("x.HTML"), "text/html");
  67. ck_assert_str_eq(mg_get_builtin_mime_type("x.hTmL"), "text/html");
  68. ck_assert_str_eq(mg_get_builtin_mime_type("/abc/def/ghi.htm"), "text/html");
  69. ck_assert_str_eq(mg_get_builtin_mime_type("x.unknown_extention_xyz"),
  70. "text/plain");
  71. }
  72. END_TEST
  73. START_TEST(test_mg_strncasecmp)
  74. {
  75. ck_assert(mg_strncasecmp("abc", "abc", 3) == 0);
  76. ck_assert(mg_strncasecmp("abc", "abcd", 3) == 0);
  77. ck_assert(mg_strncasecmp("abc", "abcd", 4) != 0);
  78. ck_assert(mg_strncasecmp("a", "A", 1) == 0);
  79. ck_assert(mg_strncasecmp("A", "B", 1) < 0);
  80. ck_assert(mg_strncasecmp("A", "b", 1) < 0);
  81. ck_assert(mg_strncasecmp("a", "B", 1) < 0);
  82. ck_assert(mg_strncasecmp("a", "b", 1) < 0);
  83. ck_assert(mg_strncasecmp("b", "A", 1) > 0);
  84. ck_assert(mg_strncasecmp("B", "A", 1) > 0);
  85. ck_assert(mg_strncasecmp("b", "a", 1) > 0);
  86. ck_assert(mg_strncasecmp("B", "a", 1) > 0);
  87. ck_assert(mg_strncasecmp("xAx", "xBx", 3) < 0);
  88. ck_assert(mg_strncasecmp("xAx", "xbx", 3) < 0);
  89. ck_assert(mg_strncasecmp("xax", "xBx", 3) < 0);
  90. ck_assert(mg_strncasecmp("xax", "xbx", 3) < 0);
  91. ck_assert(mg_strncasecmp("xbx", "xAx", 3) > 0);
  92. ck_assert(mg_strncasecmp("xBx", "xAx", 3) > 0);
  93. ck_assert(mg_strncasecmp("xbx", "xax", 3) > 0);
  94. ck_assert(mg_strncasecmp("xBx", "xax", 3) > 0);
  95. }
  96. END_TEST
  97. START_TEST(test_mg_get_cookie)
  98. {
  99. char buf[20];
  100. ck_assert_int_eq(-2, mg_get_cookie("", "foo", NULL, sizeof(buf)));
  101. ck_assert_int_eq(-2, mg_get_cookie("", "foo", buf, 0));
  102. ck_assert_int_eq(-1, mg_get_cookie("", "foo", buf, sizeof(buf)));
  103. ck_assert_int_eq(-1, mg_get_cookie("", NULL, buf, sizeof(buf)));
  104. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)));
  105. ck_assert_str_eq("1", buf);
  106. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)));
  107. ck_assert_str_eq("2", buf);
  108. ck_assert_int_eq(3, mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)));
  109. ck_assert_str_eq("123", buf);
  110. ck_assert_int_eq(-1,
  111. mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)));
  112. }
  113. END_TEST
  114. START_TEST(test_mg_md5)
  115. {
  116. char buf[33];
  117. char *ret;
  118. memset(buf, 77, sizeof(buf));
  119. ret = mg_md5(buf, NULL);
  120. ck_assert_str_eq(buf, "d41d8cd98f00b204e9800998ecf8427e");
  121. ck_assert_str_eq(ret, "d41d8cd98f00b204e9800998ecf8427e");
  122. ck_assert_ptr_eq(ret, buf);
  123. memset(buf, 77, sizeof(buf));
  124. ret = mg_md5(buf, "The quick brown fox jumps over the lazy dog.", NULL);
  125. ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0");
  126. ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0");
  127. ck_assert_ptr_eq(ret, buf);
  128. memset(buf, 77, sizeof(buf));
  129. ret = mg_md5(buf,
  130. "",
  131. "The qu",
  132. "ick bro",
  133. "",
  134. "wn fox ju",
  135. "m",
  136. "ps over the la",
  137. "",
  138. "",
  139. "zy dog.",
  140. "",
  141. NULL);
  142. ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0");
  143. ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0");
  144. ck_assert_ptr_eq(ret, buf);
  145. }
  146. END_TEST
  147. START_TEST(test_mg_url_encode)
  148. {
  149. char buf[20];
  150. int ret;
  151. memset(buf, 77, sizeof(buf));
  152. ret = mg_url_encode("abc", buf, sizeof(buf));
  153. ck_assert_int_eq(3, ret);
  154. ck_assert_str_eq("abc", buf);
  155. memset(buf, 77, sizeof(buf));
  156. ret = mg_url_encode("a%b/c&d.e", buf, sizeof(buf));
  157. ck_assert_int_eq(15, ret);
  158. ck_assert_str_eq("a%25b%2fc%26d.e", buf);
  159. memset(buf, 77, sizeof(buf));
  160. ret = mg_url_encode("%%%", buf, 4);
  161. ck_assert_int_eq(-1, ret);
  162. ck_assert_str_eq("%25", buf);
  163. }
  164. END_TEST
  165. START_TEST(test_mg_url_decode)
  166. {
  167. char buf[20];
  168. int ret;
  169. ret = mg_url_decode("abc", 3, buf, sizeof(buf), 0);
  170. ck_assert_int_eq(ret, 3);
  171. ck_assert_str_eq(buf, "abc");
  172. ret = mg_url_decode("abcdef", 3, buf, sizeof(buf), 0);
  173. ck_assert_int_eq(ret, 3);
  174. ck_assert_str_eq(buf, "abc");
  175. ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 0);
  176. ck_assert_int_eq(ret, 3);
  177. ck_assert_str_eq(buf, "x+y");
  178. ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 1);
  179. ck_assert_int_eq(ret, 3);
  180. ck_assert_str_eq(buf, "x y");
  181. ret = mg_url_decode("%25", 3, buf, sizeof(buf), 1);
  182. ck_assert_int_eq(ret, 1);
  183. ck_assert_str_eq(buf, "%");
  184. }
  185. END_TEST
  186. START_TEST(test_mg_start_stop_http_server)
  187. {
  188. struct mg_context *ctx;
  189. const char *OPTIONS[] = {
  190. "document_root", ".", "listening_ports", "8080", NULL,
  191. };
  192. ctx = mg_start(NULL, NULL, OPTIONS);
  193. ck_assert(ctx != NULL);
  194. mg_Sleep(2);
  195. mg_stop(ctx);
  196. }
  197. END_TEST
  198. START_TEST(test_mg_start_stop_https_server)
  199. {
  200. struct mg_context *ctx;
  201. const char *OPTIONS[] = {
  202. "document_root",
  203. ".",
  204. "listening_ports",
  205. "8080,8443s",
  206. "ssl_certificate",
  207. "resources/ssl_cert.pem", // TODO: check working path of CI test system
  208. NULL,
  209. };
  210. ctx = mg_start(NULL, NULL, OPTIONS);
  211. ck_assert(ctx != NULL);
  212. mg_Sleep(2);
  213. mg_stop(ctx);
  214. }
  215. END_TEST
  216. Suite *make_public_suite(void)
  217. {
  218. Suite *const suite = suite_create("Public");
  219. TCase *const version = tcase_create("Version");
  220. TCase *const get_valid_options = tcase_create("Options");
  221. TCase *const get_builtin_mime_type = tcase_create("MIME types");
  222. TCase *const tstrncasecmp = tcase_create("strcasecmp");
  223. TCase *const urlencodingdecoding = tcase_create("URL encoding decoding");
  224. TCase *const cookies = tcase_create("Cookies");
  225. TCase *const md5 = tcase_create("MD5");
  226. TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
  227. TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
  228. tcase_add_test(version, test_mg_version);
  229. suite_add_tcase(suite, version);
  230. tcase_add_test(get_valid_options, test_mg_get_valid_options);
  231. suite_add_tcase(suite, get_valid_options);
  232. tcase_add_test(get_builtin_mime_type, test_mg_get_builtin_mime_type);
  233. suite_add_tcase(suite, get_builtin_mime_type);
  234. tcase_add_test(tstrncasecmp, test_mg_strncasecmp);
  235. suite_add_tcase(suite, tstrncasecmp);
  236. tcase_add_test(urlencodingdecoding, test_mg_url_encode);
  237. tcase_add_test(urlencodingdecoding, test_mg_url_decode);
  238. suite_add_tcase(suite, urlencodingdecoding);
  239. tcase_add_test(cookies, test_mg_get_cookie);
  240. suite_add_tcase(suite, cookies);
  241. tcase_add_test(md5, test_mg_md5);
  242. suite_add_tcase(suite, md5);
  243. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  244. suite_add_tcase(suite, startstophttp);
  245. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  246. suite_add_tcase(suite, startstophttps);
  247. return suite;
  248. }