1 /*
2  * Copyright (c) 2022-2024 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 "databasedsmsdelivery_fuzzer.h"
17 
18 #ifdef GTEST_API_
19 #define private public
20 #endif
21 
22 #include "addsmstoken_fuzzer.h"
23 #include "delivery_short_message_callback_stub.h"
24 #include "send_short_message_callback_stub.h"
25 #include "sms_interface_manager.h"
26 
27 using namespace OHOS::Telephony;
28 namespace OHOS {
29 constexpr int32_t SLOT_NUM = 2;
30 bool g_flag = false;
31 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)32 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
33 {
34     if (data == nullptr || size == 0) {
35         return;
36     }
37 
38     if (g_flag) {
39         return;
40     }
41     g_flag = true;
42 
43     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
44     auto smsInterfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
45     if (smsInterfaceManager == nullptr) {
46         return;
47     }
48 
49     smsInterfaceManager->InitInterfaceManager();
50     std::string desAddr(reinterpret_cast<const char *>(data), size);
51     std::string scAddr(reinterpret_cast<const char *>(data), size);
52     std::uint16_t port = static_cast<uint16_t>(size);
53 
54     const sptr<ISendShortMessageCallback> sendCallback =
55         iface_cast<ISendShortMessageCallback>(new SendShortMessageCallbackStub());
56     if (sendCallback == nullptr) {
57         return;
58     }
59     const sptr<IDeliveryShortMessageCallback> deliveryCallback =
60         iface_cast<IDeliveryShortMessageCallback>(new DeliveryShortMessageCallbackStub());
61     if (deliveryCallback == nullptr) {
62         return;
63     }
64 
65     smsInterfaceManager->DataBasedSmsDelivery(desAddr, scAddr, port, data, size, sendCallback, deliveryCallback);
66     auto smsSendManager = std::make_shared<SmsSendManager>(slotId);
67     if (smsSendManager == nullptr) {
68         return;
69     }
70     smsSendManager->Init();
71     smsSendManager->InitNetworkHandle();
72     smsSendManager->DataBasedSmsDelivery(desAddr, scAddr, port, data, size, sendCallback, deliveryCallback);
73 
74     if (smsSendManager->gsmSmsSender_ != nullptr) {
75         smsSendManager->gsmSmsSender_->DataBasedSmsDelivery(
76             desAddr, scAddr, port, data, size, sendCallback, deliveryCallback);
77     }
78     if (smsSendManager->cdmaSmsSender_ != nullptr) {
79         smsSendManager->cdmaSmsSender_->DataBasedSmsDelivery(
80             desAddr, scAddr, port, data, size, sendCallback, deliveryCallback);
81     }
82     DelayedSingleton<ImsSmsClient>::GetInstance()->UnInit();
83     smsInterfaceManager = nullptr;
84     return;
85 }
86 } // namespace OHOS
87 
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
90 {
91     /* Run your code on data */
92     OHOS::AddSmsTokenFuzzer token;
93     OHOS::DoSomethingInterestingWithMyAPI(data, size);
94     return 0;
95 }
96