Skip to content

Commit 528015f

Browse files
committed
-
1 parent 4fb7af6 commit 528015f

4 files changed

Lines changed: 44 additions & 52 deletions

File tree

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

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,43 @@
2020
import org.xerial.snappy.Snappy;
2121
import org.xerial.snappy.BitShuffle;
2222
import java.util.Arrays;
23+
import java.util.function.Function;
2324

2425
public class BitShuffleFuzzer {
2526
private static final int SIZE = 4096;
2627

2728
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
28-
fuzzBitshuffleInts(data.consumeInts(SIZE));
29-
fuzzBitshuffleLongs(data.consumeLongs(SIZE));
30-
fuzzBitshuffleShorts(data.consumeShorts(SIZE));
31-
}
32-
33-
private static void runFuzz(RunnableWithException block) {
34-
try {
35-
block.run();
36-
} catch (Exception e) {
37-
throw new RuntimeException(e);
38-
}
29+
fuzzBitshuffle(data.consumeInts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleIntArray, "int[]");
30+
fuzzBitshuffle(data.consumeLongs(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleLongArray, "long[]");
31+
fuzzBitshuffle(data.consumeShorts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleShortArray, "short[]");
3932
}
4033

4134
@FunctionalInterface
42-
private interface RunnableWithException {
35+
private interface ExceptionRunnable {
4336
void run() throws Exception;
4437
}
4538

46-
private static void fuzzBitshuffleInts(int[] original) {
47-
runFuzz(() -> {
48-
byte[] shuffledByteArray = BitShuffle.shuffle(original);
49-
byte[] compressed = Snappy.compress(shuffledByteArray);
50-
byte[] uncompressed = Snappy.uncompress(compressed);
51-
int[] result = BitShuffle.unshuffleIntArray(uncompressed);
52-
if (!Arrays.equals(original, result)) {
53-
throw new IllegalStateException("Original and uncompressed data are different");
54-
}
55-
});
39+
@FunctionalInterface
40+
private interface ShuffleFn<T> {
41+
byte[] apply(T input) throws Exception;
5642
}
5743

58-
private static void fuzzBitshuffleLongs(long[] original) {
59-
runFuzz(() -> {
60-
byte[] shuffledByteArray = BitShuffle.shuffle(original);
61-
byte[] compressed = Snappy.compress(shuffledByteArray);
62-
byte[] uncompressed = Snappy.uncompress(compressed);
63-
long[] result = BitShuffle.unshuffleLongArray(uncompressed);
64-
if (!Arrays.equals(original, result)) {
65-
throw new IllegalStateException("Original and uncompressed data are different");
66-
}
67-
});
44+
@FunctionalInterface
45+
private interface UnshuffleFn<T> {
46+
T apply(byte[] input) throws Exception;
6847
}
6948

70-
private static void fuzzBitshuffleShorts(short[] original) {
71-
runFuzz(() -> {
72-
byte[] shuffledByteArray = BitShuffle.shuffle(original);
49+
private static <T> void fuzzBitshuffle(T original, ShuffleFn<T> shuffle, UnshuffleFn<T> unshuffle, String typeName) {
50+
try {
51+
byte[] shuffledByteArray = shuffle.apply(original);
7352
byte[] compressed = Snappy.compress(shuffledByteArray);
7453
byte[] uncompressed = Snappy.uncompress(compressed);
75-
short[] result = BitShuffle.unshuffleShortArray(uncompressed);
76-
if (!Arrays.equals(original, result)) {
77-
throw new IllegalStateException("Original and uncompressed data are different");
54+
T result = unshuffle.apply(uncompressed);
55+
if (!Arrays.equals((Object[]) original, (Object[]) result)) {
56+
throw new IllegalStateException("Original and uncompressed " + typeName + " data are different");
7857
}
79-
});
58+
} catch (Exception e) {
59+
throw new RuntimeException(e);
60+
}
8061
}
8162
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2023 Google LLC
2+
//
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+
////////////////////////////////////////////////////////////////////////////////
16+
17+
package org.xerial.snappy.fuzz;
18+
19+
@FunctionalInterface
20+
public interface FuzzBlock {
21+
void run() throws Exception;
22+
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333

3434
public class SnappyCombinedFuzzer {
3535

36-
@FunctionalInterface
37-
private interface FuzzBlock {
38-
void run() throws Exception;
39-
}
40-
4136
private static void runFuzz(FuzzBlock block) {
4237
try {
4338
block.run();
@@ -199,15 +194,14 @@ private static void testCrc32C(FuzzedDataProvider data) {
199194
}
200195

201196
PureJavaCrc32C crcChunked = new PureJavaCrc32C();
202-
for (int i = 0; i < Math.min(input.length, 1000); i++) {
197+
for (int i = 0; i < input.length; i++) {
203198
crcChunked.update(input[i] & 0xFF);
204199
}
205200

206201
PureJavaCrc32C crcWhole = new PureJavaCrc32C();
207202
crcWhole.update(input, 0, input.length);
208-
long wholeValue = crcWhole.getValue();
209203

210-
if (input.length <= 1000 && crcChunked.getValue() != wholeValue) {
204+
if (crcChunked.getValue() != crcWhole.getValue()) {
211205
throw new IllegalStateException("CRC32C chunked vs whole mismatch");
212206
}
213207
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
*/
3232
public class SnappyStreamFuzzer {
3333

34-
@FunctionalInterface
35-
private interface FuzzBlock {
36-
void run() throws Exception;
37-
}
38-
3934
private static void runFuzz(FuzzBlock block) {
4035
try {
4136
block.run();

0 commit comments

Comments
 (0)