|
| 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 | + * @created 2018 |
| 14 | + */ |
| 15 | +package org.owasp.esapi.logging.java; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.TestName; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.mockito.ArgumentCaptor; |
| 25 | +import org.owasp.esapi.Logger; |
| 26 | +import org.owasp.esapi.logging.appender.LogAppender; |
| 27 | +import org.owasp.esapi.logging.appender.LogPrefixAppender; |
| 28 | +import org.owasp.esapi.logging.cleaning.CodecLogScrubber; |
| 29 | +import org.owasp.esapi.logging.cleaning.CompositeLogScrubber; |
| 30 | +import org.owasp.esapi.logging.cleaning.LogScrubber; |
| 31 | +import org.owasp.esapi.logging.cleaning.NewlineLogScrubber; |
| 32 | +import org.powermock.api.mockito.PowerMockito; |
| 33 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 34 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 35 | + |
| 36 | +@RunWith(PowerMockRunner.class) |
| 37 | +@PrepareForTest (JavaLogFactory.class) |
| 38 | +public class JavaLogFactoryTest { |
| 39 | + @Rule |
| 40 | + public TestName testName = new TestName(); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testCreateLoggerByString() { |
| 44 | + Logger logger = new JavaLogFactory().getLogger("test"); |
| 45 | + Assert.assertTrue(logger instanceof JavaLogger); |
| 46 | + } |
| 47 | + |
| 48 | + @Test public void testCreateLoggerByClass() { |
| 49 | + Logger logger = new JavaLogFactory().getLogger(JavaLogBridgeImplTest.class); |
| 50 | + Assert.assertTrue(logger instanceof JavaLogger); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void checkScrubberWithEncoding() throws Exception { |
| 55 | + ArgumentCaptor<List> delegates = ArgumentCaptor.forClass(List.class); |
| 56 | + PowerMockito.whenNew(CompositeLogScrubber.class).withArguments(delegates.capture()).thenReturn(null); |
| 57 | + |
| 58 | + //Call to invoke the constructor capture |
| 59 | + JavaLogFactory.createLogScrubber(true); |
| 60 | + |
| 61 | + List<LogScrubber> scrubbers = delegates.getValue(); |
| 62 | + Assert.assertEquals(2, scrubbers.size()); |
| 63 | + Assert.assertTrue(scrubbers.get(0) instanceof NewlineLogScrubber); |
| 64 | + Assert.assertTrue(scrubbers.get(1) instanceof CodecLogScrubber); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void checkScrubberWithoutEncoding() throws Exception { |
| 69 | + ArgumentCaptor<List> delegates = ArgumentCaptor.forClass(List.class); |
| 70 | + PowerMockito.whenNew(CompositeLogScrubber.class).withArguments(delegates.capture()).thenReturn(null); |
| 71 | + |
| 72 | + //Call to invoke the constructor capture |
| 73 | + JavaLogFactory.createLogScrubber(false); |
| 74 | + |
| 75 | + List<LogScrubber> scrubbers = delegates.getValue(); |
| 76 | + Assert.assertEquals(1, scrubbers.size()); |
| 77 | + Assert.assertTrue(scrubbers.get(0) instanceof NewlineLogScrubber); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * At this time there are no special considerations or handling for the appender |
| 82 | + * creation in this scope. It is expected that the arguments to the internal |
| 83 | + * creation method are passed directly to the constructor of the |
| 84 | + * LogPrefixAppender with no mutation or additional validation. |
| 85 | + */ |
| 86 | + @Test |
| 87 | + public void checkPassthroughAppenderConstruct() throws Exception { |
| 88 | + LogPrefixAppender stubAppender = new LogPrefixAppender(true, true, true, ""); |
| 89 | + ArgumentCaptor<Boolean> clientInfoCapture = ArgumentCaptor.forClass(Boolean.class); |
| 90 | + ArgumentCaptor<Boolean> serverInfoCapture = ArgumentCaptor.forClass(Boolean.class); |
| 91 | + ArgumentCaptor<Boolean> logAppNameCapture = ArgumentCaptor.forClass(Boolean.class); |
| 92 | + ArgumentCaptor<String> appNameCapture = ArgumentCaptor.forClass(String.class); |
| 93 | + |
| 94 | + PowerMockito.whenNew(LogPrefixAppender.class).withArguments(clientInfoCapture.capture(), serverInfoCapture.capture(), logAppNameCapture.capture(), appNameCapture.capture()).thenReturn(stubAppender); |
| 95 | + |
| 96 | + LogAppender appender = JavaLogFactory.createLogAppender(true, false, true, testName.getMethodName()); |
| 97 | + |
| 98 | + Assert.assertEquals(stubAppender, appender); |
| 99 | + Assert.assertTrue(clientInfoCapture.getValue()); |
| 100 | + Assert.assertFalse(serverInfoCapture.getValue()); |
| 101 | + Assert.assertTrue(logAppNameCapture.getValue()); |
| 102 | + Assert.assertEquals(testName.getMethodName(), appNameCapture.getValue()); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | +} |
0 commit comments