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
19 #define TEST_PROTO_NUM 6
20 #define TEST_INVALID_NUMBER 10240
21 #define TEST_TERMINATE_SIZE 2
22
23 static int test_index = 0;
24 static const unsigned char test_protos[] = {"\000ip\0"
25 "\001icmp\0"
26 "\002igmp\0"
27 "\003ggp\0"
28 "\004ipencap\0"
29 "\005st\0"
30 "\006tcp\0"
31 "\010egp\0"
32 "\014pup\0"
33 "\021udp\0"
34 "\024hmp\0"
35 "\026xns-idp\0"
36 "\033rdp\0"
37 "\035iso-tp4\0"
38 "\044xtp\0"
39 "\045ddp\0"
40 "\046idpr-cmtp\0"
41 "\051ipv6\0"
42 "\053ipv6-route\0"
43 "\054ipv6-frag\0"
44 "\055idrp\0"
45 "\056rsvp\0"
46 "\057gre\0"
47 "\062esp\0"
48 "\063ah\0"
49 "\071skip\0"
50 "\072ipv6-icmp\0"
51 "\073ipv6-nonxt\0"
52 "\074ipv6-opts\0"
53 "\111rspf\0"
54 "\121vmtp\0"
55 "\131ospf\0"
56 "\136ipip\0"
57 "\142encap\0"
58 "\147pim\0"
59 "\377raw"};
60
test_getprotoent(void)61 struct protoent *test_getprotoent(void)
62 {
63 static struct protoent p;
64 if (test_index >= sizeof(test_protos)) {
65 return NULL;
66 }
67 p.p_proto = test_protos[test_index];
68 p.p_name = (char *)&test_protos[test_index + 1];
69 test_index += strlen(p.p_name) + TEST_TERMINATE_SIZE;
70 return &p;
71 }
72
test_resetprotoent(void)73 void test_resetprotoent(void)
74 {
75 test_index = 0;
76 setprotoent(0);
77 endprotoent();
78 }
79
80 /**
81 * @tc.name : getprotobyname_0100
82 * @tc.desc : Get the specified protocol name by name
83 * @tc.level : Level 0
84 */
getprotobyname_0100(void)85 void getprotobyname_0100(void)
86 {
87 struct protoent *ret = getprotobyname("tcp");
88 EXPECT_PTRNE("getprotobyname_0100", ret, NULL);
89 if (ret) {
90 EXPECT_STREQ("getprotobyname_0100", ret->p_name, "tcp");
91 EXPECT_EQ("getprotobyname_0100", ret->p_proto, TEST_PROTO_NUM);
92 }
93 }
94
95 /**
96 * @tc.name : getprotobyname_0200
97 * @tc.desc : Get the specified protocol name by name
98 * @tc.level : Level 2
99 */
getprotobyname_0200(void)100 void getprotobyname_0200(void)
101 {
102 struct protoent *ret = getprotobyname("abcd");
103 EXPECT_PTREQ("getprotobyname_0200", ret, NULL);
104 }
105
106 /**
107 * @tc.name : getprotobynumber_0100
108 * @tc.desc : Get the specified protocol name by number
109 * @tc.level : Level 0
110 */
getprotobynumber_0100(void)111 void getprotobynumber_0100(void)
112 {
113 struct protoent *ret = getprotobynumber(TEST_PROTO_NUM);
114 EXPECT_PTRNE("getprotobynumber_0100", ret, NULL);
115 if (ret) {
116 EXPECT_STREQ("getprotobynumber_0100", ret->p_name, "tcp");
117 EXPECT_EQ("getprotobynumber_0100", ret->p_proto, TEST_PROTO_NUM);
118 }
119 }
120
121 /**
122 * @tc.name : getprotobynumber_0200
123 * @tc.desc : Get the specified protocol name by number
124 * @tc.level : Level 2
125 */
getprotobynumber_0200(void)126 void getprotobynumber_0200(void)
127 {
128 struct protoent *ret = getprotobynumber(TEST_INVALID_NUMBER);
129 EXPECT_PTREQ("getprotobynumber_0200", ret, NULL);
130 }
131
132 /**
133 * @tc.name : getprotoent_0100
134 * @tc.desc : Get the protocol information scheduled by the system
135 * @tc.level : Level 0
136 */
getprotoent_0100(void)137 void getprotoent_0100(void)
138 {
139 test_resetprotoent();
140 struct protoent *dst = NULL;
141 struct protoent *src = NULL;
142 int count = 0;
143 while (1) {
144 dst = getprotoent();
145 src = test_getprotoent();
146 if (dst && src) {
147 count++;
148 EXPECT_EQ("getprotoent_0100", dst->p_proto, src->p_proto);
149 EXPECT_STREQ("getprotoent_0100", dst->p_name, src->p_name);
150 } else {
151 break;
152 }
153 }
154 EXPECT_TRUE("getprotoent_0100", count > 0);
155 test_resetprotoent();
156 }
157
main(void)158 int main(void)
159 {
160 getprotobyname_0100();
161 getprotobyname_0200();
162
163 getprotobynumber_0100();
164 getprotobynumber_0200();
165
166 getprotoent_0100();
167 return t_status;
168 }