public.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_cookie)
  50. {
  51. char buf[20];
  52. ck_assert_int_eq(-2, mg_get_cookie("", "foo", NULL, sizeof(buf)));
  53. ck_assert_int_eq(-2, mg_get_cookie("", "foo", buf, 0));
  54. ck_assert_int_eq(-1, mg_get_cookie("", "foo", buf, sizeof(buf)));
  55. ck_assert_int_eq(-1, mg_get_cookie("", NULL, buf, sizeof(buf)));
  56. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)));
  57. ck_assert_str_eq("1", buf);
  58. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)));
  59. ck_assert_str_eq("2", buf);
  60. ck_assert_int_eq(3, mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)));
  61. ck_assert_str_eq("123", buf);
  62. ck_assert_int_eq(-1, mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)));
  63. }
  64. END_TEST
  65. START_TEST (test_mg_md5)
  66. {
  67. char buf[33];
  68. char *ret;
  69. memset(buf, 77, sizeof(buf));
  70. ret = mg_md5(buf, NULL);
  71. ck_assert_str_eq(buf, "d41d8cd98f00b204e9800998ecf8427e");
  72. ck_assert_str_eq(ret, "d41d8cd98f00b204e9800998ecf8427e");
  73. ck_assert_ptr_eq(ret, buf);
  74. memset(buf, 77, sizeof(buf));
  75. ret = mg_md5(buf, "The quick brown fox jumps over the lazy dog.", NULL);
  76. ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0");
  77. ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0");
  78. ck_assert_ptr_eq(ret, buf);
  79. memset(buf, 77, sizeof(buf));
  80. ret = mg_md5(buf, "", "The qu", "ick bro", "", "wn fox ju", "m", "ps over the la", "", "", "zy dog.", "", NULL);
  81. ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0");
  82. ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0");
  83. ck_assert_ptr_eq(ret, buf);
  84. }
  85. END_TEST
  86. START_TEST (test_mg_url_encode)
  87. {
  88. char buf[20];
  89. int ret;
  90. memset(buf, 77, sizeof(buf));
  91. ret = mg_url_encode("abc", buf, sizeof(buf));
  92. ck_assert_int_eq(3, ret);
  93. ck_assert_str_eq("abc", buf);
  94. memset(buf, 77, sizeof(buf));
  95. ret = mg_url_encode("a%b/c&d.e", buf, sizeof(buf));
  96. ck_assert_int_eq(15, ret);
  97. ck_assert_str_eq("a%25b%2fc%26d.e", buf);
  98. memset(buf, 77, sizeof(buf));
  99. ret = mg_url_encode("%%%", buf, 4);
  100. ck_assert_int_eq(-1, ret);
  101. ck_assert_str_eq("%25", buf);
  102. }
  103. END_TEST
  104. START_TEST (test_mg_url_decode)
  105. {
  106. char buf[20];
  107. int ret;
  108. ret = mg_url_decode("abc", 3, buf, sizeof(buf), 0);
  109. ck_assert_int_eq(ret, 3);
  110. ck_assert_str_eq(buf, "abc");
  111. ret = mg_url_decode("abcdef", 3, buf, sizeof(buf), 0);
  112. ck_assert_int_eq(ret, 3);
  113. ck_assert_str_eq(buf, "abc");
  114. ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 0);
  115. ck_assert_int_eq(ret, 3);
  116. ck_assert_str_eq(buf, "x+y");
  117. ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 1);
  118. ck_assert_int_eq(ret, 3);
  119. ck_assert_str_eq(buf, "x y");
  120. ret = mg_url_decode("%25", 3, buf, sizeof(buf), 1);
  121. ck_assert_int_eq(ret, 1);
  122. ck_assert_str_eq(buf, "%");
  123. }
  124. END_TEST
  125. START_TEST (test_mg_start_stop_http_server)
  126. {
  127. struct mg_context *ctx;
  128. const char *OPTIONS[] = {
  129. "document_root", ".",
  130. "listening_ports", "8080",
  131. NULL,
  132. };
  133. ctx = mg_start(NULL, NULL, OPTIONS);
  134. ck_assert(ctx != NULL);
  135. mg_Sleep(2);
  136. mg_stop(ctx);
  137. }
  138. END_TEST
  139. START_TEST (test_mg_start_stop_https_server)
  140. {
  141. struct mg_context *ctx;
  142. const char *OPTIONS[] = {
  143. "document_root", ".",
  144. "listening_ports", "8080,8443s",
  145. "ssl_certificate", "resources/ssl_cert.pem", // TODO: check working path of CI test system
  146. NULL,
  147. };
  148. ctx = mg_start(NULL, NULL, OPTIONS);
  149. ck_assert(ctx != NULL);
  150. mg_Sleep(2);
  151. mg_stop(ctx);
  152. }
  153. END_TEST
  154. Suite * make_public_suite (void) {
  155. Suite * const suite = suite_create("Public");
  156. TCase * const version = tcase_create("Version");
  157. TCase * const urlencodingdecoding = tcase_create("URL encoding decoding");
  158. TCase * const cookies = tcase_create("Cookies");
  159. TCase * const md5 = tcase_create("MD5");
  160. TCase * const startstophttp = tcase_create("Start Stop HTTP Server");
  161. TCase * const startstophttps = tcase_create("Start Stop HTTPS Server");
  162. tcase_add_test(version, test_mg_version);
  163. suite_add_tcase(suite, version);
  164. tcase_add_test(urlencodingdecoding, test_mg_url_encode);
  165. tcase_add_test(urlencodingdecoding, test_mg_url_decode);
  166. suite_add_tcase(suite, urlencodingdecoding);
  167. tcase_add_test(cookies, test_mg_get_cookie);
  168. suite_add_tcase(suite, cookies);
  169. tcase_add_test(md5, test_mg_md5);
  170. suite_add_tcase(suite, md5);
  171. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  172. suite_add_tcase(suite, startstophttp);
  173. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  174. suite_add_tcase(suite, startstophttps);
  175. return suite;
  176. }