1 /*
2  * Copyright (c) 2022-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 <algorithm>
17 #include <thread>
18 #include <unistd.h>
19 
20 #include "ipc_client_stub.h"
21 #include "dm_device_info.h"
22 #include "ipc_server_stub.h"
23 #include "device_manager_impl.h"
24 #include "dm_constants.h"
25 #include "if_system_ability_manager.h"
26 #include "ipc_cmd_register.h"
27 #include "ipc_remote_broker.h"
28 #include "ipc_skeleton.h"
29 #include "ipc_types.h"
30 #include "iservice_registry.h"
31 #include "string_ex.h"
32 #include "system_ability_definition.h"
33 #include "ipc_server_stub_fuzzer.h"
34 #include "iremote_object.h"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
IpcServerStubFuzzTest(const uint8_t * data,size_t size)38 void IpcServerStubFuzzTest(const uint8_t* data, size_t size)
39 {
40     if ((data == nullptr) || (size < sizeof(uint32_t))) {
41         return;
42     }
43     uint32_t code = *(reinterpret_cast<const uint32_t*>(data));
44     MessageParcel data1;
45     MessageParcel reply;
46     MessageOption option;
47     std::string pkgName(reinterpret_cast<const char*>(data), size);
48     sptr<IpcRemoteBroker> listener = sptr<IpcServerStub>(new IpcServerStub());
49     std::shared_ptr<IpcReq> req = nullptr;
50     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
51 
52     IpcServerStub::GetInstance().Init();
53     IpcServerStub::GetInstance().OnRemoteRequest(code, data1, reply, option);
54     IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
55     IpcServerStub::GetInstance().GetDmListener(pkgName);
56     IpcServerStub::GetInstance().SendCmd(code, req, rsp);
57     IpcServerStub::GetInstance().GetAllPkgName();
58     IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
59 }
60 }
61 }
62 
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66     /* Run your code on data */
67     OHOS::DistributedHardware::IpcServerStubFuzzTest(data, size);
68     return 0;
69 }