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 #include "printcupsattribute_fuzzer.h"
16 #include "fuzzer/FuzzedDataProvider.h"
17 #include "print_cups_attribute.h"
18
19 namespace OHOS {
20 namespace Print {
21 constexpr uint8_t MAX_STRING_LENGTH = 255;
22 constexpr int MAX_SET_NUMBER = 100;
23 constexpr size_t FOO_MAX_LEN = 1024;
24 constexpr size_t U32_AT_SIZE = 4;
25
TestParsePrinterStatusAttributes(const uint8_t * data,size_t size,FuzzedDataProvider * dataProvider)26 void TestParsePrinterStatusAttributes(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
27 {
28 ipp_t *response = ippNew();
29 std::string printerUri = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
30 std::string jobName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
31 ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printerUri.c_str());
32 ippAddString(response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-name", NULL, jobName.c_str());
33 ippAddInteger(response, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies",
34 dataProvider->ConsumeIntegralInRange<int>(0, MAX_SET_NUMBER));
35 PrinterStatus status = PRINTER_STATUS_UNAVAILABLE;
36 OHOS::Print::ParsePrinterStatusAttributes(response, status);
37 ippDelete(response);
38 }
39
TestParsePrinterAttributes(const uint8_t * data,size_t size,FuzzedDataProvider * dataProvider)40 void TestParsePrinterAttributes(const uint8_t *data, size_t size, FuzzedDataProvider *dataProvider)
41 {
42 ipp_t *response = ippNew();
43 std::string printerUri = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
44 std::string jobName = dataProvider->ConsumeRandomLengthString(MAX_STRING_LENGTH);
45 ippAddString(response, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printerUri.c_str());
46 ippAddString(response, IPP_TAG_JOB, IPP_TAG_KEYWORD, "job-name", NULL, jobName.c_str());
47 ippAddInteger(response, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies",
48 dataProvider->ConsumeIntegralInRange<int>(0, MAX_SET_NUMBER));
49 PrinterCapability printerCaps;
50 OHOS::Print::ParsePrinterAttributes(response, printerCaps);
51 ippDelete(response);
52 }
53
54 } // namespace Print
55 } // namespace OHOS
56
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
59 {
60 /* Run your code on data */
61 if (data == nullptr) {
62 return 0;
63 }
64
65 if (size < OHOS::Print::U32_AT_SIZE || size > OHOS::Print::FOO_MAX_LEN) {
66 }
67
68 FuzzedDataProvider dataProvider(data, size);
69 OHOS::Print::TestParsePrinterStatusAttributes(data, size, &dataProvider);
70 OHOS::Print::TestParsePrinterAttributes(data, size, &dataProvider);
71
72 return 0;
73 }
74