2013-01-04 12:43:35 +01:00
|
|
|
/**
|
2014-02-17 18:57:38 +01:00
|
|
|
*
|
2019-10-27 07:51:25 +01:00
|
|
|
* Copyright 2012-2019 Florian Schmaus
|
2013-01-04 12:43:35 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-01-04 12:43:35 +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.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.ping;
|
|
|
|
|
2020-02-23 18:30:59 +01:00
|
|
|
import static org.hamcrest.MatcherAssert.assertThat;
|
2017-12-13 23:10:11 +01:00
|
|
|
import static org.jivesoftware.smack.test.util.CharSequenceEquals.equalsCharSequence;
|
2020-04-06 13:25:07 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
2015-02-14 17:15:02 +01:00
|
|
|
import static org.jxmpp.jid.JidTestUtil.DUMMY_AT_EXAMPLE_ORG;
|
2014-01-31 04:49:47 +01:00
|
|
|
|
2014-09-29 15:56:50 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
import org.jivesoftware.smack.DummyConnection;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2013-03-19 03:37:36 +01:00
|
|
|
import org.jivesoftware.smack.ThreadedDummyConnection;
|
2014-09-29 15:56:50 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2013-03-19 03:37:36 +01:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2013-03-19 03:37:36 +01:00
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-03-10 21:48:46 +01:00
|
|
|
import org.jivesoftware.smackx.InitExtensions;
|
2014-02-14 18:13:51 +01:00
|
|
|
import org.jivesoftware.smackx.ping.packet.Ping;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2020-04-06 13:25:07 +02:00
|
|
|
import org.junit.jupiter.api.Test;
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2014-03-10 21:48:46 +01:00
|
|
|
public class PingTest extends InitExtensions {
|
2014-09-29 15:56:50 +02:00
|
|
|
|
2013-04-16 03:39:17 +02:00
|
|
|
@Test
|
|
|
|
public void checkProvider() throws Exception {
|
|
|
|
// @formatter:off
|
|
|
|
String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>"
|
2018-05-09 23:06:12 +02:00
|
|
|
+ "<ping xmlns='urn:xmpp:ping'/>"
|
2013-04-16 03:39:17 +02:00
|
|
|
+ "</iq>";
|
|
|
|
// @formatter:on
|
|
|
|
DummyConnection con = new DummyConnection();
|
2014-11-09 18:30:16 +01:00
|
|
|
con.connect();
|
2013-04-16 03:39:17 +02:00
|
|
|
// Enable ping for this connection
|
|
|
|
PingManager.getInstanceFor(con);
|
2017-12-13 23:10:11 +01:00
|
|
|
IQ pingRequest = PacketParserUtils.parseStanza(control);
|
2013-04-16 03:39:17 +02:00
|
|
|
|
|
|
|
assertTrue(pingRequest instanceof Ping);
|
|
|
|
|
2015-06-05 14:04:17 +02:00
|
|
|
con.processStanza(pingRequest);
|
2013-04-16 03:39:17 +02:00
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
Stanza pongPacket = con.getSentPacket();
|
2013-04-16 03:39:17 +02:00
|
|
|
assertTrue(pongPacket instanceof IQ);
|
|
|
|
|
|
|
|
IQ pong = (IQ) pongPacket;
|
2015-02-14 17:15:02 +01:00
|
|
|
assertThat("capulet.lit", equalsCharSequence(pong.getTo()));
|
2015-02-05 11:17:27 +01:00
|
|
|
assertEquals("s2c1", pong.getStanzaId());
|
2014-06-06 02:20:45 +02:00
|
|
|
assertEquals(IQ.Type.result, pong.getType());
|
2013-04-16 03:39:17 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 12:43:35 +01:00
|
|
|
@Test
|
2014-09-29 15:56:50 +02:00
|
|
|
public void checkSendingPing() throws InterruptedException, SmackException, IOException, XMPPException {
|
2017-12-13 23:10:11 +01:00
|
|
|
DummyConnection dummyCon = getAuthenticatedDummyConnection();
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(dummyCon);
|
2014-03-12 11:50:05 +01:00
|
|
|
try {
|
2015-02-14 17:15:02 +01:00
|
|
|
pinger.ping(DUMMY_AT_EXAMPLE_ORG);
|
2014-03-12 11:50:05 +01:00
|
|
|
}
|
|
|
|
catch (SmackException e) {
|
|
|
|
// Ignore the fact the server won't answer for this unit test.
|
|
|
|
}
|
2013-03-19 03:37:36 +01:00
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
Stanza sentPacket = dummyCon.getSentPacket();
|
2013-03-19 03:37:36 +01:00
|
|
|
assertTrue(sentPacket instanceof Ping);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void checkSuccessfulPing() throws Exception {
|
2017-12-13 23:10:11 +01:00
|
|
|
ThreadedDummyConnection threadedCon = getAuthenticatedDummyConnection();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(threadedCon);
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
boolean pingSuccess = pinger.ping(DUMMY_AT_EXAMPLE_ORG);
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
assertTrue(pingSuccess);
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
}
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
/**
|
|
|
|
* DummyConnection will not reply so it will timeout.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws SmackException if Smack detected an exceptional situation.
|
|
|
|
* @throws XMPPException if an XMPP protocol error was received.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2013-03-19 03:37:36 +01:00
|
|
|
*/
|
|
|
|
@Test
|
2015-02-14 09:43:44 +01:00
|
|
|
public void checkFailedPingOnTimeout() throws SmackException, IOException, XMPPException, InterruptedException {
|
2014-09-29 15:56:50 +02:00
|
|
|
DummyConnection dummyCon = getAuthenticatedDummyConnectionWithoutIqReplies();
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(dummyCon);
|
2013-03-19 03:37:36 +01:00
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
try {
|
2015-02-14 17:15:02 +01:00
|
|
|
pinger.ping(DUMMY_AT_EXAMPLE_ORG);
|
2014-03-12 11:50:05 +01:00
|
|
|
}
|
|
|
|
catch (NoResponseException e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fail();
|
2013-01-04 12:43:35 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
/**
|
2013-03-23 12:59:08 +01:00
|
|
|
* Server returns an exception for entity.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws Exception if an exception occurs.
|
2013-03-19 03:37:36 +01:00
|
|
|
*/
|
|
|
|
@Test
|
2013-03-23 12:59:08 +01:00
|
|
|
public void checkFailedPingToEntityError() throws Exception {
|
2017-12-13 23:10:11 +01:00
|
|
|
ThreadedDummyConnection threadedCon = getAuthenticatedDummyConnection();
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:off
|
2018-05-09 23:06:12 +02:00
|
|
|
String reply =
|
2013-03-19 03:37:36 +01:00
|
|
|
"<iq type='error' id='qrzSp-16' to='test@myserver.com'>" +
|
|
|
|
"<ping xmlns='urn:xmpp:ping'/>" +
|
|
|
|
"<error type='cancel'>" +
|
|
|
|
"<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
|
2018-05-09 23:06:12 +02:00
|
|
|
"</error>" +
|
2013-03-19 03:37:36 +01:00
|
|
|
"</iq>";
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:on
|
2017-12-13 23:10:11 +01:00
|
|
|
IQ serviceUnavailable = PacketParserUtils.parseStanza(reply);
|
2013-03-23 12:59:08 +01:00
|
|
|
threadedCon.addIQReply(serviceUnavailable);
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(threadedCon);
|
2013-03-19 03:37:36 +01:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
boolean pingSuccess = pinger.ping(DUMMY_AT_EXAMPLE_ORG);
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
assertFalse(pingSuccess);
|
2013-03-23 12:59:08 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
@Test
|
|
|
|
public void checkPingToServerSuccess() throws Exception {
|
2017-12-13 23:10:11 +01:00
|
|
|
ThreadedDummyConnection con = getAuthenticatedDummyConnection();
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(con);
|
2013-03-23 12:59:08 +01:00
|
|
|
|
|
|
|
boolean pingSuccess = pinger.pingMyServer();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
assertTrue(pingSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server returns an exception.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws Exception if an exception occurs.
|
2013-03-23 12:59:08 +01:00
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void checkPingToServerError() throws Exception {
|
2017-12-13 23:10:11 +01:00
|
|
|
ThreadedDummyConnection con = getAuthenticatedDummyConnection();
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:off
|
2018-05-09 23:06:12 +02:00
|
|
|
String reply =
|
2015-05-18 16:48:23 +02:00
|
|
|
"<iq type='error' id='qrzSp-16' to='test@myserver.com' from='" + con.getXMPPServiceDomain() + "'>" +
|
2013-03-23 12:59:08 +01:00
|
|
|
"<ping xmlns='urn:xmpp:ping'/>" +
|
|
|
|
"<error type='cancel'>" +
|
|
|
|
"<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
|
2018-05-09 23:06:12 +02:00
|
|
|
"</error>" +
|
2013-03-23 12:59:08 +01:00
|
|
|
"</iq>";
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:on
|
2017-12-13 23:10:11 +01:00
|
|
|
IQ serviceUnavailable = PacketParserUtils.parseStanza(reply);
|
2013-03-23 12:59:08 +01:00
|
|
|
con.addIQReply(serviceUnavailable);
|
|
|
|
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(con);
|
2013-03-23 12:59:08 +01:00
|
|
|
|
|
|
|
boolean pingSuccess = pinger.pingMyServer();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
assertTrue(pingSuccess);
|
2013-03-19 03:37:36 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
@Test
|
2015-02-14 09:43:44 +01:00
|
|
|
public void checkPingToServerTimeout() throws SmackException, IOException, XMPPException, InterruptedException {
|
2014-09-29 15:56:50 +02:00
|
|
|
DummyConnection con = getAuthenticatedDummyConnectionWithoutIqReplies();
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(con);
|
2013-03-23 12:59:08 +01:00
|
|
|
|
2014-03-28 14:27:14 +01:00
|
|
|
boolean res = pinger.pingMyServer();
|
|
|
|
assertFalse(res);
|
2013-03-23 12:59:08 +01:00
|
|
|
}
|
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
@Test
|
|
|
|
public void checkSuccessfulDiscoRequest() throws Exception {
|
2017-12-13 23:10:11 +01:00
|
|
|
ThreadedDummyConnection con = getAuthenticatedDummyConnection();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:off
|
2018-05-09 23:06:12 +02:00
|
|
|
String reply =
|
2013-03-19 03:37:36 +01:00
|
|
|
"<iq type='result' id='qrzSp-16' to='test@myserver.com'>" +
|
|
|
|
"<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc' name='Pidgin'/>" +
|
|
|
|
"<feature var='urn:xmpp:ping'/>" +
|
|
|
|
"</query></iq>";
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:on
|
2017-12-13 23:10:11 +01:00
|
|
|
IQ discoReply = PacketParserUtils.parseStanza(reply);
|
2013-03-19 03:37:36 +01:00
|
|
|
con.addIQReply(discoReply);
|
|
|
|
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(con);
|
2015-02-14 17:15:02 +01:00
|
|
|
boolean pingSupported = pinger.isPingSupported(DUMMY_AT_EXAMPLE_ORG);
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
assertTrue(pingSupported);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-12-13 23:10:11 +01:00
|
|
|
public void checkUnsuccessfulDiscoRequest() throws Exception {
|
|
|
|
ThreadedDummyConnection con = getAuthenticatedDummyConnection();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:off
|
2018-05-09 23:06:12 +02:00
|
|
|
String reply =
|
2013-03-19 03:37:36 +01:00
|
|
|
"<iq type='result' id='qrzSp-16' to='test@myserver.com'>" +
|
|
|
|
"<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc' name='Pidgin'/>" +
|
|
|
|
"<feature var='urn:xmpp:noping'/>" +
|
|
|
|
"</query></iq>";
|
2017-11-20 08:53:19 +01:00
|
|
|
// @formatter:on
|
2017-12-13 23:10:11 +01:00
|
|
|
IQ discoReply = PacketParserUtils.parseStanza(reply);
|
2013-03-19 03:37:36 +01:00
|
|
|
con.addIQReply(discoReply);
|
|
|
|
|
2013-04-16 03:39:17 +02:00
|
|
|
PingManager pinger = PingManager.getInstanceFor(con);
|
2015-02-14 17:15:02 +01:00
|
|
|
boolean pingSupported = pinger.isPingSupported(DUMMY_AT_EXAMPLE_ORG);
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
assertFalse(pingSupported);
|
|
|
|
}
|
2014-09-29 15:56:50 +02:00
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private static ThreadedDummyConnection getAuthenticatedDummyConnection() throws SmackException, IOException, XMPPException, InterruptedException {
|
2014-09-29 15:56:50 +02:00
|
|
|
ThreadedDummyConnection connection = new ThreadedDummyConnection();
|
|
|
|
connection.connect();
|
2014-11-09 18:30:16 +01:00
|
|
|
connection.login();
|
2014-09-29 15:56:50 +02:00
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The returned connection won't send replies to IQs
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-09-29 15:56:50 +02:00
|
|
|
* @return
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws XMPPException if an XMPP protocol error was received.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws SmackException if Smack detected an exceptional situation.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-09-29 15:56:50 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
private static DummyConnection getAuthenticatedDummyConnectionWithoutIqReplies() throws SmackException, IOException, XMPPException, InterruptedException {
|
2014-09-29 15:56:50 +02:00
|
|
|
DummyConnection con = new DummyConnection();
|
2017-01-12 20:57:19 +01:00
|
|
|
con.setReplyTimeout(500);
|
2014-09-29 15:56:50 +02:00
|
|
|
con.connect();
|
2014-11-09 18:30:16 +01:00
|
|
|
con.login();
|
2014-09-29 15:56:50 +02:00
|
|
|
return con;
|
|
|
|
}
|
2013-01-04 12:43:35 +01:00
|
|
|
}
|