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