1 /*
2 * Copyright (c) 2024-2025 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 "abilityinterfacesappmgrrenderstateobserverstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #define private public
23 #include "render_state_observer_stub.h"
24 #undef private
25
26 #include "securec.h"
27 #include "parcel.h"
28 #include "ability_record.h"
29
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32
33 namespace OHOS {
34 namespace {
35 constexpr size_t STRING_MAX_LENGTH = 128;
36 }
37 const std::u16string IR_ENDER_STATE_OBSERVER_TOKEN = u"ohos.appexecfwk.IRenderStateObserver";
38 class RenderStateObserverStubFUZZ : public RenderStateObserverStub {
39 public:
RenderStateObserverStubFUZZ()40 explicit RenderStateObserverStubFUZZ() {};
~RenderStateObserverStubFUZZ()41 virtual ~ RenderStateObserverStubFUZZ() {};
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int OnRemoteRequest(
43 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override{ return 0; };
OnRenderStateChanged(const RenderStateData & renderStateData)44 void OnRenderStateChanged(const RenderStateData &renderStateData) override{};
45 };
46
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)47 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
48 {
49 std::shared_ptr<RenderStateObserverStub> stub = std::make_shared<RenderStateObserverStubFUZZ>();
50 uint32_t code;
51 MessageParcel parcel;
52 MessageParcel reply;
53 MessageOption option;
54 FuzzedDataProvider fdp(data, size);
55 parcel.WriteInterfaceToken(IR_ENDER_STATE_OBSERVER_TOKEN);
56 parcel.WriteString(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
57 parcel.WriteInt32(fdp.ConsumeIntegral<int32_t>());
58 parcel.RewindRead(0);
59 stub->OnRenderStateChangedInner(parcel, reply);
60 code = static_cast<uint32_t>(IRenderStateObserver::ON_RENDER_STATE_CHANGED);
61 stub->OnRemoteRequest(code, parcel, reply, option);
62 return true;
63 }
64 }
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68 {
69 // Run your code on data.
70 OHOS::DoSomethingInterestingWithMyAPI(data, size);
71 return 0;
72 }