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 <arpa/inet.h>
18 #include <sys/socket.h>
19 #include "functionalext.h"
20
21 /**
22 * @tc.name : gethostbyaddre_0100
23 * @tc.desc : Can get host information through an IPv4 address.
24 * @tc.level : Level 0
25 */
gethostbyaddre_0100(void)26 void gethostbyaddre_0100(void)
27 {
28 char *ptr = "127.0.0.1";
29 struct hostent *hptr = NULL;
30 struct in_addr addr;
31 if (inet_pton(AF_INET, ptr, &addr) <= 0) {
32 t_error("%s inet_pton error:%s\n", __func__, strerror(errno));
33 }
34 hptr = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
35 if (hptr == NULL) {
36 t_error("%s gethostbyaddr error:%s\n", __func__, strerror(h_errno));
37 }
38 EXPECT_TRUE("gethostbyaddre_0100", hptr != NULL);
39 EXPECT_STREQ("gethostbyaddre_0100", hptr->h_name, "localhost");
40 }
41
42 /**
43 * @tc.name : gethostbyaddre_0200
44 * @tc.desc : Can obtain host information through an IPv6 address.
45 * @tc.level : Level 0
46 */
gethostbyaddre_0200(void)47 void gethostbyaddre_0200(void)
48 {
49 char *ptr = "fe80::bed5:4695:6cac:bef8";
50 struct hostent *hptr = NULL;
51 struct in_addr addr;
52 addr.s_addr = inet_addr(ptr);
53 hptr = gethostbyaddr((void *)&addr.s_addr, 16, AF_INET6);
54 EXPECT_TRUE("gethostbyaddre_0200", hptr != NULL);
55 }
56
main(int argc,char * argv[])57 int main(int argc, char *argv[])
58 {
59 gethostbyaddre_0100();
60 gethostbyaddre_0200();
61 return t_status;
62 }
63