1 /*
2 * Copyright (C) 2021 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 "gsm_sms_sender_test.h"
17
18 #include <iostream>
19
20 #include "ability_context.h"
21 #include "ability_info.h"
22 #include "accesstoken_kit.h"
23 #include "context_deal.h"
24 #include "data_ability_predicates.h"
25 #include "iservice_registry.h"
26 #include "sms_delivery_callback_test.h"
27 #include "sms_send_callback_test.h"
28 #include "string_utils.h"
29 #include "telephony_errors.h"
30 #include "token_setproc.h"
31 #include "values_bucket.h"
32
33 namespace OHOS {
34 namespace Telephony {
35 using namespace Security::AccessToken;
36 using Security::AccessToken::AccessTokenID;
37
38 HapInfoParams testMmsInfoParams = {
39 .bundleName = "tel_sms_mms_test",
40 .userID = 1,
41 .instIndex = 0,
42 .appIDDesc = "test",
43 };
44
45 PermissionDef testPermReceiveSmsDef = {
46 .permissionName = "ohos.permission.RECEIVE_SMS",
47 .bundleName = "tel_sms_mms_test",
48 .grantMode = 1, // SYSTEM_GRANT
49 .label = "label",
50 .labelId = 1,
51 .description = "Test sms manager",
52 .descriptionId = 1,
53 .availableLevel = APL_SYSTEM_BASIC,
54 };
55
56 PermissionStateFull testReceiveSmsState = {
57 .grantFlags = { 2 }, // PERMISSION_USER_SET
58 .grantStatus = { PermissionState::PERMISSION_GRANTED },
59 .isGeneral = true,
60 .permissionName = "ohos.permission.RECEIVE_SMS",
61 .resDeviceID = { "local" },
62 };
63
64 PermissionDef testPermSendSmsDef = {
65 .permissionName = "ohos.permission.SEND_MESSAGES",
66 .bundleName = "tel_sms_mms_test",
67 .grantMode = 1, // SYSTEM_GRANT
68 .label = "label",
69 .labelId = 1,
70 .description = "Test sms manager",
71 .descriptionId = 1,
72 .availableLevel = APL_SYSTEM_BASIC,
73 };
74
75 PermissionStateFull testSendSmsState = {
76 .grantFlags = { 2 }, // PERMISSION_USER_SET
77 .grantStatus = { PermissionState::PERMISSION_GRANTED },
78 .isGeneral = true,
79 .permissionName = "ohos.permission.SEND_MESSAGES",
80 .resDeviceID = { "local" },
81 };
82
83 PermissionDef testPermSetTelephonyDef = {
84 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
85 .bundleName = "tel_sms_mms_test",
86 .grantMode = 1, // SYSTEM_GRANT
87 .label = "label",
88 .labelId = 1,
89 .description = "Test sms manager",
90 .descriptionId = 1,
91 .availableLevel = APL_SYSTEM_BASIC,
92 };
93
94 PermissionStateFull testSetTelephonyState = {
95 .grantFlags = { 2 }, // PERMISSION_USER_SET
96 .grantStatus = { PermissionState::PERMISSION_GRANTED },
97 .isGeneral = true,
98 .permissionName = "ohos.permission.SET_TELEPHONY_STATE",
99 .resDeviceID = { "local" },
100 };
101
102 PermissionDef testPermGetTelephonyDef = {
103 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
104 .bundleName = "tel_sms_mms_test",
105 .grantMode = 1, // SYSTEM_GRANT
106 .label = "label",
107 .labelId = 1,
108 .description = "Test sms manager",
109 .descriptionId = 1,
110 .availableLevel = APL_SYSTEM_BASIC,
111 };
112
113 PermissionStateFull testGetTelephonyState = {
114 .grantFlags = { 2 }, // PERMISSION_USER_SET
115 .grantStatus = { PermissionState::PERMISSION_GRANTED },
116 .isGeneral = true,
117 .permissionName = "ohos.permission.GET_TELEPHONY_STATE",
118 .resDeviceID = { "local" },
119 };
120
121 HapPolicyParams testMmsPolicyParams = {
122 .apl = APL_SYSTEM_BASIC,
123 .domain = "test.domain",
124 .permList = { testPermReceiveSmsDef, testPermSendSmsDef, testPermSetTelephonyDef, testPermGetTelephonyDef },
125 .permStateList = { testReceiveSmsState, testSendSmsState, testSetTelephonyState, testGetTelephonyState },
126 };
127
128 class AccessMmsToken {
129 public:
AccessMmsToken()130 AccessMmsToken()
131 {
132 currentID_ = GetSelfTokenID();
133 std::cout << "AccessMmsToken currentID_" << currentID_ << std::endl;
134 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testMmsInfoParams, testMmsPolicyParams);
135 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
136 SetSelfTokenID(accessID_);
137 }
~AccessMmsToken()138 ~AccessMmsToken()
139 {
140 std::cout << "AccessMmsToken ~AccessMmsToken" << std::endl;
141 AccessTokenKit::DeleteToken(accessID_);
142 SetSelfTokenID(currentID_);
143 }
144
145 private:
146 AccessTokenID currentID_ = 0;
147 AccessTokenID accessID_ = 0;
148 };
149
TestGsmSendShortData(const sptr<ISmsServiceInterface> & smsService) const150 void GsmSmsSenderTest::TestGsmSendShortData(const sptr<ISmsServiceInterface> &smsService) const
151 {
152 AccessMmsToken token;
153 if (smsService == nullptr) {
154 std::cout << "smsService is nullptr." << std::endl;
155 return;
156 }
157 std::string dest;
158 std::cout << "Please enter the card id" << std::endl;
159 int32_t slotId;
160 std::cin >> dest;
161 slotId = atoi(dest.c_str());
162 dest.clear();
163 std::cout << "Please enter the receiver's telephone number" << std::endl;
164 std::cin >> dest;
165 std::string sca("");
166 OHOS::sptr<SmsSendCallbackTest> sendCallBackPtr(new SmsSendCallbackTest());
167 OHOS::sptr<SmsDeliveryCallbackTest> deliveryCallBackPtr(new SmsDeliveryCallbackTest());
168 uint16_t port = 100;
169 uint8_t data[] = "hello world";
170 smsService->SendMessage(slotId, StringUtils::ToUtf16(dest), StringUtils::ToUtf16(sca), port, data,
171 (sizeof(data) / sizeof(data[0]) - 1), sendCallBackPtr, deliveryCallBackPtr);
172 std::cout << "TestGsmSendShortData" << std::endl;
173 }
174
TestGsmSendLongData(const sptr<ISmsServiceInterface> & smsService) const175 void GsmSmsSenderTest::TestGsmSendLongData(const sptr<ISmsServiceInterface> &smsService) const
176 {
177 AccessMmsToken token;
178 if (smsService == nullptr) {
179 std::cout << "smsService is nullptr." << std::endl;
180 return;
181 }
182 std::string dest;
183 std::cout << "Please enter the card id" << std::endl;
184 int32_t slotId;
185 std::cin >> dest;
186 slotId = atoi(dest.c_str());
187 dest.clear();
188 std::cout << "Please enter the receiver's telephone number" << std::endl;
189 std::cin >> dest;
190 std::string sca("");
191 OHOS::sptr<SmsSendCallbackTest> sendCallBackPtr(new SmsSendCallbackTest());
192 OHOS::sptr<SmsDeliveryCallbackTest> deliveryCallBackPtr(new SmsDeliveryCallbackTest());
193 uint8_t data[] =
194 R"(This is the test data!This is the test data!This is the test data!
195 This is the test data!This is the test data!This is the test data!
196 This is the test data!This is the test data!This is the test data!
197 This is the test data!This is the test data!This is the test data!
198 This is the test data!This is the test data!This is the test data!
199 This is the test data!This is the test data!This is the test data!)";
200 uint16_t port = 100;
201
202 smsService->SendMessage(slotId, StringUtils::ToUtf16(dest), StringUtils::ToUtf16(sca), port, data,
203 sizeof(data) / sizeof(data[0]), sendCallBackPtr, deliveryCallBackPtr);
204 std::cout << "TestGsmSendLongData" << std::endl;
205 }
206
TestSendShortText(const sptr<ISmsServiceInterface> & smsService) const207 void GsmSmsSenderTest::TestSendShortText(const sptr<ISmsServiceInterface> &smsService) const
208 {
209 AccessMmsToken token;
210 if (smsService == nullptr) {
211 std::cout << "smsService is nullptr." << std::endl;
212 return;
213 }
214 std::string dest;
215 std::cout << "Please enter the card id" << std::endl;
216 int32_t slotId;
217 std::cin >> dest;
218 slotId = atoi(dest.c_str());
219 dest.clear();
220 std::string text;
221 std::cin.ignore();
222 std::cout << "Please enter the receiver's telephone number" << std::endl;
223 getline(std::cin, dest);
224 std::cout << "Please enter text" << std::endl;
225 getline(std::cin, text);
226 std::string sca("");
227 OHOS::sptr<SmsSendCallbackTest> sendCallBackPtr(new SmsSendCallbackTest());
228 OHOS::sptr<SmsDeliveryCallbackTest> deliveryCallBackPtr(new SmsDeliveryCallbackTest());
229 smsService->SendMessage(slotId, StringUtils::ToUtf16(dest), StringUtils::ToUtf16(sca),
230 StringUtils::ToUtf16(text), sendCallBackPtr, deliveryCallBackPtr);
231 std::cout << "TestGsmSendShortText" << std::endl;
232 }
233
TestSendLongText(const sptr<ISmsServiceInterface> & smsService) const234 void GsmSmsSenderTest::TestSendLongText(const sptr<ISmsServiceInterface> &smsService) const
235 {
236 AccessMmsToken token;
237 if (smsService == nullptr) {
238 std::cout << "smsService is nullptr." << std::endl;
239 return;
240 }
241 std::string dest;
242 std::cout << "Please enter the card id" << std::endl;
243 int32_t slotId;
244 std::cin >> dest;
245 slotId = atoi(dest.c_str());
246 dest.clear();
247 std::cout << "Please enter the receiver's telephone number" << std::endl;
248 std::cin >> dest;
249 std::string sca("");
250 std::string text(
251 R"(This is the test data!This is the test data!This is the test data!
252 This is the test data!This is the test data!This is the test data!
253 This is the test data!This is the test data!This is the test data!
254 This is the test data!This is the test data!This is the test data!
255 This is the test data!This is the test data!This is the test data!
256 This is the test data!This is the test data!This is the test data!)");
257 OHOS::sptr<SmsSendCallbackTest> sendCallBackPtr(new SmsSendCallbackTest());
258 OHOS::sptr<SmsDeliveryCallbackTest> deliveryCallBackPtr(new SmsDeliveryCallbackTest());
259 smsService->SendMessage(slotId, StringUtils::ToUtf16(dest), StringUtils::ToUtf16(sca),
260 StringUtils::ToUtf16(text), sendCallBackPtr, deliveryCallBackPtr);
261 std::cout << "TestGsmSendLongText" << std::endl;
262 }
263
TestSetSmscAddr(const sptr<ISmsServiceInterface> & smsService) const264 void GsmSmsSenderTest::TestSetSmscAddr(const sptr<ISmsServiceInterface> &smsService) const
265 {
266 AccessMmsToken token;
267 bool result = false;
268 if (smsService == nullptr) {
269 std::cout << "smsService is nullptr." << std::endl;
270 return;
271 }
272 std::string dest;
273 std::cout << "Please enter the card id" << std::endl;
274 int32_t slotId;
275 std::cin >> dest;
276 slotId = atoi(dest.c_str());
277 dest.clear();
278 std::string sca;
279 std::cout << "Please enter smsc" << std::endl;
280 std::cin >> sca;
281 result = smsService->SetSmscAddr(slotId, StringUtils::ToUtf16(sca));
282 if (result) {
283 std::cout << "TestSetSmscAddr OK!" << std::endl;
284 } else {
285 std::cout << "TestSetSmscAddr failed!" << std::endl;
286 }
287 }
288
TestGetSmscAddr(const sptr<ISmsServiceInterface> & smsService) const289 void GsmSmsSenderTest::TestGetSmscAddr(const sptr<ISmsServiceInterface> &smsService) const
290 {
291 AccessMmsToken token;
292 std::u16string result;
293 if (smsService == nullptr) {
294 std::cout << "smsService is nullptr." << std::endl;
295 return;
296 }
297 std::string dest;
298 std::cout << "Please enter the card id" << std::endl;
299 int32_t slotId;
300 std::cin >> dest;
301 slotId = atoi(dest.c_str());
302 dest.clear();
303 smsService->GetSmscAddr(slotId, result);
304 std::cout << "TestGetSmscAddr:" << StringUtils::ToUtf8(result) << std::endl;
305 }
306
TestAddSimMessage(const sptr<ISmsServiceInterface> & smsService) const307 void GsmSmsSenderTest::TestAddSimMessage(const sptr<ISmsServiceInterface> &smsService) const
308 {
309 AccessMmsToken token;
310 bool result = false;
311 if (smsService == nullptr) {
312 std::cout << "smsService is nullptr." << std::endl;
313 return;
314 }
315 std::string dest;
316 std::cout << "Please enter the card id" << std::endl;
317 int32_t slotId;
318 std::cin >> dest;
319 slotId = atoi(dest.c_str());
320 dest.clear();
321 std::string smsc;
322 std::cout << "Please enter smsc" << std::endl;
323 std::cin >> smsc;
324 std::string pdu;
325 std::cout << "Please enter pdu" << std::endl;
326 std::cin >> pdu;
327 std::string input;
328 std::cout << "Please enter status" << std::endl;
329 std::cin >> input;
330 uint32_t status = std::atoi(input.c_str());
331 result = smsService->AddSimMessage(slotId, StringUtils::ToUtf16(smsc), StringUtils::ToUtf16(pdu),
332 static_cast<ISmsServiceInterface::SimMessageStatus>(status));
333 if (result) {
334 std::cout << "TestAddSimMessage OK!" << std::endl;
335 } else {
336 std::cout << "TestAddSimMessage failed!" << std::endl;
337 }
338 }
339
TestDelSimMessage(const sptr<ISmsServiceInterface> & smsService) const340 void GsmSmsSenderTest::TestDelSimMessage(const sptr<ISmsServiceInterface> &smsService) const
341 {
342 AccessMmsToken token;
343 bool result = false;
344 if (smsService == nullptr) {
345 std::cout << "smsService is nullptr." << std::endl;
346 return;
347 }
348 std::string dest;
349 std::cout << "Please enter the card id" << std::endl;
350 int32_t slotId;
351 std::cin >> dest;
352 slotId = atoi(dest.c_str());
353 dest.clear();
354 std::string input;
355 std::cout << "Please enter msgIndex" << std::endl;
356 std::cin >> input;
357 uint32_t msgIndex = std::atoi(input.c_str());
358 result = smsService->DelSimMessage(slotId, msgIndex);
359 if (result) {
360 std::cout << "TestDelSimMessage OK!" << std::endl;
361 } else {
362 std::cout << "TestDelSimMessage failed!" << std::endl;
363 }
364 }
365
TestUpdateSimMessage(const sptr<ISmsServiceInterface> & smsService) const366 void GsmSmsSenderTest::TestUpdateSimMessage(const sptr<ISmsServiceInterface> &smsService) const
367 {
368 AccessMmsToken token;
369 bool result = false;
370 if (smsService == nullptr) {
371 std::cout << "smsService is nullptr." << std::endl;
372 return;
373 }
374 std::string dest;
375 std::cout << "Please enter the card id" << std::endl;
376 int32_t slotId;
377 std::cin >> dest;
378 slotId = atoi(dest.c_str());
379 dest.clear();
380 std::string input;
381 std::cout << "Please enter msgIndex" << std::endl;
382 std::cin >> input;
383 uint32_t msgIndex = std::atoi(input.c_str());
384 std::string pdu;
385 std::cout << "Please enter pdu" << std::endl;
386 std::cin >> pdu;
387 std::string smsc;
388 std::cout << "Please enter smsc" << std::endl;
389 std::cin >> smsc;
390 int32_t status;
391 std::cout << "Please enter status" << std::endl;
392 std::cin >> status;
393 std::u16string pduData = StringUtils::ToUtf16(pdu);
394 std::u16string smscData = StringUtils::ToUtf16(smsc);
395 result = smsService->UpdateSimMessage(
396 slotId, msgIndex, static_cast<ISmsServiceInterface::SimMessageStatus>(status), pduData, smscData);
397 if (result) {
398 std::cout << "TestUpdateSimMessage OK!" << std::endl;
399 } else {
400 std::cout << "TestUpdateSimMessage failed!" << std::endl;
401 }
402 }
403
TestGetAllSimMessages(const sptr<ISmsServiceInterface> & smsService) const404 void GsmSmsSenderTest::TestGetAllSimMessages(const sptr<ISmsServiceInterface> &smsService) const
405 {
406 AccessMmsToken token;
407 std::vector<ShortMessage> result;
408 if (smsService == nullptr) {
409 std::cout << "smsService is nullptr." << std::endl;
410 return;
411 }
412 std::string dest;
413 std::cout << "Please enter the card id" << std::endl;
414 int32_t slotId;
415 std::cin >> dest;
416 slotId = atoi(dest.c_str());
417 dest.clear();
418 smsService->GetAllSimMessages(slotId, result);
419 std::cout << "TestGetAllSimMessages Begin:" << std::endl;
420 for (auto &item : result) {
421 std::cout << "[" << item.GetIndexOnSim() << "] " << StringUtils::StringToHex(item.GetPdu()) << std::endl;
422 std::cout << "status:" << item.GetIccMessageStatus()
423 << " message:" << StringUtils::ToUtf8(item.GetVisibleMessageBody()) << std::endl;
424 }
425 std::cout << "TestGetAllSimMessages End!" << std::endl;
426 }
427
TestEnableCBRangeConfig(const sptr<ISmsServiceInterface> & smsService) const428 void GsmSmsSenderTest::TestEnableCBRangeConfig(const sptr<ISmsServiceInterface> &smsService) const
429 {
430 AccessMmsToken token;
431 if (smsService == nullptr) {
432 std::cout << "smsService is nullptr." << std::endl;
433 return;
434 }
435 std::string dest;
436 std::cout << "Please enter the card id" << std::endl;
437 int32_t slotId;
438 std::cin >> dest;
439 slotId = atoi(dest.c_str());
440 dest.clear();
441 bool enable = true;
442 uint32_t startMessageId = 0;
443 uint32_t endMessageId = 0;
444 uint8_t ranType = 1;
445 std::string input;
446 std::cout << "Please enter startMessageId" << std::endl;
447 std::cin >> input;
448 startMessageId = std::atoi(input.c_str());
449 std::cout << "Please enter endMessageId" << std::endl;
450 std::cin >> input;
451 endMessageId = std::atoi(input.c_str());
452 int32_t result = smsService->SetCBConfig(slotId, enable, startMessageId, endMessageId, ranType);
453 std::cout << "TestEnableCBRangeConfig:" << result << std::endl;
454 }
455
TestDisableCBRangeConfig(const sptr<ISmsServiceInterface> & smsService) const456 void GsmSmsSenderTest::TestDisableCBRangeConfig(const sptr<ISmsServiceInterface> &smsService) const
457 {
458 AccessMmsToken token;
459 if (smsService == nullptr) {
460 std::cout << "smsService is nullptr." << std::endl;
461 return;
462 }
463 std::string dest;
464 std::cout << "Please enter the card id" << std::endl;
465 int32_t slotId;
466 std::cin >> dest;
467 slotId = atoi(dest.c_str());
468 dest.clear();
469 bool enable = false;
470 uint32_t startMessageId = 0;
471 uint32_t endMessageId = 0;
472 uint8_t ranType = 1;
473 std::string input;
474 std::cout << "Please enter startMessageId" << std::endl;
475 std::cin >> input;
476 startMessageId = std::atoi(input.c_str());
477 std::cout << "Please enter endMessageId" << std::endl;
478 std::cin >> input;
479 endMessageId = std::atoi(input.c_str());
480 int32_t result = smsService->SetCBConfig(slotId, enable, startMessageId, endMessageId, ranType);
481 std::cout << "TestDisableCBRangeConfig:" << result << std::endl;
482 }
483
TestEnableCBConfig(const sptr<ISmsServiceInterface> & smsService) const484 void GsmSmsSenderTest::TestEnableCBConfig(const sptr<ISmsServiceInterface> &smsService) const
485 {
486 AccessMmsToken token;
487 if (smsService == nullptr) {
488 std::cout << "smsService is nullptr." << std::endl;
489 return;
490 }
491 std::string dest;
492 std::cout << "Please enter the card id" << std::endl;
493 int32_t slotId;
494 std::cin >> dest;
495 slotId = atoi(dest.c_str());
496 dest.clear();
497 bool enable = true;
498 uint32_t identifier = 0;
499 uint8_t ranType = 1;
500 std::string input;
501 std::cout << "Please enter identifier" << std::endl;
502 std::cin >> input;
503 identifier = std::atoi(input.c_str());
504 int32_t result = smsService->SetCBConfig(slotId, enable, identifier, identifier, ranType);
505 std::cout << "TestEnableCBConfig:" << result << std::endl;
506 }
507
TestDisableCBConfig(const sptr<ISmsServiceInterface> & smsService) const508 void GsmSmsSenderTest::TestDisableCBConfig(const sptr<ISmsServiceInterface> &smsService) const
509 {
510 AccessMmsToken token;
511 if (smsService == nullptr) {
512 std::cout << "smsService is nullptr." << std::endl;
513 return;
514 }
515 std::string dest;
516 std::cout << "Please enter the card id" << std::endl;
517 int32_t slotId;
518 std::cin >> dest;
519 slotId = atoi(dest.c_str());
520 dest.clear();
521 bool enable = false;
522 uint32_t identifier = 0;
523 uint8_t ranType = 1;
524 std::string input;
525 std::cout << "Please enter identifier" << std::endl;
526 std::cin >> input;
527 identifier = std::atoi(input.c_str());
528 int32_t result = smsService->SetCBConfig(slotId, enable, identifier, identifier, ranType);
529 std::cout << "TestDisableCBConfig:" << result << std::endl;
530 }
531
TestSetDefaultSmsSlotId(const sptr<ISmsServiceInterface> & smsService) const532 void GsmSmsSenderTest::TestSetDefaultSmsSlotId(const sptr<ISmsServiceInterface> &smsService) const
533 {
534 AccessMmsToken token;
535 if (smsService == nullptr) {
536 std::cout << "smsService is nullptr." << std::endl;
537 return;
538 }
539 int32_t slotId;
540 std::string input;
541 std::cout << "Please enter Slot Id" << std::endl;
542 std::cin >> input;
543 slotId = std::atoi(input.c_str());
544 int32_t result = smsService->SetDefaultSmsSlotId(slotId);
545 std::cout << "TestSetDefaultSmsSlotId:" << result << std::endl;
546 }
547
TestGetDefaultSmsSlotId(const sptr<ISmsServiceInterface> & smsService) const548 void GsmSmsSenderTest::TestGetDefaultSmsSlotId(const sptr<ISmsServiceInterface> &smsService) const
549 {
550 int32_t result;
551 if (smsService == nullptr) {
552 std::cout << "smsService is nullptr." << std::endl;
553 return;
554 }
555 result = smsService->GetDefaultSmsSlotId();
556 std::cout << "TestGetDefaultSmsSlotId:" << result << std::endl;
557 }
558
TestSplitMessage(const sptr<ISmsServiceInterface> & smsService) const559 void GsmSmsSenderTest::TestSplitMessage(const sptr<ISmsServiceInterface> &smsService) const
560 {
561 AccessMmsToken token;
562 std::vector<std::u16string> result;
563 if (smsService == nullptr) {
564 std::cout << "smsService is nullptr." << std::endl;
565 return;
566 }
567 std::string input;
568 std::cout << "Please enter message" << std::endl;
569 std::getline(std::cin, input);
570 smsService->SplitMessage(StringUtils::ToUtf16(input), result);
571 std::cout << "TestSplitMessage size:" << result.size() << std::endl;
572 for (auto &item : result) {
573 std::cout << StringUtils::ToUtf8(item) << std::endl;
574 }
575 }
576
TestGetSmsSegmentsInfo(const sptr<ISmsServiceInterface> & smsService) const577 void GsmSmsSenderTest::TestGetSmsSegmentsInfo(const sptr<ISmsServiceInterface> &smsService) const
578 {
579 if (smsService == nullptr) {
580 std::cout << "TestGetSmsSegmentsInfo smsService is nullptr." << std::endl;
581 return;
582 }
583 int32_t slotId;
584 std::string input;
585 std::cout << "TestGetSmsSegmentsInfo Please enter Slot Id" << std::endl;
586 std::getline(std::cin, input);
587 slotId = std::atoi(input.c_str());
588 input.clear();
589 std::cout << "Please enter message" << std::endl;
590 std::getline(std::cin, input);
591 ISmsServiceInterface::SmsSegmentsInfo result;
592 if (smsService->GetSmsSegmentsInfo(slotId, StringUtils::ToUtf16(input), false, result) != TELEPHONY_ERR_SUCCESS) {
593 std::cout << "Get Sms SegmentsInfo Fail." << std::endl;
594 return;
595 }
596
597 int32_t codeScheme = static_cast<int32_t>(result.msgCodeScheme);
598 std::cout << "msgSegCount:" << result.msgSegCount << " msgEncodingCount:" << result.msgEncodingCount << std::endl;
599 std::cout << "msgRemainCount:" << result.msgRemainCount << " msgCodeScheme:" << codeScheme << std::endl;
600 }
601
TestIsImsSmsSupported(const sptr<ISmsServiceInterface> & smsService) const602 void GsmSmsSenderTest::TestIsImsSmsSupported(const sptr<ISmsServiceInterface> &smsService) const
603 {
604 if (smsService == nullptr) {
605 std::cout << "TestIsImsSmsSupported smsService is nullptr." << std::endl;
606 return;
607 }
608 int32_t slotId;
609 std::string input;
610 std::cout << "TestIsImsSmsSupported Please enter Slot Id" << std::endl;
611 std::getline(std::cin, input);
612 slotId = std::atoi(input.c_str());
613 bool result = false;
614 smsService->IsImsSmsSupported(slotId, result);
615 std::string res = result ? "true" : "false";
616 std::cout << "IsImsSmsSupported:" << res << std::endl;
617 }
618
TestSetImsSmsConfig(const sptr<ISmsServiceInterface> & smsService) const619 void GsmSmsSenderTest::TestSetImsSmsConfig(const sptr<ISmsServiceInterface> &smsService) const
620 {
621 AccessMmsToken token;
622 if (smsService == nullptr) {
623 std::cout << "TestSetImsSmsConfig smsService is nullptr." << std::endl;
624 return;
625 }
626 int32_t slotId;
627 std::string input;
628 std::cout << "TestSetImsSmsConfig Please enter Slot Id" << std::endl;
629 std::getline(std::cin, input);
630 slotId = std::atoi(input.c_str());
631 int32_t enable;
632 std::cout << "Please enter enable" << std::endl;
633 std::getline(std::cin, input);
634 enable = std::atoi(input.c_str());
635 std::string res = smsService->SetImsSmsConfig(slotId, enable) ? "true" : "false";
636 std::cout << "SetImsSmsConfig:" << res << std::endl;
637 }
638
TestGetImsShortMessageFormat(const sptr<ISmsServiceInterface> & smsService) const639 void GsmSmsSenderTest::TestGetImsShortMessageFormat(const sptr<ISmsServiceInterface> &smsService) const
640 {
641 if (smsService == nullptr) {
642 std::cout << "smsService is nullptr." << std::endl;
643 return;
644 }
645 std::u16string format;
646 smsService->GetImsShortMessageFormat(format);
647 std::cout << "GetImsShortMessageFormat:" << StringUtils::ToUtf8(format) << std::endl;
648 }
649
TestAddBlockPhone() const650 void GsmSmsSenderTest::TestAddBlockPhone() const
651 {
652 std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateDataAHelper();
653 if (helper == nullptr) {
654 std::cout << "Creator helper nullptr error." << std::endl;
655 return;
656 }
657 std::string input;
658 std::cout << "Please enter block phone number" << std::endl;
659 std::getline(std::cin, input);
660
661 Uri uri("dataability:///com.ohos.contactsdataability/contacts/contact_blocklist");
662 NativeRdb::ValuesBucket value;
663 value.PutString("phone_number", input);
664 int ret = helper->Insert(uri, value);
665 helper->Release();
666 std::cout << "add block:" << input << ((ret >= 0) ? " success" : " error") << std::endl;
667 }
668
TestRemoveBlockPhone() const669 void GsmSmsSenderTest::TestRemoveBlockPhone() const
670 {
671 std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateDataAHelper();
672 if (helper == nullptr) {
673 std::cout << "Creator helper nullptr error." << std::endl;
674 return;
675 }
676 std::string input;
677 std::cout << "Please enter Remove phone number" << std::endl;
678 std::getline(std::cin, input);
679 Uri uri("dataability:///com.ohos.contactsdataability/contacts/contact_blocklist");
680 NativeRdb::DataAbilityPredicates predicates;
681 predicates.EqualTo("phone_number", input);
682 int ret = helper->Delete(uri, predicates);
683 helper->Release();
684 std::cout << "remove block:" << input << ((ret >= 0) ? " success" : " error") << std::endl;
685 }
686
TestHasSmsCapability(const sptr<ISmsServiceInterface> & smsService) const687 void GsmSmsSenderTest::TestHasSmsCapability(const sptr<ISmsServiceInterface> &smsService) const
688 {
689 if (smsService == nullptr) {
690 std::cout << "GsmSmsSenderTest smsService is nullptr error." << std::endl;
691 return;
692 }
693 std::string res = smsService->HasSmsCapability() ? "true" : "false";
694 std::cout << "HasSmsCapability:" << res << std::endl;
695 }
696
CreateDataAHelper() const697 std::shared_ptr<AppExecFwk::DataAbilityHelper> GsmSmsSenderTest::CreateDataAHelper() const
698 {
699 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
700 if (saManager == nullptr) {
701 std::cout << "Get system ability mgr failed." << std::endl;
702 return nullptr;
703 }
704 auto remoteObj = saManager->GetSystemAbility(TELEPHONY_SMS_MMS_SYS_ABILITY_ID);
705 if (remoteObj == nullptr) {
706 std::cout << "GetSystemAbility Service Failed." << std::endl;
707 return nullptr;
708 }
709 const std::string uriContact("dataability:///com.ohos.contactsdataability");
710 std::shared_ptr<Uri> dataAbilityUri = std::make_shared<Uri>(uriContact);
711 if (dataAbilityUri == nullptr) {
712 std::cout << "make dataAbilityUri Error." << std::endl;
713 return nullptr;
714 }
715 return AppExecFwk::DataAbilityHelper::Creator(remoteObj, dataAbilityUri);
716 }
717 } // namespace Telephony
718 } // namespace OHOS
719