Search in sources :

Example 86 with EncoderException

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

the class EndTransactionRequestTest method testEndTransactionRequestNoCommitIdentifier.

/**
 * Test the decoding of a EndTransactionRequest with an identifier but no commit
 * @throws EncoderException
 */
@Test
public void testEndTransactionRequestNoCommitIdentifier() throws EncoderException {
    Asn1Decoder decoder = new Asn1Decoder();
    ByteBuffer bb = ByteBuffer.allocate(0x08);
    bb.put(new byte[] { // EndTransactionRequest ::= SEQUENCE {
    0x30, // EndTransactionRequest ::= SEQUENCE {
    0x06, // identifier (test)
    0x04, // identifier (test)
    0x04, // identifier (test)
    't', // identifier (test)
    'e', // identifier (test)
    's', // identifier (test)
    't' });
    String decodedPdu = Strings.dumpBytes(bb.array());
    bb.flip();
    EndTransactionRequestContainer container = new EndTransactionRequestContainer();
    try {
        decoder.decode(bb, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    EndTransactionRequest endTransactionRequest = container.getEndTransactionRequest();
    assertTrue(endTransactionRequest.getCommit());
    assertEquals("test", Strings.utf8ToString(endTransactionRequest.getTransactionId()));
    // Check the length
    assertEquals(0x08, ((EndTransactionRequestDecorator) endTransactionRequest).computeLengthInternal());
    // Check the encoding
    ByteBuffer bb1 = ((EndTransactionRequestDecorator) endTransactionRequest).encodeInternal();
    String encodedPdu = Strings.dumpBytes(bb1.array());
    assertEquals(encodedPdu, decodedPdu);
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) EndTransactionRequest(org.apache.directory.api.ldap.extras.extended.endTransaction.EndTransactionRequest) Test(org.junit.Test)

Example 87 with EncoderException

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

the class EndTransactionResponseTest method testEndTransactionResponseMessageId.

/**
 * Test the decoding of a EndTransactionResponse with a messageId and no updateControls
 */
@Test
public void testEndTransactionResponseMessageId() throws DecoderException, EncoderException {
    Asn1Decoder decoder = new Asn1Decoder();
    ByteBuffer bb = ByteBuffer.allocate(0x05);
    bb.put(new byte[] { // EndTransactionResponse ::= SEQUENCE {
    0x30, // EndTransactionResponse ::= SEQUENCE {
    0x03, // MessageId
    0x02, // MessageId
    0x01, // MessageId
    0x04 });
    String decodedPdu = Strings.dumpBytes(bb.array());
    bb.flip();
    EndTransactionResponseContainer container = new EndTransactionResponseContainer();
    try {
        decoder.decode(bb, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    EndTransactionResponse endTransactionResponse = container.getEndTransactionResponse();
    assertEquals(4, endTransactionResponse.getFailedMessageId());
    assertEquals(0, endTransactionResponse.getUpdateControls().size());
    // Check the length
    assertEquals(0x05, ((EndTransactionResponseDecorator) endTransactionResponse).computeLengthInternal());
    // Check the encoding
    ByteBuffer bb1 = ((EndTransactionResponseDecorator) endTransactionResponse).encodeInternal();
    String encodedPdu = Strings.dumpBytes(bb1.array());
    assertEquals(encodedPdu, decodedPdu);
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) EndTransactionResponse(org.apache.directory.api.ldap.extras.extended.endTransaction.EndTransactionResponse) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 88 with EncoderException

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

the class EndTransactionResponseTest method testEndTransactionResponseUpdateControls.

/**
 * Test the decoding of a EndTransactionResponse with updateControls
 */
@Test
public void testEndTransactionResponseUpdateControls() throws DecoderException, EncoderException {
    Asn1Decoder decoder = new Asn1Decoder();
    ByteBuffer bb = ByteBuffer.allocate(0xAC);
    bb.put(new byte[] { // EndTransactionResponse ::= SEQUENCE {
    0x30, // EndTransactionResponse ::= SEQUENCE {
    (byte) 0x81, // EndTransactionResponse ::= SEQUENCE {
    (byte) 0xA9, // UpdateControls
    0x30, // UpdateControls
    (byte) 0x81, // UpdateControls
    (byte) 0xA6, // updateControl
    0x30, // updateControl
    0x5F, // messageID
    0x02, // messageID
    0x01, // messageID
    0x01, // controls
    0x30, // controls
    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', // updateControl
    0x30, // updateControl
    0x43, // messageID
    0x02, // messageID
    0x01, // messageID
    0x02, // controls
    0x30, // controls
    0x3E, // 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' });
    String decodedPdu = Strings.dumpBytes(bb.array());
    bb.flip();
    EndTransactionResponseContainer container = new EndTransactionResponseContainer();
    try {
        decoder.decode(bb, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    EndTransactionResponse endTransactionResponse = container.getEndTransactionResponse();
    assertEquals(-1, endTransactionResponse.getFailedMessageId());
    assertEquals(2, endTransactionResponse.getUpdateControls().size());
    UpdateControls updateControls1 = endTransactionResponse.getUpdateControls().get(0);
    assertEquals(1, updateControls1.getMessageId());
    assertNotNull(updateControls1.getControls());
    assertEquals(4, updateControls1.getControls().size());
    for (Control control : updateControls1.getControls()) {
        switch(control.getOid()) {
            case "1.3.6.1.5.5.1":
                assertTrue(control.isCritical());
                assertEquals("abcdef", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
                break;
            case "1.3.6.1.5.5.2":
                assertFalse(control.isCritical());
                assertEquals("ghijkl", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
                break;
            case "1.3.6.1.5.5.3":
                assertTrue(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            case "1.3.6.1.5.5.4":
                assertFalse(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            default:
                fail();
                break;
        }
    }
    UpdateControls updateControls2 = endTransactionResponse.getUpdateControls().get(1);
    assertEquals(2, updateControls2.getMessageId());
    assertNotNull(updateControls2.getControls());
    assertEquals(3, updateControls2.getControls().size());
    for (Control control : updateControls2.getControls()) {
        switch(control.getOid()) {
            case "1.3.6.1.5.5.2":
                assertFalse(control.isCritical());
                assertEquals("ghijkl", Strings.utf8ToString(((CodecControl<?>) control).getValue()));
                break;
            case "1.3.6.1.5.5.3":
                assertTrue(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            case "1.3.6.1.5.5.4":
                assertFalse(control.isCritical());
                assertNull(((CodecControl<?>) control).getValue());
                break;
            default:
                fail();
                break;
        }
    }
    // Check the length
    assertEquals(0xAC, ((EndTransactionResponseDecorator) endTransactionResponse).computeLengthInternal());
    // Check the encoding
    ByteBuffer bb1 = ((EndTransactionResponseDecorator) endTransactionResponse).encodeInternal();
    String encodedPdu = Strings.dumpBytes(bb1.array());
    assertEquals(encodedPdu, decodedPdu);
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) Control(org.apache.directory.api.ldap.model.message.Control) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) UpdateControls(org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CodecControl(org.apache.directory.api.ldap.codec.api.CodecControl) EndTransactionResponse(org.apache.directory.api.ldap.extras.extended.endTransaction.EndTransactionResponse) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 89 with EncoderException

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

the class GracefulDisconnectResponseTest method testDecodeGracefulDisconnectReplicatedContextsOnly.

/**
 * Test the decoding of a GracefulDisconnect with replicatedContexts only
 */
@Test
public void testDecodeGracefulDisconnectReplicatedContextsOnly() {
    Asn1Decoder decoder = new GracefulDisconnectDecoder();
    ByteBuffer stream = ByteBuffer.allocate(0x6A);
    stream.put(new byte[] { // GracefulDisconnec ::= SEQUENCE {
    0x30, // GracefulDisconnec ::= SEQUENCE {
    0x68, 0x30, // replicatedContexts Referral OPTIONAL
    0x66, 0x04, 0x1F, 'l', 'd', 'a', 'p', ':', '/', '/', 'd', 'i', 'r', 'e', 'c', 't', 'o', 'r', 'y', '.', 'a', 'p', 'a', 'c', 'h', 'e', '.', 'o', 'r', 'g', ':', '8', '0', '/', 0x04, 0x43, 'l', 'd', 'a', 'p', ':', '/', '/', 'l', 'd', 'a', 'p', '.', 'n', 'e', 't', 's', 'c', 'a', 'p', 'e', '.', 'c', 'o', 'm', '/', 'o', '=', 'B', 'a', 'b', 's', 'c', 'o', ',', 'c', '=', 'U', 'S', '?', '?', '?', '(', 'i', 'n', 't', '=', '%', '5', 'c', '0', '0', '%', '5', 'c', '0', '0', '%', '5', 'c', '0', '0', '%', '5', 'c', '0', '4', ')' });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    GracefulDisconnectContainer container = new GracefulDisconnectContainer();
    try {
        decoder.decode(stream, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    GracefulDisconnectResponseDecorator gracefulDisconnect = container.getGracefulDisconnectResponse();
    assertEquals(0, gracefulDisconnect.getTimeOffline());
    assertEquals(0, gracefulDisconnect.getDelay());
    assertEquals(2, gracefulDisconnect.getReplicatedContexts().getLdapUrls().size());
    Iterator<String> ldapUrls = gracefulDisconnect.getReplicatedContexts().getLdapUrls().iterator();
    assertEquals("ldap://directory.apache.org:80/", ldapUrls.next());
    assertEquals("ldap://ldap.netscape.com/o=Babsco,c=US???(int=%5c00%5c00%5c00%5c04)", ldapUrls.next());
    // Check the length
    assertEquals(0x6A, gracefulDisconnect.computeLengthInternal());
    // Check the encoding
    try {
        ByteBuffer bb = gracefulDisconnect.encodeInternal();
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : GracefulDisconnectContainer(org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulDisconnectContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) GracefulDisconnectDecoder(org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulDisconnectDecoder) GracefulDisconnectResponseDecorator(org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulDisconnect.GracefulDisconnectResponseDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 90 with EncoderException

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

the class GracefulShutdownTest method testDecodeGracefulShutdownSuccess.

/**
 * Test the decoding of a GracefulShutdown
 */
@Test
public void testDecodeGracefulShutdownSuccess() {
    Asn1Decoder decoder = new Asn1Decoder();
    ByteBuffer bb = ByteBuffer.allocate(0x08);
    bb.put(new byte[] { // GracefulShutdown ::= SEQUENCE {
    0x30, // GracefulShutdown ::= SEQUENCE {
    0x06, 0x02, 0x01, // timeOffline INTEGER (0..720) DEFAULT 0,
    0x01, (byte) 0x80, 0x01, // delay INTEGER (0..86400) DEFAULT
    0x01 // 0
    // }
    });
    String decodedPdu = Strings.dumpBytes(bb.array());
    bb.flip();
    GracefulShutdownContainer container = new GracefulShutdownContainer();
    try {
        decoder.decode(bb, container);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    GracefulShutdownRequestDecorator gracefulShutdownRequest = container.getGracefulShutdownRequest();
    assertEquals(1, gracefulShutdownRequest.getTimeOffline());
    assertEquals(1, gracefulShutdownRequest.getDelay());
    // Check the length
    assertEquals(0x08, gracefulShutdownRequest.computeLengthInternal());
    // Check the encoding
    try {
        ByteBuffer bb1 = gracefulShutdownRequest.encodeInternal();
        String encodedPdu = Strings.dumpBytes(bb1.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : GracefulShutdownRequestDecorator(org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulShutdown.GracefulShutdownRequestDecorator) 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) GracefulShutdownContainer(org.apache.directory.api.ldap.extras.extended.ads_impl.gracefulShutdown.GracefulShutdownContainer) Test(org.junit.Test)

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