From 54a7463e9479ae2b7aaa771179b96fc7e53f345c Mon Sep 17 00:00:00 2001 From: Mickey Zarev Date: Wed, 31 Jul 2024 13:30:12 +0100 Subject: [PATCH] Update the logging properties to opt-out of the prefix events #844 seventh iteration --- .../configuration/EsapiPropertyManager.java | 6 +- .../EsapiPropertyManagerTest.java | 78 +++++++ .../StandardEsapiPropertyLoaderTest.java | 192 ++++++++++++++++++ .../XmlEsapiPropertyLoaderTest.java | 183 +++++++++++++++++ 4 files changed, 454 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/owasp/esapi/configuration/EsapiPropertyManager.java b/src/main/java/org/owasp/esapi/configuration/EsapiPropertyManager.java index 999309500..2a7b2cecd 100644 --- a/src/main/java/org/owasp/esapi/configuration/EsapiPropertyManager.java +++ b/src/main/java/org/owasp/esapi/configuration/EsapiPropertyManager.java @@ -82,11 +82,7 @@ public Boolean getBooleanProp(String propertyName) throws ConfigurationException @Override public Boolean getBooleanProp(String propertyName, Boolean defaultValue) { for (AbstractPrioritizedPropertyLoader loader : loaders) { - try { - return loader.getBooleanProp(propertyName); - } catch (ConfigurationException e) { - return defaultValue; - } + return loader.getBooleanProp(propertyName, defaultValue); } return defaultValue; } diff --git a/src/test/java/org/owasp/esapi/configuration/EsapiPropertyManagerTest.java b/src/test/java/org/owasp/esapi/configuration/EsapiPropertyManagerTest.java index 507f33ce1..dcf98c40c 100644 --- a/src/test/java/org/owasp/esapi/configuration/EsapiPropertyManagerTest.java +++ b/src/test/java/org/owasp/esapi/configuration/EsapiPropertyManagerTest.java @@ -288,6 +288,46 @@ public void testBooleanPropFoundInLoader() { assertEquals(expectedPropertyValue, propertyValue); } + + @Test + public void testBooleanPropFoundInLoaderWithDefaultValueTrue() { + // given + System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), xmlFilename1); + String propertyKey = "boolean_property"; + boolean expectedPropertyValue = true; + + // when + try { + testPropertyManager = new EsapiPropertyManager(); + } catch (IOException e) { + fail(e.getMessage()); + } + boolean propertyValue = testPropertyManager.getBooleanProp(propertyKey, true); + + // then + assertEquals(expectedPropertyValue, propertyValue); + } + + @Test + public void testBooleanPropFoundInLoaderWithDefaultValueFalse() { + // given + System.setProperty(EsapiConfiguration.DEVTEAM_ESAPI_CFG.getConfigName(), xmlFilename1); + String propertyKey = "boolean_property"; + boolean expectedPropertyValue = true; + + // when + try { + testPropertyManager = new EsapiPropertyManager(); + } catch (IOException e) { + fail(e.getMessage()); + } + boolean propertyValue = testPropertyManager.getBooleanProp(propertyKey, false); + + // then + assertEquals(expectedPropertyValue, propertyValue); + } + + @Test(expected = ConfigurationException.class) public void testBooleanPropertyNotFoundByLoaderAndThrowException() { // given @@ -304,6 +344,44 @@ public void testBooleanPropertyNotFoundByLoaderAndThrowException() { // then expect exception } + @Test + public void testBooleanPropertyNotFoundByLoaderWithDefaultValueTrue() { + // given + String propertyKey = "non.existing.property"; + boolean expectedPropertyValue = true; + + // when + try { + testPropertyManager = new EsapiPropertyManager(); + } catch (IOException e) { + fail(e.getMessage()); + } + + boolean propertyValue = testPropertyManager.getBooleanProp(propertyKey, true); + + // then + assertEquals(expectedPropertyValue, propertyValue); + } + + @Test + public void testBooleanPropertyNotFoundByLoaderWithDefaultValueFalse() { + // given + String propertyKey = "non.existing.property"; + boolean expectedPropertyValue = false; + + // when + try { + testPropertyManager = new EsapiPropertyManager(); + } catch (IOException e) { + fail(e.getMessage()); + } + + boolean propertyValue = testPropertyManager.getBooleanProp(propertyKey, false); + + // then + assertEquals(expectedPropertyValue, propertyValue); + } + @Test public void testByteArrayPropFoundInLoader() { // given diff --git a/src/test/java/org/owasp/esapi/configuration/StandardEsapiPropertyLoaderTest.java b/src/test/java/org/owasp/esapi/configuration/StandardEsapiPropertyLoaderTest.java index 080644642..77fe1643b 100644 --- a/src/test/java/org/owasp/esapi/configuration/StandardEsapiPropertyLoaderTest.java +++ b/src/test/java/org/owasp/esapi/configuration/StandardEsapiPropertyLoaderTest.java @@ -239,6 +239,48 @@ public void testGetBooleanProp() { assertEquals(expectedValue, value); } + @Test + public void testGetBooleanPropWithDefaultValueTrue() { + // given + String filename = "src" + File.separator + "test" + File.separator + "resources" + File.separator + + "esapi" + File.separator + "ESAPI-test.properties"; + int priority = 1; + String propertyKey = "boolean_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(propertyKey,true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testGetBooleanPropWithDefaultValueFalse() { + // given + String filename = "src" + File.separator + "test" + File.separator + "resources" + File.separator + + "esapi" + File.separator + "ESAPI-test.properties"; + int priority = 1; + String propertyKey = "boolean_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(propertyKey,false); + + // then + assertEquals(expectedValue, value); + } + @Test public void testGetBooleanYesProperty() { // given @@ -257,6 +299,42 @@ public void testGetBooleanYesProperty() { assertEquals(expectedValue, value); } + @Test + public void testGetBooleanYesPropertyWithDefaultValueTrue() { + // given + String key = "boolean_yes_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key,true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testGetBooleanYesPropertyWithDefaultValueFalse() { + // given + String key = "boolean_yes_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key,false); + + // then + assertEquals(expectedValue, value); + } + @Test public void testGetBooleanNoProperty() { // given @@ -275,6 +353,42 @@ public void testGetBooleanNoProperty() { assertEquals(expectedValue, value); } + @Test + public void testGetBooleanNoPropertyWithDefaultValueTrue() { + // given + String key = "boolean_no_property"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testGetBooleanNoPropertyWithDefaultValueFalse() { + // given + String key = "boolean_no_property"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, false); + + // then + assertEquals(expectedValue, value); + } + @Test(expected = ConfigurationException.class) public void testBooleanPropertyNotFound() throws ConfigurationException { // given @@ -294,6 +408,48 @@ public void testBooleanPropertyNotFound() throws ConfigurationException { // then expect exception } + @Test + public void testBooleanPropertyNotFoundWithDefaultValueTrue() { + // given + String filename = "src" + File.separator + "test" + File.separator + "resources" + File.separator + + "esapi" + File.separator + "ESAPI-test.properties"; + int priority = 1; + String propertyKey = "non-existing-key"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(propertyKey, true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testBooleanPropertyNotFoundWithDefaultValueFalse() { + // given + String filename = "src" + File.separator + "test" + File.separator + "resources" + File.separator + + "esapi" + File.separator + "ESAPI-test.properties"; + int priority = 1; + String propertyKey = "non-existing-key"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(propertyKey, false); + + // then + assertEquals(expectedValue, value); + } + @Test(expected = ConfigurationException.class) public void testIncorrectBooleanPropertyType() throws ConfigurationException { // given @@ -310,6 +466,42 @@ public void testIncorrectBooleanPropertyType() throws ConfigurationException { // then expect exception } + @Test + public void testIncorrectBooleanPropertyTypeWithDefaultValueTrue() { + // given + String key = "invalid_boolean_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testIncorrectBooleanPropertyTypeWithDefaultValueFalse() { + // given + String key = "invalid_boolean_property"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new StandardEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, false); + + // then + assertEquals(expectedValue, value); + } + @Test public void testGetByteArrayProp() { // given diff --git a/src/test/java/org/owasp/esapi/configuration/XmlEsapiPropertyLoaderTest.java b/src/test/java/org/owasp/esapi/configuration/XmlEsapiPropertyLoaderTest.java index 6aecec533..f52be60e9 100644 --- a/src/test/java/org/owasp/esapi/configuration/XmlEsapiPropertyLoaderTest.java +++ b/src/test/java/org/owasp/esapi/configuration/XmlEsapiPropertyLoaderTest.java @@ -254,6 +254,42 @@ public void testGetBooleanProp() { assertEquals(expectedValue, value); } + @Test + public void testGetBooleanPropWithDefaultValueTrue() { + // given + String key = "boolean_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testGetBooleanPropWithDefaultValueFalse() { + // given + String key = "boolean_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, false); + + // then + assertEquals(expectedValue, value); + } + @Test public void testGetBooleanYesProperty() { // given @@ -272,6 +308,42 @@ public void testGetBooleanYesProperty() { assertEquals(expectedValue, value); } + @Test + public void testGetBooleanYesPropertyWithDefaultValueTrue() { + // given + String key = "boolean_yes_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testGetBooleanYesPropertyWithDefaultValueFalse() { + // given + String key = "boolean_yes_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, false); + + // then + assertEquals(expectedValue, value); + } + @Test public void testGetBooleanNoProperty() { // given @@ -290,6 +362,42 @@ public void testGetBooleanNoProperty() { assertEquals(expectedValue, value); } + @Test + public void testGetBooleanNoPropertyWithDefaultValueTrue() { + // given + String key = "boolean_no_property"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testGetBooleanNoPropertyWithDefaultValueFalse() { + // given + String key = "boolean_no_property"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, false); + + // then + assertEquals(expectedValue, value); + } + @Test(expected = ConfigurationException.class) public void testBooleanPropertyNotFound() throws ConfigurationException { // given @@ -306,6 +414,45 @@ public void testBooleanPropertyNotFound() throws ConfigurationException { // then expect exception } + @Test + public void testBooleanPropertyNotFoundWithDefaultValueTrue() { + // given + String key = "non-existing-key"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, true); + + // then + assertEquals(expectedValue, value); + + + // then expect exception + } + + @Test + public void testBooleanPropertyNotFoundWithDefaultValueFalse() { + // given + String key = "non-existing-key"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, false); + + // then + assertEquals(expectedValue, value); + } + @Test(expected = ConfigurationException.class) public void testIncorrectBooleanPropertyType() throws ConfigurationException { // given @@ -322,6 +469,42 @@ public void testIncorrectBooleanPropertyType() throws ConfigurationException { // then expect exception } + @Test + public void testIncorrectBooleanPropertyTypeWithDefaultValueTrue() { + // given + String key = "invalid_boolean_property"; + boolean expectedValue = true; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, true); + + // then + assertEquals(expectedValue, value); + } + + @Test + public void testIncorrectBooleanPropertyTypeWithDefaultValueFalse() { + // given + String key = "invalid_boolean_property"; + boolean expectedValue = false; + + // when + try { + testPropertyLoader = new XmlEsapiPropertyLoader(filename, priority); + } catch ( IOException e ) { + fail( e.getMessage() ); + } + boolean value = testPropertyLoader.getBooleanProp(key, false); + + // then + assertEquals(expectedValue, value); + } + @Test public void testGetByteArrayProp() { // given