1 /*
2 * Copyright (c) 2023 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 "file_uri.h"
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <fcntl.h>
21
22 #include "accesstoken_kit.h"
23 #include "ipc_skeleton.h"
24 #include "parameter.h"
25 #include "uri.h"
26
27 #include "common_func.h"
28 #include "file_share.h"
29 #include "log.h"
30
31 using namespace std;
32 using namespace OHOS::Security::AccessToken;
33 using namespace OHOS::AppFileService;
34
35 namespace {
36 const string BUNDLE_A = "com.example.filesharea";
37 const string FULL_MOUNT_ENABLE_PARAMETER = "const.filemanager.full_mount.enable";
38 }
39
GetSelfBundleName()40 string CommonFunc::GetSelfBundleName()
41 {
42 return BUNDLE_A;
43 }
44
CheckFileManagerFullMountEnable()45 static bool CheckFileManagerFullMountEnable()
46 {
47 char value[] = "false";
48 int retSystem = GetParameter(FULL_MOUNT_ENABLE_PARAMETER.c_str(), "false", value, sizeof(value));
49 if ((retSystem > 0) && (string_view(value) == "true")) {
50 return true;
51 }
52 LOGE("Not supporting all mounts");
53 return false;
54 }
55
56 namespace OHOS::AppFileService::ModuleFileUri {
57 const string PATH_SHARE = "/data/storage/el2/share";
58 const string MODE_RW = "/rw/";
59 const string MODE_R = "/r/";
60 const int E_OK = 0;
61
62 class FileUriTest : public testing::Test {
63 public:
SetUpTestCase(void)64 static void SetUpTestCase(void) {};
TearDownTestCase()65 static void TearDownTestCase() {};
SetUp()66 void SetUp() {};
TearDown()67 void TearDown() {};
68 };
69
70 /**
71 * @tc.name: file_uri_test_0000
72 * @tc.desc: Test function of ToString() interface for SUCCESS.
73 * @tc.size: MEDIUM
74 * @tc.type: FUNC
75 * @tc.level Level 1
76 * @tc.require: I7LW57
77 */
78 HWTEST_F(FileUriTest, File_uri_ToString_0000, testing::ext::TestSize.Level1)
79 {
80 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_ToString_0000";
81
82 string fileStr = "/data/storage/el2/base/files/test.txt";
83 string uri = "file://" + BUNDLE_A + fileStr;
84 FileUri fileUri(fileStr);
85 EXPECT_EQ(fileUri.ToString(), uri);
86
87 FileUri fileUri2(uri);
88 EXPECT_EQ(fileUri2.ToString(), uri);
89 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_ToString_0000";
90 }
91
92 /**
93 * @tc.name: file_uri_test_0001
94 * @tc.desc: Test function of GetName() interface for SUCCESS.
95 * @tc.size: MEDIUM
96 * @tc.type: FUNC
97 * @tc.level Level 1
98 * @tc.require: I7LW57
99 */
100 HWTEST_F(FileUriTest, File_uri_GetName_0000, testing::ext::TestSize.Level1)
101 {
102 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetName_0000";
103
104 string fileStr = "/data/storage/el2/base/files/test.txt";
105 string uri = "file://" + BUNDLE_A + fileStr;
106 FileUri fileUri(fileStr);
107 string name = fileUri.GetName();
108 EXPECT_EQ(name, "test.txt");
109
110 string fileStr2 = "/data/storage/el2/base/files/test.txt/";
111 string uri2 = "file://" + BUNDLE_A + fileStr;
112 FileUri fileUri2(fileStr2);
113 EXPECT_EQ(fileUri2.GetName(), "");
114 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetName_0000";
115 }
116
117 /**
118 * @tc.name: file_uri_test_0002
119 * @tc.desc: Test function of GetPath() interface for SUCCESS.
120 * @tc.size: MEDIUM
121 * @tc.type: FUNC
122 * @tc.level Level 1
123 * @tc.require: I7LW57
124 */
125 HWTEST_F(FileUriTest, File_uri_GetPath_0000, testing::ext::TestSize.Level1)
126 {
127 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0000";
128 string fileStr = "/data/storage/el2/base/files/test.txt";
129 string uri = "file://" + BUNDLE_A + fileStr;
130 FileUri fileUri(uri);
131 string path = fileUri.GetPath();
132 EXPECT_EQ(path, fileStr);
133
134 string uri2 = "file://media/Photo/12/IMG_12345_999999/test.jpg";
135 FileUri fileUri2(uri2);
136 path = fileUri2.GetPath();
137 EXPECT_EQ(path, "/Photo/12/IMG_12345_999999");
138
139 string uri4 = "file://media/Photo/12/IMG_12345_999999";
140 FileUri fileUri4(uri4);
141 EXPECT_EQ(fileUri2.GetPath(), "/Photo/12/IMG_12345_999999");
142 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000";
143 }
144
145 /**
146 * @tc.name: file_uri_test_0003
147 * @tc.desc: Test function of GetPath() interface for SUCCESS.
148 * @tc.size: MEDIUM
149 * @tc.type: FUNC
150 * @tc.level Level 1
151 * @tc.require: I7LW57
152 */
153 HWTEST_F(FileUriTest, File_uri_GetPath_0001, testing::ext::TestSize.Level1)
154 {
155 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0001";
156 string fileStr = "/Documents/test.txt";
157 string uri = "file://" + BUNDLE_A + fileStr;
158 FileUri fileUri(uri);
159 string path = fileUri.GetRealPath();
160 EXPECT_EQ(path, fileStr);
161
162 string fileStrPath = "docs/storage/Users/currentUser/Documents/1.txt";
163 string fileStrRealPath = "/storage/Users/currentUser/Documents/1.txt";
164 string uri2 = "file://" + fileStrPath;
165 FileUri fileUri2(uri2);
166 path.clear();
167 path = fileUri2.GetRealPath();
168 if (CheckFileManagerFullMountEnable()) {
169 EXPECT_EQ(path, fileStrRealPath);
170 } else {
171 EXPECT_EQ(path, PATH_SHARE + MODE_R + fileStrPath);
172 };
173 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0001";
174 }
175
176 /**
177 * @tc.name: file_uri_test_0004
178 * @tc.desc: Test function of GetPath() interface for SUCCESS.
179 * @tc.size: MEDIUM
180 * @tc.type: FUNC
181 * @tc.level Level 1
182 * @tc.require: I7LW57
183 */
184 HWTEST_F(FileUriTest, File_uri_GetPath_0002, testing::ext::TestSize.Level1)
185 {
186 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0002";
187 string fileStr = "/data/storage/el2/base/files/test.txt";
188 string bundleB = "com.demo.b";
189 string uri = "file://" + bundleB + fileStr;
190 string rltStr = PATH_SHARE + MODE_R + bundleB + fileStr;
191 FileUri fileUri(uri);
192 string path = fileUri.GetRealPath();
193 EXPECT_EQ(path, rltStr);
194 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0002";
195 }
196
197 /**
198 * @tc.name: file_uri_test_0005
199 * @tc.desc: Test function of GetPath() interface for SUCCESS.
200 * @tc.size: MEDIUM
201 * @tc.type: FUNC
202 * @tc.level Level 1
203 * @tc.require: I7LW57
204 */
205 HWTEST_F(FileUriTest, File_uri_GetPath_0003, testing::ext::TestSize.Level1)
206 {
207 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0003";
208 int32_t uid = 100;
209 string bundleB = "com.example.fileshareb";
210 string fileStr = "/data/app/el2/" + to_string(uid) + "/base/" + bundleB + "/files/test.txt";
211 int32_t fd = open(fileStr.c_str(), O_RDWR | O_CREAT);
212 ASSERT_TRUE(fd != -1) << "FileShareTest Create File Failed!";
213
214 string actStr = "/data/storage/el2/base/files/test.txt";
215 string uri = "file://" + bundleB + actStr;
216 uint32_t tokenId = AccessTokenKit::GetHapTokenID(uid, BUNDLE_A, 0);
217
218 int32_t flag = 3;
219 vector<string> uriList(1, uri);
220 vector<int32_t> retList;
221 int32_t ret = FileShare::CreateShareFile(uriList, tokenId, flag, retList);
222 EXPECT_EQ(ret, E_OK);
223
224 string rltStr = PATH_SHARE + MODE_R + bundleB + actStr;
225 FileUri fileUri(uri);
226 string path = fileUri.GetRealPath();
227 EXPECT_EQ(path, rltStr);
228
229 vector<string> sharePathList;
230 sharePathList.push_back(uri);
231 ret = FileShare::DeleteShareFile(tokenId, sharePathList);
232 EXPECT_EQ(ret, E_OK);
233 close(fd);
234 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0003";
235 }
236
237 /**
238 * @tc.name: file_uri_test_0006
239 * @tc.desc: Test function of GetPath() interface for SUCCESS.
240 * @tc.size: MEDIUM
241 * @tc.type: FUNC
242 * @tc.level Level 1
243 * @tc.require: I7LW57
244 */
245 HWTEST_F(FileUriTest, File_uri_GetPath_0004, testing::ext::TestSize.Level1)
246 {
247 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004";
248 string fileStr = "/data/storage/el2/base/files/test.txt";
249 string uri = "file://" + fileStr;
250 FileUri fileUri(uri);
251 EXPECT_EQ(fileUri.ToString(), uri);
252 EXPECT_EQ(fileUri.GetName(), "test.txt");
253 EXPECT_EQ(fileUri.GetPath(), fileStr);
254 EXPECT_EQ(fileUri.GetRealPath(), fileStr);
255 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0004";
256 }
257
258 /**
259 * @tc.name: file_uri_test_0005
260 * @tc.desc: Test function of GetPath() interface for SUCCESS.
261 * @tc.size: MEDIUM
262 * @tc.type: FUNC
263 * @tc.level Level 1
264 * @tc.require: I7LW57
265 */
266 HWTEST_F(FileUriTest, File_uri_GetPath_0005, testing::ext::TestSize.Level1)
267 {
268 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0005";
269 string fileStr = "/data/storage/el2/media/Photo/12/IMG_12345_999999/test.jpg";
270 string uri = "file://media/Photo/12/IMG_12345_999999/test.jpg";
271 FileUri fileUri(uri);
272 EXPECT_EQ(fileUri.GetRealPath(), fileStr);
273 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPath_0005";
274 }
275
276 /**
277 * @tc.name: File_uri_GetPathBySA_0001
278 * @tc.desc: Test function of GetPath() interface for SUCCESS.
279 * @tc.size: MEDIUM
280 * @tc.type: FUNC
281 * @tc.level Level 1
282 * @tc.require: I7LW57
283 */
284 HWTEST_F(FileUriTest, File_uri_GetPathBySA_0001, testing::ext::TestSize.Level1)
285 {
286 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0001";
287 string fileStr = "";
288 string uri = "file://com.example.demoa/data/storage/el2/base%()test.jpg";
289 FileUri fileUri(uri);
290 EXPECT_EQ(fileUri.GetRealPathBySA(), fileStr);
291 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0001";
292 }
293
294 /**
295 * @tc.name: File_uri_GetPathBySA_0002
296 * @tc.desc: Test function of GetPath() interface for SUCCESS.
297 * @tc.size: MEDIUM
298 * @tc.type: FUNC
299 * @tc.level Level 1
300 * @tc.require: I7LW57
301 */
302 HWTEST_F(FileUriTest, File_uri_GetPathBySA_0002, testing::ext::TestSize.Level1)
303 {
304 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0002";
305 string fileStr = "";
306 string uri = "file://com.example.demoa/data/storage/el2/base/files/%你好test.jpg";
307 FileUri fileUri(uri);
308 EXPECT_EQ(fileUri.GetRealPathBySA(), fileStr);
309 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0002";
310 }
311
312 /**
313 * @tc.name: File_uri_GetPathBySA_0003
314 * @tc.desc: Test function of GetPath() interface for SUCCESS.
315 * @tc.size: MEDIUM
316 * @tc.type: FUNC
317 * @tc.level Level 1
318 * @tc.require: I7LW57
319 */
320 HWTEST_F(FileUriTest, File_uri_GetPathBySA_0003, testing::ext::TestSize.Level1)
321 {
322 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0003";
323 string fileStr = "";
324 string uri = "file://com.example.demoa/data/storage/el2%/base/files/test.jpg";
325 FileUri fileUri(uri);
326 EXPECT_EQ(fileUri.GetRealPathBySA(), fileStr);
327 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0003";
328 }
329
330 /**
331 * @tc.name: File_uri_GetPathBySA_0004
332 * @tc.desc: Test function of GetPath() interface for SUCCESS.
333 * @tc.size: MEDIUM
334 * @tc.type: FUNC
335 * @tc.level Level 1
336 * @tc.require: I7LW57
337 */
338 HWTEST_F(FileUriTest, File_uri_GetPathBySA_0004, testing::ext::TestSize.Level1)
339 {
340 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0004";
341 string fileStr = "";
342 string uri = "file://com.example.demoa/data/storage/el2/base%#files/test.jpg";
343 FileUri fileUri(uri);
344 EXPECT_EQ(fileUri.GetRealPathBySA(), fileStr);
345 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetPathBySA_0004";
346 }
347
348 /**
349 * @tc.name: file_uri_test_0007
350 * @tc.desc: Test function of GetFullDirectoryUri() interface for SUCCESS.
351 * @tc.size: MEDIUM
352 * @tc.type: FUNC
353 * @tc.level Level 1
354 * @tc.require:
355 */
356 HWTEST_F(FileUriTest, File_uri_GetFullDirectoryUri_0000, testing::ext::TestSize.Level1)
357 {
358 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetFullDirectoryUri_0000";
359 string fileStr = "/data/test/remote_file_share_test.txt";
360 FileUri fileUriObject(fileStr);
361 string fileDirectoryUri = "file://" + BUNDLE_A + "/data/test";
362 EXPECT_EQ(fileUriObject.GetFullDirectoryUri(), fileDirectoryUri);
363 string folderStr = "/data/test";
364 string folderUri = "file://" + BUNDLE_A + folderStr;
365 FileUri folderUriObject(folderUri);
366 string folderDirectoryUri = "file://" + BUNDLE_A + folderStr;
367 EXPECT_EQ(folderUriObject.GetFullDirectoryUri(), folderDirectoryUri);
368 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000";
369 }
370
371 /**
372 * @tc.name: File_uri_IsRemoteUri_0000
373 * @tc.desc: Test function of IsRemoteUri() interface for SUCCESS.
374 * @tc.size: MEDIUM
375 * @tc.type: FUNC
376 * @tc.level Level 1
377 * @tc.require:
378 */
379 HWTEST_F(FileUriTest, File_uri_IsRemoteUri_0000, testing::ext::TestSize.Level1)
380 {
381 GTEST_LOG_(INFO) << "FileUriTest-begin File_uri_GetFullDirectoryUri_0000";
382 string fileStr = "file://" + BUNDLE_A + "/data/test";
383 FileUri fileUriObject(fileStr);
384 EXPECT_EQ(fileUriObject.IsRemoteUri(), false);
385
386 string fileStr2 = "file://" + BUNDLE_A + "/data/test?networkid=";
387 FileUri fileUriObject2(fileStr2);
388 EXPECT_EQ(fileUriObject2.IsRemoteUri(), false);
389
390 string fileStr3 = "file://" + BUNDLE_A + "/data/test?networkid=123456/";
391 FileUri fileUriObject3(fileStr3);
392 EXPECT_EQ(fileUriObject3.IsRemoteUri(), false);
393
394 string fileStr4 = "file://" + BUNDLE_A + "/data/test?networkid=123456";
395 FileUri fileUriObject4(fileStr4);
396 EXPECT_EQ(fileUriObject4.IsRemoteUri(), true);
397 GTEST_LOG_(INFO) << "FileUriTest-end File_uri_GetPath_0000";
398 }
399 }