Search in sources :

Example 1 with DummySession

use of org.apache.mina.core.session.DummySession in project directory-ldap-api by apache.

the class LdapDecoderTest method testDecode2Messages.

/**
 * Test the decoding of two messages in a PDU
 */
@Test
public void testDecode2Messages() throws Exception {
    LdapMessageContainer<MessageDecorator<? extends Message>> container = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
    IoSession dummySession = new DummySession();
    dummySession.setAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR, container);
    ByteBuffer stream = ByteBuffer.allocate(0x6A);
    stream.put(new byte[] { // LDAPMessage ::=SEQUENCE {
    0x30, // LDAPMessage ::=SEQUENCE {
    0x33, 0x02, 0x01, // messageID MessageID
    0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name 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', (byte) 0x80, // authentication
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd', 0x30, // LDAPMessage ::=SEQUENCE {
    0x33, 0x02, 0x01, // messageID MessageID
    0x02, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
    0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
    0x02, 0x01, // version INTEGER (1..127),
    0x03, 0x04, // name 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', (byte) 0x80, // authentication
    0x08, // ...
    'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
    stream.flip();
    List<Message> result = new ArrayList<Message>();
    // Decode a BindRequest PDU
    try {
        decode(stream, container, result);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    // Check the decoded PDU
    BindRequest bindRequest = (BindRequest) (result.get(0));
    assertEquals(1, bindRequest.getMessageId());
    assertTrue(bindRequest.isVersion3());
    assertEquals("uid=akarasulu,dc=example,dc=com", bindRequest.getName().toString());
    assertTrue(bindRequest.isSimple());
    assertEquals("password", Strings.utf8ToString(bindRequest.getCredentials()));
    // The second message
    bindRequest = (BindRequest) (result.get(1));
    assertEquals(2, bindRequest.getMessageId());
    assertTrue(bindRequest.isVersion3());
    assertEquals("uid=akarasulu,dc=example,dc=com", bindRequest.getName().toString());
    assertTrue(bindRequest.isSimple());
    assertEquals("password", Strings.utf8ToString(bindRequest.getCredentials()));
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) ArrayList(java.util.ArrayList) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) DummySession(org.apache.mina.core.session.DummySession) ByteBuffer(java.nio.ByteBuffer) IoSession(org.apache.mina.core.session.IoSession) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 2 with DummySession

use of org.apache.mina.core.session.DummySession in project zm-mailbox by Zimbra.

the class NioOutputStreamTest method writeString.

@Test
public void writeString() throws Exception {
    DummySession session = new DummySession();
    TestIoHandler handler = new TestIoHandler();
    session.setHandler(handler);
    NioOutputStream out = new NioOutputStream(session, 10, Integer.MAX_VALUE, Integer.MAX_VALUE);
    out.write("1234567890");
    out.write(" ");
    out.write("12345678901");
    Assert.assertEquals(3, handler.getWriteCount());
    Assert.assertEquals("1234567890 12345678901", handler.toString());
    out.close();
}
Also used : DummySession(org.apache.mina.core.session.DummySession) Test(org.junit.Test)

Example 3 with DummySession

use of org.apache.mina.core.session.DummySession in project zm-mailbox by Zimbra.

the class NioOutputStreamTest method writeByteArray.

@Test
public void writeByteArray() throws Exception {
    DummySession session = new DummySession();
    TestIoHandler handler = new TestIoHandler();
    session.setHandler(handler);
    NioOutputStream out = new NioOutputStream(session, 10, Integer.MAX_VALUE, Integer.MAX_VALUE);
    byte[] b = new byte[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1' };
    out.write(b, 0, 10);
    out.write(new byte[] { ' ' }, 0, 1);
    out.write(b, 0, 11);
    out.close();
    Assert.assertEquals(3, handler.getWriteCount());
    Assert.assertEquals("1234567890 12345678901", handler.toString());
}
Also used : DummySession(org.apache.mina.core.session.DummySession) Test(org.junit.Test)

Example 4 with DummySession

use of org.apache.mina.core.session.DummySession in project zm-mailbox by Zimbra.

the class NioOutputStreamTest method writeByte.

@Test
public void writeByte() throws Exception {
    DummySession session = new DummySession();
    TestIoHandler handler = new TestIoHandler();
    session.setHandler(handler);
    NioOutputStream out = new NioOutputStream(session, 10, Integer.MAX_VALUE, Integer.MAX_VALUE);
    out.write('1');
    out.write('2');
    out.write('3');
    out.write('4');
    out.write('5');
    out.write('6');
    out.write('7');
    out.write('8');
    out.write('9');
    out.write('0');
    out.write('1');
    out.close();
    Assert.assertEquals(2, handler.getWriteCount());
    Assert.assertEquals("12345678901", handler.toString());
}
Also used : DummySession(org.apache.mina.core.session.DummySession) Test(org.junit.Test)

Aggregations

DummySession (org.apache.mina.core.session.DummySession)4 Test (org.junit.Test)4 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 DecoderException (org.apache.directory.api.asn1.DecoderException)1 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)1 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)1 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)1 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)1 Message (org.apache.directory.api.ldap.model.message.Message)1 IoSession (org.apache.mina.core.session.IoSession)1