1
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10
* The above copyright notice and this permission notice shall be included in
11
* all copies or substantial portions of the Software.
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
28
/* See test-ipc.ctx */
29
void spawn_helper(uv_pipe_t* channel,
30
uv_process_t* process,
44
uv_handle_type expected_type;
49
static struct echo_ctx ctx;
50
static int num_recv_handles;
53
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
54
/* we're not actually reading anything so a small buffer is okay */
56
return uv_buf_init(buf, sizeof(buf));
60
static void recv_cb(uv_pipe_t* handle,
63
uv_handle_type pending) {
66
ASSERT(pending == ctx.expected_type);
67
ASSERT(handle == &ctx.channel);
70
if (pending == UV_NAMED_PIPE)
71
r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
72
else if (pending == UV_TCP)
73
r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
78
r = uv_accept((uv_stream_t*)&ctx.channel, &ctx.recv.stream);
81
uv_close((uv_handle_t*)&ctx.channel, NULL);
82
uv_close(&ctx.send.handle, NULL);
83
uv_close(&ctx.recv.handle, NULL);
88
static int run_test(void) {
93
spawn_helper(&ctx.channel, &process, "ipc_send_recv_helper");
95
buf = uv_buf_init(".", 1);
96
r = uv_write2(&ctx.write_req,
97
(uv_stream_t*)&ctx.channel,
103
r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, recv_cb);
106
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
109
ASSERT(num_recv_handles == 1);
115
TEST_IMPL(ipc_send_recv_pipe) {
118
ctx.expected_type = UV_NAMED_PIPE;
120
r = uv_pipe_init(uv_default_loop(), &ctx.send.pipe, 1);
123
r = uv_pipe_bind(&ctx.send.pipe, TEST_PIPENAME);
129
MAKE_VALGRIND_HAPPY();
134
TEST_IMPL(ipc_send_recv_tcp) {
137
ctx.expected_type = UV_TCP;
139
r = uv_tcp_init(uv_default_loop(), &ctx.send.tcp);
142
r = uv_tcp_bind(&ctx.send.tcp, uv_ip4_addr("127.0.0.1", TEST_PORT));
148
MAKE_VALGRIND_HAPPY();
153
/* Everything here runs in a child process. */
155
static void write2_cb(uv_write_t* req, int status) {
157
uv_close(&ctx.recv.handle, NULL);
158
uv_close((uv_handle_t*)&ctx.channel, NULL);
162
static void read2_cb(uv_pipe_t* handle,
165
uv_handle_type pending) {
168
ASSERT(pending == UV_NAMED_PIPE || pending == UV_TCP);
169
ASSERT(handle == &ctx.channel);
172
buf = uv_buf_init(".", 1);
174
if (pending == UV_NAMED_PIPE)
175
r = uv_pipe_init(ctx.channel.loop, &ctx.recv.pipe, 0);
176
else if (pending == UV_TCP)
177
r = uv_tcp_init(ctx.channel.loop, &ctx.recv.tcp);
182
r = uv_accept((uv_stream_t*)handle, &ctx.recv.stream);
185
r = uv_write2(&ctx.write_req,
186
(uv_stream_t*)&ctx.channel,
194
/* stdin is a duplex channel over which a handle is sent.
195
* We receive it and send it back where it came from.
197
int ipc_send_recv_helper(void) {
200
memset(&ctx, 0, sizeof(ctx));
202
r = uv_pipe_init(uv_default_loop(), &ctx.channel, 1);
205
uv_pipe_open(&ctx.channel, 0);
206
ASSERT(uv_is_readable((uv_stream_t*)&ctx.channel));
207
ASSERT(uv_is_writable((uv_stream_t*)&ctx.channel));
208
ASSERT(!uv_is_closing((uv_handle_t*)&ctx.channel));
210
r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, read2_cb);
213
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
216
MAKE_VALGRIND_HAPPY();