libuv/test/test-ipc.c
Jameson Nash 4e774df4cc 2023.05.19, Version 1.45.0 (Stable)
Changes since version 1.44.2:
 
 * win: remove stdint-msvc2008.h (Ben Noordhuis)
 
 * android: remove pthread-fixes.c (Ben Noordhuis)
 
 * build: enable MSVC_RUNTIME_LIBRARY setting (自发对称破缺)
 
 * unix: switch to c11 atomics (Ben Noordhuis)
 
 * unix: don't accept() connections in a loop (Ben Noordhuis)
 
 * win: fix off-by-1 buffer overrun in uv_exepath() (Ben Noordhuis)
 
 * build: switch ci from macos-10.15 to macos-11 (Ben Noordhuis)
 
 * win: fix thread race in uv_cwd() and uv_chdir() (Ben Noordhuis)
 
 * unix,win: remove UV_HANDLE_SHUTTING flag (Santiago Gimeno)
 
 * win: support Windows 11 in uv_os_uname() (Luan Devecchi)
 
 * unix: fix uv_getrusage() ru_maxrss reporting (Ben Noordhuis)
 
 * doc: add note about offset -1 in uv_fs_read/write (Steven Schveighoffer)
 
 * test: fix musl libc.a dlerror() test expectation (Ben Noordhuis)
 
 * kqueue: DRY file descriptor deletion logic (Ben Noordhuis)
 
 * linux: teach uv_get_constrained_memory() cgroupsv2 (Ben Noordhuis)
 
 * build: upgrade qemu-user-static package (Ben Noordhuis)
 
 * linux: move epoll.c back into linux-core.c (Ben Noordhuis)
 
 * unix: remove pre-macos 10.8 compatibility hack (Ben Noordhuis)
 
 * unix,win: fix memory leak in uv_fs_scandir() (Ben Noordhuis)
 
 * build: restore qemu download logic (Ben Noordhuis)
 
 * win: fix uv__pipe_accept memory leak (number201724)
 
 * doc: update LINKS.md (Daniel)
 
 * unix: simplify atomic op in uv_tty_reset_mode() (Ben Noordhuis)
 
 * build: add LIBUV_BUILD_SHARED cmake option (Christian Clason)
 
 * linux: remove unused or obsolete syscall wrappers (Ben Noordhuis)
 
 * linux: merge files back into single file (Ben Noordhuis)
 
 * stream: process more than one write req per loop tick (ywave620)
 
 * unix,win: give thread pool threads an 8 MB stack (Ben Noordhuis)
 
 * build: add MemorySanitizer (MSAN) support (Ben Noordhuis)
 
 * doc: add uv_poll_cb status==UV_EBADF note (jensbjorgensen)
 
 * build: support AddressSanitizer on MSVC (Jameson Nash)
 
 * win,pipe: improve method of obtaining pid for ipc (number201724)
 
 * thread: add support for affinity (daomingq)
 
 * include: map ENODATA error code (Ben Noordhuis)
 
 * build: remove bashism from autogen.sh (Santiago Gimeno)
 
 * win,tcp,udp: remove "active streams" optimization (Saúl Ibarra Corretgé)
 
 * win: drop code checking for Windows XP / Server 2k3 (Saúl Ibarra Corretgé)
 
 * unix,win: fix 'sprintf' is deprecated warning (twosee)
 
 * doc: mention close_cb can be NULL (Qix)
 
 * win: optimize udp receive performance (ywave620)
 
 * win: fix an incompatible types warning (twosee)
 
 * doc: document 0 return value for free/total memory (Ben Noordhuis)
 
 * darwin: use hw.cpufrequency again for frequency info (Jameson Nash)
 
 * win,test: change format of TEST_PIPENAME's (Santiago Gimeno)
 
 * win,pipe: fixes in uv_pipe_connect() (Santiago Gimeno)
 
 * misc: fix return value of memory functions (theanarkh)
 
 * src: add new metrics APIs (Trevor Norris)
 
 * thread: add uv_thread_getcpu() (daomingq)
 
 * build: don't use ifaddrs.h on solaris 10 (Edward Humes)
 
 * unix,win: add uv_get_available_memory() (Tim Besard)
 
 * test: fix -Wunused-but-set-variable warnings (Ben Noordhuis)
 
 * doc: bump min supported linux and freebsd versions (Ben Noordhuis)
 
 * Add Socket Runtime to the LINKS.md (Sergey Rubanov)
 
 * unix: drop kfreebsd support (Ben Noordhuis)
 
 * win: fix fstat for pipes and character files (Stefan Stojanovic)
 
 * win: fix -Wunused-variable warning (Ben Noordhuis)
 
 * win: fix -Wunused-function warning (Ben Noordhuis)
 
 * build: drop qemu-alpha from ci matrix (Ben Noordhuis)
 
 * win: move child_stdio_buffer out of uv_process_t (Santiago Gimeno)
 
 * test: fix some unreachable code warnings (Santiago Gimeno)
 
 * linux: simplify uv_uptime() (Ben Noordhuis)
 
 * test: unflake fs_event_watch_dir test (Ben Noordhuis)
 
 * darwin: remove unused fsevents symbol lookups (Ben Noordhuis)
 
 * build: add define guard around UV_EXTERN (Zvicii)
 
 * build: add UndefinedBehaviorSanitizer support (Ben Noordhuis)
 
 * build: enable platform_output test on qemu (Ben Noordhuis)
 
 * linux: handle cpu hotplugging in uv_cpu_info() (Ben Noordhuis)
 
 * build: remove unnecessary policy setting (dundargoc)
 
 * docs: add vcpkg instruction step (Jack·Boos·Yu)
 
 * win,fs: fix readlink errno for a non-symlink file (Darshan Sen)
 
 * misc: extend getpw to take uid as an argument (Jameson Nash)
 
 * unix,win: use static_assert when available (Ben Noordhuis)
 
 * docs: delete code Makefile (Jameson Nash)
 
 * docs: add CI for docs PRs (Jameson Nash)
 
 * docs: update Sphinx version on RTD (Jameson Nash)
 
 * doc: clean up license file (Ben Noordhuis)
 
 * test: fix some warnings when compiling tests (panran)
 
 * build,win: add mingw-w64 CI configuration (Jameson Nash)
 
 * build: add CI for distcheck (Jameson Nash)
 
 * unix: remove busy loop from uv_async_send (Jameson Nash)
 
 * doc: document uv_fs_cb type (Tamás Bálint Misius)
 
 * build: Improve build by cmake for Cygwin (erw7)
 
 * build: add libuv:: namespace to libuvConfig.cmake (AJ Heller)
 
 * test: fix ThreadSanitizer thread leak warning (Ben Noordhuis)
 
 * test: fix ThreadSanitizer data race warning (Ben Noordhuis)
 
 * test: fix ThreadSanitizer data race warning (Ben Noordhuis)
 
 * test: fix ThreadSanitizer data race warning (Ben Noordhuis)
 
 * test: cond-skip fork_threadpool_queue_work_simple (Ben Noordhuis)
 
 * test: cond-skip signal_multiple_loops (Ben Noordhuis)
 
 * test: cond-skip tcp_writealot (Ben Noordhuis)
 
 * build: promote tsan ci to must-pass (Ben Noordhuis)
 
 * build: add CI for OpenBSD and FreeBSD (James McCoy)
 
 * build,test: fix distcheck errors (Jameson Nash)
 
 * test: remove bad tty window size assumption (Ben Noordhuis)
 
 * darwin,process: feed kevent the signal to reap children (Jameson Nash)
 
 * unix: abort on clock_gettime() error (Ben Noordhuis)
 
 * test: remove timing-sensitive check (Ben Noordhuis)
 
 * unix: DRY and fix tcp bind error path (Jameson Nash)
 
 * macos: fix fsevents thread race conditions (Ben Noordhuis)
 
 * win: fix leak in uv_chdir (Trevor Norris)
 
 * test: make valgrind happy (Trevor Norris)
 
 * barrier: wait for prior out before next in (Jameson Nash)
 
 * test: fix visual studio 2015 build error (Ben Noordhuis)
 
 * linux: fix ceph copy error truncating readonly files (Bruno Passeri)
 
 * test: silence more valgrind warnings (Trevor Norris)
 
 * doc: add entries to LINKS.md (Trevor Norris)
 
 * win,unix: change execution order of timers (Trevor Norris)
 
 * doc: add trevnorris to maintainers (Trevor Norris)
 
 * linux: remove epoll_pwait() emulation code path (Ben Noordhuis)
 
 * linux: replace unsafe macro with inline function (Ben Noordhuis)
 
 * linux: remove arm oabi support (Ben Noordhuis)
 
 * unix,sunos: SO_REUSEPORT not valid on all sockets (Stacey Marshall)
 
 * doc: consistent single backquote in misc.rst (Jason Zhang)
 
 * src: switch to use C11 atomics where available (Trevor Norris)
 
 * test: don't use static buffer for formatting (Ben Noordhuis)
 
 * linux: introduce io_uring support (Ben Noordhuis)
 
 * linux: fix academic valgrind warning (Ben Noordhuis)
 
 * test: disable signal test under ASan and MSan (Ben Noordhuis)
 
 * linux: add IORING_OP_OPENAT support (Ben Noordhuis)
 
 * linux: add IORING_OP_CLOSE support (Ben Noordhuis)
 
 * linux: remove bug workaround for obsolete kernels (Ben Noordhuis)
 
 * doc: update active maintainers list (Ben Noordhuis)
 
 * test: add ASSERT_OK (Trevor Norris)
 
 * src: fix events/events_waiting metrics counter (Trevor Norris)
 
 * unix,win: add uv_clock_gettime() (Ben Noordhuis)
 
 * build: remove freebsd and openbsd buildbots (Ben Noordhuis)
 
 * win: fix race condition in uv__init_console() (sivadeilra)
 
 * linux: fix logic bug in sqe ring space check (Ben Noordhuis)
 
 * linux: use io_uring to batch epoll_ctl calls (Ben Noordhuis)
 
 * macos: update minimum supported version (Santiago Gimeno)
 
 * docs: fix some typos (cui fliter)
 
 * unix: use memcpy() instead of type punning (Ben Noordhuis)
 
 * test: add additional assert (Mohammed Keyvanzadeh)
 
 * build: export compile_commands.json (Lewis Russell)
 
 * win,process: write minidumps when sending SIGQUIT (Elliot Saba)
 
 * unix: constrained_memory should return UINT64_MAX (Tim Besard)
 
 * unix: handle CQ overflow in iou ring (Santiago Gimeno)
 
 * unix: remove clang compiler warning pragmas (Ben Noordhuis)
 
 * win: fix mingw build (gengjiawen)
 
 * test: fix -Wbool-compare compiler warning (Ben Noordhuis)
 
 * win: define MiniDumpWithAvxXStateContext always (Santiago Gimeno)
 
 * freebsd: hard-code UV_ENODATA definition (Santiago Gimeno)
 
 * linux: work around EOWNERDEAD io_uring kernel bug (Ben Noordhuis)
 
 * linux: fix WRITEV with lots of bufs using io_uring (Santiago Gimeno)
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEYS8OrZQBYiN530QC8ow8jaM8A74FAmRnW50ACgkQ8ow8jaM8
 A76noBAAiavsCBvAC554x4Naic+B3fEYjKnLe8OLTbdN4XQsS2uSkCEuP0TuwLE7
 Q93CHgDFcL//XmNAXwIKMIvZ7G25zga93qxrZdd7Hsuthg5oJwsrZb8faHG7CeKy
 Fq+SLCUdLcwOR9m9RDp1u3h/pIhoqQvLUlPcBvDKVaJOVhIngawKU/vC125z6ZJs
 CYaKMlPbU2WjIkkoQjNRYL8oolmWTs5T/8AhIVWntTCyc/+/n7uxmWwlfL2iR8Kj
 u5IT7Vhh6a7VC+dGh26k/FN0RGdAgJCZ1Jnrt+llHxRVuw0xEBnrTAyS3hOkA7Me
 s+8VUaCsreVi9p66hqE5b4IL5e9yUEB18A5dQBY+TFNeXaQETVg9BxPMXbhoh+XB
 2643LZn/kN1iilZBMSMl9kqB66qeUihv8cvjMnrTzq+nSxJvuc13XPfbH+zhMkVh
 CQaFMq3v5vc4Kyk2YrjcsfGCIkRxRGlhSmk5riz1Xf+erf4GuOl+o2fA6YvVDAnE
 rzlzpGoRSHgHYC23KirkBHo7qQVbWy+NeLWFRFSHfSN4+czttdusiFSjxERavfSh
 ezRKH6dNHI8C9fqT3oAitXOgFt6AsMiYMt7JpJwPm0SdAFjIEkgjAfhkSQADUHz7
 qezLtJoCL+bcUXE8tgRRuz+J6x4FhKBZVu5uA6fV1UuyWI74DBU=
 =YAOh
 -----END PGP SIGNATURE-----

Merge tag 'v1.45.0' into merge_1.45.0
2023-05-23 09:09:41 -04:00

861 lines
22 KiB
C

/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
#include <stdio.h>
#include <string.h>
static uv_pipe_t channel;
static uv_tcp_t tcp_server;
static uv_tcp_t tcp_server2;
static uv_tcp_t tcp_connection;
static int exit_cb_called;
static int read_cb_called;
static int tcp_write_cb_called;
static int tcp_read_cb_called;
static int on_pipe_read_called;
static int local_conn_accepted;
static int remote_conn_accepted;
static int tcp_server_listening;
static uv_write_t write_req;
static uv_write_t write_req2;
static uv_write_t conn_notify_req;
static int close_cb_called;
static int connection_accepted;
static int tcp_conn_read_cb_called;
static int tcp_conn_write_cb_called;
static int send_zero_write;
typedef struct {
uv_connect_t conn_req;
uv_write_t tcp_write_req;
uv_tcp_t conn;
} tcp_conn;
#define CONN_COUNT 100
#define BACKLOG 128
static void close_server_conn_cb(uv_handle_t* handle) {
free(handle);
}
static void on_connection(uv_stream_t* server, int status) {
uv_tcp_t* conn;
int r;
if (!local_conn_accepted) {
/* Accept the connection and close it. Also and close the server. */
ASSERT_EQ(status, 0);
ASSERT_PTR_EQ(&tcp_server, server);
conn = malloc(sizeof(*conn));
ASSERT_NOT_NULL(conn);
r = uv_tcp_init(server->loop, conn);
ASSERT_EQ(r, 0);
r = uv_accept(server, (uv_stream_t*)conn);
ASSERT_EQ(r, 0);
uv_close((uv_handle_t*)conn, close_server_conn_cb);
uv_close((uv_handle_t*)server, NULL);
local_conn_accepted = 1;
}
}
static void exit_cb(uv_process_t* process,
int64_t exit_status,
int term_signal) {
printf("exit_cb\n");
exit_cb_called++;
ASSERT_EQ(exit_status, 0);
ASSERT_EQ(term_signal, 0);
uv_close((uv_handle_t*)process, NULL);
}
static void on_alloc(uv_handle_t* handle,
size_t suggested_size,
uv_buf_t* buf) {
buf->base = malloc(suggested_size);
buf->len = suggested_size;
}
static void close_client_conn_cb(uv_handle_t* handle) {
tcp_conn* p = (tcp_conn*)handle->data;
free(p);
}
static void connect_cb(uv_connect_t* req, int status) {
uv_close((uv_handle_t*)req->handle, close_client_conn_cb);
}
static void make_many_connections(void) {
tcp_conn* conn;
struct sockaddr_in addr;
int r, i;
for (i = 0; i < CONN_COUNT; i++) {
conn = malloc(sizeof(*conn));
ASSERT_NOT_NULL(conn);
r = uv_tcp_init(uv_default_loop(), &conn->conn);
ASSERT_EQ(r, 0);
ASSERT_EQ(0, uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
r = uv_tcp_connect(&conn->conn_req,
(uv_tcp_t*) &conn->conn,
(const struct sockaddr*) &addr,
connect_cb);
ASSERT_EQ(r, 0);
conn->conn.data = conn;
}
}
static void on_read(uv_stream_t* handle,
ssize_t nread,
const uv_buf_t* buf) {
int r;
uv_pipe_t* pipe;
uv_handle_type pending;
uv_buf_t outbuf;
pipe = (uv_pipe_t*) handle;
if (nread == 0) {
/* Everything OK, but nothing read. */
free(buf->base);
return;
}
if (nread < 0) {
if (nread == UV_EOF) {
free(buf->base);
return;
}
printf("error recving on channel: %s\n", uv_strerror(nread));
abort();
}
fprintf(stderr, "got %d bytes\n", (int)nread);
pending = uv_pipe_pending_type(pipe);
if (!tcp_server_listening) {
ASSERT_EQ(1, uv_pipe_pending_count(pipe));
ASSERT_GT(nread, 0);
ASSERT_NOT_NULL(buf->base);
ASSERT_NE(pending, UV_UNKNOWN_HANDLE);
read_cb_called++;
/* Accept the pending TCP server, and start listening on it. */
ASSERT_EQ(pending, UV_TCP);
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT_EQ(r, 0);
r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server);
ASSERT_EQ(r, 0);
r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, on_connection);
ASSERT_EQ(r, 0);
tcp_server_listening = 1;
/* Make sure that the expected data is correctly multiplexed. */
ASSERT_MEM_EQ("hello\n", buf->base, nread);
outbuf = uv_buf_init("foobar\n", 7);
r = uv_write(&write_req, (uv_stream_t*)pipe, &outbuf, 1, NULL);
ASSERT_EQ(r, 0);
/* Create a bunch of connections to get both servers to accept. */
make_many_connections();
} else if (memcmp("accepted_connection\n", buf->base, nread) == 0) {
/* Remote server has accepted a connection. Close the channel. */
ASSERT_EQ(0, uv_pipe_pending_count(pipe));
ASSERT_EQ(pending, UV_UNKNOWN_HANDLE);
remote_conn_accepted = 1;
uv_close((uv_handle_t*)&channel, NULL);
}
free(buf->base);
}
#ifdef _WIN32
static void on_read_listen_after_bound_twice(uv_stream_t* handle,
ssize_t nread,
const uv_buf_t* buf) {
int r;
uv_pipe_t* pipe;
uv_handle_type pending;
pipe = (uv_pipe_t*) handle;
if (nread == 0) {
/* Everything OK, but nothing read. */
free(buf->base);
return;
}
if (nread < 0) {
if (nread == UV_EOF) {
free(buf->base);
return;
}
printf("error recving on channel: %s\n", uv_strerror(nread));
abort();
}
fprintf(stderr, "got %d bytes\n", (int)nread);
ASSERT_GT(uv_pipe_pending_count(pipe), 0);
pending = uv_pipe_pending_type(pipe);
ASSERT_GT(nread, 0);
ASSERT_NOT_NULL(buf->base);
ASSERT_NE(pending, UV_UNKNOWN_HANDLE);
read_cb_called++;
if (read_cb_called == 1) {
/* Accept the first TCP server, and start listening on it. */
ASSERT_EQ(pending, UV_TCP);
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT_EQ(r, 0);
r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server);
ASSERT_EQ(r, 0);
r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, on_connection);
ASSERT_EQ(r, 0);
} else if (read_cb_called == 2) {
/* Accept the second TCP server, and start listening on it. */
ASSERT_EQ(pending, UV_TCP);
r = uv_tcp_init(uv_default_loop(), &tcp_server2);
ASSERT_EQ(r, 0);
r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server2);
ASSERT_EQ(r, 0);
r = uv_listen((uv_stream_t*)&tcp_server2, BACKLOG, on_connection);
ASSERT_EQ(r, UV_EADDRINUSE);
uv_close((uv_handle_t*)&tcp_server, NULL);
uv_close((uv_handle_t*)&tcp_server2, NULL);
ASSERT_EQ(0, uv_pipe_pending_count(pipe));
uv_close((uv_handle_t*)&channel, NULL);
}
free(buf->base);
}
#endif
void spawn_helper(uv_pipe_t* channel,
uv_process_t* process,
const char* helper) {
uv_process_options_t options;
size_t exepath_size;
char exepath[1024];
char* args[3];
int r;
uv_stdio_container_t stdio[3];
r = uv_pipe_init(uv_default_loop(), channel, 1);
ASSERT_EQ(r, 0);
ASSERT_NE(channel->ipc, 0);
exepath_size = sizeof(exepath);
r = uv_exepath(exepath, &exepath_size);
ASSERT_EQ(r, 0);
exepath[exepath_size] = '\0';
args[0] = exepath;
args[1] = (char*)helper;
args[2] = NULL;
memset(&options, 0, sizeof(options));
options.file = exepath;
options.args = args;
options.exit_cb = exit_cb;
options.stdio = stdio;
options.stdio_count = ARRAY_SIZE(stdio);
stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE;
stdio[0].data.stream = (uv_stream_t*) channel;
stdio[1].flags = UV_INHERIT_FD;
stdio[1].data.file = uv_get_osfhandle(1);
stdio[2].flags = UV_INHERIT_FD;
stdio[2].data.file = uv_get_osfhandle(2);
r = uv_spawn(uv_default_loop(), process, &options);
ASSERT_EQ(r, 0);
}
static void on_tcp_write(uv_write_t* req, int status) {
ASSERT_EQ(status, 0);
ASSERT_PTR_EQ(req->handle, &tcp_connection);
tcp_write_cb_called++;
}
static void on_read_alloc(uv_handle_t* handle,
size_t suggested_size,
uv_buf_t* buf) {
buf->base = malloc(suggested_size);
buf->len = suggested_size;
}
static void on_tcp_read(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
ASSERT_GT(nread, 0);
ASSERT_MEM_EQ("hello again\n", buf->base, nread);
ASSERT_PTR_EQ(tcp, &tcp_connection);
free(buf->base);
tcp_read_cb_called++;
uv_close((uv_handle_t*)tcp, NULL);
uv_close((uv_handle_t*)&channel, NULL);
}
static void on_read_connection(uv_stream_t* handle,
ssize_t nread,
const uv_buf_t* buf) {
int r;
uv_buf_t outbuf;
uv_pipe_t* pipe;
uv_handle_type pending;
pipe = (uv_pipe_t*) handle;
if (nread == 0) {
/* Everything OK, but nothing read. */
free(buf->base);
return;
}
if (nread < 0) {
if (nread == UV_EOF) {
free(buf->base);
return;
}
printf("error recving on channel: %s\n", uv_strerror(nread));
abort();
}
fprintf(stderr, "got %d bytes\n", (int)nread);
ASSERT_EQ(1, uv_pipe_pending_count(pipe));
pending = uv_pipe_pending_type(pipe);
ASSERT_GT(nread, 0);
ASSERT_NOT_NULL(buf->base);
ASSERT_NE(pending, UV_UNKNOWN_HANDLE);
read_cb_called++;
/* Accept the pending TCP connection */
ASSERT_EQ(pending, UV_TCP);
r = uv_tcp_init(uv_default_loop(), &tcp_connection);
ASSERT_EQ(r, 0);
r = uv_accept(handle, (uv_stream_t*)&tcp_connection);
ASSERT_EQ(r, 0);
/* Make sure that the expected data is correctly multiplexed. */
ASSERT_MEM_EQ("hello\n", buf->base, nread);
/* Write/read to/from the connection */
outbuf = uv_buf_init("world\n", 6);
r = uv_write(&write_req, (uv_stream_t*)&tcp_connection, &outbuf, 1,
on_tcp_write);
ASSERT_EQ(r, 0);
r = uv_read_start((uv_stream_t*)&tcp_connection, on_read_alloc, on_tcp_read);
ASSERT_EQ(r, 0);
free(buf->base);
}
static void on_read_send_zero(uv_stream_t* handle,
ssize_t nread,
const uv_buf_t* buf) {
ASSERT(nread == 0 || nread == UV_EOF);
free(buf->base);
}
static int run_ipc_test(const char* helper, uv_read_cb read_cb) {
uv_process_t process;
int r;
spawn_helper(&channel, &process, helper);
uv_read_start((uv_stream_t*)&channel, on_alloc, read_cb);
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT_EQ(r, 0);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(ipc_listen_before_write) {
#if defined(NO_SEND_HANDLE_ON_PIPE)
RETURN_SKIP(NO_SEND_HANDLE_ON_PIPE);
#endif
int r = run_ipc_test("ipc_helper_listen_before_write", on_read);
ASSERT_EQ(local_conn_accepted, 1);
ASSERT_EQ(remote_conn_accepted, 1);
ASSERT_EQ(read_cb_called, 1);
ASSERT_EQ(exit_cb_called, 1);
return r;
}
TEST_IMPL(ipc_listen_after_write) {
#if defined(NO_SEND_HANDLE_ON_PIPE)
RETURN_SKIP(NO_SEND_HANDLE_ON_PIPE);
#endif
int r = run_ipc_test("ipc_helper_listen_after_write", on_read);
ASSERT_EQ(local_conn_accepted, 1);
ASSERT_EQ(remote_conn_accepted, 1);
ASSERT_EQ(read_cb_called, 1);
ASSERT_EQ(exit_cb_called, 1);
return r;
}
TEST_IMPL(ipc_tcp_connection) {
#if defined(NO_SEND_HANDLE_ON_PIPE)
RETURN_SKIP(NO_SEND_HANDLE_ON_PIPE);
#endif
int r = run_ipc_test("ipc_helper_tcp_connection", on_read_connection);
ASSERT_EQ(read_cb_called, 1);
ASSERT_EQ(tcp_write_cb_called, 1);
ASSERT_EQ(tcp_read_cb_called, 1);
ASSERT_EQ(exit_cb_called, 1);
return r;
}
#ifdef _WIN32
TEST_IMPL(listen_with_simultaneous_accepts) {
uv_tcp_t server;
int r;
struct sockaddr_in addr;
ASSERT_EQ(0, uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
r = uv_tcp_init(uv_default_loop(), &server);
ASSERT_EQ(r, 0);
r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
ASSERT_EQ(r, 0);
r = uv_tcp_simultaneous_accepts(&server, 1);
ASSERT_EQ(r, 0);
r = uv_listen((uv_stream_t*)&server, SOMAXCONN, NULL);
ASSERT_EQ(r, 0);
ASSERT_EQ(server.reqs_pending, 32);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(listen_no_simultaneous_accepts) {
uv_tcp_t server;
int r;
struct sockaddr_in addr;
ASSERT_EQ(0, uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
r = uv_tcp_init(uv_default_loop(), &server);
ASSERT_EQ(r, 0);
r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
ASSERT_EQ(r, 0);
r = uv_tcp_simultaneous_accepts(&server, 0);
ASSERT_EQ(r, 0);
r = uv_listen((uv_stream_t*)&server, SOMAXCONN, NULL);
ASSERT_EQ(r, 0);
ASSERT_EQ(server.reqs_pending, 1);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(ipc_listen_after_bind_twice) {
#if defined(NO_SEND_HANDLE_ON_PIPE)
RETURN_SKIP(NO_SEND_HANDLE_ON_PIPE);
#endif
int r = run_ipc_test("ipc_helper_bind_twice", on_read_listen_after_bound_twice);
ASSERT_EQ(read_cb_called, 2);
ASSERT_EQ(exit_cb_called, 1);
return r;
}
#endif
TEST_IMPL(ipc_send_zero) {
int r;
r = run_ipc_test("ipc_helper_send_zero", on_read_send_zero);
ASSERT_EQ(r, 0);
return 0;
}
/* Everything here runs in a child process. */
static tcp_conn conn;
static void close_cb(uv_handle_t* handle) {
close_cb_called++;
}
static void conn_notify_write_cb(uv_write_t* req, int status) {
uv_close((uv_handle_t*)&tcp_server, close_cb);
uv_close((uv_handle_t*)&channel, close_cb);
}
static void tcp_connection_write_cb(uv_write_t* req, int status) {
ASSERT_PTR_EQ(&conn.conn, req->handle);
uv_close((uv_handle_t*)req->handle, close_cb);
uv_close((uv_handle_t*)&channel, close_cb);
uv_close((uv_handle_t*)&tcp_server, close_cb);
tcp_conn_write_cb_called++;
}
static void send_zero_write_cb(uv_write_t* req, int status) {
ASSERT_EQ(status, 0);
send_zero_write++;
}
static void on_tcp_child_process_read(uv_stream_t* tcp,
ssize_t nread,
const uv_buf_t* buf) {
uv_buf_t outbuf;
int r;
if (nread < 0) {
if (nread == UV_EOF) {
free(buf->base);
return;
}
printf("error recving on tcp connection: %s\n", uv_strerror(nread));
abort();
}
ASSERT_GT(nread, 0);
ASSERT_MEM_EQ("world\n", buf->base, nread);
on_pipe_read_called++;
free(buf->base);
/* Write to the socket */
outbuf = uv_buf_init("hello again\n", 12);
r = uv_write(&conn.tcp_write_req, tcp, &outbuf, 1, tcp_connection_write_cb);
ASSERT_EQ(r, 0);
tcp_conn_read_cb_called++;
}
static void connect_child_process_cb(uv_connect_t* req, int status) {
int r;
ASSERT_EQ(status, 0);
r = uv_read_start(req->handle, on_read_alloc, on_tcp_child_process_read);
ASSERT_EQ(r, 0);
}
static void ipc_on_connection(uv_stream_t* server, int status) {
int r;
uv_buf_t buf;
if (!connection_accepted) {
/*
* Accept the connection and close it. Also let the other
* side know.
*/
ASSERT_EQ(status, 0);
ASSERT_PTR_EQ(&tcp_server, server);
r = uv_tcp_init(server->loop, &conn.conn);
ASSERT_EQ(r, 0);
r = uv_accept(server, (uv_stream_t*)&conn.conn);
ASSERT_EQ(r, 0);
uv_close((uv_handle_t*)&conn.conn, close_cb);
buf = uv_buf_init("accepted_connection\n", 20);
r = uv_write2(&conn_notify_req, (uv_stream_t*)&channel, &buf, 1,
NULL, conn_notify_write_cb);
ASSERT_EQ(r, 0);
connection_accepted = 1;
}
}
static void close_and_free_cb(uv_handle_t* handle) {
close_cb_called++;
free(handle);
}
static void ipc_on_connection_tcp_conn(uv_stream_t* server, int status) {
int r;
uv_buf_t buf;
uv_tcp_t* conn;
ASSERT_EQ(status, 0);
ASSERT_PTR_EQ(&tcp_server, server);
conn = malloc(sizeof(*conn));
ASSERT_NOT_NULL(conn);
r = uv_tcp_init(server->loop, conn);
ASSERT_EQ(r, 0);
r = uv_accept(server, (uv_stream_t*)conn);
ASSERT_EQ(r, 0);
/* Send the accepted connection to the other process */
buf = uv_buf_init("hello\n", 6);
r = uv_write2(&conn_notify_req, (uv_stream_t*)&channel, &buf, 1,
(uv_stream_t*)conn, NULL);
ASSERT_EQ(r, 0);
r = uv_read_start((uv_stream_t*) conn,
on_read_alloc,
on_tcp_child_process_read);
ASSERT_EQ(r, 0);
uv_close((uv_handle_t*)conn, close_and_free_cb);
}
int ipc_helper(int listen_after_write) {
/*
* This is launched from test-ipc.c. stdin is a duplex channel that we
* over which a handle will be transmitted.
*/
struct sockaddr_in addr;
int r;
uv_buf_t buf;
uv_os_fd_t stdin_handle = uv_convert_fd_to_handle(0);
ASSERT_EQ(0, uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
r = uv_pipe_init(uv_default_loop(), &channel, 1);
ASSERT_EQ(r, 0);
uv_pipe_open(&channel, stdin_handle);
ASSERT_EQ(1, uv_is_readable((uv_stream_t*) &channel));
ASSERT_EQ(1, uv_is_writable((uv_stream_t*) &channel));
ASSERT_EQ(0, uv_is_closing((uv_handle_t*) &channel));
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT_EQ(r, 0);
r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
ASSERT_EQ(r, 0);
if (!listen_after_write) {
r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection);
ASSERT_EQ(r, 0);
}
buf = uv_buf_init("hello\n", 6);
r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
(uv_stream_t*)&tcp_server, NULL);
ASSERT_EQ(r, 0);
if (listen_after_write) {
r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection);
ASSERT_EQ(r, 0);
}
notify_parent_process();
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT_EQ(r, 0);
ASSERT_EQ(connection_accepted, 1);
ASSERT_EQ(close_cb_called, 3);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
int ipc_helper_tcp_connection(void) {
/*
* This is launched from test-ipc.c. stdin is a duplex channel
* over which a handle will be transmitted.
*/
int r;
struct sockaddr_in addr;
uv_os_fd_t stdin_handle = uv_convert_fd_to_handle(0);
r = uv_pipe_init(uv_default_loop(), &channel, 1);
ASSERT_EQ(r, 0);
uv_pipe_open(&channel, stdin_handle);
ASSERT_EQ(1, uv_is_readable((uv_stream_t*) &channel));
ASSERT_EQ(1, uv_is_writable((uv_stream_t*) &channel));
ASSERT_EQ(0, uv_is_closing((uv_handle_t*) &channel));
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT_EQ(r, 0);
ASSERT_EQ(0, uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
ASSERT_EQ(r, 0);
r = uv_listen((uv_stream_t*)&tcp_server, BACKLOG, ipc_on_connection_tcp_conn);
ASSERT_EQ(r, 0);
/* Make a connection to the server */
r = uv_tcp_init(uv_default_loop(), &conn.conn);
ASSERT_EQ(r, 0);
ASSERT_EQ(0, uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
r = uv_tcp_connect(&conn.conn_req,
(uv_tcp_t*) &conn.conn,
(const struct sockaddr*) &addr,
connect_child_process_cb);
ASSERT_EQ(r, 0);
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT_EQ(r, 0);
ASSERT_EQ(tcp_conn_read_cb_called, 1);
ASSERT_EQ(tcp_conn_write_cb_called, 1);
ASSERT_EQ(close_cb_called, 4);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
int ipc_helper_bind_twice(void) {
/*
* This is launched from test-ipc.c. stdin is a duplex channel
* over which two handles will be transmitted.
*/
struct sockaddr_in addr;
int r;
uv_buf_t buf;
uv_os_fd_t stdin_handle = uv_convert_fd_to_handle(0);
ASSERT_EQ(0, uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
r = uv_pipe_init(uv_default_loop(), &channel, 1);
ASSERT_EQ(r, 0);
uv_pipe_open(&channel, stdin_handle);
ASSERT_EQ(1, uv_is_readable((uv_stream_t*) &channel));
ASSERT_EQ(1, uv_is_writable((uv_stream_t*) &channel));
ASSERT_EQ(0, uv_is_closing((uv_handle_t*) &channel));
buf = uv_buf_init("hello\n", 6);
r = uv_tcp_init(uv_default_loop(), &tcp_server);
ASSERT_EQ(r, 0);
r = uv_tcp_init(uv_default_loop(), &tcp_server2);
ASSERT_EQ(r, 0);
r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
ASSERT_EQ(r, 0);
r = uv_tcp_bind(&tcp_server2, (const struct sockaddr*) &addr, 0);
ASSERT_EQ(r, 0);
r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
(uv_stream_t*)&tcp_server, NULL);
ASSERT_EQ(r, 0);
r = uv_write2(&write_req2, (uv_stream_t*)&channel, &buf, 1,
(uv_stream_t*)&tcp_server2, NULL);
ASSERT_EQ(r, 0);
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT_EQ(r, 0);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
int ipc_helper_send_zero(void) {
int r;
uv_buf_t zero_buf;
zero_buf = uv_buf_init(0, 0);
r = uv_pipe_init(uv_default_loop(), &channel, 0);
ASSERT_EQ(r, 0);
uv_pipe_open(&channel, UV_STDIN_FD);
ASSERT_EQ(1, uv_is_readable((uv_stream_t*) &channel));
ASSERT_EQ(1, uv_is_writable((uv_stream_t*) &channel));
ASSERT_EQ(0, uv_is_closing((uv_handle_t*) &channel));
r = uv_write(&write_req,
(uv_stream_t*)&channel,
&zero_buf,
1,
send_zero_write_cb);
ASSERT_EQ(r, 0);
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT_EQ(r, 0);
ASSERT_EQ(send_zero_write, 1);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}