|
4 | 4 | import org.junit.Test; |
5 | 5 | import org.mockito.Mockito; |
6 | 6 |
|
| 7 | +import java.security.SecureRandom; |
7 | 8 | import java.util.Arrays; |
8 | 9 | import java.util.Set; |
9 | 10 | import java.util.zip.Deflater; |
10 | 11 |
|
11 | 12 | import static org.hamcrest.MatcherAssert.assertThat; |
12 | 13 | import static org.hamcrest.Matchers.*; |
| 14 | +import static org.junit.Assert.assertThrows; |
13 | 15 | import static org.mockito.Matchers.argThat; |
14 | 16 |
|
15 | 17 | public class CompressionCodecTest { |
@@ -137,6 +139,69 @@ public void testSizeCompressionCodecLogging() throws Exception { |
137 | 139 | Mockito.verify(mockLogger).info(argThat(stringContainsInOrder("compression support", "enabled", "size"))); |
138 | 140 | } |
139 | 141 |
|
| 142 | + @Test(timeout=1000) |
| 143 | + public void testCompressionCodecDecodeTailTruncated() throws Exception { |
| 144 | + final byte[] truncatedInput = copyWithTruncatedTail(DEFLATE_BALANCED_BYTES.bytes(), 32); |
| 145 | + |
| 146 | + final RuntimeException thrownException = assertThrows(RuntimeException.class, () -> codecNone.decode(truncatedInput)); |
| 147 | + assertThat(thrownException.getMessage(), containsString("IOException while decoding")); |
| 148 | + final Throwable rootCause = extractRootCause(thrownException); |
| 149 | + assertThat(rootCause.getMessage(), anyOf(containsString("prematurely reached end"), containsString("incorrect data check"))); |
| 150 | + } |
| 151 | + |
| 152 | + byte[] copyWithTruncatedTail(final byte[] input, final int tailSize) { |
| 153 | + int startIndex = (input.length < tailSize) ? 0 : input.length - tailSize; |
| 154 | + |
| 155 | + final byte[] result = Arrays.copyOf(input, input.length); |
| 156 | + Arrays.fill(result, startIndex, result.length, (byte) 0); |
| 157 | + |
| 158 | + return result; |
| 159 | + } |
| 160 | + |
| 161 | + @Test(timeout=1000) |
| 162 | + public void testCompressionCodecDecodeTailScrambled() throws Exception { |
| 163 | + final byte[] scrambledInput = copyWithScrambledTail(DEFLATE_BALANCED_BYTES.bytes(), 32); |
| 164 | + |
| 165 | + final RuntimeException thrownException = assertThrows(RuntimeException.class, () -> codecNone.decode(scrambledInput)); |
| 166 | + assertThat(thrownException.getMessage(), containsString("IOException while decoding")); |
| 167 | + final Throwable rootCause = extractRootCause(thrownException); |
| 168 | + assertThat(rootCause.getMessage(), anyOf(containsString("prematurely reached end"), containsString("incorrect data check"))); |
| 169 | + } |
| 170 | + |
| 171 | + byte[] copyWithScrambledTail(final byte[] input, final int tailSize) { |
| 172 | + final SecureRandom secureRandom = new SecureRandom(); |
| 173 | + int startIndex = (input.length < tailSize) ? 0 : input.length - tailSize; |
| 174 | + |
| 175 | + byte[] randomBytes = new byte[input.length - startIndex]; |
| 176 | + secureRandom.nextBytes(randomBytes); |
| 177 | + |
| 178 | + final byte[] result = Arrays.copyOf(input, input.length); |
| 179 | + System.arraycopy(randomBytes, 0, result, startIndex, randomBytes.length); |
| 180 | + |
| 181 | + return result; |
| 182 | + } |
| 183 | + |
| 184 | + @Test(timeout=1000) |
| 185 | + public void testCompressionDecodeTailNullPadded() throws Exception { |
| 186 | + final byte[] nullPaddedInput = copyWithNullPaddedTail(DEFLATE_BALANCED_BYTES.bytes(), 32); |
| 187 | + |
| 188 | + assertThat(codecNone.decode(nullPaddedInput), is(equalTo(RAW_BYTES.bytes()))); |
| 189 | + } |
| 190 | + |
| 191 | + byte[] copyWithNullPaddedTail(final byte[] input, final int tailSize) { |
| 192 | + return Arrays.copyOf(input, Math.addExact(input.length, tailSize)); |
| 193 | + } |
| 194 | + |
| 195 | + Throwable extractRootCause(final Throwable throwable) { |
| 196 | + Throwable current; |
| 197 | + Throwable cause = throwable; |
| 198 | + do { |
| 199 | + current = cause; |
| 200 | + cause = current.getCause(); |
| 201 | + } while (cause != null && cause != current); |
| 202 | + return current; |
| 203 | + } |
| 204 | + |
140 | 205 | void assertDecodesRaw(final CompressionCodec codec) { |
141 | 206 | assertThat(codec.decode(RAW_BYTES.bytes()), is(equalTo(RAW_BYTES.bytes()))); |
142 | 207 | } |
|
0 commit comments