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 <netinet/in.h>
19 #include <sys/socket.h>
20 #include "functionalext.h"
21
22 /**
23 * @tc.name : gethostbyname2_r_0100
24 * @tc.desc : The parameter fd is AF_INET, and multiple threads simultaneously obtain host information.
25 * @tc.level : Level 0
26 */
gethostbyname2_r_0100(void)27 void gethostbyname2_r_0100(void)
28 {
29 struct hostent h, *res = NULL;
30 char buf[8192] = {0};
31 int err = 0;
32
33 char *name = "127.0.0.1";
34 int ret = gethostbyname2_r(name, AF_INET, &h, buf, sizeof(buf), &res, &err);
35 EXPECT_EQ("gethostbyname2_r_0100", ret, 0);
36 EXPECT_STREQ("gethostbyname2_r_0100", h.h_name, "127.0.0.1");
37 }
38
39 /**
40 * @tc.name : gethostbyname2_r_0200
41 * @tc.desc : The parameter fd is AF_INET6, and multiple threads simultaneously obtain host information.
42 * @tc.level : Level 0
43 */
gethostbyname2_r_0200(void)44 void gethostbyname2_r_0200(void)
45 {
46 struct hostent h, *res = NULL;
47 char buf[8192] = {0};
48 int err = 0;
49
50 char *name = "::1";
51 int ret = gethostbyname2_r(name, AF_INET6, &h, buf, sizeof(buf), &res, &err);
52 EXPECT_EQ("gethostbyname2_r_0200", ret, 0);
53 EXPECT_TRUE("gethostbyname2_r_0200", strcmp(h.h_name, "::1") == 0);
54 }
55
56 /**
57 * @tc.name : gethostbyname2_r_0300
58 * @tc.desc : The parameter name is invalid, the host information cannot be obtained.
59 * @tc.level : Level 2
60 */
gethostbyname2_r_0300(void)61 void gethostbyname2_r_0300(void)
62 {
63 struct hostent h, *res = NULL;
64 char buf[8192] = {0};
65 int err = 0;
66 char *name = "aaaa";
67 int ret = gethostbyname2_r(name, AF_INET, &h, buf, sizeof(buf), &res, &err);
68 EXPECT_NE("gethostbyname2_r_0300", ret, 0);
69 }
70
71 /**
72 * @tc.name : gethostbyname2_r_0400
73 * @tc.desc : The parameter fd is invalid, the host information cannot be obtained.
74 * @tc.level : Level 2
75 */
gethostbyname2_r_0400(void)76 void gethostbyname2_r_0400(void)
77 {
78 struct hostent h, *res = NULL;
79 char buf[8192] = {0};
80 int err = 0;
81 char *name = "localhost";
82 int ret = gethostbyname2_r(name, 0, &h, buf, sizeof(buf), &res, &err);
83 EXPECT_EQ("gethostbyname2_r_0400", ret, 0);
84 EXPECT_STREQ("gethostbyname2_r_0400", "localhost", h.h_name);
85 }
86
main(int argc,char * argv[])87 int main(int argc, char *argv[])
88 {
89 gethostbyname2_r_0100();
90 gethostbyname2_r_0200();
91 gethostbyname2_r_0300();
92 gethostbyname2_r_0400();
93 return t_status;
94 }
95