Skip to content

Commit

Permalink
[SUREFIRE-2058] Add readString unit test covering cases with overflow…
Browse files Browse the repository at this point in the history
…ing output buffer

- shouldReadStringOverflowOnNewLine - ends up with 1 single byte (LF) remaining on input buffer
- shouldReadStringOverflowOn4BytesEncodedSymbol - causing an infinite loop with 4 bytes left on input buffer
  • Loading branch information
zoltanmeze committed Apr 25, 2022
1 parent 2657a32 commit a771bbb
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,56 @@ public void shouldReadString() throws Exception
.isEqualTo( "0123456789" );
}

@Test
public void shouldReadStringOverflowOnNewLine() throws Exception
{
StringBuilder s = new StringBuilder( 1025 );
for ( int i = 0; i < 10; i++ )
{
s.append( PATTERN1 );
}
s.append( PATTERN1, 0, 23 );
s.append( "\u00FA\n" ); // 2-bytes encoded character + LF

Channel channel = new Channel( s.toString().getBytes( UTF_8 ), s.length() );

Mock thread = new Mock( channel, new MockForkNodeArguments(),
Collections.<Segment, ForkedProcessEventType>emptyMap() );

Memento memento = thread.new Memento();

assertThat( (String) invokeMethod( thread, "readString", memento, 1026 ) )
.isEqualTo( s.toString() );

assertThat ( memento.getByteBuffer().remaining() )
.isEqualTo( 0 );
}

@Test
public void shouldReadStringOverflowOn4BytesEncodedSymbol() throws Exception
{
StringBuilder s = new StringBuilder( 1025 );
for ( int i = 0; i < 10; i++ )
{
s.append( PATTERN1 );
}
s.append( PATTERN1, 0, 23 );
s.append( "\uD83D\uDE35" ); // 4-bytes encoded character

Channel channel = new Channel( s.toString().getBytes( UTF_8 ), s.length() );

Mock thread = new Mock( channel, new MockForkNodeArguments(),
Collections.<Segment, ForkedProcessEventType>emptyMap() );

Memento memento = thread.new Memento();

assertThat( (String) invokeMethod( thread, "readString", memento, 1027 ) )
.isEqualTo( s.toString() );

assertThat ( memento.getByteBuffer().remaining() )
.isEqualTo( 0 );
}

@Test
public void shouldReadStringShiftedBuffer() throws Exception
{
Expand Down

0 comments on commit a771bbb

Please sign in to comment.