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 "smsrecvitem_fuzzer.h"
17
18 #define private public
19 #define protected public
20
21 #include "addsmstoken_fuzzer.h"
22 #include "cdma_sms_message.h"
23 #include "common_event.h"
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 #include "delivery_short_message_callback_stub.h"
27 #include "send_short_message_callback_stub.h"
28 #include "sms_broadcast_subscriber_receiver.h"
29 #include "sms_interface_manager.h"
30 #include "sms_receive_reliability_handler.h"
31 #include "string_utils.h"
32
33 using namespace OHOS::Telephony;
34 namespace OHOS {
35 using namespace EventFwk;
36 constexpr int32_t SLOT_NUM = 2;
37 bool g_flag = false;
38
DoRecvItemsTest(const uint8_t * data,size_t size,std::shared_ptr<SmsReceiveManager> smsReceiveManager)39 void DoRecvItemsTest(const uint8_t *data, size_t size, std::shared_ptr<SmsReceiveManager> smsReceiveManager)
40 {
41 const std::shared_ptr<GsmSmsMessage> smsGsmMessage = std::make_shared<GsmSmsMessage>();
42 const std::shared_ptr<CdmaSmsMessage> smsCdmaMessage = std::make_shared<CdmaSmsMessage>();
43 smsReceiveManager->gsmSmsReceiveHandler_->HandleReceivedSms(smsGsmMessage);
44 smsReceiveManager->cdmaSmsReceiveHandler_->HandleReceivedSms(smsCdmaMessage);
45 smsReceiveManager->gsmSmsReceiveHandler_->HandleSmsByType(smsGsmMessage);
46 smsReceiveManager->cdmaSmsReceiveHandler_->HandleSmsByType(smsCdmaMessage);
47
48 std::int32_t result = static_cast<int32_t>(size);
49 smsReceiveManager->gsmSmsReceiveHandler_->ReplySmsToSmsc(result, smsGsmMessage);
50 smsReceiveManager->cdmaSmsReceiveHandler_->ReplySmsToSmsc(result, smsCdmaMessage);
51
52 const std::shared_ptr<SmsReceiveIndexer> smsReceiveIndexer = std::make_shared<SmsReceiveIndexer>();
53 smsReceiveManager->gsmSmsReceiveHandler_->CombineMessagePart(smsReceiveIndexer);
54 smsReceiveManager->cdmaSmsReceiveHandler_->CombineMessagePart(smsReceiveIndexer);
55
56 smsReceiveManager->gsmSmsReceiveHandler_->IsRepeatedMessagePart(smsReceiveIndexer);
57 smsReceiveManager->cdmaSmsReceiveHandler_->IsRepeatedMessagePart(smsReceiveIndexer);
58
59 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
60 auto reliabilityHandler = std::make_shared<SmsReceiveReliabilityHandler>(slotId);
61 if (reliabilityHandler == nullptr) {
62 return;
63 }
64 reliabilityHandler->DeleteMessageFormDb(smsReceiveIndexer->GetMsgRefId());
65
66 std::vector<SmsReceiveIndexer> dbIndexers;
67 std::string strData(reinterpret_cast<const char *>(data), size);
68 auto indexer = SmsReceiveIndexer(StringUtils::HexToByteVector(strData), size, size, size % SLOT_NUM, strData,
69 strData, size, size, size, size % SLOT_NUM, strData);
70
71 dbIndexers.push_back(indexer);
72 indexer = SmsReceiveIndexer(
73 StringUtils::HexToByteVector(strData), size, size, size % SLOT_NUM, size % SLOT_NUM, strData, strData, strData);
74 dbIndexers.push_back(indexer);
75 reliabilityHandler->CheckUnReceiveWapPush(dbIndexers);
76
77 std::shared_ptr<std::vector<std::string>> userDataRaws = std::make_shared<std::vector<std::string>>();
78 userDataRaws->push_back(strData);
79
80 int32_t pages = 0;
81 reliabilityHandler->GetWapPushUserDataSinglePage(indexer, userDataRaws);
82 reliabilityHandler->GetWapPushUserDataMultipage(pages, dbIndexers, size, userDataRaws);
83 reliabilityHandler->ReadyDecodeWapPushUserData(indexer, userDataRaws);
84 reliabilityHandler->GetSmsUserDataMultipage(pages, dbIndexers, size, userDataRaws);
85 reliabilityHandler->ReadySendSmsBroadcast(indexer, userDataRaws);
86 reliabilityHandler->DeleteMessageFormDb(size, size);
87 reliabilityHandler->RemoveBlockedSms(dbIndexers);
88
89 std::shared_ptr<SmsReceiveIndexer> indexerPtr =
90 std::make_shared<SmsReceiveIndexer>(StringUtils::HexToByteVector(strData), size, size, size % SLOT_NUM, strData,
91 strData, size, size, size, size % SLOT_NUM, strData);
92 if (indexerPtr == nullptr) {
93 return;
94 }
95 reliabilityHandler->SendBroadcast(indexerPtr, userDataRaws);
96 }
97
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)98 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
99 {
100 if (data == nullptr || size == 0) {
101 return;
102 }
103
104 if (g_flag) {
105 return;
106 }
107 g_flag = true;
108
109 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
110 auto smsInterfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
111 if (smsInterfaceManager == nullptr) {
112 return;
113 }
114
115 smsInterfaceManager->InitInterfaceManager();
116 auto smsReceiveManager = std::make_shared<SmsReceiveManager>(slotId);
117 if (smsReceiveManager == nullptr) {
118 return;
119 }
120 smsReceiveManager->Init();
121 if (smsReceiveManager->gsmSmsReceiveHandler_ == nullptr || smsReceiveManager->cdmaSmsReceiveHandler_ == nullptr) {
122 return;
123 }
124 smsReceiveManager->gsmSmsReceiveHandler_->Init();
125 smsReceiveManager->cdmaSmsReceiveHandler_->Init();
126
127 std::int32_t eventId = static_cast<int32_t>(size);
128 std::int64_t refId = static_cast<int64_t>(size);
129
130 AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(eventId, refId);
131 smsReceiveManager->gsmSmsReceiveHandler_->ProcessEvent(response);
132 smsReceiveManager->cdmaSmsReceiveHandler_->ProcessEvent(response);
133
134 MatchingSkills smsSkills;
135 std::string strData(reinterpret_cast<const char *>(data), size);
136 smsSkills.AddEvent(strData);
137 CommonEventSubscribeInfo smsSubscriberInfo(smsSkills);
138 auto smsReceiver = std::make_shared<SmsBroadcastSubscriberReceiver>(smsSubscriberInfo, nullptr, size, size);
139 CommonEventData comData;
140 smsReceiver->OnReceiveEvent(comData);
141
142 DoRecvItemsTest(data, size, smsReceiveManager);
143 }
144 } // namespace OHOS
145
146 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)147 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
148 {
149 /* Run your code on data */
150 OHOS::AddSmsTokenFuzzer token;
151 OHOS::DoSomethingInterestingWithMyAPI(data, size);
152 return 0;
153 }
154