1 /*A
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 #include "common_func.h"
16 #include "error_code.h"
17 #include "oh_file_uri.h"
18 #include "parameter.h"
19
20 #include <cstdlib>
21 #include <cstring>
22 #include <gtest/gtest.h>
23 #include <string>
24
25 using namespace testing::ext;
26 using namespace OHOS::AppFileService;
27 namespace {
28 const std::string BUNDLE_A = "com.example.filesharea";
29 const std::string FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER_STR = "const.filemanager.full_mount.enable";
30 } // namespace
31
GetSelfBundleName()32 std::string CommonFunc::GetSelfBundleName()
33 {
34 return BUNDLE_A;
35 }
36
37 namespace OHOS::AppFileService::ModuleFileUri {
38
FreeResult(char ** freeResult)39 static void FreeResult(char **freeResult)
40 {
41 if ((*freeResult) != nullptr) {
42 free(*freeResult);
43 *freeResult = nullptr;
44 }
45 }
46
CheckFileManagerFullMountEnable()47 static bool CheckFileManagerFullMountEnable()
48 {
49 char value[] = "false";
50 const char *fileManagerFullMountEnableParameter = FILE_MANAGER_FULL_MOUNT_ENABLE_PARAMETER_STR.c_str();
51 int retSystem = GetParameter(fileManagerFullMountEnableParameter, "false", value, sizeof(value));
52 if (retSystem > 0 && !std::strcmp(value, "true")) {
53 return true;
54 }
55 GTEST_LOG_(INFO) << "Not supporting all mounts";
56 return false;
57 }
58
59 class NDKFileUriTest : public testing::Test {
60 public:
SetUpTestCase(void)61 static void SetUpTestCase(void) {};
TearDownTestCase(void)62 static void TearDownTestCase(void) {};
SetUp()63 void SetUp() {};
TearDown()64 void TearDown() {};
65 };
66
67 /**
68 * @tc.number: get_path_from_uri_test_001
69 * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for sandbox uri
70 * @tc.desc: Set uri and get path
71 * @tc.type: FUNC
72 */
73 HWTEST_F(NDKFileUriTest, get_path_from_uri_test_001, TestSize.Level1)
74 {
75 GTEST_LOG_(INFO) << "get_path_from_uri_test_001 start";
76 std::string fileUriStr = "file://" + BUNDLE_A + "/data/storage/el2/base/files/GetPathFromUri001.txt";
77 const char *fileUri = fileUriStr.c_str();
78 const char filePath[] = "/data/storage/el2/base/files/GetPathFromUri001.txt";
79 char *result = nullptr;
80 unsigned int length = fileUriStr.size();
81 FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result);
82 EXPECT_EQ(ret, ERR_OK);
83 if (result != nullptr) {
84 GTEST_LOG_(INFO) << result;
85 EXPECT_EQ(strcmp(result, filePath), 0);
86 FreeResult(&result);
87 }
88 GTEST_LOG_(INFO) << "get_path_from_uri_test_001 end";
89 }
90
91 /**
92 * @tc.number: get_path_from_uri_test_002
93 * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for document uri
94 * @tc.desc: Set uri and get path
95 * @tc.type: FUNC
96 */
97 HWTEST_F(NDKFileUriTest, get_path_from_uri_test_002, TestSize.Level1)
98 {
99 GTEST_LOG_(INFO) << "get_path_from_uri_test_002 start";
100 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri002.txt";
101 char *result = nullptr;
102 unsigned int length = strlen(fileUri);
103 FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result);
104 EXPECT_EQ(ret, ERR_OK);
105 GTEST_LOG_(INFO) << result;
106 if (!CheckFileManagerFullMountEnable()) {
107 const char filePath[] =
108 "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/GetPathFromUri002.txt";
109 EXPECT_EQ(strcmp(result, filePath), 0);
110 } else {
111 const char filePath[] = "/storage/Users/currentUser/Documents/GetPathFromUri002.txt";
112 EXPECT_EQ(strcmp(result, filePath), 0);
113 }
114 FreeResult(&result);
115 GTEST_LOG_(INFO) << "get_path_from_uri_test_002 end";
116 }
117
118 /**
119 * @tc.number: get_path_from_uri_test_003
120 * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for different applications uri
121 * @tc.desc: Set uri and get path
122 * @tc.type: FUNC
123 */
124 HWTEST_F(NDKFileUriTest, get_path_from_uri_test_003, TestSize.Level1)
125 {
126 GTEST_LOG_(INFO) << "get_path_from_uri_test_003 start";
127 const char fileUri[] = "file://demoa/data/storage/el2/base/files/GetPathFromUri003.txt";
128 std::string filePathStr = "/data/storage/el2/share/r/demoa/data/storage/el2/base/files/GetPathFromUri003.txt";
129 const char *filePath = filePathStr.c_str();
130 char *result = nullptr;
131 unsigned int length = strlen(fileUri);
132 FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result);
133 EXPECT_EQ(ret, ERR_OK);
134 if (result != nullptr) {
135 GTEST_LOG_(INFO) << result;
136 EXPECT_EQ(strcmp(result, filePath), 0);
137 FreeResult(&result);
138 }
139 GTEST_LOG_(INFO) << "get_path_from_uri_test_003 end";
140 }
141
142 /**
143 * @tc.number: get_path_from_uri_test_004
144 * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for distributed files uri
145 * @tc.desc: Set uri and get path
146 * @tc.type: FUNC
147 */
148 HWTEST_F(NDKFileUriTest, get_path_from_uri_test_004, TestSize.Level1)
149 {
150 GTEST_LOG_(INFO) << "get_path_from_uri_test_004 start";
151 std::string fileUriStr = "file://" + BUNDLE_A + "/data/storage/el2/distributedfiles/.remote_share/";
152 fileUriStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri004.txt";
153 fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9";
154 const char *fileUri = fileUriStr.c_str();
155 std::string filePathStr =
156 "/data/storage/el2/share/r/" + BUNDLE_A + "/data/storage/el2/distributedfiles/.remote_share/";
157 filePathStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri004.txt";
158 const char *filePath = filePathStr.c_str();
159 char *result = nullptr;
160 unsigned int length = fileUriStr.size();
161 FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result);
162 EXPECT_EQ(ret, ERR_OK);
163 if (result != nullptr) {
164 GTEST_LOG_(INFO) << result;
165 EXPECT_EQ(strcmp(result, filePath), 0);
166 FreeResult(&result);
167 }
168 GTEST_LOG_(INFO) << "get_path_from_uri_test_004 end";
169 }
170
171 /**
172 * @tc.number: get_path_from_uri_test_005
173 * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for distributed files uri
174 * @tc.desc: Set uri and get path
175 * @tc.type: FUNC
176 */
177 HWTEST_F(NDKFileUriTest, get_path_from_uri_test_005, TestSize.Level1)
178 {
179 GTEST_LOG_(INFO) << "get_path_from_uri_test_005 start";
180 std::string fileUriStr = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt";
181 fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9";
182 const char *fileUri = fileUriStr.c_str();
183 const char filePath[] = "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/GetPathFromUri005.txt";
184 char *result = nullptr;
185 unsigned int length = fileUriStr.size();
186 FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result);
187 EXPECT_EQ(ret, ERR_OK);
188 if (result != nullptr) {
189 GTEST_LOG_(INFO) << result;
190 EXPECT_EQ(strcmp(result, filePath), 0);
191 FreeResult(&result);
192 }
193 GTEST_LOG_(INFO) << "get_path_from_uri_test_005 end";
194 }
195
196 /**
197 * @tc.number: get_path_from_uri_test_006
198 * @tc.name: Test function of OH_FileUri_GetPathFromUri() interface for distributed files uri
199 * @tc.desc: Set uri and get path
200 * @tc.type: FUNC
201 */
202 HWTEST_F(NDKFileUriTest, get_path_from_uri_test_006, TestSize.Level1)
203 {
204 GTEST_LOG_(INFO) << "get_path_from_uri_test_006 start";
205 std::string bundleB = "com.example.fileshareb";
206 std::string fileUriStr = "file://" + bundleB + "/data/storage/el2/distributedfiles/.remote_share/";
207 fileUriStr += "data/storage/el2/base/haps/entry/files/GetPathFromUri006.txt";
208 fileUriStr += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9";
209 const char *fileUri = fileUriStr.c_str();
210 std::string filePathUri = "/data/storage/el2/share/r/" + bundleB;
211 filePathUri += "/data/storage/el2/distributedfiles/.remote_share/";
212 filePathUri += "data/storage/el2/base/haps/entry/files/GetPathFromUri006.txt";
213 const char *filePath = filePathUri.c_str();
214 char *result = nullptr;
215 unsigned int length = fileUriStr.size();
216 FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(fileUri, length, &result);
217 EXPECT_EQ(ret, ERR_OK);
218 if (result != nullptr) {
219 GTEST_LOG_(INFO) << result;
220 EXPECT_EQ(strcmp(result, filePath), 0);
221 FreeResult(&result);
222 }
223 GTEST_LOG_(INFO) << "get_path_from_uri_test_006 end";
224 }
225
226 /**
227 * @tc.number: get_uri_from_path_test_001
228 * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri
229 * @tc.desc: Set path and get uri
230 * @tc.type: FUNC
231 */
232 HWTEST_F(NDKFileUriTest, get_uri_from_path_test_001, TestSize.Level1)
233 {
234 GTEST_LOG_(INFO) << "get_uri_from_path_test_001 start";
235 const char filePath[] = "storage/Users/currentUser/Documents/GetPathFromUri001.txt";
236 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetPathFromUri001.txt";
237 char *result = nullptr;
238 unsigned int length = strlen(filePath);
239 FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result);
240 EXPECT_EQ(ret, ERR_OK);
241 if (result != nullptr) {
242 GTEST_LOG_(INFO) << result;
243 EXPECT_EQ(strcmp(result, fileUri), 0);
244 FreeResult(&result);
245 }
246 GTEST_LOG_(INFO) << "get_uri_from_path_test_001 end";
247 }
248
249 /**
250 * @tc.number: get_uri_from_path_test_003
251 * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri
252 * @tc.desc: Set path and get uri
253 * @tc.type: FUNC
254 */
255 HWTEST_F(NDKFileUriTest, get_uri_from_path_test_003, TestSize.Level1)
256 {
257 GTEST_LOG_(INFO) << "get_uri_from_path_test_003 start";
258 const char filePath[] = "data/storage/el2/media/Photo/12/IMG_12345_999999/test.jpg";
259 const char fileUri[] = "file://media/Photo/12/IMG_12345_999999/test.jpg";
260 char *result = nullptr;
261 unsigned int length = strlen(filePath);
262 FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result);
263 EXPECT_EQ(ret, ERR_OK);
264 if (result != nullptr) {
265 GTEST_LOG_(INFO) << result;
266 EXPECT_EQ(strcmp(result, fileUri), 0);
267 FreeResult(&result);
268 }
269 GTEST_LOG_(INFO) << "get_uri_from_path_test_003 end";
270 }
271
272 /**
273 * @tc.number: get_uri_from_path_test_004
274 * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri
275 * @tc.desc: Set path and get uri
276 * @tc.type: FUNC
277 */
278 HWTEST_F(NDKFileUriTest, get_uri_from_path_test_004, TestSize.Level1)
279 {
280 GTEST_LOG_(INFO) << "get_uri_from_path_test_004 start";
281 const char filePath[] = "data/storage/el2/base/Photo/12/IMG_12345_999999/test.jpg";
282 const char fileUri[] = "file://com.example.filesharea/data/storage/el2/base/Photo/12/IMG_12345_999999/test.jpg";
283 char *result = nullptr;
284 unsigned int length = strlen(filePath);
285 FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result);
286 EXPECT_EQ(ret, ERR_OK);
287 if (result != nullptr) {
288 GTEST_LOG_(INFO) << result;
289 EXPECT_EQ(strcmp(result, fileUri), 0);
290 FreeResult(&result);
291 }
292 GTEST_LOG_(INFO) << "get_uri_from_path_test_003 end";
293 }
294
295 /**
296 * @tc.number: get_uri_from_path_test_005
297 * @tc.name: Test function of OH_FileUri_GetUriFromPath() interface for document uri
298 * @tc.desc: Set path and get uri
299 * @tc.type: FUNC
300 */
301 HWTEST_F(NDKFileUriTest, get_uri_from_path_test_005, TestSize.Level1)
302 {
303 GTEST_LOG_(INFO) << "get_uri_from_path_test_005 start";
304 const char filePath[] = "data/storage/el1/media/Photo/12/IMG_12345_999999/test.jpg";
305 const char fileUri[] = "file://com.example.filesharea/data/storage/el1/media/Photo/12/IMG_12345_999999/test.jpg";
306 char *result = nullptr;
307 unsigned int length = strlen(filePath);
308 FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(filePath, length, &result);
309 EXPECT_EQ(ret, ERR_OK);
310 if (result != nullptr) {
311 GTEST_LOG_(INFO) << result;
312 EXPECT_EQ(strcmp(result, fileUri), 0);
313 FreeResult(&result);
314 }
315 GTEST_LOG_(INFO) << "get_uri_from_path_test_005 end";
316 }
317
318 /**
319 * @tc.number: get_full_directory_uri_test_001
320 * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for unknown path
321 * @tc.desc: Set uri and get full directory uri
322 * @tc.type: FUNC
323 */
324 HWTEST_F(NDKFileUriTest, get_full_directory_uri_test_001, TestSize.Level1)
325 {
326 GTEST_LOG_(INFO) << "get_full_directory_uri_test_001 start";
327 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetFullDirectoryUri001.txt";
328 char *result = nullptr;
329 unsigned int length = strlen(fileUri);
330 FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(fileUri, length, &result);
331 EXPECT_EQ(ret, ERR_ENOENT);
332 FreeResult(&result);
333 GTEST_LOG_(INFO) << "get_full_directory_uri_test_001 end";
334 }
335
336 /**
337 * @tc.number: get_full_directory_uri_test_002
338 * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for success
339 * @tc.desc: Set uri and get full directory uri
340 * @tc.type: FUNC
341 */
342 HWTEST_F(NDKFileUriTest, get_full_directory_uri_test_002, TestSize.Level1)
343 {
344 GTEST_LOG_(INFO) << "get_full_directory_uri_test_002 start";
345 const char fileUri[] = "file://com.example.filesharea/data/test/file_uri_test.txt";
346 const char resultUri[] = "file://com.example.filesharea/data/test";
347 char *result = nullptr;
348 unsigned int length = strlen(fileUri);
349 FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(fileUri, length, &result);
350 EXPECT_EQ(ret, ERR_OK);
351 if (result != nullptr) {
352 GTEST_LOG_(INFO) << result;
353 EXPECT_EQ(strcmp(result, resultUri), 0);
354 }
355 FreeResult(&result);
356 GTEST_LOG_(INFO) << "get_full_directory_uri_test_002 end";
357 }
358
359 /**
360 * @tc.number: is_valid_uri_test_001
361 * @tc.name: Test function of OH_FileUri_IsValidUri() interface for real uri
362 * @tc.desc: Set URI and make a judgment
363 * @tc.type: FUNC
364 */
365 HWTEST_F(NDKFileUriTest, is_valid_uri_test_001, TestSize.Level1)
366 {
367 GTEST_LOG_(INFO) << "is_valid_uri_test_001";
368 const char fileUri[] = "file://com.demo.a/data/storage/el2/base/files/IsValidUriTest001.txt";
369 unsigned int length = strlen(fileUri);
370 bool flags = OH_FileUri_IsValidUri(fileUri, length);
371 EXPECT_EQ(flags, true);
372 GTEST_LOG_(INFO) << "is_valid_uri_test_001";
373 }
374
375 /**
376 * @tc.number: is_valid_uri_test_002
377 * @tc.name: Test function of OH_FileUri_IsValidUri() interface for unreal uri
378 * @tc.desc: Set URI and make a judgment
379 * @tc.type: FUNC
380 */
381 HWTEST_F(NDKFileUriTest, is_valid_uri_test_002, TestSize.Level1)
382 {
383 GTEST_LOG_(INFO) << "is_valid_uri_test_002";
384 const char fileUri[] = "com.demo.a/data/storage/el2/base/files/IsValidUriTest002.txt";
385 unsigned int length = strlen(fileUri);
386 bool flags = OH_FileUri_IsValidUri(fileUri, length);
387 EXPECT_EQ(flags, false);
388 GTEST_LOG_(INFO) << "is_valid_uri_test_002";
389 }
390
391 /**
392 * @tc.number: get_filename_test_001
393 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
394 * @tc.desc: Set uri and get filename
395 * @tc.type: FUNC
396 */
397 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_001, TestSize.Level1)
398 {
399 GTEST_LOG_(INFO) << "get_fileUri_filename_test_001 start";
400 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetFullDirectoryUri001.txt";
401 const char resultUri[] = "GetFullDirectoryUri001.txt";
402 char *result = nullptr;
403 unsigned int length = strlen(fileUri);
404 FileManagement_ErrCode ret = OH_FileUri_GetFileName(fileUri, length, &result);
405 EXPECT_EQ(ret, ERR_OK);
406 if (result != nullptr) {
407 GTEST_LOG_(INFO) << result;
408 EXPECT_EQ(strcmp(result, resultUri), 0);
409 }
410 FreeResult(&result);
411 GTEST_LOG_(INFO) << "get_fileUri_filename_test_001 end";
412 }
413
414 /**
415 * @tc.number: get_filename_test_002
416 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
417 * @tc.desc: Set uri and get filename
418 * @tc.type: FUNC
419 */
420 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_002, TestSize.Level1)
421 {
422 GTEST_LOG_(INFO) << "get_fileUri_filename_test_002 start";
423 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/dirName";
424 const char resultUri[] = "dirName";
425 char *result = nullptr;
426 unsigned int length = strlen(fileUri);
427 FileManagement_ErrCode ret = OH_FileUri_GetFileName(fileUri, length, &result);
428 EXPECT_EQ(ret, ERR_OK);
429 if (result != nullptr) {
430 GTEST_LOG_(INFO) << result;
431 EXPECT_EQ(strcmp(result, resultUri), 0);
432 }
433 FreeResult(&result);
434 GTEST_LOG_(INFO) << "get_fileUri_filename_test_002 end";
435 }
436
437 /**
438 * @tc.number: get_filename_test_003
439 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
440 * @tc.desc: Set uri and get filename
441 * @tc.type: FUNC
442 */
443 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_003, TestSize.Level1)
444 {
445 GTEST_LOG_(INFO) << "get_fileUri_filename_test_003 start";
446 const char fileUri[] = "file://com.example.filesharea/data/test/file_uri_test.txt";
447 char *result = nullptr;
448 unsigned int length = strlen(fileUri);
449 FileManagement_ErrCode ret = OH_FileUri_GetFileName(nullptr, length, &result);
450 EXPECT_EQ(ret, ERR_PARAMS);
451
452 ret = OH_FileUri_GetFileName(fileUri, length - 1, &result);
453 EXPECT_EQ(ret, ERR_PARAMS);
454
455 ret = OH_FileUri_GetFileName(fileUri, length, nullptr);
456 EXPECT_EQ(ret, ERR_PARAMS);
457 FreeResult(&result);
458 GTEST_LOG_(INFO) << "get_fileUri_filename_test_003 end";
459 }
460
461 /**
462 * @tc.number: get_filename_test_004
463 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
464 * @tc.desc: Set uri and get filename
465 * @tc.type: FUNC
466 */
467 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_004, TestSize.Level1)
468 {
469 GTEST_LOG_(INFO) << "get_fileUri_filename_test_004 start";
470 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/dirName/";
471 char *result = nullptr;
472 unsigned int length = strlen(fileUri);
473 FileManagement_ErrCode ret = OH_FileUri_GetFileName(fileUri, length, &result);
474 EXPECT_EQ(ret, ERR_PARAMS);
475 GTEST_LOG_(INFO) << "get_fileUri_filename_test_004 end";
476 }
477
478 } // namespace OHOS::AppFileService::ModuleFileUri