• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <netdb.h>
17 #include <stdlib.h>
18 #include <sys/socket.h>
19 #include <pthread.h>
20 #include <string.h>
21 #include <errno.h>
22 
23 #include "functionalext.h"
24 
25 const int GETADDRINFO_RESULT = 0;
26 const int FLAGS_FIELD = -1;
27 const int SERVICE_UNKNOEN = -2;
28 const int FAMILY_NOTSUPPORTED = -6;
29 const int SOCKTYPE_NOTSUPPORTED = -8;
30 
31 /**
32  * @tc.name      : getaddrinfo_0100
33  * @tc.desc      : Each parameter is valid, the ai_flags of hint is AI_PASSIVE, which can resolve the IP
34  *                 address of the host name.
35  * @tc.level     : Level 0
36  */
getaddrinfo_0100(void)37 void getaddrinfo_0100(void)
38 {
39     struct addrinfo *result, hint;
40     hint.ai_flags = AI_PASSIVE;
41     hint.ai_family = AF_UNSPEC;
42     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
43     EXPECT_EQ("getaddrinfo_0100", ret, GETADDRINFO_RESULT);
44     EXPECT_TRUE("getaddrinfo_0100", result->ai_addr != NULL);
45     freeaddrinfo(result);
46     result = NULL;
47 }
48 
49 /**
50  * @tc.name      : getaddrinfo_0200
51  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name.
52  * @tc.level     : Level 0
53  */
getaddrinfo_0200(void)54 void getaddrinfo_0200(void)
55 {
56     struct addrinfo *result, hint;
57     hint.ai_flags = AI_ALL;
58     hint.ai_family = AF_UNSPEC;
59     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
60     EXPECT_EQ("getaddrinfo_0200", ret, GETADDRINFO_RESULT);
61     EXPECT_TRUE("getaddrinfo_0200", result->ai_addr != NULL);
62     freeaddrinfo(result);
63     result = NULL;
64 }
65 
66 /**
67  * @tc.name      : getaddrinfo_0300
68  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name
69  *                 (hint.ai_flags =AI_NUMERICHOST).
70  * @tc.level     : Level 0
71  */
getaddrinfo_0300(void)72 void getaddrinfo_0300(void)
73 {
74     struct addrinfo *result, hint;
75     hint.ai_flags = AI_NUMERICHOST;
76     hint.ai_family = AF_INET6;
77     char buf[] = "fe80::bed5:4695:6cac:bef8";
78     int ret = getaddrinfo(buf, NULL, &hint, &result);
79     EXPECT_EQ("getaddrinfo_0300", ret, GETADDRINFO_RESULT);
80     EXPECT_TRUE("getaddrinfo_0300", result->ai_addr != NULL);
81     freeaddrinfo(result);
82     result = NULL;
83 }
84 
85 /**
86  * @tc.name      : getaddrinfo_0400
87  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name(hint =AI_V4MAPPED).
88  * @tc.level     : Level 0
89  */
getaddrinfo_0400(void)90 void getaddrinfo_0400(void)
91 {
92     struct addrinfo *result, hint;
93     hint.ai_flags = AI_V4MAPPED;
94     hint.ai_family = AF_INET6;
95     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
96     EXPECT_EQ("getaddrinfo_0400", ret, GETADDRINFO_RESULT);
97     EXPECT_TRUE("getaddrinfo_0400", result->ai_addr != NULL);
98     freeaddrinfo(result);
99     result = NULL;
100 }
101 
102 /**
103  * @tc.name      : getaddrinfo_0500
104  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name(hint =AI_V4MAPPED).
105  * @tc.level     : Level 0
106  */
getaddrinfo_0500(void)107 void getaddrinfo_0500(void)
108 {
109     struct addrinfo *result, hint;
110     hint.ai_flags = AI_V4MAPPED;
111     hint.ai_family = AF_UNSPEC;
112     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
113     EXPECT_EQ("getaddrinfo_0500", ret, GETADDRINFO_RESULT);
114     EXPECT_TRUE("getaddrinfo_0500", result->ai_addr != NULL);
115     freeaddrinfo(result);
116     result = NULL;
117 }
118 
119 /**
120  * @tc.name      : getaddrinfo_0600
121  * @tc.desc      : Each parameter is valid and can resolve the IP address of
122  *                 the host name(hint.ai_flags =AI_ADDRCONFIG).
123  * @tc.level     : Level 0
124  */
getaddrinfo_0600(void)125 void getaddrinfo_0600(void)
126 {
127     struct addrinfo *result, hint;
128     hint.ai_flags = AI_ADDRCONFIG;
129     hint.ai_family = AF_UNSPEC;
130     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
131     EXPECT_EQ("getaddrinfo_0600", ret, GETADDRINFO_RESULT);
132     EXPECT_TRUE("getaddrinfo_0600", result->ai_addr != NULL);
133     freeaddrinfo(result);
134     result = NULL;
135 }
136 
137 /**
138  * @tc.name      : getaddrinfo_0700
139  * @tc.desc      : Each parameter is valid and can resolve the IP address
140  *                 of the host name(hint.ai_flags =AI_NUMERICSERV).
141  * @tc.level     : Level 1
142  */
getaddrinfo_0700(void)143 void getaddrinfo_0700(void)
144 {
145     struct addrinfo *result, hint;
146     hint.ai_flags = AI_NUMERICSERV;
147     hint.ai_family = AF_UNSPEC;
148     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
149     EXPECT_EQ("getaddrinfo_0700", ret, GETADDRINFO_RESULT);
150     EXPECT_TRUE("getaddrinfo_0700", result->ai_addr != NULL);
151     freeaddrinfo(result);
152     result = NULL;
153 }
154 
155 /**
156  * @tc.name      : getaddrinfo_0800
157  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name(hint =NULL).
158  * @tc.level     : Level 1
159  */
getaddrinfo_0800(void)160 void getaddrinfo_0800(void)
161 {
162     struct addrinfo *result;
163     int ret = getaddrinfo("127.0.0.1", NULL, NULL, &result);
164     EXPECT_EQ("getaddrinfo_0800", ret, GETADDRINFO_RESULT);
165     EXPECT_TRUE("getaddrinfo_0800", result->ai_addr != NULL);
166     freeaddrinfo(result);
167     result = NULL;
168 }
169 
170 /**
171  * @tc.name      : getaddrinfo_0900
172  * @tc.desc      : Invalid parameter, Hint’s ai_flags is invalid, can not resolve the IP address of the host name.
173  * @tc.level     : Level 2
174  */
getaddrinfo_0900(void)175 void getaddrinfo_0900(void)
176 {
177     struct addrinfo *result, hint;
178     hint.ai_flags = 0x4000;
179     hint.ai_family = AF_UNSPEC;
180     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
181     EXPECT_EQ("getaddrinfo_0900", ret, FLAGS_FIELD);
182 }
183 
184 /**
185  * @tc.name      : getaddrinfo_1000
186  * @tc.desc      : Invalid parameter, Hint’s ai_family is invalid, can not resolve the IP address of the host name.
187  * @tc.level     : Level 2
188  */
getaddrinfo_1000(void)189 void getaddrinfo_1000(void)
190 {
191     struct addrinfo *result, hint;
192     hint.ai_flags = AI_ALL;
193     hint.ai_family = PF_AX25;
194     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
195     EXPECT_EQ("getaddrinfo_1000", ret, FAMILY_NOTSUPPORTED);
196 }
197 
198 /**
199  * @tc.name      : getaddrinfo_1100
200  * @tc.desc      : The parameters are invalid,hint ai_flags is AI_NUMERICHOST,
201  *                 host format is incorrect, can not resolve the host name Ip address.
202  * @tc.level     : Level 2
203  */
getaddrinfo_1100(void)204 void getaddrinfo_1100(void)
205 {
206     struct addrinfo *result, hint;
207     hint.ai_flags = AI_NUMERICHOST;
208     hint.ai_family = AF_INET6;
209     char one[300] = "fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
210     char two[] = ":fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
211     char three[] = ":fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
212     char four[] = ":fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
213     strcat(one, two);
214     strcat(one, three);
215     strcat(one, four);
216     int ret = getaddrinfo(one, NULL, &hint, &result);
217     EXPECT_EQ("getaddrinfo_1100", ret, SERVICE_UNKNOEN);
218 }
219 
220 /**
221  * @tc.name      : getaddrinfo_1200
222  * @tc.desc      : The parameter is invalid, host is NULL, SERV is NULL, and the IP address of the
223  *                 host name can not be resolved.
224  * @tc.level     : Level 2
225  */
getaddrinfo_1200(void)226 void getaddrinfo_1200(void)
227 {
228     struct addrinfo *result, hint;
229     hint.ai_flags = AI_NUMERICHOST;
230     hint.ai_family = AF_INET6;
231     int ret = getaddrinfo(NULL, NULL, &hint, &result);
232     EXPECT_EQ("getaddrinfo_1200", ret, SERVICE_UNKNOEN);
233 }
234 
235 /**
236  * @tc.name      : getaddrinfo_1300
237  * @tc.desc      : The parameter is invalid and the IP address of the host name can not be resolved.
238  * @tc.level     : Level 2
239  */
getaddrinfo_1300(void)240 void getaddrinfo_1300(void)
241 {
242     struct addrinfo *result, hint;
243     hint.ai_flags = AI_CANONNAME;
244     hint.ai_family = AF_INET;
245     hint.ai_protocol = IPPROTO_UDP;
246     hint.ai_socktype = SOCK_STREAM;
247     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
248     EXPECT_EQ("getaddrinfo_1300", ret, SOCKTYPE_NOTSUPPORTED);
249 }
250 
251 /**
252  * @tc.name      : getaddrinfo_1400
253  * @tc.desc      : The parameter is invalid and the IP address of the host name can not be resolved.
254  * @tc.level     : Level 2
255  */
getaddrinfo_1400(void)256 void getaddrinfo_1400(void)
257 {
258     struct addrinfo *result, hint;
259     hint.ai_flags = AI_CANONNAME;
260     hint.ai_family = AF_INET;
261     hint.ai_protocol = IPPROTO_TCP;
262     hint.ai_socktype = SOCK_DGRAM;
263     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
264     EXPECT_EQ("getaddrinfo_1400", ret, SOCKTYPE_NOTSUPPORTED);
265 }
266 
267 /**
268  * @tc.name      : getaddrinfo_1500
269  * @tc.desc      : The parameter is invalid and the IP address of the host name can not be resolved.
270  * @tc.level     : Level 2
271  */
getaddrinfo_1500(void)272 void getaddrinfo_1500(void)
273 {
274     struct addrinfo *result, hint;
275     hint.ai_flags = AI_CANONNAME;
276     hint.ai_family = AF_INET;
277     hint.ai_socktype = SOCK_RAW;
278     int ret = getaddrinfo("127.0.0.1", "2000", &hint, &result);
279     EXPECT_EQ("getaddrinfo_1500", ret, SOCKTYPE_NOTSUPPORTED);
280 }
281 
test_all_cases(void * arg)282 static void *test_all_cases(void *arg)
283 {
284     getaddrinfo_0100();
285     getaddrinfo_0200();
286     getaddrinfo_0300();
287     getaddrinfo_0400();
288     getaddrinfo_0500();
289     getaddrinfo_0600();
290     getaddrinfo_0700();
291     getaddrinfo_0800();
292     getaddrinfo_0900();
293     getaddrinfo_1000();
294     getaddrinfo_1100();
295     getaddrinfo_1200();
296     getaddrinfo_1300();
297     getaddrinfo_1400();
298     getaddrinfo_1500();
299     return arg;
300 }
301 
do_test_concurrently(void * (* test)(void * arg),size_t num_threads)302 static void do_test_concurrently(void *(*test) (void *arg), size_t num_threads)
303 {
304     pthread_t *threads = (pthread_t *) malloc(sizeof(pthread_t) * num_threads);
305     if (threads == NULL) {
306         t_error("Failed to allocate memory: %s\n", strerror(errno));
307         return;
308     }
309 
310     size_t last = 0;
311     while (last < num_threads) {
312         if (pthread_create(&(threads[last]), NULL, test, NULL)) {
313             t_error("Failed to create thread: %s\n", strerror(errno));
314             break;
315         }
316         last++;
317     }
318 
319     for (size_t i = 0; i < last; i++) {
320         if (pthread_join(threads[i], NULL)) {
321             t_error("Failed to join thread: %s\n", strerror(errno));
322         }
323     }
324 
325     free(threads);
326     return;
327 }
328 
main(int argc,char * argv[])329 int main(int argc, char *argv[])
330 {
331     size_t num_threads = 16;
332     do_test_concurrently(test_all_cases, num_threads);
333 
334     return t_status;
335 }
336