Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /* Copyright libuv project and other Node contributors. All rights reserved.
      2      1.1  christos  *
      3      1.1  christos  * Permission is hereby granted, free of charge, to any person obtaining a copy
      4      1.1  christos  * of this software and associated documentation files (the "Software"), to
      5      1.1  christos  * deal in the Software without restriction, including without limitation the
      6      1.1  christos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      7      1.1  christos  * sell copies of the Software, and to permit persons to whom the Software is
      8      1.1  christos  * furnished to do so, subject to the following conditions:
      9      1.1  christos  *
     10      1.1  christos  * The above copyright notice and this permission notice shall be included in
     11      1.1  christos  * all copies or substantial portions of the Software.
     12      1.1  christos  *
     13      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14      1.1  christos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15      1.1  christos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16      1.1  christos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17      1.1  christos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     18      1.1  christos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     19      1.1  christos  * IN THE SOFTWARE.
     20      1.1  christos  */
     21      1.1  christos 
     22      1.1  christos #include "uv.h"
     23      1.1  christos #include "task.h"
     24      1.1  christos 
     25      1.1  christos #include <stdio.h>
     26      1.1  christos #include <stdlib.h>
     27      1.1  christos #include <string.h>
     28      1.1  christos 
     29      1.1  christos #define CHECK_HANDLE(handle) \
     30  1.1.1.3  christos   ASSERT_NE((uv_udp_t*)(handle) == &server || (uv_udp_t*)(handle) == &client, 0)
     31      1.1  christos 
     32      1.1  christos static uv_udp_t server;
     33      1.1  christos static uv_udp_t client;
     34      1.1  christos 
     35      1.1  christos static int cl_send_cb_called;
     36      1.1  christos static int cl_recv_cb_called;
     37      1.1  christos 
     38      1.1  christos static int sv_send_cb_called;
     39      1.1  christos static int sv_recv_cb_called;
     40      1.1  christos 
     41      1.1  christos static int close_cb_called;
     42      1.1  christos 
     43      1.1  christos 
     44      1.1  christos static void sv_alloc_cb(uv_handle_t* handle,
     45      1.1  christos                         size_t suggested_size,
     46      1.1  christos                         uv_buf_t* buf) {
     47      1.1  christos   static char slab[65536];
     48      1.1  christos   CHECK_HANDLE(handle);
     49      1.1  christos   buf->base = slab;
     50      1.1  christos   buf->len = sizeof(slab);
     51      1.1  christos }
     52      1.1  christos 
     53      1.1  christos 
     54      1.1  christos static void cl_alloc_cb(uv_handle_t* handle,
     55      1.1  christos                         size_t suggested_size,
     56      1.1  christos                         uv_buf_t* buf) {
     57      1.1  christos   /* Do nothing, recv_cb should be called with UV_ENOBUFS. */
     58      1.1  christos }
     59      1.1  christos 
     60      1.1  christos 
     61      1.1  christos static void close_cb(uv_handle_t* handle) {
     62      1.1  christos   CHECK_HANDLE(handle);
     63  1.1.1.3  christos   ASSERT_EQ(1, uv_is_closing(handle));
     64      1.1  christos   close_cb_called++;
     65      1.1  christos }
     66      1.1  christos 
     67      1.1  christos 
     68      1.1  christos static void cl_recv_cb(uv_udp_t* handle,
     69      1.1  christos                        ssize_t nread,
     70      1.1  christos                        const uv_buf_t* buf,
     71      1.1  christos                        const struct sockaddr* addr,
     72      1.1  christos                        unsigned flags) {
     73      1.1  christos   CHECK_HANDLE(handle);
     74  1.1.1.3  christos   ASSERT_OK(flags);
     75  1.1.1.3  christos   ASSERT_EQ(nread, UV_ENOBUFS);
     76      1.1  christos 
     77      1.1  christos   cl_recv_cb_called++;
     78      1.1  christos 
     79      1.1  christos   uv_close((uv_handle_t*) handle, close_cb);
     80      1.1  christos }
     81      1.1  christos 
     82      1.1  christos 
     83      1.1  christos static void cl_send_cb(uv_udp_send_t* req, int status) {
     84      1.1  christos   int r;
     85      1.1  christos 
     86  1.1.1.2  christos   ASSERT_NOT_NULL(req);
     87  1.1.1.3  christos   ASSERT_OK(status);
     88      1.1  christos   CHECK_HANDLE(req->handle);
     89      1.1  christos 
     90      1.1  christos   r = uv_udp_recv_start(req->handle, cl_alloc_cb, cl_recv_cb);
     91  1.1.1.3  christos   ASSERT_OK(r);
     92      1.1  christos 
     93      1.1  christos   cl_send_cb_called++;
     94      1.1  christos }
     95      1.1  christos 
     96      1.1  christos 
     97      1.1  christos static void sv_send_cb(uv_udp_send_t* req, int status) {
     98  1.1.1.2  christos   ASSERT_NOT_NULL(req);
     99  1.1.1.3  christos   ASSERT_OK(status);
    100      1.1  christos   CHECK_HANDLE(req->handle);
    101      1.1  christos 
    102      1.1  christos   uv_close((uv_handle_t*) req->handle, close_cb);
    103      1.1  christos   free(req);
    104      1.1  christos 
    105      1.1  christos   sv_send_cb_called++;
    106      1.1  christos }
    107      1.1  christos 
    108      1.1  christos 
    109      1.1  christos static void sv_recv_cb(uv_udp_t* handle,
    110      1.1  christos                        ssize_t nread,
    111      1.1  christos                        const uv_buf_t* rcvbuf,
    112      1.1  christos                        const struct sockaddr* addr,
    113      1.1  christos                        unsigned flags) {
    114      1.1  christos   uv_udp_send_t* req;
    115      1.1  christos   uv_buf_t sndbuf;
    116      1.1  christos   int r;
    117      1.1  christos 
    118      1.1  christos   if (nread < 0) {
    119      1.1  christos     ASSERT(0 && "unexpected error");
    120      1.1  christos   }
    121      1.1  christos 
    122      1.1  christos   if (nread == 0) {
    123      1.1  christos     /* Returning unused buffer. Don't count towards sv_recv_cb_called */
    124  1.1.1.2  christos     ASSERT_NULL(addr);
    125      1.1  christos     return;
    126      1.1  christos   }
    127      1.1  christos 
    128      1.1  christos   CHECK_HANDLE(handle);
    129  1.1.1.3  christos   ASSERT_OK(flags);
    130      1.1  christos 
    131  1.1.1.2  christos   ASSERT_NOT_NULL(addr);
    132  1.1.1.3  christos   ASSERT_EQ(4, nread);
    133      1.1  christos   ASSERT(!memcmp("PING", rcvbuf->base, nread));
    134      1.1  christos 
    135      1.1  christos   r = uv_udp_recv_stop(handle);
    136  1.1.1.3  christos   ASSERT_OK(r);
    137      1.1  christos 
    138      1.1  christos   req = malloc(sizeof *req);
    139  1.1.1.2  christos   ASSERT_NOT_NULL(req);
    140      1.1  christos 
    141      1.1  christos   sndbuf = uv_buf_init("PONG", 4);
    142      1.1  christos   r = uv_udp_send(req, handle, &sndbuf, 1, addr, sv_send_cb);
    143  1.1.1.3  christos   ASSERT_OK(r);
    144      1.1  christos 
    145      1.1  christos   sv_recv_cb_called++;
    146      1.1  christos }
    147      1.1  christos 
    148      1.1  christos 
    149      1.1  christos TEST_IMPL(udp_alloc_cb_fail) {
    150      1.1  christos   struct sockaddr_in addr;
    151      1.1  christos   uv_udp_send_t req;
    152      1.1  christos   uv_buf_t buf;
    153      1.1  christos   int r;
    154      1.1  christos 
    155  1.1.1.3  christos   ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
    156      1.1  christos 
    157      1.1  christos   r = uv_udp_init(uv_default_loop(), &server);
    158  1.1.1.3  christos   ASSERT_OK(r);
    159      1.1  christos 
    160      1.1  christos   r = uv_udp_bind(&server, (const struct sockaddr*) &addr, 0);
    161  1.1.1.3  christos   ASSERT_OK(r);
    162      1.1  christos 
    163      1.1  christos   r = uv_udp_recv_start(&server, sv_alloc_cb, sv_recv_cb);
    164  1.1.1.3  christos   ASSERT_OK(r);
    165      1.1  christos 
    166  1.1.1.3  christos   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
    167      1.1  christos 
    168      1.1  christos   r = uv_udp_init(uv_default_loop(), &client);
    169  1.1.1.3  christos   ASSERT_OK(r);
    170      1.1  christos 
    171      1.1  christos   buf = uv_buf_init("PING", 4);
    172      1.1  christos   r = uv_udp_send(&req,
    173      1.1  christos                   &client,
    174      1.1  christos                   &buf,
    175      1.1  christos                   1,
    176      1.1  christos                   (const struct sockaddr*) &addr,
    177      1.1  christos                   cl_send_cb);
    178  1.1.1.3  christos   ASSERT_OK(r);
    179      1.1  christos 
    180  1.1.1.3  christos   ASSERT_OK(close_cb_called);
    181  1.1.1.3  christos   ASSERT_OK(cl_send_cb_called);
    182  1.1.1.3  christos   ASSERT_OK(cl_recv_cb_called);
    183  1.1.1.3  christos   ASSERT_OK(sv_send_cb_called);
    184  1.1.1.3  christos   ASSERT_OK(sv_recv_cb_called);
    185      1.1  christos 
    186      1.1  christos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    187      1.1  christos 
    188  1.1.1.3  christos   ASSERT_EQ(1, cl_send_cb_called);
    189  1.1.1.3  christos   ASSERT_EQ(1, cl_recv_cb_called);
    190  1.1.1.3  christos   ASSERT_EQ(1, sv_send_cb_called);
    191  1.1.1.3  christos   ASSERT_EQ(1, sv_recv_cb_called);
    192  1.1.1.3  christos   ASSERT_EQ(2, close_cb_called);
    193      1.1  christos 
    194  1.1.1.3  christos   MAKE_VALGRIND_HAPPY(uv_default_loop());
    195      1.1  christos   return 0;
    196      1.1  christos }
    197