1e5d0e473Sopenharmony_ci/*
2e5d0e473Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3e5d0e473Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e5d0e473Sopenharmony_ci * you may not use this file except in compliance with the License.
5e5d0e473Sopenharmony_ci * You may obtain a copy of the License at
6e5d0e473Sopenharmony_ci *
7e5d0e473Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e5d0e473Sopenharmony_ci *
9e5d0e473Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e5d0e473Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e5d0e473Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e5d0e473Sopenharmony_ci * See the License for the specific language governing permissions and
13e5d0e473Sopenharmony_ci * limitations under the License.
14e5d0e473Sopenharmony_ci */
15e5d0e473Sopenharmony_ci
16e5d0e473Sopenharmony_ci#include "smseventissue_fuzzer.h"
17e5d0e473Sopenharmony_ci
18e5d0e473Sopenharmony_ci#ifdef GTEST_API_
19e5d0e473Sopenharmony_ci#define private public
20e5d0e473Sopenharmony_ci#define protected public
21e5d0e473Sopenharmony_ci#endif
22e5d0e473Sopenharmony_ci
23e5d0e473Sopenharmony_ci#include "addsmstoken_fuzzer.h"
24e5d0e473Sopenharmony_ci#include "delivery_short_message_callback_stub.h"
25e5d0e473Sopenharmony_ci#include "send_short_message_callback_stub.h"
26e5d0e473Sopenharmony_ci#include "sms_interface_manager.h"
27e5d0e473Sopenharmony_ci
28e5d0e473Sopenharmony_ciusing namespace OHOS::Telephony;
29e5d0e473Sopenharmony_cinamespace OHOS {
30e5d0e473Sopenharmony_ciconst std::int32_t SLOT_NUM = 2;
31e5d0e473Sopenharmony_cibool g_flag = false;
32e5d0e473Sopenharmony_ci
33e5d0e473Sopenharmony_civoid DoSentIssueTest(const uint8_t *data, size_t size, std::shared_ptr<SmsSendManager> smsSendManager)
34e5d0e473Sopenharmony_ci{
35e5d0e473Sopenharmony_ci    std::string desAddr(reinterpret_cast<const char *>(data), size);
36e5d0e473Sopenharmony_ci    std::string scAddr(reinterpret_cast<const char *>(data), size);
37e5d0e473Sopenharmony_ci    std::string text(reinterpret_cast<const char *>(data), size);
38e5d0e473Sopenharmony_ci    const sptr<ISendShortMessageCallback> sendCallback =
39e5d0e473Sopenharmony_ci        iface_cast<ISendShortMessageCallback>(new SendShortMessageCallbackStub());
40e5d0e473Sopenharmony_ci    if (sendCallback == nullptr) {
41e5d0e473Sopenharmony_ci        return;
42e5d0e473Sopenharmony_ci    }
43e5d0e473Sopenharmony_ci    const sptr<IDeliveryShortMessageCallback> deliveryCallback =
44e5d0e473Sopenharmony_ci        iface_cast<IDeliveryShortMessageCallback>(new DeliveryShortMessageCallbackStub());
45e5d0e473Sopenharmony_ci    if (deliveryCallback == nullptr) {
46e5d0e473Sopenharmony_ci        return;
47e5d0e473Sopenharmony_ci    }
48e5d0e473Sopenharmony_ci    const std::shared_ptr<SmsSendIndexer> smsSendIndexer =
49e5d0e473Sopenharmony_ci        std::make_shared<SmsSendIndexer>(desAddr, scAddr, text, sendCallback, deliveryCallback);
50e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendMessageSucceed(smsSendIndexer);
51e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->HandleMessageResponse(smsSendIndexer);
52e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SyncSwitchISmsResponse();
53e5d0e473Sopenharmony_ci
54e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendResultCallBack(
55e5d0e473Sopenharmony_ci        smsSendIndexer, ISendShortMessageCallback::SEND_SMS_FAILURE_UNKNOWN);
56e5d0e473Sopenharmony_ci
57e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendCacheMapTimeoutCheck();
58e5d0e473Sopenharmony_ci    int64_t id = static_cast<int16_t>(size);
59e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendCacheMapAddItem(id, smsSendIndexer);
60e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendCacheMapEraseItem(id);
61e5d0e473Sopenharmony_ci    uint8_t refId = static_cast<uint8_t>(size);
62e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->UpdateUnSentCellCount(refId);
63e5d0e473Sopenharmony_ci
64e5d0e473Sopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
65e5d0e473Sopenharmony_ci    bool value = slotId == 0 ? true : false;
66e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SetImsSmsConfig(slotId, value);
67e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->GetMsgRef8Bit();
68e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->GetMsgRef64Bit();
69e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->CheckForce7BitEncodeType();
70e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->GetNetworkId();
71e5d0e473Sopenharmony_ci
72e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SendMessageSucceed(smsSendIndexer);
73e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendMessageFailed(smsSendIndexer);
74e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SendMessageFailed(smsSendIndexer);
75e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->HandleResend(smsSendIndexer);
76e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->HandleResend(smsSendIndexer);
77e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SendMessageSucceed(nullptr);
78e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendMessageFailed(nullptr);
79e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SendMessageFailed(nullptr);
80e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->HandleResend(nullptr);
81e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->HandleResend(nullptr);
82e5d0e473Sopenharmony_ci}
83e5d0e473Sopenharmony_ci
84e5d0e473Sopenharmony_civoid DoSentIssuePartTest(const uint8_t *data, size_t size, std::shared_ptr<SmsSendManager> smsSendManager)
85e5d0e473Sopenharmony_ci{
86e5d0e473Sopenharmony_ci    std::int64_t erase = static_cast<int64_t>(size);
87e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendCacheMapEraseItem(erase);
88e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SendCacheMapEraseItem(erase);
89e5d0e473Sopenharmony_ci
90e5d0e473Sopenharmony_ci    std::int32_t eventId = static_cast<int32_t>(size);
91e5d0e473Sopenharmony_ci    std::int64_t refId = static_cast<int64_t>(size);
92e5d0e473Sopenharmony_ci    AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(eventId, refId);
93e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->FindCacheMapAndTransform(response);
94e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->FindCacheMapAndTransform(response);
95e5d0e473Sopenharmony_ci
96e5d0e473Sopenharmony_ci    std::int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
97e5d0e473Sopenharmony_ci    std::int32_t enable = static_cast<int32_t>(size % SLOT_NUM);
98e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SetImsSmsConfig(slotId, enable);
99e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SetImsSmsConfig(slotId, enable);
100e5d0e473Sopenharmony_ci
101e5d0e473Sopenharmony_ci    bool isImsNetDomain = slotId == 1 ? true : false;
102e5d0e473Sopenharmony_ci    std::int32_t voiceServiceState = static_cast<int32_t>(size);
103e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SetNetworkState(isImsNetDomain, voiceServiceState);
104e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SetNetworkState(isImsNetDomain, voiceServiceState);
105e5d0e473Sopenharmony_ci
106e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->SendCacheMapTimeoutCheck();
107e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->SendCacheMapTimeoutCheck();
108e5d0e473Sopenharmony_ci}
109e5d0e473Sopenharmony_ci
110e5d0e473Sopenharmony_civoid DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
111e5d0e473Sopenharmony_ci{
112e5d0e473Sopenharmony_ci    if (data == nullptr || size == 0) {
113e5d0e473Sopenharmony_ci        return;
114e5d0e473Sopenharmony_ci    }
115e5d0e473Sopenharmony_ci
116e5d0e473Sopenharmony_ci    if (g_flag) {
117e5d0e473Sopenharmony_ci        return;
118e5d0e473Sopenharmony_ci    }
119e5d0e473Sopenharmony_ci    g_flag = true;
120e5d0e473Sopenharmony_ci
121e5d0e473Sopenharmony_ci    int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
122e5d0e473Sopenharmony_ci    auto smsInterfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
123e5d0e473Sopenharmony_ci    if (smsInterfaceManager == nullptr) {
124e5d0e473Sopenharmony_ci        return;
125e5d0e473Sopenharmony_ci    }
126e5d0e473Sopenharmony_ci
127e5d0e473Sopenharmony_ci    smsInterfaceManager->InitInterfaceManager();
128e5d0e473Sopenharmony_ci    auto smsSendManager = std::make_shared<SmsSendManager>(slotId);
129e5d0e473Sopenharmony_ci    if (smsSendManager == nullptr) {
130e5d0e473Sopenharmony_ci        return;
131e5d0e473Sopenharmony_ci    }
132e5d0e473Sopenharmony_ci    smsSendManager->Init();
133e5d0e473Sopenharmony_ci    smsSendManager->InitNetworkHandle();
134e5d0e473Sopenharmony_ci    if (smsSendManager->gsmSmsSender_ == nullptr || smsSendManager->cdmaSmsSender_ == nullptr) {
135e5d0e473Sopenharmony_ci        return;
136e5d0e473Sopenharmony_ci    }
137e5d0e473Sopenharmony_ci
138e5d0e473Sopenharmony_ci    std::int32_t eventId = static_cast<int32_t>(size);
139e5d0e473Sopenharmony_ci    std::int64_t refId = static_cast<int64_t>(size);
140e5d0e473Sopenharmony_ci
141e5d0e473Sopenharmony_ci    AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(eventId, refId);
142e5d0e473Sopenharmony_ci    smsSendManager->gsmSmsSender_->ProcessEvent(response);
143e5d0e473Sopenharmony_ci    smsSendManager->cdmaSmsSender_->ProcessEvent(response);
144e5d0e473Sopenharmony_ci
145e5d0e473Sopenharmony_ci    DoSentIssueTest(data, size, smsSendManager);
146e5d0e473Sopenharmony_ci    DoSentIssuePartTest(data, size, smsSendManager);
147e5d0e473Sopenharmony_ci}
148e5d0e473Sopenharmony_ci} // namespace OHOS
149e5d0e473Sopenharmony_ci
150e5d0e473Sopenharmony_ci/* Fuzzer entry point */
151e5d0e473Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
152e5d0e473Sopenharmony_ci{
153e5d0e473Sopenharmony_ci    /* Run your code on data */
154e5d0e473Sopenharmony_ci    OHOS::AddSmsTokenFuzzer token;
155e5d0e473Sopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
156e5d0e473Sopenharmony_ci    return 0;
157e5d0e473Sopenharmony_ci}
158