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://com.example.filesharea/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_full_directory_uri_test_001
251 * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for unknown path
252 * @tc.desc: Set uri and get full directory uri
253 * @tc.type: FUNC
254 */
255 HWTEST_F(NDKFileUriTest, get_full_directory_uri_test_001, TestSize.Level1)
256 {
257 GTEST_LOG_(INFO) << "get_full_directory_uri_test_001 start";
258 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetFullDirectoryUri001.txt";
259 char *result = nullptr;
260 unsigned int length = strlen(fileUri);
261 FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(fileUri, length, &result);
262 EXPECT_EQ(ret, ERR_ENOENT);
263 FreeResult(&result);
264 GTEST_LOG_(INFO) << "get_full_directory_uri_test_001 end";
265 }
266
267 /**
268 * @tc.number: get_full_directory_uri_test_002
269 * @tc.name: Test function of OH_FileUri_GetFullDirectoryUri() interface for success
270 * @tc.desc: Set uri and get full directory uri
271 * @tc.type: FUNC
272 */
273 HWTEST_F(NDKFileUriTest, get_full_directory_uri_test_002, TestSize.Level1)
274 {
275 GTEST_LOG_(INFO) << "get_full_directory_uri_test_002 start";
276 const char fileUri[] = "file://com.example.filesharea/data/test/file_uri_test.txt";
277 const char resultUri[] = "file://com.example.filesharea/data/test";
278 char *result = nullptr;
279 unsigned int length = strlen(fileUri);
280 FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(fileUri, length, &result);
281 EXPECT_EQ(ret, ERR_OK);
282 if (result != nullptr) {
283 GTEST_LOG_(INFO) << result;
284 EXPECT_EQ(strcmp(result, resultUri), 0);
285 }
286 FreeResult(&result);
287 GTEST_LOG_(INFO) << "get_full_directory_uri_test_002 end";
288 }
289
290 /**
291 * @tc.number: is_valid_uri_test_001
292 * @tc.name: Test function of OH_FileUri_IsValidUri() interface for real uri
293 * @tc.desc: Set URI and make a judgment
294 * @tc.type: FUNC
295 */
296 HWTEST_F(NDKFileUriTest, is_valid_uri_test_001, TestSize.Level1)
297 {
298 GTEST_LOG_(INFO) << "is_valid_uri_test_001";
299 const char fileUri[] = "file://com.demo.a/data/storage/el2/base/files/IsValidUriTest001.txt";
300 unsigned int length = strlen(fileUri);
301 bool flags = OH_FileUri_IsValidUri(fileUri, length);
302 EXPECT_EQ(flags, true);
303 GTEST_LOG_(INFO) << "is_valid_uri_test_001";
304 }
305
306 /**
307 * @tc.number: is_valid_uri_test_002
308 * @tc.name: Test function of OH_FileUri_IsValidUri() interface for unreal uri
309 * @tc.desc: Set URI and make a judgment
310 * @tc.type: FUNC
311 */
312 HWTEST_F(NDKFileUriTest, is_valid_uri_test_002, TestSize.Level1)
313 {
314 GTEST_LOG_(INFO) << "is_valid_uri_test_002";
315 const char fileUri[] = "com.demo.a/data/storage/el2/base/files/IsValidUriTest002.txt";
316 unsigned int length = strlen(fileUri);
317 bool flags = OH_FileUri_IsValidUri(fileUri, length);
318 EXPECT_EQ(flags, false);
319 GTEST_LOG_(INFO) << "is_valid_uri_test_002";
320 }
321
322 /**
323 * @tc.number: get_filename_test_001
324 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
325 * @tc.desc: Set uri and get filename
326 * @tc.type: FUNC
327 */
328 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_001, TestSize.Level1)
329 {
330 GTEST_LOG_(INFO) << "get_fileUri_filename_test_001 start";
331 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/GetFullDirectoryUri001.txt";
332 const char resultUri[] = "GetFullDirectoryUri001.txt";
333 char *result = nullptr;
334 unsigned int length = strlen(fileUri);
335 FileManagement_ErrCode ret = OH_FileUri_GetFileName(fileUri, length, &result);
336 EXPECT_EQ(ret, ERR_OK);
337 if (result != nullptr) {
338 GTEST_LOG_(INFO) << result;
339 EXPECT_EQ(strcmp(result, resultUri), 0);
340 }
341 FreeResult(&result);
342 GTEST_LOG_(INFO) << "get_fileUri_filename_test_001 end";
343 }
344
345 /**
346 * @tc.number: get_filename_test_002
347 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
348 * @tc.desc: Set uri and get filename
349 * @tc.type: FUNC
350 */
351 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_002, TestSize.Level1)
352 {
353 GTEST_LOG_(INFO) << "get_fileUri_filename_test_002 start";
354 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/dirName";
355 const char resultUri[] = "dirName";
356 char *result = nullptr;
357 unsigned int length = strlen(fileUri);
358 FileManagement_ErrCode ret = OH_FileUri_GetFileName(fileUri, length, &result);
359 EXPECT_EQ(ret, ERR_OK);
360 if (result != nullptr) {
361 GTEST_LOG_(INFO) << result;
362 EXPECT_EQ(strcmp(result, resultUri), 0);
363 }
364 FreeResult(&result);
365 GTEST_LOG_(INFO) << "get_fileUri_filename_test_002 end";
366 }
367
368 /**
369 * @tc.number: get_filename_test_003
370 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
371 * @tc.desc: Set uri and get filename
372 * @tc.type: FUNC
373 */
374 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_003, TestSize.Level1)
375 {
376 GTEST_LOG_(INFO) << "get_fileUri_filename_test_003 start";
377 const char fileUri[] = "file://com.example.filesharea/data/test/file_uri_test.txt";
378 char *result = nullptr;
379 unsigned int length = strlen(fileUri);
380 FileManagement_ErrCode ret = OH_FileUri_GetFileName(nullptr, length, &result);
381 EXPECT_EQ(ret, ERR_PARAMS);
382
383 ret = OH_FileUri_GetFileName(fileUri, length - 1, &result);
384 EXPECT_EQ(ret, ERR_PARAMS);
385
386 ret = OH_FileUri_GetFileName(fileUri, length, nullptr);
387 EXPECT_EQ(ret, ERR_PARAMS);
388 FreeResult(&result);
389 GTEST_LOG_(INFO) << "get_fileUri_filename_test_003 end";
390 }
391
392 /**
393 * @tc.number: get_filename_test_004
394 * @tc.name: Test function of OH_FileUri_GetFileName() interface for unknown path
395 * @tc.desc: Set uri and get filename
396 * @tc.type: FUNC
397 */
398 HWTEST_F(NDKFileUriTest, get_fileUri_filename_test_004, TestSize.Level1)
399 {
400 GTEST_LOG_(INFO) << "get_fileUri_filename_test_004 start";
401 const char fileUri[] = "file://docs/storage/Users/currentUser/Documents/dirName/";
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_PARAMS);
406 GTEST_LOG_(INFO) << "get_fileUri_filename_test_004 end";
407 }
408
409 } // namespace OHOS::AppFileService::ModuleFileUri