|
| 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 | +package org.apache.tika.ossfuzz; |
| 18 | + |
| 19 | +import java.io.InputStream; |
| 20 | + |
| 21 | +import org.xml.sax.ContentHandler; |
| 22 | + |
| 23 | +import org.apache.tika.io.TikaInputStream; |
| 24 | +import org.apache.tika.metadata.Metadata; |
| 25 | +import org.apache.tika.parser.ParseContext; |
| 26 | +import org.apache.tika.parser.Parser; |
| 27 | +import org.apache.tika.parser.RecursiveParserWrapper; |
| 28 | +import org.apache.tika.sax.BasicContentHandlerFactory; |
| 29 | +import org.apache.tika.sax.RecursiveParserWrapperHandler; |
| 30 | + |
| 31 | +import org.apache.tika.sax.ToTextContentHandler; |
| 32 | + |
| 33 | + |
| 34 | +class ParserFuzzer { |
| 35 | + |
| 36 | + public static void parseOne(Parser parser, byte[] bytes, ParseContext parseContext) throws Throwable { |
| 37 | + parseBytes(parser, bytes, parseContext); |
| 38 | + parseFile(parser, bytes, parseContext); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + public static void parseOne(Parser parser, byte[] bytes) throws Throwable { |
| 43 | + parseBytes(parser, bytes, new ParseContext()); |
| 44 | + parseFile(parser, bytes, new ParseContext()); |
| 45 | + } |
| 46 | + |
| 47 | + public static void parseRMetaFile(Parser parser, byte[] bytes) throws Throwable { |
| 48 | + RecursiveParserWrapper wrapper = new RecursiveParserWrapper(parser); |
| 49 | + RecursiveParserWrapperHandler rpwh = new RecursiveParserWrapperHandler( |
| 50 | + new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.XML, -1)); |
| 51 | + try (TikaInputStream tis = TikaInputStream.get(bytes)) { |
| 52 | + tis.getPath(); |
| 53 | + wrapper.parse(tis, rpwh, new Metadata(), new ParseContext()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static void parseBytes(Parser parser, byte[] bytes, ParseContext parseContext) throws Throwable { |
| 58 | + ContentHandler handler = new ToTextContentHandler(); |
| 59 | + //make sure that other parsers cannot be invoked |
| 60 | + parseContext.set(Parser.class, parser); |
| 61 | + //try first with bytes |
| 62 | + try (InputStream is = TikaInputStream.get(bytes)) { |
| 63 | + parser.parse(is, handler, new Metadata(), parseContext); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static void parseFile(Parser parser, byte[] bytes, ParseContext parseContext) throws Throwable { |
| 68 | + ContentHandler handler = new ToTextContentHandler(); |
| 69 | + //make sure that other parsers cannot be invoked |
| 70 | + parseContext.set(Parser.class, parser); |
| 71 | + try (TikaInputStream tis = TikaInputStream.get(bytes)) { |
| 72 | + //force writing to tmp file |
| 73 | + tis.getPath(); |
| 74 | + parser.parse(tis, handler, new Metadata(), parseContext); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments