2013-02-07 15:19:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-02-17 23:58:40 +01:00
|
|
|
package org.jivesoftware.smackx.bytestreams.ibb;
|
|
|
|
|
2020-04-06 13:25:07 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2015-03-01 10:28:15 +01:00
|
|
|
import org.jivesoftware.smack.StanzaListener;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-02-18 15:05:19 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2018-04-07 21:25:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaError;
|
2020-04-11 21:59:21 +02:00
|
|
|
import org.jivesoftware.smack.test.util.Whitebox;
|
2014-09-04 11:04:51 +02:00
|
|
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-03-24 21:38:21 +01:00
|
|
|
import org.jivesoftware.smackx.InitExtensions;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.packet.Data;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.util.ConnectionUtils;
|
|
|
|
import org.jivesoftware.util.Protocol;
|
|
|
|
import org.jivesoftware.util.Verification;
|
2020-04-06 13:25:07 +02:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
2015-05-27 19:29:51 +02:00
|
|
|
import org.jxmpp.jid.EntityFullJid;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.JidTestUtil;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for InBandBytestreamSession.
|
|
|
|
* <p>
|
|
|
|
* Tests the basic behavior of an In-Band Bytestream session along with sending data encapsulated in
|
|
|
|
* IQ stanzas.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
2015-03-24 21:38:21 +01:00
|
|
|
public class InBandBytestreamSessionTest extends InitExtensions {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// settings
|
2017-12-13 23:10:11 +01:00
|
|
|
private static final EntityFullJid initiatorJID = JidTestUtil.DUMMY_AT_EXAMPLE_ORG_SLASH_DUMMYRESOURCE;
|
|
|
|
private static final EntityFullJid targetJID = JidTestUtil.FULL_JID_1_RESOURCE_1;
|
|
|
|
private static final String sessionID = "session_id";
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private static final int blockSize = 10;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// protocol verifier
|
2017-12-13 23:10:11 +01:00
|
|
|
private Protocol protocol;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// mocked XMPP connection
|
2017-12-13 23:10:11 +01:00
|
|
|
private XMPPConnection connection;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private Open initBytestream;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private Verification<Data, IQ> incrementingSequence;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize fields used in the tests.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws XMPPException if an XMPP protocol error was received.
|
|
|
|
* @throws SmackException if Smack detected an exceptional situation.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2020-04-06 13:25:07 +02:00
|
|
|
@BeforeEach
|
2015-02-14 09:43:44 +01:00
|
|
|
public void setup() throws XMPPException, SmackException, InterruptedException {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// build protocol verifier
|
|
|
|
protocol = new Protocol();
|
|
|
|
|
|
|
|
// create mocked XMPP connection
|
2018-04-18 14:10:14 +02:00
|
|
|
connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public void verify(Data request, IQ response) {
|
|
|
|
assertEquals(lastSeq++, request.getDataPacketExtension().getSeq());
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the output stream write(byte[]) method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @throws Exception should not happen
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void shouldSendThreeDataPackets1() throws Exception {
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
|
|
|
|
// set acknowledgments for the data packets
|
|
|
|
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
|
|
|
|
byte[] controlData = new byte[blockSize * 3];
|
|
|
|
|
|
|
|
OutputStream outputStream = session.getOutputStream();
|
|
|
|
outputStream.write(controlData);
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the output stream write(byte) method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @throws Exception should not happen
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void shouldSendThreeDataPackets2() throws Exception {
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
|
|
|
|
// set acknowledgments for the data packets
|
|
|
|
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
|
|
|
|
byte[] controlData = new byte[blockSize * 3];
|
|
|
|
|
|
|
|
OutputStream outputStream = session.getOutputStream();
|
|
|
|
for (byte b : controlData) {
|
|
|
|
outputStream.write(b);
|
|
|
|
}
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the output stream write(byte[], int, int) method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @throws Exception should not happen
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void shouldSendThreeDataPackets3() throws Exception {
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
|
|
|
|
// set acknowledgments for the data packets
|
|
|
|
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
|
|
|
|
byte[] controlData = new byte[(blockSize * 3) - 2];
|
|
|
|
|
|
|
|
OutputStream outputStream = session.getOutputStream();
|
|
|
|
int off = 0;
|
|
|
|
for (int i = 1; i <= 7; i++) {
|
|
|
|
outputStream.write(controlData, off, i);
|
|
|
|
off += i;
|
|
|
|
}
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the output stream flush() method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @throws Exception should not happen
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void shouldSendThirtyDataPackets() throws Exception {
|
|
|
|
byte[] controlData = new byte[blockSize * 3];
|
|
|
|
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
|
|
|
|
// set acknowledgments for the data packets
|
|
|
|
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
|
|
|
|
for (int i = 0; i < controlData.length; i++) {
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputStream outputStream = session.getOutputStream();
|
|
|
|
for (byte b : controlData) {
|
|
|
|
outputStream.write(b);
|
|
|
|
outputStream.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test successive calls to the output stream flush() method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @throws Exception should not happen
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void shouldSendNothingOnSuccessiveCallsToFlush() throws Exception {
|
|
|
|
byte[] controlData = new byte[blockSize * 3];
|
|
|
|
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
|
|
|
|
// set acknowledgments for the data packets
|
|
|
|
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence);
|
|
|
|
|
|
|
|
OutputStream outputStream = session.getOutputStream();
|
|
|
|
outputStream.write(controlData);
|
|
|
|
|
|
|
|
outputStream.flush();
|
|
|
|
outputStream.flush();
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the data is correctly chunked.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @throws Exception should not happen
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void shouldSendDataCorrectly() throws Exception {
|
|
|
|
// create random data
|
|
|
|
Random rand = new Random();
|
|
|
|
final byte[] controlData = new byte[256 * blockSize];
|
|
|
|
rand.nextBytes(controlData);
|
|
|
|
|
|
|
|
// compares the data of each packet with the control data
|
|
|
|
Verification<Data, IQ> dataVerification = new Verification<Data, IQ>() {
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public void verify(Data request, IQ response) {
|
|
|
|
byte[] decodedData = request.getDataPacketExtension().getDecodedData();
|
|
|
|
int seq = (int) request.getDataPacketExtension().getSeq();
|
|
|
|
for (int i = 0; i < decodedData.length; i++) {
|
|
|
|
assertEquals(controlData[(seq * blockSize) + i], decodedData[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// set acknowledgments for the data packets
|
|
|
|
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
|
|
|
|
for (int i = 0; i < controlData.length / blockSize; i++) {
|
|
|
|
protocol.addResponse(resultIQ, incrementingSequence, dataVerification);
|
|
|
|
}
|
|
|
|
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
|
|
|
|
OutputStream outputStream = session.getOutputStream();
|
|
|
|
outputStream.write(controlData);
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the input stream is closed the output stream should not be closed as well.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encode("Data");
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
|
|
|
|
Data data = new Data(dpe);
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Valid data packets should be confirmed.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encode("Data");
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
|
|
|
|
Data data = new Data(dpe);
|
|
|
|
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* If the data stanza has a sequence that is already used an 'unexpected-request' error should
|
2014-02-17 23:58:40 +01:00
|
|
|
* be returned. See XEP-0047 Section 2.2.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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>() {
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public void verify(IQ request, IQ response) {
|
2018-04-07 21:25:40 +02:00
|
|
|
assertEquals(StanzaError.Condition.unexpected_request,
|
2014-02-17 23:58:40 +01:00
|
|
|
request.getError().getCondition());
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// get IBB sessions data packet listener
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
InputStream inputStream = session.getInputStream();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// build data packets
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encode("Data");
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
|
|
|
|
Data data1 = new Data(dpe);
|
|
|
|
Data data2 = new Data(dpe);
|
|
|
|
|
|
|
|
// notify listener
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data1);
|
|
|
|
listener.processStanza(data2);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* If the data stanza contains invalid Base64 encoding an 'bad-request' error should be
|
2014-02-17 23:58:40 +01:00
|
|
|
* returned. See XEP-0047 Section 2.2.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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>() {
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
public void verify(IQ request, IQ response) {
|
2018-04-07 21:25:40 +02:00
|
|
|
assertEquals(StanzaError.Condition.bad_request,
|
2014-02-17 23:58:40 +01:00
|
|
|
request.getError().getCondition());
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// get IBB sessions data packet listener
|
|
|
|
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream,
|
|
|
|
initiatorJID);
|
|
|
|
InputStream inputStream = session.getInputStream();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// build data packets
|
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, "AA=BB");
|
|
|
|
Data data = new Data(dpe);
|
|
|
|
|
|
|
|
// notify listener
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
protocol.verifyAll();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* If a data stanza is received out of order the session should be closed. See XEP-0047 Section
|
2014-02-17 23:58:40 +01:00
|
|
|
* 2.2.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// build invalid packet with out of order sequence
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encode("Data");
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, 123, base64Data);
|
|
|
|
Data data = new Data(dpe);
|
|
|
|
|
|
|
|
// add data packets
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the input stream read(byte[], int, int) method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// set data packet acknowledgment and notify listener
|
|
|
|
for (int i = 0; i < controlData.length / blockSize; i++) {
|
|
|
|
protocol.addResponse(resultIQ);
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
|
|
|
|
Data data = new Data(dpe);
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
byte[] bytes = new byte[3 * blockSize];
|
2019-05-07 22:58:02 +02:00
|
|
|
int read;
|
2014-02-17 23:58:40 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the input stream read() method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// set data packet acknowledgment and notify listener
|
|
|
|
for (int i = 0; i < controlData.length / blockSize; i++) {
|
|
|
|
protocol.addResponse(resultIQ);
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
|
|
|
|
Data data = new Data(dpe);
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the output stream is closed the input stream should not be closed as well.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// build data packet
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encode("Data");
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
|
|
|
|
Data data = new Data(dpe);
|
|
|
|
|
|
|
|
// add data packets
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the input stream is closed concurrently there should be no deadlock.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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();
|
2020-04-11 21:59:21 +02:00
|
|
|
StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// build data packet
|
2014-09-04 11:04:51 +02:00
|
|
|
String base64Data = Base64.encode("Data");
|
2014-02-17 23:58:40 +01:00
|
|
|
DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
|
|
|
|
Data data = new Data(dpe);
|
|
|
|
|
|
|
|
// add data packets
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
Thread closer = new Thread(new Runnable() {
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-02-17 23:58:40 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|