add fuzz to improve line cover

Signed-off-by: wangpeng <wangpeng477@huawei.com>
This commit is contained in:
wangpggg 2024-11-06 18:31:11 +08:00
parent 576a0f148c
commit e3df61cb68
9 changed files with 1128 additions and 35 deletions

View File

@ -30,6 +30,7 @@ group("user_file_service_fuzz_test") {
"externalfileaccessscanfile_fuzzer:ExternalFileAccessScanFileFuzzTest",
"fileaccessextconnection_fuzzer:FileAccessExtConnectionFuzzTest",
"fileaccessextstub_fuzzer:FileAccessExtStubFuzzTest",
"fileaccessextstubbranch_fuzzer:FileAccessExtStubBranchFuzzTest",
"fileaccessserviceproxy_fuzzer:FileAccessServiceProxyFuzzTest",
"fileaccessservicestub_fuzzer:FileAccessServiceStubFuzzTest",
"fileinfosharedmemory_fuzzer:FileInfoSharedMemoryFuzzTest",

View File

@ -24,40 +24,33 @@ using namespace AbilityRuntime;
class FileAccessExtAbilityMock : public FileAccessExtAbility {
public:
void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
const sptr<IRemoteObject> &token) override {};
void OnStart(const AAFwk::Want &want) override {};
sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override { return 0; };
int OpenFile(const Uri &uri, const int flags, int &fd) override { return 0; };
int CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile) override { return 0; };
int Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile) override { return 0; };
int Delete(const Uri &sourceFile) override { return 0; };
int Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile) override { return 0; };
int Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> &copyResult, bool force = false) override
{ return 0; };
int CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName,
Uri &newFileUri) override { return 0; };
int Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile) override { return 0; };
int ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter,
std::vector<FileInfo> &fileInfoVec) override { return 0; };
int ScanFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount, const FileFilter &filter,
std::vector<FileInfo> &fileInfoVec) override { return 0; };
int GetFileInfoFromUri(const Uri &selectFile, FileInfo &fileInfo) override { return 0; };
int GetFileInfoFromRelativePath(const std::string &selectFile, FileInfo &fileInfo) override { return 0; };
int GetRoots(std::vector<RootInfo> &rootInfoVec) override { return 0; };
int Access(const Uri &uri, bool &isExist) override { return 0; };
int Query(const Uri &uri, std::vector<std::string> &columns, std::vector<std::string> &results) override
{ return 0; };
int StartWatcher(const Uri &uri) override { return 0; };
int StopWatcher(const Uri &uri) override { return 0; };
int MoveItem(const Uri &sourceFile, const Uri &targetParent, std::vector<Result> &moveResult,
bool force = false) override { return 0; };
int MoveFile(const Uri &sourceFile, const Uri &targetParent, std::string &fileName, Uri &newFile) override
{ return 0; };
int GetFileInfoNum(const std::string &sourceFileUri, const FileFilter &filter, bool recursion,
uint32_t &counts) override { return 0; };
void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord>&,
const std::shared_ptr<AppExecFwk::OHOSApplication>&, std::shared_ptr<AppExecFwk::AbilityHandler>&,
const sptr<IRemoteObject>&) override {}
void OnStart(const AAFwk::Want&) override {}
sptr<IRemoteObject> OnConnect(const AAFwk::Want&) override { return nullptr; }
int OpenFile(const Uri&, const int, int&) override { return 0; }
int CreateFile(const Uri&, const std::string&, Uri&) override { return 0; }
int Mkdir(const Uri&, const std::string&, Uri&) override { return 0; }
int Delete(const Uri&) override { return 0; }
int Move(const Uri&, const Uri&, Uri&) override { return 0; }
int Copy(const Uri&, const Uri&, std::vector<Result>&, bool force = false) override { return 0; }
int CopyFile(const Uri&, const Uri&, const std::string&, Uri&) override { return 0; }
int Rename(const Uri&, const std::string&, Uri&) override { return 0; }
int ListFile(const FileInfo&, const int64_t, const int64_t, const FileFilter&, std::vector<FileInfo>&) override
{ return 0; }
int ScanFile(const FileInfo&, const int64_t, const int64_t, const FileFilter&, std::vector<FileInfo>&) override
{ return 0; }
int GetFileInfoFromUri(const Uri&, FileInfo&) override { return 0; }
int GetFileInfoFromRelativePath(const std::string&, FileInfo&) override { return 0; }
int GetRoots(std::vector<RootInfo>&) override { return 0; }
int Access(const Uri&, bool&) override { return 0; }
int Query(const Uri&, std::vector<std::string>&, std::vector<std::string>&) override { return 0; }
int StartWatcher(const Uri&) override { return 0; }
int StopWatcher(const Uri&) override { return 0; }
int MoveItem(const Uri&, const Uri&, std::vector<Result>&, bool force = false) override { return 0; }
int MoveFile(const Uri&, const Uri&, std::string&, Uri&) override { return 0; }
int GetFileInfoNum(const std::string&, const FileFilter&, bool, uint32_t&) override { return 0; }
};
} // namespace FileAccessFwk
} // namespace OHOS

View File

@ -0,0 +1,58 @@
# 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("FileAccessExtStubBranchFuzzTest") {
module_out_path = "user_file_service/user_file_service"
fuzz_config_file =
"${user_file_service_path}/test/fuzztest/fileaccessextstubbranch_fuzzer"
include_dirs = [
"${ability_runtime_napi_path}/inner/napi_common",
"${ability_runtime_path}/interfaces/kits/native/ability/ability_runtime",
"${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime",
"${user_file_service_path}/interfaces/inner_api/file_access/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}/interfaces/inner_api/file_access/src/file_access_ext_stub.cpp",
"${user_file_service_path}/interfaces/inner_api/file_access/src/file_access_ext_stub_impl.cpp",
"${user_file_service_path}/test/fuzztest/fileaccessextstubbranch_fuzzer/fileaccessextstubbranch_fuzzer.cpp",
]
external_deps = [
"ability_base:want",
"ability_base:zuri",
"ability_runtime:app_context",
"ability_runtime:extensionkit_native",
"ability_runtime:napi_common",
"ability_runtime:runtime",
"access_token:libaccesstoken_sdk",
"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,57 @@
/*
* 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_EXT_ABILITY_MOCK_H
#define FILE_ACCESS_EXT_ABILITY_MOCK_H
#include "file_access_ext_ability.h"
namespace OHOS {
namespace FileAccessFwk {
using namespace AbilityRuntime;
class FileAccessExtAbilityMock : public FileAccessExtAbility {
public:
void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord>&,
const std::shared_ptr<AppExecFwk::OHOSApplication>&, std::shared_ptr<AppExecFwk::AbilityHandler>&,
const sptr<IRemoteObject>&) override {}
void OnStart(const AAFwk::Want&) override {}
sptr<IRemoteObject> OnConnect(const AAFwk::Want&) override { return nullptr; }
int OpenFile(const Uri&, const int, int&) override { return 0; }
int CreateFile(const Uri&, const std::string&, Uri&) override { return 0; }
int Mkdir(const Uri&, const std::string&, Uri&) override { return 0; }
int Delete(const Uri&) override { return 0; }
int Move(const Uri&, const Uri&, Uri&) override { return 0; }
int Copy(const Uri&, const Uri&, std::vector<Result>&, bool force = false) override { return 0; }
int CopyFile(const Uri&, const Uri&, const std::string&, Uri&) override { return 0; }
int Rename(const Uri&, const std::string&, Uri&) override { return 0; }
int ListFile(const FileInfo&, const int64_t, const int64_t, const FileFilter&, std::vector<FileInfo>&) override
{ return 0; }
int ScanFile(const FileInfo&, const int64_t, const int64_t, const FileFilter&, std::vector<FileInfo>&) override
{ return 0; }
int GetFileInfoFromUri(const Uri&, FileInfo&) override { return 0; }
int GetFileInfoFromRelativePath(const std::string&, FileInfo&) override { return 0; }
int GetRoots(std::vector<RootInfo>&) override { return 0; }
int Access(const Uri&, bool&) override { return 0; }
int Query(const Uri&, std::vector<std::string>&, std::vector<std::string>&) override { return 0; }
int StartWatcher(const Uri&) override { return 0; }
int StopWatcher(const Uri&) override { return 0; }
int MoveItem(const Uri&, const Uri&, std::vector<Result>&, bool force = false) override { return 0; }
int MoveFile(const Uri&, const Uri&, std::string&, Uri&) override { return 0; }
int GetFileInfoNum(const std::string&, const FileFilter&, bool, uint32_t&) override { return 0; }
};
} // namespace FileAccessFwk
} // namespace OHOS
#endif // FILE_ACCESS_EXT_ABILITY_MOCK_H

View File

@ -0,0 +1,922 @@
/*
* 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 "fileaccessextstubbranch_fuzzer.h"
#include <cstring>
#include <memory>
#include <queue>
#include "accesstoken_kit.h"
#include "extension_base.h"
#include "extension_context.h"
#include "file_access_ext_stub.h"
#include "file_access_ext_stub_impl.h"
#include "fileaccessextabilitymock.h"
#include "iremote_broker.h"
#include "message_parcel.h"
namespace OHOS {
using namespace std;
using namespace FileAccessFwk;
using namespace AbilityRuntime;
using FAFVariant = std::variant<int32_t, int64_t, uint32_t, bool, std::vector<std::string>>;
queue<bool> que;
FAFVariant arg;
bool GetResult()
{
bool ret = true;
if (!que.empty()) {
ret = que.front();
que.pop();
}
return ret;
}
Parcelable::Parcelable() : Parcelable(false)
{}
Parcelable::Parcelable(bool asRemote)
{
asRemote_ = asRemote;
behavior_ = 0;
}
template <typename T>
bool Parcel::Write(T value)
{
size_t desireCapacity = sizeof(T);
if (EnsureWritableCapacity(desireCapacity)) {
*reinterpret_cast<T *>(data_ + writeCursor_) = value;
writeCursor_ += desireCapacity;
dataSize_ += desireCapacity;
return true;
}
return false;
}
bool Parcel::WriteParcelable(const Parcelable *object)
{
if (object == nullptr) {
return WriteInt32(0);
}
if (!object->asRemote_) {
bool flag = WriteInt32(1);
object->Marshalling(*this);
return flag;
}
bool flag = WriteInt32(1);
WriteRemoteObject(const_cast<Parcelable*>(object));
return flag;
}
bool Parcel::WriteInt32(int32_t value)
{
Write<int32_t>(value);
return GetResult();
}
bool Parcel::WriteUint32(uint32_t value)
{
Write<uint32_t>(value);
return GetResult();
}
int32_t Parcel::ReadInt32()
{
return GetResult();
}
bool Parcel::ReadInt32(int32_t &value)
{
value = std::get<int32_t>(arg);
return GetResult();
}
bool Parcel::ReadBool()
{
return GetResult();
}
bool Parcel::ReadBool(bool &value)
{
if (std::get_if<bool>(&arg)) {
value = std::get<bool>(arg);
}
return GetResult();
}
bool Parcel::WriteBool(bool value)
{
return GetResult();
}
bool Parcel::WriteString(const std::string &value)
{
return GetResult();
}
bool Parcel::ReadString(std::string &value)
{
std::vector<std::string> vstr = std::get<std::vector<std::string>>(arg);
if (!vstr.empty()) {
value = vstr[0];
vstr.erase(vstr.begin());
}
return GetResult();
}
bool Parcel::ReadStringVector(std::vector<std::string> *value)
{
*value = std::get<std::vector<std::string>>(arg);
return GetResult();
}
bool MessageParcel::WriteFileDescriptor(int fd)
{
return GetResult();
}
bool Parcel::ReadUint32(uint32_t &value)
{
value = std::get<uint32_t>(arg);
return GetResult();
}
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<FileAccessExtStub> fileAccessExtStub, 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;
MessageParcel reply;
MessageOption option;
fileAccessExtStub->OnRemoteRequest(code, msg, reply, option);
msg.WriteInterfaceToken(FileAccessExtStub::GetDescriptor());
fileAccessExtStub->OnRemoteRequest(code, msg, reply, option);
return true;
}
bool CmdOpenFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
if (data == nullptr || size < sizeof(int32_t)) {
return true;
}
MessageParcel msg;
MessageParcel reply;
Uri uri(string(reinterpret_cast<const char*>(data), size));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
fileAccessExtStub->CmdOpenFile(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
arg = -1;
fileAccessExtStub->CmdOpenFile(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(false);
arg = TypeCast<int32_t>(data);
fileAccessExtStub->CmdOpenFile(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(false);
arg = TypeCast<int32_t>(data);
fileAccessExtStub->CmdOpenFile(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(true);
arg = TypeCast<int32_t>(data);
fileAccessExtStub->CmdOpenFile(msg, reply);
return true;
}
bool CmdCreateFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
int pos = size >> 1;
vector<string> displayName{""};
arg = displayName;
fileAccessExtStub->CmdCreateFile(msg, reply);
Uri uri(string(reinterpret_cast<const char*>(data), pos));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
arg = displayName;
fileAccessExtStub->CmdCreateFile(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
arg = displayName;
fileAccessExtStub->CmdCreateFile(msg, reply);
displayName.clear();
displayName.emplace_back(reinterpret_cast<const char*>(data + pos), size - pos);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(false);
arg = displayName;
fileAccessExtStub->CmdCreateFile(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(false);
arg = displayName;
fileAccessExtStub->CmdCreateFile(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(true);
arg = displayName;
fileAccessExtStub->CmdCreateFile(msg, reply);
return true;
}
bool CmdMkdirFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
int pos = size >> 1;
Uri uri(string(reinterpret_cast<const char*>(data), pos));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
fileAccessExtStub->CmdMkdir(msg, reply);
vector<string> displayName{""};
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
arg = displayName;
fileAccessExtStub->CmdMkdir(msg, reply);
displayName.clear();
displayName.emplace_back(reinterpret_cast<const char*>(data + pos), size - pos);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(false);
arg = displayName;
fileAccessExtStub->CmdMkdir(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(false);
arg = displayName;
fileAccessExtStub->CmdMkdir(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(true);
arg = displayName;
fileAccessExtStub->CmdMkdir(msg, reply);
return true;
}
bool CmdDeleteFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
Uri uri(string(reinterpret_cast<const char*>(data), size));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
fileAccessExtStub->CmdDelete(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
fileAccessExtStub->CmdDelete(msg, reply);
return true;
}
bool CmdMoveFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
int pos = size >> 1;
Uri sourceFile(string(reinterpret_cast<const char*>(data), pos));
Uri targetParent(string(reinterpret_cast<const char*>(data + pos), size - pos));
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
fileAccessExtStub->CmdMove(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
msg.WriteParcelable(&targetParent);
que = {};
que.push(false);
fileAccessExtStub->CmdMove(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
msg.WriteParcelable(&targetParent);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdMove(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
msg.WriteParcelable(&targetParent);
que = {};
que.push(true);
que.push(true);
fileAccessExtStub->CmdMove(msg, reply);
return true;
}
bool CmdCopyFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
if (data == nullptr || size == 0) {
return true;
}
MessageParcel msg;
MessageParcel reply;
int pos = (size - 1) >> 1;
Uri sourceUri(string(reinterpret_cast<const char*>(data + 1), pos));
Uri destUri(string(reinterpret_cast<const char*>(data + pos + 1), size - pos - 1));
msg.FlushBuffer();
msg.WriteParcelable(&sourceUri);
fileAccessExtStub->CmdCopy(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceUri);
msg.WriteParcelable(&destUri);
que = {};
que.push(false);
fileAccessExtStub->CmdCopy(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceUri);
msg.WriteParcelable(&destUri);
que = {};
que.push(true);
que.push(false);
arg = TypeCast<bool>(data);
fileAccessExtStub->CmdCopy(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceUri);
msg.WriteParcelable(&destUri);
que = {};
que.push(true);
que.push(true);
que.push(false);
arg = TypeCast<bool>(data);
fileAccessExtStub->CmdCopy(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceUri);
msg.WriteParcelable(&destUri);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(false);
arg = TypeCast<bool>(data);
fileAccessExtStub->CmdCopy(msg, reply);
return true;
}
bool CmdCopyFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
vector<string> str{ string(reinterpret_cast<const char*>(data), size) };
que = {};
que.push(false);
arg = str;
fileAccessExtStub->CmdCopyFile(msg, reply);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdCopyFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdCopyFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdCopyFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdCopyFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(true);
fileAccessExtStub->CmdCopyFile(msg, reply);
return true;
}
bool CmdRenameFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
int pos = size >> 1;
Uri sourceFile(string(reinterpret_cast<const char*>(data), pos));
vector<string> displayName{ string(reinterpret_cast<const char*>(data + pos), size - pos) };
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
que = {};
que.push(false);
fileAccessExtStub->CmdRename(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
que = {};
que.push(true);
arg = vector<string>();
fileAccessExtStub->CmdRename(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
que = {};
que.push(true);
que.push(false);
arg = displayName;
fileAccessExtStub->CmdRename(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
que = {};
que.push(true);
que.push(true);
que.push(false);
arg = displayName;
fileAccessExtStub->CmdRename(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&sourceFile);
que = {};
que.push(true);
que.push(true);
que.push(true);
arg = displayName;
fileAccessExtStub->CmdRename(msg, reply);
return true;
}
bool CmdGetRootsFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
que = {};
que.push(false);
fileAccessExtStub->CmdGetRoots(msg, reply);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdGetRoots(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdGetRoots(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
fileAccessExtStub->CmdGetRoots(msg, reply);
return true;
}
bool CmdQueryFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
if (data == nullptr || size < sizeof(int64_t)) {
return true;
}
MessageParcel msg;
MessageParcel reply;
Uri uri(string(reinterpret_cast<const char*>(data + sizeof(int64_t)), size - sizeof(int64_t)));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
fileAccessExtStub->CmdQuery(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(false);
arg = TypeCast<int64_t>(data);
fileAccessExtStub->CmdQuery(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(false);
arg = TypeCast<int64_t>(data);
fileAccessExtStub->CmdQuery(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(false);
arg = TypeCast<int64_t>(data);
fileAccessExtStub->CmdQuery(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
arg = TypeCast<int64_t>(data);
fileAccessExtStub->CmdQuery(msg, reply);
return true;
}
bool CmdGetFileInfoFromUriFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
Uri uri(string(reinterpret_cast<const char*>(data), size));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
fileAccessExtStub->CmdGetFileInfoFromUri(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdGetFileInfoFromUri(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
fileAccessExtStub->CmdGetFileInfoFromUri(msg, reply);
return true;
}
bool CmdGetFileInfoFromRelativePathFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data,
size_t size)
{
MessageParcel msg;
MessageParcel reply;
Uri uri(string(reinterpret_cast<const char*>(data), size));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
fileAccessExtStub->CmdGetFileInfoFromRelativePath(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdGetFileInfoFromRelativePath(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
fileAccessExtStub->CmdGetFileInfoFromRelativePath(msg, reply);
return true;
}
bool CmdAccessFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
Uri uri(string(reinterpret_cast<const char*>(data), size));
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(false);
fileAccessExtStub->CmdAccess(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdAccess(msg, reply);
msg.FlushBuffer();
msg.WriteParcelable(&uri);
que = {};
que.push(true);
que.push(true);
fileAccessExtStub->CmdAccess(msg, reply);
return true;
}
bool CmdStartWatcherFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
vector<string> uriString{ string(reinterpret_cast<const char*>(data), size) };
que = {};
que.push(false);
arg = vector<string>();
fileAccessExtStub->CmdStartWatcher(msg, reply);
que = {};
que.push(true);
arg = vector<string>();
fileAccessExtStub->CmdStartWatcher(msg, reply);
que = {};
que.push(true);
que.push(false);
arg = uriString;
fileAccessExtStub->CmdStartWatcher(msg, reply);
que = {};
que.push(true);
que.push(true);
arg = uriString;
fileAccessExtStub->CmdStartWatcher(msg, reply);
return true;
}
bool CmdStopWatcherFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
vector<string> uriString{ string(reinterpret_cast<const char*>(data), size) };
que = {};
que.push(false);
arg = vector<string>();
fileAccessExtStub->CmdStopWatcher(msg, reply);
que = {};
que.push(true);
arg = vector<string>();
fileAccessExtStub->CmdStopWatcher(msg, reply);
que = {};
que.push(true);
que.push(false);
arg = uriString;
fileAccessExtStub->CmdStopWatcher(msg, reply);
que = {};
que.push(true);
que.push(true);
arg = uriString;
fileAccessExtStub->CmdStopWatcher(msg, reply);
return true;
}
bool CmdMoveItemFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
int pos = size >> 1;
string sourceFile(reinterpret_cast<const char*>(data), pos);
string targetParent(reinterpret_cast<const char*>(data + pos), size - pos);
arg = vector<string>{sourceFile, targetParent};
que = {};
que.push(false);
fileAccessExtStub->CmdMoveItem(msg, reply);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveItem(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveItem(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveItem(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveItem(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveItem(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(true);
fileAccessExtStub->CmdMoveItem(msg, reply);
return true;
}
bool CmdMoveFileFuzzTest(shared_ptr<FileAccessExtStub> fileAccessExtStub, const uint8_t *data, size_t size)
{
MessageParcel msg;
MessageParcel reply;
int pos = size / 3;
vector<string> item { string(reinterpret_cast<const char*>(data), pos),
string(reinterpret_cast<const char*>(data + pos), pos),
string(reinterpret_cast<const char*>(data + pos + pos), size - pos - pos)};
arg = item;
que = {};
que.push(false);
fileAccessExtStub->CmdMoveFile(msg, reply);
que = {};
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(false);
fileAccessExtStub->CmdMoveFile(msg, reply);
que = {};
que.push(true);
que.push(true);
que.push(true);
que.push(true);
que.push(true);
fileAccessExtStub->CmdMoveFile(msg, reply);
return true;
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
auto ability = std::make_shared<OHOS::FileAccessFwk::FileAccessExtAbilityMock>();
auto stub = std::make_shared<OHOS::FileAccessFwk::FileAccessExtStubImpl>(ability, nullptr);
if (stub == nullptr) {
return 0;
}
OHOS::OnRemoteRequestFuzzTest(stub, data, size);
OHOS::CmdOpenFileFuzzTest(stub, data, size);
OHOS::CmdCreateFileFuzzTest(stub, data, size);
OHOS::CmdMkdirFuzzTest(stub, data, size);
OHOS::CmdDeleteFuzzTest(stub, data, size);
OHOS::CmdMoveFuzzTest(stub, data, size);
OHOS::CmdCopyFuzzTest(stub, data, size);
OHOS::CmdCopyFileFuzzTest(stub, data, size);
OHOS::CmdRenameFuzzTest(stub, data, size);
OHOS::CmdGetRootsFuzzTest(stub, data, size);
OHOS::CmdQueryFuzzTest(stub, data, size);
OHOS::CmdGetFileInfoFromUriFuzzTest(stub, data, size);
OHOS::CmdGetFileInfoFromRelativePathFuzzTest(stub, data, size);
OHOS::CmdAccessFuzzTest(stub, data, size);
OHOS::CmdStartWatcherFuzzTest(stub, data, size);
OHOS::CmdStopWatcherFuzzTest(stub, data, size);
OHOS::CmdMoveItemFuzzTest(stub, data, size);
OHOS::CmdMoveFileFuzzTest(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_EXT_STUB_BRANCH_FUZZER_H
#define FILE_ACCESS_EXT_STUB_BRANCH_FUZZER_H
#define FUZZ_PROJECT_NAME "fileaccessextstubbranch_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>

View File

@ -603,7 +603,7 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0005, testing::ext
result = g_fah->Delete(uri_dirSub1);
EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK);
usleep(SLEEP_TIME);
EXPECT_EQ(g_notifyEvent, MOVED_FROM);
EXPECT_TRUE(g_notifyEvent == MOVED_FROM || g_notifyEvent == MOVED_SELF);
EXPECT_EQ(g_notifyUri, uri_dirSub1.ToString());
sleep(2);