From 5160895252193595eeabb2ee4eb88b56b90044f1 Mon Sep 17 00:00:00 2001 From: zheng_zhi_hao Date: Mon, 9 Sep 2024 10:59:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=A1=86=E6=9E=B6DT=20FUZZ?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zheng_zhi_hao --- cfi_blocklist.txt | 1 + .../printserviceability_fuzzer.cpp | 222 +++++++----------- 2 files changed, 83 insertions(+), 140 deletions(-) diff --git a/cfi_blocklist.txt b/cfi_blocklist.txt index 9a0170e..95d8636 100644 --- a/cfi_blocklist.txt +++ b/cfi_blocklist.txt @@ -1,2 +1,3 @@ src:*/base/print/print_fwk/test/unittest/* +src:*/base/print/print_fwk/test/fuzztest/* src:*/third_party/googletest/* diff --git a/test/fuzztest/printserviceability_fuzzer/printserviceability_fuzzer.cpp b/test/fuzztest/printserviceability_fuzzer/printserviceability_fuzzer.cpp index db7a253..bfab1e9 100644 --- a/test/fuzztest/printserviceability_fuzzer/printserviceability_fuzzer.cpp +++ b/test/fuzztest/printserviceability_fuzzer/printserviceability_fuzzer.cpp @@ -21,28 +21,35 @@ #include "print_log.h" #include "print_service_ability.h" #include "print_service_ability_mock_permission.h" +#include "print_callback.h" +#include "iprint_adapter_inner.h" namespace OHOS { namespace Print { -constexpr uint8_t MAX_STRING_LENGTH = 255; -constexpr int MAX_SET_NUMBER = 100; +constexpr uint8_t MAX_STRING_LENGTH = 20; +constexpr int MAX_SET_NUMBER = 128; constexpr size_t U32_AT_SIZE = 4; static constexpr const char *JOB_OPTIONS = "{\"jobName\":\"xx\",\"jobNum\":1,\"mediaType\":\"stationery\",\"documentCategory\":0,\"printQuality\":\"4\"," - "\"printerName\":\"printer1\",\"printerUri\":\"ipp://192.168.0.1:111/ipp/print\"," + "\"printerName\":\"testId\",\"printerUri\":\"ipp://192.168.0.1:111/ipp/print\"," "\"documentFormat\":\"application/pdf\",\"files\":[\"/data/1.pdf\"]}"; +static const std::string DEFAULT_PRINTERID = "testId"; + +void TestInitialize(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) +{ + PrintServiceAbilityMockPermission::MockPermission(); + PrintServiceAbility::GetInstance()->OnStart(); + auto cupsPrinter = std::make_shared(); + PrintServiceAbility::GetInstance()->printSystemData_.addedPrinterMap_.Insert(DEFAULT_PRINTERID, cupsPrinter); +} void TestStartService(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->StartService(); } void TestStartPrint(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string fileUri = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::vector fileList; fileList.push_back(fileUri); @@ -55,97 +62,101 @@ void TestStartPrint(const uint8_t *data, size_t size, FuzzedDataProvider *dataPr void TestStopPrint(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->StopPrint(taskId); } void TestConnectPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); - std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); + std::string printerId = size ? dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH) : DEFAULT_PRINTERID; + PrinterInfo printerInfo; + printerInfo.SetPrinterId(printerId); + printerInfo.SetPrinterName(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); + printerInfo.SetOption(JOB_OPTIONS); + std::vector printerInfos; + printerInfos.push_back(printerInfo); + PrintServiceAbility::GetInstance()->AddPrinters(printerInfos); PrintServiceAbility::GetInstance()->ConnectPrinter(printerId); + PrintServiceAbility::GetInstance()->DisconnectPrinter(printerId); } void TestDisconnectPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->DisconnectPrinter(printerId); } void TestStartDiscoverPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); + std::vector printExtensionInfos; + PrintServiceAbility::GetInstance()->QueryAllExtension(printExtensionInfos); std::string extensionId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - std::vector extensionIds; + std::vector extensionIds; extensionIds.push_back(extensionId); + for (auto &printExtensionInfo: printExtensionInfos) { + extensionIds.push_back(printExtensionInfo.GetExtensionId()); + } PrintServiceAbility::GetInstance()->StartDiscoverPrinter(extensionIds); } void TestStopDiscoverPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->StopDiscoverPrinter(); } void TestQueryAllExtension(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrintExtensionInfo printExtensionInfo; printExtensionInfo.SetExtensionId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::vector printExtensionInfos; printExtensionInfos.push_back(printExtensionInfo); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->QueryAllExtension(printExtensionInfos); } void TestStartPrintJob(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); + PrinterInfo printerInfo; + printerInfo.SetPrinterId(DEFAULT_PRINTERID); + printerInfo.SetPrinterName(DEFAULT_PRINTERID); + std::vector printerInfos; + PrinterCapability printerCaps; + printerCaps.SetColorMode(dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER)); + printerInfos.push_back(printerInfo); + + PrintServiceAbility::GetInstance()->AddPrinters(printerInfos); + PrintServiceAbility::GetInstance()->UpdatePrinters(printerInfos); + PrintJob testJob; testJob.SetJobId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); - std::vector files = {1}; + std::vector files = {1}; testJob.SetFdList(files); OHOS::Print::PrintPageSize pageSize; pageSize.SetId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); testJob.SetPageSize(pageSize); testJob.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); testJob.SetOption(JOB_OPTIONS); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->StartPrintJob(testJob); } void TestCancelPrintJob(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->CancelPrintJob(jobId); } void TestAddPrinters(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrinterInfo printerInfo; printerInfo.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printerInfo.SetPrinterName(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printerInfo.SetOption(JOB_OPTIONS); std::vector printerInfos; printerInfos.push_back(printerInfo); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->AddPrinters(printerInfos); } void TestRemovePrinters(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::vector printerIds; printerIds.push_back(printerId); @@ -154,22 +165,18 @@ void TestRemovePrinters(const uint8_t *data, size_t size, FuzzedDataProvider *da void TestUpdatePrinters(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrinterInfo printerInfo; printerInfo.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printerInfo.SetPrinterName(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printerInfo.SetOption(JOB_OPTIONS); std::vector printerInfos; printerInfos.push_back(printerInfo); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->UpdatePrinters(printerInfos); return; } void TestUpdatePrinterState(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t state = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); PrintServiceAbility::GetInstance()->UpdatePrinterState(printerId, state); @@ -177,8 +184,6 @@ void TestUpdatePrinterState(const uint8_t *data, size_t size, FuzzedDataProvider void TestUpdatePrintJobStateOnlyForSystemApp(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t state = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); uint32_t subState = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); @@ -187,15 +192,12 @@ void TestUpdatePrintJobStateOnlyForSystemApp(const uint8_t *data, size_t size, F void TestUpdateExtensionInfo(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string extInfo = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->UpdateExtensionInfo(extInfo); } void TestRequestPreview(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrintJob printJob; printJob.SetJobId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::vector files = {1}; @@ -206,80 +208,79 @@ void TestRequestPreview(const uint8_t *data, size_t size, FuzzedDataProvider *da printJob.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printJob.SetOption(JOB_OPTIONS); std::string previewResult = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->RequestPreview(printJob, previewResult); - return; } void TestQueryPrinterCapability(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->QueryPrinterCapability(printerId); } void TestOn(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); - std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - PrintServiceAbility::GetInstance()->On(taskId, type, nullptr); + PrintDocumentAdapter *printerAdapterPtr = new PrintDocumentAdapter(); + sptr callback = new(std::nothrow) PrintCallback(printerAdapterPtr); + if (callback != nullptr) { + std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); + std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); + PrintServiceAbility::GetInstance()->On(taskId, type, callback); + } else { + delete printerAdapterPtr; + printerAdapterPtr = nullptr; + } } void TestOff(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->Off(taskId, type); } -void TestRegisterPrinterCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) +void TestPrinterCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); - std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - PrintServiceAbility::GetInstance()->RegisterPrinterCallback(type, nullptr); + PrintDocumentAdapter *printerAdapterPtr = new PrintDocumentAdapter(); + sptr callback = new(std::nothrow) PrintCallback(printerAdapterPtr); + if (callback != nullptr) { + std::string type = PRINTER_DISCOVER_EVENT_TYPE; + PrintServiceAbility::GetInstance()->RegisterPrinterCallback(type, callback); + PrintServiceAbility::GetInstance()->UnregisterPrinterCallback(type); + } else { + delete printerAdapterPtr; + printerAdapterPtr = nullptr; + } } -void TestUnregisterPrinterCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) -{ - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); - std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - PrintServiceAbility::GetInstance()->UnregisterPrinterCallback(type); -} -void TestRegisterExtCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) +void TestExtCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); + std::vector printExtensionInfos; + PrintServiceAbility::GetInstance()->QueryAllExtension(printExtensionInfos); + std::vector extensionIds; + for (auto &printExtensionInfo: printExtensionInfos) { + extensionIds.push_back(printExtensionInfo.GetExtensionId()); + } + PrintServiceAbility::GetInstance()->StartDiscoverPrinter(extensionIds); + + for (auto &printExtensionInfo: printExtensionInfos) { + PrintServiceAbility::GetInstance()->RegisterExtCallback(printExtensionInfo.GetExtensionId(), nullptr); + PrintServiceAbility::GetInstance()->UnregisterAllExtCallback(printExtensionInfo.GetExtensionId()); + } + std::string extensionCID = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->RegisterExtCallback(extensionCID, nullptr); -} - -void TestUnregisterAllExtCallback(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) -{ - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); - std::string extensionCID = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->UnregisterAllExtCallback(extensionCID); } void TestLoadExtSuccess(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string extensionId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->LoadExtSuccess(extensionId); } void TestQueryAllPrintJob(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrintJob printJob; printJob.SetJobId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::vector files = {0}; @@ -290,13 +291,11 @@ void TestQueryAllPrintJob(const uint8_t *data, size_t size, FuzzedDataProvider * printJob.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::vector printJobs; printJobs.push_back(printJob); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->QueryAllPrintJob(printJobs); } void TestQueryPrintJobById(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrintJob printJob; printJob.SetJobId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::vector files = {0}; @@ -306,14 +305,11 @@ void TestQueryPrintJobById(const uint8_t *data, size_t size, FuzzedDataProvider printJob.SetPageSize(pageSize); printJob.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::string printJobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->QueryPrintJobById(printJobId, printJob); } void TestAddPrinterToCups(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerUri = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerMake = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); @@ -322,8 +318,6 @@ void TestAddPrinterToCups(const uint8_t *data, size_t size, FuzzedDataProvider * void TestQueryPrinterCapabilityByUri(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerUri = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrinterCapability printerCaps; @@ -332,47 +326,37 @@ void TestQueryPrinterCapabilityByUri(const uint8_t *data, size_t size, FuzzedDat void TestSetHelper(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->SetHelper(nullptr); } void TestPrintByAdapter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); std::string jobName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintAttributes printAttributes; std::string taskId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->PrintByAdapter(jobName, printAttributes, taskId); } void TestStartGetPrintFile(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintAttributes printAttributes; uint32_t fd = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->StartGetPrintFile(jobId, printAttributes, fd); } void TestQueryPrinterInfoByPrinterId(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrinterInfo printerInfo; printerInfo.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printerInfo.SetPrinterName(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printerInfo.SetOption(JOB_OPTIONS); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->QueryPrinterInfoByPrinterId(printerId, printerInfo); } void TestNotifyPrintService(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string type = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->NotifyPrintService(jobId, type); @@ -380,8 +364,6 @@ void TestNotifyPrintService(const uint8_t *data, size_t size, FuzzedDataProvider void TestQueryAddedPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::vector printerNameList; printerNameList.push_back(printerName); @@ -390,8 +372,6 @@ void TestQueryAddedPrinter(const uint8_t *data, size_t size, FuzzedDataProvider void TestQueryPrinterProperties(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string key = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::vector keyList; @@ -404,8 +384,6 @@ void TestQueryPrinterProperties(const uint8_t *data, size_t size, FuzzedDataProv void TestUpdatePrintJobState(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t state = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); uint32_t subState = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); @@ -414,7 +392,6 @@ void TestUpdatePrintJobState(const uint8_t *data, size_t size, FuzzedDataProvide void TestGetPrinterPreference(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerPreference = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->GetPrinterPreference(printerId, printerPreference); @@ -422,7 +399,6 @@ void TestGetPrinterPreference(const uint8_t *data, size_t size, FuzzedDataProvid void TestSetPrinterPreference(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerPreference = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->SetPrinterPreference(printerId, printerPreference); @@ -430,7 +406,6 @@ void TestSetPrinterPreference(const uint8_t *data, size_t size, FuzzedDataProvid void TestSetDefaultPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t type = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); PrintServiceAbility::GetInstance()->SetDefaultPrinter(printerId, type); @@ -438,7 +413,6 @@ void TestSetDefaultPrinter(const uint8_t *data, size_t size, FuzzedDataProvider void TestDeletePrinterFromCups(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printerMake = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); @@ -447,7 +421,6 @@ void TestDeletePrinterFromCups(const uint8_t *data, size_t size, FuzzedDataProvi void TestDiscoverUsbPrinters(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrinterInfo printerInfo; printerInfo.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printerInfo.SetPrinterName(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); @@ -460,7 +433,6 @@ void TestDiscoverUsbPrinters(const uint8_t *data, size_t size, FuzzedDataProvide void TestStartNativePrintJob(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrintJob testJob; testJob.SetJobId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::vector files = {1}; @@ -475,7 +447,6 @@ void TestStartNativePrintJob(const uint8_t *data, size_t size, FuzzedDataProvide void TestNotifyPrintServiceEvent(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t event = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); PrintServiceAbility::GetInstance()->NotifyPrintServiceEvent(jobId, event); @@ -484,19 +455,16 @@ void TestNotifyPrintServiceEvent(const uint8_t *data, size_t size, FuzzedDataPro // below are private test void TestUpdateQueuedJobList(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string printJobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); auto printJob = std::make_shared(); printJob->SetJobId(printJobId); printJob->SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->UpdateQueuedJobList(printJobId, printJob); } void TestUpdatePrintJobOptionByPrinterId(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbilityMockPermission::MockPermission(); PrintJob printJob; printJob.SetJobId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); std::vector files = {0}; @@ -505,20 +473,17 @@ void TestUpdatePrintJobOptionByPrinterId(const uint8_t *data, size_t size, Fuzze pageSize.SetId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); printJob.SetPageSize(pageSize); printJob.SetPrinterId(dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH)); - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->UpdatePrintJobOptionByPrinterId(printJob); } void TestDelayStartDiscovery(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string extensionId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->DelayStartDiscovery(extensionId); } void TestAdapterGetFileCallBack(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t state = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); uint32_t subState = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); @@ -527,7 +492,6 @@ void TestAdapterGetFileCallBack(const uint8_t *data, size_t size, FuzzedDataProv void TestAddNativePrintJob(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintJob printJob; PrintServiceAbility::GetInstance()->AddNativePrintJob(jobId, printJob); @@ -535,28 +499,24 @@ void TestAddNativePrintJob(const uint8_t *data, size_t size, FuzzedDataProvider void TestIsQueuedJobListEmpty(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->IsQueuedJobListEmpty(jobId); } void TestSetPrintJobCanceled(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintJob printJob; PrintServiceAbility::GetInstance()->SetPrintJobCanceled(printJob); } void TestCancelUserPrintJobs(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); int32_t userId = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); PrintServiceAbility::GetInstance()->CancelUserPrintJobs(userId); } void TestSendExtensionEvent(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string extensionId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string extInfo = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->SendExtensionEvent(extensionId, extInfo); @@ -564,19 +524,16 @@ void TestSendExtensionEvent(const uint8_t *data, size_t size, FuzzedDataProvider void TestCallStatusBar(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->CallStatusBar(); } void TestUpdatePrintUserMap(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->UpdatePrintUserMap(); } -void TestnotifyAdapterJobChanged(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) +void TestNotifyAdapterJobChanged(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); uint32_t state = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); uint32_t subState = dataProvider->ConsumeIntegralInRange(0, MAX_SET_NUMBER); @@ -585,45 +542,38 @@ void TestnotifyAdapterJobChanged(const uint8_t *data, size_t size, FuzzedDataPro void TestRegisterAdapterListener(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string jobId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->RegisterAdapterListener(jobId); } void TestisEprint(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->isEprint(printerId); } void TestManualStart(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->ManualStart(); } void TestGetPrintJobOrderId(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->GetPrintJobOrderId(); } void TestOnStop(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->OnStop(); } void TestWritePreferenceToFile(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintServiceAbility::GetInstance()->WritePreferenceToFile(); } void TestBuildPrinterPreference(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string option = "{\ \"cupsOptions\" : {\ \"supportedPageSizeArray\" : \"String\",\ @@ -639,7 +589,6 @@ void TestBuildPrinterPreference(const uint8_t *data, size_t size, FuzzedDataProv void TestBuildPrinterPreferenceByDefault(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string optJson = "{\ \"defaultPageSizeId\" : \"String\",\ \"orientation-requested-default\" : \"String\",\ @@ -653,7 +602,6 @@ void TestBuildPrinterPreferenceByDefault(const uint8_t *data, size_t size, Fuzze void TestBuildPrinterPreferenceByOption(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string key = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string supportedOpts = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string optAttr = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); @@ -664,7 +612,6 @@ void TestBuildPrinterPreferenceByOption(const uint8_t *data, size_t size, Fuzzed void TestBuildPrinterAttrComponentByJson(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string key = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); std::string arrObject = "{\ \"cupsOptions\" : [\ @@ -682,28 +629,24 @@ void TestBuildPrinterAttrComponentByJson(const uint8_t *data, size_t size, Fuzze void TestCheckIsDefaultPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->CheckIsDefaultPrinter(printerId); } void TestCheckIsLastUsedPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->CheckIsLastUsedPrinter(printerName); } void TestSetLastUsedPrinter(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); std::string printerId = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH); PrintServiceAbility::GetInstance()->SetLastUsedPrinter(printerId); } void TestSendPrintJobEvent(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider) { - PrintServiceAbility::GetInstance()->Init(); PrintJob jobInfo; uint32_t jobStateArr[] = {PRINT_JOB_COMPLETED, PRINT_JOB_BLOCKED, PRINT_JOB_COMPLETED}; for (auto jobState : jobStateArr) { @@ -883,7 +826,7 @@ void TestNotPublicFunction(const uint8_t *data, size_t size, FuzzedDataProvider TestSendExtensionEvent(data, size, dataProvider); TestCallStatusBar(data, size, dataProvider); TestUpdatePrintUserMap(data, size, dataProvider); - TestnotifyAdapterJobChanged(data, size, dataProvider); + TestNotifyAdapterJobChanged(data, size, dataProvider); TestRegisterAdapterListener(data, size, dataProvider); TestisEprint(data, size, dataProvider); TestWritePreferenceToFile(data, size, dataProvider); @@ -924,6 +867,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) } FuzzedDataProvider dataProvider(data, size); + OHOS::Print::TestInitialize(data, size, &dataProvider); OHOS::Print::TestStartService(data, size, &dataProvider); OHOS::Print::TestStartPrint(data, size, &dataProvider); OHOS::Print::TestStopPrint(data, size, &dataProvider); @@ -931,7 +875,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::Print::TestDisconnectPrinter(data, size, &dataProvider); OHOS::Print::TestStartDiscoverPrinter(data, size, &dataProvider); OHOS::Print::TestStopDiscoverPrinter(data, size, &dataProvider); - // OHOS::Print::TestQueryAllExtension(data, size, &dataProvider); + OHOS::Print::TestQueryAllExtension(data, size, &dataProvider); OHOS::Print::TestStartPrintJob(data, size, &dataProvider); OHOS::Print::TestCancelPrintJob(data, size, &dataProvider); OHOS::Print::TestAddPrinters(data, size, &dataProvider); @@ -944,10 +888,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::Print::TestQueryPrinterCapability(data, size, &dataProvider); OHOS::Print::TestOn(data, size, &dataProvider); OHOS::Print::TestOff(data, size, &dataProvider); - OHOS::Print::TestRegisterPrinterCallback(data, size, &dataProvider); - OHOS::Print::TestUnregisterPrinterCallback(data, size, &dataProvider); - OHOS::Print::TestRegisterExtCallback(data, size, &dataProvider); - OHOS::Print::TestUnregisterAllExtCallback(data, size, &dataProvider); + OHOS::Print::TestPrinterCallback(data, size, &dataProvider); + OHOS::Print::TestExtCallback(data, size, &dataProvider); OHOS::Print::TestLoadExtSuccess(data, size, &dataProvider); OHOS::Print::TestQueryAllPrintJob(data, size, &dataProvider); OHOS::Print::TestQueryPrintJobById(data, size, &dataProvider);