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 #include "gtest/gtest.h"
17
18 #define private public
19 #define protected public
20 #include "cellular_call_callback.h"
21 #include "cellular_call_handler.h"
22 #include "cellular_call_proxy.h"
23 #include "cellular_call_register.h"
24 #include "cellular_call_service.h"
25 #include "cellular_call_supplement.h"
26 #include "config_request.h"
27 #include "core_service_client.h"
28 #include "cs_control.h"
29 #include "tel_ril_call_parcel.h"
30 #include "operator_config_types.h"
31 #include "radio_event.h"
32 #include "securec.h"
33 #include "sim_state_type.h"
34 #include "system_ability_definition.h"
35 #include "token.h"
36
37 namespace OHOS {
38 namespace Telephony {
39 using namespace testing::ext;
40 const int32_t SIM1_SLOTID = 0;
41 const int32_t SIM2_SLOTID = 1;
42
43 class Cs1Test : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 int32_t TestDialCallByCs(int32_t slotId, std::string code);
50 void JudgeIsEmergencyPhoneNumber();
HasSimCard(int32_t slotId)51 bool HasSimCard(int32_t slotId)
52 {
53 bool hasSimCard = false;
54 DelayedRefSingleton<CoreServiceClient>::GetInstance().HasSimCard(slotId, hasSimCard);
55 return hasSimCard;
56 }
57 int32_t InitCellularCallInfo(int32_t accountId, std::string phonenumber, CellularCallInfo &callInfo);
58 };
59
TestDialCallByCs(int32_t slotId,std::string code)60 int32_t Cs1Test::TestDialCallByCs(int32_t slotId, std::string code)
61 {
62 AccessToken token;
63 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (systemAbilityMgr == nullptr) {
65 return TELEPHONY_ERR_FAIL;
66 }
67 auto remote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
68 if (remote == nullptr) {
69 return TELEPHONY_ERR_FAIL;
70 }
71 auto telephonyService = iface_cast<CellularCallInterface>(remote);
72 if (telephonyService == nullptr) {
73 return TELEPHONY_ERR_FAIL;
74 }
75 CellularCallInfo callInfo;
76 int32_t ret = TELEPHONY_SUCCESS;
77 ret = InitCellularCallInfo(slotId, code, callInfo);
78 if (ret != TELEPHONY_SUCCESS) {
79 return ret;
80 }
81 // close ims, make this time use cs to test
82 ret = telephonyService->SetImsSwitchStatus(slotId, false);
83 if (ret != TELEPHONY_SUCCESS) {
84 return ret;
85 }
86 ret = telephonyService->Dial(callInfo);
87 return ret;
88 };
89
InitCellularCallInfo(int32_t accountId,std::string phonenumber,CellularCallInfo & callInfo)90 int32_t Cs1Test::InitCellularCallInfo(int32_t accountId, std::string phonenumber,
91 CellularCallInfo &callInfo)
92 {
93 callInfo.accountId = accountId;
94 callInfo.slotId = accountId;
95 callInfo.index = accountId;
96 callInfo.callType = CallType::TYPE_CS;
97 callInfo.videoState = 0; // 0 means audio
98 if (memset_s(callInfo.phoneNum, kMaxNumberLen, 0, kMaxNumberLen) != EOK) {
99 return TELEPHONY_ERR_MEMSET_FAIL;
100 }
101 if (phonenumber.length() > static_cast<size_t>(kMaxNumberLen)) {
102 return CALL_ERR_NUMBER_OUT_OF_RANGE;
103 }
104 if (memcpy_s(callInfo.phoneNum, kMaxNumberLen, phonenumber.c_str(), phonenumber.length()) != EOK) {
105 return TELEPHONY_ERR_MEMCPY_FAIL;
106 }
107 return TELEPHONY_SUCCESS;
108 };
109
JudgeIsEmergencyPhoneNumber()110 void Cs1Test::JudgeIsEmergencyPhoneNumber()
111 {
112 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
113 ASSERT_TRUE(systemAbilityMgr != nullptr);
114 auto remote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
115 ASSERT_TRUE(remote != nullptr);
116 auto telephonyService = iface_cast<CellularCallInterface>(remote);
117 ASSERT_TRUE(telephonyService != nullptr);
118
119 if (!HasSimCard(SIM1_SLOTID) && !HasSimCard(SIM2_SLOTID)) {
120 return;
121 }
122 bool enabled = false;
123 int32_t successCode = 1;
124 if (HasSimCard(SIM1_SLOTID)) {
125 telephonyService->IsEmergencyPhoneNumber(SIM1_SLOTID, "499", enabled);
126 EXPECT_NE(enabled, successCode);
127 telephonyService->IsEmergencyPhoneNumber(SIM1_SLOTID, "443", enabled);
128 EXPECT_NE(enabled, successCode);
129 telephonyService->IsEmergencyPhoneNumber(SIM1_SLOTID, "356", enabled);
130 EXPECT_NE(enabled, successCode);
131 telephonyService->IsEmergencyPhoneNumber(SIM1_SLOTID, "975", enabled);
132 EXPECT_NE(enabled, successCode);
133 telephonyService->IsEmergencyPhoneNumber(SIM1_SLOTID, "783", enabled);
134 EXPECT_NE(enabled, successCode);
135 telephonyService->IsEmergencyPhoneNumber(SIM1_SLOTID, "350", enabled);
136 EXPECT_NE(enabled, successCode);
137 }
138 if (HasSimCard(SIM2_SLOTID)) {
139 telephonyService->IsEmergencyPhoneNumber(SIM2_SLOTID, "499", enabled);
140 EXPECT_NE(enabled, successCode);
141 telephonyService->IsEmergencyPhoneNumber(SIM2_SLOTID, "443", enabled);
142 EXPECT_NE(enabled, successCode);
143 telephonyService->IsEmergencyPhoneNumber(SIM2_SLOTID, "356", enabled);
144 EXPECT_NE(enabled, successCode);
145 telephonyService->IsEmergencyPhoneNumber(SIM2_SLOTID, "975", enabled);
146 EXPECT_NE(enabled, successCode);
147 telephonyService->IsEmergencyPhoneNumber(SIM2_SLOTID, "783", enabled);
148 EXPECT_NE(enabled, successCode);
149 telephonyService->IsEmergencyPhoneNumber(SIM2_SLOTID, "350", enabled);
150 EXPECT_NE(enabled, successCode);
151 }
152 }
153
SetUpTestCase(void)154 void Cs1Test::SetUpTestCase(void)
155 {
156 // step 3: Set Up Test Case
157 }
158
TearDownTestCase(void)159 void Cs1Test::TearDownTestCase(void)
160 {
161 // step 3: Tear Down Test Case
162 }
163
SetUp(void)164 void Cs1Test::SetUp(void)
165 {
166 // step 3: input testcase setup step
167 }
168
TearDown(void)169 void Cs1Test::TearDown(void)
170 {
171 // step 3: input testcase teardown step
172 }
173
174 /**
175 * @tc.number cellular_call_cs_test_001
176 * @tc.name Test the corresponding functions by entering commands, such as 1 -- Dial, 2 -- Hangup, 3 -- Reject, etc
177 * @tc.desc Function test
178 */
179 HWTEST_F(Cs1Test, cellular_call_cs_test_001, Function | MediumTest | Level0)
180 {
181 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
182 ASSERT_TRUE(systemAbilityMgr != nullptr);
183 auto remote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
184 ASSERT_TRUE(remote != nullptr);
185 auto telephonyService = iface_cast<CellularCallInterface>(remote);
186 ASSERT_TRUE(telephonyService != nullptr);
187 std::cout << "HWTEST_F cellular_call_cs_test_001";
188 }
189
190 /**
191 * @tc.number cellular_call_cs_test_002
192 * @tc.name Test the corresponding functions by entering commands, such as 1 -- Dial, 2 -- Hangup, 3 -- Reject, etc
193 * @tc.desc Function test
194 */
195 HWTEST_F(Cs1Test, cellular_call_cs_test_002, Function | MediumTest | Level1)
196 {
197 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
198 ASSERT_TRUE(systemAbilityMgr != nullptr);
199 auto remote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
200 ASSERT_TRUE(remote != nullptr);
201 auto telephonyService = iface_cast<CellularCallInterface>(remote);
202 ASSERT_TRUE(telephonyService != nullptr);
203 std::cout << "HWTEST_F cellular_call_cs_test_002";
204 }
205
206 /**
207 * @tc.number Telephony_CallManager_SetEmergencyCallList_0101
208 * @tc.name SetEmergencyCallList "443,356,789",test SetEmergencyCallList(),return success
209 * @tc.desc Function test
210 */
211 HWTEST_F(Cs1Test, cellular_call_SetEmergencyCallList_0101, Function | MediumTest | Level3)
212 {
213 AccessToken token;
214 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
215 ASSERT_TRUE(systemAbilityMgr != nullptr);
216 auto emergencyRemote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
217 ASSERT_TRUE(emergencyRemote != nullptr);
218 auto telephonyService = iface_cast<CellularCallInterface>(emergencyRemote);
219 ASSERT_TRUE(telephonyService != nullptr);
220
221 if (!HasSimCard(SIM1_SLOTID) && !HasSimCard(SIM2_SLOTID)) {
222 return;
223 }
224
225 std::vector<EmergencyCall> eccVec;
226 EmergencyCall temp0 = { "499", "460", EccType::TYPE_CATEGORY, SimpresentType::TYPE_NO_CARD,
227 AbnormalServiceType::TYPE_ALL };
228 EmergencyCall temp1 = { "443", "460", EccType::TYPE_POLICE, SimpresentType::TYPE_NO_CARD,
229 AbnormalServiceType::TYPE_ALL };
230 EmergencyCall temp2 = { "356", "460", EccType::TYPE_AMBULANCE, SimpresentType::TYPE_NO_CARD,
231 AbnormalServiceType::TYPE_ALL };
232 EmergencyCall temp3 = { "783", "460", EccType::TYPE_FIRE, SimpresentType::TYPE_NO_CARD,
233 AbnormalServiceType::TYPE_ALL };
234 EmergencyCall temp4 = { "975", "460", EccType::TYPE_SEA, SimpresentType::TYPE_HAS_CARD,
235 AbnormalServiceType::TYPE_ONLY_CS };
236 EmergencyCall temp5 = { "350", "460", EccType::TYPE_MOUNTAIN, SimpresentType::TYPE_HAS_CARD,
237 AbnormalServiceType::TYPE_ALL };
238 eccVec.push_back(temp0);
239 eccVec.push_back(temp1);
240 eccVec.push_back(temp2);
241 eccVec.push_back(temp3);
242 eccVec.push_back(temp4);
243 eccVec.push_back(temp5);
244 if (HasSimCard(SIM2_SLOTID)) {
245 EXPECT_EQ(telephonyService->SetEmergencyCallList(SIM2_SLOTID, eccVec), 0);
246 }
247 JudgeIsEmergencyPhoneNumber();
248 }
249
250 HWTEST_F(Cs1Test, cellular_call_IsOperatorConfigEmergencyCallList_0001, Function | MediumTest | Level3)
251 {
252 AccessToken token;
253 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
254 ASSERT_TRUE(systemAbilityMgr != nullptr);
255 auto operatorRemote = systemAbilityMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
256 ASSERT_TRUE(operatorRemote != nullptr);
257 auto telephonyService = iface_cast<CellularCallInterface>(operatorRemote);
258 ASSERT_TRUE(telephonyService != nullptr);
259
260 if (!HasSimCard(SIM1_SLOTID) && !HasSimCard(SIM2_SLOTID)) {
261 return;
262 }
263 OperatorConfig opc;
264 if (HasSimCard(SIM1_SLOTID)) {
265 DelayedRefSingleton<CoreServiceClient>::GetInstance().GetOperatorConfigs(SIM1_SLOTID, opc);
266 if (opc.stringArrayValue.find(KEY_EMERGENCY_CALL_STRING_ARRAY) != opc.stringArrayValue.end()) {
267 for (auto number : opc.stringArrayValue[KEY_EMERGENCY_CALL_STRING_ARRAY]) {
268 bool enabled = false;
269 int32_t errorCode = telephonyService->IsEmergencyPhoneNumber(SIM1_SLOTID, number, enabled);
270 EXPECT_EQ(TELEPHONY_SUCCESS, errorCode);
271 }
272 }
273 }
274 if (HasSimCard(SIM2_SLOTID)) {
275 DelayedRefSingleton<CoreServiceClient>::GetInstance().GetOperatorConfigs(SIM2_SLOTID, opc);
276 if (opc.stringArrayValue.find(KEY_EMERGENCY_CALL_STRING_ARRAY) != opc.stringArrayValue.end()) {
277 for (auto number : opc.stringArrayValue[KEY_EMERGENCY_CALL_STRING_ARRAY]) {
278 bool enabled = false;
279 int32_t errorCode = telephonyService->IsEmergencyPhoneNumber(SIM2_SLOTID, number, enabled);
280 EXPECT_EQ(TELEPHONY_SUCCESS, errorCode);
281 }
282 }
283 }
284 }
285 } // namespace Telephony
286 } // namespace OHOS
287