mirror of
https://gitee.com/openharmony/filemanagement_user_file_service
synced 2024-11-23 15:29:42 +00:00
!808 add stub fuzz testcases
Merge pull request !808 from wangpggg/master
This commit is contained in:
commit
cbce2fe255
@ -29,5 +29,6 @@ group("user_file_service_fuzz_test") {
|
||||
"externalfileaccessrename_fuzzer:ExternalFileAccessRenameFuzzTest",
|
||||
"externalfileaccessscanfile_fuzzer:ExternalFileAccessScanFileFuzzTest",
|
||||
"fileaccessextstub_fuzzer:FileAccessExtStubFuzzTest",
|
||||
"fileaccessservicestub_fuzzer:FileAccessServiceStubFuzzTest",
|
||||
]
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ bool OnRemoteRequestFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, co
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t code = reinterpret_cast<uint32_t>(data);
|
||||
uint32_t code = TypeCast<uint32_t>(data);
|
||||
MessageParcel msg;
|
||||
msg.WriteInterfaceToken(FileAccessExtStub::GetDescriptor());
|
||||
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)
|
||||
{
|
||||
size_t minSize = sizeof(int64_t) + sizeof(int32_t) + sizeof(int64_t) + sizeof(int64_t) + sizeof(int64_t)
|
||||
+ sizeof(double) + sizeof(bool) + sizeof(bool);
|
||||
size_t minSize = sizeof(int64_t) + sizeof(int32_t) + sizeof(int64_t) + sizeof(int64_t) + sizeof(int64_t) +
|
||||
sizeof(double) + sizeof(bool) + sizeof(bool);
|
||||
if (data == nullptr || size < minSize) {
|
||||
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)
|
||||
{
|
||||
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);
|
||||
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);
|
||||
if (data == nullptr || size < minSize) {
|
||||
return true;
|
||||
}
|
||||
|
55
test/fuzztest/fileaccessservicestub_fuzzer/BUILD.gn
Normal file
55
test/fuzztest/fileaccessservicestub_fuzzer/BUILD.gn
Normal 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" ]
|
||||
}
|
16
test/fuzztest/fileaccessservicestub_fuzzer/corpus/init
Normal file
16
test/fuzztest/fileaccessservicestub_fuzzer/corpus/init
Normal 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
|
@ -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
|
@ -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;
|
||||
}
|
@ -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
|
25
test/fuzztest/fileaccessservicestub_fuzzer/project.xml
Normal file
25
test/fuzztest/fileaccessservicestub_fuzzer/project.xml
Normal 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>
|
Loading…
Reference in New Issue
Block a user