mirror of
https://github.com/tnodir/fort
synced 2024-11-15 01:25:52 +00:00
Add test for LogBuffer.
This commit is contained in:
parent
84f17a9aca
commit
9dc617efe6
@ -1,4 +1,4 @@
|
||||
TEMPLATE = subdirs
|
||||
|
||||
SUBDIRS = \
|
||||
fortmanager
|
||||
logbuffer
|
||||
|
@ -1,7 +0,0 @@
|
||||
include(../common/Test.pri)
|
||||
|
||||
SOURCES += \
|
||||
$$UIPATH/fortmanager.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$UIPATH/fortmanager.h
|
@ -1,10 +0,0 @@
|
||||
#include "test.h"
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include "fortmanager.h"
|
||||
|
||||
void Test::dummy()
|
||||
{
|
||||
QVERIFY(false);
|
||||
}
|
11
src/tests/logbuffer/LogBuffer.pro
Normal file
11
src/tests/logbuffer/LogBuffer.pro
Normal file
@ -0,0 +1,11 @@
|
||||
include(../common/Test.pri)
|
||||
|
||||
SOURCES += \
|
||||
$$UIPATH/firewallLog/logbuffer.cpp \
|
||||
$$UIPATH/firewallLog/logentry.cpp \
|
||||
$$UIPATH/fortcommon.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$UIPATH/firewallLog/logbuffer.h \
|
||||
$$UIPATH/firewallLog/logentry.h \
|
||||
$$UIPATH/fortcommon.h
|
39
src/tests/logbuffer/test.cpp
Normal file
39
src/tests/logbuffer/test.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "test.h"
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include "firewallLog/logbuffer.h"
|
||||
#include "firewallLog/logentry.h"
|
||||
#include "fortcommon.h"
|
||||
|
||||
void Test::logWriteRead()
|
||||
{
|
||||
const QString path("C:\\test\\");
|
||||
|
||||
const int pathSize = path.size();
|
||||
QCOMPARE(pathSize, 8);
|
||||
|
||||
const int entrySize = FortCommon::logHeaderSize()
|
||||
+ pathSize * sizeof(wchar_t);
|
||||
|
||||
LogBuffer buf(entrySize);
|
||||
|
||||
const quint32 ip = 1, pid = 2;
|
||||
LogEntry entry(ip, pid, path);
|
||||
|
||||
const int testCount = 3;
|
||||
|
||||
// Write
|
||||
for (int n = testCount; --n >= 0; ) {
|
||||
QCOMPARE(buf.write(entry), entrySize);
|
||||
}
|
||||
|
||||
// Read
|
||||
for (int n = testCount; --n >= 0; ) {
|
||||
QCOMPARE(buf.read(entry), entrySize);
|
||||
QCOMPARE(entry.ip(), ip);
|
||||
QCOMPARE(entry.pid(), pid);
|
||||
QCOMPARE(entry.path(), path);
|
||||
}
|
||||
QCOMPARE(buf.read(entry), 0);
|
||||
}
|
@ -8,7 +8,7 @@ class Test : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void dummy();
|
||||
void logWriteRead();
|
||||
};
|
||||
|
||||
#endif // TEST_H
|
@ -9,14 +9,14 @@ TEMPLATE = app
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
fortcommon.cpp \
|
||||
firewallLog/logbuffer.cpp \
|
||||
firewallLog/logentry.cpp
|
||||
firewallLog/logentry.cpp \
|
||||
fortcommon.cpp
|
||||
|
||||
HEADERS += \
|
||||
fortcommon.h \
|
||||
firewallLog/logbuffer.h \
|
||||
firewallLog/logentry.h
|
||||
firewallLog/logentry.h \
|
||||
fortcommon.h
|
||||
|
||||
QML_FILES += \
|
||||
qml/*.qml
|
||||
|
@ -3,11 +3,11 @@
|
||||
#include "fortcommon.h"
|
||||
#include "logentry.h"
|
||||
|
||||
LogBuffer::LogBuffer(QObject *parent) :
|
||||
LogBuffer::LogBuffer(int bufferSize, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_top(0),
|
||||
m_offset(0),
|
||||
m_array(0, Qt::Uninitialized)
|
||||
m_array(bufferSize, Qt::Uninitialized)
|
||||
{
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ void LogBuffer::prepareFor(int len)
|
||||
}
|
||||
}
|
||||
|
||||
void LogBuffer::write(const LogEntry &logEntry)
|
||||
int LogBuffer::write(const LogEntry &logEntry)
|
||||
{
|
||||
const QString path = logEntry.path();
|
||||
const int pathLen = path.size() * sizeof(wchar_t);
|
||||
@ -38,12 +38,14 @@ void LogBuffer::write(const LogEntry &logEntry)
|
||||
}
|
||||
|
||||
m_top += entrySize;
|
||||
|
||||
return entrySize;
|
||||
}
|
||||
|
||||
bool LogBuffer::read(LogEntry &logEntry)
|
||||
int LogBuffer::read(LogEntry &logEntry)
|
||||
{
|
||||
if (m_offset >= m_top)
|
||||
return false;
|
||||
return 0;
|
||||
|
||||
const char *input = m_array.constData() + m_offset;
|
||||
|
||||
@ -64,5 +66,5 @@ bool LogBuffer::read(LogEntry &logEntry)
|
||||
const int entrySize = FortCommon::logSize(pathLen);
|
||||
m_offset += entrySize;
|
||||
|
||||
return true;
|
||||
return entrySize;
|
||||
}
|
||||
|
@ -11,13 +11,14 @@ class LogBuffer : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LogBuffer(QObject *parent = nullptr);
|
||||
explicit LogBuffer(int bufferSize = 0,
|
||||
QObject *parent = nullptr);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
void write(const LogEntry &logEntry);
|
||||
bool read(LogEntry &logEntry);
|
||||
int write(const LogEntry &logEntry);
|
||||
int read(LogEntry &logEntry);
|
||||
|
||||
private:
|
||||
void prepareFor(int len);
|
||||
|
@ -1,7 +1,12 @@
|
||||
#include "logentry.h"
|
||||
|
||||
LogEntry::LogEntry(QObject *parent) :
|
||||
QObject(parent)
|
||||
LogEntry::LogEntry(quint32 ip, quint32 pid,
|
||||
const QString &path,
|
||||
QObject *parent) :
|
||||
QObject(parent),
|
||||
m_ip(ip),
|
||||
m_pid(pid),
|
||||
m_path(path)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,9 @@ class LogEntry : public QObject
|
||||
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
|
||||
|
||||
public:
|
||||
explicit LogEntry(QObject *parent = nullptr);
|
||||
explicit LogEntry(quint32 ip = 0, quint32 pid = 0,
|
||||
const QString &path = QString(),
|
||||
QObject *parent = nullptr);
|
||||
|
||||
quint32 ip() const { return m_ip; }
|
||||
void setIp(quint32 ip);
|
||||
|
Loading…
Reference in New Issue
Block a user