Search in sources :

Example 16 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntryTypeAlone.

/**
 * Test the decoding of a SearchResultEntry with a type alone
 */
@Test
public void testDecodeSearchResultEntryTypeAlone() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x35);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x33, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x2E, // 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, 0x0F, 0x30, 0x0D, // type AttributeDescription,
    0x04, 0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's' });
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    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) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 17 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntryEmptyAttributeValue.

/**
 * Test the decoding of a SearchResultEntry with an empty attribute value
 */
@Test
public void testDecodeSearchResultEntryEmptyAttributeValue() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x39);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x37, 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 });
    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 encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x39, 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 18 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntryObjectNameAlone.

/**
 * Test the decoding of an SearchResultEntry with an object name alone
 */
@Test
public void testDecodeSearchResultEntryObjectNameAlone() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x24);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x22, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x1D, // 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' });
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchResultEntryDecorator> ldapMessageContainer = new LdapMessageContainer<SearchResultEntryDecorator>(codec);
    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) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntryDecorator(org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 19 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntry2AttrsSuccess.

/**
 * Test the decoding of a SearchResultEntry
 */
@Test
public void testDecodeSearchResultEntry2AttrsSuccess() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x7b);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x79, 0x02, 0x01, // messageID MessageID
    0x01, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x74, // 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, 0x55, 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', 0x30, 0x29, // type AttributeDescription,
    0x04, 0x0c, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', '2', // 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' });
    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());
    String[] expectedAttributes = new String[] { "objectClass", "objectClass2" };
    for (int i = 0; i < expectedAttributes.length; i++) {
        Attribute attribute = entry.get(expectedAttributes[i]);
        assertEquals(Strings.toLowerCaseAscii(expectedAttributes[i]), 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(0x7B, bb.limit());
    // We can't compare the encodings, the order of the attributes has
    // changed
    } 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) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 20 with SearchResultEntryDecorator

use of org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator in project directory-ldap-api by apache.

the class SearchResultEntryTest method testDecodeSearchResultEntrySuccessWithFollowingMessage.

/**
 * Test the decoding of a SearchResultEntry with more bytes to be decoded at
 * the end
 */
@Test
public void testDecodeSearchResultEntrySuccessWithFollowingMessage() throws NamingException {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x66);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x5F, 0x02, 0x01, // messageID MessageID
    0x02, 0x64, // CHOICE { ..., searchResEntry SearchResultEntry,
    0x5A, // objectName LDAPDN,
    0x04, 0x13, 'u', 'i', 'd', '=', 'a', 'd', 'm', 'i', 'n', ',', 'o', 'u', '=', 's', 'y', 's', 't', 'e', 'm', // attributes PartialAttributeList }
    0x30, // PartialAttributeList ::= SEQUENCE OF SEQUENCE {
    0x43, 0x30, 0x41, // type AttributeDescription,
    0x04, 0x0B, 'o', 'b', 'j', 'e', 'c', 't', 'c', 'l', 'a', 's', 's', 0x31, // vals
    0x32, // AttributeValue ::= OCTET STRING
    0x04, 0x0D, 'i', 'n', 'e', 't', 'O', 'r', 'g', 'P', 'e', 'r', 's', 'o', 'n', // AttributeValue ::= OCTET STRING
    0x04, 0x14, 'o', 'r', 'g', 'a', 'n', 'i', 'z', 'a', 't', 'i', 'o', 'n', 'a', 'l', 'P', 'e', 'r', 's', 'o', 'n', // AttributeValue ::= OCTET STRING
    0x04, 0x06, 'p', 'e', 'r', 's', 'o', 'n', // AttributeValue ::= OCTET STRING
    0x04, 0x03, 't', 'o', 'p', 0x30, // Start of the next
    0x45, // message
    0x02, 0x01, // messageID MessageID ...
    0x02 });
    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(2, searchResultEntry.getMessageId());
    assertEquals("uid=admin,ou=system", 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("person"));
        assertTrue(attribute.contains("organizationalPerson"));
        assertTrue(attribute.contains("inetOrgPerson"));
    }
    // Check that the next bytes is the first of the next PDU
    assertEquals(0x30, stream.get(stream.position()));
    assertEquals(0x45, stream.get(stream.position() + 1));
    assertEquals(0x02, stream.get(stream.position() + 2));
    assertEquals(0x01, stream.get(stream.position() + 3));
    assertEquals(0x02, stream.get(stream.position() + 4));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchResultEntry);
        // Check the length
        assertEquals(0x61, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        // We have to supress the last 5 chars from the decodedPDU, as they
        // belongs to the next message.
        assertEquals(encodedPdu, decodedPdu.substring(0, 0x61 * 5));
    } 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

SearchResultEntryDecorator (org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator)21 DecoderException (org.apache.directory.api.asn1.DecoderException)18 ByteBuffer (java.nio.ByteBuffer)16 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)16 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)16 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)16 Test (org.junit.Test)16 SearchResultEntry (org.apache.directory.api.ldap.model.message.SearchResultEntry)10 EncoderException (org.apache.directory.api.asn1.EncoderException)9 Entry (org.apache.directory.api.ldap.model.entry.Entry)9 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)8 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)3 Control (org.apache.directory.api.ldap.model.message.Control)3 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)2 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)2 AbandonRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator)1 AddRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AddRequestDecorator)1 AddResponseDecorator (org.apache.directory.api.ldap.codec.decorators.AddResponseDecorator)1 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)1 BindResponseDecorator (org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator)1