Home | History | Annotate | Line # | Download | only in test
test-getaddrinfo.c revision 1.1
      1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
      2  *
      3  * Permission is hereby granted, free of charge, to any person obtaining a copy
      4  * of this software and associated documentation files (the "Software"), to
      5  * deal in the Software without restriction, including without limitation the
      6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      7  * sell copies of the Software, and to permit persons to whom the Software is
      8  * furnished to do so, subject to the following conditions:
      9  *
     10  * The above copyright notice and this permission notice shall be included in
     11  * all copies or substantial portions of the Software.
     12  *
     13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     19  * IN THE SOFTWARE.
     20  */
     21 
     22 #include "uv.h"
     23 #include "task.h"
     24 #include <stdlib.h>
     25 
     26 #define CONCURRENT_COUNT    10
     27 
     28 static const char* name = "localhost";
     29 
     30 static int getaddrinfo_cbs = 0;
     31 
     32 /* data used for running multiple calls concurrently */
     33 static uv_getaddrinfo_t* getaddrinfo_handle;
     34 static uv_getaddrinfo_t getaddrinfo_handles[CONCURRENT_COUNT];
     35 static int callback_counts[CONCURRENT_COUNT];
     36 static int fail_cb_called;
     37 
     38 
     39 static void getaddrinfo_fail_cb(uv_getaddrinfo_t* req,
     40                                 int status,
     41                                 struct addrinfo* res) {
     42   ASSERT(fail_cb_called == 0);
     43   ASSERT(status < 0);
     44   ASSERT(res == NULL);
     45   uv_freeaddrinfo(res);  /* Should not crash. */
     46   fail_cb_called++;
     47 }
     48 
     49 
     50 static void getaddrinfo_basic_cb(uv_getaddrinfo_t* handle,
     51                                  int status,
     52                                  struct addrinfo* res) {
     53   ASSERT(handle == getaddrinfo_handle);
     54   getaddrinfo_cbs++;
     55   free(handle);
     56   uv_freeaddrinfo(res);
     57 }
     58 
     59 
     60 static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle,
     61                                       int status,
     62                                       struct addrinfo* res) {
     63   int i;
     64   int* data = (int*)handle->data;
     65 
     66   for (i = 0; i < CONCURRENT_COUNT; i++) {
     67     if (&getaddrinfo_handles[i] == handle) {
     68       ASSERT(i == *data);
     69 
     70       callback_counts[i]++;
     71       break;
     72     }
     73   }
     74   ASSERT (i < CONCURRENT_COUNT);
     75 
     76   free(data);
     77   uv_freeaddrinfo(res);
     78 
     79   getaddrinfo_cbs++;
     80 }
     81 
     82 
     83 TEST_IMPL(getaddrinfo_fail) {
     84   uv_getaddrinfo_t req;
     85 
     86   ASSERT(UV_EINVAL == uv_getaddrinfo(uv_default_loop(),
     87                                      &req,
     88                                      (uv_getaddrinfo_cb) abort,
     89                                      NULL,
     90                                      NULL,
     91                                      NULL));
     92 
     93   /* Use a FQDN by ending in a period */
     94   ASSERT(0 == uv_getaddrinfo(uv_default_loop(),
     95                              &req,
     96                              getaddrinfo_fail_cb,
     97                              "xyzzy.xyzzy.xyzzy.",
     98                              NULL,
     99                              NULL));
    100   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
    101   ASSERT(fail_cb_called == 1);
    102 
    103   MAKE_VALGRIND_HAPPY();
    104   return 0;
    105 }
    106 
    107 
    108 TEST_IMPL(getaddrinfo_fail_sync) {
    109 /* TODO(gengjiawen): Fix test on QEMU. */
    110 #if defined(__QEMU__)
    111   RETURN_SKIP("Test does not currently work in QEMU");
    112 #endif
    113   uv_getaddrinfo_t req;
    114 
    115   /* Use a FQDN by ending in a period */
    116   ASSERT(0 > uv_getaddrinfo(uv_default_loop(),
    117                             &req,
    118                             NULL,
    119                             "xyzzy.xyzzy.xyzzy.",
    120                             NULL,
    121                             NULL));
    122   uv_freeaddrinfo(req.addrinfo);
    123 
    124   MAKE_VALGRIND_HAPPY();
    125   return 0;
    126 }
    127 
    128 
    129 TEST_IMPL(getaddrinfo_basic) {
    130   int r;
    131   getaddrinfo_handle = (uv_getaddrinfo_t*)malloc(sizeof(uv_getaddrinfo_t));
    132 
    133   r = uv_getaddrinfo(uv_default_loop(),
    134                      getaddrinfo_handle,
    135                      &getaddrinfo_basic_cb,
    136                      name,
    137                      NULL,
    138                      NULL);
    139   ASSERT(r == 0);
    140 
    141   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    142 
    143   ASSERT(getaddrinfo_cbs == 1);
    144 
    145   MAKE_VALGRIND_HAPPY();
    146   return 0;
    147 }
    148 
    149 
    150 TEST_IMPL(getaddrinfo_basic_sync) {
    151 /* TODO(gengjiawen): Fix test on QEMU. */
    152 #if defined(__QEMU__)
    153   RETURN_SKIP("Test does not currently work in QEMU");
    154 #endif
    155   uv_getaddrinfo_t req;
    156 
    157   ASSERT(0 == uv_getaddrinfo(uv_default_loop(),
    158                              &req,
    159                              NULL,
    160                              name,
    161                              NULL,
    162                              NULL));
    163   uv_freeaddrinfo(req.addrinfo);
    164 
    165   MAKE_VALGRIND_HAPPY();
    166   return 0;
    167 }
    168 
    169 
    170 TEST_IMPL(getaddrinfo_concurrent) {
    171   int i, r;
    172   int* data;
    173 
    174   for (i = 0; i < CONCURRENT_COUNT; i++) {
    175     callback_counts[i] = 0;
    176 
    177     data = (int*)malloc(sizeof(int));
    178     ASSERT(data != NULL);
    179     *data = i;
    180     getaddrinfo_handles[i].data = data;
    181 
    182     r = uv_getaddrinfo(uv_default_loop(),
    183                        &getaddrinfo_handles[i],
    184                        &getaddrinfo_cuncurrent_cb,
    185                        name,
    186                        NULL,
    187                        NULL);
    188     ASSERT(r == 0);
    189   }
    190 
    191   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    192 
    193   for (i = 0; i < CONCURRENT_COUNT; i++) {
    194     ASSERT(callback_counts[i] == 1);
    195   }
    196 
    197   MAKE_VALGRIND_HAPPY();
    198   return 0;
    199 }
    200