/src/logging-log4cxx/src/fuzzers/cpp/TimeBasedRollingPolicyFuzzer.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include "stdint.h" |
19 | | #include <fuzzer/FuzzedDataProvider.h> |
20 | | #include <log4cxx/patternlayout.h> |
21 | | #include <log4cxx/rolling/rollingfileappender.h> |
22 | | #include <log4cxx/rolling/timebasedrollingpolicy.h> |
23 | | #include <log4cxx/logmanager.h> |
24 | | |
25 | | #define DATE_PATTERN "yyyy-MM-dd_HH_mm_ss" |
26 | 623 | #define PATTERN_LAYOUT LOG4CXX_STR("%c{1} - %m%n") |
27 | | |
28 | | using namespace log4cxx; |
29 | | using namespace log4cxx::helpers; |
30 | | using namespace log4cxx::rolling; |
31 | | |
32 | | // A fuzzer for TimeBasedRollingPolicy |
33 | 623 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
34 | 623 | FuzzedDataProvider fdp(data, size); |
35 | | |
36 | | // Set up logger |
37 | 623 | Pool pool; |
38 | 623 | PatternLayoutPtr layout( new PatternLayout(PATTERN_LAYOUT)); |
39 | 623 | RollingFileAppenderPtr rfa( new RollingFileAppender()); |
40 | 623 | rfa->setAppend(fdp.ConsumeBool()); |
41 | 623 | rfa->setLayout(layout); |
42 | | |
43 | 623 | TimeBasedRollingPolicyPtr tbrp = TimeBasedRollingPolicyPtr(new TimeBasedRollingPolicy()); |
44 | 623 | bool usegz = fdp.ConsumeBool(); |
45 | 623 | if(usegz) { |
46 | 242 | tbrp->setFileNamePattern(LogString(LOG4CXX_STR("fuzz-%d{" DATE_PATTERN "}.gz"))); |
47 | 381 | } else { |
48 | 381 | tbrp->setFileNamePattern(LogString(LOG4CXX_STR("fuzz-%d{" DATE_PATTERN "}.zip"))); |
49 | 381 | } |
50 | 623 | rfa->setFile(LOG4CXX_STR("test.log")); |
51 | | |
52 | 623 | tbrp->activateOptions(pool); |
53 | 623 | rfa->setRollingPolicy(tbrp); |
54 | 623 | rfa->activateOptions(pool); |
55 | 623 | rfa->setBufferedSeconds(fdp.ConsumeIntegral<int>()); |
56 | 623 | rfa->activateOptions(pool); |
57 | 623 | LoggerPtr logger = LogManager::getLogger("org.apache.log4j.TimeBasedRollingTest"); |
58 | 623 | logger->addAppender(rfa); |
59 | | |
60 | | // Log and rollover |
61 | 6.85k | for (int i = 0; i < 10; i++) |
62 | 6.23k | { |
63 | 6.23k | if (i == 4 || i == 9) |
64 | 1.24k | { |
65 | 1.24k | rfa->rollover(pool); |
66 | 1.24k | } |
67 | | |
68 | 6.23k | LOG4CXX_DEBUG(logger, fdp.ConsumeRandomLengthString()); |
69 | 6.23k | } |
70 | | |
71 | | // Cleanup |
72 | 623 | logger->removeAppender(rfa); |
73 | 623 | rfa->close(); |
74 | 623 | LogManager::shutdown(); |
75 | 623 | std::remove("test.log"); |
76 | | |
77 | 623 | return 0; |
78 | 623 | } |