1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "devicestatusclient_fuzzer.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include <cstring>
19f857971dSopenharmony_ci#include <cstddef>
20f857971dSopenharmony_ci#include <cstdint>
21f857971dSopenharmony_ci#include "securec.h"
22f857971dSopenharmony_ci
23f857971dSopenharmony_ci#include "fi_log.h"
24f857971dSopenharmony_ci
25f857971dSopenharmony_ci#undef LOG_TAG
26f857971dSopenharmony_ci#define LOG_TAG "DeviceStatusClientFuzzTest"
27f857971dSopenharmony_ci
28f857971dSopenharmony_cinamespace OHOS {
29f857971dSopenharmony_cinamespace Msdp {
30f857971dSopenharmony_cinamespace DeviceStatus {
31f857971dSopenharmony_cinamespace {
32f857971dSopenharmony_ciconstexpr int32_t WAIT_TIME { 1000 };
33f857971dSopenharmony_ci
34f857971dSopenharmony_cibool DeviceStatusClientFuzzTest(const uint8_t* data, size_t size)
35f857971dSopenharmony_ci{
36f857971dSopenharmony_ci    int32_t idSize = 8;
37f857971dSopenharmony_ci    if (static_cast<int32_t>(size) > idSize) {
38f857971dSopenharmony_ci        DeviceStatusClientFuzzer::TestSubscribeCallback(data);
39f857971dSopenharmony_ci    }
40f857971dSopenharmony_ci    return true;
41f857971dSopenharmony_ci}
42f857971dSopenharmony_ci} // namespace
43f857971dSopenharmony_ci
44f857971dSopenharmony_ciauto stationaryMgr = StationaryManager::GetInstance();
45f857971dSopenharmony_cisptr<DeviceStatusClientFuzzer::DeviceStatusTestCallback> cb =
46f857971dSopenharmony_ci    new (std::nothrow) DeviceStatusClientFuzzer::DeviceStatusTestCallback();
47f857971dSopenharmony_civoid DeviceStatusClientFuzzer::DeviceStatusTestCallback::OnDeviceStatusChanged(const \
48f857971dSopenharmony_ci    Data& devicestatusData)
49f857971dSopenharmony_ci{
50f857971dSopenharmony_ci    std::cout << "DeviceStatusTestCallback type: " << devicestatusData.type << std::endl;
51f857971dSopenharmony_ci    std::cout << "DeviceStatusTestCallback value: " << devicestatusData.value << std::endl;
52f857971dSopenharmony_ci}
53f857971dSopenharmony_ci
54f857971dSopenharmony_civoid DeviceStatusClientFuzzer::TestSubscribeCallback(const uint8_t* data)
55f857971dSopenharmony_ci{
56f857971dSopenharmony_ci    std::cout << "TestSubscribeCallback: Enter" << std::endl;
57f857971dSopenharmony_ci    int32_t type[1];
58f857971dSopenharmony_ci    int32_t idSize = 4;
59f857971dSopenharmony_ci    errno_t ret = memcpy_s(type, sizeof(type), data, idSize);
60f857971dSopenharmony_ci    if (ret != EOK) {
61f857971dSopenharmony_ci        FI_HILOGE("memcpy_s failed");
62f857971dSopenharmony_ci        return;
63f857971dSopenharmony_ci    }
64f857971dSopenharmony_ci
65f857971dSopenharmony_ci    stationaryMgr->SubscribeCallback(static_cast<Type>(type[0]), ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG, cb);
66f857971dSopenharmony_ci
67f857971dSopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
68f857971dSopenharmony_ci    TestGetDevicestatusData(static_cast<Type>(type[0]));
69f857971dSopenharmony_ci}
70f857971dSopenharmony_ci
71f857971dSopenharmony_civoid DeviceStatusClientFuzzer::TestGetDevicestatusData(Type type)
72f857971dSopenharmony_ci{
73f857971dSopenharmony_ci    std::cout << "TestGetDevicestatusData: Enter" << std::endl;
74f857971dSopenharmony_ci    stationaryMgr->GetDeviceStatusData(type);
75f857971dSopenharmony_ci
76f857971dSopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
77f857971dSopenharmony_ci    TestUnSubscribeCallback(type);
78f857971dSopenharmony_ci}
79f857971dSopenharmony_ci
80f857971dSopenharmony_civoid DeviceStatusClientFuzzer::TestUnSubscribeCallback(Type type)
81f857971dSopenharmony_ci{
82f857971dSopenharmony_ci    std::cout << "TestUnSubscribeCallback: Enter" << std::endl;
83f857971dSopenharmony_ci
84f857971dSopenharmony_ci    stationaryMgr->UnsubscribeCallback(type, ActivityEvent::ENTER_EXIT, cb);
85f857971dSopenharmony_ci}
86f857971dSopenharmony_ci
87f857971dSopenharmony_ci/* Fuzzer entry point */
88f857971dSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89f857971dSopenharmony_ci{
90f857971dSopenharmony_ci    /* Run your code on data */
91f857971dSopenharmony_ci    DeviceStatusClientFuzzTest(data, size);
92f857971dSopenharmony_ci    return 0;
93f857971dSopenharmony_ci}
94f857971dSopenharmony_ci} // namespace DeviceStatus
95f857971dSopenharmony_ci} // namespace Msdp
96f857971dSopenharmony_ci} // namespace OHOS