Home | History | Annotate | Line # | Download | only in test
test-getaddrinfo.c revision 1.1
      1  1.1  christos /* Copyright Joyent, Inc. 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 #include <stdlib.h>
     25  1.1  christos 
     26  1.1  christos #define CONCURRENT_COUNT    10
     27  1.1  christos 
     28  1.1  christos static const char* name = "localhost";
     29  1.1  christos 
     30  1.1  christos static int getaddrinfo_cbs = 0;
     31  1.1  christos 
     32  1.1  christos /* data used for running multiple calls concurrently */
     33  1.1  christos static uv_getaddrinfo_t* getaddrinfo_handle;
     34  1.1  christos static uv_getaddrinfo_t getaddrinfo_handles[CONCURRENT_COUNT];
     35  1.1  christos static int callback_counts[CONCURRENT_COUNT];
     36  1.1  christos static int fail_cb_called;
     37  1.1  christos 
     38  1.1  christos 
     39  1.1  christos static void getaddrinfo_fail_cb(uv_getaddrinfo_t* req,
     40  1.1  christos                                 int status,
     41  1.1  christos                                 struct addrinfo* res) {
     42  1.1  christos   ASSERT(fail_cb_called == 0);
     43  1.1  christos   ASSERT(status < 0);
     44  1.1  christos   ASSERT(res == NULL);
     45  1.1  christos   uv_freeaddrinfo(res);  /* Should not crash. */
     46  1.1  christos   fail_cb_called++;
     47  1.1  christos }
     48  1.1  christos 
     49  1.1  christos 
     50  1.1  christos static void getaddrinfo_basic_cb(uv_getaddrinfo_t* handle,
     51  1.1  christos                                  int status,
     52  1.1  christos                                  struct addrinfo* res) {
     53  1.1  christos   ASSERT(handle == getaddrinfo_handle);
     54  1.1  christos   getaddrinfo_cbs++;
     55  1.1  christos   free(handle);
     56  1.1  christos   uv_freeaddrinfo(res);
     57  1.1  christos }
     58  1.1  christos 
     59  1.1  christos 
     60  1.1  christos static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle,
     61  1.1  christos                                       int status,
     62  1.1  christos                                       struct addrinfo* res) {
     63  1.1  christos   int i;
     64  1.1  christos   int* data = (int*)handle->data;
     65  1.1  christos 
     66  1.1  christos   for (i = 0; i < CONCURRENT_COUNT; i++) {
     67  1.1  christos     if (&getaddrinfo_handles[i] == handle) {
     68  1.1  christos       ASSERT(i == *data);
     69  1.1  christos 
     70  1.1  christos       callback_counts[i]++;
     71  1.1  christos       break;
     72  1.1  christos     }
     73  1.1  christos   }
     74  1.1  christos   ASSERT (i < CONCURRENT_COUNT);
     75  1.1  christos 
     76  1.1  christos   free(data);
     77  1.1  christos   uv_freeaddrinfo(res);
     78  1.1  christos 
     79  1.1  christos   getaddrinfo_cbs++;
     80  1.1  christos }
     81  1.1  christos 
     82  1.1  christos 
     83  1.1  christos TEST_IMPL(getaddrinfo_fail) {
     84  1.1  christos   uv_getaddrinfo_t req;
     85  1.1  christos 
     86  1.1  christos   ASSERT(UV_EINVAL == uv_getaddrinfo(uv_default_loop(),
     87  1.1  christos                                      &req,
     88  1.1  christos                                      (uv_getaddrinfo_cb) abort,
     89  1.1  christos                                      NULL,
     90  1.1  christos                                      NULL,
     91  1.1  christos                                      NULL));
     92  1.1  christos 
     93  1.1  christos   /* Use a FQDN by ending in a period */
     94  1.1  christos   ASSERT(0 == uv_getaddrinfo(uv_default_loop(),
     95  1.1  christos                              &req,
     96  1.1  christos                              getaddrinfo_fail_cb,
     97  1.1  christos                              "xyzzy.xyzzy.xyzzy.",
     98  1.1  christos                              NULL,
     99  1.1  christos                              NULL));
    100  1.1  christos   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
    101  1.1  christos   ASSERT(fail_cb_called == 1);
    102  1.1  christos 
    103  1.1  christos   MAKE_VALGRIND_HAPPY();
    104  1.1  christos   return 0;
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos 
    108  1.1  christos TEST_IMPL(getaddrinfo_fail_sync) {
    109  1.1  christos /* TODO(gengjiawen): Fix test on QEMU. */
    110  1.1  christos #if defined(__QEMU__)
    111  1.1  christos   RETURN_SKIP("Test does not currently work in QEMU");
    112  1.1  christos #endif
    113  1.1  christos   uv_getaddrinfo_t req;
    114  1.1  christos 
    115  1.1  christos   /* Use a FQDN by ending in a period */
    116  1.1  christos   ASSERT(0 > uv_getaddrinfo(uv_default_loop(),
    117  1.1  christos                             &req,
    118  1.1  christos                             NULL,
    119  1.1  christos                             "xyzzy.xyzzy.xyzzy.",
    120  1.1  christos                             NULL,
    121  1.1  christos                             NULL));
    122  1.1  christos   uv_freeaddrinfo(req.addrinfo);
    123  1.1  christos 
    124  1.1  christos   MAKE_VALGRIND_HAPPY();
    125  1.1  christos   return 0;
    126  1.1  christos }
    127  1.1  christos 
    128  1.1  christos 
    129  1.1  christos TEST_IMPL(getaddrinfo_basic) {
    130  1.1  christos   int r;
    131  1.1  christos   getaddrinfo_handle = (uv_getaddrinfo_t*)malloc(sizeof(uv_getaddrinfo_t));
    132  1.1  christos 
    133  1.1  christos   r = uv_getaddrinfo(uv_default_loop(),
    134  1.1  christos                      getaddrinfo_handle,
    135  1.1  christos                      &getaddrinfo_basic_cb,
    136  1.1  christos                      name,
    137  1.1  christos                      NULL,
    138  1.1  christos                      NULL);
    139  1.1  christos   ASSERT(r == 0);
    140  1.1  christos 
    141  1.1  christos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    142  1.1  christos 
    143  1.1  christos   ASSERT(getaddrinfo_cbs == 1);
    144  1.1  christos 
    145  1.1  christos   MAKE_VALGRIND_HAPPY();
    146  1.1  christos   return 0;
    147  1.1  christos }
    148  1.1  christos 
    149  1.1  christos 
    150  1.1  christos TEST_IMPL(getaddrinfo_basic_sync) {
    151  1.1  christos /* TODO(gengjiawen): Fix test on QEMU. */
    152  1.1  christos #if defined(__QEMU__)
    153  1.1  christos   RETURN_SKIP("Test does not currently work in QEMU");
    154  1.1  christos #endif
    155  1.1  christos   uv_getaddrinfo_t req;
    156  1.1  christos 
    157  1.1  christos   ASSERT(0 == uv_getaddrinfo(uv_default_loop(),
    158  1.1  christos                              &req,
    159  1.1  christos                              NULL,
    160  1.1  christos                              name,
    161  1.1  christos                              NULL,
    162  1.1  christos                              NULL));
    163  1.1  christos   uv_freeaddrinfo(req.addrinfo);
    164  1.1  christos 
    165  1.1  christos   MAKE_VALGRIND_HAPPY();
    166  1.1  christos   return 0;
    167  1.1  christos }
    168  1.1  christos 
    169  1.1  christos 
    170  1.1  christos TEST_IMPL(getaddrinfo_concurrent) {
    171  1.1  christos   int i, r;
    172  1.1  christos   int* data;
    173  1.1  christos 
    174  1.1  christos   for (i = 0; i < CONCURRENT_COUNT; i++) {
    175  1.1  christos     callback_counts[i] = 0;
    176  1.1  christos 
    177  1.1  christos     data = (int*)malloc(sizeof(int));
    178  1.1  christos     ASSERT(data != NULL);
    179  1.1  christos     *data = i;
    180  1.1  christos     getaddrinfo_handles[i].data = data;
    181  1.1  christos 
    182  1.1  christos     r = uv_getaddrinfo(uv_default_loop(),
    183  1.1  christos                        &getaddrinfo_handles[i],
    184  1.1  christos                        &getaddrinfo_cuncurrent_cb,
    185  1.1  christos                        name,
    186  1.1  christos                        NULL,
    187  1.1  christos                        NULL);
    188  1.1  christos     ASSERT(r == 0);
    189  1.1  christos   }
    190  1.1  christos 
    191  1.1  christos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    192  1.1  christos 
    193  1.1  christos   for (i = 0; i < CONCURRENT_COUNT; i++) {
    194  1.1  christos     ASSERT(callback_counts[i] == 1);
    195  1.1  christos   }
    196  1.1  christos 
    197  1.1  christos   MAKE_VALGRIND_HAPPY();
    198  1.1  christos   return 0;
    199  1.1  christos }
    200