diff --git a/ipc/test/ipc/server/ipc_test_server.c b/ipc/test/ipc/server/ipc_test_server.c index d740bf959dab200474a61cbde0b838c90e36c430..6a3bd3ffda241df3089707e1e67229d7a6ea0be8 100644 --- a/ipc/test/ipc/server/ipc_test_server.c +++ b/ipc/test/ipc/server/ipc_test_server.c @@ -17,6 +17,8 @@ #include #include + + #include "ipc_proxy.h" #include "ipc_skeleton.h" #include "iproxy_server.h" diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 24fa5ec73702056f063aae0979ab26c7f9775928..272ce37083d2630beca746bea62bf38c1ea72536 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -21,6 +21,7 @@ group("fuzztest") { "ipc/native/src/core/binderinvokernew_fuzzer:BinderInvokerNewFuzzTest", "ipc/native/src/core/bufferobject_fuzzer:BufferObjectFuzzTest", "ipc/native/src/core/databussocketlistener_fuzzer:DataBusSocketListenerFuzzTest", + "ipc/native/src/core/databussocketlistener_fuzzer:DataBusSocketListenerMockFuzzTest", "ipc/native/src/core/dbindercallbackstub_fuzzer:DBinderCallBackStubFuzzTest", "ipc/native/src/core/dbinderdatabusinvoker_fuzzer:DBinderDatabusInvokerFuzzTest", "ipc/native/src/core/dbindergrantpermission_fuzzer:DBinderGrantPermissionFuzzTest", diff --git a/test/fuzztest/ipc/native/src/core/databussocketlistener_fuzzer/databussocketlistener_fuzzer.cpp b/test/fuzztest/ipc/native/src/core/databussocketlistener_fuzzer/databussocketlistener_fuzzer.cpp index 6f310c74c742ae85af06d7f0cbe58936f70e0b32..d080462d7dafcd76fb998f216e140e8a140b17f7 100644 --- a/test/fuzztest/ipc/native/src/core/databussocketlistener_fuzzer/databussocketlistener_fuzzer.cpp +++ b/test/fuzztest/ipc/native/src/core/databussocketlistener_fuzzer/databussocketlistener_fuzzer.cpp @@ -14,7 +14,7 @@ */ #include "databussocketlistener_fuzzer.h" - +#include #define private public #define protected public #include "databus_socket_listener.h" @@ -25,6 +25,7 @@ using OHOS::DatabusSocketListener; namespace OHOS { +static constexpr size_t STR_MAX_LEN = 100; static std::string TEST_SOCKET_NAME = "DBinder1_1"; static std::string TEST_SOCKET_PEER_NAME = "DBinderService"; static std::string TEST_SOCKET_PEER_NETWORKID = "wad213hkad213jh123jk213j1h2312h3jk12dadadeawd721hledhjlad22djhla"; @@ -240,6 +241,88 @@ static void EraseDeviceLockFuzzTest(const uint8_t *data, size_t size) listener->EraseDeviceLock(info3); listener->RemoveSessionName(); } + +static void ServerOnShutdownFuzzTest(FuzzedDataProvider &provider) +{ + int32_t socketId = provider.ConsumeIntegral(); + int32_t reason = provider.ConsumeIntegral(); + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->ServerOnShutdown(socketId, static_cast(reason)); +} + +static void ClientOnBindFuzzTest(FuzzedDataProvider &provider) +{ + int32_t socketId = provider.ConsumeIntegral(); + std::string name = provider.ConsumeRandomLengthString(STR_MAX_LEN); + std::string networkId = provider.ConsumeRandomLengthString(STR_MAX_LEN); + std::string pkgName = provider.ConsumeRandomLengthString(STR_MAX_LEN); + int32_t dataType = provider.ConsumeIntegral(); + PeerSocketInfo info = { + .name = const_cast(name.c_str()), + .networkId = const_cast(networkId.c_str()), + .pkgName = const_cast(pkgName.c_str()), + .dataType = static_cast(dataType), + }; + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->ClientOnBind(socketId, info); +} + +static void ClientOnShutdownFuzzTest(FuzzedDataProvider &provider) +{ + int32_t socketId = provider.ConsumeIntegral(); + int32_t reason = provider.ConsumeIntegral(); + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->ClientOnShutdown(socketId, static_cast(reason)); + + std::string ownName = provider.ConsumeRandomLengthString(STR_MAX_LEN); + std::string peerName = provider.ConsumeRandomLengthString(STR_MAX_LEN); + std::string networkId = provider.ConsumeRandomLengthString(STR_MAX_LEN); + listener->CreateClientSocket(ownName, peerName, networkId); + listener->ClientOnShutdown(socketId, static_cast(reason)); +} + +static void OnBytesReceivedFuzzTest(FuzzedDataProvider &provider) +{ + int32_t socketId = provider.ConsumeIntegral(); + std::string recivedData = provider.ConsumeRandomLengthString(STR_MAX_LEN); + uint32_t recivedLen = recivedData.size(); + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->OnBytesReceived(socketId, static_cast(recivedData.data()), recivedLen); +} + +static void ShutdownSocketFuzzTest(FuzzedDataProvider &provider) +{ + int32_t socketId = provider.ConsumeIntegral(); + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->ShutdownSocket(socketId); +} + +static void GetPidAndUidFromServiceNameFuzzTest(FuzzedDataProvider &provider) +{ + std::string serviceName = provider.ConsumeRandomLengthString(STR_MAX_LEN); + int32_t pid = provider.ConsumeIntegral(); + int32_t uid = provider.ConsumeIntegral(); + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->GetPidAndUidFromServiceName(serviceName, pid, uid); +} } // namespace OHOS /* Fuzzer entry point */ @@ -256,5 +339,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::CreateClientSocketFuzzTest(data, size); OHOS::ShutdownSocketFuzzTest(data, size); OHOS::EraseDeviceLockFuzzTest(data, size); + + FuzzedDataProvider provider(data, size); + OHOS::ServerOnShutdownFuzzTest(provider); + OHOS::ClientOnBindFuzzTest(provider); + OHOS::ClientOnShutdownFuzzTest(provider); + OHOS::OnBytesReceivedFuzzTest(provider); + OHOS::ShutdownSocketFuzzTest(provider); + OHOS::GetPidAndUidFromServiceNameFuzzTest(provider); return 0; } diff --git a/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/BUILD.gn b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..126379e7bc83f8f5e919757d6d74000f9b1d0598 --- /dev/null +++ b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/BUILD.gn @@ -0,0 +1,41 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DataBusSocketListenerMockFuzzTest") { + module_out_path = "ipc/ipc" + fuzz_config_file = "../databussocketlistenermock_fuzzer" + + include_dirs = [ "../../../../../../../utils/include" ] + cflags = [ + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "databussocketlistenermock_fuzzer.cpp" ] + + deps = [ + "../../../../../../../interfaces/innerkits/ipc_single:ipc_single_test", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "hitrace:libhitracechain", + "googletest:gmock", + "googletest:gtest", + ] +} diff --git a/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/corpus/init b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/databussocketlistenermock_fuzzer.cpp b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/databussocketlistenermock_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b86465117d1ae5576ae81f74a3372d806daf179e --- /dev/null +++ b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/databussocketlistenermock_fuzzer.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "databussocketlistenermock_fuzzer.h" + +#include +#include +#include +#include +#include + +#include "databus_socket_listener.h" +#include "dbinder_softbus_client.h" +#include "string_ex.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +const static size_t MAX_STRING_PARAM_LEN = 100; + +class databussocketlistenerInterface { +public: + databussocketlistenerInterface() {}; + virtual ~databussocketlistenerInterface() {}; + + virtual int32_t Socket(SocketInfo info) = 0; + virtual int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, + const ISocketListener *listener) = 0; +}; + +class databussocketlistenerInterfaceMock : public databussocketlistenerInterface { +public: + databussocketlistenerInterfaceMock(); + ~databussocketlistenerInterfaceMock() override; + MOCK_METHOD1(Socket, int32_t(SocketInfo)); + MOCK_METHOD4(Listen, int32_t(int32_t, const QosTV*, uint32_t, const ISocketListener *)); +}; + +static void *g_interface = nullptr; + +databussocketlistenerInterfaceMock::databussocketlistenerInterfaceMock() +{ + g_interface = reinterpret_cast(this); +} + +databussocketlistenerInterfaceMock::~databussocketlistenerInterfaceMock() +{ + g_interface = nullptr; +} + +static databussocketlistenerInterface *GetdatabussocketlistenerInterface() +{ + return reinterpret_cast(g_interface); +} + +extern "C" { + int32_t DBinderSoftbusClient::Socket(SocketInfo info) + { + if (g_interface == nullptr) { + return false; + } + return GetdatabussocketlistenerInterface()->Socket(info); + } + + int32_t DBinderSoftbusClient::Listen( + int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener) + { + if (g_interface == nullptr) { + return false; + } + return GetdatabussocketlistenerInterface()->Listen(socket, qos, qosCount, listener); + } +} + +static void CreateClientSocketFuzzTest(FuzzedDataProvider &provider) +{ + std::string ownName = provider.ConsumeRandomLengthString(MAX_STRING_PARAM_LEN); + std::string peerName = provider.ConsumeRandomLengthString(MAX_STRING_PARAM_LEN); + std::string networkId = provider.ConsumeRandomLengthString(MAX_STRING_PARAM_LEN); + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->CreateClientSocket(ownName, peerName, networkId); + + NiceMock mockClient; + EXPECT_CALL(mockClient, Socket).WillRepeatedly(testing::Return(1)); + listener->CreateClientSocket(ownName, peerName, networkId); +} + +static void StartServerListenerFuzzTest(FuzzedDataProvider &provider) +{ + std::string ownName = provider.ConsumeRandomLengthString(MAX_STRING_PARAM_LEN); + std::shared_ptr listener = DelayedSingleton::GetInstance(); + if (listener == nullptr) { + return; + } + listener->StartServerListener(ownName); + + NiceMock mockClient; + EXPECT_CALL(mockClient, Socket).WillRepeatedly(testing::Return(1)); + listener->StartServerListener(ownName); +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider provider(data, size); + OHOS::CreateClientSocketFuzzTest(provider); + OHOS::StartServerListenerFuzzTest(provider); + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/databussocketlistenermock_fuzzer.h b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/databussocketlistenermock_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..f1d540eb141fb2f433900f8804cb997de8c05f4c --- /dev/null +++ b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/databussocketlistenermock_fuzzer.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef databussocketlistenermock_fuzzer_H +#define databussocketlistenermock_fuzzer_H + +#include +#include +#include +#include +#include +#include + +#define FUZZ_PROJECT_NAME "databussocketlistenermock_fuzzer" + +#endif // databussocketlistenermock_fuzzer_H \ No newline at end of file diff --git a/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/project.xml b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..66e1dcac475475fb101b6f8670ec699e6e9696aa --- /dev/null +++ b/test/fuzztest/ipc/native/src/core/databussocketlistenermock_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +