1 /*
2 * Copyright (c) 2024 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 <cinttypes>
17 #include <chrono>
18 #include <thread>
19 #include <gtest/gtest.h>
20 #include "common.h"
21 #include "session.h"
22 #include "tmessenger.h"
23
24 #define SERVER_IDLE_WAIT_TIME 5
25
26 using namespace testing::ext;
27
28 namespace OHOS {
29 std::mutex g_recvMutex;
30 static std::string g_recvData;
31
32 class StreamEncryptServerMt : public testing::Test {
33 public:
StreamEncryptServerMt()34 StreamEncryptServerMt() { }
~StreamEncryptServerMt()35 ~StreamEncryptServerMt() { }
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
SetUp()38 void SetUp() override { }
TearDown()39 void TearDown() override { }
40 };
41
SetUpTestCase(void)42 void StreamEncryptServerMt::SetUpTestCase(void)
43 {
44 int32_t ret = TestInit();
45 ASSERT_EQ(ret, SOFTBUS_OK);
46
47 ret = TMessenger::GetInstance().Open(PKG_NAME, TEST_NOTIFY_SRV_NAME, "", true);
48 ASSERT_EQ(ret, SOFTBUS_OK);
49 }
50
TearDownTestCase(void)51 void StreamEncryptServerMt::TearDownTestCase(void)
52 {
53 int32_t ret = TestDeInit();
54 ASSERT_EQ(ret, SOFTBUS_OK);
55 TMessenger::GetInstance().Close();
56 }
57
OnBindServer(int32_t socket,PeerSocketInfo info)58 void OnBindServer(int32_t socket, PeerSocketInfo info)
59 {
60 LOGI(">> OnBind {socket:%d, name:%s, networkId:%s, pkgName:%s, dataType:%d}", socket, info.name, info.networkId,
61 info.pkgName, info.dataType);
62 }
63
OnShutdownServer(int32_t socket,ShutdownReason reason)64 void OnShutdownServer(int32_t socket, ShutdownReason reason)
65 {
66 LOGI(">> OnOnShutdown {socket:%d, reason:%d}", socket, reason);
67 }
68
OnStreamReceived(int32_t sessionId,const char * testCaseName,const StreamData * data)69 static void OnStreamReceived(int32_t sessionId, const char *testCaseName, const StreamData *data)
70 {
71 if (sessionId <= 0) {
72 LOGI(">> OnStreamReceived, invalid sessionId=%d", sessionId);
73 return;
74 }
75
76 if (testCaseName == nullptr) {
77 LOGI(">> OnStreamReceived, testCaseName is nullptr, sessionId=%d", sessionId);
78 return;
79 }
80
81 if (data == nullptr) {
82 LOGI(">> OnStreamReceived, data is nullptr, sessionId:%d", sessionId);
83 return;
84 }
85
86 LOGI(">> OnStreamReceived, sessionId:%d", sessionId);
87 LOGI(">> OnStreamReceived, testCaseName:%s", testCaseName);
88 LOGI(">> OnStreamReceived, buf:%s", (data->buf != nullptr ? data->buf : "null"));
89 LOGI(">> OnStreamReceived, bufLen:%d", data->bufLen);
90
91 std::lock_guard<std::mutex> lock(g_recvMutex);
92 g_recvData = std::string((char *)data->buf, data->bufLen);
93 }
94
OnStreamReceivedWithNoDataType(int32_t socket,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)95 static void OnStreamReceivedWithNoDataType(
96 int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
97 {
98 OnStreamReceived(socket, "RawStreamEncryptTestServer001", data);
99 }
100
101 static QosTV g_qosInfo[] = {
102 { .qos = QOS_TYPE_MIN_BW, .value = 80 },
103 { .qos = QOS_TYPE_MAX_LATENCY, .value = 4000 },
104 { .qos = QOS_TYPE_MIN_LATENCY, .value = 2000 },
105 };
106
107 /*
108 * @tc.name: RawStreamEncryptTestServer001
109 * @tc.desc: Unencrypted raw stream data transmission test
110 * @tc.type: FUNC
111 * @tc.require:
112 */
113 HWTEST_F(StreamEncryptServerMt, RawStreamEncryptTestServer001, TestSize.Level1)
114 {
115 /**
116 * @tc.steps: step 1. do not set dataType and create socket by 'Socket' function.
117 * @tc.expect: socket greater zero.
118 */
119 SocketInfo info = {
120 .name = (char *)TEST_SESSION_NAME_SRV,
121 .pkgName = (char *)PKG_NAME,
122 };
123 int32_t socket = Socket(info);
124 ASSERT_GT(socket, 0);
125
126 ISocketListener listener = {
127 .OnBind = OnBindServer,
128 .OnShutdown = OnShutdownServer,
129 .OnBytes = nullptr,
130 .OnMessage = nullptr,
131 .OnStream = OnStreamReceivedWithNoDataType,
132 .OnFile = nullptr,
133 .OnQos = nullptr,
134 };
135
136 int32_t ret = Listen(socket, g_qosInfo, sizeof(g_qosInfo) / sizeof(g_qosInfo[0]), &listener);
137 ASSERT_EQ(ret, SOFTBUS_OK);
138
139 /**
140 * @tc.steps: step 3. Register a callback interface for querying.
141 */
__anon0dc34adb0102null142 TMessenger::GetInstance().RegisterOnQuery([] {
143 std::lock_guard<std::mutex> lock(g_recvMutex);
144 std::shared_ptr<Response> resp = std::make_shared<Response>(false, g_recvData);
145 g_recvData.clear();
146 LOGI("isEcrtypr:%d, recvData:%s", resp->isEncrypt_, resp->recvData_.c_str());
147 return resp;
148 });
149
150 /**
151 * @tc.steps: step 4. Waiting for new connections.
152 */
153 while (true) {
154 LOG("waiting ...");
155 std::this_thread::sleep_for(std::chrono::seconds(SERVER_IDLE_WAIT_TIME));
156 }
157 Shutdown(socket);
158 }
159
OnStreamReceivedWithUnencryptOpt(int32_t socket,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)160 static void OnStreamReceivedWithUnencryptOpt(
161 int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
162 {
163 OnStreamReceived(socket, "RawStreamEncryptTestServer002", data);
164 }
165 /*
166 * @tc.name: RawStreamEncryptTestServer002
167 * @tc.desc: Unencrypted raw stream data transmission test
168 * @tc.type: FUNC
169 * @tc.require:
170 */
171 HWTEST_F(StreamEncryptServerMt, RawStreamEncryptTestServer002, TestSize.Level1)
172 {
173 /**
174 * @tc.steps: step 1. set dataType is DATA_TYPE_RAW_STREAM and create socket by 'Socket' function.
175 * @tc.expect: socket greater zero.
176 */
177 SocketInfo info = {
178 .name = (char *)TEST_SESSION_NAME_SRV,
179 .pkgName = (char *)PKG_NAME,
180 .dataType = DATA_TYPE_RAW_STREAM,
181 };
182 int32_t socket = Socket(info);
183 ASSERT_GT(socket, 0);
184
185 ISocketListener listener = {
186 .OnBind = OnBindServer,
187 .OnShutdown = OnShutdownServer,
188 .OnBytes = nullptr,
189 .OnMessage = nullptr,
190 .OnStream = OnStreamReceivedWithUnencryptOpt,
191 .OnFile = nullptr,
192 .OnQos = nullptr,
193 };
194
195 int32_t ret = Listen(socket, g_qosInfo, sizeof(g_qosInfo) / sizeof(g_qosInfo[0]), &listener);
196 ASSERT_EQ(ret, SOFTBUS_OK);
197
198 /**
199 * @tc.steps: step 3. Register a callback interface for querying.
200 */
__anon0dc34adb0202null201 TMessenger::GetInstance().RegisterOnQuery([] {
202 std::lock_guard<std::mutex> lock(g_recvMutex);
203 std::shared_ptr<Response> resp = std::make_shared<Response>(false, g_recvData);
204 g_recvData.clear();
205 LOGI("isEcrtypr:%d, recvData:%s", resp->isEncrypt_, resp->recvData_.c_str());
206 return resp;
207 });
208
209 /**
210 * @tc.steps: step 4. Waiting for new connections.
211 */
212 while (true) {
213 LOG("waiting ...");
214 std::this_thread::sleep_for(std::chrono::seconds(SERVER_IDLE_WAIT_TIME));
215 }
216 Shutdown(socket);
217 }
218
OnStreamReceivedWithEncryptOpt(int32_t socket,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)219 static void OnStreamReceivedWithEncryptOpt(
220 int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
221 {
222 OnStreamReceived(socket, "RawStreamEncryptTestServer003", data);
223 }
224
225 /*
226 * @tc.name: RawStreamEncryptTestServer003
227 * @tc.desc: Unencrypted raw stream data transmission test
228 * @tc.type: FUNC
229 * @tc.require:
230 */
231 HWTEST_F(StreamEncryptServerMt, RawStreamEncryptTestServer003, TestSize.Level1)
232 {
233 /**
234 * @tc.steps: step 1. set dataType is DATA_TYPE_RAW_STREAM and create socket by 'Socket' function.
235 * @tc.expect: socket greater zero.
236 */
237 SocketInfo info = {
238 .name = (char *)TEST_SESSION_NAME_SRV,
239 .pkgName = (char *)PKG_NAME,
240 .dataType = DATA_TYPE_RAW_STREAM_ENCRYPED,
241 };
242 int32_t socket = Socket(info);
243 ASSERT_GT(socket, 0);
244
245 ISocketListener listener = {
246 .OnBind = OnBindServer,
247 .OnShutdown = OnShutdownServer,
248 .OnBytes = nullptr,
249 .OnMessage = nullptr,
250 .OnStream = OnStreamReceivedWithEncryptOpt,
251 .OnFile = nullptr,
252 .OnQos = nullptr,
253 };
254
255 int32_t ret = Listen(socket, g_qosInfo, sizeof(g_qosInfo) / sizeof(g_qosInfo[0]), &listener);
256 ASSERT_EQ(ret, SOFTBUS_OK);
257
258 /**
259 * @tc.steps: step 3. Register a callback interface for querying.
260 */
__anon0dc34adb0302null261 TMessenger::GetInstance().RegisterOnQuery([] {
262 std::lock_guard<std::mutex> lock(g_recvMutex);
263 std::shared_ptr<Response> resp = std::make_shared<Response>(true, g_recvData);
264 g_recvData.clear();
265 LOGI("isEcrtypr:%d, recvData:%s", resp->isEncrypt_, resp->recvData_.c_str());
266 return resp;
267 });
268
269 /**
270 * @tc.steps: step 4. Waiting for new connections.
271 */
272 while (true) {
273 LOG("waiting ...");
274 std::this_thread::sleep_for(std::chrono::seconds(SERVER_IDLE_WAIT_TIME));
275 }
276 Shutdown(socket);
277 }
278
OnSessionOpenedServer(int32_t sessionId,int32_t result)279 static int32_t OnSessionOpenedServer(int32_t sessionId, int32_t result)
280 {
281 LOGI(">> OnSessionOpenedServer {sessionId:%d, result=%d", sessionId, result);
282 if (sessionId <= 0 || result != SOFTBUS_OK) {
283 return result;
284 }
285 return SOFTBUS_OK;
286 }
287
OnSessionClosedServer(int32_t sessionId)288 static void OnSessionClosedServer(int32_t sessionId)
289 {
290 LOGI(">> OnSessionClosedServer {sessionId:%d", sessionId);
291 }
292
OnStreamReceivedWithOldInterface(int32_t socket,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)293 static void OnStreamReceivedWithOldInterface(
294 int32_t socket, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
295 {
296 OnStreamReceived(socket, "RawStreamEncryptTestServer004", data);
297 }
298 /*
299 * @tc.name: RawStreamEncryptTestServer004
300 * @tc.desc: Use old interace as the server side.
301 * @tc.type: FUNC
302 * @tc.require:
303 */
304 HWTEST_F(StreamEncryptServerMt, RawStreamEncryptTestServer004, TestSize.Level1)
305 {
306 /**
307 * @tc.steps: step 1. call 'CreateSessionServer' function to start server.
308 * @tc.expect: return value is SOFTBUS_OK.
309 */
310 ISessionListener sessionListener = {
311 .OnSessionOpened = OnSessionOpenedServer,
312 .OnSessionClosed = OnSessionClosedServer,
313 .OnStreamReceived = OnStreamReceivedWithOldInterface,
314 };
315 int32_t ret = CreateSessionServer(PKG_NAME, TEST_SESSION_NAME_SRV, &sessionListener);
316 ASSERT_EQ(ret, SOFTBUS_OK);
317
318 /**
319 * @tc.steps: step 2. Register a callback interface for querying.
320 */
__anon0dc34adb0402null321 TMessenger::GetInstance().RegisterOnQuery([] {
322 std::lock_guard<std::mutex> lock(g_recvMutex);
323 std::shared_ptr<Response> resp = std::make_shared<Response>(false, g_recvData);
324 g_recvData.clear();
325 LOGI("isEcrtypr:%d, recvData:%s", resp->isEncrypt_, resp->recvData_.c_str());
326 return resp;
327 });
328
329 /**
330 * @tc.steps: step 3. Waiting for new connections.
331 */
332 while (true) {
333 LOG("waiting ...");
334 std::this_thread::sleep_for(std::chrono::seconds(SERVER_IDLE_WAIT_TIME));
335 }
336 RemoveSessionServer(PKG_NAME, TEST_SESSION_NAME_SRV);
337 }
338 } // namespace OHOS