File indexing completed on 2025-02-16 04:55:52

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "xmlprintingscriptbuildertest.h"
0008 #include "parser.h"
0009 #include <KSieveCore/XMLPrintingScriptBuilder>
0010 #include <QProcess>
0011 #include <QTest>
0012 
0013 #ifndef Q_OS_WIN
0014 void initLocale()
0015 {
0016     setenv("LC_ALL", "en_US.utf-8", 1);
0017 }
0018 
0019 Q_CONSTRUCTOR_FUNCTION(initLocale)
0020 #endif
0021 
0022 QByteArray readSieveFile(const QString &sieveFile)
0023 {
0024     QFile file(sieveFile);
0025     file.open(QIODevice::ReadOnly);
0026     Q_ASSERT(file.isOpen());
0027     return file.readAll();
0028 }
0029 
0030 XMLPrintingScriptBuilderTest::XMLPrintingScriptBuilderTest(QObject *parent)
0031     : QObject(parent)
0032 {
0033 }
0034 
0035 void XMLPrintingScriptBuilderTest::shouldCreateXml_data()
0036 {
0037     QTest::addColumn<QString>("input");
0038     QTest::addColumn<bool>("success");
0039     QTest::newRow("emptyscript") << QStringLiteral("empty") << true;
0040     QTest::newRow("simplescript") << QStringLiteral("simple") << true;
0041     QTest::newRow("bodywithlist") << QStringLiteral("body") << true;
0042     QTest::newRow("add-flags") << QStringLiteral("add-flags") << true;
0043     QTest::newRow("test-virus") << QStringLiteral("test-virus") << true;
0044     QTest::newRow("replace") << QStringLiteral("replace") << true;
0045     QTest::newRow("test-envelop") << QStringLiteral("test-envelop") << true;
0046     QTest::newRow("test-servermetadataexists") << QStringLiteral("test-servermetadataexists") << true;
0047     QTest::newRow("test-foreach") << QStringLiteral("test-foreach") << true;
0048     QTest::newRow("test-vacation") << QStringLiteral("test-vacation") << true;
0049     QTest::newRow("test-forwarding") << QStringLiteral("test-forwarding") << true;
0050     QTest::newRow("test-elseif") << QStringLiteral("test-elseif") << true;
0051     QTest::newRow("test-address-order") << QStringLiteral("test-address-order") << true;
0052     QTest::newRow("test-regexp") << QStringLiteral("test-regexp") << true;
0053     QTest::newRow("test-envelop-without-part") << QStringLiteral("test-envelop-without-part") << true;
0054     QTest::newRow("test-anti-slash") << QStringLiteral("test-anti-slash") << true;
0055     QTest::newRow("test-current-date") << QStringLiteral("test-current-date") << true;
0056     QTest::newRow("not-condition") << QStringLiteral("not-condition") << true;
0057     QTest::newRow("delete-headers") << QStringLiteral("delete-headers") << true;
0058     QTest::newRow("mailboxexist") << QStringLiteral("mailboxexist") << true;
0059     QTest::newRow("extract-text") << QStringLiteral("extract-text") << true;
0060     QTest::newRow("enclose") << QStringLiteral("enclose") << true;
0061     QTest::newRow("add-single-flag") << QStringLiteral("add-single-flag") << true;
0062     QTest::newRow("fileinfo") << QStringLiteral("fileinfo") << true;
0063     QTest::newRow("include") << QStringLiteral("include") << true;
0064     QTest::newRow("variable-lower") << QStringLiteral("variable-lower") << true;
0065     QTest::newRow("test-environnement") << QStringLiteral("test-environnement") << true;
0066     QTest::newRow("test-has-flags") << QStringLiteral("test-has-flags") << true;
0067     QTest::newRow("test-spam-plus") << QStringLiteral("test-spam-plus") << true;
0068     QTest::newRow("remove-flags") << QStringLiteral("remove-flags") << true;
0069     QTest::newRow("variable") << QStringLiteral("variable") << true;
0070     QTest::newRow("break") << QStringLiteral("break") << true;
0071     QTest::newRow("notify") << QStringLiteral("notify") << true;
0072     QTest::newRow("test-exists") << QStringLiteral("test-exists") << true;
0073     QTest::newRow("vacation") << QStringLiteral("vacation") << true;
0074     QTest::newRow("test-metadataexists") << QStringLiteral("test-metadataexists") << true;
0075     QTest::newRow("test-date") << QStringLiteral("test-date") << true;
0076     QTest::newRow("redirect") << QStringLiteral("redirect") << true;
0077     QTest::newRow("convert-action") << QStringLiteral("convert-action") << true;
0078     QTest::newRow("address-with-default-all-parameter") << QStringLiteral("address-with-default-all-parameter") << true;
0079     QTest::newRow("not-condition2") << QStringLiteral("not-condition2") << true;
0080     QTest::newRow("address-with-default-is-parameter") << QStringLiteral("address-with-default-is-parameter") << true;
0081     QTest::newRow("test-spam") << QStringLiteral("test-spam") << true;
0082     QTest::newRow("reject") << QStringLiteral("reject") << true;
0083     QTest::newRow("keep") << QStringLiteral("keep") << true;
0084     QTest::newRow("test-headers") << QStringLiteral("test-headers") << true;
0085     QTest::newRow("vacation-active-discard") << QStringLiteral("vacation-active-discard") << true;
0086     QTest::newRow("vacation-multiple") << QStringLiteral("vacation-multiple") << true;
0087     QTest::newRow("vacation-multiple-with-undefined-command") << QStringLiteral("vacation-multiple-with-undefined-command") << true;
0088     QTest::newRow("full-example1") << QStringLiteral("full-example1") << true;
0089     QTest::newRow("regexp") << QStringLiteral("regexp") << true;
0090     QTest::newRow("comparator") << QStringLiteral("comparator") << true;
0091     QTest::newRow("test-comment") << QStringLiteral("test-comment") << true;
0092     QTest::newRow("hasflag") << QStringLiteral("hasflag") << true;
0093     QTest::newRow("setflag") << QStringLiteral("setflag") << true;
0094     QTest::newRow("add-header") << QStringLiteral("add-header") << true;
0095     QTest::newRow("problem-with-inferior-char") << QStringLiteral("problem-with-inferior-char") << true;
0096     QTest::newRow("delete-headers-with-index") << QStringLiteral("delete-headers-with-index") << true;
0097     QTest::newRow("notify-2") << QStringLiteral("notify-2") << true;
0098     QTest::newRow("foreverypart") << QStringLiteral("foreverypart") << true;
0099     QTest::newRow("test-foreverypart-complex") << QStringLiteral("test-foreverypart-complex") << true;
0100     QTest::newRow("test-envelop-subadress") << QStringLiteral("test-envelop-subadress") << true;
0101     QTest::newRow("test-current-date-custom") << QStringLiteral("test-current-date-custom") << true;
0102     QTest::newRow("failed-1") << QStringLiteral("failed-1") << true;
0103     QTest::newRow("failed-2") << QStringLiteral("failed-2") << true;
0104     QTest::newRow("failed-if-in-if") << QStringLiteral("failed-if-in-if") << true;
0105     QTest::newRow("test-comment2") << QStringLiteral("test-comment2") << true;
0106     QTest::newRow("test-comment3") << QStringLiteral("test-comment3") << true;
0107     QTest::newRow("test-comment4") << QStringLiteral("test-comment4") << true;
0108     QTest::newRow("test-value-as-list-quoted") << QStringLiteral("test-value-as-list-quoted") << true;
0109     QTest::newRow("test-header-value") << QStringLiteral("test-header-value") << true;
0110     QTest::newRow("test-comment5") << QStringLiteral("test-comment5") << true;
0111     QTest::newRow("test-comment6") << QStringLiteral("test-comment6") << true;
0112     QTest::newRow("test-comment7") << QStringLiteral("test-comment7") << true;
0113     QTest::newRow("test-comment8") << QStringLiteral("test-comment8") << true;
0114     QTest::newRow("test-comment9") << QStringLiteral("test-comment9") << true;
0115     QTest::newRow("test-date1") << QStringLiteral("test-date1") << true;
0116 }
0117 
0118 void XMLPrintingScriptBuilderTest::shouldCreateXml()
0119 {
0120     QFETCH(QString, input);
0121     QFETCH(bool, success);
0122 
0123     const QString originalFile = QLatin1StringView(XMLPRINTINGSCRIPTBUILDER_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral(".siv");
0124     const QString refFile = QLatin1StringView(XMLPRINTINGSCRIPTBUILDER_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral("-ref.siv");
0125     const QString generatedFile = QLatin1StringView(XMLPRINTINGSCRIPTBUILDER_GENERATED_DATA_DIR) + QLatin1Char('/') + input + QStringLiteral("-generated.siv");
0126     const QByteArray script = readSieveFile(originalFile);
0127     KSieve::Parser parser(script.begin(), script.begin() + script.length());
0128     KSieveCore::XMLPrintingScriptBuilder builder;
0129     parser.setScriptBuilder(&builder);
0130     const bool result = parser.parse();
0131     QCOMPARE(result, success);
0132     QString resultStr = builder.result();
0133 
0134     QFile f(generatedFile);
0135     QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
0136     f.write(resultStr.toUtf8());
0137     f.close();
0138 
0139     // compare to reference file
0140     QStringList args = QStringList() << QStringLiteral("-u") << refFile << generatedFile;
0141     QProcess proc;
0142     proc.setProcessChannelMode(QProcess::ForwardedChannels);
0143     proc.start(QStringLiteral("diff"), args);
0144     QVERIFY(proc.waitForFinished());
0145     QCOMPARE(proc.exitCode(), 0);
0146 }
0147 
0148 QTEST_MAIN(XMLPrintingScriptBuilderTest)
0149 
0150 #include "moc_xmlprintingscriptbuildertest.cpp"