xref: /libuv/test/test-poll-closesocket.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 
23 #include <errno.h>
24 
25 #include "uv.h"
26 #include "task.h"
27 
28 #ifdef _WIN32
29 static uv_os_sock_t sock;
30 static uv_poll_t handle;
31 static int close_cb_called = 0;
32 
33 
close_cb(uv_handle_t * h)34 static void close_cb(uv_handle_t* h) {
35   close_cb_called++;
36 }
37 
38 
poll_cb(uv_poll_t * h,int status,int events)39 static void poll_cb(uv_poll_t* h, int status, int events) {
40   int r;
41 
42   ASSERT_OK(status);
43   ASSERT_PTR_EQ(h, &handle);
44 
45   r = uv_poll_start(&handle, UV_READABLE, poll_cb);
46   ASSERT_OK(r);
47 
48   closesocket(sock);
49   uv_close((uv_handle_t*) &handle, close_cb);
50 
51 }
52 #endif
53 
54 
TEST_IMPL(poll_closesocket)55 TEST_IMPL(poll_closesocket) {
56 #ifndef _WIN32
57   RETURN_SKIP("Test only relevant on Windows");
58 #else
59   struct WSAData wsa_data;
60   int r;
61   unsigned long on;
62   struct sockaddr_in addr;
63 
64   r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
65   ASSERT_OK(r);
66 
67   sock = socket(AF_INET, SOCK_STREAM, 0);
68   ASSERT_NE(sock, INVALID_SOCKET);
69   on = 1;
70   r = ioctlsocket(sock, FIONBIO, &on);
71   ASSERT_OK(r);
72 
73   r = uv_ip4_addr("127.0.0.1", TEST_PORT, &addr);
74   ASSERT_OK(r);
75 
76   r = connect(sock, (const struct sockaddr*) &addr, sizeof addr);
77   ASSERT(r);
78   ASSERT_EQ(WSAGetLastError(), WSAEWOULDBLOCK);
79 
80   r = uv_poll_init_socket(uv_default_loop(), &handle, sock);
81   ASSERT_OK(r);
82   r = uv_poll_start(&handle, UV_WRITABLE, poll_cb);
83   ASSERT_OK(r);
84 
85   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
86 
87   ASSERT_EQ(1, close_cb_called);
88 
89   MAKE_VALGRIND_HAPPY(uv_default_loop());
90   return 0;
91 #endif
92 }
93