• 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 <pthread.h>
23 #include <unistd.h>
24 #include <arpa/inet.h>
25 #include <gtest/gtest.h>
26 #include <netinet/in.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 static const int BUFFER_SIZE = 1024;
35 static const int TEST_PORT = 22354;
36 static const char *TEST_LOCAL_IP = "127.0.0.1";
37 static int g_serviceFd = -1;
38 
39 class HatsSendtoTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp();
44     void TearDown();
45 private:
46 };
SetUp()47 void HatsSendtoTest::SetUp()
48 {
49     int ret;
50     int socketFd = -1;
51     int32_t optVal = 1;
52     struct sockaddr_in serAddr = {
53         .sin_family = AF_INET,
54         .sin_port = htons(TEST_PORT),
55         .sin_addr = {
56             .s_addr = inet_addr(TEST_LOCAL_IP),
57         }
58     };
59 
60     socketFd = socket(AF_INET, SOCK_STREAM, 0);
61     EXPECT_TRUE(socketFd > 0);
62 
63     ret = setsockopt(socketFd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(optVal));
64     EXPECT_EQ(ret, 0);
65 
66     ret = bind(socketFd, reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(serAddr));
67     EXPECT_EQ(ret, 0);
68 
69     g_serviceFd = socketFd;
70 }
TearDown()71 void HatsSendtoTest::TearDown()
72 {
73     close(g_serviceFd);
74     g_serviceFd = -1;
75 }
SetUpTestCase()76 void HatsSendtoTest::SetUpTestCase()
77 {
78 }
TearDownTestCase()79 void HatsSendtoTest::TearDownTestCase()
80 {
81 }
82 
ClientConnect(void * args)83 static void *ClientConnect(void *args)
84 {
85     int ret;
86     ssize_t size;
87     int clientFd = -1;
88     char buffer[BUFFER_SIZE] = { 0 };
89     struct sockaddr_in serAddr = {
90         .sin_family = AF_INET,
91         .sin_port = htons(TEST_PORT),
92         .sin_addr = {
93             .s_addr = inet_addr(TEST_LOCAL_IP),
94         }
95     };
96 
97     clientFd = socket(AF_INET, SOCK_STREAM, 0);
98     EXPECT_TRUE(clientFd > 0);
99 
100     ret = connect(clientFd, reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(struct sockaddr_in));
101     EXPECT_EQ(ret, 0);
102 
103     struct sockaddr_in clientAddr;
104     socklen_t len = sizeof(clientAddr);
105 
106     size = recvfrom(clientFd, buffer, BUFFER_SIZE, 0, reinterpret_cast<struct sockaddr *>(&clientAddr), &len);
107     EXPECT_TRUE(size >= 0);
108 
109     close(clientFd);
110     return nullptr;
111 }
112 
113 /*
114  * @tc.number : SUB_KERNEL_SYSCALL_SENDTO_0100
115  * @tc.name   : SendtoSuccess_0001
116  * @tc.desc   : Sendto test success by MSG_MORE.
117  * @tc.size   : MediumTest
118  * @tc.type   : Function
119  * @tc.level  : Level 1
120  */
121 HWTEST_F(HatsSendtoTest, SendtoSuccess_0001, Function | MediumTest | Level1)
122 {
123     int ret;
124     ssize_t size;
125     pthread_t thread;
126     int acceptFd = -1;
127     int32_t backLog = 5;
128     struct sockaddr_in dstAddr = { 0 };
129     socklen_t addrLen = sizeof(struct sockaddr_in);
130 
131     ret = listen(g_serviceFd, backLog);
132     EXPECT_EQ(ret, 0);
133 
134     pthread_create(&thread, nullptr, ClientConnect, nullptr);
135 
136     acceptFd = accept4(g_serviceFd, reinterpret_cast<struct sockaddr *>(&dstAddr), &addrLen, 0);
137     EXPECT_TRUE(acceptFd > 0);
138 
139     struct sockaddr_in serAddr = {
140         .sin_family = AF_INET,
141         .sin_port = htons(TEST_PORT),
142         .sin_addr = {
143             .s_addr = inet_addr(TEST_LOCAL_IP),
144         }
145     };
146 
147     char buf1[] = "Hello, ";
148     char buf2[] = "world!";
149     // 第一次发送, 使用 MSG_MORE 标志,表示还有后续数据要发送
150     size = sendto(acceptFd, buf1, sizeof(buf1) - 1, MSG_MORE,
151                   reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(serAddr));
152     EXPECT_TRUE(size > 0);
153 
154     // 第二次发送, 不使用 MSG_MORE,表示数据发送完成
155     size = sendto(acceptFd, buf2, sizeof(buf2) - 1, 0,
156                   reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(serAddr));
157     EXPECT_TRUE(size > 0);
158 
159     close(acceptFd);
160     pthread_join(thread, nullptr);
161 }
162 
163 /*
164  * @tc.number : SUB_KERNEL_SYSCALL_SENDTO_0200
165  * @tc.name   : SendtoSuccess_0002
166  * @tc.desc   : Sendto test success by MSG_OOB.
167  * @tc.size   : MediumTest
168  * @tc.type   : Function
169  * @tc.level  : Level 1
170  */
171 HWTEST_F(HatsSendtoTest, SendtoSuccess_0002, Function | MediumTest | Level1)
172 {
173     int ret;
174     ssize_t size;
175     pthread_t thread;
176     int acceptFd = -1;
177     int32_t backLog = 5;
178     const char *message = "Hello, world!";
179     struct sockaddr_in dstAddr = { 0 };
180     socklen_t addrLen = sizeof(struct sockaddr_in);
181     struct sockaddr_in serAddr = {
182         .sin_family = AF_INET,
183         .sin_port = htons(TEST_PORT),
184         .sin_addr = {
185             .s_addr = inet_addr(TEST_LOCAL_IP),
186         }
187     };
188 
189     ret = listen(g_serviceFd, backLog);
190     EXPECT_EQ(ret, 0);
191 
192     pthread_create(&thread, nullptr, ClientConnect, nullptr);
193 
194     acceptFd = accept4(g_serviceFd, reinterpret_cast<struct sockaddr *>(&dstAddr), &addrLen, 0);
195     EXPECT_TRUE(acceptFd > 0);
196 
197     size = sendto(acceptFd, message, strlen(message), MSG_OOB,
198                   reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(serAddr));
199     EXPECT_TRUE(size > 0);
200     size = sendto(acceptFd, message, strlen(message), 0,
201                   reinterpret_cast<struct sockaddr *>(&serAddr), sizeof(serAddr));
202     EXPECT_TRUE(size >= 0);
203 
204     close(acceptFd);
205     pthread_join(thread, nullptr);
206 }