1 /*
2 * Copyright (c) 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 #include "getsmssegmentsinfo_fuzzer.h"
17
18 #define private public
19 #include "addsmstoken_fuzzer.h"
20 #include "cdma_sms_message.h"
21 #include "napi_util.h"
22 #include "sms_service.h"
23
24 using namespace OHOS::Telephony;
25 namespace OHOS {
26 static bool g_isInited = false;
27 constexpr int32_t SLOT_NUM = 2;
28
IsServiceInited()29 bool IsServiceInited()
30 {
31 if (!g_isInited) {
32 DelayedSingleton<SmsService>::GetInstance()->OnStart();
33 if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() ==
34 static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) {
35 g_isInited = true;
36 }
37 }
38 return g_isInited;
39 }
40
GetSmsSegmentsInfo(const uint8_t * data,size_t size)41 void GetSmsSegmentsInfo(const uint8_t *data, size_t size)
42 {
43 if (!IsServiceInited()) {
44 return;
45 }
46
47 MessageParcel dataParcel;
48 MessageParcel replyParcel;
49 MessageOption option(MessageOption::TF_SYNC);
50
51 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
52 std::string message(reinterpret_cast<const char *>(data), size);
53 auto messageU16 = Str8ToStr16(message);
54 bool force7BitCode = slotId == 1 ? true : false;
55
56 dataParcel.WriteInt32(slotId);
57 dataParcel.WriteString16(messageU16);
58 dataParcel.WriteBool(force7BitCode);
59 dataParcel.RewindRead(0);
60
61 DelayedSingleton<SmsService>::GetInstance()->OnGetSmsSegmentsInfo(dataParcel, replyParcel, option);
62
63 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
64 if (interfaceManager == nullptr) {
65 TELEPHONY_LOGE("interfaceManager nullptr error");
66 return;
67 }
68 LengthInfo lenInfo;
69 interfaceManager->GetSmsSegmentsInfo(message, force7BitCode, lenInfo);
70
71 auto smsSendManager = std::make_unique<SmsSendManager>(slotId);
72 if (smsSendManager == nullptr) {
73 TELEPHONY_LOGE("failed to create SmsSendManager");
74 return;
75 }
76 smsSendManager->GetSmsSegmentsInfo(message, force7BitCode, lenInfo);
77 CdmaSmsMessage cdmaSmsMessage;
78 cdmaSmsMessage.GetSmsSegmentsInfo(message, force7BitCode, lenInfo);
79 GsmSmsMessage gsmSmsMessage;
80 gsmSmsMessage.GetSmsSegmentsInfo(message, force7BitCode, lenInfo);
81 }
82
IsImsSmsSupported(const uint8_t * data,size_t size)83 void IsImsSmsSupported(const uint8_t *data, size_t size)
84 {
85 if (!IsServiceInited()) {
86 return;
87 }
88
89 MessageParcel dataParcel;
90 MessageParcel replyParcel;
91 MessageOption option(MessageOption::TF_SYNC);
92
93 dataParcel.WriteBuffer(data, size);
94 dataParcel.RewindRead(0);
95 DelayedSingleton<SmsService>::GetInstance()->OnIsImsSmsSupported(dataParcel, replyParcel, option);
96
97 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
98 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
99 if (interfaceManager == nullptr) {
100 TELEPHONY_LOGE("interfaceManager nullptr error");
101 return;
102 }
103 bool isSupported = false;
104 interfaceManager->IsImsSmsSupported(slotId, isSupported);
105 auto smsSendManager = std::make_unique<SmsSendManager>(slotId);
106 if (smsSendManager == nullptr) {
107 TELEPHONY_LOGE("failed to create SmsSendManager");
108 return;
109 }
110 smsSendManager->IsImsSmsSupported(slotId, isSupported);
111 }
112
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)113 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
114 {
115 if (data == nullptr || size == 0) {
116 return;
117 }
118
119 GetSmsSegmentsInfo(data, size);
120 IsImsSmsSupported(data, size);
121 }
122 } // namespace OHOS
123
124 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
126 {
127 /* Run your code on data */
128 OHOS::AddSmsTokenFuzzer token;
129 OHOS::DoSomethingInterestingWithMyAPI(data, size);
130 return 0;
131 }
132