PropertyAuditFilter implements AuditFilter { @Override public boolean accept(String rootPath, Map<String, Serializable> auditMap) { String[] root = splitPath(rootPath); String rootProperty = getPropertyName(PROPERY_NAME_PREFIX, getPropertyName(root)); String defaultRootProperty = getDefaultRootProperty(root); if ("true".equalsIgnoreCase(getProperty(rootProperty, defaultRootProperty, ENABLED))) { for (Map.Entry<String, Serializable> entry : auditMap.entrySet()) { Serializable value = entry.getValue(); if (value == null) { value = "null"; } String stringValue = (value instanceof String) ? (String)value : value.toString(); String[] key = splitPath(entry.getKey()); String propertyValue = getProperty(rootProperty, defaultRootProperty, key); if (!acceptValue(stringValue, propertyValue, rootProperty, key)) { if (logger.isDebugEnabled()) { logger.debug("Rejected \n\t "+rootPath+'/'+entry.getKey()+"="+stringValue+ "\n\t"+getPropertyName(rootProperty, getPropertyName(key))+"="+propertyValue); } return false; } } } return true; }  void setProperties(Properties properties); @Override boolean accept(String rootPath, Map<String, Serializable> auditMap);  }
@Test public void testNoFilterIfNoProperties() { boolean actual = filter.accept(rootPath, auditMap); assertTrue("Filter should only run if properties are set.", actual); }
@Test public void testNoRegexOnValue() { properties.put("audit.filter.root.action.enabled", "true"); boolean actual = filter.accept(rootPath, auditMap); assertTrue("Value should have been accepted.", actual); }
@Test public void testRegexOnValue() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "value"); boolean actual = filter.accept(rootPath, auditMap); assertTrue("Value should have been accepted.", actual); }
@Test public void testRegexOnBadValue() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "~value"); boolean actual = filter.accept(rootPath, auditMap); assertFalse("Value should have been rejected.", actual); }
@Test public void testNullValue() { auditMap.put("name", null); properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "null"); boolean actual = filter.accept(rootPath, auditMap); assertTrue("A null value should match null", actual); }
@Test public void testNullStringValue() { auditMap.put("name", "null"); properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "null"); boolean actual = filter.accept(rootPath, auditMap); assertTrue("A null value should match null", actual); }
@Test public void testNonStringValue() { LinkedHashSet<Integer> value = new LinkedHashSet<Integer>(); value.add(Integer.valueOf(1)); value.add(Integer.valueOf(2)); value.add(Integer.valueOf(3)); auditMap.put("name", value); properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "\\[1, 2, 3\\]"); boolean actual = filter.accept(rootPath, auditMap); assertTrue("The check should have worked on the value.toString().", actual); }
@Test public void testZeroLengthRegex() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", ""); boolean actual = filter.accept(rootPath, auditMap); assertTrue("Should match any values just like having no property", actual); }
@Test public void testDefaultActionUsedAsFallback() { properties.put("audit.filter.root.default.enabled", "true"); properties.put("audit.filter.root.default.name", "~value"); boolean actual = filter.accept(rootPath, auditMap); assertFalse("The 'default' fallback action should have been used to " + "enable the filter and reject the value.", actual); }
@Test public void testRedirect() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "$anotherProperty"); properties.put("anotherProperty", "$theFinalProperty"); properties.put("theFinalProperty", "~value"); boolean actual = filter.accept(rootPath, auditMap); assertFalse("Redirected properties should have rejected the value.", actual); }
@Test public void testMultipleRegExp() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "beGood;~b.*;.*"); auditMap.put("name", "beGood"); assertTrue("Should match 1st regex", filter.accept(rootPath, auditMap)); auditMap.put("name", "bad"); assertFalse("Should match 2nd regex", filter.accept(rootPath, auditMap)); auditMap.put("name", "value"); assertTrue("Should match 3rd regex", filter.accept(rootPath, auditMap)); }
@Test public void testMultipleRegExpWithNoCatchAll() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "beGood;~b.*"); auditMap.put("name", "value"); assertFalse("Should match nothing", filter.accept(rootPath, auditMap)); }
@Test public void testEscapedSemicolon() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "value\\\\;value"); auditMap.put("name", "value\\;value"); assertTrue("Should match 1st regex", filter.accept(rootPath, auditMap)); }
@Test public void testEscapedRedirect() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "\\$"); auditMap.put("name", ""); assertTrue("Should match only zero length values", filter.accept(rootPath, auditMap)); }
@Test public void testEscapedNot() { properties.put("audit.filter.root.action.enabled", "true"); properties.put("audit.filter.root.action.name", "\\~.*"); auditMap.put("name", "~good"); assertTrue("Should match any value starting with '~'.", filter.accept(rootPath, auditMap)); }