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 <ctype.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <stdio.h>
21 #include <sys/socket.h>
22 #include <stdbool.h>
23 #include "functionalext.h"
24
25 typedef void (*TEST_FUN)();
26
27 /**
28 * @tc.name : gethostbyname2_0100
29 * @tc.desc : Verify that host information can be obtained (all parameters are valid, fd is AF_INET)
30 * @tc.level : Level 0
31 */
gethostbyname2_0100(void)32 void gethostbyname2_0100(void)
33 {
34 bool flag = false;
35 struct hostent *hptr;
36 hptr = gethostbyname2("127.0.0.1", AF_INET);
37 if (hptr != NULL) {
38 flag = true;
39 }
40 EXPECT_TRUE("gethostbyname2_0100", flag);
41 EXPECT_STREQ("gethostbyname2_0100", hptr->h_name, "127.0.0.1");
42 }
43
44 /**
45 * @tc.name : gethostbyname2_0200
46 * @tc.desc : Verify that the host information can be obtained (all parameters are valid, fd is AF_INET6)
47 * @tc.level : Level 0
48 */
gethostbyname2_0200(void)49 void gethostbyname2_0200(void)
50 {
51 bool flag = false;
52 struct hostent *hptr;
53 hptr = gethostbyname2("::1", AF_INET6);
54 if (hptr != NULL) {
55 flag = true;
56 }
57 EXPECT_TRUE("gethostbyname2_0200", flag);
58 EXPECT_STREQ("gethostbyname2_0200", hptr->h_name, "::1");
59 }
60
61 /**
62 * @tc.name : gethostbyname2_0300
63 * @tc.desc : Verify cannot get host information (name parameter invalid)
64 * @tc.level : Level 2
65 */
gethostbyname2_0300(void)66 void gethostbyname2_0300(void)
67 {
68 bool flag = false;
69 struct hostent *hptr;
70 hptr = gethostbyname2("127.0.w.1", AF_INET6);
71 if (hptr == NULL) {
72 flag = true;
73 }
74 EXPECT_TRUE("gethostbyname2_0300", flag);
75 }
76
77 /**
78 * @tc.name : gethostbyname2_0400
79 * @tc.desc : Verify that host information cannot be obtained (fd parameter invalid)
80 * @tc.level : Level 2
81 */
gethostbyname2_0400(void)82 void gethostbyname2_0400(void)
83 {
84 bool flag = false;
85 struct hostent *hptr;
86 hptr = gethostbyname2("::1", AF_INET);
87 if (hptr == NULL) {
88 flag = true;
89 }
90 EXPECT_TRUE("gethostbyname2_0400", flag);
91 }
92
93 TEST_FUN G_Fun_Array[] = {
94 gethostbyname2_0100,
95 gethostbyname2_0200,
96 gethostbyname2_0300,
97 gethostbyname2_0400,
98 };
99
main(int argc,char * argv[])100 int main(int argc, char *argv[])
101 {
102 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
103 for (int pos = 0; pos < num; ++pos) {
104 G_Fun_Array[pos]();
105 }
106
107 return t_status;
108 }