Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-15123: Add tests for ChunkedBytesStream #13941

Merged
merged 3 commits into from Jul 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.spy;

import java.io.IOException;
Expand Down Expand Up @@ -64,6 +65,16 @@ public void testCorrectnessForMethodReadFully(ByteBuffer input) throws IOExcepti
assertArrayEquals(input.array(), got);
}

@ParameterizedTest
@MethodSource("provideCasesWithInvalidInputsForMethodRead")
public void testInvalidInputsForMethodRead(byte[] b, int off, int len) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(16);

try (InputStream is = new ChunkedBytesStream(new ByteBufferInputStream(buffer.duplicate()), supplier, 10, false)) {
assertThrows(IndexOutOfBoundsException.class, () -> is.read(b, off, len));
}
}

@ParameterizedTest
@MethodSource("provideSourceBytebuffersForTest")
public void testCorrectnessForMethodReadByte(ByteBuffer input) throws IOException {
Expand Down Expand Up @@ -149,6 +160,45 @@ public void testCorrectnessForMethodSkip(int bytesToPreRead, ByteBuffer inputBuf
}
}

@ParameterizedTest
@MethodSource("provideEdgeCaseInputForMethodSkip")
public void testEdgeCaseInputForMethodSkip(int bufferLength, long toSkip, boolean delegateSkipToSourceStream, long expected) throws IOException {
ByteBuffer inputBuf = ByteBuffer.allocate(bufferLength);
RANDOM.nextBytes(inputBuf.array());
inputBuf.rewind();

try (InputStream is = new ChunkedBytesStream(new ByteBufferInputStream(inputBuf.duplicate()), supplier, 10, delegateSkipToSourceStream)) {
assertEquals(expected, is.skip(toSkip));
}
}

private static Stream<Arguments> provideEdgeCaseInputForMethodSkip() {
int bufferLength = 16;
// Test toSkip larger than int and negative for both delegateToSourceStream true and false
return Stream.of(
Arguments.of(bufferLength, Integer.MAX_VALUE + 1L, true, bufferLength),
Arguments.of(bufferLength, -1, true, 0),
Arguments.of(bufferLength, Integer.MAX_VALUE + 1L, false, bufferLength),
Arguments.of(bufferLength, -1, false, 0)
);
}

private static Stream<Arguments> provideCasesWithInvalidInputsForMethodRead() {
byte[] b = new byte[16];
return Stream.of(
// negative off
Arguments.of(b, -1, b.length),
// negative len
Arguments.of(b, 0, -1),
// overflow off + len
Arguments.of(b, Integer.MAX_VALUE, 10),
// len greater than size of target array
Arguments.of(b, 0, b.length + 1),
// off + len greater than size of target array
Arguments.of(b, b.length - 1, 2)
);
}

private static List<Arguments> provideSourceSkipValuesForTest() {
ByteBuffer bufGreaterThanIntermediateBuf = ByteBuffer.allocate(16);
RANDOM.nextBytes(bufGreaterThanIntermediateBuf.array());
Expand Down