• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <arpa/inet.h>
24 #include <gtest/gtest.h>
25 #include <netinet/in.h>
26 #include <netinet/tcp.h>
27 #include <sys/stat.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include "securec.h"
31 
32 using namespace testing::ext;
33 
34 class HatsGetsockoptTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 private:
41 };
SetUp()42 void HatsGetsockoptTest::SetUp()
43 {
44 }
TearDown()45 void HatsGetsockoptTest::TearDown()
46 {
47 }
SetUpTestCase()48 void HatsGetsockoptTest::SetUpTestCase()
49 {
50 }
TearDownTestCase()51 void HatsGetsockoptTest::TearDownTestCase()
52 {
53 }
54 
55 /*
56  * @tc.number : SUB_KERNEL_SYSCALL_GETSOCKOPT_0100
57  * @tc.name   : GetsockoptSuccess_0001
58  * @tc.desc   : Getsockopt test success.
59  * @tc.size   : MediumTest
60  * @tc.type   : Function
61  * @tc.level  : Level 1
62  */
63 HWTEST_F(HatsGetsockoptTest, GetsockoptSuccess_0001, Function | MediumTest | Level1)
64 {
65     int ret;
66     int fd = -1;
67     struct ucred ucred;
68     socklen_t optLen;
69 
70     fd = socket(AF_UNIX, SOCK_STREAM, 0);
71     EXPECT_TRUE(fd > 0);
72 
73     ret = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &optLen);
74     EXPECT_EQ(ret, 0);
75     EXPECT_TRUE(ucred.uid != -1);
76 
77     close(fd);
78 }
79 
80 /*
81  * @tc.number : SUB_KERNEL_SYSCALL_GETSOCKOPT_0200
82  * @tc.name   : GetsockoptSuccess_0002
83  * @tc.desc   : getsockopt SO_SNDBUF option test success.
84  * @tc.size   : MediumTest
85  * @tc.type   : Function
86  * @tc.level  : Level 1
87  */
88 HWTEST_F(HatsGetsockoptTest, GetsockoptSuccess_0002, Function | MediumTest | Level1)
89 {
90     int ret;
91     int fd = -1;
92     int32_t optVal;
93     socklen_t optLen;
94 
95     fd = socket(AF_INET, SOL_SOCKET, 0);
96     EXPECT_TRUE(fd > 0);
97 
98     optVal = 0x20000;
99     optLen = sizeof(optVal);
100     ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optVal, optLen);
101     EXPECT_EQ(ret, 0);
102 
103     optVal = 0;
104     optLen = sizeof(optVal);
105     ret = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optVal, &optLen);
106     EXPECT_EQ(ret, 0);
107     EXPECT_TRUE(optVal > 0);
108 
109     close(fd);
110 }
111 
112 /*
113  * @tc.number : SUB_KERNEL_SYSCALL_GETSOCKOPT_0300
114  * @tc.name   : GetsockoptSuccess_0003
115  * @tc.desc   : getsockopt SO_RCVBUF option test success.
116  * @tc.size   : MediumTest
117  * @tc.type   : Function
118  * @tc.level  : Level 1
119  */
120 HWTEST_F(HatsGetsockoptTest, GetsockoptSuccess_0003, Function | MediumTest | Level1)
121 {
122     int ret;
123     int fd = -1;
124     int32_t optVal;
125     socklen_t optLen;
126 
127     fd = socket(AF_INET, SOL_SOCKET, 0);
128     EXPECT_TRUE(fd > 0);
129 
130     optVal = 0x20000;
131     optLen = sizeof(optVal);
132     ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &optVal, optLen);
133     EXPECT_EQ(ret, 0);
134     EXPECT_TRUE(optVal > 0);
135 
136     optVal = 0;
137     optLen = sizeof(optVal);
138     ret = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &optVal, &optLen);
139     EXPECT_EQ(ret, 0);
140 
141     close(fd);
142 }