Skip to content

Commit

Permalink
[SUREFIRE-2058] BufferOverflowException when encoding message with nu…
Browse files Browse the repository at this point in the history
…ll runMode
  • Loading branch information
zoltanmeze committed Apr 25, 2022
1 parent 2a72800 commit 511b5c4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,8 @@ public int estimateBufferLength( int opcodeLength, RunMode runMode, CharsetEncod
// one byte + one delimiter character ':' + <string> + one delimiter character ':'
int lengthOfMetadata = 1 + getEncodedMagicNumber().length + 1 + 1 + 1 + opcodeLength + 1;

if ( runMode != null )
{
// one byte of length + one delimiter character ':' + <string> + one delimiter character ':'
lengthOfMetadata += 1 + 1 + runMode.getRunmode().length() + 1;
}
// one byte of length + one delimiter character ':' + <string> + one delimiter character ':'
lengthOfMetadata += 1 + 1 + ( runMode == null ? 0 : runMode.getRunmodeBinary().length ) + 1;

if ( encoder != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,45 +134,45 @@ public void shouldComputeStreamPreemptiveLength()
NORMAL_RUN, encoder, 0, 1, "s" ) )
.isEqualTo( 88 );

// :maven-surefire-event:16:console-info-log:5:UTF-8:0003:sss:
// :maven-surefire-event:16:console-info-log:0::5:UTF-8:0001:s:
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_CONSOLE_INFO.getOpcodeBinary().length,
null, encoder, 0, 0, "s" ) )
.isEqualTo( 58 );
.isEqualTo( 61 );

// :maven-surefire-event:17:console-debug-log:5:UTF-8:0003:sss:
// :maven-surefire-event:17:console-debug-log:0::5:UTF-8:0001:s:
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_CONSOLE_DEBUG.getOpcodeBinary().length,
null, encoder, 0, 0, "s" ) )
.isEqualTo( 59 );
.isEqualTo( 62 );

// :maven-surefire-event:19:console-warning-log:5:UTF-8:0003:sss:
// :maven-surefire-event:19:console-warning-log:0::5:UTF-8:0001:s:
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_CONSOLE_WARNING.getOpcodeBinary().length,
null, encoder, 0, 0, "s" ) )
.isEqualTo( 61 );
.isEqualTo( 64 );

// :maven-surefire-event:17:console-error-log:5:UTF-8:0003:sss:
// :maven-surefire-event:17:console-error-log:0::5:UTF-8:0001:s:
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_CONSOLE_ERROR.getOpcodeBinary().length,
null, encoder, 0, 0, "s" ) )
.isEqualTo( 59 );
.isEqualTo( 62 );

// :maven-surefire-event:3:bye:
// :maven-surefire-event:3:bye:0::
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_BYE.getOpcodeBinary().length,
null, null, 0, 0 ) )
.isEqualTo( 28 );
.isEqualTo( 31 );

// :maven-surefire-event:17:stop-on-next-test:
// :maven-surefire-event:17:stop-on-next-test:0::
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_STOP_ON_NEXT_TEST.getOpcodeBinary().length,
null, null, 0, 0 ) )
.isEqualTo( 42 );
.isEqualTo( 45 );

// :maven-surefire-event:9:next-test:
// :maven-surefire-event:9:next-test:0::
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_NEXT_TEST.getOpcodeBinary().length,
null, null, 0, 0 ) )
.isEqualTo( 34 );
.isEqualTo( 37 );

// :maven-surefire-event:14:jvm-exit-error:
// :maven-surefire-event:14:jvm-exit-error:0::
assertThat( streamEncoder.estimateBufferLength( BOOTERCODE_JVM_EXIT_ERROR.getOpcodeBinary().length,
null, null, 0, 0 ) )
.isEqualTo( 39 );
.isEqualTo( 42 );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,25 @@ public void testStdErrStreamEmptyMessageNullTestId() throws IOException
.isEqualTo( expected );
}

@Test
public void testStdErrStreamEmptyMessageNullRunMode() throws IOException
{
Stream out = Stream.newStream();
WritableBufferedByteChannel channel = newBufferedChannel( out );
EventChannelEncoder encoder = new EventChannelEncoder( channel );

// This used to produce a BufferOverflowException; see SUREFIRE-2056.
encoder.testOutput( new TestOutputReportEntry( stdErr( "" ), null, 1L ) );
channel.close();

String expected = ":maven-surefire-event:\u000e:std-err-stream:"
+ (char) 0 + "::" // One byte for length and 1+1 bytes for the 2 delimiters (0 for null run mode)
+ "\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001:"
+ "\u0005:UTF-8:\u0000\u0000\u0000\u0000::";

assertThat( new String( out.toByteArray(), UTF_8 ) )
.isEqualTo( expected );
}

@Test
@SuppressWarnings( "checkstyle:innerassignment" )
Expand Down

0 comments on commit 511b5c4

Please sign in to comment.