Search in sources :

Example 26 with Message

use of org.apache.directory.api.ldap.model.message.Message in project directory-ldap-api by apache.

the class ModifyRequestTest method testDecodeModifyRequestAddOperationModificationEmptyType.

/**
 * Test the decoding of a ModifyRequest with an add operation, and a
 * modification with an empty type
 */
@Test
public void testDecodeModifyRequestAddOperationModificationEmptyType() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x34);
    stream.put(new byte[] { 0x30, // LdapMessage
    0x32, 0x02, 0x01, // Message ID : 49
    0x31, 0x66, // ModifyRequest
    0x2D, 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, 0x09, 0x30, 0x07, 0x0A, 0x01, 0x00, 0x30, 0x02, 0x04, 0x00 });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ModifyRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyRequestDecorator>(codec);
    // Decode a ModifyRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
        fail("We should never reach this point !!!");
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof ModifyResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, ((ModifyResponseImpl) response).getLdapResult().getResultCode());
        return;
    }
}
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)

Example 27 with Message

use of org.apache.directory.api.ldap.model.message.Message in project directory-ldap-api by apache.

the class ModifyDNRequestTest method testDecodeModifyDNRequestBadNewSuperior.

/**
 * Test the decoding of a bad Rdn ModifyDNRequest
 */
@Test
public void testDecodeModifyDNRequestBadNewSuperior() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x48);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x46, 0x02, 0x01, // messageID MessageID
    0x01, 0x6C, // CHOICE { ..., modifyDNRequest ModifyDNRequest,
    0x41, // entry 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', // newrdn RelativeLDAPDN,
    0x04, 0x0F, 'c', 'n', '=', 't', 'e', 's', 't', 'D', 'N', 'M', 'o', 'd', 'i', 'f', 'y', 0x01, 0x01, // deleteoldrdn BOOLEAN,
    0x00, // newSuperior [0] LDAPDN OPTIONAL }
    (byte) 0x80, 0x09, 'o', 'u', ':', 's', 'y', 's', 't', 'e', 'm' });
    stream.flip();
    // Allocate a ModifyRequest Container
    LdapMessageContainer<ModifyDnRequestDecorator> ldapMessageContainer = new LdapMessageContainer<ModifyDnRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof ModifyDnResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_DN_SYNTAX, ((ModifyDnResponseImpl) response).getLdapResult().getResultCode());
        return;
    }
    fail("We should not reach this point");
}
Also used : 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) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ModifyDnResponseImpl(org.apache.directory.api.ldap.model.message.ModifyDnResponseImpl) ModifyDnRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ModifyDnRequestDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 28 with Message

use of org.apache.directory.api.ldap.model.message.Message in project directory-ldap-api by apache.

the class LdapDecoderTest method testDecodeBadLengthTooSmall.

/**
 * Test the decoding of a PDU with a bad Length. The first TLV has a length
 * of 0x32 when the PDU is 0x33 bytes long.
 */
@Test
public void testDecodeBadLengthTooSmall() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x35);
    stream.put(new byte[] { // Length should be 0x33...
    0x30, // LDAPMessage ::=SEQUENCE {
    0x32, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name LDAPDN,
    0x1F, '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, // authentication
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<MessageDecorator<? extends Message>> ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a BindRequest PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertEquals("ERR_01003_VALUE_LENGTH_ABOVE_EXPECTED_LENGTH The current Value length 48 is above the expected length 47", de.getMessage());
        return;
    }
    fail("Should never reach this point..");
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) 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 29 with Message

use of org.apache.directory.api.ldap.model.message.Message in project directory-ldap-api by apache.

the class LdapDecoderTest method decode.

/**
 * Decode an incoming buffer into LDAP messages. The result can be 0, 1 or many
 * LDAP messages, which will be stored into the array the caller has created.
 *
 * @param buffer The incoming byte buffer
 * @param messageContainer The LdapMessageContainer which will be used to store the
 * message being decoded. If the message is not fully decoded, the ucrrent state
 * is stored into this container
 * @param decodedMessages The list of decoded messages
 * @throws Exception If the decoding failed
 */
private void decode(ByteBuffer buffer, LdapMessageContainer<MessageDecorator<? extends Message>> messageContainer, List<Message> decodedMessages) throws DecoderException {
    buffer.mark();
    while (buffer.hasRemaining()) {
        try {
            asn1Decoder.decode(buffer, messageContainer);
            if (messageContainer.getState() == TLVStateEnum.PDU_DECODED) {
                Message message = messageContainer.getMessage();
                decodedMessages.add(message);
                messageContainer.clean();
            }
        } catch (DecoderException de) {
            buffer.clear();
            messageContainer.clean();
            if (de instanceof ResponseCarryingException) {
                // Transform the DecoderException message to a MessageException
                ResponseCarryingMessageException rcme = new ResponseCarryingMessageException(de.getMessage());
                rcme.setResponse(((ResponseCarryingException) de).getResponse());
                throw rcme;
            } else {
                // TODO : This is certainly not the way we should handle such an exception !
                throw new ResponseCarryingException(de.getMessage());
            }
        }
    }
}
Also used : 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) ResponseCarryingMessageException(org.apache.directory.api.ldap.model.exception.ResponseCarryingMessageException)

Example 30 with Message

use of org.apache.directory.api.ldap.model.message.Message in project directory-ldap-api by apache.

the class LdapDecoderTest method testDecodeFull.

/**
 * Test the decoding of a full PDU
 */
@Test
public void testDecodeFull() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    LdapMessageContainer<MessageDecorator<? extends Message>> container = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    ByteBuffer stream = ByteBuffer.allocate(0x35);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x33, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name LDAPDN,
    0x1F, '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, // authentication
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
    stream.flip();
    // Decode a BindRequest PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, container.getState());
    // Check the decoded PDU
    BindRequest bindRequest = (BindRequest) container.getMessage();
    assertEquals(1, bindRequest.getMessageId());
    assertTrue(bindRequest.isVersion3());
    assertEquals("uid=akarasulu,dc=example,dc=com", bindRequest.getName().toString());
    assertTrue(bindRequest.isSimple());
    assertEquals("password", Strings.utf8ToString(bindRequest.getCredentials()));
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

Message (org.apache.directory.api.ldap.model.message.Message)30 DecoderException (org.apache.directory.api.asn1.DecoderException)26 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)24 ByteBuffer (java.nio.ByteBuffer)22 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)21 Test (org.junit.Test)21 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)20 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)16 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)11 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)3 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)3 ModifyDnRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyDnRequestDecorator)3 AddResponseImpl (org.apache.directory.api.ldap.model.message.AddResponseImpl)3 ModifyDnResponseImpl (org.apache.directory.api.ldap.model.message.ModifyDnResponseImpl)3 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)2 ArrayList (java.util.ArrayList)2 SchemaBinaryAttributeDetector (org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector)2 CompareRequestDecorator (org.apache.directory.api.ldap.codec.decorators.CompareRequestDecorator)2