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 "updatesimmessage_fuzzer.h"
17
18 #define private public
19 #include "addsmstoken_fuzzer.h"
20 #include "i_sms_service_interface.h"
21 #include "napi_util.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 DelayedSingleton<SmsService>::GetInstance()->OnStart();
34 if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() ==
35 static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) {
36 g_isInited = true;
37 }
38 }
39 return g_isInited;
40 }
41
UpdateSimMessage(const uint8_t * data,size_t size)42 void UpdateSimMessage(const uint8_t *data, size_t size)
43 {
44 if (!IsServiceInited()) {
45 return;
46 }
47
48 MessageParcel dataParcel;
49 MessageParcel replyParcel;
50 MessageOption option(MessageOption::TF_SYNC);
51
52 std::string smsc(reinterpret_cast<const char *>(data), size);
53 std::string pdu(reinterpret_cast<const char *>(data), size);
54 auto smscU16 = Str8ToStr16(smsc);
55 auto pduU16 = Str8ToStr16(pdu);
56 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
57 ISmsServiceInterface::SimMessageStatus status =
58 static_cast<ISmsServiceInterface::SimMessageStatus>(size % SIM_MESSAGE_STATUE);
59
60 dataParcel.WriteInt32(slotId);
61 dataParcel.WriteUint32(size);
62 dataParcel.WriteUint32(status);
63 dataParcel.WriteString16(smscU16);
64 dataParcel.WriteString16(pduU16);
65 dataParcel.RewindRead(0);
66
67 DelayedSingleton<SmsService>::GetInstance()->OnUpdateSimMessage(dataParcel, replyParcel, option);
68
69 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId);
70 if (interfaceManager == nullptr) {
71 TELEPHONY_LOGE("interfaceManager nullptr error");
72 return;
73 }
74 interfaceManager->UpdateSimMessage(size, status, pdu, smsc);
75
76 auto smsMiscRunner = AppExecFwk::EventRunner::Create("SmsMiscRunner");
77 if (smsMiscRunner == nullptr) {
78 TELEPHONY_LOGE("failed to create SmsMiscRunner");
79 return;
80 }
81 std::shared_ptr<SmsMiscManager> smsMiscManager = std::make_shared<SmsMiscManager>(smsMiscRunner, slotId);
82 if (smsMiscManager == nullptr) {
83 TELEPHONY_LOGE("smsMiscManager nullptr error");
84 return;
85 }
86 smsMiscManager->UpdateSimMessage(size, status, pdu, smsc);
87 }
88
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)89 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
90 {
91 if (data == nullptr || size == 0) {
92 return;
93 }
94
95 UpdateSimMessage(data, size);
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