Search in sources :

Example 1 with DataPacketExtension

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

the class InBandBytestreamSessionMessageTest 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);
    // 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);
    }
    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) Message(org.jivesoftware.smack.packet.Message) InputStream(java.io.InputStream) StanzaListener(org.jivesoftware.smack.StanzaListener) Test(org.junit.Test)

Example 2 with DataPacketExtension

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

the class InBandBytestreamSessionMessageTest method shouldSendCloseRequestIfInvalidSequenceReceived.

/**
     * If a data stanza(/packet) is received out of order the session should be closed. See XEP-0047 Section
     * 2.2.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldSendCloseRequestIfInvalidSequenceReceived() throws Exception {
    // confirm close request
    IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
    protocol.addResponse(resultIQ, Verification.requestTypeSET, Verification.correspondingSenderReceiver);
    // 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 invalid packet with out of order sequence
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 123, base64Data);
    Message dataMessage = new Message();
    dataMessage.addExtension(dpe);
    // add data packets
    listener.processStanza(dataMessage);
    // read until exception is thrown
    try {
        inputStream.read();
        fail("exception should be thrown");
    } catch (IOException e) {
        assertTrue(e.getMessage().contains("Packets out of sequence"));
    }
    protocol.verifyAll();
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) Message(org.jivesoftware.smack.packet.Message) InputStream(java.io.InputStream) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with DataPacketExtension

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

the class InBandBytestreamSessionTest 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);
    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);
    }
    // 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) 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 4 with DataPacketExtension

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

the class InBandBytestreamSessionTest method shouldSendCloseRequestIfInvalidSequenceReceived.

/**
     * If a data stanza(/packet) is received out of order the session should be closed. See XEP-0047 Section
     * 2.2.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldSendCloseRequestIfInvalidSequenceReceived() throws Exception {
    IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
    // confirm data packet with invalid sequence
    protocol.addResponse(resultIQ);
    // confirm close request
    protocol.addResponse(resultIQ, Verification.requestTypeSET, Verification.correspondingSenderReceiver);
    // 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 invalid packet with out of order sequence
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 123, base64Data);
    Data data = new Data(dpe);
    // add data packets
    listener.processStanza(data);
    // read until exception is thrown
    try {
        inputStream.read();
        fail("exception should be thrown");
    } catch (IOException e) {
        assertTrue(e.getMessage().contains("Packets out of sequence"));
    }
    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) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with DataPacketExtension

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

the class InBandBytestreamSessionTest method shouldReplyWithErrorIfDataIsInvalid.

/**
     * If the data stanza(/packet) contains invalid Base64 encoding an 'bad-request' error should be
     * returned. See XEP-0047 Section 2.2.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldReplyWithErrorIfDataIsInvalid() throws Exception {
    // 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.bad_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
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, "AA=BB");
    Data data = new Data(dpe);
    // notify listener
    listener.processStanza(data);
    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

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