1 /*
2 * Copyright (C) 2024 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 #define private public
17 #define protected public
18
19 #include "esim_mock_iremote_object.h"
20 #include "esim_service_client.h"
21 #include "esim_state_type.h"
22 #include "if_system_ability_manager_mock.h"
23 #include "iservice_registry.h"
24 #include "telephony_errors.h"
25 #include "telephony_log_wrapper.h"
26
27 #include "gtest/gtest.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace Telephony {
34 constexpr int32_t SLOT_ID = 0;
35 class EsimServiceClientBranchTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41
42 static inline std::shared_ptr<ISystemAbilityManagerMock> samgr = std::make_shared<ISystemAbilityManagerMock>();
43 };
44
SetUpTestCase()45 void EsimServiceClientBranchTest::SetUpTestCase()
46 {
47 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = sptr<ISystemAbilityManager>(samgr.get());
48 }
49
TearDownTestCase()50 void EsimServiceClientBranchTest::TearDownTestCase()
51 {
52 samgr = nullptr;
53 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = nullptr;
54 }
55
SetUp()56 void EsimServiceClientBranchTest::SetUp() {}
57
TearDown()58 void EsimServiceClientBranchTest::TearDown() {}
59
60 HWTEST_F(EsimServiceClientBranchTest, GetEid_0001, Function | MediumTest | Level1)
61 {
62 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
63 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
64 sptr<IEsimServiceCallback> callback = nullptr;
65 int32_t result = EsimServiceClient::GetInstance().GetEid(SLOT_ID, callback);
66 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
67 }
68
69 HWTEST_F(EsimServiceClientBranchTest, GetOsuStatus_0001, Function | MediumTest | Level1)
70 {
71 int32_t osuStatus;
72 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
73 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
74 int32_t result = EsimServiceClient::GetInstance().GetOsuStatus(SLOT_ID, osuStatus);
75 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
76 }
77
78 HWTEST_F(EsimServiceClientBranchTest, StartOsu_0001, Function | MediumTest | Level1)
79 {
80 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
81 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
82 sptr<IEsimServiceCallback> callback = nullptr;
83 int32_t result = EsimServiceClient::GetInstance().StartOsu(SLOT_ID, callback);
84 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
85 }
86
87 HWTEST_F(EsimServiceClientBranchTest, GetDownloadableProfileMetadata_0001, Function | MediumTest | Level1)
88 {
89 int32_t portIndex = 0;
90 DownloadableProfile profile;
91 bool forceDisableProfile = true;
92
93 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
94 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
95 sptr<IEsimServiceCallback> callback = nullptr;
96 int32_t result = EsimServiceClient::GetInstance().GetDownloadableProfileMetadata(
97 SLOT_ID, portIndex, profile, forceDisableProfile, callback);
98 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
99 }
100
101 HWTEST_F(EsimServiceClientBranchTest, GetDownloadableProfiles_0001, Function | MediumTest | Level1)
102 {
103 bool forceDisableProfile = false;
104 int32_t portIndex = 0;
105
106 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
107 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
108 sptr<IEsimServiceCallback> callback = nullptr;
109 int32_t result = EsimServiceClient::GetInstance().GetDownloadableProfiles(
110 SLOT_ID, portIndex, forceDisableProfile, callback);
111 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
112 }
113
114 HWTEST_F(EsimServiceClientBranchTest, DownloadProfile_0001, Function | MediumTest | Level1)
115 {
116 DownloadProfileConfigInfo configInfo;
117 DownloadableProfile profile;
118
119 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
120 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
121 sptr<IEsimServiceCallback> callback = nullptr;
122 int32_t result = EsimServiceClient::GetInstance().DownloadProfile(
123 SLOT_ID, configInfo, profile, callback);
124 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
125 }
126
127 HWTEST_F(EsimServiceClientBranchTest, GetEuiccProfileInfoList_0001, Function | MediumTest | Level1)
128 {
129 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
130 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
131 sptr<IEsimServiceCallback> callback = nullptr;
132 int32_t result = EsimServiceClient::GetInstance().GetEuiccProfileInfoList(SLOT_ID, callback);
133 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
134 }
135
136 HWTEST_F(EsimServiceClientBranchTest, GetEuiccInfo_0001, Function | MediumTest | Level1)
137 {
138 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
139 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
140 sptr<IEsimServiceCallback> callback = nullptr;
141 int32_t result = EsimServiceClient::GetInstance().GetEuiccInfo(SLOT_ID, callback);
142 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
143 }
144
145 HWTEST_F(EsimServiceClientBranchTest, DeleteProfile_0001, Function | MediumTest | Level1)
146 {
147 std::string iccId = "98760000000000543210";
148
149 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
150 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
151 sptr<IEsimServiceCallback> callback = nullptr;
152 int32_t result = EsimServiceClient::GetInstance().DeleteProfile(SLOT_ID, iccId, callback);
153 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
154 }
155
156 HWTEST_F(EsimServiceClientBranchTest, SwitchToProfile_0001, Function | MediumTest | Level1)
157 {
158 int32_t portIndex = 0;
159 std::string iccId = "98760000000000543210";
160 bool forceDisableProfile = true;
161
162 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
163 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
164 sptr<IEsimServiceCallback> callback = nullptr;
165 int32_t result = EsimServiceClient::GetInstance().SwitchToProfile(
166 SLOT_ID, portIndex, iccId, forceDisableProfile, callback);
167 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
168 }
169
170 HWTEST_F(EsimServiceClientBranchTest, SetProfileNickname_0001, Function | MediumTest | Level1)
171 {
172 std::string iccId = "98760000000000543210";
173 std::string nickname = "nick";
174
175 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
176 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
177 sptr<IEsimServiceCallback> callback = nullptr;
178 int32_t result = EsimServiceClient::GetInstance().SetProfileNickname(
179 SLOT_ID, iccId, nickname, callback);
180 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
181 }
182
183 HWTEST_F(EsimServiceClientBranchTest, ResetMemory_0001, Function | MediumTest | Level1)
184 {
185 int32_t resetOption = static_cast<int32_t>(ResetOption::DELETE_FIELD_LOADED_TEST_PROFILES);
186
187 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
188 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
189 sptr<IEsimServiceCallback> callback = nullptr;
190 int32_t result = EsimServiceClient::GetInstance().ResetMemory(SLOT_ID, resetOption, callback);
191 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
192 }
193
194 HWTEST_F(EsimServiceClientBranchTest, ReserveProfilesForFactoryRestore_0001, Function | MediumTest | Level1)
195 {
196 int32_t reserveProfilesForFactoryRestoreResult;
197 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
198 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
199 int32_t result = EsimServiceClient::GetInstance().ReserveProfilesForFactoryRestore(
200 SLOT_ID, reserveProfilesForFactoryRestoreResult);
201 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
202 }
203
204 HWTEST_F(EsimServiceClientBranchTest, SetDefaultSmdpAddress_0001, Function | MediumTest | Level1)
205 {
206 std::string defaultSmdpAddress = "smdp.gsma.com";
207
208 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
209 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
210 sptr<IEsimServiceCallback> callback = nullptr;
211 int32_t result = EsimServiceClient::GetInstance().SetDefaultSmdpAddress(
212 SLOT_ID, defaultSmdpAddress, callback);
213 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
214 }
215
216 HWTEST_F(EsimServiceClientBranchTest, GetDefaultSmdpAddress_0001, Function | MediumTest | Level1)
217 {
218 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
219 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
220 sptr<IEsimServiceCallback> callback = nullptr;
221 int32_t result = EsimServiceClient::GetInstance().GetDefaultSmdpAddress(SLOT_ID, callback);
222 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
223 }
224
225 HWTEST_F(EsimServiceClientBranchTest, CancelSession_0001, Function | MediumTest | Level1)
226 {
227 std::string transactionId = "A1B2C3";
228 const int32_t cancelReason = static_cast<int32_t>(CancelReason::CANCEL_REASON_POSTPONED);
229
230 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
231 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
232 sptr<IEsimServiceCallback> callback = nullptr;
233 int32_t result = EsimServiceClient::GetInstance().CancelSession(
234 SLOT_ID, transactionId, cancelReason, callback);
235 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
236 }
237
238 HWTEST_F(EsimServiceClientBranchTest, IsSupported_0001, Function | MediumTest | Level1)
239 {
240 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
241 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
242 int32_t result = EsimServiceClient::GetInstance().IsSupported(SLOT_ID);
243 EXPECT_NE(result, 0);
244 }
245
246 HWTEST_F(EsimServiceClientBranchTest, AddProfile_0001, Function | MediumTest | Level1)
247 {
248 DownloadableProfile profile;
249 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
250 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
251 int32_t result = EsimServiceClient::GetInstance().AddProfile(SLOT_ID, profile);
252 EXPECT_NE(result, false);
253 }
254
255 HWTEST_F(EsimServiceClientBranchTest, GetSupportedPkids_0001, Function | MediumTest | Level1)
256 {
257 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
258 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
259 sptr<IEsimServiceCallback> callback = nullptr;
260 int32_t result = EsimServiceClient::GetInstance().GetSupportedPkids(SLOT_ID, callback);
261 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
262 }
263
264 HWTEST_F(EsimServiceClientBranchTest, GetContractInfo_0001, Function | MediumTest | Level1)
265 {
266 ContractRequestData requestData;
267 EXPECT_CALL(*samgr, LoadSystemAbility(testing::_,
268 testing::A<const sptr<ISystemAbilityLoadCallback>&>())).WillOnce(testing::Return(-1));
269 sptr<IEsimServiceCallback> callback = nullptr;
270 int32_t result = EsimServiceClient::GetInstance().GetContractInfo(SLOT_ID, requestData, callback);
271 EXPECT_EQ(result, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL);
272 }
273
274 HWTEST_F(EsimServiceClientBranchTest, RemoveDeathRecipient_0001, Function | MediumTest | Level1)
275 {
276 sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
277 bool isRemoteDied = true;
278 EsimServiceClient::GetInstance().RemoveDeathRecipient(remote, isRemoteDied);
279 EXPECT_NE(remote, nullptr);
280 }
281
282 HWTEST_F(EsimServiceClientBranchTest, RemoveDeathRecipient_0002, Function | MediumTest | Level1)
283 {
284 sptr<MockIRemoteObject> remote = nullptr;
285 bool isRemoteDied = true;
286 EsimServiceClient::GetInstance().RemoveDeathRecipient(remote, isRemoteDied);
287 EXPECT_EQ(remote, nullptr);
288
289 remote = new (std::nothrow) MockIRemoteObject();
290 isRemoteDied = false;
291 EsimServiceClient::GetInstance().RemoveDeathRecipient(remote, isRemoteDied);
292 EXPECT_NE(remote, nullptr);
293 }
294
295 HWTEST_F(EsimServiceClientBranchTest, OnLoadSystemAbilitySuccess_0001, Function | MediumTest | Level1)
296 {
297 sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
298 EXPECT_NE(remote, nullptr);
299 int32_t systemAbilityId = 66250;
300 EsimServiceClientCallback call;
301 call.OnLoadSystemAbilitySuccess(systemAbilityId, remote);
302 EXPECT_NE(call.remoteObject_, nullptr);
303 }
304
305 HWTEST_F(EsimServiceClientBranchTest, OnLoadSystemAbilityFail_0001, Function | MediumTest | Level1)
306 {
307 int32_t systemAbilityId = 66250;
308 EsimServiceClientCallback call;
309 call.OnLoadSystemAbilityFail(systemAbilityId);
310 EXPECT_TRUE(call.isLoadSAFailed_);
311 }
312
313 HWTEST_F(EsimServiceClientBranchTest, IsFailed_0001, Function | MediumTest | Level1)
314 {
315 EsimServiceClientCallback call;
316 call.isLoadSAFailed_ = true;
317 call.IsFailed();
318 EXPECT_TRUE(call.isLoadSAFailed_);
319 }
320
321 HWTEST_F(EsimServiceClientBranchTest, GetRemoteObject_0001, Function | MediumTest | Level1)
322 {
323 EsimServiceClientCallback call;
324 call.remoteObject_ = new (std::nothrow) MockIRemoteObject();
325 call.GetRemoteObject();
326 EXPECT_NE(call.remoteObject_, nullptr);
327 }
328
329 HWTEST_F(EsimServiceClientBranchTest, OnRemoteDied_0001, Function | MediumTest | Level1)
330 {
331 sptr<MockIRemoteObject> remote = new (std::nothrow) MockIRemoteObject();
332 EXPECT_NE(remote, nullptr);
333 EsimServiceClient::GetInstance().proxy_ = nullptr;
334 EsimServiceClient::GetInstance().OnRemoteDied(remote);
335 EXPECT_EQ(EsimServiceClient::GetInstance().proxy_, nullptr);
336 }
337
338 } // namespace Telephony
339 } // namespace OHOS