src: add test for shutdown, fix format

This commit is contained in:
reito 2025-02-04 16:00:33 +08:00
parent 2baab70a48
commit f7c8cd1438
3 changed files with 49 additions and 17 deletions

View File

@ -109,12 +109,14 @@ static void eof_timer_close_cb(uv_handle_t* handle);
* The UDS on Windows only supports this pathname mode.
*/
static int uv__win_uds_pipe_file_exists(const char* path, int* exists) {
if (!exists || !path) {
int err;
WCHAR* wpath;
if (exists == NULL || path == NULL) {
return UV_EINVAL;
}
WCHAR* wpath;
int err = uv__convert_utf8_to_utf16(path, &wpath);
err = uv__convert_utf8_to_utf16(path, &wpath);
if (err) {
return err;
}
@ -730,7 +732,7 @@ void uv__pipe_shutdown(uv_loop_t* loop, uv_pipe_t* handle, uv_shutdown_t *req) {
}
if (handle->flags & UV_HANDLE_WIN_UDS_PIPE) {
/* Unix domain socket seems ok to just skip the following code.*/
/* Unix domain socket support neither query 'named pipe' info, nor FlushFileBuffer. */
uv__insert_pending_req(loop, (uv_req_t*) req);
return;
}
@ -940,7 +942,7 @@ int uv_pipe_bind2(uv_pipe_t* handle,
if (err) {
goto error;
}
#if defined(UV__ENABLE_WIN_UDS_PIPE)
if (use_uds_pipe) {
int exists;
@ -1164,7 +1166,7 @@ int uv_pipe_connect2(uv_connect_t* req,
err = ERROR_NO_UNICODE_TRANSLATION;
goto error;
}
#if defined(UV__ENABLE_WIN_UDS_PIPE)
if (use_uds_pipe) {
int exists;
@ -1217,7 +1219,7 @@ int uv_pipe_connect2(uv_connect_t* req,
addr2.sun_family = AF_UNIX;
memcpy(addr2.sun_path, name, namelen);
addr2.sun_path[namelen] = '\0';
memset(&req->u.io.overlapped, 0, sizeof(req->u.io.overlapped));
/*
@ -1259,9 +1261,6 @@ int uv_pipe_connect2(uv_connect_t* req,
}
#endif
/*
* When matched with named pipe prefix, use named pipe as backend.
*/
pipeHandle = open_named_pipe(handle->name, &duplex_flags);
if (pipeHandle == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_PIPE_BUSY) {

View File

@ -217,6 +217,7 @@ TEST_DECLARE (pipe_server_close)
#ifdef _WIN32
TEST_DECLARE (pipe_win_uds)
TEST_DECLARE (pipe_win_uds_bad_name)
TEST_DECLARE (pipe_win_uds_shutdown)
#endif
TEST_DECLARE (connection_fail)
TEST_DECLARE (connection_fail_doesnt_auto_close)
@ -635,6 +636,7 @@ TASK_LIST_START
#ifdef _WIN32
TEST_ENTRY (pipe_win_uds)
TEST_ENTRY (pipe_win_uds_bad_name)
TEST_ENTRY (pipe_win_uds_shutdown)
#endif
TEST_ENTRY (pipe_set_chmod)

View File

@ -28,7 +28,11 @@
#define UV_SUPPORTS_WIN_UDS
#endif
static int use_shutdown = 0;
static uv_shutdown_t shutdown_client;
static int close_cb_called = 0;
static int shutdown_cb_called = 0;
static int server_connect_cb_called = 0;
static int client_connect_cb_called = 0;
@ -46,6 +50,12 @@ static void close_cb(uv_handle_t *handle) {
close_cb_called++;
}
static void shutdown_cb(uv_shutdown_t *req, int status) {
ASSERT_NOT_NULL(req);
uv_close((uv_handle_t *) req->handle, close_cb);
shutdown_cb_called++;
}
static void after_write_cb(uv_write_t *req, int status) {
ASSERT_OK(status);
free(req->data);
@ -67,6 +77,8 @@ static void client_connect_cb(uv_connect_t *connect_req, int status) {
static void read_cb(uv_stream_t *stream,
ssize_t nread,
const uv_buf_t *buf) {
char read[256];
// Ignore read error.
if (nread < 0 || !buf)
return;
@ -74,15 +86,18 @@ static void read_cb(uv_stream_t *stream,
// Test if the buffer length equal.
ASSERT_EQ(nread, strlen(pipe_test_data));
char read[256];
memcpy(read, buf->base, nread);
read[nread] = '\0';
// Test if data equal.
ASSERT_STR_EQ(read, pipe_test_data);
// Everything good, close pipe, exit loop.
uv_close((uv_handle_t *) &pipe_client, close_cb);
if (use_shutdown) {
uv_shutdown(&shutdown_client, (uv_stream_t *) &pipe_client, shutdown_cb);
} else {
uv_close((uv_handle_t *) &pipe_client, close_cb);
}
uv_close((uv_handle_t *) &pipe_server, close_cb);
}
@ -97,7 +112,7 @@ static void server_connect_cb(uv_stream_t *handle, int status) {
ASSERT_OK(uv_read_start((uv_stream_t*) conn, alloc_cb, read_cb));
}
TEST_IMPL(pipe_win_uds) {
int test_pipe_win_uds() {
#if defined(UV_SUPPORTS_WIN_UDS)
int r;
uv_fs_t fs;
@ -130,6 +145,10 @@ TEST_IMPL(pipe_win_uds) {
// Run the loop
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
if (use_shutdown) {
ASSERT_EQ(1, shutdown_cb_called);
}
ASSERT_EQ(2, close_cb_called);
ASSERT_EQ(1, server_connect_cb_called);
ASSERT_EQ(1, client_connect_cb_called);
@ -140,6 +159,18 @@ TEST_IMPL(pipe_win_uds) {
}
TEST_IMPL(pipe_win_uds) {
use_shutdown = 0;
return test_pipe_win_uds();
}
TEST_IMPL(pipe_win_uds_shutdown) {
use_shutdown = 1;
return test_pipe_win_uds();
}
static void bad_name_connect_cb(uv_connect_t *connect_req, int status) {
ASSERT_EQ(status, UV_ENOENT);
}
@ -152,15 +183,15 @@ TEST_IMPL(pipe_win_uds_bad_name) {
uv_pipe_t pipe_server_1;
uv_pipe_t pipe_server_2;
uv_pipe_t pipe_client_1;
const char * path_1 = "not/exist/file/path";
const char * path_2 = "test/fixtures/empty_file";
const char *path_1 = "not/exist/file/path";
const char *path_2 = "test/fixtures/empty_file";
// Bind server 1 which has a bad path
r = uv_pipe_init_ex(uv_default_loop(), &pipe_server_1, UV_PIPE_INIT_WIN_UDS);
ASSERT_OK(r);
r = uv_pipe_bind(&pipe_server_1, path_1);
ASSERT_EQ(r, UV_EINVAL);
// Bind server 2 which file exists
r = uv_pipe_init_ex(uv_default_loop(), &pipe_server_2, UV_PIPE_INIT_WIN_UDS);
ASSERT_OK(r);