/* * Copyright (C) 2008-2012 The QXmpp developers * * Author: * Jeremy Lainé * * Source: * http://code.google.com/p/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. * */ #include "QXmppRpcIq.h" #include "rpc.h" #include "tests.h" 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; QCOMPARE(doc.setContent(xml, true), true); QDomElement element = doc.documentElement(); QStringList errors; QVariant test = QXmppRpcMarshaller::demarshall(element, errors); if (!errors.isEmpty()) qDebug() << errors; QCOMPARE(errors, QStringList()); QCOMPARE(test, value); } void TestXmlRpc::testBase64() { checkVariant(QByteArray("\0\1\2\3", 4), QByteArray("AAECAw==")); } void TestXmlRpc::testBool() { checkVariant(false, QByteArray("0")); checkVariant(true, QByteArray("1")); } void TestXmlRpc::testDateTime() { checkVariant(QDateTime(QDate(1998, 7, 17), QTime(14, 8, 55)), QByteArray("1998-07-17T14:08:55")); } void TestXmlRpc::testDouble() { checkVariant(double(-12.214), QByteArray("-12.214")); } void TestXmlRpc::testInt() { checkVariant(int(-12), QByteArray("-12")); } void TestXmlRpc::testNil() { checkVariant(QVariant(), QByteArray("")); } void TestXmlRpc::testString() { checkVariant(QString("hello world"), QByteArray("hello world")); } void TestXmlRpc::testArray() { checkVariant(QVariantList() << QString("hello world") << double(-12.214), QByteArray("" "hello world" "-12.214" "")); } void TestXmlRpc::testStruct() { QMap map; map["bar"] = QString("hello world"); map["foo"] = double(-12.214); checkVariant(map, QByteArray("" "" "bar" "hello world" "" "" "foo" "-12.214" "" "")); } void TestXmlRpc::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 TestXmlRpc::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 TestXmlRpc::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); }