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 #include <sstream>
16 #include <thread>
17
18 #include "accesstoken_kit.h"
19 #include "ipc_skeleton.h"
20 #include "test_token.h"
21 #include "token_setproc.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25 using namespace OHOS::Security::AccessToken;
26
27 uint64_t TestToken::shellTokenID_ = IPCSkeleton::GetSelfTokenID();
28
GetMockedTokenId()29 uint64_t TestToken::GetMockedTokenId()
30 {
31 return IPCSkeleton::GetSelfTokenID();
32 }
33
GetOriginalTokenId()34 uint64_t TestToken::GetOriginalTokenId()
35 {
36 return shellTokenID_;
37 }
38
AddPermission(const std::string & permission)39 void TestToken::AddPermission(const std::string& permission)
40 {
41 permissions_.emplace_back(permission);
42 }
43
GetTokenId(const AtmToolsParamInfo & info)44 uint64_t TestToken::GetTokenId(const AtmToolsParamInfo& info)
45 {
46 std::string dumpInfo;
47 AccessTokenKit::DumpTokenInfo(info, dumpInfo);
48 size_t pos = dumpInfo.find("\"tokenID\": ");
49 if (pos == std::string::npos) {
50 return 0;
51 }
52 pos += std::string("\"tokenID\": ").length();
53 std::string numStr;
54 while (pos < dumpInfo.length() && std::isdigit(dumpInfo[pos])) {
55 numStr += dumpInfo[pos];
56 ++pos;
57 }
58
59 std::istringstream iss(numStr);
60 uint64_t tokenID;
61 iss >> tokenID;
62 return tokenID;
63 }
64
GetTokenIdFromProcess(const std::string & process)65 uint64_t TestToken::GetTokenIdFromProcess(const std::string& process)
66 {
67 auto tokenId = IPCSkeleton::GetSelfTokenID();
68 SetSelfTokenID(shellTokenID_); // only shell can dump tokenid
69
70 AtmToolsParamInfo info;
71 info.processName = process;
72 auto processId = GetTokenId(info);
73
74 SetSelfTokenID(tokenId);
75 return processId;
76 }
77
MockTokenId(const std::string & process)78 bool TestToken::MockTokenId(const std::string& process)
79 {
80 auto mockTokenId = GetTokenIdFromProcess(process);
81 if (mockTokenId == 0) {
82 return false;
83 }
84 if (SetSelfTokenID(mockTokenId) != 0) {
85 return false;
86 }
87 return IPCSkeleton::GetSelfTokenID() != 0;
88 }
89
GetAllCameraPermission()90 bool TestToken::GetAllCameraPermission()
91 {
92 if (!MockTokenId("foundation")) {
93 return false;
94 }
95
96 std::vector<std::string> cameraPermissions {
97 "ohos.permission.CAMERA",
98 "ohos.permission.CAMERA_BACKGROUND",
99 "ohos.permission.CAMERA_CONTROL",
100 "ohos.permission.DISTRIBUTED_DATASYNC",
101 "ohos.permission.GET_SENSITIVE_PERMISSIONS",
102 "ohos.permission.MICROPHONE",
103 "ohos.permission.PERMISSION_USED_STATS",
104 "ohos.permission.READ_IMAGEVIDEO",
105 "ohos.permission.READ_MEDIA",
106 "ohos.permission.WRITE_IMAGEVIDEO",
107 "ohos.permission.WRITE_MEDIA",
108 };
109 cameraPermissions.insert(cameraPermissions.end(), permissions_.begin(), permissions_.end());
110 std::vector<PermissionStateFull> permissionStates;
111 for (const auto& permission : cameraPermissions) {
112 PermissionStateFull permissionState = { .permissionName = permission,
113 .isGeneral = true,
114 .resDeviceID = { "local" },
115 .grantStatus = { PermissionState::PERMISSION_GRANTED },
116 .grantFlags = { PERMISSION_SYSTEM_FIXED } };
117 permissionStates.emplace_back(permissionState);
118 }
119 HapPolicyParams hapPolicyParams = {
120 .apl = APL_NORMAL, .domain = "camera_test_setup.domain", .permList = {}, .permStateList = permissionStates
121 };
122
123 HapInfoParams hapInfoParams = { .userID = 100,
124 .bundleName = "camera_test_setup",
125 .instIndex = 0,
126 .appIDDesc = "camera_test_setup",
127 .apiVersion = 8,
128 .isSystemApp = isSystemApp_ };
129
130 AccessTokenIDEx tokenIdEx = { 0 };
131 tokenIdEx = AccessTokenKit::AllocHapToken(hapInfoParams, hapPolicyParams);
132 auto tokenID = tokenIdEx.tokenIDEx;
133 if (!(INVALID_TOKENID != tokenID && 0 == SetSelfTokenID(tokenID) && tokenID == IPCSkeleton::GetSelfTokenID())) {
134 return false;
135 }
136 return true;
137 }
138
139 } // namespace CameraStandard
140 } // namespace OHOS