1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "printmanager_adapter_fuzz.h"
17
18 #include <cstdint>
19 #include <cstring>
20 #include <memory>
21 #include <string>
22 #include <vector>
23
24 #include "print_manager_adapter_impl.h"
25
26 namespace OHOS::NWeb {
27
28 const size_t MAX_LEN_STRING = 256;
29 const size_t MAX_FD_LIST_SIZE = 10;
30
StartPrintFuzzTest(PrintManagerAdapterImpl & adapter,const uint8_t * data,size_t size)31 bool StartPrintFuzzTest(PrintManagerAdapterImpl& adapter, const uint8_t* data, size_t size)
32 {
33 std::vector<std::string> fileList;
34 std::vector<uint32_t> fdList;
35
36 for (size_t i = 0; i < (size % MAX_FD_LIST_SIZE + 1); ++i) {
37 std::string fileName(reinterpret_cast<const char*>(data), std::min(size, MAX_LEN_STRING - 1));
38 fileList.push_back(fileName);
39 fdList.push_back(i);
40 }
41
42 std::string taskId;
43
44 adapter.StartPrint(fileList, fdList, taskId);
45 return true;
46 }
47
PrintFuzzTest(PrintManagerAdapterImpl & adapter,const uint8_t * data,size_t size)48 bool PrintFuzzTest(PrintManagerAdapterImpl& adapter, const uint8_t* data, size_t size)
49 {
50 std::string printJobName(reinterpret_cast<const char*>(data), std::min(size, MAX_LEN_STRING - 1));
51 std::shared_ptr<PrintDocumentAdapterAdapter> listener = nullptr;
52 PrintAttributesAdapter printAttributes;
53
54 adapter.Print(printJobName, listener, printAttributes);
55 return true;
56 }
57
PrintWithContextFuzzTest(PrintManagerAdapterImpl & adapter,const uint8_t * data,size_t size)58 bool PrintWithContextFuzzTest(PrintManagerAdapterImpl& adapter, const uint8_t* data, size_t size)
59 {
60 std::string printJobName(reinterpret_cast<const char*>(data), std::min(size, MAX_LEN_STRING - 1));
61 std::shared_ptr<PrintDocumentAdapterAdapter> listener = nullptr;
62 PrintAttributesAdapter printAttributes;
63 void* contextToken = nullptr;
64
65 adapter.Print(printJobName, listener, printAttributes, contextToken);
66
67 return true;
68 }
69
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72 if (size == 0) {
73 return 0;
74 }
75
76 PrintManagerAdapterImpl& adapter = PrintManagerAdapterImpl::GetInstance();
77
78 enum PrintFuzzOperation { START_PRINT, PRINT, PRINT_WITH_CONTEXT, MAX_PRINT_OPERATION };
79
80 PrintFuzzOperation operation = static_cast<PrintFuzzOperation>(data[0] % MAX_PRINT_OPERATION);
81
82 switch (operation) {
83 case START_PRINT:
84 return StartPrintFuzzTest(adapter, data + 1, size - 1);
85 case PRINT:
86 return PrintFuzzTest(adapter, data + 1, size - 1);
87 case PRINT_WITH_CONTEXT:
88 return PrintWithContextFuzzTest(adapter, data + 1, size - 1);
89 default:
90 return 0;
91 }
92
93 return 0;
94 }
95
96 } // namespace OHOS::NWeb