Search in sources :

Example 1 with PagedResultsDecorator

use of org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator in project directory-ldap-api by apache.

the class BindResponseTest method testDecodeBindResponseWithControlSuccess.

/**
 * Test the decoding of a BindResponse with a control
 */
@Test
public void testDecodeBindResponseWithControlSuccess() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x3C);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x3A, 0x02, 0x01, // messageID MessageID
    0x01, 0x61, // CHOICE { ..., bindResponse BindResponse, ...
    0x07, // COMPONENTS OF LDAPResult,
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00, // serverSaslCreds [7] OCTET STRING OPTIONAL }
    (byte) 0xa0, // controls
    0x2C, 0x30, // The PagedSearchControl
    0x2A, 0x04, // Oid : 1.2.840.113556.1.4.319
    0x16, 0x31, 0x2e, 0x32, 0x2e, 0x38, 0x34, 0x30, 0x2e, 0x31, 0x31, 0x33, 0x35, 0x35, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x33, 0x31, 0x39, 0x01, 0x01, // criticality: false
    (byte) 0xff, 0x04, 0x0D, 0x30, 0x0B, 0x02, 0x01, // Size = 5, cookie = "abcdef"
    0x05, 0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<BindResponseDecorator> container = new LdapMessageContainer<BindResponseDecorator>(codec);
    // Decode the BindResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded BindResponse
    BindResponse bindResponse = container.getMessage();
    assertEquals(1, bindResponse.getMessageId());
    assertEquals(ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode());
    assertEquals("", bindResponse.getLdapResult().getMatchedDn().getName());
    assertEquals("", bindResponse.getLdapResult().getDiagnosticMessage());
    // Check the Control
    Map<String, Control> controls = bindResponse.getControls();
    assertEquals(1, controls.size());
    Control control = controls.get("1.2.840.113556.1.4.319");
    assertEquals("1.2.840.113556.1.4.319", control.getOid());
    assertTrue(control instanceof PagedResultsDecorator);
    PagedResultsDecorator pagedSearchControl = (PagedResultsDecorator) control;
    assertEquals(5, pagedSearchControl.getSize());
    assertTrue(Arrays.equals(Strings.getBytesUtf8("abcdef"), pagedSearchControl.getCookie()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(bindResponse);
        // Check the length
        assertEquals(0x3C, 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) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) PagedResultsDecorator(org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BindResponseDecorator(org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 2 with PagedResultsDecorator

use of org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator in project directory-ldap-api by apache.

the class PagedSearchControlTest method testEncodePagedSearchControlEmptyCookie.

/**
 * Test encoding of a PagedSearchControl with a empty cookie
 */
@Test
public void testEncodePagedSearchControlEmptyCookie() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x07);
    bb.put(new byte[] { // realSearchControlValue ::= SEQUENCE {
    0x30, // realSearchControlValue ::= SEQUENCE {
    0x05, 0x02, 0x01, // size INTEGER,
    0x20, 0x04, // cookie OCTET STRING,
    0x00 });
    bb.flip();
    PagedResultsDecorator decorator = new PagedResultsDecorator(codec);
    PagedResults pagedSearch = (PagedResults) decorator.decode(bb.array());
    assertEquals(32, pagedSearch.getSize());
    assertNull(pagedSearch.getCookie());
    PagedResultsDecorator ctrl = new PagedResultsDecorator(codec);
    ctrl.setSize(32);
    ctrl.setCookie(null);
    ByteBuffer buffer = ctrl.encode(ByteBuffer.allocate(ctrl.computeLength()));
    String decoded = Strings.dumpBytes(buffer.array());
    String expected = Strings.dumpBytes(bb.array());
    assertEquals(expected, decoded);
}
Also used : PagedResults(org.apache.directory.api.ldap.model.message.controls.PagedResults) PagedResultsDecorator(org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 3 with PagedResultsDecorator

use of org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator in project directory-ldap-api by apache.

the class PagedSearchControlTest method testDecodePagedSearchRequestNoCookie.

/**
 * Test the decoding of a PagedSearchControl with no cookie
 */
@Test(expected = DecoderException.class)
public void testDecodePagedSearchRequestNoCookie() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x05);
    bb.put(new byte[] { // realSearchControlValue ::= SEQUENCE {
    0x30, // realSearchControlValue ::= SEQUENCE {
    0x03, 0x02, 0x01, // size INTEGER,
    0x20 });
    bb.flip();
    PagedResultsDecorator decorator = new PagedResultsDecorator(codec);
    decorator.decode(bb.array());
}
Also used : PagedResultsDecorator(org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 4 with PagedResultsDecorator

use of org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator in project directory-ldap-api by apache.

the class PagedSearchControlTest method testEncodePagedSearchControlEmptySize.

/**
 * Test encoding of a PagedSearchControl with a empty size
 */
@Test(expected = DecoderException.class)
public void testEncodePagedSearchControlEmptySize() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x0a);
    bb.put(new byte[] { // realSearchControlValue ::= SEQUENCE {
    0x30, // realSearchControlValue ::= SEQUENCE {
    0x08, 0x02, // size INTEGER,
    0x00, 0x04, 0x04, 't', 'e', 's', // cookie OCTET STRING,
    't' });
    bb.flip();
    PagedResultsDecorator decorator = new PagedResultsDecorator(codec);
    decorator.decode(bb.array());
}
Also used : PagedResultsDecorator(org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 5 with PagedResultsDecorator

use of org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator in project directory-ldap-api by apache.

the class PagedSearchControlTest method testEncodePagedSearchControlNegativeSize.

/**
 * Test encoding of a PagedSearchControl with a negative size
 */
@Test
public void testEncodePagedSearchControlNegativeSize() throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(0x0b);
    bb.put(new byte[] { // realSearchControlValue ::= SEQUENCE {
    0x30, // realSearchControlValue ::= SEQUENCE {
    0x09, 0x02, 0x01, // size INTEGER,
    (byte) 0xFF, 0x04, 0x04, 't', 'e', 's', // cookie OCTET STRING,
    't' });
    bb.flip();
    PagedResultsDecorator decorator = new PagedResultsDecorator(codec);
    PagedResults pagedSearch = (PagedResults) decorator.decode(bb.array());
    assertEquals(Integer.MAX_VALUE, pagedSearch.getSize());
    assertTrue(Arrays.equals(Strings.getBytesUtf8("test"), pagedSearch.getCookie()));
    bb.flip();
    PagedResultsDecorator ctrl = new PagedResultsDecorator(codec);
    ctrl.setSize(-1);
    ctrl.setCookie(Strings.getBytesUtf8("test"));
    ByteBuffer buffer = ctrl.encode(ByteBuffer.allocate(ctrl.computeLength()));
    String decoded = Strings.dumpBytes(buffer.array());
    String expected = Strings.dumpBytes(bb.array());
    assertEquals(expected, decoded);
}
Also used : PagedResults(org.apache.directory.api.ldap.model.message.controls.PagedResults) PagedResultsDecorator(org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8 PagedResultsDecorator (org.apache.directory.api.ldap.codec.controls.search.pagedSearch.PagedResultsDecorator)8 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)8 Test (org.junit.Test)8 PagedResults (org.apache.directory.api.ldap.model.message.controls.PagedResults)3 DecoderException (org.apache.directory.api.asn1.DecoderException)1 EncoderException (org.apache.directory.api.asn1.EncoderException)1 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)1 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)1 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)1 BindResponseDecorator (org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator)1 BindResponse (org.apache.directory.api.ldap.model.message.BindResponse)1 Control (org.apache.directory.api.ldap.model.message.Control)1