From 3e3cdaf674b07ac1c37b1552e2bd0c13b0f6594a Mon Sep 17 00:00:00 2001 From: YOUR_NAME Date: Sat, 9 Nov 2024 15:01:48 +0800 Subject: [PATCH] fix: fix fuzz test Signed-off-by: liujiandong --- .../hexvectohexstr_fuzzer.cpp | 11 ++++++++++- .../imsregcallback_fuzzer.cpp | 19 ++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/test/fuzztest/hexvectohexstr_fuzzer/hexvectohexstr_fuzzer.cpp b/test/fuzztest/hexvectohexstr_fuzzer/hexvectohexstr_fuzzer.cpp index 53c3ba3c7..4906ba549 100644 --- a/test/fuzztest/hexvectohexstr_fuzzer/hexvectohexstr_fuzzer.cpp +++ b/test/fuzztest/hexvectohexstr_fuzzer/hexvectohexstr_fuzzer.cpp @@ -19,19 +19,28 @@ #include #include "addcoreservicetoken_fuzzer.h" +#include #include "napi_util.h" #include "system_ability_definition.h" #include "tag_service.h" using namespace OHOS::Telephony; namespace OHOS { +constexpr int32_t TEST_MAX_UINT8 = 255; + +int32_t GetRandomInt(int min, int max, const uint8_t *data, size_t size) +{ + FuzzedDataProvider fdp(data, size); + return fdp.ConsumeIntegralInRange(min, max); +} + void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size) { if (data == nullptr || size == 0) { return; } - uint8_t result = static_cast(*data); + uint8_t result = static_cast(GetRandomInt(0, TEST_MAX_UINT8, data, size)); std::vector parameter; parameter.push_back(result); auto tagService = std::make_shared(parameter); diff --git a/test/fuzztest/imsregcallback_fuzzer/imsregcallback_fuzzer.cpp b/test/fuzztest/imsregcallback_fuzzer/imsregcallback_fuzzer.cpp index 418ad0fc6..f2e3a7c65 100644 --- a/test/fuzztest/imsregcallback_fuzzer/imsregcallback_fuzzer.cpp +++ b/test/fuzztest/imsregcallback_fuzzer/imsregcallback_fuzzer.cpp @@ -19,6 +19,7 @@ #include #include "addcoreservicetoken_fuzzer.h" +#include #include "ims_reg_info_callback_stub.h" #include "napi_ims_reg_info_callback.h" #include "napi_ims_reg_info_callback_manager.h" @@ -27,24 +28,32 @@ using namespace OHOS::Telephony; namespace OHOS { -constexpr int32_t SLOT_NUM = 2; constexpr int32_t BOOL_NUM = 2; constexpr int32_t TECH_NUM = 4; constexpr int32_t IMS_SERVICE_TYPE_NUM = 4; +constexpr int32_t MIN_SLOT_ID = -1; +constexpr int32_t MAX_SLOT_ID = 4; + +int32_t GetRandomInt(int min, int max, const uint8_t *data, size_t size) +{ + FuzzedDataProvider fdp(data, size); + return fdp.ConsumeIntegralInRange(min, max); +} + void OnRemoteRequest(const uint8_t *data, size_t size) { MessageParcel dataMessageParcel; if (!dataMessageParcel.WriteInterfaceToken(ImsRegInfoCallbackStub::GetDescriptor())) { return; } - int32_t slotId = static_cast(*data % SLOT_NUM); - int32_t reg = static_cast(*data % BOOL_NUM); - int32_t tech = static_cast(*data % TECH_NUM); + int32_t slotId = GetRandomInt(MIN_SLOT_ID, MAX_SLOT_ID, data, size); + int32_t reg = GetRandomInt(0, BOOL_NUM, data, size); + int32_t tech = GetRandomInt(0, TECH_NUM, data, size); dataMessageParcel.WriteInt32(slotId); dataMessageParcel.WriteInt32(reg); dataMessageParcel.WriteInt32(tech); - uint32_t code = static_cast(*data % IMS_SERVICE_TYPE_NUM); + uint32_t code = static_cast(GetRandomInt(0, IMS_SERVICE_TYPE_NUM, data, size)); MessageParcel reply; MessageOption option; sptr imsCallback = new NapiImsRegInfoCallback();