File indexing completed on 2025-02-23 05:24:01

0001 /*
0002     SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "loadcategoriesjobtest.h"
0009 #include "jobs/loadcategoriesjob.h"
0010 #include "kdebugsettingsutil.h"
0011 
0012 #include <QDebug>
0013 #include <QFile>
0014 #include <QTest>
0015 
0016 LoadCategoriesJobTest::LoadCategoriesJobTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 LoadCategoriesJobTest::~LoadCategoriesJobTest() = default;
0022 
0023 void LoadCategoriesJobTest::shouldHaveDefaultValue()
0024 {
0025     LoadCategoriesJob job;
0026     QVERIFY(job.customCategories().isEmpty());
0027     QVERIFY(!job.foundOverrideRule());
0028     QVERIFY(job.qtKdeCategories().isEmpty());
0029 }
0030 
0031 void LoadCategoriesJobTest::shouldReadRules_data()
0032 {
0033     QTest::addColumn<QString>("filename");
0034     QTest::addColumn<QString>("filenamelistKdeLoggingCategories");
0035     QTest::addColumn<bool>("foundoverriderules");
0036     QTest::addColumn<LoggingCategory::List>("customcategories");
0037     QTest::addColumn<LoggingCategory::List>("qtkdecategories");
0038     QTest::newRow("empty") << QStringLiteral("emptyrulefiles.ini") << QString() << false << LoggingCategory::List() << LoggingCategory::List();
0039     QTest::newRow("commentedlines") << QStringLiteral("commentedrulefiles.ini") << QString() << false << LoggingCategory::List() << LoggingCategory::List();
0040     QTest::newRow("rulesbeforesection") << QStringLiteral("rulebeforerulessectionfiles.ini") << QString() << false << LoggingCategory::List()
0041                                         << LoggingCategory::List();
0042 
0043     const LoggingCategory ikwsInfo(QStringLiteral("KUriFilter IKWS (KIO)"), QStringLiteral("org.kde.kurifilter-ikws"), LoggingCategory::Info, QString(), true);
0044     const LoggingCategory shorturiInfo(QStringLiteral("KUriFilter Shorturi (KIO)"),
0045                                        QStringLiteral("org.kde.kurifilter-shorturi"),
0046                                        LoggingCategory::Info,
0047                                        QString(),
0048                                        true);
0049     const LoggingCategory bluezQtInfo(QStringLiteral("BluezQt"), QStringLiteral("BluezQt"), LoggingCategory::Info, QString(), true);
0050     const LoggingCategory kauthInfo(QStringLiteral("KAuth"), QStringLiteral("kf5.kauth"), LoggingCategory::Info, QString(), true);
0051 
0052     LoggingCategory::List qtKdeCategories;
0053     qtKdeCategories.append(
0054         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::Info, QString(), true));
0055     qtKdeCategories.append(ikwsInfo);
0056     qtKdeCategories.append(shorturiInfo);
0057     qtKdeCategories.append(bluezQtInfo);
0058     qtKdeCategories.append(kauthInfo);
0059 
0060     QTest::newRow("emptywithlistkdeloggingcategories")
0061         << QStringLiteral("rulebeforerulessectionfiles.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List() << qtKdeCategories;
0062 
0063     qtKdeCategories[0] =
0064         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::Off, QString(), false);
0065 
0066     QTest::newRow("oneelementoff") << QStringLiteral("oneelementoff.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0067                                    << qtKdeCategories;
0068 
0069     QTest::newRow("oneelementonelineoff") << QStringLiteral("oneelementonelineoff.ini") << QStringLiteral("correct.categories") << false
0070                                           << LoggingCategory::List() << qtKdeCategories;
0071 
0072     qtKdeCategories[0] =
0073         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::All, QString(), true);
0074 
0075     QTest::newRow("oneelementall") << QStringLiteral("oneelementall.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0076                                    << qtKdeCategories;
0077 
0078     QTest::newRow("oneelementonelineall") << QStringLiteral("oneelementonelineall.ini") << QStringLiteral("correct.categories") << false
0079                                           << LoggingCategory::List() << qtKdeCategories;
0080 
0081     qtKdeCategories[0] =
0082         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::Info, QString(), true);
0083 
0084     QTest::newRow("oneelementinfo") << QStringLiteral("oneelementinfo.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0085                                     << qtKdeCategories;
0086 
0087     QTest::newRow("oneelementinfo") << QStringLiteral("oneelementinfobis.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0088                                     << qtKdeCategories;
0089 
0090     qtKdeCategories[0] =
0091         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::Warning, QString(), true);
0092 
0093     QTest::newRow("oneelementwarning") << QStringLiteral("oneelementwarning.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0094                                        << qtKdeCategories;
0095 
0096     qtKdeCategories[0] =
0097         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::Critical, QString(), true);
0098 
0099     QTest::newRow("oneelementcritical") << QStringLiteral("oneelementcritical.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0100                                         << qtKdeCategories;
0101 
0102     QTest::newRow("oneelementonelinecritical") << QStringLiteral("oneelementonelinecritical.ini") << QStringLiteral("correct.categories") << false
0103                                                << LoggingCategory::List() << qtKdeCategories;
0104     QTest::newRow("oneelementtwolinecritical") << QStringLiteral("oneelementtwolinecritical.ini") << QStringLiteral("correct.categories") << false
0105                                                << LoggingCategory::List() << qtKdeCategories;
0106 
0107     qtKdeCategories[0] =
0108         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::Warning, QString(), true);
0109 
0110     QTest::newRow("oneelementdebug") << QStringLiteral("oneelementdebug.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0111                                      << qtKdeCategories;
0112     // Duplicate entries
0113     qtKdeCategories[0] =
0114         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::All, QString(), true);
0115     QTest::newRow("duplicates") << QStringLiteral("duplicates.ini") << QStringLiteral("correct.categories") << false << LoggingCategory::List()
0116                                 << qtKdeCategories;
0117 
0118     // Duplicate entries with duplicate categories
0119     qtKdeCategories[0] =
0120         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::All, QString(), true);
0121     QTest::newRow("duplicates") << QStringLiteral("duplicates.ini") << QStringLiteral("duplicates.categories") << false << LoggingCategory::List()
0122                                 << qtKdeCategories;
0123 
0124     // Test with extract elements
0125     LoggingCategory::List customCategories;
0126     LoggingCategory customTmp;
0127     customTmp.categoryName = QStringLiteral("toto");
0128     customTmp.loggingType = LoggingCategory::Critical;
0129     customTmp.enabled = true;
0130     customCategories.append(customTmp);
0131     qtKdeCategories[0] =
0132         LoggingCategory(QStringLiteral("KPasswdServer (KIO)"), QStringLiteral("org.kde.kio.kpasswdserver"), LoggingCategory::Warning, QString(), true);
0133 
0134     QTest::newRow("oneelementdebugandanextract") << QStringLiteral("oneelementdebugandanextract.ini") << QStringLiteral("correct.categories") << false
0135                                                  << customCategories << qtKdeCategories;
0136 
0137     // Test with extract elements
0138     customCategories.clear();
0139     customTmp.categoryName = QStringLiteral("toto");
0140     customTmp.loggingType = LoggingCategory::Critical;
0141     customTmp.enabled = false;
0142     customCategories.append(customTmp);
0143 
0144     QTest::newRow("oneelementdebugandanextractfalse") << QStringLiteral("oneelementdebugandanextractfalse.ini") << QStringLiteral("correct.categories") << false
0145                                                       << customCategories << qtKdeCategories;
0146 
0147     // Test without categories
0148     qtKdeCategories.clear();
0149     customCategories.clear();
0150     customTmp.categoryName = QStringLiteral("toto");
0151     customTmp.loggingType = LoggingCategory::Info;
0152     customTmp.enabled = false;
0153     customCategories.append(customTmp);
0154     customTmp.categoryName = QStringLiteral("toto");
0155     customTmp.loggingType = LoggingCategory::Warning;
0156     customTmp.enabled = false;
0157     customCategories.append(customTmp);
0158     customTmp.categoryName = QStringLiteral("toto");
0159     customTmp.loggingType = LoggingCategory::Debug;
0160     customTmp.enabled = false;
0161     customCategories.append(customTmp);
0162 
0163     customTmp.categoryName = QStringLiteral("toto");
0164     customTmp.loggingType = LoggingCategory::Critical;
0165     customTmp.enabled = false;
0166     customCategories.append(customTmp);
0167 
0168     QTest::newRow("testwithoutcategories") << QStringLiteral("testwithoutcategories.ini") << QString() << false << customCategories << qtKdeCategories;
0169 
0170     qtKdeCategories.clear();
0171     customCategories.clear();
0172     customTmp.categoryName = QStringLiteral("toto");
0173     customTmp.loggingType = LoggingCategory::Warning;
0174     customTmp.enabled = false;
0175     customCategories.append(customTmp);
0176     QTest::newRow("testwithoutcategorieswarning") << QStringLiteral("testwithoutcategorieswarning.ini") << QString() << false << customCategories
0177                                                   << qtKdeCategories;
0178 }
0179 
0180 void LoadCategoriesJobTest::shouldReadRules()
0181 {
0182     QFETCH(QString, filename);
0183     QFETCH(QString, filenamelistKdeLoggingCategories);
0184     QFETCH(bool, foundoverriderules);
0185     QFETCH(LoggingCategory::List, customcategories);
0186     QFETCH(LoggingCategory::List, qtkdecategories);
0187     LoadCategoriesJob job;
0188     KdeLoggingCategory::List listKdeLoggingCategories;
0189     if (!filenamelistKdeLoggingCategories.isEmpty()) {
0190         const QString kdeLoggingCategoriesPath = QString(QLatin1StringView(KDEBUGSETTINGS_DATA_DIR) + QLatin1Char('/') + filenamelistKdeLoggingCategories);
0191         KDebugSettingsUtil::readLoggingCategories(kdeLoggingCategoriesPath, listKdeLoggingCategories, true);
0192     }
0193 
0194     const QString path = QString(QLatin1StringView(KDEBUGSETTINGS_DATA_DIR) + QLatin1Char('/') + filename);
0195     QFile file(path);
0196     QVERIFY(file.exists());
0197     job.setFileName(path);
0198     job.setCategories(listKdeLoggingCategories);
0199     job.start();
0200 
0201     if (job.customCategories() != customcategories) {
0202         const auto catList = job.customCategories();
0203         for (const LoggingCategory &cat : catList) {
0204             qDebug() << "customcategories cat." << cat.description << " logname" << cat.categoryName << " enabled " << cat.enabled << "type "
0205                      << cat.loggingType;
0206         }
0207 
0208         qDebug() << "AFTER";
0209         for (const LoggingCategory &cat : std::as_const(customcategories)) {
0210             qDebug() << "customcategories cat." << cat.description << " logname" << cat.categoryName << " enabled " << cat.enabled << "type "
0211                      << cat.loggingType;
0212         }
0213     }
0214     QCOMPARE(job.customCategories().count(), customcategories.count());
0215     QCOMPARE(job.customCategories(), customcategories);
0216 
0217     QCOMPARE(job.foundOverrideRule(), foundoverriderules);
0218 
0219     if (job.qtKdeCategories() != qtkdecategories) {
0220         const auto catList = job.qtKdeCategories();
0221         for (const LoggingCategory &cat : catList) {
0222             qDebug() << "qtKdeCategories cat." << cat.description << " logname" << cat.categoryName << " enabled " << cat.enabled << "type " << cat.loggingType;
0223         }
0224 
0225         qDebug() << "AFTER";
0226         for (const LoggingCategory &cat : std::as_const(qtkdecategories)) {
0227             qDebug() << "qtKdeCategories cat." << cat.description << " logname" << cat.categoryName << " enabled " << cat.enabled << "type " << cat.loggingType;
0228         }
0229     }
0230     QCOMPARE(job.qtKdeCategories().count(), qtkdecategories.count());
0231     QCOMPARE(job.qtKdeCategories(), qtkdecategories);
0232 }
0233 
0234 QTEST_MAIN(LoadCategoriesJobTest)
0235 
0236 #include "moc_loadcategoriesjobtest.cpp"