mirror of
https://gitee.com/openharmony/communication_dsoftbus
synced 2024-12-20 00:21:45 +00:00
Add fuzz testcase for dsoftbus
Signed-off-by: verystone <xudaqing@huawei.com>
This commit is contained in:
parent
42eb729817
commit
6f8959d4c8
@ -99,6 +99,7 @@
|
||||
"//foundation/communication/dsoftbus/tests/core/common/dfx/hisysevent_adapter/unittest:unittest",
|
||||
"//foundation/communication/dsoftbus/tests/core/common/message_handler:fuzztest",
|
||||
"//foundation/communication/dsoftbus/tests/core/common/json_utils:fuzztest",
|
||||
"//foundation/communication/dsoftbus/tests/core/common/log:fuzztest",
|
||||
"//foundation/communication/dsoftbus/tests/core/connection:unittest",
|
||||
"//foundation/communication/dsoftbus/tests/core/connection:fuzztest",
|
||||
"//foundation/communication/dsoftbus/tests/core/discovery:unittest",
|
||||
|
@ -15,6 +15,7 @@ group("fuzztest") {
|
||||
testonly = true
|
||||
deps = [
|
||||
"softbusadapterhisysevent_fuzzer:fuzztest",
|
||||
"softbusadapterrange_fuzzer:fuzztest",
|
||||
"softbusdecryptdata_fuzzer:SoftBusDecryptDataTest",
|
||||
]
|
||||
}
|
||||
|
48
tests/adapter/fuzztest/softbusadapterrange_fuzzer/BUILD.gn
Normal file
48
tests/adapter/fuzztest/softbusadapterrange_fuzzer/BUILD.gn
Normal file
@ -0,0 +1,48 @@
|
||||
# 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("//build/test.gni")
|
||||
import("//foundation/communication/dsoftbus/dsoftbus.gni")
|
||||
|
||||
##############################fuzztest##########################################
|
||||
ohos_fuzztest("SoftBusAdapterRangeFuzzTest") {
|
||||
module_out_path = "dsoftbus/adapter"
|
||||
fuzz_config_file = "//foundation/communication/dsoftbus/tests/adapter/fuzztest/softbusadapterrange_fuzzer"
|
||||
include_dirs = [ "$dsoftbus_root_path/adapter/common/include" ]
|
||||
cflags = [
|
||||
"-g",
|
||||
"-O0",
|
||||
"-Wno-unused-variable",
|
||||
"-fno-omit-frame-pointer",
|
||||
"-fstack-protector-strong",
|
||||
]
|
||||
sources = [ "softbusadapterrange_fuzzer.cpp" ]
|
||||
|
||||
deps = [ "//foundation/communication/dsoftbus/adapter:softbus_adapter" ]
|
||||
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
]
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
group("fuzztest") {
|
||||
testonly = true
|
||||
deps = [
|
||||
# deps file
|
||||
":SoftBusAdapterRangeFuzzTest",
|
||||
]
|
||||
}
|
||||
###############################################################################
|
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* 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
|
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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_config>
|
||||
<fuzztest>
|
||||
<!-- maximum length of a test input -->
|
||||
<max_len>1000</max_len>
|
||||
<!-- maximum total time in seconds to run the fuzzer -->
|
||||
<max_total_time>300</max_total_time>
|
||||
<!-- memory usage limit in Mb -->
|
||||
<rss_limit_mb>4096</rss_limit_mb>
|
||||
</fuzztest>
|
||||
</fuzz_config>
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 "softbusadapterrange_fuzzer.h"
|
||||
|
||||
#include <securec.h>
|
||||
#include "softbus_adapter_range.h"
|
||||
|
||||
namespace OHOS {
|
||||
static void SoftBusAdapterRangeFuzzTest(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (size < sizeof(SoftBusRangeParam)) {
|
||||
return;
|
||||
}
|
||||
|
||||
SoftBusRangeParam rangeParam;
|
||||
if (memcpy_s(&rangeParam, sizeof(SoftBusRangeParam), data, sizeof(SoftBusRangeParam)) != EOK) {
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t range;
|
||||
SoftBusBleRange(&rangeParam, &range);
|
||||
|
||||
int8_t power;
|
||||
SoftBusGetBlePower(&power);
|
||||
}
|
||||
}
|
||||
|
||||
/* Fuzzer entry point */
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (data == nullptr || size < sizeof(int32_t)) {
|
||||
return 0;
|
||||
}
|
||||
/* Run your code on data */
|
||||
OHOS::SoftBusAdapterRangeFuzzTest(data, size);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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 SOFTBUSADAPTERRANGE_FUZZER_H
|
||||
#define SOFTBUSADAPTERRANGE_FUZZER_H
|
||||
|
||||
#define FUZZ_PROJECT_NAME "softbusadapterrange_fuzzer"
|
||||
|
||||
#endif // SOFTBUSADAPTERRANGE_FUZZER_H
|
17
tests/core/common/log/BUILD.gn
Normal file
17
tests/core/common/log/BUILD.gn
Normal file
@ -0,0 +1,17 @@
|
||||
# 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.
|
||||
|
||||
group("fuzztest") {
|
||||
testonly = true
|
||||
deps = [ "fuzztest/softbuslog_fuzzer:fuzztest" ]
|
||||
}
|
66
tests/core/common/log/fuzztest/softbuslog_fuzzer/BUILD.gn
Normal file
66
tests/core/common/log/fuzztest/softbuslog_fuzzer/BUILD.gn
Normal file
@ -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.
|
||||
|
||||
#####################hydra-fuzz###################
|
||||
import("//build/config/features.gni")
|
||||
import("//build/ohos.gni")
|
||||
import("//build/test.gni")
|
||||
import("//foundation/communication/dsoftbus/dsoftbus.gni")
|
||||
|
||||
##############################fuzztest##########################################
|
||||
ohos_fuzztest("SoftBusLogFuzzTest") {
|
||||
module_out_path = "dsoftbus/common"
|
||||
fuzz_config_file =
|
||||
"$dsoftbus_root_path/tests/core/common/log/fuzztest/softbuslog_fuzzer"
|
||||
include_dirs = [
|
||||
"$dsoftbus_root_path/adapter/common/include",
|
||||
"$dsoftbus_root_path/core/common/include",
|
||||
"//commonlibrary/c_utils/base/include",
|
||||
]
|
||||
cflags = [
|
||||
"-g",
|
||||
"-O0",
|
||||
"-Wno-unused-variable",
|
||||
"-fno-omit-frame-pointer",
|
||||
"-fstack-protector-strong",
|
||||
]
|
||||
sources = [ "softbuslog_fuzzer.cpp" ]
|
||||
|
||||
deps = [
|
||||
"$dsoftbus_root_path/adapter:softbus_adapter",
|
||||
"$dsoftbus_root_path/core/common:softbus_utils",
|
||||
"$dsoftbus_root_path/core/frame:softbus_server",
|
||||
]
|
||||
|
||||
if (is_standard_system) {
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hiviewdfx_hilog_native:libhilog",
|
||||
]
|
||||
} else {
|
||||
external_deps = [
|
||||
"c_utils:utils",
|
||||
"hilog:libhilog",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
###############################################################################
|
||||
group("fuzztest") {
|
||||
testonly = true
|
||||
deps = [
|
||||
# deps file
|
||||
":SoftBusLogFuzzTest",
|
||||
]
|
||||
}
|
||||
###############################################################################
|
16
tests/core/common/log/fuzztest/softbuslog_fuzzer/corpus/init
Normal file
16
tests/core/common/log/fuzztest/softbuslog_fuzzer/corpus/init
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* 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
|
25
tests/core/common/log/fuzztest/softbuslog_fuzzer/project.xml
Normal file
25
tests/core/common/log/fuzztest/softbuslog_fuzzer/project.xml
Normal file
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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_config>
|
||||
<fuzztest>
|
||||
<!-- maximum length of a test input -->
|
||||
<max_len>1000</max_len>
|
||||
<!-- maximum total time in seconds to run the fuzzer -->
|
||||
<max_total_time>300</max_total_time>
|
||||
<!-- memory usage limit in Mb -->
|
||||
<rss_limit_mb>4096</rss_limit_mb>
|
||||
</fuzztest>
|
||||
</fuzz_config>
|
@ -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 "softbuslog_fuzzer.h"
|
||||
|
||||
#include "softbus_log.h"
|
||||
#include <cstddef>
|
||||
#include <securec.h>
|
||||
#include <string>
|
||||
#include "softbus_adapter_mem.h"
|
||||
|
||||
namespace OHOS {
|
||||
static constexpr size_t MAX_BUFFER_LEN = 100;
|
||||
static void NstackxLogTest(const char *buffer)
|
||||
{
|
||||
NstackxLog(buffer, SOFTBUS_LOG_INFO, buffer);
|
||||
}
|
||||
|
||||
static void AnonymizesTest(const char *buffer)
|
||||
{
|
||||
Anonymizes(buffer, 10);
|
||||
}
|
||||
|
||||
static void AnonyPacketPrintoutTest(const char *buffer, size_t size)
|
||||
{
|
||||
AnonyPacketPrintout(SOFTBUS_LOG_DISC, buffer, buffer, size);
|
||||
}
|
||||
|
||||
static void AnonyDevIdTest(const char *buffer)
|
||||
{
|
||||
char *outName = nullptr;
|
||||
AnonyDevId(&outName, buffer);
|
||||
}
|
||||
} // namespace OHOS
|
||||
|
||||
/* Fuzzer entry point */
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (data == nullptr || size < OHOS::MAX_BUFFER_LEN) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char buffer[OHOS::MAX_BUFFER_LEN] = { 0 };
|
||||
if (memcpy_s(buffer, OHOS::MAX_BUFFER_LEN, data, size) != EOK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
OHOS::NstackxLogTest(buffer);
|
||||
OHOS::AnonymizesTest(buffer);
|
||||
OHOS::AnonyPacketPrintoutTest(buffer, OHOS::MAX_BUFFER_LEN);
|
||||
OHOS::AnonyDevIdTest(buffer);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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 SOFTBUSLOG_FUZZER_H
|
||||
#define SOFTBUSLOG_FUZZER_H
|
||||
|
||||
#define FUZZ_PROJECT_NAME "softbuslog_fuzzer"
|
||||
|
||||
#endif // SOFTBUSLOG_FUZZER_H
|
@ -14,11 +14,22 @@
|
||||
*/
|
||||
|
||||
#include "connectioncommon_fuzzer.h"
|
||||
|
||||
#include <vector>
|
||||
#include <securec.h>
|
||||
#include <pthread.h>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include "softbus_datahead_transform.h"
|
||||
#include "softbus_socket.h"
|
||||
#include "softbus_tcp_socket.h"
|
||||
#include "softbus_conn_manager.h"
|
||||
#include "softbus_base_listener.h"
|
||||
#include "softbus_protocol_def.h"
|
||||
#include "softbus_thread_pool.h"
|
||||
|
||||
static pthread_mutex_t g_isInitedLock;
|
||||
static int g_count = 0;
|
||||
|
||||
namespace OHOS {
|
||||
static void DoDataHeadTransformFuzz(const uint8_t *data, size_t size)
|
||||
@ -85,6 +96,57 @@ static void DoSocketFuzz(const uint8_t *data, size_t size)
|
||||
}
|
||||
ConnDeinitSockets();
|
||||
}
|
||||
|
||||
static int32_t ConnectEvent(ListenerModule module, int32_t events, int32_t cfd, const ConnectOption *clientAddr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t DataEvent(ListenerModule module, int32_t events, int32_t fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void DoBaseListenerFuzz(const uint8_t *data, size_t size)
|
||||
{
|
||||
ListenerModule module = DIRECT_CHANNEL_CLIENT;
|
||||
StartBaseClient(module);
|
||||
StopBaseListener(module);
|
||||
DestroyBaseListener(module);
|
||||
|
||||
LocalListenerInfo info;
|
||||
if (memcpy_s(&info, sizeof(LocalListenerInfo), data, size) == EOK) {
|
||||
StartBaseListener(&info);
|
||||
}
|
||||
|
||||
SoftbusBaseListener listener;
|
||||
listener.onConnectEvent = ConnectEvent;
|
||||
listener.onDataEvent = DataEvent;
|
||||
GetSoftbusBaseListener(module, &listener);
|
||||
SetSoftbusBaseListener(module, &listener);
|
||||
}
|
||||
|
||||
static void DoTriggerFuzz()
|
||||
{
|
||||
AddTrigger(AUTH_P2P, 0, WRITE_TRIGGER);
|
||||
DelTrigger(AUTH_P2P, 0, WRITE_TRIGGER);
|
||||
}
|
||||
|
||||
static int32_t ThreadPoolTask(void* arg)
|
||||
{
|
||||
pthread_mutex_lock(&g_isInitedLock);
|
||||
g_count++;
|
||||
pthread_mutex_unlock(&g_isInitedLock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void DoThreadPoolFuzz()
|
||||
{
|
||||
ThreadPool* pool = ThreadPoolInit(5, 5);
|
||||
ThreadPoolAddJob(pool, ThreadPoolTask, nullptr, PERSISTENT, (uintptr_t)0);
|
||||
ThreadPoolRemoveJob(pool, (uintptr_t)0);
|
||||
ThreadPoolDestroy(pool);
|
||||
}
|
||||
}
|
||||
/* Fuzzer entry point */
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
@ -95,5 +157,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
/* Run your code on data */
|
||||
OHOS::DoDataHeadTransformFuzz(data, size);
|
||||
OHOS::DoSocketFuzz(data, size);
|
||||
OHOS::DoBaseListenerFuzz(data, size);
|
||||
OHOS::DoTriggerFuzz();
|
||||
OHOS::DoThreadPoolFuzz();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user