File indexing completed on 2024-04-21 14:59:34

0001 /*
0002     SPDX-FileCopyrightText: 2002, 2003 Leo Savernik <l.savernik@aon.at>
0003     SPDX-FileCopyrightText: 2012 Rolf Eike Beer <kde@opensource.sf-tec.de>
0004 */
0005 
0006 #ifndef TESTKIO
0007 #define TESTKIO
0008 #endif
0009 
0010 #include "dataprotocoltest.h"
0011 
0012 #include <QDebug>
0013 #include <QTest>
0014 
0015 QTEST_MAIN(DataProtocolTest)
0016 
0017 class TestWorker
0018 {
0019 public:
0020     TestWorker()
0021     {
0022     }
0023     virtual ~TestWorker()
0024     {
0025     }
0026 
0027     void mimeType(const QString &type)
0028     {
0029         QCOMPARE(type, mime_type_expected);
0030     }
0031 
0032     void totalSize(KIO::filesize_t /*bytes*/)
0033     {
0034         //    qDebug() << "content size: " << bytes << " bytes";
0035     }
0036 
0037     void setMetaData(const QString &key, const QString &value)
0038     {
0039         KIO::MetaData::Iterator it = attributes_expected.find(key);
0040         QVERIFY(it != attributes_expected.end());
0041         QCOMPARE(value, it.value());
0042         attributes_expected.erase(it);
0043     }
0044 
0045     void setAllMetaData(const KIO::MetaData &md)
0046     {
0047         KIO::MetaData::ConstIterator it = md.begin();
0048         KIO::MetaData::ConstIterator end = md.end();
0049 
0050         for (; it != end; ++it) {
0051             KIO::MetaData::Iterator eit = attributes_expected.find(it.key());
0052             QVERIFY(eit != attributes_expected.end());
0053             QCOMPARE(it.value(), eit.value());
0054             attributes_expected.erase(eit);
0055         }
0056     }
0057 
0058     void sendMetaData()
0059     {
0060         // check here if attributes_expected contains any excess keys
0061         KIO::MetaData::ConstIterator it = attributes_expected.constBegin();
0062         KIO::MetaData::ConstIterator end = attributes_expected.constEnd();
0063         for (; it != end; ++it) {
0064             qDebug() << "Metadata[\"" << it.key() << "\"] was expected but not defined";
0065         }
0066         QVERIFY(attributes_expected.isEmpty());
0067     }
0068 
0069     void data(const QByteArray &a)
0070     {
0071         if (a.isEmpty()) {
0072             //    qDebug() << "<no more data>";
0073         } else {
0074             QCOMPARE(a, content_expected);
0075         } /*end if*/
0076     }
0077 
0078     void dispatch_data(const QByteArray &a)
0079     {
0080         data(a);
0081     }
0082 
0083     void finished()
0084     {
0085     }
0086 
0087     void dispatch_finished()
0088     {
0089     }
0090 
0091     void ref()
0092     {
0093     }
0094     void deref()
0095     {
0096     }
0097 
0098 private:
0099     // -- testcase related members
0100     QString mime_type_expected; // expected MIME type
0101     /** contains all attributes and values the testcase has to set */
0102     KIO::MetaData attributes_expected;
0103     /** contains the content as it is expected to be returned */
0104     QByteArray content_expected;
0105 
0106 public:
0107     /**
0108      * sets the MIME type that this testcase is expected to return
0109      */
0110     void setExpectedMimeType(const QString &mime_type)
0111     {
0112         mime_type_expected = mime_type;
0113     }
0114 
0115     /**
0116      * sets all attribute-value pairs the testcase must deliver.
0117      */
0118     void setExpectedAttributes(const KIO::MetaData &attres)
0119     {
0120         attributes_expected = attres;
0121     }
0122 
0123     /**
0124      * sets content as expected to be delivered by the testcase.
0125      */
0126     void setExpectedContent(const QByteArray &content)
0127     {
0128         content_expected = content;
0129     }
0130 };
0131 
0132 #include "dataprotocol.cpp" // we need access to static data & functions
0133 
0134 void runTest(const QByteArray &mimetype, const QStringList &metalist, const QByteArray &content, const QUrl &url)
0135 {
0136     DataProtocol kio_data;
0137 
0138     kio_data.setExpectedMimeType(mimetype);
0139     MetaData exp_attrs;
0140     for (const QString &meta : metalist) {
0141         const QStringList metadata = meta.split(QLatin1Char('='));
0142         Q_ASSERT(metadata.count() == 2);
0143         exp_attrs[metadata[0]] = metadata[1];
0144     }
0145     kio_data.setExpectedAttributes(exp_attrs);
0146     kio_data.setExpectedContent(content);
0147 
0148     // check that mimetype(url) returns the same value as the complete parsing
0149     kio_data.mimetype(url);
0150 
0151     kio_data.get(url);
0152 }
0153 
0154 void DataProtocolTest::runAllTests()
0155 {
0156     QFETCH(QByteArray, expected_mime_type);
0157     QFETCH(QString, metadata);
0158     QFETCH(QByteArray, exp_content);
0159     QFETCH(QByteArray, url);
0160 
0161     const QStringList metalist = metadata.split(QLatin1Char('\n'));
0162 
0163     runTest(expected_mime_type, metalist, exp_content, QUrl(url));
0164 }
0165 
0166 void DataProtocolTest::runAllTests_data()
0167 {
0168     QTest::addColumn<QByteArray>("expected_mime_type");
0169     QTest::addColumn<QString>("metadata");
0170     QTest::addColumn<QByteArray>("exp_content");
0171     QTest::addColumn<QByteArray>("url");
0172 
0173     const QByteArray textplain = "text/plain";
0174     const QString usascii = QStringLiteral("charset=us-ascii");
0175     const QString csutf8 = QStringLiteral("charset=utf-8");
0176     const QString cslatin1 = QStringLiteral("charset=iso-8859-1");
0177     const QString csiso7 = QStringLiteral("charset=iso-8859-7");
0178 
0179     QTest::newRow("escape resolving") << textplain << usascii << QByteArray("blah blah") << QByteArray("data:,blah%20blah");
0180 
0181     QTest::newRow("MIME type, escape resolving") << QByteArray(
0182         "text/html") << usascii << QByteArray("<div style=\"border:thin orange solid;padding:1ex;background-color:yellow;color:black\">Rich <b>text</b></div>")
0183                                                  << QByteArray(
0184                                                         "data:text/html,<div%20style=\"border:thin%20orange%20solid;"
0185                                                         "padding:1ex;background-color:yellow;color:black\">Rich%20<b>text</b>"
0186                                                         "</div>");
0187 
0188     QTest::newRow("whitespace test I") << QByteArray("text/css") << QStringLiteral("charset=iso-8859-15")
0189                                        << QByteArray(" body { color: yellow; background:darkblue; font-weight:bold }")
0190                                        << QByteArray(
0191                                               "data:text/css  ;  charset =  iso-8859-15 , body { color: yellow; "
0192                                               "background:darkblue; font-weight:bold }");
0193 
0194     QTest::newRow("out of spec argument order, base64 decoding, whitespace test II")
0195         << textplain << QStringLiteral("charset=iso-8859-1") << QByteArray("paaaaaaaasd!!\n")
0196         << QByteArray("data: ;  base64 ; charset =  \"iso-8859-1\" ,cGFhYWFhYWFhc2QhIQo=");
0197 
0198     QTest::newRow("arbitrary keys, reserved names as keys, whitespace test III") << textplain
0199                                                                                  << QString::fromLatin1(
0200                                                                                         "base64=nospace\n"
0201                                                                                         "key=onespaceinner\n"
0202                                                                                         "key2=onespaceouter\n"
0203                                                                                         "charset=utf8\n"
0204                                                                                         "<<empty>>=")
0205                                                                                  << QByteArray("Die, Allied Schweinehund (C) 1990 Wolfenstein 3D")
0206                                                                                  << QByteArray(
0207                                                                                         "data: ;base64=nospace;key = onespaceinner; key2=onespaceouter ;"
0208                                                                                         " charset = utf8 ; <<empty>>= ,Die, Allied Schweinehund "
0209                                                                                         "(C) 1990 Wolfenstein 3D");
0210 
0211     QTest::newRow("string literal with escaped chars, testing delimiters within string")
0212         << textplain << QStringLiteral("fortune-cookie=Master Leep say: \"Rabbit is humble, Rabbit is gentle; follow the Rabbit\"\ncharset=us-ascii")
0213         << QByteArray("(C) 1997 Shadow Warrior ;-)")
0214         << QByteArray(
0215                "data:;fortune-cookie=\"Master Leep say: \\\"Rabbit is humble, "
0216                "Rabbit is gentle; follow the Rabbit\\\"\",(C) 1997 Shadow Warrior "
0217                ";-)");
0218 
0219     QTest::newRow("escaped charset") << textplain << QStringLiteral("charset=iso-8859-7") << QByteArray("test")
0220                                      << QByteArray("data:text/plain;charset=%22%5cis%5co%5c-%5c8%5c8%5c5%5c9%5c-%5c7%22,test");
0221 
0222     // the "greenbytes" tests are taken from http://greenbytes.de/tech/tc/datauri/
0223     QTest::newRow("greenbytes-simple") << textplain << usascii << QByteArray("test") << QByteArray("data:,test");
0224 
0225     QTest::newRow("greenbytes-simplewfrag") << textplain << usascii << QByteArray("test") << QByteArray("data:,test#foo");
0226 
0227     QTest::newRow("greenbytes-simple-utf8-dec") << textplain << csutf8 << QByteArray("test \xc2\xa3 pound sign")
0228                                                 << QByteArray("data:text/plain;charset=utf-8,test%20%c2%a3%20pound%20sign");
0229 
0230     QTest::newRow("greenbytes-simple-iso8859-1-dec") << textplain << cslatin1 << QByteArray("test \xc2\xa3 pound sign")
0231                                                      << QByteArray("data:text/plain;charset=iso-8859-1,test%20%a3%20pound%20sign");
0232 
0233     QTest::newRow("greenbytes-simple-iso8859-7-dec") << textplain << csiso7 << QByteArray("test \xce\xa3 sigma")
0234                                                      << QByteArray("data:text/plain;charset=iso-8859-7,test%20%d3%20sigma");
0235 
0236     QTest::newRow("greenbytes-simple-utf8-dec-dq") << textplain << csutf8 << QByteArray("test \xc2\xa3 pound sign")
0237                                                    << QByteArray("data:text/plain;charset=%22utf-8%22,test%20%c2%a3%20pound%20sign");
0238 
0239     QTest::newRow("greenbytes-simple-iso8859-1-dec-dq")
0240         << textplain << cslatin1 << QByteArray("test \xc2\xa3 pound sign") << QByteArray("data:text/plain;charset=%22iso-8859-1%22,test%20%a3%20pound%20sign");
0241 
0242     QTest::newRow("greenbytes-simple-iso8859-7-dec-dq")
0243         << textplain << csiso7 << QByteArray("test \xce\xa3 sigma") << QByteArray("data:text/plain;charset=%22iso-8859-7%22,test%20%d3%20sigma");
0244 
0245     QTest::newRow("greenbytes-simple-utf8-dec-dq-escaped") << textplain << csutf8 << QByteArray("test \xc2\xa3 pound sign")
0246                                                            << QByteArray("data:text/plain;charset=%22%5cu%5ct%5cf%5c-%5c8%22,test%20%c2%a3%20pound%20sign");
0247 
0248     QTest::newRow("greenbytes-simple-iso8859-1-dec-dq-escaped")
0249         << textplain << cslatin1 << QByteArray("test \xc2\xa3 pound sign")
0250         << QByteArray("data:text/plain;charset=%22%5ci%5cs%5co%5c-%5c8%5c8%5c5%5c9%5c-%5c1%22,test%20%a3%20pound%20sign");
0251 
0252     QTest::newRow("greenbytes-simple-iso8859-7-dec-dq-escaped")
0253         << textplain << csiso7 << QByteArray("test \xce\xa3 sigma")
0254         << QByteArray("data:text/plain;charset=%22%5ci%5cs%5co%5c-%5c8%5c8%5c5%5c9%5c-%5c7%22,test%20%d3%20sigma");
0255 
0256     QTest::newRow("greenbytes-simplefrag") << QByteArray("text/html") << usascii << QByteArray("<p>foo</p>")
0257                                            << QByteArray("data:text/html,%3Cp%3Efoo%3C%2Fp%3E#bar");
0258 
0259     QTest::newRow("greenbytes-svg") << QByteArray("image/svg+xml") << usascii
0260                                     << QByteArray(
0261                                            "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
0262                                            "  <circle cx=\"100\" cy=\"100\" r=\"25\" stroke=\"black\" stroke-width=\"1\" fill=\"green\"/>\n"
0263                                            "</svg>\n")
0264                                     << QByteArray(
0265                                            "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1"
0266                                            "%22%3E%0A%20%20%3Ccircle%20cx%3D%22100%22%20cy%3D%22100%22%20r%3D%2225%22%20stroke%3D%22black%22%20"
0267                                            "stroke-width%3D%221%22%20fill%3D%22green%22%2F%3E%0A%3C%2Fsvg%3E%0A#bar");
0268 
0269     QTest::newRow("greenbytes-ext-simple") << QByteArray("image/svg+xml") << QStringLiteral("foo=bar\ncharset=us-ascii")
0270                                            << QByteArray(
0271                                                   "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
0272                                                   "  <circle cx=\"100\" cy=\"100\" r=\"25\" stroke=\"black\" stroke-width=\"1\" fill=\"green\"/>\n"
0273                                                   "</svg>\n")
0274                                            << QByteArray(
0275                                                   "data:image/svg+xml;foo=bar,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1"
0276                                                   "%22%3E%0A%20%20%3Ccircle%20cx%3D%22100%22%20cy%3D%22100%22%20r%3D%2225%22%20stroke%3D%22black%22%20"
0277                                                   "stroke-width%3D%221%22%20fill%3D%22green%22%2F%3E%0A%3C%2Fsvg%3E%0A");
0278 
0279     QTest::newRow("greenbytes-ext-simple-qs")
0280         << QByteArray("image/svg+xml") << QStringLiteral("foo=bar,bar\ncharset=us-ascii")
0281         << QByteArray(
0282                "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
0283                "  <circle cx=\"100\" cy=\"100\" r=\"25\" stroke=\"black\" stroke-width=\"1\" fill=\"green\"/>\n"
0284                "</svg>\n")
0285         << QByteArray(
0286                "data:image/svg+xml;foo=%22bar,bar%22,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20"
0287                "version%3D%221.1%22%3E%0A%20%20%3Ccircle%20cx%3D%22100%22%20cy%3D%22100%22%20r%3D%2225%22%20stroke%3D%22black"
0288                "%22%20stroke-width%3D%221%22%20fill%3D%22green%22%2F%3E%0A%3C%2Fsvg%3E%0A");
0289 }
0290 
0291 #include "moc_dataprotocoltest.cpp"