|
| 1 | +/* Copyright libuv project contributors. All rights reserved. |
| 2 | + * |
| 3 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | + * purpose with or without fee is hereby granted, provided that the above |
| 5 | + * copyright notice and this permission notice appear in all copies. |
| 6 | + * |
| 7 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "uv.h" |
| 17 | +#include "task.h" |
| 18 | + |
| 19 | +#include <string.h> /* memcmp, memset */ |
| 20 | +#ifndef _WIN32 |
| 21 | +#include <unistd.h> /* close */ |
| 22 | +#endif |
| 23 | + |
| 24 | +/* Verify that uv_pipe_open_ex() accepts a native OS handle directly. |
| 25 | + * |
| 26 | + * On Windows uv_pipe_open() treats its argument as a CRT file descriptor and |
| 27 | + * converts it to a HANDLE via _get_osfhandle(). A raw HANDLE (e.g. an |
| 28 | + * inherited anonymous pipe) is in a different namespace and fails with EBADF. |
| 29 | + * uv_pipe_open_ex() takes the HANDLE as-is. On Unix uv_os_fd_t is the file |
| 30 | + * descriptor itself, so the call is equivalent to uv_pipe_open(). |
| 31 | + */ |
| 32 | + |
| 33 | +#ifdef _WIN32 |
| 34 | +static void write_cb(uv_write_t* req, int status) { |
| 35 | + ASSERT_OK(status); |
| 36 | + req->handle = NULL; /* signal completion of write_cb */ |
| 37 | +} |
| 38 | +#endif |
| 39 | + |
| 40 | + |
| 41 | +TEST_IMPL(pipe_open_ex) { |
| 42 | + uv_pipe_t pipe_handle; |
| 43 | + uv_os_fd_t os_handle; |
| 44 | + uv_buf_t buf; |
| 45 | + char data[] = "test data"; |
| 46 | + char readbuf[64]; |
| 47 | +#ifdef _WIN32 |
| 48 | + HANDLE read_handle; |
| 49 | + DWORD bytes_read; |
| 50 | + uv_write_t write_req; |
| 51 | +#else |
| 52 | + uv_fs_t req; |
| 53 | + uv_file fd[2]; |
| 54 | + int n; |
| 55 | +#endif |
| 56 | + |
| 57 | + ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_handle, 0)); |
| 58 | + |
| 59 | +#ifdef _WIN32 |
| 60 | + /* CreatePipe() returns raw anonymous pipe HANDLEs that have no associated |
| 61 | + * CRT file descriptor - the exact scenario uv_pipe_open_ex() exists for. |
| 62 | + * The handles are synchronous, which also exercises the non-overlapped |
| 63 | + * pipe code path. */ |
| 64 | + ASSERT(CreatePipe(&read_handle, &os_handle, NULL, 0)); |
| 65 | +#else |
| 66 | + ASSERT_OK(uv_pipe(fd, 0, 0)); |
| 67 | + os_handle = fd[1]; |
| 68 | +#endif |
| 69 | + |
| 70 | + /* Open the write end directly from its native OS handle. The handle is |
| 71 | + * owned by pipe_handle after this call. */ |
| 72 | + ASSERT_OK(uv_pipe_open_ex(&pipe_handle, os_handle)); |
| 73 | + ASSERT_OK(uv_stream_set_blocking((uv_stream_t*) &pipe_handle, 1)); |
| 74 | + |
| 75 | + buf = uv_buf_init(data, sizeof(data)); |
| 76 | +#ifdef _WIN32 |
| 77 | + /* uv_try_write() is not implemented for pipes on Windows. */ |
| 78 | + ASSERT_OK(uv_write(&write_req, |
| 79 | + (uv_stream_t*) &pipe_handle, |
| 80 | + &buf, |
| 81 | + 1, |
| 82 | + write_cb)); |
| 83 | + ASSERT_NOT_NULL(write_req.handle); |
| 84 | + ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE)); |
| 85 | + ASSERT_NULL(write_req.handle); /* signaled completion of write_cb */ |
| 86 | +#else |
| 87 | + n = uv_try_write((uv_stream_t*) &pipe_handle, &buf, 1); |
| 88 | + ASSERT_EQ(n, (int) sizeof(data)); |
| 89 | +#endif |
| 90 | + |
| 91 | + /* Read the data back from the other end and verify it round-trips. */ |
| 92 | + memset(readbuf, 0, sizeof(readbuf)); |
| 93 | +#ifdef _WIN32 |
| 94 | + ASSERT(ReadFile(read_handle, readbuf, sizeof(readbuf), &bytes_read, NULL)); |
| 95 | + ASSERT_EQ(bytes_read, (DWORD) sizeof(data)); |
| 96 | +#else |
| 97 | + buf = uv_buf_init(readbuf, sizeof(readbuf)); |
| 98 | + n = uv_fs_read(NULL, &req, fd[0], &buf, 1, -1, NULL); |
| 99 | + ASSERT_EQ(n, (int) sizeof(data)); |
| 100 | + uv_fs_req_cleanup(&req); |
| 101 | +#endif |
| 102 | + ASSERT_OK(memcmp(readbuf, data, sizeof(data))); |
| 103 | + |
| 104 | + uv_close((uv_handle_t*) &pipe_handle, NULL); |
| 105 | + ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT)); |
| 106 | + |
| 107 | +#ifdef _WIN32 |
| 108 | + ASSERT(CloseHandle(read_handle)); |
| 109 | +#else |
| 110 | + ASSERT_OK(close(fd[0])); |
| 111 | +#endif |
| 112 | + |
| 113 | + MAKE_VALGRIND_HAPPY(uv_default_loop()); |
| 114 | + return 0; |
| 115 | +} |
0 commit comments