File indexing completed on 2024-11-10 04:50:01
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only 0005 */ 0006 0007 #include "filterlogtest.h" 0008 #include "filter/filterlog.h" 0009 #include <QStandardPaths> 0010 #include <QTest> 0011 0012 QTEST_MAIN(FilterLogTest) 0013 FilterLogTest::FilterLogTest(QObject *parent) 0014 : QObject{parent} 0015 { 0016 QStandardPaths::setTestModeEnabled(true); 0017 } 0018 0019 void FilterLogTest::cleanup() 0020 { 0021 auto log = MailCommon::FilterLog::instance(); 0022 log->clear(); 0023 log->setLogging(false); 0024 log->setContentTypeEnabled(MailCommon::FilterLog::Meta, true); 0025 log->setContentTypeEnabled(MailCommon::FilterLog::PatternDescription, true); 0026 log->setContentTypeEnabled(MailCommon::FilterLog::RuleResult, true); 0027 log->setContentTypeEnabled(MailCommon::FilterLog::PatternResult, true); 0028 log->setContentTypeEnabled(MailCommon::FilterLog::AppliedAction, true); 0029 } 0030 0031 void FilterLogTest::shouldHaveDefaultValues() 0032 { 0033 auto log = MailCommon::FilterLog::instance(); 0034 QVERIFY(!log->isLogging()); 0035 constexpr long size = 512 * 1024; 0036 QCOMPARE(log->maxLogSize(), size); 0037 QVERIFY(log->logEntries().isEmpty()); 0038 QVERIFY(log->isContentTypeEnabled(MailCommon::FilterLog::Meta)); 0039 QVERIFY(log->isContentTypeEnabled(MailCommon::FilterLog::PatternDescription)); 0040 QVERIFY(log->isContentTypeEnabled(MailCommon::FilterLog::RuleResult)); 0041 QVERIFY(log->isContentTypeEnabled(MailCommon::FilterLog::PatternResult)); 0042 QVERIFY(log->isContentTypeEnabled(MailCommon::FilterLog::AppliedAction)); 0043 } 0044 0045 void FilterLogTest::shouldNotAddLogEntries() 0046 { 0047 auto log = MailCommon::FilterLog::instance(); 0048 log->setLogging(false); 0049 log->add(QStringLiteral("foo"), MailCommon::FilterLog::AppliedAction); 0050 QVERIFY(log->logEntries().isEmpty()); 0051 0052 log->setLogging(true); 0053 log->add(QStringLiteral("foo"), MailCommon::FilterLog::AppliedAction); 0054 QCOMPARE(log->logEntries().size(), 1); 0055 } 0056 0057 void FilterLogTest::shouldAddLogEntries() 0058 { 0059 auto log = MailCommon::FilterLog::instance(); 0060 log->setLogging(true); 0061 log->add(QStringLiteral("foo"), MailCommon::FilterLog::AppliedAction); 0062 QCOMPARE(log->logEntries().size(), 1); 0063 0064 log->add(QStringLiteral("foo"), MailCommon::FilterLog::AppliedAction); 0065 QCOMPARE(log->logEntries().size(), 2); 0066 log->add(QStringLiteral("foo"), MailCommon::FilterLog::AppliedAction); 0067 QCOMPARE(log->logEntries().size(), 3); 0068 } 0069 0070 void FilterLogTest::shouldAllowSpecificLogEntries() 0071 { 0072 auto log = MailCommon::FilterLog::instance(); 0073 log->setLogging(true); 0074 log->setContentTypeEnabled(MailCommon::FilterLog::Meta, true); 0075 log->setContentTypeEnabled(MailCommon::FilterLog::PatternDescription, false); 0076 log->setContentTypeEnabled(MailCommon::FilterLog::RuleResult, false); 0077 log->setContentTypeEnabled(MailCommon::FilterLog::PatternResult, false); 0078 log->setContentTypeEnabled(MailCommon::FilterLog::AppliedAction, false); 0079 log->add(QStringLiteral("foo"), MailCommon::FilterLog::AppliedAction); 0080 QCOMPARE(log->logEntries().size(), 0); 0081 0082 log->add(QStringLiteral("foo"), MailCommon::FilterLog::Meta); 0083 QCOMPARE(log->logEntries().size(), 1); 0084 0085 log->add(QStringLiteral("foo"), MailCommon::FilterLog::PatternDescription); 0086 QCOMPARE(log->logEntries().size(), 1); 0087 0088 log->add(QStringLiteral("foo"), MailCommon::FilterLog::RuleResult); 0089 QCOMPARE(log->logEntries().size(), 1); 0090 0091 log->add(QStringLiteral("foo"), MailCommon::FilterLog::PatternResult); 0092 QCOMPARE(log->logEntries().size(), 1); 0093 0094 log->add(QStringLiteral("foo"), MailCommon::FilterLog::AppliedAction); 0095 QCOMPARE(log->logEntries().size(), 1); 0096 0097 log->add(QStringLiteral("foo"), MailCommon::FilterLog::Meta); 0098 QCOMPARE(log->logEntries().size(), 2); 0099 0100 log->setContentTypeEnabled(MailCommon::FilterLog::PatternResult, true); 0101 log->add(QStringLiteral("foo"), MailCommon::FilterLog::Meta); 0102 QCOMPARE(log->logEntries().size(), 3); 0103 0104 log->add(QStringLiteral("foo"), MailCommon::FilterLog::PatternResult); 0105 QCOMPARE(log->logEntries().size(), 4); 0106 } 0107 0108 void FilterLogTest::shouldClearLog() 0109 { 0110 auto log = MailCommon::FilterLog::instance(); 0111 log->setLogging(true); 0112 log->add(QStringLiteral("foo"), MailCommon::FilterLog::Meta); 0113 QCOMPARE(log->logEntries().size(), 1); 0114 0115 log->clear(); 0116 QCOMPARE(log->logEntries().size(), 0); 0117 } 0118 0119 #include "moc_filterlogtest.cpp"