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 "splitmessage_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 static 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
SplitMessage(const uint8_t * data,size_t size)41 void SplitMessage(const uint8_t *data, size_t size)
42 {
43 MessageParcel dataParcel;
44 MessageParcel replyParcel;
45 MessageOption option(MessageOption::TF_SYNC);
46
47 std::string message(reinterpret_cast<const char *>(data), size);
48 auto messageU16 = Str8ToStr16(message);
49 dataParcel.WriteString16(messageU16);
50 dataParcel.RewindRead(0);
51
52 DelayedSingleton<SmsService>::GetInstance()->OnSplitMessage(dataParcel, replyParcel, option);
53
54 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
55 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
56 if (interfaceManager == nullptr) {
57 TELEPHONY_LOGE("interfaceManager nullptr error");
58 return;
59 }
60
61 std::string messageData(reinterpret_cast<const char *>(data), size);
62 std::vector<std::u16string> splitMessage;
63 interfaceManager->SplitMessage(messageData, splitMessage);
64 auto smsSendManager = std::make_unique<SmsSendManager>(slotId);
65 if (smsSendManager == nullptr) {
66 TELEPHONY_LOGE("failed to create SmsSendManager");
67 return;
68 }
69 smsSendManager->SplitMessage(messageData, splitMessage);
70
71 SmsCodingScheme codingType;
72 std::vector<struct SplitInfo> cellsInfos;
73 GsmSmsMessage gsmSmsMessage;
74 gsmSmsMessage.SplitMessage(cellsInfos, messageData, false, codingType, false);
75 CdmaSmsMessage cdmaSmsMessage;
76 cdmaSmsMessage.SplitMessage(cellsInfos, messageData, false, codingType, false);
77 }
78
GetImsShortMessageFormat(const uint8_t * data,size_t size)79 void GetImsShortMessageFormat(const uint8_t *data, size_t size)
80 {
81 MessageParcel dataParcel;
82 MessageParcel replyParcel;
83 MessageOption option(MessageOption::TF_SYNC);
84
85 dataParcel.WriteBuffer(data, size);
86 dataParcel.RewindRead(0);
87 DelayedSingleton<SmsService>::GetInstance()->OnGetImsShortMessageFormat(dataParcel, replyParcel, option);
88
89 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
90 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
91 if (interfaceManager == nullptr) {
92 TELEPHONY_LOGE("interfaceManager nullptr error");
93 return;
94 }
95 std::u16string format;
96 interfaceManager->GetImsShortMessageFormat(format);
97
98 auto smsSendManager = std::make_unique<SmsSendManager>(slotId);
99 if (smsSendManager == nullptr) {
100 TELEPHONY_LOGE("failed to create SmsSendManager");
101 return;
102 }
103 smsSendManager->GetImsShortMessageFormat(format);
104 }
105
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)106 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
107 {
108 if (data == nullptr || size == 0) {
109 return;
110 }
111
112 if (!IsServiceInited()) {
113 return;
114 }
115
116 SplitMessage(data, size);
117 GetImsShortMessageFormat(data, size);
118 }
119 } // namespace OHOS
120
121 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)122 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
123 {
124 /* Run your code on data */
125 OHOS::AddSmsTokenFuzzer token;
126 OHOS::DoSomethingInterestingWithMyAPI(data, size);
127 return 0;
128 }
129