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 | #if !defined(_WIN32) |
23 | |
24 | #include "uv.h" |
25 | #include "task.h" |
26 | #include <fcntl.h> |
27 | #include <unistd.h> |
28 | |
29 | static unsigned int read_cb_called; |
30 | |
31 | static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf) { |
32 | static char slab[1]; |
33 | buf->base = slab; |
34 | buf->len = sizeof(slab); |
35 | } |
36 | |
37 | static void read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) { |
38 | switch (++read_cb_called) { |
39 | case 1: |
40 | ASSERT(nread == 1); |
41 | uv_read_stop(handle); |
42 | break; |
43 | case 2: |
44 | ASSERT(nread == UV_EOF); |
45 | uv_close((uv_handle_t *) handle, NULL); |
46 | break; |
47 | default: |
48 | ASSERT(!"read_cb_called > 2" ); |
49 | } |
50 | } |
51 | |
52 | TEST_IMPL(close_fd) { |
53 | uv_pipe_t pipe_handle; |
54 | int fd[2]; |
55 | |
56 | ASSERT(0 == pipe(fd)); |
57 | ASSERT(0 == uv_pipe_init(uv_default_loop(), &pipe_handle, 0)); |
58 | ASSERT(0 == uv_pipe_open(&pipe_handle, fd[0])); |
59 | fd[0] = -1; /* uv_pipe_open() takes ownership of the file descriptor. */ |
60 | ASSERT(1 == write(fd[1], "" , 1)); |
61 | ASSERT(0 == close(fd[1])); |
62 | fd[1] = -1; |
63 | ASSERT(0 == uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb)); |
64 | ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); |
65 | ASSERT(1 == read_cb_called); |
66 | ASSERT(0 == uv_is_active((const uv_handle_t *) &pipe_handle)); |
67 | ASSERT(0 == uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb)); |
68 | ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); |
69 | ASSERT(2 == read_cb_called); |
70 | ASSERT(0 != uv_is_closing((const uv_handle_t *) &pipe_handle)); |
71 | |
72 | MAKE_VALGRIND_HAPPY(); |
73 | return 0; |
74 | } |
75 | |
76 | #else |
77 | |
78 | typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */ |
79 | |
80 | #endif /* !_WIN32 */ |
81 | |