• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 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 
20 #include "functionalext.h"
21 
22 const int GETADDRINFO_RESULT = 0;
23 const int FLAGS_FIELD = -1;
24 const int SERVICE_UNKNOEN = -2;
25 const int FAMILY_NOTSUPPORTED = -6;
26 const int SOCKTYPE_NOTSUPPORTED = -8;
27 
28 /**
29  * @tc.name      : getaddrinfo_0100
30  * @tc.desc      : Each parameter is valid, the ai_flags of hint is AI_PASSIVE, which can resolve the IP
31  *                 address of the host name.
32  * @tc.level     : Level 0
33  */
getaddrinfo_0100(void)34 void getaddrinfo_0100(void)
35 {
36     struct addrinfo *result, hint;
37     hint.ai_flags = AI_PASSIVE;
38     hint.ai_family = AF_UNSPEC;
39     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
40     EXPECT_EQ("getaddrinfo_0100", ret, GETADDRINFO_RESULT);
41     EXPECT_TRUE("getaddrinfo_0100", result->ai_addr != NULL);
42     freeaddrinfo(result);
43     result = NULL;
44 }
45 
46 /**
47  * @tc.name      : getaddrinfo_0200
48  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name.
49  * @tc.level     : Level 0
50  */
getaddrinfo_0200(void)51 void getaddrinfo_0200(void)
52 {
53     struct addrinfo *result, hint;
54     hint.ai_flags = AI_ALL;
55     hint.ai_family = AF_UNSPEC;
56     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
57     EXPECT_EQ("getaddrinfo_0200", ret, GETADDRINFO_RESULT);
58     EXPECT_TRUE("getaddrinfo_0200", result->ai_addr != NULL);
59     freeaddrinfo(result);
60     result = NULL;
61 }
62 
63 /**
64  * @tc.name      : getaddrinfo_0300
65  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name
66  *                 (hint.ai_flags =AI_NUMERICHOST).
67  * @tc.level     : Level 0
68  */
getaddrinfo_0300(void)69 void getaddrinfo_0300(void)
70 {
71     struct addrinfo *result, hint;
72     hint.ai_flags = AI_NUMERICHOST;
73     hint.ai_family = AF_INET6;
74     char buf[] = "fe80::bed5:4695:6cac:bef8";
75     int ret = getaddrinfo(buf, NULL, &hint, &result);
76     EXPECT_EQ("getaddrinfo_0300", ret, GETADDRINFO_RESULT);
77     EXPECT_TRUE("getaddrinfo_0300", result->ai_addr != NULL);
78     freeaddrinfo(result);
79     result = NULL;
80 }
81 
82 /**
83  * @tc.name      : getaddrinfo_0400
84  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name(hint =AI_V4MAPPED).
85  * @tc.level     : Level 0
86  */
getaddrinfo_0400(void)87 void getaddrinfo_0400(void)
88 {
89     struct addrinfo *result, hint;
90     hint.ai_flags = AI_V4MAPPED;
91     hint.ai_family = AF_INET6;
92     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
93     EXPECT_EQ("getaddrinfo_0400", ret, GETADDRINFO_RESULT);
94     EXPECT_TRUE("getaddrinfo_0400", result->ai_addr != NULL);
95     freeaddrinfo(result);
96     result = NULL;
97 }
98 
99 /**
100  * @tc.name      : getaddrinfo_0500
101  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name(hint =AI_V4MAPPED).
102  * @tc.level     : Level 0
103  */
getaddrinfo_0500(void)104 void getaddrinfo_0500(void)
105 {
106     struct addrinfo *result, hint;
107     hint.ai_flags = AI_V4MAPPED;
108     hint.ai_family = AF_UNSPEC;
109     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
110     EXPECT_EQ("getaddrinfo_0500", ret, GETADDRINFO_RESULT);
111     EXPECT_TRUE("getaddrinfo_0500", result->ai_addr != NULL);
112     freeaddrinfo(result);
113     result = NULL;
114 }
115 
116 /**
117  * @tc.name      : getaddrinfo_0600
118  * @tc.desc      : Each parameter is valid and can resolve the IP address of
119  *                 the host name(hint.ai_flags =AI_ADDRCONFIG).
120  * @tc.level     : Level 0
121  */
getaddrinfo_0600(void)122 void getaddrinfo_0600(void)
123 {
124     struct addrinfo *result, hint;
125     hint.ai_flags = AI_ADDRCONFIG;
126     hint.ai_family = AF_UNSPEC;
127     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
128     EXPECT_EQ("getaddrinfo_0600", ret, GETADDRINFO_RESULT);
129     EXPECT_TRUE("getaddrinfo_0600", result->ai_addr != NULL);
130     freeaddrinfo(result);
131     result = NULL;
132 }
133 
134 /**
135  * @tc.name      : getaddrinfo_0700
136  * @tc.desc      : Each parameter is valid and can resolve the IP address
137  *                 of the host name(hint.ai_flags =AI_NUMERICSERV).
138  * @tc.level     : Level 1
139  */
getaddrinfo_0700(void)140 void getaddrinfo_0700(void)
141 {
142     struct addrinfo *result, hint;
143     hint.ai_flags = AI_NUMERICSERV;
144     hint.ai_family = AF_UNSPEC;
145     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
146     EXPECT_EQ("getaddrinfo_0700", ret, GETADDRINFO_RESULT);
147     EXPECT_TRUE("getaddrinfo_0700", result->ai_addr != NULL);
148     freeaddrinfo(result);
149     result = NULL;
150 }
151 
152 /**
153  * @tc.name      : getaddrinfo_0800
154  * @tc.desc      : Each parameter is valid and can resolve the IP address of the host name(hint =NULL).
155  * @tc.level     : Level 1
156  */
getaddrinfo_0800(void)157 void getaddrinfo_0800(void)
158 {
159     struct addrinfo *result;
160     int ret = getaddrinfo("127.0.0.1", NULL, NULL, &result);
161     EXPECT_EQ("getaddrinfo_0800", ret, GETADDRINFO_RESULT);
162     EXPECT_TRUE("getaddrinfo_0800", result->ai_addr != NULL);
163     freeaddrinfo(result);
164     result = NULL;
165 }
166 
167 /**
168  * @tc.name      : getaddrinfo_0900
169  * @tc.desc      : Invalid parameter, Hint’s ai_flags is invalid, can not resolve the IP address of the host name.
170  * @tc.level     : Level 2
171  */
getaddrinfo_0900(void)172 void getaddrinfo_0900(void)
173 {
174     struct addrinfo *result, hint;
175     hint.ai_flags = 0x4000;
176     hint.ai_family = AF_UNSPEC;
177     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
178     EXPECT_EQ("getaddrinfo_0900", ret, FLAGS_FIELD);
179 }
180 
181 /**
182  * @tc.name      : getaddrinfo_1000
183  * @tc.desc      : Invalid parameter, Hint’s ai_family is invalid, can not resolve the IP address of the host name.
184  * @tc.level     : Level 2
185  */
getaddrinfo_1000(void)186 void getaddrinfo_1000(void)
187 {
188     struct addrinfo *result, hint;
189     hint.ai_flags = AI_ALL;
190     hint.ai_family = PF_AX25;
191     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
192     EXPECT_EQ("getaddrinfo_1000", ret, FAMILY_NOTSUPPORTED);
193 }
194 
195 /**
196  * @tc.name      : getaddrinfo_1100
197  * @tc.desc      : The parameters are invalid,hint ai_flags is AI_NUMERICHOST,
198  *                 host format is incorrect, can not resolve the host name Ip address.
199  * @tc.level     : Level 2
200  */
getaddrinfo_1100(void)201 void getaddrinfo_1100(void)
202 {
203     struct addrinfo *result, hint;
204     hint.ai_flags = AI_NUMERICHOST;
205     hint.ai_family = AF_INET6;
206     char one[300] = "fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
207     char two[] = ":fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
208     char three[] = ":fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
209     char four[] = ":fe80::bed5:4695:6cac:bef8:4695:6cac:bef8:4695:bef8:4695:6cac:bef8:4695";
210     strcat(one, two);
211     strcat(one, three);
212     strcat(one, four);
213     int ret = getaddrinfo(one, NULL, &hint, &result);
214     EXPECT_EQ("getaddrinfo_1100", ret, SERVICE_UNKNOEN);
215 }
216 
217 /**
218  * @tc.name      : getaddrinfo_1200
219  * @tc.desc      : The parameter is invalid, host is NULL, SERV is NULL, and the IP address of the
220  *                 host name can not be resolved.
221  * @tc.level     : Level 2
222  */
getaddrinfo_1200(void)223 void getaddrinfo_1200(void)
224 {
225     struct addrinfo *result, hint;
226     hint.ai_flags = AI_NUMERICHOST;
227     hint.ai_family = AF_INET6;
228     int ret = getaddrinfo(NULL, NULL, &hint, &result);
229     EXPECT_EQ("getaddrinfo_1200", ret, SERVICE_UNKNOEN);
230 }
231 
232 /**
233  * @tc.name      : getaddrinfo_1300
234  * @tc.desc      : The parameter is invalid and the IP address of the host name can not be resolved.
235  * @tc.level     : Level 2
236  */
getaddrinfo_1300(void)237 void getaddrinfo_1300(void)
238 {
239     struct addrinfo *result, hint;
240     hint.ai_flags = AI_CANONNAME;
241     hint.ai_family = AF_INET;
242     hint.ai_protocol = IPPROTO_UDP;
243     hint.ai_socktype = SOCK_STREAM;
244     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
245     EXPECT_EQ("getaddrinfo_1300", ret, SOCKTYPE_NOTSUPPORTED);
246 }
247 
248 /**
249  * @tc.name      : getaddrinfo_1400
250  * @tc.desc      : The parameter is invalid and the IP address of the host name can not be resolved.
251  * @tc.level     : Level 2
252  */
getaddrinfo_1400(void)253 void getaddrinfo_1400(void)
254 {
255     struct addrinfo *result, hint;
256     hint.ai_flags = AI_CANONNAME;
257     hint.ai_family = AF_INET;
258     hint.ai_protocol = IPPROTO_TCP;
259     hint.ai_socktype = SOCK_DGRAM;
260     int ret = getaddrinfo("127.0.0.1", NULL, &hint, &result);
261     EXPECT_EQ("getaddrinfo_1400", ret, SOCKTYPE_NOTSUPPORTED);
262 }
263 
264 /**
265  * @tc.name      : getaddrinfo_1500
266  * @tc.desc      : The parameter is invalid and the IP address of the host name can not be resolved.
267  * @tc.level     : Level 2
268  */
getaddrinfo_1500(void)269 void getaddrinfo_1500(void)
270 {
271     struct addrinfo *result, hint;
272     hint.ai_flags = AI_CANONNAME;
273     hint.ai_family = AF_INET;
274     hint.ai_socktype = SOCK_RAW;
275     int ret = getaddrinfo("127.0.0.1", "2000", &hint, &result);
276     EXPECT_EQ("getaddrinfo_1500", ret, SOCKTYPE_NOTSUPPORTED);
277 }
278 
main(int argc,char * argv[])279 int main(int argc, char *argv[])
280 {
281     getaddrinfo_0100();
282     getaddrinfo_0200();
283     getaddrinfo_0300();
284     getaddrinfo_0400();
285     getaddrinfo_0500();
286     getaddrinfo_0600();
287     getaddrinfo_0700();
288     getaddrinfo_0800();
289     getaddrinfo_0900();
290     getaddrinfo_1000();
291     getaddrinfo_1100();
292     getaddrinfo_1200();
293     getaddrinfo_1300();
294     getaddrinfo_1400();
295     getaddrinfo_1500();
296 
297     return t_status;
298 }
299