Search in sources :

Example 61 with EncoderException

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

the class ExtendedRequestTest method testDecodeExtendedRequestName.

/**
 * Test the decoding of a name only ExtendedRequest
 */
@Test
public void testDecodeExtendedRequestName() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x16);
    stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
    0x30, // LDAPMessage ::= SEQUENCE {
    0x14, 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' });
    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());
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(extendedRequest);
        // Check the length
        assertEquals(0x16, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ExtendedRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator) 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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 62 with EncoderException

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

the class ExtendedRequestTest method testDecodeExtendedRequestSuccess.

/**
 * Test the decoding of a full ExtendedRequest
 */
@Test
public void testDecodeExtendedRequestSuccess() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x1D);
    stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
    0x30, // LDAPMessage ::= SEQUENCE {
    0x1B, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., extendedReq ExtendedRequest, ...
    0x77, // ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
    0x16, // requestName [0] LDAPOID,
    (byte) 0x80, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // requestValue [1] OCTET STRING OPTIONAL }
    (byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e' });
    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("value", Strings.utf8ToString(extendedRequest.getRequestValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(extendedRequest);
        // Check the length
        assertEquals(0x1D, bb.limit());
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : ExtendedRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator) 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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 63 with EncoderException

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

the class ExtendedResponseTest method testDecodeExtendedRequestNoNameWithControls.

/**
 * Test the decoding of a ExtendedRequest with no name and a control
 */
@Test
public void testDecodeExtendedRequestNoNameWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x2B);
    stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
    0x29, 0x02, 0x01, // messageID MessageID
    0x01, // CHOICE { ..., extendedResp Response, ...
    0x78, // ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
    0x07, // COMPONENTS OF LDAPResult,
    0x0A, 0x01, // LDAPResult ::= SEQUENCE {
    0x00, // },
    0x04, // matchedDN LDAPDN,
    0x00, 0x04, // errorMessage LDAPString,
    0x00, // responseName [0] LDAPOID,
    (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<ExtendedResponseDecorator<?>> container = new LdapMessageContainer<ExtendedResponseDecorator<?>>(codec);
    // Decode the ExtendedResponse PDU
    try {
        ldapDecoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded ExtendedResponse PDU
    ExtendedResponse extendedResponse = container.getMessage();
    assertEquals(1, extendedResponse.getMessageId());
    assertEquals(ResultCodeEnum.SUCCESS, extendedResponse.getLdapResult().getResultCode());
    assertEquals("", extendedResponse.getLdapResult().getMatchedDn().getName());
    assertEquals("", extendedResponse.getLdapResult().getDiagnosticMessage());
    // Check the Control
    Map<String, Control> controls = extendedResponse.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(control.getValue()));
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(extendedResponse);
        // Check the length
        assertEquals(0x2B, 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) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 64 with EncoderException

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

the class ExtensibleMatchFilter method encode.

/**
 * Encode the ExtensibleMatch Filters to a PDU.
 * <br>
 * ExtensibleMatch filter :
 * <pre>
 * 0xA9 LL
 *  |     0x81 LL matchingRule
 *  |    / |   0x82 LL Type
 *  |   /  |  /0x83 LL matchValue
 *  +--+   +-+
 *  |   \     \
 *  |    \     0x83 LL MatchValue
 *  |     0x82 LL type
 *  |     0x83 LL matchValue
 *  +--[0x84 0x01 dnAttributes]
 * </pre>
 *
 * @param buffer The buffer where to put the PDU
 * @return The PDU.
 */
@Override
public ByteBuffer encode(ByteBuffer buffer) throws EncoderException {
    if (buffer == null) {
        throw new EncoderException(I18n.err(I18n.ERR_04023));
    }
    try {
        // The ExtensibleMatch Tag
        buffer.put((byte) LdapCodecConstants.EXTENSIBLE_MATCH_FILTER_TAG);
        buffer.put(TLV.getBytes(extensibleMatchLength));
        if ((matchingRule == null) && (type == null)) {
            throw new EncoderException(I18n.err(I18n.ERR_04056));
        }
        // The matching rule
        if (matchingRule != null) {
            buffer.put((byte) LdapCodecConstants.MATCHING_RULE_ID_TAG);
            buffer.put(TLV.getBytes(matchingRuleBytes.length));
            buffer.put(matchingRuleBytes);
        }
        // The type
        if (type != null) {
            buffer.put((byte) LdapCodecConstants.MATCHING_RULE_TYPE_TAG);
            buffer.put(TLV.getBytes(typeBytes.length));
            buffer.put(typeBytes);
        }
        // The match value
        if (matchValue != null) {
            buffer.put((byte) LdapCodecConstants.MATCH_VALUE_TAG);
            byte[] bytes = matchValue.getBytes();
            int bytesLength = bytes.length;
            buffer.put(TLV.getBytes(bytesLength));
            if (bytesLength != 0) {
                buffer.put(bytes);
            }
        }
        // The dnAttributes flag, if true only
        if (dnAttributes) {
            buffer.put((byte) LdapCodecConstants.DN_ATTRIBUTES_FILTER_TAG);
            buffer.put((byte) 1);
            buffer.put(BerValue.TRUE_VALUE);
        }
    } catch (BufferOverflowException boe) {
        throw new EncoderException(I18n.err(I18n.ERR_04005), boe);
    }
    return buffer;
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) BufferOverflowException(java.nio.BufferOverflowException)

Example 65 with EncoderException

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

the class LdapControlTest method testDecodeRequestWithControls.

/**
 * Test the decoding of a Request with controls
 */
@SuppressWarnings("unchecked")
@Test
public void testDecodeRequestWithControls() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x64);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x62, // messageID MessageID
    0x02, // messageID MessageID
    0x01, // messageID MessageID
    0x03, // CHOICE { ..., abandonRequest
    0x50, // CHOICE { ..., abandonRequest
    0x01, // CHOICE { ..., abandonRequest
    0x02, // controls [0] Controls OPTIONAL }
    (byte) 0xA0, // controls [0] Controls OPTIONAL }
    0x5A, // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x1A, // controlType LDAPOID,
    0x04, // controlType LDAPOID,
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1', // criticality BOOLEAN DEFAULT FALSE,
    0x01, // criticality BOOLEAN DEFAULT FALSE,
    0x01, // criticality BOOLEAN DEFAULT FALSE,
    (byte) 0xFF, // controlValue OCTET STRING OPTIONAL }
    0x04, // controlValue OCTET STRING OPTIONAL }
    0x06, 'a', 'b', 'c', 'd', 'e', 'f', // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x17, // controlType LDAPOID,
    0x04, // controlType LDAPOID,
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // controlValue OCTET STRING OPTIONAL }
    0x04, // controlValue OCTET STRING OPTIONAL }
    0x06, 'g', 'h', 'i', 'j', 'k', 'l', // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x12, // controlType LDAPOID,
    0x04, // controlType LDAPOID,
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3', // criticality BOOLEAN DEFAULT FALSE}
    0x01, // criticality BOOLEAN DEFAULT FALSE}
    0x01, // criticality BOOLEAN DEFAULT FALSE}
    (byte) 0xFF, // Control ::= SEQUENCE {
    0x30, // Control ::= SEQUENCE {
    0x0F, // controlType LDAPOID}
    0x04, // controlType LDAPOID}
    0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '4' });
    stream.flip();
    // Allocate a LdapMessageContainer Container
    LdapMessageContainer<AbandonRequestDecorator> ldapMessageContainer = new LdapMessageContainer<AbandonRequestDecorator>(codec);
    // Decode the PDU
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check that everything is OK
    AbandonRequestDecorator abandonRequest = ldapMessageContainer.getMessage();
    // Copy the message
    AbandonRequest internalAbandonRequest = new AbandonRequestImpl(abandonRequest.getAbandoned());
    internalAbandonRequest.setMessageId(abandonRequest.getMessageId());
    assertEquals(3, abandonRequest.getMessageId());
    assertEquals(2, abandonRequest.getAbandoned());
    // Check the Controls
    Map<String, Control> controls = abandonRequest.getControls();
    assertEquals(4, controls.size());
    CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.1");
    assertEquals("1.3.6.1.5.5.1", control.getOid());
    assertEquals("0x61 0x62 0x63 0x64 0x65 0x66 ", Strings.dumpBytes((byte[]) control.getValue()));
    assertTrue(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.2");
    assertEquals("1.3.6.1.5.5.2", control.getOid());
    assertEquals("0x67 0x68 0x69 0x6A 0x6B 0x6C ", Strings.dumpBytes((byte[]) control.getValue()));
    assertFalse(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.3");
    assertEquals("1.3.6.1.5.5.3", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    assertTrue(control.isCritical());
    internalAbandonRequest.addControl(control);
    control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("1.3.6.1.5.5.4");
    assertEquals("1.3.6.1.5.5.4", control.getOid());
    assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
    assertFalse(control.isCritical());
    internalAbandonRequest.addControl(control);
    // Check the encoding
    try {
        ByteBuffer bb = encoder.encodeMessage(internalAbandonRequest);
        // Check the length
        assertEquals(0x64, bb.limit());
        // So we decode the generated PDU, and we compare it with the initial message
        try {
            ldapDecoder.decode(bb, ldapMessageContainer);
        } catch (DecoderException de) {
            de.printStackTrace();
            fail(de.getMessage());
        }
        AbandonRequest abandonRequest2 = ldapMessageContainer.getMessage();
        assertEquals(abandonRequest, abandonRequest2);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) AbandonRequest(org.apache.directory.api.ldap.model.message.AbandonRequest) ByteBuffer(java.nio.ByteBuffer) AbandonRequestDecorator(org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator) AbandonRequestImpl(org.apache.directory.api.ldap.model.message.AbandonRequestImpl) 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