add ut and fuzz test

Signed-off-by: 张凯 <zhangkai357@huawei.com>
This commit is contained in:
张凯 2024-07-12 15:31:03 +08:00
parent 8167b2539d
commit 5b30ec3c3c
61 changed files with 5845 additions and 66 deletions

View File

@ -133,7 +133,9 @@
], ],
"test": [ "test": [
"//base/print/print_fwk/test/unittest/fwk_inner_napi_test:print_fwk_inner_napi_test", "//base/print/print_fwk/test/unittest/fwk_inner_napi_test:print_fwk_inner_napi_test",
"//base/print/print_fwk/test/unittest/fwk_print_cups_client_test:fwk_print_cups_client_test",
"//base/print/print_fwk/test/unittest/fwk_print_helper_test:fwk_print_helper_test", "//base/print/print_fwk/test/unittest/fwk_print_helper_test:fwk_print_helper_test",
"//base/print/print_fwk/test/unittest/fwk_print_http_request_process_test:fwk_print_http_request_process_test",
"//base/print/print_fwk/test/unittest/fwk_print_service_ability_test:fwk_print_service_ability_test", "//base/print/print_fwk/test/unittest/fwk_print_service_ability_test:fwk_print_service_ability_test",
"//base/print/print_fwk/test/unittest/service_test:print_service_test", "//base/print/print_fwk/test/unittest/service_test:print_service_test",
"//base/print/print_fwk/test/fuzztest:fuzztest" "//base/print/print_fwk/test/fuzztest:fuzztest"

View File

@ -19,5 +19,12 @@ group("fuzztest") {
"printcupsclient_fuzzer:fuzztest", "printcupsclient_fuzzer:fuzztest",
"printservice_fuzzer:fuzztest", "printservice_fuzzer:fuzztest",
"printserviceability_fuzzer:fuzztest", "printserviceability_fuzzer:fuzztest",
"scancallbackproxy_fuzzer:fuzztest",
"scancapi_fuzzer:fuzztest",
"scanhelper_fuzzer:fuzztest",
"scanmdnsservice_fuzzer:fuzztest",
"scanserviceability_fuzzer:fuzztest",
"scanservicestub_fuzzer:fuzztest",
"scanusbmanager_fuzzer:fuzztest",
] ]
} }

View File

@ -25,7 +25,7 @@ ohos_fuzztest("PrintCupsClientFuzzTest") {
include_dirs = [ include_dirs = [
"${print_path}/frameworks/innerkitsimpl/print_impl/include", "${print_path}/frameworks/innerkitsimpl/print_impl/include",
"${print_utils_path}/include", "${print_utils_path}/include",
"//third_party/cpp-httplib", "//vendor/open_source/cpp-httplib",
] ]
cflags = [ cflags = [

View File

@ -24,8 +24,8 @@
namespace OHOS { namespace OHOS {
namespace Print { namespace Print {
constexpr uint8_t MAX_STRING_LENGTH = 255; constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr float MAX_FLOAT_VALUE = 1000.0; constexpr size_t FOO_MAX_LEN = 1024;
constexpr int MAX_SET_NUMBER = 100; constexpr size_t U32_AT_SIZE = 4;
void TestInitCupsResources(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) void TestInitCupsResources(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{ {
@ -131,16 +131,6 @@ void TestQueryPrinterInfoByPrinterId(const uint8_t *data, size_t size, FuzzedDat
PrintCupsClient::GetInstance()->QueryPrinterInfoByPrinterId(printerId, info); PrintCupsClient::GetInstance()->QueryPrinterInfoByPrinterId(printerId, info);
} }
void TestDeletePrinterFromCups(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{
PrintCupsClient::GetInstance()->InitCupsResources();
std::string printerUri = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string printerName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string printerMake = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
PrintCupsClient::GetInstance()->DeletePrinterFromCups(printerUri, printerName, printerMake);
}
// below are private
void TestJobCompleteCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) void TestJobCompleteCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{ {
PrintCupsClient::GetInstance()->InitCupsResources(); PrintCupsClient::GetInstance()->InitCupsResources();
@ -154,49 +144,13 @@ void TestCheckPrinterMakeModel(const uint8_t *data, size_t size, FuzzedDataProvi
PrintCupsClient::CheckPrinterMakeModel(&jobParams); PrintCupsClient::CheckPrinterMakeModel(&jobParams);
} }
void TestCheckPrinterOnline(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) void TestDeletePrinterFromCups(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{ {
PrintCupsClient::GetInstance()->InitCupsResources(); PrintCupsClient::GetInstance()->InitCupsResources();
std::string printerUriStr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerUri = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
PrintCupsClient::CheckPrinterOnline(printerUriStr.c_str(), printerId); std::string printerMake = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
} PrintCupsClient::GetInstance()->DeletePrinterFromCups(printerUri, printerName, printerMake);
void TestDumpJobParameters(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{
PrintCupsClient::GetInstance()->InitCupsResources();
JobParameters* jobParams = new (std::nothrow) JobParameters;
if (jobParams != nullptr) {
PrintCupsClient::GetInstance()->DumpJobParameters(jobParams);
}
}
void TestGetMedieSize(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{
PrintCupsClient::GetInstance()->InitCupsResources();
PrintJob jobInfo;
PrintCupsClient::GetInstance()->GetMedieSize(jobInfo);
}
void TestGetColorString(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{
PrintCupsClient::GetInstance()->InitCupsResources();
uint32_t colorCode = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
PrintCupsClient::GetInstance()->GetColorString(colorCode);
}
void TestConvertInchTo100MM(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{
PrintCupsClient::GetInstance()->InitCupsResources();
float num = dataProvider->ConsumeFloatingPointInRange<float>(0, MAX_FLOAT_VALUE);
PrintCupsClient::ConvertInchTo100MM(num);
}
void TestGetIpAddress(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
{
PrintCupsClient::GetInstance()->InitCupsResources();
unsigned int number = dataProvider->ConsumeIntegralInRange<unsigned int>(0, MAX_SET_NUMBER);
PrintCupsClient::GetIpAddress(number);
} }
} // namespace Print } // namespace Print
@ -210,6 +164,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
return 0; return 0;
} }
if (size < OHOS::Print::U32_AT_SIZE || size > OHOS::Print::FOO_MAX_LEN) {
}
FuzzedDataProvider dataProvider(data, size); FuzzedDataProvider dataProvider(data, size);
OHOS::Print::TestInitCupsResources(data, size, &dataProvider); OHOS::Print::TestInitCupsResources(data, size, &dataProvider);
OHOS::Print::TestStopCupsdService(data, size, &dataProvider); OHOS::Print::TestStopCupsdService(data, size, &dataProvider);
@ -224,15 +181,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
OHOS::Print::TestSetDefaultPrinter(data, size, &dataProvider); OHOS::Print::TestSetDefaultPrinter(data, size, &dataProvider);
OHOS::Print::TestQueryPrinterAttrList(data, size, &dataProvider); OHOS::Print::TestQueryPrinterAttrList(data, size, &dataProvider);
OHOS::Print::TestQueryPrinterInfoByPrinterId(data, size, &dataProvider); OHOS::Print::TestQueryPrinterInfoByPrinterId(data, size, &dataProvider);
OHOS::Print::TestDeletePrinterFromCups(data, size, &dataProvider);
OHOS::Print::TestJobCompleteCallback(data, size, &dataProvider); OHOS::Print::TestJobCompleteCallback(data, size, &dataProvider);
OHOS::Print::TestCheckPrinterMakeModel(data, size, &dataProvider); OHOS::Print::TestCheckPrinterMakeModel(data, size, &dataProvider);
OHOS::Print::TestCheckPrinterOnline(data, size, &dataProvider); OHOS::Print::TestDeletePrinterFromCups(data, size, &dataProvider);
OHOS::Print::TestDumpJobParameters(data, size, &dataProvider);
OHOS::Print::TestGetMedieSize(data, size, &dataProvider);
OHOS::Print::TestGetColorString(data, size, &dataProvider);
OHOS::Print::TestConvertInchTo100MM(data, size, &dataProvider);
OHOS::Print::TestGetIpAddress(data, size, &dataProvider);
return 0; return 0;
} }

View File

@ -26,7 +26,7 @@ ohos_fuzztest("PrintServiceAbilityFuzzTest") {
"mock", "mock",
"${print_path}/frameworks/innerkitsimpl/print_impl/include", "${print_path}/frameworks/innerkitsimpl/print_impl/include",
"${print_utils_path}/include", "${print_utils_path}/include",
"//third_party/cpp-httplib", "//vendor/open_source/cpp-httplib",
] ]
cflags = [ cflags = [

View File

@ -0,0 +1,99 @@
# 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.
import("//base/print/print_fwk/print.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("ScanCallbackProxyFuzzTest") {
module_out_path = fuzz_test_output_path
fuzz_config_file = "${print_path}/test/fuzztest/scancallbackproxy_fuzzer"
include_dirs = [
"${print_path}/services/scan_service/include",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_utils_path}/include",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
deps = [
"${print_path}/frameworks/helper/scan_helper:scan_helper",
"${print_path}/frameworks/innerkitsimpl/scan_impl:scan_client",
"${print_path}/services/scan_service:scan_service",
]
if (sane_enable) {
include_dirs += [ "//vendor/open_source/backends/include" ]
deps += [ "//vendor/open_source/backends:third_sane" ]
cflags_cc = [ "-DSANE_ENABLE" ]
}
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"core_service:tel_core_service_api",
"distributed_notification_service:ans_innerkits",
"drivers_interface_usb:libusb_proxy_1.0",
"eventhandler:libeventhandler",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
"init:libbegetutil",
"ipc:ipc_core",
"napi:ace_napi",
"netmanager_base:net_conn_manager_if",
"netmanager_ext:mdns_manager_if",
"os_account:os_account_innerkits",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"usb_manager:usbsrv_client",
]
if (security_guard_enabled) {
external_deps += [
"security_guard:libsg_collect_sdk",
"time_service:time_client",
]
}
sources = [ "scancallbackproxy_fuzzer.cpp" ]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ScanCallbackProxyFuzzTest",
]
}
###############################################################################

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,101 @@
/*
* 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 "fuzzer/FuzzedDataProvider.h"
#include "scancallbackproxy_fuzzer.h"
#include "scan_callback_proxy.h"
namespace OHOS {
namespace Scan {
constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr int MAX_SET_NUMBER = 100;
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
void TestTcpOnCallback(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
uint32_t state = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
ScanDeviceInfoTCP info;
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
scanCallbackProxy.OnCallback(state, info);
}
void TestUsbOnCallback(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
uint32_t state = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
ScanDeviceInfo info;
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
scanCallbackProxy.OnCallback(state, info);
}
void TestOnCallbackSync(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
uint32_t state = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
ScanDeviceInfoSync info;
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
scanCallbackProxy.OnCallbackSync(state, info);
}
void TestOnGetFrameResCallback(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
bool isGetSucc = dataProvider->ConsumeBool();
int32_t sizeRead = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
scanCallbackProxy.OnGetFrameResCallback(isGetSucc, sizeRead);
}
void TestOnScanInitCallback(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
scanCallbackProxy.OnScanInitCallback(scanVersion);
}
void TestOnSendSearchMessage(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string message = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
scanCallbackProxy.OnSendSearchMessage(message);
}
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (data == nullptr) {
return 0;
}
if (size < OHOS::Scan::U32_AT_SIZE || size > OHOS::Scan::FOO_MAX_LEN) {
return 0;
}
FuzzedDataProvider dataProvider(data, size);
OHOS::Scan::TestTcpOnCallback(data, size, &dataProvider);
OHOS::Scan::TestUsbOnCallback(data, size, &dataProvider);
OHOS::Scan::TestOnCallbackSync(data, size, &dataProvider);
OHOS::Scan::TestOnGetFrameResCallback(data, size, &dataProvider);
OHOS::Scan::TestOnScanInitCallback(data, size, &dataProvider);
OHOS::Scan::TestOnSendSearchMessage(data, size, &dataProvider);
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 OHOS_SCAN_CALLBACK_PROXY_FUZZER_H
#define OHOS_SCAN_CALLBACK_PROXY_FUZZER_H
#define FUZZ_PROJECT_NAME "scancallbackproxy_fuzzer"
#endif // OHOS_SCAN_CALLBACK_PROXY_FUZZER_H

View File

@ -0,0 +1,82 @@
# 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.
import("//base/print/print_fwk/print.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
ohos_fuzztest("ScanCapiFuzzTest") {
module_out_path = fuzz_test_output_path
fuzz_config_file = "${print_path}/test/fuzztest/scanhelper_fuzzer"
include_dirs = [
"${print_path}/frameworks/ohscan/include",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_path}/frameworks/helper/scan_helper/include",
"${print_utils_path}/include",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
deps = [
"${print_path}/frameworks/helper/scan_helper:scan_helper",
"${print_path}/frameworks/ohscan/:ohscan",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"eventhandler:libeventhandler",
"hilog:libhilog",
"hisysevent:libhisysevent",
"init:libbegetutil",
"ipc:ipc_core",
"napi:ace_napi",
"os_account:os_account_innerkits",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
]
if (security_guard_enabled) {
external_deps += [
"security_guard:libsg_collect_sdk",
"time_service:time_client",
]
}
sources = [ "scancapi_fuzzer.cpp" ]
}
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ScanCapiFuzzTest",
]
}

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) 2022 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,116 @@
/*
* 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 <functional>
#include "fuzzer/FuzzedDataProvider.h"
#include "ohscan.h"
#include "scancapi_fuzzer.h"
namespace OHOS {
namespace Scan {
constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr int MAX_SET_NUMBER = 100;
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
void OHScanInitFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
OH_Scan_Init();
}
void DiscoveryCallBack(Scan_ScannerDevice** devices, int32_t deviceCount) {}
void OHScanStartScannerDiscoveryFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
OH_Scan_StartScannerDiscovery(DiscoveryCallBack);
}
void OHScanOpenScannerFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
OH_Scan_OpenScanner(scannerId.c_str());
}
void OHScanCloseScannerFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
OH_Scan_CloseScanner(scannerId.c_str());
}
void OHScanGetScannerParameterFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
int32_t errorCode = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
OH_Scan_GetScannerParameter(scannerId.c_str(), &errorCode);
}
void OHScanSetScannerParameterFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
int32_t option = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
std::string value = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
OH_Scan_SetScannerParameter(scannerId.c_str(), option, value.c_str());
}
void OHScanStartScanFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
bool batchMode = dataProvider->ConsumeBool();
OH_Scan_StartScan(scannerId.c_str(), batchMode);
}
void OHScanCancelScanFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
OH_Scan_CancelScan(scannerId.c_str());
}
void OHScanGetScanPictureProgressFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
Scan_ScanPictureProgress progress;
progress.progress = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
progress.fd = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
progress.isFinal = dataProvider->ConsumeBool();
OH_Scan_GetScanPictureProgress(scannerId.c_str(), &progress);
}
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (data == nullptr) {
return 0;
}
if (size < OHOS::Scan::U32_AT_SIZE || size > OHOS::Scan::FOO_MAX_LEN) {
return 0;
}
FuzzedDataProvider dataProvider(data, size);
OHOS::Scan::OHScanInitFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanStartScannerDiscoveryFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanOpenScannerFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanCloseScannerFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanGetScannerParameterFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanSetScannerParameterFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanStartScanFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanCancelScanFuzzTest(data, size, &dataProvider);
OHOS::Scan::OHScanGetScanPictureProgressFuzzTest(data, size, &dataProvider);
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 OHOS_SCAN_OPTION_DESCRIPTOR_FUZZER_H
#define OHOS_SCAN_OPTION_DESCRIPTOR_FUZZER_H
#define FUZZ_PROJECT_NAME "scanoptiondescriptor_fuzzer"
#endif // OHOS_SCAN_OPTION_DESCRIPTOR_FUZZER_H

View File

@ -0,0 +1,74 @@
# 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.
import("//base/print/print_fwk/print.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
ohos_fuzztest("ScanHelperFuzzTest") {
module_out_path = fuzz_test_output_path
fuzz_config_file = "${print_path}/test/fuzztest/scanhelper_fuzzer"
include_dirs = [
"//third_party/node/${print_path}/frameworks/helper/scan_helper/src",
"//third_party/curl/include",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_path}/frameworks/helper/scan_helper/include",
"${print_utils_path}/include",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
deps = [ "${print_path}/frameworks/helper/scan_helper:scan_helper" ]
external_deps = [
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"ability_runtime:data_ability_helper",
"ability_runtime:napi_base_context",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"eventhandler:libeventhandler",
"hilog:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
"samgr:samgr_proxy",
]
if (security_guard_enabled) {
external_deps += [
"security_guard:libsg_collect_sdk",
"time_service:time_client",
]
}
sources = [ "scanhelper_fuzzer.cpp" ]
}
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ScanHelperFuzzTest",
]
}

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) 2022 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,802 @@
/*
* 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 "fuzzer/FuzzedDataProvider.h"
#include "napi/native_api.h"
#include "message_parcel.h"
#include "scanhelper_fuzzer.h"
#include "scan_option_descriptor.h"
#include "scan_option_value.h"
#include "scan_parameters.h"
#include "scan_progress.h"
#include "scanner_info.h"
#include "scan_range.h"
#include "scan_option_descriptor_helper.h"
#include "scan_option_value_helper.h"
#include "scan_parameters_helper.h"
#include "scan_progress_helper.h"
#include "scan_range_helper.h"
#include "scanner_info_helper.h"
namespace OHOS {
namespace Scan {
constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr int MAX_SET_NUMBER = 100;
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
void TestSetOptionName(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
std::string optionName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanOptDes.SetOptionName(optionName);
optionName = scanOptDes.GetOptionName();
}
void TestSetOptionTitle(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
std::string optionTitle = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanOptDes.SetOptionTitle(optionTitle);
optionTitle = scanOptDes.GetOptionTitle();
}
void TestSetOptionDesc(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
std::string optionDesc = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanOptDes.SetOptionDesc(optionDesc);
optionDesc = scanOptDes.GetOptionDesc();
}
void TestSetOptionType(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
uint32_t optionType = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
scanOptDes.SetOptionType(optionType);
optionType = scanOptDes.GetOptionType();
}
void TestSetOptionUnit(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
uint32_t optionUnit = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
scanOptDes.SetOptionUnit(optionUnit);
optionUnit = scanOptDes.GetOptionUnit();
}
void TestSetOptionSize(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
int32_t optionSize = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanOptDes.SetOptionSize(optionSize);
optionSize = scanOptDes.GetOptionSize();
}
void TestSetOptionCap(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
int32_t optionCap = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanOptDes.SetOptionCap(optionCap);
optionCap = scanOptDes.GetOptionCap();
}
void TestSetOptionConstraintType(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
uint32_t optionConstraintType = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
scanOptDes.SetOptionConstraintType(optionConstraintType);
optionConstraintType = scanOptDes.GetOptionConstraintType();
}
void TestSetOptionConstraintString(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
std::vector<std::string> optionConstraintString;
int32_t vectorSize = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
for (int i = 0; i< vectorSize; i++) {
std::string teststr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
optionConstraintString.emplace_back(teststr);
}
scanOptDes.SetOptionConstraintString(optionConstraintString);
scanOptDes.GetOptionConstraintString(optionConstraintString);
}
void TestSetOptionConstraintNumber(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
std::vector<int32_t> optionConstraintNumber;
int32_t vectorSize = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
for (int i = 0; i< vectorSize; i++) {
int32_t testNumber = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
optionConstraintNumber.emplace_back(testNumber);
}
scanOptDes.SetOptionConstraintNumber(optionConstraintNumber);
scanOptDes.GetOptionConstraintNumber(optionConstraintNumber);
}
void TestSetOptionConstraintRange(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDes;
ScanRange optionConstraintRange;
scanOptDes.SetOptionConstraintRange(optionConstraintRange);
scanOptDes.GetOptionConstraintRange(optionConstraintRange);
}
void TestScanOptionDescriptor(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor scanOptDesTmp;
uint32_t optionConstraintType = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
scanOptDesTmp.SetOptionConstraintType(optionConstraintType);
ScanOptionDescriptor scanOptDesBak(scanOptDesTmp);
ScanOptionDescriptor scanOptDesOpera = scanOptDesTmp;
scanOptDesOpera.Dump();
MessageParcel parcel;
scanOptDesOpera.Marshalling(parcel);
auto scanOptDesMarShall = ScanOptionDescriptor::Unmarshalling(parcel);
}
void TestSetScanOptionValueType(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionValue scanOptVal;
int32_t valueType = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanOptVal.SetScanOptionValueType(static_cast<ScanOptionValueType>(valueType));
valueType = scanOptVal.GetScanOptionValueType();
}
void TestSetValueSize(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionValue scanOptVal;
int32_t valueSize = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanOptVal.SetValueSize(valueSize);
valueSize = scanOptVal.GetValueSize();
}
void TestSetNumValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionValue scanOptVal;
int32_t numValue = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanOptVal.SetNumValue(numValue);
numValue = scanOptVal.GetNumValue();
}
void TestSetNumListValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionValue scanOptVal;
int32_t vecSize = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
std::vector<int32_t> numListValue;
for (int i = 0; i < vecSize; i++) {
int32_t number = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
numListValue.emplace_back(number);
}
scanOptVal.SetNumListValue(numListValue);
scanOptVal.GetNumListValue(numListValue);
}
void TestSetStrValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionValue scanOptVal;
std::string optionTitle = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanOptVal.SetStrValue(optionTitle);
optionTitle = scanOptVal.GetStrValue();
}
void TestSetBoolValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionValue scanOptVal;
bool boolValue = dataProvider->ConsumeBool();
scanOptVal.SetBoolValue(boolValue);
boolValue = scanOptVal.GetBoolValue();
}
void TestScanOptionValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionValue scanOptVal;
int32_t valueType = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanOptVal.SetScanOptionValueType(static_cast<ScanOptionValueType>(valueType));
ScanOptionValue scanOptValBak = scanOptVal;
scanOptValBak.Reset();
ScanOptionValue scanOptValOpera(scanOptValBak);
int32_t valueSize = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanOptValOpera.SetValueSize(valueSize);
MessageParcel parcel;
scanOptValOpera.Marshalling(parcel);
auto scanOptValMarShall = ScanOptionDescriptor::Unmarshalling(parcel);
scanOptValMarShall->Dump();
}
void TestSetFormat(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanParameters scanPara;
int32_t format = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanPara.SetFormat(static_cast<ScanFrame>(format));
format = scanPara.GetFormat();
}
void TestSetLastFrame(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanParameters scanPara;
bool lastFrame = dataProvider->ConsumeBool();
scanPara.SetLastFrame(lastFrame);
lastFrame = scanPara.GetLastFrame();
}
void TestSetBytesPerLine(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanParameters scanPara;
int32_t bytesPerLine = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanPara.SetBytesPerLine(bytesPerLine);
bytesPerLine = scanPara.GetBytesPerLine();
}
void TestSetPixelsPerLine(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanParameters scanPara;
int32_t pixelsPerLine = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanPara.SetPixelsPerLine(pixelsPerLine);
pixelsPerLine = scanPara.GetPixelsPerLine();
}
void TestSetLines(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanParameters scanPara;
int32_t lines = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanPara.SetLines(lines);
lines = scanPara.GetLines();
}
void TestSetDepth(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanParameters scanPara;
int32_t depth = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanPara.SetDepth(depth);
depth = scanPara.GetDepth();
}
void TestScanParameters(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanParameters scanPara;
int32_t lines = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanPara.SetLines(lines);
ScanParameters scanParaTmp(scanPara);
ScanParameters scanParaBak = scanParaTmp;
scanParaBak.Dump();
MessageParcel parcel;
scanParaBak.Marshalling(parcel);
auto scanParaBakMarShall = ScanParameters::Unmarshalling(parcel);
}
void TestSetScanProgress(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanProgress scanProg;
int32_t progress = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanProg.SetScanProgress(progress);
progress = scanProg.GetScanProgress();
}
void TestSetScanPictureFd(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanProgress scanProg;
int32_t fd = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanProg.SetScanPictureFd(fd);
fd = scanProg.GetScanPictureFd();
}
void TestSetIsFinal(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanProgress scanProg;
bool isFinal = dataProvider->ConsumeBool();
scanProg.SetIsFinal(isFinal);
isFinal = scanProg.GetIsFinal();
}
void TestSetPictureId(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanProgress scanProg;
int32_t pictureId = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanProg.SetPictureId(pictureId);
pictureId = scanProg.GetPictureId();
}
void TestSetTaskCode(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanProgress scanProg;
int32_t taskCode = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanProg.SetTaskCode(static_cast<ScanErrorCode>(taskCode));
taskCode = scanProg.GetTaskCode();
}
void TestScanProgress(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanProgress scanProg;
int32_t taskCode = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanProg.SetTaskCode(static_cast<ScanErrorCode>(taskCode));
scanProg.Dump();
taskCode = scanProg.GetTaskCode();
ScanProgress scanProgTmp(scanProg);
ScanProgress scanProgBak = scanProgTmp;
MessageParcel parcel;
scanProgBak.Marshalling(parcel);
auto scanOptValMarShall = ScanProgress::Unmarshalling(parcel);
}
void TestTCPSetDeviceName(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string deviceName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetDeviceName(deviceName);
deviceName = scanDevInfoTcp.GetDeviceName();
}
void TestTCPSetUuid(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string uuid = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetUuid(uuid);
uuid = scanDevInfoTcp.GetUuid();
}
void TestTCPSetModel(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string model = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetModel(model);
model = scanDevInfoTcp.GetModel();
}
void TestTCPSetManufacturer(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string manufacturer = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetManufacturer(manufacturer);
manufacturer = scanDevInfoTcp.GetManufacturer();
}
void TestTCPSetDeviceType(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string deviceType = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetDeviceType(deviceType);
deviceType = scanDevInfoTcp.GetDeviceType();
}
void TestTCPSetPort(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string port = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetPort(port);
port = scanDevInfoTcp.GetPort();
}
void TestTCPSetAddr(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string addr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetAddr(addr);
addr = scanDevInfoTcp.GetAddr();
}
void TestTCPSetButton(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string button = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetButton(button);
button = scanDevInfoTcp.GetButton();
}
void TestTCPSetFeeder(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
std::string feeder = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDevInfoTcp.SetFeeder(feeder);
feeder = scanDevInfoTcp.GetFeeder();
}
void TestTCPSetDeviceState(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP scanDevInfoTcp;
uint32_t deviceState = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
scanDevInfoTcp.SetDeviceState(deviceState);
deviceState = scanDevInfoTcp.GetDeviceState();
}
void TestUSBSetDeviceId(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo deviceInfo;
std::string deviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
deviceInfo.SetDeviceId(deviceId);
deviceId = deviceInfo.GetDeviceId();
}
void TestUSBSetManufacturer(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo deviceInfo;
std::string manufacturer = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
deviceInfo.SetManufacturer(manufacturer);
manufacturer = deviceInfo.GetManufacturer();
}
void TestUSBSetModel(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo deviceInfo;
std::string model = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
deviceInfo.SetModel(model);
model = deviceInfo.GetModel();
}
void TestUSBSetDeviceType(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo deviceInfo;
std::string deviceType = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
deviceInfo.SetDeviceType(deviceType);
deviceType = deviceInfo.GetDeviceType();
}
void TestUSBSetDeviceState(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo deviceInfo;
uint32_t deviceState = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
deviceInfo.SetDeviceState(deviceState);
deviceState = deviceInfo.GetDeviceState();
}
void TestUSBSetDiscoverMode(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo deviceInfo;
std::string discoverMode = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
deviceInfo.SetDiscoverMode(discoverMode);
discoverMode = deviceInfo.GetDiscoverMode();
}
void TestUSBSetSerialNumber(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo deviceInfo;
std::string serialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
deviceInfo.SetSerialNumber(serialNumber);
serialNumber = deviceInfo.GetSerialNumber();
}
void TestSetMinValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanRange range;
int32_t minValue = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
range.SetMinValue(minValue);
minValue = range.GetMinValue();
}
void TestSetMaxValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanRange range;
int32_t maxValue = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
range.SetMaxValue(maxValue);
maxValue = range.GetMaxValue();
}
void TestSetQuantValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanRange range;
int32_t quantValue = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
range.SetQuantValue(quantValue);
quantValue = range.GetQuantValue();
}
void TestScanRange(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanRange range;
range.Reset();
int32_t maxValue = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
range.SetMaxValue(maxValue);
ScanRange rangeBak(range);
MessageParcel parcel;
range.Marshalling(parcel);
auto rangeMarshall = ScanRange::Unmarshalling(parcel);
}
void TestDeviceInfo(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP infoTcp;
uint32_t deviceState = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
infoTcp.SetDeviceState(deviceState);
ScanDeviceInfoTCP infoTcpTmp(infoTcp);
ScanDeviceInfoTCP infoTcpBak(infoTcpTmp);
MessageParcel parcel;
infoTcpBak.Marshalling(parcel);
auto infoTcpMarshall = ScanDeviceInfoTCP::Unmarshalling(parcel);
ScanDeviceInfo info;
info.SetDeviceState(deviceState);
ScanDeviceInfo infoTmp(info);
ScanDeviceInfo infoBak(infoTmp);
infoBak.Marshalling(parcel);
auto infoMarshall = ScanDeviceInfo::Unmarshalling(parcel);
}
void TestScanOptDesNapiInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
napi_env env = nullptr;
ScanOptionDescriptor info;
uint32_t optionUnit = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
info.SetOptionUnit(optionUnit);
ScanOptionDescriptorHelper::MakeJsObject(env, info);
napi_value jsValue = nullptr;
ScanOptionDescriptorHelper::BuildFromJs(env, jsValue);
}
void TestScanOptValNapiInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
napi_env env = nullptr;
ScanOptionValue info;
int32_t valueType = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
info.SetScanOptionValueType(static_cast<ScanOptionValueType>(valueType));
ScanOptionValueHelper::MakeJsObject(env, info);
napi_value jsValue = nullptr;
ScanOptionValueHelper::BuildFromJs(env, jsValue);
}
void TestScanParaNapiInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
napi_env env = nullptr;
ScanParameters info;
int32_t format = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
info.SetFormat(static_cast<ScanFrame>(format));
ScanParametersHelper::MakeJsObject(env, info);
napi_value jsValue = nullptr;
ScanParametersHelper::BuildFromJs(env, jsValue);
}
void TestScanProgNapiInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
napi_env env = nullptr;
ScanProgress info;
int32_t progress = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
info.SetScanProgress(progress);
ScanProgressHelper::MakeJsObject(env, info);
}
void TestScanRangeNapiInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
napi_env env = nullptr;
ScanRange info;
int32_t maxValue = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
info.SetMaxValue(maxValue);
ScanRangeHelper::MakeJsObject(env, info);
napi_value jsValue = nullptr;
ScanRangeHelper::BuildFromJs(env, jsValue);
}
void TestScannerInfoNapiInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
napi_env env = nullptr;
ScanDeviceInfo info;
std::string deviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
info.SetDeviceId(deviceId);
ScannerInfoHelper::MakeJsObject(env, info);
}
void TestScannerInfoTCPNapiInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
napi_env env = nullptr;
ScanDeviceInfoTCP info;
uint32_t deviceState = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
info.SetDeviceState(deviceState);
ScannerInfoHelperTCP::MakeJsObject(env, info);
}
void TestGetExtensionIdInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string globalId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
NapiScanUtils::GetExtensionId(globalId);
}
void TestGetGlobalIdInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string globalId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string localId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
NapiScanUtils::GetGlobalId(globalId, localId);
}
void TestGetLocalIdInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string globalId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string extensionId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
NapiScanUtils::GetLocalId(globalId, extensionId);
}
void TestEncodeExtensionCidInterface(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string globalId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
uint32_t callbackId = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
NapiScanUtils::EncodeExtensionCid(globalId, callbackId);
}
void TestDecodeExtensionCid(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string cid = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string extensionId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
uint32_t callbackId = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
NapiScanUtils::DecodeExtensionCid(cid, extensionId, callbackId);
}
void TestGetTaskEventId(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
NapiScanUtils::GetTaskEventId(taskId, type);
}
void TestOpenFile(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string filePath = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
NapiScanUtils::OpenFile(filePath);
}
void TestIsPathValid(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string filePath = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
NapiScanUtils::IsPathValid(filePath);
}
void ScanOptionDescriptorFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestSetOptionName(data, size, dataProvider);
TestSetOptionTitle(data, size, dataProvider);
TestSetOptionDesc(data, size, dataProvider);
TestSetOptionType(data, size, dataProvider);
TestSetOptionUnit(data, size, dataProvider);
TestSetOptionSize(data, size, dataProvider);
TestSetOptionCap(data, size, dataProvider);
TestSetOptionConstraintType(data, size, dataProvider);
TestSetOptionConstraintString(data, size, dataProvider);
TestSetOptionConstraintNumber(data, size, dataProvider);
TestSetOptionConstraintRange(data, size, dataProvider);
TestScanOptionDescriptor(data, size, dataProvider);
}
void ScanOptionValueFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestSetScanOptionValueType(data, size, dataProvider);
TestSetValueSize(data, size, dataProvider);
TestSetNumValue(data, size, dataProvider);
TestSetNumListValue(data, size, dataProvider);
TestSetStrValue(data, size, dataProvider);
TestSetBoolValue(data, size, dataProvider);
TestScanOptionValue(data, size, dataProvider);
}
void ScanParametersFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestSetFormat(data, size, dataProvider);
TestSetLastFrame(data, size, dataProvider);
TestSetBytesPerLine(data, size, dataProvider);
TestSetPixelsPerLine(data, size, dataProvider);
TestSetLines(data, size, dataProvider);
TestSetDepth(data, size, dataProvider);
TestScanParameters(data, size, dataProvider);
}
void ScanDeviceInfoFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestSetScanProgress(data, size, dataProvider);
TestSetScanPictureFd(data, size, dataProvider);
TestSetIsFinal(data, size, dataProvider);
TestSetPictureId(data, size, dataProvider);
TestSetTaskCode(data, size, dataProvider);
TestScanProgress(data, size, dataProvider);
}
void ScannerInfoFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestTCPSetDeviceName(data, size, dataProvider);
TestTCPSetUuid(data, size, dataProvider);
TestTCPSetModel(data, size, dataProvider);
TestTCPSetManufacturer(data, size, dataProvider);
TestTCPSetDeviceType(data, size, dataProvider);
TestTCPSetPort(data, size, dataProvider);
TestTCPSetAddr(data, size, dataProvider);
TestTCPSetButton(data, size, dataProvider);
TestTCPSetFeeder(data, size, dataProvider);
TestTCPSetDeviceState(data, size, dataProvider);
TestUSBSetDeviceId(data, size, dataProvider);
TestUSBSetManufacturer(data, size, dataProvider);
TestUSBSetModel(data, size, dataProvider);
TestUSBSetDeviceType(data, size, dataProvider);
TestUSBSetDeviceState(data, size, dataProvider);
TestUSBSetDiscoverMode(data, size, dataProvider);
TestUSBSetSerialNumber(data, size, dataProvider);
TestDeviceInfo(data, size, dataProvider);
}
void ScanRangeFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestSetMinValue(data, size, dataProvider);
TestSetMaxValue(data, size, dataProvider);
TestSetQuantValue(data, size, dataProvider);
TestScanRange(data, size, dataProvider);
}
void ScanOptionDescriptorHelperFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestScanOptDesNapiInterface(data, size, dataProvider);
}
void ScanOptionValueHelperFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestScanOptValNapiInterface(data, size, dataProvider);
}
void ScanParametersHelperFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestScanParaNapiInterface(data, size, dataProvider);
}
void ScanProgressHelperFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestScanProgNapiInterface(data, size, dataProvider);
}
void ScanRangeHelperFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestScanRangeNapiInterface(data, size, dataProvider);
}
void ScannerInfoHelperFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestScannerInfoNapiInterface(data, size, dataProvider);
TestScannerInfoTCPNapiInterface(data, size, dataProvider);
}
void NapiScanUtilsFuzzTest(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
TestGetExtensionIdInterface(data, size, dataProvider);
TestGetGlobalIdInterface(data, size, dataProvider);
TestGetLocalIdInterface(data, size, dataProvider);
TestEncodeExtensionCidInterface(data, size, dataProvider);
TestDecodeExtensionCid(data, size, dataProvider);
TestGetTaskEventId(data, size, dataProvider);
TestOpenFile(data, size, dataProvider);
TestIsPathValid(data, size, dataProvider);
}
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (data == nullptr) {
return 0;
}
if (size < OHOS::Scan::U32_AT_SIZE || size > OHOS::Scan::FOO_MAX_LEN) {
return 0;
}
FuzzedDataProvider dataProvider(data, size);
OHOS::Scan::ScanOptionDescriptorFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanOptionValueFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanParametersFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanDeviceInfoFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScannerInfoFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanRangeFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanOptionDescriptorHelperFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanOptionValueHelperFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanParametersHelperFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanProgressHelperFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScanRangeHelperFuzzTest(data, size, &dataProvider);
OHOS::Scan::ScannerInfoHelperFuzzTest(data, size, &dataProvider);
OHOS::Scan::NapiScanUtilsFuzzTest(data, size, &dataProvider);
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 OHOS_SCAN_OPTION_DESCRIPTOR_FUZZER_H
#define OHOS_SCAN_OPTION_DESCRIPTOR_FUZZER_H
#define FUZZ_PROJECT_NAME "scanoptiondescriptor_fuzzer"
#endif // OHOS_SCAN_OPTION_DESCRIPTOR_FUZZER_H

View File

@ -0,0 +1,100 @@
# 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.
import("//base/print/print_fwk/print.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("ScanMdnsServiceFuzzTest") {
module_out_path = fuzz_test_output_path
fuzz_config_file = "${print_path}/test/fuzztest/scanmdnsservice_fuzzer"
include_dirs = [
"${print_path}/services/scan_service/include",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_utils_path}/include",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
deps = [
"${print_path}/frameworks/helper/scan_helper:scan_helper",
"${print_path}/frameworks/innerkitsimpl/scan_impl:scan_client",
"${print_path}/services/scan_service:scan_service",
]
if (sane_enable) {
include_dirs += [ "//vendor/open_source/backends/include" ]
deps += [ "//vendor/open_source/backends:third_sane" ]
cflags_cc = [ "-DSANE_ENABLE" ]
}
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"core_service:tel_core_service_api",
"distributed_notification_service:ans_innerkits",
"drivers_interface_usb:libusb_proxy_1.0",
"eventhandler:libeventhandler",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
"init:libbegetutil",
"ipc:ipc_core",
"libjpeg-turbo:turbojpeg",
"napi:ace_napi",
"netmanager_base:net_conn_manager_if",
"netmanager_ext:mdns_manager_if",
"os_account:os_account_innerkits",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"usb_manager:usbsrv_client",
]
if (security_guard_enabled) {
external_deps += [
"security_guard:libsg_collect_sdk",
"time_service:time_client",
]
}
sources = [ "scanmdnsservice_fuzzer.cpp" ]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ScanMdnsServiceFuzzTest",
]
}
###############################################################################

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,152 @@
/*
* 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 "fuzzer/FuzzedDataProvider.h"
#include "scanmdnsservice_fuzzer.h"
#include "scan_mdns_service.h"
namespace OHOS {
namespace Scan {
constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr int MAX_SET_NUMBER = 100;
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
void TestOnStartDiscoverService(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanMdnsService::GetInstance().SetServiceType("_scanner._tcp");
ScanMdnsService::GetInstance().onStartDiscoverService();
}
void TestSetServiceInfo(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MDnsServiceInfo info;
ScanMdnsService::GetInstance().SetServiceInfo(info);
}
void TestSetMDnsResolveCallBack(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
sptr<ScanMDnsResolveObserver> cb;
ScanMdnsService::GetInstance().SetMDnsResolveCallBack(cb);
}
void TestSetMDnsDiscoveryCallBack(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
sptr<ScanMDnsDiscoveryObserver> cb;
ScanMdnsService::GetInstance().SetMDnsDiscoveryCallBack(cb);
ScanMdnsService::GetInstance().onStartDiscoverService();
}
void TestSetServiceType(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string stype = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanMdnsService::GetInstance().SetServiceType(stype);
}
void TestGetMDnsResolveCallBack(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanMdnsService::GetInstance().GetMDnsResolveCallBack();
}
void TestGetMDnsDiscoveryCallBack(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanMdnsService::GetInstance().GetMDnsDiscoveryCallBack();
}
void TestGetServiceInfo(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanMdnsService::GetInstance().GetServiceInfo();
}
void TestGetServiceAttribute(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MDnsServiceInfo serviceInfo;
std::string keyStr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanMdnsService::GetInstance().GetServiceAttribute(serviceInfo, keyStr);
}
void TestonStopDiscoverService(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanMdnsService::GetInstance().onStopDiscoverService();
}
void TestonResolveService(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MDnsServiceInfo serviceInfo;
ScanMdnsService::GetInstance().onResolveService(serviceInfo);
}
void TestToMDnsScaner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MDnsServiceInfo serviceInfo;
ScanMdnsService::GetInstance().ToMDnsScaner(serviceInfo);
}
void TestHandleServiceFound(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MDnsServiceInfo serviceInfo;
ScanMDnsDiscoveryObserver scanMDnsDiscoveryObserver = ScanMDnsDiscoveryObserver(serviceInfo);
int32_t retCode = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanMDnsDiscoveryObserver.HandleServiceFound(serviceInfo, retCode);
}
void TestHandleResolveResult(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MDnsServiceInfo serviceInfo;
ScanMDnsResolveObserver scanMDnsResolveObserver = ScanMDnsResolveObserver(serviceInfo);
int32_t retCode = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanMDnsResolveObserver.HandleResolveResult(serviceInfo, retCode);
}
void TestHandleStopDiscover(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MDnsServiceInfo serviceInfo;
ScanMDnsDiscoveryObserver scanMDnsDiscoveryObserver = ScanMDnsDiscoveryObserver(serviceInfo);
int32_t retCode = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
scanMDnsDiscoveryObserver.HandleStopDiscover(serviceInfo, retCode);
}
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (data == nullptr) {
return 0;
}
if (size < OHOS::Scan::U32_AT_SIZE || size > OHOS::Scan::FOO_MAX_LEN) {
return 0;
}
FuzzedDataProvider dataProvider(data, size);
OHOS::Scan::TestOnStartDiscoverService(data, size, &dataProvider);
OHOS::Scan::TestSetServiceInfo(data, size, &dataProvider);
OHOS::Scan::TestSetMDnsResolveCallBack(data, size, &dataProvider);
OHOS::Scan::TestSetMDnsDiscoveryCallBack(data, size, &dataProvider);
OHOS::Scan::TestSetServiceType(data, size, &dataProvider);
OHOS::Scan::TestGetMDnsResolveCallBack(data, size, &dataProvider);
OHOS::Scan::TestGetMDnsDiscoveryCallBack(data, size, &dataProvider);
OHOS::Scan::TestGetServiceInfo(data, size, &dataProvider);
OHOS::Scan::TestGetServiceAttribute(data, size, &dataProvider);
OHOS::Scan::TestonStopDiscoverService(data, size, &dataProvider);
OHOS::Scan::TestonResolveService(data, size, &dataProvider);
OHOS::Scan::TestToMDnsScaner(data, size, &dataProvider);
OHOS::Scan::TestHandleServiceFound(data, size, &dataProvider);
OHOS::Scan::TestHandleResolveResult(data, size, &dataProvider);
OHOS::Scan::TestHandleStopDiscover(data, size, &dataProvider);
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 OHOS_SCAN_MDNS_SERVICE_FUZZER_H
#define OHOS_SCAN_MDNS_SERVICE_FUZZER_H
#define FUZZ_PROJECT_NAME "scanmdnsservice_fuzzer"
#endif // OHOS_SCAN_MDNS_SERVICE_FUZZER_H

View File

@ -0,0 +1,106 @@
# 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.
import("//base/print/print_fwk/print.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("ScanServiceAbilityFuzzTest") {
module_out_path = fuzz_test_output_path
fuzz_config_file = "${print_path}/test/fuzztest/scanserviceability_fuzzer"
include_dirs = [
"mock",
"${print_path}/services/scan_service/include",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_utils_path}/include",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
deps = [
"${print_path}/frameworks/helper/scan_helper:scan_helper",
"${print_path}/frameworks/innerkitsimpl/scan_impl:scan_client",
"${print_path}/services/scan_service:scan_service",
]
if (sane_enable) {
include_dirs += [ "//vendor/open_source/backends/include" ]
deps += [ "//vendor/open_source/backends:third_sane" ]
cflags_cc = [ "-DSANE_ENABLE" ]
}
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"core_service:tel_core_service_api",
"distributed_notification_service:ans_innerkits",
"drivers_interface_usb:libusb_proxy_1.0",
"eventhandler:libeventhandler",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
"init:libbegetutil",
"ipc:ipc_core",
"libjpeg-turbo:turbojpeg",
"napi:ace_napi",
"netmanager_base:net_conn_manager_if",
"netmanager_ext:mdns_manager_if",
"os_account:os_account_innerkits",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"usb_manager:usbsrv_client",
]
if (security_guard_enabled) {
external_deps += [
"security_guard:libsg_collect_sdk",
"time_service:time_client",
]
}
sources = [
"scanserviceability_fuzzer.cpp",
"scanserviceability_mock.cpp",
]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ScanServiceAbilityFuzzTest",
]
}
###############################################################################

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,27 @@
/*
* 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 SCAN_SERVICE_ABILITY_MOCK_H
#define SCAN_SERVICE_ABILITY_MOCK_H
namespace OHOS {
namespace Scan {
class ScanServiceAbilityMock {
public:
static void MockPermission();
};
} // namespace Scan
} // namespace OHOS
#endif // SCAN_SERVICE_ABILITY_MOCK_H

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,606 @@
/*
* 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.
*/
#define private public
#define protected public
#include "fuzzer/FuzzedDataProvider.h"
#include "scanserviceability_fuzzer.h"
#include "scan_service_ability.h"
#include "scan_callback_proxy.h"
#include "scan_service_ability_mock.h"
namespace OHOS {
namespace Scan {
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
#ifdef SANE_ENABLE
constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr int MAX_SET_NUMBER = 100;
void TestInitScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
}
void TestExitScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanServiceAbility::GetInstance()->ExitScan();
}
void TestGetScannerList(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
OHOS::Scan::ScanServiceAbilityMock::MockPermission();
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
ScanServiceAbility::GetInstance()->GetScannerList();
}
void TestStopDiscover(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
ScanServiceAbility::GetInstance()->StopDiscover();
}
void TestOpenScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->OpenScanner(scannerId);
}
void TestCloseScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->OpenScanner(scannerId);
ScanServiceAbility::GetInstance()->CloseScanner(scannerId);
}
void TestGetScanOptionDesc(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
int32_t optionIndex = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanOptionDescriptor desc;
ScanServiceAbility::GetInstance()->GetScanOptionDesc(scannerId, optionIndex, desc);
}
void TestOpScanOptionValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestGetScanParameters(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestStartScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
bool batchMode = dataProvider->ConsumeBool();
ScanServiceAbility::GetInstance()->StartScan(scannerId, batchMode);
}
void TestGetSingleFrameFD(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
uint32_t frameSize = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
uint32_t fd = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->GetSingleFrameFD(scannerId, frameSize, fd);
}
void TestCancelScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->CancelScan(scannerId);
}
void TestSetScanIOMode(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
bool isNonBlocking = dataProvider->ConsumeBool();
ScanServiceAbility::GetInstance()->SetScanIOMode(scannerId, isNonBlocking);
}
void TestGetScanSelectFd(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
int32_t fd = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->GetScanSelectFd(scannerId, fd);
}
void TestOn(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
sptr<IScanCallback> listener = iface_cast<IScanCallback>(impl);
ScanServiceAbility::registeredListeners_[type] = listener;
ScanServiceAbility::GetInstance()->On(taskId, type, listener);
ScanServiceAbility::registeredListeners_.clear();
}
void TestOff(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
sptr<IRemoteObject> impl;
ScanCallbackProxy scanCallbackProxy(impl);
sptr<IScanCallback> listener = iface_cast<IScanCallback>(impl);
ScanServiceAbility::registeredListeners_[type] = listener;
ScanServiceAbility::GetInstance()->Off(taskId, type);
}
void TestGetScannerState(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->InitScan(scanVersion);
int32_t scannerState = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->GetScannerState(scannerState);
}
void TestGetScanProgress(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestActionSetAuto(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestActionGetValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestActionSetValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestSelectScanOptionDesc(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
SANE_Option_Descriptor optionDesc;
const SANE_Option_Descriptor *optionDescPtr = &optionDesc;
ScanOptionDescriptor desc;
optionDesc.constraint_type = SANE_CONSTRAINT_RANGE;
SANE_Range range;
range.min = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
range.max = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
range.quant = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
const SANE_Range* rangePtr = &range;
optionDesc.constraint.range = rangePtr;
ScanServiceAbility::GetInstance()->SelectScanOptionDesc(optionDescPtr, desc);
optionDesc.constraint_type = SANE_CONSTRAINT_WORD_LIST;
int32_t sizeNumber = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
optionDesc.constraint.word_list = &sizeNumber;
ScanServiceAbility::GetInstance()->SelectScanOptionDesc(optionDescPtr, desc);
optionDesc.constraint_type = SANE_CONSTRAINT_STRING_LIST;
optionDesc.constraint.string_list = nullptr;
ScanServiceAbility::GetInstance()->SelectScanOptionDesc(optionDescPtr, desc);
}
void TestDoScanTask(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestOnStartScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
bool batchMode = dataProvider->ConsumeBool();
ScanServiceAbility::GetInstance()->OnStartScan(scannerId, batchMode);
}
void TestStartScanTask(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->StartScanTask(scannerId);
ScanServiceAbility::GetInstance()->batchMode_ = true;
ScanServiceAbility::GetInstance()->StartScanTask(scannerId);
}
void TestSendDeviceInfoTCP(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoTCP info;
std::string event = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->SendDeviceInfoTCP(info, event);
}
void TestSendDeviceInfo(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo info;
std::string event = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->SendDeviceInfo(info, event);
}
void TestSendDeviceInfoSync(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfoSync info;
std::string event = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->SendDeviceInfoSync(info, event);
}
void TestDisConnectUsbScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string serialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string newDeviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->DisConnectUsbScanner(serialNumber, newDeviceId);
}
void TestUpdateUsbScannerId(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string serialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string newDeviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->UpdateUsbScannerId(serialNumber, newDeviceId);
}
void TestSendInitEvent(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
std::string event = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->SendInitEvent(scanVersion, event);
}
void TestSendDeviceSearchEnd(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string message = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string event = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->SendDeviceSearchEnd(message, event);
}
void TestSetScannerSerialNumber(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo tcpInfo;
tcpInfo.deviceId = "pantum6500:tcp 192.168.223.1 9200-M6700DW";
std::string ip = "192.168.223.1";
ScanDeviceInfoTCP scanDeviceInfoTCP;
scanDeviceInfoTCP.deviceName = "Pantum 6666 54QWER";
ScanServiceAbility::scanDeviceInfoTCPMap_[ip] = scanDeviceInfoTCP;
ScanServiceAbility::GetInstance()->SetScannerSerialNumber(tcpInfo);
tcpInfo.deviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ip = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDeviceInfoTCP.deviceName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::scanDeviceInfoTCPMap_[ip] = scanDeviceInfoTCP;
ScanServiceAbility::GetInstance()->SetScannerSerialNumber(tcpInfo);
ScanDeviceInfo usbInfo;
usbInfo.deviceId = "pantum6500:libusb:002:003";
ScanServiceAbility::usbSnMap["2-3"] = "QWERTY";
ScanServiceAbility::GetInstance()->SetScannerSerialNumber(usbInfo);
usbInfo.deviceId = "pantum6500:libusb:002:004";
ScanServiceAbility::GetInstance()->SetScannerSerialNumber(usbInfo);
usbInfo.deviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->SetScannerSerialNumber(usbInfo);
}
void TestSaneGetScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanServiceAbility::GetInstance()->SaneGetScanner();
}
void TestOnStart(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanServiceAbility::GetInstance()->OnStart();
}
void TestOnStop(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanServiceAbility::GetInstance()->OnStop();
}
void TestServiceInit(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanServiceAbility::GetInstance()->ServiceInit();
}
void TestInitServiceHandler(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanServiceAbility::GetInstance()->InitServiceHandler();
}
void TestManualStart(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanServiceAbility::GetInstance()->ManualStart();
}
void TestReInitScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t scanVersion = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->ReInitScan(scanVersion);
}
void TestCheckPermission(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string permissionName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->CheckPermission(permissionName);
}
void TestSendGetFrameResEvent(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
bool isGetSucc = dataProvider->ConsumeBool();
int32_t sizeRead = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
ScanServiceAbility::GetInstance()->SendGetFrameResEvent(isGetSucc, sizeRead);
}
void TestSetScanOptionDescriptor(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanOptionDescriptor desc;
SANE_Option_Descriptor optionDesc;
optionDesc.name = "";
optionDesc.title = "";
optionDesc.desc = "";
ScanServiceAbility::GetInstance()->SetScanOptionDescriptor(desc, &optionDesc);
}
void TestGetScanHandle(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->GetScanHandle(scannerId);
}
void TestWriteJpegHeader(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestGeneratePicture(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestGetPicFrame(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestWritePicData(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestGeneratePictureBatch(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
int32_t status = E_SCAN_NONE;
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string file_name = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string output_file = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanProgress* scanProPtr = nullptr;
ScanServiceAbility::GetInstance()->GeneratePictureSingle(scannerId, file_name, output_file, status, scanProPtr);
}
void TestGeneratePictureSingle(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
return;
}
void TestAddFoundUsbScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo info;
info.serialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->AddFoundUsbScanner(info);
}
void TestAddFoundTcpScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo info;
info.serialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->AddFoundTcpScanner(info);
}
void TestSetScannerInfo(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
SANE_Device currentDevice;
const SANE_Device* currentDevicePtr = &currentDevice;
const SANE_Device** currentDevicePtrPtr = &currentDevicePtr;
std::string name = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string vendor = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string model = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
currentDevice.name = name.c_str();
currentDevice.vendor = vendor.c_str();
currentDevice.vendor = model.c_str();
currentDevice.type = type.c_str();
ScanDeviceInfo info;
ScanServiceAbility::GetInstance()->SetScannerInfo(currentDevicePtrPtr, info);
}
void TestSendDeviceList(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo infoItem;
std::vector<ScanDeviceInfo> info;
info.push_back(infoItem);
std::string event = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->SendDeviceList(info, event);
}
void TestAddScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string serialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string discoverMode = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->AddScanner(serialNumber, discoverMode);
}
void TestUpdateScannerName(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string serialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string discoverMode = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string deviceName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->UpdateScannerName(serialNumber, discoverMode, deviceName);
}
void TestCleanScanTask(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::GetInstance()->CleanScanTask(scannerId);
}
void TestGetAddedScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
ScanDeviceInfo addedScanner;
std::vector<ScanDeviceInfo> allAddedScanner;
allAddedScanner.push_back(addedScanner);
ScanServiceAbility::GetInstance()->GetAddedScanner(allAddedScanner);
}
void TestNotPublicFunction(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
if (dataProvider == nullptr) {
return;
}
OHOS::Scan::TestOnStart(data, size, dataProvider);
OHOS::Scan::TestOnStop(data, size, dataProvider);
OHOS::Scan::TestServiceInit(data, size, dataProvider);
OHOS::Scan::TestInitServiceHandler(data, size, dataProvider);
OHOS::Scan::TestManualStart(data, size, dataProvider);
OHOS::Scan::TestReInitScan(data, size, dataProvider);
OHOS::Scan::TestCheckPermission(data, size, dataProvider);
OHOS::Scan::TestSendGetFrameResEvent(data, size, dataProvider);
OHOS::Scan::TestSetScanOptionDescriptor(data, size, dataProvider);
OHOS::Scan::TestGetScanHandle(data, size, dataProvider);
OHOS::Scan::TestWriteJpegHeader(data, size, dataProvider);
OHOS::Scan::TestGeneratePicture(data, size, dataProvider);
OHOS::Scan::TestGetPicFrame(data, size, dataProvider);
OHOS::Scan::TestWritePicData(data, size, dataProvider);
OHOS::Scan::TestGeneratePictureBatch(data, size, dataProvider);
OHOS::Scan::TestGeneratePictureSingle(data, size, dataProvider);
OHOS::Scan::TestAddFoundUsbScanner(data, size, dataProvider);
OHOS::Scan::TestAddFoundTcpScanner(data, size, dataProvider);
OHOS::Scan::TestSetScannerInfo(data, size, dataProvider);
OHOS::Scan::TestSendDeviceList(data, size, dataProvider);
OHOS::Scan::TestAddScanner(data, size, dataProvider);
OHOS::Scan::TestUpdateScannerName(data, size, dataProvider);
OHOS::Scan::TestCleanScanTask(data, size, dataProvider);
OHOS::Scan::TestGetAddedScanner(data, size, dataProvider);
}
void TestPublicFunction(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
if (dataProvider == nullptr) {
return;
}
OHOS::Scan::TestInitScan(data, size, dataProvider);
OHOS::Scan::TestExitScan(data, size, dataProvider);
OHOS::Scan::TestGetScannerList(data, size, dataProvider);
OHOS::Scan::TestStopDiscover(data, size, dataProvider);
OHOS::Scan::TestOpenScanner(data, size, dataProvider);
OHOS::Scan::TestCloseScanner(data, size, dataProvider);
OHOS::Scan::TestGetScanOptionDesc(data, size, dataProvider);
OHOS::Scan::TestOpScanOptionValue(data, size, dataProvider);
OHOS::Scan::TestGetScanParameters(data, size, dataProvider);
OHOS::Scan::TestStartScan(data, size, dataProvider);
OHOS::Scan::TestGetSingleFrameFD(data, size, dataProvider);
OHOS::Scan::TestCancelScan(data, size, dataProvider);
OHOS::Scan::TestSetScanIOMode(data, size, dataProvider);
OHOS::Scan::TestGetScanSelectFd(data, size, dataProvider);
OHOS::Scan::TestOn(data, size, dataProvider);
OHOS::Scan::TestOff(data, size, dataProvider);
OHOS::Scan::TestGetScannerState(data, size, dataProvider);
OHOS::Scan::TestGetScanProgress(data, size, dataProvider);
OHOS::Scan::TestActionSetAuto(data, size, dataProvider);
OHOS::Scan::TestActionGetValue(data, size, dataProvider);
OHOS::Scan::TestActionSetValue(data, size, dataProvider);
OHOS::Scan::TestSelectScanOptionDesc(data, size, dataProvider);
OHOS::Scan::TestDoScanTask(data, size, dataProvider);
OHOS::Scan::TestOnStartScan(data, size, dataProvider);
OHOS::Scan::TestStartScanTask(data, size, dataProvider);
OHOS::Scan::TestSendDeviceInfoTCP(data, size, dataProvider);
OHOS::Scan::TestSendDeviceInfo(data, size, dataProvider);
OHOS::Scan::TestSendDeviceInfoSync(data, size, dataProvider);
OHOS::Scan::TestDisConnectUsbScanner(data, size, dataProvider);
OHOS::Scan::TestUpdateUsbScannerId(data, size, dataProvider);
OHOS::Scan::TestSendInitEvent(data, size, dataProvider);
OHOS::Scan::TestSetScannerSerialNumber(data, size, dataProvider);
OHOS::Scan::TestSaneGetScanner(data, size, dataProvider);
}
#endif
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (data == nullptr) {
return 0;
}
if (size < OHOS::Scan::U32_AT_SIZE || size > OHOS::Scan::FOO_MAX_LEN) {
return 0;
}
#ifdef SANE_ENABLE
FuzzedDataProvider dataProvider(data, size);
OHOS::Scan::TestPublicFunction(data, size, &dataProvider);
OHOS::Scan::ScanServiceAbilityMock::MockPermission();
OHOS::Scan::TestInitScan(data, size, &dataProvider);
OHOS::Scan::TestExitScan(data, size, &dataProvider);
// OHOS::Scan::TestGetScannerList(data, size, &dataProvider);
// OHOS::Scan::TestStopDiscover(data, size, dataProvider);
OHOS::Scan::TestOpenScanner(data, size, &dataProvider);
OHOS::Scan::TestCloseScanner(data, size, &dataProvider);
OHOS::Scan::TestGetScanOptionDesc(data, size, &dataProvider);
OHOS::Scan::TestOpScanOptionValue(data, size, &dataProvider);
OHOS::Scan::TestGetScanParameters(data, size, &dataProvider);
OHOS::Scan::TestStartScan(data, size, &dataProvider);
OHOS::Scan::TestGetSingleFrameFD(data, size, &dataProvider);
OHOS::Scan::TestCancelScan(data, size, &dataProvider);
OHOS::Scan::TestSetScanIOMode(data, size, &dataProvider);
OHOS::Scan::TestGetScanSelectFd(data, size, &dataProvider);
OHOS::Scan::TestOn(data, size, &dataProvider);
OHOS::Scan::TestOff(data, size, &dataProvider);
OHOS::Scan::TestGetScannerState(data, size, &dataProvider);
OHOS::Scan::TestGetScanProgress(data, size, &dataProvider);
OHOS::Scan::TestActionSetAuto(data, size, &dataProvider);
OHOS::Scan::TestActionGetValue(data, size, &dataProvider);
OHOS::Scan::TestActionSetValue(data, size, &dataProvider);
OHOS::Scan::TestSelectScanOptionDesc(data, size, &dataProvider);
OHOS::Scan::TestDoScanTask(data, size, &dataProvider);
OHOS::Scan::TestOnStartScan(data, size, &dataProvider);
OHOS::Scan::TestStartScanTask(data, size, &dataProvider);
OHOS::Scan::TestSendDeviceInfoTCP(data, size, &dataProvider);
OHOS::Scan::TestSendDeviceInfo(data, size, &dataProvider);
OHOS::Scan::TestSendDeviceInfoSync(data, size, &dataProvider);
OHOS::Scan::TestDisConnectUsbScanner(data, size, &dataProvider);
// OHOS::Scan::TestUpdateUsbScannerId(data, size, &dataProvider);
OHOS::Scan::TestSendInitEvent(data, size, &dataProvider);
// OHOS::Scan::TestSendDeviceSearchEnd(data, size, &dataProvider);
OHOS::Scan::TestSetScannerSerialNumber(data, size, &dataProvider);
OHOS::Scan::TestSaneGetScanner(data, size, &dataProvider);
OHOS::Scan::TestNotPublicFunction(data, size, &dataProvider);
#endif
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 OHOS_SCAN_SERVICE_ABILITY_FUZZER_H
#define OHOS_SCAN_SERVICE_ABILITY_FUZZER_H
#define FUZZ_PROJECT_NAME "scanserviceability_fuzzer"
#endif // OHOS_SCAN_SERVICE_ABILITY_FUZZER_H

View File

@ -0,0 +1,43 @@
/*
* 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 "scan_service_ability_mock.h"
#include "nativetoken_kit.h"
#include "token_setproc.h"
namespace OHOS {
namespace Scan {
void ScanServiceAbilityMock::MockPermission()
{
static const char *PERMS[] = {
"ohos.permission.MANAGE_PRINT_JOB"
};
uint64_t tokenId;
NativeTokenInfoParams infoInstance = {
.dcapsNum = 0,
.permsNum = 1,
.aclsNum = 0,
.dcaps = nullptr,
.perms = PERMS,
.acls = nullptr,
.processName = "scan_service",
.aplStr = "system_core",
};
tokenId = GetAccessTokenId(&infoInstance);
SetSelfTokenID(tokenId);
}
}
}

View File

@ -0,0 +1,100 @@
# Copyright (c) 2022 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.
import("//base/print/print_fwk/print.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("ScanServiceStubFuzzTest") {
module_out_path = fuzz_test_output_path
fuzz_config_file = "${print_path}/test/fuzztest/scanservicestub_fuzzer"
include_dirs = [
"${print_path}/services/scan_service/include",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_utils_path}/include",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
deps = [
"${print_path}/frameworks/helper/scan_helper:scan_helper",
"${print_path}/frameworks/innerkitsimpl/scan_impl:scan_client",
"${print_path}/services/scan_service:scan_service",
]
if (sane_enable) {
include_dirs += [ "//vendor/open_source/backends/include" ]
deps += [ "//vendor/open_source/backends:third_sane" ]
cflags_cc = [ "-DSANE_ENABLE" ]
}
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"core_service:tel_core_service_api",
"distributed_notification_service:ans_innerkits",
"drivers_interface_usb:libusb_proxy_1.0",
"eventhandler:libeventhandler",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
"init:libbegetutil",
"ipc:ipc_core",
"libjpeg-turbo:turbojpeg",
"napi:ace_napi",
"netmanager_base:net_conn_manager_if",
"netmanager_ext:mdns_manager_if",
"os_account:os_account_innerkits",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"usb_manager:usbsrv_client",
]
if (security_guard_enabled) {
external_deps += [
"security_guard:libsg_collect_sdk",
"time_service:time_client",
]
}
sources = [ "scanservicestub_fuzzer.cpp" ]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ScanServiceStubFuzzTest",
]
}
###############################################################################

View File

@ -0,0 +1,16 @@
/*
* Copyright (c) 2022 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,324 @@
/*
* 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 "fuzzer/FuzzedDataProvider.h"
#include "scanservicestub_fuzzer.h"
#include "scan_service_ability.h"
namespace OHOS {
namespace Scan {
constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr int MAX_SET_NUMBER = 100;
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
bool WriteInterfaceToken(MessageParcel &datas)
{
if (!datas.WriteInterfaceToken(ScanServiceAbility::GetInstance()->GetDescriptor())) {
return false;
}
return true;
}
bool TestOnInitScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_INIT_SCAN, datas, reply, option);
return true;
}
bool TestOnExitScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_EXIT_SCAN, datas, reply, option);
return true;
}
bool TestOnGetScannerList(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_GET_SCANNER_LIST, datas, reply, option);
return true;
}
bool TestOnStopDiscover(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_STOP_DISCOVER, datas, reply, option);
return true;
}
bool TestOnOpenScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_OPEN_SCANNER, datas, reply, option);
return true;
}
bool TestOnCloseScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_CLOSE_SCANNER, datas, reply, option);
return true;
}
bool TestOnGetScanOptionDesc(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
int32_t optionIndex = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
datas.WriteInt32(optionIndex);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_GET_SCAN_OPTION_DESC, datas, reply, option);
return true;
}
bool TestOnOpScanOptionValue(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
int32_t optionIndex = dataProvider->ConsumeIntegralInRange<int32_t>(0, MAX_SET_NUMBER);
datas.WriteInt32(optionIndex);
ScanOptionOpType op = SCAN_ACTION_SET_VALUE;
datas.WriteUint32(op);
ScanOptionValue value;
value.Marshalling(datas);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_OP_SCAN_OPTION_VALUE, datas, reply, option);
return true;
}
bool TestOnGetScanParameters(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_GET_SCAN_PARAMETERS, datas, reply, option);
return true;
}
bool TestOnStartScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_START_SCAN, datas, reply, option);
return true;
}
bool TestOnGetSingleFrameFD(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
uint32_t fd = dataProvider->ConsumeIntegralInRange<uint32_t>(0, MAX_SET_NUMBER);
datas.WriteFileDescriptor(fd);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_GET_SINGLE_FRAME_FD, datas, reply, option);
return true;
}
bool TestOnCancelScan(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_CANCEL_SCAN, datas, reply, option);
return true;
}
bool TestOnSetScanIOMode(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
bool isNonBlocking = dataProvider->ConsumeBool();
datas.WriteBool(isNonBlocking);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_SET_SCAN_IO_MODE, datas, reply, option);
return true;
}
bool TestOnGetScanSelectFd(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_GET_SCAN_SELECT_FD, datas, reply, option);
return true;
}
bool TestOnGetScannerState(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_GET_SCANNER_STATE, datas, reply, option);
return true;
}
bool TestOnGetScanProgress(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string scannerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(scannerId);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_GET_SCAN_PROGRESS, datas, reply, option);
return true;
}
bool TestOnEventOn(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string taskId = "";
datas.WriteString(taskId);
std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(type);
return true;
}
bool TestOnEventOff(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
MessageParcel datas;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(datas)) {
return false;
}
std::string taskId = "";
datas.WriteString(taskId);
std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
datas.WriteString(type);
ScanServiceAbility::GetInstance()->OnRemoteRequest(CMD_OFF, datas, reply, option);
return true;
}
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (data == nullptr) {
return 0;
}
if (size < OHOS::Scan::U32_AT_SIZE || size > OHOS::Scan::FOO_MAX_LEN) {
return 0;
}
FuzzedDataProvider dataProvider(data, size);
OHOS::Scan::TestOnInitScan(data, size, &dataProvider);
OHOS::Scan::TestOnExitScan(data, size, &dataProvider);
OHOS::Scan::TestOnGetScannerList(data, size, &dataProvider);
OHOS::Scan::TestOnStopDiscover(data, size, &dataProvider);
OHOS::Scan::TestOnOpenScanner(data, size, &dataProvider);
OHOS::Scan::TestOnCloseScanner(data, size, &dataProvider);
OHOS::Scan::TestOnGetScanOptionDesc(data, size, &dataProvider);
OHOS::Scan::TestOnOpScanOptionValue(data, size, &dataProvider);
OHOS::Scan::TestOnGetScanParameters(data, size, &dataProvider);
OHOS::Scan::TestOnStartScan(data, size, &dataProvider);
OHOS::Scan::TestOnGetSingleFrameFD(data, size, &dataProvider);
OHOS::Scan::TestOnCancelScan(data, size, &dataProvider);
OHOS::Scan::TestOnSetScanIOMode(data, size, &dataProvider);
OHOS::Scan::TestOnGetScanSelectFd(data, size, &dataProvider);
OHOS::Scan::TestOnGetScannerState(data, size, &dataProvider);
OHOS::Scan::TestOnGetScanProgress(data, size, &dataProvider);
OHOS::Scan::TestOnEventOn(data, size, &dataProvider);
OHOS::Scan::TestOnEventOff(data, size, &dataProvider);
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 OHOS_SCAN_SERVICE_STUB_FUZZER_H
#define OHOS_SCAN_SERVICE_STUB_FUZZER_H
#define FUZZ_PROJECT_NAME "scanservicestub_fuzzer"
#endif // OHOS_SCAN_SERVICE_STUB_FUZZER_H

View File

@ -0,0 +1,100 @@
# 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.
import("//base/print/print_fwk/print.gni")
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
##############################fuzztest##########################################
ohos_fuzztest("ScanUsbManagerFuzzTest") {
module_out_path = fuzz_test_output_path
fuzz_config_file = "${print_path}/test/fuzztest/scanusbmanager_fuzzer"
include_dirs = [
"${print_path}/services/scan_service/include",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_utils_path}/include",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
deps = [
"${print_path}/frameworks/helper/scan_helper:scan_helper",
"${print_path}/frameworks/innerkitsimpl/scan_impl:scan_client",
"${print_path}/services/scan_service:scan_service",
]
if (sane_enable) {
include_dirs += [ "//vendor/open_source/backends/include" ]
deps += [ "//vendor/open_source/backends:third_sane" ]
cflags_cc = [ "-DSANE_ENABLE" ]
}
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"common_event_service:cesfwk_innerkits",
"core_service:tel_core_service_api",
"distributed_notification_service:ans_innerkits",
"drivers_interface_usb:libusb_proxy_1.0",
"eventhandler:libeventhandler",
"hilog:libhilog",
"hisysevent:libhisysevent",
"hitrace:hitrace_meter",
"init:libbegetutil",
"ipc:ipc_core",
"libjpeg-turbo:turbojpeg",
"napi:ace_napi",
"netmanager_base:net_conn_manager_if",
"netmanager_ext:mdns_manager_if",
"os_account:os_account_innerkits",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"usb_manager:usbsrv_client",
]
if (security_guard_enabled) {
external_deps += [
"security_guard:libsg_collect_sdk",
"time_service:time_client",
]
}
sources = [ "scanusbmanager_fuzzer.cpp" ]
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ScanUsbManagerFuzzTest",
]
}
###############################################################################

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,121 @@
/*
* 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.
*/
#define private public
#define protected public
#include "fuzzer/FuzzedDataProvider.h"
#include "scanusbmanager_fuzzer.h"
#include "scan_usb_manager.h"
#include "scan_service_ability.h"
namespace OHOS {
namespace Scan {
constexpr uint8_t MAX_STRING_LENGTH = 255;
constexpr size_t FOO_MAX_LEN = 1024;
constexpr size_t U32_AT_SIZE = 4;
void TestRefreshUsbDevice(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
DelayedSingleton<ScanUsbManager>::GetInstance()->RefreshUsbDevice();
}
void TestInit(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
DelayedSingleton<ScanUsbManager>::GetInstance()->Init();
}
void TestDealUsbDevStatusChange(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string devStr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
bool isAttach = dataProvider->ConsumeBool();
std::string devStrOne = "2-3";
DelayedSingleton<ScanUsbManager>::GetInstance()->DealUsbDevStatusChange(devStr, isAttach);
DelayedSingleton<ScanUsbManager>::GetInstance()->DealUsbDevStatusChange(devStrOne, true);
ScanServiceAbility::usbSnMap[devStrOne] = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
DelayedSingleton<ScanUsbManager>::GetInstance()->DealUsbDevStatusChange(devStrOne, false);
}
void TestGetDeviceSerialNumber(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
USB::USBDevicePipe usbDevicePipe;
DelayedSingleton<ScanUsbManager>::GetInstance()->GetDeviceSerialNumber(usbDevicePipe);
}
void TestUsbUpdateUsbScannerId(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string usbDeviceName = "2-4";
std::string serialNumber = "QWERTY";
ScanServiceAbility::usbSnMap[usbDeviceName] = serialNumber;
ScanDeviceInfo scanDeviceInfo;
scanDeviceInfo.deviceId = "pantu:libusb:002:004";
scanDeviceInfo.serialNumber = serialNumber;
ScanServiceAbility::saneGetUsbDeviceInfoMap[serialNumber] = scanDeviceInfo;
ScanUsbManager::GetInstance()->UpdateUsbScannerId(serialNumber, usbDeviceName);
std::string fuzzUsbDeviceName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string fuzzSerialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::usbSnMap[fuzzUsbDeviceName] = fuzzSerialNumber;
ScanDeviceInfo fuzzScanDeviceInfo;
scanDeviceInfo.deviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDeviceInfo.serialNumber = fuzzSerialNumber;
ScanServiceAbility::saneGetUsbDeviceInfoMap[fuzzSerialNumber] = fuzzScanDeviceInfo;
ScanUsbManager::GetInstance()->UpdateUsbScannerId(fuzzSerialNumber, fuzzUsbDeviceName);
}
void TestUsbDisConnectUsbScanner(const uint8_t* data, size_t size, FuzzedDataProvider* dataProvider)
{
std::string usbDeviceName = "2-3";
std::string serialNumber = "ASDFGH";
ScanServiceAbility::usbSnMap[usbDeviceName] = serialNumber;
ScanDeviceInfo scanDeviceInfo;
scanDeviceInfo.deviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDeviceInfo.serialNumber = serialNumber;
ScanServiceAbility::saneGetUsbDeviceInfoMap[serialNumber] = scanDeviceInfo;
ScanUsbManager::GetInstance()->DisConnectUsbScanner(usbDeviceName);
std::string fuzzUsbDeviceName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
std::string fuzzSerialNumber = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
ScanServiceAbility::usbSnMap[fuzzUsbDeviceName] = fuzzSerialNumber;
ScanDeviceInfo fuzzScanDeviceInfo;
scanDeviceInfo.deviceId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
scanDeviceInfo.serialNumber = fuzzSerialNumber;
ScanServiceAbility::saneGetUsbDeviceInfoMap[fuzzSerialNumber] = fuzzScanDeviceInfo;
ScanUsbManager::GetInstance()->DisConnectUsbScanner(fuzzUsbDeviceName);
}
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (data == nullptr) {
return 0;
}
if (size < OHOS::Scan::U32_AT_SIZE || size > OHOS::Scan::FOO_MAX_LEN) {
return 0;
}
FuzzedDataProvider dataProvider(data, size);
OHOS::Scan::TestRefreshUsbDevice(data, size, &dataProvider);
OHOS::Scan::TestInit(data, size, &dataProvider);
OHOS::Scan::TestDealUsbDevStatusChange(data, size, &dataProvider);
OHOS::Scan::TestGetDeviceSerialNumber(data, size, &dataProvider);
OHOS::Scan::TestUsbUpdateUsbScannerId(data, size, &dataProvider);
OHOS::Scan::TestUsbDisConnectUsbScanner(data, size, &dataProvider);
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 OHOS_SCAN_USB_MANAGER_FUZZER_H
#define OHOS_SCAN_USB_MANAGER_FUZZER_H
#define FUZZ_PROJECT_NAME "scanusbmanager_fuzzer"
#endif // OHOS_SCAN_USB_MANAGER_FUZZER_H

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class NapiInnerScanTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void NapiInnerScanTest::SetUpTestCase(void) {}
void NapiInnerScanTest::TearDownTestCase(void) {}
void NapiInnerScanTest::SetUp(void) {}
void NapiInnerScanTest::TearDown(void) {}
/**
* @tc.name: NapiInnerScanTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NapiInnerScanTest, NapiInnerScanTest_0001, TestSize.Level1) { }
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,62 @@
/*
* 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 <gtest/gtest.h>
#include <functional>
#include <memory>
#include <map>
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "scan_constant.h"
namespace OHOS {
namespace Scan {
class ScanAsyncCallTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ScanAsyncCallTest::SetUpTestCase(void) {}
void ScanAsyncCallTest::TearDownTestCase(void) {}
void ScanAsyncCallTest::SetUp(void) {}
void ScanAsyncCallTest::TearDown(void) {}
/**
* @tc.name: ScanAsyncCallTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanAsyncCallTest, ScanAsyncCallTest_0001, TestSize.Level1)
{
napi_env env;
napi_callback_info info;
auto context = std::make_shared<NapiScanContext>();
ScanAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<ScanAsyncCall::Context>(context));
auto exec = [context](ScanAsyncCall::Context *ctx) {
EXPECT_NE(context, nullptr);
};
asyncCall.Call(env, exec);
}
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class ScanCallbackProxyTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ScanCallbackProxyTest::SetUpTestCase(void) {}
void ScanCallbackProxyTest::TearDownTestCase(void) {}
void ScanCallbackProxyTest::SetUp(void) {}
void ScanCallbackProxyTest::TearDown(void) {}
/**
* @tc.name: ScanCallbackProxyTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanCallbackProxyTest, ScanCallbackProxyTest_0001, TestSize.Level1) { }
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class ScanCallbackStubTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ScanCallbackStubTest::SetUpTestCase(void) {}
void ScanCallbackStubTest::TearDownTestCase(void) {}
void ScanCallbackStubTest::SetUp(void) {}
void ScanCallbackStubTest::TearDown(void) {}
/**
* @tc.name: ScanCallbackStubTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanCallbackStubTest, ScanCallbackStubTest_0001, TestSize.Level1) { }
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class ScanCallbackTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ScanCallbackTest::SetUpTestCase(void) {}
void ScanCallbackTest::TearDownTestCase(void) {}
void ScanCallbackTest::SetUp(void) {}
void ScanCallbackTest::TearDown(void) {}
/**
* @tc.name: ScanCallbackTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanCallbackTest, ScanCallbackTest_0001, TestSize.Level1) { }
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,70 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class ScanManagerClientTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
std::shared_ptr<ScanManagerClient> smgPtr;
};
void ScanManagerClientTest::SetUpTestCase(void) {}
void ScanManagerClientTest::TearDownTestCase(void) {}
void ScanManagerClientTest::SetUp(void)
{
smgPtr = std::make_shared<ScanManagerClient>();
EXPECT_NE(smgPtr, nullptr);
}
void ScanManagerClientTest::TearDown(void) {}
/**
* @tc.name: ScanManagerClientTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanManagerClientTest, ScanManagerClientTest_0001, TestSize.Level1)
{
int32_t scanVersion = 0;
auto status = smgPtr->InitScan(scanVersion);
EXPECT_NE(scanVersion, 0);
EXPECT_EQ(status, 0);
}
/**
* @tc.name: ScanManagerClientTest_0002
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanManagerClientTest, ScanManagerClientTest_0002, TestSize.Level1)
{
int32_t scanVersion = 0;
auto status = smgPtr->ExitScan(scanVersion);
EXPECT_NE(scanVersion, 0);
EXPECT_EQ(status, 0);
}
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class ScanModuleTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ScanModuleTest::SetUpTestCase(void) {}
void ScanModuleTest::TearDownTestCase(void) {}
void ScanModuleTest::SetUp(void) {}
void ScanModuleTest::TearDown(void) {}
/**
* @tc.name: ScanModuleTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanModuleTest, ScanModuleTest_0001, TestSize.Level1) { }
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class ScanServiceProxyTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ScanServiceProxyTest::SetUpTestCase(void) {}
void ScanServiceProxyTest::TearDownTestCase(void) {}
void ScanServiceProxyTest::SetUp(void) {}
void ScanServiceProxyTest::TearDown(void) {}
/**
* @tc.name: ScanServiceProxyTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanServiceProxyTest, ScanServiceProxyTest_0001, TestSize.Level1) { }
} // namespace Scan
} // namespace OHOS

View File

@ -23,7 +23,7 @@ ohos_unittest("fwk_print_cups_client_test") {
"${print_path}/frameworks/models/print_models/include", "${print_path}/frameworks/models/print_models/include",
"${print_path}/interfaces/kits/napi/print_napi/include", "${print_path}/interfaces/kits/napi/print_napi/include",
"${print_path}/services/print_service/include", "${print_path}/services/print_service/include",
"//third_party/cpp-httplib", "//vendor/open_source/cpp-httplib",
"${print_utils_path}/include", "${print_utils_path}/include",
"//third_party/node/src", "//third_party/node/src",
] ]

View File

@ -192,7 +192,7 @@ HWTEST_F(PrintCupsClientTest, PrintCupsClientTest_0009, TestSize.Level1)
{ {
OHOS::Print::PrintCupsClient printCupsClient; OHOS::Print::PrintCupsClient printCupsClient;
std::string printerName = "DIRECT-PixLab_V1-0105"; std::string printerName = "DIRECT-PixLab_V1-0105";
std::string printerId = "com.ohos.spooler:p2p://DIRECT-HW_PixLab_V1-0105"; std::string printerId = "com.huawei.hmos.spooler:p2p://DIRECT-HUAWEI_PixLab_V1-0105";
PrinterInfo info; PrinterInfo info;
info.SetPrinterName(printerName); info.SetPrinterName(printerName);
printCupsClient.QueryPrinterInfoByPrinterId(printerId, info); printCupsClient.QueryPrinterInfoByPrinterId(printerId, info);
@ -1480,10 +1480,10 @@ HWTEST_F(PrintCupsClientTest, PrintCupsClientTest_0074, TestSize.Level1)
HWTEST_F(PrintCupsClientTest, PrintCupsClientTest_0075, TestSize.Level1) HWTEST_F(PrintCupsClientTest, PrintCupsClientTest_0075, TestSize.Level1)
{ {
OHOS::Print::PrintCupsClient printCupsClient; OHOS::Print::PrintCupsClient printCupsClient;
std::string printerId = "com.ohos.spooler:usb://DIRECT-HW_PixLab_V1-1620"; std::string printerId = "com.huawei.hmos.spooler:usb://DIRECT-HUAWEI_PixLab_V1-1620";
std::string nic = ""; std::string nic = "";
printCupsClient.IsIpConflict(printerId, nic); printCupsClient.IsIpConflict(printerId, nic);
printerId = "com.ohos.spooler:p2p://DIRECT-HW_PixLab_V1-1620"; printerId = "com.huawei.hmos.spooler:p2p://DIRECT-HUAWEI_PixLab_V1-1620";
printCupsClient.IsIpConflict(printerId, nic); printCupsClient.IsIpConflict(printerId, nic);
} }

View File

@ -0,0 +1,94 @@
# Copyright (c) 2022 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.
import("//base/print/print_fwk/print.gni")
import("//build/test.gni")
module_output_path = "print_fwk/fwk_print_http_request_process_test"
ohos_unittest("fwk_print_http_request_process_test") {
module_out_path = module_output_path
include_dirs = [
"${print_path}/frameworks/innerkitsimpl/print_impl/include",
"${print_path}/frameworks/models/print_models/include",
"${print_path}/interfaces/kits/napi/print_napi/include",
"${print_path}/services/print_service/include",
"//vendor/open_source/cpp-httplib",
"${print_utils_path}/include",
"//third_party/node/src",
]
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
blocklist = "${print_path}/cfi_blocklist.txt"
}
sources = [ "print_http_request_process_test.cpp" ]
deps = [
"${print_path}/etc/init:printservice.rc",
"${print_path}/frameworks/innerkitsimpl/print_impl:print_client",
"${print_path}/frameworks/kits/extension:print_extension_framework",
"${print_path}/frameworks/kits/extension:print_extension_module",
"${print_path}/frameworks/models/print_models:print_models",
"${print_path}/interfaces/kits/jsnapi/print_extension:printextensionability_napi",
"${print_path}/interfaces/kits/jsnapi/print_extensionctx:printextensioncontext_napi",
"${print_path}/interfaces/kits/napi/print_napi:print_napi",
"${print_path}/profile:print_sa_profiles",
"${print_path}/services/print_service:print_service",
"//third_party/cJSON:cjson",
"//third_party/googletest:gmock_main",
"//third_party/zlib:libz",
]
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"access_token:libnativetoken",
"access_token:libtoken_setproc",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"drivers_interface_usb:libusb_proxy_1.0",
"hilog:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"usb_manager:usbsrv_client",
]
if (cups_enable) {
cflags_cc = [ "-DCUPS_ENABLE" ]
external_deps += [
"cups:cups",
"cups-filters:cupsfilters",
]
}
cflags_cc += [ "-DCPPHTTPLIB_NO_EXCEPTIONS" ]
}
group("unittest") {
testonly = true
deps = []
deps += [ ":fwk_print_http_request_process_test" ]
}

View File

@ -106,10 +106,67 @@ ohos_unittest("print_service_test") {
] ]
} }
ohos_unittest("scan_service_test") {
module_out_path = module_output_path
include_dirs = [
"mock",
"../fwk_inner_napi_test/mock",
"${print_path}/frameworks/innerkitsimpl/scan_impl/include",
"${print_path}/interfaces/kits/napi/scan_napi/include",
"${print_path}/services/scan_service/include",
"${print_utils_path}/include",
"//third_party/node/src",
]
sources = [
"scan_callback_proxy_test.cpp",
"scan_mdns_service_test.cpp",
"scan_service_ability_test.cpp",
"scan_service_stub_test.cpp",
]
deps = [
"${print_path}/etc/init:scanservice.rc",
"${print_path}/frameworks/innerkitsimpl/scan_impl:scan_client",
"${print_path}/interfaces/kits/napi/scan_napi:scan_napi",
"${print_path}/profile:scan_sa_profiles",
"${print_path}/services/scan_service:scan_service",
"//third_party/googletest:gmock_main",
"//third_party/jsoncpp:jsoncpp",
"//third_party/zlib:libz",
]
if (sane_enable) {
include_dirs += [ "//vendor/open_source/backends/include" ]
deps += [ "//vendor/open_source/backends:third_sane" ]
cflags_cc = [ "-DSANE_ENABLE" ]
}
external_deps = [
"ability_base:base",
"ability_base:want",
"ability_base:zuri",
"ability_runtime:ability_manager",
"ability_runtime:abilitykit_native",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"drivers_interface_usb:libusb_proxy_1.0",
"hilog:libhilog",
"ipc:ipc_core",
"napi:ace_napi",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",
"usb_manager:usbsrv_client",
]
}
group("unittest") { group("unittest") {
testonly = true testonly = true
deps = [] deps = []
deps += [ ":print_service_test" ] deps += [ ":print_service_test" ]
deps += [ ":scan_service_test" ]
} }

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2022 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 MOCK_SCAN_CALLBACK_PROXY_H
#define MOCK_SCAN_CALLBACK_PROXY_H
#include <gmock/gmock.h>
#include "scan_callback_proxy.h"
namespace OHOS {
namespace Scan {
class MockScanCallbackProxy final : public ScanCallbackProxy {
public:
MockScanCallbackProxy() : ScanCallbackProxy(nullptr) {}
MOCK_METHOD2(OnCallback, bool(uint32_t, const ScanDeviceInfoTCP &));
MOCK_METHOD2(OnCallback, bool(uint32_t, const ScanDeviceInfo &));
MOCK_METHOD2(OnGetFrameResCallback, bool(bool, int32_t));
MOCK_METHOD1(OnScanInitCallback, bool(int32_t &));
MOCK_METHOD1(OnSendSearchMessage, bool(std::string &));
};
} // namespace Print
} // namespace OHOS
#endif // MOCK_PRINT_CALLBACK_PROXY_H

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 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 MOCK_PRINT_SERVICE_HELPER_H
#define MOCK_PRINT_SERVICE_HELPER_H
#include <gmock/gmock.h>
#include "scan_service_helper.h"
namespace OHOS {
namespace Scan {
class MockScanServiceHelper final : public ScanServiceHelper {
public:
MOCK_METHOD1(CheckPermission, bool(const std::string&));
MOCK_METHOD1(StartAbility, bool(const AAFwk::Want&));
MOCK_METHOD0(GetBundleMgr, sptr<IRemoteObject>());
MOCK_METHOD1(QueryAccounts, bool(std::vector<int>&));
MOCK_METHOD3(QueryExtension, bool(sptr<AppExecFwk::IBundleMgr>, int,
std::vector<AppExecFwk::ExtensionAbilityInfo>&));
MOCK_METHOD3(QueryNameForUid, bool(sptr<AppExecFwk::IBundleMgr>, int32_t, std::string&));
MOCK_METHOD0(IsSyncMode, bool());
};
} // namespace Print
} // namespace OHOS
#endif // MOCK_PRINT_SERVICE_HELPER_H

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
namespace OHOS {
namespace Scan {
class ScanCallbackProxyTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ScanCallbackProxyTest::SetUpTestCase(void) {}
void ScanCallbackProxyTest::TearDownTestCase(void) {}
void ScanCallbackProxyTest::SetUp(void) {}
void ScanCallbackProxyTest::TearDown(void) {}
/**
* @tc.name: ScanMdnsServiceTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanCallbackProxyTest, ScanMdnsServiceTest_0001, TestSize.Level1) { }
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
#include "mock_bundle_mgr.h"
#include "scan_service_ability.h"
#include "scan_constant.h"
#include "system_ability_definition.h"
#include "scan_utils.h"
#include "mock_scan_callback_proxy.h"
#include "scan_mdns_service.h"
namespace OHOS {
namespace Scan {
class ScanMdnsServiceTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
std::shared_ptr<ScanMdnsService> scanMdnsPtr;
std::shared_ptr<ScanServiceAbility> scanServicePtr;
};
void ScanMdnsServiceTest::SetUpTestCase(void) {}
void ScanMdnsServiceTest::TearDownTestCase(void) {}
void ScanMdnsServiceTest::SetUp(void)
{
scanMdnsPtr = ScanMdnsService::GetInstance();
scanServicePtr = ScanServiceAbility::GetInstance();
EXPECT_NE(scanMdnsPtr, nullptr);
EXPECT_NE(scanServicePtr, nullptr);
}
void ScanMdnsServiceTest::TearDown(void) {}
/**
* @tc.name: ScanMdnsServiceTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanMdnsServiceTest, ScanMdnsServiceTest_0001, TestSize.Level1)
{
scanMdnsPtr->SetServiceType("_scanner._tcp");
scanMdnsPtr->onStartDiscoverService();
EXPECT_NE(scanServicePtr->scanDeviceInfoTCPMap_.size(), 0);
}
/**
* @tc.name: ScanMdnsServiceTest_0002
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanMdnsServiceTest, ScanMdnsServiceTest_0002, TestSize.Level1)
{
scanMdnsPtr->SetServiceType("_scanner._tcp");
scanMdnsPtr->onStartDiscoverService();
EXPECT_NE(scanServicePtr->_scanMDnsDiscoveryCallBack, nullptr);
}
/**
* @tc.name: ScanMdnsServiceTest_0003
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanMdnsServiceTest, ScanMdnsServiceTest_0003, TestSize.Level1)
{
scanMdnsPtr->SetServiceType("_scanner._tcp");
scanMdnsPtr->onStartDiscoverService();
EXPECT_NE(scanServicePtr->_scanMDnsResolveCallBack, nullptr);
}
} // namespace Scan
} // namespace OHOS

View File

@ -0,0 +1,456 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
#include "mock_bundle_mgr.h"
#include "scan_service_ability.h"
#include "scan_constant.h"
#include "system_ability_definition.h"
#include "scan_utils.h"
#include "mock_scan_callback_proxy.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS {
namespace Scan {
class ScanServiceAbilityTest : public testing::Test {
public:
ScanServiceAbilityTest() : deathRecipient(nullptr), obj(nullptr), scanSa(nullptr), testNo(0), scannerId(nullptr) {}
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
private:
constexpr int32_t DEFAULT_NUMBER = 0;
sptr<IRemoteObject::DeathRecipient> deathRecipient;
sptr<MockBundleMgr> obj;
sptr<ScanServiceAbility> scanSa;
int32_t testNo;
std::string scannerId;
std::set<ScanErrorCode> allStatus;
};
void ScanServiceAbilityTest::SetUpTestCase(void)
{
SCAN_HILOGD("ScanServiceAbilityTest SetUpTestCase");
testNo = 0;
}
void ScanServiceAbilityTest::TearDownTestCase(void)
{
SCAN_HILOGD("ScanServiceAbilityTest TearDownTestCase");
}
void ScanServiceAbilityTest::SetUp(void)
{
scanSa = ScanServiceAbility::GetInstance();
EXPECT_TRUE(scanSa != nullptr);
EXPECT_TRUE(scannerId != nullptr && scannerId != "")
allStatus.insert({E_SCAN_NONE, E_SCAN_NO_PERMISSION, E_SCAN_INVALID_PARAMETER,
E_SCAN_GENERIC_FAILURE, E_SCAN_SERVER_FAILURE, E_SCAN_GOOD, E_SCAN_UNSUPPORTED,
E_SCAN_CANCELLED, E_SCAN_DEVICE_BUSY, E_SCAN_INVAL, E_SCAN_EOF, E_SCAN_JAMMED,
E_SCAN_NO_DOCS, E_SCAN_COVER_OPEN, E_SCAN_IO_ERROR, E_SCAN_NO_MEM,
E_SCAN_ACCESS_DENIED})
SCAN_HILOGD("ScanServiceAbilityTest : %{public}d", ++testNo);
}
void ScanServiceAbilityTest::TearDown(void) {}
void ScanServiceAbilityTest::SetScannerId(const std::string& scannerId)
{
EXPECT_TRUE(scannerId != nullptr && scannerId != "")
this->scannerId = scannerId;
}
/**
* @tc.name: ScanServiceAbilityTest_0001
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_001, TestSize.Level1) {
SetUp();
int32_t scanVersion = DEFAULT_NUMBER;
int32_t status = E_SCAN_NONE;
status = InitScan(scanVersion);
EXPECT_EQ(status, E_SCAN_NONE)
EXPECT_NE(scanVersion, DEFAULT_NUMBER);
}
/**
* @tc.name: ScanServiceAbilityTest_0002
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_002, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = ExitScan();
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0003
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_004, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetScannerList();
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0004
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_004, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = StopDiscover();
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0005
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_005, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = OpenScanner(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0006
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_006, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = CloseScanner(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0007
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_007, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetScanOptionDesc(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
--
/**
* @tc.name: ScanServiceAbilityTest_0008
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_008, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetScanOptionDesc(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0009
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_009, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = OpScanOptionValue(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0010
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_010, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetScanParameters(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0011
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_011, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = StartScan(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0012
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_012, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetSingleFrameFD(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0013
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_013, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = CancelScan(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0014
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_014, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = SetScanIOMode(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0015
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_015, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetScanSelectFd(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0016
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_016, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = On(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0017
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_017, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = Off(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0018
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_018, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetScannerState(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0019
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_019, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = GetScanProgress(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0020
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_020, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = OnStartScan(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0021
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_021, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
StartScanTask(scannerId);
status = GetScanProgress(scannerId);
EXPECT_EQ(status, E_SCAN_NONE)
}
/**
* @tc.name: ScanServiceAbilityTest_0022
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_022, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
int32_t scanVersion = 0;
status= scanSa->InitScan(scanVersion);
EXPECT_EQ(scanSa->appCount_, 0);
}
/**
* @tc.name: ScanServiceAbilityTest_0023
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_023, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status= scanSa->ExitScan();
EXPECT_EQ(scanSa->appCount_, 0);
}
/**
* @tc.name: ScanServiceAbilityTest_0024
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_023, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
scanSa->UnloadSystemAbility();
EXPECT_EQ(scanSa, nullptr);
}
/**
* @tc.name: ScanServiceAbilityTest_0025
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_025, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = scanSa->OnStartScan(scannerId);
EXPECT_TRUE(ScanErrorCode.count(status))
}
/**
* @tc.name: ScanServiceAbilityTest_0026
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_026, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
scanSa->StartScanTask(scannerId);
status = scanSa->GetScanProgress(scannerId);
EXPECT_TRUE(ScanErrorCode.count(status))
}
/**
* @tc.name: ScanServiceAbilityTest_0027
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_027, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
scanSa->StartScanTask(scannerId);
status = scanSa->GetScanProgress(scannerId);
EXPECT_TRUE(ScanErrorCode.count(status))
}
/**
* @tc.name: ScanServiceAbilityTest_0028
* @tc.desc: Verify the sub function.
* @tc.type: FUNC
* @tc.require: Issue Number
*/
HWTEST_F(ScanServiceAbilityTest, integer_sub_028, TestSize.Level1) {
SetUp();
int32_t status = E_SCAN_NONE;
status = scanSa->OnStartScan(scannerId);
EXPECT_TRUE(ScanErrorCode.count(status))
}
}
}

View File

@ -0,0 +1,170 @@
/*
* Copyright (c) 2023 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 <gtest/gtest.h>
#include "mock_bundle_mgr.h"
#include "scan_service_ability.h"
#include "scan_constant.h"
#include "system_ability_definition.h"
#include "scan_utils.h"
#include "mock_scan_callback_proxy.h"
namespace OHOS {
namespace Scan {
class ScanServiceStubTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
std::shared_ptr<ScanServiceStub> stubSpr;
using ScanCmdHandler = bool (ScanServiceStub::*)(MessageParcel &, MessageParcel &);
std::map<uint32_t, ScanCmdHandler> cmdMap_;
sptr<ScanServiceAbility> scanSa;
};
void ScanServiceStubTest::SetUpTestCase(void) {}
void ScanServiceStubTest::TearDownTestCase(void) {}
void ScanServiceStubTest::SetUp(void)
{
stubSpr = std::make_shared<ScanServiceStub>();
EXPECT_NE(stubSpr, nullptr);
cmdMap_[CMD_INIT_SCAN] = &ScanServiceStub::OnInitScan;
cmdMap_[CMD_EXIT_SCAN] = &ScanServiceStub::OnExitScan;
cmdMap_[CMD_GET_SCANNER_LIST] = &ScanServiceStub::OnGetScannerList;
cmdMap_[CMD_STOP_DISCOVER] = &ScanServiceStub::OnStopDiscover;
cmdMap_[CMD_OPEN_SCANNER] = &ScanServiceStub::OnOpenScanner;
cmdMap_[CMD_CLOSE_SCANNER] = &ScanServiceStub::OnCloseScanner;
cmdMap_[CMD_GET_SCAN_OPTION_DESC] = &ScanServiceStub::OnGetScanOptionDesc;
cmdMap_[CMD_OP_SCAN_OPTION_VALUE] = &ScanServiceStub::OnOpScanOptionValue;
cmdMap_[CMD_GET_SCAN_PARAMETERS] = &ScanServiceStub::OnGetScanParameters;
cmdMap_[CMD_START_SCAN] = &ScanServiceStub::OnStartScan;
cmdMap_[CMD_GET_SINGLE_FRAME_FD] = &ScanServiceStub::OnGetSingleFrameFD;
cmdMap_[CMD_CANCEL_SCAN] = &ScanServiceStub::OnCancelScan;
cmdMap_[CMD_SET_SCAN_IO_MODE] = &ScanServiceStub::OnSetScanIOMode;
cmdMap_[CMD_GET_SCAN_SELECT_FD] = &ScanServiceStub::OnGetScanSelectFd;
cmdMap_[CMD_GET_SCANNER_STATE] = &ScanServiceStub::OnGetScannerState;
cmdMap_[CMD_GET_SCAN_PROGRESS] = &ScanServiceStub::OnGetScanProgress;
cmdMap_[CMD_ON] = &ScanServiceStub::OnEventOn;
cmdMap_[CMD_OFF] = &ScanServiceStub::OnEventOff;
scanSa = ScanServiceAbility::GetInstance();
EXPECT_TRUE(scanSa != nullptr);
EXPECT_TRUE(scannerId != nullptr && scannerId != "")
}
void ScanServiceStubTest::TearDown(void) {}
/**
* @tc.name: ScanMdnsServiceTest_0001
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanServiceStubTest, ScanMdnsServiceTest_0001, TestSize.Level1) {
auto itFunc = cmdMap_.find(CMD_INIT_SCAN);
if (itFunc != cmdMap_.end()) {
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
return (this->*requestFunc)(data, reply);
}
}
EXPECT_EQ(scanSa->appCount_, 1);
}
/**
* @tc.name: ScanMdnsServiceTest_0002
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanServiceStubTest, ScanMdnsServiceTest_0002, TestSize.Level1) {
auto itFunc = cmdMap_.find(CMD_EXIT_SCAN);
if (itFunc != cmdMap_.end()) {
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
return (this->*requestFunc)(data, reply);
}
}
EXPECT_EQ(scanSa->appCount_, 0);
}
/**
* @tc.name: ScanMdnsServiceTest_0003
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanServiceStubTest, ScanMdnsServiceTest_0003, TestSize.Level1) {
auto itFunc = cmdMap_.find(CMD_INIT_SCAN);
if (itFunc != cmdMap_.end()) {
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
return (this->*requestFunc)(data, reply);
}
}
itFunc = cmdMap_.find(CMD_EXIT_SCAN);
if (itFunc != cmdMap_.end()) {
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
return (this->*requestFunc)(data, reply);
}
}
EXPECT_GE(scanSa->appCount_, 0);
}
/**
* @tc.name: ScanMdnsServiceTest_0004
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanServiceStubTest, ScanMdnsServiceTest_0004, TestSize.Level1) {
for (int i = 0; i < 5; i++) {
auto itFunc = cmdMap_.find(CMD_INIT_SCAN);
if (itFunc != cmdMap_.end()) {
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
return (this->*requestFunc)(data, reply);
}
}
}
EXPECT_EQ(scanSa->appCount_, 5);
}
/**
* @tc.name: ScanMdnsServiceTest_0004
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(ScanServiceStubTest, ScanMdnsServiceTest_0004, TestSize.Level1) {
for (int i = 0; i < 5; i++) {
auto itFunc = cmdMap_.find(CMD_EXIT_SCAN);
if (itFunc != cmdMap_.end()) {
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
return (this->*requestFunc)(data, reply);
}
}
}
EXPECT_EQ(scanSa->appCount_, 0);
}
} // namespace Scan
} // namespace OHOS