Skip to content

Commit 355b931

Browse files
committed
Test round trip of circular exceptions through LoggingEventVO
Check that genuinely circular exceptions, and exceptions reaching the same throwable twice, still deserialize now that cyclic or shared throwable proxies are rejected. Replace reflection in the test with a package-private LoggingEventVO.setThrowableProxyVO(). Signed-off-by: ceki <[email protected]>
1 parent c7b8e2f commit 355b931

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventVO.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public IThrowableProxy getThrowableProxy() {
140140
return throwableProxy;
141141
}
142142

143+
// for tests only
144+
void setThrowableProxyVO(ThrowableProxyVO tpVO) {
145+
this.throwableProxy = tpVO;
146+
}
147+
143148
public StackTraceElement[] getCallerData() {
144149
return callerDataArray;
145150
}

‎logback-classic/src/test/java/ch/qos/logback/classic/spi/LoggingEventSerializationTest.java‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.junit.jupiter.api.AfterEach;
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
26-
import org.slf4j.MDC;
2726
import org.slf4j.Marker;
2827
import org.slf4j.MarkerFactory;
2928

@@ -34,14 +33,14 @@
3433
import java.io.ObjectInputStream;
3534
import java.io.ObjectOutputStream;
3635
import java.io.Serializable;
37-
import java.lang.reflect.Field;
3836
import java.util.Arrays;
3937
import java.util.Map;
4038

4139
import static org.junit.jupiter.api.Assertions.assertEquals;
4240
import static org.junit.jupiter.api.Assertions.assertNotNull;
4341
import static org.junit.jupiter.api.Assertions.assertNull;
4442
import static org.junit.jupiter.api.Assertions.assertThrows;
43+
import static org.junit.jupiter.api.Assertions.assertTrue;
4544

4645
public class LoggingEventSerializationTest {
4746

@@ -251,11 +250,47 @@ public void sharedSuppressedThrowableProxyIsRejected() throws Exception {
251250
assertThrows(InvalidObjectException.class, () -> writeAndReadWithThrowableProxy(tp));
252251
}
253252

253+
// ThrowableProxy replaces the repeated occurrence of a throwable by a distinct proxy
254+
// marked as cyclic, so genuinely circular exceptions must survive the round trip
255+
@Test
256+
public void circularExceptionRoundTrip() throws Exception {
257+
Exception root = new Exception("root");
258+
Exception cause = new Exception("cause");
259+
root.initCause(cause);
260+
// root -> cause -> suppressed root
261+
cause.addSuppressed(root);
262+
263+
LoggingEvent event = createLoggingEventWithThrowable(root);
264+
ILoggingEvent remoteEvent = writeAndRead(event);
265+
checkForEquality(event, remoteEvent);
266+
267+
IThrowableProxy remoteCause = remoteEvent.getThrowableProxy().getCause();
268+
assertTrue(remoteCause.getSuppressed()[0].isCyclic());
269+
String rendered = ThrowableProxyUtil.asString(remoteEvent.getThrowableProxy());
270+
assertTrue(rendered.contains("[CIRCULAR REFERENCE: java.lang.Exception: root]"), rendered);
271+
}
272+
273+
// the same throwable reachable twice without forming a cycle
274+
@Test
275+
public void exceptionWithSameCauseAndSuppressedRoundTrip() throws Exception {
276+
Exception root = new Exception("root");
277+
Exception other = new Exception("other");
278+
root.initCause(other);
279+
root.addSuppressed(other);
280+
281+
LoggingEvent event = createLoggingEventWithThrowable(root);
282+
ILoggingEvent remoteEvent = writeAndRead(event);
283+
checkForEquality(event, remoteEvent);
284+
285+
IThrowableProxy remoteProxy = remoteEvent.getThrowableProxy();
286+
assertTrue(remoteProxy.getSuppressed()[0].isCyclic());
287+
String rendered = ThrowableProxyUtil.asString(remoteProxy);
288+
assertTrue(rendered.contains("[CIRCULAR REFERENCE: java.lang.Exception: other]"), rendered);
289+
}
290+
254291
private void writeAndReadWithThrowableProxy(ThrowableProxyVO tp) throws Exception {
255292
LoggingEventVO vo = LoggingEventVO.build(createLoggingEvent());
256-
Field field = LoggingEventVO.class.getDeclaredField("throwableProxy");
257-
field.setAccessible(true);
258-
field.set(vo, tp);
293+
vo.setThrowableProxyVO(tp);
259294
oos.writeObject(vo);
260295
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
261296
inputStream = new HardenedLoggingEventInputStream(loggerContext, bis);

0 commit comments

Comments
 (0)