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