• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef TEST_COMMON_H
17 #define TEST_COMMON_H
18 
19 #include "access_token.h"
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "permission_def.h"
23 #include "permission_state_full.h"
24 #include "token_setproc.h"
25 
26 namespace OHOS {
27 namespace Security {
28 namespace AccessToken {
29 class MockNativeToken {
30 public:
31     explicit MockNativeToken(const std::string& process);
32     ~MockNativeToken();
33 private:
34     uint64_t selfToken_;
35 };
36 
37 class MockHapToken {
38 public:
39     explicit MockHapToken(
40         const std::string& bundle, const std::vector<std::string>& reqPerm, bool isSystemApp = true);
41     ~MockHapToken();
42 private:
43     uint64_t selfToken_;
44     uint32_t mockToken_;
45 };
46 
47 class TestCommon {
48 public:
49     static constexpr int32_t DEFAULT_API_VERSION = 12;
50 
51     static void SetTestEvironment(uint64_t shellTokenId);
52     static void ResetTestEvironment();
53     static uint64_t GetShellTokenId();
54 
55     static void GetHapParams(HapInfoParams& infoParams, HapPolicyParams& policyParams);
56     static void TestPreparePermStateList(HapPolicyParams &policy);
57     static void TestPreparePermDefList(HapPolicyParams &policy);
58     static void TestPrepareKernelPermissionStatus(HapPolicyParams& policyParams);
59     static HapPolicyParams GetTestPolicyParams();
60     static HapInfoParams GetInfoManagerTestInfoParms();
61     static HapInfoParams GetInfoManagerTestNormalInfoParms();
62     static HapInfoParams GetInfoManagerTestSystemInfoParms();
63     static HapPolicyParams GetInfoManagerTestPolicyPrams();
64 
65     static int32_t AllocTestHapToken(const HapInfoParams& hapInfo,
66         HapPolicyParams& hapPolicy, AccessTokenIDEx& tokenIdEx);
67     static AccessTokenIDEx AllocAndGrantHapTokenByTest(const HapInfoParams& info, HapPolicyParams& policy);
68     static int32_t DeleteTestHapToken(AccessTokenID tokenID);
69     static AccessTokenID GetNativeTokenIdFromProcess(const std::string& process);
70     static AccessTokenIDEx GetHapTokenIdFromBundle(
71         int32_t userID, const std::string& bundleName, int32_t instIndex);
72     static int32_t GrantPermissionByTest(AccessTokenID tokenID, const std::string& permission, uint32_t flag);
73     static int32_t RevokePermissionByTest(AccessTokenID tokenID, const std::string& permission, uint32_t flag);
74     static int32_t ClearUserGrantedPermissionStateByTest(AccessTokenID tokenID);
75     static int32_t GetReqPermissionsByTest(
76         AccessTokenID tokenID, std::vector<PermissionStateFull>& permStatList, bool isSystemGrant);
77     static int32_t GetPermissionFlagByTest(AccessTokenID tokenID, const std::string& permission, uint32_t& flag);
78 };
79 }  // namespace AccessToken
80 }  // namespace Security
81 }  // namespace OHOS
82 #endif  // TEST_COMMON_H
83