oss-fuzz/projects/jackson-core/DataInputFuzzer.java

134 lines
3.5 KiB
Java
Raw Normal View History

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
public class DataInputFuzzer {
public static class MockFuzzDataInput implements DataInput
{
private final InputStream _input;
public MockFuzzDataInput(byte[] data) {
_input = new ByteArrayInputStream(data);
}
public MockFuzzDataInput(String utf8Data) throws IOException {
_input = new ByteArrayInputStream(utf8Data.getBytes("UTF-8"));
}
public MockFuzzDataInput(InputStream in) {
_input = in;
}
@Override
public void readFully(byte[] b) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public int skipBytes(int n) throws IOException {
return (int) _input.skip(n);
}
@Override
public boolean readBoolean() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public byte readByte() throws IOException {
int ch = _input.read();
if (ch < 0) {
throw new EOFException("End-of-input for readByte()");
}
return (byte) ch;
}
@Override
public int readUnsignedByte() throws IOException {
return readByte() & 0xFF;
}
@Override
public short readShort() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public int readUnsignedShort() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public char readChar() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public int readInt() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public long readLong() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public float readFloat() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public double readDouble() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public String readLine() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public String readUTF() throws IOException {
throw new UnsupportedOperationException();
}
}
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
JsonFactory jf = new JsonFactory();
try {
JsonParser jp = jf.createParser(new MockFuzzDataInput(data.consumeRemainingAsString()));
while (jp.nextToken() != null) {
;
}
} catch (IOException ignored) {
}
}
}