Search in sources :

Example 36 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class BindRequestPerfTest method testEncodeBindRequestPerf.

/**
 * Test the decoding of a BindRequest with Simple authentication and no
 * controls
 */
@Test
@Ignore
public void testEncodeBindRequestPerf() throws Exception {
    Dn dn = new Dn("uid=akarasulu,dc=example,dc=com");
    int nbLoops = 1000000;
    long t0 = System.currentTimeMillis();
    for (int i = 0; i < nbLoops; i++) {
        // Check the decoded BindRequest
        BindRequest bindRequest = new BindRequestImpl();
        bindRequest.setMessageId(1);
        bindRequest.setSimple(true);
        bindRequest.setDn(dn);
        bindRequest.setCredentials(Strings.getBytesUtf8("password"));
        Control control = new OpaqueControl("2.16.840.1.113730.3.4.2");
        bindRequest.addControl(control);
        // Check the encoding
        try {
            encoder.encodeMessage(bindRequest);
        } catch (EncoderException ee) {
            ee.printStackTrace();
            fail(ee.getMessage());
        }
    }
    long t1 = System.currentTimeMillis();
    System.out.println("BindRequest testEncodeBindRequestPerf, " + nbLoops + " loops, Delta = " + (t1 - t0));
}
Also used : 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) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) Dn(org.apache.directory.api.ldap.model.name.Dn) OpaqueControl(org.apache.directory.api.ldap.model.message.controls.OpaqueControl) BindRequestImpl(org.apache.directory.api.ldap.model.message.BindRequestImpl) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 37 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class BindRequestTest method testDecodeBindRequestEmptyMechanism.

/**
 * Test the decoding of a BindRequest with an empty mechanism
 */
@Test
public void testDecodeBindRequestEmptyMechanism() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x10);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x0E, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x09, 0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, 0x00, (byte) 0xA3, 0x02, 0x04, 0x00 });
    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());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(bindRequest);
        // Check the length
        assertEquals(0x10, 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) BindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 38 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class BindRequestTest method testDecodeBindRequestBadDN.

/**
 * Test the decoding of a BindRequest with Simple authentication and
 * controls
 */
@Test
public void testDecodeBindRequestBadDN() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x35);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x33, 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' });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<MessageDecorator<? extends Message>> container = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode the BindRequest PDU
    try {
        ldapDecoder.decode(stream, container);
        BindRequest bindRequest = ((BindRequestDecorator) container.getMessage());
        assertNull(bindRequest.getDn());
        assertEquals("uid:akarasulu,dc=example,dc=com", bindRequest.getName());
    } catch (DecoderException de) {
        assertTrue(de instanceof ResponseCarryingException);
        Message response = ((ResponseCarryingException) de).getResponse();
        assertTrue(response instanceof BindResponseImpl);
        assertEquals(ResultCodeEnum.INVALID_DN_SYNTAX, ((BindResponseImpl) response).getLdapResult().getResultCode());
        return;
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) Message(org.apache.directory.api.ldap.model.message.Message) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) BindResponseImpl(org.apache.directory.api.ldap.model.message.BindResponseImpl) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 39 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class BindRequestTest method testDecodeBindRequestSaslCredsNoControls.

/**
 * Test the decoding of a BindRequest with Sasl authentication, a
 * credentials and no controls
 */
@Test
public void testDecodeBindRequestSaslCredsNoControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x42);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x40, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x3B, // 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) 0xA3, // authentication AuthenticationChoice
    0x15, // ...
    0x04, 0x0B, 'K', 'E', 'R', 'B', 'E', 'R', 'O', 'S', '_', 'V', '4', (byte) 0x04, // SaslCredentials ::= SEQUENCE {
    0x06, // 
    'a', 'b', 'c', 'd', 'e', 'f' });
    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("uid=akarasulu,dc=example,dc=com", bindRequest.getName());
    assertFalse(bindRequest.isSimple());
    assertEquals("KERBEROS_V4", bindRequest.getSaslMechanism());
    assertEquals("abcdef", Strings.utf8ToString(bindRequest.getCredentials()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(bindRequest);
        // Check the length
        assertEquals(0x42, 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) BindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 40 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class BindRequestTest method testDecodeBindRequestEmptyCredentials.

/**
 * Test the decoding of a BindRequest with an bad mechanism
 */
/* This test is not valid. I don't know how to generate a UnsupportedEncodingException ...
    @Test
    public void testDecodeBindRequestBadMechanism()
    {
        Asn1Decoder ldapDecoder = new Asn1Decoder();

        ByteBuffer stream = ByteBuffer.allocate( 0x11 );
        stream.put( new byte[]
            { 
            0x30, 0x0F,                 // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,         // messageID MessageID
              0x60, 0x0A,               // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x01, 0x03,       // version INTEGER (1..127),
                0x04, 0x00, 
                ( byte ) 0xA3, 0x03, 
                  0x04, 0x01, (byte)0xFF
            } );

        stream.flip();

        // Allocate a LdapMessage Container
        Asn1Container container = new LdapMessageContainer();

        // Decode the BindRequest PDU
        try
        {
            ldapDecoder.decode( stream, container );
        }
        catch ( DecoderException de )
        {
            assertTrue( de instanceof ResponseCarryingException );
            Message response = ((ResponseCarryingException)de).getResponse();
            assertTrue( response instanceof BindResponseImpl );
            assertEquals( ResultCodeEnum.INAPPROPRIATEAUTHENTICATION, ((BindResponseImpl)response).getLdapResult().getResultCode() );
            return;
        }

        fail( "We should not reach this point" );
    }
    */
/**
 * Test the decoding of a BindRequest with an empty credentials
 */
@Test
public void testDecodeBindRequestEmptyCredentials() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x12);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x10, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x0B, 0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, 0x00, (byte) 0xA3, 0x04, 0x04, 0x00, 0x04, 0x00 });
    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 encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(bindRequest);
        // Check the length
        assertEquals(0x12, 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) BindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Aggregations

BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)45 Test (org.junit.Test)27 DecoderException (org.apache.directory.api.asn1.DecoderException)19 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)17 ByteBuffer (java.nio.ByteBuffer)16 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)16 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)15 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)15 EncoderException (org.apache.directory.api.asn1.EncoderException)12 Control (org.apache.directory.api.ldap.model.message.Control)11 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)9 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)9 BindRequestImpl (org.apache.directory.api.ldap.model.message.BindRequestImpl)9 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)7 BindResponse (org.apache.directory.api.ldap.model.message.BindResponse)7 DsmlControl (org.apache.directory.api.dsmlv2.DsmlControl)5 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)4 Dn (org.apache.directory.api.ldap.model.name.Dn)4 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)3 AbandonRequest (org.apache.directory.api.ldap.model.message.AbandonRequest)3