Search in sources :

Example 6 with CodecControl

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

the class AddResponseTest method testDecodeAddResponseSuccessWithControl.

/**
 * Test the decoding of a AddResponse with a control
 */
@Test
public void testDecodeAddResponseSuccessWithControl() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2B);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x29, 0x02, 0x01, // messageID MessageID
    0x01, 0x69, // CHOICE { ..., addResponse AddResponse, ...
    0x07, // AddResponse ::= [APPLICATION 9] LDAPResult
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    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<AddResponseDecorator> container = new LdapMessageContainer<AddResponseDecorator>(codec);
    // Decode the AddResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded AddResponse
    AddResponse addResponse = container.getMessage();
    assertEquals(1, addResponse.getMessageId());
    assertEquals(ResultCodeEnum.SUCCESS, addResponse.getLdapResult().getResultCode());
    assertEquals("", addResponse.getLdapResult().getMatchedDn().getName());
    assertEquals("", addResponse.getLdapResult().getDiagnosticMessage());
    // Check the Control
    Map<String, Control> controls = addResponse.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()));
    try {
        /**
         * The encoder instance
         */
        ByteBuffer bb = encoder.encodeMessage(addResponse);
        // Check the length
        assertEquals(0x02B, 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) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) ByteBuffer(java.nio.ByteBuffer) AddResponseDecorator(org.apache.directory.api.ldap.codec.decorators.AddResponseDecorator) 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) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 7 with CodecControl

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

the class BindRequestPerfTest method testDecodeBindRequestSimpleNoControlsPerf.

/**
 * Test the decoding of a BindRequest with Simple authentication and no
 * controls
 */
@Test
@Ignore
public void testDecodeBindRequestSimpleNoControlsPerf() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x52);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x50, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name LDAPDN,
    0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', (byte) 0x80, // authentication AuthenticationChoice
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd', (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<BindRequestDecorator> container = new LdapMessageContainer<BindRequestDecorator>(codec);
    // Decode the BindRequest PDU
    try {
        int nbLoops = 1000000;
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < nbLoops; i++) {
            ldapDecoder.decode(stream, container);
            container.clean();
            stream.flip();
        }
        long t1 = System.currentTimeMillis();
        System.out.println("testDecodeBindRequestSimpleNoControlsPerf, " + nbLoops + " loops, Delta = " + (t1 - t0));
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded BindRequest
    BindRequest bindRequest = container.getMessage();
    assertEquals(1, bindRequest.getMessageId());
    assertTrue(bindRequest.isVersion3());
    assertEquals("uid=akarasulu,dc=example,dc=com", bindRequest.getName().toString());
    assertTrue(bindRequest.isSimple());
    assertEquals("password", Strings.utf8ToString(bindRequest.getCredentials()));
    // Check the Control
    Map<String, Control> controls = bindRequest.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(bindRequest);
        // Check the length
        assertEquals(0x52, 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) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) ByteBuffer(java.nio.ByteBuffer) 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) OpaqueControl(org.apache.directory.api.ldap.model.message.controls.OpaqueControl) BindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 8 with CodecControl

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

the class BindRequestTest method testDecodeBindRequestEmptyMechanismWithControls.

/**
 * Test the decoding of a BindRequest with an empty mechanisms with controls
 */
@Test
public void testDecodeBindRequestEmptyMechanismWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2D);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x2B, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x09, 0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, 0x00, (byte) 0xA3, 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<BindRequestDecorator> container = new LdapMessageContainer<BindRequestDecorator>(codec);
    // Decode the BindRequest PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded BindRequest
    BindRequest bindRequest = container.getMessage();
    assertEquals(1, bindRequest.getMessageId());
    assertTrue(bindRequest.isVersion3());
    assertEquals("", bindRequest.getName());
    assertFalse(bindRequest.isSimple());
    assertEquals("", bindRequest.getSaslMechanism());
    assertEquals("", Strings.utf8ToString(bindRequest.getCredentials()));
    // Check the Control
    Map<String, Control> controls = bindRequest.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(bindRequest);
        // Check the length
        assertEquals(0x2D, 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) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) ByteBuffer(java.nio.ByteBuffer) 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) BindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 9 with CodecControl

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

the class BindResponseTest method testDecodeBindResponseServerSASLEmptyCredentialsWithControls.

/**
 * Test the decoding of a BindResponse with an empty credentials with
 * controls
 */
@Test
public void testDecodeBindResponseServerSASLEmptyCredentialsWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2D);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x2B, 0x02, 0x01, // messageID MessageID
    0x01, 0x61, // CHOICE { ..., bindResponse BindResponse, ...
    0x09, // COMPONENTS OF LDAPResult,
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00, // referral [3] Referral OPTIONAL }
    (byte) 0x87, // serverSaslCreds [7] OCTET STRING
    0x00, // OPTIONAL }
    (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<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());
    assertEquals("", Strings.utf8ToString(bindResponse.getServerSaslCreds()));
    // Check the Control
    Map<String, Control> controls = bindResponse.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(bindResponse);
        // Check the length
        assertEquals(0x2D, 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) BindResponseDecorator(org.apache.directory.api.ldap.codec.decorators.BindResponseDecorator) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) ByteBuffer(java.nio.ByteBuffer) 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) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 10 with CodecControl

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

the class CompareRequestTest method testDecodeCompareRequestWithControls.

/**
 * Test the decoding of an compare request with controls
 */
@Test
public void testDecodeCompareRequestWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x55);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x53, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., compareRequest CompareRequest, ...
    0x6E, // CompareRequest ::= [APPLICATION 14] SEQUENCE {
    0x31, // 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', // ava AttributeValueAssertion }
    0x30, // AttributeValueAssertion ::= SEQUENCE {
    0x0D, // attributeDesc AttributeDescription,
    0x04, 0x04, 't', 'e', 's', 't', // assertionValue AssertionValue }
    0x04, 0x05, 'v', 'a', 'l', 'u', 'e', (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<CompareRequestDecorator> container = new LdapMessageContainer<CompareRequestDecorator>(codec);
    // Decode the CompareRequest PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Ceck the decoded CompareRequest PDU
    CompareRequest compareRequest = container.getMessage();
    assertEquals(1, compareRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", compareRequest.getName().toString());
    assertEquals("test", compareRequest.getAttributeId());
    assertEquals("value", compareRequest.getAssertionValue().toString());
    // Check the Control
    Map<String, Control> controls = compareRequest.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(compareRequest);
        // Check the length
        assertEquals(0x55, 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) ByteBuffer(java.nio.ByteBuffer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) CompareRequest(org.apache.directory.api.ldap.model.message.CompareRequest) 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) CompareRequestDecorator(org.apache.directory.api.ldap.codec.decorators.CompareRequestDecorator) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)34 Control (org.apache.directory.api.ldap.model.message.Control)34 ByteBuffer (java.nio.ByteBuffer)33 DecoderException (org.apache.directory.api.asn1.DecoderException)32 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)32 Test (org.junit.Test)32 EncoderException (org.apache.directory.api.asn1.EncoderException)31 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)31 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)31 ExtendedResponseDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)4 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)4 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)3 Entry (org.apache.directory.api.ldap.model.entry.Entry)3 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)3 AbandonRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator)2 ExtendedRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator)2 IntermediateResponseDecorator (org.apache.directory.api.ldap.codec.decorators.IntermediateResponseDecorator)2 ModifyDnRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyDnRequestDecorator)2 SearchResultEntryDecorator (org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator)2 UpdateControls (org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls)2