1 /*
2 * Copyright (c) 2022-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 "publisher_fuzzer.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 #include <unistd.h>
24
25 #include "distributed_hardware_errno.h"
26 #include "publisher.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 namespace {
31 const uint32_t TOPIC_SIZE = 4;
32 const DHTopic topicFuzz[TOPIC_SIZE] = {
33 DHTopic::TOPIC_START_DSCREEN, DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO,
34 DHTopic::TOPIC_STOP_DSCREEN, DHTopic::TOPIC_DEV_OFFLINE
35 };
36 }
37
OnMessage(const DHTopic topic,const std::string & message)38 void MockPublisherListener::OnMessage(const DHTopic topic, const std::string &message)
39 {
40 (void)topic;
41 (void)message;
42 }
43
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t MockPublisherListener::OnRemoteRequest(
45 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
46 {
47 (void)code;
48 (void)data;
49 (void)reply;
50 (void)option;
51 return DH_FWK_SUCCESS;
52 }
53
PublisherListenerFuzzTest(const uint8_t * data,size_t size)54 void PublisherListenerFuzzTest(const uint8_t* data, size_t size)
55 {
56 if ((data == nullptr) || (size == 0)) {
57 return;
58 }
59
60 DHTopic topic = topicFuzz[data[0] % TOPIC_SIZE];
61 sptr<IPublisherListener> listener(new MockPublisherListener());
62 std::string message(reinterpret_cast<const char*>(data), size);
63
64 Publisher::GetInstance().RegisterListener(topic, listener);
65 Publisher::GetInstance().PublishMessage(topic, message);
66 Publisher::GetInstance().UnregisterListener(topic, listener);
67 }
68 } // namespace DistributedHardware
69 } // namespace OHOS
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
73 {
74 /* Run your code on data */
75 OHOS::DistributedHardware::PublisherListenerFuzzTest(data, size);
76 return 0;
77 }
78
79