Search in sources :

Example 6 with Data

use of org.jivesoftware.smackx.bytestreams.ibb.packet.Data 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 Data

use of org.jivesoftware.smackx.bytestreams.ibb.packet.Data 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 Data

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

the class InBandBytestreamSessionTest 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
    initBytestream = new Open(sessionID, blockSize);
    initBytestream.setFrom(initiatorJID);
    initBytestream.setTo(targetJID);
    incrementingSequence = new Verification<Data, IQ>() {

        long lastSeq = 0;

        @Override
        public void verify(Data request, IQ response) {
            assertEquals(lastSeq++, request.getDataPacketExtension().getSeq());
        }
    };
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) Protocol(org.jivesoftware.util.Protocol) Open(org.jivesoftware.smackx.bytestreams.ibb.packet.Open) Before(org.junit.Before)

Example 9 with Data

use of org.jivesoftware.smackx.bytestreams.ibb.packet.Data 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)

Example 10 with Data

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

the class InBandBytestreamSessionTest method shouldReplyWithErrorIfAlreadyUsedSequenceIsReceived.

/**
     * If the data stanza(/packet) has a sequence that is already used an 'unexpected-request' error should
     * be returned. See XEP-0047 Section 2.2.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldReplyWithErrorIfAlreadyUsedSequenceIsReceived() throws Exception {
    // verify reply to first valid data packet is of type RESULT
    protocol.addResponse(null, Verification.requestTypeRESULT);
    // verify reply to invalid data packet is an error
    protocol.addResponse(null, Verification.requestTypeERROR, new Verification<IQ, IQ>() {

        @Override
        public void verify(IQ request, IQ response) {
            assertEquals(XMPPError.Condition.unexpected_request, request.getError().getCondition());
        }
    });
    // 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 packets
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
    Data data1 = new Data(dpe);
    Data data2 = new Data(dpe);
    // notify listener
    listener.processStanza(data1);
    listener.processStanza(data2);
    protocol.verifyAll();
}
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) Test(org.junit.Test)

Aggregations

Data (org.jivesoftware.smackx.bytestreams.ibb.packet.Data)13 Test (org.junit.Test)11 IQ (org.jivesoftware.smack.packet.IQ)10 DataPacketExtension (org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension)10 InputStream (java.io.InputStream)9 StanzaListener (org.jivesoftware.smack.StanzaListener)9 IOException (java.io.IOException)4 Random (java.util.Random)3 OutputStream (java.io.OutputStream)2 SmackException (org.jivesoftware.smack.SmackException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)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 Verification (org.jivesoftware.util.Verification)1 Before (org.junit.Before)1