• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetServEntTest(void)33 static int GetServEntTest(void)
34 {
35     char serv_file[] = "tcpmux		1/tcp    # TCP port service multiplexer\n"
36                        "echo		7/tcp\n"
37                        "echo		7/udp\n"
38                        "discard		9/tcp		sink null\n"
39                        "ssh         100000/tcp\n"
40                        "ssh         /tcp\n"
41                        "ssh         22/";
42     char *pathList[] = {"/etc/services"};
43     char *streamList[] = {static_cast<char *>(serv_file)};
44     int streamLen[] = {sizeof(serv_file)};
45     const int file_number = 1;
46     int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
47     if (flag != 0) {
48         RecoveryFileEnv(pathList, file_number);
49         return -1;
50     }
51 
52     /* tcpmux,echo,discard port number is 1,7,9 */
53     const int tcpmux_port_no  = 1;
54     const int echo_port_no    = 7;
55     const int discard_port_no = 9;
56 
57     struct servent *se1 = nullptr;
58     struct servent *se2 = nullptr;
59     struct servent *se3 = nullptr;
60 
61     se1 = getservent();
62     ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
63     ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "tcpmux", -1);
64     ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1);
65     ICUNIT_ASSERT_EQUAL(se1->s_port, ntohs(tcpmux_port_no), -1);
66 
67     endservent();
68     se2 = getservent();
69     ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
70     ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, se2->s_name, -1);
71     ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, se2->s_proto, -1);
72     ICUNIT_ASSERT_EQUAL(se1->s_port, se2->s_port, -1);
73 
74     setservent(0);
75     se3 = getservent();
76     ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
77     ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, se3->s_name, -1);
78     ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, se3->s_proto, -1);
79     ICUNIT_ASSERT_EQUAL(se1->s_port, se3->s_port, -1);
80 
81     se3 = getservent();
82     ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
83     ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1);
84     ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1);
85     ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(echo_port_no), -1);
86 
87     se3 = getservent();
88     ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
89     ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1);
90     ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "udp", -1);
91     ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(echo_port_no), -1);
92 
93     se3 = getservent();
94     ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
95     ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "discard", -1);
96     ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1);
97     ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(discard_port_no), -1);
98     ICUNIT_ASSERT_STRING_EQUAL(se3->s_aliases[0], "sink", -1);
99     ICUNIT_ASSERT_STRING_EQUAL(se3->s_aliases[1], "null", -1);
100     ICUNIT_ASSERT_EQUAL(se3->s_aliases[2], nullptr, -1);
101 
102     se3 = getservent();
103     ICUNIT_ASSERT_EQUAL(se3, nullptr, -1);
104     endservent();
105 
106     RecoveryFileEnv(pathList, file_number);
107     return ICUNIT_SUCCESS;
108 }
109 
NetNetDbTest018(void)110 void NetNetDbTest018(void)
111 {
112     TEST_ADD_CASE(__FUNCTION__, GetServEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
113 }
114