1 /*
2  * Copyright (C) 2021-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_CS_TEST_H
17 #define TELEPHONY_CS_TEST_H
18 #include <securec.h>
19 #include <iostream>
20 #include <random>
21 
22 #include "accesstoken_kit.h"
23 #include "call_manager_errors.h"
24 #include "cellular_call_data_struct.h"
25 #include "cellular_call_interface.h"
26 #include "cellular_call_ipc_interface_code.h"
27 #include "gtest/gtest.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30 #include "token_setproc.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 using namespace Security::AccessToken;
35 using Security::AccessToken::AccessTokenID;
36 
37 inline HapInfoParams testInfoParams = {
38     .bundleName = "tel_cellular_call_cs_gtest",
39     .userID = 1,
40     .instIndex = 0,
41     .appIDDesc = "test",
42 };
43 
44 inline PermissionDef testPermPlaceCallDef = {
45     .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE",
46     .bundleName = "tel_cellular_call_cs_gtest",
47     .grantMode = 1, // SYSTEM_GRANT
48     .label = "label",
49     .labelId = 1,
50     .description = "Test cellular call",
51     .descriptionId = 1,
52     .availableLevel = APL_SYSTEM_BASIC,
53 };
54 
55 inline PermissionStateFull testPlaceCallState = {
56     .grantFlags = { 2 }, // PERMISSION_USER_SET
57     .grantStatus = { PermissionState::PERMISSION_GRANTED },
58     .isGeneral = true,
59     .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE",
60     .resDeviceID = { "local" },
61 };
62 
63 inline HapPolicyParams testPolicyParams = {
64     .apl = APL_SYSTEM_BASIC,
65     .domain = "test.domain",
66     .permList = { testPermPlaceCallDef },
67     .permStateList = { testPlaceCallState },
68 };
69 
70 class AccessToken {
71 public:
AccessToken()72     AccessToken()
73     {
74         currentID_ = GetSelfTokenID();
75         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
76         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
77         SetSelfTokenID(accessID_);
78     }
~AccessToken()79     ~AccessToken()
80     {
81         AccessTokenKit::DeleteToken(accessID_);
82         SetSelfTokenID(currentID_);
83     }
84 
85 private:
86     AccessTokenID currentID_ = 0;
87     AccessTokenID accessID_ = 0;
88 };
89 
90 class CsTest : public testing::Test {
91 public:
92     static void SetUpTestCase();
93     static void TearDownTestCase();
94     void SetUp();
95     void TearDown();
96 
97     int32_t Dial(const sptr<CellularCallInterface> &telephonyService) const;
98     int32_t HangUp(const sptr<CellularCallInterface> &telephonyService) const;
99     int32_t Answer(const sptr<CellularCallInterface> &telephonyService) const;
100     int32_t Reject(const sptr<CellularCallInterface> &telephonyService) const;
101     int32_t HoldCall(const sptr<CellularCallInterface> &telephonyService) const;
102     int32_t UnHoldCall(const sptr<CellularCallInterface> &telephonyService) const;
103     int32_t SwitchCall(const sptr<CellularCallInterface> &telephonyService) const;
104     int32_t IsEmergencyPhoneNumber(const sptr<CellularCallInterface> &telephonyService) const;
105     int32_t CombineConference(const sptr<CellularCallInterface> &telephonyService) const;
106     int32_t SeparateConference(const sptr<CellularCallInterface> &telephonyService) const;
107     int32_t InviteToConference(const sptr<CellularCallInterface> &telephonyService) const;
108     int32_t KickOutFromConference(const sptr<CellularCallInterface> &telephonyService) const;
109     int32_t HangUpAllConnection(const sptr<CellularCallInterface> &telephonyService) const;
110     int32_t UpdateImsCallMode(const sptr<CellularCallInterface> &telephonyService) const;
111     int32_t RegisterCallBack(const sptr<CellularCallInterface> &telephonyService) const;
112     int32_t UnRegisterCallBack(const sptr<CellularCallInterface> &telephonyService) const;
113     int32_t StartDtmf(const sptr<CellularCallInterface> &telephonyService) const;
114     int32_t StopDtmf(const sptr<CellularCallInterface> &telephonyService) const;
115     int32_t SendDtmf(const sptr<CellularCallInterface> &telephonyService) const;
116     int32_t StartRtt(const sptr<CellularCallInterface> &telephonyService) const;
117     int32_t StopRtt(const sptr<CellularCallInterface> &telephonyService) const;
118     int32_t SetCallTransferInfo(const sptr<CellularCallInterface> &telephonyService) const;
119     int32_t GetCallTransferInfo(const sptr<CellularCallInterface> &telephonyService) const;
120     int32_t SetCallWaiting(const sptr<CellularCallInterface> &telephonyService) const;
121     int32_t GetCallWaiting(const sptr<CellularCallInterface> &telephonyService) const;
122     int32_t SetCallRestriction(const sptr<CellularCallInterface> &telephonyService) const;
123     int32_t GetCallRestriction(const sptr<CellularCallInterface> &telephonyService) const;
124     int32_t SetCallRestrictionPassword(const sptr<CellularCallInterface> &telephonyService) const;
125     int32_t SetMute(const sptr<CellularCallInterface> &telephonyService) const;
126     int32_t GetMute(const sptr<CellularCallInterface> &telephonyService) const;
127     int32_t InputNumForInterface(const sptr<CellularCallInterface> &telephonyService) const;
128     void JudgeIsEmergencyPhoneNumber();
129     bool HasSimCard(int32_t slotId);
InitCellularCallInfo(int32_t accountId, std::string phonenumber, CellularCallInfo &callInfo)130     int32_t InitCellularCallInfo(int32_t accountId, std::string phonenumber, CellularCallInfo &callInfo)
131     {
132         callInfo.accountId = accountId;
133         callInfo.slotId = accountId;
134         callInfo.index = accountId;
135         callInfo.callType = CallType::TYPE_CS;
136         callInfo.videoState = 0; // 0 means audio
137         if (memset_s(callInfo.phoneNum, kMaxNumberLen, 0, kMaxNumberLen) != EOK) {
138             return TELEPHONY_ERR_MEMSET_FAIL;
139         }
140         if (phonenumber.length() > static_cast<size_t>(kMaxNumberLen)) {
141             return CALL_ERR_NUMBER_OUT_OF_RANGE;
142         }
143         if (memcpy_s(callInfo.phoneNum, kMaxNumberLen, phonenumber.c_str(), phonenumber.length()) != EOK) {
144             return TELEPHONY_ERR_MEMCPY_FAIL;
145         }
146         return TELEPHONY_SUCCESS;
147     };
148 
TestDialCallByCs(int32_t slotId, std::string code)149     int32_t TestDialCallByCs(int32_t slotId, std::string code)
150     {
151         AccessToken token;
152         auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
153         if (systemAbilityMgr == nullptr) {
154             return TELEPHONY_ERR_FAIL;
155         }
156         auto remote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
157         if (remote == nullptr) {
158             return TELEPHONY_ERR_FAIL;
159         }
160         auto telephonyService = iface_cast<CellularCallInterface>(remote);
161         if (telephonyService == nullptr) {
162             return TELEPHONY_ERR_FAIL;
163         }
164         CellularCallInfo callInfo;
165         int32_t ret = TELEPHONY_SUCCESS;
166         ret = InitCellularCallInfo(slotId, code, callInfo);
167         if (ret != TELEPHONY_SUCCESS) {
168             return ret;
169         }
170         // close ims, make this time use cs to test
171         ret = telephonyService->SetImsSwitchStatus(slotId, false);
172         if (ret != TELEPHONY_SUCCESS) {
173             return ret;
174         }
175         ret = telephonyService->Dial(callInfo);
176         return ret;
177     };
178 
179 private:
180     int32_t InitCallInfo(CellularCallInfo &callInfo) const;
181 
182 private:
183     using RequestFuncType = int32_t (CsTest::*)(const sptr<CellularCallInterface> &telephonyService) const;
184     std::map<int32_t, RequestFuncType> requestFuncMap_;
185 };
186 } // namespace Telephony
187 } // namespace OHOS
188 #endif // TELEPHONY_CS_TEST_H
189