use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension 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.DataPacketExtension 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.DataPacketExtension in project Smack by igniterealtime.
the class InBandBytestreamSessionMessageTest 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);
// 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);
}
// 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();
}
use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension in project Smack by igniterealtime.
the class InBandBytestreamSessionMessageTest 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 with message stanza
initBytestream = new Open(sessionID, blockSize, StanzaType.MESSAGE);
initBytestream.setFrom(initiatorJID);
initBytestream.setTo(targetJID);
incrementingSequence = new Verification<Message, IQ>() {
long lastSeq = 0;
@Override
public void verify(Message request, IQ response) {
DataPacketExtension dpe = (DataPacketExtension) request.getExtension(DataPacketExtension.ELEMENT, DataPacketExtension.NAMESPACE);
assertEquals(lastSeq++, dpe.getSeq());
}
};
}
use of org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension 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();
}
Aggregations