10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos
250e552da7Schristos #include <stdio.h>
260e552da7Schristos #include <stdlib.h>
270e552da7Schristos #include <string.h>
280e552da7Schristos
290e552da7Schristos static void connection_cb(uv_stream_t* server, int status);
300e552da7Schristos static void connect_cb(uv_connect_t* req, int status);
310e552da7Schristos static void write_cb(uv_write_t* req, int status);
320e552da7Schristos static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
330e552da7Schristos static void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
340e552da7Schristos
350e552da7Schristos static uv_tcp_t tcp_server;
360e552da7Schristos static uv_tcp_t tcp_client;
370e552da7Schristos static uv_tcp_t tcp_peer; /* client socket as accept()-ed by server */
380e552da7Schristos static uv_connect_t connect_req;
390e552da7Schristos static uv_write_t write_req;
400e552da7Schristos
410e552da7Schristos static int write_cb_called;
420e552da7Schristos static int read_cb_called;
430e552da7Schristos
connection_cb(uv_stream_t * server,int status)440e552da7Schristos static void connection_cb(uv_stream_t* server, int status) {
450e552da7Schristos int r;
460e552da7Schristos uv_buf_t buf;
470e552da7Schristos
480e552da7Schristos ASSERT(server == (uv_stream_t*)&tcp_server);
490e552da7Schristos ASSERT(status == 0);
500e552da7Schristos
510e552da7Schristos r = uv_tcp_init(server->loop, &tcp_peer);
520e552da7Schristos ASSERT(r == 0);
530e552da7Schristos
540e552da7Schristos r = uv_accept(server, (uv_stream_t*)&tcp_peer);
550e552da7Schristos ASSERT(r == 0);
560e552da7Schristos
570e552da7Schristos r = uv_read_start((uv_stream_t*)&tcp_peer, alloc_cb, read_cb);
580e552da7Schristos ASSERT(r == 0);
590e552da7Schristos
600e552da7Schristos buf.base = "hello\n";
610e552da7Schristos buf.len = 6;
620e552da7Schristos
630e552da7Schristos r = uv_write(&write_req, (uv_stream_t*)&tcp_peer, &buf, 1, write_cb);
640e552da7Schristos ASSERT(r == 0);
650e552da7Schristos }
660e552da7Schristos
670e552da7Schristos
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)680e552da7Schristos static void alloc_cb(uv_handle_t* handle,
690e552da7Schristos size_t suggested_size,
700e552da7Schristos uv_buf_t* buf) {
710e552da7Schristos static char slab[1024];
720e552da7Schristos buf->base = slab;
730e552da7Schristos buf->len = sizeof(slab);
740e552da7Schristos }
750e552da7Schristos
760e552da7Schristos
read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)770e552da7Schristos static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
780e552da7Schristos if (nread < 0) {
790e552da7Schristos fprintf(stderr, "read_cb error: %s\n", uv_err_name(nread));
800e552da7Schristos ASSERT(nread == UV_ECONNRESET || nread == UV_EOF);
810e552da7Schristos
820e552da7Schristos uv_close((uv_handle_t*)&tcp_server, NULL);
830e552da7Schristos uv_close((uv_handle_t*)&tcp_peer, NULL);
840e552da7Schristos }
850e552da7Schristos
860e552da7Schristos read_cb_called++;
870e552da7Schristos }
880e552da7Schristos
890e552da7Schristos
connect_cb(uv_connect_t * req,int status)900e552da7Schristos static void connect_cb(uv_connect_t* req, int status) {
910e552da7Schristos ASSERT(req == &connect_req);
920e552da7Schristos ASSERT(status == 0);
930e552da7Schristos
940e552da7Schristos /* Close the client. */
950e552da7Schristos uv_close((uv_handle_t*)&tcp_client, NULL);
960e552da7Schristos }
970e552da7Schristos
980e552da7Schristos
write_cb(uv_write_t * req,int status)990e552da7Schristos static void write_cb(uv_write_t* req, int status) {
1000e552da7Schristos ASSERT(status == 0);
1010e552da7Schristos write_cb_called++;
1020e552da7Schristos }
1030e552da7Schristos
1040e552da7Schristos
TEST_IMPL(tcp_write_to_half_open_connection)1050e552da7Schristos TEST_IMPL(tcp_write_to_half_open_connection) {
1060e552da7Schristos struct sockaddr_in addr;
1070e552da7Schristos uv_loop_t* loop;
1080e552da7Schristos int r;
1090e552da7Schristos
1100e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
1110e552da7Schristos
1120e552da7Schristos loop = uv_default_loop();
113*5f2f4271Schristos ASSERT_NOT_NULL(loop);
1140e552da7Schristos
1150e552da7Schristos r = uv_tcp_init(loop, &tcp_server);
1160e552da7Schristos ASSERT(r == 0);
1170e552da7Schristos
1180e552da7Schristos r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
1190e552da7Schristos ASSERT(r == 0);
1200e552da7Schristos
1210e552da7Schristos r = uv_listen((uv_stream_t*)&tcp_server, 1, connection_cb);
1220e552da7Schristos ASSERT(r == 0);
1230e552da7Schristos
1240e552da7Schristos r = uv_tcp_init(loop, &tcp_client);
1250e552da7Schristos ASSERT(r == 0);
1260e552da7Schristos
1270e552da7Schristos r = uv_tcp_connect(&connect_req,
1280e552da7Schristos &tcp_client,
1290e552da7Schristos (const struct sockaddr*) &addr,
1300e552da7Schristos connect_cb);
1310e552da7Schristos ASSERT(r == 0);
1320e552da7Schristos
1330e552da7Schristos r = uv_run(loop, UV_RUN_DEFAULT);
1340e552da7Schristos ASSERT(r == 0);
1350e552da7Schristos
1360e552da7Schristos ASSERT(write_cb_called > 0);
1370e552da7Schristos ASSERT(read_cb_called > 0);
1380e552da7Schristos
1390e552da7Schristos MAKE_VALGRIND_HAPPY();
1400e552da7Schristos return 0;
1410e552da7Schristos }
142