From a5abbd312a4fca6d9e311ba91a1e6a2e3a45c8ab Mon Sep 17 00:00:00 2001 From: wujiqin Date: Wed, 2 Nov 2022 15:09:43 +0800 Subject: [PATCH] =?UTF-8?q?IssueNo:https://gitee.com/openharmony/notificat?= =?UTF-8?q?ion=5Fcommon=5Fevent=5Fservice/issues/I5Z20W=3Ffrom=3Dproject-i?= =?UTF-8?q?ssue=20Description:ans=E8=A6=86=E7=9B=96=E7=8E=87=E6=8F=90?= =?UTF-8?q?=E5=8D=87common=5Fevent01=20Sig:SIG=5FApplicationFramework=20Fe?= =?UTF-8?q?ature=20or=20Bugfix:Bugfix=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wujiqin Change-Id: I49ef06722a7b7cc86b32fe856f2d671ae33b397c --- test/fuzztest/BUILD.gn | 3 + .../fuzztest/commoneventproxy_fuzzer/BUILD.gn | 52 ++++++++++++++ .../commoneventproxy_fuzzer.cpp | 65 +++++++++++++++++ .../commoneventproxy_fuzzer.h | 29 ++++++++ .../commoneventproxy_fuzzer/corpus/init | 13 ++++ .../commoneventproxy_fuzzer/project.xml | 25 +++++++ .../commoneventpublishinfo_fuzzer/BUILD.gn | 49 +++++++++++++ .../commoneventpublishinfo_fuzzer.cpp | 66 ++++++++++++++++++ .../commoneventpublishinfo_fuzzer.h | 29 ++++++++ .../commoneventpublishinfo_fuzzer/corpus/init | 13 ++++ .../commoneventpublishinfo_fuzzer/project.xml | 25 +++++++ .../commoneventstub_fuzzer.cpp | 6 ++ .../eventreceiveproxy_fuzzer/BUILD.gn | 52 ++++++++++++++ .../eventreceiveproxy_fuzzer/corpus/init | 13 ++++ .../eventreceiveproxy_fuzzer.cpp | 69 +++++++++++++++++++ .../eventreceiveproxy_fuzzer.h | 29 ++++++++ .../eventreceiveproxy_fuzzer/project.xml | 25 +++++++ .../matchingskills_fuzzer.cpp | 7 ++ .../subscribecommonevent_fuzzer.cpp | 9 +++ 19 files changed, 579 insertions(+) create mode 100644 test/fuzztest/commoneventproxy_fuzzer/BUILD.gn create mode 100644 test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.cpp create mode 100644 test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.h create mode 100644 test/fuzztest/commoneventproxy_fuzzer/corpus/init create mode 100644 test/fuzztest/commoneventproxy_fuzzer/project.xml create mode 100644 test/fuzztest/commoneventpublishinfo_fuzzer/BUILD.gn create mode 100644 test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.cpp create mode 100644 test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.h create mode 100644 test/fuzztest/commoneventpublishinfo_fuzzer/corpus/init create mode 100644 test/fuzztest/commoneventpublishinfo_fuzzer/project.xml create mode 100644 test/fuzztest/eventreceiveproxy_fuzzer/BUILD.gn create mode 100644 test/fuzztest/eventreceiveproxy_fuzzer/corpus/init create mode 100644 test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.cpp create mode 100644 test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.h create mode 100644 test/fuzztest/eventreceiveproxy_fuzzer/project.xml diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index ddf212fc..005890bb 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -18,8 +18,11 @@ group("fuzztest") { deps = [ "asynccommoneventresult_fuzzer:AsyncCommonEventResultFuzzTest", + "commoneventproxy_fuzzer:CommonEventProxyFuzzTest", + "commoneventpublishinfo_fuzzer:CommonEventPublishInfoFuzzTest", "commoneventstub_fuzzer:CommonEventStubFuzzTest", "dumpstate_fuzzer:DumpStateFuzzTest", + "eventreceiveproxy_fuzzer:EventReceiveProxyFuzzTest", "getstickycommonevent_fuzzer:GetStickyCommonEventFuzzTest", "matchingskills_fuzzer:MatchingSkillsFuzzTest", "publishcommonevent_fuzzer:PublishCommonEventFuzzTest", diff --git a/test/fuzztest/commoneventproxy_fuzzer/BUILD.gn b/test/fuzztest/commoneventproxy_fuzzer/BUILD.gn new file mode 100644 index 00000000..b4408397 --- /dev/null +++ b/test/fuzztest/commoneventproxy_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//base/notification/common_event_service/event.gni") +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "common_event_service/services" + +##############################fuzztest########################################## +ohos_fuzztest("CommonEventProxyFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/notification/common_event_service/test/fuzztest/commoneventproxy_fuzzer" + + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "commoneventproxy_fuzzer.cpp" ] + + deps = [ + "${ces_core_path}:cesfwk_core", + "${ces_native_path}:cesfwk_innerkits", + ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + "ipc:ipc_core", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ ":CommonEventProxyFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.cpp b/test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.cpp new file mode 100644 index 00000000..a71f37c0 --- /dev/null +++ b/test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common_event_proxy.h" +#include "common_event_listener.h" +#include "commoneventproxy_fuzzer.h" +#include "securec.h" + +namespace OHOS { +namespace { + constexpr size_t U32_AT_SIZE = 4; +} +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + sptr object = nullptr; + EventFwk::CommonEventProxy commonEventProxy(object); + commonEventProxy.UnsubscribeCommonEvent(object); + std::shared_ptr commonEventSubscriber = nullptr; + EventFwk::CommonEventListener commonEventListener(commonEventSubscriber); + commonEventListener.Stop(); + return true; +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + if (size < OHOS::U32_AT_SIZE) { + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} diff --git a/test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.h b/test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.h new file mode 100644 index 00000000..4d1b81d7 --- /dev/null +++ b/test/fuzztest/commoneventproxy_fuzzer/commoneventproxy_fuzzer.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_FUZZTEST_COMMONEVENTPROXY_FUZZER_COMMONEVENTPROXY_FUZZER_H +#define TEST_FUZZTEST_COMMONEVENTPROXY_FUZZER_COMMONEVENTPROXY_FUZZER_H + +#define FUZZ_PROJECT_NAME "commoneventproxy_fuzzer" + +#include + +uint32_t U32_AT(const uint8_t *ptr) +{ + // convert fuzz input data to an integer + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; +} + +#endif // TEST_FUZZTEST_COMMONEVENTPROXY_FUZZER_COMMONEVENTPROXY_FUZZER_H diff --git a/test/fuzztest/commoneventproxy_fuzzer/corpus/init b/test/fuzztest/commoneventproxy_fuzzer/corpus/init new file mode 100644 index 00000000..1b910144 --- /dev/null +++ b/test/fuzztest/commoneventproxy_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/commoneventproxy_fuzzer/project.xml b/test/fuzztest/commoneventproxy_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/test/fuzztest/commoneventproxy_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/commoneventpublishinfo_fuzzer/BUILD.gn b/test/fuzztest/commoneventpublishinfo_fuzzer/BUILD.gn new file mode 100644 index 00000000..a838339e --- /dev/null +++ b/test/fuzztest/commoneventpublishinfo_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//base/notification/common_event_service/event.gni") +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "common_event_service/services" + +##############################fuzztest########################################## +ohos_fuzztest("CommonEventPublishInfoFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/notification/common_event_service/test/fuzztest/commoneventpublishinfo_fuzzer" + + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "commoneventpublishinfo_fuzzer.cpp" ] + + deps = [ "${ces_native_path}:cesfwk_innerkits" ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + "ipc:ipc_core", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ ":CommonEventPublishInfoFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.cpp b/test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.cpp new file mode 100644 index 00000000..6478537d --- /dev/null +++ b/test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "commoneventpublishinfo_fuzzer.h" +#include "securec.h" + +#define private public +#define protected public +#include "common_event_publish_info.h" +#undef private +#undef protected + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + Parcel parcel; + EventFwk::CommonEventPublishInfo PublishInfo; + EventFwk::CommonEventPublishInfo commonEventPublishInfo(PublishInfo); + commonEventPublishInfo.ReadFromParcel(parcel); + commonEventPublishInfo.Unmarshalling(parcel); + return true; +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + if (size < OHOS::U32_AT_SIZE) { + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} diff --git a/test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.h b/test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.h new file mode 100644 index 00000000..ae3f123a --- /dev/null +++ b/test/fuzztest/commoneventpublishinfo_fuzzer/commoneventpublishinfo_fuzzer.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_FUZZTEST_COMMONENTPUBLISHINFO_FUZZER_COMMONENTPUBLISHINFO_FUZZER_H +#define TEST_FUZZTEST_COMMONENTPUBLISHINFO_FUZZER_COMMONENTPUBLISHINFO_FUZZER_H + +#define FUZZ_PROJECT_NAME "commoneventpublishinfo_fuzzer" + +#include + +uint32_t U32_AT(const uint8_t *ptr) +{ + // convert fuzz input data to an integer + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; +} + +#endif // TEST_FUZZTEST_COMMONENTPUBLISHINFO_FUZZER_COMMONENTPUBLISHINFO_FUZZER_H diff --git a/test/fuzztest/commoneventpublishinfo_fuzzer/corpus/init b/test/fuzztest/commoneventpublishinfo_fuzzer/corpus/init new file mode 100644 index 00000000..1b910144 --- /dev/null +++ b/test/fuzztest/commoneventpublishinfo_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/commoneventpublishinfo_fuzzer/project.xml b/test/fuzztest/commoneventpublishinfo_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/test/fuzztest/commoneventpublishinfo_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/commoneventstub_fuzzer/commoneventstub_fuzzer.cpp b/test/fuzztest/commoneventstub_fuzzer/commoneventstub_fuzzer.cpp index 5b680528..8ff1950f 100644 --- a/test/fuzztest/commoneventstub_fuzzer/commoneventstub_fuzzer.cpp +++ b/test/fuzztest/commoneventstub_fuzzer/commoneventstub_fuzzer.cpp @@ -14,6 +14,7 @@ */ #include "common_event_stub.h" +#include "common_event_data.h" #include "commoneventstub_fuzzer.h" #include "securec.h" @@ -27,10 +28,14 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) std::string stringData(data); int32_t code = U32_AT(reinterpret_cast(data)); bool enabled = *data % ENABLE; + MessageParcel dataParcel; + MessageParcel reply; + MessageOption option; EventFwk::CommonEventStub commonEventStub; // test PublishCommonEvent function AAFwk::Want want; EventFwk::CommonEventData commonEventData; + EventFwk::CommonEventData eventData(want, code, stringData); commonEventData.SetWant(want); commonEventData.SetCode(code); commonEventData.SetData(stringData); @@ -62,6 +67,7 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) commonEventStub.Freeze(code); // test Unfreeze function commonEventStub.Unfreeze(code); + commonEventStub.OnRemoteRequest(code, dataParcel, reply, option); // test UnfreezeAll function return commonEventStub.UnfreezeAll(); } diff --git a/test/fuzztest/eventreceiveproxy_fuzzer/BUILD.gn b/test/fuzztest/eventreceiveproxy_fuzzer/BUILD.gn new file mode 100644 index 00000000..b9550f3f --- /dev/null +++ b/test/fuzztest/eventreceiveproxy_fuzzer/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//base/notification/common_event_service/event.gni") +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "common_event_service/services" + +##############################fuzztest########################################## +ohos_fuzztest("EventReceiveProxyFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/notification/common_event_service/test/fuzztest/eventreceiveproxy_fuzzer" + + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "eventreceiveproxy_fuzzer.cpp" ] + + deps = [ + "${ces_core_path}:cesfwk_core", + "${ces_native_path}:cesfwk_innerkits", + ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + "ipc:ipc_core", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ ":EventReceiveProxyFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/eventreceiveproxy_fuzzer/corpus/init b/test/fuzztest/eventreceiveproxy_fuzzer/corpus/init new file mode 100644 index 00000000..1b910144 --- /dev/null +++ b/test/fuzztest/eventreceiveproxy_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.cpp b/test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.cpp new file mode 100644 index 00000000..3370dd8d --- /dev/null +++ b/test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "eventreceiveproxy_fuzzer.h" +#include "securec.h" +#include "event_receive_proxy.h" + +namespace OHOS { +namespace EventFwk { +} // namespace EventFwk +namespace { + constexpr size_t U32_AT_SIZE = 4; + constexpr uint8_t ENABLE = 2; +} +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + // make common event data + EventFwk::CommonEventData commonEventData; + bool enabled = *data % ENABLE; + std::shared_ptr result = std::make_shared(nullptr); + if (result != nullptr) { + result->NotifyEvent(commonEventData, enabled, enabled); + return true; + } + return false; +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + if (size < OHOS::U32_AT_SIZE) { + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} diff --git a/test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.h b/test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.h new file mode 100644 index 00000000..08a02d48 --- /dev/null +++ b/test/fuzztest/eventreceiveproxy_fuzzer/eventreceiveproxy_fuzzer.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_FUZZTEST_EVENTRECEIVEPROXY_FUZZER_EVENTRECEIVEPROXY_FUZZER_H +#define TEST_FUZZTEST_EVENTRECEIVEPROXY_FUZZER_EVENTRECEIVEPROXY_FUZZER_H + +#define FUZZ_PROJECT_NAME "eventreceiveproxy_fuzzer" + +#include + +uint32_t U32_AT(const uint8_t *ptr) +{ + // convert fuzz input data to an integer + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; +} + +#endif // TEST_FUZZTEST_EVENTRECEIVEPROXY_FUZZER_EVENTRECEIVEPROXY_FUZZER_H diff --git a/test/fuzztest/eventreceiveproxy_fuzzer/project.xml b/test/fuzztest/eventreceiveproxy_fuzzer/project.xml new file mode 100644 index 00000000..6e8ad2cf --- /dev/null +++ b/test/fuzztest/eventreceiveproxy_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/matchingskills_fuzzer/matchingskills_fuzzer.cpp b/test/fuzztest/matchingskills_fuzzer/matchingskills_fuzzer.cpp index 7248dab6..31f1371e 100644 --- a/test/fuzztest/matchingskills_fuzzer/matchingskills_fuzzer.cpp +++ b/test/fuzztest/matchingskills_fuzzer/matchingskills_fuzzer.cpp @@ -28,6 +28,8 @@ namespace { bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { std::string stringData(data); + size_t index = U32_AT(reinterpret_cast(data)); + Parcel parcel; // test MatchingSkills class function EventFwk::MatchingSkills matchingSkills; // test HasEntity function @@ -56,6 +58,11 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) matchingSkills.MatchEntity(permissions); // test MatchScheme function matchingSkills.MatchScheme(stringData); + matchingSkills.GetEntity(index); + matchingSkills.GetEvent(index); + matchingSkills.GetScheme(index); + matchingSkills.ReadFromParcel(parcel); + matchingSkills.Unmarshalling(parcel); // test Match function AAFwk::Want want; return matchingSkills.Match(want); diff --git a/test/fuzztest/subscribecommonevent_fuzzer/subscribecommonevent_fuzzer.cpp b/test/fuzztest/subscribecommonevent_fuzzer/subscribecommonevent_fuzzer.cpp index ebac5193..3973ba46 100644 --- a/test/fuzztest/subscribecommonevent_fuzzer/subscribecommonevent_fuzzer.cpp +++ b/test/fuzztest/subscribecommonevent_fuzzer/subscribecommonevent_fuzzer.cpp @@ -13,6 +13,12 @@ * limitations under the License. */ +#define private public +#define protected public +#include "common_event_subscribe_info.h" +#undef private +#undef protected + #include "subscribecommonevent_fuzzer.h" #include "securec.h" #include "common_event_manager.h" @@ -39,6 +45,7 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) std::string stringData(data); EventFwk::MatchingSkills matchingSkills; + Parcel parcel; matchingSkills.AddEvent(stringData); matchingSkills.AddEntity(stringData); matchingSkills.AddScheme(stringData); @@ -48,6 +55,8 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) EventFwk::CommonEventSubscribeInfo::ThreadMode(mode); EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); int32_t priority = U32_AT(reinterpret_cast(data)); + subscribeInfo.ReadFromParcel(parcel); + subscribeInfo.Unmarshalling(parcel); subscribeInfo.SetPriority(priority); subscribeInfo.SetPermission(stringData); subscribeInfo.SetDeviceId(stringData);