• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <gtest/gtest.h>
3 #include <netdb.h>
4 #include <string.h>
5 #include <vector>
6 using namespace testing::ext;
7 
8 class NetdbTest : public testing::Test {
SetUp()9     void SetUp() override {}
TearDown()10     void TearDown() override {}
11 };
12 
13 /**
14  * @tc.name: getaddrinfo_001
15  * @tc.desc: Verify that the "getaddrinfo" function correctly translates the "localhost" host name into socket
16  *           addresses
17  * @tc.type: FUNC
18  **/
19 HWTEST_F(NetdbTest, getaddrinfo_001, TestSize.Level1)
20 {
21     const char* host = "localhost";
22     const char* serv = nullptr;
23     addrinfo hints;
24     addrinfo* addr = nullptr;
25     int result = getaddrinfo(host, serv, &hints, &addr);
26     EXPECT_EQ(0, result);
27     ASSERT_NE(addr, nullptr);
28     freeaddrinfo(addr);
29 }
30 
31 /**
32  * @tc.name: getaddrinfo_002
33  * @tc.desc: Verify that the "getaddrinfo" function correctly translates the "localhost" host name and "9999"
34  *           service name into socket addresses for both TCP and UDP protocols
35  * @tc.type: FUNC
36  **/
37 HWTEST_F(NetdbTest, getaddrinfo_002, TestSize.Level1)
38 {
39     const char* host = "localhost";
40     const char* serv = "9999";
41     addrinfo hints;
42     addrinfo* addr = nullptr;
43     EXPECT_EQ(0, getaddrinfo(host, serv, &hints, &addr));
44 
45     bool tcp = false;
46     bool udp = false;
47 
48     for (auto p = addr; p != nullptr; p = p->ai_next) {
49         EXPECT_TRUE(p->ai_family == AF_INET || p->ai_family == AF_INET6);
50         if (p->ai_socktype != SOCK_STREAM && p->ai_socktype != SOCK_DGRAM) {
51             continue;
52         } else if (p->ai_socktype == SOCK_STREAM && IPPROTO_TCP == p->ai_protocol) {
53             tcp = true;
54         } else if (p->ai_socktype == SOCK_DGRAM && IPPROTO_UDP == p->ai_protocol) {
55             udp = true;
56         }
57     }
58 
59     EXPECT_TRUE(tcp);
60     EXPECT_TRUE(udp);
61 
62     freeaddrinfo(addr);
63 }
64 
65 /**
66  * @tc.name: getaddrinfo_003
67  * @tc.desc: Verify the correct behavior of the getaddrinfo function in resolving network addresses for
68  *           a given host and service.
69  * @tc.type: FUNC
70  **/
71 HWTEST_F(NetdbTest, getaddrinfo_003, TestSize.Level1)
72 {
73     const char* host = "localhost";
74     const char* service = "9999";
75     addrinfo hints;
76     memset(&hints, 0, sizeof(hints));
77     hints.ai_family = AF_INET;
78     hints.ai_socktype = SOCK_STREAM;
79     hints.ai_protocol = IPPROTO_TCP;
80     addrinfo* addr = nullptr;
81     int result = getaddrinfo(host, service, &hints, &addr);
82     EXPECT_EQ(0, result);
83     ASSERT_NE(addr, nullptr);
84     for (addrinfo* cur = addr; cur != nullptr; cur = cur->ai_next) {
85         EXPECT_EQ(AF_INET, cur->ai_family);
86         EXPECT_EQ(SOCK_STREAM, cur->ai_socktype);
87         EXPECT_EQ(IPPROTO_TCP, cur->ai_protocol);
88     }
89     freeaddrinfo(addr);
90 }
91 
92 /**
93  * @tc.name: getaddrinfo_004
94  * @tc.desc: Verify the correct behavior of the getaddrinfo function when resolving IPv6 addresses for a given host.
95  * @tc.type: FUNC
96  **/
97 HWTEST_F(NetdbTest, getaddrinfo_004, TestSize.Level1)
98 {
99     const char* host = "ip6-localhost";
100     const char* service = nullptr;
101     addrinfo hints;
102     memset(&hints, 0, sizeof(struct addrinfo));
103     hints.ai_family = AF_INET6;
104     addrinfo* addr;
105     int ret = getaddrinfo(host, service, &hints, &addr);
106     EXPECT_EQ(ret, 0);
107     ASSERT_NE(addr, nullptr);
108 
109     struct sockaddr_in6* addr6 = reinterpret_cast<struct sockaddr_in6*>(addr->ai_addr);
110     EXPECT_EQ(addr6->sin6_family, AF_INET6);
111     EXPECT_EQ(memcmp(&(addr6->sin6_addr), &in6addr_loopback, sizeof(struct in6_addr)), 0);
112 
113     freeaddrinfo(addr);
114 }
115 
116 /**
117  * @tc.name: getnameinfo_001
118  * @tc.desc: Verify the basic functionality of getnameinfo for retrieving the textual representation of an IPv4 address.
119  * @tc.type: FUNC
120  **/
121 HWTEST_F(NetdbTest, getnameinfo_001, TestSize.Level1)
122 {
123     sockaddr_storage addr;
124     memset(&addr, 0, sizeof(addr));
125     sockaddr* sa = reinterpret_cast<sockaddr*>(&addr);
126     char node[BUFSIZ];
127     memset(&node, 0, sizeof(node));
128     addr.ss_family = AF_INET;
129     socklen_t plumb = sizeof(sockaddr_in);
130     int result = getnameinfo(sa, plumb, node, sizeof(node), nullptr, 0, NI_NUMERICHOST);
131     EXPECT_EQ(0, result);
132     EXPECT_STREQ("0.0.0.0", node);
133 }
134 
135 /**
136  * @tc.name: getnameinfo_002
137  * @tc.desc: Verify that the getnameinfo() function properly converts an IPv4 address to its numeric form and stores
138  *           it in the "node" buffer. In this case, the expected result is "0.0.0.0".
139  * @tc.type: FUNC
140  **/
141 HWTEST_F(NetdbTest, getnameinfo_002, TestSize.Level1)
142 {
143     sockaddr_storage addr;
144     memset(&addr, 0, sizeof(addr));
145     sockaddr* sa = reinterpret_cast<sockaddr*>(&addr);
146     char node[BUFSIZ];
147     memset(&node, 0, sizeof(node));
148     addr.ss_family = AF_INET;
149     socklen_t more = sizeof(addr);
150     int result = getnameinfo(sa, more, node, sizeof(node), nullptr, 0, NI_NUMERICHOST);
151     EXPECT_EQ(0, result);
152     EXPECT_STREQ("0.0.0.0", node);
153 }
154 
155 /**
156  * @tc.name: getnameinfo_003
157  * @tc.desc: Verify that the getnameinfo() function returns the expected error code (EAI_FAMILY) when the length
158  *           argument is smaller than the size of the sockaddr structure.
159  * @tc.type: FUNC
160  **/
161 HWTEST_F(NetdbTest, getnameinfo_003, TestSize.Level1)
162 {
163     sockaddr_storage addr;
164     memset(&addr, 0, sizeof(addr));
165     sockaddr* sa = reinterpret_cast<sockaddr*>(&addr);
166     char node[BUFSIZ];
167     memset(&node, 0, sizeof(node));
168     addr.ss_family = AF_INET;
169     socklen_t less = sizeof(sockaddr_in) - 1;
170     int result = getnameinfo(sa, less, node, sizeof(node), nullptr, 0, NI_NUMERICHOST);
171     EXPECT_EQ(EAI_FAMILY, result);
172 }
173 
174 /**
175  * @tc.name: getnameinfo_004
176  * @tc.desc: Verify that the getnameinfo() function properly converts an IPv6 address to its numeric form and stores
177  *           it in the "node" buffer. In this case, the expected result is "::".
178  * @tc.type: FUNC
179  **/
180 HWTEST_F(NetdbTest, getnameinfo_004, TestSize.Level1)
181 {
182     sockaddr_storage addr;
183     memset(&addr, 0, sizeof(addr));
184     sockaddr* sa = reinterpret_cast<sockaddr*>(&addr);
185     char node[BUFSIZ];
186     memset(&node, 0, sizeof(node));
187     addr.ss_family = AF_INET6;
188     socklen_t plumb = sizeof(sockaddr_in6);
189     int result = getnameinfo(sa, plumb, node, sizeof(node), nullptr, 0, NI_NUMERICHOST);
190     EXPECT_EQ(0, result);
191     EXPECT_STREQ("::", node);
192 }
193 
194 /**
195  * @tc.name: getnameinfo_005
196  * @tc.desc: Verify that the getnameinfo() function properly converts an IPv6 address to its numeric form and stores
197  *           it in the "node" buffer. In this case, the expected result is "::".
198  * @tc.type: FUNC
199  **/
200 HWTEST_F(NetdbTest, getnameinfo_005, TestSize.Level1)
201 {
202     sockaddr_storage addr;
203     memset(&addr, 0, sizeof(addr));
204     sockaddr* sa = reinterpret_cast<sockaddr*>(&addr);
205     char node[BUFSIZ];
206     memset(&node, 0, sizeof(node));
207     addr.ss_family = AF_INET6;
208     socklen_t more = sizeof(sockaddr_in6) + 1;
209     int result = getnameinfo(sa, more, node, sizeof(node), nullptr, 0, NI_NUMERICHOST);
210     EXPECT_EQ(0, result);
211     EXPECT_STREQ("::", node);
212 }
213 
214 /**
215  * @tc.name: getnameinfo_006
216  * @tc.desc: Verify that the getnameinfo() function correctly handles the scenario where the length argument
217  *           provided is smaller than the actual size of the sockaddr structure.
218  * @tc.type: FUNC
219  **/
220 HWTEST_F(NetdbTest, getnameinfo_006, TestSize.Level1)
221 {
222     sockaddr_storage addr;
223     memset(&addr, 0, sizeof(addr));
224     sockaddr* sa = reinterpret_cast<sockaddr*>(&addr);
225     char node[BUFSIZ];
226     memset(&node, 0, sizeof(node));
227     addr.ss_family = AF_INET6;
228     socklen_t less = sizeof(sockaddr_in6) - 1;
229     int result = getnameinfo(sa, less, node, sizeof(node), nullptr, 0, NI_NUMERICHOST);
230     EXPECT_EQ(EAI_FAMILY, result);
231 }
232 
233 /**
234  * @tc.name: getnameinfo_007
235  * @tc.desc: Verify the getnameinfo function's ability to retrieve the hostname associated with a
236  *           specific IPv4 address.
237  * @tc.type: FUNC
238  **/
239 HWTEST_F(NetdbTest, getnameinfo_007, TestSize.Level1)
240 {
241     sockaddr_in addr;
242     memset(&addr, 0, sizeof(sockaddr_in));
243     addr.sin_family = AF_INET;
244     addr.sin_addr.s_addr = htonl(0x7f000001);
245 
246     char host[NI_MAXHOST];
247     memset(&host, 0, sizeof(host));
248     int result = getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr), host, sizeof(host), nullptr, 0, 0);
249     EXPECT_EQ(0, result);
250     EXPECT_STREQ(host, "localhost");
251 }
252 
253 /**
254  * @tc.name: getnameinfo_008
255  * @tc.desc: Verify the getnameinfo function's ability to retrieve the hostname associated with
256  *           the IPv6 loopback address.
257  * @tc.type: FUNC
258  **/
259 HWTEST_F(NetdbTest, getnameinfo_008, TestSize.Level1)
260 {
261     sockaddr_in6 addr = { .sin6_family = AF_INET6, .sin6_addr = in6addr_loopback };
262     char host[NI_MAXHOST];
263     memset(&host, 0, sizeof(host));
264     int result = getnameinfo(reinterpret_cast<sockaddr*>(&addr), sizeof(addr), host, sizeof(host), nullptr, 0, 0);
265     EXPECT_EQ(0, result);
266 }
267 
268 /**
269  * @tc.name: gethostbyname_001
270  * @tc.desc: Verify the gethostbyname function's ability to retrieve host information for the "localhost" hostname.
271  * @tc.type: FUNC
272  **/
273 HWTEST_F(NetdbTest, gethostbyname_001, TestSize.Level1)
274 {
275     hostent* hptr = gethostbyname("localhost");
276     ASSERT_NE(hptr, nullptr);
277     EXPECT_EQ(hptr->h_addrtype, AF_INET);
278 
279     in_addr hostAddr;
280     hostAddr.s_addr = htonl(INADDR_LOOPBACK);
281     EXPECT_TRUE(memcmp(hptr->h_addr, &hostAddr, sizeof(in_addr)) == 0);
282 }
283 
284 /**
285  * @tc.name: gethostbyname_r_001
286  * @tc.desc: Verify the thread-safe version of the gethostbyname function's ability to retrieve
287  *           host information for the "localhost" hostname.
288  * @tc.type: FUNC
289  **/
290 HWTEST_F(NetdbTest, gethostbyname_r_001, TestSize.Level1)
291 {
292     hostent ht;
293     hostent* hptr1 = nullptr;
294     char buf[BUFSIZ];
295     memset(&buf, 0, sizeof(buf));
296     int err;
297     int result = gethostbyname_r("localhost", &ht, buf, sizeof(buf), &hptr1, &err);
298     EXPECT_EQ(0, err);
299     EXPECT_EQ(0, result);
300     ASSERT_NE(hptr1, nullptr);
301     EXPECT_EQ(hptr1->h_addrtype, AF_INET);
302 
303     in_addr hostAddr1;
304     hostAddr1.s_addr = htonl(INADDR_LOOPBACK);
305     EXPECT_TRUE(memcmp(hptr1->h_addr, &hostAddr1, sizeof(in_addr)) == 0);
306     hptr1->h_addr_list[0][0] = 2;
307 
308     hostent ht2;
309     hostent* hptr2 = nullptr;
310     char buf2[BUFSIZ];
311     memset(&buf2, 0, sizeof(buf2));
312     result = gethostbyname_r("localhost", &ht2, buf2, sizeof(buf2), &hptr2, &err);
313     EXPECT_EQ(0, err);
314     EXPECT_EQ(0, result);
315     ASSERT_NE(hptr2, nullptr);
316     EXPECT_EQ(hptr2->h_addrtype, AF_INET);
317 
318     in_addr hostAddr2;
319     hostAddr2.s_addr = htonl(INADDR_LOOPBACK);
320     EXPECT_TRUE(memcmp(hptr2->h_addr, &hostAddr2, sizeof(in_addr)) == 0);
321     EXPECT_EQ(2, hptr1->h_addr_list[0][0]);
322 }
323 
324 /**
325  * @tc.name: gethostbyname_r_002
326  * @tc.desc: Verify the thread-safe version of the gethostbyname function's ability to handle non-existing hostnames.
327  * @tc.type: FUNC
328  **/
329 HWTEST_F(NetdbTest, gethostbyname_r_002, TestSize.Level1)
330 {
331     hostent ht;
332     char buf[BUFSIZ];
333     memset(&buf, 0, sizeof(buf));
334     hostent* hptr1 = nullptr;
335     size_t buflen = sizeof(buf);
336 
337     int err;
338     int result = gethostbyname_r("musl.not.exist", &ht, buf, buflen, &hptr1, &err);
339     EXPECT_EQ(HOST_NOT_FOUND, err);
340     EXPECT_NE(0, result);
341     EXPECT_EQ(nullptr, hptr1);
342 }
343 
344 /**
345  * @tc.name: gethostbyname2_001
346  * @tc.desc: Verify the gethostbyname2 function's ability to retrieve host information for the "localhost"
347  *           hostname and AF_INET address family.
348  * @tc.type: FUNC
349  **/
350 HWTEST_F(NetdbTest, gethostbyname2_001, TestSize.Level1)
351 {
352     hostent* hptr = gethostbyname2("localhost", AF_INET);
353     ASSERT_NE(hptr, nullptr);
354     EXPECT_EQ(hptr->h_addrtype, AF_INET);
355 
356     in_addr hostAddr;
357     hostAddr.s_addr = htonl(INADDR_LOOPBACK);
358     EXPECT_TRUE(memcmp(hptr->h_addr, &hostAddr, sizeof(in_addr)) == 0);
359 }
360 
361 /**
362  * @tc.name: gethostbyname2_r_001
363  * @tc.desc: Verify the thread-safe version of the gethostbyname2 function's ability to handle insufficient
364  *           buffer sizes when retrieving host information.
365  * @tc.type: FUNC
366  **/
367 HWTEST_F(NetdbTest, gethostbyname2_r_001, TestSize.Level1)
368 {
369     hostent ht;
370     hostent* hptr = nullptr;
371     char buf[BUFSIZ];
372     memset(&buf, 0, sizeof(buf));
373     size_t buflen = sizeof(buf);
374     int err;
375     int result = gethostbyname2_r("localhost", AF_INET, &ht, buf, buflen, &hptr, &err);
376     EXPECT_EQ(0, result);
377     EXPECT_NE(nullptr, hptr);
378     EXPECT_EQ(0, err);
379 }
380 
381 /**
382  * @tc.name: gethostbyname2_r_002
383  * @tc.desc: Verify the thread-safe version of the gethostbyname2 function's ability to handle non-existing hostnames.
384  * @tc.type: FUNC
385  **/
386 HWTEST_F(NetdbTest, gethostbyname2_r_002, TestSize.Level1)
387 {
388     hostent ht;
389     hostent* hptr = nullptr;
390     char buf[BUFSIZ];
391     memset(&buf, 0, sizeof(buf));
392     size_t buflen = sizeof(buf);
393     int err;
394     int result = gethostbyname2_r("musl.not.exist", AF_INET, &ht, buf, buflen, &hptr, &err);
395     EXPECT_EQ(HOST_NOT_FOUND, err);
396     EXPECT_NE(0, result);
397     EXPECT_EQ(nullptr, hptr);
398 }
399 
400 /**
401  * @tc.name: gethostbyaddr_001
402  * @tc.desc: Verify the ability of the gethostbyaddr function to retrieve host information based on an IPv4 address.
403  * @tc.type: FUNC
404  **/
405 HWTEST_F(NetdbTest, gethostbyaddr_001, TestSize.Level1)
406 {
407     in_addr addr = { htonl(0x7f000001) };
408     hostent* hptr = gethostbyaddr(&addr, sizeof(addr), AF_INET);
409     ASSERT_NE(hptr, nullptr);
410     EXPECT_EQ(hptr->h_addrtype, AF_INET);
411 
412     in_addr hostAddr;
413     hostAddr.s_addr = htonl(INADDR_LOOPBACK);
414     EXPECT_TRUE(memcmp(hptr->h_addr, &hostAddr, sizeof(in_addr)) == 0);
415 }
416 
417 /**
418  * @tc.name: gethostbyaddr_r_001
419  * @tc.desc: The purpose of this test case is to validate the functionality of the gethostbyaddr_r function in
420  *           retrieving host information based on an IP address, and to verify its thread safety
421  * @tc.type: FUNC
422  **/
423 HWTEST_F(NetdbTest, gethostbyaddr_r_001, TestSize.Level1)
424 {
425     in_addr addr = { htonl(0x7f000001) };
426     hostent ht;
427     hostent* hptr1 = nullptr;
428     char buf[BUFSIZ];
429     memset(&buf, 0, sizeof(buf));
430     int err;
431     int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &ht, buf, sizeof(buf), &hptr1, &err);
432     EXPECT_EQ(0, result);
433     EXPECT_EQ(0, err);
434     ASSERT_NE(hptr1, nullptr);
435     EXPECT_EQ(hptr1->h_addrtype, AF_INET);
436 
437     in_addr hostAddr1;
438     hostAddr1.s_addr = htonl(INADDR_LOOPBACK);
439     EXPECT_TRUE(memcmp(hptr1->h_addr, &hostAddr1, sizeof(in_addr)) == 0);
440     hptr1->h_addr_list[0][0] = 2;
441 
442     hostent ht2;
443     hostent* hptr2 = nullptr;
444     char buf2[BUFSIZ];
445     memset(&buf2, 0, sizeof(buf2));
446     result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &ht2, buf2, sizeof(buf2), &hptr2, &err);
447     EXPECT_EQ(0, err);
448     EXPECT_EQ(0, result);
449     ASSERT_NE(hptr2, nullptr);
450     EXPECT_EQ(hptr2->h_addrtype, AF_INET);
451 
452     in_addr hostAddr2;
453     hostAddr2.s_addr = htonl(INADDR_LOOPBACK);
454     EXPECT_TRUE(memcmp(hptr2->h_addr, &hostAddr2, sizeof(in_addr)) == 0);
455     EXPECT_EQ(2, hptr1->h_addr_list[0][0]);
456 }
457 
458 /**
459  * @tc.name: getnetbyaddr_001
460  * @tc.desc: Verify that the "getnetbyaddr" function handles the case of an invalid or non-existent IP address
461  *           gracefully and returns nullptr when no network information is available.
462  * @tc.type: FUNC
463  **/
464 HWTEST_F(NetdbTest, getnetbyaddr_001, TestSize.Level1)
465 {
466     EXPECT_EQ(nullptr, getnetbyaddr(0, 0));
467 }
468 
469 /**
470  * @tc.name: getnetbyname_001
471  * @tc.desc: Verify that the "getnetbyname" function handles the case of an invalid or non-existent network name
472  *           gracefully and returns nullptr when no network information is available.
473  * @tc.type: FUNC
474  **/
475 HWTEST_F(NetdbTest, getnetbyname_001, TestSize.Level1)
476 {
477     EXPECT_EQ(nullptr, getnetbyname("z"));
478 }