libuv-svace-build
290 строк · 8.2 Кб
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#include <stdio.h>25#include <stdlib.h>26#include <string.h>27
28#ifndef _WIN3229# include <unistd.h> /* close */30#else31# include <fcntl.h>32#endif33
34static uv_pipe_t pipe_client;35static uv_pipe_t pipe_server;36static uv_connect_t connect_req;37
38static int pipe_close_cb_called = 0;39static int pipe_client_connect_cb_called = 0;40
41
42static void pipe_close_cb(uv_handle_t* handle) {43ASSERT_NE(handle == (uv_handle_t*) &pipe_client ||44handle == (uv_handle_t*) &pipe_server, 0);45pipe_close_cb_called++;46}
47
48
49static void pipe_client_connect_cb(uv_connect_t* req, int status) {50char buf[1024];51size_t len;52int r;53
54ASSERT_PTR_EQ(req, &connect_req);55ASSERT_OK(status);56
57len = sizeof buf;58r = uv_pipe_getpeername(&pipe_client, buf, &len);59ASSERT_OK(r);60
61if (*buf == '\0') { /* Linux abstract socket. */62const char expected[] = "\0" TEST_PIPENAME;63ASSERT_EQ(len, sizeof(expected) - 1);64ASSERT_MEM_EQ(buf, expected, len);65} else {66ASSERT_NE(0, buf[len - 1]);67ASSERT_MEM_EQ(buf, TEST_PIPENAME, len);68}69
70len = sizeof buf;71r = uv_pipe_getsockname(&pipe_client, buf, &len);72ASSERT(r == 0 && len == 0);73
74pipe_client_connect_cb_called++;75
76uv_close((uv_handle_t*) &pipe_client, pipe_close_cb);77uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);78}
79
80
81static void pipe_server_connection_cb(uv_stream_t* handle, int status) {82/* This function *may* be called, depending on whether accept or the83* connection callback is called first.
84*/
85ASSERT_OK(status);86}
87
88
89TEST_IMPL(pipe_getsockname) {90#if defined(NO_SELF_CONNECT)91RETURN_SKIP(NO_SELF_CONNECT);92#endif93uv_loop_t* loop;94char namebuf[256];95char buf[1024];96size_t namelen;97size_t len;98int r;99
100snprintf(namebuf, sizeof(namebuf), "%s-oob", TEST_PIPENAME);101namelen = sizeof(TEST_PIPENAME) - 1;102
103loop = uv_default_loop();104ASSERT_NOT_NULL(loop);105
106r = uv_pipe_init(loop, &pipe_server, 0);107ASSERT_OK(r);108
109r = uv_pipe_bind2(&pipe_server, "bad\0path", 8, 0);110ASSERT_EQ(r, UV_EINVAL);111
112len = sizeof buf;113r = uv_pipe_getsockname(&pipe_server, buf, &len);114ASSERT_EQ(r, UV_EBADF);115
116len = sizeof buf;117r = uv_pipe_getpeername(&pipe_server, buf, &len);118ASSERT_EQ(r, UV_EBADF);119
120r = uv_pipe_bind2(&pipe_server, namebuf, namelen, 0);121ASSERT_OK(r);122
123#ifndef _WIN32124ASSERT_STR_EQ(pipe_server.pipe_fname, TEST_PIPENAME);125#endif126
127len = sizeof buf;128r = uv_pipe_getsockname(&pipe_server, buf, &len);129ASSERT_OK(r);130
131ASSERT_NE(0, buf[len - 1]);132ASSERT_EQ(buf[len], '\0');133ASSERT_OK(memcmp(buf, TEST_PIPENAME, len));134
135len = sizeof buf;136r = uv_pipe_getpeername(&pipe_server, buf, &len);137ASSERT_EQ(r, UV_ENOTCONN);138
139r = uv_listen((uv_stream_t*) &pipe_server, 0, pipe_server_connection_cb);140ASSERT_OK(r);141
142r = uv_pipe_init(loop, &pipe_client, 0);143ASSERT_OK(r);144
145len = sizeof buf;146r = uv_pipe_getsockname(&pipe_client, buf, &len);147ASSERT_EQ(r, UV_EBADF);148
149len = sizeof buf;150r = uv_pipe_getpeername(&pipe_client, buf, &len);151ASSERT_EQ(r, UV_EBADF);152
153r = uv_pipe_connect2(&connect_req,154&pipe_client,155namebuf,156namelen,1570,158pipe_client_connect_cb);159ASSERT_OK(r);160
161len = sizeof buf;162r = uv_pipe_getsockname(&pipe_client, buf, &len);163ASSERT(r == 0 && len == 0);164
165len = sizeof buf;166r = uv_pipe_getpeername(&pipe_client, buf, &len);167ASSERT_OK(r);168
169ASSERT_NE(0, buf[len - 1]);170ASSERT_OK(memcmp(buf, TEST_PIPENAME, len));171
172r = uv_run(loop, UV_RUN_DEFAULT);173ASSERT_OK(r);174ASSERT_EQ(1, pipe_client_connect_cb_called);175ASSERT_EQ(2, pipe_close_cb_called);176
177MAKE_VALGRIND_HAPPY(loop);178return 0;179}
180
181
182TEST_IMPL(pipe_getsockname_abstract) {183/* TODO(bnoordhuis) Use unique name, susceptible to concurrent test runs. */184static const char name[] = "\0" TEST_PIPENAME;185#if defined(__linux__)186char buf[256];187size_t buflen;188
189buflen = sizeof(buf);190memset(buf, 0, sizeof(buf));191ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));192ASSERT_OK(uv_pipe_bind2(&pipe_server, name, sizeof(name) - 1, 0));193ASSERT_OK(uv_pipe_getsockname(&pipe_server, buf, &buflen));194ASSERT_UINT64_EQ(sizeof(name) - 1, buflen);195ASSERT_MEM_EQ(name, buf, buflen);196ASSERT_OK(uv_listen((uv_stream_t*) &pipe_server,1970,198pipe_server_connection_cb));199ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));200ASSERT_OK(uv_pipe_connect2(&connect_req,201&pipe_client,202name,203sizeof(name) - 1,2040,205pipe_client_connect_cb));206ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));207ASSERT_EQ(1, pipe_client_connect_cb_called);208ASSERT_EQ(2, pipe_close_cb_called);209MAKE_VALGRIND_HAPPY(uv_default_loop());210return 0;211#else212/* On other platforms it should simply fail with UV_EINVAL. */213ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_server, 0));214ASSERT_EQ(UV_EINVAL, uv_pipe_bind2(&pipe_server, name, sizeof(name), 0));215ASSERT_OK(uv_pipe_init(uv_default_loop(), &pipe_client, 0));216uv_close((uv_handle_t*) &pipe_server, pipe_close_cb);217ASSERT_EQ(UV_EINVAL, uv_pipe_connect2(&connect_req,218&pipe_client,219name,220sizeof(name),2210,222(uv_connect_cb) abort));223uv_close((uv_handle_t*) &pipe_client, pipe_close_cb);224ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));225ASSERT_EQ(2, pipe_close_cb_called);226MAKE_VALGRIND_HAPPY(uv_default_loop());227return 0;228#endif229}
230
231TEST_IMPL(pipe_getsockname_blocking) {232#ifdef _WIN32233HANDLE readh, writeh;234int readfd;235char buf1[1024], buf2[1024];236size_t len1, len2;237int r;238
239r = CreatePipe(&readh, &writeh, NULL, 65536);240ASSERT(r);241
242r = uv_pipe_init(uv_default_loop(), &pipe_client, 0);243ASSERT_OK(r);244readfd = _open_osfhandle((intptr_t)readh, _O_RDONLY);245ASSERT_NE(r, -1);246r = uv_pipe_open(&pipe_client, readfd);247ASSERT_OK(r);248r = uv_read_start((uv_stream_t*) &pipe_client,249(uv_alloc_cb) abort,250(uv_read_cb) abort);251ASSERT_OK(r);252Sleep(100);253r = uv_read_stop((uv_stream_t*)&pipe_client);254ASSERT_OK(r);255
256len1 = sizeof buf1;257r = uv_pipe_getsockname(&pipe_client, buf1, &len1);258ASSERT_OK(r);259ASSERT_OK(len1); /* It's an annonymous pipe. */260
261r = uv_read_start((uv_stream_t*)&pipe_client,262(uv_alloc_cb) abort,263(uv_read_cb) abort);264ASSERT_OK(r);265Sleep(100);266
267len2 = sizeof buf2;268r = uv_pipe_getsockname(&pipe_client, buf2, &len2);269ASSERT_OK(r);270ASSERT_OK(len2); /* It's an annonymous pipe. */271
272r = uv_read_stop((uv_stream_t*)&pipe_client);273ASSERT_OK(r);274
275ASSERT_EQ(len1, len2);276ASSERT_OK(memcmp(buf1, buf2, len1));277
278pipe_close_cb_called = 0;279uv_close((uv_handle_t*)&pipe_client, pipe_close_cb);280
281uv_run(uv_default_loop(), UV_RUN_DEFAULT);282
283ASSERT_EQ(1, pipe_close_cb_called);284
285CloseHandle(writeh);286#endif287
288MAKE_VALGRIND_HAPPY(uv_default_loop());289return 0;290}
291