Test: Add Fuzz Test for SoftBusServerStub & SoftbusClientStub with OnRemoteRequest

Signed-off-by: zhusiyuan <zhusiyuan2@huawei.com>
This commit is contained in:
zhusiyuan 2024-07-30 10:32:20 +08:00
parent 4f92ba03ad
commit 26daff14ee
14 changed files with 574 additions and 4 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2024 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 ACCESSTOKEN_KIT_H
#define ACCESSTOKEN_KIT_H
#include <string>
namespace OHOS {
namespace Security {
namespace AccessToken {
typedef uint32_t AccessTokenID;
typedef enum TypeATokenTypeEnum {
TOKEN_INVALID = -1,
TOKEN_HAP = 0,
TOKEN_NATIVE,
TOKEN_SHELL,
TOKEN_TYPE_BUTT,
} ATokenTypeEnum;
class AccessTokenKit {
public:
static ATokenTypeEnum GetTokenTypeFlag(AccessTokenID tokenID);
static int32_t VerifyAccessToken(AccessTokenID tokenID, const std::string &permissionName);
};
} // namespace AccessToken
} // namespace Security
} // namespace OHOS
#endif // ACCESSTOKEN_KIT_H

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 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 "accesstoken_kit.h"
namespace OHOS {
namespace Security {
namespace AccessToken {
ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID)
{
return TOKEN_NATIVE;
}
int32_t AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string &permissionName)
{
return 0;
}
} // namespace AccessToken
} // namespace Security
} // namespace OHOS

View File

@ -1,4 +1,4 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Copyright (c) 2023-2024 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,5 +13,8 @@
group("fuzztest") {
testonly = true
deps = [ "softbusserverstub_fuzzer:fuzztest" ]
deps = [
"softbusserverstub2_fuzzer:fuzztest",
"softbusserverstub_fuzzer:fuzztest",
]
}

View File

@ -0,0 +1,67 @@
# Copyright (c) 2024 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("../../../../../dsoftbus.gni")
##############################fuzztest##########################################
dsoftbus_root_path = "../../../../.."
ohos_fuzztest("SoftBusServerStub2FuzzTest") {
module_out_path = dsoftbus_fuzz_out_path
fuzz_config_file =
"$dsoftbus_root_path/tests/core/frame/fuzztest/softbusserverstub2_fuzzer"
sources = [
"$dsoftbus_root_path/tests/common/src/accesstoken_kit.cpp",
"softbusserverstub2_fuzzer.cpp",
]
include_dirs = [
"$dsoftbus_root_path/core/bus_center/interface",
"$dsoftbus_root_path/core/common/include",
"$dsoftbus_root_path/core/discovery/interface",
"$dsoftbus_root_path/core/discovery/manager/include",
"$dsoftbus_root_path/core/frame/common/include",
"$dsoftbus_root_path/core/frame/standard/init/include",
"$dsoftbus_root_path/interfaces/kits/common",
"$dsoftbus_root_path/tests/common/include",
]
deps = [
"$dsoftbus_root_path/core/common:softbus_utils",
"$dsoftbus_root_path/core/frame:softbus_server",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"ipc:ipc_single",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
]
cflags = [
"-g",
"-O0",
"-fno-omit-frame-pointer",
]
}
group("fuzztest") {
testonly = true
deps = [ ":SoftBusServerStub2FuzzTest" ]
}

View File

@ -0,0 +1,16 @@
/*
* Copyright (c) 2024 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

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2024 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>

View File

@ -0,0 +1,131 @@
/*
* Copyright (c) 2024 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 "softbusserverstub2_fuzzer.h"
#include "softbus_server_ipc_interface_code.h"
#include "softbus_server.h"
extern "C" {
bool GetServerIsInit(void)
{
return true;
}
} // extern "C"
namespace {
constexpr int32_t SOFTBUS_SERVER_SA_ID = 4700;
const std::u16string SOFTBUS_SERVER_STUB_INTERFACE_TOKEN = u"OHOS.ISoftBusServer";
const std::vector<SoftBusFuncId> CODE_LIST = {
MANAGE_REGISTER_SERVICE,
SERVER_PUBLISH_SERVICE,
SERVER_UNPUBLISH_SERVICE,
SERVER_CREATE_SESSION_SERVER,
SERVER_REMOVE_SESSION_SERVER,
SERVER_OPEN_SESSION,
SERVER_OPEN_AUTH_SESSION,
SERVER_NOTIFY_AUTH_SUCCESS,
SERVER_CLOSE_CHANNEL,
SERVER_CLOSE_CHANNEL_STATISTICS,
SERVER_SESSION_SENDMSG,
SERVER_QOS_REPORT,
SERVER_GRANT_PERMISSION,
SERVER_REMOVE_PERMISSION,
SERVER_STREAM_STATS,
SERVER_GET_SOFTBUS_SPEC_OBJECT,
SERVER_START_DISCOVERY,
SERVER_STOP_DISCOVERY,
SERVER_JOIN_LNN,
SERVER_JOIN_METANODE,
SERVER_LEAVE_LNN,
SERVER_LEAVE_METANODE,
SERVER_GET_ALL_ONLINE_NODE_INFO,
SERVER_GET_LOCAL_DEVICE_INFO,
SERVER_GET_NODE_KEY_INFO,
SERVER_SET_NODE_DATA_CHANGE_FLAG,
SERVER_START_TIME_SYNC,
SERVER_STOP_TIME_SYNC,
SERVER_PUBLISH_LNN,
SERVER_STOP_PUBLISH_LNN,
SERVER_REFRESH_LNN,
SERVER_STOP_REFRESH_LNN,
SERVER_ACTIVE_META_NODE,
SERVER_DEACTIVE_META_NODE,
SERVER_GET_ALL_META_NODE_INFO,
SERVER_SHIFT_LNN_GEAR,
SERVER_RIPPLE_STATS,
SERVER_GET_BUS_CENTER_EX_OBJ,
SERVER_EVALUATE_QOS,
SERVER_RELEASE_RESOURCES,
SERVER_REG_DATA_LEVEL_CHANGE_CB,
SERVER_UNREG_DATA_LEVEL_CHANGE_CB,
SERVER_SET_DATA_LEVEL,
};
class TestEnv {
public:
TestEnv()
{
isInited_ = false;
stub_ = new OHOS::SoftBusServer(SOFTBUS_SERVER_SA_ID, true);
if (stub_ == nullptr) {
return;
}
isInited_ = true;
}
~TestEnv()
{
isInited_ = false;
stub_ = nullptr;
}
bool IsInited(void) const noexcept
{
return isInited_;
}
void DoRemoteRequest(SoftBusFuncId code, OHOS::MessageParcel &data)
{
OHOS::MessageParcel reply;
OHOS::MessageOption option;
if (stub_ != nullptr) {
stub_->OnRemoteRequest(static_cast<uint32_t>(code), data, reply, option);
}
}
private:
volatile bool isInited_;
OHOS::sptr<OHOS::SoftBusServer> stub_;
};
} // anonymous namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
static size_t codeIndex = 0;
static TestEnv env;
if (!env.IsInited()) {
return 0;
}
SoftBusFuncId code = CODE_LIST[codeIndex++ % CODE_LIST.size()];
OHOS::MessageParcel parcel;
parcel.WriteInterfaceToken(SOFTBUS_SERVER_STUB_INTERFACE_TOKEN);
parcel.WriteBuffer(data, size);
env.DoRemoteRequest(code, parcel);
return 0;
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 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 SOFTBUSSERVERSTUB2_FUZZER_H
#define SOFTBUSSERVERSTUB2_FUZZER_H
#define FUZZ_PROJECT_NAME "softbusserverstub2_fuzzer"
#endif // SOFTBUSSERVERSTUB2_FUZZER_H

View File

@ -1,4 +1,4 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Copyright (c) 2023-2024 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,5 +13,8 @@
group("fuzztest") {
testonly = true
deps = [ "softbusclientstub_fuzzer:SoftbusClientStubFuzzTest" ]
deps = [
"softbusclientstub2_fuzzer:fuzztest",
"softbusclientstub_fuzzer:fuzztest",
]
}

View File

@ -0,0 +1,55 @@
# Copyright (c) 2024 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("../../../../../dsoftbus.gni")
dsoftbus_root_path = "../../../../.."
##############################fuzztest##########################################
ohos_fuzztest("SoftbusClientStub2FuzzTest") {
module_out_path = dsoftbus_fuzz_out_path
fuzz_config_file =
"$dsoftbus_root_path/tests/sdk/frame/fuzztest/softbusclientstub2_fuzzer"
sources = [ "softbusclientstub2_fuzzer.cpp" ]
include_dirs = [
"$dsoftbus_root_path/adapter/common/include",
"$dsoftbus_root_path/core/common/dfx/interface/include",
"$dsoftbus_root_path/core/common/include",
"$dsoftbus_root_path/sdk/frame/standard/include",
]
deps = [ "$dsoftbus_root_path/tests/sdk/frame/fuzztest/softbusclientstub_fuzzer:softbus_client_static" ]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"ipc:ipc_core",
"ipc:ipc_single",
]
cflags = [
"-g",
"-O0",
"-fno-omit-frame-pointer",
"-fstack-protector-strong",
]
}
group("fuzztest") {
testonly = true
deps = [ ":SoftbusClientStub2FuzzTest" ]
}

View File

@ -0,0 +1,16 @@
/*
* Copyright (c) 2024 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

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2024 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>

View File

@ -0,0 +1,111 @@
/*
* Copyright (c) 2024 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 "softbusclientstub2_fuzzer.h"
#include "softbus_client_stub.h"
#include "softbus_server_ipc_interface_code.h"
namespace {
const std::u16string SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN = u"OHOS.ISoftBusClient";
const std::vector<SoftBusFuncId> CODE_LIST = {
CLIENT_ON_CHANNEL_OPENED,
CLIENT_ON_CHANNEL_OPENFAILED,
CLIENT_ON_CHANNEL_LINKDOWN,
CLIENT_ON_CHANNEL_CLOSED,
CLIENT_ON_CHANNEL_MSGRECEIVED,
CLIENT_ON_CHANNEL_QOSEVENT,
CLIENT_DISCOVERY_SUCC,
CLIENT_DISCOVERY_FAIL,
CLIENT_DISCOVERY_DEVICE_FOUND,
CLIENT_PUBLISH_SUCC,
CLIENT_PUBLISH_FAIL,
CLIENT_ON_JOIN_RESULT,
CLIENT_ON_JOIN_METANODE_RESULT,
CLIENT_ON_LEAVE_RESULT,
CLIENT_ON_LEAVE_METANODE_RESULT,
CLIENT_ON_NODE_DEVICE_NOT_TRUST,
CLIENT_ON_NODE_ONLINE_STATE_CHANGED,
CLIENT_ON_NODE_BASIC_INFO_CHANGED,
CLIENT_ON_LOCAL_NETWORK_ID_CHANGED,
CLIENT_ON_TIME_SYNC_RESULT,
CLIENT_ON_PUBLISH_LNN_RESULT,
CLIENT_ON_REFRESH_LNN_RESULT,
CLIENT_ON_REFRESH_DEVICE_FOUND,
CLIENT_ON_PERMISSION_CHANGE,
CLIENT_SET_CHANNEL_INFO,
CLIENT_ON_DATA_LEVEL_CHANGED,
CLIENT_ON_TRANS_LIMIT_CHANGE,
CLIENT_ON_CHANNEL_BIND,
};
class TestEnv {
public:
TestEnv()
{
isInited_ = false;
stub_ = new OHOS::SoftBusClientStub();
if (stub_ == nullptr) {
return;
}
isInited_ = true;
}
~TestEnv()
{
isInited_ = false;
stub_ = nullptr;
}
bool IsInited(void) const noexcept
{
return isInited_;
}
void DoRemoteRequest(SoftBusFuncId code, OHOS::MessageParcel &data)
{
if (code == CLIENT_ON_CHANNEL_OPENFAILED) {
return;
}
OHOS::MessageParcel reply;
OHOS::MessageOption option;
if (stub_ != nullptr) {
stub_->OnRemoteRequest(static_cast<uint32_t>(code), data, reply, option);
}
}
private:
volatile bool isInited_;
OHOS::sptr<OHOS::SoftBusClientStub> stub_;
};
} // anonymous namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
static size_t codeIndex = 0;
static TestEnv env;
if (!env.IsInited()) {
return 0;
}
SoftBusFuncId code = CODE_LIST[codeIndex++ % CODE_LIST.size()];
OHOS::MessageParcel parcel;
parcel.WriteInterfaceToken(SOFTBUS_CLIENT_STUB_INTERFACE_TOKEN);
parcel.WriteBuffer(data, size);
env.DoRemoteRequest(code, parcel);
return 0;
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 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 SOFTBUSCLIENTSTUB2_FUZZER_H
#define SOFTBUSCLIENTSTUB2_FUZZER_H
#define FUZZ_PROJECT_NAME "softbusclientstub2_fuzzer"
#endif // SOFTBUSCLIENTSTUB2_FUZZER_H