|
@@ -25,377 +25,15 @@
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
#include <stdio.h>
|
|
|
-#include <time.h>
|
|
|
-#include <sys/types.h>
|
|
|
-#include <sys/stat.h>
|
|
|
|
|
|
-#include "public.h"
|
|
|
+#include "public_func.h"
|
|
|
#include <civetweb.h>
|
|
|
|
|
|
-#if defined(_WIN32)
|
|
|
-#include <Windows.h>
|
|
|
-#define mg_Sleep(x) (Sleep(x * 1000))
|
|
|
-#else
|
|
|
-#include <unistd.h>
|
|
|
-#define mg_Sleep(x) (sleep(x))
|
|
|
-#endif
|
|
|
-
|
|
|
/* This unit test file uses the excellent Check unit testing library.
|
|
|
* The API documentation is available here:
|
|
|
* http://check.sourceforge.net/doc/check_html/index.html
|
|
|
*/
|
|
|
|
|
|
-START_TEST(test_mg_version)
|
|
|
-{
|
|
|
- const char *ver = mg_version();
|
|
|
- unsigned major = 0, minor = 0;
|
|
|
- int ret;
|
|
|
-
|
|
|
- ck_assert(ver != NULL);
|
|
|
- ck_assert_str_eq(ver, CIVETWEB_VERSION);
|
|
|
-
|
|
|
- /* check structure of version string */
|
|
|
- ret = sscanf(ver, "%u.%u", &major, &minor);
|
|
|
- ck_assert_int_eq(ret, 2);
|
|
|
- ck_assert_uint_ge(major, 1);
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_get_valid_options)
|
|
|
-{
|
|
|
- int i;
|
|
|
- const struct mg_option *default_options = mg_get_valid_options();
|
|
|
-
|
|
|
- ck_assert(default_options != NULL);
|
|
|
-
|
|
|
- for (i = 0; default_options[i].name != NULL; i++) {
|
|
|
- ck_assert(default_options[i].name != NULL);
|
|
|
- ck_assert(strlen(default_options[i].name) > 0);
|
|
|
- ck_assert(((int)default_options[i].type) > 0);
|
|
|
- }
|
|
|
-
|
|
|
- ck_assert(i > 0);
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_get_builtin_mime_type)
|
|
|
-{
|
|
|
- ck_assert_str_eq(mg_get_builtin_mime_type("x.txt"), "text/plain");
|
|
|
- ck_assert_str_eq(mg_get_builtin_mime_type("x.html"), "text/html");
|
|
|
- ck_assert_str_eq(mg_get_builtin_mime_type("x.HTML"), "text/html");
|
|
|
- ck_assert_str_eq(mg_get_builtin_mime_type("x.hTmL"), "text/html");
|
|
|
- ck_assert_str_eq(mg_get_builtin_mime_type("/abc/def/ghi.htm"), "text/html");
|
|
|
- ck_assert_str_eq(mg_get_builtin_mime_type("x.unknown_extention_xyz"),
|
|
|
- "text/plain");
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_strncasecmp)
|
|
|
-{
|
|
|
- ck_assert(mg_strncasecmp("abc", "abc", 3) == 0);
|
|
|
- ck_assert(mg_strncasecmp("abc", "abcd", 3) == 0);
|
|
|
- ck_assert(mg_strncasecmp("abc", "abcd", 4) != 0);
|
|
|
- ck_assert(mg_strncasecmp("a", "A", 1) == 0);
|
|
|
-
|
|
|
- ck_assert(mg_strncasecmp("A", "B", 1) < 0);
|
|
|
- ck_assert(mg_strncasecmp("A", "b", 1) < 0);
|
|
|
- ck_assert(mg_strncasecmp("a", "B", 1) < 0);
|
|
|
- ck_assert(mg_strncasecmp("a", "b", 1) < 0);
|
|
|
- ck_assert(mg_strncasecmp("b", "A", 1) > 0);
|
|
|
- ck_assert(mg_strncasecmp("B", "A", 1) > 0);
|
|
|
- ck_assert(mg_strncasecmp("b", "a", 1) > 0);
|
|
|
- ck_assert(mg_strncasecmp("B", "a", 1) > 0);
|
|
|
-
|
|
|
- ck_assert(mg_strncasecmp("xAx", "xBx", 3) < 0);
|
|
|
- ck_assert(mg_strncasecmp("xAx", "xbx", 3) < 0);
|
|
|
- ck_assert(mg_strncasecmp("xax", "xBx", 3) < 0);
|
|
|
- ck_assert(mg_strncasecmp("xax", "xbx", 3) < 0);
|
|
|
- ck_assert(mg_strncasecmp("xbx", "xAx", 3) > 0);
|
|
|
- ck_assert(mg_strncasecmp("xBx", "xAx", 3) > 0);
|
|
|
- ck_assert(mg_strncasecmp("xbx", "xax", 3) > 0);
|
|
|
- ck_assert(mg_strncasecmp("xBx", "xax", 3) > 0);
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_get_cookie)
|
|
|
-{
|
|
|
- char buf[32];
|
|
|
- int ret;
|
|
|
- const char *longcookie = "key1=1; key2=2; key3; key4=4; key5=; key6; "
|
|
|
- "key7=this+is+it; key8=8; key9";
|
|
|
-
|
|
|
- /* invalid result buffer */
|
|
|
- ret = mg_get_cookie("", "notfound", NULL, 999);
|
|
|
- ck_assert_int_eq(ret, -2);
|
|
|
-
|
|
|
- /* zero size result buffer */
|
|
|
- ret = mg_get_cookie("", "notfound", buf, 0);
|
|
|
- ck_assert_int_eq(ret, -2);
|
|
|
-
|
|
|
- /* too small result buffer */
|
|
|
- ret = mg_get_cookie("key=toooooooooolong", "key", buf, 4);
|
|
|
- ck_assert_int_eq(ret, -3);
|
|
|
-
|
|
|
- /* key not found in string */
|
|
|
- ret = mg_get_cookie("", "notfound", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- ret = mg_get_cookie(longcookie, "notfound", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- /* key not found in string */
|
|
|
- ret = mg_get_cookie("key1=1; key2=2; key3=3", "notfound", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- /* keys are found as first, middle and last key */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_cookie("key1=1; key2=2; key3=3", "key1", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, 1);
|
|
|
- ck_assert_str_eq("1", buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_cookie("key1=1; key2=2; key3=3", "key2", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, 1);
|
|
|
- ck_assert_str_eq("2", buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_cookie("key1=1; key2=2; key3=3", "key3", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, 1);
|
|
|
- ck_assert_str_eq("3", buf);
|
|
|
-
|
|
|
- /* longer value in the middle of a longer string */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_cookie(longcookie, "key7", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, 10);
|
|
|
- ck_assert_str_eq("this+is+it", buf);
|
|
|
-
|
|
|
- /* key with = but without value in the middle of a longer string */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_cookie(longcookie, "key5", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, 0);
|
|
|
- ck_assert_str_eq("", buf);
|
|
|
-
|
|
|
- /* key without = and without value in the middle of a longer string */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_cookie(longcookie, "key6", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
- /* TODO: mg_get_cookie and mg_get_var(2) should have the same behavior */
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_get_var)
|
|
|
-{
|
|
|
- char buf[32];
|
|
|
- int ret;
|
|
|
- const char *shortquery = "key1=1&key2=2&key3=3";
|
|
|
- const char *longquery = "key1=1&key2=2&key3&key4=4&key5=&key6&"
|
|
|
- "key7=this+is+it&key8=8&key9&&key10=&&"
|
|
|
- "key7=that+is+it&key12=12";
|
|
|
-
|
|
|
- /* invalid result buffer */
|
|
|
- ret = mg_get_var2("", 0, "notfound", NULL, 999, 0);
|
|
|
- ck_assert_int_eq(ret, -2);
|
|
|
-
|
|
|
- /* zero size result buffer */
|
|
|
- ret = mg_get_var2("", 0, "notfound", buf, 0, 0);
|
|
|
- ck_assert_int_eq(ret, -2);
|
|
|
-
|
|
|
- /* too small result buffer */
|
|
|
- ret = mg_get_var2("key=toooooooooolong", 19, "key", buf, 4, 0);
|
|
|
- /* ck_assert_int_eq(ret, -3);
|
|
|
- --> TODO: mg_get_cookie returns -3, mg_get_var -2. This should be
|
|
|
- unified. */
|
|
|
- ck_assert(ret < 0);
|
|
|
-
|
|
|
- /* key not found in string */
|
|
|
- ret = mg_get_var2("", 0, "notfound", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- ret = mg_get_var2(
|
|
|
- longquery, strlen(longquery), "notfound", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- /* key not found in string */
|
|
|
- ret = mg_get_var2(
|
|
|
- shortquery, strlen(shortquery), "notfound", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- /* key not found in string */
|
|
|
- ret = mg_get_var2("key1=1&key2=2&key3=3¬found=here",
|
|
|
- strlen(shortquery),
|
|
|
- "notfound",
|
|
|
- buf,
|
|
|
- sizeof(buf),
|
|
|
- 0);
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- /* key not found in string */
|
|
|
- ret = mg_get_var2(
|
|
|
- shortquery, strlen(shortquery), "key1", buf, sizeof(buf), 1);
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
-
|
|
|
- /* keys are found as first, middle and last key */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_var2(
|
|
|
- shortquery, strlen(shortquery), "key1", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 1);
|
|
|
- ck_assert_str_eq("1", buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_var2(
|
|
|
- shortquery, strlen(shortquery), "key2", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 1);
|
|
|
- ck_assert_str_eq("2", buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_get_var2(
|
|
|
- shortquery, strlen(shortquery), "key3", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 1);
|
|
|
- ck_assert_str_eq("3", buf);
|
|
|
-
|
|
|
- /* longer value in the middle of a longer string */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret =
|
|
|
- mg_get_var2(longquery, strlen(longquery), "key7", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 10);
|
|
|
- ck_assert_str_eq("this is it", buf);
|
|
|
-
|
|
|
- /* longer value in the middle of a longer string - seccond occurance of key
|
|
|
- */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret =
|
|
|
- mg_get_var2(longquery, strlen(longquery), "key7", buf, sizeof(buf), 1);
|
|
|
- ck_assert_int_eq(ret, 10);
|
|
|
- ck_assert_str_eq("that is it", buf);
|
|
|
-
|
|
|
- /* key with = but without value in the middle of a longer string */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret =
|
|
|
- mg_get_var2(longquery, strlen(longquery), "key5", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 0);
|
|
|
- ck_assert_str_eq(buf, "");
|
|
|
-
|
|
|
- /* key without = and without value in the middle of a longer string */
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret =
|
|
|
- mg_get_var2(longquery, strlen(longquery), "key6", buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, -1);
|
|
|
- ck_assert_str_eq(buf, "");
|
|
|
- /* TODO: this is the same situation as with mg_get_value */
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_md5)
|
|
|
-{
|
|
|
- char buf[33];
|
|
|
- char *ret;
|
|
|
- const char *long_str =
|
|
|
- "_123456789A123456789B123456789C123456789D123456789E123456789F123456789"
|
|
|
- "G123456789H123456789I123456789J123456789K123456789L123456789M123456789"
|
|
|
- "N123456789O123456789P123456789Q123456789R123456789S123456789T123456789"
|
|
|
- "U123456789V123456789W123456789X123456789Y123456789Z";
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_md5(buf, NULL);
|
|
|
- ck_assert_str_eq(buf, "d41d8cd98f00b204e9800998ecf8427e");
|
|
|
- ck_assert_str_eq(ret, "d41d8cd98f00b204e9800998ecf8427e");
|
|
|
- ck_assert_ptr_eq(ret, buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_md5(buf, "The quick brown fox jumps over the lazy dog.", NULL);
|
|
|
- ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0");
|
|
|
- ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0");
|
|
|
- ck_assert_ptr_eq(ret, buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_md5(buf,
|
|
|
- "",
|
|
|
- "The qu",
|
|
|
- "ick bro",
|
|
|
- "",
|
|
|
- "wn fox ju",
|
|
|
- "m",
|
|
|
- "ps over the la",
|
|
|
- "",
|
|
|
- "",
|
|
|
- "zy dog.",
|
|
|
- "",
|
|
|
- NULL);
|
|
|
- ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0");
|
|
|
- ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0");
|
|
|
- ck_assert_ptr_eq(ret, buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_md5(buf, long_str, NULL);
|
|
|
- ck_assert_str_eq(buf, "1cb13cf9f16427807f081b2138241f08");
|
|
|
- ck_assert_str_eq(ret, "1cb13cf9f16427807f081b2138241f08");
|
|
|
- ck_assert_ptr_eq(ret, buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_md5(buf, long_str + 1, NULL);
|
|
|
- ck_assert_str_eq(buf, "cf62d3264334154f5779d3694cc5093f");
|
|
|
- ck_assert_str_eq(ret, "cf62d3264334154f5779d3694cc5093f");
|
|
|
- ck_assert_ptr_eq(ret, buf);
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_url_encode)
|
|
|
-{
|
|
|
- char buf[20];
|
|
|
- int ret;
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_url_encode("abc", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(3, ret);
|
|
|
- ck_assert_str_eq("abc", buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_url_encode("a%b/c&d.e", buf, sizeof(buf));
|
|
|
- ck_assert_int_eq(15, ret);
|
|
|
- ck_assert_str_eq("a%25b%2fc%26d.e", buf);
|
|
|
-
|
|
|
- memset(buf, 77, sizeof(buf));
|
|
|
- ret = mg_url_encode("%%%", buf, 4);
|
|
|
- ck_assert_int_eq(-1, ret);
|
|
|
- ck_assert_str_eq("%25", buf);
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
-
|
|
|
-START_TEST(test_mg_url_decode)
|
|
|
-{
|
|
|
- char buf[20];
|
|
|
- int ret;
|
|
|
-
|
|
|
- ret = mg_url_decode("abc", 3, buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 3);
|
|
|
- ck_assert_str_eq(buf, "abc");
|
|
|
-
|
|
|
- ret = mg_url_decode("abcdef", 3, buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 3);
|
|
|
- ck_assert_str_eq(buf, "abc");
|
|
|
-
|
|
|
- ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 0);
|
|
|
- ck_assert_int_eq(ret, 3);
|
|
|
- ck_assert_str_eq(buf, "x+y");
|
|
|
-
|
|
|
- ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 1);
|
|
|
- ck_assert_int_eq(ret, 3);
|
|
|
- ck_assert_str_eq(buf, "x y");
|
|
|
-
|
|
|
- ret = mg_url_decode("%25", 3, buf, sizeof(buf), 1);
|
|
|
- ck_assert_int_eq(ret, 1);
|
|
|
- ck_assert_str_eq(buf, "%");
|
|
|
-}
|
|
|
-END_TEST
|
|
|
-
|
|
|
|
|
|
START_TEST(test_the_test_environment)
|
|
|
{
|
|
@@ -913,11 +551,6 @@ Suite *make_public_suite(void)
|
|
|
TCase *const urlencodingdecoding = tcase_create("URL encoding decoding");
|
|
|
TCase *const cookies = tcase_create("Cookies and variables");
|
|
|
TCase *const md5 = tcase_create("MD5");
|
|
|
- TCase *const checktestenv = tcase_create("Check test environment");
|
|
|
- TCase *const startstophttp = tcase_create("Start Stop HTTP Server");
|
|
|
- TCase *const startstophttps = tcase_create("Start Stop HTTPS Server");
|
|
|
- TCase *const serverrequests = tcase_create("Server Requests");
|
|
|
-
|
|
|
|
|
|
tcase_add_test(version, test_mg_version);
|
|
|
suite_add_tcase(suite, version);
|
|
@@ -942,59 +575,5 @@ Suite *make_public_suite(void)
|
|
|
tcase_add_test(md5, test_mg_md5);
|
|
|
suite_add_tcase(suite, md5);
|
|
|
|
|
|
- tcase_add_test(checktestenv, test_the_test_environment);
|
|
|
- suite_add_tcase(suite, checktestenv);
|
|
|
-
|
|
|
- tcase_add_test(startstophttp, test_mg_start_stop_http_server);
|
|
|
- suite_add_tcase(suite, startstophttp);
|
|
|
-
|
|
|
- tcase_add_test(startstophttps, test_mg_start_stop_https_server);
|
|
|
- suite_add_tcase(suite, startstophttps);
|
|
|
-
|
|
|
- tcase_add_test(serverrequests, test_request_handlers);
|
|
|
- suite_add_tcase(suite, serverrequests);
|
|
|
-
|
|
|
return suite;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-#ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
|
|
|
-/* Used to debug test cases without using the check framework */
|
|
|
-
|
|
|
-static int chk_ok = 0;
|
|
|
-static int chk_failed = 0;
|
|
|
-
|
|
|
-void main(void)
|
|
|
-{
|
|
|
- test_the_test_environment(0);
|
|
|
- test_mg_start_stop_http_server(0);
|
|
|
- test_mg_start_stop_https_server(0);
|
|
|
- test_request_handlers(0);
|
|
|
-
|
|
|
- printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);
|
|
|
-}
|
|
|
-
|
|
|
-void _ck_assert_failed(const char *file, int line, const char *expr, ...)
|
|
|
-{
|
|
|
- va_list va;
|
|
|
- va_start(va, expr);
|
|
|
- fprintf(stderr, "Error: %s, line %i\n", file, line); /* breakpoint here ! */
|
|
|
- vfprintf(stderr, expr, va);
|
|
|
- va_end(va);
|
|
|
- chk_failed++;
|
|
|
-}
|
|
|
-
|
|
|
-void _mark_point(const char *file, int line) { chk_ok++; }
|
|
|
-
|
|
|
-void tcase_fn_start(const char *fname, const char *file, int line) {}
|
|
|
-void suite_add_tcase(Suite *s, TCase *tc){};
|
|
|
-void _tcase_add_test(TCase *tc,
|
|
|
- TFun tf,
|
|
|
- const char *fname,
|
|
|
- int _signal,
|
|
|
- int allowed_exit_value,
|
|
|
- int start,
|
|
|
- int end){};
|
|
|
-TCase *tcase_create(const char *name) { return NULL; };
|
|
|
-Suite *suite_create(const char *name) { return NULL; };
|
|
|
-#endif
|