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();
}
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();
}
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());
}
};
}
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();
}
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();
}
Aggregations