Search in sources :

Example 56 with EncoderException

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

the class SearchRequestMatchingRuleAssertionTest method testDecodeSearchRequestExtensibleMatch.

/**
 * Tests an search request decode with a simple equality match filter.
 */
@Test
public void testDecodeSearchRequestExtensibleMatch() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x63);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x61, 0x02, 0x01, // messageID       MessageID,
    0x01, 0x63, // protocolOp      CHOICE {
    0x5C, // SearchRequest ::= [APPLICATION 3] SEQUENCE {
    0x04, // baseObject      LDAPDN, (dc=example,dc=com)
    0x11, 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', // scope           ENUMERATED {
    0x0A, 0x01, // baseObject              (0), ...
    0x00, // derefAliases    ENUMERATED {
    0x0A, 0x01, // derefFindingBaseObj     (2),...
    0x02, 0x02, 0x01, // sizeLimit       INTEGER (0 .. maxInt), (2)
    0x02, 0x02, 0x01, // timeLimit       INTEGER (0 .. maxInt), (3)
    0x03, 0x01, 0x01, // typesOnly       BOOLEAN, (true)
    (byte) 0xFF, (byte) 0xA9, // filter          Filter,
    0x21, // MatchingRuleAssertion ::= SEQUENCE {
    (byte) 0x81, // matchingRule    [1] MatchingRuleId OPTIONAL,
    0x02, 'c', 'n', (byte) 0x82, // type            [2] AttributeDescription OPTIONAL,
    0x13, '1', '.', '2', '.', '8', '4', '0', '.', '4', '8', '0', '1', '8', '.', '1', '.', '2', '.', '2', (byte) 0x83, // matchValue      [3] AssertionValue,
    0x03, 'a', 'o', 'k', // dnAttributes    [4] BOOLEAN DEFAULT FALSE  }
    (byte) 0x84, 0x01, (byte) 0xFF, 0x30, // attributes      AttributeDescriptionList }
    0x15, 0x04, 0x05, 'a', 't', 't', 'r', '0', 0x04, 0x05, 'a', 't', 't', 'r', '1', 0x04, 0x05, 'a', 't', 't', 'r', '2' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a BindRequest Container
    LdapMessageContainer<SearchRequestDecorator> ldapMessageContainer = new LdapMessageContainer<SearchRequestDecorator>(codec);
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    SearchRequest searchRequest = ldapMessageContainer.getMessage();
    assertEquals(1, searchRequest.getMessageId());
    assertEquals("dc=example,dc=com", searchRequest.getBase().toString());
    assertEquals(SearchScope.OBJECT, searchRequest.getScope());
    assertEquals(AliasDerefMode.DEREF_FINDING_BASE_OBJ, searchRequest.getDerefAliases());
    assertEquals(2, searchRequest.getSizeLimit());
    assertEquals(3, searchRequest.getTimeLimit());
    assertEquals(true, searchRequest.getTypesOnly());
    // The attributes
    List<String> attributes = searchRequest.getAttributes();
    for (String attribute : attributes) {
        assertNotNull(attribute);
    }
    // attributes may have been reordered
    try {
        ByteBuffer bb = encoder.encodeMessage(searchRequest);
        // Check the length
        assertEquals(0x63, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu.substring(0, 0x56), decodedPdu.substring(0, 0x56));
    } 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) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) SearchRequestDecorator(org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 57 with EncoderException

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

the class DelRequestTest method testDecodeDelRequestSuccess.

/**
 * Test the decoding of a full DelRequest
 */
@Test
public void testDecodeDelRequestSuccess() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x27);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x25, 0x02, 0x01, // messageID MessageID
    0x01, // DelRequest ::= [APPLICATION 10] LDAPDN;
    0x4A, 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' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<DeleteRequestDecorator> container = new LdapMessageContainer<DeleteRequestDecorator>(codec);
    // Decode a DelRequest PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded DelRequest PDU
    DeleteRequest delRequest = container.getMessage();
    assertEquals(1, delRequest.getMessageId());
    assertEquals("cn=testModify,ou=users,ou=system", delRequest.getName().toString());
    // Check the length
    DeleteRequest internalDeleteRequest = new DeleteRequestImpl();
    internalDeleteRequest.setMessageId(delRequest.getMessageId());
    internalDeleteRequest.setName(delRequest.getName());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(internalDeleteRequest);
        // Check the length
        assertEquals(0x27, 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) DeleteRequestDecorator(org.apache.directory.api.ldap.codec.decorators.DeleteRequestDecorator) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) DeleteRequestImpl(org.apache.directory.api.ldap.model.message.DeleteRequestImpl) ByteBuffer(java.nio.ByteBuffer) DeleteRequest(org.apache.directory.api.ldap.model.message.DeleteRequest) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 58 with EncoderException

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

the class DelResponseTest method testDecodeDelResponseSuccess.

/**
 * Test the decoding of a DelResponse
 */
@Test
public void testDecodeDelResponseSuccess() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2D);
    stream.put(new byte[] { 0x30, // LDAPMessage ::=SEQUENCE {
    0x2B, 0x02, 0x01, // messageID MessageID
    0x01, 0x6B, // CHOICE { ..., delResponse DelResponse, ...
    0x26, // DelResponse ::= [APPLICATION 11] LDAPResult
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x21, // },
    0x04, // matchedDN 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', 0x04, // errorMessage
    0x00 // LDAPString,
    // referral [3] Referral OPTIONAL }
    // }
    });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<DeleteResponseDecorator> container = new LdapMessageContainer<DeleteResponseDecorator>(codec);
    // Decode the DelResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded DelResponse PDU
    DeleteResponse delResponse = container.getMessage();
    assertEquals(1, delResponse.getMessageId());
    assertEquals(ResultCodeEnum.ALIAS_PROBLEM, delResponse.getLdapResult().getResultCode());
    assertEquals("uid=akarasulu,dc=example,dc=com", delResponse.getLdapResult().getMatchedDn().getName());
    assertEquals("", delResponse.getLdapResult().getDiagnosticMessage());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(delResponse);
        // 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) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) DeleteResponse(org.apache.directory.api.ldap.model.message.DeleteResponse) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) DeleteResponseDecorator(org.apache.directory.api.ldap.codec.decorators.DeleteResponseDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 59 with EncoderException

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

the class DelResponseTest method testDecodeDelResponseSuccessWithControls.

/**
 * Test the decoding of a DelResponse with controls
 */
@Test
public void testDecodeDelResponseSuccessWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x4A);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x48, 0x02, 0x01, // messageID MessageID
    0x01, 0x6B, // CHOICE { ..., delResponse DelResponse, ...
    0x26, // DelResponse ::= [APPLICATION 11] LDAPResult
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x21, // },
    0x04, // matchedDN 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', 0x04, // errorMessage
    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<DeleteResponseDecorator> container = new LdapMessageContainer<DeleteResponseDecorator>(codec);
    // Decode the DelResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded DelResponse PDU
    DeleteResponse delResponse = container.getMessage();
    assertEquals(1, delResponse.getMessageId());
    assertEquals(ResultCodeEnum.ALIAS_PROBLEM, delResponse.getLdapResult().getResultCode());
    assertEquals("uid=akarasulu,dc=example,dc=com", delResponse.getLdapResult().getMatchedDn().getName());
    assertEquals("", delResponse.getLdapResult().getDiagnosticMessage());
    // Check the Control
    Map<String, Control> controls = delResponse.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(delResponse);
        // Check the length
        assertEquals(0x4A, 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) DeleteResponseDecorator(org.apache.directory.api.ldap.codec.decorators.DeleteResponseDecorator) 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) DeleteResponse(org.apache.directory.api.ldap.model.message.DeleteResponse) 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 60 with EncoderException

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

the class ExtendedRequestTest method testDecodeExtendedRequestNoValueWithControls.

/**
 * Test the decoding of a full ExtendedRequest with no value and with
 * controls
 */
@Test
public void testDecodeExtendedRequestNoValueWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x33);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x31, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., extendedReq ExtendedRequest, ...
    0x77, // ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
    0x0F, // requestName [0] LDAPOID,
    (byte) 0x80, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // requestValue [1] OCTET STRING OPTIONAL }
    (byte) 0xA0, // A control
    0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a LdapMessage Container
    LdapMessageContainer<ExtendedRequestDecorator<?>> container = new LdapMessageContainer<ExtendedRequestDecorator<?>>(codec);
    // Decode the ExtendedRequest PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded ExtendedRequest PDU
    ExtendedRequestDecorator<?> extendedRequest = container.getMessage();
    assertEquals(1, extendedRequest.getMessageId());
    assertEquals("1.3.6.1.5.5.2", extendedRequest.getRequestName());
    assertEquals("", Strings.utf8ToString(extendedRequest.getRequestValue()));
    // Check the Control
    Map<String, Control> controls = extendedRequest.getControls();
    assertEquals(1, controls.size());
    assertTrue(extendedRequest.hasControl("2.16.840.1.113730.3.4.2"));
    @SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) extendedRequest.getControl("2.16.840.1.113730.3.4.2");
    assertEquals("", Strings.dumpBytes(control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(extendedRequest);
        // Check the length
        assertEquals(0x33, 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) ExtendedRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator) 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)

Aggregations

EncoderException (org.apache.directory.api.asn1.EncoderException)226 ByteBuffer (java.nio.ByteBuffer)191 Test (org.junit.Test)189 DecoderException (org.apache.directory.api.asn1.DecoderException)151 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)150 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)127 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)124 BufferOverflowException (java.nio.BufferOverflowException)40 Control (org.apache.directory.api.ldap.model.message.Control)38 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)35 AbstractCodecServiceTest (org.apache.directory.api.ldap.extras.AbstractCodecServiceTest)35 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)33 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)33 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)28 SyncInfoValue (org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValue)20 SyncInfoValueDecorator (org.apache.directory.api.ldap.extras.intermediate.syncrepl.SyncInfoValueDecorator)20 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)20 AndNode (org.apache.directory.api.ldap.model.filter.AndNode)15 Entry (org.apache.directory.api.ldap.model.entry.Entry)14 EqualityNode (org.apache.directory.api.ldap.model.filter.EqualityNode)14