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 | uv_getaddrinfo_t req; |
110 | |
111 | /* Use a FQDN by ending in a period */ |
112 | ASSERT(0 > uv_getaddrinfo(uv_default_loop(), |
113 | &req, |
114 | NULL, |
115 | "xyzzy.xyzzy.xyzzy." , |
116 | NULL, |
117 | NULL)); |
118 | uv_freeaddrinfo(req.addrinfo); |
119 | |
120 | MAKE_VALGRIND_HAPPY(); |
121 | return 0; |
122 | } |
123 | |
124 | |
125 | TEST_IMPL(getaddrinfo_basic) { |
126 | int r; |
127 | getaddrinfo_handle = (uv_getaddrinfo_t*)malloc(sizeof(uv_getaddrinfo_t)); |
128 | |
129 | r = uv_getaddrinfo(uv_default_loop(), |
130 | getaddrinfo_handle, |
131 | &getaddrinfo_basic_cb, |
132 | name, |
133 | NULL, |
134 | NULL); |
135 | ASSERT(r == 0); |
136 | |
137 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
138 | |
139 | ASSERT(getaddrinfo_cbs == 1); |
140 | |
141 | MAKE_VALGRIND_HAPPY(); |
142 | return 0; |
143 | } |
144 | |
145 | |
146 | TEST_IMPL(getaddrinfo_basic_sync) { |
147 | uv_getaddrinfo_t req; |
148 | |
149 | ASSERT(0 == uv_getaddrinfo(uv_default_loop(), |
150 | &req, |
151 | NULL, |
152 | name, |
153 | NULL, |
154 | NULL)); |
155 | uv_freeaddrinfo(req.addrinfo); |
156 | |
157 | MAKE_VALGRIND_HAPPY(); |
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 | |
174 | r = uv_getaddrinfo(uv_default_loop(), |
175 | &getaddrinfo_handles[i], |
176 | &getaddrinfo_cuncurrent_cb, |
177 | name, |
178 | NULL, |
179 | NULL); |
180 | ASSERT(r == 0); |
181 | } |
182 | |
183 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
184 | |
185 | for (i = 0; i < CONCURRENT_COUNT; i++) { |
186 | ASSERT(callback_counts[i] == 1); |
187 | } |
188 | |
189 | MAKE_VALGRIND_HAPPY(); |
190 | return 0; |
191 | } |
192 | |