Search in sources :

Example 1 with EncoderException

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

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyAttributeValueWithControls.

/**
 * Test the decoding of a SearchResultEntry with an empty attribute value
 * with controls
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributeValueWithControls() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x56);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x54, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x32, // objectName LDAPDN,
    0x04, 0x1b, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x13, 0x30, 0x11, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // vals SET OF AttributeValue }
    0x31, 0x02, // AttributeValue ::= OCTET STRING
    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 BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultEntry.getMessageId());
    assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
    Entry entry = searchResultEntry.getEntry();
    assertEquals(1, entry.size());
    for (int i = 0; i < entry.size(); i++) {
        Attribute attribute = entry.get("objectclass");
        assertEquals(Strings.toLowerCaseAscii("objectClass"), Strings.toLowerCaseAscii(attribute.getUpId()));
        assertTrue(attribute.contains(""));
    }
    // Check the Control
    Map<String, Control> controls = searchResultEntry.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(searchResultEntry);
        // Check the length
        assertEquals(0x56, 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) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) 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) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 2 with EncoderException

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

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyVals.

/**
 * Test the decoding of a SearchResultEntry with an empty vals
 */
@Test
public void testDecodeSearchResultEntryEmptyVals() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x37);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x35, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x30, // objectName LDAPDN,
    0x04, 0x1b, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x11, 0x30, 0x0F, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x31, 0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultEntry.getMessageId());
    assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
    Entry entry = searchResultEntry.getEntry();
    assertEquals(1, entry.size());
    for (int i = 0; i < entry.size(); i++) {
        Attribute attribute = entry.get("objectclass");
        assertEquals(Strings.toLowerCaseAscii("objectClass"), Strings.toLowerCaseAscii(attribute.getUpId()));
        assertEquals(0, attribute.size());
    }
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x37, 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) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with EncoderException

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

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyVals2.

/**
 * Test the decoding of a SearchResultEntry with two empty vals
 */
@Test
public void testDecodeSearchResultEntryEmptyVals2() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x48);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x46, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x41, // objectName LDAPDN,
    0x04, 0x1b, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x22, 0x30, 0x0F, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x31, 0x00, 0x30, 0x0F, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 'z', 'z', 0x31, 0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultEntry.getMessageId());
    assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
    Entry entry = searchResultEntry.getEntry();
    assertEquals(2, entry.size());
    Attribute attribute = entry.get("objectclass");
    assertEquals(Strings.toLowerCaseAscii("objectClass"), Strings.toLowerCaseAscii(attribute.getUpId()));
    assertEquals(0, attribute.size());
    attribute = entry.get("objectclazz");
    assertEquals(Strings.toLowerCaseAscii("objectClazz"), Strings.toLowerCaseAscii(attribute.getUpId()));
    assertEquals(0, attribute.size());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x48, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.length(), decodedPdu.length());
    } 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) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 4 with EncoderException

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

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyAttributes.

/**
 * Test the decoding of an SearchResultEntry with an empty attributes
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributes() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x26);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x24, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x1F, // objectName LDAPDN,
    0x04, 0x1B, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x00 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultEntry.getMessageId());
    assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
    Entry entry = searchResultEntry.getEntry();
    assertEquals(0, entry.size());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x26, 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) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 5 with EncoderException

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

the class SearchResultEntryTest method testDecodeSearchResultEntrySuccess.

/**
 * Test the decoding of a SearchResultEntry
 */
@Test
public void testDecodeSearchResultEntrySuccess() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x50);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x4e, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x49, // objectName LDAPDN,
    0x04, 0x1b, 'o', 'u', '=', 'c', 'o', 'n', 't', 'a', 'c', 't', 's', ',', 'd', 'c', '=', 'i', 'k', 't', 'e', 'k', ',', 'd', 'c', '=', 'c', 'o', 'm', // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x30, 0x2a, 0x30, 0x28, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', // vals SET OF AttributeValue }
    0x31, 0x19, // AttributeValue ::= OCTET STRING
    0x04, 0x03, 't', 'o', 'p', // AttributeValue ::= OCTET STRING
    0x04, 0x12, 'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'a', 'l', 'U', 'n', 'i', 't' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchResultEntry searchResultEntry = ldapMessageContainer.getMessage();
    assertEquals(1, searchResultEntry.getMessageId());
    assertEquals("ou=contacts,dc=iktek,dc=com", searchResultEntry.getObjectName().toString());
    Entry entry = searchResultEntry.getEntry();
    assertEquals(1, entry.size());
    for (int i = 0; i < entry.size(); i++) {
        Attribute attribute = entry.get("objectclass");
        assertEquals(Strings.toLowerCaseAscii("objectClass"), Strings.toLowerCaseAscii(attribute.getUpId()));
        assertTrue(attribute.contains("top"));
        assertTrue(attribute.contains("organizationalUnit"));
    }
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x50, 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) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) 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