2013-01-04 12:43:35 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2012 Florian Schmaus
|
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* 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;
|
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
import org.jivesoftware.smack.DummyConnection;
|
|
|
|
import org.jivesoftware.smack.TestUtils;
|
|
|
|
import org.jivesoftware.smack.ThreadedDummyConnection;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.ping.packet.Ping;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
|
|
|
import org.jivesoftware.smackx.packet.DiscoverInfo;
|
2013-03-23 12:59:08 +01:00
|
|
|
import org.junit.Before;
|
2013-01-04 12:43:35 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
public class PingTest {
|
|
|
|
private DummyConnection dummyCon;
|
|
|
|
private ThreadedDummyConnection threadedCon;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
dummyCon = new DummyConnection();
|
|
|
|
threadedCon = new ThreadedDummyConnection();
|
|
|
|
}
|
|
|
|
|
2013-01-04 12:43:35 +01:00
|
|
|
@Test
|
2013-03-19 03:37:36 +01:00
|
|
|
public void checkSendingPing() throws Exception {
|
2013-03-23 12:59:08 +01:00
|
|
|
dummyCon = new DummyConnection();
|
|
|
|
PingManager pinger = new PingManager(dummyCon);
|
2013-03-19 03:37:36 +01:00
|
|
|
pinger.ping("test@myserver.com");
|
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
Packet sentPacket = dummyCon.getSentPacket();
|
2013-03-19 03:37:36 +01:00
|
|
|
|
|
|
|
assertTrue(sentPacket instanceof Ping);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void checkSuccessfulPing() throws Exception {
|
2013-03-23 12:59:08 +01:00
|
|
|
threadedCon = new ThreadedDummyConnection();
|
2013-03-19 03:37:36 +01:00
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
PingManager pinger = new PingManager(threadedCon);
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
boolean pingSuccess = pinger.ping("test@myserver.com");
|
|
|
|
|
|
|
|
assertTrue(pingSuccess);
|
|
|
|
|
|
|
|
}
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
/**
|
|
|
|
* DummyConnection will not reply so it will timeout.
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void checkFailedPingOnTimeout() throws Exception {
|
2013-03-23 12:59:08 +01:00
|
|
|
dummyCon = new DummyConnection();
|
|
|
|
PingManager pinger = new PingManager(dummyCon);
|
2013-03-19 03:37:36 +01:00
|
|
|
|
|
|
|
boolean pingSuccess = pinger.ping("test@myserver.com");
|
|
|
|
|
|
|
|
assertFalse(pingSuccess);
|
|
|
|
|
2013-01-04 12:43:35 +01:00
|
|
|
}
|
2013-03-19 03:37:36 +01:00
|
|
|
|
|
|
|
/**
|
2013-03-23 12:59:08 +01:00
|
|
|
* Server returns an exception for entity.
|
2013-03-19 03:37:36 +01:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
@Test
|
2013-03-23 12:59:08 +01:00
|
|
|
public void checkFailedPingToEntityError() throws Exception {
|
|
|
|
threadedCon = new ThreadedDummyConnection();
|
2013-03-19 03:37:36 +01:00
|
|
|
//@formatter:off
|
|
|
|
String reply =
|
|
|
|
"<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'/>" +
|
|
|
|
"</error>" +
|
|
|
|
"</iq>";
|
|
|
|
//@formatter:on
|
2013-03-23 12:59:08 +01:00
|
|
|
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), threadedCon);
|
|
|
|
threadedCon.addIQReply(serviceUnavailable);
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
PingManager pinger = new PingManager(threadedCon);
|
2013-03-19 03:37:36 +01:00
|
|
|
|
|
|
|
boolean pingSuccess = pinger.ping("test@myserver.com");
|
|
|
|
|
|
|
|
assertFalse(pingSuccess);
|
2013-03-23 12:59:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void checkPingToServerSuccess() throws Exception {
|
|
|
|
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
|
|
|
PingManager pinger = new PingManager(con);
|
|
|
|
|
|
|
|
boolean pingSuccess = pinger.pingMyServer();
|
|
|
|
|
|
|
|
assertTrue(pingSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server returns an exception.
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void checkPingToServerError() throws Exception {
|
|
|
|
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
|
|
|
//@formatter:off
|
|
|
|
String reply =
|
|
|
|
"<iq type='error' id='qrzSp-16' to='test@myserver.com' from='" + con.getServiceName() + "'>" +
|
|
|
|
"<ping xmlns='urn:xmpp:ping'/>" +
|
|
|
|
"<error type='cancel'>" +
|
|
|
|
"<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
|
|
|
|
"</error>" +
|
|
|
|
"</iq>";
|
|
|
|
//@formatter:on
|
|
|
|
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
|
|
|
|
con.addIQReply(serviceUnavailable);
|
|
|
|
|
|
|
|
PingManager pinger = new PingManager(con);
|
|
|
|
|
|
|
|
boolean pingSuccess = pinger.pingMyServer();
|
2013-03-19 03:37:36 +01:00
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
assertTrue(pingSuccess);
|
2013-03-19 03:37:36 +01:00
|
|
|
}
|
|
|
|
|
2013-03-23 12:59:08 +01:00
|
|
|
@Test
|
|
|
|
public void checkPingToServerTimeout() throws Exception {
|
|
|
|
DummyConnection con = new DummyConnection();
|
|
|
|
PingManager pinger = new PingManager(con);
|
|
|
|
|
|
|
|
boolean pingSuccess = pinger.pingMyServer();
|
|
|
|
|
|
|
|
assertFalse(pingSuccess);
|
|
|
|
}
|
|
|
|
|
2013-03-19 03:37:36 +01:00
|
|
|
@Test
|
|
|
|
public void checkSuccessfulDiscoRequest() throws Exception {
|
|
|
|
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
|
|
|
DiscoverInfo info = new DiscoverInfo();
|
|
|
|
info.addFeature(Ping.NAMESPACE);
|
|
|
|
|
|
|
|
//@formatter:off
|
|
|
|
String reply =
|
|
|
|
"<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>";
|
|
|
|
//@formatter:on
|
|
|
|
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
|
|
|
|
con.addIQReply(discoReply);
|
|
|
|
|
|
|
|
PingManager pinger = new PingManager(con);
|
|
|
|
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
|
|
|
|
|
|
|
|
assertTrue(pingSupported);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void checkUnuccessfulDiscoRequest() throws Exception {
|
|
|
|
ThreadedDummyConnection con = new ThreadedDummyConnection();
|
|
|
|
DiscoverInfo info = new DiscoverInfo();
|
|
|
|
info.addFeature(Ping.NAMESPACE);
|
|
|
|
|
|
|
|
//@formatter:off
|
|
|
|
String reply =
|
|
|
|
"<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>";
|
|
|
|
//@formatter:on
|
|
|
|
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
|
|
|
|
con.addIQReply(discoReply);
|
|
|
|
|
|
|
|
PingManager pinger = new PingManager(con);
|
|
|
|
boolean pingSupported = pinger.isPingSupported("test@myserver.com");
|
|
|
|
|
|
|
|
assertFalse(pingSupported);
|
|
|
|
}
|
2013-01-04 12:43:35 +01:00
|
|
|
}
|