10e552da7Schristos /* Copyright libuv project and 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 <stdio.h>
230e552da7Schristos #include <stdlib.h>
240e552da7Schristos #include <string.h>
250e552da7Schristos
260e552da7Schristos #include "uv.h"
270e552da7Schristos #include "task.h"
280e552da7Schristos
290e552da7Schristos static uv_tcp_t server;
300e552da7Schristos static uv_tcp_t client;
310e552da7Schristos static uv_tcp_t incoming;
320e552da7Schristos static int connect_cb_called;
330e552da7Schristos static int close_cb_called;
340e552da7Schristos static int connection_cb_called;
350e552da7Schristos static uv_write_t write_req;
360e552da7Schristos
370e552da7Schristos static char hello[] = "HELLO!";
380e552da7Schristos
390e552da7Schristos
close_cb(uv_handle_t * handle)400e552da7Schristos static void close_cb(uv_handle_t* handle) {
410e552da7Schristos close_cb_called++;
420e552da7Schristos }
430e552da7Schristos
write_cb(uv_write_t * req,int status)440e552da7Schristos static void write_cb(uv_write_t* req, int status) {
450e552da7Schristos ASSERT(status == 0);
460e552da7Schristos }
470e552da7Schristos
conn_alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)480e552da7Schristos static void conn_alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
490e552da7Schristos /* Do nothing, read_cb should be called with UV_ENOBUFS. */
500e552da7Schristos }
510e552da7Schristos
conn_read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)520e552da7Schristos static void conn_read_cb(uv_stream_t* stream,
530e552da7Schristos ssize_t nread,
540e552da7Schristos const uv_buf_t* buf) {
550e552da7Schristos ASSERT(nread == UV_ENOBUFS);
56*5f2f4271Schristos ASSERT_NULL(buf->base);
570e552da7Schristos ASSERT(buf->len == 0);
580e552da7Schristos
590e552da7Schristos uv_close((uv_handle_t*) &incoming, close_cb);
600e552da7Schristos uv_close((uv_handle_t*) &client, close_cb);
610e552da7Schristos uv_close((uv_handle_t*) &server, close_cb);
620e552da7Schristos }
630e552da7Schristos
connect_cb(uv_connect_t * req,int status)640e552da7Schristos static void connect_cb(uv_connect_t* req, int status) {
650e552da7Schristos int r;
660e552da7Schristos uv_buf_t buf;
670e552da7Schristos
680e552da7Schristos ASSERT(status == 0);
690e552da7Schristos connect_cb_called++;
700e552da7Schristos
710e552da7Schristos buf = uv_buf_init(hello, sizeof(hello));
720e552da7Schristos r = uv_write(&write_req, req->handle, &buf, 1, write_cb);
730e552da7Schristos ASSERT(r == 0);
740e552da7Schristos }
750e552da7Schristos
760e552da7Schristos
connection_cb(uv_stream_t * tcp,int status)770e552da7Schristos static void connection_cb(uv_stream_t* tcp, int status) {
780e552da7Schristos ASSERT(status == 0);
790e552da7Schristos
800e552da7Schristos ASSERT(0 == uv_tcp_init(tcp->loop, &incoming));
810e552da7Schristos ASSERT(0 == uv_accept(tcp, (uv_stream_t*) &incoming));
820e552da7Schristos ASSERT(0 == uv_read_start((uv_stream_t*) &incoming,
830e552da7Schristos conn_alloc_cb,
840e552da7Schristos conn_read_cb));
850e552da7Schristos
860e552da7Schristos connection_cb_called++;
870e552da7Schristos }
880e552da7Schristos
890e552da7Schristos
start_server(void)900e552da7Schristos static void start_server(void) {
910e552da7Schristos struct sockaddr_in addr;
920e552da7Schristos
930e552da7Schristos ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
940e552da7Schristos
950e552da7Schristos ASSERT(0 == uv_tcp_init(uv_default_loop(), &server));
960e552da7Schristos ASSERT(0 == uv_tcp_bind(&server, (struct sockaddr*) &addr, 0));
970e552da7Schristos ASSERT(0 == uv_listen((uv_stream_t*) &server, 128, connection_cb));
980e552da7Schristos }
990e552da7Schristos
1000e552da7Schristos
TEST_IMPL(tcp_alloc_cb_fail)1010e552da7Schristos TEST_IMPL(tcp_alloc_cb_fail) {
1020e552da7Schristos uv_connect_t connect_req;
1030e552da7Schristos struct sockaddr_in addr;
1040e552da7Schristos
1050e552da7Schristos start_server();
1060e552da7Schristos
1070e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
1080e552da7Schristos
1090e552da7Schristos ASSERT(0 == uv_tcp_init(uv_default_loop(), &client));
1100e552da7Schristos ASSERT(0 == uv_tcp_connect(&connect_req,
1110e552da7Schristos &client,
1120e552da7Schristos (struct sockaddr*) &addr,
1130e552da7Schristos connect_cb));
1140e552da7Schristos
1150e552da7Schristos ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
1160e552da7Schristos
1170e552da7Schristos ASSERT(connect_cb_called == 1);
1180e552da7Schristos ASSERT(connection_cb_called == 1);
1190e552da7Schristos ASSERT(close_cb_called == 3);
1200e552da7Schristos
1210e552da7Schristos MAKE_VALGRIND_HAPPY();
1220e552da7Schristos return 0;
1230e552da7Schristos }
124