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 
16 #ifndef TELEPHONY_SATELLITE_TEST_H
17 #define TELEPHONY_SATELLITE_TEST_H
18 #include <securec.h>
19 
20 #include "accesstoken_kit.h"
21 #include "call_manager_errors.h"
22 #include "cellular_call_handler.h"
23 #include "cellular_call_interface.h"
24 #include "cellular_call_ipc_interface_code.h"
25 #include "core_manager_inner.h"
26 #include "core_service_client.h"
27 #include "gtest/gtest.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30 #include "telephony_log_wrapper.h"
31 #include "telephony_permission.h"
32 #include "token_setproc.h"
33 
34 namespace OHOS {
35 namespace Telephony {
36 using namespace Security::AccessToken;
37 using Security::AccessToken::AccessTokenID;
38 
39 inline HapInfoParams testInfoParams = {
40     .bundleName = "tel_cellular_call_Satellite_gtest",
41     .userID = 1,
42     .instIndex = 0,
43     .appIDDesc = "test",
44     .isSystemApp = true,
45 };
46 
47 inline PermissionDef testConnectSatelliteServiceDef = {
48     .permissionName = "ohos.permission.CONNECT_SATELLITE_SERVICE",
49     .bundleName = "tel_cellular_call_Satellite_gtest",
50     .grantMode = 1, // SYSTEM_GRANT
51     .label = "label",
52     .labelId = 1,
53     .description = "Test cellular call",
54     .descriptionId = 1,
55     .availableLevel = APL_SYSTEM_BASIC,
56 };
57 
58 inline PermissionStateFull testConnectSatelliteServiceState = {
59     .grantFlags = { 2 }, // PERMISSION_USER_SET
60     .grantStatus = { PermissionState::PERMISSION_GRANTED },
61     .isGeneral = true,
62     .permissionName = "ohos.permission.CONNECT_SATELLITE_SERVICE",
63     .resDeviceID = { "local" },
64 };
65 
66 inline PermissionDef testPermPlaceCallDef = {
67     .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE",
68     .bundleName = "tel_cellular_call_Satellite_gtest",
69     .grantMode = 1, // SYSTEM_GRANT
70     .label = "label",
71     .labelId = 1,
72     .description = "Test cellular call",
73     .descriptionId = 1,
74     .availableLevel = APL_SYSTEM_BASIC,
75 };
76 
77 inline PermissionStateFull testPlaceCallState = {
78     .grantFlags = { 2 }, // PERMISSION_USER_SET
79     .grantStatus = { PermissionState::PERMISSION_GRANTED },
80     .isGeneral = true,
81     .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE",
82     .resDeviceID = { "local" },
83 };
84 
85 inline PermissionDef testGetTelephonyStateDef = {
86     .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
87     .bundleName = "tel_cellular_call_Satellite_gtest",
88     .grantMode = 1, // SYSTEM_GRANT
89     .label = "label",
90     .labelId = 1,
91     .description = "Test cellular call",
92     .descriptionId = 1,
93     .availableLevel = APL_SYSTEM_BASIC,
94 };
95 
96 inline PermissionStateFull testGetTelephonyState = {
97     .grantFlags = { 2 }, // PERMISSION_USER_SET
98     .grantStatus = { PermissionState::PERMISSION_GRANTED },
99     .isGeneral = true,
100     .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
101     .resDeviceID = { "local" },
102 };
103 
104 inline HapPolicyParams testPolicyParams = {
105     .apl = APL_SYSTEM_BASIC,
106     .domain = "test.domain",
107     .permList = { testPermPlaceCallDef, testConnectSatelliteServiceDef, testGetTelephonyStateDef },
108     .permStateList = { testPlaceCallState, testConnectSatelliteServiceState, testGetTelephonyState },
109 };
110 
111 class AccessToken {
112 public:
AccessToken()113     AccessToken()
114     {
115         currentID_ = GetSelfTokenID();
116         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
117         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
118         SetSelfTokenID(tokenIdEx.tokenIDEx);
119     }
~AccessToken()120     ~AccessToken()
121     {
122         AccessTokenKit::DeleteToken(accessID_);
123         SetSelfTokenID(currentID_);
124     }
125 
126 private:
127     AccessTokenID currentID_ = 0;
128     AccessTokenID accessID_ = 0;
129 };
130 
131 class SatelliteTest : public testing::Test {
132 public:
133     static void SetUpTestCase();
134     static void TearDownTestCase();
135     void SetUp();
136     void TearDown();
137 
HasSimCard(int32_t slotId)138     bool HasSimCard(int32_t slotId)
139     {
140         bool hasSimCard = false;
141         DelayedRefSingleton<CoreServiceClient>::GetInstance().HasSimCard(slotId, hasSimCard);
142         return hasSimCard;
143     }
144 
InitCellularCallInfo(int32_t accountId,std::string phonenumber,CellularCallInfo & callInfo)145     int32_t InitCellularCallInfo(int32_t accountId, std::string phonenumber, CellularCallInfo &callInfo)
146     {
147         callInfo.accountId = accountId;
148         callInfo.slotId = accountId;
149         callInfo.index = 0;
150         callInfo.callType = CallType::TYPE_SATELLITE;
151         callInfo.videoState = 0; // 0 means audio
152         if (memset_s(callInfo.phoneNum, kMaxNumberLen, 0, kMaxNumberLen) != EOK) {
153             return TELEPHONY_ERR_MEMSET_FAIL;
154         }
155         if (phonenumber.length() > static_cast<size_t>(kMaxNumberLen)) {
156             return CALL_ERR_NUMBER_OUT_OF_RANGE;
157         }
158         if (memcpy_s(callInfo.phoneNum, kMaxNumberLen, phonenumber.c_str(), phonenumber.length()) != EOK) {
159             return TELEPHONY_ERR_MEMCPY_FAIL;
160         }
161         return TELEPHONY_SUCCESS;
162     };
163 
TestDialCallBySatellite(int32_t slotId,std::string code)164     int32_t TestDialCallBySatellite(int32_t slotId, std::string code)
165     {
166         AccessToken token;
167         auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
168         if (saMgr == nullptr) {
169             return TELEPHONY_ERR_FAIL;
170         }
171         auto remote = saMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
172         if (remote == nullptr) {
173             return TELEPHONY_ERR_FAIL;
174         }
175         auto telephonyService = iface_cast<CellularCallInterface>(remote);
176         if (telephonyService == nullptr) {
177             return TELEPHONY_ERR_FAIL;
178         }
179         CellularCallInfo SatelliteCellularCallInfo;
180         int32_t ret = TELEPHONY_SUCCESS;
181         ret = InitCellularCallInfo(slotId, code, SatelliteCellularCallInfo);
182         if (ret != TELEPHONY_SUCCESS) {
183             return ret;
184         }
185         ret = telephonyService->Dial(SatelliteCellularCallInfo);
186         return ret;
187     };
188 };
189 } // namespace Telephony
190 } // namespace OHOS
191 
192 #endif // TELEPHONY_SATELLITE_TEST_H
193