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