diff --git a/test/main.c b/test/main.c index 88bef1a7..10ad0d80 100644 --- a/test/main.c +++ b/test/main.c @@ -66,7 +66,8 @@ int main(const int argc, const char * const * const argv) { } // Run up the tests - SRunner * const srunner = srunner_create(make_public_suite()); + SRunner * const srunner = srunner_create(make_public_func_suite()); + srunner_add_suite(srunner, make_public_server_suite()); srunner_add_suite(srunner, make_private_suite()); srunner_run(srunner, suite, test_case, CK_NORMAL); const int number_run = srunner_ntests_run(srunner); diff --git a/test/public_func.c b/test/public_func.c index 413b9bd4..ec660ecd 100644 --- a/test/public_func.c +++ b/test/public_func.c @@ -34,515 +34,9 @@ * http://check.sourceforge.net/doc/check_html/index.html */ - -START_TEST(test_the_test_environment) +Suite *make_public_func_suite(void) { - char wd[300]; - char buf[500]; - FILE *f; - struct stat st; - int ret; - - memset(wd, 0, sizeof(wd)); - memset(buf, 0, sizeof(buf)); - -/* Get the current working directory */ -#ifdef _WIN32 - (void)GetCurrentDirectoryA(sizeof(wd), wd); - wd[sizeof(wd) - 1] = 0; -#else - (void)getcwd(wd, sizeof(wd)); - wd[sizeof(wd) - 1] = 0; -#endif - -/* Check the pem file */ -#ifdef _WIN32 - strcpy(buf, wd); - strcat(buf, "\\resources\\ssl_cert.pem"); - f = fopen(buf, "rb"); -#else - strcpy(buf, wd); - strcat(buf, "/resources/ssl_cert.pem"); - f = fopen(buf, "r"); -#endif - - if (f) { - fclose(f); - } else { - ck_abort_msg("%s not found", buf); - } - -/* Check the test dir */ -#ifdef _WIN32 - strcpy(buf, wd); - strcat(buf, "\\test"); -#else - strcpy(buf, wd); - strcat(buf, "/test"); -#endif - - memset(&st, 0, sizeof(st)); - ret = stat(buf, &st); - - if (!ret) { - fclose(f); - } else { - ck_abort_msg("%s not found", buf); - } -} -END_TEST - - -static int log_msg_func(const struct mg_connection *conn, const char *message) -{ - struct mg_context *ctx; - char *ud; - - ck_assert(conn != NULL); - ctx = mg_get_context(conn); - ck_assert(ctx != NULL); - ud = (char *)mg_get_user_data(ctx); - - strncpy(ud, message, 255); - ud[255] = 0; - return 1; -} - - -START_TEST(test_mg_start_stop_http_server) -{ - struct mg_context *ctx; - const char *OPTIONS[] = { - "document_root", ".", "listening_ports", "8080", NULL, - }; - size_t ports_cnt; - int ports[16]; - int ssl[16]; - struct mg_callbacks callbacks; - char errmsg[256]; - - memset(ports, 0, sizeof(ports)); - memset(ssl, 0, sizeof(ssl)); - memset(&callbacks, 0, sizeof(callbacks)); - memset(errmsg, 0, sizeof(errmsg)); - - callbacks.log_message = log_msg_func; - - ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS); - mg_Sleep(1); - ck_assert_str_eq(errmsg, ""); - ck_assert(ctx != NULL); - - ports_cnt = mg_get_ports(ctx, 16, ports, ssl); - ck_assert_uint_eq(ports_cnt, 1); - ck_assert_int_eq(ports[0], 8080); - ck_assert_int_eq(ssl[0], 0); - ck_assert_int_eq(ports[1], 0); - ck_assert_int_eq(ssl[1], 0); - - mg_Sleep(1); - mg_stop(ctx); -} -END_TEST - - -START_TEST(test_mg_start_stop_https_server) -{ - struct mg_context *ctx; - const char *OPTIONS[] = { - "document_root", - ".", - "listening_ports", - "8080,8443s", - "ssl_certificate", - "resources/ssl_cert.pem", // TODO: check working path of CI test - // system - NULL, - }; - size_t ports_cnt; - int ports[16]; - int ssl[16]; - struct mg_callbacks callbacks; - char errmsg[256]; - - memset(ports, 0, sizeof(ports)); - memset(ssl, 0, sizeof(ssl)); - memset(&callbacks, 0, sizeof(callbacks)); - memset(errmsg, 0, sizeof(errmsg)); - - callbacks.log_message = log_msg_func; - - ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS); - mg_Sleep(1); - ck_assert_str_eq(errmsg, ""); - ck_assert(ctx != NULL); - - ports_cnt = mg_get_ports(ctx, 16, ports, ssl); - ck_assert_uint_eq(ports_cnt, 2); - ck_assert_int_eq(ports[0], 8080); - ck_assert_int_eq(ssl[0], 0); - ck_assert_int_eq(ports[1], 8443); - ck_assert_int_eq(ssl[1], 1); - ck_assert_int_eq(ports[2], 0); - ck_assert_int_eq(ssl[2], 0); - - mg_Sleep(1); - mg_stop(ctx); -} -END_TEST - - -static struct mg_context *g_ctx; - -static int request_test_handler(struct mg_connection *conn, void *cbdata) -{ - int i; - char chunk_data[32]; - const struct mg_request_info *ri; - struct mg_context *ctx; - void *ud, *cud; - - ctx = mg_get_context(conn); - ud = mg_get_user_data(ctx); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert(ctx == g_ctx); - ck_assert(ud == &g_ctx); - - mg_set_user_connection_data(conn, (void *)6543); - cud = mg_get_user_connection_data(conn); - ck_assert(cud == (void *)6543); - - ck_assert(cbdata == (void *)7); - strcpy(chunk_data, "123456789A123456789B123456789C"); - - mg_printf(conn, - "HTTP/1.1 200 OK\r\n" - "Transfer-Encoding: chunked\r\n" - "Content-Type: text/plain\r\n\r\n"); - - for (i = 1; i <= 10; i++) { - mg_printf(conn, "%x\r\n", i); - mg_write(conn, chunk_data, (unsigned)i); - mg_printf(conn, "\r\n"); - } - - mg_printf(conn, "0\r\n\r\n"); - - return 1; -} - - -START_TEST(test_request_handlers) -{ - char ebuf[100]; - struct mg_context *ctx; - struct mg_connection *conn; - const struct mg_request_info *ri; - char uri[64]; - char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8]; - const char *expected = - "112123123412345123456123456712345678123456789123456789A"; - int i; - const char *request = "GET /U7 HTTP/1.0\r\n\r\n"; -#if defined(USE_IPV6) && defined(NO_SSL) - const char *HTTP_PORT = "8084,[::]:8086"; - short ipv4_port = 8084; - short ipv6_port = 8086; -#elif !defined(USE_IPV6) && defined(NO_SSL) - const char *HTTP_PORT = "8084"; - short ipv4_port = 8084; -#elif defined(USE_IPV6) && !defined(NO_SSL) - const char *HTTP_PORT = "8084,[::]:8086,8194r,[::]:8196r,8094s,[::]:8096s"; - short ipv4_port = 8084; - short ipv4s_port = 8094; - short ipv4r_port = 8194; - short ipv6_port = 8086; - short ipv6s_port = 8096; - short ipv6r_port = 8196; -#elif !defined(USE_IPV6) && !defined(NO_SSL) - const char *HTTP_PORT = "8084,8194r,8094s"; - short ipv4_port = 8084; - short ipv4s_port = 8094; - short ipv4r_port = 8194; -#endif - - const char *OPTIONS[8]; /* initializer list here is rejected by CI test */ - const char *opt; - FILE *f; - - memset((void *)OPTIONS, 0, sizeof(OPTIONS)); - OPTIONS[0] = "listening_ports"; - OPTIONS[1] = HTTP_PORT; - OPTIONS[2] = "document_root"; - OPTIONS[3] = "."; -#ifndef NO_SSL - OPTIONS[4] = "ssl_certificate"; - OPTIONS[5] = "resources/ssl_cert.pem"; -#endif - ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL); - ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL); - - ctx = mg_start(NULL, &g_ctx, OPTIONS); - ck_assert(ctx != NULL); - g_ctx = ctx; - - opt = mg_get_option(ctx, "listening_ports"); - ck_assert_str_eq(opt, HTTP_PORT); - opt = mg_get_option(ctx, "cgi_environment"); - ck_assert_str_eq(opt, ""); - opt = mg_get_option(ctx, "unknown_option_name"); - ck_assert(opt == NULL); - - for (i = 0; i < 1000; i++) { - sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, request_test_handler, NULL); - } - for (i = 500; i < 800; i++) { - sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)1); - } - for (i = 600; i >= 0; i--) { - sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)2); - } - for (i = 750; i <= 1000; i++) { - sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)3); - } - for (i = 5; i < 9; i++) { - sprintf(uri, "/U%u", i); - mg_set_request_handler( - ctx, uri, request_test_handler, (void *)(ptrdiff_t)i); - } - - - /* Try to load non existing file */ - conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /file/not/found HTTP/1.0\r\n\r\n"); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "404"); - mg_close_connection(conn); - - - /* Get data from callback */ - conn = mg_download( - "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "200"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, (int)strlen(expected)); - buf[i] = 0; - ck_assert_str_eq(buf, expected); - mg_close_connection(conn); - - - /* Get data from callback using http://127.0.0.1 */ - conn = mg_download( - "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "200"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, (int)strlen(expected)); - buf[i] = 0; - ck_assert_str_eq(buf, expected); - mg_close_connection(conn); - - -#if defined(USE_IPV6) - /* Get data from callback using http://[::1] */ - conn = - mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "200"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, (int)strlen(expected)); - buf[i] = 0; - ck_assert_str_eq(buf, expected); - mg_close_connection(conn); -#endif - - -#if !defined(NO_SSL) - /* Get data from callback using https://127.0.0.1 */ - conn = mg_download( - "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "200"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, (int)strlen(expected)); - buf[i] = 0; - ck_assert_str_eq(buf, expected); - mg_close_connection(conn); - - /* Get redirect from callback using http://127.0.0.1 */ - conn = mg_download( - "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "302"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, -1); - mg_close_connection(conn); -#endif - - -#if defined(USE_IPV6) && !defined(NO_SSL) - /* Get data from callback using https://[::1] */ - conn = - mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "200"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, (int)strlen(expected)); - buf[i] = 0; - ck_assert_str_eq(buf, expected); - mg_close_connection(conn); - - /* Get redirect from callback using http://127.0.0.1 */ - conn = - mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "302"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, -1); - mg_close_connection(conn); -#endif - - -/* It seems to be impossible to find out what the actual working - * directory of the CI test environment is. Before breaking another - * dozen of builds by trying blindly with different paths, just - * create the file here */ -#ifdef _WIN32 - f = fopen("test.txt", "wb"); -#else - f = fopen("test.txt", "w"); -#endif - fwrite("simple text file\n", 17, 1, f); - fclose(f); - - - /* Get static data */ - conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /test.txt HTTP/1.0\r\n\r\n"); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "200"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert_int_eq(i, 17); - if ((i >= 0) && (i < (int)sizeof(buf))) { - buf[i] = 0; - } - ck_assert_str_eq(buf, "simple text file\n"); - mg_close_connection(conn); - - - /* Get directory listing */ - conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET / HTTP/1.0\r\n\r\n"); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "200"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert(i > 6); - buf[6] = 0; - ck_assert_str_eq(buf, ""); - mg_close_connection(conn); - - - /* POST to static file (will not work) */ - conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "POST /test.txt HTTP/1.0\r\n\r\n"); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "405"); - i = mg_read(conn, buf, sizeof(buf)); - ck_assert(i >= 29); - buf[29] = 0; - ck_assert_str_eq(buf, "Error 405: Method Not Allowed"); - mg_close_connection(conn); - - - /* PUT to static file (will not work) */ - conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "PUT /test.txt HTTP/1.0\r\n\r\n"); - ck_assert(conn != NULL); - ri = mg_get_request_info(conn); - - ck_assert(ri != NULL); - ck_assert_str_eq(ri->uri, "401"); /* not authorized */ - mg_close_connection(conn); - - /* TODO: Test websockets */ - - - /* Close the server */ - g_ctx = NULL; - mg_stop(ctx); - mg_Sleep(1); -} -END_TEST - - -Suite *make_public_suite(void) -{ - Suite *const suite = suite_create("Public"); + Suite *const suite = suite_create("PublicFunc"); TCase *const version = tcase_create("Version"); TCase *const get_valid_options = tcase_create("Options"); diff --git a/test/public_func.h b/test/public_func.h index b22c6ebb..37eb4942 100644 --- a/test/public_func.h +++ b/test/public_func.h @@ -18,11 +18,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#ifndef TEST_PUBLIC_H_ -#define TEST_PUBLIC_H_ +#ifndef TEST_PUBLIC_FUNC_H_ +#define TEST_PUBLIC_FUNC_H_ #include "civetweb_check.h" -Suite *make_public_suite(void); +Suite *make_public_func_suite(void); #endif /* TEST_PUBLIC_H_ */ diff --git a/test/public_server.c b/test/public_server.c index 6c6da1ca..a1c2b0df 100644 --- a/test/public_server.c +++ b/test/public_server.c @@ -551,9 +551,9 @@ START_TEST(test_request_handlers) END_TEST -Suite *make_public_suite(void) +Suite *make_public_server_suite(void) { - Suite *const suite = suite_create("Public"); + Suite *const suite = suite_create("PublicServer"); TCase *const checktestenv = tcase_create("Check test environment"); TCase *const startstophttp = tcase_create("Start Stop HTTP Server"); diff --git a/test/public_server.h b/test/public_server.h index b22c6ebb..8d92c5a4 100644 --- a/test/public_server.h +++ b/test/public_server.h @@ -18,11 +18,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#ifndef TEST_PUBLIC_H_ -#define TEST_PUBLIC_H_ +#ifndef TEST_PUBLIC_SERVER_H_ +#define TEST_PUBLIC_SERVER_H_ #include "civetweb_check.h" -Suite *make_public_suite(void); +Suite *make_public_server_suite(void); #endif /* TEST_PUBLIC_H_ */