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