Commit 12eba2f8 authored by Guus der Kinderen's avatar Guus der Kinderen

OF-1087: Whitespace does not take into account element boundary.

The original code depends on a whitespace to be present within the limits
of the element. This is not safe (eg: <message><foo/></message> fails).
Instead, the closing characters ( > or /> ) of the first element should
be used, as that's guaranteed to be present.
parent 543ce079
......@@ -1154,7 +1154,7 @@ public class HttpSession extends LocalClientSession {
}
}
private class Deliverable {
static class Deliverable {
private final String text;
private final Collection<String> packets;
......@@ -1171,7 +1171,10 @@ public class HttpSession extends LocalClientSession {
if (Namespace.NO_NAMESPACE.equals(packet.getElement().getNamespace())) {
// use string-based operation here to avoid cascading xmlns wonkery
StringBuilder packetXml = new StringBuilder(packet.toXML());
packetXml.insert(packetXml.indexOf(" "), " xmlns=\"jabber:client\"");
final int noslash = packetXml.indexOf( ">" );
final int slash = packetXml.indexOf( "/>" );
final int insertAt = ( noslash - 1 == slash ? slash : noslash );
packetXml.insert( insertAt, " xmlns=\"jabber:client\"");
this.packets.add(packetXml.toString());
} else {
this.packets.add(packet.toXML());
......
package org.jivesoftware.openfire.http;
import org.dom4j.QName;
import org.junit.Test;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
/**
* Unit tests for {@link HttpSession.Deliverable}
*
* @author Guus der Kinderen, guus.der.kinderen@gmail.com
*/
public class HttpSessionDeliverable
{
/**
* Verifies that the default namespace is set on empty stanzas.
*
* @see <a href="https://igniterealtime.org/issues/browse/OF-1087">OF-1087</a>
*/
@Test
public void testNamespaceOnEmptyStanza() throws Exception
{
// Setup fixture
final Message message = new Message();
message.addChildElement( "unittest", "unit:test:namespace" );
final List<Packet> packets = new ArrayList<>();
packets.add( message );
// Execute system under test
final HttpSession.Deliverable deliverable = new HttpSession.Deliverable( packets );
final String result = deliverable.getDeliverable();
// verify results
// Note that this assertion depends on the Openfire XML parser-specific ordering of attributes.
assertEquals( "<message xmlns=\"jabber:client\"><unittest xmlns=\"unit:test:namespace\"/></message>", result );
}
/**
* Verifies that the default namespace is set on empty stanzas (that do not have a child element)
*
* @see <a href="https://igniterealtime.org/issues/browse/OF-1087">OF-1087</a>
*/
@Test
public void testNamespaceOnEmptyStanzaWithoutChildElement() throws Exception
{
// Setup fixture
final Message message = new Message();
final List<Packet> packets = new ArrayList<>();
packets.add( message );
// Execute system under test
final HttpSession.Deliverable deliverable = new HttpSession.Deliverable( packets );
final String result = deliverable.getDeliverable();
// verify results
// Note that this assertion depends on the Openfire XML parser-specific ordering of attributes.
assertEquals( "<message xmlns=\"jabber:client\"/>", result );
}
/**
* Verifies that the default namespace is set on (non-empty) stanzas.
*
* @see <a href="https://igniterealtime.org/issues/browse/OF-1087">OF-1087</a>
*/
@Test
public void testNamespaceOnStanza() throws Exception
{
// Setup fixture
final Message message = new Message();
message.setTo( "unittest@example.org/test" );
message.addChildElement( "unittest", "unit:test:namespace" );
final List<Packet> packets = new ArrayList<>();
packets.add( message );
// Execute system under test
final HttpSession.Deliverable deliverable = new HttpSession.Deliverable( packets );
final String result = deliverable.getDeliverable();
// verify results
// Note that this assertion depends on the Openfire XML parser-specific ordering of attributes.
assertEquals( "<message to=\"unittest@example.org/test\" xmlns=\"jabber:client\"><unittest xmlns=\"unit:test:namespace\"/></message>", result );
}
/**
* Verifies that the default namespace is not set on stanzas that already have defined a default namespace.
*
* @see <a href="https://igniterealtime.org/issues/browse/OF-1087">OF-1087</a>
*/
@Test
public void testNamespaceOnStanzaWithNamespace() throws Exception
{
// Setup fixture
final Message message = new Message();
message.getElement().setQName( QName.get( "message", "unit:test:preexisting:namespace" ) );
message.setTo( "unittest@example.org/test" );
message.addChildElement( "unittest", "unit:test:namespace" );
final List<Packet> packets = new ArrayList<>();
packets.add( message );
// Execute system under test
final HttpSession.Deliverable deliverable = new HttpSession.Deliverable( packets );
final String result = deliverable.getDeliverable();
// verify results
// Note that this assertion depends on the Openfire XML parser-specific ordering of attributes.
assertEquals( "<message xmlns=\"unit:test:preexisting:namespace\" to=\"unittest@example.org/test\"><unittest xmlns=\"unit:test:namespace\"/></message>", result );
}
}
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