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_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 "cellular_call_ipc_interface_code.h"
24#include "core_manager_inner.h"
25#include "core_service_client.h"
26#include "gtest/gtest.h"
27#include "ims_core_service_client.h"
28#include "ims_core_service_proxy.h"
29#include "iservice_registry.h"
30#include "surface_utils.h"
31#include "system_ability_definition.h"
32#include "telephony_log_wrapper.h"
33#include "telephony_permission.h"
34#include "token_setproc.h"
35#include "ims_video_call_control.h"
36
37namespace OHOS {
38namespace Telephony {
39using namespace Security::AccessToken;
40using Security::AccessToken::AccessTokenID;
41
42inline HapInfoParams testInfoParams = {
43    .bundleName = "tel_cellular_call_ims_gtest",
44    .userID = 1,
45    .instIndex = 0,
46    .appIDDesc = "test",
47    .isSystemApp = true,
48};
49
50inline PermissionDef testConnectImsServiceDef = {
51    .permissionName = "ohos.permission.CONNECT_IMS_SERVICE",
52    .bundleName = "tel_cellular_call_ims_gtest",
53    .grantMode = 1, // SYSTEM_GRANT
54    .label = "label",
55    .labelId = 1,
56    .description = "Test cellular call",
57    .descriptionId = 1,
58    .availableLevel = APL_SYSTEM_BASIC,
59};
60
61inline PermissionStateFull testConnectImsServiceState = {
62    .grantFlags = { 2 }, // PERMISSION_USER_SET
63    .grantStatus = { PermissionState::PERMISSION_GRANTED },
64    .isGeneral = true,
65    .permissionName = "ohos.permission.CONNECT_IMS_SERVICE",
66    .resDeviceID = { "local" },
67};
68
69inline PermissionDef testPermPlaceCallDef = {
70    .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE",
71    .bundleName = "tel_cellular_call_ims_gtest",
72    .grantMode = 1, // SYSTEM_GRANT
73    .label = "label",
74    .labelId = 1,
75    .description = "Test cellular call",
76    .descriptionId = 1,
77    .availableLevel = APL_SYSTEM_BASIC,
78};
79
80inline PermissionStateFull testPlaceCallState = {
81    .grantFlags = { 2 }, // PERMISSION_USER_SET
82    .grantStatus = { PermissionState::PERMISSION_GRANTED },
83    .isGeneral = true,
84    .permissionName = "ohos.permission.CONNECT_CELLULAR_CALL_SERVICE",
85    .resDeviceID = { "local" },
86};
87
88inline PermissionDef testGetTelephonyStateDef = {
89    .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
90    .bundleName = "tel_cellular_call_ims_gtest",
91    .grantMode = 1, // SYSTEM_GRANT
92    .label = "label",
93    .labelId = 1,
94    .description = "Test cellular call",
95    .descriptionId = 1,
96    .availableLevel = APL_SYSTEM_BASIC,
97};
98
99inline PermissionStateFull testGetTelephonyState = {
100    .grantFlags = { 2 }, // PERMISSION_USER_SET
101    .grantStatus = { PermissionState::PERMISSION_GRANTED },
102    .isGeneral = true,
103    .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
104    .resDeviceID = { "local" },
105};
106
107inline HapPolicyParams testPolicyParams = {
108    .apl = APL_SYSTEM_BASIC,
109    .domain = "test.domain",
110    .permList = { testPermPlaceCallDef, testConnectImsServiceDef, testGetTelephonyStateDef },
111    .permStateList = { testPlaceCallState, testConnectImsServiceState, testGetTelephonyState },
112};
113
114class AccessToken {
115public:
116    AccessToken()
117    {
118        currentID_ = GetSelfTokenID();
119        AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParams, testPolicyParams);
120        accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
121        SetSelfTokenID(tokenIdEx.tokenIDEx);
122    }
123    ~AccessToken()
124    {
125        AccessTokenKit::DeleteToken(accessID_);
126        SetSelfTokenID(currentID_);
127    }
128
129private:
130    AccessTokenID currentID_ = 0;
131    AccessTokenID accessID_ = 0;
132};
133
134class ImsTest : public testing::Test {
135public:
136    static void SetUpTestCase();
137    static void TearDownTestCase();
138    void SetUp();
139    void TearDown();
140
141    int32_t SetDomainPreferenceMode(const sptr<CellularCallInterface> &telephonyService) const;
142    int32_t GetDomainPreferenceMode(const sptr<CellularCallInterface> &telephonyService) const;
143    int32_t SetImsSwitchStatus(const sptr<CellularCallInterface> &telephonyService) const;
144    int32_t GetImsSwitchStatus(const sptr<CellularCallInterface> &telephonyService) const;
145    int32_t SetImsConfigString(const sptr<CellularCallInterface> &telephonyService) const;
146    int32_t SetImsConfigInt(const sptr<CellularCallInterface> &telephonyService) const;
147    int32_t GetImsConfig(const sptr<CellularCallInterface> &telephonyService) const;
148    int32_t SetImsFeatureValue(const sptr<CellularCallInterface> &telephonyService) const;
149    int32_t GetImsFeatureValue(const sptr<CellularCallInterface> &telephonyService) const;
150    int32_t ControlCamera(const sptr<CellularCallInterface> &telephonyService) const;
151    int32_t SetPreviewWindow(const sptr<CellularCallInterface> &telephonyService) const;
152    int32_t SetDisplayWindow(const sptr<CellularCallInterface> &telephonyService) const;
153    int32_t SetCameraZoom(const sptr<CellularCallInterface> &telephonyService) const;
154    int32_t SetPausePicture(const sptr<CellularCallInterface> &telephonyService) const;
155    int32_t SetDeviceDirection(const sptr<CellularCallInterface> &telephonyService) const;
156    int32_t SendUpdateCallMediaModeRequest(const sptr<CellularCallInterface> &telephonyService) const;
157    int32_t SendUpdateCallMediaModeResponse(const sptr<CellularCallInterface> &telephonyService) const;
158    int32_t CancelCallUpgrade(const sptr<CellularCallInterface> &telephonyService) const;
159    int32_t RequestCameraCapabilities(const sptr<CellularCallInterface> &telephonyService) const;
160    int32_t InputNumForInterface(const sptr<CellularCallInterface> &telephonyService) const;
161
162    bool HasSimCard(int32_t slotId)
163    {
164        bool hasSimCard = false;
165        DelayedRefSingleton<CoreServiceClient>::GetInstance().HasSimCard(slotId, hasSimCard);
166        return hasSimCard;
167    }
168    bool CanUseImsService(int32_t slotId, ImsServiceType type)
169    {
170        ImsRegInfo info;
171        CoreServiceClient::GetInstance().GetImsRegStatus(slotId, type, info);
172        bool imsReg = info.imsRegState == ImsRegState::IMS_REGISTERED;
173        return imsReg;
174    }
175
176    int32_t InitCellularCallInfo(int32_t accountId, std::string phonenumber, CellularCallInfo &callInfo)
177    {
178        callInfo.accountId = accountId;
179        callInfo.slotId = accountId;
180        callInfo.index = accountId;
181        callInfo.callType = CallType::TYPE_IMS;
182        callInfo.videoState = 0; // 0 means audio
183        if (memset_s(callInfo.phoneNum, kMaxNumberLen, 0, kMaxNumberLen) != EOK) {
184            return TELEPHONY_ERR_MEMSET_FAIL;
185        }
186        if (phonenumber.length() > static_cast<size_t>(kMaxNumberLen)) {
187            return CALL_ERR_NUMBER_OUT_OF_RANGE;
188        }
189        if (memcpy_s(callInfo.phoneNum, kMaxNumberLen, phonenumber.c_str(), phonenumber.length()) != EOK) {
190            return TELEPHONY_ERR_MEMCPY_FAIL;
191        }
192        return TELEPHONY_SUCCESS;
193    };
194
195    int32_t TestDialCallByIms(int32_t slotId, std::string code)
196    {
197        AccessToken token;
198        auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
199        if (saMgr == nullptr) {
200            return TELEPHONY_ERR_FAIL;
201        }
202        auto remote = saMgr->CheckSystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID);
203        if (remote == nullptr) {
204            return TELEPHONY_ERR_FAIL;
205        }
206        auto telephonyService = iface_cast<CellularCallInterface>(remote);
207        if (telephonyService == nullptr) {
208            return TELEPHONY_ERR_FAIL;
209        }
210        CellularCallInfo imsCellularCallInfo;
211        int32_t ret = TELEPHONY_SUCCESS;
212        ret = InitCellularCallInfo(slotId, code, imsCellularCallInfo);
213        if (ret != TELEPHONY_SUCCESS) {
214            return ret;
215        }
216        // open ims, make this time use ims to test
217        ret = telephonyService->SetImsSwitchStatus(slotId, true);
218        if (ret != TELEPHONY_SUCCESS) {
219            return ret;
220        }
221        ret = telephonyService->Dial(imsCellularCallInfo);
222        return ret;
223    };
224
225    int32_t WriteSsBaseResult(MessageParcel &in, const SsBaseResult &ssResult)
226    {
227        if (!in.WriteInt32(ssResult.index)) {
228            return TELEPHONY_ERR_WRITE_DATA_FAIL;
229        }
230        if (!in.WriteInt32(ssResult.result)) {
231            return TELEPHONY_ERR_WRITE_DATA_FAIL;
232        }
233        if (!in.WriteInt32(ssResult.reason)) {
234            return TELEPHONY_ERR_WRITE_DATA_FAIL;
235        }
236        if (!in.WriteString(ssResult.message)) {
237            return TELEPHONY_ERR_WRITE_DATA_FAIL;
238        }
239        return TELEPHONY_SUCCESS;
240    };
241
242    int32_t WriteSsResult(MessageParcel &in, const SsBaseResult &ssResult, const int32_t action, const int32_t state)
243    {
244        int32_t ret = WriteSsBaseResult(in, ssResult);
245        if (ret != TELEPHONY_SUCCESS) {
246            return ret;
247        }
248        if (!in.WriteInt32(action)) {
249            return TELEPHONY_ERR_WRITE_DATA_FAIL;
250        }
251        if (!in.WriteInt32(state)) {
252            return TELEPHONY_ERR_WRITE_DATA_FAIL;
253        }
254        return TELEPHONY_SUCCESS;
255    };
256
257    int32_t WriteCallForwardResult(MessageParcel &in, const CallForwardQueryInfoList &cFQueryList)
258    {
259        int32_t ret = WriteSsBaseResult(in, cFQueryList.result);
260        if (ret != TELEPHONY_SUCCESS) {
261            return ret;
262        }
263        if (!in.WriteInt32(cFQueryList.callSize) || !in.WriteInt32(cFQueryList.flag)) {
264            return TELEPHONY_ERR_WRITE_DATA_FAIL;
265        }
266        if (!in.WriteInt32(static_cast<int32_t>(cFQueryList.calls.size()))) {
267            return TELEPHONY_ERR_WRITE_DATA_FAIL;
268        }
269        for (auto call : cFQueryList.calls) {
270            if (!in.WriteInt32(call.serial) || !in.WriteInt32(call.result) || !in.WriteInt32(call.status) ||
271                !in.WriteInt32(call.classx) || !in.WriteString(call.number) || !in.WriteInt32(call.type) ||
272                !in.WriteInt32(call.reason) || !in.WriteInt32(call.time) || !in.WriteInt32(call.startHour) ||
273                !in.WriteInt32(call.startMinute) || !in.WriteInt32(call.endHour) || !in.WriteInt32(call.endMinute)) {
274                return TELEPHONY_ERR_WRITE_DATA_FAIL;
275            }
276        }
277        return TELEPHONY_SUCCESS;
278    }
279
280private:
281    using RequestFuncType = int32_t (ImsTest::*)(const sptr<CellularCallInterface> &telephonyService) const;
282    std::map<int32_t, RequestFuncType> requestFuncMap_;
283};
284} // namespace Telephony
285} // namespace OHOS
286
287#endif // TELEPHONY_IMS_TEST_H
288