diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index f64a1e4bb317b2bb70dd9ad00e26765ab789c121..8468e7fbd79380495fd33d6aeaf10d4b3e107884 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 0000000000000000000000000000000000000000..9cbc63641f582ebaab656845d92ae47d8557adc0 --- /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 e71b57d67b3896b932e855bc39de2461581b5118..3ae5f194326e6e42218095049f133dc107dd5057 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 0000000000000000000000000000000000000000..ee3825748542faa6c67672beaa77165cd6316a2d --- /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 0000000000000000000000000000000000000000..9cae2b4ee0b62a926ab76bcb32ebd630a7b2a349 --- /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 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /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 0000000000000000000000000000000000000000..86c3958043a0552d38b76d2ee59eb178a8ed7d6e --- /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 0000000000000000000000000000000000000000..226522bd2ad3eaf2db4f710f1924d82d2912c235 --- /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 0000000000000000000000000000000000000000..398046ea0cf690381fdde7c8b3fb673378c2c5e6 --- /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 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /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 0000000000000000000000000000000000000000..6d613769cbceba949c4a280a1841a828813dc351 --- /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 0000000000000000000000000000000000000000..226522bd2ad3eaf2db4f710f1924d82d2912c235 --- /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 0000000000000000000000000000000000000000..e6278b30178542e256f336daac01dacd6f37254f --- /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 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /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 3b1962174efb18ac727d5e3f61ed82216c341190..b9665120e74a3c50da9d2d49d09e37e133935b1c 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 0000000000000000000000000000000000000000..226522bd2ad3eaf2db4f710f1924d82d2912c235 --- /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 0000000000000000000000000000000000000000..09264340e88297490a7f5889748c5ed1f761a02f --- /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 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /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 0000000000000000000000000000000000000000..2bf112a62df4219e7f48015ca7e8f096779a4779 --- /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 0000000000000000000000000000000000000000..226522bd2ad3eaf2db4f710f1924d82d2912c235 --- /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 7d4b5530ca81cc2b2bfc6d2ae97f43fb98bd79a0..28748bb2270c12b2077fe583f16cc4fa3e731cf3 100644 --- a/test/resource/ipc/ohos_test.xml +++ b/test/resource/ipc/ohos_test.xml @@ -431,4 +431,34 @@