File indexing completed on 2024-04-14 03:52:36

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