1 /*
2  * Copyright (C) 2023 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 SMS_MMS_GTEST_H
16 #define SMS_MMS_GTEST_H
17 
18 #include "accesstoken_kit.h"
19 #include "telephony_log_wrapper.h"
20 #include "token_setproc.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 using namespace Security::AccessToken;
25 using Security::AccessToken::AccessTokenID;
26 
27 class AccessMmsToken {
28     HapInfoParams testMmsInfoParams = {
29         .userID = 1,
30         .bundleName = "tel_sms_mms_gtest",
31         .instIndex = 0,
32         .appIDDesc = "test",
33         .isSystemApp = true,
34     };
35     PermissionDef testPermReceiveSmsDef = {
36         .permissionName = "ohos.permission.RECEIVE_SMS",
37         .bundleName = "tel_sms_mms_gtest",
38         .grantMode = 1, // SYSTEM_GRANT
39         .availableLevel = APL_SYSTEM_BASIC,
40         .label = "label",
41         .labelId = 1,
42         .description = "Test sms manager",
43         .descriptionId = 1,
44     };
45     PermissionStateFull testReceiveSmsState = {
46         .permissionName = "ohos.permission.RECEIVE_SMS",
47         .isGeneral = true,
48         .resDeviceID = { "local" },
49         .grantStatus = { PermissionState::PERMISSION_GRANTED },
50         .grantFlags = { 2 }, // PERMISSION_USER_SET
51     };
52     PermissionDef testPermSendSmsDef = {
53         .permissionName = "ohos.permission.SEND_MESSAGES",
54         .bundleName = "tel_sms_mms_gtest",
55         .grantMode = 1, // SYSTEM_GRANT
56         .availableLevel = APL_SYSTEM_BASIC,
57         .label = "label",
58         .labelId = 1,
59         .description = "Test sms manager",
60         .descriptionId = 1,
61     };
62     PermissionStateFull testSendSmsState = {
63         .permissionName = "ohos.permission.SEND_MESSAGES",
64         .isGeneral = true,
65         .resDeviceID = { "local" },
66         .grantStatus = { PermissionState::PERMISSION_GRANTED },
67         .grantFlags = { 2 }, // PERMISSION_USER_SET
68     };
69     PermissionDef testPermSetTelephonyDef = {
70         .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
71         .bundleName = "tel_sms_mms_gtest",
72         .grantMode = 1, // SYSTEM_GRANT
73         .availableLevel = APL_SYSTEM_BASIC,
74         .label = "label",
75         .labelId = 1,
76         .description = "Test sms manager",
77         .descriptionId = 1,
78     };
79     PermissionStateFull testSetTelephonyState = {
80         .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
81         .isGeneral = true,
82         .resDeviceID = { "local" },
83         .grantStatus = { PermissionState::PERMISSION_GRANTED },
84         .grantFlags = { 2 }, // PERMISSION_USER_SET
85     };
86     PermissionDef testPermGetTelephonyDef = {
87         .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
88         .bundleName = "tel_sms_mms_gtest",
89         .grantMode = 1, // SYSTEM_GRANT
90         .availableLevel = APL_SYSTEM_BASIC,
91         .label = "label",
92         .labelId = 1,
93         .description = "Test sms manager",
94         .descriptionId = 1,
95     };
96     PermissionStateFull testGetTelephonyState = {
97         .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
98         .isGeneral = true,
99         .resDeviceID = { "local" },
100         .grantStatus = { PermissionState::PERMISSION_GRANTED },
101         .grantFlags = { 2 }, // PERMISSION_USER_SET
102     };
103     PermissionDef testSetDataBaseDef = {
104         .permissionName = "ohos.permission.READ_MESSAGES",
105         .bundleName = "tel_sms_mms_gtest",
106         .grantMode = 1, // SYSTEM_GRANT
107         .availableLevel = APL_SYSTEM_BASIC,
108         .label = "label",
109         .labelId = 1,
110         .description = "Test sms manager",
111         .descriptionId = 1,
112     };
113     PermissionStateFull testSetDataBaseState = {
114         .permissionName = "ohos.permission.READ_MESSAGES",
115         .isGeneral = true,
116         .resDeviceID = { "local" },
117         .grantStatus = { PermissionState::PERMISSION_GRANTED },
118         .grantFlags = { 2 }, // PERMISSION_USER_SET
119     };
120     PermissionDef testReceiveMmsDef = {
121         .permissionName = "ohos.permission.RECEIVE_MMS",
122         .bundleName = "tel_sms_mms_gtest",
123         .grantMode = 1, // SYSTEM_GRANT
124         .availableLevel = APL_SYSTEM_BASIC,
125         .label = "label",
126         .labelId = 1,
127         .description = "Test sms manager",
128         .descriptionId = 1,
129     };
130     PermissionStateFull testReceiveMmsState = {
131         .permissionName = "ohos.permission.RECEIVE_MMS",
132         .isGeneral = true,
133         .resDeviceID = { "local" },
134         .grantStatus = { PermissionState::PERMISSION_GRANTED },
135         .grantFlags = { 2 }, // PERMISSION_USER_SET
136     };
137         PermissionDef testReceiveMessagesDef = {
138         .permissionName = "ohos.permission.RECEIVE_MESSAGES",
139         .bundleName = "tel_sms_mms_gtest",
140         .grantMode = 1, // SYSTEM_GRANT
141         .availableLevel = APL_SYSTEM_BASIC,
142         .label = "label",
143         .labelId = 1,
144         .description = "Test sms manager",
145         .descriptionId = 1,
146     };
147     PermissionStateFull testReceiveMessagesState = {
148         .permissionName = "ohos.permission.RECEIVE_MESSAGES",
149         .isGeneral = true,
150         .resDeviceID = { "local" },
151         .grantStatus = { PermissionState::PERMISSION_GRANTED },
152         .grantFlags = { 2 }, // PERMISSION_USER_SET
153     };
154     HapPolicyParams testMmsPolicyParams = {
155         .apl = APL_SYSTEM_BASIC,
156         .domain = "test.domain",
157         .permList = { testPermReceiveSmsDef, testPermSendSmsDef, testPermSetTelephonyDef, testPermGetTelephonyDef,
158             testSetDataBaseDef, testReceiveMmsDef, testReceiveMessagesDef, },
159         .permStateList = { testReceiveSmsState, testSendSmsState, testSetTelephonyState, testGetTelephonyState,
160             testSetDataBaseState, testReceiveMmsState, testReceiveMessagesState, },
161     };
162 
163 public:
AccessMmsToken()164     AccessMmsToken()
165     {
166         currentID_ = GetSelfTokenID();
167         TELEPHONY_LOGI("AccessMmsToken currentID_%{public}d", currentID_);
168         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testMmsInfoParams, testMmsPolicyParams);
169         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
170         SetSelfTokenID(tokenIdEx.tokenIDEx);
171     }
~AccessMmsToken()172     ~AccessMmsToken()
173     {
174         TELEPHONY_LOGI("AccessMmsToken  ~AccessMmsToken");
175         AccessTokenKit::DeleteToken(accessID_);
176         SetSelfTokenID(currentID_);
177     }
178 
179 private:
180     AccessTokenID currentID_ = 0;
181     AccessTokenID accessID_ = 0;
182 };
183 } // namespace Telephony
184 } // namespace OHOS
185 #endif