diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index c0264818ab3938c9b614d442948d527c5f3de07d..b3901c585f39e4a1bdd64561eeed489105057c6d 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -29,5 +29,6 @@ group("user_file_service_fuzz_test") { "externalfileaccessrename_fuzzer:ExternalFileAccessRenameFuzzTest", "externalfileaccessscanfile_fuzzer:ExternalFileAccessScanFileFuzzTest", "fileaccessextstub_fuzzer:FileAccessExtStubFuzzTest", + "fileaccessservicestub_fuzzer:FileAccessServiceStubFuzzTest", ] } diff --git a/test/fuzztest/fileaccessservicestub_fuzzer/BUILD.gn b/test/fuzztest/fileaccessservicestub_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d9cdf12c7d0bf03961cd6f26295fabe3f87ffa24 --- /dev/null +++ b/test/fuzztest/fileaccessservicestub_fuzzer/BUILD.gn @@ -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" ] +} diff --git a/test/fuzztest/fileaccessservicestub_fuzzer/corpus/init b/test/fuzztest/fileaccessservicestub_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..6198079a28e860189d4294f6598f8ac6804c0dff --- /dev/null +++ b/test/fuzztest/fileaccessservicestub_fuzzer/corpus/init @@ -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 \ No newline at end of file diff --git a/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicemock.h b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicemock.h new file mode 100644 index 0000000000000000000000000000000000000000..839e7d05cb7f86487fd7ee0776d5cedbb6e3a2f4 --- /dev/null +++ b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicemock.h @@ -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 &observer, + const std::shared_ptr &info) override { return 0; } + virtual int32_t UnregisterNotify(Uri uri, const sptr &observer, + const std::shared_ptr &info) override { return 0; } + virtual int32_t GetExensionProxy(const std::shared_ptr &info, + sptr &extensionProxy) override { return 0; } + virtual int32_t CleanAllNotify(Uri uri, const std::shared_ptr &info) override { return 0; } +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_ACCESS_SERVICE_MOCK_H \ No newline at end of file diff --git a/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicestub_fuzzer.cpp b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicestub_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdca93c6e7a8dfe7f78032b27bebf74003d4054a --- /dev/null +++ b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicestub_fuzzer.cpp @@ -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 +#include + +#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 +T TypeCast(const uint8_t *data, int *pos = nullptr) +{ + if (pos) { + *pos += sizeof(T); + } + return *(reinterpret_cast(data)); +} + +bool OnRemoteRequestFuzzTest(shared_ptr fileAccessServiceStub, const uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(uint32_t)) { + return true; + } + + uint32_t code = reinterpret_cast(data); + MessageParcel msg; + msg.WriteInterfaceToken(FileAccessServiceStub::GetDescriptor()); + MessageParcel reply; + MessageOption option; + + fileAccessServiceStub->OnRemoteRequest(code, msg, reply, option); + return true; +} + +ErrCode CmdOnChangeFuzzTest(shared_ptr fileAccessServiceStub, const uint8_t *data, size_t size) +{ + if (data == nullptr || size < sizeof(uint32_t)) { + return true; + } + + int pos = 0; + int32_t notifyType = TypeCast(data, &pos); + Uri uri(string(reinterpret_cast(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, const uint8_t *data, + size_t size) +{ + Uri uri(string(reinterpret_cast(data), size)); + + MessageParcel msg; + MessageParcel reply; + msg.WriteParcelable(&uri); + + fileAccessServiceStub->CmdOnChange(msg, reply); + return FileAccessFwk::ERR_OK; +} + +ErrCode CmdUnregisterNotifyFuzzTest(shared_ptr fileAccessServiceStub, const uint8_t *data, + size_t size) +{ + if (data == nullptr || size < sizeof(bool)) { + return true; + } + + int pos = 0; + bool observerNotNull = TypeCast(data, &pos); + Uri uri(string(reinterpret_cast(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, 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(); + 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; +} diff --git a/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicestub_fuzzer.h b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicestub_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..9d13e85541206b20396c99180edfc60ef4853ab3 --- /dev/null +++ b/test/fuzztest/fileaccessservicestub_fuzzer/fileaccessservicestub_fuzzer.h @@ -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 \ No newline at end of file diff --git a/test/fuzztest/fileaccessservicestub_fuzzer/project.xml b/test/fuzztest/fileaccessservicestub_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..d6679ccc2ec868b072c934ed45102d545f31f91d --- /dev/null +++ b/test/fuzztest/fileaccessservicestub_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +