1 /*
2 * Copyright (c) 2022 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 "ondeviceremoved_fuzzer.h"
17
18 #include <cstring>
19 #include <securec.h>
20
21 #include "mmi_adapter_impl.h"
22
23 using namespace OHOS::NWeb;
24
25 namespace OHOS {
26 class MMIListenerTest : public MMIListenerAdapter {
27 public:
28 MMIListenerTest() = default;
29 virtual ~MMIListenerTest() = default;
OnDeviceAdded(int32_t deviceId,const std::string & type)30 void OnDeviceAdded(int32_t deviceId, const std::string& type) override {};
OnDeviceRemoved(int32_t deviceId,const std::string & type)31 void OnDeviceRemoved(int32_t deviceId, const std::string& type) override {};
32 };
OnDeviceRemovedFuzzTest(const uint8_t * data,size_t size)33 bool OnDeviceRemovedFuzzTest(const uint8_t* data, size_t size)
34 {
35 if ((data == nullptr) || (size < sizeof(int32_t))) {
36 return false;
37 }
38 std::shared_ptr<MMIListenerAdapter> listener = std::make_shared<MMIListenerTest>();
39 auto listenerAdapterImpl = std::make_shared<MMIListenerAdapterImpl>(listener);
40 int32_t deviceId;
41 if (memcpy_s(&deviceId, sizeof(int32_t), data, sizeof(int32_t)) != 0) {
42 return false;
43 }
44 std::string type((const char*)data, size);
45 listenerAdapterImpl->OnDeviceRemoved(deviceId, type);
46 return true;
47 }
48 } // namespace OHOS
49
50 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)51 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
52 {
53 /* Run your code on data */
54 OHOS::OnDeviceRemovedFuzzTest(data, size);
55 return 0;
56 }
57