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