mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-15 20:12:04 +01:00
Ensure that connection is authenticated in PingManager
This commit is contained in:
parent
50e068b6a7
commit
7521ef951a
2 changed files with 51 additions and 20 deletions
|
@ -177,9 +177,15 @@ public class PingManager extends Manager {
|
||||||
* @throws NotConnectedException
|
* @throws NotConnectedException
|
||||||
*/
|
*/
|
||||||
public boolean ping(String jid, long pingTimeout) throws NotConnectedException, NoResponseException {
|
public boolean ping(String jid, long pingTimeout) throws NotConnectedException, NoResponseException {
|
||||||
|
final XMPPConnection connection = connection();
|
||||||
|
// Packet collector for IQs needs an connection that was at least authenticated once,
|
||||||
|
// otherwise the client JID will be null causing an NPE
|
||||||
|
if (!connection.isAuthenticated()) {
|
||||||
|
throw new NotConnectedException();
|
||||||
|
}
|
||||||
Ping ping = new Ping(jid);
|
Ping ping = new Ping(jid);
|
||||||
try {
|
try {
|
||||||
connection().createPacketCollectorAndSend(ping).nextResultOrThrow(pingTimeout);
|
connection.createPacketCollectorAndSend(ping).nextResultOrThrow(pingTimeout);
|
||||||
}
|
}
|
||||||
catch (XMPPException exc) {
|
catch (XMPPException exc) {
|
||||||
return jid.equals(connection().getServiceName());
|
return jid.equals(connection().getServiceName());
|
||||||
|
|
|
@ -21,13 +21,16 @@ import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.jivesoftware.smack.DummyConnection;
|
import org.jivesoftware.smack.DummyConnection;
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
|
||||||
import org.jivesoftware.smack.ThreadedDummyConnection;
|
import org.jivesoftware.smack.ThreadedDummyConnection;
|
||||||
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
import org.jivesoftware.smackx.InitExtensions;
|
import org.jivesoftware.smackx.InitExtensions;
|
||||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
||||||
|
@ -36,13 +39,10 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class PingTest extends InitExtensions {
|
public class PingTest extends InitExtensions {
|
||||||
private DummyConnection dummyCon;
|
|
||||||
private ThreadedDummyConnection threadedCon;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void initSmackTestSuite() {
|
||||||
dummyCon = new DummyConnection();
|
SmackTestSuite.init();
|
||||||
threadedCon = new ThreadedDummyConnection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -72,8 +72,8 @@ public class PingTest extends InitExtensions {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkSendingPing() throws InterruptedException {
|
public void checkSendingPing() throws InterruptedException, SmackException, IOException, XMPPException {
|
||||||
dummyCon = new DummyConnection();
|
DummyConnection dummyCon = getAuthentiactedDummyConnection();
|
||||||
PingManager pinger = PingManager.getInstanceFor(dummyCon);
|
PingManager pinger = PingManager.getInstanceFor(dummyCon);
|
||||||
try {
|
try {
|
||||||
pinger.ping("test@myserver.com");
|
pinger.ping("test@myserver.com");
|
||||||
|
@ -88,7 +88,7 @@ public class PingTest extends InitExtensions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkSuccessfulPing() throws Exception {
|
public void checkSuccessfulPing() throws Exception {
|
||||||
threadedCon = new ThreadedDummyConnection();
|
ThreadedDummyConnection threadedCon = getAuthentiactedDummyConnection();
|
||||||
|
|
||||||
PingManager pinger = PingManager.getInstanceFor(threadedCon);
|
PingManager pinger = PingManager.getInstanceFor(threadedCon);
|
||||||
|
|
||||||
|
@ -101,10 +101,12 @@ public class PingTest extends InitExtensions {
|
||||||
/**
|
/**
|
||||||
* DummyConnection will not reply so it will timeout.
|
* DummyConnection will not reply so it will timeout.
|
||||||
* @throws SmackException
|
* @throws SmackException
|
||||||
|
* @throws XMPPException
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void checkFailedPingOnTimeout() throws SmackException {
|
public void checkFailedPingOnTimeout() throws SmackException, IOException, XMPPException {
|
||||||
dummyCon = new DummyConnection();
|
DummyConnection dummyCon = getAuthenticatedDummyConnectionWithoutIqReplies();
|
||||||
PingManager pinger = PingManager.getInstanceFor(dummyCon);
|
PingManager pinger = PingManager.getInstanceFor(dummyCon);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -122,7 +124,7 @@ public class PingTest extends InitExtensions {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void checkFailedPingToEntityError() throws Exception {
|
public void checkFailedPingToEntityError() throws Exception {
|
||||||
threadedCon = new ThreadedDummyConnection();
|
ThreadedDummyConnection threadedCon = getAuthentiactedDummyConnection();
|
||||||
//@formatter:off
|
//@formatter:off
|
||||||
String reply =
|
String reply =
|
||||||
"<iq type='error' id='qrzSp-16' to='test@myserver.com'>" +
|
"<iq type='error' id='qrzSp-16' to='test@myserver.com'>" +
|
||||||
|
@ -144,7 +146,7 @@ public class PingTest extends InitExtensions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkPingToServerSuccess() throws Exception {
|
public void checkPingToServerSuccess() throws Exception {
|
||||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
ThreadedDummyConnection con = getAuthentiactedDummyConnection();
|
||||||
PingManager pinger = PingManager.getInstanceFor(con);
|
PingManager pinger = PingManager.getInstanceFor(con);
|
||||||
|
|
||||||
boolean pingSuccess = pinger.pingMyServer();
|
boolean pingSuccess = pinger.pingMyServer();
|
||||||
|
@ -158,7 +160,7 @@ public class PingTest extends InitExtensions {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void checkPingToServerError() throws Exception {
|
public void checkPingToServerError() throws Exception {
|
||||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
ThreadedDummyConnection con = getAuthentiactedDummyConnection();
|
||||||
//@formatter:off
|
//@formatter:off
|
||||||
String reply =
|
String reply =
|
||||||
"<iq type='error' id='qrzSp-16' to='test@myserver.com' from='" + con.getServiceName() + "'>" +
|
"<iq type='error' id='qrzSp-16' to='test@myserver.com' from='" + con.getServiceName() + "'>" +
|
||||||
|
@ -179,8 +181,8 @@ public class PingTest extends InitExtensions {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkPingToServerTimeout() throws NotConnectedException {
|
public void checkPingToServerTimeout() throws SmackException, IOException, XMPPException {
|
||||||
DummyConnection con = new DummyConnection();
|
DummyConnection con = getAuthenticatedDummyConnectionWithoutIqReplies();
|
||||||
PingManager pinger = PingManager.getInstanceFor(con);
|
PingManager pinger = PingManager.getInstanceFor(con);
|
||||||
|
|
||||||
boolean res = pinger.pingMyServer();
|
boolean res = pinger.pingMyServer();
|
||||||
|
@ -189,7 +191,7 @@ public class PingTest extends InitExtensions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkSuccessfulDiscoRequest() throws Exception {
|
public void checkSuccessfulDiscoRequest() throws Exception {
|
||||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
ThreadedDummyConnection con = getAuthentiactedDummyConnection();
|
||||||
DiscoverInfo info = new DiscoverInfo();
|
DiscoverInfo info = new DiscoverInfo();
|
||||||
info.addFeature(Ping.NAMESPACE);
|
info.addFeature(Ping.NAMESPACE);
|
||||||
|
|
||||||
|
@ -211,7 +213,7 @@ public class PingTest extends InitExtensions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkUnuccessfulDiscoRequest() throws Exception {
|
public void checkUnuccessfulDiscoRequest() throws Exception {
|
||||||
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
ThreadedDummyConnection con = getAuthentiactedDummyConnection();
|
||||||
DiscoverInfo info = new DiscoverInfo();
|
DiscoverInfo info = new DiscoverInfo();
|
||||||
info.addFeature(Ping.NAMESPACE);
|
info.addFeature(Ping.NAMESPACE);
|
||||||
|
|
||||||
|
@ -230,4 +232,27 @@ public class PingTest extends InitExtensions {
|
||||||
|
|
||||||
assertFalse(pingSupported);
|
assertFalse(pingSupported);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ThreadedDummyConnection getAuthentiactedDummyConnection() throws SmackException, IOException, XMPPException {
|
||||||
|
ThreadedDummyConnection connection = new ThreadedDummyConnection();
|
||||||
|
connection.connect();
|
||||||
|
connection.login("foo", "bar");
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The returned connection won't send replies to IQs
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws XMPPException
|
||||||
|
* @throws IOException
|
||||||
|
* @throws SmackException
|
||||||
|
*/
|
||||||
|
private static DummyConnection getAuthenticatedDummyConnectionWithoutIqReplies() throws SmackException, IOException, XMPPException {
|
||||||
|
DummyConnection con = new DummyConnection();
|
||||||
|
con.setPacketReplyTimeout(500);
|
||||||
|
con.connect();
|
||||||
|
con.login("foo", "bar");
|
||||||
|
return con;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue