~ubuntu-branches/ubuntu/saucy/nodejs/saucy-proposed

« back to all changes in this revision

Viewing changes to deps/uv/test/test-ipc-send-recv.c

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
 
 
28
/* See test-ipc.ctx */
 
29
void spawn_helper(uv_pipe_t* channel,
 
30
                  uv_process_t* process,
 
31
                  const char* helper);
 
32
 
 
33
union handles {
 
34
  uv_handle_t handle;
 
35
  uv_stream_t stream;
 
36
  uv_pipe_t pipe;
 
37
  uv_tcp_t tcp;
 
38
  uv_tty_t tty;
 
39
};
 
40
 
 
41
struct echo_ctx {
 
42
  uv_pipe_t channel;
 
43
  uv_write_t write_req;
 
44
  uv_handle_type expected_type;
 
45
  union handles send;
 
46
  union handles recv;
 
47
};
 
48
 
 
49
static struct echo_ctx ctx;
 
50
static int num_recv_handles;
 
51
 
 
52
 
 
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 */
 
55
  static char buf[8];
 
56
  return uv_buf_init(buf, sizeof(buf));
 
57
}
 
58
 
 
59
 
 
60
static void recv_cb(uv_pipe_t* handle,
 
61
                    ssize_t nread,
 
62
                    uv_buf_t buf,
 
63
                    uv_handle_type pending) {
 
64
  int r;
 
65
 
 
66
  ASSERT(pending == ctx.expected_type);
 
67
  ASSERT(handle == &ctx.channel);
 
68
  ASSERT(nread >= 0);
 
69
 
 
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);
 
74
  else
 
75
    abort();
 
76
  ASSERT(r == 0);
 
77
 
 
78
  r = uv_accept((uv_stream_t*)&ctx.channel, &ctx.recv.stream);
 
79
  ASSERT(r == 0);
 
80
 
 
81
  uv_close((uv_handle_t*)&ctx.channel, NULL);
 
82
  uv_close(&ctx.send.handle, NULL);
 
83
  uv_close(&ctx.recv.handle, NULL);
 
84
  num_recv_handles++;
 
85
}
 
86
 
 
87
 
 
88
static int run_test(void) {
 
89
  uv_process_t process;
 
90
  uv_buf_t buf;
 
91
  int r;
 
92
 
 
93
  spawn_helper(&ctx.channel, &process, "ipc_send_recv_helper");
 
94
 
 
95
  buf = uv_buf_init(".", 1);
 
96
  r = uv_write2(&ctx.write_req,
 
97
                (uv_stream_t*)&ctx.channel,
 
98
                &buf, 1,
 
99
                &ctx.send.stream,
 
100
                NULL);
 
101
  ASSERT(r == 0);
 
102
 
 
103
  r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, recv_cb);
 
104
  ASSERT(r == 0);
 
105
 
 
106
  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
 
107
  ASSERT(r == 0);
 
108
 
 
109
  ASSERT(num_recv_handles == 1);
 
110
 
 
111
  return 0;
 
112
}
 
113
 
 
114
 
 
115
TEST_IMPL(ipc_send_recv_pipe) {
 
116
  int r;
 
117
 
 
118
  ctx.expected_type = UV_NAMED_PIPE;
 
119
 
 
120
  r = uv_pipe_init(uv_default_loop(), &ctx.send.pipe, 1);
 
121
  ASSERT(r == 0);
 
122
 
 
123
  r = uv_pipe_bind(&ctx.send.pipe, TEST_PIPENAME);
 
124
  ASSERT(r == 0);
 
125
 
 
126
  r = run_test();
 
127
  ASSERT(r == 0);
 
128
 
 
129
  MAKE_VALGRIND_HAPPY();
 
130
  return 0;
 
131
}
 
132
 
 
133
 
 
134
TEST_IMPL(ipc_send_recv_tcp) {
 
135
  int r;
 
136
 
 
137
  ctx.expected_type = UV_TCP;
 
138
 
 
139
  r = uv_tcp_init(uv_default_loop(), &ctx.send.tcp);
 
140
  ASSERT(r == 0);
 
141
 
 
142
  r = uv_tcp_bind(&ctx.send.tcp, uv_ip4_addr("127.0.0.1", TEST_PORT));
 
143
  ASSERT(r == 0);
 
144
 
 
145
  r = run_test();
 
146
  ASSERT(r == 0);
 
147
 
 
148
  MAKE_VALGRIND_HAPPY();
 
149
  return 0;
 
150
}
 
151
 
 
152
 
 
153
/* Everything here runs in a child process. */
 
154
 
 
155
static void write2_cb(uv_write_t* req, int status) {
 
156
  ASSERT(status == 0);
 
157
  uv_close(&ctx.recv.handle, NULL);
 
158
  uv_close((uv_handle_t*)&ctx.channel, NULL);
 
159
}
 
160
 
 
161
 
 
162
static void read2_cb(uv_pipe_t* handle,
 
163
                     ssize_t nread,
 
164
                     uv_buf_t buf,
 
165
                     uv_handle_type pending) {
 
166
  int r;
 
167
 
 
168
  ASSERT(pending == UV_NAMED_PIPE || pending == UV_TCP);
 
169
  ASSERT(handle == &ctx.channel);
 
170
  ASSERT(nread >= 0);
 
171
 
 
172
  buf = uv_buf_init(".", 1);
 
173
 
 
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);
 
178
  else
 
179
    abort();
 
180
  ASSERT(r == 0);
 
181
 
 
182
  r = uv_accept((uv_stream_t*)handle, &ctx.recv.stream);
 
183
  ASSERT(r == 0);
 
184
 
 
185
  r = uv_write2(&ctx.write_req,
 
186
                (uv_stream_t*)&ctx.channel,
 
187
                &buf, 1,
 
188
                &ctx.recv.stream,
 
189
                write2_cb);
 
190
  ASSERT(r == 0);
 
191
}
 
192
 
 
193
 
 
194
/* stdin is a duplex channel over which a handle is sent.
 
195
 * We receive it and send it back where it came from.
 
196
 */
 
197
int ipc_send_recv_helper(void) {
 
198
  int r;
 
199
 
 
200
  memset(&ctx, 0, sizeof(ctx));
 
201
 
 
202
  r = uv_pipe_init(uv_default_loop(), &ctx.channel, 1);
 
203
  ASSERT(r == 0);
 
204
 
 
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));
 
209
 
 
210
  r = uv_read2_start((uv_stream_t*)&ctx.channel, alloc_cb, read2_cb);
 
211
  ASSERT(r == 0);
 
212
 
 
213
  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
 
214
  ASSERT(r == 0);
 
215
 
 
216
  MAKE_VALGRIND_HAPPY();
 
217
  return 0;
 
218
}