Index: modules/jndi/src/main/java/org/apache/harmony/jndi/internal/JndiBerInputStream.java =================================================================== --- modules/jndi/src/main/java/org/apache/harmony/jndi/internal/JndiBerInputStream.java (revision 0) +++ modules/jndi/src/main/java/org/apache/harmony/jndi/internal/JndiBerInputStream.java (revision 0) @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.harmony.jndi.internal; + +import java.io.IOException; + +import org.apache.harmony.security.asn1.ASN1Constants; +import org.apache.harmony.security.asn1.ASN1Exception; +import org.apache.harmony.security.asn1.ASN1Sequence; +import org.apache.harmony.security.asn1.ASN1Type; +import org.apache.harmony.security.asn1.BerInputStream; +import org.apache.harmony.jndi.internal.nls.Messages; + +public final class JndiBerInputStream extends BerInputStream { + public JndiBerInputStream(byte[] encoded) throws IOException { + int offset = 0; + int encodingLen = encoded.length; + + this.buffer = encoded; + this.offset = offset; + + next(); + + // compare expected and decoded length + if (length != INDEFINIT_LENGTH + && (offset + encodingLen) != (this.offset + this.length)) { + this.length = offset + encodingLen - this.offset; + } + } + + public void readSequence(ASN1Sequence sequence) throws IOException { + + if (tag != ASN1Constants.TAG_C_SEQUENCE) { + throw new ASN1Exception(Messages.getString( + "security.12F", tagOffset, //$NON-NLS-1$ + Integer.toHexString(tag))); + } + + int begOffset = offset; + int endOffset = begOffset + length; + + ASN1Type[] type = sequence.type; + + int i = 0; + + if (isVerify) { + + for (; (offset < endOffset) && (i < type.length); i++) { + + next(); + while (!type[i].checkTag(tag)) { + // check whether it is optional component or not + if (!sequence.OPTIONAL[i] || (i == type.length - 1)) { + throw new ASN1Exception(Messages.getString( + "security.130", //$NON-NLS-1$ + tagOffset)); + } + i++; + } + + type[i].decode(this); + } + + // check the rest of components + for (; i < type.length; i++) { + if (!sequence.OPTIONAL[i]) { + throw new ASN1Exception(Messages.getString("security.131", //$NON-NLS-1$ + tagOffset)); + } + } + + } else { + + int seqTagOffset = tagOffset; // store tag offset + + Object[] values = new Object[type.length]; + for (; (offset < endOffset) && (i < type.length); i++) { + + next(); + while (i < type.length && !type[i].checkTag(tag)) { + // check whether it is optional component or not + if (!sequence.OPTIONAL[i]) { + throw new ASN1Exception(Messages.getString( + "security.132", //$NON-NLS-1$ + tagOffset)); + } + + // sets default value + if (sequence.DEFAULT[i] != null) { + values[i] = sequence.DEFAULT[i]; + } + i++; + } + if (i == type.length) + break; + values[i] = type[i].decode(this); + } + + // check the rest of components + for (; i < type.length; i++) { + if (!sequence.OPTIONAL[i]) { + throw new ASN1Exception(Messages.getString("security.133", //$NON-NLS-1$ + tagOffset)); + } + if (sequence.DEFAULT[i] != null) { + values[i] = sequence.DEFAULT[i]; + } + } + content = values; + + tagOffset = seqTagOffset; // retrieve tag offset + } + } +} Index: modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ldap/TestSortResponseControl.java =================================================================== --- modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ldap/TestSortResponseControl.java (revision 548914) +++ modules/jndi/src/test/java/org/apache/harmony/jndi/tests/javax/naming/ldap/TestSortResponseControl.java (working copy) @@ -424,7 +424,33 @@ } } + /** + *

Test method for 'javax.naming.ldap.SortResponseControl.SortResponseControl(String, boolean, byte[])'

+ */ + public void testSortResponseControl019() throws IOException{ + String Id="test"; + boolean crit=false; + byte[] ber1={48,1,10,1,0}; + byte[] ber2={48,5,10,1,3}; + byte[] ber3={48,3,10,2,3,3}; + byte[] ber4={48,4,10,1,3,3,3}; + byte[] ber5={48,8,10,1,3,(byte)128,3,'T','e','s','t'}; + SortResponseControl src=null; + src = new SortResponseControl(Id, crit, ber1); + assertEquals(Id, src.getID()); + assertEquals(src.getResultCode(), 0); + src = new SortResponseControl(Id, crit, ber2); + assertEquals(src.getResultCode(), 3); + src = new SortResponseControl(Id, crit, ber3); + assertEquals(src.getResultCode(), 771); + src = new SortResponseControl(Id, crit, ber4); + assertEquals(src.getResultCode(), 3); + src = new SortResponseControl(Id, crit, ber5); + assertEquals(src.getResultCode(), 3); + assertEquals("Tes", src.getAttributeID()); + } + /** *

Test method for 'javax.naming.ldap.SortResponseControl.isSorted()'

*

Here we are testing if this method determines if the search results have been successfully sorted.

Index: modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties =================================================================== --- modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties (revision 548914) +++ modules/jndi/src/main/java/org/apache/harmony/jndi/internal/nls/messages.properties (working copy) @@ -195,3 +195,8 @@ ldap.21=was not found ldap.22=foundObjects is null ldap.23= not found in jar file +security.12F=ASN.1 sequence identifier is expected at [{0}], but encountered: {1} +security.130=ASN.1 Sequence: mandatory value is missing at [{0}] +security.131=Mandatory value is missing at [{0}] +security.132=ASN.1 Sequence: mandatory value is missing at [{0}] +security.133=Mandatory value is missing at [{0}] Index: modules/jndi/src/main/java/javax/naming/ldap/SortResponseControl.java =================================================================== --- modules/jndi/src/main/java/javax/naming/ldap/SortResponseControl.java (revision 548914) +++ modules/jndi/src/main/java/javax/naming/ldap/SortResponseControl.java (working copy) @@ -22,9 +22,11 @@ import javax.naming.NamingException; +import org.apache.harmony.jndi.internal.JndiBerInputStream; import org.apache.harmony.jndi.internal.SortResult; import org.apache.harmony.jndi.internal.Util; import org.apache.harmony.security.asn1.ASN1Enumerated; +import org.apache.harmony.security.asn1.ASN1Implicit; import org.apache.harmony.security.asn1.ASN1OctetString; import org.apache.harmony.security.asn1.ASN1Sequence; import org.apache.harmony.security.asn1.ASN1Type; @@ -71,7 +73,7 @@ */ static ASN1Type ASN1_SORTRESPONSE = new ASN1Sequence(new ASN1Type[] { ASN1Enumerated.getInstance(), // sortResult - ASN1OctetString.getInstance(), // attributeType + new ASN1Implicit(0, ASN1OctetString.getInstance()), // attributeType }) { { @@ -108,9 +110,9 @@ * @ar.org.fitc.spec_ref */ public SortResponseControl(String id, boolean criticality, byte[] value) throws IOException { - super(OID,criticality,value); + super(id,criticality,value); SortResult sr; - sr = (SortResult) ASN1_SORTRESPONSE.decode(value); + sr = (SortResult) ASN1_SORTRESPONSE.decode(new JndiBerInputStream(value)); resultCode = sr.getSortresult(); badAttrId = sr.getAttributeType(); if (getResultCode() == 0) { Index: modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java =================================================================== --- modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java (revision 548914) +++ modules/security/src/main/java/common/org/apache/harmony/security/asn1/BerInputStream.java (working copy) @@ -65,6 +65,9 @@ */ protected static final int INDEFINIT_LENGTH = -1; + public BerInputStream() { + } + /** * Creates stream for decoding. *