1 /*
2 * Copyright (c) 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include "securec.h"
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include "client_trans_proxy_file_helper.c"
23 #include "client_trans_proxy_file_helper_mock.h"
24 #include "softbus_access_token_test.h"
25 #include "softbus_def.h"
26 #include "softbus_error_code.h"
27 #include "trans_proxy_process_data.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 class ClientTransProxyFileHelperMockTest : public testing::Test {
34 public:
ClientTransProxyFileHelperMockTest()35 ClientTransProxyFileHelperMockTest() {}
~ClientTransProxyFileHelperMockTest()36 ~ClientTransProxyFileHelperMockTest() {}
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
SetUp()39 void SetUp() override {}
TearDown()40 void TearDown() override {}
41 };
42
SetUpTestCase(void)43 void ClientTransProxyFileHelperMockTest::SetUpTestCase(void)
44 {
45 }
46
TearDownTestCase(void)47 void ClientTransProxyFileHelperMockTest::TearDownTestCase(void)
48 {
49 }
50
51 /**
52 * @tc.name: SendFileTransResult001
53 * @tc.desc: SendFileTransResult, use normal parameter.
54 * @tc.type: FUNC
55 * @tc.require:
56 */
57 HWTEST_F(ClientTransProxyFileHelperMockTest, SendFileTransResult001, TestSize.Level1)
58 {
59 int32_t ret = 0;
60 int32_t channelId = 1;
61 uint32_t seq = 1;
62 uint64_t seq64 = 1;
63 int32_t result = 0;
64 uint32_t side = 0;
65
66 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
67 EXPECT_CALL(ClientHelperMock, SoftBusHtoLl).WillRepeatedly(Return(seq));
68 EXPECT_CALL(ClientHelperMock, SoftBusHtoLll).WillRepeatedly(Return(seq64));
69 EXPECT_CALL(ClientHelperMock, ClientTransProxyGetInfoByChannelId).WillRepeatedly(Return(SOFTBUS_OK));
70 EXPECT_CALL(ClientHelperMock, ClientTransProxyPackAndSendData).WillRepeatedly(Return(SOFTBUS_OK));
71 ret = SendFileTransResult(channelId, seq, result, side);
72 EXPECT_EQ(SOFTBUS_OK, ret);
73 }
74
75 /**
76 * @tc.name: SendFileAckReqAndResData001
77 * @tc.desc: SendFileAckReqAndResData, use normal parameter.
78 * @tc.type: FUNC
79 * @tc.require:
80 */
81 HWTEST_F(ClientTransProxyFileHelperMockTest, SendFileAckReqAndResData001, TestSize.Level1)
82 {
83 int32_t ret = 0;
84 int32_t channelId = 1;
85 uint32_t seq = 1;
86 uint64_t seq64 = 1;
87 int32_t result = 0;
88 uint32_t side = 0;
89
90 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
91 EXPECT_CALL(ClientHelperMock, SoftBusHtoLl).WillRepeatedly(Return(seq));
92 EXPECT_CALL(ClientHelperMock, SoftBusHtoLll).WillRepeatedly(Return(seq64));
93 EXPECT_CALL(ClientHelperMock, ClientTransProxyGetInfoByChannelId).WillRepeatedly(Return(SOFTBUS_OK));
94 EXPECT_CALL(ClientHelperMock, ClientTransProxyPackAndSendData).WillRepeatedly(Return(SOFTBUS_OK));
95 ret = SendFileAckReqAndResData(channelId, seq, result, side);
96 EXPECT_EQ(SOFTBUS_OK, ret);
97 }
98
99 /**
100 * @tc.name: PackReadFileData001
101 * @tc.desc: PackReadFileData, use abnormal parameter.
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(ClientTransProxyFileHelperMockTest, PackReadFileData001, TestSize.Level1)
106 {
107 int64_t ret = 0;
108 FileFrame *fileFrame = reinterpret_cast<FileFrame*>(SoftBusCalloc(sizeof(FileFrame)));
109 SendListenerInfo *info = reinterpret_cast<SendListenerInfo*>(SoftBusCalloc(sizeof(SendListenerInfo)));
110 fileFrame->data = reinterpret_cast<uint8_t*>(SoftBusCalloc(sizeof(uint64_t) * 64));
111 fileFrame->fileData = reinterpret_cast<uint8_t*>(SoftBusCalloc(sizeof(uint32_t) * 64));
112 info->crc = APP_INFO_FILE_FEATURES_SUPPORT;
113 info->osType = OH_TYPE;
114 info->packetSize = 0;
115 uint32_t seq = 0;
116 uint64_t seq64 = 0;
117 uint16_t seq16 = 0;
118 uint64_t readLength = 8;
119 uint64_t fileOffset = 0;
120 int64_t len = 1;
121 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
122
123 ret = PackReadFileData(NULL, readLength, fileOffset, info);
124 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
125 ret = PackReadFileData(fileFrame, readLength, fileOffset, NULL);
126 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
127
128 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillRepeatedly(Return(len));
129 ret = PackReadFileData(fileFrame, readLength, fileOffset, info);
130 EXPECT_EQ(SOFTBUS_TRANS_INVALID_DATA_LENGTH, ret);
131
132
133 info->packetSize = 64;
134 EXPECT_CALL(ClientHelperMock, SoftBusHtoLl).WillRepeatedly(Return(seq));
135 EXPECT_CALL(ClientHelperMock, SoftBusHtoLll).WillRepeatedly(Return(seq64));
136 EXPECT_CALL(ClientHelperMock, SoftBusHtoLs).WillRepeatedly(Return(seq16));
137 EXPECT_CALL(ClientHelperMock, RTU_CRC).WillOnce(Return(seq16));
138 ret = PackReadFileData(fileFrame, readLength, fileOffset, info);
139 EXPECT_EQ(len, ret);
140
141 SoftBusFree(fileFrame->data);
142 SoftBusFree(fileFrame->fileData);
143 SoftBusFree(fileFrame);
144 SoftBusFree(info);
145 }
146
147 /**
148 * @tc.name: PackReadFileData002
149 * @tc.desc: PackReadFileData, use abnormal parameter.
150 * @tc.type: FUNC
151 * @tc.require:
152 */
153 HWTEST_F(ClientTransProxyFileHelperMockTest, PackReadFileData002, TestSize.Level1)
154 {
155 int64_t ret = 0;
156 FileFrame *fileFrame = reinterpret_cast<FileFrame*>(SoftBusCalloc(sizeof(FileFrame)));
157 SendListenerInfo *info = reinterpret_cast<SendListenerInfo*>(SoftBusCalloc(sizeof(SendListenerInfo)));
158 fileFrame->data = reinterpret_cast<uint8_t*>(SoftBusCalloc(sizeof(uint64_t) * 64));
159 fileFrame->fileData = reinterpret_cast<uint8_t*>(SoftBusCalloc(sizeof(uint32_t) * 64));
160 info->crc = APP_INFO_FILE_FEATURES_SUPPORT;
161 info->osType = 0;
162 info->packetSize = 0;
163 uint64_t readLength = 8;
164 uint64_t fileOffset = 0;
165 int64_t len = UINT32_MAX;
166 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
167
168 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillOnce(Return(len));
169 ret = PackReadFileData(fileFrame, readLength, fileOffset, info);
170 EXPECT_EQ(SOFTBUS_INVALID_NUM, ret);
171
172 len = 64;
173 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillOnce(Return(len));
174 ret = PackReadFileData(fileFrame, readLength, fileOffset, info);
175 EXPECT_EQ(SOFTBUS_TRANS_INVALID_DATA_LENGTH, ret);
176
177 SoftBusFree(fileFrame->data);
178 SoftBusFree(fileFrame->fileData);
179 SoftBusFree(fileFrame);
180 SoftBusFree(info);
181 }
182
183 /**
184 * @tc.name: PackReadFileRetransData001
185 * @tc.desc: PackReadFileRetransData, use abnormal parameter.
186 * @tc.type: FUNC
187 * @tc.require:
188 */
189 HWTEST_F(ClientTransProxyFileHelperMockTest, PackReadFileRetransData001, TestSize.Level1)
190 {
191 int64_t ret = 0;
192 FileFrame *fileFrame = reinterpret_cast<FileFrame*>(SoftBusCalloc(sizeof(FileFrame)));
193 SendListenerInfo *info = reinterpret_cast<SendListenerInfo*>(SoftBusCalloc(sizeof(SendListenerInfo)));
194 fileFrame->data = reinterpret_cast<uint8_t*>(SoftBusCalloc(sizeof(uint64_t) * 64));
195 fileFrame->fileData = reinterpret_cast<uint8_t*>(SoftBusCalloc(sizeof(uint32_t) * 64));
196 info->crc = APP_INFO_FILE_FEATURES_SUPPORT;
197 info->osType = OH_TYPE;
198 info->packetSize = 0;
199 uint32_t seq = 0;
200 uint64_t seq64 = 0;
201 uint16_t seq16 = 0;
202 uint64_t readLength = 8;
203 uint64_t fileOffset = 0;
204 int64_t len = 1;
205 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
206
207 EXPECT_CALL(ClientHelperMock, FrameIndexToType).WillRepeatedly(Return(0));
208 ret = PackReadFileRetransData(NULL, seq, readLength, fileOffset, info);
209 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
210 ret = PackReadFileRetransData(fileFrame, seq, readLength, fileOffset, NULL);
211 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
212
213 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillRepeatedly(Return(len));
214 ret = PackReadFileRetransData(fileFrame, seq, readLength, fileOffset, info);
215 EXPECT_EQ(SOFTBUS_TRANS_INVALID_DATA_LENGTH, ret);
216
217
218 info->packetSize = 64;
219 EXPECT_CALL(ClientHelperMock, SoftBusHtoLl).WillRepeatedly(Return(seq));
220 EXPECT_CALL(ClientHelperMock, SoftBusHtoLll).WillRepeatedly(Return(seq64));
221 EXPECT_CALL(ClientHelperMock, SoftBusHtoLs).WillRepeatedly(Return(seq16));
222 EXPECT_CALL(ClientHelperMock, RTU_CRC).WillOnce(Return(seq16));
223 EXPECT_CALL(ClientHelperMock, ClientTransProxyGetInfoByChannelId).WillRepeatedly(Return(SOFTBUS_OK));
224 EXPECT_CALL(ClientHelperMock, ClientTransProxyPackAndSendData).WillRepeatedly(Return(SOFTBUS_OK));
225 ret = PackReadFileRetransData(fileFrame, seq, readLength, fileOffset, info);
226 EXPECT_EQ(len, ret);
227
228 SoftBusFree(fileFrame->data);
229 SoftBusFree(fileFrame->fileData);
230 SoftBusFree(fileFrame);
231 SoftBusFree(info);
232 }
233
234 /**
235 * @tc.name: UnpackFileDataFrame001
236 * @tc.desc: UnpackFileDataFrame, use abnormal parameter.
237 * @tc.type: FUNC
238 * @tc.require:
239 */
240 HWTEST_F(ClientTransProxyFileHelperMockTest, UnpackFileDataFrame001, TestSize.Level1)
241 {
242 int32_t ret = 0;
243 FileFrame *fileFrame = reinterpret_cast<FileFrame*>(SoftBusCalloc(sizeof(FileFrame)));
244 FileRecipientInfo *info = reinterpret_cast<FileRecipientInfo*>(SoftBusCalloc(sizeof(FileRecipientInfo)));
245 uint32_t *fileDataLen = reinterpret_cast<uint32_t *>(SoftBusCalloc(sizeof(uint32_t)));
246 fileFrame->data = reinterpret_cast<uint8_t*>(SoftBusCalloc(sizeof(uint64_t) * 64));
247 fileFrame->frameLength = FRAME_HEAD_LEN + FRAME_CRC_LEN + 16;
248 fileFrame->magic = FILE_MAGIC_NUMBER;
249
250 info->crc = APP_INFO_FILE_FEATURES_SUPPORT;
251 info->osType = OH_TYPE;
252 *fileDataLen = 0;
253 uint32_t seq16 = 0;
254 uint64_t dataLen = 16;
255 int32_t magic = FILE_MAGIC_NUMBER;
256 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
257
258 ret = UnpackFileDataFrame(NULL, fileFrame, fileDataLen);
259 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
260 ret = UnpackFileDataFrame(info, NULL, fileDataLen);
261 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
262 ret = UnpackFileDataFrame(info, fileFrame, NULL);
263 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
264
265 EXPECT_CALL(ClientHelperMock, SoftBusLtoHl).WillRepeatedly(Return(magic));
266 EXPECT_CALL(ClientHelperMock, SoftBusLtoHll).WillRepeatedly(Return(dataLen));
267 EXPECT_CALL(ClientHelperMock, SoftBusHtoLs).WillRepeatedly(Return(seq16));
268 EXPECT_CALL(ClientHelperMock, RTU_CRC).WillOnce(Return(seq16));
269 ret = UnpackFileDataFrame(info, fileFrame, fileDataLen);
270 EXPECT_EQ(SOFTBUS_OK, ret);
271
272 EXPECT_CALL(ClientHelperMock, SoftBusLtoHl).WillRepeatedly(Return(magic));
273 EXPECT_CALL(ClientHelperMock, SoftBusLtoHll).WillRepeatedly(Return(dataLen));
274 EXPECT_CALL(ClientHelperMock, SoftBusHtoLs).WillRepeatedly(Return(seq16));
275 EXPECT_CALL(ClientHelperMock, RTU_CRC).WillOnce(Return(seq16 + 1));
276 ret = UnpackFileDataFrame(info, fileFrame, fileDataLen);
277 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
278
279 SoftBusFree(fileFrame->data);
280 SoftBusFree(fileFrame);
281 SoftBusFree(info);
282 }
283
284 /**
285 * @tc.name: AckResponseDataHandle001
286 * @tc.desc: AckResponseDataHandle, use abnormal parameter.
287 * @tc.type: FUNC
288 * @tc.require:
289 */
290 HWTEST_F(ClientTransProxyFileHelperMockTest, AckResponseDataHandle001, TestSize.Level1)
291 {
292 int32_t ret = 0;
293 SendListenerInfo *info = reinterpret_cast<SendListenerInfo*>(SoftBusCalloc(sizeof(SendListenerInfo)));
294 AckResponseData *resData = reinterpret_cast<AckResponseData *>(SoftBusCalloc(sizeof(AckResponseData)));
295 resData->startSeq = 0;
296 resData->seqResult = FILE_SEND_ACK_RESULT_SUCCESS;
297 char *data = (char *)resData;
298 uint32_t len = sizeof(AckResponseData) + 1;
299
300 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
301 EXPECT_CALL(ClientHelperMock, FrameIndexToType).WillRepeatedly(Return(0));
302 ret = AckResponseDataHandle(NULL, data, len);
303 EXPECT_EQ(SOFTBUS_OK, ret);
304 ret = AckResponseDataHandle(info, NULL, len);
305 EXPECT_EQ(SOFTBUS_OK, ret);
306 ret = AckResponseDataHandle(info, data, len - 1);
307 EXPECT_EQ(SOFTBUS_OK, ret);
308
309 ret = AckResponseDataHandle(info, data, len);
310 EXPECT_EQ(SOFTBUS_OK, ret);
311
312 SoftBusFree(info);
313 SoftBusFree(resData);
314 }
315
316 /**
317 * @tc.name: RetransFileFrameBySeq001
318 * @tc.desc: RetransFileFrameBySeq, use abnormal parameter.
319 * @tc.type: FUNC
320 * @tc.require:
321 */
322 HWTEST_F(ClientTransProxyFileHelperMockTest, RetransFileFrameBySeq001, TestSize.Level1)
323 {
324 int32_t ret = 0;
325 SendListenerInfo *info = reinterpret_cast<SendListenerInfo*>(SoftBusCalloc(sizeof(SendListenerInfo)));
326 info->crc = APP_INFO_FILE_FEATURES_SUPPORT;
327 info->packetSize = 64;
328 info->fileSize = 1;
329 uint32_t seq = 1;
330 uint64_t seq64 = 1;
331 uint16_t seq16 = 1;
332 int32_t len = 0;
333
334 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
335 EXPECT_CALL(ClientHelperMock, FrameIndexToType).WillRepeatedly(Return(0));
336 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillOnce(Return(len));
337 ret = RetransFileFrameBySeq(info, seq);
338 EXPECT_EQ(SOFTBUS_TRANS_INVALID_DATA_LENGTH, ret);
339
340 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillOnce(Return(len + 1));
341 EXPECT_CALL(ClientHelperMock, SoftBusHtoLl).WillRepeatedly(Return(seq));
342 EXPECT_CALL(ClientHelperMock, SoftBusHtoLll).WillRepeatedly(Return(seq64));
343 EXPECT_CALL(ClientHelperMock, SoftBusHtoLs).WillRepeatedly(Return(seq16));
344 EXPECT_CALL(ClientHelperMock, RTU_CRC).WillOnce(Return(seq16));
345 EXPECT_CALL(ClientHelperMock, ClientTransProxyGetInfoByChannelId).WillRepeatedly(Return(SOFTBUS_OK));
346 EXPECT_CALL(ClientHelperMock, ClientTransProxyPackAndSendData).WillRepeatedly(Return(SOFTBUS_OK));
347 ret = RetransFileFrameBySeq(info, seq);
348 SoftBusFree(info);
349 }
350
351 /**
352 * @tc.name: GetAbsFullPath001
353 * @tc.desc: GetAbsFullPath, use abnormal parameter.
354 * @tc.type: FUNC
355 * @tc.require:
356 */
357 HWTEST_F(ClientTransProxyFileHelperMockTest, GetAbsFullPath001, TestSize.Level1)
358 {
359 int32_t ret = 0;
360 SendListenerInfo *info = reinterpret_cast<SendListenerInfo*>(SoftBusCalloc(sizeof(SendListenerInfo)));
361 info->crc = APP_INFO_FILE_FEATURES_SUPPORT;
362 info->packetSize = 64;
363 info->fileSize = 1;
364 uint32_t seq = 1;
365 uint64_t seq64 = 1;
366 uint16_t seq16 = 1;
367 int32_t len = 0;
368
369 NiceMock<ClientTransProxyFileHelperInterfaceMock> ClientHelperMock;
370
371 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillOnce(Return(len));
372 ret = RetransFileFrameBySeq(info, seq);
373 EXPECT_EQ(SOFTBUS_TRANS_INVALID_DATA_LENGTH, ret);
374
375 EXPECT_CALL(ClientHelperMock, SoftBusPreadFile).WillOnce(Return(len + 1));
376 EXPECT_CALL(ClientHelperMock, SoftBusHtoLl).WillRepeatedly(Return(seq));
377 EXPECT_CALL(ClientHelperMock, SoftBusHtoLll).WillRepeatedly(Return(seq64));
378 EXPECT_CALL(ClientHelperMock, SoftBusHtoLs).WillRepeatedly(Return(seq16));
379 EXPECT_CALL(ClientHelperMock, RTU_CRC).WillOnce(Return(seq16));
380 EXPECT_CALL(ClientHelperMock, ClientTransProxyGetInfoByChannelId).WillRepeatedly(Return(SOFTBUS_OK));
381 EXPECT_CALL(ClientHelperMock, ClientTransProxyPackAndSendData).WillRepeatedly(Return(SOFTBUS_OK));
382 ret = RetransFileFrameBySeq(info, seq);
383 SoftBusFree(info);
384 }
385 }