Search in sources :

Example 61 with DecoderException

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

the class AddRequestTest method testDecodeAddRequestNullBody.

/**
 * Test the decoding of a AddRequest with a null body
 */
@Test
public void testDecodeAddRequestNullBody() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x07);
    stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
    0x30, // LDAPMessage ::= SEQUENCE {
    0x05, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x00 });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) 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 62 with DecoderException

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

the class AddRequestTest method testDecodeAddRequestEmptyAttributeValueWithControl.

/**
 * Test the decoding of a AddRequest with a empty attributeList and a
 * control
 */
@Test
public void testDecodeAddRequestEmptyAttributeValueWithControl() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x51);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x4F, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x2D, // 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', // attributes AttributeList }
    0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
    0x09, 0x30, // attribute 1
    0x07, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, 0x02, 0x04, 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 LdapMessage Container
    LdapMessageContainer<AddRequestDecorator> container = new LdapMessageContainer<AddRequestDecorator>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    AddRequest addRequest = container.getMessage();
    // Check the decoded message
    assertEquals(1, addRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", addRequest.getEntryDn().toString());
    Entry entry = addRequest.getEntry();
    assertEquals(1, entry.size());
    Attribute attribute = entry.get("l");
    assertEquals("l", Strings.toLowerCaseAscii(attribute.getId()));
    for (Value value : attribute) {
        assertEquals("", value.getValue());
    }
    // Check the Control
    Map<String, Control> controls = addRequest.getControls();
    assertEquals(1, controls.size());
    assertTrue(addRequest.hasControl("2.16.840.1.113730.3.4.2"));
    @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(addRequest);
        // Check the length
        assertEquals(0x51, 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) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ByteBuffer(java.nio.ByteBuffer) AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Entry(org.apache.directory.api.ldap.model.entry.Entry) 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) Value(org.apache.directory.api.ldap.model.entry.Value) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) AddRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 63 with DecoderException

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

the class AddRequestTest method testDecodeAddRequestbadDN.

/**
 * Test the decoding of a AddRequest
 */
@Test
public void testDecodeAddRequestbadDN() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x59);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x57, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x52, // 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', // attributes AttributeList }
    0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
    0x2E, 0x30, // attribute 1
    0x0c, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, // vals SET OF AttributeValue }
    0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // attribute 2
    0x1E, // type AttributeDescription,
    0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET
    0x15, // }
    0x04, 0x05, 't', 'e', 's', 't', '1', 0x04, 0x05, 't', 'e', 's', 't', '2', 0x04, 0x05, 't', 'e', 's', 't', '3' });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof AddResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_DN_SYNTAX, ((AddResponseImpl) 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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Message(org.apache.directory.api.ldap.model.message.Message) AddResponseImpl(org.apache.directory.api.ldap.model.message.AddResponseImpl) 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 64 with DecoderException

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

the class AddRequestTest method testDecodeAddRequestNullEntry.

/**
 * Test the decoding of a AddRequest with a null entry
 */
@Test
public void testDecodeAddRequestNullEntry() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x39);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x37, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x26, // entry LDAPDN,
    0x04, 0x00, // attributes AttributeList }
    0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
    0x2E, 0x30, // attribute 1
    0x0c, 0x04, 0x01, // type AttributeDescription,
    'l', 0x31, // vals SET OF AttributeValue }
    0x07, 0x04, 0x05, 'P', 'a', 'r', 'i', 's', 0x30, // attribute 2
    0x1E, // type AttributeDescription,
    0x04, 0x05, 'a', 't', 't', 'r', 's', 0x31, // vals SET
    0x15, // }
    0x04, 0x05, 't', 'e', 's', 't', '1', 0x04, 0x05, 't', 'e', 's', 't', '2', 0x04, 0x05, 't', 'e', 's', 't', '3' });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof AddResponseImpl);
        assertEquals(ResultCodeEnum.NAMING_VIOLATION, ((AddResponseImpl) 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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Message(org.apache.directory.api.ldap.model.message.Message) AddResponseImpl(org.apache.directory.api.ldap.model.message.AddResponseImpl) 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 65 with DecoderException

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

the class AddRequestTest method testDecodeAddRequestNullVals.

/**
 * Test the decoding of a AddRequest with a empty attributeList
 */
@Test
public void testDecodeAddRequestNullVals() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x32);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x30, 0x02, 0x01, // messageID MessageID
    0x01, 0x68, // CHOICE { ..., addRequest AddRequest, ...
    0x2B, // 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', // attributes AttributeList }
    0x30, // AttributeList ::= SEQUENCE OF SEQUENCE {
    0x07, 0x30, // attribute 1
    0x05, 0x04, 0x01, // type AttributeDescription,
    'A', 0x31, 0x00 });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a AddRequest message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        assertTrue(true);
        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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) 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