test-getaddrinfo.c
Go to the documentation of this file.
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 */
36 static int fail_cb_called;
37 
38 
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. */
47 }
48 
49 
51  int status,
52  struct addrinfo* res) {
55  free(handle);
56  uv_freeaddrinfo(res);
57 }
58 
59 
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  }
75 
76  free(data);
77  uv_freeaddrinfo(res);
78 
80 }
81 
82 
83 TEST_IMPL(getaddrinfo_fail) {
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 */
95  &req,
97  "xyzzy.xyzzy.xyzzy.",
98  NULL,
99  NULL));
101  ASSERT(fail_cb_called == 1);
102 
104  return 0;
105 }
106 
107 
108 TEST_IMPL(getaddrinfo_fail_sync) {
110 
111  /* Use a FQDN by ending in a period */
113  &req,
114  NULL,
115  "xyzzy.xyzzy.xyzzy.",
116  NULL,
117  NULL));
118  uv_freeaddrinfo(req.addrinfo);
119 
121  return 0;
122 }
123 
124 
125 TEST_IMPL(getaddrinfo_basic) {
126  int r;
128 
132  name,
133  NULL,
134  NULL);
135  ASSERT(r == 0);
136 
138 
139  ASSERT(getaddrinfo_cbs == 1);
140 
142  return 0;
143 }
144 
145 
146 TEST_IMPL(getaddrinfo_basic_sync) {
148 
150  &req,
151  NULL,
152  name,
153  NULL,
154  NULL));
155  uv_freeaddrinfo(req.addrinfo);
156 
158  return 0;
159 }
160 
161 
162 TEST_IMPL(getaddrinfo_concurrent) {
163  int i, r;
164  int* data;
165 
166  for (i = 0; i < CONCURRENT_COUNT; i++) {
167  callback_counts[i] = 0;
168 
169  data = (int*)malloc(sizeof(int));
170  ASSERT(data != NULL);
171  *data = i;
172  getaddrinfo_handles[i].data = data;
173 
177  name,
178  NULL,
179  NULL);
180  ASSERT(r == 0);
181  }
182 
184 
185  for (i = 0; i < CONCURRENT_COUNT; i++) {
186  ASSERT(callback_counts[i] == 1);
187  }
188 
190  return 0;
191 }
getaddrinfo_cuncurrent_cb
static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t *handle, int status, struct addrinfo *res)
Definition: test-getaddrinfo.c:60
task.h
uv_getaddrinfo_s
Definition: uv.h:871
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
getaddrinfo_handle
static uv_getaddrinfo_t * getaddrinfo_handle
Definition: test-getaddrinfo.c:33
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
getaddrinfo_handles
static uv_getaddrinfo_t getaddrinfo_handles[CONCURRENT_COUNT]
Definition: test-getaddrinfo.c:34
uv_getaddrinfo_cb
void(* uv_getaddrinfo_cb)(uv_getaddrinfo_t *req, int status, struct addrinfo *res)
Definition: uv.h:328
for
for(map_begin_internal(intern, &it);!map_done(&it);map_next(&it))
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:207
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
TEST_IMPL
TEST_IMPL(getaddrinfo_fail)
Definition: test-getaddrinfo.c:83
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
CONCURRENT_COUNT
#define CONCURRENT_COUNT
Definition: test-getaddrinfo.c:26
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
uv_freeaddrinfo
UV_EXTERN void uv_freeaddrinfo(struct addrinfo *ai)
Definition: unix/getaddrinfo.c:223
callback_counts
static int callback_counts[CONCURRENT_COUNT]
Definition: test-getaddrinfo.c:35
getaddrinfo_basic_cb
static void getaddrinfo_basic_cb(uv_getaddrinfo_t *handle, int status, struct addrinfo *res)
Definition: test-getaddrinfo.c:50
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
getaddrinfo_cbs
static int getaddrinfo_cbs
Definition: test-getaddrinfo.c:30
fail_cb_called
static int fail_cb_called
Definition: test-getaddrinfo.c:36
name
static const char * name
Definition: test-getaddrinfo.c:28
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_getaddrinfo
UV_EXTERN int uv_getaddrinfo(uv_loop_t *loop, uv_getaddrinfo_t *req, uv_getaddrinfo_cb getaddrinfo_cb, const char *node, const char *service, const struct addrinfo *hints)
Definition: unix/getaddrinfo.c:141
handle
static csh handle
Definition: test_arm_regression.c:16
addrinfo
Definition: ares_ipv6.h:43
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
getaddrinfo_fail_cb
static void getaddrinfo_fail_cb(uv_getaddrinfo_t *req, int status, struct addrinfo *res)
Definition: test-getaddrinfo.c:39


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26