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