File: loggerunittest.cpp

package info (click to toggle)
qca2 2.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,672 kB
  • sloc: cpp: 58,980; ansic: 832; perl: 133; sh: 79; makefile: 22
file content (281 lines) | stat: -rw-r--r-- 9,867 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
 * Copyright (C)  2007  Brad Hards <bradh@frogmouth.net>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <QtCrypto>
#include <QtTest/QtTest>

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

class LoggerUnitTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void basicSetup();
    void logText1();
    void logText2();
    void logBlob();
    void logLevel();

private:
    QCA::Initializer *m_init;
};

class NullLogger : public QCA::AbstractLogDevice
{
    Q_OBJECT
public:
    NullLogger()
        : QCA::AbstractLogDevice(QStringLiteral("null logger"))
    {
    }
};

class LastLogger : public QCA::AbstractLogDevice
{
    Q_OBJECT
public:
    LastLogger()
        : QCA::AbstractLogDevice(QStringLiteral("last logger"))
    {
    }

    void logTextMessage(const QString &message, enum QCA::Logger::Severity severity) override
    {
        m_lastMessage     = message;
        m_messageSeverity = severity;
    }

    QString lastMessage() const
    {
        return m_lastMessage;
    }

    void logBinaryMessage(const QByteArray &blob, enum QCA::Logger::Severity severity) override
    {
        m_lastBlob     = blob;
        m_blobSeverity = severity;
    }

    QByteArray lastBlob() const
    {
        return m_lastBlob;
    }

    QCA::Logger::Severity lastMessageSeverity() const
    {
        return m_messageSeverity;
    }

    QCA::Logger::Severity lastBlobSeverity() const
    {
        return m_blobSeverity;
    }

private:
    QString               m_lastMessage;
    QByteArray            m_lastBlob;
    QCA::Logger::Severity m_messageSeverity;
    QCA::Logger::Severity m_blobSeverity;
};

void LoggerUnitTest::initTestCase()
{
    m_init = new QCA::Initializer;
}

void LoggerUnitTest::cleanupTestCase()
{
    QCA::unloadAllPlugins();
    delete m_init;
}

void LoggerUnitTest::basicSetup()
{
    QCA::Logger *logSystem = QCA::logger();

    QCOMPARE(logSystem->currentLogDevices().count(), 0);

    logSystem->setLevel(QCA::Logger::Debug);
    QCOMPARE(logSystem->level(), QCA::Logger::Debug);

    NullLogger *nullLogger = new NullLogger;

    logSystem->registerLogDevice(nullLogger);
    QCOMPARE(logSystem->currentLogDevices().count(), 1);
    QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("null logger")));
    logSystem->unregisterLogDevice(QStringLiteral("null logger"));
    QCOMPARE(logSystem->currentLogDevices().count(), 0);

    delete nullLogger;
}

void LoggerUnitTest::logText1()
{
    QCA::Logger *logSystem = QCA::logger();

    logSystem->logTextMessage(QStringLiteral("Sending with no recipients"));

    LastLogger *lastlogger = new LastLogger;
    logSystem->registerLogDevice(lastlogger);
    QCOMPARE(logSystem->currentLogDevices().count(), 1);
    QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));

    logSystem->logTextMessage(QStringLiteral("Sending to system, checking for log device"));
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for log device"));
    QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);

    logSystem->logTextMessage(QStringLiteral("Sending at Error severity"), QCA::Logger::Error);
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending at Error severity"));
    QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Error);

    LastLogger *lastlogger2 = new LastLogger;
    logSystem->registerLogDevice(lastlogger2);
    QCOMPARE(logSystem->currentLogDevices().count(), 2);
    QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));

    logSystem->logTextMessage(QStringLiteral("Sending to system, checking for two log devices"));
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
    QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
    QCOMPARE(lastlogger2->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
    QCOMPARE(lastlogger2->lastMessageSeverity(), QCA::Logger::Information);

    logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both

    QCOMPARE(logSystem->currentLogDevices().count(), 0);

    delete lastlogger;
    delete lastlogger2;
}

// same as above, but use convenience routine.
void LoggerUnitTest::logText2()
{
    QCA_logTextMessage(QStringLiteral("Sending with no recipients"), QCA::Logger::Notice);

    LastLogger *lastlogger = new LastLogger;

    QCA::Logger *logSystem = QCA::logger();
    logSystem->registerLogDevice(lastlogger);
    QCOMPARE(logSystem->currentLogDevices().count(), 1);
    QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));

    QCA_logTextMessage(QStringLiteral("Sending to system, checking for log device"), QCA::Logger::Information);
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for log device"));
    QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);

    QCA_logTextMessage(QStringLiteral("Sending at Error severity"), QCA::Logger::Error);
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending at Error severity"));
    QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Error);

    LastLogger *lastlogger2 = new LastLogger;
    logSystem->registerLogDevice(lastlogger2);
    QCOMPARE(logSystem->currentLogDevices().count(), 2);
    QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));

    QCA_logTextMessage(QStringLiteral("Sending to system, checking for two log devices"), QCA::Logger::Information);
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
    QCOMPARE(lastlogger->lastMessageSeverity(), QCA::Logger::Information);
    QCOMPARE(lastlogger2->lastMessage(), QStringLiteral("Sending to system, checking for two log devices"));
    QCOMPARE(lastlogger2->lastMessageSeverity(), QCA::Logger::Information);

    logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both

    QCOMPARE(logSystem->currentLogDevices().count(), 0);

    delete lastlogger;
    delete lastlogger2;
}

void LoggerUnitTest::logBlob()
{
    QCA::Logger *logSystem = QCA::logger();

    QCOMPARE(logSystem->currentLogDevices().count(), 0);

    QByteArray test("abcd\x34");
    logSystem->logBinaryMessage(test);

    LastLogger *lastlogger = new LastLogger;
    logSystem->registerLogDevice(lastlogger);
    QCOMPARE(logSystem->currentLogDevices().count(), 1);
    QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));

    logSystem->logBinaryMessage(test);
    QCOMPARE(lastlogger->lastBlob(), test);
    QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Information);

    logSystem->logBinaryMessage(test, QCA::Logger::Critical);
    QCOMPARE(lastlogger->lastBlob(), test);
    QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Critical);

    LastLogger *lastlogger2 = new LastLogger;
    logSystem->registerLogDevice(lastlogger2);
    QCOMPARE(logSystem->currentLogDevices().count(), 2);
    QVERIFY(logSystem->currentLogDevices().contains(QStringLiteral("last logger")));

    test += test;
    logSystem->logBinaryMessage(test);
    QCOMPARE(lastlogger->lastBlob(), test);
    QCOMPARE(lastlogger->lastBlobSeverity(), QCA::Logger::Information);
    QCOMPARE(lastlogger2->lastBlob(), test);
    QCOMPARE(lastlogger2->lastBlobSeverity(), QCA::Logger::Information);

    logSystem->unregisterLogDevice(QStringLiteral("last logger")); // this will remove them both

    QCOMPARE(logSystem->currentLogDevices().count(), 0);
    delete lastlogger;
    delete lastlogger2;
}

void LoggerUnitTest::logLevel()
{
    QCA::Logger *logSystem = QCA::logger();

    LastLogger *lastlogger = new LastLogger;
    logSystem->registerLogDevice(lastlogger);

    logSystem->setLevel(QCA::Logger::Error);
    QCOMPARE(logSystem->level(), QCA::Logger::Error);

    QCA_logTextMessage(QStringLiteral("Sending to system, checking that it is filtered out"), QCA::Logger::Information);
    QEXPECT_FAIL("", "Should fail", Continue);
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking that it is filtered out"));

    QCA_logTextMessage(QStringLiteral("Sending to system, checking that it is not filtered out"), QCA::Logger::Error);
    QCOMPARE(lastlogger->lastMessage(), QStringLiteral("Sending to system, checking that it is not filtered out"));

    logSystem->setLevel(QCA::Logger::Debug);

    delete lastlogger;
}

QTEST_MAIN(LoggerUnitTest)

#include "loggerunittest.moc"