From bb49676cfd969c87fe1741faf45710a03ec3c535 Mon Sep 17 00:00:00 2001 From: fanzhe Date: Thu, 28 Aug 2025 18:16:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85FUZZ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fanzhe Change-Id: Ib6f8a3aae17c17c0fde62f6f6447efb21a597339 --- test/fuzztest/BUILD.gn | 2 +- .../src/mock/ipcobjectstubmock/BUILD.gn | 23 ++ .../include}/ipcobjectstubmock_fuzzer.h | 0 .../ipcobjectstubmock001_fuzzer}/BUILD.gn | 12 +- .../ipcobjectstubmock001_fuzzer}/corpus/init | 0 .../ipcobjectstubmock001_fuzzer.cpp | 172 +++++++++++ .../ipcobjectstubmock001_fuzzer}/project.xml | 0 .../ipcobjectstubmock002_fuzzer/BUILD.gn | 39 +++ .../ipcobjectstubmock002_fuzzer/corpus/init | 16 + .../ipcobjectstubmock002_fuzzer.cpp | 169 +++++++++++ .../ipcobjectstubmock002_fuzzer/project.xml | 25 ++ .../ipcobjectstubmock003_fuzzer/BUILD.gn | 39 +++ .../ipcobjectstubmock003_fuzzer/corpus/init | 16 + .../ipcobjectstubmock003_fuzzer.cpp | 173 +++++++++++ .../ipcobjectstubmock003_fuzzer/project.xml | 25 ++ .../ipcobjectstubmock004_fuzzer/BUILD.gn | 39 +++ .../ipcobjectstubmock004_fuzzer/corpus/init | 16 + .../ipcobjectstubmock004_fuzzer.cpp} | 286 ------------------ .../ipcobjectstubmock004_fuzzer/project.xml | 25 ++ .../ipcobjectstubmock005_fuzzer/BUILD.gn | 39 +++ .../ipcobjectstubmock005_fuzzer/corpus/init | 16 + .../ipcobjectstubmock005_fuzzer.cpp | 131 ++++++++ .../ipcobjectstubmock005_fuzzer/project.xml | 25 ++ test/resource/ipc/ohos_test.xml | 30 ++ 24 files changed, 1026 insertions(+), 292 deletions(-) create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/BUILD.gn rename test/fuzztest/ipc/native/src/mock/{ipcobjectstubmock_fuzzer => ipcobjectstubmock/include}/ipcobjectstubmock_fuzzer.h (100%) rename test/fuzztest/ipc/native/src/mock/{ipcobjectstubmock_fuzzer => ipcobjectstubmock/ipcobjectstubmock001_fuzzer}/BUILD.gn (71%) rename test/fuzztest/ipc/native/src/mock/{ipcobjectstubmock_fuzzer => ipcobjectstubmock/ipcobjectstubmock001_fuzzer}/corpus/init (100%) create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/ipcobjectstubmock001_fuzzer.cpp rename test/fuzztest/ipc/native/src/mock/{ipcobjectstubmock_fuzzer => ipcobjectstubmock/ipcobjectstubmock001_fuzzer}/project.xml (100%) create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/BUILD.gn create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/corpus/init create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/ipcobjectstubmock002_fuzzer.cpp create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/project.xml create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/BUILD.gn create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/corpus/init create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/ipcobjectstubmock003_fuzzer.cpp create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/project.xml create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/BUILD.gn create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/corpus/init rename test/fuzztest/ipc/native/src/mock/{ipcobjectstubmock_fuzzer/ipcobjectstubmock_fuzzer.cpp => ipcobjectstubmock/ipcobjectstubmock004_fuzzer/ipcobjectstubmock004_fuzzer.cpp} (40%) create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/project.xml create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/BUILD.gn create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/corpus/init create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/ipcobjectstubmock005_fuzzer.cpp create mode 100644 test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/project.xml diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index f64a1e4b..8468e7fb 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -37,7 +37,7 @@ group("fuzztest") { "ipc/native/src/core/ipcfiledescriptorunmarshalling_fuzzer:IPCFileDescriptorUnmarshallingFuzzTest", "ipc/native/src/core/ipcobjectproxy_fuzzer:IPCObjectProxyFuzzTest", "ipc/native/src/core/ipcobjectstub_fuzzer:IPCObjectStubFuzzTest", - "ipc/native/src/mock/ipcobjectstubmock_fuzzer:IPCObjectStubMockFuzzTest", + "ipc/native/src/mock/ipcobjectstubmock:ipcobjectstubmockfuzz", "ipc/native/src/core/ipcpayloadstatistics_fuzzer:IPCPayloadStatisticsFuzzTest", "ipc/native/src/core/ipcprocessskeleton_fuzzer:IPCProcessSkeletonFuzzTest", "ipc/native/src/core/ipcskeleton_fuzzer:IPCSkeletonFuzzTest", diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/BUILD.gn b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/BUILD.gn new file mode 100644 index 00000000..9cbc6364 --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/BUILD.gn @@ -0,0 +1,23 @@ +# 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. + +group("ipcobjectstubmockfuzz") { + testonly = true + deps = [ + "ipcobjectstubmock001_fuzzer:IPCObjectStubMock001FuzzTest", + "ipcobjectstubmock002_fuzzer:IPCObjectStubMock002FuzzTest", + "ipcobjectstubmock003_fuzzer:IPCObjectStubMock003FuzzTest", + "ipcobjectstubmock004_fuzzer:IPCObjectStubMock004FuzzTest", + "ipcobjectstubmock005_fuzzer:IPCObjectStubMock005FuzzTest", + ] +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/ipcobjectstubmock_fuzzer.h b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/include/ipcobjectstubmock_fuzzer.h similarity index 100% rename from test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/ipcobjectstubmock_fuzzer.h rename to test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/include/ipcobjectstubmock_fuzzer.h diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/BUILD.gn b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/BUILD.gn similarity index 71% rename from test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/BUILD.gn rename to test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/BUILD.gn index e71b57d6..3ae5f194 100644 --- a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/BUILD.gn +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/BUILD.gn @@ -14,19 +14,20 @@ #####################hydra-fuzz################### import("//build/config/features.gni") import("//build/test.gni") +SUBSYSTEM_DIR = "//foundation/communication/ipc" ##############################fuzztest########################################## -ohos_fuzztest("IPCObjectStubMockFuzzTest") { +ohos_fuzztest("IPCObjectStubMock001FuzzTest") { module_out_path = "ipc/ipc" - fuzz_config_file = "../ipcobjectstubmock_fuzzer" + fuzz_config_file = "../ipcobjectstubmock001_fuzzer" defines = [ "private = public", "protected = public", ] + include_dirs = [ "../include" ] + sources = [ "ipcobjectstubmock001_fuzzer.cpp" ] - sources = [ "ipcobjectstubmock_fuzzer.cpp" ] - - deps = [ "../../../../../../../interfaces/innerkits/ipc_single:ipc_single_test" ] + deps = [ "../../../../../../../../interfaces/innerkits/ipc_single:ipc_single_test" ] external_deps = [ "c_utils:utils", @@ -34,4 +35,5 @@ ohos_fuzztest("IPCObjectStubMockFuzzTest") { "googletest:gmock", "googletest:gtest", ] + resource_config_file = "$SUBSYSTEM_DIR/test/resource/ipc/ohos_test.xml" } diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/corpus/init b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/corpus/init similarity index 100% rename from test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/corpus/init rename to test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/corpus/init diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/ipcobjectstubmock001_fuzzer.cpp b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/ipcobjectstubmock001_fuzzer.cpp new file mode 100644 index 00000000..ee382574 --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/ipcobjectstubmock001_fuzzer.cpp @@ -0,0 +1,172 @@ +/* + * 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. + */ + +#include +#include +#include +#include "ipcobjectstubmock_fuzzer.h" +#include "ipc_object_stub.h" +#include "ipc_skeleton.h" +#include "ipc_thread_skeleton.h" +#include "message_parcel.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +class IPCObjectStubInterface { +public: + IPCObjectStubInterface() {}; + virtual ~IPCObjectStubInterface() {}; + + virtual bool WriteInt32(int32_t value) = 0; + virtual bool WriteString16(const std::u16string &value) = 0; + virtual bool IsLocalCalling() = 0; +}; + +class IPCObjectStubInterfaceMock : public IPCObjectStubInterface { +public: + IPCObjectStubInterfaceMock(); + ~IPCObjectStubInterfaceMock() override; + MOCK_METHOD1(WriteInt32, bool(int32_t value)); + MOCK_METHOD1(WriteString16, bool(const std::u16string &value)); + MOCK_METHOD0(IsLocalCalling, bool()); +}; + +static void *g_interface = nullptr; + +IPCObjectStubInterfaceMock::IPCObjectStubInterfaceMock() +{ + g_interface = reinterpret_cast(this); +} + +IPCObjectStubInterfaceMock::~IPCObjectStubInterfaceMock() +{ + g_interface = nullptr; +} + +static IPCObjectStubInterface *GetIPCObjectStubInterface() +{ + return reinterpret_cast(g_interface); +} + +extern "C" { + bool Parcel::WriteInt32(int32_t value) + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->WriteInt32(value); + } + + bool Parcel::WriteString16(const std::u16string &value) + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->WriteString16(value); + } + + bool IPCSkeleton::IsLocalCalling() + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->IsLocalCalling(); + } +} + +static void DBinderPingTransactionFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + size_t bytesSize = provider.ConsumeIntegralInRange(1, 50); + std::vector bytes = provider.ConsumeBytes(bytesSize); + reply.WriteBuffer(bytes.data(), bytes.size()); + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, WriteInt32).WillRepeatedly(Return(0)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderPingTransaction(code, data, reply, option); +} + +static void DBinderSearchDescriptorFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + size_t bytesSize = provider.ConsumeIntegralInRange(1, 50); + std::vector bytes = provider.ConsumeBytes(bytesSize); + reply.WriteBuffer(bytes.data(), bytes.size()); + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, WriteString16).WillRepeatedly(Return(0)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderSearchDescriptor(code, data, reply, option); +} + +static void DBinderDumpTransactionFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderDumpTransaction(code, data, reply, option); +} + +static void DBinderInvokeListenThreadFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderInvokeListenThread(code, data, reply, option); +} + +static void DBinderIncRefsTransactionFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderIncRefsTransaction(code, data, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider provider(data, size); + OHOS::DBinderPingTransactionFuzzTest(provider); + OHOS::DBinderSearchDescriptorFuzzTest(provider); + OHOS::DBinderDumpTransactionFuzzTest(provider); + OHOS::DBinderInvokeListenThreadFuzzTest(provider); + OHOS::DBinderIncRefsTransactionFuzzTest(provider); + return 0; +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/project.xml b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/project.xml similarity index 100% rename from test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/project.xml rename to test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock001_fuzzer/project.xml diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/BUILD.gn b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/BUILD.gn new file mode 100644 index 00000000..9cae2b4e --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/BUILD.gn @@ -0,0 +1,39 @@ +# 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") +SUBSYSTEM_DIR = "//foundation/communication/ipc" + +##############################fuzztest########################################## +ohos_fuzztest("IPCObjectStubMock002FuzzTest") { + module_out_path = "ipc/ipc" + fuzz_config_file = "../ipcobjectstubmock002_fuzzer" + defines = [ + "private = public", + "protected = public", + ] + include_dirs = [ "../include" ] + sources = [ "ipcobjectstubmock002_fuzzer.cpp" ] + + deps = [ "../../../../../../../../interfaces/innerkits/ipc_single:ipc_single_test" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "googletest:gmock", + "googletest:gtest", + ] + resource_config_file = "$SUBSYSTEM_DIR/test/resource/ipc/ohos_test.xml" +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/corpus/init b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/corpus/init new file mode 100644 index 00000000..7ade8a0f --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_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/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/ipcobjectstubmock002_fuzzer.cpp b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/ipcobjectstubmock002_fuzzer.cpp new file mode 100644 index 00000000..86c39580 --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/ipcobjectstubmock002_fuzzer.cpp @@ -0,0 +1,169 @@ +/* + * 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. + */ + +#include +#include +#include +#include "ipcobjectstubmock_fuzzer.h" +#include "ipc_object_stub.h" +#include "ipc_skeleton.h" +#include "ipc_thread_skeleton.h" +#include "message_parcel.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +class IPCObjectStubInterface { +public: + IPCObjectStubInterface() {}; + virtual ~IPCObjectStubInterface() {}; + virtual bool WriteString(const std::string &value) = 0; + virtual bool IsLocalCalling() = 0; + virtual std::string GetSessionName() = 0; +}; + +class IPCObjectStubInterfaceMock : public IPCObjectStubInterface { +public: + IPCObjectStubInterfaceMock(); + ~IPCObjectStubInterfaceMock() override; + MOCK_METHOD1(WriteString, bool(const std::string &value)); + MOCK_METHOD0(IsLocalCalling, bool()); + MOCK_METHOD0(GetSessionName, std::string()); +}; + +static void *g_interface = nullptr; + +IPCObjectStubInterfaceMock::IPCObjectStubInterfaceMock() +{ + g_interface = reinterpret_cast(this); +} + +IPCObjectStubInterfaceMock::~IPCObjectStubInterfaceMock() +{ + g_interface = nullptr; +} + +static IPCObjectStubInterface *GetIPCObjectStubInterface() +{ + return reinterpret_cast(g_interface); +} + +extern "C" { + bool Parcel::WriteString(const std::string &value) + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->WriteString(value); + } + + bool IPCSkeleton::IsLocalCalling() + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->IsLocalCalling(); + } + + std::string IPCObjectStub::GetSessionName() + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return ""; + } + return interface->GetSessionName(); + } +} + +static void DBinderDecRefsTransactionFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderDecRefsTransaction(code, data, reply, option); +} + +static void DBinderAddCommAuthFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderAddCommAuth(code, data, reply, option); +} + +static void DBinderGetSessionNameFuzzTest001(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderGetSessionName(code, data, reply, option); +} + +static void DBinderGetSessionNameFuzzTest002(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(true)); + EXPECT_CALL(mockClient, GetSessionName).WillRepeatedly(Return("")); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderGetSessionName(code, data, reply, option); +} + +static void DBinderGetSessionNameFuzzTest003(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(Return(true)); + EXPECT_CALL(mockClient, GetSessionName).WillRepeatedly(Return("sessionname")); + EXPECT_CALL(mockClient, WriteString).WillRepeatedly(Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderGetSessionName(code, data, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider provider(data, size); + OHOS::DBinderDecRefsTransactionFuzzTest(provider); + OHOS::DBinderAddCommAuthFuzzTest(provider); + OHOS::DBinderGetSessionNameFuzzTest001(provider); + OHOS::DBinderGetSessionNameFuzzTest002(provider); + OHOS::DBinderGetSessionNameFuzzTest003(provider); + return 0; +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/project.xml b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/project.xml new file mode 100644 index 00000000..226522bd --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock002_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 10000 + + 300 + + 4096 + + diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/BUILD.gn b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/BUILD.gn new file mode 100644 index 00000000..398046ea --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/BUILD.gn @@ -0,0 +1,39 @@ +# 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") +SUBSYSTEM_DIR = "//foundation/communication/ipc" + +##############################fuzztest########################################## +ohos_fuzztest("IPCObjectStubMock003FuzzTest") { + module_out_path = "ipc/ipc" + fuzz_config_file = "../ipcobjectstubmock003_fuzzer" + defines = [ + "private = public", + "protected = public", + ] + include_dirs = [ "../include" ] + sources = [ "ipcobjectstubmock003_fuzzer.cpp" ] + + deps = [ "../../../../../../../../interfaces/innerkits/ipc_single:ipc_single_test" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "googletest:gmock", + "googletest:gtest", + ] + resource_config_file = "$SUBSYSTEM_DIR/test/resource/ipc/ohos_test.xml" +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/corpus/init b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/corpus/init new file mode 100644 index 00000000..7ade8a0f --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_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/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/ipcobjectstubmock003_fuzzer.cpp b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/ipcobjectstubmock003_fuzzer.cpp new file mode 100644 index 00000000..6d613769 --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/ipcobjectstubmock003_fuzzer.cpp @@ -0,0 +1,173 @@ +/* + * 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. + */ + +#include +#include +#include +#include "ipcobjectstubmock_fuzzer.h" +#include "ipc_object_stub.h" +#include "ipc_skeleton.h" +#include "ipc_thread_skeleton.h" +#include "message_parcel.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +class IPCObjectStubInterface { +public: + IPCObjectStubInterface() {}; + virtual ~IPCObjectStubInterface() {}; + virtual bool IsLocalCalling() = 0; + virtual bool IsSamgrCall() = 0; + virtual bool WriteUint32(uint32_t value) = 0; +}; + +class IPCObjectStubInterfaceMock : public IPCObjectStubInterface { +public: + IPCObjectStubInterfaceMock(); + ~IPCObjectStubInterfaceMock() override; + MOCK_METHOD0(IsLocalCalling, bool()); + MOCK_METHOD0(IsSamgrCall, bool()); + MOCK_METHOD1(WriteUint32, bool(uint32_t value)); +}; + +static void *g_interface = nullptr; + +IPCObjectStubInterfaceMock::IPCObjectStubInterfaceMock() +{ + g_interface = reinterpret_cast(this); +} + +IPCObjectStubInterfaceMock::~IPCObjectStubInterfaceMock() +{ + g_interface = nullptr; +} + +static IPCObjectStubInterface *GetIPCObjectStubInterface() +{ + return reinterpret_cast(g_interface); +} + +extern "C" { + bool IPCSkeleton::IsLocalCalling() + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->IsLocalCalling(); + } + + bool IPCObjectStub::IsSamgrCall() + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->IsSamgrCall(); + } + + bool Parcel::WriteUint32(uint32_t value) + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->WriteUint32(value); + } +} + +static void DBinderGetGrantedSessionNameFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderGetGrantedSessionName(code, data, reply, option); +} + +static void DBinderGetSessionNameForPidUidFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderGetSessionNameForPidUid(code, data, reply, option); +} + +static void DBinderGetPidUidFuzzTest001(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderGetPidUid(code, data, reply, option); +} + +static void DBinderGetPidUidFuzzTest002(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(true)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderGetPidUid(code, data, reply, option); +} + +static void DBinderRemoveSessionNameFuzzTest(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.DBinderRemoveSessionName(code, data, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider provider(data, size); + OHOS::DBinderGetGrantedSessionNameFuzzTest(provider); + OHOS::DBinderGetSessionNameForPidUidFuzzTest(provider); + OHOS::DBinderGetPidUidFuzzTest001(provider); + OHOS::DBinderGetPidUidFuzzTest002(provider); + OHOS::DBinderRemoveSessionNameFuzzTest(provider); + return 0; +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/project.xml b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/project.xml new file mode 100644 index 00000000..226522bd --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock003_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 10000 + + 300 + + 4096 + + diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/BUILD.gn b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/BUILD.gn new file mode 100644 index 00000000..e6278b30 --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/BUILD.gn @@ -0,0 +1,39 @@ +# 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") +SUBSYSTEM_DIR = "//foundation/communication/ipc" + +##############################fuzztest########################################## +ohos_fuzztest("IPCObjectStubMock004FuzzTest") { + module_out_path = "ipc/ipc" + fuzz_config_file = "../ipcobjectstubmock004_fuzzer" + defines = [ + "private = public", + "protected = public", + ] + include_dirs = [ "../include" ] + sources = [ "ipcobjectstubmock004_fuzzer.cpp" ] + + deps = [ "../../../../../../../../interfaces/innerkits/ipc_single:ipc_single_test" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "googletest:gmock", + "googletest:gtest", + ] + resource_config_file = "$SUBSYSTEM_DIR/test/resource/ipc/ohos_test.xml" +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/corpus/init b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/corpus/init new file mode 100644 index 00000000..7ade8a0f --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_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/mock/ipcobjectstubmock_fuzzer/ipcobjectstubmock_fuzzer.cpp b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/ipcobjectstubmock004_fuzzer.cpp similarity index 40% rename from test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/ipcobjectstubmock_fuzzer.cpp rename to test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/ipcobjectstubmock004_fuzzer.cpp index 3b196217..b9665120 100644 --- a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock_fuzzer/ipcobjectstubmock_fuzzer.cpp +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/ipcobjectstubmock004_fuzzer.cpp @@ -56,29 +56,20 @@ class IPCObjectStubInterface { public: IPCObjectStubInterface() {}; virtual ~IPCObjectStubInterface() {}; - - virtual bool WriteInt32(int32_t value) = 0; virtual bool WriteString16(const std::u16string &value) = 0; virtual bool WriteString(const std::string &value) = 0; - virtual bool IsLocalCalling() = 0; - virtual bool IsSamgrCall() = 0; virtual bool WriteUint32(uint32_t value) = 0; virtual std::string CreateSessionName(int uid, int pid) = 0; - virtual std::string GetSessionName() = 0; }; class IPCObjectStubInterfaceMock : public IPCObjectStubInterface { public: IPCObjectStubInterfaceMock(); ~IPCObjectStubInterfaceMock() override; - MOCK_METHOD1(WriteInt32, bool(int32_t value)); MOCK_METHOD1(WriteString16, bool(const std::u16string &value)); MOCK_METHOD1(WriteString, bool(const std::string &value)); - MOCK_METHOD0(IsLocalCalling, bool()); - MOCK_METHOD0(IsSamgrCall, bool()); MOCK_METHOD1(WriteUint32, bool(uint32_t value)); MOCK_METHOD2(CreateSessionName, std::string(int uid, int pid)); - MOCK_METHOD0(GetSessionName, std::string()); }; static void *g_interface = nullptr; @@ -99,15 +90,6 @@ static IPCObjectStubInterface *GetIPCObjectStubInterface() } extern "C" { - bool Parcel::WriteInt32(int32_t value) - { - IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); - if (interface == nullptr) { - return false; - } - return interface->WriteInt32(value); - } - bool Parcel::WriteString16(const std::u16string &value) { IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); @@ -126,24 +108,6 @@ extern "C" { return interface->WriteString(value); } - bool IPCSkeleton::IsLocalCalling() - { - IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); - if (interface == nullptr) { - return false; - } - return interface->IsLocalCalling(); - } - - bool IPCObjectStub::IsSamgrCall() - { - IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); - if (interface == nullptr) { - return false; - } - return interface->IsSamgrCall(); - } - bool Parcel::WriteUint32(uint32_t value) { IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); @@ -161,211 +125,6 @@ extern "C" { } return interface->CreateSessionName(uid, pid); } - - std::string IPCObjectStub::GetSessionName() - { - IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); - if (interface == nullptr) { - return ""; - } - return interface->GetSessionName(); - } -} - -static void DBinderPingTransactionFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - size_t bytesSize = provider.ConsumeIntegralInRange(1, 50); - std::vector bytes = provider.ConsumeBytes(bytesSize); - reply.WriteBuffer(bytes.data(), bytes.size()); - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, WriteInt32).WillOnce(Return(0)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderPingTransaction(code, data, reply, option); -} - -static void DBinderSearchDescriptorFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - size_t bytesSize = provider.ConsumeIntegralInRange(1, 50); - std::vector bytes = provider.ConsumeBytes(bytesSize); - reply.WriteBuffer(bytes.data(), bytes.size()); - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, WriteString16).WillOnce(Return(0)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderSearchDescriptor(code, data, reply, option); -} - -static void DBinderDumpTransactionFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderDumpTransaction(code, data, reply, option); -} - -static void DBinderInvokeListenThreadFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderInvokeListenThread(code, data, reply, option); -} - -static void DBinderIncRefsTransactionFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderIncRefsTransaction(code, data, reply, option); -} - -static void DBinderDecRefsTransactionFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderDecRefsTransaction(code, data, reply, option); -} - -static void DBinderAddCommAuthFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderAddCommAuth(code, data, reply, option); -} - -static void DBinderGetSessionNameFuzzTest001(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderGetSessionName(code, data, reply, option); -} - -static void DBinderGetSessionNameFuzzTest002(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(true)); - EXPECT_CALL(mockClient, GetSessionName).WillOnce(Return("")); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderGetSessionName(code, data, reply, option); -} - -static void DBinderGetSessionNameFuzzTest003(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillOnce(Return(true)); - EXPECT_CALL(mockClient, GetSessionName).WillOnce(Return("sessionname")); - EXPECT_CALL(mockClient, WriteString).WillOnce(Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderGetSessionName(code, data, reply, option); -} - -static void DBinderGetGrantedSessionNameFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderGetGrantedSessionName(code, data, reply, option); -} - -static void DBinderGetSessionNameForPidUidFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderGetSessionNameForPidUid(code, data, reply, option); -} - -static void DBinderGetPidUidFuzzTest001(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderGetPidUid(code, data, reply, option); -} - -static void DBinderGetPidUidFuzzTest002(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(true)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderGetPidUid(code, data, reply, option); -} - -static void DBinderRemoveSessionNameFuzzTest(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, IsLocalCalling).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, IsSamgrCall).WillRepeatedly(testing::Return(true)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.DBinderRemoveSessionName(code, data, reply, option); } static void SendRequestFuzzTest001(FuzzedDataProvider &provider) @@ -443,34 +202,6 @@ static void GetGrantedSessionNameFuzzTest002(FuzzedDataProvider &provider) IPCObjectStub ipcObjectStub; ipcObjectStub.GetGrantedSessionName(code, data, reply, option); } - -static void GetSessionNameForPidUidFuzzTest001(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, CreateSessionName).WillRepeatedly(testing::Return("sessionName")); - EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(true)); - EXPECT_CALL(mockClient, WriteString).WillRepeatedly(testing::Return(true)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.GetSessionNameForPidUid(code, data, reply, option); -} - -static void GetSessionNameForPidUidFuzzTest002(FuzzedDataProvider &provider) -{ - uint32_t code = provider.ConsumeIntegral(); - MessageParcel data; - MessageParcel reply; - MessageOption option; - NiceMock mockClient; - EXPECT_CALL(mockClient, CreateSessionName).WillRepeatedly(testing::Return("sessionName")); - EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(false)); - EXPECT_CALL(mockClient, WriteString).WillRepeatedly(testing::Return(false)); - IPCObjectStub ipcObjectStub; - ipcObjectStub.GetSessionNameForPidUid(code, data, reply, option); -} } // namespace OHOS /* Fuzzer entry point */ @@ -478,27 +209,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { /* Run your code on data */ FuzzedDataProvider provider(data, size); - OHOS::DBinderPingTransactionFuzzTest(provider); - OHOS::DBinderSearchDescriptorFuzzTest(provider); - OHOS::DBinderDumpTransactionFuzzTest(provider); - OHOS::DBinderInvokeListenThreadFuzzTest(provider); - OHOS::DBinderIncRefsTransactionFuzzTest(provider); - OHOS::DBinderDecRefsTransactionFuzzTest(provider); - OHOS::DBinderAddCommAuthFuzzTest(provider); - OHOS::DBinderGetSessionNameFuzzTest001(provider); - OHOS::DBinderGetSessionNameFuzzTest002(provider); - OHOS::DBinderGetSessionNameFuzzTest003(provider); - OHOS::DBinderGetGrantedSessionNameFuzzTest(provider); - OHOS::DBinderGetSessionNameForPidUidFuzzTest(provider); - OHOS::DBinderGetPidUidFuzzTest001(provider); - OHOS::DBinderGetPidUidFuzzTest002(provider); - OHOS::DBinderRemoveSessionNameFuzzTest(provider); OHOS::SendRequestFuzzTest001(provider); OHOS::SendRequestFuzzTest002(provider); OHOS::ProcessProtoFuzzTest(provider); OHOS::GetGrantedSessionNameFuzzTest001(provider); OHOS::GetGrantedSessionNameFuzzTest002(provider); - OHOS::GetSessionNameForPidUidFuzzTest001(provider); - OHOS::GetSessionNameForPidUidFuzzTest002(provider); return 0; } diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/project.xml b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/project.xml new file mode 100644 index 00000000..226522bd --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock004_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 10000 + + 300 + + 4096 + + diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/BUILD.gn b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/BUILD.gn new file mode 100644 index 00000000..09264340 --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/BUILD.gn @@ -0,0 +1,39 @@ +# 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") +SUBSYSTEM_DIR = "//foundation/communication/ipc" + +##############################fuzztest########################################## +ohos_fuzztest("IPCObjectStubMock005FuzzTest") { + module_out_path = "ipc/ipc" + fuzz_config_file = "../ipcobjectstubmock005_fuzzer" + defines = [ + "private = public", + "protected = public", + ] + include_dirs = [ "../include" ] + sources = [ "ipcobjectstubmock005_fuzzer.cpp" ] + + deps = [ "../../../../../../../../interfaces/innerkits/ipc_single:ipc_single_test" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "googletest:gmock", + "googletest:gtest", + ] + resource_config_file = "$SUBSYSTEM_DIR/test/resource/ipc/ohos_test.xml" +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/corpus/init b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/corpus/init new file mode 100644 index 00000000..7ade8a0f --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_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/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/ipcobjectstubmock005_fuzzer.cpp b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/ipcobjectstubmock005_fuzzer.cpp new file mode 100644 index 00000000..2bf112a6 --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/ipcobjectstubmock005_fuzzer.cpp @@ -0,0 +1,131 @@ +/* + * 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. + */ + +#include +#include +#include +#include "ipcobjectstubmock_fuzzer.h" +#include "ipc_object_stub.h" +#include "ipc_skeleton.h" +#include "ipc_thread_skeleton.h" +#include "message_parcel.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { + +class IPCObjectStubInterface { +public: + IPCObjectStubInterface() {}; + virtual ~IPCObjectStubInterface() {}; + virtual bool WriteString(const std::string &value) = 0; + virtual bool WriteUint32(uint32_t value) = 0; + virtual std::string CreateSessionName(int uid, int pid) = 0; +}; + +class IPCObjectStubInterfaceMock : public IPCObjectStubInterface { +public: + IPCObjectStubInterfaceMock(); + ~IPCObjectStubInterfaceMock() override; + MOCK_METHOD1(WriteString, bool(const std::string &value)); + MOCK_METHOD1(WriteUint32, bool(uint32_t value)); + MOCK_METHOD2(CreateSessionName, std::string(int uid, int pid)); +}; + +static void *g_interface = nullptr; + +IPCObjectStubInterfaceMock::IPCObjectStubInterfaceMock() +{ + g_interface = reinterpret_cast(this); +} + +IPCObjectStubInterfaceMock::~IPCObjectStubInterfaceMock() +{ + g_interface = nullptr; +} + +static IPCObjectStubInterface *GetIPCObjectStubInterface() +{ + return reinterpret_cast(g_interface); +} + +extern "C" { + bool Parcel::WriteString(const std::string &value) + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->WriteString(value); + } + + bool Parcel::WriteUint32(uint32_t value) + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return false; + } + return interface->WriteUint32(value); + } + + std::string IPCObjectStub::CreateSessionName(int uid, int pid) + { + IPCObjectStubInterface* interface = GetIPCObjectStubInterface(); + if (interface == nullptr) { + return ""; + } + return interface->CreateSessionName(uid, pid); + } +} + +static void GetSessionNameForPidUidFuzzTest001(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, CreateSessionName).WillRepeatedly(testing::Return("sessionName")); + EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(true)); + EXPECT_CALL(mockClient, WriteString).WillRepeatedly(testing::Return(true)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.GetSessionNameForPidUid(code, data, reply, option); +} + +static void GetSessionNameForPidUidFuzzTest002(FuzzedDataProvider &provider) +{ + uint32_t code = provider.ConsumeIntegral(); + MessageParcel data; + MessageParcel reply; + MessageOption option; + NiceMock mockClient; + EXPECT_CALL(mockClient, CreateSessionName).WillRepeatedly(testing::Return("sessionName")); + EXPECT_CALL(mockClient, WriteUint32).WillRepeatedly(testing::Return(false)); + EXPECT_CALL(mockClient, WriteString).WillRepeatedly(testing::Return(false)); + IPCObjectStub ipcObjectStub; + ipcObjectStub.GetSessionNameForPidUid(code, data, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider provider(data, size); + OHOS::GetSessionNameForPidUidFuzzTest001(provider); + OHOS::GetSessionNameForPidUidFuzzTest002(provider); + return 0; +} diff --git a/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/project.xml b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/project.xml new file mode 100644 index 00000000..226522bd --- /dev/null +++ b/test/fuzztest/ipc/native/src/mock/ipcobjectstubmock/ipcobjectstubmock005_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 10000 + + 300 + + 4096 + + diff --git a/test/resource/ipc/ohos_test.xml b/test/resource/ipc/ohos_test.xml index 7d4b5530..28748bb2 100644 --- a/test/resource/ipc/ohos_test.xml +++ b/test/resource/ipc/ohos_test.xml @@ -431,4 +431,34 @@