Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
*/
public class DirstreamClientHandler extends ChannelInboundHandlerAdapter {

private static final String INVALID_FORMAT_MESSAGE =
"Expected format: <size> <filename> where <size> is a number and <filename> "
+ "is a string separated by a single space. Example: '1024 myfile.txt'";

private final StreamingDestination destination;
private boolean headerMode = true;
private String currentFileName = "";
Expand Down Expand Up @@ -80,7 +84,16 @@ public void doRead(ChannelHandlerContext ctx, ByteBuf buffer)
name.release();
buffer.skipBytes(1);
String[] parts = currentFileName.split(" ", 2);
remaining = Long.parseLong(parts[0]);
if (parts.length < 2 || parts[1].isEmpty()) {
throw new IllegalArgumentException("Invalid file name format: " + currentFileName + ". "
+ INVALID_FORMAT_MESSAGE);
}
try {
remaining = Long.parseLong(parts[0]);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid file name format: " + currentFileName + ". "
+ INVALID_FORMAT_MESSAGE, e);
}
Path destFilePath = destination.mapToDestination(parts[1]);
final Path destfileParent = destFilePath.getParent();
if (destfileParent == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.hadoop.ozone.container.stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.netty.buffer.ByteBuf;
Expand All @@ -27,8 +29,12 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Test streaming client.
Expand Down Expand Up @@ -113,6 +119,32 @@ public void splitContent() throws IOException {
assertEquals("yyy", getContent("bsd.txt"));
}

@ParameterizedTest(name = "Invalid format: {0}")
@MethodSource("provideInvalidFormatTestCases")
public void testInvalidFormat(String testCaseName, String invalidInput) {
final DirstreamClientHandler handler = new DirstreamClientHandler(
new DirectoryServerDestination(tmpDir));

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
handler.doRead(null, wrap(invalidInput));
});
assertThat(exception)
.hasMessageContaining("Invalid file name format");
}

private static Stream<Arguments> provideInvalidFormatTestCases() {
return Stream.of(
// Test case: Missing space between size and filename
Arguments.of("Missing space", "123File.txt\n"),
// Test case: Empty filename after space
Arguments.of("Empty filename", "123 \n"),
// Test case: Only size number, no filename
Arguments.of("Only size", "12345\n"),
// Test case: Size is not a number
Arguments.of("Invalid size (non-numeric)", "oops filename.txt\n")
);
}

@Nonnull
private String getContent(String name) throws IOException {
return new String(Files.readAllBytes(tmpDir.resolve(name)),
Expand Down