1 /*
2 * Copyright (c) 2022-2023 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 "devicestatusagent_fuzzer.h"
17
18 #include "fi_log.h"
19
20 using namespace std;
21 using namespace OHOS;
22 using namespace OHOS::Msdp::DeviceStatus;
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, OHOS::Msdp::MSDP_DOMAIN_ID, "DeviceStatusAgentFuzzTest" };
25 constexpr int32_t WAIT_TIME { 1000 };
26 } // namespace
27
28 static std::shared_ptr<DevicestatusAgentFuzzer::DeviceStatusAgentClient> agentEvent_ =
29 std::make_shared<DevicestatusAgentFuzzer::DeviceStatusAgentClient>();
30 static std::shared_ptr<DeviceStatusAgent> agent_ = std::make_shared<DeviceStatusAgent>();
31
OnEventResult(const Data & devicestatusData)32 bool DevicestatusAgentFuzzer::DeviceStatusAgentClient::OnEventResult(
33 const Data& devicestatusData)
34 {
35 std::cout << "type: " << devicestatusData.type << std::endl;
36 std::cout << "value: " << devicestatusData.value << std::endl;
37 return true;
38 }
39
TestSubscribeAgentEvent(const uint8_t * data)40 void DevicestatusAgentFuzzer::TestSubscribeAgentEvent(const uint8_t* data)
41 {
42 std::cout << "TestSubscribeAgentEvent: Enter" << std::endl;
43 int32_t type[1] { -1 };
44 int32_t idSize = 4;
45 errno_t ret = memcpy_s(type, sizeof(type), data, idSize);
46 if (ret != EOK) {
47 FI_HILOGE("memcpy_s failed");
48 return;
49 }
50
51 agent_->SubscribeAgentEvent(static_cast<Type>(type[0]), ActivityEvent::ENTER_EXIT,
52 ReportLatencyNs::LONG, agentEvent_);
53
54 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
55 TestUnSubscribeAgentEvent(static_cast<Type>(type[0]));
56 }
57
TestUnSubscribeAgentEvent(Type type)58 void DevicestatusAgentFuzzer::TestUnSubscribeAgentEvent(Type type)
59 {
60 std::cout << "TestUnSubscribeAgentEvent: Enter" << std::endl;
61
62 agent_->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
63 }
64
TestSubscribeAgentEventIsNullptr(const uint8_t * data)65 void DevicestatusAgentFuzzer::TestSubscribeAgentEventIsNullptr(const uint8_t* data)
66 {
67 std::cout << "TestSubscribeAgentEventIsNullptr: Enter" << std::endl;
68 int32_t type[1] { -1 };
69 int32_t idSize = 4;
70 errno_t ret = memcpy_s(type, sizeof(type), data, idSize);
71 if (ret != EOK) {
72 FI_HILOGE("memcpy_s failed");
73 return;
74 }
75 agentEvent_ = nullptr;
76
77 agent_->SubscribeAgentEvent(static_cast<Type>(type[0]), ActivityEvent::ENTER_EXIT,
78 ReportLatencyNs::LONG, agentEvent_);
79
80 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
81 TestUnSubscribeAgentEvent(static_cast<Type>(type[0]));
82 }
83
TestSubscribeAgentEventTypeIsNullptr(const uint8_t * data)84 void DevicestatusAgentFuzzer::TestSubscribeAgentEventTypeIsNullptr(const uint8_t* data)
85 {
86 std::cout << "TestSubscribeAgentEventTypeIsNullptr: Enter" << std::endl;
87 int32_t type[1];
88 int32_t idSize = 4;
89 errno_t ret = memcpy_s(type, sizeof(type), data, idSize);
90 if (ret != EOK) {
91 FI_HILOGE("memcpy_s failed");
92 return;
93 }
94
95 agent_->SubscribeAgentEvent(static_cast<Type>(type[0]), ActivityEvent::ENTER_EXIT,
96 ReportLatencyNs::LONG, agentEvent_);
97
98 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
99 TestUnSubscribeAgentEventTypeIsNullptr(static_cast<Type>(type[0]));
100 }
101
TestUnSubscribeAgentEventTypeIsNullptr(Type type)102 void DevicestatusAgentFuzzer::TestUnSubscribeAgentEventTypeIsNullptr(Type type)
103 {
104 std::cout << "TestUnSubscribeAgentEventTypeIsNullptr: Enter" << std::endl;
105
106 agent_->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
107 }
108
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)109 bool DevicestatusAgentFuzzer::DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
110 {
111 size_t idSize = 8;
112 if (size > idSize) {
113 DevicestatusAgentFuzzer::TestSubscribeAgentEvent(data);
114 DevicestatusAgentFuzzer::TestSubscribeAgentEventIsNullptr(data);
115 DevicestatusAgentFuzzer::TestSubscribeAgentEventTypeIsNullptr(data);
116 }
117 return true;
118 }
119
120 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
122 {
123 /* Run your code on data */
124 OHOS::Msdp::DeviceStatus::DevicestatusAgentFuzzer::DoSomethingInterestingWithMyAPI(data, size);
125 return 0;
126 }
127