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 #ifndef OHOS_STATE_REGISTRY_BRANCH_TEST_H
16 #define OHOS_STATE_REGISTRY_BRANCH_TEST_H
17
18 #include <iostream>
19 #include "security_token.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 using namespace Security::AccessToken;
26 using Security::AccessToken::AccessTokenID;
27 inline HapInfoParams testInfoParams = {
28 .bundleName = "tel_state_registry_branch_test",
29 .userID = 1,
30 .instIndex = 0,
31 .appIDDesc = "test",
32 .isSystemApp = false,
33 };
34
35 inline PermissionDef testPermGetTelephonyStateDef = {
36 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
37 .bundleName = "tel_state_registry_branch_test",
38 .grantMode = 1, // SYSTEM_GRANT
39 .label = "label",
40 .labelId = 1,
41 .description = "Test state registry",
42 .descriptionId = 1,
43 .availableLevel = APL_SYSTEM_BASIC,
44 };
45
46 inline PermissionStateFull testGetTelephonyState = {
47 .grantFlags = { 2 }, // PERMISSION_USER_SET
48 .grantStatus = { PermissionState::PERMISSION_GRANTED },
49 .isGeneral = true,
50 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
51 .resDeviceID = { "local" },
52 };
53
54 inline PermissionDef testPermSetTelephonyStateDef = {
55 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
56 .bundleName = "tel_state_registry_branch_test",
57 .grantMode = 1, // SYSTEM_GRANT
58 .label = "label",
59 .labelId = 1,
60 .description = "Test core service",
61 .descriptionId = 1,
62 .availableLevel = APL_SYSTEM_BASIC,
63 };
64
65 inline PermissionStateFull testSetTelephonyState = {
66 .grantFlags = { 2 }, // PERMISSION_USER_SET
67 .grantStatus = { PermissionState::PERMISSION_GRANTED },
68 .isGeneral = true,
69 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
70 .resDeviceID = { "local" },
71 };
72
73 inline PermissionDef testPermGetNetworkInfoDef = {
74 .permissionName = "ohos.permission.GET_NETWORK_INFO",
75 .bundleName = "tel_state_registry_branch_test",
76 .grantMode = 1, // SYSTEM_GRANT
77 .label = "label",
78 .labelId = 1,
79 .description = "Test core service",
80 .descriptionId = 1,
81 .availableLevel = APL_SYSTEM_BASIC,
82 };
83
84 inline PermissionStateFull testPermGetNetworkInfo = {
85 .grantFlags = { 2 }, // PERMISSION_USER_SET
86 .grantStatus = { PermissionState::PERMISSION_GRANTED },
87 .isGeneral = true,
88 .permissionName = "ohos.permission.GET_NETWORK_INFO",
89 .resDeviceID = { "local" },
90 };
91
92 inline PermissionDef testSimPermWriteContactsDef = {
93 .permissionName = "ohos.permission.WRITE_CONTACTS",
94 .bundleName = "tel_state_registry_branch_test",
95 .grantMode = 1, // SYSTEM_GRANT
96 .label = "label",
97 .labelId = 1,
98 .description = "Test core service",
99 .descriptionId = 1,
100 .availableLevel = APL_SYSTEM_BASIC,
101 };
102
103 inline PermissionStateFull testSimPermWriteContacts = {
104 .grantFlags = { 2 }, // PERMISSION_USER_SET
105 .grantStatus = { PermissionState::PERMISSION_GRANTED },
106 .isGeneral = true,
107 .permissionName = "ohos.permission.WRITE_CONTACTS",
108 .resDeviceID = { "local" },
109 };
110
111 inline PermissionDef testSimPermReadContactsDef = {
112 .permissionName = "ohos.permission.READ_CONTACTS",
113 .bundleName = "tel_state_registry_branch_test",
114 .grantMode = 1, // SYSTEM_GRANT
115 .label = "label",
116 .labelId = 1,
117 .description = "Test core service",
118 .descriptionId = 1,
119 .availableLevel = APL_SYSTEM_BASIC,
120 };
121
122 inline PermissionStateFull testSimPermReadContacts = {
123 .grantFlags = { 2 }, // PERMISSION_USER_SET
124 .grantStatus = { PermissionState::PERMISSION_GRANTED },
125 .isGeneral = true,
126 .permissionName = "ohos.permission.READ_CONTACTS",
127 .resDeviceID = { "local" },
128 };
129
130 inline HapPolicyParams testStateRegistryPolicyParams = {
131 .apl = APL_SYSTEM_BASIC,
132 .domain = "test.domain",
133 .permList = { testPermGetTelephonyStateDef, testPermSetTelephonyStateDef, testPermGetNetworkInfoDef,
134 testSimPermWriteContactsDef, testSimPermReadContactsDef },
135 .permStateList = { testGetTelephonyState, testSetTelephonyState, testPermGetNetworkInfo, testSimPermWriteContacts,
136 testSimPermReadContacts },
137 };
138
SecurityToken()139 SecurityToken::SecurityToken()
140 {
141 currentID_ = GetSelfTokenID();
142 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testStateRegistryPolicyParams);
143 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
144 SetSelfTokenID(tokenIdEx.tokenIDEx);
145 }
146
~SecurityToken()147 SecurityToken::~SecurityToken()
148 {
149 AccessTokenKit::DeleteToken(accessID_);
150 SetSelfTokenID(currentID_);
151 }
152
153 } // namespace Telephony
154 } // namespace OHOS
155 #endif // OHOS_STATE_REGISTRY_BRANCH_TEST_H