Search in sources :

Example 91 with DecoderException

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

the class ExtendedResponseTest method testDecodeExtendedResponseNoResponseWithControls.

/**
 * Test the decoding of an ExtendedResponse with no response with controls
 */
@Test
public void testDecodeExtendedResponseNoResponseWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3A);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x38, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., extendedResp Response, ...
    0x78, // ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
    0x16, // COMPONENTS OF LDAPResult,
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00, // responseName [0] LDAPOID,
    (byte) 0x8A, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', (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 LdapMessage Container
    LdapMessageContainer<ExtendedResponseDecorator<?>> container = new LdapMessageContainer<ExtendedResponseDecorator<?>>(codec);
    // Decode the ExtendedResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded ExtendedResponse PDU
    ExtendedResponseDecorator<?> extendedResponse = container.getMessage();
    assertEquals(1, extendedResponse.getMessageId());
    assertEquals(ResultCodeEnum.SUCCESS, extendedResponse.getLdapResult().getResultCode());
    assertEquals(Dn.EMPTY_DN, extendedResponse.getLdapResult().getMatchedDn());
    assertEquals("", extendedResponse.getLdapResult().getDiagnosticMessage());
    assertEquals("1.3.6.1.5.5.2", extendedResponse.getResponseName());
    assertEquals("", Strings.utf8ToString(extendedResponse.getResponseValue()));
    // Check the Control
    Map<String, Control> controls = extendedResponse.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(control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(extendedResponse);
        // Check the length
        assertEquals(0x3A, 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) 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) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 92 with DecoderException

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

the class IntermediateResponseTest method testDecodeIntermediateResponseWithControls.

/**
 * Test the decoding of a full IntermediateResponse with controls
 */
@Test
public void testDecodeIntermediateResponseWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3A);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x38, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., intermediateResponse IntermediateResponse, ...
    0x79, // IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
    0x16, // responseName [0] LDAPOID,
    (byte) 0x80, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // requestValue [1] OCTET STRING OPTIONAL }
    (byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e', (byte) 0xA0, // A control
    0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<IntermediateResponseDecorator<?>> ldapMessageContainer = new LdapMessageContainer<IntermediateResponseDecorator<?>>(codec);
    // Decode the IntermediateResponse PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded IntermediateResponse PDU
    IntermediateResponse intermediateResponse = ldapMessageContainer.getMessage();
    assertEquals(1, intermediateResponse.getMessageId());
    assertEquals("1.3.6.1.5.5.2", intermediateResponse.getResponseName());
    assertEquals("value", Strings.utf8ToString(intermediateResponse.getResponseValue()));
    // Check the Control
    Map<String, Control> controls = intermediateResponse.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(intermediateResponse);
        // Check the length
        assertEquals(0x3A, 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) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) IntermediateResponseDecorator(org.apache.directory.api.ldap.codec.decorators.IntermediateResponseDecorator) IntermediateResponse(org.apache.directory.api.ldap.model.message.IntermediateResponse) 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) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 93 with DecoderException

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

the class IntermediateResponseTest method testDecodeIntermediateResponseNoName.

/**
 * Test the decoding of an IntermediateResponse without name
 */
@Test
public void testDecodeIntermediateResponseNoName() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0E);
    stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
    0x30, // LDAPMessage ::= SEQUENCE {
    0x0C, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., intermediateResponse IntermediateResponse, ...
    0x79, // IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
    0x07, // responseValue [1] OCTET STRING OPTIONAL,
    (byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<IntermediateResponseDecorator<?>> ldapMessageContainer = new LdapMessageContainer<IntermediateResponseDecorator<?>>(codec);
    // Decode the IntermediateResponse PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded IntermediateResponse PDU
    IntermediateResponse intermediateResponse = ldapMessageContainer.getMessage();
    assertEquals(1, intermediateResponse.getMessageId());
    assertEquals("", intermediateResponse.getResponseName());
    assertEquals("value", Strings.utf8ToString(intermediateResponse.getResponseValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(intermediateResponse);
        // Check the length
        assertEquals(0x0E, 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) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) IntermediateResponseDecorator(org.apache.directory.api.ldap.codec.decorators.IntermediateResponseDecorator) IntermediateResponse(org.apache.directory.api.ldap.model.message.IntermediateResponse) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 94 with DecoderException

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

the class IntermediateResponseTest method testDecodeIntermediateResponseSuccess.

/**
 * Test the decoding of a full IntermediateResponse
 */
@Test
public void testDecodeIntermediateResponseSuccess() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x1D);
    stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
    0x30, // LDAPMessage ::= SEQUENCE {
    0x1B, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., intermediateResponse IntermediateResponse, ...
    0x79, // IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
    0x16, // responseName [0] LDAPOID,
    (byte) 0x80, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // responseValue [1] OCTET STRING OPTIONAL }
    (byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<IntermediateResponseDecorator<?>> ldapMessageContainer = new LdapMessageContainer<IntermediateResponseDecorator<?>>(codec);
    // Decode the IntermediateResponse PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded IntermediateResponse PDU
    IntermediateResponse intermediateResponse = ldapMessageContainer.getMessage();
    assertEquals(1, intermediateResponse.getMessageId());
    assertEquals("1.3.6.1.5.5.2", intermediateResponse.getResponseName());
    assertEquals("value", Strings.utf8ToString(intermediateResponse.getResponseValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(intermediateResponse);
        // Check the length
        assertEquals(0x1D, 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) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) IntermediateResponseDecorator(org.apache.directory.api.ldap.codec.decorators.IntermediateResponseDecorator) IntermediateResponse(org.apache.directory.api.ldap.model.message.IntermediateResponse) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 95 with DecoderException

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

the class ModifyRequestTest method testDecodeModifyRequestBadDN.

/**
 * Test the decoding of a ModifyRequest
 */
@Test
public void testDecodeModifyRequestBadDN() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x54);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x52, 0x02, 0x01, // messageID MessageID
    0x01, 0x66, // CHOICE { ..., modifyRequest ModifyRequest, ...
    0x4d, // object LDAPDN,
    0x04, 0x20, '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', 0x30, 0x29, // modification SEQUENCE OF SEQUENCE {
    0x30, 0x11, 0x0A, 0x01, // operation ENUMERATED {
    0x02, // modification AttributeTypeAndValues } }
    0x30, // AttributeTypeAndValues ::= SEQUENCE {
    0x0c, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, // vals SET OF AttributeValue }
    0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // modification SEQUENCE OF *SEQUENCE* {
    0x14, 0x0A, 0x01, // operation ENUMERATED {
    0x00, // modification AttributeTypeAndValues } }
    0x30, // AttributeTypeAndValues ::= SEQUENCE {
    0x0f, // type AttributeDescription,
    0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET OF AttributeValue }
    0x06, 0x04, 0x04, 't', 'e', 's', 't' });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
    // Decode a ModifyRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof ModifyResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_DN_SYNTAX, ((ModifyResponseImpl) response).getLdapResult().getResultCode());
        return;
    }
    fail("We should not reach this point");
}
Also used : ModifyRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator) LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) Message(org.apache.directory.api.ldap.model.message.Message) ModifyResponseImpl(org.apache.directory.api.ldap.model.message.ModifyResponseImpl) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

DecoderException (org.apache.directory.api.asn1.DecoderException)404 ByteBuffer (java.nio.ByteBuffer)346 Test (org.junit.Test)346 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)334 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)269 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)269 EncoderException (org.apache.directory.api.asn1.EncoderException)145 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)102 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)73 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)50 Control (org.apache.directory.api.ldap.model.message.Control)40 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)38 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)36 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)34 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)28 Message (org.apache.directory.api.ldap.model.message.Message)25 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)22 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)22 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)22 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)20