Search in sources :

Example 11 with DataPacketExtension

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

Example 12 with DataPacketExtension

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

the class InBandBytestreamSessionTest method shouldConfirmReceivedDataPacket.

/**
     * Valid data packets should be confirmed.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldConfirmReceivedDataPacket() throws Exception {
    // verify data packet confirmation is of type RESULT
    protocol.addResponse(null, Verification.requestTypeRESULT);
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
    Data data = new Data(dpe);
    listener.processStanza(data);
    protocol.verifyAll();
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) InputStream(java.io.InputStream) StanzaListener(org.jivesoftware.smack.StanzaListener) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) Test(org.junit.Test)

Example 13 with DataPacketExtension

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

the class InBandBytestreamSessionTest method shouldNotCloseBothStreamsIfOutputStreamIsClosed.

/**
     * If the input stream is closed the output stream should not be closed as well.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldNotCloseBothStreamsIfOutputStreamIsClosed() throws Exception {
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    OutputStream outputStream = session.getOutputStream();
    outputStream.close();
    // verify data packet confirmation is of type RESULT
    protocol.addResponse(null, Verification.requestTypeRESULT);
    // insert data to read
    InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, StanzaListener.class);
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
    Data data = new Data(dpe);
    listener.processStanza(data);
    // verify no packet send
    protocol.verifyAll();
    try {
        outputStream.flush();
        fail("should throw an exception");
    } catch (IOException e) {
        assertTrue(e.getMessage().contains("closed"));
    }
    assertTrue(inputStream.read() != 0);
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) StanzaListener(org.jivesoftware.smack.StanzaListener) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with DataPacketExtension

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

the class DataListenerTest method shouldReplyErrorIfSessionIsUnknown.

/**
     * If a data stanza(/packet) of an unknown session is received it should be replied
     * with an &lt;item-not-found/&gt; error.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldReplyErrorIfSessionIsUnknown() throws Exception {
    // mock connection
    XMPPConnection connection = mock(XMPPConnection.class);
    // initialize InBandBytestreamManager to get the DataListener
    InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);
    // get the DataListener from InBandByteStreamManager
    DataListener dataListener = Whitebox.getInternalState(byteStreamManager, DataListener.class);
    DataPacketExtension dpe = new DataPacketExtension("unknownSessionID", 0, "Data");
    Data data = new Data(dpe);
    data.setFrom(initiatorJID);
    data.setTo(targetJID);
    dataListener.handleIQRequest(data);
    // wait because packet is processed in an extra thread
    Thread.sleep(200);
    // capture reply to the In-Band Bytestream close request
    ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
    verify(connection).sendStanza(argument.capture());
    // assert that reply is the correct error packet
    assertEquals(initiatorJID, argument.getValue().getTo());
    assertEquals(IQ.Type.error, argument.getValue().getType());
    assertEquals(XMPPError.Condition.item_not_found, argument.getValue().getError().getCondition());
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) IQ(org.jivesoftware.smack.packet.IQ) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) XMPPConnection(org.jivesoftware.smack.XMPPConnection) 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