• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "displaydestroy_fuzzer.h"
17 
18 #include <cstring>
19 #include <securec.h>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #define private public
23 #include "display_manager_adapter_impl.h"
24 #undef private
25 #include "display_info.h"
26 
27 using namespace OHOS::NWeb;
28 using namespace OHOS::Rosen;
29 
30 namespace OHOS {
31 class DisplayListenerAdapterFuzzTest : public DisplayListenerAdapter {
32 public:
33 DisplayListenerAdapterFuzzTest() = default;
34 
35     virtual ~DisplayListenerAdapterFuzzTest() = default;
36 
OnCreate(DisplayId)37     void OnCreate(DisplayId) override {}
OnDestroy(DisplayId)38     void OnDestroy(DisplayId) override {}
OnChange(DisplayId)39     void OnChange(DisplayId) override {}
40 };
41 constexpr int MAX_SET_NUMBER = 1000;
42 
DisplayDestroyFuzzTest(const uint8_t * data,size_t size)43 bool DisplayDestroyFuzzTest(const uint8_t* data, size_t size)
44 {
45     if ((data == nullptr) || (size == 0)) {
46         return false;
47     }
48     FuzzedDataProvider dataProvider(data, size);
49     std::shared_ptr<DisplayListenerAdapter> listener = nullptr;
50     DisplayListenerAdapterImpl display(listener);
51     DisplayId displayId = dataProvider.ConsumeIntegralInRange<DisplayId>(0, MAX_SET_NUMBER);
52     display.OnCreate(displayId);
53     display.OnDestroy(displayId);
54     display.OnChange(displayId);
55     display.CheckOnlyRefreshRateDecreased(displayId);
56     std::shared_ptr<DisplayListenerAdapter> listener1
57         = std::make_shared<DisplayListenerAdapterFuzzTest>();
58     DisplayListenerAdapterImpl display1(listener1);
59     display1.OnCreate(displayId);
60     display1.OnDestroy(displayId);
61     display1.OnChange(displayId);
62     displayId = DisplayManager::GetInstance().GetDefaultDisplayId();
63     display1.OnChange(displayId);
64     auto displayPtr = DisplayManager::GetInstance().GetDefaultDisplay();
65     if (displayPtr == nullptr) {
66         return false;
67     }
68     auto displayInfo = displayPtr->GetDisplayInfo();
69     if (displayInfo == nullptr) {
70         return false;
71     }
72     display1.ConvertDisplayInfo(*displayInfo);
73     return true;
74 }
75 } // namespace OHOS
76 
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
79 {
80     /* Run your code on data */
81     OHOS::DisplayDestroyFuzzTest(data, size);
82     return 0;
83 }
84