Skip to content

Commit 1e930c4

Browse files
Log4J SPI Factory Tests
Basic test implementation asserting that the appender/scrubber utils are applied when log content is passed through the Logger implementations.
1 parent a904dab commit 1e930c4

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* OWASP Enterprise Security API (ESAPI)
3+
*
4+
* This file is part of the Open Web Application Security Project (OWASP)
5+
* Enterprise Security API (ESAPI) project. For details, please see
6+
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
7+
*
8+
* Copyright (c) 2007 - The OWASP Foundation
9+
*
10+
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
11+
* LICENSE before you use, modify, and/or redistribute this software.
12+
*
13+
* @author Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a>
14+
* @created 2019
15+
*/
16+
package org.owasp.esapi.logging.log4j;
17+
18+
import org.apache.log4j.Level;
19+
import org.apache.log4j.Logger;
20+
import org.apache.log4j.spi.LoggerRepository;
21+
import org.junit.Before;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import org.junit.rules.TestName;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.ArgumentMatchers;
27+
import org.mockito.Mock;
28+
import org.mockito.Mockito;
29+
import org.mockito.junit.MockitoJUnitRunner;
30+
import org.owasp.esapi.logging.appender.LogAppender;
31+
import org.owasp.esapi.logging.cleaning.LogScrubber;
32+
import org.powermock.reflect.Whitebox;
33+
34+
/**
35+
* Basic test implementation that verifies that the {@link LogAppender} and
36+
* {@link LogScrubber} references are applied by the {@link Logger} instances
37+
* created by the {@link Log4JLoggerFactory}.
38+
*
39+
*/
40+
@RunWith(MockitoJUnitRunner.class)
41+
public class Log4JLoggerFactoryTest {
42+
@Mock
43+
private LogScrubber scrubber;
44+
@Mock
45+
private LogAppender appender;
46+
@Rule
47+
public TestName testName = new TestName();
48+
49+
private String logMsg = testName.getMethodName() + "-MESSAGE";
50+
private Logger logger;
51+
52+
@Before
53+
public void configureStaticFactoryState() {
54+
Log4JLoggerFactory factory = new Log4JLoggerFactory();
55+
Whitebox.setInternalState(Log4JLoggerFactory.class, "LOG4J_LOG_SCRUBBER", scrubber);
56+
Whitebox.setInternalState(Log4JLoggerFactory.class, "LOG4J_LOG_APPENDER", appender);
57+
58+
Mockito.when(scrubber.cleanMessage(logMsg)).thenReturn(logMsg);
59+
Mockito.when(appender.appendTo(testName.getMethodName(), null, logMsg)).thenReturn(logMsg);
60+
61+
LoggerRepository mockRepo = Mockito.mock(LoggerRepository.class);
62+
Mockito.when(mockRepo.isDisabled(ArgumentMatchers.anyInt())).thenReturn(false);
63+
64+
logger = factory.makeNewLoggerInstance(testName.getMethodName());
65+
Whitebox.setInternalState(logger, "repository", mockRepo);
66+
logger.setLevel(Level.ALL);
67+
}
68+
69+
@Test
70+
public void testLogDebug() {
71+
logger.debug(logMsg);
72+
Mockito.verify(scrubber, Mockito.times(1)).cleanMessage(logMsg);
73+
Mockito.verify(appender, Mockito.times(1)).appendTo(testName.getMethodName(), null, logMsg);
74+
}
75+
76+
@Test
77+
public void testLogInfo() {
78+
logger.info(logMsg);
79+
Mockito.verify(scrubber, Mockito.times(1)).cleanMessage(logMsg);
80+
Mockito.verify(appender, Mockito.times(1)).appendTo(testName.getMethodName(), null, logMsg);
81+
}
82+
83+
@Test
84+
public void testLogWarn() {
85+
logger.warn(logMsg);
86+
Mockito.verify(scrubber, Mockito.times(1)).cleanMessage(logMsg);
87+
Mockito.verify(appender, Mockito.times(1)).appendTo(testName.getMethodName(), null, logMsg);
88+
}
89+
90+
@Test
91+
public void testLogError() {
92+
logger.error(logMsg);
93+
Mockito.verify(scrubber, Mockito.times(1)).cleanMessage(logMsg);
94+
Mockito.verify(appender, Mockito.times(1)).appendTo(testName.getMethodName(), null, logMsg);
95+
}
96+
97+
}

0 commit comments

Comments
 (0)