1 /*
2 * opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "lt_net_netdb.h"
32 #include<arpa/nameser.h>
33
GetHostEntTest(void)34 static int GetHostEntTest(void)
35 {
36 char host_file[] = "127.0.0.1 localhost #localhost\n"
37 "::1 ip6-localhost\n"
38 "10.0.0.0 example example.com example.cn\n"
39 "10.0.0.0\n"
40 "10.0.0 example.com";
41 char *pathList[] = {"/etc/hosts"};
42 char *streamList[] = {static_cast<char *>(host_file)};
43 int streamLen[] = {sizeof(host_file)};
44 const int file_number = 1;
45 int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
46 if (flag != 0) {
47 RecoveryFileEnv(pathList, file_number);
48 return -1;
49 }
50
51 struct hostent *se1 = nullptr;
52 struct hostent *se2 = nullptr;
53 struct hostent *se3 = nullptr;
54 char addr[INET6_ADDRSTRLEN];
55
56 se1 = gethostent();
57 ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
58 ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, "localhost", -1);
59 ICUNIT_ASSERT_EQUAL(se1->h_addrtype, AF_INET, -1);
60 ICUNIT_ASSERT_EQUAL(se1->h_length, INADDRSZ, -1);
61 ICUNIT_ASSERT_STRING_EQUAL("127.0.0.1", inet_ntop(AF_INET, se1->h_addr_list[0], addr, INET_ADDRSTRLEN), -1);
62 ICUNIT_ASSERT_EQUAL(se1->h_aliases[0], nullptr, -1);
63
64 endhostent();
65 se2 = gethostent();
66 ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
67 ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, se2->h_name, -1);
68 ICUNIT_ASSERT_EQUAL(se1->h_addrtype, se2->h_addrtype, -1);
69 ICUNIT_ASSERT_EQUAL(se1->h_length, se2->h_length, -1);
70
71 sethostent(0);
72 se3 = gethostent();
73 ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
74 ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, se3->h_name, -1);
75 ICUNIT_ASSERT_EQUAL(se1->h_addrtype, se3->h_addrtype, -1);
76 ICUNIT_ASSERT_EQUAL(se1->h_length, se3->h_length, -1);
77
78 se3 = gethostent();
79 ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
80 ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET6, -1);
81 ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "ip6-localhost", -1);
82 ICUNIT_ASSERT_EQUAL(se3->h_length, IN6ADDRSZ, -1);
83 ICUNIT_ASSERT_STRING_EQUAL("::1", inet_ntop(AF_INET6, se3->h_addr_list[0], addr, INET6_ADDRSTRLEN), -1);
84
85 se3 = gethostent();
86 ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
87 ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET, -1);
88 ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "example", -1);
89 ICUNIT_ASSERT_STRING_EQUAL("10.0.0.0", inet_ntop(AF_INET, se3->h_addr_list[0], addr, INET_ADDRSTRLEN), -1);
90 ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[0], "example.com", -1);
91 ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[1], "example.cn", -1);
92 ICUNIT_ASSERT_EQUAL(se3->h_aliases[2], nullptr, -1);
93
94 se3 = gethostent();
95 ICUNIT_ASSERT_EQUAL(se3, nullptr, -1);
96 endhostent();
97
98 RecoveryFileEnv(pathList, file_number);
99 return ICUNIT_SUCCESS;
100 }
101
NetNetDbTest019(void)102 void NetNetDbTest019(void)
103 {
104 TEST_ADD_CASE(__FUNCTION__, GetHostEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
105 }
106