public.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "public.h"
  23. #include <civetweb.h>
  24. #if defined(_WIN32)
  25. #include <Windows.h>
  26. #define mg_sleep(x) (Sleep(x))
  27. #else
  28. #include <unistd.h>
  29. #define mg_sleep(x) (usleep((x)*1000))
  30. #endif
  31. /* This unit test file uses the excellent Check unit testing library.
  32. * The API documentation is available here:
  33. * http://check.sourceforge.net/doc/check_html/index.html
  34. */
  35. START_TEST (test_mg_get_cookie)
  36. {
  37. char buf[20];
  38. ck_assert_int_eq(-2, mg_get_cookie("", "foo", NULL, sizeof(buf)));
  39. ck_assert_int_eq(-2, mg_get_cookie("", "foo", buf, 0));
  40. ck_assert_int_eq(-1, mg_get_cookie("", "foo", buf, sizeof(buf)));
  41. ck_assert_int_eq(-1, mg_get_cookie("", NULL, buf, sizeof(buf)));
  42. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)));
  43. ck_assert_str_eq("1", buf);
  44. ck_assert_int_eq(1, mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)));
  45. ck_assert_str_eq("2", buf);
  46. ck_assert_int_eq(3, mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)));
  47. ck_assert_str_eq("123", buf);
  48. ck_assert_int_eq(-1, mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)));
  49. }
  50. END_TEST
  51. START_TEST (test_mg_start_stop_server)
  52. {
  53. struct mg_context *ctx;
  54. const char *OPTIONS[] = {
  55. "document_root", ".",
  56. "listening_ports", "8080",
  57. NULL,
  58. };
  59. ctx = mg_start(NULL, NULL, OPTIONS);
  60. ck_assert(ctx != NULL);
  61. mg_sleep(1000);
  62. mg_stop(ctx);
  63. }
  64. END_TEST
  65. Suite * make_public_suite (void) {
  66. Suite * const suite = suite_create("Public");
  67. TCase * const cookies = tcase_create("Cookies");
  68. TCase * const startserver = tcase_create("StartServer");
  69. tcase_add_test(cookies, test_mg_get_cookie);
  70. suite_add_tcase(suite, cookies);
  71. tcase_add_test(startserver, test_mg_start_stop_server);
  72. suite_add_tcase(suite, startserver);
  73. return suite;
  74. }