public.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_start_stop_server)
  53. {
  54. struct mg_context *ctx;
  55. const char *OPTIONS[] = {
  56. "document_root", ".",
  57. "listening_ports", "8080",
  58. NULL,
  59. };
  60. ctx = mg_start(NULL, NULL, OPTIONS);
  61. ck_assert(ctx != NULL);
  62. mg_Sleep(2);
  63. mg_stop(ctx);
  64. }
  65. END_TEST
  66. Suite * make_public_suite (void) {
  67. Suite * const suite = suite_create("Public");
  68. TCase * const cookies = tcase_create("Cookies");
  69. TCase * const startserver = tcase_create("StartServer");
  70. tcase_add_test(cookies, test_mg_get_cookie);
  71. suite_add_tcase(suite, cookies);
  72. tcase_add_test(startserver, test_mg_start_stop_server);
  73. suite_add_tcase(suite, startserver);
  74. return suite;
  75. }