1570af302Sopenharmony_ci/** 2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci#include <netdb.h> 17570af302Sopenharmony_ci#include "functionalext.h" 18570af302Sopenharmony_ci 19570af302Sopenharmony_ci#define TEST_PROTO_NUM 6 20570af302Sopenharmony_ci#define TEST_INVALID_NUMBER 10240 21570af302Sopenharmony_ci#define TEST_TERMINATE_SIZE 2 22570af302Sopenharmony_ci 23570af302Sopenharmony_cistatic int test_index = 0; 24570af302Sopenharmony_cistatic const unsigned char test_protos[] = {"\000ip\0" 25570af302Sopenharmony_ci "\001icmp\0" 26570af302Sopenharmony_ci "\002igmp\0" 27570af302Sopenharmony_ci "\003ggp\0" 28570af302Sopenharmony_ci "\004ipencap\0" 29570af302Sopenharmony_ci "\005st\0" 30570af302Sopenharmony_ci "\006tcp\0" 31570af302Sopenharmony_ci "\010egp\0" 32570af302Sopenharmony_ci "\014pup\0" 33570af302Sopenharmony_ci "\021udp\0" 34570af302Sopenharmony_ci "\024hmp\0" 35570af302Sopenharmony_ci "\026xns-idp\0" 36570af302Sopenharmony_ci "\033rdp\0" 37570af302Sopenharmony_ci "\035iso-tp4\0" 38570af302Sopenharmony_ci "\044xtp\0" 39570af302Sopenharmony_ci "\045ddp\0" 40570af302Sopenharmony_ci "\046idpr-cmtp\0" 41570af302Sopenharmony_ci "\051ipv6\0" 42570af302Sopenharmony_ci "\053ipv6-route\0" 43570af302Sopenharmony_ci "\054ipv6-frag\0" 44570af302Sopenharmony_ci "\055idrp\0" 45570af302Sopenharmony_ci "\056rsvp\0" 46570af302Sopenharmony_ci "\057gre\0" 47570af302Sopenharmony_ci "\062esp\0" 48570af302Sopenharmony_ci "\063ah\0" 49570af302Sopenharmony_ci "\071skip\0" 50570af302Sopenharmony_ci "\072ipv6-icmp\0" 51570af302Sopenharmony_ci "\073ipv6-nonxt\0" 52570af302Sopenharmony_ci "\074ipv6-opts\0" 53570af302Sopenharmony_ci "\111rspf\0" 54570af302Sopenharmony_ci "\121vmtp\0" 55570af302Sopenharmony_ci "\131ospf\0" 56570af302Sopenharmony_ci "\136ipip\0" 57570af302Sopenharmony_ci "\142encap\0" 58570af302Sopenharmony_ci "\147pim\0" 59570af302Sopenharmony_ci "\377raw"}; 60570af302Sopenharmony_ci 61570af302Sopenharmony_cistruct protoent *test_getprotoent(void) 62570af302Sopenharmony_ci{ 63570af302Sopenharmony_ci static struct protoent p; 64570af302Sopenharmony_ci if (test_index >= sizeof(test_protos)) { 65570af302Sopenharmony_ci return NULL; 66570af302Sopenharmony_ci } 67570af302Sopenharmony_ci p.p_proto = test_protos[test_index]; 68570af302Sopenharmony_ci p.p_name = (char *)&test_protos[test_index + 1]; 69570af302Sopenharmony_ci test_index += strlen(p.p_name) + TEST_TERMINATE_SIZE; 70570af302Sopenharmony_ci return &p; 71570af302Sopenharmony_ci} 72570af302Sopenharmony_ci 73570af302Sopenharmony_civoid test_resetprotoent(void) 74570af302Sopenharmony_ci{ 75570af302Sopenharmony_ci test_index = 0; 76570af302Sopenharmony_ci setprotoent(0); 77570af302Sopenharmony_ci endprotoent(); 78570af302Sopenharmony_ci} 79570af302Sopenharmony_ci 80570af302Sopenharmony_ci/** 81570af302Sopenharmony_ci * @tc.name : getprotobyname_0100 82570af302Sopenharmony_ci * @tc.desc : Get the specified protocol name by name 83570af302Sopenharmony_ci * @tc.level : Level 0 84570af302Sopenharmony_ci */ 85570af302Sopenharmony_civoid getprotobyname_0100(void) 86570af302Sopenharmony_ci{ 87570af302Sopenharmony_ci struct protoent *ret = getprotobyname("tcp"); 88570af302Sopenharmony_ci EXPECT_PTRNE("getprotobyname_0100", ret, NULL); 89570af302Sopenharmony_ci if (ret) { 90570af302Sopenharmony_ci EXPECT_STREQ("getprotobyname_0100", ret->p_name, "tcp"); 91570af302Sopenharmony_ci EXPECT_EQ("getprotobyname_0100", ret->p_proto, TEST_PROTO_NUM); 92570af302Sopenharmony_ci } 93570af302Sopenharmony_ci} 94570af302Sopenharmony_ci 95570af302Sopenharmony_ci/** 96570af302Sopenharmony_ci * @tc.name : getprotobyname_0200 97570af302Sopenharmony_ci * @tc.desc : Get the specified protocol name by name 98570af302Sopenharmony_ci * @tc.level : Level 2 99570af302Sopenharmony_ci */ 100570af302Sopenharmony_civoid getprotobyname_0200(void) 101570af302Sopenharmony_ci{ 102570af302Sopenharmony_ci struct protoent *ret = getprotobyname("abcd"); 103570af302Sopenharmony_ci EXPECT_PTREQ("getprotobyname_0200", ret, NULL); 104570af302Sopenharmony_ci} 105570af302Sopenharmony_ci 106570af302Sopenharmony_ci/** 107570af302Sopenharmony_ci * @tc.name : getprotobynumber_0100 108570af302Sopenharmony_ci * @tc.desc : Get the specified protocol name by number 109570af302Sopenharmony_ci * @tc.level : Level 0 110570af302Sopenharmony_ci */ 111570af302Sopenharmony_civoid getprotobynumber_0100(void) 112570af302Sopenharmony_ci{ 113570af302Sopenharmony_ci struct protoent *ret = getprotobynumber(TEST_PROTO_NUM); 114570af302Sopenharmony_ci EXPECT_PTRNE("getprotobynumber_0100", ret, NULL); 115570af302Sopenharmony_ci if (ret) { 116570af302Sopenharmony_ci EXPECT_STREQ("getprotobynumber_0100", ret->p_name, "tcp"); 117570af302Sopenharmony_ci EXPECT_EQ("getprotobynumber_0100", ret->p_proto, TEST_PROTO_NUM); 118570af302Sopenharmony_ci } 119570af302Sopenharmony_ci} 120570af302Sopenharmony_ci 121570af302Sopenharmony_ci/** 122570af302Sopenharmony_ci * @tc.name : getprotobynumber_0200 123570af302Sopenharmony_ci * @tc.desc : Get the specified protocol name by number 124570af302Sopenharmony_ci * @tc.level : Level 2 125570af302Sopenharmony_ci */ 126570af302Sopenharmony_civoid getprotobynumber_0200(void) 127570af302Sopenharmony_ci{ 128570af302Sopenharmony_ci struct protoent *ret = getprotobynumber(TEST_INVALID_NUMBER); 129570af302Sopenharmony_ci EXPECT_PTREQ("getprotobynumber_0200", ret, NULL); 130570af302Sopenharmony_ci} 131570af302Sopenharmony_ci 132570af302Sopenharmony_ci/** 133570af302Sopenharmony_ci * @tc.name : getprotoent_0100 134570af302Sopenharmony_ci * @tc.desc : Get the protocol information scheduled by the system 135570af302Sopenharmony_ci * @tc.level : Level 0 136570af302Sopenharmony_ci */ 137570af302Sopenharmony_civoid getprotoent_0100(void) 138570af302Sopenharmony_ci{ 139570af302Sopenharmony_ci test_resetprotoent(); 140570af302Sopenharmony_ci struct protoent *dst = NULL; 141570af302Sopenharmony_ci struct protoent *src = NULL; 142570af302Sopenharmony_ci int count = 0; 143570af302Sopenharmony_ci while (1) { 144570af302Sopenharmony_ci dst = getprotoent(); 145570af302Sopenharmony_ci src = test_getprotoent(); 146570af302Sopenharmony_ci if (dst && src) { 147570af302Sopenharmony_ci count++; 148570af302Sopenharmony_ci EXPECT_EQ("getprotoent_0100", dst->p_proto, src->p_proto); 149570af302Sopenharmony_ci EXPECT_STREQ("getprotoent_0100", dst->p_name, src->p_name); 150570af302Sopenharmony_ci } else { 151570af302Sopenharmony_ci break; 152570af302Sopenharmony_ci } 153570af302Sopenharmony_ci } 154570af302Sopenharmony_ci EXPECT_TRUE("getprotoent_0100", count > 0); 155570af302Sopenharmony_ci test_resetprotoent(); 156570af302Sopenharmony_ci} 157570af302Sopenharmony_ci 158570af302Sopenharmony_ciint main(void) 159570af302Sopenharmony_ci{ 160570af302Sopenharmony_ci getprotobyname_0100(); 161570af302Sopenharmony_ci getprotobyname_0200(); 162570af302Sopenharmony_ci 163570af302Sopenharmony_ci getprotobynumber_0100(); 164570af302Sopenharmony_ci getprotobynumber_0200(); 165570af302Sopenharmony_ci 166570af302Sopenharmony_ci getprotoent_0100(); 167570af302Sopenharmony_ci return t_status; 168570af302Sopenharmony_ci}