Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 33249 invoked from network); 9 Sep 2005 21:44:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 9 Sep 2005 21:44:04 -0000 Received: (qmail 70805 invoked by uid 500); 9 Sep 2005 21:44:00 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 70748 invoked by uid 500); 9 Sep 2005 21:44:00 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 70670 invoked by uid 99); 9 Sep 2005 21:43:59 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Sep 2005 14:43:59 -0700 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Fri, 09 Sep 2005 14:43:59 -0700 Received: (qmail 32131 invoked by uid 65534); 9 Sep 2005 21:43:45 -0000 Message-ID: <20050909214345.32129.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r279887 [12/15] - in /directory/shared/ldap/branches/elecharny-cleanup/apache2-provider: ./ conf/ perfs/ perfs/org/ perfs/org/apache/ perfs/org/apache/asn1new/ perfs/org/apache/asn1new/ber/ src/ src/java/ src/java/main/ src/java/main/org/ s... Date: Fri, 09 Sep 2005 21:43:24 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/AttributeValueAssertionFilter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/AttributeValueAssertionFilter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/AttributeValueAssertionFilter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/AttributeValueAssertionFilter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,206 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; + +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.tlv.Length; +import org.apache.asn1new.ber.tlv.Value; +import org.apache.asn1new.ldap.codec.LdapConstants; +import org.apache.asn1new.ldap.pojo.AttributeValueAssertion; + + +/** + * Object to store the filter. A filter is seen as a tree with a root. + * + * @author Apache Directory Project + */ +public class AttributeValueAssertionFilter extends Filter +{ + //~ Instance fields ---------------------------------------------------------------------------- + + /** The assertion. */ + private AttributeValueAssertion assertion; + + /** The filter type */ + private int filterType; + + /** The attributeValueAssertion length */ + private transient int avaLength; + + //~ Constructors ------------------------------------------------------------------------------- + + /** + * The constructor. + * @param filterType DOCUMENT ME! + */ + public AttributeValueAssertionFilter( int filterType ) + { + this.filterType = filterType; + } + + //~ Methods ------------------------------------------------------------------------------------ + + /** + * Get the assertion + * + * @return Returns the assertion. + */ + public AttributeValueAssertion getAssertion() + { + return assertion; + } + + /** + * Set the assertion + * + * @param assertion The assertion to set. + */ + public void setAssertion( AttributeValueAssertion assertion ) + { + this.assertion = assertion; + } + + /** + * Get the filter type + * + * @return Returns the filterType. + */ + public int getFilterType() + { + return filterType; + } + + /** + * Set the filter type + * + * @param filterType The filterType to set. + */ + public void setFilterType( int filterType ) + { + this.filterType = filterType; + } + + /** + * Compute the AttributeValueFilter length + * + * AttributeValueFilter : + * + * 0xA(3, 5, 6, 8) L1 + * | + * +--> 0x04 L2 attributeDesc + * +--> 0x04 L3 assertionValue + * + * + * L2 = Length(attributeDesc) + * L3 = Length(assertionValue) + * L1 = 1 + Length(L2) + L2 + * + 1 + Length(L3) + L3 + * + * Length(AttributeValueFilter) = Length(0xA?) + Length(L1) + * + 1 + Length(L2) + L2 + * + 1 + Length(L3) + L3 + * + */ + public int computeLength() + { + avaLength = 0; + int attributeDescLength = assertion.getAttributeDesc().length(); + + avaLength = 1 + Length.getNbBytes( attributeDescLength ) + attributeDescLength; + + int assertionValueLength = assertion.getAssertionValue().getLength(); + + avaLength += 1 + Length.getNbBytes( assertionValueLength ) + assertionValueLength; + + return 1 + Length.getNbBytes( avaLength ) + avaLength; + } + + /** + * Encode the AttributeValueAssertion Filters to a PDU. The + * following filters are to be encoded : + * - equality match + * - greater or equal + * - less or equal + * - approx match + * + * AttributeValueAssertion filters : + * + * 0xA[3, 5, 6, 8] LL + * 0x04 LL attributeDesc + * 0x04 LL assertionValue + * + * @param buffer The buffer where to put the PDU + * @return The PDU. + */ + public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException + { + if (buffer == null) + { + throw new EncoderException("Cannot put a PDU in a null buffer !"); + } + + try + { + // The AttributeValueAssertion Tag + switch (filterType) + { + case LdapConstants.EQUALITY_MATCH_FILTER : + buffer.put( (byte)LdapConstants.EQUALITY_MATCH_FILTER_TAG ); + break; + + case LdapConstants.LESS_OR_EQUAL_FILTER : + buffer.put( (byte)LdapConstants.LESS_OR_EQUAL_FILTER_TAG ); + break; + + case LdapConstants.GREATER_OR_EQUAL_FILTER : + buffer.put( (byte)LdapConstants.GREATER_OR_EQUAL_FILTER_TAG ); + break; + + case LdapConstants.APPROX_MATCH_FILTER : + buffer.put( (byte)LdapConstants.APPROX_MATCH_FILTER_TAG ); + break; + } + + buffer.put( Length.getBytes( avaLength ) ); + } + catch ( BufferOverflowException boe ) + { + throw new EncoderException("The PDU buffer size is too small !"); + } + + // The attribute desc + Value.encode( buffer, assertion.getAttributeDesc() ); + + // The assertion desc + Value.encode( buffer, assertion.getAssertionValue() ); + + return buffer; + } + + /** + * Return a string compliant with RFC 2254 representing an item filter + * + * @return The item filter string + */ + public String toString() + { + return assertion.toStringRFC2254( filterType ); + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/AttributeValueAssertionFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ConnectorFilter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ConnectorFilter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ConnectorFilter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ConnectorFilter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,166 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import org.apache.asn1.codec.DecoderException; +import org.apache.asn1.codec.EncoderException; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Iterator; + + +/** + * This Filter abstract class is used to store a set of filters used by OR/AND/NOT + * filters. + * + * @author Apache Directory Project + */ +public abstract class ConnectorFilter extends Filter +{ + //~ Instance fields ---------------------------------------------------------------------------- + + /** The set of filters used by And/Or filters */ + protected ArrayList filterSet; + + /** The filters length */ + protected transient int filtersLength; + + //~ Constructors ------------------------------------------------------------------------------- + + /** + * The constructor. We wont initialize the ArrayList as it may not be used. + */ + public ConnectorFilter() + { + } + + //~ Methods ------------------------------------------------------------------------------------ + + /** + * Add a new Filter to the list. + * @param filter The filter to add + */ + public void addFilter( Filter filter ) throws DecoderException + { + + if ( this.filterSet == null ) + { + this.filterSet = new ArrayList(); + } + + this.filterSet.add( filter ); + } + + /** + * Get the list of filters stored in the composite filter + * + * @return And array of filters + */ + public ArrayList getFilterSet() + { + return filterSet; + } + + /** + * Compute the ConnectorFilter length + * + * Length(ConnectorFilter) = sum(filterSet.computeLength()) + * + */ + public int computeLength() + { + int connectorFilterLength = 0; + + if ( ( filterSet != null ) && ( filterSet.size() != 0 ) ) + { + Iterator filterIterator = filterSet.iterator(); + + while ( filterIterator.hasNext() ) + { + Filter filter = (Filter)filterIterator.next(); + + connectorFilterLength += filter.computeLength(); + } + } + + return connectorFilterLength; + } + + /** + * Encode the ConnectorFilter message to a PDU. + * + * ConnectorFilter : + * + * filter.encode() + * ... + * filter.encode() + * + * @param buffer The buffer where to put the PDU + * @return The PDU. + */ + public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException + { + if (buffer == null) + { + throw new EncoderException("Cannot put a PDU in a null buffer !"); + } + + // encode each filter + if ( ( filterSet != null ) && ( filterSet.size() != 0 ) ) + { + Iterator filterIterator = filterSet.iterator(); + + while ( filterIterator.hasNext() ) + { + Filter filter = (Filter)filterIterator.next(); + + filter.encode( buffer ); + } + } + + return buffer; + } + + /** + * Return a string compliant with RFC 2254 representing a composite + * filter, one of AND, OR and NOT + * + * @return The composite filter string + */ + public String toString() + { + + StringBuffer sb = new StringBuffer(); + + if ( ( filterSet != null ) && ( filterSet.size() != 0 ) ) + { + + Iterator filterIterator = filterSet.iterator(); + + while ( filterIterator.hasNext() ) + { + + Filter filter = ( Filter ) filterIterator.next(); + + sb.append( '(' ).append( filter.toString() ).append( ')' ); + } + } + + return sb.toString(); + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ConnectorFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ExtensibleMatchFilter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ExtensibleMatchFilter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ExtensibleMatchFilter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ExtensibleMatchFilter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,336 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; + +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.tlv.Length; +import org.apache.asn1new.ber.tlv.UniversalTag; +import org.apache.asn1new.ber.tlv.Value; +import org.apache.asn1new.primitives.OctetString; +import org.apache.asn1new.ldap.codec.LdapConstants; +import org.apache.asn1new.ldap.codec.primitives.LdapString; + + +/** + * The search request filter Matching Rule assertion + * + * + * @author Apache Directory Project + */ +public class ExtensibleMatchFilter extends Filter +{ + //~ Instance fields ---------------------------------------------------------------------------- + + /** The expected lenth of the Matching Rule Assertion */ + private transient int expectedMatchingRuleLength; + + /** Matching rule */ + private LdapString matchingRule; + + /** Matching rule type */ + private LdapString type; + + /** Matching rule value */ + private OctetString matchValue; + + /** The dnAttributes flag */ + private boolean dnAttributes; + + /** The extensible match length */ + private transient int extensibleMatchLength; + + /** The matching Rule Assertion Length */ + private transient int matchingRuleAssertionLength; + + //~ Constructors ------------------------------------------------------------------------------- + + /** + * Creates a new ExtensibleMatchFilter object. + * The dnAttributes flag defaults to false. + */ + public ExtensibleMatchFilter() + { + dnAttributes = false; + } + + //~ Methods ------------------------------------------------------------------------------------ + + /** + * Get the dnAttributes flag + * + * @return Returns the dnAttributes. + */ + public boolean isDnAttributes() + { + return dnAttributes; + } + + /** + * Set the dnAttributes flag + * + * @param dnAttributes The dnAttributes to set. + */ + public void setDnAttributes( boolean dnAttributes ) + { + this.dnAttributes = dnAttributes; + } + + /** + * Get the matchingRule + * + * @return Returns the matchingRule. + */ + public LdapString getMatchingRule() + { + return matchingRule; + } + + /** + * Set the matchingRule + * + * @param matchingRule The matchingRule to set. + */ + public void setMatchingRule( LdapString matchingRule ) + { + this.matchingRule = matchingRule; + } + + /** + * Get the matchValue + * + * @return Returns the matchValue. + */ + public OctetString getMatchValue() + { + return matchValue; + } + + /** + * Set the matchValue + * + * @param matchValue The matchValue to set. + */ + public void setMatchValue( OctetString matchValue ) + { + this.matchValue = matchValue; + } + + /** + * Get the type + * + * @return Returns the type. + */ + public LdapString getType() + { + return type; + } + + /** + * Set the type + * + * @param type The type to set. + */ + public void setType( LdapString type ) + { + this.type = type; + } + + /** + * get the expectedMatchingRuleLength + * + * @return Returns the expectedMatchingRuleLength. + */ + public int getExpectedMatchingRuleLength() + { + return expectedMatchingRuleLength; + } + + /** + * Set the expectedMatchingRuleLength + * + * @param expectedMatchingRuleLength The expectedMatchingRuleLength to set. + */ + public void setExpectedMatchingRuleLength( int expectedMatchingRuleLength ) + { + this.expectedMatchingRuleLength = expectedMatchingRuleLength; + } + + /** + * Compute the ExtensibleMatchFilter length + * + * ExtensibleMatchFilter : + * + * 0xA9 L1 + * | + * +--> 0x30 L2 + * | + * [+--> 0x81 L3 matchingRule] + * [+--> 0x82 L4 type] + * [+--> 0x83 L5 matchValue] + * [+--> 0x01 0x01 dnAttributes] + * + */ + public int computeLength() + { + if ( matchingRule != null ) + { + matchingRuleAssertionLength = 1 + Length.getNbBytes( matchingRule.getLength() ) + matchingRule.getLength(); + } + + if ( type != null ) + { + matchingRuleAssertionLength += 1 + Length.getNbBytes( type.getLength() ) + type.getLength(); + } + + if ( matchValue != null ) + { + matchingRuleAssertionLength += 1 + Length.getNbBytes( matchValue.getLength() ) + matchValue.getLength(); + } + + if ( dnAttributes == true ) + { + matchingRuleAssertionLength += 1 + 1 + 1; + } + + extensibleMatchLength = 1 + Length.getNbBytes( matchingRuleAssertionLength ) + matchingRuleAssertionLength; + + return 1 + Length.getNbBytes( extensibleMatchLength ) + extensibleMatchLength; + } + + /** + * Encode the ExtensibleMatch Filters to a PDU. + * + * ExtensibleMatch filter : + * + * 0xA9 LL + * 0x30 LL matchingRuleAssertion + * | 0x81 LL matchingRule + * | / | 0x82 LL Type + * | / | /0x83 LL matchValue + * +--+ +-+ + * | \ \ + * | \ 0x83 LL MatchValue + * | 0x82 LL type + * | 0x83 LL matchValue + * +--[0x84 0x01 dnAttributes] + * + * @param buffer The buffer where to put the PDU + * @return The PDU. + */ + public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException + { + if (buffer == null) + { + throw new EncoderException("Cannot put a PDU in a null buffer !"); + } + + try + { + // The ExtensibleMatch Tag + buffer.put( (byte)LdapConstants.EXTENSIBLE_MATCH_FILTER_TAG ); + buffer.put( Length.getBytes( extensibleMatchLength ) ); + + // The MatchingRuleAssertion sequence tag + buffer.put( UniversalTag.SEQUENCE_TAG ); + buffer.put( Length.getBytes( matchingRuleAssertionLength ) ); + + if ( ( matchingRule == null ) && ( type == null ) ) + { + throw new EncoderException("Cannot have a null matching rule and a null type"); + } + + // The matching rule + if ( matchingRule != null ) + { + buffer.put( (byte)LdapConstants.SEARCH_MATCHING_RULE_TAG ); + buffer.put( Length.getBytes( matchingRule.getLength() ) ); + buffer.put( matchingRule.getData() ); + } + + // The type + if ( type != null ) + { + buffer.put( (byte)LdapConstants.SEARCH_TYPE_TAG ); + buffer.put( Length.getBytes( type.getLength() ) ); + buffer.put( type.getData() ); + } + + // The match value + if ( matchValue != null ) + { + buffer.put( (byte)LdapConstants.SEARCH_MATCH_VALUE_TAG ); + buffer.put( Length.getBytes( matchValue.getLength() ) ); + buffer.put( matchValue.getValue() ); + } + + // The dnAttributes flag, if true only + if ( dnAttributes == true ) + { + buffer.put( (byte)LdapConstants.DN_ATTRIBUTES_FILTER_TAG ); + buffer.put( (byte)1 ); + buffer.put( Value.TRUE_VALUE ); + } + } + catch ( BufferOverflowException boe ) + { + throw new EncoderException("The PDU buffer size is too small !"); + } + + return buffer; + } + + /** + * Return a String representing an extended filter as of RFC 2254 + * + * @return An Extened Filter String + */ + public String toString() + { + + StringBuffer sb = new StringBuffer(); + + if ( type != null ) + { + sb.append( type.toString() ); + } + + if ( dnAttributes == true ) + { + sb.append( ":dn" ); + } + + if ( matchingRule == null ) + { + + if ( type == null ) + { + return "Extended Filter wrong syntax"; + } + } + else + { + sb.append( ':' ).append( matchingRule.toString() ); + } + + sb.append( ":=" ).append( matchValue.toString() ); + + return sb.toString(); + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/ExtensibleMatchFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/Filter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/Filter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/Filter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/Filter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import org.apache.asn1new.Asn1Object; + + +/** + * An abstract Asn1Object used to store the filter. A filter is seen as a tree with a root. + * This class does nothing, it's just the root of all the different filters. + * + * + * @author Apache Directory Project + */ +public abstract class Filter extends Asn1Object +{ + //~ Constructors ------------------------------------------------------------------------------- + + /** + * The constructor. + */ + public Filter() + { + } + + //~ Methods ------------------------------------------------------------------------------------ +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/Filter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/NotFilter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/NotFilter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/NotFilter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/NotFilter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,147 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; + +import org.apache.asn1.codec.DecoderException; +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.tlv.Length; +import org.apache.asn1new.ldap.codec.LdapConstants; + +/** + * Not Filter Object to store the Not filter. + * + * @author Apache Directory Project + */ +public class NotFilter extends ConnectorFilter +{ + //~ Methods ------------------------------------------------------------------------------------ + + /** + * The constructor. + */ + public NotFilter() + { + } + + /** + * Subclass the addFilterMethod, as this is specific for a NotFilter + * (we cannot have more than one elements). + * @param Filter The Filter to add + */ + public void addFilter( Filter filter ) throws DecoderException + { + if ( filterSet != null ) + { + throw new DecoderException("Cannot have more than one Filter within a Not Filter"); + } + + super.addFilter( filter ); + } + + /** + * Get the NotFilter + * + * @return Returns the notFilter. + */ + public Filter getNotFilter() + { + return (Filter)filterSet.get(0); + } + + /** + * Set the NotFilter + * + * @param not The notFilter to set. + */ + public void setNotFilter( Filter notFilter ) throws DecoderException + { + if ( filterSet != null ) + { + throw new DecoderException("Cannot have more than one Filter within a Not Filter"); + } + + super.addFilter( notFilter ); + } + + /** + * Compute the NotFilter length + * + * NotFilter : + * + * 0xA2 L1 super.computeLength() + * + * Length(NotFilter) = Length(0xA2) + Length(super.computeLength()) + super.computeLength() + * + */ + public int computeLength() + { + filtersLength = super.computeLength(); + + return 1 + Length.getNbBytes( filtersLength ) + filtersLength; + } + + /** + * Encode the NotFilter message to a PDU. + * + * NotFilter : + * + * 0xA2 LL + * filter.encode() + * + * @param buffer The buffer where to put the PDU + * @return The PDU. + */ + public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException + { + if (buffer == null) + { + throw new EncoderException("Cannot put a PDU in a null buffer !"); + } + + try + { + // The NotFilter Tag + buffer.put( (byte)LdapConstants.NOT_FILTER_TAG ); + buffer.put( Length.getBytes( filtersLength ) ); + } + catch ( BufferOverflowException boe ) + { + throw new EncoderException("The PDU buffer size is too small !"); + } + + super.encode( buffer ); + + return buffer; + } + + /** + * Return a string compliant with RFC 2254 representing a NOT filter + * + * @return The NOT filter string + */ + public String toString() + { + StringBuffer sb = new StringBuffer(); + + sb.append('!').append(super.toString()); + + return sb.toString(); + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/NotFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/OrFilter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/OrFilter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/OrFilter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/OrFilter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,121 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; +import java.util.ArrayList; + +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.tlv.Length; +import org.apache.asn1new.ldap.codec.LdapConstants; + + +/** + * Or Filter Object to store the Or filter. + * + * @author Apache Directory Project + */ +public class OrFilter extends ConnectorFilter +{ + //~ Constructors ------------------------------------------------------------------------------- + + /** + * The constructor. We wont initialize the ArrayList as they may not be used. + */ + public OrFilter() + { + } + + //~ Methods ------------------------------------------------------------------------------------ + + /** + * Get the OrFilter + * + * @return Returns the orFilter. + */ + public ArrayList getOrFilter() + { + return filterSet; + } + + /** + * Compute the OrFilter length + * + * OrFilter : + * + * 0xA1 L1 super.computeLength() + * + * Length(OrFilter) = Length(0xA1) + Length(super.computeLength()) + super.computeLength() + * + */ + public int computeLength() + { + filtersLength = super.computeLength(); + + return 1 + Length.getNbBytes( filtersLength ) + filtersLength; + } + + /** + * Encode the OrFilter message to a PDU. + * + * OrFilter : + * + * 0xA1 LL + * filter.encode() + * + * @param buffer The buffer where to put the PDU + * @return The PDU. + */ + public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException + { + if (buffer == null) + { + throw new EncoderException("Cannot put a PDU in a null buffer !"); + } + + try + { + // The OrFilter Tag + buffer.put( (byte)LdapConstants.OR_FILTER_TAG ); + buffer.put( Length.getBytes( filtersLength ) ); + } + catch ( BufferOverflowException boe ) + { + throw new EncoderException("The PDU buffer size is too small !"); + } + + super.encode( buffer ); + + return buffer; + } + + /** + * Return a string compliant with RFC 2254 representing an OR filter + * + * @return The OR filter string + */ + public String toString() + { + + StringBuffer sb = new StringBuffer(); + + sb.append( '|' ).append( super.toString() ); + + return sb.toString(); + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/OrFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/PresentFilter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/PresentFilter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/PresentFilter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/PresentFilter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,134 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; + +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.tlv.Length; +import org.apache.asn1new.ldap.codec.LdapConstants; +import org.apache.asn1new.ldap.codec.primitives.LdapString; + + +/** + * Object to store the filter. A filter is seen as a tree with a root. + * + * @author Apache Directory Project + */ +public class PresentFilter extends Filter +{ + //~ Instance fields ---------------------------------------------------------------------------- + + /** The attribute description. */ + private LdapString attributeDescription; + + //~ Constructors ------------------------------------------------------------------------------- + + /** + * The constructor. + */ + public PresentFilter() + { + } + + //~ Methods ------------------------------------------------------------------------------------ + + /** + * Get the attribute + * + * @return Returns the attributeDescription. + */ + public LdapString getAttributeDescription() + { + return attributeDescription; + } + + /** + * Set the attributeDescription + * + * @param attributeDescription The attributeDescription to set. + */ + public void setAttributeDescription( LdapString attributeDescription ) + { + this.attributeDescription = attributeDescription; + } + + /** + * Compute the PresentFilter length + * + * PresentFilter : + * + * 0x87 L1 present + * + * Length(PresentFilter) = Length(0x87) + Length(super.computeLength()) + super.computeLength() + * + */ + public int computeLength() + { + return 1 + Length.getNbBytes( attributeDescription.getLength() ) + attributeDescription.getLength(); + } + + /** + * Encode the PresentFilter message to a PDU. + * + * PresentFilter : + * + * 0x87 LL attributeDescription + * + * @param buffer The buffer where to put the PDU + * @return The PDU. + */ + public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException + { + if (buffer == null) + { + throw new EncoderException("Cannot put a PDU in a null buffer !"); + } + + try + { + // The PresentFilter Tag + buffer.put( (byte)LdapConstants.PRESENT_FILTER_TAG ); + buffer.put( Length.getBytes( attributeDescription.getLength() ) ); + buffer.put( attributeDescription.getData() ); + } + catch ( BufferOverflowException boe ) + { + throw new EncoderException("The PDU buffer size is too small !"); + } + + super.encode( buffer ); + + return buffer; + } + + /** + * Return a string compliant with RFC 2254 representing a Present filter + * + * @return The Present filter string + */ + public String toString() + { + + StringBuffer sb = new StringBuffer(); + + sb.append( attributeDescription.toString() ).append( "=*" ); + + return sb.toString(); + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/PresentFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/SubstringFilter.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/SubstringFilter.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/SubstringFilter.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/SubstringFilter.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,344 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.pojo.filters; + +import java.nio.BufferOverflowException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Iterator; + +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.tlv.Length; +import org.apache.asn1new.ber.tlv.UniversalTag; +import org.apache.asn1new.ber.tlv.Value; +import org.apache.asn1new.ldap.codec.LdapConstants; +import org.apache.asn1new.ldap.codec.primitives.LdapString; + + +/** + * A Object that stores the substring filter. A substring filter follow this grammar : + * substring = attr "=" ([initial] any [final] | + * (initial [any] [final) | + * ([initial] [any] final) + * initial = value + * any = "*" *(value "*") + * final = value + * + * @author Apache Directory Project + */ +public class SubstringFilter extends Filter +{ + //~ Instance fields ---------------------------------------------------------------------------- + + /** The substring filter type (an attributeDescription) */ + private LdapString type; + + /** This member is used to control the length of the three parts of the substring filter **/ + private transient int substringsLength; + + /** The initial filter */ + private LdapString initialSubstrings; + + /** The any filter. It's a list of LdapString */ + private ArrayList anySubstrings; + + /** The final filter */ + private LdapString finalSubstrings; + + private transient int substringsFilterLength; + + private transient int substringsFilterSequenceLength; + + //~ Methods ------------------------------------------------------------------------------------ + + /** + * The constructor. We will create the 'any' subsring arraylist with only one element. + */ + public SubstringFilter() + { + anySubstrings = new ArrayList(1); + } + + /** + * Get the internal substrings + * + * @return Returns the anySubstrings. + */ + public ArrayList getAnySubstrings() + { + return anySubstrings; + } + + /** + * Add a internal substring + * + * @param anySubstrings The anySubstrings to set. + */ + public void addAnySubstrings( LdapString anySubstrings ) + { + this.anySubstrings.add(anySubstrings); + } + + /** + * Get the final substring + * + * @return Returns the finalSubstrings. + */ + public LdapString getFinalSubstrings() + { + return finalSubstrings; + } + + /** + * Set the final substring + * + * @param finalSubstrings The finalSubstrings to set. + */ + public void setFinalSubstrings( LdapString finalSubstrings ) + { + this.finalSubstrings = finalSubstrings; + } + + /** + * Get the initial substring + * + * @return Returns the initialSubstrings. + */ + public LdapString getInitialSubstrings() + { + return initialSubstrings; + } + + /** + * Set the initial substring + * + * @param initialSubstrings The initialSubstrings to set. + */ + public void setInitialSubstrings( LdapString initialSubstrings ) + { + this.initialSubstrings = initialSubstrings; + } + + /** + * Get the attribute + * + * @return Returns the type. + */ + public LdapString getType() + { + return type; + } + + /** + * Set the attribute to match + * + * @param type The type to set. + */ + public void setType( LdapString type ) + { + this.type = type; + } + + /** + * @return Returns the substringsLength. + */ + public int getSubstringsLength() + { + return substringsLength; + } + + /** + * @param substringsLength The substringsLength to set. + */ + public void setSubstringsLength(int substringsLength) + { + this.substringsLength = substringsLength; + } + + /** + * Compute the SubstringFilter length + * + * SubstringFilter : + * + * 0xA4 L1 + * | + * +--> 0x04 L2 type + * +--> 0x30 L3 + * | + * [+--> 0x80 L4 initial] + * [+--> 0x81 L5-1 any] + * [+--> 0x81 L5-2 any] + * [+--> ... + * [+--> 0x81 L5-i any] + * [+--> ... + * [+--> 0x81 L5-n any] + * [+--> 0x82 L6 final] + * + */ + public int computeLength() + { + // The type + substringsFilterLength = 1 + Length.getNbBytes( type.getLength() ) + type.getLength(); + substringsFilterSequenceLength = 0; + + if ( initialSubstrings != null ) + { + substringsFilterSequenceLength += 1 + Length.getNbBytes( initialSubstrings.getLength() ) + initialSubstrings.getLength(); + } + + if ( anySubstrings != null) + { + Iterator anyIterator = anySubstrings.iterator(); + + while ( anyIterator.hasNext() ) + { + LdapString any = (LdapString)anyIterator.next(); + substringsFilterSequenceLength += 1 + Length.getNbBytes( any.getLength() ) + any.getLength(); + } + } + + if ( finalSubstrings != null ) + { + substringsFilterSequenceLength += 1 + Length.getNbBytes( finalSubstrings.getLength() ) + finalSubstrings.getLength(); + } + + substringsFilterLength += 1 + Length.getNbBytes( substringsFilterSequenceLength ) + substringsFilterSequenceLength; + + return 1 + Length.getNbBytes( substringsFilterLength ) + substringsFilterLength; + } + + /** + * Encode the Substrings Filter to a PDU. + * + * Substrings Filter : + * + * 0xA4 LL + * 0x30 LL substringsFilter + * 0x04 LL type + * 0x30 LL substrings sequence + * | 0x80 LL initial + * | / [0x81 LL any]* + * |/ [0x82 LL final] + * +--[0x81 LL any]+ + * \ [0x82 LL final] + * \ + * 0x82 LL final + * + * @param buffer The buffer where to put the PDU + * @return The PDU. + */ + public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException + { + if (buffer == null) + { + throw new EncoderException("Cannot put a PDU in a null buffer !"); + } + + try + { + // The SubstringFilter Tag + buffer.put( (byte)LdapConstants.SUBSTRINGS_FILTER_TAG ); + buffer.put( Length.getBytes( substringsFilterLength ) ); + + // The type + Value.encode( buffer, type ); + + // The SubstringSequenceFilter Tag + buffer.put( UniversalTag.SEQUENCE_TAG ); + buffer.put( Length.getBytes( substringsFilterSequenceLength ) ); + + + if ( ( initialSubstrings == null ) && + ( ( anySubstrings == null ) || ( anySubstrings.size() == 0 ) ) && + ( finalSubstrings == null ) ) + { + throw new EncoderException("Cannot have a null initial, any and final substring"); + } + + // The initial substring + if ( initialSubstrings != null ) + { + buffer.put( (byte)LdapConstants.SEARCH_SUBSTRINGS_INITIAL_TAG ); + buffer.put( Length.getBytes( initialSubstrings.getLength() ) ); + buffer.put( initialSubstrings.getData() ); + } + + // The any substrings + if ( anySubstrings != null) + { + Iterator anyIterator = anySubstrings.iterator(); + + while ( anyIterator.hasNext() ) + { + LdapString any = (LdapString)anyIterator.next(); + buffer.put( (byte)LdapConstants.SEARCH_SUBSTRINGS_ANY_TAG ); + buffer.put( Length.getBytes( any.getLength() ) ); + buffer.put( any.getData() ); + } + } + + // The final substring + if ( finalSubstrings != null ) + { + buffer.put( (byte)LdapConstants.SEARCH_SUBSTRINGS_FINAL_TAG ); + buffer.put( Length.getBytes( finalSubstrings.getLength() ) ); + buffer.put( finalSubstrings.getData() ); + } + } + catch ( BufferOverflowException boe ) + { + throw new EncoderException("The PDU buffer size is too small !"); + } + + return buffer; + } + + /** + * Return a string compliant with RFC 2254 representing a Substring filter + * + * @return The substring filter string + */ + public String toString() + { + + StringBuffer sb = new StringBuffer(); + + if (initialSubstrings != null) + { + sb.append( initialSubstrings.toString() ); + } + + sb.append('*'); + + if ( anySubstrings != null ) + { + Iterator anyIterator = anySubstrings.iterator(); + + while (anyIterator.hasNext()) + { + sb.append( (LdapString)anyIterator.next() ).append('*'); + } + } + + if (finalSubstrings != null) + { + sb.append( finalSubstrings.toString() ); + } + + return sb.toString(); + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/java/main/org/apache/asn1new/ldap/pojo/filters/SubstringFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AbandonRequestTest.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AbandonRequestTest.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AbandonRequestTest.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AbandonRequestTest.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,176 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.codec; + +import java.nio.ByteBuffer; + +import org.apache.asn1.codec.DecoderException; +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.Asn1Decoder; +import org.apache.asn1new.ber.containers.IAsn1Container; +import org.apache.asn1new.ldap.pojo.AbandonRequest; +import org.apache.asn1new.ldap.pojo.LdapMessage; +import org.apache.asn1new.util.StringUtils; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * Test an AbandonRequest + * + * @author Apache Directory Project + */ +public class AbandonRequestTest extends TestCase { + /** + * Test the decoding of a AbandonRequest with no controls + */ + public void testDecodeAbandonRequestNoControls() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x08 ); + stream.put( + new byte[] + { + 0x30, 0x06, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x03, // messageID MessageID + 0x50, 0x01, 0x02 // CHOICE { ..., abandonRequest AbandonRequest,... + // AbandonRequest ::= [APPLICATION 16] MessageID + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessageContainer Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + // Check that everything is OK + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + AbandonRequest abandonRequest = message.getAbandonRequest(); + + Assert.assertEquals( 3, message.getMessageId() ); + Assert.assertEquals( 2, abandonRequest.getAbandonedMessageId() ); + + // Check the length + Assert.assertEquals(8, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + } + + /** + * Test the decoding of a AbandonRequest with controls + */ + public void testDecodeAbandonRequestWithControls() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x4A ); + stream.put( + new byte[] + { + 0x30, 0x48, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x03, // messageID MessageID + 0x50, 0x01, 0x02, // CHOICE { ..., abandonRequest AbandonRequest,... + (byte)0xA0, 0x40, // controls [0] Controls OPTIONAL } + 0x30, 0x3E, // Controls ::= SEQUENCE OF Control + 0x30, 0x13, // Control ::= SEQUENCE { + // controlType LDAPOID, + 0x04, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x01, + 0x01, 0x01, (byte)0xFF, // criticality BOOLEAN DEFAULT FALSE, + // controlValue OCTET STRING OPTIONAL } + 0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f', + 0x30, 0x10, // Control ::= SEQUENCE { + // controlType LDAPOID, + 0x04, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02, + // controlValue OCTET STRING OPTIONAL } + 0x04, 0x06, 'g', 'h', 'i', 'j', 'k', 'l', + 0x30, 0x0B, // Control ::= SEQUENCE { + // controlType LDAPOID, + 0x04, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x03, + 0x01, 0x01, (byte)0xFF, // criticality BOOLEAN DEFAULT FALSE} + 0x30, 0x08, // Control ::= SEQUENCE { + // controlType LDAPOID} + 0x04, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x04 + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessageContainer Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + // Check that everything is OK + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + AbandonRequest abandonRequest = message.getAbandonRequest(); + + Assert.assertEquals( 3, message.getMessageId() ); + Assert.assertEquals( 2, abandonRequest.getAbandonedMessageId() ); + + // Check the length + Assert.assertEquals(0x4A, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AbandonRequestTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddRequestTest.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddRequestTest.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddRequestTest.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddRequestTest.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,175 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.codec; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.HashSet; + +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; + +import org.apache.asn1.codec.DecoderException; +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.Asn1Decoder; +import org.apache.asn1new.ber.containers.IAsn1Container; +import org.apache.asn1new.ldap.pojo.AddRequest; +import org.apache.asn1new.ldap.pojo.LdapMessage; +import org.apache.asn1new.primitives.OctetString; +import org.apache.asn1new.util.StringUtils; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * Test the AddRequest codec + * + * @author Apache Directory Project + */ +public class AddRequestTest extends TestCase { + /** + * Test the decoding of a AddRequest + */ + public void testDecodeAddRequestSuccess() throws NamingException + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x5B ); + + stream.put( + new byte[] + { + 0x30, 0x59, // LDAPMessage ::= SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x68, 0x54, // CHOICE { ..., addRequest AddRequest, ... + // AddRequest ::= [APPLICATION 8] SEQUENCE { + // entry LDAPDN, + 0x04, 0x22, 'c', 'n', '=', 't', 'e', 's', 't', 'M', 'o', 'd', 'i', 'f', 'y', ',', ' ', 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', ' ', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', + // attributes AttributeList } + 0x30, 0x2E, // AttributeList ::= SEQUENCE OF SEQUENCE { + 0x30, 0x0c, // attribute 1 + 0x04, 0x01, 'l', // type AttributeDescription, + 0x31, 0x07, // vals SET OF AttributeValue } + 0x04, 0x05, 'P', 'a', 'r', 'i', 's', + + 0x30, 0x1E, // attribute 2 + // type AttributeDescription, + 0x04, 0x05, 'a', 't', 't', 'r', 's', + 0x31, 0x15, // vals SET OF AttributeValue } + 0x04, 0x05, 't', 'e', 's', 't', '1', + 0x04, 0x05, 't', 'e', 's', 't', '2', + 0x04, 0x05, 't', 'e', 's', 't', '3', + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode a AddRequest message + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + AddRequest addRequest = message.getAddRequest(); + + // Check the decoded message + Assert.assertEquals( 1, message.getMessageId() ); + Assert.assertEquals( "cn=testModify, ou=users, ou=system", addRequest.getEntry() ); + + Attributes attributes = addRequest.getAttributes(); + + Assert.assertEquals( 2, attributes.size() ); + + HashSet expectedTypes = new HashSet(); + + expectedTypes.add("l"); + expectedTypes.add("attrs"); + + HashMap typesVals = new HashMap(); + + HashSet lVal1 = new HashSet(); + lVal1.add("Paris"); + typesVals.put("l", lVal1); + + HashSet lVal2 = new HashSet(); + lVal2.add("test1"); + lVal2.add("test2"); + lVal2.add("test3"); + typesVals.put("attrs", lVal2); + + BasicAttribute attributeValue = (BasicAttribute)attributes.get( "l" ); + + Assert.assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) ); + + NamingEnumeration values = attributeValue.getAll(); + HashSet vals = (HashSet)typesVals.get( attributeValue.getID().toLowerCase() ); + + while ( values.hasMore() ) + { + OctetString value = (OctetString)values.next(); + + Assert.assertTrue( vals.contains( value.toString() ) ); + + vals.remove( value.toString() ); + } + + attributeValue = (BasicAttribute)attributes.get( "attrs" ); + + Assert.assertTrue( expectedTypes.contains( attributeValue.getID().toLowerCase() ) ); + + values = attributeValue.getAll(); + vals = (HashSet)typesVals.get( attributeValue.getID().toLowerCase() ); + + while ( values.hasMore() ) + { + OctetString value = (OctetString)values.next(); + + Assert.assertTrue( vals.contains( value.toString() ) ); + + vals.remove( value.toString() ); + } + + // Check the length + Assert.assertEquals(0x5B, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddRequestTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddResponseTest.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddResponseTest.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddResponseTest.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddResponseTest.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,108 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.codec; + +import java.nio.ByteBuffer; + +import org.apache.asn1.codec.DecoderException; +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.Asn1Decoder; +import org.apache.asn1new.ber.containers.IAsn1Container; +import org.apache.asn1new.ldap.pojo.AddResponse; +import org.apache.asn1new.ldap.pojo.LdapMessage; +import org.apache.asn1new.util.StringUtils; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * @author Apache Directory Project + */ +public class AddResponseTest extends TestCase { + /** + * Test the decoding of a AddResponse + */ + public void testDecodeAddResponseSuccess() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x2D ); + + stream.put( + new byte[] + { + 0x30, 0x2B, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x69, 0x26, // CHOICE { ..., addResponse AddResponse, ... + // AddResponse ::= [APPLICATION 9] LDAPResult + 0x0A, 0x01, 0x00, // LDAPResult ::= SEQUENCE { + // resultCode ENUMERATED { + // success (0), ... + // }, + 0x04, 0x1F, // matchedDN LDAPDN, + 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', + 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', + 0x04, 0x00 // errorMessage LDAPString, + // referral [3] Referral OPTIONAL } + // } + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the AddResponse PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + // Check the decoded AddResponse + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + AddResponse addResponse = message.getAddResponse(); + + Assert.assertEquals( 1, message.getMessageId() ); + Assert.assertEquals( 0, addResponse.getLdapResult().getResultCode() ); + Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", addResponse.getLdapResult().getMatchedDN() ); + Assert.assertEquals( "", addResponse.getLdapResult().getErrorMessage() ); + + // Check the length + Assert.assertEquals(0x2D, message.computeLength()); + + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/AddResponseTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/BindRequestTest.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/BindRequestTest.java?rev=279887&view=auto ============================================================================== --- directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/BindRequestTest.java (added) +++ directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/BindRequestTest.java Fri Sep 9 14:41:22 2005 @@ -0,0 +1,375 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap.codec; + +import java.nio.ByteBuffer; + +import org.apache.asn1.codec.DecoderException; +import org.apache.asn1.codec.EncoderException; +import org.apache.asn1new.ber.Asn1Decoder; +import org.apache.asn1new.ber.containers.IAsn1Container; +import org.apache.asn1new.ldap.pojo.BindRequest; +import org.apache.asn1new.ldap.pojo.LdapMessage; +import org.apache.asn1new.ldap.pojo.SaslCredentials; +import org.apache.asn1new.ldap.pojo.SimpleAuthentication; +import org.apache.asn1new.util.StringUtils; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * @author Apache Directory Project + */ +public class BindRequestTest extends TestCase { + /** + * Test the decoding of a BindRequest with Simple authentication + * and no controls + */ + public void testDecodeBindRequestSimpleNoControls() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x35 ); + stream.put( + new byte[] + { + 0x30, 0x33, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x60, 0x2E, // CHOICE { ..., bindRequest BindRequest, ... + // BindRequest ::= APPLICATION[0] SEQUENCE { + 0x02, 0x01, 0x03, // version INTEGER (1..127), + 0x04, 0x1F, // name LDAPDN, + 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', + 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', + ( byte ) 0x80, 0x08, // authentication AuthenticationChoice + // AuthenticationChoice ::= CHOICE { simple [0] OCTET STRING, ... + 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the BindRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + // Check the decoded BindRequest + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + BindRequest br = message.getBindRequest(); + + Assert.assertEquals( 1, message.getMessageId() ); + Assert.assertEquals( 3, br.getVersion() ); + Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", br.getName() ); + Assert.assertEquals( true, ( br.getAuthentication() instanceof SimpleAuthentication ) ); + Assert.assertEquals( "password", ( ( SimpleAuthentication ) br.getAuthentication() ).getSimple().toString() ); + + // Check the length + Assert.assertEquals(0x35, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + } + + /** + * Test the decoding of a BindRequest with Simple authentication, + * no name + * and no controls + */ + public void testDecodeBindRequestSimpleNoName() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x15 ); + stream.put( + new byte[] + { + 0x30, 0x13, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x60, 0x0D, // CHOICE { ..., bindRequest BindRequest, ... + // BindRequest ::= APPLICATION[0] SEQUENCE { + 0x02, 0x01, 0x03, // version INTEGER (1..127), + ( byte ) 0x80, 0x08, // authentication AuthenticationChoice + // AuthenticationChoice ::= CHOICE { simple [0] OCTET STRING, ... + 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' + } ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the BindRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + Assert.assertEquals( "Bad transition !", de.getMessage() ); + return; + } + + Assert.fail("Should never reach this point."); + } + + /** + * Test the decoding of a BindRequest with Simple authentication, + * empty name (an anonymous bind) + * and no controls + */ + public void testDecodeBindRequestSimpleEmptyName() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x16 ); + stream.put( + new byte[] + { + 0x30, 0x14, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x60, 0x0F, // CHOICE { ..., bindRequest BindRequest, ... + // BindRequest ::= APPLICATION[0] SEQUENCE { + 0x02, 0x01, 0x03, // version INTEGER (1..127), + 0x04, 0x00, // name LDAPDN, + ( byte ) 0x80, 0x08, // authentication AuthenticationChoice + // AuthenticationChoice ::= CHOICE { simple [0] OCTET STRING, ... + 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the BindRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + // Check the decoded BindRequest + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + BindRequest br = message.getBindRequest(); + + Assert.assertEquals( 1, message.getMessageId() ); + Assert.assertEquals( 3, br.getVersion() ); + Assert.assertEquals( "", br.getName() ); + Assert.assertEquals( true, ( br.getAuthentication() instanceof SimpleAuthentication ) ); + Assert.assertEquals( "password", ( ( (SimpleAuthentication)br.getAuthentication() ).getSimple().toString() ) ); + + // Check the length + Assert.assertEquals(0x16, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + } + + /** + * Test the decoding of a BindRequest with Sasl authentication, + * no credentials and no controls + */ + public void testDecodeBindRequestSaslNoCredsNoControls() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x38 ); + stream.put( + new byte[] + { + 0x30, 0x36, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x60, 0x31, // CHOICE { ..., bindRequest BindRequest, ... + // BindRequest ::= APPLICATION[0] SEQUENCE { + 0x02, 0x01, 0x03, // version INTEGER (1..127), + 0x04, 0x1F, // name LDAPDN, + 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', + 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', + ( byte ) 0x83, 0x0B, // authentication AuthenticationChoice + // AuthenticationChoice ::= CHOICE { ... sasl [3] SaslCredentials } + // SaslCredentials ::= SEQUENCE { + // mechanism LDAPSTRING, + // ... + 'K', 'E', 'R', 'B', 'E', 'R', 'O', 'S', '_', 'V', '4' + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the BindRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + // Check the decoded BindRequest + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + BindRequest br = message.getBindRequest(); + + Assert.assertEquals( 1, message.getMessageId() ); + Assert.assertEquals( 3, br.getVersion() ); + Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", br.getName() ); + Assert.assertEquals( true, ( br.getAuthentication() instanceof SaslCredentials ) ); + Assert.assertEquals( "KERBEROS_V4", ( ( SaslCredentials ) br.getAuthentication() ).getMechanism() ); + + // Check the length + Assert.assertEquals(0x38, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + } + + /** + * Test the decoding of a BindRequest with Sasl authentication, + * a credentials and no controls + */ + public void testDecodeBindRequestSaslCredsNoControls() + { + Asn1Decoder ldapDecoder = new LdapDecoder(); + + ByteBuffer stream = ByteBuffer.allocate( 0x40 ); + stream.put( + new byte[] + { + 0x30, 0x3E, // LDAPMessage ::=SEQUENCE { + 0x02, 0x01, 0x01, // messageID MessageID + 0x60, 0x39, // CHOICE { ..., bindRequest BindRequest, ... + // BindRequest ::= APPLICATION[0] SEQUENCE { + 0x02, 0x01, 0x03, // version INTEGER (1..127), + 0x04, 0x1F, // name LDAPDN, + 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', + 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', + ( byte ) 0x83, 0x0B, // authentication AuthenticationChoice } + // AuthenticationChoice ::= CHOICE { ... sasl [3] SaslCredentials } + // SaslCredentials ::= SEQUENCE { + // mechanism LDAPSTRING, + // ... + 'K', 'E', 'R', 'B', 'E', 'R', 'O', 'S', '_', 'V', '4', + ( byte ) 0x04, 0x06, // SaslCredentials ::= SEQUENCE { + // ... + // credentials OCTET STRING OPTIONAL } + // + 'a', 'b', 'c', 'd', 'e', 'f' + } ); + + String decodedPdu = StringUtils.dumpBytes( stream.array() ); + stream.flip(); + + // Allocate a LdapMessage Container + IAsn1Container ldapMessageContainer = new LdapMessageContainer(); + + // Decode the BindRequest PDU + try + { + ldapDecoder.decode( stream, ldapMessageContainer ); + } + catch ( DecoderException de ) + { + de.printStackTrace(); + Assert.fail( de.getMessage() ); + } + + // Check the decoded BindRequest + LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); + BindRequest br = message.getBindRequest(); + + Assert.assertEquals( 1, message.getMessageId() ); + Assert.assertEquals( 3, br.getVersion() ); + Assert.assertEquals( "uid=akarasulu,dc=example,dc=com", br.getName() ); + Assert.assertEquals( true, ( br.getAuthentication() instanceof SaslCredentials ) ); + Assert.assertEquals( "KERBEROS_V4", ( ( SaslCredentials ) br.getAuthentication() ).getMechanism() ); + Assert.assertEquals( "abcdef", ( ( SaslCredentials ) br.getAuthentication() ).getCredentials().toString() ); + + // Check the length + Assert.assertEquals(0x40, message.computeLength()); + + // Check the encoding + try + { + ByteBuffer bb = message.encode( null ); + + String encodedPdu = StringUtils.dumpBytes( bb.array() ); + + Assert.assertEquals(encodedPdu, decodedPdu ); + } + catch ( EncoderException ee ) + { + ee.printStackTrace(); + Assert.fail( ee.getMessage() ); + } + } +} Propchange: directory/shared/ldap/branches/elecharny-cleanup/apache2-provider/src/test/org/apache/asn1new/ldap/codec/BindRequestTest.java ------------------------------------------------------------------------------ svn:eol-style = native