File indexing completed on 2024-04-28 04:44:09

0001 /**
0002  * Copyright (C)  2007  Brad Hards <bradh@frogmouth.net>
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright
0009  *   notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *   notice, this list of conditions and the following disclaimer in the
0012  *   documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #include <QtCrypto>
0027 #include <QtTest/QtTest>
0028 
0029 #ifdef QT_STATICPLUGIN
0030 #include "import_plugins.h"
0031 #endif
0032 
0033 class LoggerUnitTest : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 private Q_SLOTS:
0038     void initTestCase();
0039     void cleanupTestCase();
0040     void basicSetup();
0041     void logText1();
0042     void logText2();
0043     void logBlob();
0044     void logLevel();
0045 
0046 private:
0047     QCA::Initializer *m_init;
0048 };
0049 
0050 class NullLogger : public QCA::AbstractLogDevice
0051 {
0052     Q_OBJECT
0053 public:
0054     NullLogger()
0055         : QCA::AbstractLogDevice(QStringLiteral("null logger"))
0056     {
0057     }
0058 };
0059 
0060 class LastLogger : public QCA::AbstractLogDevice
0061 {
0062     Q_OBJECT
0063 public:
0064     LastLogger()
0065         : QCA::AbstractLogDevice(QStringLiteral("last logger"))
0066     {
0067     }
0068 
0069     void logTextMessage(const QString &message, enum QCA::Logger::Severity severity) override
0070     {
0071         m_lastMessage     = message;
0072         m_messageSeverity = severity;
0073     }
0074 
0075     QString lastMessage() const
0076     {
0077         return m_lastMessage;
0078     }
0079 
0080     void logBinaryMessage(const QByteArray &blob, enum QCA::Logger::Severity severity) override
0081     {
0082         m_lastBlob     = blob;
0083         m_blobSeverity = severity;
0084     }
0085 
0086     QByteArray lastBlob() const
0087     {
0088         return m_lastBlob;
0089     }
0090 
0091     QCA::Logger::Severity lastMessageSeverity() const
0092     {
0093         return m_messageSeverity;
0094     }
0095 
0096     QCA::Logger::Severity lastBlobSeverity() const
0097     {
0098         return m_blobSeverity;
0099     }
0100 
0101 private:
0102     QString               m_lastMessage;
0103     QByteArray            m_lastBlob;
0104     QCA::Logger::Severity m_messageSeverity;
0105     QCA::Logger::Severity m_blobSeverity;
0106 };
0107 
0108 void LoggerUnitTest::initTestCase()
0109 {
0110     m_init = new QCA::Initializer;
0111 }
0112 
0113 void LoggerUnitTest::cleanupTestCase()
0114 {
0115     QCA::unloadAllPlugins();
0116     delete m_init;
0117 }
0118 
0119 void LoggerUnitTest::basicSetup()
0120 {
0121     QCA::Logger *logSystem = QCA::logger();
0122 
0123     QCOMPARE(logSystem->currentLogDevices().count(), 0);
0124 
0125     logSystem->setLevel(QCA::Logger::Debug);
0126     QCOMPARE(logSystem->level(), QCA::Logger::Debug);
0127 
0128     NullLogger *nullLogger = new NullLogger;
0129 
0130     logSystem->registerLogDevice(nullLogger);
0131     QCOMPARE(logSystem->currentLogDevices().count(), 1);
0132     QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("null logger")));
0133     logSystem->unregisterLogDevice(QStringLiteral("null logger"));
0134     QCOMPARE(logSystem->currentLogDevices().count(), 0);
0135 
0136     delete nullLogger;
0137 }
0138 
0139 void LoggerUnitTest::logText1()
0140 {
0141     QCA::Logger *logSystem = QCA::logger();
0142 
0143     logSystem->logTextMessage(QStringLiteral("Sending with no recipients"));
0144 
0145     LastLogger *lastlogger = new LastLogger;
0146     logSystem->registerLogDevice(lastlogger);
0147     QCOMPARE(logSystem->currentLogDevices().count(), 1);
0148     QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
0149 
0150     logSystem->logTextMessage(QStringLiteral("Sending to system, checking for log device"));
0151     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for log device"));
0152     QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
0153 
0154     logSystem->logTextMessage(QStringLiteral("Sending at Error severity"), QCA::Logger::Error);
0155     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending at Error severity"));
0156     QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Error);
0157 
0158     LastLogger *lastlogger2 = new LastLogger;
0159     logSystem->registerLogDevice(lastlogger2);
0160     QCOMPARE(logSystem->currentLogDevices().count(), 2);
0161     QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
0162 
0163     logSystem->logTextMessage(QStringLiteral("Sending to system, checking for two log devices"));
0164     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
0165     QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
0166     QCOMPARE(lastlogger2->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
0167     QCOMPARE(lastlogger2->lastMessageSeverity(), QCA::Logger::Information);
0168 
0169     logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both
0170 
0171     QCOMPARE(logSystem->currentLogDevices().count(), 0);
0172 
0173     delete lastlogger;
0174     delete lastlogger2;
0175 }
0176 
0177 // same as above, but use convenience routine.
0178 void LoggerUnitTest::logText2()
0179 {
0180     QCA_logTextMessage(QStringLiteral("Sending with no recipients"), QCA::Logger::Notice);
0181 
0182     LastLogger *lastlogger = new LastLogger;
0183 
0184     QCA::Logger *logSystem = QCA::logger();
0185     logSystem->registerLogDevice(lastlogger);
0186     QCOMPARE(logSystem->currentLogDevices().count(), 1);
0187     QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
0188 
0189     QCA_logTextMessage(QStringLiteral("Sending to system, checking for log device"), QCA::Logger::Information);
0190     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for log device"));
0191     QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
0192 
0193     QCA_logTextMessage(QStringLiteral("Sending at Error severity"), QCA::Logger::Error);
0194     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending at Error severity"));
0195     QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Error);
0196 
0197     LastLogger *lastlogger2 = new LastLogger;
0198     logSystem->registerLogDevice(lastlogger2);
0199     QCOMPARE(logSystem->currentLogDevices().count(), 2);
0200     QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
0201 
0202     QCA_logTextMessage(QStringLiteral("Sending to system, checking for two log devices"), QCA::Logger::Information);
0203     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
0204     QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
0205     QCOMPARE(lastlogger2->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
0206     QCOMPARE(lastlogger2->lastMessageSeverity(), QCA::Logger::Information);
0207 
0208     logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both
0209 
0210     QCOMPARE(logSystem->currentLogDevices().count(), 0);
0211 
0212     delete lastlogger;
0213     delete lastlogger2;
0214 }
0215 
0216 void LoggerUnitTest::logBlob()
0217 {
0218     QCA::Logger *logSystem = QCA::logger();
0219 
0220     QCOMPARE(logSystem->currentLogDevices().count(), 0);
0221 
0222     QByteArray test("abcd\x34");
0223     logSystem->logBinaryMessage(test);
0224 
0225     LastLogger *lastlogger = new LastLogger;
0226     logSystem->registerLogDevice(lastlogger);
0227     QCOMPARE(logSystem->currentLogDevices().count(), 1);
0228     QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
0229 
0230     logSystem->logBinaryMessage(test);
0231     QCOMPARE(lastlogger->lastBlob(), test);
0232     QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Information);
0233 
0234     logSystem->logBinaryMessage(test, QCA::Logger::Critical);
0235     QCOMPARE(lastlogger->lastBlob(), test);
0236     QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Critical);
0237 
0238     LastLogger *lastlogger2 = new LastLogger;
0239     logSystem->registerLogDevice(lastlogger2);
0240     QCOMPARE(logSystem->currentLogDevices().count(), 2);
0241     QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
0242 
0243     test += test;
0244     logSystem->logBinaryMessage(test);
0245     QCOMPARE(lastlogger->lastBlob(), test);
0246     QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Information);
0247     QCOMPARE(lastlogger2->lastBlob(), test);
0248     QCOMPARE(lastlogger2->lastBlobSeverity(), QCA::Logger::Information);
0249 
0250     logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both
0251 
0252     QCOMPARE(logSystem->currentLogDevices().count(), 0);
0253     delete lastlogger;
0254     delete lastlogger2;
0255 }
0256 
0257 void LoggerUnitTest::logLevel()
0258 {
0259     QCA::Logger *logSystem = QCA::logger();
0260 
0261     LastLogger *lastlogger = new LastLogger;
0262     logSystem->registerLogDevice(lastlogger);
0263 
0264     logSystem->setLevel(QCA::Logger::Error);
0265     QCOMPARE(logSystem->level(), QCA::Logger::Error);
0266 
0267     QCA_logTextMessage(QStringLiteral("Sending to system, checking that it is filtered out"), QCA::Logger::Information);
0268     QEXPECT_FAIL("", "Should fail", Continue);
0269     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking that it is filtered out"));
0270 
0271     QCA_logTextMessage(QStringLiteral("Sending to system, checking that it is not filtered out"), QCA::Logger::Error);
0272     QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking that it is not filtered out"));
0273 
0274     logSystem->setLevel(QCA::Logger::Debug);
0275 
0276     delete lastlogger;
0277 }
0278 
0279 QTEST_MAIN(LoggerUnitTest)
0280 
0281 #include "loggerunittest.moc"