Search in sources :

Example 31 with Asn1Container

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

the class WhoAmIResponseTest method testDecodeWhoAmINull.

/**
 * Test the normal WhoAmI response message
 */
@Test
public void testDecodeWhoAmINull() {
    Asn1Decoder whoAmIResponseDecoder = new WhoAmIResponseDecoder();
    ByteBuffer stream = ByteBuffer.allocate(0x00);
    stream.put(new byte[] {}).flip();
    Strings.dumpBytes(stream.array());
    // Allocate a WhoAmI Container
    Asn1Container whoAmIResponseContainer = new WhoAmIResponseContainer();
    // Decode a WhoAmI message
    try {
        whoAmIResponseDecoder.decode(stream, whoAmIResponseContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    WhoAmIResponseDecorator whoAmIResponse = ((WhoAmIResponseContainer) whoAmIResponseContainer).getWhoAmIResponse();
    assertNull(whoAmIResponse);
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 32 with Asn1Container

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

the class ApiAsn1BerOsgiTest method useBundleClasses.

@Override
protected void useBundleClasses() throws Exception {
    new CheckNotNullLength<Asn1Container>();
    new Asn1Decoder().getMaxLengthLength();
    new BerValue().init(5);
    new TLV(1).getValue();
}
Also used : CheckNotNullLength(org.apache.directory.api.asn1.actions.CheckNotNullLength) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 33 with Asn1Container

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

the class Asn1Decoder method treatTagStartState.

/**
 * Treat the start of a TLV. It reads the tag and get its value.
 *
 * @param stream The ByteBuffer containing the PDU to decode
 * @param container The container that stores the current state,
 * the result and other informations.
 * @return <code>true</code> if there are more bytes to read, <code>false
 * </code> otherwise
 */
private boolean treatTagStartState(ByteBuffer stream, Asn1Container container) {
    if (stream.hasRemaining()) {
        byte octet = stream.get();
        TLV tlv = new TLV(container.getNewTlvId());
        tlv.setTag(octet);
        // Store the current TLV in the container.
        container.setCurrentTLV(tlv);
        // Create a link between the current TLV with its parent
        tlv.setParent(container.getParentTLV());
        // Switch to the next state, which is the Length decoding
        container.setState(TLVStateEnum.LENGTH_STATE_START);
        if (IS_DEBUG) {
            byte tag = container.getCurrentTLV().getTag();
            LOG.debug(I18n.msg(I18n.MSG_01000_TAG_DECODED, Asn1StringUtils.dumpByte(tag)));
        }
        return MORE;
    } else {
        // The stream has been exhausted
        return END;
    }
}
Also used : TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 34 with Asn1Container

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

the class Asn1Decoder method treatLengthStartState.

/**
 * Treat the Length start. The tag has been decoded, so we have to deal with
 * the LENGTH, which can be multi-bytes.
 *
 * @param stream  The ByteBuffer containing the PDU to decode
 * @param container The container that stores the current state,
 * the result and other informations.
 * @return <code>true</code> if there are more bytes to read, <code>false
 * </code> otherwise
 * @throws DecoderException Thrown if anything went wrong
 */
private boolean treatLengthStartState(ByteBuffer stream, Asn1Container container) throws DecoderException {
    if (stream.hasRemaining()) {
        byte octet = stream.get();
        TLV tlv = container.getCurrentTLV();
        if ((octet & TLV.LENGTH_LONG_FORM) == 0) {
            // We don't have a long form. The Length of the Value part is
            // given by this byte.
            tlv.setLength(octet);
            tlv.setLengthNbBytes(1);
            container.setState(TLVStateEnum.LENGTH_STATE_END);
        } else if ((octet & TLV.LENGTH_EXTENSION_RESERVED) != TLV.LENGTH_EXTENSION_RESERVED) {
            int expectedLength = octet & TLV.LENGTH_SHORT_MASK;
            if (expectedLength > 4) {
                String msg = I18n.err(I18n.ERR_01000_LENGTH_OVERFLOW);
                LOG.error(msg);
                throw new DecoderException(msg);
            }
            tlv.setLength(0);
            tlv.setLengthNbBytes(1 + expectedLength);
            tlv.setLengthBytesRead(1);
            container.setState(TLVStateEnum.LENGTH_STATE_PENDING);
        } else {
            String msg = I18n.err(I18n.ERR_01001_LENGTH_EXTENSION_RESERVED);
            LOG.error(msg);
            throw new DecoderException(msg);
        }
        return MORE;
    } else {
        return END;
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 35 with Asn1Container

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

the class LdapResultTest method testDecodeAddResponseEmptyResultCodeNoMatchedDN.

/**
 * Test the decoding of a AddResponse with no matched Dn
 */
@Test
public void testDecodeAddResponseEmptyResultCodeNoMatchedDN() {
    Asn1Decoder ldapDecoder = new Asn1Decoder();
    ByteBuffer stream = ByteBuffer.allocate(0x0A);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x08, 0x02, 0x01, // messageID MessageID
    0x01, 0x69, // CHOICE { ..., addResponse AddResponse, ...
    0x03, 0x0A, 0x01, // resultCode success
    0x00 });
    stream.flip();
    // Allocate a LdapMessage Container
    Asn1Container ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    // Decode a AddResponse message
    try {
        ldapDecoder.decode(stream, ldapMessageContainer);
    } 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) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) 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)

Aggregations

DecoderException (org.apache.directory.api.asn1.DecoderException)40 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)39 ByteBuffer (java.nio.ByteBuffer)38 Test (org.junit.Test)38 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)36 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)30 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)29 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)10 EncoderException (org.apache.directory.api.asn1.EncoderException)6 CancelContainer (org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelContainer)5 CancelDecoder (org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelDecoder)5 Message (org.apache.directory.api.ldap.model.message.Message)4 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)3 AddResponseImpl (org.apache.directory.api.ldap.model.message.AddResponseImpl)3 BerValue (org.apache.directory.api.asn1.ber.tlv.BerValue)2 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)2 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)2 CheckNotNullLength (org.apache.directory.api.asn1.actions.CheckNotNullLength)1 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)1 AndFilter (org.apache.directory.api.ldap.codec.search.AndFilter)1