xref: /netbsd-src/external/mit/libuv/dist/test/test-tcp-write-fail.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
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 #include <stdio.h>
250e552da7Schristos #include <stdlib.h>
260e552da7Schristos #ifndef _WIN32
270e552da7Schristos # include <unistd.h>
280e552da7Schristos #endif
290e552da7Schristos 
300e552da7Schristos 
310e552da7Schristos static int connect_cb_called = 0;
320e552da7Schristos static int write_cb_called = 0;
330e552da7Schristos static int close_cb_called = 0;
340e552da7Schristos 
350e552da7Schristos static uv_connect_t connect_req;
360e552da7Schristos static uv_write_t write_req;
370e552da7Schristos 
380e552da7Schristos 
close_socket(uv_tcp_t * sock)390e552da7Schristos static void close_socket(uv_tcp_t* sock) {
400e552da7Schristos   uv_os_fd_t fd;
410e552da7Schristos   int r;
420e552da7Schristos 
430e552da7Schristos   r = uv_fileno((uv_handle_t*)sock, &fd);
440e552da7Schristos   ASSERT(r == 0);
450e552da7Schristos #ifdef _WIN32
460e552da7Schristos   r = closesocket((uv_os_sock_t)fd);
470e552da7Schristos #else
480e552da7Schristos   r = close(fd);
490e552da7Schristos #endif
500e552da7Schristos   ASSERT(r == 0);
510e552da7Schristos }
520e552da7Schristos 
530e552da7Schristos 
close_cb(uv_handle_t * handle)540e552da7Schristos static void close_cb(uv_handle_t* handle) {
55*5f2f4271Schristos   ASSERT_NOT_NULL(handle);
560e552da7Schristos   close_cb_called++;
570e552da7Schristos }
580e552da7Schristos 
590e552da7Schristos 
write_cb(uv_write_t * req,int status)600e552da7Schristos static void write_cb(uv_write_t* req, int status) {
61*5f2f4271Schristos   ASSERT_NOT_NULL(req);
620e552da7Schristos 
630e552da7Schristos   ASSERT(status != 0);
640e552da7Schristos   fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
650e552da7Schristos   write_cb_called++;
660e552da7Schristos 
670e552da7Schristos   uv_close((uv_handle_t*)(req->handle), close_cb);
680e552da7Schristos }
690e552da7Schristos 
700e552da7Schristos 
connect_cb(uv_connect_t * req,int status)710e552da7Schristos static void connect_cb(uv_connect_t* req, int status) {
720e552da7Schristos   uv_buf_t buf;
730e552da7Schristos   uv_stream_t* stream;
740e552da7Schristos   int r;
750e552da7Schristos 
760e552da7Schristos   ASSERT(req == &connect_req);
770e552da7Schristos   ASSERT(status == 0);
780e552da7Schristos 
790e552da7Schristos   stream = req->handle;
800e552da7Schristos   connect_cb_called++;
810e552da7Schristos 
820e552da7Schristos   /* close the socket, the hard way */
830e552da7Schristos   close_socket((uv_tcp_t*)stream);
840e552da7Schristos 
850e552da7Schristos   buf = uv_buf_init("hello\n", 6);
860e552da7Schristos   r = uv_write(&write_req, stream, &buf, 1, write_cb);
870e552da7Schristos   ASSERT(r == 0);
880e552da7Schristos }
890e552da7Schristos 
900e552da7Schristos 
TEST_IMPL(tcp_write_fail)910e552da7Schristos TEST_IMPL(tcp_write_fail) {
920e552da7Schristos   struct sockaddr_in addr;
930e552da7Schristos   uv_tcp_t client;
940e552da7Schristos   int r;
950e552da7Schristos 
960e552da7Schristos   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
970e552da7Schristos 
980e552da7Schristos   r = uv_tcp_init(uv_default_loop(), &client);
990e552da7Schristos   ASSERT(r == 0);
1000e552da7Schristos 
1010e552da7Schristos   r = uv_tcp_connect(&connect_req,
1020e552da7Schristos                      &client,
1030e552da7Schristos                      (const struct sockaddr*) &addr,
1040e552da7Schristos                      connect_cb);
1050e552da7Schristos   ASSERT(r == 0);
1060e552da7Schristos 
1070e552da7Schristos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1080e552da7Schristos 
1090e552da7Schristos   ASSERT(connect_cb_called == 1);
1100e552da7Schristos   ASSERT(write_cb_called == 1);
1110e552da7Schristos   ASSERT(close_cb_called == 1);
1120e552da7Schristos 
1130e552da7Schristos   MAKE_VALGRIND_HAPPY();
1140e552da7Schristos   return 0;
1150e552da7Schristos }
116