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