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();
}
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();
}
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);
}
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 <item-not-found/> 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());
}
Aggregations