forked from vy/log4j2-logstash-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test to demonstrate stack overflow "bomb" in logstash layout
- Loading branch information
1 parent
b2f8351
commit f933ba0
Showing
2 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
layout/src/test/java/com/vlkan/log4j2/logstash/layout/LogStashLayoutGeneratorStateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.vlkan.log4j2.logstash.layout; | ||
|
||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.config.Configuration; | ||
import org.apache.logging.log4j.core.config.DefaultConfiguration; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
import java.nio.BufferOverflowException; | ||
|
||
public class LogStashLayoutGeneratorStateTest { | ||
private static final Configuration CONFIGURATION = new DefaultConfiguration(); | ||
|
||
@Test | ||
public void testGeneratorStateCorruptedAfterUnhandledException() { | ||
LogstashLayout layout = LogstashLayout | ||
.newBuilder() | ||
.setConfiguration(CONFIGURATION) | ||
.setEventTemplateUri("classpath:LogstashTestLayout.json") | ||
.setStackTraceEnabled(true) | ||
.setLocationInfoEnabled(true) | ||
.setEmptyPropertyExclusionEnabled(true) | ||
.build(); | ||
LogEvent hugeMessageLogEvent = LogEventFixture.createHugeMeessagLogEvent(512 * 1024); | ||
LogEvent fullEvent = LogEventFixture.createFullLogEvents(1).get(0); | ||
long counter = 0; | ||
encodeSafe(layout, fullEvent); | ||
Assertions.assertThatThrownBy(()->layout.toByteArray(hugeMessageLogEvent)).hasCauseInstanceOf(BufferOverflowException.class); | ||
long startTime = System.currentTimeMillis(); | ||
while(true){ | ||
encodeSafe(layout, fullEvent); | ||
long l = ++counter; | ||
if(System.currentTimeMillis()-startTime>=1000){ | ||
System.out.println(l); | ||
startTime=System.currentTimeMillis(); | ||
} | ||
} | ||
|
||
} | ||
|
||
private void encodeSafe(LogstashLayout layout, LogEvent fullEvent) { | ||
try { | ||
layout.toByteArray(fullEvent); | ||
} | ||
catch (Exception e){ | ||
//do nothing | ||
} | ||
} | ||
} |