From f8e4921037362a9eec192cb40d06f423d1173bf4 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Tue, 26 Aug 2025 14:18:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangzhaohao --- .../businesseventmanager_fuzzer.cpp | 28 ++--- .../profileutils_fuzzer.cpp | 98 +++-------------- .../trustprofilemanager_fuzzer.cpp | 103 ++++-------------- ...stributed_device_profile_stub_new_test.cpp | 4 +- .../core/test/unittest/ipc_utils_test.cpp | 4 +- 5 files changed, 57 insertions(+), 180 deletions(-) diff --git a/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.cpp b/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.cpp index be4656bf..87bfd92f 100644 --- a/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.cpp +++ b/services/core/test/fuzztest/businesseventmanager_fuzzer/businesseventmanager_fuzzer.cpp @@ -31,12 +31,8 @@ namespace OHOS { namespace DistributedDeviceProfile { -void PutBusinessEventFuzzTest(const uint8_t* data, size_t size) +void PutBusinessEventFuzzTest(FuzzedDataProvider& fdp) { - if ((data == nullptr) || (size < sizeof(int64_t))) { - return; - } - FuzzedDataProvider fdp(data, size); std::string businessKey = fdp.ConsumeRandomLengthString(); std::string businessValue = fdp.ConsumeRandomLengthString(); if (businessKey.empty() || businessValue.empty() || businessKey.size() > MAX_STRING_LEN || @@ -51,12 +47,8 @@ void PutBusinessEventFuzzTest(const uint8_t* data, size_t size) BusinessEventManager::GetInstance().PutBusinessEvent(businessEvent); } -void GetBusinessEventFuzzTest(const uint8_t* data, size_t size) +void GetBusinessEventFuzzTest(FuzzedDataProvider& fdp) { - if ((data == nullptr) || (size < sizeof(int64_t))) { - return; - } - FuzzedDataProvider fdp(data, size); std::string businessKey = fdp.ConsumeRandomLengthString(); if (businessKey.empty() || businessKey.size() > MAX_STRING_LEN) { return; @@ -66,13 +58,23 @@ void GetBusinessEventFuzzTest(const uint8_t* data, size_t size) businessEvent.SetBusinessKey(businessKey); BusinessEventManager::GetInstance().GetBusinessEvent(businessEvent); } + +void BusinessEventManagerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int64_t))) { + return; + } + FuzzedDataProvider fdp(data, size); + + PutBusinessEventFuzzTest(fdp); + GetBusinessEventFuzzTest(fdp); +} } } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - OHOS::DistributedDeviceProfile::PutBusinessEventFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GetBusinessEventFuzzTest(data, size); + OHOS::DistributedDeviceProfile::BusinessEventManagerFuzzTest(data, size); return 0; -} \ No newline at end of file +} diff --git a/services/core/test/fuzztest/profileutils_fuzzer/profileutils_fuzzer.cpp b/services/core/test/fuzztest/profileutils_fuzzer/profileutils_fuzzer.cpp index 8768bb99..91e6b651 100644 --- a/services/core/test/fuzztest/profileutils_fuzzer/profileutils_fuzzer.cpp +++ b/services/core/test/fuzztest/profileutils_fuzzer/profileutils_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -13,6 +13,8 @@ * limitations under the License. */ +#include + #include "profileutils_fuzzer.h" #include @@ -30,85 +32,29 @@ namespace OHOS { namespace DistributedDeviceProfile { -void GetProfileTypeFuzzTest(const uint8_t* data, size_t size) +void ProfileUtilsFuzzTest(const uint8_t* data, size_t size) { - if ((data == nullptr) || (size == 0)) { + if ((data == nullptr) || (size < sizeof(int64_t))) { return; } - std::string dbKey(reinterpret_cast(data), size); - ProfileUtils::GetProfileType(dbKey); -} - -void StartsWithFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - std::string str(reinterpret_cast(data), size); - std::string prefix(reinterpret_cast(data), size); - ProfileUtils::StartsWith(str, prefix); -} - -void SplitStringFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - std::string str(reinterpret_cast(data), size); - std::string splits(reinterpret_cast(data), size); + FuzzedDataProvider fdp(data, size); + std::string dbKey = fdp.ConsumeRandomLengthString(); + std::string str = fdp.ConsumeRandomLengthString(); + std::string prefix = fdp.ConsumeRandomLengthString(); + std::string splits = fdp.ConsumeRandomLengthString(); std::vector res; - std::string strs(reinterpret_cast(data), size); - res.push_back(strs); - ProfileUtils::SplitString(str, splits, res); -} + std::string deviceId = fdp.ConsumeRandomLengthString(); + std::string serviceName = fdp.ConsumeRandomLengthString(); + std::string charKey = fdp.ConsumeRandomLengthString(); + std::string profileProperty = fdp.ConsumeRandomLengthString(); + std::string profileKey = fdp.ConsumeRandomLengthString(); -void IsKeyValidFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - std::string dbKey(reinterpret_cast(data), size); + ProfileUtils::GetProfileType(dbKey); + ProfileUtils::StartsWith(str, prefix); ProfileUtils::IsKeyValid(dbKey); -} - -void GenerateDeviceProfileKeyFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - std::string deviceId(reinterpret_cast(data), size); ProfileUtils::GenerateDeviceProfileKey(deviceId); -} - -void GenerateServiceProfileKeyFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - std::string deviceId(reinterpret_cast(data), size); - std::string serviceName(reinterpret_cast(data), size); ProfileUtils::GenerateServiceProfileKey(deviceId, serviceName); -} - -void GenerateCharProfileKeyFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - std::string deviceId(reinterpret_cast(data), size); - std::string serviceName(reinterpret_cast(data), size); - std::string charKey(reinterpret_cast(data), size); ProfileUtils::GenerateCharProfileKey(deviceId, serviceName, charKey); -} - -void GenerateDBKeyFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - std::string profileProperty(reinterpret_cast(data), size); - std::string profileKey(reinterpret_cast(data), size); - ProfileUtils::GenerateDBKey(profileKey, profileProperty); } } @@ -117,14 +63,6 @@ void GenerateDBKeyFuzzTest(const uint8_t* data, size_t size) /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - OHOS::DistributedDeviceProfile::GetProfileTypeFuzzTest(data, size); - OHOS::DistributedDeviceProfile::StartsWithFuzzTest(data, size); - OHOS::DistributedDeviceProfile::SplitStringFuzzTest(data, size); - OHOS::DistributedDeviceProfile::IsKeyValidFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GenerateDeviceProfileKeyFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GenerateServiceProfileKeyFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GenerateCharProfileKeyFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GenerateDBKeyFuzzTest(data, size); - + OHOS::DistributedDeviceProfile::ProfileUtilsFuzzTest(data, size); return 0; } diff --git a/services/core/test/fuzztest/trustprofilemanager_fuzzer/trustprofilemanager_fuzzer.cpp b/services/core/test/fuzztest/trustprofilemanager_fuzzer/trustprofilemanager_fuzzer.cpp index 270cc537..62ffe950 100644 --- a/services/core/test/fuzztest/trustprofilemanager_fuzzer/trustprofilemanager_fuzzer.cpp +++ b/services/core/test/fuzztest/trustprofilemanager_fuzzer/trustprofilemanager_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -31,91 +31,34 @@ namespace OHOS { namespace DistributedDeviceProfile { - -void GetAccessControlProfileByTokenIdFuzzTest(const uint8_t* data, size_t size) +void TrustProfileManagerFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size < sizeof(int64_t))) { return; } + FuzzedDataProvider fdp(data, size); - int64_t tokenId = fdp.ConsumeIntegral(); std::string trustDeviceId = fdp.ConsumeRandomLengthString(); - int32_t status = fdp.ConsumeIntegral(); - std::vector profile; - AccessControlProfile acProfile; - profile.push_back(acProfile); - TrustProfileManager::GetInstance().GetAccessControlProfileByTokenId(tokenId, trustDeviceId, status, profile); -} - -void GetAccessControlProfileOneFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size < sizeof(int32_t))) { - return; - } - std::string accountId(reinterpret_cast(data), size); - int32_t userId = *(reinterpret_cast(data)); - std::vector profile; - AccessControlProfile acProfile; - profile.push_back(acProfile); - TrustProfileManager::GetInstance().GetAccessControlProfile(userId, accountId, profile); -} + std::string accountId = fdp.ConsumeRandomLengthString(); + std::string bundleName = fdp.ConsumeRandomLengthString(); + std::string deviceId = fdp.ConsumeRandomLengthString(); -void GetAccessControlProfileTwoFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size < sizeof(int32_t))) { - return; - } - int32_t userId = *(reinterpret_cast(data)); - std::vector profile; - AccessControlProfile acProfile; - profile.push_back(acProfile); - TrustProfileManager::GetInstance().GetAccessControlProfile(userId, profile); -} - -void GetAccessControlProfileThreeFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size < sizeof(int32_t))) { - return; - } - std::string bundleName(reinterpret_cast(data), size); - int32_t bindType = *(reinterpret_cast(data)); - int32_t status = *(reinterpret_cast(data)); - std::vector profile; - AccessControlProfile acProfile; - profile.push_back(acProfile); - TrustProfileManager::GetInstance().GetAccessControlProfile(bundleName, bindType, status, profile); -} + int32_t status = fdp.ConsumeIntegral(); + int32_t userId = fdp.ConsumeIntegral(); + int32_t bindType = fdp.ConsumeIntegral(); -void GetAccessControlProfileFourFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size < sizeof(int32_t))) { - return; - } - std::string bundleName(reinterpret_cast(data), size); - std::string trustDeviceId(reinterpret_cast(data), size); - int32_t status = *(reinterpret_cast(data)); - std::vector profile; - AccessControlProfile acProfile; - profile.push_back(acProfile); - TrustProfileManager::GetInstance().GetAccessControlProfile(bundleName, trustDeviceId, status, profile); -} + int64_t tokenId = fdp.ConsumeIntegral(); + int64_t accessControlId = fdp.ConsumeIntegral(); + + std::vector outProfile; -void DeleteTrustDeviceProfileFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size < sizeof(int32_t))) { - return; - } - std::string deviceId(reinterpret_cast(data), size); + TrustProfileManager::GetInstance().GetAccessControlProfileByTokenId(tokenId, trustDeviceId, status, outProfile); + TrustProfileManager::GetInstance().GetAccessControlProfile(userId, accountId, outProfile); + TrustProfileManager::GetInstance().GetAccessControlProfile(userId, outProfile); + TrustProfileManager::GetInstance().GetAccessControlProfile(bundleName, bindType, status, outProfile); + TrustProfileManager::GetInstance().GetAccessControlProfile(bundleName, trustDeviceId, status, outProfile); TrustProfileManager::GetInstance().DeleteTrustDeviceProfile(deviceId); -} - -void DeleteAccessControlProfileFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size < sizeof(int64_t))) { - return; - } - int64_t status = *(reinterpret_cast(data)); - TrustProfileManager::GetInstance().DeleteAccessControlProfile(status); + TrustProfileManager::GetInstance().DeleteAccessControlProfile(accessControlId); } } } @@ -123,12 +66,6 @@ void DeleteAccessControlProfileFuzzTest(const uint8_t* data, size_t size) /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - OHOS::DistributedDeviceProfile::GetAccessControlProfileByTokenIdFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GetAccessControlProfileOneFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GetAccessControlProfileTwoFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GetAccessControlProfileThreeFuzzTest(data, size); - OHOS::DistributedDeviceProfile::GetAccessControlProfileFourFuzzTest(data, size); - OHOS::DistributedDeviceProfile::DeleteTrustDeviceProfileFuzzTest(data, size); - OHOS::DistributedDeviceProfile::DeleteAccessControlProfileFuzzTest(data, size); + OHOS::DistributedDeviceProfile::TrustProfileManagerFuzzTest(data, size); return 0; } diff --git a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp index 7bd48b73..c16f50bd 100644 --- a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp @@ -630,7 +630,7 @@ HWTEST_F(DistributedDeviceProfileStubNewTest, GetAllAccessControlProfileInner_00 MessageParcel data; MessageParcel reply; int32_t ret = ProfileStub_->GetAllAccessControlProfileInner(data, reply); - EXPECT_EQ(DP_WRITE_PARCEL_FAIL, ret); + EXPECT_EQ(DP_SUCCESS, ret); } /** @@ -924,7 +924,7 @@ HWTEST_F(DistributedDeviceProfileStubNewTest, NotifyAclEventInner_007, TestSize. MessageParcel reply; MessageOption option; int32_t ret = ProfileStub_->NotifyAclEventInner(code, data, reply, option); - EXPECT_EQ(DP_WRITE_PARCEL_FAIL, ret); + EXPECT_EQ(DP_SUCCESS, ret); } /** diff --git a/services/core/test/unittest/ipc_utils_test.cpp b/services/core/test/unittest/ipc_utils_test.cpp index c0a0baef..a048b874 100644 --- a/services/core/test/unittest/ipc_utils_test.cpp +++ b/services/core/test/unittest/ipc_utils_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -87,7 +87,7 @@ HWTEST_F(IpcUtilsTest, Marshalling_002, TestSize.Level1) OHOS::MessageParcel parcel; std::vector profiles; bool ret = IpcUtils::Marshalling(parcel, profiles); - EXPECT_EQ(ret, false); + EXPECT_EQ(ret, true); ret = IpcUtils::UnMarshalling(parcel, profiles); EXPECT_EQ(ret, false); AccessControlProfile aclProfile; -- Gitee