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 "abilityinterfacesappmgrappforegroundstateobserverstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #define private public
23 #include "app_foreground_state_observer_stub.h"
24 #include "app_foreground_state_observer_interface.h"
25 #undef private
26
27 #include "securec.h"
28 #include "parcel.h"
29 #include "ability_record.h"
30
31 using namespace OHOS::AAFwk;
32 using namespace OHOS::AppExecFwk;
33
34 namespace OHOS {
35 namespace {
36 constexpr size_t STRING_MAX_LENGTH = 128;
37 }
38 const std::u16string IA_APP_FOREGROUND_STATE_OBSERVER_TOKEN = u"ohos.appexecfwk.IAppForegroundStateObserver";
39
40 class AppForegroundStateObserverStubFUZZ : public AppForegroundStateObserverStub {
41 public:
AppForegroundStateObserverStubFUZZ()42 explicit AppForegroundStateObserverStubFUZZ() {};
~AppForegroundStateObserverStubFUZZ()43 virtual ~ AppForegroundStateObserverStubFUZZ() {};
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int OnRemoteRequest(
45 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override{ return 0; };
OnAppStateChanged(const AppStateData & appStateData)46 void OnAppStateChanged(const AppStateData &appStateData) override{};
47 };
48
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)49 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
50 {
51 std::shared_ptr<AppForegroundStateObserverStub> stub = std::make_shared<AppForegroundStateObserverStubFUZZ>();
52 uint32_t code;
53 MessageParcel parcel;
54 MessageParcel reply;
55 MessageOption option;
56 FuzzedDataProvider fdp(data, size);
57 parcel.WriteInterfaceToken(IA_APP_FOREGROUND_STATE_OBSERVER_TOKEN);
58 parcel.WriteString(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
59 parcel.WriteInt32(fdp.ConsumeIntegral<int32_t>());
60 parcel.RewindRead(0);
61 code = static_cast<uint32_t>(IAppForegroundStateObserver::Message::ON_APP_STATE_CHANGED);
62 stub->OnRemoteRequest(code, parcel, reply, option);
63 stub->HandleOnAppStateChanged(parcel, reply);
64 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>;
65 RemoteDiedHandler handler;
66 std::shared_ptr<AppForegroundStateObserverRecipient> infos =
67 std::make_shared<AppForegroundStateObserverRecipient>(handler);
68 wptr<IRemoteObject> remote;
69 infos->OnRemoteDied(remote);
70 return true;
71 }
72 }
73
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 // Run your code on data.
78 OHOS::DoSomethingInterestingWithMyAPI(data, size);
79 return 0;
80 }