Home | History | Annotate | Line # | Download | only in test
test-udp-create-socket-early.c revision 1.1
      1 /* Copyright (c) 2015 Sal Ibarra Corretg <saghul (at) gmail.com>.
      2  * All rights reserved.
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a copy
      5  * of this software and associated documentation files (the "Software"), to
      6  * deal in the Software without restriction, including without limitation the
      7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      8  * sell copies of the Software, and to permit persons to whom the Software is
      9  * furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice shall be included in
     12  * all copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     20  * IN THE SOFTWARE.
     21  */
     22 
     23 #include "uv.h"
     24 #include "task.h"
     25 #include <string.h>
     26 
     27 #ifdef _WIN32
     28 # define INVALID_FD (INVALID_HANDLE_VALUE)
     29 #else
     30 # define INVALID_FD (-1)
     31 #endif
     32 
     33 
     34 TEST_IMPL(udp_create_early) {
     35   struct sockaddr_in addr;
     36   struct sockaddr_in sockname;
     37   uv_udp_t client;
     38   uv_os_fd_t fd;
     39   int r, namelen;
     40 
     41   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
     42 
     43   r = uv_udp_init_ex(uv_default_loop(), &client, AF_INET);
     44   ASSERT(r == 0);
     45 
     46   r = uv_fileno((const uv_handle_t*) &client, &fd);
     47   ASSERT(r == 0);
     48   ASSERT(fd != INVALID_FD);
     49 
     50   /* Windows returns WSAEINVAL if the socket is not bound */
     51 #ifndef _WIN32
     52   namelen = sizeof sockname;
     53   r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
     54   ASSERT(r == 0);
     55   ASSERT(sockname.sin_family == AF_INET);
     56 #endif
     57 
     58   r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
     59   ASSERT(r == 0);
     60 
     61   namelen = sizeof sockname;
     62   r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
     63   ASSERT(r == 0);
     64   ASSERT(memcmp(&addr.sin_addr,
     65                 &sockname.sin_addr,
     66                 sizeof(addr.sin_addr)) == 0);
     67 
     68   uv_close((uv_handle_t*) &client, NULL);
     69   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
     70 
     71   MAKE_VALGRIND_HAPPY();
     72   return 0;
     73 }
     74 
     75 
     76 TEST_IMPL(udp_create_early_bad_bind) {
     77   struct sockaddr_in addr;
     78   uv_udp_t client;
     79   uv_os_fd_t fd;
     80   int r;
     81 
     82   if (!can_ipv6())
     83     RETURN_SKIP("IPv6 not supported");
     84 
     85   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
     86 
     87   r = uv_udp_init_ex(uv_default_loop(), &client, AF_INET6);
     88   ASSERT(r == 0);
     89 
     90   r = uv_fileno((const uv_handle_t*) &client, &fd);
     91   ASSERT(r == 0);
     92   ASSERT(fd != INVALID_FD);
     93 
     94   /* Windows returns WSAEINVAL if the socket is not bound */
     95 #ifndef _WIN32
     96   {
     97     int namelen;
     98     struct sockaddr_in6 sockname;
     99     namelen = sizeof sockname;
    100     r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
    101     ASSERT(r == 0);
    102     ASSERT(sockname.sin6_family == AF_INET6);
    103   }
    104 #endif
    105 
    106   r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0);
    107 #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MSYS__)
    108   ASSERT(r == UV_EINVAL);
    109 #else
    110   ASSERT(r == UV_EFAULT);
    111 #endif
    112 
    113   uv_close((uv_handle_t*) &client, NULL);
    114   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    115 
    116   MAKE_VALGRIND_HAPPY();
    117   return 0;
    118 }
    119 
    120 
    121 TEST_IMPL(udp_create_early_bad_domain) {
    122   uv_udp_t client;
    123   int r;
    124 
    125   r = uv_udp_init_ex(uv_default_loop(), &client, 47);
    126   ASSERT(r == UV_EINVAL);
    127 
    128   r = uv_udp_init_ex(uv_default_loop(), &client, 1024);
    129   ASSERT(r == UV_EINVAL);
    130 
    131   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    132 
    133   MAKE_VALGRIND_HAPPY();
    134   return 0;
    135 }
    136