xref: /netbsd-src/external/mit/libuv/dist/test/test-udp-try-send.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 
250e552da7Schristos #include <stdio.h>
260e552da7Schristos #include <stdlib.h>
270e552da7Schristos #include <string.h>
280e552da7Schristos 
290e552da7Schristos #define CHECK_HANDLE(handle) \
300e552da7Schristos   ASSERT((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client)
310e552da7Schristos 
320e552da7Schristos static uv_udp_t server;
330e552da7Schristos static uv_udp_t client;
340e552da7Schristos 
350e552da7Schristos static int sv_recv_cb_called;
360e552da7Schristos 
370e552da7Schristos static int close_cb_called;
380e552da7Schristos 
390e552da7Schristos 
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)400e552da7Schristos static void alloc_cb(uv_handle_t* handle,
410e552da7Schristos                      size_t suggested_size,
420e552da7Schristos                      uv_buf_t* buf) {
430e552da7Schristos   static char slab[65536];
440e552da7Schristos   CHECK_HANDLE(handle);
450e552da7Schristos   ASSERT(suggested_size <= sizeof(slab));
460e552da7Schristos   buf->base = slab;
470e552da7Schristos   buf->len = sizeof(slab);
480e552da7Schristos }
490e552da7Schristos 
500e552da7Schristos 
close_cb(uv_handle_t * handle)510e552da7Schristos static void close_cb(uv_handle_t* handle) {
520e552da7Schristos   CHECK_HANDLE(handle);
530e552da7Schristos   ASSERT(uv_is_closing(handle));
540e552da7Schristos   close_cb_called++;
550e552da7Schristos }
560e552da7Schristos 
570e552da7Schristos 
sv_recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * rcvbuf,const struct sockaddr * addr,unsigned flags)580e552da7Schristos static void sv_recv_cb(uv_udp_t* handle,
590e552da7Schristos                        ssize_t nread,
600e552da7Schristos                        const uv_buf_t* rcvbuf,
610e552da7Schristos                        const struct sockaddr* addr,
620e552da7Schristos                        unsigned flags) {
630e552da7Schristos   ASSERT(nread > 0);
640e552da7Schristos 
650e552da7Schristos   if (nread == 0) {
66*5f2f4271Schristos     ASSERT_NULL(addr);
670e552da7Schristos     return;
680e552da7Schristos   }
690e552da7Schristos 
700e552da7Schristos   ASSERT(nread == 4);
71*5f2f4271Schristos   ASSERT_NOT_NULL(addr);
720e552da7Schristos 
730e552da7Schristos   ASSERT(memcmp("EXIT", rcvbuf->base, nread) == 0);
740e552da7Schristos   uv_close((uv_handle_t*) handle, close_cb);
750e552da7Schristos   uv_close((uv_handle_t*) &client, close_cb);
760e552da7Schristos 
770e552da7Schristos   sv_recv_cb_called++;
780e552da7Schristos }
790e552da7Schristos 
800e552da7Schristos 
TEST_IMPL(udp_try_send)810e552da7Schristos TEST_IMPL(udp_try_send) {
820e552da7Schristos   struct sockaddr_in addr;
830e552da7Schristos   static char buffer[64 * 1024];
840e552da7Schristos   uv_buf_t buf;
850e552da7Schristos   int r;
860e552da7Schristos 
870e552da7Schristos   ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
880e552da7Schristos 
890e552da7Schristos   r = uv_udp_init(uv_default_loop(), &server);
900e552da7Schristos   ASSERT(r == 0);
910e552da7Schristos 
920e552da7Schristos   r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
930e552da7Schristos   ASSERT(r == 0);
940e552da7Schristos 
950e552da7Schristos   r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
960e552da7Schristos   ASSERT(r == 0);
970e552da7Schristos 
980e552da7Schristos   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
990e552da7Schristos 
1000e552da7Schristos   r = uv_udp_init(uv_default_loop(), &client);
1010e552da7Schristos   ASSERT(r == 0);
1020e552da7Schristos 
1030e552da7Schristos   buf = uv_buf_init(buffer, sizeof(buffer));
1040e552da7Schristos   r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &addr);
1050e552da7Schristos   ASSERT(r == UV_EMSGSIZE);
1060e552da7Schristos 
1070e552da7Schristos   buf = uv_buf_init("EXIT", 4);
1080e552da7Schristos   r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &addr);
1090e552da7Schristos   ASSERT(r == 4);
1100e552da7Schristos 
1110e552da7Schristos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1120e552da7Schristos 
1130e552da7Schristos   ASSERT(close_cb_called == 2);
1140e552da7Schristos   ASSERT(sv_recv_cb_called == 1);
1150e552da7Schristos 
1160e552da7Schristos   ASSERT(client.send_queue_size == 0);
1170e552da7Schristos   ASSERT(server.send_queue_size == 0);
1180e552da7Schristos 
1190e552da7Schristos   MAKE_VALGRIND_HAPPY();
1200e552da7Schristos   return 0;
1210e552da7Schristos }
122