Search in sources :

Example 1 with SubentriesDecorator

use of org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator in project directory-ldap-api by apache.

the class SearchRequestTest method testDecodeSearchRequestSubEntryControl.

/**
 * Test the decoding of a SearchRequest with SubEntry control.
 */
@Test
public void testDecodeSearchRequestSubEntryControl() {
    byte[] asn1BER = new byte[] { 0x30, 0x5D, 0x02, 0x01, // messageID
    0x04, 0x63, 0x33, 0x04, // baseObject: dc=my-domain,dc=com
    0x13, 'd', 'c', '=', 'm', 'y', '-', 'd', 'o', 'm', 'a', 'i', 'n', ',', 'd', 'c', '=', 'c', 'o', 'm', 0x0a, 0x01, // scope: subtree
    0x02, 0x0a, 0x01, // derefAliases: derefAlways
    0x03, 0x02, 0x01, // sizeLimit: 0
    0x00, 0x02, 0x01, // timeLimit: 0
    0x00, 0x01, 0x01, // typesOnly: false
    0x00, (byte) 0x87, // filter: (objectClass=*)
    0x0b, 'o', 'b', 'j', 'e', 'c', 't', 'C', 'l', 'a', 's', 's', 0x30, 0x00, (byte) 0xa0, // controls
    0x23, 0x30, 0x21, 0x04, 0x17, '1', '.', '3', '.', '6', '.', '1', '.', '4', '.', '1', '.', '4', '2', '0', '3', '.', '1', '.', '1', '0', '.', // SubEntry OID
    '1', 0x01, 0x01, // criticality: true
    (byte) 0xFF, 0x04, 0x03, 0x01, 0x01, // SubEntry visibility
    (byte) 0xFF };
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(asn1BER.length);
    stream.put(asn1BER);
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    assertEquals(TLVStateEnum.PDU_DECODED, ldapMessageContainer.getState());
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(4, searchRequest.getMessageId());
    assertEquals(1, searchRequest.getControls().size());
    // SubEntry Control
    String subEntryControlOID = "1.3.6.1.4.1.4203.1.10.1";
    Control subEntryControl = searchRequest.getControl(subEntryControlOID);
    assertEquals(subEntryControlOID, subEntryControl.getOid());
    assertTrue(subEntryControl.isCritical());
    assertTrue(subEntryControl instanceof SubentriesDecorator);
    assertTrue(((SubentriesDecorator) subEntryControl).getDecorated().isVisible());
    assertEquals("dc=my-domain,dc=com", searchRequest.getBase().toString());
    assertEquals(SearchScope.SUBTREE, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_ALWAYS, searchRequest.getDerefAliases());
    assertEquals(0, searchRequest.getSizeLimit());
    assertEquals(0, searchRequest.getTimeLimit());
    assertEquals(false, searchRequest.getTypesOnly());
    ExprNode filter = searchRequest.getFilter();
    assertTrue(filter instanceof PresenceNode);
    assertEquals("objectClass", ((PresenceNode) filter).getAttribute());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x5F, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(decodedPdu, encodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) PresenceNode(org.apache.directory.api.ldap.model.filter.PresenceNode) ByteBuffer(java.nio.ByteBuffer) ExprNode(org.apache.directory.api.ldap.model.filter.ExprNode) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Control(org.apache.directory.api.ldap.model.message.Control) SubentriesDecorator(org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 2 with SubentriesDecorator

use of org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator in project directory-ldap-api by apache.

the class SubEntryControlTest method testDecodeSubEntryVisibilityFalse.

/**
 * Test the decoding of a SubEntryControl with a false visibility
 */
@Test
public void testDecodeSubEntryVisibilityFalse() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x03);
    bb.put(new byte[] { // Visibility ::= BOOLEAN
    0x01, // Visibility ::= BOOLEAN
    0x01, // Visibility ::= BOOLEAN
    0x00 });
    bb.flip();
    SubentriesDecorator decorator = new SubentriesDecorator(codec);
    Subentries subentries = (Subentries) decorator.decode(bb.array());
    assertFalse(subentries.isVisible());
    // test encoding
    try {
        ByteBuffer buffer = decorator.encode(ByteBuffer.allocate(decorator.computeLength()));
        String expected = Strings.dumpBytes(bb.array());
        String decoded = Strings.dumpBytes(buffer.array());
        assertEquals(expected, decoded);
    } catch (EncoderException e) {
        fail(e.getMessage());
    }
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) SubentriesDecorator(org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator) Subentries(org.apache.directory.api.ldap.model.message.controls.Subentries) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with SubentriesDecorator

use of org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator in project directory-ldap-api by apache.

the class SubEntryControlTest method testDecodeSubEntryEmptyVisibility.

/**
 * Test the decoding of a SubEntryControl with an empty visibility
 */
@Test(expected = DecoderException.class)
public void testDecodeSubEntryEmptyVisibility() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x02);
    bb.put(new byte[] { // Visibility ::= BOOLEAN
    0x01, // Visibility ::= BOOLEAN
    0x00 });
    bb.flip();
    // Allocate a LdapMessage Container
    SubentriesDecorator decorator = new SubentriesDecorator(codec);
    decorator.decode(bb.array());
}
Also used : SubentriesDecorator(org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 4 with SubentriesDecorator

use of org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator in project directory-ldap-api by apache.

the class SubEntryControlTest method testDecodeSubEntryBad.

/**
 * Test the decoding of a bad SubEntryControl
 */
@Test(expected = DecoderException.class)
public void testDecodeSubEntryBad() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x03);
    bb.put(new byte[] { // Visibility ::= BOOLEAN
    0x02, // Visibility ::= BOOLEAN
    0x01, // Visibility ::= BOOLEAN
    0x01 });
    bb.flip();
    // Allocate a LdapMessage Container
    SubentriesDecorator decorator = new SubentriesDecorator(codec);
    decorator.decode(bb.array());
}
Also used : SubentriesDecorator(org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 5 with SubentriesDecorator

use of org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator in project directory-ldap-api by apache.

the class SubEntryControlTest method testDecodeSubEntryVisibilityTrue.

/**
 * Test the decoding of a SubEntryControl with a true visibility
 */
@Test
public void testDecodeSubEntryVisibilityTrue() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x03);
    bb.put(new byte[] { // Visibility ::= BOOLEAN
    0x01, // Visibility ::= BOOLEAN
    0x01, // Visibility ::= BOOLEAN
    (byte) 0xFF });
    bb.flip();
    SubentriesDecorator decorator = new SubentriesDecorator(codec);
    Subentries subentries = (Subentries) decorator.decode(bb.array());
    assertTrue(subentries.isVisible());
    // test encoding
    try {
        ByteBuffer buffer = decorator.encode(ByteBuffer.allocate(decorator.computeLength()));
        String expected = Strings.dumpBytes(bb.array());
        String decoded = Strings.dumpBytes(buffer.array());
        assertEquals(expected, decoded);
    } catch (EncoderException e) {
        fail(e.getMessage());
    }
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) SubentriesDecorator(org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator) Subentries(org.apache.directory.api.ldap.model.message.controls.Subentries) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

ByteBuffer (java.nio.ByteBuffer)5 SubentriesDecorator (org.apache.directory.api.ldap.codec.controls.search.subentries.SubentriesDecorator)5 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)5 Test (org.junit.Test)5 EncoderException (org.apache.directory.api.asn1.EncoderException)3 Subentries (org.apache.directory.api.ldap.model.message.controls.Subentries)2 DecoderException (org.apache.directory.api.asn1.DecoderException)1 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)1 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)1 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)1 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)1 PresenceNode (org.apache.directory.api.ldap.model.filter.PresenceNode)1 Control (org.apache.directory.api.ldap.model.message.Control)1 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)1