Skip to content

Commit

Permalink
Add extra unit test for QXmppStream
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed Oct 2, 2020
1 parent 06d0583 commit 4e58dc9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/base/QXmppStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ void QXmppStream::_q_socketError(QAbstractSocket::SocketError socketError)
}

void QXmppStream::_q_socketReadyRead()
{
processData(QString::fromUtf8(d->socket->readAll()));
}

void QXmppStream::processData(const QString &data)
{
// As we may only have partial XML content, we need to cache the received
// data until it has been successfully parsed. In case it can't be parsed,
Expand All @@ -226,7 +231,7 @@ void QXmppStream::_q_socketReadyRead()
// However, both issues could only be solved using an XML stream reader
// which would cause many other problems since we don't actually use it for
// parsing the content.
d->dataBuffer.append(QString::fromUtf8(d->socket->readAll()));
d->dataBuffer.append(data);

//
// Check for whitespace pings
Expand Down
5 changes: 5 additions & 0 deletions src/base/QXmppStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ private Q_SLOTS:
void _q_socketReadyRead();

private:
#ifdef QXMPP_TEST
public:
#endif
void processData(const QString &data);

QXmppStreamPrivate *const d;
};

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ add_simple_test(qxmppsessioniq)
add_simple_test(qxmppsocks)
add_simple_test(qxmppstanza)
add_simple_test(qxmppstarttlspacket)
add_simple_test(qxmppstream)
add_simple_test(qxmppstreamfeatures)
add_simple_test(qxmppstunmessage)
add_simple_test(qxmppvcardiq)
Expand Down
Empty file added tests/qxmppstream/cd
Empty file.
143 changes: 143 additions & 0 deletions tests/qxmppstream/tst_qxmppstream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright (C) 2008-2020 The QXmpp developers
*
* Authors:
* Linus Jahn
*
* Source:
* https://github.com/qxmpp-project/qxmpp
*
* This file is a part of QXmpp library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/

#define QXMPP_TEST

#include "QXmppStream.h"

#include "util.h"

Q_DECLARE_METATYPE(QDomElement)

class TestStream : public QXmppStream
{
Q_OBJECT

public:
TestStream(QObject *parent)
: QXmppStream(parent)
{
}

void handleStart() override
{
QXmppStream::handleStart();
emit started();
}

void handleStream(const QDomElement &element) override
{
emit streamReceived(element);
}

void handleStanza(const QDomElement &element) override
{
emit stanzaReceived(element);
}

Q_SIGNAL void started();
Q_SIGNAL void streamReceived(const QDomElement &element);
Q_SIGNAL void stanzaReceived(const QDomElement &element);
};

class tst_QXmppStream : public QObject
{
Q_OBJECT

private:
Q_SLOT void initTestCase();
Q_SLOT void testProcessData();
};

void tst_QXmppStream::initTestCase()
{
qRegisterMetaType<QDomElement>();
}

void tst_QXmppStream::testProcessData()
{
TestStream stream(this);

QSignalSpy onStarted(&stream, &TestStream::started);
QSignalSpy onStreamReceived(&stream, &TestStream::streamReceived);
QSignalSpy onStanzaReceived(&stream, &TestStream::stanzaReceived);

stream.processData(R"(<?xml version="1.0" encoding="UTF-8"?>)");
stream.processData(R"(
<stream:stream from='juliet@im.example.com'
to='im.example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>)");

// check stream was found
QCOMPARE(onStreamReceived.size(), 1);
QCOMPARE(onStanzaReceived.size(), 0);
QCOMPARE(onStarted.size(), 0);

// check stream information
const auto streamElement = onStreamReceived[0][0].value<QDomElement>();
QCOMPARE(streamElement.tagName(), QStringLiteral("stream"));
QCOMPARE(streamElement.namespaceURI(), QStringLiteral("http://etherx.jabber.org/streams"));
QCOMPARE(streamElement.attribute("from"), QStringLiteral("juliet@im.example.com"));
QCOMPARE(streamElement.attribute("to"), QStringLiteral("im.example.com"));
QCOMPARE(streamElement.attribute("version"), QStringLiteral("1.0"));
QCOMPARE(streamElement.attribute("lang"), QStringLiteral("en"));

stream.processData(R"(
<stream:features>
<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>
<required/>
</starttls>
</stream:features>)");

QCOMPARE(onStreamReceived.size(), 1);
QCOMPARE(onStanzaReceived.size(), 1);
QCOMPARE(onStarted.size(), 0);

const auto features = onStanzaReceived[0][0].value<QDomElement>();
QCOMPARE(features.tagName(), QStringLiteral("features"));
QCOMPARE(features.namespaceURI(), QStringLiteral("http://etherx.jabber.org/streams"));

// test partial data
stream.processData(R"(<message from="juliet@im.example.co)");
QCOMPARE(onStreamReceived.size(), 1);
QCOMPARE(onStanzaReceived.size(), 1);
QCOMPARE(onStarted.size(), 0);
stream.processData(R"(m" to="stpeter@im.example.com">)");
stream.processData(R"(<body>Moin</body>)");
stream.processData(R"(</message>)");
QCOMPARE(onStreamReceived.size(), 1);
QCOMPARE(onStanzaReceived.size(), 2);
QCOMPARE(onStarted.size(), 0);

const auto message = onStanzaReceived[1][0].value<QDomElement>();
QCOMPARE(message.tagName(), QStringLiteral("message"));
QCOMPARE(message.namespaceURI(), QStringLiteral("jabber:client"));

stream.processData(R"(</stream:stream>)");
}

QTEST_MAIN(tst_QXmppStream)
#include "tst_qxmppstream.moc"

0 comments on commit 4e58dc9

Please sign in to comment.