19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cerrno>
179762338dSopenharmony_ci#include <cstdio>
189762338dSopenharmony_ci#include <cstdlib>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <vector>
219762338dSopenharmony_ci#include <fcntl.h>
229762338dSopenharmony_ci#include <unistd.h>
239762338dSopenharmony_ci#include <arpa/inet.h>
249762338dSopenharmony_ci#include <gtest/gtest.h>
259762338dSopenharmony_ci#include <linux/filter.h>
269762338dSopenharmony_ci#include <linux/if_ether.h>
279762338dSopenharmony_ci#include <linux/in6.h>
289762338dSopenharmony_ci#include <linux/netlink.h>
299762338dSopenharmony_ci#include <netinet/in.h>
309762338dSopenharmony_ci#include <netinet/tcp.h>
319762338dSopenharmony_ci#include <sys/stat.h>
329762338dSopenharmony_ci#include <sys/socket.h>
339762338dSopenharmony_ci#include <sys/types.h>
349762338dSopenharmony_ci#include <linux/netfilter_ipv4/ip_tables.h>
359762338dSopenharmony_ci#include <linux/netfilter_ipv6/ip6_tables.h>
369762338dSopenharmony_ci#include "securec.h"
379762338dSopenharmony_ci
389762338dSopenharmony_ciusing namespace testing::ext;
399762338dSopenharmony_ci
409762338dSopenharmony_cistatic const int BAD_SOCKET_FD = -1;
419762338dSopenharmony_cistatic const int MAX_SIZE = 1024;
429762338dSopenharmony_ci
439762338dSopenharmony_ci
449762338dSopenharmony_cistruct SetOptionSupportTest {
459762338dSopenharmony_ci    std::string name;
469762338dSopenharmony_ci    int domain;
479762338dSopenharmony_ci    int type;
489762338dSopenharmony_ci    int optLevel;
499762338dSopenharmony_ci    int optName;
509762338dSopenharmony_ci    int ret;
519762338dSopenharmony_ci    int optVal;
529762338dSopenharmony_ci    int optLen;
539762338dSopenharmony_ci} g_setOpt[] = {
549762338dSopenharmony_ci    {"SO_BINDTODEVICE",     AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_BINDTODEVICE,    0,      0,  0},
559762338dSopenharmony_ci    {"SO_DEBUG",            AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_DEBUG,           0,      1,  4},
569762338dSopenharmony_ci    {"SO_REUSEADDR",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_REUSEADDR,       0,      1,  4},
579762338dSopenharmony_ci    {"SO_REUSEPORT",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_REUSEPORT,       0,      1,  4},
589762338dSopenharmony_ci    {"SO_BUSY_POLL",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_BUSY_POLL,       0,      1,  4},
599762338dSopenharmony_ci    {"SO_DONTROUTE",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_DONTROUTE,       0,      1,  4},
609762338dSopenharmony_ci    {"SO_BROADCAST",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_BROADCAST,       0,      1,  4},
619762338dSopenharmony_ci    {"SO_SNDBUFFORCE",      AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_SNDBUFFORCE,     0, 0x40000, 4},
629762338dSopenharmony_ci    {"IP_TRANSPARENT",      AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_TRANSPARENT,     0,      1,  4},
639762338dSopenharmony_ci    {"SO_RCVBUFFORCE",      AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_RCVBUFFORCE,     0, 0x40000, 4},
649762338dSopenharmony_ci    {"TCP_REPAIR",          AF_INET,    SOCK_STREAM,    SOL_TCP,        TCP_REPAIR,         0,      1,      4},
659762338dSopenharmony_ci    {"SO_KEEPALIVE",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_KEEPALIVE,       0,      1,  4},
669762338dSopenharmony_ci    {"SO_OOBINLINE",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_OOBINLINE,       0,      1,  4},
679762338dSopenharmony_ci    {"SO_NO_CHECK",         AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_NO_CHECK,        0,      1,  4},
689762338dSopenharmony_ci    {"SO_PRIORITY",         AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_PRIORITY,        0,      1,  4},
699762338dSopenharmony_ci    {"SO_BSDCOMPAT",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_BSDCOMPAT,       0,      0,  4},
709762338dSopenharmony_ci    {"SO_PASSCRED",         AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_PASSCRED,        0,      1,  4},
719762338dSopenharmony_ci    {"SO_TIMESTAMP",        AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_TIMESTAMP,       0,      1,  4},
729762338dSopenharmony_ci    {"SO_TIMESTAMPNS",      AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_TIMESTAMPNS,     0,      1,  4},
739762338dSopenharmony_ci    {"SO_RCVLOWAT",         AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_RCVLOWAT,        0,      1,  4},
749762338dSopenharmony_ci    {"SO_PASSSEC",          AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_PASSSEC,         0,      1,  4},
759762338dSopenharmony_ci    {"SO_MARK",             AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_MARK,            0,    100,  4},
769762338dSopenharmony_ci    {"SO_RXQ_OVFL",         AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_RXQ_OVFL,        0,      1,  4},
779762338dSopenharmony_ci    {"SO_WIFI_STATUS",      AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_WIFI_STATUS,     0,      1,  4},
789762338dSopenharmony_ci    {"SO_NOFCS",            AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_NOFCS,           0,      1,  4},
799762338dSopenharmony_ci    {"SO_SELECT_ERR_QUEUE", AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_SELECT_ERR_QUEUE, 0,     1,  4},
809762338dSopenharmony_ci    {"SO_MAX_PACING_RATE",  AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_MAX_PACING_RATE, 0,      1,  4},
819762338dSopenharmony_ci    {"SO_INCOMING_CPU",     AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_INCOMING_CPU,    0,      1,  4},
829762338dSopenharmony_ci    {"IP_RECVOPTS",         AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_RECVOPTS,        0,      1,  4},
839762338dSopenharmony_ci    {"IP_RECVTOS",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_RECVTOS,         0,      1,  4},
849762338dSopenharmony_ci    {"IP_RETOPTS",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_RETOPTS,         0,      1,  4},
859762338dSopenharmony_ci    {"IP_MTU_DISCOVER",     AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_MTU_DISCOVER,    0,      1,  4},
869762338dSopenharmony_ci    {"IP_RECVERR",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_RECVERR,         0,      1,  4},
879762338dSopenharmony_ci    {"IP_FREEBIND",         AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_FREEBIND,        0,      1,  4},
889762338dSopenharmony_ci    {"IP_PASSSEC",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_PASSSEC,         0,      1,  4},
899762338dSopenharmony_ci    {"IP_MINTTL",           AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_MINTTL,          0,      1,  4},
909762338dSopenharmony_ci    {"IP_BIND_ADDRESS_NO_PORT", AF_INET, SOCK_STREAM,   IPPROTO_IP,     IP_BIND_ADDRESS_NO_PORT, 0, 1,  4},
919762338dSopenharmony_ci    {"IP_RECVORIGDSTADDR",  AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_RECVORIGDSTADDR, 0,      1,  4},
929762338dSopenharmony_ci    {"IP_CHECKSUM",         AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_CHECKSUM,        0,      1,  4},
939762338dSopenharmony_ci    {"IP_OPTIONS",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_OPTIONS,         0,      1,  4},
949762338dSopenharmony_ci    {"IP_PKTINFO",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_PKTINFO,         0,      1,  4},
959762338dSopenharmony_ci    {"IP_RECVTTL",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_RECVTTL,         0,      1,  4},
969762338dSopenharmony_ci    {"IP_TOS",              AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_TOS,             0,      0,  4},
979762338dSopenharmony_ci    {"IP_TTL",              AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_TTL,             0,      1,  4},
989762338dSopenharmony_ci    {"IP_MULTICAST_LOOP",   AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_MULTICAST_LOOP,  0,      1,  4},
999762338dSopenharmony_ci    {"IP_MULTICAST_ALL",    AF_INET6,   SOCK_STREAM,    IPPROTO_IP,     IP_MULTICAST_ALL,   0,      1,  4},
1009762338dSopenharmony_ci    {"IPV6_V6ONLY",         AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_V6ONLY,        0,      1,  4},
1019762338dSopenharmony_ci    {"IPV6_RECVPKTINFO",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVPKTINFO,   0,      1,  4},
1029762338dSopenharmony_ci    {"IPV6_2292PKTINFO",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_2292PKTINFO,   0,      1,  4},
1039762338dSopenharmony_ci    {"IPV6_RECVHOPLIMIT",   AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVHOPLIMIT,  0,      1,  4},
1049762338dSopenharmony_ci    {"IPV6_2292HOPLIMIT",   AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_2292HOPLIMIT,  0,      1,  4},
1059762338dSopenharmony_ci    {"IPV6_RECVRTHDR",      AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVRTHDR,     0,      1,  4},
1069762338dSopenharmony_ci    {"IPV6_2292RTHDR",      AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_2292RTHDR,     0,      1,  4},
1079762338dSopenharmony_ci    {"IPV6_RECVHOPOPTS",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVHOPOPTS,   0,      1,  4},
1089762338dSopenharmony_ci    {"IPV6_2292HOPOPTS",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_2292HOPOPTS,   0,      1,  4},
1099762338dSopenharmony_ci    {"IPV6_RECVDSTOPTS",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVDSTOPTS,   0,      1,  4},
1109762338dSopenharmony_ci    {"IPV6_2292DSTOPTS",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_2292DSTOPTS,   0,      1,  4},
1119762338dSopenharmony_ci    {"IPV6_TCLASS",         AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_TCLASS,        0,      1,  4},
1129762338dSopenharmony_ci    {"IPV6_RECVTCLASS",     AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVTCLASS,    0,      1,  4},
1139762338dSopenharmony_ci    {"IPV6_RECVPATHMTU",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVPATHMTU,   0,      1,  4},
1149762338dSopenharmony_ci    {"IPV6_TRANSPARENT",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_TRANSPARENT,   0,      1,  4},
1159762338dSopenharmony_ci    {"IPV6_RECVORIGDSTADDR", AF_INET6,  SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVORIGDSTADDR, 0,    1,  4},
1169762338dSopenharmony_ci    {"IPV6_UNICAST_HOPS",   AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_UNICAST_HOPS,   0,     1,  4},
1179762338dSopenharmony_ci    {"IPV6_MULTICAST_HOPS", AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_MULTICAST_HOPS, 0,     1,  4},
1189762338dSopenharmony_ci    {"IPV6_MULTICAST_LOOP", AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_MULTICAST_LOOP, 0,     1,  4},
1199762338dSopenharmony_ci    {"IPV6_MULTICAST_IF",   AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_MULTICAST_IF,   0,     1,  4},
1209762338dSopenharmony_ci    {"IPV6_MTU_DISCOVER",   AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_MTU_DISCOVER,   0,     1,  4},
1219762338dSopenharmony_ci    {"IPV6_RECVERR",        AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_RECVERR,        0,     1,  4},
1229762338dSopenharmony_ci    {"IPV6_ADDR_PREFERENCES", AF_INET6, SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_ADDR_PREFERENCES, 0,   1,  4},
1239762338dSopenharmony_ci    {"IPV6_MINHOPCOUNT",    AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_MINHOPCOUNT,    0,     1,  4},
1249762338dSopenharmony_ci    {"IPV6_DONTFRAG",       AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_DONTFRAG,       0,     1,  4},
1259762338dSopenharmony_ci    {"IPV6_AUTOFLOWLABEL",  AF_INET6,   SOCK_DGRAM,     IPPROTO_IPV6,   IPV6_AUTOFLOWLABEL,  0,     1,  4},
1269762338dSopenharmony_ci    {"TCP_NODELAY",         AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_NODELAY,         0,     1,  4},
1279762338dSopenharmony_ci    {"TCP_THIN_LINEAR_TIMEOUTS", AF_INET, SOCK_STREAM,  IPPROTO_TCP, TCP_THIN_LINEAR_TIMEOUTS, 0,   1,  4},
1289762338dSopenharmony_ci    {"TCP_THIN_DUPACK",     AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_THIN_DUPACK,     0,     0,  4},
1299762338dSopenharmony_ci    {"TCP_CORK",            AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_CORK,            0,     1,  4},
1309762338dSopenharmony_ci    {"TCP_KEEPIDLE",        AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_KEEPIDLE,        0,     1,  4},
1319762338dSopenharmony_ci    {"TCP_KEEPINTVL",       AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_KEEPINTVL,       0,     1,  4},
1329762338dSopenharmony_ci    {"TCP_KEEPCNT",         AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_KEEPCNT,         0,     1,  4},
1339762338dSopenharmony_ci    {"TCP_SYNCNT",          AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_SYNCNT,          0,     1,  4},
1349762338dSopenharmony_ci    {"TCP_SAVE_SYN",        AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_SAVE_SYN,        0,     1,  4},
1359762338dSopenharmony_ci    {"TCP_LINGER2",         AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_LINGER2,         0,     1,  4},
1369762338dSopenharmony_ci    {"TCP_DEFER_ACCEPT",    AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_DEFER_ACCEPT,    0,     1,  4},
1379762338dSopenharmony_ci    {"TCP_WINDOW_CLAMP",    AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_WINDOW_CLAMP,    0,     0,  4},
1389762338dSopenharmony_ci    {"TCP_QUICKACK",        AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_QUICKACK,        0,     1,  4},
1399762338dSopenharmony_ci    {"TCP_USER_TIMEOUT",    AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_USER_TIMEOUT,    0,     1,  4},
1409762338dSopenharmony_ci    {"TCP_FASTOPEN",        AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_FASTOPEN,        0,     1,  4},
1419762338dSopenharmony_ci    {"TCP_NOTSENT_LOWAT",   AF_INET,    SOCK_STREAM,    IPPROTO_TCP,    TCP_NOTSENT_LOWAT,   0,     1,  4},
1429762338dSopenharmony_ci    {"TCP_FASTOPEN_CONNECT", AF_INET,   SOCK_STREAM,    IPPROTO_TCP,    TCP_FASTOPEN_CONNECT, 0,    1,  4},
1439762338dSopenharmony_ci};
1449762338dSopenharmony_ci
1459762338dSopenharmony_cistruct GetOptionOnlySupportTest {
1469762338dSopenharmony_ci    std::string name;
1479762338dSopenharmony_ci    int domain;
1489762338dSopenharmony_ci    int type;
1499762338dSopenharmony_ci    int optLevel;
1509762338dSopenharmony_ci    int optName;
1519762338dSopenharmony_ci    int ret;
1529762338dSopenharmony_ci    int optVal;
1539762338dSopenharmony_ci    int optLen;
1549762338dSopenharmony_ci} g_getOnlyOpt[] = {
1559762338dSopenharmony_ci    {"SO_TYPE",             AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_TYPE,            0,      1,  4},
1569762338dSopenharmony_ci    {"SO_PROTOCOL",         AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_PROTOCOL,        0,      6,  4},
1579762338dSopenharmony_ci    {"SO_DOMAIN",           AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_DOMAIN,          0,      2,  4},
1589762338dSopenharmony_ci    {"SO_ERROR",            AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_ERROR,           0,      0,  4},
1599762338dSopenharmony_ci    {"IP_HDRINCL",          AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_HDRINCL,         0,      0,  4},
1609762338dSopenharmony_ci    {"IP_NODEFRAG",         AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_NODEFRAG,        0,      0,  4},
1619762338dSopenharmony_ci    {"IP_UNICAST_IF",       AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_UNICAST_IF,      0,      0,  4},
1629762338dSopenharmony_ci    {"IP_MULTICAST_TTL",    AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_MULTICAST_TTL,   0,      1,  4},
1639762338dSopenharmony_ci    {"IP_MULTICAST_IF",     AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_MULTICAST_IF,    0,      0,  4},
1649762338dSopenharmony_ci    {"IPV6_HOPOPTS",        AF_INET6,   SOCK_STREAM,    SOL_IPV6,       IPV6_HOPOPTS,       0,      0,  0},
1659762338dSopenharmony_ci    {"IPV6_RTHDRDSTOPTS",   AF_INET6,   SOCK_DGRAM,     SOL_IPV6,       IPV6_RTHDRDSTOPTS,  0,      0,  0},
1669762338dSopenharmony_ci    {"IPV6_RTHDR",          AF_INET6,   SOCK_DGRAM,     SOL_IPV6,       IPV6_RTHDR,         0,      0,  0},
1679762338dSopenharmony_ci    {"IPV6_DSTOPTS",        AF_INET6,   SOCK_DGRAM,     SOL_IPV6,       IPV6_DSTOPTS,       0,      0,  0},
1689762338dSopenharmony_ci    {"TCP_MAXSEG",          AF_INET,    SOCK_STREAM,    SOL_TCP,        TCP_MAXSEG,         0,    536,  4},
1699762338dSopenharmony_ci    {"IP_PKTOPTIONS",       AF_INET,    SOCK_STREAM,    IPPROTO_IP,     IP_PKTOPTIONS,      0,      0,  0},
1709762338dSopenharmony_ci    {"TCP_INFO",            AF_INET,    SOCK_STREAM,    IPPROTO_IP,     TCP_INFO,           0,      0,   4},
1719762338dSopenharmony_ci    {"TCP_CC_INFO",         AF_INET,    SOCK_STREAM,    IPPROTO_IP,     TCP_CC_INFO,        0,      0,   4},
1729762338dSopenharmony_ci    {"SO_SNDLOWAT",         AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_SNDLOWAT,        0,      1,  4},
1739762338dSopenharmony_ci    {"SO_ACCEPTCONN",       AF_INET,    SOCK_STREAM,    SOL_SOCKET,     SO_ACCEPTCONN,      0,      0,  4},
1749762338dSopenharmony_ci};
1759762338dSopenharmony_ci
1769762338dSopenharmony_ciclass HatsSockoptTest : public testing::Test {
1779762338dSopenharmony_cipublic:
1789762338dSopenharmony_ci    static void SetUpTestCase();
1799762338dSopenharmony_ci    static void TearDownTestCase();
1809762338dSopenharmony_ci    void SetUp();
1819762338dSopenharmony_ci    void TearDown();
1829762338dSopenharmony_ciprivate:
1839762338dSopenharmony_ci};
1849762338dSopenharmony_civoid HatsSockoptTest::SetUp()
1859762338dSopenharmony_ci{
1869762338dSopenharmony_ci}
1879762338dSopenharmony_civoid HatsSockoptTest::TearDown()
1889762338dSopenharmony_ci{
1899762338dSopenharmony_ci}
1909762338dSopenharmony_civoid HatsSockoptTest::SetUpTestCase()
1919762338dSopenharmony_ci{
1929762338dSopenharmony_ci}
1939762338dSopenharmony_civoid HatsSockoptTest::TearDownTestCase()
1949762338dSopenharmony_ci{
1959762338dSopenharmony_ci}
1969762338dSopenharmony_ci
1979762338dSopenharmony_ci/*
1989762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0100
1999762338dSopenharmony_ci * @tc.name   : SetAndGetsockoptSupportOptSuccess_0001
2009762338dSopenharmony_ci * @tc.desc   : setsockopt and getsockopt own support option test success.
2019762338dSopenharmony_ci * @tc.size   : MediumTest
2029762338dSopenharmony_ci * @tc.type   : Function
2039762338dSopenharmony_ci * @tc.level  : Level 1
2049762338dSopenharmony_ci */
2059762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetAndGetsockoptSupportOptSuccess_0001, Function | MediumTest | Level1)
2069762338dSopenharmony_ci{
2079762338dSopenharmony_ci    int ret;
2089762338dSopenharmony_ci    int fd = -1;
2099762338dSopenharmony_ci    int32_t optVal;
2109762338dSopenharmony_ci    socklen_t optLen;
2119762338dSopenharmony_ci    for (int32_t i = 0; i < sizeof(g_setOpt) / sizeof(g_setOpt[0]); i++) {
2129762338dSopenharmony_ci        fd = socket(g_setOpt[i].domain, g_setOpt[i].type, 0);
2139762338dSopenharmony_ci        EXPECT_TRUE(fd > 0);
2149762338dSopenharmony_ci
2159762338dSopenharmony_ci        ret = setsockopt(fd, g_setOpt[i].optLevel, g_setOpt[i].optName, &g_setOpt[i].optVal, g_setOpt[i].optLen);
2169762338dSopenharmony_ci        EXPECT_EQ(ret, g_setOpt[i].ret);
2179762338dSopenharmony_ci
2189762338dSopenharmony_ci        if (g_setOpt[i].optName == SO_SNDBUFFORCE ||
2199762338dSopenharmony_ci            g_setOpt[i].optName == SO_RCVBUFFORCE ||
2209762338dSopenharmony_ci            g_setOpt[i].optName == IPV6_ADDR_PREFERENCES) {
2219762338dSopenharmony_ci            close(fd);
2229762338dSopenharmony_ci            continue;
2239762338dSopenharmony_ci        }
2249762338dSopenharmony_ci        optVal = 0;
2259762338dSopenharmony_ci        optLen = g_setOpt[i].optLen;
2269762338dSopenharmony_ci        ret = getsockopt(fd, g_setOpt[i].optLevel, g_setOpt[i].optName, &optVal, &optLen);
2279762338dSopenharmony_ci        EXPECT_EQ(ret, g_setOpt[i].ret);
2289762338dSopenharmony_ci        EXPECT_EQ(optVal, g_setOpt[i].optVal);
2299762338dSopenharmony_ci        EXPECT_EQ(optLen, g_setOpt[i].optLen);
2309762338dSopenharmony_ci        close(fd);
2319762338dSopenharmony_ci    }
2329762338dSopenharmony_ci
2339762338dSopenharmony_ci    close(fd);
2349762338dSopenharmony_ci}
2359762338dSopenharmony_ci
2369762338dSopenharmony_ci/*
2379762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0200
2389762338dSopenharmony_ci * @tc.name   : SetAndGetsockoptSndAndRcvTestSuccess_0002
2399762338dSopenharmony_ci * @tc.desc   : setsockopt and getsockopt SO_SNDBUF and SO_RCVBUF option test success.
2409762338dSopenharmony_ci * @tc.size   : MediumTest
2419762338dSopenharmony_ci * @tc.type   : Function
2429762338dSopenharmony_ci * @tc.level  : Level 1
2439762338dSopenharmony_ci */
2449762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetAndGetsockoptSndAndRcvTestSuccess_0002, Function | MediumTest | Level1)
2459762338dSopenharmony_ci{
2469762338dSopenharmony_ci    int ret;
2479762338dSopenharmony_ci    int fd = -1;
2489762338dSopenharmony_ci    int32_t optVal;
2499762338dSopenharmony_ci    socklen_t optLen;
2509762338dSopenharmony_ci
2519762338dSopenharmony_ci    fd = socket(AF_INET, SOL_SOCKET, 0);
2529762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
2539762338dSopenharmony_ci
2549762338dSopenharmony_ci    // setsockopt SO_SNDBUF test success
2559762338dSopenharmony_ci    optVal = 0x20000;
2569762338dSopenharmony_ci    optLen = sizeof(optVal);
2579762338dSopenharmony_ci    ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optVal, optLen);
2589762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
2599762338dSopenharmony_ci
2609762338dSopenharmony_ci    // getsockopt SO_SNDBUF test success
2619762338dSopenharmony_ci    optVal = 0;
2629762338dSopenharmony_ci    optLen = sizeof(optVal);
2639762338dSopenharmony_ci    ret = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optVal, &optLen);
2649762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
2659762338dSopenharmony_ci    EXPECT_TRUE(optVal > 0);
2669762338dSopenharmony_ci
2679762338dSopenharmony_ci    // setsockopt SO_RCVBUF test success
2689762338dSopenharmony_ci    optVal = 0x20000;
2699762338dSopenharmony_ci    optLen = sizeof(optVal);
2709762338dSopenharmony_ci    ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &optVal, optLen);
2719762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
2729762338dSopenharmony_ci    EXPECT_TRUE(optVal > 0);
2739762338dSopenharmony_ci
2749762338dSopenharmony_ci    // getsockopt SO_RCVBUF test success
2759762338dSopenharmony_ci    optVal = 0;
2769762338dSopenharmony_ci    optLen = sizeof(optVal);
2779762338dSopenharmony_ci    ret = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &optVal, &optLen);
2789762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
2799762338dSopenharmony_ci
2809762338dSopenharmony_ci    close(fd);
2819762338dSopenharmony_ci}
2829762338dSopenharmony_ci
2839762338dSopenharmony_ci/*
2849762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0300
2859762338dSopenharmony_ci * @tc.name   : SetAndGetsockoptTCP_TIMESTAMPTestSuccess_0003
2869762338dSopenharmony_ci * @tc.desc   : setsockopt SO_TIMESTAMPING and getsockopt TCP_TIMESTAMP option test success.
2879762338dSopenharmony_ci * @tc.size   : MediumTest
2889762338dSopenharmony_ci * @tc.type   : Function
2899762338dSopenharmony_ci * @tc.level  : Level 1
2909762338dSopenharmony_ci */
2919762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, GetsockoptTCP_TIMESTAMPTestSuccess_0003, Function | MediumTest | Level1)
2929762338dSopenharmony_ci{
2939762338dSopenharmony_ci    int ret;
2949762338dSopenharmony_ci    int fd = -1;
2959762338dSopenharmony_ci    int32_t optVal;
2969762338dSopenharmony_ci    socklen_t optLen;
2979762338dSopenharmony_ci
2989762338dSopenharmony_ci    fd = socket(AF_INET, SOCK_STREAM, 0);
2999762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
3009762338dSopenharmony_ci
3019762338dSopenharmony_ci    optVal = 1;
3029762338dSopenharmony_ci    optLen = sizeof(optVal);
3039762338dSopenharmony_ci    ret = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &optVal, optLen);
3049762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
3059762338dSopenharmony_ci
3069762338dSopenharmony_ci    optVal = 0;
3079762338dSopenharmony_ci    optLen = sizeof(optVal);
3089762338dSopenharmony_ci    ret = getsockopt(fd, IPPROTO_TCP, TCP_TIMESTAMP, &optVal, &optLen);
3099762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
3109762338dSopenharmony_ci    EXPECT_TRUE(optVal > 0);
3119762338dSopenharmony_ci
3129762338dSopenharmony_ci    close(fd);
3139762338dSopenharmony_ci}
3149762338dSopenharmony_ci
3159762338dSopenharmony_ci/*
3169762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0400
3179762338dSopenharmony_ci * @tc.name   : SetAndGetsockoptIPV6_2292PKTOPTIONSTestSuccess_0004
3189762338dSopenharmony_ci * @tc.desc   : setsockopt and getsockopt IPV6_2292PKTOPTIONS option test success.
3199762338dSopenharmony_ci * @tc.size   : MediumTest
3209762338dSopenharmony_ci * @tc.type   : Function
3219762338dSopenharmony_ci * @tc.level  : Level 1
3229762338dSopenharmony_ci */
3239762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetAndGetsockoptIPV6_2292PKTOPTIONSTestSuccess_0004, Function | MediumTest | Level1)
3249762338dSopenharmony_ci{
3259762338dSopenharmony_ci    int ret;
3269762338dSopenharmony_ci    int fd = -1;
3279762338dSopenharmony_ci    int32_t optVal;
3289762338dSopenharmony_ci    socklen_t optLen;
3299762338dSopenharmony_ci
3309762338dSopenharmony_ci    fd = socket(AF_INET6, SOCK_STREAM, 0);
3319762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
3329762338dSopenharmony_ci
3339762338dSopenharmony_ci    optVal = 1;
3349762338dSopenharmony_ci    optLen = sizeof(optVal);
3359762338dSopenharmony_ci    ret = setsockopt(fd, IPPROTO_IPV6, IPV6_2292PKTOPTIONS, &optVal, optLen);
3369762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
3379762338dSopenharmony_ci
3389762338dSopenharmony_ci    optVal = 0;
3399762338dSopenharmony_ci    optLen = sizeof(optVal);
3409762338dSopenharmony_ci    ret = getsockopt(fd, IPPROTO_IPV6, IPV6_2292PKTOPTIONS, &optVal, &optLen);
3419762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
3429762338dSopenharmony_ci
3439762338dSopenharmony_ci    close(fd);
3449762338dSopenharmony_ci}
3459762338dSopenharmony_ci
3469762338dSopenharmony_ci/*
3479762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0500
3489762338dSopenharmony_ci * @tc.name   : SetAndGetsockoptSO_LINGERTestSuccess_0005
3499762338dSopenharmony_ci * @tc.desc   : setsockopt and getsockopt SO_LINGER option test success.
3509762338dSopenharmony_ci * @tc.size   : MediumTest
3519762338dSopenharmony_ci * @tc.type   : Function
3529762338dSopenharmony_ci * @tc.level  : Level 1
3539762338dSopenharmony_ci */
3549762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetAndGetsockoptSO_LINGERTestSuccess_0005, Function | MediumTest | Level1)
3559762338dSopenharmony_ci{
3569762338dSopenharmony_ci    int ret;
3579762338dSopenharmony_ci    int fd = -1;
3589762338dSopenharmony_ci    struct linger optVal = {
3599762338dSopenharmony_ci        .l_onoff = 1,
3609762338dSopenharmony_ci        .l_linger = 1,
3619762338dSopenharmony_ci    };
3629762338dSopenharmony_ci    socklen_t optLen = sizeof(optVal);
3639762338dSopenharmony_ci
3649762338dSopenharmony_ci    fd = socket(AF_INET, SOCK_STREAM, 0);
3659762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
3669762338dSopenharmony_ci
3679762338dSopenharmony_ci    // setsockopt SO_LINGER test success
3689762338dSopenharmony_ci    ret = setsockopt(fd, SOL_SOCKET, SO_LINGER, &optVal, optLen);
3699762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
3709762338dSopenharmony_ci
3719762338dSopenharmony_ci    // getsockopt SO_LINGER test success
3729762338dSopenharmony_ci    memset_s(&optVal, sizeof(optVal), 0, sizeof(optVal));
3739762338dSopenharmony_ci    optLen = sizeof(optVal);
3749762338dSopenharmony_ci    ret = getsockopt(fd, SOL_SOCKET, SO_LINGER, &optVal, &optLen);
3759762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
3769762338dSopenharmony_ci    EXPECT_EQ(optVal.l_onoff, 1);
3779762338dSopenharmony_ci    EXPECT_EQ(optVal.l_linger, 1);
3789762338dSopenharmony_ci
3799762338dSopenharmony_ci    close(fd);
3809762338dSopenharmony_ci}
3819762338dSopenharmony_ci
3829762338dSopenharmony_ci/*
3839762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0600
3849762338dSopenharmony_ci * @tc.name   : SetAndGetsockoptSO_TIMEOTestSuccess_0006
3859762338dSopenharmony_ci * @tc.desc   : setsockopt and getsockopt SO_RCVTIMEO and SO_SNDTIMEO option test success.
3869762338dSopenharmony_ci * @tc.size   : MediumTest
3879762338dSopenharmony_ci * @tc.type   : Function
3889762338dSopenharmony_ci * @tc.level  : Level 1
3899762338dSopenharmony_ci */
3909762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetAndGetsockoptSO_TIMEOTestSuccess_0006, Function | MediumTest | Level1)
3919762338dSopenharmony_ci{
3929762338dSopenharmony_ci    int ret;
3939762338dSopenharmony_ci    int fd = -1;
3949762338dSopenharmony_ci    struct timeval optVal = {
3959762338dSopenharmony_ci        .tv_sec = 1,
3969762338dSopenharmony_ci        .tv_usec = 1,
3979762338dSopenharmony_ci    };
3989762338dSopenharmony_ci    socklen_t optLen = sizeof(optVal);
3999762338dSopenharmony_ci
4009762338dSopenharmony_ci    fd = socket(AF_INET, SOCK_STREAM, 0);
4019762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
4029762338dSopenharmony_ci
4039762338dSopenharmony_ci    // setsockopt SO_RCVTIMEO test success
4049762338dSopenharmony_ci    ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &optVal, optLen);
4059762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
4069762338dSopenharmony_ci
4079762338dSopenharmony_ci    // getsockopt SO_RCVTIMEO test success
4089762338dSopenharmony_ci    memset_s(&optVal, sizeof(optVal), -1, sizeof(optVal));
4099762338dSopenharmony_ci    optLen = sizeof(optVal);
4109762338dSopenharmony_ci    ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &optVal, &optLen);
4119762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
4129762338dSopenharmony_ci    EXPECT_TRUE(optVal.tv_sec != -1);
4139762338dSopenharmony_ci    EXPECT_TRUE(optVal.tv_usec != -1);
4149762338dSopenharmony_ci
4159762338dSopenharmony_ci    close(fd);
4169762338dSopenharmony_ci}
4179762338dSopenharmony_ci
4189762338dSopenharmony_ci/*
4199762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0700
4209762338dSopenharmony_ci * @tc.name   : SetsockoptSO_FILTERTestSuccess_0007
4219762338dSopenharmony_ci * @tc.desc   : setsockopt SO_ATTACH_FILTER and SO_DETACH_FILTER option test success.
4229762338dSopenharmony_ci * @tc.size   : MediumTest
4239762338dSopenharmony_ci * @tc.type   : Function
4249762338dSopenharmony_ci * @tc.level  : Level 1
4259762338dSopenharmony_ci */
4269762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetsockoptSO_FILTERTestSuccess_0007, Function | MediumTest | Level1)
4279762338dSopenharmony_ci{
4289762338dSopenharmony_ci    int ret;
4299762338dSopenharmony_ci    int fd = -1;
4309762338dSopenharmony_ci    struct sock_filter code[] = {
4319762338dSopenharmony_ci        { 0x28, 0, 0, 0x0000000c },
4329762338dSopenharmony_ci        { 0x15, 0, 8, 0x000086dd },
4339762338dSopenharmony_ci        { 0x30, 0, 0, 0x00000014 },
4349762338dSopenharmony_ci        { 0x15, 2, 0, 0x00000084 },
4359762338dSopenharmony_ci        { 0x15, 1, 0, 0x00000006 },
4369762338dSopenharmony_ci        { 0x15, 0, 17, 0x00000011 },
4379762338dSopenharmony_ci        { 0x28, 0, 0, 0x00000036 },
4389762338dSopenharmony_ci        { 0x15, 14, 0, 0x00000016 },
4399762338dSopenharmony_ci        { 0x28, 0, 0, 0x00000038 },
4409762338dSopenharmony_ci        { 0x15, 12, 13, 0x00000016 },
4419762338dSopenharmony_ci        { 0x15, 0, 12, 0x00000800 },
4429762338dSopenharmony_ci        { 0x30, 0, 0, 0x00000017 },
4439762338dSopenharmony_ci        { 0x15, 2, 0, 0x00000084 },
4449762338dSopenharmony_ci        { 0x15, 1, 0, 0x00000006 },
4459762338dSopenharmony_ci        { 0x15, 0, 8, 0x00000011 },
4469762338dSopenharmony_ci        { 0x28, 0, 0, 0x00000014 },
4479762338dSopenharmony_ci        { 0x45, 6, 0, 0x00001fff },
4489762338dSopenharmony_ci        { 0xb1, 0, 0, 0x0000000e },
4499762338dSopenharmony_ci        { 0x48, 0, 0, 0x0000000e },
4509762338dSopenharmony_ci        { 0x15, 2, 0, 0x00000016 },
4519762338dSopenharmony_ci        { 0x48, 0, 0, 0x00000010 },
4529762338dSopenharmony_ci        { 0x15, 0, 1, 0x00000016 },
4539762338dSopenharmony_ci        { 0x6, 0, 0, 0x00040000 },
4549762338dSopenharmony_ci        { 0x6, 0, 0, 0x00000000 },
4559762338dSopenharmony_ci    };
4569762338dSopenharmony_ci    struct sock_fprog optVal = {
4579762338dSopenharmony_ci        .len = sizeof(code) / sizeof(code[0]),
4589762338dSopenharmony_ci        .filter = code,
4599762338dSopenharmony_ci    };
4609762338dSopenharmony_ci    socklen_t optLen = sizeof(optVal);
4619762338dSopenharmony_ci
4629762338dSopenharmony_ci    fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
4639762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
4649762338dSopenharmony_ci
4659762338dSopenharmony_ci    // setsockopt SO_ATTACH_FILTER test success
4669762338dSopenharmony_ci    ret = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &optVal, optLen);
4679762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
4689762338dSopenharmony_ci
4699762338dSopenharmony_ci    // setsockopt SO_DETACH_FILTER test success
4709762338dSopenharmony_ci    ret = setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, &optVal, optLen);
4719762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
4729762338dSopenharmony_ci
4739762338dSopenharmony_ci    close(fd);
4749762338dSopenharmony_ci}
4759762338dSopenharmony_ci
4769762338dSopenharmony_ci/*
4779762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0800
4789762338dSopenharmony_ci * @tc.name   : SetsockoptTCP_CONGESTIONTestSuccess_0008
4799762338dSopenharmony_ci * @tc.desc   : setsockopt and getsockopt TCP_CONGESTION option test success.
4809762338dSopenharmony_ci * @tc.size   : MediumTest
4819762338dSopenharmony_ci * @tc.type   : Function
4829762338dSopenharmony_ci * @tc.level  : Level 1
4839762338dSopenharmony_ci */
4849762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetsockoptTCP_CONGESTIONTestSuccess_0008, Function | MediumTest | Level1)
4859762338dSopenharmony_ci{
4869762338dSopenharmony_ci    int ret;
4879762338dSopenharmony_ci    int fd = -1;
4889762338dSopenharmony_ci    char optVal[MAX_SIZE] = { 0 };
4899762338dSopenharmony_ci    socklen_t optLen = sizeof(optVal);
4909762338dSopenharmony_ci
4919762338dSopenharmony_ci    fd = socket(AF_INET, SOCK_STREAM, 0);
4929762338dSopenharmony_ci    EXPECT_TRUE(fd > 0);
4939762338dSopenharmony_ci
4949762338dSopenharmony_ci    // setsockopt TCP_CONGESTION test success
4959762338dSopenharmony_ci    const char *algo = "cubic";
4969762338dSopenharmony_ci    ret = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, algo, strlen(algo));
4979762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
4989762338dSopenharmony_ci
4999762338dSopenharmony_ci    // getsockopt get algo success
5009762338dSopenharmony_ci    ret = getsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, &optVal, &optLen);
5019762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
5029762338dSopenharmony_ci    EXPECT_STREQ(optVal, algo);
5039762338dSopenharmony_ci
5049762338dSopenharmony_ci    close(fd);
5059762338dSopenharmony_ci}
5069762338dSopenharmony_ci
5079762338dSopenharmony_ci/*
5089762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_SOCKOPT_0900
5099762338dSopenharmony_ci * @tc.name   : GetsockoptOnlySupportOptSuccess_0009
5109762338dSopenharmony_ci * @tc.desc   : getsockopt only support option test success.
5119762338dSopenharmony_ci * @tc.size   : MediumTest
5129762338dSopenharmony_ci * @tc.type   : Function
5139762338dSopenharmony_ci * @tc.level  : Level 1
5149762338dSopenharmony_ci */
5159762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, GetsockoptOnlySupportOptSuccess_0009, Function | MediumTest | Level1)
5169762338dSopenharmony_ci{
5179762338dSopenharmony_ci    int ret;
5189762338dSopenharmony_ci    int fd = -1;
5199762338dSopenharmony_ci    int32_t optVal;
5209762338dSopenharmony_ci    socklen_t optLen;
5219762338dSopenharmony_ci    for (int32_t i = 0; i < sizeof(g_getOnlyOpt) / sizeof(g_getOnlyOpt[0]); i++) {
5229762338dSopenharmony_ci        fd = socket(g_getOnlyOpt[i].domain, g_getOnlyOpt[i].type, 0);
5239762338dSopenharmony_ci        EXPECT_TRUE(fd > 0);
5249762338dSopenharmony_ci
5259762338dSopenharmony_ci        optVal = 0;
5269762338dSopenharmony_ci        optLen = g_getOnlyOpt[i].optLen;
5279762338dSopenharmony_ci        ret = getsockopt(fd, g_getOnlyOpt[i].optLevel, g_getOnlyOpt[i].optName, &optVal, &optLen);
5289762338dSopenharmony_ci        EXPECT_EQ(ret, g_getOnlyOpt[i].ret);
5299762338dSopenharmony_ci        EXPECT_EQ(optVal, g_getOnlyOpt[i].optVal);
5309762338dSopenharmony_ci        EXPECT_EQ(optLen, g_getOnlyOpt[i].optLen);
5319762338dSopenharmony_ci        close(fd);
5329762338dSopenharmony_ci    }
5339762338dSopenharmony_ci
5349762338dSopenharmony_ci    close(fd);
5359762338dSopenharmony_ci}
5369762338dSopenharmony_ci
5379762338dSopenharmony_ci/*
5389762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_GETSOCKOPT_1000
5399762338dSopenharmony_ci * @tc.name   : SetAndGetsockoptInvalidFdFailed_0010
5409762338dSopenharmony_ci * @tc.desc   : setsockopt and getsockopt use invalid socket fd test failed.
5419762338dSopenharmony_ci * @tc.size   : MediumTest
5429762338dSopenharmony_ci * @tc.type   : Function
5439762338dSopenharmony_ci * @tc.level  : Level 2
5449762338dSopenharmony_ci */
5459762338dSopenharmony_ciHWTEST_F(HatsSockoptTest, SetAndGetsockoptInvalidFdFailed_0010, Function | MediumTest | Level2)
5469762338dSopenharmony_ci{
5479762338dSopenharmony_ci    int ret;
5489762338dSopenharmony_ci    int32_t optVal = 1;
5499762338dSopenharmony_ci    socklen_t optLen = sizeof(optVal);
5509762338dSopenharmony_ci
5519762338dSopenharmony_ci    errno = 0;
5529762338dSopenharmony_ci    ret = setsockopt(BAD_SOCKET_FD, IPPROTO_TCP, TCP_TIMESTAMP, &optVal, optLen);
5539762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
5549762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
5559762338dSopenharmony_ci
5569762338dSopenharmony_ci    errno = 0;
5579762338dSopenharmony_ci    ret = setsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_TIMESTAMP, &optVal, optLen);
5589762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
5599762338dSopenharmony_ci    EXPECT_EQ(errno, ENOTSOCK);
5609762338dSopenharmony_ci
5619762338dSopenharmony_ci    errno = 0;
5629762338dSopenharmony_ci    ret = getsockopt(BAD_SOCKET_FD, IPPROTO_TCP, TCP_TIMESTAMP, &optVal, &optLen);
5639762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
5649762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
5659762338dSopenharmony_ci
5669762338dSopenharmony_ci    errno = 0;
5679762338dSopenharmony_ci    ret = getsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_TIMESTAMP, &optVal, &optLen);
5689762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
5699762338dSopenharmony_ci    EXPECT_EQ(errno, ENOTSOCK);
5709762338dSopenharmony_ci}
571