From 3c56c48a2dc3db2ecd9f1d32ad85cefc1a4cdded Mon Sep 17 00:00:00 2001 From: reito Date: Mon, 20 Jan 2025 00:43:15 +0800 Subject: [PATCH] test: add test for win uds --- src/win/pipe.c | 24 ++++++++++++----------- test/test-list.h | 7 +++++++ test/test-pipe-win-uds.c | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/win/pipe.c b/src/win/pipe.c index 40107d2f..5ddfcf4c 100644 --- a/src/win/pipe.c +++ b/src/win/pipe.c @@ -882,7 +882,8 @@ int uv_pipe_bind2(uv_pipe_t* handle, handle->pipe.serv.pending_instances = default_pending_pipe_instances; } - if (handle->flags & UV_HANDLE_WIN_UDS_PIPE) { + if (use_uds_pipe) { + handle->flags |= UV_HANDLE_WIN_UDS_PIPE; /* Only use 1 pending instance when use unix domain socket, cause * call AcceptEx multiple times seems result in multiple accept events. * Not the expected queue behavior, that only one of them is triggered. */ @@ -921,7 +922,6 @@ int uv_pipe_bind2(uv_pipe_t* handle, #if defined(UV__ENABLE_WIN_UDS_PIPE) if (use_uds_pipe) { - handle->flags |= UV_HANDLE_WIN_UDS_PIPE; int uds_err = pipe_alloc_accept_unix_domain_socket( loop, handle, &handle->pipe.serv.accept_reqs[0], handle->pathname, TRUE); if (uds_err) { @@ -1079,6 +1079,9 @@ int uv_pipe_connect2(uv_connect_t* req, #if defined(UV__ENABLE_WIN_UDS_PIPE) if (use_uds_pipe) { + /* Set flag indicates it is a unix domain socket; */ + handle->flags |= UV_HANDLE_WIN_UDS_PIPE; + /* https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ */ /* a null-terminated UTF-8 file system path */ if (flags & UV_PIPE_NO_TRUNCATE) @@ -1182,9 +1185,6 @@ int uv_pipe_connect2(uv_connect_t* req, } } - /* Set flag indicates it is a unix domain socket; */ - handle->flags |= UV_HANDLE_WIN_UDS_PIPE; - /* Since we use IOCP, we can't set value to u.connect.pipeHandle * as it will be rewritten by the result of IOCP. Thus, we set the socket * to uds_socket (reuse the `name`) and set it to pipeHandle later at req @@ -2641,12 +2641,14 @@ void uv__process_pipe_connect_req(uv_loop_t* loop, uv_pipe_t* handle, /* IOCP overwrites the connect.pipeHandle, so workaround here. */ req->u.connect.pipeHandle = (HANDLE) req->u.connect.uds_socket; - /* If it is unix domain handle, the event comes from ConnectEx IOCP. */ - setsockopt((SOCKET) req->u.connect.pipeHandle, - SOL_SOCKET, - SO_UPDATE_CONNECT_CONTEXT, - NULL, - 0); + if (req->u.connect.pipeHandle) { + /* If it is unix domain handle, the event comes from ConnectEx IOCP. */ + setsockopt((SOCKET) req->u.connect.pipeHandle, + SOL_SOCKET, + SO_UPDATE_CONNECT_CONTEXT, + NULL, + 0); + } } UNREGISTER_HANDLE_REQ(loop, handle); diff --git a/test/test-list.h b/test/test-list.h index c6651299..b946511e 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -214,6 +214,9 @@ TEST_DECLARE (pipe_getsockname_blocking) TEST_DECLARE (pipe_pending_instances) TEST_DECLARE (pipe_sendmsg) TEST_DECLARE (pipe_server_close) +#ifdef _WIN32 +TEST_DECLARE (pipe_win_uds) +#endif TEST_DECLARE (connection_fail) TEST_DECLARE (connection_fail_doesnt_auto_close) TEST_DECLARE (shutdown_close_tcp) @@ -628,6 +631,10 @@ TASK_LIST_START #endif /* Seems to be either about 0.5s or 5s, depending on the OS. */ TEST_ENTRY_CUSTOM (pipe_set_non_blocking, 0, 0, 20000) +#ifdef _WIN32 + TEST_ENTRY (pipe_win_uds) +#endif + TEST_ENTRY (pipe_set_chmod) TEST_ENTRY (tty) #ifdef _WIN32 diff --git a/test/test-pipe-win-uds.c b/test/test-pipe-win-uds.c index 546d4fd1..7430c522 100644 --- a/test/test-pipe-win-uds.c +++ b/test/test-pipe-win-uds.c @@ -24,7 +24,49 @@ #ifdef _WIN32 +static int close_cb_called = 0; +static int connect_cb_called = 0; + +static void close_cb(uv_handle_t *handle) { + ASSERT_NOT_NULL(handle); + close_cb_called++; +} + +static void connect_cb_file(uv_connect_t *connect_req, int status) { + ASSERT_EQ(status, 0); + uv_close((uv_handle_t *) connect_req->handle, close_cb); + connect_cb_called++; +} + TEST_IMPL(pipe_win_uds) { + size_t size = MAX_PATH; + char path[MAX_PATH]; + ASSERT_OK(uv_os_tmpdir(path, &size)); + strcat_s(path, MAX_PATH, "\\uv_pipe_win_uds"); + + uv_fs_t fs; + uv_fs_unlink(uv_default_loop(), &fs, path, NULL); + uv_run(uv_default_loop(), UV_RUN_DEFAULT); + + uv_pipe_t server; + uv_pipe_t client; + uv_connect_t req; + int r; + + r = uv_pipe_init_ex(uv_default_loop(), &server, UV_PIPE_INIT_WIN_UDS); + ASSERT_OK(r); + r = uv_pipe_bind(&server, path); + ASSERT_OK(r); + uv_run(uv_default_loop(), UV_RUN_DEFAULT); + + r = uv_pipe_init_ex(uv_default_loop(), &client, UV_PIPE_INIT_WIN_UDS); + ASSERT_OK(r); + uv_pipe_connect(&req, &client, path, connect_cb_file); + uv_run(uv_default_loop(), UV_RUN_DEFAULT); + + ASSERT_EQ(1, close_cb_called); + ASSERT_EQ(1, connect_cb_called); + MAKE_VALGRIND_HAPPY(uv_default_loop()); return 0; }