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 <arpa/inet.h>
17 #include <netdb.h>
18 #include <stdlib.h>
19 #include <sys/socket.h>
20 #include "functionalext.h"
21
22 const int INFO_RESULT = 0;
23 const int FAMILY_NOTSUPPORTED = -6;
24
25 /**
26 * @tc.name : getnameinfo_0100
27 * @tc.desc : Each parameter is valid and can resolve the IP address of the host name(hint.ai_flags =AI_ALL).
28 * @tc.level : Level 0
29 */
getnameinfo_0100()30 void getnameinfo_0100()
31 {
32 int ret;
33 char host[NI_MAXHOST];
34 char server[NI_MAXSERV];
35 struct sockaddr_in sa;
36 memset(&sa, 0, sizeof(sa));
37 sa.sin_family = AF_INET;
38 inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr.s_addr);
39 ret = getnameinfo((const struct sockaddr *)(&sa), sizeof(sa), host, sizeof(host), server, sizeof(server), 0);
40 EXPECT_EQ("getnameinfo_0100", ret, INFO_RESULT);
41 EXPECT_NE("getnameinfo_0100", strlen(host), 0);
42 EXPECT_NE("getnameinfo_0100", strlen(server), 0);
43 }
44
45 /**
46 * @tc.name : getnameinfo_0200
47 * @tc.desc : Each parameter is valid and can resolve the IP address of the host name(hint.ai_flags =AI_ALL).
48 * @tc.level : Level 0
49 */
getnameinfo_0200()50 void getnameinfo_0200()
51 {
52 int ret = -1;
53 int num = -1;
54 int res = -1;
55 struct addrinfo *result, hint;
56 hint.ai_flags = AI_NUMERICHOST;
57 hint.ai_family = AF_INET6;
58 char buf[] = "fe80::bed5:4695:6cac:bef8";
59 ret = getaddrinfo(buf, NULL, &hint, &result);
60 char host[NI_MAXHOST];
61 char server[NI_MAXSERV];
62 if (ret == 0) {
63 res = getnameinfo(result->ai_addr, result->ai_addrlen, host, sizeof(host), server, sizeof(server), 0);
64 }
65 EXPECT_EQ("getnameinfo_0200", res, INFO_RESULT);
66 EXPECT_NE("getnameinfo_0200", strlen(host), 0);
67 EXPECT_NE("getnameinfo_0200", strlen(server), 0);
68 freeaddrinfo(result);
69 result = NULL;
70 }
71
72 /**
73 * @tc.name : getnameinfo_0300
74 * @tc.desc : Each parameter is valid and can resolve the IP address of the host name(hint.ai_flags =AI_ALL).
75 * @tc.level : Level 1
76 */
getnameinfo_0300()77 void getnameinfo_0300()
78 {
79 int ret;
80 char host[NI_MAXHOST];
81 char server[NI_MAXSERV];
82 struct sockaddr_in sa;
83 memset(&sa, 0, sizeof(sa));
84 sa.sin_family = AF_INET;
85 inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr.s_addr);
86 ret = getnameinfo((const struct sockaddr *)(&sa), sizeof(sa), 0, 0, server, sizeof(server), 0);
87 EXPECT_EQ("getnameinfo_0300", ret, INFO_RESULT);
88 EXPECT_NE("getnameinfo_0300", strlen(server), 0);
89 }
90
91 /**
92 * @tc.name : getnameinfo_0400
93 * @tc.desc : Each parameter is valid and can resolve the IP address of the host name(hint.ai_flags =AI_ALL).
94 * @tc.level : Level 1
95 */
getnameinfo_0400()96 void getnameinfo_0400()
97 {
98 int ret;
99 char host[NI_MAXHOST];
100 char server[NI_MAXSERV];
101 struct sockaddr_in sa;
102 memset(&sa, 0, sizeof(sa));
103 sa.sin_family = AF_INET;
104 inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr.s_addr);
105 ret = getnameinfo((const struct sockaddr *)(&sa), sizeof(sa), host, sizeof(host), 0, 0, 0);
106 EXPECT_EQ("getnameinfo_0400", ret, INFO_RESULT);
107 EXPECT_NE("getnameinfo_0400", strlen(host), 0);
108 }
109 /**
110 * @tc.name : getnameinfo_0500
111 * @tc.desc : Each parameter is valid and can resolve the IP address of the host name(hint.ai_flags =AI_ALL).
112 * @tc.level : Level 2
113 */
getnameinfo_0500()114 void getnameinfo_0500()
115 {
116 int ret;
117 char host[NI_MAXHOST];
118 char server[NI_MAXSERV];
119 struct sockaddr_in sa;
120 memset(&sa, 0, sizeof(sa));
121 sa.sin_family = AF_INET;
122 inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr.s_addr);
123 ret = getnameinfo((const struct sockaddr *)(&sa), 4, host, sizeof(host), server, sizeof(server), 0);
124 EXPECT_EQ("getnameinfo_0500", ret, FAMILY_NOTSUPPORTED);
125 }
126
127 /**
128 * @tc.name : getnameinfo_0600
129 * @tc.desc : Each parameter is valid and can resolve the IP address of the host name(hint.ai_flags =AI_ALL).
130 * @tc.level : Level 2
131 */
getnameinfo_0600()132 void getnameinfo_0600()
133 {
134 int ret = -1;
135 int num = -1;
136 int res = -1;
137 struct addrinfo *result, hint;
138 hint.ai_flags = AI_NUMERICHOST;
139 hint.ai_family = AF_INET6;
140 char buf[] = "fe80::bed5:4695:6cac:bef8";
141 ret = getaddrinfo(buf, NULL, &hint, &result);
142 char host[NI_MAXHOST];
143 char server[NI_MAXSERV];
144 if (ret == 0) {
145 res = getnameinfo(result->ai_addr, 4, host, sizeof(host), server, sizeof(server), 0);
146 }
147 EXPECT_EQ("getnameinfo_0600", res, FAMILY_NOTSUPPORTED);
148 freeaddrinfo(result);
149 result = NULL;
150 }
151
152 /**
153 * @tc.name : getnameinfo_0700
154 * @tc.desc : Each parameter is valid, sa->sa_family is AF_LOCAL, the information of the host cannot be obtained.
155 * @tc.level : Level 2
156 */
getnameinfo_0700()157 void getnameinfo_0700()
158 {
159 int ret;
160 char host[NI_MAXHOST];
161 char server[NI_MAXSERV];
162 struct sockaddr_in sa;
163 memset(&sa, 0, sizeof(sa));
164 sa.sin_family = AF_LOCAL;
165 ret = getnameinfo((const struct sockaddr *)(&sa), sizeof(sa), host, sizeof(host), server, sizeof(server), 0);
166 EXPECT_EQ("getnameinfo_0700", ret, FAMILY_NOTSUPPORTED);
167 }
168
main(int argc,char * argv[])169 int main(int argc, char *argv[])
170 {
171 getnameinfo_0100();
172 getnameinfo_0200();
173 getnameinfo_0300();
174 getnameinfo_0400();
175 getnameinfo_0500();
176 getnameinfo_0600();
177 getnameinfo_0700();
178 return t_status;
179 }
180