1 /*
2 * Copyright (c) 2022-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
16 #include <gtest/gtest.h>
17 #include <nlohmann/json.hpp>
18
19 #include "accesstoken_kit.h"
20 #include "context_impl.h"
21 #include "file_access_framework_errno.h"
22 #include "iservice_registry.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25
26 #include "file_access_helper.h"
27
28 namespace {
29 using namespace std;
30 using namespace OHOS;
31 using namespace FileAccessFwk;
32 using json = nlohmann::json;
33 const int ABILITY_ID = 5003;
34 shared_ptr<FileAccessHelper> g_fah = nullptr;
35 const int UID_TRANSFORM_TMP = 20000000;
36 const int UID_DEFAULT = 0;
37 const int COPY_EXCEPTION = -1;
38 shared_ptr<OHOS::AbilityRuntime::Context> g_context = nullptr;
39
SetNativeToken(bool isSystemApp)40 static void SetNativeToken(bool isSystemApp)
41 {
42 uint64_t tokenId;
43 const char *perms[] = {
44 "ohos.permission.FILE_ACCESS_MANAGER",
45 "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED",
46 "ohos.permission.CONNECT_FILE_ACCESS_EXTENSION"
47 };
48 NativeTokenInfoParams infoInstance = {
49 .dcapsNum = 0,
50 .permsNum = 3,
51 .aclsNum = 0,
52 .dcaps = nullptr,
53 .perms = perms,
54 .acls = nullptr,
55 .aplStr = "system_core",
56 };
57
58 infoInstance.processName = "SetUpTestCase";
59 tokenId = GetAccessTokenId(&infoInstance);
60 if (isSystemApp) {
61 const uint64_t systemAppMask = (static_cast<uint64_t>(1) << 32);
62 tokenId |= systemAppMask;
63 }
64 SetSelfTokenID(tokenId);
65 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
66 }
67
68 class AbnormalFileExtensionHelperTest : public testing::Test {
69 public:
SetUpTestCase(void)70 static void SetUpTestCase(void)
71 {
72 cout << "AbnormalFileExtensionHelperTest code test" << endl;
73 SetNativeToken(true);
74 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
75 if (saManager == nullptr) {
76 return;
77 }
78 auto remoteObj = saManager->GetSystemAbility(ABILITY_ID);
79 g_context = make_shared<OHOS::AbilityRuntime::ContextImpl>();
80 g_context->SetToken(remoteObj);
81 AAFwk::Want want;
82 vector<AAFwk::Want> wantVec;
83 setuid(UID_TRANSFORM_TMP);
84 int ret = FileAccessHelper::GetRegisteredFileAccessExtAbilityInfo(wantVec);
85 EXPECT_EQ(ret, OHOS::FileAccessFwk::ERR_OK);
86 bool sus = false;
87 for (size_t i = 0; i < wantVec.size(); i++) {
88 auto element = wantVec[i].GetElement();
89 if (element.GetBundleName() == "com.ohos.UserFile.ExternalFileManager" &&
90 element.GetAbilityName() == "FileExtensionAbility") {
91 want = wantVec[i];
92 sus = true;
93 break;
94 }
95 }
96 EXPECT_TRUE(sus);
97 vector<AAFwk::Want> wants{want};
98 g_fah = FileAccessHelper::Creator(remoteObj, wants);
99 if (g_fah == nullptr) {
100 GTEST_LOG_(ERROR) << "AbnormalFileExtensionHelperTest g_fah is nullptr";
101 exit(1);
102 }
103 setuid(UID_DEFAULT);
104 SetNativeToken(false);
105 }
TearDownTestCase()106 static void TearDownTestCase()
107 {
108 if (g_fah) {
109 g_fah->Release();
110 }
111 g_fah = nullptr;
112 };
SetUp()113 void SetUp(){};
TearDown()114 void TearDown(){};
115 };
116
117 /**
118 * @tc.number: user_file_service_external_file_access_OpenFile_0000
119 * @tc.name: abnormal_external_file_access_OpenFile_0000
120 * @tc.desc: Test function of OpenFile interface for ERROR because of set not system app flag.
121 * @tc.size: MEDIUM
122 * @tc.type: FUNC
123 * @tc.level Level 1
124 * @tc.require: I76YA0
125 */
126 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_OpenFile_0000, testing::ext::TestSize.Level1)
127 {
128 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_OpenFile_0000";
129 try {
130 Uri uri("");
131 int fd;
132 EXPECT_NE(g_fah, nullptr);
133 int result = g_fah->OpenFile(uri, WRITE_READ, fd);
134 EXPECT_EQ(result, E_PERMISSION_SYS);
135 } catch (...) {
136 GTEST_LOG_(ERROR) << "abnormal_external_file_access_OpenFile_0000 occurs an exception.";
137 }
138 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_OpenFile_0000";
139 }
140
141 /**
142 * @tc.number: user_file_service_external_file_access_CreateFile_0000
143 * @tc.name: abnormal_external_file_access_CreateFile_0000
144 * @tc.desc: Test function of CreateFile interface for ERROR because of set not system app flag.
145 * @tc.size: MEDIUM
146 * @tc.type: FUNC
147 * @tc.level Level 1
148 * @tc.require: I76YA0
149 */
150 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_CreateFile_0000, testing::ext::TestSize.Level1)
151 {
152 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_CreateFile_0000";
153 try {
154 Uri parent("");
155 string displayName("");
156 Uri newFile("");
157 EXPECT_NE(g_fah, nullptr);
158 int result = g_fah->CreateFile(parent, displayName, newFile);
159 EXPECT_EQ(result, E_PERMISSION_SYS);
160 } catch (...) {
161 GTEST_LOG_(ERROR) << "abnormal_external_file_access_CreateFile_0000 occurs an exception.";
162 }
163 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_CreateFile_0000";
164 }
165
166 /**
167 * @tc.number: user_file_service_external_file_access_Mkdir_0000
168 * @tc.name: abnormal_external_file_access_Mkdir_0000
169 * @tc.desc: Test function of Mkdir interface for ERROR because of set not system app flag.
170 * @tc.size: MEDIUM
171 * @tc.type: FUNC
172 * @tc.level Level 1
173 * @tc.require: I76YA0
174 */
175 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_Mkdir_0000, testing::ext::TestSize.Level1)
176 {
177 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_Mkdir_0000";
178 try {
179 Uri parent("");
180 string displayName("");
181 Uri newDir("");
182 EXPECT_NE(g_fah, nullptr);
183 int result = g_fah->Mkdir(parent, displayName, newDir);
184 EXPECT_EQ(result, E_PERMISSION_SYS);
185 } catch (...) {
186 GTEST_LOG_(ERROR) << "abnormal_external_file_access_Mkdir_0000 occurs an exception.";
187 }
188 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_Mkdir_0000";
189 }
190
191 /**
192 * @tc.number: user_file_service_external_file_access_Delete_0000
193 * @tc.name: abnormal_external_file_access_Delete_0000
194 * @tc.desc: Test function of Delete interface for ERROR because of set not system app flag.
195 * @tc.size: MEDIUM
196 * @tc.type: FUNC
197 * @tc.level Level 1
198 * @tc.require: I76YA0
199 */
200 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_Delete_0000, testing::ext::TestSize.Level1)
201 {
202 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_Delete_0000";
203 try {
204 Uri uri("");
205 EXPECT_NE(g_fah, nullptr);
206 int result = g_fah->Delete(uri);
207 EXPECT_EQ(result, E_PERMISSION_SYS);
208 } catch (...) {
209 GTEST_LOG_(ERROR) << "abnormal_external_file_access_Delete_0000 occurs an exception.";
210 }
211 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_Delete_0000";
212 }
213
214 /**
215 * @tc.number: user_file_service_external_file_access_Move_0000
216 * @tc.name: abnormal_external_file_access_Move_0000
217 * @tc.desc: Test function of Move interface for ERROR because of set not system app flag.
218 * @tc.size: MEDIUM
219 * @tc.type: FUNC
220 * @tc.level Level 1
221 * @tc.require: I76YA0
222 */
223 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_Move_0000, testing::ext::TestSize.Level1)
224 {
225 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_Move_0000";
226 try {
227 Uri sourceFile("");
228 Uri targetParent("");
229 Uri newFile("");
230 EXPECT_NE(g_fah, nullptr);
231 int result = g_fah->Move(sourceFile, targetParent, newFile);
232 EXPECT_EQ(result, E_PERMISSION_SYS);
233 } catch (...) {
234 GTEST_LOG_(ERROR) << "abnormal_external_file_access_Move_0000 occurs an exception.";
235 }
236 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_Move_0000";
237 }
238
239 /**
240 * @tc.number: user_file_service_external_file_access_Copy_0000
241 * @tc.name: abnormal_external_file_access_Copy_0000
242 * @tc.desc: Test function of Copy interface for ERROR because of set not system app flag.
243 * @tc.size: MEDIUM
244 * @tc.type: FUNC
245 * @tc.level Level 1
246 * @tc.require: I76YA0
247 */
248 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_Copy_0000, testing::ext::TestSize.Level1)
249 {
250 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_Copy_0000";
251 try {
252 Uri sourceUri("");
253 Uri destUri("");
254 vector<Result> copyResult;
255 bool force = false;
256 EXPECT_NE(g_fah, nullptr);
257 int result = g_fah->Copy(sourceUri, destUri, copyResult, force);
258 EXPECT_EQ(result, COPY_EXCEPTION);
259 } catch (...) {
260 GTEST_LOG_(ERROR) << "abnormal_external_file_access_Copy_0000 occurs an exception.";
261 }
262 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_Copy_0000";
263 }
264
265 /**
266 * @tc.number: user_file_service_external_file_access_Rename_0000
267 * @tc.name: abnormal_external_file_access_Rename_0000
268 * @tc.desc: Test function of Rename interface for ERROR because of set not system app flag.
269 * @tc.size: MEDIUM
270 * @tc.type: FUNC
271 * @tc.level Level 1
272 * @tc.require: I76YA0
273 */
274 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_Rename_0000, testing::ext::TestSize.Level1)
275 {
276 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_Rename_0000";
277 try {
278 Uri sourceFile("");
279 string displayName("");
280 Uri newFile("");
281 EXPECT_NE(g_fah, nullptr);
282 int result = g_fah->Rename(sourceFile, displayName, newFile);
283 EXPECT_EQ(result, E_PERMISSION_SYS);
284 } catch (...) {
285 GTEST_LOG_(ERROR) << "abnormal_external_file_access_Rename_0000 occurs an exception.";
286 }
287 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_Rename_0000";
288 }
289
290 /**
291 * @tc.number: user_file_service_external_file_access_ListFile_0000
292 * @tc.name: abnormal_external_file_access_ListFile_0000
293 * @tc.desc: Test function of ListFile interface for ERROR because of set not system app flag.
294 * @tc.size: MEDIUM
295 * @tc.type: FUNC
296 * @tc.level Level 1
297 * @tc.require: I76YA0
298 */
299 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_ListFile_0000, testing::ext::TestSize.Level1)
300 {
301 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_ListFile_0000";
302 try {
303 FileInfo fileInfo;
304 int64_t offset = 0;
305 FileFilter filter;
306 SharedMemoryInfo memInfo;
307 int result = FileAccessFwk::SharedMemoryOperation::CreateSharedMemory("FileInfo List", DEFAULT_CAPACITY_200KB,
308 memInfo);
309 EXPECT_NE(g_fah, nullptr);
310 result = g_fah->ListFile(fileInfo, offset, filter, memInfo);
311 EXPECT_EQ(result, E_PERMISSION_SYS);
312 SharedMemoryOperation::DestroySharedMemory(memInfo);
313 } catch (...) {
314 GTEST_LOG_(ERROR) << "abnormal_external_file_access_ListFile_0000 occurs an exception.";
315 }
316 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_ListFile_0000";
317 }
318
319 /**
320 * @tc.number: user_file_service_external_file_access_ScanFile_0000
321 * @tc.name: abnormal_external_file_access_ScanFile_0000
322 * @tc.desc: Test function of ScanFile interface for ERROR because of set not system app flag.
323 * @tc.size: MEDIUM
324 * @tc.type: FUNC
325 * @tc.level Level 1
326 * @tc.require: I76YA0
327 */
328 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_ScanFile_0000, testing::ext::TestSize.Level1)
329 {
330 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_ScanFile_0000";
331 try {
332 FileInfo fileInfo;
333 int64_t offset = 0;
334 int64_t maxCount = 0;
335 FileFilter filter;
336 vector<FileInfo> fileInfoVec;
337 EXPECT_NE(g_fah, nullptr);
338 int result = g_fah->ScanFile(fileInfo, offset, maxCount, filter, fileInfoVec);
339 EXPECT_EQ(result, E_PERMISSION_SYS);
340 } catch (...) {
341 GTEST_LOG_(ERROR) << "abnormal_external_file_access_ScanFile_0000 occurs an exception.";
342 }
343 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_ScanFile_0000";
344 }
345
346 /**
347 * @tc.number: user_file_service_external_file_access_Query_0000
348 * @tc.name: abnormal_external_file_access_Query_0000
349 * @tc.desc: Test function of Query interface for ERROR because of set not system app flag.
350 * @tc.size: MEDIUM
351 * @tc.type: FUNC
352 * @tc.level Level 1
353 * @tc.require: I76YA0
354 */
355 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_Query_0000, testing::ext::TestSize.Level1)
356 {
357 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_Query_0000";
358 try {
359 Uri uri("");
360 string metaJson("");
361 EXPECT_NE(g_fah, nullptr);
362 int result = g_fah->Query(uri, metaJson);
363 EXPECT_EQ(result, E_PERMISSION_SYS);
364 } catch (...) {
365 GTEST_LOG_(ERROR) << "abnormal_external_file_access_Query_0000 occurs an exception.";
366 }
367 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_Query_0000";
368 }
369
370 /**
371 * @tc.number: user_file_service_external_file_access_GetRoots_0000
372 * @tc.name: abnormal_external_file_access_GetRoots_0000
373 * @tc.desc: Test function of GetRoots interface for ERROR because of set not system app flag.
374 * @tc.size: MEDIUM
375 * @tc.type: FUNC
376 * @tc.level Level 1
377 * @tc.require: I76YA0
378 */
379 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_GetRoots_0000, testing::ext::TestSize.Level1)
380 {
381 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_GetRoots_0000";
382 try {
383 vector<RootInfo> rootInfoVec;
384 EXPECT_NE(g_fah, nullptr);
385 int result = g_fah->GetRoots(rootInfoVec);
386 EXPECT_EQ(result, E_PERMISSION_SYS);
387 } catch (...) {
388 GTEST_LOG_(ERROR) << "abnormal_external_file_access_GetRoots_0000 occurs an exception.";
389 }
390 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_GetRoots_0000";
391 }
392
393 /**
394 * @tc.number: user_file_service_external_file_access_GetRegisteredFileAccessExtAbilityInfo_0000
395 * @tc.name: abnormal_external_file_access_GetRegisteredFileAccessExtAbilityInfo_0000
396 * @tc.desc: Test function of GetRegisteredFileAccessExtAbilityInfo interface for ERROR because of set not system app
397 * flag.
398 * @tc.size: MEDIUM
399 * @tc.type: FUNC
400 * @tc.level Level 1
401 * @tc.require: I76YA0
402 */
403 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_GetRegisteredFileAccessExtAbilityInfo_0000,
404 testing::ext::TestSize.Level1)
405 {
406 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin "
407 "abnormal_external_file_access_GetRegisteredFileAccessExtAbilityInfo_0000";
408 try {
409 vector<AAFwk::Want> wantVec;
410 EXPECT_NE(g_fah, nullptr);
411 int result = g_fah->GetRegisteredFileAccessExtAbilityInfo(wantVec);
412 EXPECT_EQ(result, E_PERMISSION_SYS);
413 } catch (...) {
414 GTEST_LOG_(ERROR) << "abnormal_external_file_access_GetRegisteredFileAccessExtAbilityInfo_0000 occurs an "
415 "exception.";
416 }
417 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end "
418 "abnormal_external_file_access_GetRegisteredFileAccessExtAbilityInfo_0000";
419 }
420
421 /**
422 * @tc.number: user_file_service_external_file_access_Access_0000
423 * @tc.name: abnormal_external_file_access_Access_0000
424 * @tc.desc: Test function of Access interface for ERROR because of set not system app flag.
425 * @tc.size: MEDIUM
426 * @tc.type: FUNC
427 * @tc.level Level 1
428 * @tc.require: I76YA0
429 */
430 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_Access_0000, testing::ext::TestSize.Level1)
431 {
432 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_Access_0000";
433 try {
434 Uri uri("");
435 bool isExist = true;
436 EXPECT_NE(g_fah, nullptr);
437 int result = g_fah->Access(uri, isExist);
438 EXPECT_EQ(result, E_PERMISSION_SYS);
439 } catch (...) {
440 GTEST_LOG_(ERROR) << "abnormal_external_file_access_Access_0000 occurs an exception.";
441 }
442 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_Access_0000";
443 }
444
445 /**
446 * @tc.number: user_file_service_external_file_access_GetFileInfoFromUri_0000
447 * @tc.name: abnormal_external_file_access_GetFileInfoFromUri_0000
448 * @tc.desc: Test function of GetFileInfoFromUri interface for ERROR because of set not system app flag.
449 * @tc.size: MEDIUM
450 * @tc.type: FUNC
451 * @tc.level Level 1
452 * @tc.require: I76YA0
453 */
454 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_GetFileInfoFromUri_0000,
455 testing::ext::TestSize.Level1)
456 {
457 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin abnormal_external_file_access_GetFileInfoFromUri_0000";
458 try {
459 Uri selectFile("");
460 FileInfo fileInfo;
461 EXPECT_NE(g_fah, nullptr);
462 int result = g_fah->GetFileInfoFromUri(selectFile, fileInfo);
463 EXPECT_EQ(result, E_PERMISSION_SYS);
464 } catch (...) {
465 GTEST_LOG_(ERROR) << "abnormal_external_file_access_GetFileInfoFromUri_0000 occurs an exception.";
466 }
467 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end abnormal_external_file_access_GetFileInfoFromUri_0000";
468 }
469
470 /**
471 * @tc.number: user_file_service_external_file_access_GetFileInfoFromRelativePath_0000
472 * @tc.name: abnormal_external_file_access_GetFileInfoFromRelativePath_0000
473 * @tc.desc: Test function of GetFileInfoFromRelativePath interface for ERROR because of set not system app flag.
474 * @tc.size: MEDIUM
475 * @tc.type: FUNC
476 * @tc.level Level 1
477 * @tc.require: I76YA0
478 */
479 HWTEST_F(AbnormalFileExtensionHelperTest, abnormal_external_file_access_GetFileInfoFromRelativePath_0000,
480 testing::ext::TestSize.Level1)
481 {
482 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-begin"
483 "abnormal_external_file_access_GetFileInfoFromRelativePath_0000";
484 try {
485 string selectFile("");
486 FileInfo fileInfo;
487 EXPECT_NE(g_fah, nullptr);
488 int result = g_fah->GetFileInfoFromRelativePath(selectFile, fileInfo);
489 EXPECT_EQ(result, E_PERMISSION_SYS);
490 } catch (...) {
491 GTEST_LOG_(ERROR) << "abnormal_external_file_access_GetFileInfoFromRelativePath_0000 occurs an exception.";
492 }
493 GTEST_LOG_(INFO) << "AbnormalFileExtensionHelperTest-end"
494 "abnormal_external_file_access_GetFileInfoFromRelativePath_0000";
495 }
496 } // namespace
497