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