|
@@ -232,6 +232,7 @@ START_TEST(test_threading)
|
|
|
}
|
|
|
END_TEST
|
|
|
|
|
|
+static const char *lastMessage = NULL;
|
|
|
|
|
|
static int
|
|
|
log_msg_func(const struct mg_connection *conn, const char *message)
|
|
@@ -250,6 +251,7 @@ log_msg_func(const struct mg_connection *conn, const char *message)
|
|
|
|
|
|
printf("LOG_MSG_FUNC: %s\n", message);
|
|
|
mark_point();
|
|
|
+ lastMessage = message;
|
|
|
|
|
|
return 1; /* Return 1 means "already handled" */
|
|
|
}
|
|
@@ -262,6 +264,7 @@ test_log_message(const struct mg_connection *conn, const char *message)
|
|
|
|
|
|
printf("LOG_MESSAGE: %s\n", message);
|
|
|
mark_point();
|
|
|
+ lastMessage = message;
|
|
|
|
|
|
return 0; /* Return 0 means "not yet handled" */
|
|
|
}
|
|
@@ -270,7 +273,8 @@ test_log_message(const struct mg_connection *conn, const char *message)
|
|
|
static struct mg_context *
|
|
|
test_mg_start(const struct mg_callbacks *callbacks,
|
|
|
void *user_data,
|
|
|
- const char **configuration_options)
|
|
|
+ const char **configuration_options,
|
|
|
+ unsigned line)
|
|
|
{
|
|
|
struct mg_context *ctx;
|
|
|
struct mg_callbacks cb;
|
|
@@ -291,9 +295,15 @@ test_mg_start(const struct mg_callbacks *callbacks,
|
|
|
ctx = mg_start(&cb, user_data, configuration_options);
|
|
|
mark_point();
|
|
|
if (ctx) {
|
|
|
- /* Give the server some time to start in the test VM */
|
|
|
- /* Don't need to do this if mg_start failed */
|
|
|
+ /* Give the server some time to start in the test VM. */
|
|
|
+ /* Don't need to do this if mg_start failed. */
|
|
|
test_sleep(SLEEP_AFTER_MG_START);
|
|
|
+ } else if (line>0) {
|
|
|
+ /* mg_start is not supposed to fail anywhere, except for
|
|
|
+ * special tests (for them, line is 0). */
|
|
|
+ ck_abort_msg("mg_start failed in line %u, message %s",
|
|
|
+ line,
|
|
|
+ (lastMessage ? lastMessage : "<NULL>"));
|
|
|
}
|
|
|
mark_point();
|
|
|
|
|
@@ -302,10 +312,11 @@ test_mg_start(const struct mg_callbacks *callbacks,
|
|
|
|
|
|
|
|
|
static void
|
|
|
-test_mg_stop(struct mg_context *ctx)
|
|
|
+test_mg_stop(struct mg_context *ctx, unsigned line)
|
|
|
{
|
|
|
+ (void)line;
|
|
|
#ifdef __MACH__
|
|
|
- /* For unknown reasons, there are sporadic hands
|
|
|
+ /* For unknown reasons, there are sporadic hangs
|
|
|
* for OSX if mark_point is called here */
|
|
|
test_sleep(SLEEP_BEFORE_MG_STOP);
|
|
|
mg_stop(ctx);
|
|
@@ -376,7 +387,7 @@ test_mg_start_stop_http_server_impl(int ipv6, int bound)
|
|
|
|
|
|
callbacks.log_message = log_msg_func;
|
|
|
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert_str_eq(errmsg, "");
|
|
|
ck_assert(ctx != NULL);
|
|
@@ -532,7 +543,7 @@ test_mg_start_stop_http_server_impl(int ipv6, int bound)
|
|
|
test_sleep(1);
|
|
|
|
|
|
/* End test */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
mark_point();
|
|
|
}
|
|
|
|
|
@@ -611,7 +622,7 @@ START_TEST(test_mg_start_stop_https_server)
|
|
|
|
|
|
callbacks.log_message = log_msg_func;
|
|
|
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert_str_eq(errmsg, "");
|
|
|
ck_assert(ctx != NULL);
|
|
@@ -683,7 +694,7 @@ START_TEST(test_mg_start_stop_https_server)
|
|
|
|
|
|
test_sleep(1);
|
|
|
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
mark_point();
|
|
|
#endif
|
|
|
}
|
|
@@ -748,7 +759,7 @@ START_TEST(test_mg_server_and_client_tls)
|
|
|
|
|
|
callbacks.log_message = log_msg_func;
|
|
|
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert_str_eq(errmsg, "");
|
|
|
ck_assert(ctx != NULL);
|
|
@@ -814,7 +825,7 @@ START_TEST(test_mg_server_and_client_tls)
|
|
|
|
|
|
test_sleep(1);
|
|
|
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
#endif
|
|
|
mark_point();
|
|
|
}
|
|
@@ -875,6 +886,7 @@ request_test_handler(struct mg_connection *conn, void *cbdata)
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+static char request_handler2_url_expected[128]={0};
|
|
|
|
|
|
/* Return the same as request_test_handler using new interfaces */
|
|
|
static int
|
|
@@ -885,6 +897,8 @@ request_test_handler2(struct mg_connection *conn, void *cbdata)
|
|
|
const struct mg_request_info *ri;
|
|
|
struct mg_context *ctx;
|
|
|
void *ud, *ud2;
|
|
|
+ int err_ret;
|
|
|
+ char url_buffer[128];
|
|
|
|
|
|
ctx = mg_get_context(conn);
|
|
|
ud = mg_get_user_data(ctx);
|
|
@@ -896,6 +910,18 @@ request_test_handler2(struct mg_connection *conn, void *cbdata)
|
|
|
ck_assert(ud == &g_ctx);
|
|
|
ck_assert(ud == ud2);
|
|
|
|
|
|
+ err_ret = mg_get_request_link(NULL, url_buffer, sizeof(url_buffer)); /* param error */
|
|
|
+ ck_assert(err_ret < 0);
|
|
|
+ err_ret = mg_get_request_link(conn, NULL, sizeof(url_buffer)); /* param error */
|
|
|
+ ck_assert(err_ret < 0);
|
|
|
+ err_ret = mg_get_request_link(conn, url_buffer, 0); /* param error */
|
|
|
+ ck_assert(err_ret < 0);
|
|
|
+ err_ret = mg_get_request_link(conn, url_buffer, 5); /* buffer too small */
|
|
|
+ ck_assert(err_ret < 0);
|
|
|
+ err_ret = mg_get_request_link(conn, url_buffer, sizeof(url_buffer));
|
|
|
+ ck_assert(err_ret == 0);
|
|
|
+ ck_assert_str_eq(url_buffer, request_handler2_url_expected);
|
|
|
+
|
|
|
mg_send_http_ok(conn, "text/plain", -1);
|
|
|
|
|
|
for (i = 1; i <= 10; i++) {
|
|
@@ -1221,13 +1247,15 @@ START_TEST(test_request_handlers)
|
|
|
char cmd_buf[1024];
|
|
|
char *cgi_env_opt;
|
|
|
|
|
|
+ const char *server_host = "test.domain";
|
|
|
+
|
|
|
mark_point();
|
|
|
|
|
|
memset((void *)OPTIONS, 0, sizeof(OPTIONS));
|
|
|
OPTIONS[opt_idx++] = "listening_ports";
|
|
|
OPTIONS[opt_idx++] = HTTP_PORT;
|
|
|
OPTIONS[opt_idx++] = "authentication_domain";
|
|
|
- OPTIONS[opt_idx++] = "test.domain";
|
|
|
+ OPTIONS[opt_idx++] = server_host;
|
|
|
#if !defined(NO_FILES)
|
|
|
OPTIONS[opt_idx++] = "document_root";
|
|
|
OPTIONS[opt_idx++] = ".";
|
|
@@ -1254,7 +1282,7 @@ START_TEST(test_request_handlers)
|
|
|
ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
|
|
|
ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
|
|
|
|
|
|
- ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
|
|
|
+ ctx = test_mg_start(NULL, &g_ctx, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert(ctx != NULL);
|
|
|
g_ctx = ctx;
|
|
@@ -1309,6 +1337,7 @@ START_TEST(test_request_handlers)
|
|
|
(void *)(ptrdiff_t)i);
|
|
|
}
|
|
|
|
|
|
+ sprintf(request_handler2_url_expected, "http://%s:%u/handler2", server_host, ipv4_port);
|
|
|
mg_set_request_handler(ctx, "/handler2", request_test_handler2, NULL);
|
|
|
|
|
|
#ifdef USE_WEBSOCKET
|
|
@@ -1488,6 +1517,7 @@ START_TEST(test_request_handlers)
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
f = fopen("test.cgi", "wb");
|
|
|
+ ck_assert(f != NULL);
|
|
|
cgi_script_content = "#!test.cgi.cmd\r\n";
|
|
|
fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
|
|
|
fclose(f);
|
|
@@ -1502,6 +1532,7 @@ START_TEST(test_request_handlers)
|
|
|
fclose(f);
|
|
|
#else
|
|
|
f = fopen("test.cgi", "w");
|
|
|
+ ck_assert(f != NULL);
|
|
|
cgi_script_content = "#!/bin/sh\n\n"
|
|
|
"printf \"Connection: close\\r\\n\"\n"
|
|
|
"printf \"Content-Type: text/plain\\r\\n\"\n"
|
|
@@ -1551,7 +1582,11 @@ START_TEST(test_request_handlers)
|
|
|
sprintf(ebuf, "%scgi_test.cgi", locate_test_exes());
|
|
|
|
|
|
if (stat(ebuf, &st) != 0) {
|
|
|
+ char cwd[512];
|
|
|
+ getcwd(cwd, sizeof(cwd));
|
|
|
+
|
|
|
fprintf(stderr, "\nFile %s not found\n", ebuf);
|
|
|
+ fprintf(stderr, "Working directory is %s\n", cwd);
|
|
|
fprintf(stderr,
|
|
|
"This file needs to be compiled manually before "
|
|
|
"starting the test\n");
|
|
@@ -1559,8 +1594,8 @@ START_TEST(test_request_handlers)
|
|
|
"e.g. by gcc test/cgi_test.c -o output/cgi_test.cgi\n\n");
|
|
|
|
|
|
/* Abort test with diagnostic message */
|
|
|
- ck_abort_msg("Mandatory file %s must be built before starting the test",
|
|
|
- ebuf);
|
|
|
+ ck_abort_msg("Mandatory file %s must be built before starting the test (cwd: %s)",
|
|
|
+ ebuf, cwd);
|
|
|
}
|
|
|
#endif
|
|
|
|
|
@@ -1824,9 +1859,9 @@ START_TEST(test_request_handlers)
|
|
|
|
|
|
mg_printf(client_conn,
|
|
|
"GET /handler2 HTTP/1.1\r\n"
|
|
|
- "Host: localhost\r\n"
|
|
|
+ "Host: %s\r\n"
|
|
|
"\r\n",
|
|
|
- ipv4_port);
|
|
|
+ server_host);
|
|
|
|
|
|
i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
|
|
|
ck_assert_int_ge(i, 0);
|
|
@@ -2186,7 +2221,7 @@ START_TEST(test_request_handlers)
|
|
|
|
|
|
/* Close the server */
|
|
|
g_ctx = NULL;
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
mark_point();
|
|
|
|
|
|
#ifdef USE_WEBSOCKET
|
|
@@ -2595,7 +2630,7 @@ START_TEST(test_handle_form)
|
|
|
ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 1] == NULL);
|
|
|
ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL);
|
|
|
|
|
|
- ctx = test_mg_start(NULL, &g_ctx, OPTIONS);
|
|
|
+ ctx = test_mg_start(NULL, &g_ctx, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert(ctx != NULL);
|
|
|
g_ctx = ctx;
|
|
@@ -3119,7 +3154,7 @@ START_TEST(test_handle_form)
|
|
|
|
|
|
/* Close the server */
|
|
|
g_ctx = NULL;
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
mark_point();
|
|
|
}
|
|
|
END_TEST
|
|
@@ -3166,7 +3201,7 @@ START_TEST(test_http_auth)
|
|
|
mark_point();
|
|
|
|
|
|
/* Start with default options */
|
|
|
- ctx = test_mg_start(NULL, NULL, OPTIONS);
|
|
|
+ ctx = test_mg_start(NULL, NULL, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert(ctx != NULL);
|
|
|
domain = mg_get_option(ctx, "authentication_domain");
|
|
@@ -3456,7 +3491,7 @@ START_TEST(test_http_auth)
|
|
|
|
|
|
|
|
|
/* Stop the server and clean up */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
(void)remove(test_file);
|
|
|
(void)remove(passwd_file);
|
|
|
(void)remove("put_delete_auth_file.csv");
|
|
@@ -3493,7 +3528,7 @@ START_TEST(test_keep_alive)
|
|
|
|
|
|
mark_point();
|
|
|
|
|
|
- ctx = test_mg_start(NULL, NULL, OPTIONS);
|
|
|
+ ctx = test_mg_start(NULL, NULL, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert(ctx != NULL);
|
|
|
|
|
@@ -3540,7 +3575,7 @@ START_TEST(test_keep_alive)
|
|
|
* (will only work if NO_FILES is not set). */
|
|
|
|
|
|
/* Stop the server and clean up */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
mark_point();
|
|
|
}
|
|
@@ -3587,7 +3622,7 @@ START_TEST(test_error_handling)
|
|
|
|
|
|
/* test with unknown option */
|
|
|
memset(errmsg, 0, sizeof(errmsg));
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
|
|
|
|
|
|
/* Details of errmsg may vary, but it may not be empty */
|
|
|
ck_assert_str_ne(errmsg, "");
|
|
@@ -3603,7 +3638,7 @@ START_TEST(test_error_handling)
|
|
|
|
|
|
/* Test with bad num_thread option */
|
|
|
memset(errmsg, 0, sizeof(errmsg));
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
|
|
|
|
|
|
/* Details of errmsg may vary, but it may not be empty */
|
|
|
ck_assert_str_ne(errmsg, "");
|
|
@@ -3619,7 +3654,7 @@ START_TEST(test_error_handling)
|
|
|
|
|
|
/* Test with bad num_thread option */
|
|
|
memset(errmsg, 0, sizeof(errmsg));
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, 0);
|
|
|
|
|
|
/* Details of errmsg may vary, but it may not be empty */
|
|
|
ck_assert_str_ne(errmsg, "");
|
|
@@ -3642,7 +3677,7 @@ START_TEST(test_error_handling)
|
|
|
/* This time start the server with a valid configuration */
|
|
|
sprintf(bad_thread_num, "%i", 1);
|
|
|
memset(errmsg, 0, sizeof(errmsg));
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
|
|
|
|
|
|
ck_assert_str_eq(errmsg, "");
|
|
|
ck_assert(ctx != NULL);
|
|
@@ -3807,7 +3842,7 @@ START_TEST(test_error_handling)
|
|
|
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
|
|
|
/* HTTP 1.1 GET request - must not work, since server is already stopped */
|
|
@@ -3858,7 +3893,7 @@ START_TEST(test_error_log_file)
|
|
|
#endif
|
|
|
OPTIONS[opt_cnt] = NULL;
|
|
|
|
|
|
- ctx = test_mg_start(NULL, 0, OPTIONS);
|
|
|
+ ctx = test_mg_start(NULL, 0, OPTIONS, __LINE__);
|
|
|
ck_assert(ctx != NULL);
|
|
|
|
|
|
/* Remove log files (they may exist from previous incomplete runs of
|
|
@@ -3894,7 +3929,7 @@ START_TEST(test_error_log_file)
|
|
|
mg_close_connection(client);
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
|
|
|
/* Check access.log */
|
|
@@ -3930,7 +3965,7 @@ START_TEST(test_error_log_file)
|
|
|
ck_assert_str_eq(OPTIONS[0], "listening_ports");
|
|
|
OPTIONS[1] = "bad !"; /* no r or s in string */
|
|
|
|
|
|
- ctx = test_mg_start(NULL, 0, OPTIONS);
|
|
|
+ ctx = test_mg_start(NULL, 0, OPTIONS, 0);
|
|
|
ck_assert_msg(
|
|
|
ctx == NULL,
|
|
|
"Should not be able to start server with bad port configuration");
|
|
@@ -4066,7 +4101,7 @@ START_TEST(test_throttle)
|
|
|
callbacks.begin_request = test_throttle_begin_request;
|
|
|
callbacks.end_request = test_throttle_end_request;
|
|
|
|
|
|
- ctx = test_mg_start(&callbacks, 0, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, 0, OPTIONS, __LINE__);
|
|
|
ck_assert(ctx != NULL);
|
|
|
|
|
|
/* connect client */
|
|
@@ -4122,7 +4157,7 @@ START_TEST(test_throttle)
|
|
|
mg_close_connection(client);
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
mark_point();
|
|
|
}
|
|
@@ -4236,7 +4271,7 @@ START_TEST(test_large_file)
|
|
|
callbacks.begin_request = test_large_file_begin_request;
|
|
|
callbacks.log_message = log_msg_func;
|
|
|
|
|
|
- ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, (void *)errmsg, OPTIONS, __LINE__);
|
|
|
ck_assert_str_eq(errmsg, "");
|
|
|
ck_assert(ctx != NULL);
|
|
|
|
|
@@ -4306,7 +4341,7 @@ START_TEST(test_large_file)
|
|
|
#endif
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
mark_point();
|
|
|
}
|
|
@@ -4480,7 +4515,7 @@ START_TEST(test_mg_store_body)
|
|
|
test_sleep(5);
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
/* Un-initialize the library */
|
|
|
mg_exit_library();
|
|
@@ -4558,7 +4593,7 @@ START_TEST(test_file_in_memory)
|
|
|
memset(&callbacks, 0, sizeof(callbacks));
|
|
|
callbacks.open_file = test_file_in_memory_open_file;
|
|
|
|
|
|
- ctx = test_mg_start(&callbacks, 0, OPTIONS);
|
|
|
+ ctx = test_mg_start(&callbacks, 0, OPTIONS, __LINE__);
|
|
|
ck_assert(ctx != NULL);
|
|
|
|
|
|
/* connect client */
|
|
@@ -4608,7 +4643,7 @@ START_TEST(test_file_in_memory)
|
|
|
mg_close_connection(client);
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
/* Free test data */
|
|
|
free(file_in_mem_data);
|
|
@@ -4810,7 +4845,7 @@ START_TEST(test_minimal_http_server_callback)
|
|
|
mg_init_library(0);
|
|
|
|
|
|
/* Start the server */
|
|
|
- ctx = test_mg_start(NULL, 0, NULL);
|
|
|
+ ctx = test_mg_start(NULL, 0, NULL, __LINE__);
|
|
|
ck_assert(ctx != NULL);
|
|
|
|
|
|
/* Add some handler */
|
|
@@ -4834,7 +4869,7 @@ START_TEST(test_minimal_http_server_callback)
|
|
|
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
/* Un-initialize the library */
|
|
|
mg_exit_library();
|
|
@@ -4903,7 +4938,7 @@ START_TEST(test_minimal_https_server_callback)
|
|
|
|
|
|
|
|
|
/* Start the server */
|
|
|
- ctx = test_mg_start(NULL, 0, OPTIONS);
|
|
|
+ ctx = test_mg_start(NULL, 0, OPTIONS, __LINE__);
|
|
|
ck_assert(ctx != NULL);
|
|
|
|
|
|
/* Add some handler */
|
|
@@ -4927,7 +4962,7 @@ START_TEST(test_minimal_https_server_callback)
|
|
|
|
|
|
|
|
|
/* Stop the server */
|
|
|
- test_mg_stop(ctx);
|
|
|
+ test_mg_stop(ctx, __LINE__);
|
|
|
|
|
|
/* Un-initialize the library */
|
|
|
mg_exit_library();
|