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 "distributed_communication_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include "addcalltoken_fuzzer.h"
21 #include "ims_call.h"
22 #include "distributed_data_sink_controller.h"
23 #include "distributed_data_source_controller.h"
24
25 using namespace OHOS::Telephony;
26 namespace OHOS {
TestCommonController(const std::shared_ptr<DistributedDataController> &controller, const uint8_t *data, size_t size)27 void TestCommonController(const std::shared_ptr<DistributedDataController> &controller, const uint8_t *data,
28 size_t size)
29 {
30 std::string stringValue(reinterpret_cast<const char *>(data), size);
31 int32_t intValue = static_cast<int32_t>(size);
32 AudioDeviceType audioDevType = static_cast<AudioDeviceType>(
33 size % static_cast<uint32_t>(AudioDeviceType::DEVICE_DISTRIBUTED_PC));
34 const char *charValue = reinterpret_cast<const char *>(data);
35 uint32_t uintValue = static_cast<uint32_t>(size);
36 DialParaInfo dialParaInfo;
37 sptr<OHOS::Telephony::CallBase> call = new IMSCall(dialParaInfo);
38 DistributedDataType distributedDataType = static_cast<DistributedDataType>(
39 size % static_cast<uint32_t>(DistributedDataType::MAX));
40 DistributedMsgType msgType = static_cast<DistributedMsgType>(
41 size % static_cast<uint32_t>(DistributedMsgType::CURRENT_DATA_RSP));
42 controller->OnDistributedAudioDeviceChange(stringValue, stringValue, audioDevType, intValue);
43 controller->OnRemoveSystemAbility();
44 controller->OnReceiveMsg(charValue, uintValue);
45 controller->OnDeviceOnline(stringValue, stringValue, audioDevType);
46 controller->OnDeviceOffline(stringValue, stringValue, audioDevType);
47 controller->OnCallCreated(call, stringValue);
48 controller->OnCallDestroyed();
49 controller->ProcessCallInfo(call, distributedDataType);
50 controller->OnConnected();
51 controller->SetMuted(true);
52 controller->MuteRinger();
53
54 controller->HandleRecvMsg(intValue, nullptr);
55 std::string json = "{ \"dataType\": 101 }";
56 cJSON *msg = cJSON_Parse(json.c_str());
57 controller->HandleRecvMsg(intValue, msg);
58 int32_t jsonIntValue;
59 controller->GetInt32Value(msg, stringValue, jsonIntValue);
60 std::string jsonStringValue;
61 controller->GetStringValue(msg, stringValue, jsonStringValue);
62 bool jsonBoolValue;
63 controller->GetBoolValue(msg, stringValue, jsonBoolValue);
64 controller->HandleMuted(msg);
65 controller->CreateMuteMsg(msgType, true);
66 controller->CreateMuteRingerMsg(msgType);
67 controller->HandleMuteRinger();
68 cJSON_Delete(msg);
69 }
70
TestSinkController(const uint8_t *data, size_t size)71 void TestSinkController(const uint8_t *data, size_t size)
72 {
73 if (!IsServiceInited()) {
74 return;
75 }
76 if (data == nullptr) {
77 return;
78 }
79 auto controller = std::make_shared<DistributedDataSinkController>();
80 if (controller == nullptr) {
81 return;
82 }
83 TestCommonController(controller, data, size);
84
85 std::string stringValue(reinterpret_cast<const char *>(data), size);
86 int32_t intValue = static_cast<int32_t>(size);
87 DistributedDataType distributedDataType = static_cast<DistributedDataType>(
88 size % static_cast<uint32_t>(DistributedDataType::MAX));
89 DistributedMsgType msgType = static_cast<DistributedMsgType>(
90 size % static_cast<uint32_t>(DistributedMsgType::CURRENT_DATA_RSP));
91 uint32_t uintValue = static_cast<uint32_t>(size);
92 DialParaInfo dialParaInfo;
93 sptr<OHOS::Telephony::CallBase> call = new IMSCall(dialParaInfo);
94 controller->ConnectRemote(stringValue);
95 controller->CheckLocalData(call, distributedDataType);
96 controller->CreateDataReqMsg(msgType, uintValue, stringValue);
97 controller->SendDataQueryReq();
98 controller->ReportCallInfo(call);
99 controller->CreateCurrentDataReqMsg(stringValue);
100
101 cJSON *msg = cJSON_Parse("{ \"itemType\": 0, \"num\": \"123456\", \"name\": \"test\", \"location\": \"test\" }");
102 controller->HandleDataQueryRsp(msg);
103 controller->UpdateCallName(call, msg);
104 controller->UpdateCallLocation(call, msg);
105 controller->HandleCurrentDataQueryRsp(msg);
106 cJSON_Delete(msg);
107 }
108
TestSourceController(const uint8_t *data, size_t size)109 void TestSourceController(const uint8_t *data, size_t size)
110 {
111 if (!IsServiceInited()) {
112 return;
113 }
114 if (data == nullptr) {
115 return;
116 }
117 auto controller = std::make_shared<DistributedDataSourceController>();
118 if (controller == nullptr) {
119 return;
120 }
121 TestCommonController(controller, data, size);
122
123 std::string stringValue(reinterpret_cast<const char *>(data), size);
124 DistributedDataType distributedDataType = static_cast<DistributedDataType>(
125 size % static_cast<uint32_t>(DistributedDataType::MAX));
126 DialParaInfo dialParaInfo;
127 sptr<OHOS::Telephony::CallBase> call = new IMSCall(dialParaInfo);
128 DistributedMsgType msgType = static_cast<DistributedMsgType>(
129 size % static_cast<uint32_t>(DistributedMsgType::CURRENT_DATA_RSP));
130 uint32_t uintValue = static_cast<uint32_t>(size);
131 int32_t intValue = static_cast<int32_t>(size);
132 controller->SaveLocalData(stringValue, distributedDataType, stringValue);
133 controller->SaveLocalData(call, distributedDataType);
134 controller->CreateDataRspMsg(msgType, uintValue, stringValue, stringValue, stringValue);
135 controller->SendLocalDataRsp();
136 controller->CreateCurrentDataRspMsg(stringValue, true, intValue);
137
138 cJSON *msg = cJSON_Parse("{ \"itemType\": 0, \"num\": \"123456\" }");
139 controller->HandleDataQueryMsg(msg);
140 controller->HandleCurrentDataQueryMsg((msg));
141 cJSON_Delete(msg);
142 }
143
DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)144 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
145 {
146 if (data == nullptr || size == 0) {
147 return;
148 }
149
150 TestSinkController(data, size);
151 TestSourceController(data, size);
152 }
153 } // namespace OHOS
154
155 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)156 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
157 {
158 OHOS::AddCallTokenFuzzer token;
159 /* Run your code on data */
160 OHOS::DoSomethingInterestingWithMyAPI(data, size);
161 return 0;
162 }
163