1 /*
2 * Copyright (c) 2025 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 "audio_haptic_test_common.h"
17
18 #include <sstream>
19 #include "accesstoken_kit.h"
20 #include "ipc_skeleton.h"
21 #include "token_setproc.h"
22
23 namespace OHOS {
24 namespace Media {
25 using namespace Security::AccessToken;
26 using Security::AccessToken::AccessTokenID;
27
28 namespace {
29 static const uint64_t SHELL_TOKEN_ID = IPCSkeleton::GetSelfTokenID();
30
31 const std::vector<std::string> ALL_PERMISSION_LIST {
32 "ohos.permission.MICROPHONE",
33 "ohos.permission.ACCESS_NOTIFICATION_POLICY",
34 "ohos.permission.VIBRATE"
35 };
36 }
37
GetTokenId(const AtmToolsParamInfo & info)38 static uint64_t GetTokenId(const AtmToolsParamInfo &info)
39 {
40 std::string dumpInfo;
41 AccessTokenKit::DumpTokenInfo(info, dumpInfo);
42 size_t pos = dumpInfo.find("\"tokenID\": ");
43 if (pos == std::string::npos) {
44 return 0;
45 }
46 pos += std::string("\"tokenID\": ").length();
47 std::string numStr;
48 while (pos < dumpInfo.length() && std::isdigit(dumpInfo[pos])) {
49 numStr += dumpInfo[pos];
50 ++pos;
51 }
52
53 std::istringstream iss(numStr);
54 uint64_t tokenID;
55 iss >> tokenID;
56 return tokenID;
57 }
58
GetTokenIdFromProcess(const std::string & process)59 uint64_t GetTokenIdFromProcess(const std::string &process)
60 {
61 auto tokenId = IPCSkeleton::GetSelfTokenID();
62 SetSelfTokenID(SHELL_TOKEN_ID); // only shell can dump tokenid
63
64 AtmToolsParamInfo info;
65 info.processName = process;
66 auto res = GetTokenId(info);
67
68 SetSelfTokenID(tokenId);
69 return res;
70 }
71
GetTokenIdFromBundleName(const std::string & bundleName)72 uint64_t GetTokenIdFromBundleName(const std::string &bundleName)
73 {
74 auto tokenId = IPCSkeleton::GetSelfTokenID();
75 SetSelfTokenID(SHELL_TOKEN_ID); // only shell can dump tokenid
76
77 AtmToolsParamInfo info;
78 info.bundleName = bundleName;
79 auto res = GetTokenId(info);
80
81 SetSelfTokenID(tokenId);
82 return res;
83 }
84
MockTokenId(const std::string & process)85 bool MockTokenId(const std::string &process)
86 {
87 auto mockTokenId = GetTokenIdFromProcess(process);
88 if (mockTokenId == 0) {
89 return false;
90 }
91 if (SetSelfTokenID(mockTokenId) != 0) {
92 return false;
93 }
94 return IPCSkeleton::GetSelfTokenID() != 0;
95 }
96
GetPermission(std::vector<std::string> permissions,uint64_t & tokenID,bool isSystemApp)97 bool GetPermission(std::vector<std::string> permissions, uint64_t &tokenID, bool isSystemApp)
98 {
99 if (!MockTokenId("foundation")) {
100 return false;
101 }
102 std::vector<PermissionStateFull> permissionStates;
103 for (const auto& permission : permissions) {
104 PermissionStateFull permissionState = {
105 .permissionName = permission,
106 .isGeneral = true,
107 .resDeviceID = {"local"},
108 .grantStatus = {PermissionState::PERMISSION_GRANTED},
109 .grantFlags = {PERMISSION_SYSTEM_FIXED}
110 };
111 permissionStates.emplace_back(permissionState);
112 }
113 HapPolicyParams hapPolicyParams = {
114 .apl = APL_NORMAL,
115 .domain = "test.domain",
116 .permList = {},
117 .permStateList = permissionStates
118 };
119
120 HapInfoParams hapInfoParams = {
121 .userID = 100,
122 .bundleName = "audio_haptic_test",
123 .instIndex = 0,
124 .appIDDesc = "audio_haptic_test",
125 .apiVersion = 8,
126 .isSystemApp = isSystemApp
127 };
128
129 AccessTokenIDEx tokenIdEx = {0};
130 tokenIdEx = AccessTokenKit::AllocHapToken(hapInfoParams, hapPolicyParams);
131 tokenID = tokenIdEx.tokenIDEx;
132 if (!((INVALID_TOKENID != tokenIdEx.tokenIDEx) && (0 == SetSelfTokenID(tokenIdEx.tokenIDEx)))) {
133 return false;
134 }
135 return tokenID == IPCSkeleton::GetSelfTokenID();
136 }
137
RecoveryPermission(uint64_t tokenID,uint64_t oldTokenID)138 bool RecoveryPermission(uint64_t tokenID, uint64_t oldTokenID)
139 {
140 if (!MockTokenId("foundation")) {
141 return false;
142 }
143 if (!((ERR_OK == AccessTokenKit::DeleteToken(tokenID)) && (ERR_OK == SetSelfTokenID(oldTokenID)))) {
144 return false;
145 }
146 return oldTokenID == IPCSkeleton::GetSelfTokenID();
147 }
148
GetAllPermission()149 uint64_t GetAllPermission()
150 {
151 if (!MockTokenId("foundation")) {
152 return 0;
153 }
154 std::vector<PermissionStateFull> permissionStates;
155 for (const auto& permission : ALL_PERMISSION_LIST) {
156 PermissionStateFull permissionState = {
157 .permissionName = permission,
158 .isGeneral = true,
159 .resDeviceID = {"local"},
160 .grantStatus = {PermissionState::PERMISSION_GRANTED},
161 .grantFlags = {PERMISSION_SYSTEM_FIXED}
162 };
163 permissionStates.emplace_back(permissionState);
164 }
165 HapPolicyParams hapPolicyParams = {
166 .apl = APL_NORMAL,
167 .domain = "audio_haptic_test_setup.domain",
168 .permList = {},
169 .permStateList = permissionStates
170 };
171
172 HapInfoParams hapInfoParams = {
173 .userID = 100,
174 .bundleName = "audio_haptic_test_setup",
175 .instIndex = 0,
176 .appIDDesc = "audio_haptic_test_setup",
177 .apiVersion = 8,
178 .isSystemApp = true
179 };
180
181 AccessTokenIDEx tokenIdEx = {0};
182 tokenIdEx = AccessTokenKit::AllocHapToken(hapInfoParams, hapPolicyParams);
183 auto tokenID = tokenIdEx.tokenIDEx;
184 if (!((INVALID_TOKENID != tokenIdEx.tokenIDEx) && (0 == SetSelfTokenID(tokenIdEx.tokenIDEx)) &&
185 (tokenID == IPCSkeleton::GetSelfTokenID()))) {
186 return 0;
187 }
188 return tokenID;
189 }
190 } // namespace Media
191 } // namespace OHOS