Java Code Examples for org.jivesoftware.smack.StanzaListener#processStanza()

The following examples show how to use org.jivesoftware.smack.StanzaListener#processStanza() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 7 votes vote down vote up
/**
 * 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, "dataPacketListener", StanzaListener.class);

    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
    Data data = new Data(dpe);

    listener.processStanza(data);

    protocol.verifyAll();

}
 
Example 2
Source File: InBandBytestreamSessionMessageTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * If a data stanza 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, "dataPacketListener", StanzaListener.class);

    // build invalid packet with out of order sequence
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 123, base64Data);
    Message dataMessage = StanzaBuilder.buildMessage()
            .addExtension(dpe)
            .build();

    // 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();

}
 
Example 3
Source File: InBandBytestreamSessionMessageTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * 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, "dataPacketListener", 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 = StanzaBuilder.buildMessage()
                .addExtension(dpe)
                .build();
        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();

}
 
Example 4
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * 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, "dataPacketListener", 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);

}
 
Example 5
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * If the data stanza 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(StanzaError.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, "dataPacketListener", 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();

}
 
Example 6
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * If the data stanza 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(StanzaError.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, "dataPacketListener", 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();

}
 
Example 7
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * If a data stanza 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, "dataPacketListener", 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();

}
 
Example 8
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * 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, "dataPacketListener", 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();

}
 
Example 9
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * 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, "dataPacketListener", 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();

}
 
Example 10
Source File: InBandBytestreamSessionMessageTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * 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, "dataPacketListener", 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 = StanzaBuilder.buildMessage()
                .addExtension(dpe)
                .build();
        listener.processStanza(dataMessage);
    }

    byte[] bytes = new byte[3 * blockSize];
    int read;
    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();

}
 
Example 11
Source File: InBandBytestreamSessionTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * 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, "dataPacketListener", 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;
    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();

}