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