Search in sources :

Example 76 with Asn1Decoder

use of org.apache.directory.api.asn1.ber.Asn1Decoder in project directory-ldap-api by apache.

the class BindRequestTest method testDecodeBindRequestBadVersion128.

/**
 * Test the decoding of a BindRequest with a bad version (128)
 */
@Test
public void testDecodeBindRequestBadVersion128() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0C);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x0A, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x04, 0x02, 0x02, 0x00, // version INTEGER (1..127),
    (byte) 0x80 });
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequestDecorator> container = new LdapMessageContainer<BindRequestDecorator>(codec);
    // Decode a BindRequest message
    try {
        ldapDecoder.decode(stream, container);
    } 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) BindRequestDecorator(org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 77 with Asn1Decoder

use of org.apache.directory.api.asn1.ber.Asn1Decoder 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 78 with Asn1Decoder

use of org.apache.directory.api.asn1.ber.Asn1Decoder 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 79 with Asn1Decoder

use of org.apache.directory.api.asn1.ber.Asn1Decoder in project directory-ldap-api by apache.

the class BindResponseTest method testDecodeBindResponseServerSASL.

/**
 * Test the decoding of a BindResponse with a credentials
 */
@Test
public void testDecodeBindResponseServerSASL() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x12);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x10, 0x02, 0x01, // messageID MessageID
    0x01, 0x61, // CHOICE { ..., bindResponse BindResponse, ...
    0x0B, // COMPONENTS OF LDAPResult,
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00, // referral [3] Referral OPTIONAL }
    (byte) 0x87, 0x02, 'A', // serverSaslCreds [7] OCTET
    'B' // STRING OPTIONAL }
    });
    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("AB", Strings.utf8ToString(bindResponse.getServerSaslCreds()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(bindResponse);
        // 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) 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 80 with Asn1Decoder

use of org.apache.directory.api.asn1.ber.Asn1Decoder in project directory-ldap-api by apache.

the class BindResponseTest method testDecodeBindResponseSuccess.

/**
 * Test the decoding of a BindResponse
 */
@Test
public void testDecodeBindResponseSuccess() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0E);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x0C, 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 // referral [3] Referral OPTIONAL }
    // serverSaslCreds [7] OCTET STRING OPTIONAL }
    });
    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 encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(bindResponse);
        // Check the length
        assertEquals(0x0E, 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) 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)

Aggregations

Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)335 ByteBuffer (java.nio.ByteBuffer)334 Test (org.junit.Test)332 DecoderException (org.apache.directory.api.asn1.DecoderException)331 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)268 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)268 EncoderException (org.apache.directory.api.asn1.EncoderException)145 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)85 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)43 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)37 Control (org.apache.directory.api.ldap.model.message.Control)37 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)36 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)34 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)21 Message (org.apache.directory.api.ldap.model.message.Message)20 ModifyRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ModifyRequestDecorator)19 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)19 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)18 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)18 SearchResultEntryDecorator (org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator)16