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 "smseventissue_fuzzer.h"
17
18 #define private public
19 #define protected public
20
21 #include "addsmstoken_fuzzer.h"
22 #include "delivery_short_message_callback_stub.h"
23 #include "send_short_message_callback_stub.h"
24 #include "sms_interface_manager.h"
25
26 using namespace OHOS::Telephony;
27 namespace OHOS {
28 const std::int32_t MAX_EVENT = 1024;
29 const std::int32_t MIDDLE_EVENT = 50;
30 const std::int32_t EVERY_STEP = 100;
31 const std::int32_t SLOT_NUM = 2;
32
DoSentIssueTest(const uint8_t * data,size_t size,std::shared_ptr<SmsSendManager> smsSendManager)33 void DoSentIssueTest(const uint8_t *data, size_t size, std::shared_ptr<SmsSendManager> smsSendManager)
34 {
35 std::string desAddr(reinterpret_cast<const char *>(data), size);
36 std::string scAddr(reinterpret_cast<const char *>(data), size);
37 std::string text(reinterpret_cast<const char *>(data), size);
38 const sptr<ISendShortMessageCallback> sendCallback =
39 iface_cast<ISendShortMessageCallback>(new SendShortMessageCallbackStub());
40 if (sendCallback == nullptr) {
41 return;
42 }
43 const sptr<IDeliveryShortMessageCallback> deliveryCallback =
44 iface_cast<IDeliveryShortMessageCallback>(new DeliveryShortMessageCallbackStub());
45 if (deliveryCallback == nullptr) {
46 return;
47 }
48 const std::shared_ptr<SmsSendIndexer> smsSendIndexer =
49 std::make_shared<SmsSendIndexer>(desAddr, scAddr, text, sendCallback, deliveryCallback);
50 smsSendManager->gsmSmsSender_->SendMessageSucceed(smsSendIndexer);
51 smsSendManager->cdmaSmsSender_->SendMessageSucceed(smsSendIndexer);
52 smsSendManager->gsmSmsSender_->SendMessageFailed(smsSendIndexer);
53 smsSendManager->cdmaSmsSender_->SendMessageFailed(smsSendIndexer);
54 smsSendManager->gsmSmsSender_->HandleResend(smsSendIndexer);
55 smsSendManager->cdmaSmsSender_->HandleResend(smsSendIndexer);
56
57 std::int64_t erase = static_cast<int64_t>(size);
58 smsSendManager->gsmSmsSender_->SendCacheMapEraseItem(erase);
59 smsSendManager->cdmaSmsSender_->SendCacheMapEraseItem(erase);
60
61 std::int32_t eventId = static_cast<int32_t>(size);
62 std::int64_t refId = static_cast<int64_t>(size);
63 AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(eventId, refId);
64 smsSendManager->gsmSmsSender_->FindCacheMapAndTransform(response);
65 smsSendManager->cdmaSmsSender_->FindCacheMapAndTransform(response);
66
67 std::int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
68 std::int32_t enable = static_cast<int32_t>(size % SLOT_NUM);
69 smsSendManager->gsmSmsSender_->SetImsSmsConfig(slotId, enable);
70 smsSendManager->cdmaSmsSender_->SetImsSmsConfig(slotId, enable);
71
72 bool isImsNetDomain = slotId == 1 ? true : false;
73 std::int32_t voiceServiceState = static_cast<int32_t>(size);
74 smsSendManager->gsmSmsSender_->SetNetworkState(isImsNetDomain, voiceServiceState);
75 smsSendManager->cdmaSmsSender_->SetNetworkState(isImsNetDomain, voiceServiceState);
76
77 smsSendManager->gsmSmsSender_->SendCacheMapTimeoutCheck();
78 smsSendManager->cdmaSmsSender_->SendCacheMapTimeoutCheck();
79 }
80
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)81 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
82 {
83 if (data == nullptr || size == 0) {
84 return;
85 }
86
87 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
88 auto smsInterfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
89 if (smsInterfaceManager == nullptr) {
90 return;
91 }
92
93 smsInterfaceManager->InitInterfaceManager();
94 auto smsSendManager = std::make_shared<SmsSendManager>(slotId);
95 if (smsSendManager == nullptr) {
96 return;
97 }
98 smsSendManager->Init();
99 if (smsSendManager->gsmSmsSender_ == nullptr || smsSendManager->cdmaSmsSender_ == nullptr) {
100 return;
101 }
102
103 std::int32_t eventId = static_cast<int32_t>(size);
104 std::int64_t refId = static_cast<int64_t>(size);
105
106 while (eventId < MIDDLE_EVENT) {
107 AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(eventId, refId);
108 smsSendManager->gsmSmsSender_->ProcessEvent(response);
109 smsSendManager->cdmaSmsSender_->ProcessEvent(response);
110 eventId++;
111 }
112
113 while (eventId < MAX_EVENT) {
114 AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(eventId, refId);
115 smsSendManager->gsmSmsSender_->ProcessEvent(response);
116 smsSendManager->cdmaSmsSender_->ProcessEvent(response);
117 eventId += EVERY_STEP;
118 }
119
120 DoSentIssueTest(data, size, smsSendManager);
121 return;
122 }
123 } // namespace OHOS
124
125 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
127 {
128 /* Run your code on data */
129 OHOS::AddSmsTokenFuzzer token;
130 OHOS::DoSomethingInterestingWithMyAPI(data, size);
131 return 0;
132 }
133