public.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_get_cookie)
  37. {
  38. char buf[20];
  39. ck_assert_int_eq(-2, mg_get_cookie("", "foo", NULL, sizeof(buf)));
  40. ck_assert_int_eq(-2, mg_get_cookie("", "foo", buf, 0));
  41. ck_assert_int_eq(-1, mg_get_cookie("", "foo", buf, sizeof(buf)));
  42. ck_assert_int_eq(-1, mg_get_cookie("", NULL, buf, sizeof(buf)));
  43. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)));
  44. ck_assert_str_eq("1", buf);
  45. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)));
  46. ck_assert_str_eq("2", buf);
  47. ck_assert_int_eq(3, mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)));
  48. ck_assert_str_eq("123", buf);
  49. ck_assert_int_eq(-1, mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)));
  50. }
  51. END_TEST
  52. START_TEST (test_mg_url_encode)
  53. {
  54. char buf[20];
  55. int ret;
  56. memset(buf, 77, sizeof(buf));
  57. ret = mg_url_encode("abc", buf, sizeof(buf));
  58. ck_assert_int_eq(3, ret);
  59. ck_assert_str_eq("abc", buf);
  60. memset(buf, 77, sizeof(buf));
  61. ret = mg_url_encode("a%b/c&d.e", buf, sizeof(buf));
  62. ck_assert_int_eq(15, ret);
  63. ck_assert_str_eq("a%25b%2fc%26d.e", buf);
  64. memset(buf, 77, sizeof(buf));
  65. ret = mg_url_encode("%%%", buf, 4);
  66. ck_assert_int_eq(-1, ret);
  67. ck_assert_str_eq("%25", buf);
  68. }
  69. END_TEST
  70. START_TEST (test_mg_url_decode)
  71. {
  72. char buf[20];
  73. int ret;
  74. ret = mg_url_decode("abc", 3, buf, sizeof(buf), 0);
  75. ck_assert_int_eq(ret, 3);
  76. ck_assert_str_eq(buf, "abc");
  77. ret = mg_url_decode("abcdef", 3, buf, sizeof(buf), 0);
  78. ck_assert_int_eq(ret, 3);
  79. ck_assert_str_eq(buf, "abc");
  80. ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 0);
  81. ck_assert_int_eq(ret, 3);
  82. ck_assert_str_eq(buf, "x+y");
  83. ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 1);
  84. ck_assert_int_eq(ret, 3);
  85. ck_assert_str_eq(buf, "x y");
  86. ret = mg_url_decode("%25", 3, buf, sizeof(buf), 1);
  87. ck_assert_int_eq(ret, 1);
  88. ck_assert_str_eq(buf, "%");
  89. }
  90. END_TEST
  91. START_TEST (test_mg_start_stop_http_server)
  92. {
  93. struct mg_context *ctx;
  94. const char *OPTIONS[] = {
  95. "document_root", ".",
  96. "listening_ports", "8080",
  97. NULL,
  98. };
  99. ctx = mg_start(NULL, NULL, OPTIONS);
  100. ck_assert(ctx != NULL);
  101. mg_Sleep(2);
  102. mg_stop(ctx);
  103. }
  104. END_TEST
  105. START_TEST (test_mg_start_stop_https_server)
  106. {
  107. struct mg_context *ctx;
  108. const char *OPTIONS[] = {
  109. "document_root", ".",
  110. "listening_ports", "8080,8443s",
  111. "ssl_certificate", "resources/ssl_cert.pem", // TODO: check working path of CI test system
  112. NULL,
  113. };
  114. ctx = mg_start(NULL, NULL, OPTIONS);
  115. ck_assert(ctx != NULL);
  116. mg_Sleep(2);
  117. mg_stop(ctx);
  118. }
  119. END_TEST
  120. Suite * make_public_suite (void) {
  121. Suite * const suite = suite_create("Public");
  122. TCase * const urlencodingdecoding = tcase_create("URL encoding decoding");
  123. TCase * const cookies = tcase_create("Cookies");
  124. TCase * const startstophttp = tcase_create("Start Stop HTTP Server");
  125. TCase * const startstophttps = tcase_create("Start Stop HTTPS Server");
  126. tcase_add_test(urlencodingdecoding, test_mg_url_encode);
  127. tcase_add_test(urlencodingdecoding, test_mg_url_decode);
  128. suite_add_tcase(suite, urlencodingdecoding);
  129. tcase_add_test(cookies, test_mg_get_cookie);
  130. suite_add_tcase(suite, cookies);
  131. tcase_add_test(startstophttp, test_mg_start_stop_http_server);
  132. suite_add_tcase(suite, startstophttp);
  133. tcase_add_test(startstophttps, test_mg_start_stop_https_server);
  134. suite_add_tcase(suite, startstophttps);
  135. return suite;
  136. }