1 /*
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
16 #include "adapter_loglibrary_test_tools.h"
17
18 #include <string>
19 #include <vector>
20
21 #include "accesstoken_kit.h"
22 #include "file_util.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
CreateDir(const std::string & dir)28 void AdapterLoglibraryTestTools::CreateDir(const std::string& dir)
29 {
30 if (!FileUtil::FileExists(dir)) {
31 FileUtil::ForceCreateDirectory(dir, FileUtil::FILE_PERM_770);
32 }
33 }
34
RemoveDir(const std::string & dir)35 void AdapterLoglibraryTestTools::RemoveDir(const std::string& dir)
36 {
37 if (FileUtil::FileExists(dir)) {
38 FileUtil::ForceRemoveDirectory(dir);
39 }
40 }
41
NativeToken(const char * permList[],int permSize)42 void AdapterLoglibraryTestTools::NativeToken(const char* permList[], int permSize)
43 {
44 uint64_t tokenId;
45 NativeTokenInfoParams tokenInfo = {
46 .dcapsNum = 0,
47 .permsNum = permSize,
48 .aclsNum = 0,
49 .dcaps = nullptr,
50 .perms = permList,
51 .acls = nullptr,
52 .aplStr = "system_basic",
53 };
54
55 tokenInfo.processName = "AdapterLoglibraryIdlTest";
56 tokenId = GetAccessTokenId(&tokenInfo);
57 SetSelfTokenID(tokenId);
58 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
59 }
60
ApplyPermissionAccess()61 void AdapterLoglibraryTestTools::ApplyPermissionAccess()
62 {
63 const char* permList[] = {
64 "ohos.permission.WRITE_HIVIEW_SYSTEM",
65 "ohos.permission.READ_HIVIEW_SYSTEM",
66 };
67 constexpr int permSize = 2;
68 NativeToken(permList, permSize); // 2 is the size of the array which consists of required permissions.
69 }
70 }
71 }