Skip to content

Commit 4ec6b0b

Browse files
authored
test: consolidate frame garbage rejection checks (#62)
1 parent a3035d4 commit 4ec6b0b

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ZstdFrameTest {
1919

2020
private static final byte[] PAYLOAD =
2121
"frame inspection payload ".repeat(500).getBytes(StandardCharsets.UTF_8);
22+
private static final byte[] GARBAGE = "xx".getBytes(StandardCharsets.UTF_8);
2223

2324
@Nested
2425
class IsFrame {
@@ -30,7 +31,13 @@ void recognizesAZstdFrame() {
3031

3132
@Test
3233
void rejectsGarbage() {
33-
assertThat(ZstdFrame.isZstdFrame("not a frame".getBytes(StandardCharsets.UTF_8))).isFalse();
34+
// Given bytes that are not a zstd frame
35+
36+
// When checking whether they are a frame
37+
boolean result = ZstdFrame.isZstdFrame(ZstdFrameTest.GARBAGE);
38+
39+
// Then it reports false rather than throwing
40+
assertThat(result).isFalse();
3441
}
3542
}
3643

@@ -62,13 +69,11 @@ void locatesFirstFrameBoundaryWhenConcatenated() throws Exception {
6269
@Test
6370
void rejectsGarbage() {
6471
// Given bytes that are not a zstd frame
65-
byte[] garbage = "xxxx".getBytes(StandardCharsets.UTF_8);
66-
6772
// When asking for the first frame's compressed size
68-
ThrowingCallable result = () -> ZstdFrame.compressedSize(garbage);
73+
ThrowingCallable result = () -> ZstdFrame.compressedSize(ZstdFrameTest.GARBAGE);
6974

7075
// Then it fails
71-
assertThatThrownBy(result).isInstanceOf(ZstdException.class);
76+
ZstdFrameTest.rejectsGarbage(result);
7277
}
7378
}
7479

@@ -84,13 +89,11 @@ void boundsTheDecompressedSize() {
8489
@Test
8590
void rejectsGarbage() {
8691
// Given bytes that are not valid zstd data
87-
byte[] garbage = "xx".getBytes(StandardCharsets.UTF_8);
88-
8992
// When asking for the decompressed bound
90-
ThrowingCallable result = () -> ZstdFrame.decompressedBound(garbage);
93+
ThrowingCallable result = () -> ZstdFrame.decompressedBound(ZstdFrameTest.GARBAGE);
9194

9295
// Then it fails
93-
assertThatThrownBy(result).isInstanceOf(ZstdException.class);
96+
ZstdFrameTest.rejectsGarbage(result);
9497
}
9598
}
9699

@@ -152,13 +155,11 @@ void failsWhenAFrameDoesNotRecordItsSize() throws Exception {
152155
@Test
153156
void rejectsGarbage() {
154157
// Given bytes that are not valid zstd data
155-
byte[] garbage = "xx".getBytes(StandardCharsets.UTF_8);
156-
157158
// When the exact total is requested
158-
ThrowingCallable result = () -> ZstdFrame.decompressedSize(garbage);
159+
ThrowingCallable result = () -> ZstdFrame.decompressedSize(ZstdFrameTest.GARBAGE);
159160

160161
// Then it fails
161-
assertThatThrownBy(result).isInstanceOf(ZstdException.class);
162+
ZstdFrameTest.rejectsGarbage(result);
162163
}
163164
}
164165

@@ -256,13 +257,11 @@ void enablesInPlaceDecompression() {
256257
@Test
257258
void rejectsGarbage() {
258259
// Given bytes that are not valid zstd data
259-
byte[] garbage = "xx".getBytes(StandardCharsets.UTF_8);
260-
261260
// When the margin is requested
262-
ThrowingCallable result = () -> ZstdFrame.decompressionMargin(garbage);
261+
ThrowingCallable result = () -> ZstdFrame.decompressionMargin(ZstdFrameTest.GARBAGE);
263262

264263
// Then it fails
265-
assertThatThrownBy(result).isInstanceOf(ZstdException.class);
264+
ZstdFrameTest.rejectsGarbage(result);
266265
}
267266
}
268267

@@ -475,4 +474,8 @@ void standardNativeFrameIsNotSkippable() {
475474
}
476475
}
477476
}
477+
478+
private static void rejectsGarbage(ThrowingCallable result) {
479+
assertThatThrownBy(result).isInstanceOf(ZstdException.class);
480+
}
478481
}

0 commit comments

Comments
 (0)