Search in sources :

Example 16 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestWithControls.

/**
 * Test the decoding of a SearchRequest with controls.
 */
@Test
public void testDecodeSearchRequestWithControls() {
    byte[] asn1BERJava5 = new byte[] { 0x30, 0x7f, // messageID
    0x02, // messageID
    0x01, // messageID
    0x04, 0x63, 0x33, 0x04, // baseObject
    0x13, 'd', 'c', '=', 'm', 'y', '-', 'd', 'o', 'm', 'a', 'i', 'n', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0a, 0x01, // scope: subtree
    0x02, 0x0a, 0x01, // derefAliases: derefAlways
    0x03, 0x02, 0x01, // sizeLimit: 0
    0x00, 0x02, 0x01, // timeLimit: 0
    0x00, 0x01, 0x01, // typesOnly: false
    0x00, (byte) 0x87, // Present filter: (objectClass=*)
    0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'C', 'l', 'a', 's', 's', 0x30, // Attributes = '*'
    0x00, (byte) 0xa0, // controls
    0x45, 0x30, 0x28, 0x04, // control
    0x16, '1', '.', '2', '.', '8', '4', '0', '.', '1', '1', '3', '5', '5', '6', '.', '1', '.', '4', '.', '3', '1', '9', 0x01, 0x01, // criticality: false
    (byte) 0xff, 0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x02, 0x04, 0x04, 0x47, 0x00, 0x00, // value: pageSize=2
    0x00, 0x30, 0x19, 0x04, // control
    0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' };
    byte[] asn1BERJava6 = new byte[] { 0x30, 0x7f, // messageID
    0x02, // messageID
    0x01, // messageID
    0x04, 0x63, 0x33, 0x04, // baseObject
    0x13, 'd', 'c', '=', 'm', 'y', '-', 'd', 'o', 'm', 'a', 'i', 'n', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0a, 0x01, // scope: subtree
    0x02, 0x0a, 0x01, // derefAliases: derefAlways
    0x03, 0x02, 0x01, // sizeLimit: 0
    0x00, 0x02, 0x01, // timeLimit: 0
    0x00, 0x01, 0x01, // typesOnly: false
    0x00, (byte) 0x87, // Present filter: (objectClass=*)
    0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'C', 'l', 'a', 's', 's', 0x30, // Attributes = '*'
    0x00, (byte) 0xa0, // controls
    0x45, 0x30, 0x19, 0x04, // control
    0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2', 0x30, 0x28, 0x04, // control
    0x16, '1', '.', '2', '.', '8', '4', '0', '.', '1', '1', '3', '5', '5', '6', '.', '1', '.', '4', '.', '3', '1', '9', 0x01, 0x01, // criticality: false
    (byte) 0xff, 0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x02, 0x04, 0x04, 0x47, 0x00, 0x00, // value: pageSize=2
    0x00 };
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    // For Java6
    ByteBuffer streamJava6 = ByteBuffer.allocate(asn1BERJava6.length);
    streamJava6.put(asn1BERJava6);
    String decodedPduJava6 = Strings.dumpBytes(streamJava6.array());
    streamJava6.flip();
    // For Java5
    ByteBuffer streamJava5 = ByteBuffer.allocate(asn1BERJava5.length);
    streamJava5.put(asn1BERJava5);
    String decodedPduJava5 = Strings.dumpBytes(streamJava5.array());
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(streamJava6, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(4, searchRequest.getMessageId());
    assertEquals(2, searchRequest.getControls().size());
    // this is a constant in Java 5 API
    String pagedResultsControlOID = "1.2.840.113556.1.4.319";
    Control pagedResultsControl = searchRequest.getControl(pagedResultsControlOID);
    assertEquals(pagedResultsControlOID, pagedResultsControl.getOid());
    assertTrue(pagedResultsControl.isCritical());
    // this is a constant in Java 5 API
    String manageReferralControlOID = "2.16.840.1.113730.3.4.2";
    Control manageReferralControl = searchRequest.getControl(manageReferralControlOID);
    assertEquals(manageReferralControlOID, manageReferralControl.getOid());
    assertEquals("dc=my-domain,dc=com", searchRequest.getBase().toString());
    assertEquals(SearchScope.SUBTREE, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(false, searchRequest.getTypesOnly());
    ExprNode filter = searchRequest.getFilter();
    assertTrue(filter instanceof PresenceNode);
    assertEquals("objectClass", ((PresenceNode) filter).getAttribute());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x81, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertTrue(decodedPduJava5.equals(encodedPdu) || decodedPduJava6.equals(encodedPdu));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) ByteBuffer(java.nio.ByteBuffer) ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 17 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestAnd_AndEq_Eq.

/**
 * Test the decoding of a SearchRequest
 * (&(&(a=b))(c=d))
 */
@Test
public void testDecodeSearchRequestAnd_AndEq_Eq() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x31);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x2F, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, // CHOICE { ...,
    0x2A, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x03, 'a', '=', 'b', 0x0A, 0x01, // scope ENUMERATED {
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // derefAlways (3) },
    0x02, 0x01, // sizeLimit INTEGER (0 .. maxInt), (0)
    0x00, 0x02, 0x01, // timeLimit INTEGER (0 .. maxInt), (1000)
    0x00, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x12, (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x08, (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'a', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'b', (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'c', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'd', 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
    0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("a=b", searchRequest.getBase().toString());
    assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // (&(...
    ExprNode filter = searchRequest.getFilter();
    AndNode andNode = (AndNode) filter;
    assertNotNull(andNode);
    List<ExprNode> andNodes = andNode.getChildren();
    assertEquals(2, andNodes.size());
    // (&(&(..
    AndNode andNode2 = (AndNode) andNodes.get(0);
    assertNotNull(andNode2);
    List<ExprNode> andNodes2 = andNode2.getChildren();
    assertEquals(1, andNodes2.size());
    // (&(&(a=b))...
    EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes2.get(0);
    assertNotNull(equalityNode);
    assertEquals("a", equalityNode.getAttribute());
    assertEquals("b", equalityNode.getValue().getValue());
    // (&(&(a=b))(c=d))
    equalityNode = (EqualityNode<?>) andNodes.get(1);
    assertNotNull(equalityNode);
    assertEquals("c", equalityNode.getAttribute());
    assertEquals("d", equalityNode.getValue().getValue());
    List<String> attributes = searchRequest.getAttributes();
    assertEquals(0, attributes.size());
    // attributes may have been reordered
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x31, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x31), decodedPdu.substring(0, 0x31));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) ByteBuffer(java.nio.ByteBuffer) ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) EqualityNode(org.apache.directory.api.ldap.model.filter.EqualityNode) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 18 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestAnd_AndEqEq_AndEq.

/**
 * Test the decoding of a SearchRequest
 * (&(&(a=b)(c=d))(&(e=f)))
 */
@Test
public void testDecodeSearchRequestAnd_AndEqEq_AndEq() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3B);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x39, 0x02, 0x01, // messageID MessageID
    0x01, 0x63, // CHOICE { ...,
    0x34, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x03, 'a', '=', 'b', 0x0A, 0x01, // scope ENUMERATED {
    0x01, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // derefAlways (3) },
    0x02, 0x01, // sizeLimit INTEGER (0 .. maxInt), (0)
    0x00, 0x02, 0x01, // timeLimit INTEGER (0 .. maxInt), (1000)
    0x00, 0x01, 0x01, // typesOnly BOOLEAN, (TRUE)
    (byte) 0xFF, // filter Filter,
    (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x1C, (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x10, (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'a', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'b', (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'c', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'd', (byte) 0xA0, // Filter ::= CHOICE { and             [0] SET OF Filter,
    0x08, (byte) 0xA3, // equalityMatch [3] Assertion,
    0x06, // Assertion ::= SEQUENCE {
    0x04, 0x01, // attributeDesc AttributeDescription (LDAPString),
    'e', 0x04, 0x01, // assertionValue AssertionValue (OCTET STRING) }
    'f', 0x30, // AttributeDescriptionList ::= SEQUENCE OF AttributeDescription
    0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("a=b", searchRequest.getBase().toString());
    assertEquals(SearchScope.ONELEVEL, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // (&(...
    ExprNode filter = searchRequest.getFilter();
    AndNode andNode = (AndNode) filter;
    assertNotNull(andNode);
    List<ExprNode> andNodes = andNode.getChildren();
    assertEquals(2, andNodes.size());
    // (&(&(..
    AndNode andNode2 = (AndNode) andNodes.get(0);
    assertNotNull(andNode2);
    List<ExprNode> andNodes2 = andNode2.getChildren();
    assertEquals(2, andNodes2.size());
    // (&(&(a=b)...
    EqualityNode<?> equalityNode = (EqualityNode<?>) andNodes2.get(0);
    assertNotNull(equalityNode);
    assertEquals("a", equalityNode.getAttribute());
    assertEquals("b", equalityNode.getValue().getValue());
    // (&(&(a=b)(c=d))...
    equalityNode = (EqualityNode<?>) andNodes2.get(1);
    assertNotNull(equalityNode);
    assertEquals("c", equalityNode.getAttribute());
    assertEquals("d", equalityNode.getValue().getValue());
    // (&(&(a=b)(c=d))(&...
    andNode2 = (AndNode) andNodes.get(1);
    assertNotNull(andNode2);
    andNodes2 = andNode2.getChildren();
    assertEquals(1, andNodes2.size());
    // (&(&(a=b)(c=d))(&(e=f)))
    equalityNode = (EqualityNode<?>) andNodes2.get(0);
    assertNotNull(equalityNode);
    assertEquals("e", equalityNode.getAttribute());
    assertEquals("f", equalityNode.getValue().getValue());
    List<String> attributes = searchRequest.getAttributes();
    assertEquals(0, attributes.size());
    // attributes may have been reordered
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x3B, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x3B), decodedPdu.substring(0, 0x3B));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) AndNode(org.apache.directory.api.ldap.model.filter.AndNode) ByteBuffer(java.nio.ByteBuffer) ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) EqualityNode(org.apache.directory.api.ldap.model.filter.EqualityNode) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 19 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestNoAttributes.

/**
 * Test the decoding of a SearchRequest with no attributes. The search
 * filter is : (objectclass=*)
 */
@Test
public void testDecodeSearchRequestNoAttributes() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x40);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x37, 0x02, 0x01, // messageID MessageID
    0x03, 0x63, // CHOICE { ..., searchRequest SearchRequest, ...
    0x32, // SearchRequest ::= APPLICATION[3] SEQUENCE {
    0x04, // baseObject LDAPDN,
    0x12, 'o', 'u', '=', 'u', 's', 'e', 'r', 's', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', 0x0A, 0x01, // scope ENUMERATED {
    0x00, // wholeSubtree (2) },
    0x0A, 0x01, // derefAliases ENUMERATED {
    0x03, // sizeLimit INTEGER (0 .. maxInt), (infinite)
    0x02, 0x01, 0x00, // timeLimit INTEGER (0 .. maxInt), (infinite)
    0x02, 0x01, 0x00, 0x01, 0x01, // typesOnly
    (byte) 0x00, // Filter ::= CHOICE {
    (byte) 0x87, // present [7] AttributeDescription,
    0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'C', 'l', 'a', 's', 's', // attributes AttributeDescriptionList }
    0x30, // AttributeDescriptionList ::= SEQUENCE OF
    0x00, // AttributeDescription
    0x00, // Some trailing 00, useless.
    0x00, 0x00, 0x00, 0x00, 0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(3, searchRequest.getMessageId());
    assertEquals("ou=users,ou=system", searchRequest.getBase().toString());
    assertEquals(SearchScope.OBJECT, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(false, searchRequest.getTypesOnly());
    // (objectClass = *)
    ExprNode filter = searchRequest.getFilter();
    PresenceNode presenceNode = (PresenceNode) filter;
    assertNotNull(presenceNode);
    assertEquals("objectClass", presenceNode.getAttribute());
    // The attributes
    List<String> attributes = searchRequest.getAttributes();
    assertEquals(0, attributes.size());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x39, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu.substring(0, decodedPdu.length() - 35));
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) EncoderException(org.apache.directory.api.asn1.EncoderException) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 20 with EncoderException

use of org.apache.directory.api.asn1.EncoderException in project directory-ldap-api by apache.

the class SearchResultDoneTest method testDecodeSearchResultDoneSuccessWithControls.

/**
 * Test the decoding of a SearchResultDone with controls
 */
@Test
public void testDecodeSearchResultDoneSuccessWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2B);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x29, 0x02, 0x01, // messageID MessageID
    0x01, 0x65, // CHOICE { ..., searchResDone SearchResultDone, ...
    0x07, // SearchResultDone ::= [APPLICATION 5] LDAPResult
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00, // }
    (byte) 0xA0, // A control
    0x1B, 0x30, 0x19, 0x04, 0x17, 0x32, 0x2E, 0x31, 0x36, 0x2E, 0x38, 0x34, 0x30, 0x2E, 0x31, 0x2E, 0x31, 0x31, 0x33, 0x37, 0x33, 0x30, 0x2E, 0x33, 0x2E, 0x34, 0x2E, 0x32 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultDoneDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultDoneDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultDone searchResultDone = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultDone.getMessageId());
    assertEquals(ResultCodeEnum.SUCCESS, searchResultDone.getLdapResult().getResultCode());
    assertEquals("", searchResultDone.getLdapResult().getMatchedDn().getName());
    assertEquals("", searchResultDone.getLdapResult().getDiagnosticMessage());
    // Check the Control
    Map<String, Control> controls = searchResultDone.getControls();
    assertEquals(1, controls.size());
    @SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("2.16.840.1.113730.3.4.2");
    assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultDone);
        // Check the length
        assertEquals(0x2B, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchResultDone(org.apache.directory.api.ldap.model.message.SearchResultDone) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultDoneDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultDoneDecorator) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

EncoderException (org.apache.directory.api.asn1.EncoderException)226 ByteBuffer (java.nio.ByteBuffer)191 Test (org.junit.Test)189 DecoderException (org.apache.directory.api.asn1.DecoderException)151 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)150 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)127 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)124 BufferOverflowException (java.nio.BufferOverflowException)40 Control (org.apache.directory.api.ldap.model.message.Control)38 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)35 AbstractCodecServiceTest (org.apache.directory.api.ldap.extras.AbstractCodecServiceTest)35 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)33 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)33 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)28 SyncInfoValue (org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValue)20 SyncInfoValueDecorator (org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValueDecorator)20 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)20 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)15 Entry (org.apache.directory.api.ldap.model.entry.Entry)14 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)14