Author: akarasulu Date: Sun Aug 6 12:15:12 2006 New Revision: 429179 URL: http://svn.apache.org/viewvc?rev=429179&view=rev Log: fix for DIRSERVER-645: Wrong search filter evaluation with AND operator and undefined operands Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizationService.java directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizingVisitor.java directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/MiscTest.java directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/SearchTest.java Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizationService.java URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizationService.java?rev=429179&r1=429178&r2=429179&view=diff ============================================================================== --- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizationService.java (original) +++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizationService.java Sun Aug 6 12:15:12 2006 @@ -198,6 +198,12 @@ } bnode.getChildren().remove( e.getUndefinedFilterNode() ); + + if ( bnode.getOperator() == BranchNode.AND ) + { + return new EmptyEnumeration(); + } + if ( bnode.getChildren().size() < 2 ) { filter = bnode.getChild(); @@ -211,10 +217,10 @@ BranchNode child = ( BranchNode ) filter; // if the remaining filter branch node has no children return an empty enumeration - if ( child.getChildren().size() == 0 ) + if ( child.getChildren().size() == 0 || child.get( "undefined" ) == Boolean.TRUE ) { - log - .warn( "Undefined branchnode filter without child nodes not evaluted at all. Returning empty enumeration." ); + log.warn( "Undefined branchnode filter without child nodes not " + + "evaluted at all. Returning empty enumeration." ); return new EmptyEnumeration(); } Modified: directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizingVisitor.java URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizingVisitor.java?rev=429179&r1=429178&r2=429179&view=diff ============================================================================== --- directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizingVisitor.java (original) +++ directory/trunks/apacheds/core/src/main/java/org/apache/directory/server/core/normalization/NormalizingVisitor.java Sun Aug 6 12:15:12 2006 @@ -69,6 +69,10 @@ public void visit( ExprNode node ) { + // ------------------------------------------------------------------- + // Handle PresenceNodes + // ------------------------------------------------------------------- + if ( node instanceof PresenceNode ) { PresenceNode pnode = ( PresenceNode ) node; @@ -87,6 +91,10 @@ return; } + // ------------------------------------------------------------------- + // Handle SimpleNodes + // ------------------------------------------------------------------- + if ( node instanceof SimpleNode ) { SimpleNode snode = ( SimpleNode ) node; @@ -133,6 +141,10 @@ return; } + // ------------------------------------------------------------------- + // Handle BranchNodes + // ------------------------------------------------------------------- + if ( node instanceof BranchNode ) { BranchNode bnode = ( BranchNode ) node; @@ -147,21 +159,33 @@ LeafNode ln = ( LeafNode ) child; if ( !ncn.isDefined( ln.getAttribute() ) ) { - if ( buf == null ) + if ( log.isWarnEnabled() ) { - buf = new StringBuffer(); + if ( buf == null ) + { + buf = new StringBuffer(); + } + else + { + buf.setLength( 0 ); + } + buf.append( "Removing leaf node based on undefined attribute '" ); + buf.append( ln.getAttribute() ); + buf.append( "' from filter." ); + log.warn( buf.toString() ); } - else - { - buf.setLength( 0 ); - } - buf.append( "Removing leaf node based on undefined attribute '" ); - buf.append( ln.getAttribute() ); - buf.append( "' from filter." ); - log.warn( buf.toString() ); // remove the child at ii bnode.getChildren().remove( child ); + + if ( bnode.getOperator() != BranchNode.AND ) + { + bnode.set( "undefined", Boolean.TRUE ); + } + else + { + bnode.set( "undefined", Boolean.FALSE ); + } ii--; // decrement so we can evaluate next child which has shifted to ii continue; } @@ -178,6 +202,14 @@ catch( UndefinedFilterAttributeException e ) { bnode.getChildren().remove( ii ); + if ( bnode.getOperator() != BranchNode.AND ) + { + bnode.set( "undefined", Boolean.TRUE ); + } + else + { + bnode.set( "undefined", Boolean.FALSE ); + } ii--; continue; } @@ -194,15 +226,16 @@ { BranchNode child = ( BranchNode ) unknown; - // remove child branch node that has no children left - if ( child.getChildren().size() == 0 ) + // remove child branch node that has no children left or + // a child branch node that is undefined as a result of removals + if ( child.getChildren().size() == 0 || child.get( "undefined" ) == Boolean.TRUE ) { // remove the child at ii bnode.getChildren().remove( child ); ii--; // decrement so we can evaluate next child which has shifted to ii continue; } - + // now for AND & OR nodes with a single child left replace them // with their child at the same index they AND/OR node was in if ( child.getChildren().size() == 1 && child.getOperator() != BranchNode.NOT ) Modified: directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/MiscTest.java URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/MiscTest.java?rev=429179&r1=429178&r2=429179&view=diff ============================================================================== --- directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/MiscTest.java (original) +++ directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/MiscTest.java Sun Aug 6 12:15:12 2006 @@ -319,10 +319,10 @@ e = sysRoot.search( "", "(!(bogusAttribute=abc123))", cons ); assertNotNull( e ); assertEquals( e.getClass(), EmptyEnumeration.class ); - e = sysRoot.search( "", "(& (bogusAttribute=abc123)(bogusAttribute=abc123) )", cons ); + e = sysRoot.search( "", "(| (bogusAttribute=abc123)(bogusAttribute=abc123) )", cons ); assertNotNull( e ); assertEquals( e.getClass(), EmptyEnumeration.class ); - e = sysRoot.search( "", "(& (bogusAttribute=abc123)(ou=abc123) )", cons ); + e = sysRoot.search( "", "(| (bogusAttribute=abc123)(ou=abc123) )", cons ); assertNotNull( e ); assertFalse( e.getClass().equals( EmptyEnumeration.class ) ); Modified: directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/SearchTest.java URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/SearchTest.java?rev=429179&r1=429178&r2=429179&view=diff ============================================================================== --- directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/SearchTest.java (original) +++ directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/SearchTest.java Sun Aug 6 12:15:12 2006 @@ -352,5 +352,26 @@ fail( e.getMessage() ); } } + + /** + * Tests for + * DIRSERVER-645<\a>: Wrong search filter evaluation with AND + * operator and undefined operands. + */ + public void testUndefinedAvaInBranchFilters() throws Exception + { + // create additional entry + Attributes attributes = this.getPersonAttributes( "Bush", "Kate Bush" ); + ctx.createSubcontext( "cn=Kate Bush", attributes ); + + // ------------------------------------------------------------------- + Set results = search( "(|(sn=Bush)(numberOfOctaves=4))" ); + assertEquals( "returned size of results", 1, results.size() ); + assertTrue( "contains cn=Kate Bush", results.contains( "cn=Kate Bush" ) ); + + // if numberOfOctaves is undefined then this whole filter is undefined + results = search( "(&(sn=Bush)(numberOfOctaves=4))" ); + assertEquals( "returned size of results", 0, results.size() ); + } }