tests/auto/selftests/badxml/tst_badxml.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtCore>
       
    44 #include <QtTest/QtTest>
       
    45 
       
    46 /*
       
    47     This test makes a testlog containing lots of characters which have a special meaning in
       
    48     XML, with the purpose of exposing bugs in testlib's XML output code.
       
    49 */
       
    50 class tst_BadXml : public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 
       
    54 private slots:
       
    55     void badDataTag() const;
       
    56     void badDataTag_data() const;
       
    57 
       
    58     void badMessage() const;
       
    59     void badMessage_data() const;
       
    60 
       
    61     void failWithNoFile() const;
       
    62 
       
    63 public:
       
    64     static QList<QByteArray> const& badStrings();
       
    65 };
       
    66 
       
    67 /*
       
    68     Custom metaobject to make it possible to change class name at runtime.
       
    69 */
       
    70 class EmptyClass : public tst_BadXml
       
    71 { Q_OBJECT };
       
    72 
       
    73 class tst_BadXmlSub : public tst_BadXml
       
    74 {
       
    75 public:
       
    76     const QMetaObject* metaObject() const;
       
    77 
       
    78     static char const* className;
       
    79 };
       
    80 char const* tst_BadXmlSub::className = "tst_BadXml";
       
    81 
       
    82 const QMetaObject* tst_BadXmlSub::metaObject() const
       
    83 {
       
    84     const QMetaObject& empty = EmptyClass::staticMetaObject;
       
    85     static QMetaObject mo = {
       
    86         { empty.d.superdata, empty.d.stringdata, empty.d.data, empty.d.extradata }
       
    87     };
       
    88     static char currentClassName[1024];
       
    89     qstrcpy(currentClassName, className);
       
    90     int len = qstrlen(className);
       
    91     currentClassName[len] = 0;
       
    92     currentClassName[len+1] = 0;
       
    93 
       
    94     mo.d.stringdata = currentClassName;
       
    95 
       
    96     return &mo;
       
    97 }
       
    98 
       
    99 /*
       
   100     Outputs incidents and benchmark results with the current data tag set to a bad string.
       
   101 */
       
   102 void tst_BadXml::badDataTag() const
       
   103 {
       
   104     qDebug("a message");
       
   105 
       
   106     QBENCHMARK {
       
   107     }
       
   108 
       
   109     QFAIL("a failure");
       
   110 }
       
   111 
       
   112 void tst_BadXml::badDataTag_data() const
       
   113 {
       
   114     QTest::addColumn<int>("dummy");
       
   115 
       
   116     foreach (char const* str, badStrings()) {
       
   117         QTest::newRow(str) << 0;
       
   118     }
       
   119 }
       
   120 
       
   121 void tst_BadXml::failWithNoFile() const
       
   122 {
       
   123     QTest::qFail("failure message", 0, 0);
       
   124 }
       
   125 
       
   126 /*
       
   127     Outputs a message containing a bad string.
       
   128 */
       
   129 void tst_BadXml::badMessage() const
       
   130 {
       
   131     QFETCH(QByteArray, message);
       
   132     qDebug("%s", message.constData());
       
   133 }
       
   134 
       
   135 void tst_BadXml::badMessage_data() const
       
   136 {
       
   137     QTest::addColumn<QByteArray>("message");
       
   138 
       
   139     int i = 0;
       
   140     foreach (QByteArray const& str, badStrings()) {
       
   141         QTest::newRow(qPrintable(QString::fromLatin1("string %1").arg(i++))) << str;
       
   142     }
       
   143 }
       
   144 
       
   145 /*
       
   146     Returns a list of strings likely to expose bugs in XML output code.
       
   147 */
       
   148 QList<QByteArray> const& tst_BadXml::badStrings()
       
   149 {
       
   150     static QList<QByteArray> out;
       
   151     if (out.isEmpty()) {
       
   152         out << "end cdata ]]> text ]]> more text";
       
   153         out << "quotes \" text\" more text";
       
   154         out << "xml close > open < tags < text";
       
   155         out << "all > \" mixed ]]> up > \" in < the ]]> hopes < of triggering \"< ]]> bugs";
       
   156     }
       
   157     return out;
       
   158 }
       
   159 
       
   160 int main(int argc, char** argv)
       
   161 {
       
   162     QCoreApplication app(argc, argv);
       
   163 
       
   164     /*
       
   165         tst_selftests can't handle multiple XML documents in a single testrun, so we'll
       
   166         decide before we begin which of our "bad strings" we want to use for our testcase
       
   167         name.
       
   168     */
       
   169     int badstring = -1;
       
   170     QVector<char const*> args;
       
   171     for (int i = 0; i < argc; ++i) {
       
   172         if (!strcmp(argv[i], "-badstring")) {
       
   173             bool ok = false;
       
   174             if (i < argc-1) {
       
   175                 badstring = QByteArray(argv[i+1]).toInt(&ok);
       
   176                 ++i;
       
   177             }
       
   178             if (!ok) {
       
   179                 qFatal("Bad `-badstring' option");
       
   180             }
       
   181         }
       
   182         else {
       
   183             args << argv[i];
       
   184         }
       
   185     }
       
   186     /*
       
   187         We just want testlib to output a benchmark result, we don't actually care about the value,
       
   188         so just do one iteration to save time.
       
   189     */
       
   190     args << "-iterations" << "1";
       
   191 
       
   192     if (badstring == -1) {
       
   193         tst_BadXml test;
       
   194         return QTest::qExec(&test, args.count(), const_cast<char**>(args.data()));
       
   195     }
       
   196 
       
   197     QList<QByteArray> badstrings = tst_BadXml::badStrings();
       
   198     if (badstring >= badstrings.count())
       
   199         qFatal("`-badstring %d' is out of range", badstring);
       
   200 
       
   201     tst_BadXmlSub test;
       
   202     test.className = badstrings[badstring].constData();
       
   203     return QTest::qExec(&test, args.count(), const_cast<char**>(args.data()));
       
   204 }
       
   205 
       
   206 #include "tst_badxml.moc"