!808 add stub fuzz testcases

Merge pull request !808 from wangpggg/master
This commit is contained in:
openharmony_ci 2024-03-26 12:21:01 +00:00 committed by Gitee
commit cbce2fe255
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 296 additions and 5 deletions

View File

@ -29,5 +29,6 @@ group("user_file_service_fuzz_test") {
"externalfileaccessrename_fuzzer:ExternalFileAccessRenameFuzzTest", "externalfileaccessrename_fuzzer:ExternalFileAccessRenameFuzzTest",
"externalfileaccessscanfile_fuzzer:ExternalFileAccessScanFileFuzzTest", "externalfileaccessscanfile_fuzzer:ExternalFileAccessScanFileFuzzTest",
"fileaccessextstub_fuzzer:FileAccessExtStubFuzzTest", "fileaccessextstub_fuzzer:FileAccessExtStubFuzzTest",
"fileaccessservicestub_fuzzer:FileAccessServiceStubFuzzTest",
] ]
} }

View File

@ -45,7 +45,7 @@ bool OnRemoteRequestFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, co
return true; return true;
} }
uint32_t code = reinterpret_cast<uint32_t>(data); uint32_t code = TypeCast<uint32_t>(data);
MessageParcel msg; MessageParcel msg;
msg.WriteInterfaceToken(FileAccessExtStub::GetDescriptor()); msg.WriteInterfaceToken(FileAccessExtStub::GetDescriptor());
MessageParcel reply; MessageParcel reply;
@ -160,8 +160,8 @@ bool CmdRenameFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const ui
bool CmdListFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size) bool CmdListFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{ {
size_t minSize = sizeof(int64_t) + sizeof(int32_t) + sizeof(int64_t) + sizeof(int64_t) + sizeof(int64_t) size_t minSize = sizeof(int64_t) + sizeof(int32_t) + sizeof(int64_t) + sizeof(int64_t) + sizeof(int64_t) +
+ sizeof(double) + sizeof(bool) + sizeof(bool); sizeof(double) + sizeof(bool) + sizeof(bool);
if (data == nullptr || size < minSize) { if (data == nullptr || size < minSize) {
return true; return true;
} }
@ -210,8 +210,8 @@ bool CmdListFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const
bool CmdScanFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size) bool CmdScanFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{ {
size_t minSize = sizeof(int64_t) + sizeof(int64_t) + sizeof(int32_t) + sizeof(int64_t) + sizeof(int64_t) size_t minSize = sizeof(int64_t) + sizeof(int64_t) + sizeof(int32_t) + sizeof(int64_t) + sizeof(int64_t) +
+ sizeof(int64_t) + sizeof(double) + sizeof(bool) + sizeof(bool); sizeof(int64_t) + sizeof(double) + sizeof(bool) + sizeof(bool);
if (data == nullptr || size < minSize) { if (data == nullptr || size < minSize) {
return true; return true;
} }

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/config/features.gni")
import("//build/test.gni")
import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni")
ohos_fuzztest("FileAccessServiceStubFuzzTest") {
module_out_path = "user_file_service/user_file_service"
fuzz_config_file =
"${user_file_service_path}/test/fuzztest/fileaccessservicestub_fuzzer"
include_dirs = [
"${ability_runtime_path}/interfaces/inner_api/ability_manager/include",
"${user_file_service_path}/interfaces/inner_api/file_access/include",
"${user_file_service_path}/services/native/file_access_service/include",
"${user_file_service_path}/interfaces/kits/js/src/common",
"${user_file_service_path}/utils",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [
"${user_file_service_path}/services/native/file_access_service/src/file_access_service_stub.cpp",
"${user_file_service_path}/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicestub_fuzzer.cpp",
]
external_deps = [
"ability_base:want",
"ability_base:zuri",
"ability_runtime:app_context",
"ability_runtime:extensionkit_native",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"hilog:libhilog",
"hitrace:hitrace_meter",
"ipc:ipc_core",
"napi:ace_napi",
]
defines = [ "private=public" ]
}

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,38 @@
/*
* 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 FILE_ACCESS_SERVICE_MOCK_H
#define FILE_ACCESS_SERVICE_MOCK_H
#include "file_access_service_stub.h"
namespace OHOS {
namespace FileAccessFwk {
class FileAccessServiceMock final : public FileAccessServiceStub {
public:
virtual ~FileAccessServiceMock() = default;
virtual int32_t OnChange(Uri uri, NotifyType notifyType) override { return 0; }
virtual int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr<IFileAccessObserver> &observer,
const std::shared_ptr<ConnectExtensionInfo> &info) override { return 0; }
virtual int32_t UnregisterNotify(Uri uri, const sptr<IFileAccessObserver> &observer,
const std::shared_ptr<ConnectExtensionInfo> &info) override { return 0; }
virtual int32_t GetExensionProxy(const std::shared_ptr<ConnectExtensionInfo> &info,
sptr<IFileAccessExtBase> &extensionProxy) override { return 0; }
virtual int32_t CleanAllNotify(Uri uri, const std::shared_ptr<ConnectExtensionInfo> &info) override { return 0; }
};
} // namespace FileAccessFwk
} // namespace OHOS
#endif // FILE_ACCESS_SERVICE_MOCK_H

View File

@ -0,0 +1,135 @@
/*
* 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 "fileaccessservicestub_fuzzer.h"
#include <cstring>
#include <memory>
#include "extension_context.h"
#include "file_access_service_stub.h"
#include "fileaccessservicemock.h"
#include "message_parcel.h"
namespace OHOS {
using namespace std;
using namespace FileAccessFwk;
template<class T>
T TypeCast(const uint8_t *data, int *pos = nullptr)
{
if (pos) {
*pos += sizeof(T);
}
return *(reinterpret_cast<const T*>(data));
}
bool OnRemoteRequestFuzzTest(shared_ptr<FileAccessServiceStub> fileAccessServiceStub, const uint8_t *data, size_t size)
{
if (data == nullptr || size < sizeof(uint32_t)) {
return true;
}
uint32_t code = TypeCast<uint32_t>(data);
MessageParcel msg;
msg.WriteInterfaceToken(FileAccessServiceStub::GetDescriptor());
MessageParcel reply;
MessageOption option;
fileAccessServiceStub->OnRemoteRequest(code, msg, reply, option);
return true;
}
ErrCode CmdOnChangeFuzzTest(shared_ptr<FileAccessServiceStub> fileAccessServiceStub, const uint8_t *data, size_t size)
{
if (data == nullptr || size < sizeof(uint32_t)) {
return true;
}
int pos = 0;
int32_t notifyType = TypeCast<int32_t>(data, &pos);
Uri uri(string(reinterpret_cast<const char*>(data + pos), size - pos));
MessageParcel msg;
MessageParcel reply;
msg.WriteParcelable(&uri);
msg.WriteInt32(notifyType);
fileAccessServiceStub->CmdOnChange(msg, reply);
return FileAccessFwk::ERR_OK;
}
ErrCode CmdRegisterNotifyFuzzTest(shared_ptr<FileAccessServiceStub> fileAccessServiceStub, const uint8_t *data,
size_t size)
{
Uri uri(string(reinterpret_cast<const char*>(data), size));
MessageParcel msg;
MessageParcel reply;
msg.WriteParcelable(&uri);
fileAccessServiceStub->CmdOnChange(msg, reply);
return FileAccessFwk::ERR_OK;
}
ErrCode CmdUnregisterNotifyFuzzTest(shared_ptr<FileAccessServiceStub> fileAccessServiceStub, const uint8_t *data,
size_t size)
{
if (data == nullptr || size < sizeof(bool)) {
return true;
}
int pos = 0;
bool observerNotNull = TypeCast<bool>(data, &pos);
Uri uri(string(reinterpret_cast<const char*>(data + pos), size - pos));
MessageParcel msg;
MessageParcel reply;
msg.WriteParcelable(&uri);
msg.WriteBool(observerNotNull);
fileAccessServiceStub->CmdUnregisterNotify(msg, reply);
return FileAccessFwk::ERR_OK;
}
ErrCode CmdGetExensionProxyFuzzTest(shared_ptr<FileAccessServiceStub> fileAccessServiceStub, const uint8_t *data,
size_t size)
{
AAFwk::Want want = {};
MessageParcel msg;
MessageParcel reply;
msg.WriteParcelable(&want);
msg.WriteRemoteObject(nullptr);
fileAccessServiceStub->CmdGetExensionProxy(msg, reply);
return FileAccessFwk::ERR_OK;
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
auto stub = std::make_shared<OHOS::FileAccessFwk::FileAccessServiceMock>();
if (stub == nullptr) {
return 0;
}
OHOS::OnRemoteRequestFuzzTest(stub, data, size);
OHOS::CmdOnChangeFuzzTest(stub, data, size);
OHOS::CmdRegisterNotifyFuzzTest(stub, data, size);
OHOS::CmdUnregisterNotifyFuzzTest(stub, data, size);
OHOS::CmdGetExensionProxyFuzzTest(stub, data, size);
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 FILE_ACCESS_SERVICE_STUB_FUZZER_H
#define FILE_ACCESS_SERVICE_STUB_FUZZER_H
#define FUZZ_PROJECT_NAME "fileaccessservicestub_fuzzer"
#endif

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>