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