Commit fbe36af6 authored by Guus der Kinderen's avatar Guus der Kinderen

OF-159: Log all data, terminate only on exact response to IQ ping.

parent 6735b769
package org.jivesoftware.util; package org.jivesoftware.util;
import java.security.cert.Certificate; import org.apache.log4j.*;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import javax.xml.bind.DatatypeConverter;
import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.spi.LoggingEvent; import org.apache.log4j.spi.LoggingEvent;
import org.jivesoftware.openfire.SessionManager; import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.XMPPServer;
...@@ -29,9 +14,18 @@ import org.jivesoftware.util.cert.SANCertificateIdentityMapping; ...@@ -29,9 +14,18 @@ import org.jivesoftware.util.cert.SANCertificateIdentityMapping;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.IQ.Type; import org.xmpp.packet.IQ.Type;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
import javax.xml.bind.DatatypeConverter;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/** /**
* Runs server to server test. * Runs server to server test.
* *
...@@ -75,19 +69,24 @@ public class S2STestService { ...@@ -75,19 +69,24 @@ public class S2STestService {
outgoingServerSession.close(); outgoingServerSession.close();
} }
final IQ pingRequest = new IQ( Type.get );
pingRequest.setChildElement( "ping", IQPingHandler.NAMESPACE );
pingRequest.setFrom( XMPPServer.getInstance().getServerInfo().getXMPPDomain() );
pingRequest.setTo( domain );
// Intercept logging. // Intercept logging.
final StringBuilder logs = new StringBuilder(); final StringBuilder logs = new StringBuilder();
Appender appender = interceptLogging(logs); Appender appender = interceptLogging(logs);
// Intercept packets. // Intercept packets.
PacketInterceptor interceptor = new S2SInterceptor(); final PacketInterceptor interceptor = new S2SInterceptor( pingRequest );
InterceptorManager.getInstance().addInterceptor(interceptor); InterceptorManager.getInstance().addInterceptor(interceptor);
// Send ping. // Send ping.
try try
{ {
Log.info( "Sending server to server ping request to " + domain ); Log.info( "Sending server to server ping request to " + domain );
sendPing(); XMPPServer.getInstance().getIQRouter().route( pingRequest );
// Wait for success or exceed socket 5s timeout. // Wait for success or exceed socket 5s timeout.
waitUntil.tryAcquire( 6, TimeUnit.SECONDS ); waitUntil.tryAcquire( 6, TimeUnit.SECONDS );
...@@ -164,17 +163,6 @@ public class S2STestService { ...@@ -164,17 +163,6 @@ public class S2STestService {
} }
} }
/**
* Sends a server to server ping request.
*/
private void sendPing() {
final IQ pingRequest = new IQ(Type.get);
pingRequest.setChildElement("ping", IQPingHandler.NAMESPACE);
pingRequest.setFrom(XMPPServer.getInstance().getServerInfo().getXMPPDomain());
pingRequest.setTo(domain);
XMPPServer.getInstance().getIQRouter().route(pingRequest);
}
/** /**
* @return A String representation of the certificate chain for the connection to the domain under test. * @return A String representation of the certificate chain for the connection to the domain under test.
*/ */
...@@ -217,6 +205,16 @@ public class S2STestService { ...@@ -217,6 +205,16 @@ public class S2STestService {
private class S2SInterceptor implements PacketInterceptor { private class S2SInterceptor implements PacketInterceptor {
private StringBuilder xml = new StringBuilder(); private StringBuilder xml = new StringBuilder();
private final IQ ping;
/**
* @param ping The IQ ping request that was used to initiate the test.
*/
public S2SInterceptor( IQ ping )
{
this.ping = ping;
}
/** /**
* Keeps a log of the XMPP traffic, releasing the wait lock on response received. * Keeps a log of the XMPP traffic, releasing the wait lock on response received.
*/ */
...@@ -224,17 +222,23 @@ public class S2STestService { ...@@ -224,17 +222,23 @@ public class S2STestService {
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed)
throws PacketRejectedException { throws PacketRejectedException {
if (!processed if (!processed
&& (domain.equals(packet.getFrom().getDomain()) || domain.equals(packet.getTo().getDomain()))) { && (ping.getTo().getDomain().equals(packet.getFrom().getDomain()) || ping.getTo().getDomain().equals(packet.getTo().getDomain()))) {
// Log all traffic to and from the domain.
xml.append(packet.toXML()); xml.append(packet.toXML());
xml.append('\n'); xml.append('\n');
// If we've received our IQ response, stop waiting. // If we've received our IQ response, stop the test.
if (domain.equals(packet.getFrom().getDomain()) && "result".equals(packet.getElement().attributeValue("type"))) { if ( packet instanceof IQ )
{
final IQ iq = (IQ) packet;
if ( iq.isResponse() && ping.getID().equals( iq.getID() ) && ping.getTo().equals( iq.getFrom() ) ) {
Log.info("Successful server to server response received."); Log.info("Successful server to server response received.");
waitUntil.release(); waitUntil.release();
} }
} }
} }
}
/** /**
* Returns the received stanzas as a String. * Returns the received stanzas as a String.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment