libuv-svace-build
214 строк · 6.1 Кб
1/* Copyright libuv project 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
27#include <errno.h>
28#include <sys/socket.h>
29#include <sys/ioctl.h>
30#include <unistd.h>
31#include <string.h>
32
33static uv_tcp_t server_handle;
34static uv_tcp_t client_handle;
35static uv_tcp_t peer_handle;
36static uv_poll_t poll_req[2];
37static uv_idle_t idle;
38static uv_os_fd_t client_fd;
39static uv_os_fd_t server_fd;
40static int ticks;
41static const int kMaxTicks = 10;
42static int cli_pr_check = 0;
43static int cli_rd_check = 0;
44static int srv_rd_check = 0;
45
46static int got_eagain(void) {
47return errno == EAGAIN
48|| errno == EINPROGRESS
49#ifdef EWOULDBLOCK
50|| errno == EWOULDBLOCK
51#endif
52;
53}
54
55static void idle_cb(uv_idle_t* idle) {
56uv_sleep(100);
57if (++ticks < kMaxTicks)
58return;
59
60uv_poll_stop(&poll_req[0]);
61uv_poll_stop(&poll_req[1]);
62uv_close((uv_handle_t*) &server_handle, NULL);
63uv_close((uv_handle_t*) &client_handle, NULL);
64uv_close((uv_handle_t*) &peer_handle, NULL);
65uv_close((uv_handle_t*) idle, NULL);
66}
67
68static void poll_cb(uv_poll_t* handle, int status, int events) {
69char buffer[5];
70int n;
71int fd;
72
73ASSERT_OK(uv_fileno((uv_handle_t*)handle, &fd));
74memset(buffer, 0, 5);
75
76if (events & UV_PRIORITIZED) {
77do
78n = recv(client_fd, &buffer, 5, MSG_OOB);
79while (n == -1 && errno == EINTR);
80ASSERT(n >= 0 || errno != EINVAL);
81cli_pr_check = 1;
82ASSERT_OK(uv_poll_stop(&poll_req[0]));
83ASSERT_OK(uv_poll_start(&poll_req[0],
84UV_READABLE | UV_WRITABLE,
85poll_cb));
86}
87if (events & UV_READABLE) {
88if (fd == client_fd) {
89do
90n = recv(client_fd, &buffer, 5, 0);
91while (n == -1 && errno == EINTR);
92ASSERT(n >= 0 || errno != EINVAL);
93if (cli_rd_check == 1) {
94ASSERT_OK(strncmp(buffer, "world", n));
95ASSERT_EQ(5, n);
96cli_rd_check = 2;
97}
98if (cli_rd_check == 0) {
99ASSERT_EQ(4, n);
100ASSERT_OK(strncmp(buffer, "hello", n));
101cli_rd_check = 1;
102do {
103do
104n = recv(server_fd, &buffer, 5, 0);
105while (n == -1 && errno == EINTR);
106if (n > 0) {
107ASSERT_EQ(5, n);
108ASSERT_OK(strncmp(buffer, "world", n));
109cli_rd_check = 2;
110}
111} while (n > 0);
112
113ASSERT(got_eagain());
114}
115}
116if (fd == server_fd) {
117do
118n = recv(server_fd, &buffer, 3, 0);
119while (n == -1 && errno == EINTR);
120ASSERT(n >= 0 || errno != EINVAL);
121ASSERT_EQ(3, n);
122ASSERT_OK(strncmp(buffer, "foo", n));
123srv_rd_check = 1;
124uv_poll_stop(&poll_req[1]);
125}
126}
127if (events & UV_WRITABLE) {
128do {
129n = send(client_fd, "foo", 3, 0);
130} while (n < 0 && errno == EINTR);
131ASSERT_EQ(3, n);
132}
133}
134
135static void connection_cb(uv_stream_t* handle, int status) {
136int r;
137
138ASSERT_OK(status);
139ASSERT_OK(uv_accept(handle, (uv_stream_t*) &peer_handle));
140ASSERT_OK(uv_fileno((uv_handle_t*) &peer_handle, &server_fd));
141ASSERT_OK(uv_poll_init_socket(uv_default_loop(),
142&poll_req[0],
143client_fd));
144ASSERT_OK(uv_poll_init_socket(uv_default_loop(),
145&poll_req[1],
146server_fd));
147ASSERT_OK(uv_poll_start(&poll_req[0],
148UV_PRIORITIZED | UV_READABLE | UV_WRITABLE,
149poll_cb));
150ASSERT_OK(uv_poll_start(&poll_req[1],
151UV_READABLE,
152poll_cb));
153do {
154r = send(server_fd, "hello", 5, MSG_OOB);
155} while (r < 0 && errno == EINTR);
156ASSERT_EQ(5, r);
157
158do {
159r = send(server_fd, "world", 5, 0);
160} while (r < 0 && errno == EINTR);
161ASSERT_EQ(5, r);
162
163ASSERT_OK(uv_idle_start(&idle, idle_cb));
164}
165
166
167TEST_IMPL(poll_oob) {
168struct sockaddr_in addr;
169int r = 0;
170uv_loop_t* loop;
171
172ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
173loop = uv_default_loop();
174
175ASSERT_OK(uv_tcp_init(loop, &server_handle));
176ASSERT_OK(uv_tcp_init(loop, &client_handle));
177ASSERT_OK(uv_tcp_init(loop, &peer_handle));
178ASSERT_OK(uv_idle_init(loop, &idle));
179ASSERT_OK(uv_tcp_bind(&server_handle, (const struct sockaddr*) &addr, 0));
180ASSERT_OK(uv_listen((uv_stream_t*) &server_handle, 1, connection_cb));
181
182/* Ensure two separate packets */
183ASSERT_OK(uv_tcp_nodelay(&client_handle, 1));
184
185client_fd = socket(PF_INET, SOCK_STREAM, 0);
186ASSERT_GE(client_fd, 0);
187do {
188errno = 0;
189r = connect(client_fd, (const struct sockaddr*)&addr, sizeof(addr));
190} while (r == -1 && errno == EINTR);
191ASSERT_OK(r);
192
193ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
194
195ASSERT_EQ(ticks, kMaxTicks);
196
197/* Did client receive the POLLPRI message */
198ASSERT_EQ(1, cli_pr_check);
199/* Did client receive the POLLIN message */
200ASSERT_EQ(2, cli_rd_check);
201/* Could we write with POLLOUT and did the server receive our POLLOUT message
202* through POLLIN.
203*/
204ASSERT_EQ(1, srv_rd_check);
205
206MAKE_VALGRIND_HAPPY(loop);
207return 0;
208}
209
210#else
211
212typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
213
214#endif
215