Skip to content

Commit cf0ca81

Browse files
committed
-
1 parent 363eb19 commit cf0ca81

2 files changed

Lines changed: 10 additions & 36 deletions

File tree

src/test/java/org/xerial/snappy/fuzz/SnappyCombinedFuzzer.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
public class SnappyCombinedFuzzer {
3535

36+
@FunctionalInterface
3637
private interface FuzzBlock {
3738
void run() throws Exception;
3839
}
@@ -180,14 +181,15 @@ private static void testFramed(FuzzedDataProvider data) {
180181

181182
private static void testCrc32C(FuzzedDataProvider data) {
182183
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
183-
byte[] chunk1 = data.consumeBytes(50);
184-
byte[] chunk2 = data.consumeBytes(50);
185184

186185
PureJavaCrc32C crc = new PureJavaCrc32C();
187186
crc.update(input, 0, input.length);
188187
long value = crc.getValue();
189188

190189
int intValue = crc.getIntegerValue();
190+
if ((int) value != intValue) {
191+
throw new IllegalStateException("CRC32C int value mismatch");
192+
}
191193

192194
crc.reset();
193195
crc.update(input, 0, input.length);
@@ -200,42 +202,14 @@ private static void testCrc32C(FuzzedDataProvider data) {
200202
for (int i = 0; i < Math.min(input.length, 1000); i++) {
201203
crcChunked.update(input[i] & 0xFF);
202204
}
203-
long chunkedValue = crcChunked.getValue();
204205

205206
PureJavaCrc32C crcWhole = new PureJavaCrc32C();
206207
crcWhole.update(input, 0, input.length);
207208
long wholeValue = crcWhole.getValue();
208209

209-
if (input.length <= 1000 && chunkedValue != wholeValue) {
210+
if (input.length <= 1000 && crcChunked.getValue() != wholeValue) {
210211
throw new IllegalStateException("CRC32C chunked vs whole mismatch");
211212
}
212-
213-
if (chunk1.length > 0 && chunk2.length > 0) {
214-
PureJavaCrc32C crc1 = new PureJavaCrc32C();
215-
crc1.update(input, 0, input.length);
216-
long crc1Value = crc1.getValue();
217-
218-
PureJavaCrc32C crc2 = new PureJavaCrc32C();
219-
crc2.update(chunk1, 0, chunk1.length);
220-
crc2.update(chunk2, 0, chunk2.length);
221-
long crc2Value = crc2.getValue();
222-
}
223-
224-
PureJavaCrc32C crcEmpty = new PureJavaCrc32C();
225-
crcEmpty.update(new byte[0], 0, 0);
226-
long emptyValue = crcEmpty.getValue();
227-
228-
if (input.length > 0) {
229-
PureJavaCrc32C crcSingle = new PureJavaCrc32C();
230-
crcSingle.update(input[0] & 0xFF);
231-
long singleValue = crcSingle.getValue();
232-
}
233-
234-
if (input.length > 4) {
235-
PureJavaCrc32C crcOffset = new PureJavaCrc32C();
236-
crcOffset.update(input, 1, input.length - 1);
237-
long offsetValue = crcOffset.getValue();
238-
}
239213
}
240214

241215
private static void testBlockStream(FuzzedDataProvider data) {

src/test/java/org/xerial/snappy/fuzz/SnappyStreamFuzzer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class SnappyStreamFuzzer {
2828
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
2929

3030
byte[] original = data.consumeRemainingAsBytes();
31-
byte[] uncompressed = null;
3231

3332
try {
3433
ByteArrayOutputStream compressedBuf = new ByteArrayOutputStream();
@@ -37,6 +36,7 @@ public static void fuzzerTestOneInput(FuzzedDataProvider data) {
3736
snappyOut.close();
3837
byte[] compressed = compressedBuf.toByteArray();
3938

39+
byte[] uncompressed;
4040
try (SnappyInputStream snappyIn = new SnappyInputStream(new ByteArrayInputStream(compressed))) {
4141
ByteArrayOutputStream out = new ByteArrayOutputStream();
4242
byte[] buf = new byte[4096];
@@ -47,12 +47,12 @@ public static void fuzzerTestOneInput(FuzzedDataProvider data) {
4747
out.flush();
4848
uncompressed = out.toByteArray();
4949
}
50+
51+
if (!Arrays.equals(original, uncompressed)) {
52+
throw new IllegalStateException("Original and uncompressed data are different");
53+
}
5054
} catch (IOException e) {
5155
throw new RuntimeException(e);
52-
}
53-
54-
if (!Arrays.equals(original, uncompressed)) {
55-
throw new IllegalStateException("Original and uncompressed data are different");
5656
}
5757
}
5858
}

0 commit comments

Comments
 (0)