xref: /libuv/test/test-close-fd.c (revision 011a1ac1)
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "task.h"
24 #ifndef _WIN32
25 #include <unistd.h>
26 #endif
27 
28 static unsigned int read_cb_called;
29 
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)30 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
31   static char slab[1];
32   buf->base = slab;
33   buf->len = sizeof(slab);
34 }
35 
read_cb(uv_stream_t * handle,ssize_t nread,const uv_buf_t * buf)36 static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
37   switch (++read_cb_called) {
38   case 1:
39     ASSERT_EQ(1, nread);
40     uv_read_stop(handle);
41     break;
42   case 2:
43     ASSERT_EQ(nread, UV_EOF);
44     uv_close((uv_handle_t *) handle, NULL);
45     break;
46   default:
47     ASSERT(!"read_cb_called > 2");
48   }
49 }
50 
TEST_IMPL(close_fd)51 TEST_IMPL(close_fd) {
52   uv_pipe_t pipe_handle;
53   uv_fs_t req;
54   uv_buf_t bufs[1];
55   uv_file fd[2];
56   bufs[0] = uv_buf_init("", 1);
57 
58   ASSERT_OK(uv_pipe(fd, 0, 0));
59   ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_handle, 0));
60   ASSERT_OK(uv_pipe_open(&pipe_handle, fd[0]));
61   /* uv_pipe_open() takes ownership of the file descriptor. */
62   fd[0] = -1;
63 
64   ASSERT_EQ(1, uv_fs_write(NULL, &req, fd[1], bufs, 1, -1, NULL));
65   ASSERT_EQ(1, req.result);
66   uv_fs_req_cleanup(&req);
67 #ifdef _WIN32
68   ASSERT_OK(_close(fd[1]));
69 #else
70   ASSERT_OK(close(fd[1]));
71 #endif
72   fd[1] = -1;
73   ASSERT_OK(uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb));
74   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
75   ASSERT_EQ(1, read_cb_called);
76   ASSERT_OK(uv_is_active((const uv_handle_t *) &pipe_handle));
77   ASSERT_OK(uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb));
78   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
79   ASSERT_EQ(2, read_cb_called);
80   ASSERT_NE(0, uv_is_closing((const uv_handle_t *) &pipe_handle));
81 
82   MAKE_VALGRIND_HAPPY(uv_default_loop());
83   return 0;
84 }
85