1 /*
2  * Copyright (c) 2024 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 <cstddef>
17 #include <cstdint>
18 #include <parcel.h>
19 #include <securec.h>
20 
21 #include <iremote_stub.h>
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "marshalling_helper.h"
25 #include "window.h"
26 #include "window_agent.h"
27 #include "window_impl.h"
28 #include "window_manager.h"
29 #include "windowstubupdate_fuzzer.h"
30 
31 using namespace OHOS::Rosen;
32 
33 namespace OHOS {
34 namespace {
35 constexpr size_t DATA_MIN_SIZE = 2;
36 }
37 
WindowStubUpdateTest(sptr<WindowAgent> windowStub,MessageParcel & parcel)38 void WindowStubUpdateTest(sptr<WindowAgent> windowStub, MessageParcel& parcel)
39 {
40     MessageParcel reply;
41     MessageOption option;
42     parcel.RewindRead(0);
43     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_WINDOW_RECT),
44         parcel, reply, option);
45     parcel.RewindRead(0);
46     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_WINDOW_MODE),
47         parcel, reply, option);
48     parcel.RewindRead(0);
49     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_FOCUS_STATUS),
50         parcel, reply, option);
51     parcel.RewindRead(0);
52     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_MODE_SUPPORT_INFO),
53         parcel, reply, option);
54     parcel.RewindRead(0);
55     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_AVOID_AREA),
56         parcel, reply, option);
57     parcel.RewindRead(0);
58     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_WINDOW_STATE),
59         parcel, reply, option);
60     parcel.RewindRead(0);
61     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_DRAG_EVENT),
62         parcel, reply, option);
63     parcel.RewindRead(0);
64     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_DISPLAY_ID),
65         parcel, reply, option);
66     parcel.RewindRead(0);
67     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_OCCUPIED_AREA),
68         parcel, reply, option);
69     parcel.RewindRead(0);
70     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_OCCUPIED_AREA_AND_RECT),
71         parcel, reply, option);
72     parcel.RewindRead(0);
73     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_ACTIVE_STATUS),
74         parcel, reply, option);
75     parcel.RewindRead(0);
76     windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_UPDATE_ZOOM_TRANSFORM),
77         parcel, reply, option);
78 }
79 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)80 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
81 {
82     if (data == nullptr || size < DATA_MIN_SIZE) {
83         return false;
84     }
85 
86     MessageParcel parcel;
87     MessageParcel reply;
88     MessageOption option;
89 
90     parcel.WriteInterfaceToken(WindowStub::GetDescriptor());
91     parcel.WriteBuffer(data, size);
92 
93     sptr<WindowOption> windowOption = new(std::nothrow)WindowOption();
94     if (windowOption == nullptr) {
95         return false;
96     }
97     sptr<WindowImpl> window = new(std::nothrow)WindowImpl(windowOption);
98     if (window == nullptr) {
99         return false;
100     }
101     sptr<WindowAgent> windowStub = new(std::nothrow)WindowAgent(window);
102     if (windowStub == nullptr) {
103         return false;
104     }
105 
106     WindowStubUpdateTest(windowStub, parcel);
107     return true;
108 }
109 } // namespace.OHOS
110 
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114     /* Run your code on data */
115     OHOS::DoSomethingInterestingWithMyAPI(data, size);
116     return 0;
117 }