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