Skip to content

Commit cabae22

Browse files
committed
-
1 parent 5a14675 commit cabae22

2 files changed

Lines changed: 96 additions & 74 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,23 @@ public class BitShuffleFuzzer {
2525
private static final int SIZE = 4096;
2626

2727
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
28-
fuzzBitshuffle(data.consumeInts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleIntArray, "int[]");
29-
fuzzBitshuffle(data.consumeLongs(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleLongArray, "long[]");
30-
fuzzBitshuffle(data.consumeShorts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleShortArray, "short[]");
31-
fuzzBitshuffle(data.consumeFloats(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleFloatArray, "float[]");
32-
fuzzBitshuffle(data.consumeDoubles(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleDoubleArray, "double[]");
28+
switch (data.consumeInt(0, 4)) {
29+
case 0:
30+
fuzzBitshuffle(data.consumeInts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleIntArray, "int[]");
31+
break;
32+
case 1:
33+
fuzzBitshuffle(data.consumeLongs(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleLongArray, "long[]");
34+
break;
35+
case 2:
36+
fuzzBitshuffle(data.consumeShorts(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleShortArray, "short[]");
37+
break;
38+
case 3:
39+
fuzzBitshuffle(data.consumeFloats(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleFloatArray, "float[]");
40+
break;
41+
case 4:
42+
fuzzBitshuffle(data.consumeDoubles(SIZE), BitShuffle::shuffle, BitShuffle::unshuffleDoubleArray, "double[]");
43+
break;
44+
}
3345
}
3446

3547
@FunctionalInterface

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

Lines changed: 79 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -72,76 +72,86 @@ public static void fuzzerTestOneInput(FuzzedDataProvider data) {
7272
}
7373

7474
private static void testRawApi(FuzzedDataProvider data) {
75-
runFuzz(() -> {
76-
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
77-
byte[] compressed = Snappy.compress(input);
78-
byte[] uncompressed = Snappy.uncompress(compressed);
79-
if (!Arrays.equals(input, uncompressed)) {
80-
throw new IllegalStateException("Raw compress/uncompress failed");
81-
}
82-
});
83-
84-
runFuzz(() -> {
85-
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
86-
byte[] rawCompressed = Snappy.rawCompress(input, input.length);
87-
if (Snappy.isValidCompressedBuffer(rawCompressed)) {
88-
int uncompressedLen = Snappy.uncompressedLength(rawCompressed);
89-
if (uncompressedLen == input.length) {
90-
byte[] rawUncompressed = new byte[uncompressedLen];
91-
Snappy.rawUncompress(rawCompressed, 0, rawCompressed.length, rawUncompressed, 0);
92-
if (!Arrays.equals(input, rawUncompressed)) {
93-
throw new IllegalStateException("Raw compress/uncompress with byte[] failed");
75+
switch (data.consumeInt(0, 6)) {
76+
case 0:
77+
runFuzz(() -> {
78+
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
79+
byte[] compressed = Snappy.compress(input);
80+
byte[] uncompressed = Snappy.uncompress(compressed);
81+
if (!Arrays.equals(input, uncompressed)) {
82+
throw new IllegalStateException("Raw compress/uncompress failed");
9483
}
95-
}
96-
}
97-
});
98-
99-
runFuzz(() -> {
100-
try {
101-
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
102-
Snappy.isValidCompressedBuffer(input);
103-
Snappy.isValidCompressedBuffer(input, 0, input.length);
104-
} catch (IOException e) {
105-
}
106-
});
107-
108-
runFuzz(() -> {
109-
int inputLength = data.consumeInt(0, 4096);
110-
int maxLen = Snappy.maxCompressedLength(inputLength);
111-
if (maxLen < inputLength) {
112-
throw new IllegalStateException("maxCompressedLength too small");
113-
}
114-
});
115-
116-
runFuzz(() -> {
117-
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
118-
byte[] compressed = Snappy.compress(input);
119-
if (Snappy.isValidCompressedBuffer(compressed)) {
120-
int len = Snappy.uncompressedLength(compressed);
121-
int len2 = Snappy.uncompressedLength(compressed, 0, compressed.length);
122-
if (len != input.length || len2 != input.length) {
123-
throw new IllegalStateException("uncompressedLength did not match original length");
124-
}
125-
}
126-
});
127-
128-
runFuzz(() -> {
129-
int[] intInput = data.consumeInts(data.consumeInt(0, 100));
130-
byte[] compressedInts = Snappy.compress(intInput);
131-
int[] uncompressedInts = Snappy.uncompressIntArray(compressedInts);
132-
if (!Arrays.equals(intInput, uncompressedInts)) {
133-
throw new IllegalStateException("Int array roundtrip failed");
134-
}
135-
});
136-
137-
runFuzz(() -> {
138-
long[] longInput = data.consumeLongs(data.consumeInt(0, 50));
139-
byte[] compressedLongs = Snappy.compress(longInput);
140-
long[] uncompressedLongs = Snappy.uncompressLongArray(compressedLongs);
141-
if (!Arrays.equals(longInput, uncompressedLongs)) {
142-
throw new IllegalStateException("Long array roundtrip failed");
143-
}
144-
});
84+
});
85+
break;
86+
case 1:
87+
runFuzz(() -> {
88+
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
89+
byte[] rawCompressed = Snappy.rawCompress(input, input.length);
90+
if (Snappy.isValidCompressedBuffer(rawCompressed)) {
91+
int uncompressedLen = Snappy.uncompressedLength(rawCompressed);
92+
if (uncompressedLen == input.length) {
93+
byte[] rawUncompressed = new byte[uncompressedLen];
94+
Snappy.rawUncompress(rawCompressed, 0, rawCompressed.length, rawUncompressed, 0);
95+
if (!Arrays.equals(input, rawUncompressed)) {
96+
throw new IllegalStateException("Raw compress/uncompress with byte[] failed");
97+
}
98+
}
99+
}
100+
});
101+
break;
102+
case 2:
103+
runFuzz(() -> {
104+
try {
105+
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
106+
Snappy.isValidCompressedBuffer(input);
107+
Snappy.isValidCompressedBuffer(input, 0, input.length);
108+
} catch (IOException e) {
109+
}
110+
});
111+
break;
112+
case 3:
113+
runFuzz(() -> {
114+
int inputLength = data.consumeInt(0, 4096);
115+
int maxLen = Snappy.maxCompressedLength(inputLength);
116+
if (maxLen < inputLength) {
117+
throw new IllegalStateException("maxCompressedLength too small");
118+
}
119+
});
120+
break;
121+
case 4:
122+
runFuzz(() -> {
123+
byte[] input = data.consumeBytes(data.consumeInt(0, 4096));
124+
byte[] compressed = Snappy.compress(input);
125+
if (Snappy.isValidCompressedBuffer(compressed)) {
126+
int len = Snappy.uncompressedLength(compressed);
127+
int len2 = Snappy.uncompressedLength(compressed, 0, compressed.length);
128+
if (len != input.length || len2 != input.length) {
129+
throw new IllegalStateException("uncompressedLength did not match original length");
130+
}
131+
}
132+
});
133+
break;
134+
case 5:
135+
runFuzz(() -> {
136+
int[] intInput = data.consumeInts(data.consumeInt(0, 100));
137+
byte[] compressedInts = Snappy.compress(intInput);
138+
int[] uncompressedInts = Snappy.uncompressIntArray(compressedInts);
139+
if (!Arrays.equals(intInput, uncompressedInts)) {
140+
throw new IllegalStateException("Int array roundtrip failed");
141+
}
142+
});
143+
break;
144+
case 6:
145+
runFuzz(() -> {
146+
long[] longInput = data.consumeLongs(data.consumeInt(0, 50));
147+
byte[] compressedLongs = Snappy.compress(longInput);
148+
long[] uncompressedLongs = Snappy.uncompressLongArray(compressedLongs);
149+
if (!Arrays.equals(longInput, uncompressedLongs)) {
150+
throw new IllegalStateException("Long array roundtrip failed");
151+
}
152+
});
153+
break;
154+
}
145155
}
146156

147157
private static void testFramed(FuzzedDataProvider data) {

0 commit comments

Comments
 (0)