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 "functionalext.h"
18#define FTP_PORT 21
19#define HTTP_PORT 80
20#define TCPMUX_PORT 1
21#define FIDO_PORT 60179
22#define NOT_EXIST_PORT 62000
23
24/**
25 * @tc.name      : getservbyname_0100
26 * @tc.desc      : Get service entry
27 * @tc.level     : Level 0
28 */
29void getservbyname_0100(void)
30{
31    struct servent *se = getservbyname("smtp", "tcp");
32    if (se) {
33        EXPECT_STREQ("getservbyname_0100", se->s_proto, "tcp");
34    }
35
36    se = getservbyname("echo", "udp");
37    if (se) {
38        EXPECT_STREQ("getservbyname_0100", se->s_proto, "udp");
39    }
40}
41
42/**
43 * @tc.name      : getservbyname_0200
44 * @tc.desc      : Get the service information of the specified port
45 * @tc.level     : Level 0
46 */
47void getservbyname_0200(void)
48{
49    struct servent *se = getservbyname("name", "proto");
50    EXPECT_PTREQ("getservbyname_0200", se, NULL);
51}
52
53/**
54 * @tc.name      : getservbyname_0300
55 * @tc.desc      : Get the service information of the specified port
56 * @tc.level     : Level 0
57*/
58void getservbyname_0300(void)
59{
60    struct servent *se = getservbyname("ftp", "tcp");
61    EXPECT_PTRNE("getservbyname_0300", se, NULL);
62    EXPECT_EQ("getservbyname_0300", ntohs(se->s_port), FTP_PORT);
63}
64
65/**
66 * @tc.name      : getservbyname_0400
67 * @tc.desc      : Get the service information of the specified port
68 * @tc.level     : Level 0
69*/
70void getservbyname_0400(void)
71{
72    struct servent *se = getservbyname("http", "tcp");
73    EXPECT_PTRNE("getservbyname_0400", se, NULL);
74    EXPECT_EQ("getservbyname_0400", ntohs(se->s_port), HTTP_PORT);
75}
76
77/**
78 * @tc.name      : getservbyname_0500
79 * @tc.desc      : Get the service information of the specified port
80 * @tc.level     : Level 0
81*/
82void getservbyname_0500(void)
83{
84    struct servent *se = getservbyname("tcpmux", "tcp");
85    EXPECT_PTRNE("getservbyname_0500", se, NULL);
86    EXPECT_EQ("getservbyname_0500", ntohs(se->s_port), TCPMUX_PORT);
87}
88
89/**
90 * @tc.name      : getservbyname_0600
91 * @tc.desc      : Get the service information of the specified port
92 * @tc.level     : Level 0
93*/
94void getservbyname_0600(void)
95{
96    struct servent *se = getservbyname("fido", "tcp");
97    EXPECT_PTRNE("getservbyname_0600", se, NULL);
98    EXPECT_EQ("getservbyname_0600", ntohs(se->s_port), FIDO_PORT);
99}
100
101/**
102 * @tc.name      : getservbyname_0700
103 * @tc.desc      : Get the service information of the specified port
104 * @tc.level     : Level 0
105*/
106void getservbyname_0700(void)
107{
108    struct servent *se = getservbyname("not_exist_service_name", "tcp");
109    EXPECT_PTREQ("getservbyname_0700", se, NULL);
110}
111
112
113int main(void)
114{
115    getservbyname_0100();
116    getservbyname_0200();
117    getservbyname_0300();
118    getservbyname_0400();
119    getservbyname_0500();
120    getservbyname_0600();
121    getservbyname_0700();
122    return t_status;
123}
124