1 /*
2 * Copyright (c) 2022 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 <arpa/inet.h>
17 #include <iostream>
18
19 #include <gtest/gtest.h>
20 #include "securec.h"
21
22 #include "client_trans_proxy_file_common.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_def.h"
25 #include "softbus_error_code.h"
26
27 #define TEST_FILE_LENGTH 10
28 #define TEST_INDEX 5
29 #define TEST_FRAME_NUMBER 3
30 #define TEST_FRAME_NUMBER_SECOND 10
31 #define TEST_BUFFER_SIZE 1024
32 #define TEST_FD (-1)
33 #define TEST_RETRY_TIMES 2
34 #define TEST_DATA 26559
35 #define TEST_NORMAL_FD 128
36
37 using namespace std;
38 using namespace testing::ext;
39
40 namespace OHOS {
41 const char *g_filePath = "data/...ss/";
42 const char *g_testFileName = "test.txt";
43 const char *g_fileSet1[] = {
44 "/data/data/test.txt",
45 "/path/max/length/512/"
46 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
47 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
48 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
49 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
50 "111111111111111111111111111111111111111111111111111",
51 "ss",
52 "/data/ss",
53 };
54
55 class ClientTransProxyFileCommonTest : public testing::Test {
56 public:
ClientTransProxyFileCommonTest()57 ClientTransProxyFileCommonTest() {}
~ClientTransProxyFileCommonTest()58 ~ClientTransProxyFileCommonTest() {}
59 static void SetUpTestCase(void);
60 static void TearDownTestCase(void);
SetUp()61 void SetUp() override {}
TearDown()62 void TearDown() override {}
63 };
64
SetUpTestCase(void)65 void ClientTransProxyFileCommonTest::SetUpTestCase(void) {}
TearDownTestCase(void)66 void ClientTransProxyFileCommonTest::TearDownTestCase(void) {}
67
68 /**
69 * @tc.name: FileListToBufferTest
70 * @tc.desc: file list to buffer test, use the normal parameter.
71 * @tc.type: FUNC
72 * @tc.require:
73 */
74 HWTEST_F(ClientTransProxyFileCommonTest, FileListToBufferTest, TestSize.Level0)
75 {
76 FileListBuffer bufferInfo = {0};
77 EXPECT_EQ(0, FileListToBuffer(g_fileSet1, sizeof(g_fileSet1) / sizeof(const char *), &bufferInfo));
78
79 int32_t fileCount = 0;
80 const char *oldFirstFileName = BufferToFileList(bufferInfo.buffer, bufferInfo.bufferSize, &fileCount);
81
82 ASSERT_NE(oldFirstFileName, nullptr);
83 EXPECT_EQ(fileCount, sizeof(g_fileSet1) / sizeof(const char *));
84
85 EXPECT_EQ(0, strcmp(oldFirstFileName, g_fileSet1[0]));
86
87 SoftBusFree(bufferInfo.buffer);
88 }
89
90 /**
91 * @tc.name: FileListToBufferTestBadInput1
92 * @tc.desc: file list to buffer test, use the wrong parameter.
93 * @tc.type: FUNC
94 * @tc.require:
95 */
96 HWTEST_F(ClientTransProxyFileCommonTest, FileListToBufferTestBadInput1, TestSize.Level0)
97 {
98 FileListBuffer bufferInfo = {0};
99 EXPECT_NE(0, FileListToBuffer(nullptr, sizeof(g_fileSet1) / sizeof(const char *), &bufferInfo));
100
101 EXPECT_EQ(bufferInfo.buffer, nullptr);
102 EXPECT_EQ(bufferInfo.bufferSize, 0);
103 }
104
105 /**
106 * @tc.name: FileListToBufferTestBadInput2
107 * @tc.desc: file list to buffer test, use the wrong parameter.
108 * @tc.type: FUNC
109 * @tc.require:
110 */
111 HWTEST_F(ClientTransProxyFileCommonTest, FileListToBufferTestBadInput2, TestSize.Level0)
112 {
113 FileListBuffer bufferInfo = {0};
114 EXPECT_NE(0, FileListToBuffer(g_fileSet1, 0, &bufferInfo));
115
116 EXPECT_EQ(bufferInfo.buffer, nullptr);
117 EXPECT_EQ(bufferInfo.bufferSize, 0);
118 }
119
120 /**
121 * @tc.name: FileListToBufferTestBadInput3
122 * @tc.desc: file list to buffer test, use the wrong parameter.
123 * @tc.type: FUNC
124 * @tc.require:
125 */
126 HWTEST_F(ClientTransProxyFileCommonTest, FileListToBufferTestBadInput3, TestSize.Level0)
127 {
128 EXPECT_NE(0, FileListToBuffer(g_fileSet1, sizeof(g_fileSet1) / sizeof(const char *), nullptr));
129 }
130
131 /**
132 * @tc.name: FileListToBufferTestBadInput4
133 * @tc.desc: file list to buffer test, use the wrong parameter.
134 * @tc.type: FUNC
135 * @tc.require:
136 */
137 HWTEST_F(ClientTransProxyFileCommonTest, FileListToBufferTestBadInput4, TestSize.Level0)
138 {
139 const char *fileSet[] = {
140 "/dev/path/to",
141 "/path/max/length/more/than/512/"
142 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
143 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
144 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
145 "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
146 "111111111111111111111111111111111111111111111111111"
147 };
148 FileListBuffer bufferInfo = {0};
149 EXPECT_EQ(SOFTBUS_INVALID_PARAM, FileListToBuffer(fileSet, sizeof(fileSet) / sizeof(const char *), &bufferInfo));
150 }
151
152 /**
153 * @tc.name: FileListToBufferTestBadInput5
154 * @tc.desc: file list to buffer test, use the wrong parameter.
155 * @tc.type: FUNC
156 * @tc.require:
157 */
158 HWTEST_F(ClientTransProxyFileCommonTest, FileListToBufferTestBadInput5, TestSize.Level0)
159 {
160 const char *fileSet[] = {"/dev/path/to", ""};
161 FileListBuffer bufferInfo = {0};
162 EXPECT_EQ(SOFTBUS_INVALID_PARAM, FileListToBuffer(fileSet, sizeof(fileSet) / sizeof(const char *), &bufferInfo));
163 }
164
165 /**
166 * @tc.name: ClinetTransProxyFilePathTest
167 * @tc.desc: client trans proxy file path test, use the wrong or normal parameter.
168 * @tc.type: FUNC
169 * @tc.require:
170 */
171 HWTEST_F(ClientTransProxyFileCommonTest, ClinetTransProxyFilePathTest, TestSize.Level0)
172 {
173 bool result = IsPathValid(nullptr);
174 EXPECT_EQ(false, result);
175
176 char filePath[TEST_FILE_LENGTH] = {0};
177 result = IsPathValid(filePath);
178 EXPECT_EQ(false, result);
179
180 result = IsPathValid(const_cast<char*>(g_fileSet1[1]));
181 EXPECT_EQ(false, result);
182
183 result = IsPathValid(const_cast<char*>(g_filePath));
184 EXPECT_EQ(false, result);
185
186 result = IsPathValid(const_cast<char*>(g_fileSet1[0]));
187 EXPECT_EQ(true, result);
188
189 int ret = GetAndCheckRealPath(nullptr, const_cast<char*>(g_fileSet1[0]));
190 EXPECT_EQ(SOFTBUS_ERR, ret);
191
192 char absPath[PATH_MAX] = {0};
193 ret = GetAndCheckRealPath(g_fileSet1[0], absPath);
194 EXPECT_EQ(SOFTBUS_ERR, ret);
195
196 ret = GetAndCheckRealPath(g_fileSet1[2], absPath);
197 EXPECT_EQ(SOFTBUS_ERR, ret);
198
199 result = CheckDestFilePathValid(nullptr);
200 EXPECT_EQ(false, result);
201
202 result = CheckDestFilePathValid(filePath);
203 EXPECT_EQ(false, result);
204
205 result = CheckDestFilePathValid(g_fileSet1[1]);
206 EXPECT_EQ(false, result);
207
208 result = CheckDestFilePathValid(g_filePath);
209 EXPECT_EQ(false, result);
210
211 result = CheckDestFilePathValid(g_fileSet1[2]);
212 EXPECT_EQ(true, result);
213
214 ret = FrameIndexToType(FRAME_NUM_0, FRAME_NUM_1);
215 EXPECT_EQ(TRANS_SESSION_FILE_FIRST_FRAME, ret);
216
217 ret = FrameIndexToType(FRAME_NUM_1, FRAME_NUM_2);
218 EXPECT_EQ(TRANS_SESSION_FILE_ONLYONE_FRAME, ret);
219
220 ret = FrameIndexToType(FRAME_NUM_2, TEST_FRAME_NUMBER);
221 EXPECT_EQ(TRANS_SESSION_FILE_LAST_FRAME, ret);
222
223 ret = FrameIndexToType(FRAME_NUM_2, TEST_FRAME_NUMBER_SECOND);
224 EXPECT_EQ(TRANS_SESSION_FILE_ONGOINE_FRAME, ret);
225 }
226
227 /**
228 * @tc.name: ClinetTransProxyFileNameTest
229 * @tc.desc: client trans proxy file name test, use the wrong or normal parameter.
230 * @tc.type: FUNC
231 * @tc.require:
232 */
233 HWTEST_F(ClientTransProxyFileCommonTest, ClinetTransProxyFileNameTest, TestSize.Level0)
234 {
235 uint16_t usDataLen = 1;
236 const unsigned char strTmp[] = "test";
237 uint16_t ret = RTU_CRC(strTmp, usDataLen);
238 EXPECT_EQ(TEST_DATA, ret);
239
240 const char *resultFirst = TransGetFileName(nullptr);
241 EXPECT_STREQ(nullptr, resultFirst);
242
243 const char fileName[TEST_FILE_LENGTH] = {0};
244 const char *resultSecond = TransGetFileName(fileName);
245 EXPECT_STREQ(nullptr, resultSecond);
246
247 const char *resultThird = TransGetFileName(g_filePath);
248 EXPECT_STREQ(nullptr, resultThird);
249
250 const char *resultFourth = TransGetFileName(g_testFileName);
251 EXPECT_STREQ(g_testFileName, resultFourth);
252
253 const char *resultFifth = TransGetFileName(g_fileSet1[0]);
254 EXPECT_STREQ(g_testFileName, resultFifth);
255 }
256
257 /**
258 * @tc.name: BufferToFileListTest
259 * @tc.desc: buffer to file list test, use the wrong or normal parameter.
260 * @tc.type: FUNC
261 * @tc.require:
262 */
263 HWTEST_F(ClientTransProxyFileCommonTest, BufferToFileListTest, TestSize.Level0)
264 {
265 int32_t fileCount = 0;
266 char *result = BufferToFileList(nullptr, TEST_BUFFER_SIZE, &fileCount);
267 EXPECT_STREQ(nullptr, result);
268
269 uint8_t buffer[] = {0};
270 result = BufferToFileList(buffer, 0, &fileCount);
271 EXPECT_STREQ(nullptr, result);
272
273 result = BufferToFileList(buffer, TEST_BUFFER_SIZE, nullptr);
274 EXPECT_STREQ(nullptr, result);
275
276 result = BufferToFileList(buffer, TEST_BUFFER_SIZE, &fileCount);
277 EXPECT_STREQ(nullptr, result);
278 }
279
280 /**
281 * @tc.name: FileLockTest
282 * @tc.desc: file lock test, use the wrong or normal parameter.
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286 HWTEST_F(ClientTransProxyFileCommonTest, FileLockTest, TestSize.Level0)
287 {
288 int fd = TEST_NORMAL_FD;
289 int ret = TryFileLock(fd, SOFTBUS_F_RDLCK, 0);
290 EXPECT_EQ(SOFTBUS_FILE_BUSY, ret);
291
292 ret = TryFileLock(TEST_FD, SOFTBUS_F_RDLCK, TEST_RETRY_TIMES);
293 EXPECT_EQ(SOFTBUS_ERR, ret);
294
295 ret = TryFileLock(fd, SOFTBUS_F_RDLCK, TEST_RETRY_TIMES);
296 EXPECT_NE(SOFTBUS_OK, ret);
297
298 ret = FileLock(fd, SOFTBUS_F_RDLCK, false);
299 EXPECT_NE(SOFTBUS_OK, ret);
300
301 ret = FileLock(fd, SOFTBUS_F_RDLCK, true);
302 EXPECT_EQ(SOFTBUS_OK, ret);
303
304 ret = FileUnLock(TEST_FD);
305 EXPECT_EQ(SOFTBUS_OK, ret);
306
307 ret = FileUnLock(fd);
308 EXPECT_NE(SOFTBUS_OK, ret);
309 }
310 } // namespace OHOS
311