aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2010-06-29 13:39:11 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2010-06-29 13:39:11 +0000
commit782b7f3e1199d867d7262ae3e6cf08aea9ad0624 (patch)
treebc17d571e833d752577d1e835d87224756d817e5 /source
parent7bc846598f17eea663b265e682a016acdac6beca (diff)
downloadqxmpp-782b7f3e1199d867d7262ae3e6cf08aea9ad0624.tar.gz
cleanup & test more IQ handling
Diffstat (limited to 'source')
-rw-r--r--source/QXmppBind.cpp11
-rw-r--r--source/QXmppBind.h2
-rw-r--r--source/QXmppIq.cpp7
-rw-r--r--source/QXmppIq.h1
-rw-r--r--source/QXmppSession.cpp5
-rw-r--r--source/QXmppSession.h2
-rw-r--r--source/QXmppStream.cpp84
-rw-r--r--source/QXmppStream.h2
-rw-r--r--source/tests.cpp72
-rw-r--r--source/tests.h4
10 files changed, 120 insertions, 70 deletions
diff --git a/source/QXmppBind.cpp b/source/QXmppBind.cpp
index 34f326d5..38bb2131 100644
--- a/source/QXmppBind.cpp
+++ b/source/QXmppBind.cpp
@@ -30,12 +30,11 @@
#include "QXmppUtils.h"
#include "QXmppConstants.h"
-QXmppBind::QXmppBind(QXmppIq::Type type)
- : QXmppIq(type)
+QXmppBind::QXmppBind()
{
}
-QXmppBind::QXmppBind(const QString& type)
+QXmppBind::QXmppBind(QXmppIq::Type type)
: QXmppIq(type)
{
}
@@ -80,8 +79,10 @@ void QXmppBind::toXmlElementFromChild(QXmlStreamWriter *writer) const
{
writer->writeStartElement("bind");
helperToXmlAddAttribute(writer, "xmlns", ns_bind);
- helperToXmlAddTextElement(writer, "jid", m_jid);
- helperToXmlAddTextElement(writer, "resource", m_resource);
+ if (!m_jid.isEmpty())
+ helperToXmlAddTextElement(writer, "jid", m_jid);
+ if (!m_resource.isEmpty())
+ helperToXmlAddTextElement(writer, "resource", m_resource);
writer->writeEndElement();
}
diff --git a/source/QXmppBind.h b/source/QXmppBind.h
index e621eb4f..ad938d9b 100644
--- a/source/QXmppBind.h
+++ b/source/QXmppBind.h
@@ -31,8 +31,8 @@
class QXmppBind : public QXmppIq
{
public:
+ QXmppBind();
QXmppBind(QXmppIq::Type type);
- QXmppBind(const QString& type);
QString jid() const;
QString resource() const;
diff --git a/source/QXmppIq.cpp b/source/QXmppIq.cpp
index c367fcba..55d72748 100644
--- a/source/QXmppIq.cpp
+++ b/source/QXmppIq.cpp
@@ -34,13 +34,6 @@ QXmppIq::QXmppIq(QXmppIq::Type type)
generateAndSetNextId();
}
-QXmppIq::QXmppIq(const QString& type)
- : QXmppStanza()
-{
- generateAndSetNextId();
- setTypeFromStr(type);
-}
-
QXmppIq::~QXmppIq()
{
diff --git a/source/QXmppIq.h b/source/QXmppIq.h
index d65d360e..0796ef3c 100644
--- a/source/QXmppIq.h
+++ b/source/QXmppIq.h
@@ -45,7 +45,6 @@ public:
};
QXmppIq(QXmppIq::Type type = QXmppIq::Get);
- QXmppIq(const QString& type);
~QXmppIq();
QXmppIq::Type type() const;
diff --git a/source/QXmppSession.cpp b/source/QXmppSession.cpp
index f80bc9c8..1acf4ec7 100644
--- a/source/QXmppSession.cpp
+++ b/source/QXmppSession.cpp
@@ -29,12 +29,11 @@
#include "QXmppConstants.h"
#include "QXmppUtils.h"
-QXmppSession::QXmppSession(QXmppIq::Type type)
- : QXmppIq(type)
+QXmppSession::QXmppSession()
{
}
-QXmppSession::QXmppSession(const QString& type)
+QXmppSession::QXmppSession(QXmppIq::Type type)
: QXmppIq(type)
{
}
diff --git a/source/QXmppSession.h b/source/QXmppSession.h
index c32202cf..f577b68b 100644
--- a/source/QXmppSession.h
+++ b/source/QXmppSession.h
@@ -31,8 +31,8 @@
class QXmppSession : public QXmppIq
{
public:
+ QXmppSession();
QXmppSession(QXmppIq::Type type);
- QXmppSession(const QString& type);
static bool isSession(const QDomElement &element);
diff --git a/source/QXmppStream.cpp b/source/QXmppStream.cpp
index 6898a51b..569fda91 100644
--- a/source/QXmppStream.cpp
+++ b/source/QXmppStream.cpp
@@ -463,25 +463,23 @@ void QXmppStream::parser(const QByteArray& data)
{
QDomElement element = nodeRecv.firstChildElement();
QString id = nodeRecv.attribute("id");
- QString to = nodeRecv.attribute("to");
- QString from = nodeRecv.attribute("from");
QString type = nodeRecv.attribute("type");
if(type.isEmpty())
warning("QXmppStream: iq type can't be empty");
- if( QXmppRpcInvokeIq::isRpcInvokeIq( nodeRecv ) )
+ if(QXmppRpcInvokeIq::isRpcInvokeIq(nodeRecv))
{
QXmppRpcInvokeIq rpcIqPacket;
rpcIqPacket.parse(nodeRecv);
emit rpcCallInvoke(rpcIqPacket);
}
- else if ( QXmppRpcResponseIq::isRpcResponseIq( nodeRecv ) )
+ else if(QXmppRpcResponseIq::isRpcResponseIq(nodeRecv))
{
QXmppRpcResponseIq rpcResponseIq;
rpcResponseIq.parse(nodeRecv);
- emit rpcCallResponse( rpcResponseIq );
+ emit rpcCallResponse(rpcResponseIq);
}
- else if ( QXmppRpcErrorIq::isRpcErrorIq( nodeRecv ) )
+ else if(QXmppRpcErrorIq::isRpcErrorIq(nodeRecv))
{
QXmppRpcErrorIq rpcErrorIq;
rpcErrorIq.parse(nodeRecv);
@@ -489,6 +487,9 @@ void QXmppStream::parser(const QByteArray& data)
}
else if(id == m_sessionId)
{
+ QXmppSession session;
+ session.parse(nodeRecv);
+
// get back add configuration whether to send
// roster and intial presence in beginning
// process SessionIq
@@ -496,23 +497,22 @@ void QXmppStream::parser(const QByteArray& data)
// xmpp connection made
emit xmppConnected();
- QXmppBind session(type);
- session.setId(id);
- session.setTo(to);
- session.setFrom(from);
emit iqReceived(session);
}
- else if(id == m_bindId)
+ else if(QXmppBind::isBind(nodeRecv) && id == m_bindId)
{
- QXmppBind bind(type);
- QString jid = nodeRecv.firstChildElement("bind").
- firstChildElement("jid").text();
- bind.setResource(jidToResource(jid));
- bind.setJid(jidToBareJid(jid));
- bind.setId(id);
- bind.setTo(to);
- bind.setFrom(from);
- processBindIq(bind);
+ QXmppBind bind;
+ bind.parse(nodeRecv);
+
+ // bind result
+ if (bind.type() == QXmppIq::Result)
+ {
+ QString resource = jidToResource(bind.jid());
+ if (!resource.isEmpty())
+ configuration().setResource(resource);
+ if (m_sessionAvailable)
+ sendSessionIQ();
+ }
emit iqReceived(bind);
}
else if(QXmppRosterIq::isRosterIq(nodeRecv))
@@ -536,9 +536,8 @@ void QXmppStream::parser(const QByteArray& data)
{
// respond to info query
QXmppInformationRequestResult qxmppFeatures;
- qxmppFeatures.setId(id);
- qxmppFeatures.setTo(from);
- qxmppFeatures.setFrom(to);
+ qxmppFeatures.setId(discoIq.id());
+ qxmppFeatures.setTo(discoIq.from());
sendPacket(qxmppFeatures);
} else {
emit discoveryIqReceived(discoIq);
@@ -651,8 +650,8 @@ void QXmppStream::parser(const QByteArray& data)
// respond to query
QXmppVersionIq responseIq;
responseIq.setType(QXmppIq::Result);
- responseIq.setId(id);
- responseIq.setTo(from);
+ responseIq.setId(versionIq.id());
+ responseIq.setTo(versionIq.from());
responseIq.setName(qApp->applicationName());
responseIq.setVersion(qApp->applicationVersion());
sendPacket(responseIq);
@@ -694,30 +693,34 @@ void QXmppStream::parser(const QByteArray& data)
// XEP-0199: XMPP Ping
else if(QXmppPingIq::isPingIq(nodeRecv))
{
+ QXmppPingIq req;
+ req.parse(nodeRecv);
+
QXmppIq iq(QXmppIq::Result);
- iq.setId(id);
- iq.setTo(from);
- iq.setFrom(to);
+ iq.setId(req.id());
+ iq.setTo(req.from());
+ iq.setFrom(req.to());
sendPacket(iq);
}
else
{
+ QXmppIq iqPacket;
+ iqPacket.parse(nodeRecv);
+
// if we didn't understant the iq, reply with error
// except for "result" and "error" iqs
if (type != "result" && type != "error")
{
QXmppIq iq(QXmppIq::Error);
- iq.setId(id);
- iq.setTo(from);
- iq.setFrom(to);
+ iq.setId(iqPacket.id());
+ iq.setTo(iqPacket.from());
+ iq.setFrom(iqPacket.to());
QXmppStanza::Error error(QXmppStanza::Error::Cancel,
QXmppStanza::Error::FeatureNotImplemented);
iq.setError(error);
sendPacket(iq);
}
- QXmppIq iqPacket;
- iqPacket.parse(nodeRecv);
emit iqReceived(iqPacket);
}
}
@@ -1002,21 +1005,6 @@ void QXmppStream::sendEndStream()
sendToServer(streamRootElementEnd);
}
-void QXmppStream::processBindIq(const QXmppBind& bind)
-{
- switch(bind.type())
- {
- case QXmppIq::Result:
- if(!bind.resource().isEmpty())
- configuration().setResource(bind.resource());
- if(m_sessionAvailable)
- sendSessionIQ();
- break;
- default:
- break;
- }
-}
-
void QXmppStream::pingStart()
{
const int interval = configuration().keepAliveInterval();
diff --git a/source/QXmppStream.h b/source/QXmppStream.h
index 8c548089..6b4a5f9f 100644
--- a/source/QXmppStream.h
+++ b/source/QXmppStream.h
@@ -177,8 +177,6 @@ private:
bool hasStartStreamElement(const QByteArray&);
bool hasEndStreamElement(const QByteArray&);
- void processBindIq(const QXmppBind&);
-
void flushDataBuffer();
};
diff --git a/source/tests.cpp b/source/tests.cpp
index 4a54a691..3d99bf99 100644
--- a/source/tests.cpp
+++ b/source/tests.cpp
@@ -28,8 +28,10 @@
#include <QVariant>
#include <QtTest/QtTest>
+#include "QXmppBind.h"
#include "QXmppMessage.h"
#include "QXmppPresence.h"
+#include "QXmppSession.h"
#include "tests.h"
static void parsePacket(QXmppPacket &packet, const QByteArray &xml)
@@ -51,6 +53,58 @@ static void serializePacket(QXmppPacket &packet, const QByteArray &xml)
QCOMPARE(buffer.data(), xml);
}
+void TestPackets::testBindNoResource()
+{
+ const QByteArray xml(
+ "<iq id=\"bind_1\" type=\"set\">"
+ "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/>"
+ "</iq>");
+
+ QXmppBind bind;
+ parsePacket(bind, xml);
+ QCOMPARE(bind.type(), QXmppIq::Set);
+ QCOMPARE(bind.id(), QString("bind_1"));
+ QCOMPARE(bind.jid(), QString());
+ QCOMPARE(bind.resource(), QString());
+ serializePacket(bind, xml);
+}
+
+void TestPackets::testBindResource()
+{
+ const QByteArray xml(
+ "<iq id=\"bind_2\" type=\"set\">"
+ "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">"
+ "<resource>someresource</resource>"
+ "</bind>"
+ "</iq>");
+
+ QXmppBind bind;
+ parsePacket(bind, xml);
+ QCOMPARE(bind.type(), QXmppIq::Set);
+ QCOMPARE(bind.id(), QString("bind_2"));
+ QCOMPARE(bind.jid(), QString());
+ QCOMPARE(bind.resource(), QString("someresource"));
+ serializePacket(bind, xml);
+}
+
+void TestPackets::testBindResult()
+{
+ const QByteArray xml(
+ "<iq id=\"bind_2\" type=\"result\">"
+ "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">"
+ "<jid>somenode@example.com/someresource</jid>"
+ "</bind>"
+ "</iq>");
+
+ QXmppBind bind;
+ parsePacket(bind, xml);
+ QCOMPARE(bind.type(), QXmppIq::Result);
+ QCOMPARE(bind.id(), QString("bind_2"));
+ QCOMPARE(bind.jid(), QString("somenode@example.com/someresource"));
+ QCOMPARE(bind.resource(), QString());
+ serializePacket(bind, xml);
+}
+
void TestPackets::testMessage()
{
const QByteArray xml(
@@ -130,8 +184,7 @@ void TestPackets::testPresenceFull()
"<show>away</show>"
"<status>In a meeting</status>"
"<priority>5</priority>"
- "</presence>"
- );
+ "</presence>");
QXmppPresence presence;
parsePacket(presence, xml);
@@ -143,6 +196,21 @@ void TestPackets::testPresenceFull()
serializePacket(presence, xml);
}
+void TestPackets::testSession()
+{
+ const QByteArray xml(
+ "<iq id=\"session_1\" to=\"example.com\" type=\"set\">"
+ "<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/>"
+ "</iq>");
+
+ QXmppSession session;
+ parsePacket(session, xml);
+ QCOMPARE(session.id(), QString("session_1"));
+ QCOMPARE(session.to(), QString("example.com"));
+ QCOMPARE(session.type(), QXmppIq::Set);
+ serializePacket(session, xml);
+}
+
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
diff --git a/source/tests.h b/source/tests.h
index 7d17e5bc..880682e0 100644
--- a/source/tests.h
+++ b/source/tests.h
@@ -28,11 +28,15 @@ class TestPackets : public QObject
Q_OBJECT
private slots:
+ void testBindNoResource();
+ void testBindResource();
+ void testBindResult();
void testMessage();
void testMessageFull();
void testMessageDelay();
void testMessageLegacyDelay();
void testPresence();
void testPresenceFull();
+ void testSession();
};