Skip to content

Commit

Permalink
feat: close connection if IllegalArgumentException or PduValidationEx…
Browse files Browse the repository at this point in the history
…ception is caught

Co-authored-by: Torsten Will <[email protected]>
  • Loading branch information
volkland and towi committed Sep 25, 2024
1 parent 2f97563 commit 50e45f8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ protected void decode(
final List<Object> out
) throws Exception {
LOGGER.debug("Child handler decoding message: {}", byteBuf);
x2x3Decoder.decode(byteBuf, out);

try {
x2x3Decoder.decode(byteBuf, out);
} catch (final IllegalArgumentException e) {
LOGGER.error(e.getMessage(), e);
channelHandlerContext.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.sipgate.li.simulator.x2x3;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import com.sipgate.li.lib.x2x3.PduValidationException;
import com.sipgate.li.lib.x2x3.X2X3Decoder;
import io.netty.channel.ChannelHandlerContext;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class X2X3NettyDecoderTest {

@Mock
X2X3Decoder x2X3Decoder;

@InjectMocks
X2X3NettyDecoder underTest;

@Spy
private ChannelHandlerContext channelHandlerContext;

static Stream<Arguments> decoderExceptions() {
return Stream.of(
Arguments.arguments(new PduValidationException("something's broken", "it's your fault")),
Arguments.arguments(new IllegalArgumentException("Header too large"))
);
}

@ParameterizedTest
@MethodSource("decoderExceptions")
void it_closes_on_pdu_validation_exception(final Exception e) throws Exception {
// GIVEN
doThrow(e).when(x2X3Decoder).decode(any(), any());

// WHEN
underTest.decode(channelHandlerContext, null, null);

// THEN
verify(channelHandlerContext).close();
}
}

0 comments on commit 50e45f8

Please sign in to comment.