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 #include <iostream>
16 #include "common_test_utils.h"
17
18 #include <access_token.h>
19 #include <accesstoken_kit.h>
20 #include <nativetoken_kit.h>
21 #include <token_setproc.h>
22 #include <securec.h>
23
24 namespace OHOS::Rosen {
InjectTokenInfoByHapName(int userID, const std::string& bundleName, int instIndex)25 void CommonTestUtils::InjectTokenInfoByHapName(int userID, const std::string& bundleName, int instIndex)
26 {
27 Security::AccessToken::AccessTokenID tokenId =
28 Security::AccessToken::AccessTokenKit::GetHapTokenID(userID, bundleName, instIndex);
29 SetSelfTokenID(tokenId);
30 }
31
CreatePixelMap()32 std::shared_ptr<Media::PixelMap> CommonTestUtils::CreatePixelMap()
33 {
34 // pixel_map testing code
35 Media::InitializationOptions opt;
36 opt.size.width = TEST_IMAGE_WIDTH;
37 opt.size.height = TEST_IMAGE_HEIGHT;
38 opt.pixelFormat = Media::PixelFormat::RGBA_8888;
39 opt.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
40 opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
41 opt.editable = false;
42 opt.useSourceIfMatch = false;
43
44 const int bitmapDepth = 8; // color depth
45 const int bpp = 4; // bytes per pixel
46 const int pixelValue = 125;
47
48 const int voulumeSize = opt.size.width * opt.size.height * bpp;
49 auto data = (uint32_t *)malloc(voulumeSize);
50 if (data == nullptr) {
51 return nullptr;
52 }
53
54 uint8_t *pic = (uint8_t *)data;
55 if (memset_s(pic, voulumeSize, pixelValue, voulumeSize) != EOK) {
56 free(data);
57 return nullptr;
58 }
59
60 uint32_t colorLen = voulumeSize * bitmapDepth;
61 auto pixelMap = Media::PixelMap::Create(data, colorLen, opt);
62 free(data);
63 if (pixelMap == nullptr) {
64 return nullptr;
65 }
66 std::shared_ptr<Media::PixelMap> pixelMap_(pixelMap.release());
67 return pixelMap_;
68 }
69
SetAceessTokenPermission(const std::string processName)70 void CommonTestUtils::SetAceessTokenPermission(const std::string processName)
71 {
72 uint64_t tokenId;
73 NativeTokenInfoParams infoInstance = {
74 .dcapsNum = 0,
75 .permsNum = 0,
76 .aclsNum = 0,
77 .dcaps = nullptr,
78 .perms = nullptr,
79 .acls = nullptr,
80 .processName = processName.c_str(),
81 .aplStr = "system_basic",
82 };
83 tokenId = GetAccessTokenId(&infoInstance);
84 SetSelfTokenID(tokenId);
85 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
86 }
87
SetAceessTokenPermission(const std::string processName, const char** perms, const int permCount)88 void CommonTestUtils::SetAceessTokenPermission(const std::string processName,
89 const char** perms, const int permCount)
90 {
91 if (perms == nullptr || permCount == 0) {
92 return;
93 }
94 uint64_t tokenId;
95 NativeTokenInfoParams infoInstance = {
96 .dcapsNum = 0,
97 .permsNum = permCount,
98 .aclsNum = 0,
99 .dcaps = nullptr,
100 .perms = perms,
101 .acls = nullptr,
102 .processName = processName.c_str(),
103 .aplStr = "system_basic",
104 };
105 tokenId = GetAccessTokenId(&infoInstance);
106 SetSelfTokenID(tokenId);
107 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
108 }
109
GuaranteeFloatWindowPermission(const std::string processName)110 void CommonTestUtils::GuaranteeFloatWindowPermission(const std::string processName)
111 {
112 const char **perms = new const char *[1];
113 perms[0] = "ohos.permission.SYSTEM_FLOAT_WINDOW";
114 NativeTokenInfoParams infoInstance = {
115 .dcapsNum = 0,
116 .permsNum = 1,
117 .aclsNum = 0,
118 .dcaps = nullptr,
119 .perms = perms,
120 .acls = nullptr,
121 .processName = processName.c_str(),
122 .aplStr = "system_core",
123 };
124 uint64_t tokenId = GetAccessTokenId(&infoInstance);
125 auto ret = SetSelfTokenID(tokenId);
126 auto ret1 = OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
127 std::cout << "Guarantee float window permission, processName: " << processName << ", tokenId: " <<
128 tokenId << ", set selfTokenId return: " << ret << ", reload native tokenInfo return: " << ret1 << std::endl;
129 delete[] perms;
130 }
131 } // namespace OHOS::Rosen