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