// SPDX-FileCopyrightText: 2012 Jeremy Lainé // // SPDX-License-Identifier: LGPL-2.1-or-later #include "QXmppRpcIq.h" #include "util.h" #include static void checkVariant(const QVariant &value, const QByteArray &xml) { // serialise QBuffer buffer; buffer.open(QIODevice::ReadWrite); QXmlStreamWriter writer(&buffer); QXmppRpcMarshaller::marshall(&writer, value); qDebug() << "expect " << xml; qDebug() << "writing" << buffer.data(); QCOMPARE(buffer.data(), xml); // parse QDomDocument doc; QVERIFY(doc.setContent(xml, true)); QDomElement element = doc.documentElement(); QStringList errors; QVariant test = QXmppRpcMarshaller::demarshall(element, errors); if (!errors.isEmpty()) { qDebug() << errors; } QCOMPARE(errors, QStringList()); QCOMPARE(test, value); } class tst_QXmppRpcIq : public QObject { Q_OBJECT private: Q_SLOT void testBase64(); Q_SLOT void testBool(); Q_SLOT void testDateTime(); Q_SLOT void testDouble(); Q_SLOT void testInt(); Q_SLOT void testNil(); Q_SLOT void testString(); Q_SLOT void testArray(); Q_SLOT void testStruct(); Q_SLOT void testInvoke(); Q_SLOT void testResponse(); Q_SLOT void testResponseFault(); }; void tst_QXmppRpcIq::testBase64() { checkVariant(QByteArray("\0\1\2\3", 4), QByteArray("AAECAw==")); } void tst_QXmppRpcIq::testBool() { checkVariant(false, QByteArray("0")); checkVariant(true, QByteArray("1")); } void tst_QXmppRpcIq::testDateTime() { checkVariant(QDateTime(QDate(1998, 7, 17), QTime(14, 8, 55)), QByteArray("1998-07-17T14:08:55")); } void tst_QXmppRpcIq::testDouble() { checkVariant(double(-12.214), QByteArray("-12.214")); } void tst_QXmppRpcIq::testInt() { checkVariant(int(-12), QByteArray("-12")); } void tst_QXmppRpcIq::testNil() { checkVariant(QVariant(), QByteArray("")); } void tst_QXmppRpcIq::testString() { checkVariant(QString("hello world"), QByteArray("hello world")); } void tst_QXmppRpcIq::testArray() { checkVariant(QVariantList() << QString("hello world") << double(-12.214), QByteArray("" "hello world" "-12.214" "")); } void tst_QXmppRpcIq::testStruct() { QMap map; map["bar"] = QString("hello world"); map["foo"] = double(-12.214); checkVariant(map, QByteArray("" "" "bar" "hello world" "" "" "foo" "-12.214" "" "")); } void tst_QXmppRpcIq::testInvoke() { const QByteArray xml( "" "" "" "examples.getStateName" "" "" "6" "" "" "" "" ""); QXmppRpcInvokeIq iq; parsePacket(iq, xml); QCOMPARE(iq.method(), QLatin1String("examples.getStateName")); QCOMPARE(iq.arguments(), QVariantList() << int(6)); serializePacket(iq, xml); } void tst_QXmppRpcIq::testResponse() { const QByteArray xml( "" "" "" "" "" "Colorado" "" "" "" "" ""); QXmppRpcResponseIq iq; parsePacket(iq, xml); QCOMPARE(iq.faultCode(), 0); QCOMPARE(iq.faultString(), QString()); QCOMPARE(iq.values(), QVariantList() << QString("Colorado")); serializePacket(iq, xml); } void tst_QXmppRpcIq::testResponseFault() { const QByteArray xml( "" "" "" "" "" "" "" "faultCode" "404" "" "" "faultString" "Not found" "" "" "" "" "" "" ""); QXmppRpcResponseIq iq; parsePacket(iq, xml); QCOMPARE(iq.faultCode(), 404); QCOMPARE(iq.faultString(), QLatin1String("Not found")); QCOMPARE(iq.values(), QVariantList()); serializePacket(iq, xml); } QTEST_MAIN(tst_QXmppRpcIq) #include "tst_qxmpprpciq.moc"