1 /*
2 * Copyright (c) 2025 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 #include <fuzzer/FuzzedDataProvider.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #include "print_manager_adapter_impl.h"
28
29 namespace OHOS::NWeb {
30 constexpr uint8_t MAX_STRING_LENGTH = 255;
31 const char *TESTFILE_PATH = "/data/test/fuzz_testfile";
32
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)33 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
34 {
35 if (size == 0) {
36 return 0;
37 }
38
39 std::vector<std::string> fileList = { TESTFILE_PATH };
40 int32_t fd = open(TESTFILE_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
41 if (fd < 0) {
42 return 0;
43 }
44 (void)write(fd, data, size);
45
46 std::vector<uint32_t> fdList = { fd };
47 FuzzedDataProvider dataProvider(data, size);
48 std::string taskId = dataProvider.ConsumeRandomLengthString(MAX_STRING_LENGTH);
49 PrintManagerAdapterImpl::GetInstance().StartPrint(fileList, fdList, taskId);
50 std::shared_ptr<PrintDocumentAdapterAdapter> printDocumentAdapterImpl;
51 PrintAttributesAdapter printAttributesAdapter;
52 PrintManagerAdapterImpl::GetInstance().Print("webPrintTestJob", printDocumentAdapterImpl, printAttributesAdapter);
53 void* token = nullptr;
54 PrintManagerAdapterImpl::GetInstance().Print("webPrintTestJob", printDocumentAdapterImpl,
55 printAttributesAdapter, token);
56
57 close(fd);
58 return 0;
59 }
60
61 } // namespace OHOS::NWeb