Search in sources :

Example 6 with DataPacketExtension

use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension in project Smack by igniterealtime.

the class InBandBytestreamSessionTest method shouldReadAllReceivedData1.

/**
     * Test the input stream read(byte[], int, int) method.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldReadAllReceivedData1() throws Exception {
    // create random data
    Random rand = new Random();
    byte[] controlData = new byte[3 * blockSize];
    rand.nextBytes(controlData);
    IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
    // get IBB sessions data packet listener
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
    // set data packet acknowledgment and notify listener
    for (int i = 0; i < controlData.length / blockSize; i++) {
        protocol.addResponse(resultIQ);
        String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
        DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
        Data data = new Data(dpe);
        listener.processStanza(data);
    }
    byte[] bytes = new byte[3 * blockSize];
    int read = 0;
    read = inputStream.read(bytes, 0, blockSize);
    assertEquals(blockSize, read);
    read = inputStream.read(bytes, 10, blockSize);
    assertEquals(blockSize, read);
    read = inputStream.read(bytes, 20, blockSize);
    assertEquals(blockSize, read);
    // verify data
    for (int i = 0; i < bytes.length; i++) {
        assertEquals(controlData[i], bytes[i]);
    }
    protocol.verifyAll();
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) Random(java.util.Random) InputStream(java.io.InputStream) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) Test(org.junit.Test)

Example 7 with DataPacketExtension

use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension in project Smack by igniterealtime.

the class InBandBytestreamSessionTest method shouldNotDeadlockIfInputStreamIsClosed.

/**
     * If the input stream is closed concurrently there should be no deadlock.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldNotDeadlockIfInputStreamIsClosed() throws Exception {
    // acknowledgment for data packet
    IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
    protocol.addResponse(resultIQ);
    // get IBB sessions data packet listener
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    final InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
    // build data packet
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
    Data data = new Data(dpe);
    // add data packets
    listener.processStanza(data);
    Thread closer = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(200);
                inputStream.close();
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    });
    closer.start();
    try {
        byte[] bytes = new byte[20];
        while (inputStream.read(bytes) != -1) {
        }
        inputStream.read();
        fail("should throw an exception");
    } catch (IOException e) {
        assertTrue(e.getMessage().contains("closed"));
    }
    protocol.verifyAll();
}
Also used : InputStream(java.io.InputStream) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException) DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) Test(org.junit.Test)

Example 8 with DataPacketExtension

use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension in project Smack by igniterealtime.

the class InBandBytestreamSessionMessageTest method shouldReadAllReceivedData2.

/**
     * Test the input stream read() method.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldReadAllReceivedData2() throws Exception {
    // create random data
    Random rand = new Random();
    byte[] controlData = new byte[3 * blockSize];
    rand.nextBytes(controlData);
    // get IBB sessions data packet listener
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
    // verify data packet and notify listener
    for (int i = 0; i < controlData.length / blockSize; i++) {
        String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
        DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
        Message dataMessage = new Message();
        dataMessage.addExtension(dpe);
        listener.processStanza(dataMessage);
    }
    // read data
    byte[] bytes = new byte[3 * blockSize];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) inputStream.read();
    }
    // verify data
    for (int i = 0; i < bytes.length; i++) {
        assertEquals(controlData[i], bytes[i]);
    }
    protocol.verifyAll();
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) Random(java.util.Random) Message(org.jivesoftware.smack.packet.Message) InputStream(java.io.InputStream) StanzaListener(org.jivesoftware.smack.StanzaListener) Test(org.junit.Test)

Example 9 with DataPacketExtension

use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension in project Smack by igniterealtime.

the class InBandBytestreamSessionMessageTest method setup.

/**
     * Initialize fields used in the tests.
     * @throws XMPPException 
     * @throws SmackException 
     * @throws InterruptedException 
     */
@Before
public void setup() throws XMPPException, SmackException, InterruptedException {
    // build protocol verifier
    protocol = new Protocol();
    // create mocked XMPP connection
    connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID, xmppServer);
    // initialize InBandBytestreamManager to get the InitiationListener
    byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);
    // create a In-Band Bytestream open packet with message stanza
    initBytestream = new Open(sessionID, blockSize, StanzaType.MESSAGE);
    initBytestream.setFrom(initiatorJID);
    initBytestream.setTo(targetJID);
    incrementingSequence = new Verification<Message, IQ>() {

        long lastSeq = 0;

        @Override
        public void verify(Message request, IQ response) {
            DataPacketExtension dpe = (DataPacketExtension) request.getExtension(DataPacketExtension.ELEMENT, DataPacketExtension.NAMESPACE);
            assertEquals(lastSeq++, dpe.getSeq());
        }
    };
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) Message(org.jivesoftware.smack.packet.Message) IQ(org.jivesoftware.smack.packet.IQ) Protocol(org.jivesoftware.util.Protocol) Open(org.jivesoftware.smackx.bytestreams.ibb.packet.Open) Before(org.junit.Before)

Example 10 with DataPacketExtension

use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension in project Smack by igniterealtime.

the class InBandBytestreamSessionTest method shouldNotCloseBothStreamsIfInputStreamIsClosed.

/**
     * If the output stream is closed the input stream should not be closed as well.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldNotCloseBothStreamsIfInputStreamIsClosed() throws Exception {
    // acknowledgment for data packet
    IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
    protocol.addResponse(resultIQ);
    // get IBB sessions data packet listener
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
    // build data packet
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
    Data data = new Data(dpe);
    // add data packets
    listener.processStanza(data);
    inputStream.close();
    protocol.verifyAll();
    try {
        while (inputStream.read() != -1) {
        }
        inputStream.read();
        fail("should throw an exception");
    } catch (IOException e) {
        assertTrue(e.getMessage().contains("closed"));
    }
    session.getOutputStream().flush();
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) InputStream(java.io.InputStream) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

DataPacketExtension (org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension)14 Test (org.junit.Test)13 InputStream (java.io.InputStream)12 StanzaListener (org.jivesoftware.smack.StanzaListener)12 IQ (org.jivesoftware.smack.packet.IQ)10 Data (org.jivesoftware.smackx.bytestreams.ibb.packet.Data)10 IOException (java.io.IOException)5 Random (java.util.Random)4 Message (org.jivesoftware.smack.packet.Message)4 OutputStream (java.io.OutputStream)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 XMPPException (org.jivesoftware.smack.XMPPException)1 Open (org.jivesoftware.smackx.bytestreams.ibb.packet.Open)1 Protocol (org.jivesoftware.util.Protocol)1 Before (org.junit.Before)1