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