1 /*
2 * Copyright (c) 2021 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 "devicestatusclient_fuzzer.h"
17
18 #include <cstring>
19 #include <cstddef>
20 #include <cstdint>
21 #include "securec.h"
22
23 #include "fi_log.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "DeviceStatusClientFuzzTest"
27
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 namespace {
32 constexpr int32_t WAIT_TIME { 1000 };
33
DeviceStatusClientFuzzTest(const uint8_t * data,size_t size)34 bool DeviceStatusClientFuzzTest(const uint8_t* data, size_t size)
35 {
36 int32_t idSize = 8;
37 if (static_cast<int32_t>(size) > idSize) {
38 DeviceStatusClientFuzzer::TestSubscribeCallback(data);
39 }
40 return true;
41 }
42 } // namespace
43
44 auto stationaryMgr = StationaryManager::GetInstance();
45 sptr<DeviceStatusClientFuzzer::DeviceStatusTestCallback> cb =
46 new (std::nothrow) DeviceStatusClientFuzzer::DeviceStatusTestCallback();
OnDeviceStatusChanged(const Data & devicestatusData)47 void DeviceStatusClientFuzzer::DeviceStatusTestCallback::OnDeviceStatusChanged(const \
48 Data& devicestatusData)
49 {
50 std::cout << "DeviceStatusTestCallback type: " << devicestatusData.type << std::endl;
51 std::cout << "DeviceStatusTestCallback value: " << devicestatusData.value << std::endl;
52 }
53
TestSubscribeCallback(const uint8_t * data)54 void DeviceStatusClientFuzzer::TestSubscribeCallback(const uint8_t* data)
55 {
56 std::cout << "TestSubscribeCallback: Enter" << std::endl;
57 int32_t type[1];
58 int32_t idSize = 4;
59 errno_t ret = memcpy_s(type, sizeof(type), data, idSize);
60 if (ret != EOK) {
61 FI_HILOGE("memcpy_s failed");
62 return;
63 }
64
65 stationaryMgr->SubscribeCallback(static_cast<Type>(type[0]), ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, cb);
66
67 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
68 TestGetDevicestatusData(static_cast<Type>(type[0]));
69 }
70
TestGetDevicestatusData(Type type)71 void DeviceStatusClientFuzzer::TestGetDevicestatusData(Type type)
72 {
73 std::cout << "TestGetDevicestatusData: Enter" << std::endl;
74 stationaryMgr->GetDeviceStatusData(type);
75
76 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
77 TestUnSubscribeCallback(type);
78 }
79
TestUnSubscribeCallback(Type type)80 void DeviceStatusClientFuzzer::TestUnSubscribeCallback(Type type)
81 {
82 std::cout << "TestUnSubscribeCallback: Enter" << std::endl;
83
84 stationaryMgr->UnsubscribeCallback(type, ActivityEvent::ENTER_EXIT, cb);
85 }
86
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90 /* Run your code on data */
91 DeviceStatusClientFuzzTest(data, size);
92 return 0;
93 }
94 } // namespace DeviceStatus
95 } // namespace Msdp
96 } // namespace OHOS