1 /*
2 * Copyright (c) 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 "updatesimmessage_fuzzer.h"
17
18 #define private public
19 #include "addsmstoken_fuzzer.h"
20 #include "core_manager_inner.h"
21 #include "i_sms_service_interface.h"
22 #include "sms_service.h"
23
24 using namespace OHOS::Telephony;
25 namespace OHOS {
26 static bool g_isInited = false;
27 constexpr int32_t SLOT_NUM = 2;
28 constexpr int32_t SIM_MESSAGE_STATUE = 4;
29
IsServiceInited()30 bool IsServiceInited()
31 {
32 if (!g_isInited) {
33 CoreManagerInner::GetInstance().isInitAllObj_ = true;
34 DelayedSingleton<SmsService>::GetInstance()->registerToService_ = true;
35 DelayedSingleton<SmsService>::GetInstance()->WaitCoreServiceToInit();
36 DelayedSingleton<SmsService>::GetInstance()->OnStart();
37 if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() ==
38 static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) {
39 g_isInited = true;
40 }
41 }
42 return g_isInited;
43 }
44
UpdateSimMessage(const uint8_t * data,size_t size)45 void UpdateSimMessage(const uint8_t *data, size_t size)
46 {
47 if (!IsServiceInited()) {
48 return;
49 }
50
51 MessageParcel dataParcel;
52 MessageParcel replyParcel;
53 MessageOption option(MessageOption::TF_SYNC);
54
55 std::string smsc(reinterpret_cast<const char *>(data), size);
56 std::string pdu(reinterpret_cast<const char *>(data), size);
57 auto smscU16 = Str8ToStr16(smsc);
58 auto pduU16 = Str8ToStr16(pdu);
59 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
60 ISmsServiceInterface::SimMessageStatus status =
61 static_cast<ISmsServiceInterface::SimMessageStatus>(size % SIM_MESSAGE_STATUE);
62
63 dataParcel.WriteInt32(slotId);
64 dataParcel.WriteUint32(size);
65 dataParcel.WriteUint32(status);
66 dataParcel.WriteString16(smscU16);
67 dataParcel.WriteString16(pduU16);
68 dataParcel.RewindRead(0);
69
70 DelayedSingleton<SmsService>::GetInstance()->OnUpdateSimMessage(dataParcel, replyParcel, option);
71
72 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
73 if (interfaceManager == nullptr) {
74 TELEPHONY_LOGE("interfaceManager nullptr error");
75 return;
76 }
77 interfaceManager->InitInterfaceManager();
78 interfaceManager->UpdateSimMessage(size, status, pdu, smsc);
79
80 std::shared_ptr<SmsMiscManager> smsMiscManager = std::make_shared<SmsMiscManager>(slotId);
81 if (smsMiscManager == nullptr) {
82 TELEPHONY_LOGE("smsMiscManager nullptr error");
83 return;
84 }
85 smsMiscManager->UpdateSimMessage(size, status, pdu, smsc);
86 }
87
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)88 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
89 {
90 if (data == nullptr || size == 0) {
91 return;
92 }
93
94 UpdateSimMessage(data, size);
95 DelayedSingleton<SmsService>::DestroyInstance();
96 }
97 } // namespace OHOS
98
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
101 {
102 /* Run your code on data */
103 OHOS::AddSmsTokenFuzzer token;
104 OHOS::DoSomethingInterestingWithMyAPI(data, size);
105 return 0;
106 }
107