1/**
2 * Copyright (C) 2007 Brad Hards <bradh@frogmouth.net>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <QtCrypto>
27#include <QtTest/QtTest>
28
29#ifdef QT_STATICPLUGIN
30#include "import_plugins.h"
31#endif
32
33class LoggerUnitTest : public QObject
34{
35 Q_OBJECT
36
37private Q_SLOTS:
38 void initTestCase();
39 void cleanupTestCase();
40 void basicSetup();
41 void logText1();
42 void logText2();
43 void logBlob();
44 void logLevel();
45
46private:
47 QCA::Initializer *m_init;
48};
49
50class NullLogger : public QCA::AbstractLogDevice
51{
52 Q_OBJECT
53public:
54 NullLogger()
55 : QCA::AbstractLogDevice(QStringLiteral("null logger"))
56 {
57 }
58};
59
60class LastLogger : public QCA::AbstractLogDevice
61{
62 Q_OBJECT
63public:
64 LastLogger()
65 : QCA::AbstractLogDevice(QStringLiteral("last logger"))
66 {
67 }
68
69 void logTextMessage(const QString &message, enum QCA::Logger::Severity severity) override
70 {
71 m_lastMessage = message;
72 m_messageSeverity = severity;
73 }
74
75 QString lastMessage() const
76 {
77 return m_lastMessage;
78 }
79
80 void logBinaryMessage(const QByteArray &blob, enum QCA::Logger::Severity severity) override
81 {
82 m_lastBlob = blob;
83 m_blobSeverity = severity;
84 }
85
86 QByteArray lastBlob() const
87 {
88 return m_lastBlob;
89 }
90
91 QCA::Logger::Severity lastMessageSeverity() const
92 {
93 return m_messageSeverity;
94 }
95
96 QCA::Logger::Severity lastBlobSeverity() const
97 {
98 return m_blobSeverity;
99 }
100
101private:
102 QString m_lastMessage;
103 QByteArray m_lastBlob;
104 QCA::Logger::Severity m_messageSeverity;
105 QCA::Logger::Severity m_blobSeverity;
106};
107
108void LoggerUnitTest::initTestCase()
109{
110 m_init = new QCA::Initializer;
111}
112
113void LoggerUnitTest::cleanupTestCase()
114{
115 QCA::unloadAllPlugins();
116 delete m_init;
117}
118
119void LoggerUnitTest::basicSetup()
120{
121 QCA::Logger *logSystem = QCA::logger();
122
123 QCOMPARE(logSystem->currentLogDevices().count(), 0);
124
125 logSystem->setLevel(QCA::Logger::Debug);
126 QCOMPARE(logSystem->level(), QCA::Logger::Debug);
127
128 NullLogger *nullLogger = new NullLogger;
129
130 logSystem->registerLogDevice(logger: nullLogger);
131 QCOMPARE(logSystem->currentLogDevices().count(), 1);
132 QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("null logger")));
133 logSystem->unregisterLogDevice(QStringLiteral("null logger"));
134 QCOMPARE(logSystem->currentLogDevices().count(), 0);
135
136 delete nullLogger;
137}
138
139void LoggerUnitTest::logText1()
140{
141 QCA::Logger *logSystem = QCA::logger();
142
143 logSystem->logTextMessage(QStringLiteral("Sending with no recipients"));
144
145 LastLogger *lastlogger = new LastLogger;
146 logSystem->registerLogDevice(logger: lastlogger);
147 QCOMPARE(logSystem->currentLogDevices().count(), 1);
148 QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
149
150 logSystem->logTextMessage(QStringLiteral("Sending to system, checking for log device"));
151 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for log device"));
152 QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
153
154 logSystem->logTextMessage(QStringLiteral("Sending at Error severity"), QCA::Logger::Error);
155 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending at Error severity"));
156 QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Error);
157
158 LastLogger *lastlogger2 = new LastLogger;
159 logSystem->registerLogDevice(logger: lastlogger2);
160 QCOMPARE(logSystem->currentLogDevices().count(), 2);
161 QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
162
163 logSystem->logTextMessage(QStringLiteral("Sending to system, checking for two log devices"));
164 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
165 QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
166 QCOMPARE(lastlogger2->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
167 QCOMPARE(lastlogger2->lastMessageSeverity(), QCA::Logger::Information);
168
169 logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both
170
171 QCOMPARE(logSystem->currentLogDevices().count(), 0);
172
173 delete lastlogger;
174 delete lastlogger2;
175}
176
177// same as above, but use convenience routine.
178void LoggerUnitTest::logText2()
179{
180 QCA_logTextMessage(QStringLiteral("Sending with no recipients"), QCA::Logger::Notice);
181
182 LastLogger *lastlogger = new LastLogger;
183
184 QCA::Logger *logSystem = QCA::logger();
185 logSystem->registerLogDevice(logger: lastlogger);
186 QCOMPARE(logSystem->currentLogDevices().count(), 1);
187 QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
188
189 QCA_logTextMessage(QStringLiteral("Sending to system, checking for log device"), QCA::Logger::Information);
190 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for log device"));
191 QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
192
193 QCA_logTextMessage(QStringLiteral("Sending at Error severity"), QCA::Logger::Error);
194 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending at Error severity"));
195 QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Error);
196
197 LastLogger *lastlogger2 = new LastLogger;
198 logSystem->registerLogDevice(logger: lastlogger2);
199 QCOMPARE(logSystem->currentLogDevices().count(), 2);
200 QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
201
202 QCA_logTextMessage(QStringLiteral("Sending to system, checking for two log devices"), QCA::Logger::Information);
203 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
204 QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
205 QCOMPARE(lastlogger2->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
206 QCOMPARE(lastlogger2->lastMessageSeverity(), QCA::Logger::Information);
207
208 logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both
209
210 QCOMPARE(logSystem->currentLogDevices().count(), 0);
211
212 delete lastlogger;
213 delete lastlogger2;
214}
215
216void LoggerUnitTest::logBlob()
217{
218 QCA::Logger *logSystem = QCA::logger();
219
220 QCOMPARE(logSystem->currentLogDevices().count(), 0);
221
222 QByteArray test("abcd\x34");
223 logSystem->logBinaryMessage(blob: test);
224
225 LastLogger *lastlogger = new LastLogger;
226 logSystem->registerLogDevice(logger: lastlogger);
227 QCOMPARE(logSystem->currentLogDevices().count(), 1);
228 QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
229
230 logSystem->logBinaryMessage(blob: test);
231 QCOMPARE(lastlogger->lastBlob(), test);
232 QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Information);
233
234 logSystem->logBinaryMessage(blob: test, QCA::Logger::Critical);
235 QCOMPARE(lastlogger->lastBlob(), test);
236 QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Critical);
237
238 LastLogger *lastlogger2 = new LastLogger;
239 logSystem->registerLogDevice(logger: lastlogger2);
240 QCOMPARE(logSystem->currentLogDevices().count(), 2);
241 QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));
242
243 test += test;
244 logSystem->logBinaryMessage(blob: test);
245 QCOMPARE(lastlogger->lastBlob(), test);
246 QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Information);
247 QCOMPARE(lastlogger2->lastBlob(), test);
248 QCOMPARE(lastlogger2->lastBlobSeverity(), QCA::Logger::Information);
249
250 logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both
251
252 QCOMPARE(logSystem->currentLogDevices().count(), 0);
253 delete lastlogger;
254 delete lastlogger2;
255}
256
257void LoggerUnitTest::logLevel()
258{
259 QCA::Logger *logSystem = QCA::logger();
260
261 LastLogger *lastlogger = new LastLogger;
262 logSystem->registerLogDevice(logger: lastlogger);
263
264 logSystem->setLevel(QCA::Logger::Error);
265 QCOMPARE(logSystem->level(), QCA::Logger::Error);
266
267 QCA_logTextMessage(QStringLiteral("Sending to system, checking that it is filtered out"), QCA::Logger::Information);
268 QEXPECT_FAIL("", "Should fail", Continue);
269 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking that it is filtered out"));
270
271 QCA_logTextMessage(QStringLiteral("Sending to system, checking that it is not filtered out"), QCA::Logger::Error);
272 QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking that it is not filtered out"));
273
274 logSystem->setLevel(QCA::Logger::Debug);
275
276 delete lastlogger;
277}
278
279QTEST_MAIN(LoggerUnitTest)
280
281#include "loggerunittest.moc"
282

source code of qca/unittest/logger/loggerunittest.cpp